matc/clusters/codec/
admin_commissioning_cluster.rs1use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11pub fn encode_open_commissioning_window(commissioning_timeout: u16, pake_passcode_verifier: Vec<u8>, discriminator: u16, iterations: u32, salt: Vec<u8>) -> anyhow::Result<Vec<u8>> {
15 let tlv = tlv::TlvItemEnc {
16 tag: 0,
17 value: tlv::TlvItemValueEnc::StructInvisible(vec![
18 (0, tlv::TlvItemValueEnc::UInt16(commissioning_timeout)).into(),
19 (1, tlv::TlvItemValueEnc::OctetString(pake_passcode_verifier)).into(),
20 (2, tlv::TlvItemValueEnc::UInt16(discriminator)).into(),
21 (3, tlv::TlvItemValueEnc::UInt32(iterations)).into(),
22 (4, tlv::TlvItemValueEnc::OctetString(salt)).into(),
23 ]),
24 };
25 Ok(tlv.encode()?)
26}
27
28pub fn encode_open_basic_commissioning_window(commissioning_timeout: u16) -> anyhow::Result<Vec<u8>> {
30 let tlv = tlv::TlvItemEnc {
31 tag: 0,
32 value: tlv::TlvItemValueEnc::StructInvisible(vec![
33 (0, tlv::TlvItemValueEnc::UInt16(commissioning_timeout)).into(),
34 ]),
35 };
36 Ok(tlv.encode()?)
37}
38
39pub fn decode_window_status(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
43 if let tlv::TlvItemValue::Int(v) = inp {
44 Ok(*v as u8)
45 } else {
46 Err(anyhow::anyhow!("Expected Integer"))
47 }
48}
49
50pub fn decode_admin_fabric_index(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<u8>> {
52 if let tlv::TlvItemValue::Int(v) = inp {
53 Ok(Some(*v as u8))
54 } else {
55 Ok(None)
56 }
57}
58
59pub fn decode_admin_vendor_id(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<u16>> {
61 if let tlv::TlvItemValue::Int(v) = inp {
62 Ok(Some(*v as u16))
63 } else {
64 Ok(None)
65 }
66}
67
68
69pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
81 if cluster_id != 0x003C {
83 return format!("{{\"error\": \"Invalid cluster ID. Expected 0x003C, got {}\"}}", cluster_id);
84 }
85
86 match attribute_id {
87 0x0000 => {
88 match decode_window_status(tlv_value) {
89 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
90 Err(e) => format!("{{\"error\": \"{}\"}}", e),
91 }
92 }
93 0x0001 => {
94 match decode_admin_fabric_index(tlv_value) {
95 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
96 Err(e) => format!("{{\"error\": \"{}\"}}", e),
97 }
98 }
99 0x0002 => {
100 match decode_admin_vendor_id(tlv_value) {
101 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
102 Err(e) => format!("{{\"error\": \"{}\"}}", e),
103 }
104 }
105 _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
106 }
107}
108
109pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
114 vec![
115 (0x0000, "WindowStatus"),
116 (0x0001, "AdminFabricIndex"),
117 (0x0002, "AdminVendorId"),
118 ]
119}
120