matc/clusters/codec/
binding_cluster.rs1use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11#[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
21pub 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
40pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
52 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
68pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
73 vec![
74 (0x0000, "Binding"),
75 ]
76}
77