|
| 1 | +//! Rust-only RTL8720F radar simulator for pre-hardware integration. |
| 2 | +
|
| 3 | +use std::{ |
| 4 | + fs::File, |
| 5 | + io::{self, Write}, |
| 6 | + net::{SocketAddr, UdpSocket}, |
| 7 | + path::PathBuf, |
| 8 | + thread, |
| 9 | + time::Duration, |
| 10 | +}; |
| 11 | + |
| 12 | +use clap::Parser; |
| 13 | +use wifi_densepose_hardware::rtl8720f::{ |
| 14 | + simulator::{Rtl8720fSimulator, SimulatorConfig}, |
| 15 | + RadarFrame, ReportType, |
| 16 | +}; |
| 17 | + |
| 18 | +#[derive(Debug, Parser)] |
| 19 | +#[command( |
| 20 | + name = "rtl8720f-sim", |
| 21 | + about = "Emit synthetic ADR-264 RTL8720F radar frames" |
| 22 | +)] |
| 23 | +struct Args { |
| 24 | + #[arg(long, default_value_t = 100)] |
| 25 | + frames: u32, |
| 26 | + #[arg(long, default_value = "0x8720f123456789ab", value_parser = parse_u64)] |
| 27 | + seed: u64, |
| 28 | + #[arg(long, default_value_t = 40)] |
| 29 | + bandwidth: u16, |
| 30 | + #[arg(long, default_value_t = 15)] |
| 31 | + interval_ms: u64, |
| 32 | + /// UDP destination; each frame is one datagram. |
| 33 | + #[arg(long)] |
| 34 | + udp: Option<SocketAddr>, |
| 35 | + /// Replay file; LE u32 length followed by ADR-264 bytes. |
| 36 | + #[arg(long)] |
| 37 | + output: Option<PathBuf>, |
| 38 | + #[arg(long)] |
| 39 | + realtime: bool, |
| 40 | +} |
| 41 | + |
| 42 | +fn parse_u64(value: &str) -> Result<u64, String> { |
| 43 | + if let Some(hex) = value |
| 44 | + .strip_prefix("0x") |
| 45 | + .or_else(|| value.strip_prefix("0X")) |
| 46 | + { |
| 47 | + u64::from_str_radix(hex, 16).map_err(|error| error.to_string()) |
| 48 | + } else { |
| 49 | + value.parse::<u64>().map_err(|error| error.to_string()) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +fn emit( |
| 54 | + frame: RadarFrame, |
| 55 | + socket: Option<&UdpSocket>, |
| 56 | + destination: Option<SocketAddr>, |
| 57 | + output: &mut Option<File>, |
| 58 | +) -> Result<usize, Box<dyn std::error::Error>> { |
| 59 | + let wire = frame.to_bytes()?; |
| 60 | + if let (Some(socket), Some(destination)) = (socket, destination) { |
| 61 | + let sent = socket.send_to(&wire, destination)?; |
| 62 | + if sent != wire.len() { |
| 63 | + return Err(io::Error::new(io::ErrorKind::WriteZero, "partial UDP datagram").into()); |
| 64 | + } |
| 65 | + } |
| 66 | + if let Some(file) = output { |
| 67 | + file.write_all(&(wire.len() as u32).to_le_bytes())?; |
| 68 | + file.write_all(&wire)?; |
| 69 | + } |
| 70 | + Ok(wire.len()) |
| 71 | +} |
| 72 | + |
| 73 | +fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 74 | + let args = Args::parse(); |
| 75 | + if args.udp.is_none() && args.output.is_none() { |
| 76 | + return Err("select at least one sink with --udp or --output".into()); |
| 77 | + } |
| 78 | + let config = SimulatorConfig { |
| 79 | + seed: args.seed, |
| 80 | + bandwidth_mhz: args.bandwidth, |
| 81 | + frame_period_us: args.interval_ms * 1_000, |
| 82 | + ..SimulatorConfig::default() |
| 83 | + }; |
| 84 | + let mut simulator = Rtl8720fSimulator::new(config)?; |
| 85 | + let socket = args.udp.map(|_| UdpSocket::bind("0.0.0.0:0")).transpose()?; |
| 86 | + let mut output = args.output.as_ref().map(File::create).transpose()?; |
| 87 | + let mut bytes_emitted = emit( |
| 88 | + simulator.capabilities_frame(), |
| 89 | + socket.as_ref(), |
| 90 | + args.udp, |
| 91 | + &mut output, |
| 92 | + )?; |
| 93 | + |
| 94 | + for index in 0..args.frames { |
| 95 | + let report_type = match index % 16 { |
| 96 | + 15 => ReportType::Interference, |
| 97 | + value if value % 4 == 1 => ReportType::RangeNear, |
| 98 | + value if value % 4 == 3 => ReportType::RangeFar, |
| 99 | + _ => ReportType::Cfr, |
| 100 | + }; |
| 101 | + bytes_emitted += emit( |
| 102 | + simulator.next_frame(report_type), |
| 103 | + socket.as_ref(), |
| 104 | + args.udp, |
| 105 | + &mut output, |
| 106 | + )?; |
| 107 | + if args.realtime { |
| 108 | + thread::sleep(Duration::from_millis(args.interval_ms)); |
| 109 | + } |
| 110 | + } |
| 111 | + eprintln!( |
| 112 | + "emitted {} synthetic RTL8720F frames ({} bytes, seed={:#x})", |
| 113 | + args.frames + 1, |
| 114 | + bytes_emitted, |
| 115 | + args.seed |
| 116 | + ); |
| 117 | + Ok(()) |
| 118 | +} |
0 commit comments