use crate::wifi::{elements, ELEMENT_VENDOR};
pub const OUI: [u8; 3] = [0x6A, 0x5C, 0x35];
pub const OUI_TYPE: u8 = 0x01;
#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct FrenchBeacon {
pub version: Option<u8>,
pub identifier: Option<[u8; 30]>,
pub serial: Option<([u8; 32], u8)>,
pub latitude: Option<f64>,
pub longitude: Option<f64>,
pub altitude: Option<i16>,
pub height: Option<i16>,
pub takeoff_latitude: Option<f64>,
pub takeoff_longitude: Option<f64>,
pub horizontal_speed: Option<u8>,
pub true_course: Option<u16>,
}
impl FrenchBeacon {
pub fn decode(tlvs: &[u8]) -> Self {
let mut out = FrenchBeacon::default();
let mut rest = tlvs;
while rest.len() >= 2 && rest.len() >= 2 + rest[1] as usize {
let (t, l) = (rest[0], rest[1] as usize);
let v = &rest[2..2 + l];
rest = &rest[2 + l..];
let be_i32 =
|| (l == 4).then(|| i32::from_be_bytes([v[0], v[1], v[2], v[3]]) as f64 * 1e-5);
let be_i16 = || (l == 2).then(|| i16::from_be_bytes([v[0], v[1]]));
match t {
1 if l == 1 => out.version = Some(v[0]),
2 if l == 30 => {
let mut id = [0u8; 30];
id.copy_from_slice(v);
out.identifier = Some(id);
}
3 => {
let mut s = [0u8; 32];
let n = l.min(32);
s[..n].copy_from_slice(&v[..n]);
out.serial = Some((s, n as u8));
}
4 => out.latitude = be_i32(),
5 => out.longitude = be_i32(),
6 => out.altitude = be_i16(),
7 => out.height = be_i16(),
8 => out.takeoff_latitude = be_i32(),
9 => out.takeoff_longitude = be_i32(),
10 if l == 1 => out.horizontal_speed = Some(v[0]),
11 if l == 2 => out.true_course = Some(u16::from_be_bytes([v[0], v[1]])),
_ => {}
}
}
out
}
pub fn identifier_str(&self) -> Option<&str> {
let id = self.identifier.as_ref()?;
let end = id.iter().position(|&b| b == 0).unwrap_or(id.len());
core::str::from_utf8(&id[..end]).ok()
}
pub fn serial_str(&self) -> Option<&str> {
let (s, n) = self.serial.as_ref()?;
core::str::from_utf8(&s[..*n as usize]).ok()
}
}
pub fn find_in_elements(ies: &[u8]) -> Option<FrenchBeacon> {
elements(ies).find_map(|(id, d)| {
(id == ELEMENT_VENDOR && d.len() >= 4 && d[..3] == OUI && d[3] == OUI_TYPE)
.then(|| FrenchBeacon::decode(&d[4..]))
})
}