matc/clusters/codec/
descriptor_cluster.rs1use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11#[derive(Debug, serde::Serialize)]
14pub struct DeviceType {
15 pub device_type: Option<u32>,
16 pub revision: Option<u16>,
17}
18
19pub 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
35pub 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
48pub 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
61pub 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
74pub 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
87pub 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
97pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
109 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
155pub 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