|
| 1 | +//! Link-status packet decoder (issue #1542 ask 1: BSSID telemetry). |
| 2 | +//! |
| 3 | +//! The associated AP *is* the sensing-link geometry: when a station roams, |
| 4 | +//! every downstream consumer of that node's CSI is silently looking at a |
| 5 | +//! different Fresnel volume. Field data on #1542 shows published RSSI cannot |
| 6 | +//! reveal the change (−47.7 vs −48.2 dBm across two completely different |
| 7 | +//! link geometries), so the BSSID itself is the only observable. |
| 8 | +//! |
| 9 | +//! Emitted by the firmware on the same UDP socket as ADR-018 CSI frames and |
| 10 | +//! ADR-110 sync packets, distinguished by leading magic `0xC511_A111` (next |
| 11 | +//! value in the ADR-110 auxiliary-packet family). Low rate: every |
| 12 | +//! `CONFIG_LINK_STATUS_EVERY_N_FRAMES` CSI callbacks (default 600 ≈ 30 s at |
| 13 | +//! 20 Hz) plus one immediate emission whenever the BSSID observed via |
| 14 | +//! `esp_wifi_sta_get_ap_info()` differs from the previously reported one — |
| 15 | +//! so a roam is visible within one CSI callback, not one cadence period. |
| 16 | +//! |
| 17 | +//! Wire format (32 bytes, little-endian, mirrors the sync-packet layout): |
| 18 | +//! ```text |
| 19 | +//! [0..3] magic 0xC511A111 (LE u32) |
| 20 | +//! [4] node_id |
| 21 | +//! [5] proto_ver (currently 0x01) |
| 22 | +//! [6] flags: bit 0 = ap_info_valid (esp_wifi_sta_get_ap_info() == ESP_OK) |
| 23 | +//! [7] primary channel (0 when ap_info_valid = 0) |
| 24 | +//! [8..13] bssid[6] (all-zero when ap_info_valid = 0) |
| 25 | +//! [14] AP RSSI as seen by the station, dBm (i8; 0 when invalid) |
| 26 | +//! [15] reserved |
| 27 | +//! [16..19] reassoc_count (LE u32) — WIFI_EVENT_STA_CONNECTED events since boot |
| 28 | +//! [20..31] reserved |
| 29 | +//! ``` |
| 30 | +
|
| 31 | +use serde::{Deserialize, Serialize}; |
| 32 | + |
| 33 | +use crate::error::ParseError; |
| 34 | + |
| 35 | +/// Magic constant in the first 4 little-endian bytes of every link-status packet. |
| 36 | +pub const LINK_PACKET_MAGIC: u32 = 0xC511_A111; |
| 37 | +/// Total wire size of a link-status packet. |
| 38 | +pub const LINK_PACKET_SIZE: usize = 32; |
| 39 | +/// Wire protocol version currently emitted by firmware. |
| 40 | +pub const LINK_PACKET_PROTO_VER: u8 = 0x01; |
| 41 | + |
| 42 | +/// Decoded #1542 link-status packet. |
| 43 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] |
| 44 | +pub struct LinkStatusPacket { |
| 45 | + pub node_id: u8, |
| 46 | + pub proto_ver: u8, |
| 47 | + /// False when the firmware's `esp_wifi_sta_get_ap_info()` call failed |
| 48 | + /// (not associated); `bssid`/`channel`/`ap_rssi_dbm` are zero then. |
| 49 | + pub ap_info_valid: bool, |
| 50 | + /// Primary channel of the associated AP. |
| 51 | + pub channel: u8, |
| 52 | + /// BSSID of the associated AP — the sensing link's far endpoint identity. |
| 53 | + pub bssid: [u8; 6], |
| 54 | + /// RSSI of the AP as measured by the station (dBm). Complements the |
| 55 | + /// frame-level RSSI already in ADR-018 headers; comes from the same |
| 56 | + /// `wifi_ap_record_t` read as the BSSID. |
| 57 | + pub ap_rssi_dbm: i8, |
| 58 | + /// Count of `WIFI_EVENT_STA_CONNECTED` events since boot. Monotonic per |
| 59 | + /// boot; a host observing an increment without a reboot marker knows a |
| 60 | + /// re-association happened even if the BSSID ended up unchanged. |
| 61 | + pub reassoc_count: u32, |
| 62 | +} |
| 63 | + |
| 64 | +impl LinkStatusPacket { |
| 65 | + /// Decode a 32-byte link-status packet. Host should dispatch on the |
| 66 | + /// leading magic before calling (same convention as `SyncPacket`). |
| 67 | + pub fn from_bytes(buf: &[u8]) -> Result<Self, ParseError> { |
| 68 | + if buf.len() < LINK_PACKET_SIZE { |
| 69 | + return Err(ParseError::InsufficientData { |
| 70 | + needed: LINK_PACKET_SIZE, |
| 71 | + got: buf.len(), |
| 72 | + }); |
| 73 | + } |
| 74 | + let magic = u32::from_le_bytes(buf[0..4].try_into().unwrap()); |
| 75 | + if magic != LINK_PACKET_MAGIC { |
| 76 | + return Err(ParseError::InvalidMagic { expected: LINK_PACKET_MAGIC, got: magic }); |
| 77 | + } |
| 78 | + let node_id = buf[4]; |
| 79 | + let proto_ver = buf[5]; |
| 80 | + let ap_info_valid = (buf[6] & 0x01) != 0; |
| 81 | + let channel = buf[7]; |
| 82 | + let mut bssid = [0u8; 6]; |
| 83 | + bssid.copy_from_slice(&buf[8..14]); |
| 84 | + let ap_rssi_dbm = buf[14] as i8; |
| 85 | + // buf[15] reserved |
| 86 | + let reassoc_count = u32::from_le_bytes(buf[16..20].try_into().unwrap()); |
| 87 | + // buf[20..32] reserved |
| 88 | + Ok(Self { |
| 89 | + node_id, |
| 90 | + proto_ver, |
| 91 | + ap_info_valid, |
| 92 | + channel, |
| 93 | + bssid, |
| 94 | + ap_rssi_dbm, |
| 95 | + reassoc_count, |
| 96 | + }) |
| 97 | + } |
| 98 | + |
| 99 | + /// Serialize back to wire bytes (32 bytes, little-endian). |
| 100 | + pub fn to_bytes(&self) -> [u8; LINK_PACKET_SIZE] { |
| 101 | + let mut out = [0u8; LINK_PACKET_SIZE]; |
| 102 | + out[0..4].copy_from_slice(&LINK_PACKET_MAGIC.to_le_bytes()); |
| 103 | + out[4] = self.node_id; |
| 104 | + out[5] = self.proto_ver; |
| 105 | + out[6] = if self.ap_info_valid { 0x01 } else { 0x00 }; |
| 106 | + out[7] = self.channel; |
| 107 | + out[8..14].copy_from_slice(&self.bssid); |
| 108 | + out[14] = self.ap_rssi_dbm as u8; |
| 109 | + // out[15] reserved zero |
| 110 | + out[16..20].copy_from_slice(&self.reassoc_count.to_le_bytes()); |
| 111 | + // out[20..32] reserved zero |
| 112 | + out |
| 113 | + } |
| 114 | + |
| 115 | + /// Canonical lowercase colon-separated BSSID string (`aa:bb:cc:dd:ee:ff`) |
| 116 | + /// for JSON/MQTT surfaces. |
| 117 | + pub fn bssid_string(&self) -> String { |
| 118 | + let b = &self.bssid; |
| 119 | + format!( |
| 120 | + "{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}", |
| 121 | + b[0], b[1], b[2], b[3], b[4], b[5] |
| 122 | + ) |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +#[cfg(test)] |
| 127 | +mod tests { |
| 128 | + use super::*; |
| 129 | + |
| 130 | + fn typical() -> LinkStatusPacket { |
| 131 | + LinkStatusPacket { |
| 132 | + node_id: 3, |
| 133 | + proto_ver: 1, |
| 134 | + ap_info_valid: true, |
| 135 | + channel: 6, |
| 136 | + bssid: [0x6c, 0xae, 0xf6, 0xb6, 0x52, 0xc7], |
| 137 | + ap_rssi_dbm: -48, |
| 138 | + reassoc_count: 2, |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + #[test] |
| 143 | + fn typical_packet_roundtrips() { |
| 144 | + let pkt = typical(); |
| 145 | + let wire = pkt.to_bytes(); |
| 146 | + let decoded = LinkStatusPacket::from_bytes(&wire).unwrap(); |
| 147 | + assert_eq!(decoded, pkt); |
| 148 | + assert_eq!(decoded.bssid_string(), "6c:ae:f6:b6:52:c7"); |
| 149 | + } |
| 150 | + |
| 151 | + #[test] |
| 152 | + fn unassociated_packet_roundtrips_with_zero_fields() { |
| 153 | + let pkt = LinkStatusPacket { |
| 154 | + node_id: 5, |
| 155 | + proto_ver: 1, |
| 156 | + ap_info_valid: false, |
| 157 | + channel: 0, |
| 158 | + bssid: [0; 6], |
| 159 | + ap_rssi_dbm: 0, |
| 160 | + reassoc_count: 7, |
| 161 | + }; |
| 162 | + let decoded = LinkStatusPacket::from_bytes(&pkt.to_bytes()).unwrap(); |
| 163 | + assert_eq!(decoded, pkt); |
| 164 | + assert!(!decoded.ap_info_valid); |
| 165 | + assert_eq!(decoded.bssid_string(), "00:00:00:00:00:00"); |
| 166 | + } |
| 167 | + |
| 168 | + #[test] |
| 169 | + fn magic_mismatch_is_typed_error() { |
| 170 | + let mut wire = typical().to_bytes(); |
| 171 | + wire[0] = 0x01; |
| 172 | + match LinkStatusPacket::from_bytes(&wire).unwrap_err() { |
| 173 | + ParseError::InvalidMagic { got, .. } => assert_ne!(got, LINK_PACKET_MAGIC), |
| 174 | + other => panic!("expected InvalidMagic, got {other:?}"), |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + #[test] |
| 179 | + fn short_packet_is_typed_error() { |
| 180 | + let wire = [0u8; 16]; |
| 181 | + match LinkStatusPacket::from_bytes(&wire).unwrap_err() { |
| 182 | + ParseError::InsufficientData { needed, got } => { |
| 183 | + assert_eq!(needed, LINK_PACKET_SIZE); |
| 184 | + assert_eq!(got, 16); |
| 185 | + } |
| 186 | + other => panic!("expected InsufficientData, got {other:?}"), |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + /// Hosts dispatch CSI vs sync vs link purely on the leading u32; the |
| 191 | + /// three magics must never collide. |
| 192 | + #[test] |
| 193 | + fn link_magic_is_distinct_from_sync_and_csi() { |
| 194 | + assert_ne!(LINK_PACKET_MAGIC, crate::sync_packet::SYNC_PACKET_MAGIC); |
| 195 | + assert_ne!(LINK_PACKET_MAGIC, crate::esp32_parser::ESP32_CSI_MAGIC); |
| 196 | + } |
| 197 | + |
| 198 | + /// Canonical wire pin (same convention as the sync packet's |
| 199 | + /// `canonical_wire_bytes_match_python_decoder`): if this hex stops |
| 200 | + /// matching, a decoder drifted from the wire. |
| 201 | + #[test] |
| 202 | + fn canonical_wire_bytes_pin() { |
| 203 | + let canonical: [u8; 32] = [ |
| 204 | + 0x11, 0xa1, 0x11, 0xc5, // magic 0xC511A111 (LE u32) |
| 205 | + 0x03, // node_id = 3 |
| 206 | + 0x01, // proto_ver = 1 |
| 207 | + 0x01, // flags: ap_info_valid |
| 208 | + 0x06, // channel 6 |
| 209 | + 0x6c, 0xae, 0xf6, 0xb6, 0x52, 0xc7, // bssid |
| 210 | + 0xd0, // ap_rssi = -48 dBm (i8) |
| 211 | + 0x00, // reserved |
| 212 | + 0x02, 0x00, 0x00, 0x00, // reassoc_count = 2 |
| 213 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 214 | + ]; |
| 215 | + let decoded = LinkStatusPacket::from_bytes(&canonical).unwrap(); |
| 216 | + assert_eq!(decoded, typical()); |
| 217 | + assert_eq!(decoded.to_bytes(), canonical, |
| 218 | + "to_bytes drifted from the canonical pin"); |
| 219 | + } |
| 220 | +} |
0 commit comments