matc/clusters/codec/
alarm_base.rs1#![allow(clippy::too_many_arguments)]
7
8use crate::tlv;
9use anyhow;
10use serde_json;
11
12
13pub type Alarm = u8;
17
18pub fn encode_reset(alarms: Alarm) -> anyhow::Result<Vec<u8>> {
22 let tlv = tlv::TlvItemEnc {
23 tag: 0,
24 value: tlv::TlvItemValueEnc::StructInvisible(vec![
25 (0, tlv::TlvItemValueEnc::UInt8(alarms)).into(),
26 ]),
27 };
28 Ok(tlv.encode()?)
29}
30
31pub fn encode_modify_enabled_alarms(mask: Alarm) -> anyhow::Result<Vec<u8>> {
33 let tlv = tlv::TlvItemEnc {
34 tag: 0,
35 value: tlv::TlvItemValueEnc::StructInvisible(vec![
36 (0, tlv::TlvItemValueEnc::UInt8(mask)).into(),
37 ]),
38 };
39 Ok(tlv.encode()?)
40}
41
42pub fn decode_mask(inp: &tlv::TlvItemValue) -> anyhow::Result<Alarm> {
46 if let tlv::TlvItemValue::Int(v) = inp {
47 Ok(*v as u8)
48 } else {
49 Err(anyhow::anyhow!("Expected Integer"))
50 }
51}
52
53pub fn decode_latch(inp: &tlv::TlvItemValue) -> anyhow::Result<Alarm> {
55 if let tlv::TlvItemValue::Int(v) = inp {
56 Ok(*v as u8)
57 } else {
58 Err(anyhow::anyhow!("Expected Integer"))
59 }
60}
61
62pub fn decode_state(inp: &tlv::TlvItemValue) -> anyhow::Result<Alarm> {
64 if let tlv::TlvItemValue::Int(v) = inp {
65 Ok(*v as u8)
66 } else {
67 Err(anyhow::anyhow!("Expected Integer"))
68 }
69}
70
71pub fn decode_supported(inp: &tlv::TlvItemValue) -> anyhow::Result<Alarm> {
73 if let tlv::TlvItemValue::Int(v) = inp {
74 Ok(*v as u8)
75 } else {
76 Err(anyhow::anyhow!("Expected Integer"))
77 }
78}
79
80
81pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
93 if cluster_id != 0x0000 {
95 return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0000, got {}\"}}", cluster_id);
96 }
97
98 match attribute_id {
99 0x0000 => {
100 match decode_mask(tlv_value) {
101 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
102 Err(e) => format!("{{\"error\": \"{}\"}}", e),
103 }
104 }
105 0x0001 => {
106 match decode_latch(tlv_value) {
107 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
108 Err(e) => format!("{{\"error\": \"{}\"}}", e),
109 }
110 }
111 0x0002 => {
112 match decode_state(tlv_value) {
113 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
114 Err(e) => format!("{{\"error\": \"{}\"}}", e),
115 }
116 }
117 0x0003 => {
118 match decode_supported(tlv_value) {
119 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
120 Err(e) => format!("{{\"error\": \"{}\"}}", e),
121 }
122 }
123 _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
124 }
125}
126
127pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
132 vec![
133 (0x0000, "Mask"),
134 (0x0001, "Latch"),
135 (0x0002, "State"),
136 (0x0003, "Supported"),
137 ]
138}
139
140pub fn get_command_list() -> Vec<(u32, &'static str)> {
143 vec![
144 (0x00, "Reset"),
145 (0x01, "ModifyEnabledAlarms"),
146 ]
147}
148
149pub fn get_command_name(cmd_id: u32) -> Option<&'static str> {
150 match cmd_id {
151 0x00 => Some("Reset"),
152 0x01 => Some("ModifyEnabledAlarms"),
153 _ => None,
154 }
155}
156
157pub fn get_command_schema(cmd_id: u32) -> Option<Vec<crate::clusters::codec::CommandField>> {
158 match cmd_id {
159 0x00 => Some(vec![
160 crate::clusters::codec::CommandField { tag: 0, name: "alarms", kind: crate::clusters::codec::FieldKind::Bitmap { name: "Alarm", bits: &[] }, optional: false, nullable: false },
161 ]),
162 0x01 => Some(vec![
163 crate::clusters::codec::CommandField { tag: 0, name: "mask", kind: crate::clusters::codec::FieldKind::Bitmap { name: "Alarm", bits: &[] }, optional: false, nullable: false },
164 ]),
165 _ => None,
166 }
167}
168
169pub fn encode_command_json(cmd_id: u32, args: &serde_json::Value) -> anyhow::Result<Vec<u8>> {
170 match cmd_id {
171 0x00 => {
172 let alarms = crate::clusters::codec::json_util::get_u8(args, "alarms")?;
173 encode_reset(alarms)
174 }
175 0x01 => {
176 let mask = crate::clusters::codec::json_util::get_u8(args, "mask")?;
177 encode_modify_enabled_alarms(mask)
178 }
179 _ => Err(anyhow::anyhow!("unknown command ID: 0x{:02X}", cmd_id)),
180 }
181}
182
183#[derive(Debug, serde::Serialize)]
184pub struct NotifyEvent {
185 pub active: Option<Alarm>,
186 pub inactive: Option<Alarm>,
187 pub state: Option<Alarm>,
188 pub mask: Option<Alarm>,
189}
190
191pub fn decode_notify_event(inp: &tlv::TlvItemValue) -> anyhow::Result<NotifyEvent> {
195 if let tlv::TlvItemValue::List(_fields) = inp {
196 let item = tlv::TlvItem { tag: 0, value: inp.clone() };
197 Ok(NotifyEvent {
198 active: item.get_int(&[0]).map(|v| v as u8),
199 inactive: item.get_int(&[1]).map(|v| v as u8),
200 state: item.get_int(&[2]).map(|v| v as u8),
201 mask: item.get_int(&[3]).map(|v| v as u8),
202 })
203 } else {
204 Err(anyhow::anyhow!("Expected struct fields"))
205 }
206}
207