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