matc/clusters/codec/
commissioner_control_cluster.rs

1//! 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// Import serialization helpers for octet strings
12use crate::clusters::helpers::{serialize_opt_bytes_as_hex};
13
14// Bitmap definitions
15
16/// SupportedDeviceCategory bitmap type
17pub type SupportedDeviceCategory = u8;
18
19/// Constants for SupportedDeviceCategory
20pub mod supporteddevicecategory {
21    /// Aggregators which support Fabric Synchronization may be commissioned.
22    pub const FABRIC_SYNCHRONIZATION: u8 = 0x01;
23}
24
25// Command encoders
26
27/// Encode RequestCommissioningApproval command (0x00)
28pub fn encode_request_commissioning_approval(request_id: u64, vendor_id: u16, product_id: u16, label: String) -> 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(vendor_id)).into(),
34        (2, tlv::TlvItemValueEnc::UInt16(product_id)).into(),
35        (3, tlv::TlvItemValueEnc::String(label)).into(),
36        ]),
37    };
38    Ok(tlv.encode()?)
39}
40
41/// Encode CommissionNode command (0x01)
42pub fn encode_commission_node(request_id: u64, response_timeout_seconds: u16) -> anyhow::Result<Vec<u8>> {
43    let tlv = tlv::TlvItemEnc {
44        tag: 0,
45        value: tlv::TlvItemValueEnc::StructInvisible(vec![
46        (0, tlv::TlvItemValueEnc::UInt64(request_id)).into(),
47        (1, tlv::TlvItemValueEnc::UInt16(response_timeout_seconds)).into(),
48        ]),
49    };
50    Ok(tlv.encode()?)
51}
52
53// Attribute decoders
54
55/// Decode SupportedDeviceCategories attribute (0x0000)
56pub fn decode_supported_device_categories(inp: &tlv::TlvItemValue) -> anyhow::Result<SupportedDeviceCategory> {
57    if let tlv::TlvItemValue::Int(v) = inp {
58        Ok(*v as u8)
59    } else {
60        Err(anyhow::anyhow!("Expected Integer"))
61    }
62}
63
64
65// JSON dispatcher function
66
67/// Decode attribute value and return as JSON string
68///
69/// # Parameters
70/// * `cluster_id` - The cluster identifier
71/// * `attribute_id` - The attribute identifier
72/// * `tlv_value` - The TLV value to decode
73///
74/// # Returns
75/// JSON string representation of the decoded value or error
76pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
77    // Verify this is the correct cluster
78    if cluster_id != 0x0751 {
79        return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0751, got {}\"}}", cluster_id);
80    }
81
82    match attribute_id {
83        0x0000 => {
84            match decode_supported_device_categories(tlv_value) {
85                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
86                Err(e) => format!("{{\"error\": \"{}\"}}", e),
87            }
88        }
89        _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
90    }
91}
92
93/// Get list of all attributes supported by this cluster
94///
95/// # Returns
96/// Vector of tuples containing (attribute_id, attribute_name)
97pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
98    vec![
99        (0x0000, "SupportedDeviceCategories"),
100    ]
101}
102
103#[derive(Debug, serde::Serialize)]
104pub struct ReverseOpenCommissioningWindow {
105    pub commissioning_timeout: Option<u16>,
106    #[serde(serialize_with = "serialize_opt_bytes_as_hex")]
107    pub pake_passcode_verifier: Option<Vec<u8>>,
108    pub discriminator: Option<u16>,
109    pub iterations: Option<u32>,
110    #[serde(serialize_with = "serialize_opt_bytes_as_hex")]
111    pub salt: Option<Vec<u8>>,
112}
113
114// Command response decoders
115
116/// Decode ReverseOpenCommissioningWindow command response (02)
117pub fn decode_reverse_open_commissioning_window(inp: &tlv::TlvItemValue) -> anyhow::Result<ReverseOpenCommissioningWindow> {
118    if let tlv::TlvItemValue::List(_fields) = inp {
119        let item = tlv::TlvItem { tag: 0, value: inp.clone() };
120        Ok(ReverseOpenCommissioningWindow {
121                commissioning_timeout: item.get_int(&[0]).map(|v| v as u16),
122                pake_passcode_verifier: item.get_octet_string_owned(&[1]),
123                discriminator: item.get_int(&[2]).map(|v| v as u16),
124                iterations: item.get_int(&[3]).map(|v| v as u32),
125                salt: item.get_octet_string_owned(&[4]),
126        })
127    } else {
128        Err(anyhow::anyhow!("Expected struct fields"))
129    }
130}
131
132#[derive(Debug, serde::Serialize)]
133pub struct CommissioningRequestResultEvent {
134    pub request_id: Option<u64>,
135    pub client_node_id: Option<u64>,
136    pub status_code: Option<u8>,
137}
138
139// Event decoders
140
141/// Decode CommissioningRequestResult event (0x00, priority: info)
142pub fn decode_commissioning_request_result_event(inp: &tlv::TlvItemValue) -> anyhow::Result<CommissioningRequestResultEvent> {
143    if let tlv::TlvItemValue::List(_fields) = inp {
144        let item = tlv::TlvItem { tag: 0, value: inp.clone() };
145        Ok(CommissioningRequestResultEvent {
146                                request_id: item.get_int(&[0]),
147                                client_node_id: item.get_int(&[1]),
148                                status_code: item.get_int(&[2]).map(|v| v as u8),
149        })
150    } else {
151        Err(anyhow::anyhow!("Expected struct fields"))
152    }
153}
154