matc/clusters/codec/
descriptor_cluster.rs

1//! Matter TLV encoders and decoders for Descriptor Cluster
2//! Cluster ID: 0x001D
3//!
4//! This file is automatically generated from Descriptor-Cluster.xml
5
6#![allow(clippy::too_many_arguments)]
7
8use crate::tlv;
9use anyhow;
10use serde_json;
11
12
13// Struct definitions
14
15#[derive(Debug, serde::Serialize)]
16pub struct DeviceType {
17    pub device_type: Option<u32>,
18    pub revision: Option<u16>,
19}
20
21// Attribute decoders
22
23/// Decode DeviceTypeList attribute (0x0000)
24pub fn decode_device_type_list(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<DeviceType>> {
25    let mut res = Vec::new();
26    if let tlv::TlvItemValue::List(v) = inp {
27        for item in v {
28            res.push(DeviceType {
29                device_type: item.get_int(&[0]).map(|v| v as u32),
30                revision: item.get_int(&[1]).map(|v| v as u16),
31            });
32        }
33    }
34    Ok(res)
35}
36
37/// Decode ServerList attribute (0x0001)
38pub fn decode_server_list(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<u32>> {
39    let mut res = Vec::new();
40    if let tlv::TlvItemValue::List(v) = inp {
41        for item in v {
42            if let tlv::TlvItemValue::Int(i) = &item.value {
43                res.push(*i as u32);
44            }
45        }
46    }
47    Ok(res)
48}
49
50/// Decode ClientList attribute (0x0002)
51pub fn decode_client_list(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<u32>> {
52    let mut res = Vec::new();
53    if let tlv::TlvItemValue::List(v) = inp {
54        for item in v {
55            if let tlv::TlvItemValue::Int(i) = &item.value {
56                res.push(*i as u32);
57            }
58        }
59    }
60    Ok(res)
61}
62
63/// Decode PartsList attribute (0x0003)
64pub fn decode_parts_list(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<u16>> {
65    let mut res = Vec::new();
66    if let tlv::TlvItemValue::List(v) = inp {
67        for item in v {
68            if let tlv::TlvItemValue::Int(i) = &item.value {
69                res.push(*i as u16);
70            }
71        }
72    }
73    Ok(res)
74}
75
76/// Decode TagList attribute (0x0004)
77pub fn decode_tag_list(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<u8>> {
78    let mut res = Vec::new();
79    if let tlv::TlvItemValue::List(v) = inp {
80        for item in v {
81            if let tlv::TlvItemValue::Int(i) = &item.value {
82                res.push(*i as u8);
83            }
84        }
85    }
86    Ok(res)
87}
88
89/// Decode EndpointUniqueID attribute (0x0005)
90pub fn decode_endpoint_unique_id(inp: &tlv::TlvItemValue) -> anyhow::Result<String> {
91    if let tlv::TlvItemValue::String(v) = inp {
92        Ok(v.clone())
93    } else {
94        Err(anyhow::anyhow!("Expected String"))
95    }
96}
97
98
99// JSON dispatcher function
100
101/// Decode attribute value and return as JSON string
102///
103/// # Parameters
104/// * `cluster_id` - The cluster identifier
105/// * `attribute_id` - The attribute identifier
106/// * `tlv_value` - The TLV value to decode
107///
108/// # Returns
109/// JSON string representation of the decoded value or error
110pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
111    // Verify this is the correct cluster
112    if cluster_id != 0x001D {
113        return format!("{{\"error\": \"Invalid cluster ID. Expected 0x001D, got {}\"}}", cluster_id);
114    }
115
116    match attribute_id {
117        0x0000 => {
118            match decode_device_type_list(tlv_value) {
119                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
120                Err(e) => format!("{{\"error\": \"{}\"}}", e),
121            }
122        }
123        0x0001 => {
124            match decode_server_list(tlv_value) {
125                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
126                Err(e) => format!("{{\"error\": \"{}\"}}", e),
127            }
128        }
129        0x0002 => {
130            match decode_client_list(tlv_value) {
131                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
132                Err(e) => format!("{{\"error\": \"{}\"}}", e),
133            }
134        }
135        0x0003 => {
136            match decode_parts_list(tlv_value) {
137                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
138                Err(e) => format!("{{\"error\": \"{}\"}}", e),
139            }
140        }
141        0x0004 => {
142            match decode_tag_list(tlv_value) {
143                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
144                Err(e) => format!("{{\"error\": \"{}\"}}", e),
145            }
146        }
147        0x0005 => {
148            match decode_endpoint_unique_id(tlv_value) {
149                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
150                Err(e) => format!("{{\"error\": \"{}\"}}", e),
151            }
152        }
153        _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
154    }
155}
156
157/// Get list of all attributes supported by this cluster
158///
159/// # Returns
160/// Vector of tuples containing (attribute_id, attribute_name)
161pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
162    vec![
163        (0x0000, "DeviceTypeList"),
164        (0x0001, "ServerList"),
165        (0x0002, "ClientList"),
166        (0x0003, "PartsList"),
167        (0x0004, "TagList"),
168        (0x0005, "EndpointUniqueID"),
169    ]
170}
171
172// Typed facade (invokes + reads)
173
174/// Read `DeviceTypeList` attribute from cluster `Descriptor`.
175pub async fn read_device_type_list(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Vec<DeviceType>> {
176    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_DESCRIPTOR, crate::clusters::defs::CLUSTER_DESCRIPTOR_ATTR_ID_DEVICETYPELIST).await?;
177    decode_device_type_list(&tlv)
178}
179
180/// Read `ServerList` attribute from cluster `Descriptor`.
181pub async fn read_server_list(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Vec<u32>> {
182    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_DESCRIPTOR, crate::clusters::defs::CLUSTER_DESCRIPTOR_ATTR_ID_SERVERLIST).await?;
183    decode_server_list(&tlv)
184}
185
186/// Read `ClientList` attribute from cluster `Descriptor`.
187pub async fn read_client_list(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Vec<u32>> {
188    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_DESCRIPTOR, crate::clusters::defs::CLUSTER_DESCRIPTOR_ATTR_ID_CLIENTLIST).await?;
189    decode_client_list(&tlv)
190}
191
192/// Read `PartsList` attribute from cluster `Descriptor`.
193pub async fn read_parts_list(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Vec<u16>> {
194    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_DESCRIPTOR, crate::clusters::defs::CLUSTER_DESCRIPTOR_ATTR_ID_PARTSLIST).await?;
195    decode_parts_list(&tlv)
196}
197
198/// Read `TagList` attribute from cluster `Descriptor`.
199pub async fn read_tag_list(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Vec<u8>> {
200    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_DESCRIPTOR, crate::clusters::defs::CLUSTER_DESCRIPTOR_ATTR_ID_TAGLIST).await?;
201    decode_tag_list(&tlv)
202}
203
204/// Read `EndpointUniqueID` attribute from cluster `Descriptor`.
205pub async fn read_endpoint_unique_id(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<String> {
206    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_DESCRIPTOR, crate::clusters::defs::CLUSTER_DESCRIPTOR_ATTR_ID_ENDPOINTUNIQUEID).await?;
207    decode_endpoint_unique_id(&tlv)
208}
209