matc/clusters/codec/
descriptor_cluster.rs

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