Format adsb code

This commit is contained in:
2025-04-30 21:21:53 -04:00
parent ebc1f30f24
commit aa38d3c29c
16 changed files with 412 additions and 725 deletions

68
adsb/src/main.rs Normal file
View File

@@ -0,0 +1,68 @@
mod device;
mod error;
mod frame;
mod hex;
use error::Result;
use crate::device::RtlSdrDevice;
use clap::Parser;
use rusb::TransferType;
use crate::frame::ADSBFrame;
use crate::hex::hex_to_bytes;
#[derive(Parser, Debug)]
#[command(author, version, about = "An ADS-B Receiver")]
struct ReceiverArgs {
/// Hex-string to decode
#[arg(short = 'd', long)]
decode: Option<String>,
/// Connect to the USB device
#[arg(short = 'c', long, action)]
connect: bool,
/// List USB devices
#[arg(short = 'l', long, action)]
list: bool,
/// Enable debug logging
#[arg(short = 'D', long, action)]
debug: bool,
}
fn main() -> Result<()> {
let args = ReceiverArgs::parse();
let default_filter = if args.debug {
"warn,adsb=debug"
} else {
"warn,adsb=info"
};
env_logger::init_from_env(env_logger::Env::default().filter_or("RUST_LOG", default_filter));
// Handle connection
if args.connect {
log::info!("Connecting to device");
let mut device = RtlSdrDevice::open(0x0BDA, 0x2832)?;
device.read(TransferType::Bulk)
}
// List devices
else if args.list {
RtlSdrDevice::list()
}
// Handle decode mode
else if let Some(mut hex_string) = args.decode {
if let Some(stripped) = hex_string.strip_prefix("0x") {
hex_string = stripped.to_string();
}
let buf = hex_to_bytes(&hex_string)?;
let frame = ADSBFrame::decode(&buf)?;
log::info!("{}", frame);
Ok(())
} else {
log::warn!("No connection specified");
Ok(())
}
}