matc/clusters/codec/
admin_commissioning_cluster.rs

1//! Generated Matter TLV encoders and decoders for Administrator Commissioning Cluster
2//! Cluster ID: 0x003C
3//! 
4//! This file is automatically generated from AdminCommissioningCluster.xml
5
6use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11// Command encoders
12
13/// Encode OpenCommissioningWindow command (0x00)
14pub 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
28/// Encode OpenBasicCommissioningWindow command (0x01)
29pub 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
39// Attribute decoders
40
41/// Decode WindowStatus attribute (0x0000)
42pub 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
50/// Decode AdminFabricIndex attribute (0x0001)
51pub 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
59/// Decode AdminVendorId attribute (0x0002)
60pub 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
69// JSON dispatcher function
70
71/// Decode attribute value and return as JSON string
72/// 
73/// # Parameters
74/// * `cluster_id` - The cluster identifier
75/// * `attribute_id` - The attribute identifier
76/// * `tlv_value` - The TLV value to decode
77/// 
78/// # Returns
79/// JSON string representation of the decoded value or error
80pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
81    // Verify this is the correct cluster
82    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
109/// Get list of all attributes supported by this cluster
110/// 
111/// # Returns
112/// Vector of tuples containing (attribute_id, attribute_name)
113pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
114    vec![
115        (0x0000, "WindowStatus"),
116        (0x0001, "AdminFabricIndex"),
117        (0x0002, "AdminVendorId"),
118    ]
119}
120