Skip to content

Commit

Permalink
wip: Update deku
Browse files Browse the repository at this point in the history
  • Loading branch information
wcampbell0x2a committed Aug 18, 2023
1 parent 29b37de commit 3d2173b
Show file tree
Hide file tree
Showing 8 changed files with 560 additions and 532 deletions.
720 changes: 363 additions & 357 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion libadsb_deku/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ std = ["deku/std", "alloc"]
alloc = ["deku/alloc"]

[dependencies]
deku = { version = "0.16", default-features = false }
deku = { git = "/~https://github.com/sharksforarms/deku", branch = "impl-reader", default-features = false, features = ["const_generics", "read_cache"] }
serde = { version = "1.0", features = ["derive"], optional = true }
libm = "0.2.2"
env_logger = "*"
log = "*"

[dev-dependencies]
hex = "0.4"
Expand Down
66 changes: 40 additions & 26 deletions libadsb_deku/src/adsb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use core::{
result::Result::Ok,
stringify, write, writeln,
};
use std::io::Read;
#[cfg(not(feature = "alloc"))]
use std::{fmt, i64};

Expand Down Expand Up @@ -65,7 +66,7 @@ impl ADSB {
#[deku(type = "u8", bits = "5")]
pub enum ME {
#[deku(id_pat = "9..=18")]
AirbornePositionBaroAltitude(Altitude),
AirbornePositionBaroAltitude { id: u8, value: Altitude },

#[deku(id = "19")]
AirborneVelocity(AirborneVelocity),
Expand All @@ -74,22 +75,22 @@ pub enum ME {
NoPosition([u8; 6]),

#[deku(id_pat = "1..=4")]
AircraftIdentification(Identification),
AircraftIdentification { id: u8, value: Identification },

#[deku(id_pat = "5..=8")]
SurfacePosition(SurfacePosition),
SurfacePosition { id: u8, value: SurfacePosition },

#[deku(id_pat = "20..=22")]
AirbornePositionGNSSAltitude(Altitude),
AirbornePositionGNSSAltitude { id: u8, value: Altitude },

#[deku(id = "23")]
Reserved0([u8; 6]),

#[deku(id_pat = "24")]
#[deku(id = "24")]
SurfaceSystemStatus([u8; 6]),

#[deku(id_pat = "25..=27")]
Reserved1([u8; 6]),
Reserved1 { id: u8, value: [u8; 6] },

#[deku(id = "28")]
AircraftStatus(AircraftStatus),
Expand Down Expand Up @@ -124,21 +125,27 @@ impl ME {
writeln!(f, " Address: {icao} {address_type}")?;
writeln!(f, " Air/Ground: {capability}")?;
},
ME::AircraftIdentification(Identification { tc, ca, cn }) => {
ME::AircraftIdentification {
id,
value: Identification { ca, cn, .. },
} => {
writeln!(
f,
" Extended Squitter{transponder}Aircraft identification and category"
)?;
writeln!(f, " Address: {icao} {address_type}")?;
writeln!(f, " Air/Ground: {capability}")?;
writeln!(f, " Ident: {cn}")?;
writeln!(f, " Category: {tc}{ca}")?;
writeln!(f, " Category: {ca}")?;
},
ME::SurfacePosition(..) => {
ME::SurfacePosition { .. } => {
writeln!(f, " Extended Squitter{transponder}Surface position")?;
writeln!(f, " Address: {icao} {address_type}")?;
},
ME::AirbornePositionBaroAltitude(altitude) => {
ME::AirbornePositionBaroAltitude {
id,
value: altitude,
} => {
writeln!(
f,
" Extended Squitter{transponder}Airborne position (barometric altitude)"
Expand Down Expand Up @@ -204,15 +211,18 @@ impl ME {
writeln!(f, " Address: {icao} {address_type}")?;
},
},
ME::AirbornePositionGNSSAltitude(altitude) => {
ME::AirbornePositionGNSSAltitude {
id,
value: altitude,
} => {
writeln!(
f,
" Extended Squitter{transponder}Airborne position (GNSS altitude)",
)?;
writeln!(f, " Address: {icao} {address_type}")?;
write!(f, "{altitude}")?;
},
ME::Reserved0(_) | ME::Reserved1(_) => {
ME::Reserved0(_) | ME::Reserved1 { .. } => {
writeln!(f, " Extended Squitter{transponder}Unknown")?;
writeln!(f, " Address: {icao} {address_type}")?;
writeln!(f, " Air/Ground: {capability}")?;
Expand Down Expand Up @@ -300,7 +310,7 @@ impl ME {
writeln!(f, " Air/Ground: {capability}")?;
write!(f, " Aircraft Operational Status:\n {opstatus_surface}")?;
},
ME::AircraftOperationStatus(OperationStatus::Reserved(..)) => {
ME::AircraftOperationStatus(OperationStatus::Reserved { .. }) => {
writeln!(
f,
" Extended Squitter{transponder}Aircraft operational status (reserved)",
Expand Down Expand Up @@ -354,7 +364,12 @@ pub enum OperationStatus {
Surface(OperationStatusSurface),

#[deku(id_pat = "2..=7")]
Reserved(#[deku(bits = "5")] u8, [u8; 5]),
Reserved {
id: u8,
#[deku(bits = "5")]
more: u8,
value: [u8; 5],
},
}

/// [`ME::AircraftOperationStatus`] && [`OperationStatus`] == 0
Expand Down Expand Up @@ -826,13 +841,12 @@ pub struct OperationCodeSurface {
#[derive(Debug, PartialEq, Eq, DekuRead, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Identification {
pub tc: TypeCoding,

//pub tc: TypeCoding,
#[deku(bits = "3")]
pub ca: u8,

/// N-Number / Tail Number
#[deku(reader = "aircraft_identification_read(deku::rest)")]
#[deku(reader = "aircraft_identification_read(deku::container)")]
pub cn: String,
}

Expand Down Expand Up @@ -995,26 +1009,26 @@ pub enum AirborneVelocityType {
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct AirborneVelocitySubFields {
pub dew: DirectionEW,
#[deku(reader = "Self::read_v(deku::rest, t)")]
#[deku(reader = "Self::read_v(deku::container, t)")]
pub vew: u16,
pub dns: DirectionNS,
#[deku(reader = "Self::read_v(deku::rest, t)")]
#[deku(reader = "Self::read_v(deku::container, t)")]
pub vns: u16,
}

impl AirborneVelocitySubFields {
fn read_v(
rest: &BitSlice<u8, Msb0>,
fn read_v<R: Read>(
container: &mut Container<R>,
t: AirborneVelocityType,
) -> result::Result<(&BitSlice<u8, Msb0>, u16), DekuError> {
) -> result::Result<u16, DekuError> {
match t {
AirborneVelocityType::Subsonic => {
u16::read(rest, (deku::ctx::Endian::Big, deku::ctx::BitSize(10)))
.map(|(rest, value)| (rest, value - 1))
u16::from_reader(container, (deku::ctx::Endian::Big, deku::ctx::BitSize(10)))
.map(|value| value - 1)
},
AirborneVelocityType::Supersonic => {
u16::read(rest, (deku::ctx::Endian::Big, deku::ctx::BitSize(10)))
.map(|(rest, value)| (rest, 4 * (value - 1)))
u16::from_reader(container, (deku::ctx::Endian::Big, deku::ctx::BitSize(10)))
.map(|value| 4 * (value - 1))
},
}
}
Expand Down
10 changes: 6 additions & 4 deletions libadsb_deku/src/bds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use deku::prelude::*;
use crate::aircraft_identification_read;

#[derive(Debug, PartialEq, Eq, DekuRead, Clone)]
#[deku(type = "u8", bits = "8")]
#[deku(type = "u8")]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum BDS {
/// (1, 0) Table A-2-16
Expand All @@ -26,10 +26,12 @@ pub enum BDS {

/// (2, 0) Table A-2-32
#[deku(id = "0x20")]
AircraftIdentification(#[deku(reader = "aircraft_identification_read(deku::rest)")] String),
AircraftIdentification(
#[deku(reader = "aircraft_identification_read(deku::container)")] String,
),

#[deku(id_pat = "_")]
Unknown([u8; 6]),
Unknown { id: u8, value: [u8; 5] },
}

impl fmt::Display for BDS {
Expand All @@ -45,7 +47,7 @@ impl fmt::Display for BDS {
Self::DataLinkCapability(_) => {
writeln!(f, "Comm-B format: BDS1,0 Datalink capabilities")?;
},
Self::Unknown(_) => {
Self::Unknown { id, value } => {
writeln!(f, "Comm-B format: unknown format")?;
},
}
Expand Down
2 changes: 2 additions & 0 deletions libadsb_deku/src/crc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use core::{
result,
result::Result::{Err, Ok},
};
use std::io::Read;

use deku::prelude::*;

Expand Down Expand Up @@ -271,6 +272,7 @@ pub fn modes_checksum(message: &[u8], bits: usize) -> result::Result<u32, DekuEr
let mut rem: u32 = 0;
let n = bits / 8;

log::trace!("{message:02x?}");
if (n < 3) || (message.len() < n) {
return Err(DekuError::Incomplete(NeedSize::new(4)));
}
Expand Down
Loading

0 comments on commit 3d2173b

Please sign in to comment.