matc/clusters/codec/
wifi_network_management.rs1#![allow(clippy::too_many_arguments)]
7
8use crate::tlv;
9use anyhow;
10use serde_json;
11
12
13use crate::clusters::helpers::{serialize_opt_bytes_as_hex};
15
16pub 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
29pub 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
39pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
51 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
73pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
78 vec![
79 (0x0000, "SSID"),
80 (0x0001, "PassphraseSurrogate"),
81 ]
82}
83
84pub 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
119pub 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
133pub 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
141pub 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
147pub 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