matc/clusters/codec/
alarm_base.rs

1//! Generated Matter TLV encoders and decoders for Alarm Base Cluster
2//! Cluster ID: 0x0000
3//! 
4//! This file is automatically generated from AlarmBase.xml
5
6use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11// Command encoders
12
13/// Encode Reset command (0x00)
14pub fn encode_reset(alarms: 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)).into(),
19        ]),
20    };
21    Ok(tlv.encode()?)
22}
23
24/// Encode ModifyEnabledAlarms command (0x01)
25pub fn encode_modify_enabled_alarms(mask: u8) -> anyhow::Result<Vec<u8>> {
26    let tlv = tlv::TlvItemEnc {
27        tag: 0,
28        value: tlv::TlvItemValueEnc::StructInvisible(vec![
29        (0, tlv::TlvItemValueEnc::UInt8(mask)).into(),
30        ]),
31    };
32    Ok(tlv.encode()?)
33}
34
35// Attribute decoders
36
37/// Decode Mask attribute (0x0000)
38pub fn decode_mask(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 Latch attribute (0x0001)
47pub fn decode_latch(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 State attribute (0x0002)
56pub fn decode_state(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 Supported attribute (0x0003)
65pub fn decode_supported(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
74// JSON dispatcher function
75
76/// Decode attribute value and return as JSON string
77/// 
78/// # Parameters
79/// * `cluster_id` - The cluster identifier
80/// * `attribute_id` - The attribute identifier
81/// * `tlv_value` - The TLV value to decode
82/// 
83/// # Returns
84/// JSON string representation of the decoded value or error
85pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
86    // Verify this is the correct cluster
87    if cluster_id != 0x0000 {
88        return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0000, got {}\"}}", cluster_id);
89    }
90    
91    match attribute_id {
92        0x0000 => {
93            match decode_mask(tlv_value) {
94                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
95                Err(e) => format!("{{\"error\": \"{}\"}}", e),
96            }
97        }
98        0x0001 => {
99            match decode_latch(tlv_value) {
100                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
101                Err(e) => format!("{{\"error\": \"{}\"}}", e),
102            }
103        }
104        0x0002 => {
105            match decode_state(tlv_value) {
106                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
107                Err(e) => format!("{{\"error\": \"{}\"}}", e),
108            }
109        }
110        0x0003 => {
111            match decode_supported(tlv_value) {
112                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
113                Err(e) => format!("{{\"error\": \"{}\"}}", e),
114            }
115        }
116        _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
117    }
118}
119
120/// Get list of all attributes supported by this cluster
121/// 
122/// # Returns
123/// Vector of tuples containing (attribute_id, attribute_name)
124pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
125    vec![
126        (0x0000, "Mask"),
127        (0x0001, "Latch"),
128        (0x0002, "State"),
129        (0x0003, "Supported"),
130    ]
131}
132