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
6#![allow(clippy::too_many_arguments)]
7
8use crate::tlv;
9use anyhow;
10use serde_json;
11
12
13// Struct definitions
14
15#[derive(Debug, serde::Serialize)]
16pub struct ChimeSound {
17    pub chime_id: Option<u8>,
18    pub name: Option<String>,
19}
20
21// Command encoders
22
23// Attribute decoders
24
25/// Decode InstalledChimeSounds attribute (0x0000)
26pub fn decode_installed_chime_sounds(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<ChimeSound>> {
27    let mut res = Vec::new();
28    if let tlv::TlvItemValue::List(v) = inp {
29        for item in v {
30            res.push(ChimeSound {
31                chime_id: item.get_int(&[0]).map(|v| v as u8),
32                name: item.get_string_owned(&[1]),
33            });
34        }
35    }
36    Ok(res)
37}
38
39/// Decode SelectedChime attribute (0x0001)
40pub fn decode_selected_chime(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
41    if let tlv::TlvItemValue::Int(v) = inp {
42        Ok(*v as u8)
43    } else {
44        Err(anyhow::anyhow!("Expected UInt8"))
45    }
46}
47
48/// Decode Enabled attribute (0x0002)
49pub fn decode_enabled(inp: &tlv::TlvItemValue) -> anyhow::Result<bool> {
50    if let tlv::TlvItemValue::Bool(v) = inp {
51        Ok(*v)
52    } else {
53        Err(anyhow::anyhow!("Expected Bool"))
54    }
55}
56
57
58// JSON dispatcher function
59
60/// Decode attribute value and return as JSON string
61///
62/// # Parameters
63/// * `cluster_id` - The cluster identifier
64/// * `attribute_id` - The attribute identifier
65/// * `tlv_value` - The TLV value to decode
66///
67/// # Returns
68/// JSON string representation of the decoded value or error
69pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
70    // Verify this is the correct cluster
71    if cluster_id != 0x0556 {
72        return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0556, got {}\"}}", cluster_id);
73    }
74
75    match attribute_id {
76        0x0000 => {
77            match decode_installed_chime_sounds(tlv_value) {
78                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
79                Err(e) => format!("{{\"error\": \"{}\"}}", e),
80            }
81        }
82        0x0001 => {
83            match decode_selected_chime(tlv_value) {
84                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
85                Err(e) => format!("{{\"error\": \"{}\"}}", e),
86            }
87        }
88        0x0002 => {
89            match decode_enabled(tlv_value) {
90                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
91                Err(e) => format!("{{\"error\": \"{}\"}}", e),
92            }
93        }
94        _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
95    }
96}
97
98/// Get list of all attributes supported by this cluster
99///
100/// # Returns
101/// Vector of tuples containing (attribute_id, attribute_name)
102pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
103    vec![
104        (0x0000, "InstalledChimeSounds"),
105        (0x0001, "SelectedChime"),
106        (0x0002, "Enabled"),
107    ]
108}
109
110// Command listing
111
112pub fn get_command_list() -> Vec<(u32, &'static str)> {
113    vec![
114        (0x00, "PlayChimeSound"),
115    ]
116}
117
118pub fn get_command_name(cmd_id: u32) -> Option<&'static str> {
119    match cmd_id {
120        0x00 => Some("PlayChimeSound"),
121        _ => None,
122    }
123}
124
125pub fn get_command_schema(cmd_id: u32) -> Option<Vec<crate::clusters::codec::CommandField>> {
126    match cmd_id {
127        0x00 => Some(vec![]),
128        _ => None,
129    }
130}
131
132pub fn encode_command_json(cmd_id: u32, _args: &serde_json::Value) -> anyhow::Result<Vec<u8>> {
133    match cmd_id {
134        0x00 => Ok(vec![]),
135        _ => Err(anyhow::anyhow!("unknown command ID: 0x{:02X}", cmd_id)),
136    }
137}
138
139// Typed facade (invokes + reads)
140
141/// Invoke `PlayChimeSound` command on cluster `Chime`.
142pub async fn play_chime_sound(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<()> {
143    conn.invoke_request(endpoint, crate::clusters::defs::CLUSTER_ID_CHIME, crate::clusters::defs::CLUSTER_CHIME_CMD_ID_PLAYCHIMESOUND, &[]).await?;
144    Ok(())
145}
146
147/// Read `InstalledChimeSounds` attribute from cluster `Chime`.
148pub async fn read_installed_chime_sounds(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Vec<ChimeSound>> {
149    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_CHIME, crate::clusters::defs::CLUSTER_CHIME_ATTR_ID_INSTALLEDCHIMESOUNDS).await?;
150    decode_installed_chime_sounds(&tlv)
151}
152
153/// Read `SelectedChime` attribute from cluster `Chime`.
154pub async fn read_selected_chime(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<u8> {
155    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_CHIME, crate::clusters::defs::CLUSTER_CHIME_ATTR_ID_SELECTEDCHIME).await?;
156    decode_selected_chime(&tlv)
157}
158
159/// Read `Enabled` attribute from cluster `Chime`.
160pub async fn read_enabled(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<bool> {
161    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_CHIME, crate::clusters::defs::CLUSTER_CHIME_ATTR_ID_ENABLED).await?;
162    decode_enabled(&tlv)
163}
164