wifi-densepose-hardware 0.3.2

Hardware interface abstractions for WiFi CSI sensors (ESP32, Intel 5300, Atheros)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
//! ADR-264 transport-neutral framing for Realtek RTL8720F radar reports.
//!
//! This is a RuView-owned wire contract around the public Ameba API boundary,
//! not a representation of Realtek's private structs. Upstream PR #1336 exposes
//! `wifi_radar_config(struct rtw_radar_action_parm *)`; the report callback ABI
//! remains vendor-gated. Keeping this codec byte-oriented lets host development,
//! replay, and fuzzing proceed without linking the Ameba SDK.

use serde::{Deserialize, Serialize};
use thiserror::Error;

use crate::radio_ops::crc32_ieee;

pub const RTL8720F_RADAR_MAGIC: u32 = 0x3152_5452; // "RTR1" in little endian
pub const RTL8720F_RADAR_VERSION: u8 = 1;
pub const RTL8720F_RADAR_HEADER_LEN: usize = 56;
pub const RTL8720F_RADAR_CRC_LEN: usize = 4;
/// Largest payload that can be carried in one IPv4 UDP datagram.
pub const RTL8720F_RADAR_MAX_FRAME_LEN: usize = 65_507;
pub const RTL8720F_RADAR_MAX_ELEMENTS: usize = 16_384;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[repr(u8)]
pub enum ReportType {
    Cfr = 1,
    RangeNear = 2,
    RangeFar = 3,
    Interference = 4,
    Capabilities = 5,
}

impl TryFrom<u8> for ReportType {
    type Error = RadarParseError;

    fn try_from(value: u8) -> Result<Self, Self::Error> {
        match value {
            1 => Ok(Self::Cfr),
            2 => Ok(Self::RangeNear),
            3 => Ok(Self::RangeFar),
            4 => Ok(Self::Interference),
            5 => Ok(Self::Capabilities),
            _ => Err(RadarParseError::UnknownReportType(value)),
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[repr(u8)]
pub enum ElementFormat {
    /// TLV/opaque byte payload used by capabilities and interference reports.
    Bytes = 0,
    ComplexI16 = 1,
    ComplexF32 = 2,
    PowerU16 = 3,
    PowerF32 = 4,
}

impl ElementFormat {
    fn bytes_per_element(self) -> usize {
        match self {
            Self::Bytes => 1,
            Self::ComplexI16 | Self::PowerF32 => 4,
            Self::ComplexF32 => 8,
            Self::PowerU16 => 2,
        }
    }
}

impl TryFrom<u8> for ElementFormat {
    type Error = RadarParseError;

    fn try_from(value: u8) -> Result<Self, Self::Error> {
        match value {
            0 => Ok(Self::Bytes),
            1 => Ok(Self::ComplexI16),
            2 => Ok(Self::ComplexF32),
            3 => Ok(Self::PowerU16),
            4 => Ok(Self::PowerF32),
            _ => Err(RadarParseError::UnknownElementFormat(value)),
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
pub struct RadarFlags(pub u16);

impl RadarFlags {
    pub const CALIBRATED: u16 = 1 << 0;
    pub const INTERFERENCE_DETECTED: u16 = 1 << 1;
    pub const SATURATED: u16 = 1 << 2;
    pub const TIME_SYNCHRONIZED: u16 = 1 << 3;
    /// Frame was produced by a simulator/replay generator, never real hardware.
    pub const SYNTHETIC: u16 = 1 << 15;

    pub fn contains(self, flag: u16) -> bool {
        self.0 & flag != 0
    }
}

/// Deterministic, Rust-only RTL8720F source used until hardware is available.
/// It emits the same [`RadarFrame`] objects and wire bytes as the vendor adapter.
pub mod simulator {
    use super::*;

    #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
    pub struct SimulatorConfig {
        pub seed: u64,
        pub device_id: u64,
        pub center_freq_khz: u32,
        pub bandwidth_mhz: u16,
        pub frame_period_us: u64,
        pub cfr_bins: u16,
        pub range_bins: u16,
    }

    impl Default for SimulatorConfig {
        fn default() -> Self {
            Self {
                seed: 0x8720_F123_4567_89AB,
                device_id: 0x5254_4C38_3732_3046,
                center_freq_khz: 2_442_000,
                bandwidth_mhz: 40,
                frame_period_us: 15_000,
                cfr_bins: 128,
                range_bins: 32,
            }
        }
    }

    #[derive(Debug, Clone)]
    pub struct Rtl8720fSimulator {
        config: SimulatorConfig,
        rng: u64,
        sequence: u32,
        timestamp_us: u64,
        target_distance_m: f32,
        target_velocity_mps: f32,
    }

    impl Rtl8720fSimulator {
        pub fn new(config: SimulatorConfig) -> Result<Self, RadarParseError> {
            if !matches!(config.bandwidth_mhz, 20 | 40 | 70) {
                return Err(RadarParseError::InvalidBandwidth(config.bandwidth_mhz));
            }
            if config.cfr_bins == 0 || config.range_bins == 0 {
                return Err(RadarParseError::TooManyElements(0));
            }
            Ok(Self {
                rng: config.seed,
                config,
                sequence: 0,
                timestamp_us: 0,
                target_distance_m: 2.0,
                target_velocity_mps: 0.20,
            })
        }

        pub fn config(&self) -> &SimulatorConfig {
            &self.config
        }
        pub fn target_distance_m(&self) -> f32 {
            self.target_distance_m
        }
        pub fn target_velocity_mps(&self) -> f32 {
            self.target_velocity_mps
        }

        /// Emit the boot-time capabilities report as compact TLVs:
        /// type 1 = bandwidth bitset, 2 = CFR bins, 3 = range bins,
        /// 4 = minimum frame period in microseconds.
        pub fn capabilities_frame(&self) -> RadarFrame {
            let bandwidths = 0b0000_0111u8; // 20, 40, 70 MHz
            let mut bytes = vec![1, 1, bandwidths, 2, 2];
            bytes.extend_from_slice(&self.config.cfr_bins.to_le_bytes());
            bytes.extend_from_slice(&[3, 2]);
            bytes.extend_from_slice(&self.config.range_bins.to_le_bytes());
            bytes.extend_from_slice(&[4, 4]);
            bytes.extend_from_slice(&(self.config.frame_period_us as u32).to_le_bytes());
            self.frame(
                ReportType::Capabilities,
                0,
                0,
                RadarPayload::Bytes(bytes),
                0.0,
            )
        }

        pub fn next_frame(&mut self, report_type: ReportType) -> RadarFrame {
            let sequence = self.sequence;
            let timestamp_us = self.timestamp_us;
            self.sequence = self.sequence.wrapping_add(1);
            self.timestamp_us = self.timestamp_us.wrapping_add(self.config.frame_period_us);
            self.advance_target();

            match report_type {
                ReportType::Cfr => {
                    let values = (0..self.config.cfr_bins)
                        .map(|bin| {
                            let phase = bin as f32 * 0.17 + sequence as f32 * 0.05;
                            let noise_i = self.noise_i16(20);
                            let noise_q = self.noise_i16(20);
                            [
                                (phase.cos() * 1800.0) as i16 + noise_i,
                                (phase.sin() * 1800.0) as i16 + noise_q,
                            ]
                        })
                        .collect();
                    self.frame(
                        report_type,
                        sequence,
                        timestamp_us,
                        RadarPayload::ComplexI16(values),
                        self.config.bandwidth_mhz as f32 * 1_000_000.0
                            / self.config.cfr_bins as f32,
                    )
                }
                ReportType::RangeNear | ReportType::RangeFar => {
                    let bin_spacing = match self.config.bandwidth_mhz {
                        70 => 0.33,
                        40 => 0.59,
                        _ => 1.18,
                    };
                    let target_bin = (self.target_distance_m / bin_spacing).round() as usize;
                    let values = (0..self.config.range_bins as usize)
                        .map(|bin| {
                            let distance = bin.abs_diff(target_bin) as f32;
                            let peak = 1000.0 * (-0.5 * distance * distance).exp();
                            let leakage = if report_type == ReportType::RangeNear && bin < 2 {
                                250.0
                            } else {
                                0.0
                            };
                            (peak + leakage + self.noise_f32(12.0)).max(0.0)
                        })
                        .collect();
                    self.frame(
                        report_type,
                        sequence,
                        timestamp_us,
                        RadarPayload::PowerF32(values),
                        bin_spacing,
                    )
                }
                ReportType::Interference => {
                    // TLV: channel-busy %, detected-during-chirp, signed dBm.
                    let busy = (self.next_u32() % 35) as u8;
                    let detected = u8::from(busy > 25);
                    let dbm = (-90i8 + (self.next_u32() % 25) as i8) as u8;
                    let bytes = vec![1, 1, busy, 2, 1, detected, 3, 1, dbm];
                    let mut frame = self.frame(
                        report_type,
                        sequence,
                        timestamp_us,
                        RadarPayload::Bytes(bytes),
                        0.0,
                    );
                    if detected != 0 {
                        frame.flags.0 |= RadarFlags::INTERFERENCE_DETECTED;
                    }
                    frame
                }
                ReportType::Capabilities => self.capabilities_frame(),
            }
        }

        pub fn next_wire(&mut self, report_type: ReportType) -> Result<Vec<u8>, RadarParseError> {
            self.next_frame(report_type).to_bytes()
        }

        fn frame(
            &self,
            report_type: ReportType,
            sequence: u32,
            timestamp_us: u64,
            payload: RadarPayload,
            bin_spacing: f32,
        ) -> RadarFrame {
            RadarFrame {
                report_type,
                sequence,
                timestamp_us,
                device_id: self.config.device_id,
                center_freq_khz: self.config.center_freq_khz,
                bandwidth_mhz: self.config.bandwidth_mhz,
                flags: RadarFlags(RadarFlags::CALIBRATED | RadarFlags::SYNTHETIC),
                antenna_count: 1,
                scale: 1.0,
                bin_spacing,
                calibration_id: 0,
                payload,
            }
        }

        fn advance_target(&mut self) {
            let dt = self.config.frame_period_us as f32 / 1_000_000.0;
            self.target_distance_m += self.target_velocity_mps * dt;
            if self.target_distance_m >= 5.5 || self.target_distance_m <= 0.8 {
                self.target_velocity_mps = -self.target_velocity_mps;
                self.target_distance_m = self.target_distance_m.clamp(0.8, 5.5);
            }
        }

        fn next_u32(&mut self) -> u32 {
            // PCG-style state transition with xorshift output; deterministic and dependency-free.
            self.rng = self
                .rng
                .wrapping_mul(6364136223846793005)
                .wrapping_add(1442695040888963407);
            let word = (((self.rng >> 18) ^ self.rng) >> 27) as u32;
            word.rotate_right((self.rng >> 59) as u32)
        }

        fn noise_i16(&mut self, amplitude: i16) -> i16 {
            (self.next_u32() % (amplitude as u32 * 2 + 1)) as i16 - amplitude
        }

        fn noise_f32(&mut self, amplitude: f32) -> f32 {
            let unit = self.next_u32() as f32 / u32::MAX as f32;
            (unit * 2.0 - 1.0) * amplitude
        }
    }

    impl Iterator for Rtl8720fSimulator {
        type Item = RadarFrame;

        fn next(&mut self) -> Option<Self::Item> {
            let report_type = match self.sequence % 4 {
                0 | 2 => ReportType::Cfr,
                1 => ReportType::RangeNear,
                _ => ReportType::RangeFar,
            };
            Some(self.next_frame(report_type))
        }
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum RadarPayload {
    Bytes(Vec<u8>),
    ComplexI16(Vec<[i16; 2]>),
    ComplexF32(Vec<[f32; 2]>),
    PowerU16(Vec<u16>),
    PowerF32(Vec<f32>),
}

impl RadarPayload {
    pub fn format(&self) -> ElementFormat {
        match self {
            Self::Bytes(_) => ElementFormat::Bytes,
            Self::ComplexI16(_) => ElementFormat::ComplexI16,
            Self::ComplexF32(_) => ElementFormat::ComplexF32,
            Self::PowerU16(_) => ElementFormat::PowerU16,
            Self::PowerF32(_) => ElementFormat::PowerF32,
        }
    }

    pub fn len(&self) -> usize {
        match self {
            Self::Bytes(v) => v.len(),
            Self::ComplexI16(v) => v.len(),
            Self::ComplexF32(v) => v.len(),
            Self::PowerU16(v) => v.len(),
            Self::PowerF32(v) => v.len(),
        }
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    fn encoded_len(&self) -> usize {
        self.len() * self.format().bytes_per_element()
    }

    fn validate_finite(&self) -> Result<(), RadarParseError> {
        let valid = match self {
            Self::ComplexF32(values) => values.iter().flatten().all(|v| v.is_finite()),
            Self::PowerF32(values) => values.iter().all(|v| v.is_finite()),
            _ => true,
        };
        if valid {
            Ok(())
        } else {
            Err(RadarParseError::NonFiniteValue)
        }
    }

    fn encode_into(&self, out: &mut Vec<u8>) {
        match self {
            Self::Bytes(values) => out.extend_from_slice(values),
            Self::ComplexI16(values) => values.iter().for_each(|value| {
                out.extend_from_slice(&value[0].to_le_bytes());
                out.extend_from_slice(&value[1].to_le_bytes());
            }),
            Self::ComplexF32(values) => values.iter().for_each(|value| {
                out.extend_from_slice(&value[0].to_le_bytes());
                out.extend_from_slice(&value[1].to_le_bytes());
            }),
            Self::PowerU16(values) => values
                .iter()
                .for_each(|value| out.extend_from_slice(&value.to_le_bytes())),
            Self::PowerF32(values) => values
                .iter()
                .for_each(|value| out.extend_from_slice(&value.to_le_bytes())),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct RadarFrame {
    pub report_type: ReportType,
    pub sequence: u32,
    pub timestamp_us: u64,
    pub device_id: u64,
    pub center_freq_khz: u32,
    pub bandwidth_mhz: u16,
    pub flags: RadarFlags,
    pub antenna_count: u8,
    pub scale: f32,
    pub bin_spacing: f32,
    pub calibration_id: u32,
    pub payload: RadarPayload,
}

impl RadarFrame {
    pub fn to_bytes(&self) -> Result<Vec<u8>, RadarParseError> {
        self.validate()?;
        let payload_len = self.payload.encoded_len();
        let frame_len = RTL8720F_RADAR_HEADER_LEN
            .checked_add(payload_len)
            .and_then(|value| value.checked_add(RTL8720F_RADAR_CRC_LEN))
            .ok_or(RadarParseError::LengthOverflow)?;
        if frame_len > RTL8720F_RADAR_MAX_FRAME_LEN {
            return Err(RadarParseError::FrameTooLarge(frame_len));
        }

        let mut out = Vec::with_capacity(frame_len);
        out.extend_from_slice(&RTL8720F_RADAR_MAGIC.to_le_bytes());
        out.push(RTL8720F_RADAR_VERSION);
        out.push(self.report_type as u8);
        out.extend_from_slice(&(RTL8720F_RADAR_HEADER_LEN as u16).to_le_bytes());
        out.extend_from_slice(&(frame_len as u32).to_le_bytes());
        out.extend_from_slice(&self.sequence.to_le_bytes());
        out.extend_from_slice(&self.timestamp_us.to_le_bytes());
        out.extend_from_slice(&self.device_id.to_le_bytes());
        out.extend_from_slice(&self.center_freq_khz.to_le_bytes());
        out.extend_from_slice(&self.bandwidth_mhz.to_le_bytes());
        out.extend_from_slice(&self.flags.0.to_le_bytes());
        out.extend_from_slice(&(self.payload.len() as u16).to_le_bytes());
        out.push(self.payload.format() as u8);
        out.push(self.antenna_count);
        out.extend_from_slice(&self.scale.to_le_bytes());
        out.extend_from_slice(&self.bin_spacing.to_le_bytes());
        out.extend_from_slice(&self.calibration_id.to_le_bytes());
        debug_assert_eq!(out.len(), RTL8720F_RADAR_HEADER_LEN);
        self.payload.encode_into(&mut out);
        let crc = crc32_ieee(&out);
        out.extend_from_slice(&crc.to_le_bytes());
        Ok(out)
    }

    pub fn from_bytes(input: &[u8]) -> Result<(Self, usize), RadarParseError> {
        if input.len() < RTL8720F_RADAR_HEADER_LEN {
            return Err(RadarParseError::InsufficientData {
                needed: RTL8720F_RADAR_HEADER_LEN,
                got: input.len(),
            });
        }
        let magic = read_u32(input, 0);
        if magic != RTL8720F_RADAR_MAGIC {
            return Err(RadarParseError::InvalidMagic(magic));
        }
        if input[4] != RTL8720F_RADAR_VERSION {
            return Err(RadarParseError::UnsupportedVersion(input[4]));
        }
        let report_type = ReportType::try_from(input[5])?;
        let header_len = read_u16(input, 6) as usize;
        if header_len < RTL8720F_RADAR_HEADER_LEN {
            return Err(RadarParseError::InvalidHeaderLength(header_len));
        }
        let frame_len = read_u32(input, 8) as usize;
        if frame_len > RTL8720F_RADAR_MAX_FRAME_LEN {
            return Err(RadarParseError::FrameTooLarge(frame_len));
        }
        if frame_len < header_len + RTL8720F_RADAR_CRC_LEN {
            return Err(RadarParseError::InvalidFrameLength(frame_len));
        }
        if input.len() < frame_len {
            return Err(RadarParseError::InsufficientData {
                needed: frame_len,
                got: input.len(),
            });
        }

        let element_count = read_u16(input, 40) as usize;
        if element_count > RTL8720F_RADAR_MAX_ELEMENTS {
            return Err(RadarParseError::TooManyElements(element_count));
        }
        let format = ElementFormat::try_from(input[42])?;
        validate_type_format(report_type, format)?;
        let payload_len = element_count
            .checked_mul(format.bytes_per_element())
            .ok_or(RadarParseError::LengthOverflow)?;
        let expected_len = header_len
            .checked_add(payload_len)
            .and_then(|value| value.checked_add(RTL8720F_RADAR_CRC_LEN))
            .ok_or(RadarParseError::LengthOverflow)?;
        if expected_len != frame_len {
            return Err(RadarParseError::PayloadLengthMismatch {
                expected: expected_len,
                got: frame_len,
            });
        }

        let expected_crc = read_u32(input, frame_len - RTL8720F_RADAR_CRC_LEN);
        let actual_crc = crc32_ieee(&input[..frame_len - RTL8720F_RADAR_CRC_LEN]);
        if expected_crc != actual_crc {
            return Err(RadarParseError::CrcMismatch {
                expected: expected_crc,
                actual: actual_crc,
            });
        }

        let scale = read_f32(input, 44);
        let bin_spacing = read_f32(input, 48);
        if !scale.is_finite() || !bin_spacing.is_finite() {
            return Err(RadarParseError::NonFiniteValue);
        }
        let payload = decode_payload(format, &input[header_len..header_len + payload_len])?;
        let frame = Self {
            report_type,
            sequence: read_u32(input, 12),
            timestamp_us: read_u64(input, 16),
            device_id: read_u64(input, 24),
            center_freq_khz: read_u32(input, 32),
            bandwidth_mhz: read_u16(input, 36),
            flags: RadarFlags(read_u16(input, 38)),
            antenna_count: input[43],
            scale,
            bin_spacing,
            calibration_id: read_u32(input, 52),
            payload,
        };
        frame.validate()?;
        Ok((frame, frame_len))
    }

    fn validate(&self) -> Result<(), RadarParseError> {
        if !matches!(self.bandwidth_mhz, 20 | 40 | 70) {
            return Err(RadarParseError::InvalidBandwidth(self.bandwidth_mhz));
        }
        if self.antenna_count == 0 || self.antenna_count > 8 {
            return Err(RadarParseError::InvalidAntennaCount(self.antenna_count));
        }
        if self.payload.len() > RTL8720F_RADAR_MAX_ELEMENTS
            || self.payload.len() > u16::MAX as usize
        {
            return Err(RadarParseError::TooManyElements(self.payload.len()));
        }
        if !self.scale.is_finite() || !self.bin_spacing.is_finite() {
            return Err(RadarParseError::NonFiniteValue);
        }
        validate_type_format(self.report_type, self.payload.format())?;
        self.payload.validate_finite()
    }
}

fn validate_type_format(
    report_type: ReportType,
    format: ElementFormat,
) -> Result<(), RadarParseError> {
    let valid = match report_type {
        ReportType::Cfr => matches!(
            format,
            ElementFormat::ComplexI16 | ElementFormat::ComplexF32
        ),
        ReportType::RangeNear | ReportType::RangeFar => {
            matches!(format, ElementFormat::PowerU16 | ElementFormat::PowerF32)
        }
        ReportType::Interference | ReportType::Capabilities => format == ElementFormat::Bytes,
    };
    if valid {
        Ok(())
    } else {
        Err(RadarParseError::InvalidTypeFormat {
            report_type,
            format,
        })
    }
}

fn decode_payload(format: ElementFormat, bytes: &[u8]) -> Result<RadarPayload, RadarParseError> {
    let payload = match format {
        ElementFormat::Bytes => RadarPayload::Bytes(bytes.to_vec()),
        ElementFormat::ComplexI16 => RadarPayload::ComplexI16(
            bytes
                .chunks_exact(4)
                .map(|c| [read_i16(c, 0), read_i16(c, 2)])
                .collect(),
        ),
        ElementFormat::ComplexF32 => RadarPayload::ComplexF32(
            bytes
                .chunks_exact(8)
                .map(|c| [read_f32(c, 0), read_f32(c, 4)])
                .collect(),
        ),
        ElementFormat::PowerU16 => {
            RadarPayload::PowerU16(bytes.chunks_exact(2).map(|c| read_u16(c, 0)).collect())
        }
        ElementFormat::PowerF32 => {
            RadarPayload::PowerF32(bytes.chunks_exact(4).map(|c| read_f32(c, 0)).collect())
        }
    };
    payload.validate_finite()?;
    Ok(payload)
}

fn read_u16(buf: &[u8], offset: usize) -> u16 {
    u16::from_le_bytes([buf[offset], buf[offset + 1]])
}
fn read_i16(buf: &[u8], offset: usize) -> i16 {
    i16::from_le_bytes([buf[offset], buf[offset + 1]])
}
fn read_u32(buf: &[u8], offset: usize) -> u32 {
    u32::from_le_bytes(buf[offset..offset + 4].try_into().unwrap())
}
fn read_u64(buf: &[u8], offset: usize) -> u64 {
    u64::from_le_bytes(buf[offset..offset + 8].try_into().unwrap())
}
fn read_f32(buf: &[u8], offset: usize) -> f32 {
    f32::from_le_bytes(buf[offset..offset + 4].try_into().unwrap())
}

#[derive(Debug, Error, PartialEq)]
pub enum RadarParseError {
    #[error("insufficient data: need {needed} bytes, got {got}")]
    InsufficientData { needed: usize, got: usize },
    #[error("invalid RTL8720F radar magic {0:#010x}")]
    InvalidMagic(u32),
    #[error("unsupported RTL8720F radar protocol version {0}")]
    UnsupportedVersion(u8),
    #[error("unknown radar report type {0}")]
    UnknownReportType(u8),
    #[error("unknown radar element format {0}")]
    UnknownElementFormat(u8),
    #[error("invalid header length {0}")]
    InvalidHeaderLength(usize),
    #[error("invalid frame length {0}")]
    InvalidFrameLength(usize),
    #[error("frame is too large: {0} bytes")]
    FrameTooLarge(usize),
    #[error("element count exceeds limit: {0}")]
    TooManyElements(usize),
    #[error("length arithmetic overflow")]
    LengthOverflow,
    #[error("payload/frame length mismatch: expected {expected}, got {got}")]
    PayloadLengthMismatch { expected: usize, got: usize },
    #[error("CRC mismatch: encoded {expected:#010x}, computed {actual:#010x}")]
    CrcMismatch { expected: u32, actual: u32 },
    #[error("non-finite floating-point value")]
    NonFiniteValue,
    #[error("invalid bandwidth {0} MHz")]
    InvalidBandwidth(u16),
    #[error("invalid antenna count {0}")]
    InvalidAntennaCount(u8),
    #[error("report {report_type:?} cannot use element format {format:?}")]
    InvalidTypeFormat {
        report_type: ReportType,
        format: ElementFormat,
    },
}

#[cfg(test)]
mod tests {
    use super::simulator::{Rtl8720fSimulator, SimulatorConfig};
    use super::*;

    fn cfr_frame() -> RadarFrame {
        RadarFrame {
            report_type: ReportType::Cfr,
            sequence: 42,
            timestamp_us: 123_456,
            device_id: 0x1122_3344_5566_7788,
            center_freq_khz: 2_442_000,
            bandwidth_mhz: 40,
            flags: RadarFlags(RadarFlags::CALIBRATED | RadarFlags::TIME_SYNCHRONIZED),
            antenna_count: 1,
            scale: 1.0 / 4096.0,
            bin_spacing: 312_500.0,
            calibration_id: 0xAABB_CCDD,
            payload: RadarPayload::ComplexI16(vec![[12, -7], [2048, -2048], [0, 1]]),
        }
    }

    #[test]
    fn cfr_round_trip_and_stream_consumption() {
        let frame = cfr_frame();
        let mut wire = frame.to_bytes().unwrap();
        let encoded_len = wire.len();
        wire.extend_from_slice(&[9, 8, 7]);
        let (decoded, consumed) = RadarFrame::from_bytes(&wire).unwrap();
        assert_eq!(decoded, frame);
        assert_eq!(consumed, encoded_len);
    }

    #[test]
    fn every_report_family_round_trips() {
        let payloads = [
            (
                ReportType::RangeNear,
                RadarPayload::PowerU16(vec![1, 2, u16::MAX]),
            ),
            (
                ReportType::RangeFar,
                RadarPayload::PowerF32(vec![0.0, 1.5, 9.25]),
            ),
            (
                ReportType::Interference,
                RadarPayload::Bytes(vec![1, 2, 0x34, 0x12]),
            ),
            (
                ReportType::Capabilities,
                RadarPayload::Bytes(vec![2, 1, 40]),
            ),
        ];
        for (report_type, payload) in payloads {
            let mut frame = cfr_frame();
            frame.report_type = report_type;
            frame.payload = payload;
            let (decoded, _) = RadarFrame::from_bytes(&frame.to_bytes().unwrap()).unwrap();
            assert_eq!(decoded, frame);
        }
    }

    #[test]
    fn single_bit_corruption_is_detected() {
        let mut wire = cfr_frame().to_bytes().unwrap();
        wire[RTL8720F_RADAR_HEADER_LEN + 1] ^= 0x01;
        assert!(matches!(
            RadarFrame::from_bytes(&wire),
            Err(RadarParseError::CrcMismatch { .. })
        ));
    }

    #[test]
    fn truncation_is_reported_without_panicking() {
        let wire = cfr_frame().to_bytes().unwrap();
        for end in 0..wire.len() {
            assert!(RadarFrame::from_bytes(&wire[..end]).is_err());
        }
    }

    #[test]
    fn count_length_mismatch_fails_before_payload_decode() {
        let mut wire = cfr_frame().to_bytes().unwrap();
        wire[40..42].copy_from_slice(&100u16.to_le_bytes());
        let crc_offset = wire.len() - RTL8720F_RADAR_CRC_LEN;
        let crc = crc32_ieee(&wire[..crc_offset]);
        wire[crc_offset..].copy_from_slice(&crc.to_le_bytes());
        assert!(matches!(
            RadarFrame::from_bytes(&wire),
            Err(RadarParseError::PayloadLengthMismatch { .. })
        ));
    }

    #[test]
    fn invalid_semantic_combinations_are_rejected() {
        let mut frame = cfr_frame();
        frame.payload = RadarPayload::PowerU16(vec![1]);
        assert!(matches!(
            frame.to_bytes(),
            Err(RadarParseError::InvalidTypeFormat { .. })
        ));
        frame.report_type = ReportType::RangeNear;
        frame.bandwidth_mhz = 80;
        assert_eq!(
            frame.to_bytes().unwrap_err(),
            RadarParseError::InvalidBandwidth(80)
        );
    }

    #[test]
    fn non_finite_values_are_rejected() {
        let mut frame = cfr_frame();
        frame.scale = f32::NAN;
        assert_eq!(
            frame.to_bytes().unwrap_err(),
            RadarParseError::NonFiniteValue
        );
        frame.scale = 1.0;
        frame.payload = RadarPayload::ComplexF32(vec![[f32::INFINITY, 0.0]]);
        assert_eq!(
            frame.to_bytes().unwrap_err(),
            RadarParseError::NonFiniteValue
        );
    }

    #[test]
    fn arbitrary_short_inputs_never_panic() {
        let mut state = 0x1234_5678u32;
        for len in 0..256usize {
            let mut bytes = vec![0u8; len];
            for byte in &mut bytes {
                state = state.wrapping_mul(1_664_525).wrapping_add(1_013_904_223);
                *byte = (state >> 24) as u8;
            }
            let result = std::panic::catch_unwind(|| RadarFrame::from_bytes(&bytes));
            assert!(result.is_ok(), "parser panicked for {len} bytes");
        }
    }

    #[test]
    fn simulator_is_deterministic_and_uses_real_wire_boundary() {
        let mut a = Rtl8720fSimulator::new(SimulatorConfig::default()).unwrap();
        let mut b = Rtl8720fSimulator::new(SimulatorConfig::default()).unwrap();
        for _ in 0..12 {
            let a_wire = a.next_wire(ReportType::Cfr).unwrap();
            let b_wire = b.next_wire(ReportType::Cfr).unwrap();
            assert_eq!(a_wire, b_wire);
            let (decoded, consumed) = RadarFrame::from_bytes(&a_wire).unwrap();
            assert_eq!(consumed, a_wire.len());
            assert!(decoded.flags.contains(RadarFlags::SYNTHETIC));
        }
    }

    #[test]
    fn simulator_range_peak_tracks_ground_truth() {
        let mut sim = Rtl8720fSimulator::new(SimulatorConfig::default()).unwrap();
        let frame = sim.next_frame(ReportType::RangeFar);
        let RadarPayload::PowerF32(power) = frame.payload else {
            panic!("expected power bins")
        };
        let peak = power
            .iter()
            .enumerate()
            .max_by(|a, b| a.1.total_cmp(b.1))
            .unwrap()
            .0;
        let observed_m = peak as f32 * frame.bin_spacing;
        assert!((observed_m - sim.target_distance_m()).abs() <= frame.bin_spacing);
    }

    #[test]
    fn simulator_capabilities_are_explicitly_synthetic() {
        let sim = Rtl8720fSimulator::new(SimulatorConfig::default()).unwrap();
        let frame = sim.capabilities_frame();
        assert_eq!(frame.report_type, ReportType::Capabilities);
        assert!(frame.flags.contains(RadarFlags::SYNTHETIC));
        let wire = frame.to_bytes().unwrap();
        assert_eq!(RadarFrame::from_bytes(&wire).unwrap().0, frame);
    }
}