matc/clusters/codec/
binding_cluster.rs

1//! Generated Matter TLV encoders and decoders for Binding Cluster
2//! Cluster ID: 0x001E
3//! 
4//! This file is automatically generated from Binding-Cluster.xml
5
6use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11// Struct definitions
12
13#[derive(Debug, serde::Serialize)]
14pub struct Target {
15    pub node: Option<u64>,
16    pub group: Option<u8>,
17    pub endpoint: Option<u16>,
18    pub cluster: Option<u32>,
19}
20
21// Attribute decoders
22
23/// Decode Binding attribute (0x0000)
24pub fn decode_binding(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<Target>> {
25    let mut res = Vec::new();
26    if let tlv::TlvItemValue::List(v) = inp {
27        for item in v {
28            res.push(Target {
29                node: item.get_int(&[1]),
30                group: item.get_int(&[2]).map(|v| v as u8),
31                endpoint: item.get_int(&[3]).map(|v| v as u16),
32                cluster: item.get_int(&[4]).map(|v| v as u32),
33            });
34        }
35    }
36    Ok(res)
37}
38
39
40// JSON dispatcher function
41
42/// Decode attribute value and return as JSON string
43/// 
44/// # Parameters
45/// * `cluster_id` - The cluster identifier
46/// * `attribute_id` - The attribute identifier
47/// * `tlv_value` - The TLV value to decode
48/// 
49/// # Returns
50/// JSON string representation of the decoded value or error
51pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
52    // Verify this is the correct cluster
53    if cluster_id != 0x001E {
54        return format!("{{\"error\": \"Invalid cluster ID. Expected 0x001E, got {}\"}}", cluster_id);
55    }
56    
57    match attribute_id {
58        0x0000 => {
59            match decode_binding(tlv_value) {
60                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
61                Err(e) => format!("{{\"error\": \"{}\"}}", e),
62            }
63        }
64        _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
65    }
66}
67
68/// Get list of all attributes supported by this cluster
69/// 
70/// # Returns
71/// Vector of tuples containing (attribute_id, attribute_name)
72pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
73    vec![
74        (0x0000, "Binding"),
75    ]
76}
77