matc/clusters/codec/
ecosystem_information_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
19#[derive(Debug, serde::Serialize)]
20pub struct EcosystemDevice {
21 pub device_name: Option<String>,
22 pub device_name_last_edit: Option<u8>,
23 pub bridged_endpoint: Option<u16>,
24 pub original_endpoint: Option<u16>,
25 pub device_types: Option<Vec<DeviceType>>,
26 pub unique_location_i_ds: Option<Vec<String>>,
27 pub unique_location_i_ds_last_edit: Option<u8>,
28}
29
30#[derive(Debug, serde::Serialize)]
31pub struct EcosystemLocation {
32 pub unique_location_id: Option<String>,
33 pub location_descriptor: Option<LocationDescriptor>,
34 pub location_descriptor_last_edit: Option<u8>,
35}
36
37#[derive(Debug, serde::Serialize)]
38pub struct LocationDescriptor {
39 pub location_name: Option<String>,
40 pub floor_number: Option<u16>,
41 pub area_type: Option<u8>,
42}
43
44pub fn decode_device_directory(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<EcosystemDevice>> {
48 let mut res = Vec::new();
49 if let tlv::TlvItemValue::List(v) = inp {
50 for item in v {
51 res.push(EcosystemDevice {
52 device_name: item.get_string_owned(&[0]),
53 device_name_last_edit: item.get_int(&[1]).map(|v| v as u8),
54 bridged_endpoint: item.get_int(&[2]).map(|v| v as u16),
55 original_endpoint: item.get_int(&[3]).map(|v| v as u16),
56 device_types: {
57 if let Some(tlv::TlvItemValue::List(l)) = item.get(&[4]) {
58 let mut items = Vec::new();
59 for list_item in l {
60 items.push(DeviceType {
61 device_type: list_item.get_int(&[0]).map(|v| v as u32),
62 revision: list_item.get_int(&[1]).map(|v| v as u16),
63 });
64 }
65 Some(items)
66 } else {
67 None
68 }
69 },
70 unique_location_i_ds: {
71 if let Some(tlv::TlvItemValue::List(l)) = item.get(&[5]) {
72 let items: Vec<String> = l.iter().filter_map(|e| { if let tlv::TlvItemValue::String(v) = &e.value { Some(v.clone()) } else { None } }).collect();
73 Some(items)
74 } else {
75 None
76 }
77 },
78 unique_location_i_ds_last_edit: item.get_int(&[6]).map(|v| v as u8),
79 });
80 }
81 }
82 Ok(res)
83}
84
85pub fn decode_location_directory(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<EcosystemLocation>> {
87 let mut res = Vec::new();
88 if let tlv::TlvItemValue::List(v) = inp {
89 for item in v {
90 res.push(EcosystemLocation {
91 unique_location_id: item.get_string_owned(&[0]),
92 location_descriptor: {
93 if let Some(nested_tlv) = item.get(&[1]) {
94 if let tlv::TlvItemValue::List(_) = nested_tlv {
95 let nested_item = tlv::TlvItem { tag: 1, value: nested_tlv.clone() };
96 Some(LocationDescriptor {
97 location_name: nested_item.get_string_owned(&[0]),
98 floor_number: nested_item.get_int(&[1]).map(|v| v as u16),
99 area_type: nested_item.get_int(&[2]).map(|v| v as u8),
100 })
101 } else {
102 None
103 }
104 } else {
105 None
106 }
107 },
108 location_descriptor_last_edit: item.get_int(&[2]).map(|v| v as u8),
109 });
110 }
111 }
112 Ok(res)
113}
114
115
116pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
128 if cluster_id != 0x0750 {
130 return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0750, got {}\"}}", cluster_id);
131 }
132
133 match attribute_id {
134 0x0000 => {
135 match decode_device_directory(tlv_value) {
136 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
137 Err(e) => format!("{{\"error\": \"{}\"}}", e),
138 }
139 }
140 0x0001 => {
141 match decode_location_directory(tlv_value) {
142 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
143 Err(e) => format!("{{\"error\": \"{}\"}}", e),
144 }
145 }
146 _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
147 }
148}
149
150pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
155 vec![
156 (0x0000, "DeviceDirectory"),
157 (0x0001, "LocationDirectory"),
158 ]
159}
160