Skip to content

Commit

Permalink
Latest Update
Browse files Browse the repository at this point in the history
  • Loading branch information
wcampbell0x2a committed Dec 17, 2023
1 parent 164d7b8 commit 9ebea61
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 46 deletions.
4 changes: 2 additions & 2 deletions libadsb_deku/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mfackage]
[package]
name = "adsb_deku"
description = "Decoder for ADS-B(Automatic Depedent Surveillance-Broadcast) - written with deku"
repository = "/~https://github.com/wcampbel0x2a/adsb_deku"
Expand All @@ -17,7 +17,7 @@ std = ["deku/std", "alloc"]
alloc = ["deku/alloc"]

[dependencies]
deku = { git = "/~https://github.com/sharksforarms/deku", branch = "impl-reader", default-features = false }
deku = { git = "/~https://github.com/sharksforarms/deku", branch = "impl-writer", default-features = false }
serde = { version = "1.0", features = ["derive"], optional = true }
libm = "0.2.8"
env_logger = "*"
Expand Down
18 changes: 9 additions & 9 deletions libadsb_deku/src/adsb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use core::{
result::Result::Ok,
stringify, write, writeln,
};
use deku::acid_io::Read;
use deku::no_std_io::Read;
#[cfg(not(feature = "alloc"))]
use std::{fmt, i64};

Expand Down Expand Up @@ -123,18 +123,18 @@ impl ME {
writeln!(f, " Address: {icao} {address_type}")?;
writeln!(f, " Air/Ground: {capability}")?;
}
ME::AircraftIdentification(Identification { tc, ca, cn }) => {
ME::AircraftIdentification { tc, 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}")?;
}
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 @@ -197,12 +197,12 @@ 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 @@ -273,7 +273,7 @@ impl ME {
writeln!(f, " Address: {icao} {address_type}")?;
writeln!(f, " Air/Ground: {capability}")?;
write!(f, " Aircraft Operational Status:\n {opstatus_surface}")?;
},
}
ME::AircraftOperationStatus(OperationStatus::Reserved { .. }) => {
writeln!(
f,
Expand Down Expand Up @@ -970,11 +970,11 @@ impl AirborneVelocitySubFields {
AirborneVelocityType::Subsonic => {
u16::from_reader_with_ctx(reader, (deku::ctx::Endian::Big, deku::ctx::BitSize(10)))
.map(|value| value - 1)
},
}
AirborneVelocityType::Supersonic => {
u16::from_reader_with_ctx(reader, (deku::ctx::Endian::Big, deku::ctx::BitSize(10)))
.map(|value| 4 * (value - 1))
},
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion libadsb_deku/src/bds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl fmt::Display for BDS {
}
Self::DataLinkCapability(_) => {
writeln!(f, "Comm-B format: BDS1,0 Datalink capabilities")?;
},
}
Self::Unknown { id: _, value: _ } => {
writeln!(f, "Comm-B format: unknown format")?;
}
Expand Down
42 changes: 25 additions & 17 deletions libadsb_deku/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,24 @@ mod readme_test {}

use adsb::{ControlField, ADSB};
use bds::BDS;
use deku::acid_io::Read;
use deku::no_std_io::Read;
use deku::prelude::*;

/// Every read to this struct will be saved into an internal cache. This is to keep the cache
/// around for the crc without reading from the buffer twice!
struct ReaderCrc<R: Read> {
reader: R,
cache: Vec<u8>,
}

impl<R: Read> Read for ReaderCrc<R> {
fn read(&mut self, buf: &mut [u8]) -> deku::no_std_io::Result<usize> {
let n = self.reader.read(buf);
self.cache.extend_from_slice(buf);
n
}
}

/// Downlink ADS-B Packet
#[derive(Debug, PartialEq, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
Expand All @@ -164,29 +179,23 @@ pub struct Frame {
}

impl Frame {
pub fn from_reader<R>(mut r: R) -> Result<Frame, DekuError>
pub fn from_reader<R>(r: R) -> Result<Frame, DekuError>
where
R: Read,
{
let mut reader = Reader::new(&mut r);
reader.enable_read_cache();
let mut reader_crc = ReaderCrc { reader: r, cache: vec![] };
let mut reader = Reader::new(&mut reader_crc);
let df = DF::from_reader_with_ctx(&mut reader, ())?;

let mut already_read = reader.read_cache.unwrap();

let crc = Self::read_crc(&df, &mut already_read, r)?;
let crc = Self::read_crc(&df, &mut reader_crc)?;

Ok(Self { df, crc })
}
}

impl Frame {
/// Read rest as CRC bits
fn read_crc<R: Read>(
df: &DF,
cache: &mut Vec<u8>,
mut reader: R,
) -> result::Result<u32, DekuError> {
fn read_crc<R: Read>(df: &DF, reader: &mut ReaderCrc<R>) -> result::Result<u32, DekuError> {
const MODES_LONG_MSG_BYTES: usize = 14;
const MODES_SHORT_MSG_BYTES: usize = 7;

Expand All @@ -201,14 +210,13 @@ impl Frame {
MODES_LONG_MSG_BYTES * 8
};

if bit_len > cache.len() * 8 {
if bit_len > reader.cache.len() * 8 {
let mut buf = vec![];
reader.read_to_end(&mut buf).unwrap();
log::info!("reading more: {buf:02x?}");
cache.append(&mut buf);
reader.cache.append(&mut buf);
}

let crc = crc::modes_checksum(cache, bit_len)?;
let crc = crc::modes_checksum(&reader.cache, bit_len)?;
Ok(crc)
}
}
Expand Down Expand Up @@ -781,7 +789,7 @@ impl AC13Field {
} else {
Ok((n as u16).wrapping_mul(100))
}
},
}
Err(_e) => Ok(0),
}
}
Expand Down
10 changes: 2 additions & 8 deletions libadsb_deku/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,7 @@ fn testing05() {
let bytes = hex!("5dab3d17d4ba29");
let cursor = Cursor::new(bytes);
let frame = Frame::from_reader(cursor);
if let DF::AllCallReply {
icao, capability, ..
} = frame.unwrap().df
{
if let DF::AllCallReply { icao, capability, .. } = frame.unwrap().df {
assert_eq_hex!(icao.0, hex!("ab3d17"));
assert_eq!(capability, Capability::AG_AIRBORNE);
return;
Expand Down Expand Up @@ -194,10 +191,7 @@ fn testing08() {
let bytes = hex!("5da039b46d7d81");
let cursor = Cursor::new(bytes);
let frame = Frame::from_reader(cursor);
if let DF::AllCallReply {
icao, capability, ..
} = frame.unwrap().df
{
if let DF::AllCallReply { icao, capability, .. } = frame.unwrap().df {
assert_eq_hex!(icao.0, hex!("a039b4"));
assert_eq!(capability, Capability::AG_AIRBORNE);
return;
Expand Down
15 changes: 6 additions & 9 deletions rsadsb_common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,17 +124,14 @@ impl Airplanes {
let mut airplane_added = Added::No;
if let DF::ADSB(ref adsb) = frame.df {
airplane_added = match &adsb.me {
ME::AircraftIdentification {
value: identification,
..
} => self.add_identification(adsb.icao, identification),
ME::AircraftIdentification { value: identification, .. } => {
self.add_identification(adsb.icao, identification)
}
ME::AirborneVelocity(vel) => self.add_airborne_velocity(adsb.icao, vel),
ME::AirbornePositionGNSSAltitude {
value: altitude, ..
ME::AirbornePositionGNSSAltitude { value: altitude, .. }
| ME::AirbornePositionBaroAltitude { value: altitude, .. } => {
self.add_altitude(adsb.icao, altitude, lat_long, max_rang)
}
| ME::AirbornePositionBaroAltitude {
value: altitude, ..
} => self.add_altitude(adsb.icao, altitude, lat_long, max_rang),
_ => Added::No,
};
let incr_airplane_added = self.incr_messages(adsb.icao);
Expand Down

0 comments on commit 9ebea61

Please sign in to comment.