Skip to main content

matc/clusters/codec/
power_topology.rs

1//! Matter TLV encoders and decoders for Power Topology Cluster
2//! Cluster IDs: 0x009C
3//!
4//! This file is automatically generated from PowerTopology.xml
5
6#![allow(clippy::too_many_arguments)]
7
8use crate::tlv;
9use anyhow;
10use serde_json;
11
12
13// Attribute decoders
14
15/// Decode AvailableEndpoints attribute (0x0000)
16pub fn decode_available_endpoints(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<u16>> {
17    let mut res = Vec::new();
18    if let tlv::TlvItemValue::List(v) = inp {
19        for item in v {
20            if let tlv::TlvItemValue::Int(i) = &item.value {
21                res.push(*i as u16);
22            }
23        }
24    }
25    Ok(res)
26}
27
28/// Decode ActiveEndpoints attribute (0x0001)
29pub fn decode_active_endpoints(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<u16>> {
30    let mut res = Vec::new();
31    if let tlv::TlvItemValue::List(v) = inp {
32        for item in v {
33            if let tlv::TlvItemValue::Int(i) = &item.value {
34                res.push(*i as u16);
35            }
36        }
37    }
38    Ok(res)
39}
40
41
42// JSON dispatcher function
43
44/// Decode attribute value and return as JSON string
45///
46/// # Parameters
47/// * `cluster_id` - The cluster identifier
48/// * `attribute_id` - The attribute identifier
49/// * `tlv_value` - The TLV value to decode
50///
51/// # Returns
52/// JSON string representation of the decoded value or error
53pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
54    // Verify this is the correct cluster
55    if ![0x009C].contains(&cluster_id) {
56        return format!("{{\"error\": \"Invalid cluster ID. Expected [0x009C], got {}\"}}", cluster_id);
57    }
58
59    match attribute_id {
60        0x0000 => {
61            match decode_available_endpoints(tlv_value) {
62                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
63                Err(e) => format!("{{\"error\": \"{}\"}}", e),
64            }
65        }
66        0x0001 => {
67            match decode_active_endpoints(tlv_value) {
68                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
69                Err(e) => format!("{{\"error\": \"{}\"}}", e),
70            }
71        }
72        _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
73    }
74}
75
76/// Get list of all attributes supported by this cluster
77///
78/// # Returns
79/// Vector of tuples containing (attribute_id, attribute_name)
80pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
81    vec![
82        (0x0000, "AvailableEndpoints"),
83        (0x0001, "ActiveEndpoints"),
84    ]
85}
86
87// Typed facade (invokes + reads)
88
89/// Read `AvailableEndpoints` attribute from cluster `Power Topology`.
90pub async fn read_available_endpoints(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Vec<u16>> {
91    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_POWER_TOPOLOGY, crate::clusters::defs::CLUSTER_POWER_TOPOLOGY_ATTR_ID_AVAILABLEENDPOINTS).await?;
92    decode_available_endpoints(&tlv)
93}
94
95/// Read `ActiveEndpoints` attribute from cluster `Power Topology`.
96pub async fn read_active_endpoints(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Vec<u16>> {
97    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_POWER_TOPOLOGY, crate::clusters::defs::CLUSTER_POWER_TOPOLOGY_ATTR_ID_ACTIVEENDPOINTS).await?;
98    decode_active_endpoints(&tlv)
99}
100