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