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
6#![allow(clippy::too_many_arguments)]
7
8use crate::tlv;
9use anyhow;
10use serde_json;
11
12
13// Import serialization helpers for octet strings
14use crate::clusters::helpers::{serialize_opt_bytes_as_hex};
15
16// Command encoders
17
18// Attribute decoders
19
20/// Decode SSID attribute (0x0000)
21pub fn decode_ssid(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<Vec<u8>>> {
22    if let tlv::TlvItemValue::OctetString(v) = inp {
23        Ok(Some(v.clone()))
24    } else {
25        Ok(None)
26    }
27}
28
29/// Decode PassphraseSurrogate attribute (0x0001)
30pub fn decode_passphrase_surrogate(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<u64>> {
31    if let tlv::TlvItemValue::Int(v) = inp {
32        Ok(Some(*v))
33    } else {
34        Ok(None)
35    }
36}
37
38
39// JSON dispatcher function
40
41/// Decode attribute value and return as JSON string
42///
43/// # Parameters
44/// * `cluster_id` - The cluster identifier
45/// * `attribute_id` - The attribute identifier
46/// * `tlv_value` - The TLV value to decode
47///
48/// # Returns
49/// JSON string representation of the decoded value or error
50pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
51    // Verify this is the correct cluster
52    if cluster_id != 0x0451 {
53        return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0451, got {}\"}}", cluster_id);
54    }
55
56    match attribute_id {
57        0x0000 => {
58            match decode_ssid(tlv_value) {
59                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
60                Err(e) => format!("{{\"error\": \"{}\"}}", e),
61            }
62        }
63        0x0001 => {
64            match decode_passphrase_surrogate(tlv_value) {
65                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
66                Err(e) => format!("{{\"error\": \"{}\"}}", e),
67            }
68        }
69        _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
70    }
71}
72
73/// Get list of all attributes supported by this cluster
74///
75/// # Returns
76/// Vector of tuples containing (attribute_id, attribute_name)
77pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
78    vec![
79        (0x0000, "SSID"),
80        (0x0001, "PassphraseSurrogate"),
81    ]
82}
83
84// Command listing
85
86pub fn get_command_list() -> Vec<(u32, &'static str)> {
87    vec![
88        (0x00, "NetworkPassphraseRequest"),
89    ]
90}
91
92pub fn get_command_name(cmd_id: u32) -> Option<&'static str> {
93    match cmd_id {
94        0x00 => Some("NetworkPassphraseRequest"),
95        _ => None,
96    }
97}
98
99pub fn get_command_schema(cmd_id: u32) -> Option<Vec<crate::clusters::codec::CommandField>> {
100    match cmd_id {
101        0x00 => Some(vec![]),
102        _ => None,
103    }
104}
105
106pub fn encode_command_json(cmd_id: u32, _args: &serde_json::Value) -> anyhow::Result<Vec<u8>> {
107    match cmd_id {
108        0x00 => Ok(vec![]),
109        _ => Err(anyhow::anyhow!("unknown command ID: 0x{:02X}", cmd_id)),
110    }
111}
112
113#[derive(Debug, serde::Serialize)]
114pub struct NetworkPassphraseResponse {
115    #[serde(serialize_with = "serialize_opt_bytes_as_hex")]
116    pub passphrase: Option<Vec<u8>>,
117}
118
119// Command response decoders
120
121/// Decode NetworkPassphraseResponse command response (01)
122pub fn decode_network_passphrase_response(inp: &tlv::TlvItemValue) -> anyhow::Result<NetworkPassphraseResponse> {
123    if let tlv::TlvItemValue::List(_fields) = inp {
124        let item = tlv::TlvItem { tag: 0, value: inp.clone() };
125        Ok(NetworkPassphraseResponse {
126                passphrase: item.get_octet_string_owned(&[0]),
127        })
128    } else {
129        Err(anyhow::anyhow!("Expected struct fields"))
130    }
131}
132
133// Typed facade (invokes + reads)
134
135/// Invoke `NetworkPassphraseRequest` command on cluster `Wi-Fi Network Management`.
136pub async fn network_passphrase_request(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<NetworkPassphraseResponse> {
137    let tlv = conn.invoke_request2(endpoint, crate::clusters::defs::CLUSTER_ID_WI_FI_NETWORK_MANAGEMENT, crate::clusters::defs::CLUSTER_WI_FI_NETWORK_MANAGEMENT_CMD_ID_NETWORKPASSPHRASEREQUEST, &[]).await?;
138    decode_network_passphrase_response(&tlv)
139}
140
141/// Read `SSID` attribute from cluster `Wi-Fi Network Management`.
142pub async fn read_ssid(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Option<Vec<u8>>> {
143    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_WI_FI_NETWORK_MANAGEMENT, crate::clusters::defs::CLUSTER_WI_FI_NETWORK_MANAGEMENT_ATTR_ID_SSID).await?;
144    decode_ssid(&tlv)
145}
146
147/// Read `PassphraseSurrogate` attribute from cluster `Wi-Fi Network Management`.
148pub async fn read_passphrase_surrogate(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Option<u64>> {
149    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_WI_FI_NETWORK_MANAGEMENT, crate::clusters::defs::CLUSTER_WI_FI_NETWORK_MANAGEMENT_ATTR_ID_PASSPHRASESURROGATE).await?;
150    decode_passphrase_surrogate(&tlv)
151}
152