matc/clusters/codec/
wifi_network_management.rs

1//! Matter TLV encoders and decoders for Wi-Fi Network Management Cluster
2//! Cluster ID: 0x0451
3//!
4//! This file is automatically generated from WiFiNetworkManagement.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// Command encoders
15
16// Attribute decoders
17
18/// Decode SSID attribute (0x0000)
19pub fn decode_ssid(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<Vec<u8>>> {
20    if let tlv::TlvItemValue::OctetString(v) = inp {
21        Ok(Some(v.clone()))
22    } else {
23        Ok(None)
24    }
25}
26
27/// Decode PassphraseSurrogate attribute (0x0001)
28pub fn decode_passphrase_surrogate(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<u64>> {
29    if let tlv::TlvItemValue::Int(v) = inp {
30        Ok(Some(*v))
31    } else {
32        Ok(None)
33    }
34}
35
36
37// JSON dispatcher function
38
39/// Decode attribute value and return as JSON string
40///
41/// # Parameters
42/// * `cluster_id` - The cluster identifier
43/// * `attribute_id` - The attribute identifier
44/// * `tlv_value` - The TLV value to decode
45///
46/// # Returns
47/// JSON string representation of the decoded value or error
48pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
49    // Verify this is the correct cluster
50    if cluster_id != 0x0451 {
51        return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0451, got {}\"}}", cluster_id);
52    }
53
54    match attribute_id {
55        0x0000 => {
56            match decode_ssid(tlv_value) {
57                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
58                Err(e) => format!("{{\"error\": \"{}\"}}", e),
59            }
60        }
61        0x0001 => {
62            match decode_passphrase_surrogate(tlv_value) {
63                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
64                Err(e) => format!("{{\"error\": \"{}\"}}", e),
65            }
66        }
67        _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
68    }
69}
70
71/// Get list of all attributes supported by this cluster
72///
73/// # Returns
74/// Vector of tuples containing (attribute_id, attribute_name)
75pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
76    vec![
77        (0x0000, "SSID"),
78        (0x0001, "PassphraseSurrogate"),
79    ]
80}
81
82#[derive(Debug, serde::Serialize)]
83pub struct NetworkPassphraseResponse {
84    #[serde(serialize_with = "serialize_opt_bytes_as_hex")]
85    pub passphrase: Option<Vec<u8>>,
86}
87
88// Command response decoders
89
90/// Decode NetworkPassphraseResponse command response (01)
91pub fn decode_network_passphrase_response(inp: &tlv::TlvItemValue) -> anyhow::Result<NetworkPassphraseResponse> {
92    if let tlv::TlvItemValue::List(_fields) = inp {
93        let item = tlv::TlvItem { tag: 0, value: inp.clone() };
94        Ok(NetworkPassphraseResponse {
95                passphrase: item.get_octet_string_owned(&[0]),
96        })
97    } else {
98        Err(anyhow::anyhow!("Expected struct fields"))
99    }
100}
101