matc/clusters/codec/
binding_cluster.rs1#![allow(clippy::too_many_arguments)]
7
8use crate::tlv;
9use anyhow;
10use serde_json;
11
12
13#[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
23pub 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
42pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
54 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
70pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
75 vec![
76 (0x0000, "Binding"),
77 ]
78}
79
80pub 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