matc/clusters/codec/
chime.rs

1//! Matter TLV encoders and decoders for Chime Cluster
2//! Cluster ID: 0x0556
3//!
4//! This file is automatically generated from Chime.xml
5
6use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11// Struct definitions
12
13#[derive(Debug, serde::Serialize)]
14pub struct ChimeSound {
15    pub chime_id: Option<u8>,
16    pub name: Option<String>,
17}
18
19// Command encoders
20
21// Attribute decoders
22
23/// Decode InstalledChimeSounds attribute (0x0000)
24pub 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
37/// Decode SelectedChime attribute (0x0001)
38pub 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
46/// Decode Enabled attribute (0x0002)
47pub 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
56// JSON dispatcher function
57
58/// Decode attribute value and return as JSON string
59///
60/// # Parameters
61/// * `cluster_id` - The cluster identifier
62/// * `attribute_id` - The attribute identifier
63/// * `tlv_value` - The TLV value to decode
64///
65/// # Returns
66/// JSON string representation of the decoded value or error
67pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
68    // Verify this is the correct cluster
69    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
96/// Get list of all attributes supported by this cluster
97///
98/// # Returns
99/// Vector of tuples containing (attribute_id, attribute_name)
100pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
101    vec![
102        (0x0000, "InstalledChimeSounds"),
103        (0x0001, "SelectedChime"),
104        (0x0002, "Enabled"),
105    ]
106}
107