matc/clusters/codec/
boolean_state_configuration.rs

1//! Generated Matter TLV encoders and decoders for Boolean State Configuration Cluster
2//! Cluster ID: 0x0080
3//! 
4//! This file is automatically generated from BooleanStateConfiguration.xml
5
6use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11// Command encoders
12
13/// Encode SuppressAlarm command (0x00)
14pub fn encode_suppress_alarm(alarms_to_suppress: u8) -> anyhow::Result<Vec<u8>> {
15    let tlv = tlv::TlvItemEnc {
16        tag: 0,
17        value: tlv::TlvItemValueEnc::StructInvisible(vec![
18        (0, tlv::TlvItemValueEnc::UInt8(alarms_to_suppress)).into(),
19        ]),
20    };
21    Ok(tlv.encode()?)
22}
23
24/// Encode EnableDisableAlarm command (0x01)
25pub fn encode_enable_disable_alarm(alarms_to_enable_disable: u8) -> anyhow::Result<Vec<u8>> {
26    let tlv = tlv::TlvItemEnc {
27        tag: 0,
28        value: tlv::TlvItemValueEnc::StructInvisible(vec![
29        (0, tlv::TlvItemValueEnc::UInt8(alarms_to_enable_disable)).into(),
30        ]),
31    };
32    Ok(tlv.encode()?)
33}
34
35// Attribute decoders
36
37/// Decode CurrentSensitivityLevel attribute (0x0000)
38pub fn decode_current_sensitivity_level(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
39    if let tlv::TlvItemValue::Int(v) = inp {
40        Ok(*v as u8)
41    } else {
42        Err(anyhow::anyhow!("Expected Integer"))
43    }
44}
45
46/// Decode SupportedSensitivityLevels attribute (0x0001)
47pub fn decode_supported_sensitivity_levels(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
48    if let tlv::TlvItemValue::Int(v) = inp {
49        Ok(*v as u8)
50    } else {
51        Err(anyhow::anyhow!("Expected Integer"))
52    }
53}
54
55/// Decode DefaultSensitivityLevel attribute (0x0002)
56pub fn decode_default_sensitivity_level(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
57    if let tlv::TlvItemValue::Int(v) = inp {
58        Ok(*v as u8)
59    } else {
60        Err(anyhow::anyhow!("Expected Integer"))
61    }
62}
63
64/// Decode AlarmsActive attribute (0x0003)
65pub fn decode_alarms_active(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
66    if let tlv::TlvItemValue::Int(v) = inp {
67        Ok(*v as u8)
68    } else {
69        Err(anyhow::anyhow!("Expected Integer"))
70    }
71}
72
73/// Decode AlarmsSuppressed attribute (0x0004)
74pub fn decode_alarms_suppressed(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
75    if let tlv::TlvItemValue::Int(v) = inp {
76        Ok(*v as u8)
77    } else {
78        Err(anyhow::anyhow!("Expected Integer"))
79    }
80}
81
82/// Decode AlarmsEnabled attribute (0x0005)
83pub fn decode_alarms_enabled(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
84    if let tlv::TlvItemValue::Int(v) = inp {
85        Ok(*v as u8)
86    } else {
87        Err(anyhow::anyhow!("Expected Integer"))
88    }
89}
90
91/// Decode AlarmsSupported attribute (0x0006)
92pub fn decode_alarms_supported(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
93    if let tlv::TlvItemValue::Int(v) = inp {
94        Ok(*v as u8)
95    } else {
96        Err(anyhow::anyhow!("Expected Integer"))
97    }
98}
99
100/// Decode SensorFault attribute (0x0007)
101pub fn decode_sensor_fault(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
102    if let tlv::TlvItemValue::Int(v) = inp {
103        Ok(*v as u8)
104    } else {
105        Err(anyhow::anyhow!("Expected Integer"))
106    }
107}
108
109
110// JSON dispatcher function
111
112/// Decode attribute value and return as JSON string
113/// 
114/// # Parameters
115/// * `cluster_id` - The cluster identifier
116/// * `attribute_id` - The attribute identifier
117/// * `tlv_value` - The TLV value to decode
118/// 
119/// # Returns
120/// JSON string representation of the decoded value or error
121pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
122    // Verify this is the correct cluster
123    if cluster_id != 0x0080 {
124        return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0080, got {}\"}}", cluster_id);
125    }
126    
127    match attribute_id {
128        0x0000 => {
129            match decode_current_sensitivity_level(tlv_value) {
130                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
131                Err(e) => format!("{{\"error\": \"{}\"}}", e),
132            }
133        }
134        0x0001 => {
135            match decode_supported_sensitivity_levels(tlv_value) {
136                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
137                Err(e) => format!("{{\"error\": \"{}\"}}", e),
138            }
139        }
140        0x0002 => {
141            match decode_default_sensitivity_level(tlv_value) {
142                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
143                Err(e) => format!("{{\"error\": \"{}\"}}", e),
144            }
145        }
146        0x0003 => {
147            match decode_alarms_active(tlv_value) {
148                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
149                Err(e) => format!("{{\"error\": \"{}\"}}", e),
150            }
151        }
152        0x0004 => {
153            match decode_alarms_suppressed(tlv_value) {
154                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
155                Err(e) => format!("{{\"error\": \"{}\"}}", e),
156            }
157        }
158        0x0005 => {
159            match decode_alarms_enabled(tlv_value) {
160                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
161                Err(e) => format!("{{\"error\": \"{}\"}}", e),
162            }
163        }
164        0x0006 => {
165            match decode_alarms_supported(tlv_value) {
166                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
167                Err(e) => format!("{{\"error\": \"{}\"}}", e),
168            }
169        }
170        0x0007 => {
171            match decode_sensor_fault(tlv_value) {
172                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
173                Err(e) => format!("{{\"error\": \"{}\"}}", e),
174            }
175        }
176        _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
177    }
178}
179
180/// Get list of all attributes supported by this cluster
181/// 
182/// # Returns
183/// Vector of tuples containing (attribute_id, attribute_name)
184pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
185    vec![
186        (0x0000, "CurrentSensitivityLevel"),
187        (0x0001, "SupportedSensitivityLevels"),
188        (0x0002, "DefaultSensitivityLevel"),
189        (0x0003, "AlarmsActive"),
190        (0x0004, "AlarmsSuppressed"),
191        (0x0005, "AlarmsEnabled"),
192        (0x0006, "AlarmsSupported"),
193        (0x0007, "SensorFault"),
194    ]
195}
196