use super::payload::{i32_at, u8_at, u16_at, u32_at, u64_at};
pub const ID: u32 = 24;
pub const CRC_EXTRA: u8 = 24;
const LEN: usize = 52;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct GpsRawInt {
pub time_usec: u64,
pub fix_type: u8,
pub lat: i32,
pub lon: i32,
pub alt: i32,
pub eph: u16,
pub epv: u16,
pub vel: u16,
pub cog: u16,
pub satellites_visible: u8,
pub alt_ellipsoid: i32,
pub h_acc: u32,
pub v_acc: u32,
pub vel_acc: u32,
pub hdg_acc: u32,
pub yaw: u16,
}
impl GpsRawInt {
pub(crate) fn decode(payload: &[u8]) -> Self {
Self {
time_usec: u64_at(payload, 0),
lat: i32_at(payload, 8),
lon: i32_at(payload, 12),
alt: i32_at(payload, 16),
eph: u16_at(payload, 20),
epv: u16_at(payload, 22),
vel: u16_at(payload, 24),
cog: u16_at(payload, 26),
fix_type: u8_at(payload, 28),
satellites_visible: u8_at(payload, 29),
alt_ellipsoid: i32_at(payload, 30),
h_acc: u32_at(payload, 34),
v_acc: u32_at(payload, 38),
vel_acc: u32_at(payload, 42),
hdg_acc: u32_at(payload, 46),
yaw: u16_at(payload, 50),
}
}
pub(crate) fn encode(&self) -> [u8; LEN] {
let mut buf = [0u8; LEN];
buf[0..8].copy_from_slice(&self.time_usec.to_le_bytes());
buf[8..12].copy_from_slice(&self.lat.to_le_bytes());
buf[12..16].copy_from_slice(&self.lon.to_le_bytes());
buf[16..20].copy_from_slice(&self.alt.to_le_bytes());
buf[20..22].copy_from_slice(&self.eph.to_le_bytes());
buf[22..24].copy_from_slice(&self.epv.to_le_bytes());
buf[24..26].copy_from_slice(&self.vel.to_le_bytes());
buf[26..28].copy_from_slice(&self.cog.to_le_bytes());
buf[28] = self.fix_type;
buf[29] = self.satellites_visible;
buf[30..34].copy_from_slice(&self.alt_ellipsoid.to_le_bytes());
buf[34..38].copy_from_slice(&self.h_acc.to_le_bytes());
buf[38..42].copy_from_slice(&self.v_acc.to_le_bytes());
buf[42..46].copy_from_slice(&self.vel_acc.to_le_bytes());
buf[46..50].copy_from_slice(&self.hdg_acc.to_le_bytes());
buf[50..52].copy_from_slice(&self.yaw.to_le_bytes());
buf
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn decodes_a_real_captured_frame() {
let hex = "15cd5b07000000004a52401c44f41705f049020064009600f4012823030ce44b0200e8030000dc050000c80000002c01";
let mut payload = [0u8; 48];
for (i, chunk) in hex.as_bytes().chunks(2).enumerate() {
payload[i] = u8::from_str_radix(core::str::from_utf8(chunk).unwrap(), 16).unwrap();
}
let g = GpsRawInt::decode(&payload);
assert_eq!(g.time_usec, 123_456_789);
assert_eq!(g.fix_type, 3);
assert_eq!(g.lat, 473_977_418);
assert_eq!(g.lon, 85_455_940);
assert_eq!(g.alt, 150_000);
assert_eq!(g.satellites_visible, 12);
assert_eq!(g.hdg_acc, 300);
assert_eq!(g.yaw, 0); }
#[test]
fn round_trips_through_decode() {
let g = GpsRawInt {
time_usec: u64::MAX,
fix_type: 4,
lat: -1,
lon: i32::MIN,
alt: 12345,
eph: 1,
epv: 2,
vel: 3,
cog: 4,
satellites_visible: 20,
alt_ellipsoid: -5,
h_acc: 6,
v_acc: 7,
vel_acc: 8,
hdg_acc: 9,
yaw: 36000,
};
assert_eq!(GpsRawInt::decode(&g.encode()), g);
}
}