matc/clusters/codec/
descriptor_cluster.rs1#![allow(clippy::too_many_arguments)]
7
8use crate::tlv;
9use anyhow;
10use serde_json;
11
12
13#[derive(Debug, serde::Serialize)]
16pub struct DeviceType {
17 pub device_type: Option<u32>,
18 pub revision: Option<u16>,
19}
20
21pub 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
37pub 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
50pub 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
63pub 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
76pub 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
89pub 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
99pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
111 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
157pub 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
172pub 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
180pub 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
186pub 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
192pub 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
198pub 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
204pub 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