matc/clusters/codec/
power_topology.rs

1//! Generated 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// Attribute decoders
12
13/// Decode AvailableEndpoints attribute (0x0000)
14pub 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
26/// Decode ActiveEndpoints attribute (0x0001)
27pub 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
40// JSON dispatcher function
41
42/// Decode attribute value and return as JSON string
43/// 
44/// # Parameters
45/// * `cluster_id` - The cluster identifier
46/// * `attribute_id` - The attribute identifier
47/// * `tlv_value` - The TLV value to decode
48/// 
49/// # Returns
50/// JSON string representation of the decoded value or error
51pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
52    // Verify this is the correct cluster
53    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
74/// Get list of all attributes supported by this cluster
75/// 
76/// # Returns
77/// Vector of tuples containing (attribute_id, attribute_name)
78pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
79    vec![
80        (0x0000, "AvailableEndpoints"),
81        (0x0001, "ActiveEndpoints"),
82    ]
83}
84