matc/clusters/codec/
boolean_state.rs

1//! Matter TLV encoders and decoders for Boolean State Cluster
2//! Cluster ID: 0x0045
3//!
4//! This file is automatically generated from BooleanState.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 StateValue attribute (0x0000)
16pub fn decode_state_value(inp: &tlv::TlvItemValue) -> anyhow::Result<bool> {
17    if let tlv::TlvItemValue::Bool(v) = inp {
18        Ok(*v)
19    } else {
20        Err(anyhow::anyhow!("Expected Bool"))
21    }
22}
23
24
25// JSON dispatcher function
26
27/// Decode attribute value and return as JSON string
28///
29/// # Parameters
30/// * `cluster_id` - The cluster identifier
31/// * `attribute_id` - The attribute identifier
32/// * `tlv_value` - The TLV value to decode
33///
34/// # Returns
35/// JSON string representation of the decoded value or error
36pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
37    // Verify this is the correct cluster
38    if cluster_id != 0x0045 {
39        return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0045, got {}\"}}", cluster_id);
40    }
41
42    match attribute_id {
43        0x0000 => {
44            match decode_state_value(tlv_value) {
45                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
46                Err(e) => format!("{{\"error\": \"{}\"}}", e),
47            }
48        }
49        _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
50    }
51}
52
53/// Get list of all attributes supported by this cluster
54///
55/// # Returns
56/// Vector of tuples containing (attribute_id, attribute_name)
57pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
58    vec![
59        (0x0000, "StateValue"),
60    ]
61}
62
63// Typed facade (invokes + reads)
64
65/// Read `StateValue` attribute from cluster `Boolean State`.
66pub async fn read_state_value(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<bool> {
67    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_BOOLEAN_STATE, crate::clusters::defs::CLUSTER_BOOLEAN_STATE_ATTR_ID_STATEVALUE).await?;
68    decode_state_value(&tlv)
69}
70
71#[derive(Debug, serde::Serialize)]
72pub struct StateChangeEvent {
73    pub state_value: Option<bool>,
74}
75
76// Event decoders
77
78/// Decode StateChange event (0x00, priority: info)
79pub fn decode_state_change_event(inp: &tlv::TlvItemValue) -> anyhow::Result<StateChangeEvent> {
80    if let tlv::TlvItemValue::List(_fields) = inp {
81        let item = tlv::TlvItem { tag: 0, value: inp.clone() };
82        Ok(StateChangeEvent {
83                                state_value: item.get_bool(&[0]),
84        })
85    } else {
86        Err(anyhow::anyhow!("Expected struct fields"))
87    }
88}
89