matc/clusters/codec/
wifi_network_management.rs1use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11use crate::clusters::helpers::{serialize_opt_bytes_as_hex};
13
14pub 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
27pub 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
37pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
49 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
71pub 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
88pub 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