matc/clusters/codec/
localization_configuration.rs

1//! Matter TLV encoders and decoders for Localization Configuration Cluster
2//! Cluster ID: 0x002B
3//!
4//! This file is automatically generated from LocalizationConfiguration.xml
5
6#![allow(clippy::too_many_arguments)]
7
8use crate::tlv;
9use anyhow;
10use serde_json;
11
12
13// Attribute decoders
14
15/// Decode ActiveLocale attribute (0x0000)
16pub fn decode_active_locale(inp: &tlv::TlvItemValue) -> anyhow::Result<String> {
17    if let tlv::TlvItemValue::String(v) = inp {
18        Ok(v.clone())
19    } else {
20        Err(anyhow::anyhow!("Expected String"))
21    }
22}
23
24/// Decode SupportedLocales attribute (0x0001)
25pub fn decode_supported_locales(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<String>> {
26    let mut res = Vec::new();
27    if let tlv::TlvItemValue::List(v) = inp {
28        for item in v {
29            if let tlv::TlvItemValue::String(s) = &item.value {
30                res.push(s.clone());
31            }
32        }
33    }
34    Ok(res)
35}
36
37
38// JSON dispatcher function
39
40/// Decode attribute value and return as JSON string
41///
42/// # Parameters
43/// * `cluster_id` - The cluster identifier
44/// * `attribute_id` - The attribute identifier
45/// * `tlv_value` - The TLV value to decode
46///
47/// # Returns
48/// JSON string representation of the decoded value or error
49pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
50    // Verify this is the correct cluster
51    if cluster_id != 0x002B {
52        return format!("{{\"error\": \"Invalid cluster ID. Expected 0x002B, got {}\"}}", cluster_id);
53    }
54
55    match attribute_id {
56        0x0000 => {
57            match decode_active_locale(tlv_value) {
58                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
59                Err(e) => format!("{{\"error\": \"{}\"}}", e),
60            }
61        }
62        0x0001 => {
63            match decode_supported_locales(tlv_value) {
64                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
65                Err(e) => format!("{{\"error\": \"{}\"}}", e),
66            }
67        }
68        _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
69    }
70}
71
72/// Get list of all attributes supported by this cluster
73///
74/// # Returns
75/// Vector of tuples containing (attribute_id, attribute_name)
76pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
77    vec![
78        (0x0000, "ActiveLocale"),
79        (0x0001, "SupportedLocales"),
80    ]
81}
82
83// Typed facade (invokes + reads)
84
85/// Read `ActiveLocale` attribute from cluster `Localization Configuration`.
86pub async fn read_active_locale(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<String> {
87    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_LOCALIZATION_CONFIGURATION, crate::clusters::defs::CLUSTER_LOCALIZATION_CONFIGURATION_ATTR_ID_ACTIVELOCALE).await?;
88    decode_active_locale(&tlv)
89}
90
91/// Read `SupportedLocales` attribute from cluster `Localization Configuration`.
92pub async fn read_supported_locales(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Vec<String>> {
93    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_LOCALIZATION_CONFIGURATION, crate::clusters::defs::CLUSTER_LOCALIZATION_CONFIGURATION_ATTR_ID_SUPPORTEDLOCALES).await?;
94    decode_supported_locales(&tlv)
95}
96