matc/clusters/codec/
diagnostics_wifi.rs1use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11pub fn decode_bssid(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<Vec<u8>>> {
17 if let tlv::TlvItemValue::OctetString(v) = inp {
18 Ok(Some(v.clone()))
19 } else {
20 Ok(None)
21 }
22}
23
24pub fn decode_security_type(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<u8>> {
26 if let tlv::TlvItemValue::Int(v) = inp {
27 Ok(Some(*v as u8))
28 } else {
29 Ok(None)
30 }
31}
32
33pub fn decode_wifi_version(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<u8>> {
35 if let tlv::TlvItemValue::Int(v) = inp {
36 Ok(Some(*v as u8))
37 } else {
38 Ok(None)
39 }
40}
41
42pub fn decode_channel_number(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<u16>> {
44 if let tlv::TlvItemValue::Int(v) = inp {
45 Ok(Some(*v as u16))
46 } else {
47 Ok(None)
48 }
49}
50
51pub fn decode_rssi(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<i8>> {
53 if let tlv::TlvItemValue::Int(v) = inp {
54 Ok(Some(*v as i8))
55 } else {
56 Ok(None)
57 }
58}
59
60pub fn decode_beacon_lost_count(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<u32>> {
62 if let tlv::TlvItemValue::Int(v) = inp {
63 Ok(Some(*v as u32))
64 } else {
65 Ok(None)
66 }
67}
68
69pub fn decode_beacon_rx_count(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<u32>> {
71 if let tlv::TlvItemValue::Int(v) = inp {
72 Ok(Some(*v as u32))
73 } else {
74 Ok(None)
75 }
76}
77
78pub fn decode_packet_multicast_rx_count(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<u32>> {
80 if let tlv::TlvItemValue::Int(v) = inp {
81 Ok(Some(*v as u32))
82 } else {
83 Ok(None)
84 }
85}
86
87pub fn decode_packet_multicast_tx_count(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<u32>> {
89 if let tlv::TlvItemValue::Int(v) = inp {
90 Ok(Some(*v as u32))
91 } else {
92 Ok(None)
93 }
94}
95
96pub fn decode_packet_unicast_rx_count(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<u32>> {
98 if let tlv::TlvItemValue::Int(v) = inp {
99 Ok(Some(*v as u32))
100 } else {
101 Ok(None)
102 }
103}
104
105pub fn decode_packet_unicast_tx_count(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<u32>> {
107 if let tlv::TlvItemValue::Int(v) = inp {
108 Ok(Some(*v as u32))
109 } else {
110 Ok(None)
111 }
112}
113
114pub fn decode_current_max_rate(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<u64>> {
116 if let tlv::TlvItemValue::Int(v) = inp {
117 Ok(Some(*v))
118 } else {
119 Ok(None)
120 }
121}
122
123pub fn decode_overrun_count(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<u64>> {
125 if let tlv::TlvItemValue::Int(v) = inp {
126 Ok(Some(*v))
127 } else {
128 Ok(None)
129 }
130}
131
132
133pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
145 if cluster_id != 0x0036 {
147 return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0036, got {}\"}}", cluster_id);
148 }
149
150 match attribute_id {
151 0x0000 => {
152 match decode_bssid(tlv_value) {
153 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
154 Err(e) => format!("{{\"error\": \"{}\"}}", e),
155 }
156 }
157 0x0001 => {
158 match decode_security_type(tlv_value) {
159 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
160 Err(e) => format!("{{\"error\": \"{}\"}}", e),
161 }
162 }
163 0x0002 => {
164 match decode_wifi_version(tlv_value) {
165 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
166 Err(e) => format!("{{\"error\": \"{}\"}}", e),
167 }
168 }
169 0x0003 => {
170 match decode_channel_number(tlv_value) {
171 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
172 Err(e) => format!("{{\"error\": \"{}\"}}", e),
173 }
174 }
175 0x0004 => {
176 match decode_rssi(tlv_value) {
177 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
178 Err(e) => format!("{{\"error\": \"{}\"}}", e),
179 }
180 }
181 0x0005 => {
182 match decode_beacon_lost_count(tlv_value) {
183 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
184 Err(e) => format!("{{\"error\": \"{}\"}}", e),
185 }
186 }
187 0x0006 => {
188 match decode_beacon_rx_count(tlv_value) {
189 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
190 Err(e) => format!("{{\"error\": \"{}\"}}", e),
191 }
192 }
193 0x0007 => {
194 match decode_packet_multicast_rx_count(tlv_value) {
195 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
196 Err(e) => format!("{{\"error\": \"{}\"}}", e),
197 }
198 }
199 0x0008 => {
200 match decode_packet_multicast_tx_count(tlv_value) {
201 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
202 Err(e) => format!("{{\"error\": \"{}\"}}", e),
203 }
204 }
205 0x0009 => {
206 match decode_packet_unicast_rx_count(tlv_value) {
207 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
208 Err(e) => format!("{{\"error\": \"{}\"}}", e),
209 }
210 }
211 0x000A => {
212 match decode_packet_unicast_tx_count(tlv_value) {
213 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
214 Err(e) => format!("{{\"error\": \"{}\"}}", e),
215 }
216 }
217 0x000B => {
218 match decode_current_max_rate(tlv_value) {
219 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
220 Err(e) => format!("{{\"error\": \"{}\"}}", e),
221 }
222 }
223 0x000C => {
224 match decode_overrun_count(tlv_value) {
225 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
226 Err(e) => format!("{{\"error\": \"{}\"}}", e),
227 }
228 }
229 _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
230 }
231}
232
233pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
238 vec![
239 (0x0000, "BSSID"),
240 (0x0001, "SecurityType"),
241 (0x0002, "WiFiVersion"),
242 (0x0003, "ChannelNumber"),
243 (0x0004, "RSSI"),
244 (0x0005, "BeaconLostCount"),
245 (0x0006, "BeaconRxCount"),
246 (0x0007, "PacketMulticastRxCount"),
247 (0x0008, "PacketMulticastTxCount"),
248 (0x0009, "PacketUnicastRxCount"),
249 (0x000A, "PacketUnicastTxCount"),
250 (0x000B, "CurrentMaxRate"),
251 (0x000C, "OverrunCount"),
252 ]
253}
254