matc/clusters/codec/
power_topology.rs1use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11#[derive(Debug, serde::Serialize)]
14pub struct CircuitNode {
15 pub node: Option<u64>,
16 pub endpoint: Option<u16>,
17 pub label: Option<String>,
18}
19
20pub fn decode_available_endpoints(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<u16>> {
24 let mut res = Vec::new();
25 if let tlv::TlvItemValue::List(v) = inp {
26 for item in v {
27 if let tlv::TlvItemValue::Int(i) = &item.value {
28 res.push(*i as u16);
29 }
30 }
31 }
32 Ok(res)
33}
34
35pub fn decode_active_endpoints(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<u16>> {
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 u16);
42 }
43 }
44 }
45 Ok(res)
46}
47
48
49pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
61 if cluster_id != 0x009C {
63 return format!("{{\"error\": \"Invalid cluster ID. Expected 0x009C, got {}\"}}", cluster_id);
64 }
65
66 match attribute_id {
67 0x0000 => {
68 match decode_available_endpoints(tlv_value) {
69 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
70 Err(e) => format!("{{\"error\": \"{}\"}}", e),
71 }
72 }
73 0x0001 => {
74 match decode_active_endpoints(tlv_value) {
75 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
76 Err(e) => format!("{{\"error\": \"{}\"}}", e),
77 }
78 }
79 _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
80 }
81}
82
83pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
88 vec![
89 (0x0000, "AvailableEndpoints"),
90 (0x0001, "ActiveEndpoints"),
91 ]
92}
93