matc/clusters/codec/
smoke_co_alarm.rs

1//! Generated Matter TLV encoders and decoders for Smoke CO Alarm Cluster
2//! Cluster ID: 0x005C
3//! 
4//! This file is automatically generated from SmokeCOAlarm.xml
5
6use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11// Command encoders
12
13// Attribute decoders
14
15/// Decode ExpressedState attribute (0x0000)
16pub fn decode_expressed_state(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
17    if let tlv::TlvItemValue::Int(v) = inp {
18        Ok(*v as u8)
19    } else {
20        Err(anyhow::anyhow!("Expected Integer"))
21    }
22}
23
24/// Decode SmokeState attribute (0x0001)
25pub fn decode_smoke_state(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
26    if let tlv::TlvItemValue::Int(v) = inp {
27        Ok(*v as u8)
28    } else {
29        Err(anyhow::anyhow!("Expected Integer"))
30    }
31}
32
33/// Decode COState attribute (0x0002)
34pub fn decode_co_state(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
35    if let tlv::TlvItemValue::Int(v) = inp {
36        Ok(*v as u8)
37    } else {
38        Err(anyhow::anyhow!("Expected Integer"))
39    }
40}
41
42/// Decode BatteryAlert attribute (0x0003)
43pub fn decode_battery_alert(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
44    if let tlv::TlvItemValue::Int(v) = inp {
45        Ok(*v as u8)
46    } else {
47        Err(anyhow::anyhow!("Expected Integer"))
48    }
49}
50
51/// Decode DeviceMuted attribute (0x0004)
52pub fn decode_device_muted(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
53    if let tlv::TlvItemValue::Int(v) = inp {
54        Ok(*v as u8)
55    } else {
56        Err(anyhow::anyhow!("Expected Integer"))
57    }
58}
59
60/// Decode TestInProgress attribute (0x0005)
61pub fn decode_test_in_progress(inp: &tlv::TlvItemValue) -> anyhow::Result<bool> {
62    if let tlv::TlvItemValue::Bool(v) = inp {
63        Ok(*v)
64    } else {
65        Err(anyhow::anyhow!("Expected Bool"))
66    }
67}
68
69/// Decode HardwareFaultAlert attribute (0x0006)
70pub fn decode_hardware_fault_alert(inp: &tlv::TlvItemValue) -> anyhow::Result<bool> {
71    if let tlv::TlvItemValue::Bool(v) = inp {
72        Ok(*v)
73    } else {
74        Err(anyhow::anyhow!("Expected Bool"))
75    }
76}
77
78/// Decode EndOfServiceAlert attribute (0x0007)
79pub fn decode_end_of_service_alert(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
80    if let tlv::TlvItemValue::Int(v) = inp {
81        Ok(*v as u8)
82    } else {
83        Err(anyhow::anyhow!("Expected Integer"))
84    }
85}
86
87/// Decode InterconnectSmokeAlarm attribute (0x0008)
88pub fn decode_interconnect_smoke_alarm(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
89    if let tlv::TlvItemValue::Int(v) = inp {
90        Ok(*v as u8)
91    } else {
92        Err(anyhow::anyhow!("Expected Integer"))
93    }
94}
95
96/// Decode InterconnectCOAlarm attribute (0x0009)
97pub fn decode_interconnect_co_alarm(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
98    if let tlv::TlvItemValue::Int(v) = inp {
99        Ok(*v as u8)
100    } else {
101        Err(anyhow::anyhow!("Expected Integer"))
102    }
103}
104
105/// Decode ContaminationState attribute (0x000A)
106pub fn decode_contamination_state(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
107    if let tlv::TlvItemValue::Int(v) = inp {
108        Ok(*v as u8)
109    } else {
110        Err(anyhow::anyhow!("Expected Integer"))
111    }
112}
113
114/// Decode SmokeSensitivityLevel attribute (0x000B)
115pub fn decode_smoke_sensitivity_level(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
116    if let tlv::TlvItemValue::Int(v) = inp {
117        Ok(*v as u8)
118    } else {
119        Err(anyhow::anyhow!("Expected Integer"))
120    }
121}
122
123/// Decode ExpiryDate attribute (0x000C)
124pub fn decode_expiry_date(inp: &tlv::TlvItemValue) -> anyhow::Result<u64> {
125    if let tlv::TlvItemValue::Int(v) = inp {
126        Ok(*v)
127    } else {
128        Err(anyhow::anyhow!("Expected Integer"))
129    }
130}
131
132
133// JSON dispatcher function
134
135/// Decode attribute value and return as JSON string
136/// 
137/// # Parameters
138/// * `cluster_id` - The cluster identifier
139/// * `attribute_id` - The attribute identifier
140/// * `tlv_value` - The TLV value to decode
141/// 
142/// # Returns
143/// JSON string representation of the decoded value or error
144pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
145    // Verify this is the correct cluster
146    if cluster_id != 0x005C {
147        return format!("{{\"error\": \"Invalid cluster ID. Expected 0x005C, got {}\"}}", cluster_id);
148    }
149    
150    match attribute_id {
151        0x0000 => {
152            match decode_expressed_state(tlv_value) {
153                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
154                Err(e) => format!("{{\"error\": \"{}\"}}", e),
155            }
156        }
157        0x0001 => {
158            match decode_smoke_state(tlv_value) {
159                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
160                Err(e) => format!("{{\"error\": \"{}\"}}", e),
161            }
162        }
163        0x0002 => {
164            match decode_co_state(tlv_value) {
165                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
166                Err(e) => format!("{{\"error\": \"{}\"}}", e),
167            }
168        }
169        0x0003 => {
170            match decode_battery_alert(tlv_value) {
171                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
172                Err(e) => format!("{{\"error\": \"{}\"}}", e),
173            }
174        }
175        0x0004 => {
176            match decode_device_muted(tlv_value) {
177                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
178                Err(e) => format!("{{\"error\": \"{}\"}}", e),
179            }
180        }
181        0x0005 => {
182            match decode_test_in_progress(tlv_value) {
183                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
184                Err(e) => format!("{{\"error\": \"{}\"}}", e),
185            }
186        }
187        0x0006 => {
188            match decode_hardware_fault_alert(tlv_value) {
189                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
190                Err(e) => format!("{{\"error\": \"{}\"}}", e),
191            }
192        }
193        0x0007 => {
194            match decode_end_of_service_alert(tlv_value) {
195                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
196                Err(e) => format!("{{\"error\": \"{}\"}}", e),
197            }
198        }
199        0x0008 => {
200            match decode_interconnect_smoke_alarm(tlv_value) {
201                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
202                Err(e) => format!("{{\"error\": \"{}\"}}", e),
203            }
204        }
205        0x0009 => {
206            match decode_interconnect_co_alarm(tlv_value) {
207                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
208                Err(e) => format!("{{\"error\": \"{}\"}}", e),
209            }
210        }
211        0x000A => {
212            match decode_contamination_state(tlv_value) {
213                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
214                Err(e) => format!("{{\"error\": \"{}\"}}", e),
215            }
216        }
217        0x000B => {
218            match decode_smoke_sensitivity_level(tlv_value) {
219                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
220                Err(e) => format!("{{\"error\": \"{}\"}}", e),
221            }
222        }
223        0x000C => {
224            match decode_expiry_date(tlv_value) {
225                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
226                Err(e) => format!("{{\"error\": \"{}\"}}", e),
227            }
228        }
229        _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
230    }
231}
232
233/// Get list of all attributes supported by this cluster
234/// 
235/// # Returns
236/// Vector of tuples containing (attribute_id, attribute_name)
237pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
238    vec![
239        (0x0000, "ExpressedState"),
240        (0x0001, "SmokeState"),
241        (0x0002, "COState"),
242        (0x0003, "BatteryAlert"),
243        (0x0004, "DeviceMuted"),
244        (0x0005, "TestInProgress"),
245        (0x0006, "HardwareFaultAlert"),
246        (0x0007, "EndOfServiceAlert"),
247        (0x0008, "InterconnectSmokeAlarm"),
248        (0x0009, "InterconnectCOAlarm"),
249        (0x000A, "ContaminationState"),
250        (0x000B, "SmokeSensitivityLevel"),
251        (0x000C, "ExpiryDate"),
252    ]
253}
254