|
| 1 | +//! Bounded, privacy-conscious summaries of RTL8720F radar transport frames. |
| 2 | +
|
| 3 | +use serde::Serialize; |
| 4 | +use wifi_densepose_hardware::rtl8720f::{RadarFlags, RadarFrame, RadarPayload, ReportType}; |
| 5 | + |
| 6 | +#[derive(Debug, Clone, PartialEq, Serialize)] |
| 7 | +pub(crate) struct RealtekRadarSnapshot { |
| 8 | + pub event_type: &'static str, |
| 9 | + pub source: &'static str, |
| 10 | + pub report_type: &'static str, |
| 11 | + pub sequence: u32, |
| 12 | + pub timestamp_us: u64, |
| 13 | + pub device_id: String, |
| 14 | + pub center_freq_khz: u32, |
| 15 | + pub bandwidth_mhz: u16, |
| 16 | + pub antenna_count: u8, |
| 17 | + pub element_count: usize, |
| 18 | + pub calibrated: bool, |
| 19 | + pub synthetic: bool, |
| 20 | + pub interference_detected: bool, |
| 21 | + pub saturated: bool, |
| 22 | + pub time_synchronized: bool, |
| 23 | + pub calibration_id: u32, |
| 24 | + pub bin_spacing: f32, |
| 25 | + pub peak_range_m: Option<f32>, |
| 26 | + pub peak_power: Option<f32>, |
| 27 | + pub mean_cfr_amplitude: Option<f32>, |
| 28 | +} |
| 29 | + |
| 30 | +impl RealtekRadarSnapshot { |
| 31 | + pub(crate) fn from_frame(frame: &RadarFrame) -> Self { |
| 32 | + let synthetic = frame.flags.contains(RadarFlags::SYNTHETIC); |
| 33 | + let (peak_range_m, peak_power) = range_peak(frame); |
| 34 | + Self { |
| 35 | + event_type: "realtek_radar", |
| 36 | + source: if synthetic { |
| 37 | + "realtek:simulated" |
| 38 | + } else { |
| 39 | + "realtek" |
| 40 | + }, |
| 41 | + report_type: report_type_name(frame.report_type), |
| 42 | + sequence: frame.sequence, |
| 43 | + timestamp_us: frame.timestamp_us, |
| 44 | + device_id: format!("{:016x}", frame.device_id), |
| 45 | + center_freq_khz: frame.center_freq_khz, |
| 46 | + bandwidth_mhz: frame.bandwidth_mhz, |
| 47 | + antenna_count: frame.antenna_count, |
| 48 | + element_count: frame.payload.len(), |
| 49 | + calibrated: frame.flags.contains(RadarFlags::CALIBRATED), |
| 50 | + synthetic, |
| 51 | + interference_detected: frame.flags.contains(RadarFlags::INTERFERENCE_DETECTED), |
| 52 | + saturated: frame.flags.contains(RadarFlags::SATURATED), |
| 53 | + time_synchronized: frame.flags.contains(RadarFlags::TIME_SYNCHRONIZED), |
| 54 | + calibration_id: frame.calibration_id, |
| 55 | + bin_spacing: frame.bin_spacing, |
| 56 | + peak_range_m, |
| 57 | + peak_power, |
| 58 | + mean_cfr_amplitude: mean_cfr_amplitude(frame), |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +fn report_type_name(report_type: ReportType) -> &'static str { |
| 64 | + match report_type { |
| 65 | + ReportType::Cfr => "cfr", |
| 66 | + ReportType::RangeNear => "range_near", |
| 67 | + ReportType::RangeFar => "range_far", |
| 68 | + ReportType::Interference => "interference", |
| 69 | + ReportType::Capabilities => "capabilities", |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +fn range_peak(frame: &RadarFrame) -> (Option<f32>, Option<f32>) { |
| 74 | + let max = match &frame.payload { |
| 75 | + RadarPayload::PowerU16(values) => values |
| 76 | + .iter() |
| 77 | + .enumerate() |
| 78 | + .max_by_key(|(_, value)| *value) |
| 79 | + .map(|(index, value)| (index, *value as f32 * frame.scale)), |
| 80 | + RadarPayload::PowerF32(values) => values |
| 81 | + .iter() |
| 82 | + .enumerate() |
| 83 | + .max_by(|(_, a), (_, b)| a.total_cmp(b)) |
| 84 | + .map(|(index, value)| (index, *value * frame.scale)), |
| 85 | + _ => None, |
| 86 | + }; |
| 87 | + max.map_or((None, None), |(index, power)| { |
| 88 | + (Some(index as f32 * frame.bin_spacing), Some(power)) |
| 89 | + }) |
| 90 | +} |
| 91 | + |
| 92 | +fn mean_cfr_amplitude(frame: &RadarFrame) -> Option<f32> { |
| 93 | + let (sum, count) = match &frame.payload { |
| 94 | + RadarPayload::ComplexI16(values) => ( |
| 95 | + values |
| 96 | + .iter() |
| 97 | + .map(|[i, q]| ((*i as f32).hypot(*q as f32)) * frame.scale) |
| 98 | + .sum::<f32>(), |
| 99 | + values.len(), |
| 100 | + ), |
| 101 | + RadarPayload::ComplexF32(values) => ( |
| 102 | + values |
| 103 | + .iter() |
| 104 | + .map(|[i, q]| i.hypot(*q) * frame.scale) |
| 105 | + .sum::<f32>(), |
| 106 | + values.len(), |
| 107 | + ), |
| 108 | + _ => return None, |
| 109 | + }; |
| 110 | + (count != 0).then_some(sum / count as f32) |
| 111 | +} |
| 112 | + |
| 113 | +#[cfg(test)] |
| 114 | +mod tests { |
| 115 | + use super::*; |
| 116 | + use wifi_densepose_hardware::rtl8720f::simulator::{Rtl8720fSimulator, SimulatorConfig}; |
| 117 | + |
| 118 | + #[test] |
| 119 | + fn synthetic_range_summary_has_peak_and_provenance() { |
| 120 | + let mut simulator = Rtl8720fSimulator::new(SimulatorConfig::default()).unwrap(); |
| 121 | + let snapshot = |
| 122 | + RealtekRadarSnapshot::from_frame(&simulator.next_frame(ReportType::RangeNear)); |
| 123 | + assert_eq!(snapshot.source, "realtek:simulated"); |
| 124 | + assert!(snapshot.synthetic); |
| 125 | + assert!(snapshot.peak_range_m.is_some()); |
| 126 | + assert!(snapshot.peak_power.unwrap() > 0.0); |
| 127 | + assert_eq!(snapshot.mean_cfr_amplitude, None); |
| 128 | + } |
| 129 | + |
| 130 | + #[test] |
| 131 | + fn synthetic_cfr_summary_exposes_only_aggregate_amplitude() { |
| 132 | + let mut simulator = Rtl8720fSimulator::new(SimulatorConfig::default()).unwrap(); |
| 133 | + let snapshot = RealtekRadarSnapshot::from_frame(&simulator.next_frame(ReportType::Cfr)); |
| 134 | + assert!(snapshot.mean_cfr_amplitude.unwrap() > 0.0); |
| 135 | + assert_eq!(snapshot.peak_power, None); |
| 136 | + } |
| 137 | +} |
0 commit comments