matc/clusters/codec/
alarm_base.rs1use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11pub 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
24pub 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
35pub 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
46pub 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
55pub 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
64pub 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
74pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
86 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
120pub 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