matc/clusters/codec/
power_topology.rs

1//! Matter TLV encoders and decoders for Power Topology Cluster
2//! Cluster ID: 0x009C
3//!
4//! This file is automatically generated from PowerTopology.xml
5
6use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11// Struct definitions
12
13#[derive(Debug, serde::Serialize)]
14pub struct CircuitNode {
15    pub node: Option<u64>,
16    pub endpoint: Option<u16>,
17    pub label: Option<String>,
18}
19
20// Attribute decoders
21
22/// Decode AvailableEndpoints attribute (0x0000)
23pub 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
35/// Decode ActiveEndpoints attribute (0x0001)
36pub 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
49// JSON dispatcher function
50
51/// Decode attribute value and return as JSON string
52///
53/// # Parameters
54/// * `cluster_id` - The cluster identifier
55/// * `attribute_id` - The attribute identifier
56/// * `tlv_value` - The TLV value to decode
57///
58/// # Returns
59/// JSON string representation of the decoded value or error
60pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
61    // Verify this is the correct cluster
62    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
83/// Get list of all attributes supported by this cluster
84///
85/// # Returns
86/// Vector of tuples containing (attribute_id, attribute_name)
87pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
88    vec![
89        (0x0000, "AvailableEndpoints"),
90        (0x0001, "ActiveEndpoints"),
91    ]
92}
93