matc/clusters/codec/
power_topology.rs1use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11pub fn decode_available_endpoints(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<u16>> {
15 let mut res = Vec::new();
16 if let tlv::TlvItemValue::List(v) = inp {
17 for item in v {
18 if let tlv::TlvItemValue::Int(i) = &item.value {
19 res.push(*i as u16);
20 }
21 }
22 }
23 Ok(res)
24}
25
26pub fn decode_active_endpoints(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<u16>> {
28 let mut res = Vec::new();
29 if let tlv::TlvItemValue::List(v) = inp {
30 for item in v {
31 if let tlv::TlvItemValue::Int(i) = &item.value {
32 res.push(*i as u16);
33 }
34 }
35 }
36 Ok(res)
37}
38
39
40pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
52 if cluster_id != 0x009C {
54 return format!("{{\"error\": \"Invalid cluster ID. Expected 0x009C, got {}\"}}", cluster_id);
55 }
56
57 match attribute_id {
58 0x0000 => {
59 match decode_available_endpoints(tlv_value) {
60 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
61 Err(e) => format!("{{\"error\": \"{}\"}}", e),
62 }
63 }
64 0x0001 => {
65 match decode_active_endpoints(tlv_value) {
66 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
67 Err(e) => format!("{{\"error\": \"{}\"}}", e),
68 }
69 }
70 _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
71 }
72}
73
74pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
79 vec![
80 (0x0000, "AvailableEndpoints"),
81 (0x0001, "ActiveEndpoints"),
82 ]
83}
84