matc/clusters/codec/
power_source_configuration_cluster.rs

1//! Matter TLV encoders and decoders for Power Source Configuration Cluster
2//! Cluster ID: 0x002E
3//!
4//! This file is automatically generated from PowerSourceConfigurationCluster.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 Sources attribute (0x0000)
16pub fn decode_sources(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
29// JSON dispatcher function
30
31/// Decode attribute value and return as JSON string
32///
33/// # Parameters
34/// * `cluster_id` - The cluster identifier
35/// * `attribute_id` - The attribute identifier
36/// * `tlv_value` - The TLV value to decode
37///
38/// # Returns
39/// JSON string representation of the decoded value or error
40pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
41    // Verify this is the correct cluster
42    if cluster_id != 0x002E {
43        return format!("{{\"error\": \"Invalid cluster ID. Expected 0x002E, got {}\"}}", cluster_id);
44    }
45
46    match attribute_id {
47        0x0000 => {
48            match decode_sources(tlv_value) {
49                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
50                Err(e) => format!("{{\"error\": \"{}\"}}", e),
51            }
52        }
53        _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
54    }
55}
56
57/// Get list of all attributes supported by this cluster
58///
59/// # Returns
60/// Vector of tuples containing (attribute_id, attribute_name)
61pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
62    vec![
63        (0x0000, "Sources"),
64    ]
65}
66
67// Typed facade (invokes + reads)
68
69/// Read `Sources` attribute from cluster `Power Source Configuration`.
70pub async fn read_sources(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Vec<u16>> {
71    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_POWER_SOURCE_CONFIGURATION, crate::clusters::defs::CLUSTER_POWER_SOURCE_CONFIGURATION_ATTR_ID_SOURCES).await?;
72    decode_sources(&tlv)
73}
74