matc/clusters/codec/
chime.rs1use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11#[derive(Debug, serde::Serialize)]
14pub struct ChimeSound {
15 pub chime_id: Option<u8>,
16 pub name: Option<String>,
17}
18
19pub fn decode_installed_chime_sounds(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<ChimeSound>> {
25 let mut res = Vec::new();
26 if let tlv::TlvItemValue::List(v) = inp {
27 for item in v {
28 res.push(ChimeSound {
29 chime_id: item.get_int(&[0]).map(|v| v as u8),
30 name: item.get_string_owned(&[1]),
31 });
32 }
33 }
34 Ok(res)
35}
36
37pub fn decode_selected_chime(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 UInt8"))
43 }
44}
45
46pub fn decode_enabled(inp: &tlv::TlvItemValue) -> anyhow::Result<bool> {
48 if let tlv::TlvItemValue::Bool(v) = inp {
49 Ok(*v)
50 } else {
51 Err(anyhow::anyhow!("Expected Bool"))
52 }
53}
54
55
56pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
68 if cluster_id != 0x0556 {
70 return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0556, got {}\"}}", cluster_id);
71 }
72
73 match attribute_id {
74 0x0000 => {
75 match decode_installed_chime_sounds(tlv_value) {
76 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
77 Err(e) => format!("{{\"error\": \"{}\"}}", e),
78 }
79 }
80 0x0001 => {
81 match decode_selected_chime(tlv_value) {
82 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
83 Err(e) => format!("{{\"error\": \"{}\"}}", e),
84 }
85 }
86 0x0002 => {
87 match decode_enabled(tlv_value) {
88 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
89 Err(e) => format!("{{\"error\": \"{}\"}}", e),
90 }
91 }
92 _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
93 }
94}
95
96pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
101 vec![
102 (0x0000, "InstalledChimeSounds"),
103 (0x0001, "SelectedChime"),
104 (0x0002, "Enabled"),
105 ]
106}
107