matc/clusters/codec/
commissioner_control_cluster.rs

1//! Generated Matter TLV encoders and decoders for Commissioner Control Cluster
2//! Cluster ID: 0x0751
3//! 
4//! This file is automatically generated from CommissionerControlCluster.xml
5
6use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11// Command encoders
12
13/// Encode RequestCommissioningApproval command (0x00)
14pub fn encode_request_commissioning_approval(request_id: u64, vendor_id: u16, product_id: u16, label: String) -> anyhow::Result<Vec<u8>> {
15    let tlv = tlv::TlvItemEnc {
16        tag: 0,
17        value: tlv::TlvItemValueEnc::StructInvisible(vec![
18        (0, tlv::TlvItemValueEnc::UInt64(request_id)).into(),
19        (1, tlv::TlvItemValueEnc::UInt16(vendor_id)).into(),
20        (2, tlv::TlvItemValueEnc::UInt16(product_id)).into(),
21        (3, tlv::TlvItemValueEnc::String(label)).into(),
22        ]),
23    };
24    Ok(tlv.encode()?)
25}
26
27/// Encode CommissionNode command (0x01)
28pub fn encode_commission_node(request_id: u64, response_timeout_seconds: u16) -> anyhow::Result<Vec<u8>> {
29    let tlv = tlv::TlvItemEnc {
30        tag: 0,
31        value: tlv::TlvItemValueEnc::StructInvisible(vec![
32        (0, tlv::TlvItemValueEnc::UInt64(request_id)).into(),
33        (1, tlv::TlvItemValueEnc::UInt16(response_timeout_seconds)).into(),
34        ]),
35    };
36    Ok(tlv.encode()?)
37}
38
39// Attribute decoders
40
41/// Decode SupportedDeviceCategories attribute (0x0000)
42pub fn decode_supported_device_categories(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
51// JSON dispatcher function
52
53/// Decode attribute value and return as JSON string
54/// 
55/// # Parameters
56/// * `cluster_id` - The cluster identifier
57/// * `attribute_id` - The attribute identifier
58/// * `tlv_value` - The TLV value to decode
59/// 
60/// # Returns
61/// JSON string representation of the decoded value or error
62pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
63    // Verify this is the correct cluster
64    if cluster_id != 0x0751 {
65        return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0751, got {}\"}}", cluster_id);
66    }
67    
68    match attribute_id {
69        0x0000 => {
70            match decode_supported_device_categories(tlv_value) {
71                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
72                Err(e) => format!("{{\"error\": \"{}\"}}", e),
73            }
74        }
75        _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
76    }
77}
78
79/// Get list of all attributes supported by this cluster
80/// 
81/// # Returns
82/// Vector of tuples containing (attribute_id, attribute_name)
83pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
84    vec![
85        (0x0000, "SupportedDeviceCategories"),
86    ]
87}
88