matc/clusters/codec/
binding_cluster.rs

1//! Matter TLV encoders and decoders for Binding Cluster
2//! Cluster ID: 0x001E
3//!
4//! This file is automatically generated from Binding-Cluster.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 Target {
17    pub node: Option<u64>,
18    pub group: Option<u8>,
19    pub endpoint: Option<u16>,
20    pub cluster: Option<u32>,
21}
22
23// Attribute decoders
24
25/// Decode Binding attribute (0x0000)
26pub fn decode_binding(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<Target>> {
27    let mut res = Vec::new();
28    if let tlv::TlvItemValue::List(v) = inp {
29        for item in v {
30            res.push(Target {
31                node: item.get_int(&[1]),
32                group: item.get_int(&[2]).map(|v| v as u8),
33                endpoint: item.get_int(&[3]).map(|v| v as u16),
34                cluster: item.get_int(&[4]).map(|v| v as u32),
35            });
36        }
37    }
38    Ok(res)
39}
40
41
42// JSON dispatcher function
43
44/// Decode attribute value and return as JSON string
45///
46/// # Parameters
47/// * `cluster_id` - The cluster identifier
48/// * `attribute_id` - The attribute identifier
49/// * `tlv_value` - The TLV value to decode
50///
51/// # Returns
52/// JSON string representation of the decoded value or error
53pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
54    // Verify this is the correct cluster
55    if cluster_id != 0x001E {
56        return format!("{{\"error\": \"Invalid cluster ID. Expected 0x001E, got {}\"}}", cluster_id);
57    }
58
59    match attribute_id {
60        0x0000 => {
61            match decode_binding(tlv_value) {
62                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
63                Err(e) => format!("{{\"error\": \"{}\"}}", e),
64            }
65        }
66        _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
67    }
68}
69
70/// Get list of all attributes supported by this cluster
71///
72/// # Returns
73/// Vector of tuples containing (attribute_id, attribute_name)
74pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
75    vec![
76        (0x0000, "Binding"),
77    ]
78}
79
80// Typed facade (invokes + reads)
81
82/// Read `Binding` attribute from cluster `Binding`.
83pub async fn read_binding(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Vec<Target>> {
84    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_BINDING, crate::clusters::defs::CLUSTER_BINDING_ATTR_ID_BINDING).await?;
85    decode_binding(&tlv)
86}
87