wifi-densepose-mat 0.3.2

Mass Casualty Assessment Tool - WiFi-based disaster survivor detection
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
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
//! Adapter for wifi-densepose-hardware crate with real hardware support.
//!
//! This module provides adapters for various WiFi CSI hardware:
//! - ESP32 with CSI support via serial communication
//! - Intel 5300 NIC with Linux CSI Tool
#![allow(missing_docs)]
//! - Atheros CSI extraction via ath9k/ath10k drivers
//!
//! # Example
//!
//! ```ignore
//! use wifi_densepose_mat::integration::{HardwareAdapter, HardwareConfig, DeviceType};
//!
//! let config = HardwareConfig::esp32("/dev/ttyUSB0", 921600);
//! let mut adapter = HardwareAdapter::with_config(config);
//! adapter.initialize().await?;
//!
//! // Start streaming CSI data
//! let mut stream = adapter.start_csi_stream().await?;
//! while let Some(reading) = stream.next().await {
//!     // Process CSI data
//! }
//! ```

use super::AdapterError;
use crate::domain::SensorPosition;
use chrono::{DateTime, Utc};
use std::sync::Arc;
use tokio::sync::{broadcast, mpsc, RwLock};

/// Hardware configuration for CSI devices
#[derive(Debug, Clone)]
pub struct HardwareConfig {
    /// Device type selection
    pub device_type: DeviceType,
    /// Device-specific settings
    pub device_settings: DeviceSettings,
    /// Buffer size for CSI data
    pub buffer_size: usize,
    /// Whether to enable raw mode (minimal processing)
    pub raw_mode: bool,
    /// Sample rate override (Hz, 0 for device default)
    pub sample_rate_override: u32,
    /// Channel configuration
    pub channel_config: ChannelConfig,
}

impl Default for HardwareConfig {
    fn default() -> Self {
        Self {
            device_type: DeviceType::Simulated,
            device_settings: DeviceSettings::Simulated,
            buffer_size: 4096,
            raw_mode: false,
            sample_rate_override: 0,
            channel_config: ChannelConfig::default(),
        }
    }
}

impl HardwareConfig {
    /// Create configuration for ESP32 via serial
    pub fn esp32(serial_port: &str, baud_rate: u32) -> Self {
        Self {
            device_type: DeviceType::Esp32,
            device_settings: DeviceSettings::Serial(SerialSettings {
                port: serial_port.to_string(),
                baud_rate,
                data_bits: 8,
                stop_bits: 1,
                parity: Parity::None,
                flow_control: FlowControl::None,
                read_timeout_ms: 1000,
            }),
            buffer_size: 2048,
            raw_mode: false,
            sample_rate_override: 0,
            channel_config: ChannelConfig::default(),
        }
    }

    /// Create configuration for Intel 5300 NIC
    pub fn intel_5300(interface: &str) -> Self {
        Self {
            device_type: DeviceType::Intel5300,
            device_settings: DeviceSettings::NetworkInterface(NetworkInterfaceSettings {
                interface: interface.to_string(),
                monitor_mode: true,
                channel: 6,
                bandwidth: Bandwidth::HT20,
                antenna_config: AntennaConfig::default(),
            }),
            buffer_size: 8192,
            raw_mode: false,
            sample_rate_override: 0,
            channel_config: ChannelConfig {
                channel: 6,
                bandwidth: Bandwidth::HT20,
                num_subcarriers: 30, // Intel 5300 provides 30 subcarriers
            },
        }
    }

    /// Create configuration for Atheros NIC
    pub fn atheros(interface: &str, driver: AtherosDriver) -> Self {
        let num_subcarriers = match driver {
            AtherosDriver::Ath9k => 56,
            AtherosDriver::Ath10k => 114,
            AtherosDriver::Ath11k => 234,
        };

        Self {
            device_type: DeviceType::Atheros(driver),
            device_settings: DeviceSettings::NetworkInterface(NetworkInterfaceSettings {
                interface: interface.to_string(),
                monitor_mode: true,
                channel: 36,
                bandwidth: Bandwidth::HT40,
                antenna_config: AntennaConfig::default(),
            }),
            buffer_size: 16384,
            raw_mode: false,
            sample_rate_override: 0,
            channel_config: ChannelConfig {
                channel: 36,
                bandwidth: Bandwidth::HT40,
                num_subcarriers,
            },
        }
    }

    /// Create configuration for UDP receiver (generic CSI)
    pub fn udp_receiver(bind_addr: &str, port: u16) -> Self {
        Self {
            device_type: DeviceType::UdpReceiver,
            device_settings: DeviceSettings::Udp(UdpSettings {
                bind_address: bind_addr.to_string(),
                port,
                multicast_group: None,
                buffer_size: 65536,
            }),
            buffer_size: 8192,
            raw_mode: false,
            sample_rate_override: 0,
            channel_config: ChannelConfig::default(),
        }
    }
}

/// Supported device types
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DeviceType {
    /// ESP32 with ESP-CSI firmware
    Esp32,
    /// Intel 5300 NIC with Linux CSI Tool
    Intel5300,
    /// Atheros NIC with specific driver
    Atheros(AtherosDriver),
    /// Generic UDP CSI receiver
    UdpReceiver,
    /// PCAP file replay
    PcapFile,
    /// Simulated device (for testing)
    Simulated,
}

/// Atheros driver variants
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AtherosDriver {
    /// ath9k driver (legacy, 56 subcarriers)
    Ath9k,
    /// ath10k driver (802.11ac, 114 subcarriers)
    Ath10k,
    /// ath11k driver (802.11ax, 234 subcarriers)
    Ath11k,
}

/// Device-specific settings
#[derive(Debug, Clone)]
pub enum DeviceSettings {
    /// Serial port settings (ESP32)
    Serial(SerialSettings),
    /// Network interface settings (Intel 5300, Atheros)
    NetworkInterface(NetworkInterfaceSettings),
    /// UDP receiver settings
    Udp(UdpSettings),
    /// PCAP file settings
    Pcap(PcapSettings),
    /// Simulated device (no real hardware)
    Simulated,
}

/// Serial port configuration
#[derive(Debug, Clone)]
pub struct SerialSettings {
    /// Serial port path
    pub port: String,
    /// Baud rate
    pub baud_rate: u32,
    /// Data bits (5-8)
    pub data_bits: u8,
    /// Stop bits (1, 2)
    pub stop_bits: u8,
    /// Parity setting
    pub parity: Parity,
    /// Flow control
    pub flow_control: FlowControl,
    /// Read timeout in milliseconds
    pub read_timeout_ms: u64,
}

/// Parity options
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Parity {
    None,
    Odd,
    Even,
}

/// Flow control options
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FlowControl {
    None,
    Hardware,
    Software,
}

/// Network interface configuration
#[derive(Debug, Clone)]
pub struct NetworkInterfaceSettings {
    /// Interface name (e.g., "wlan0")
    pub interface: String,
    /// Enable monitor mode
    pub monitor_mode: bool,
    /// WiFi channel
    pub channel: u8,
    /// Channel bandwidth
    pub bandwidth: Bandwidth,
    /// Antenna configuration
    pub antenna_config: AntennaConfig,
}

/// Channel bandwidth options
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Bandwidth {
    /// 20 MHz (legacy)
    #[default]
    HT20,
    /// 40 MHz (802.11n)
    HT40,
    /// 80 MHz (802.11ac)
    VHT80,
    /// 160 MHz (802.11ac Wave 2)
    VHT160,
}

impl Bandwidth {
    /// Get number of subcarriers for this bandwidth
    pub fn subcarrier_count(&self) -> usize {
        match self {
            Bandwidth::HT20 => 56,
            Bandwidth::HT40 => 114,
            Bandwidth::VHT80 => 242,
            Bandwidth::VHT160 => 484,
        }
    }
}

/// Antenna configuration for MIMO
#[derive(Debug, Clone)]
pub struct AntennaConfig {
    /// Number of transmit antennas
    pub tx_antennas: u8,
    /// Number of receive antennas
    pub rx_antennas: u8,
    /// Enabled antenna mask
    pub antenna_mask: u8,
}

impl Default for AntennaConfig {
    fn default() -> Self {
        Self {
            tx_antennas: 1,
            rx_antennas: 3,
            antenna_mask: 0x07, // Enable antennas 0, 1, 2
        }
    }
}

/// UDP receiver settings
#[derive(Debug, Clone)]
pub struct UdpSettings {
    /// Bind address
    pub bind_address: String,
    /// Port number
    pub port: u16,
    /// Multicast group (optional)
    pub multicast_group: Option<String>,
    /// Socket buffer size
    pub buffer_size: usize,
}

/// PCAP file settings
#[derive(Debug, Clone)]
pub struct PcapSettings {
    /// Path to PCAP file
    pub file_path: String,
    /// Playback speed multiplier (1.0 = realtime)
    pub playback_speed: f64,
    /// Loop playback
    pub loop_playback: bool,
}

/// Channel configuration
#[derive(Debug, Clone)]
pub struct ChannelConfig {
    /// WiFi channel
    pub channel: u8,
    /// Bandwidth
    pub bandwidth: Bandwidth,
    /// Number of OFDM subcarriers
    pub num_subcarriers: usize,
}

impl Default for ChannelConfig {
    fn default() -> Self {
        Self {
            channel: 6,
            bandwidth: Bandwidth::HT20,
            num_subcarriers: 56,
        }
    }
}

/// Hardware adapter for sensor communication
pub struct HardwareAdapter {
    /// Configuration
    config: HardwareConfig,
    /// Connected sensors
    sensors: Vec<SensorInfo>,
    /// Whether hardware is initialized
    initialized: bool,
    /// CSI broadcast channel
    csi_broadcaster: Option<broadcast::Sender<CsiReadings>>,
    /// Device state (shared for async operations)
    state: Arc<RwLock<DeviceState>>,
    /// Shutdown signal
    shutdown_tx: Option<mpsc::Sender<()>>,
}

/// Internal device state
struct DeviceState {
    /// Whether streaming is active
    streaming: bool,
    /// Total packets received
    packets_received: u64,
    /// Packets with errors
    error_count: u64,
    /// Last error message
    last_error: Option<String>,
    /// Device-specific state
    device_state: DeviceSpecificState,
}

/// Device-specific runtime state
#[allow(dead_code)]
enum DeviceSpecificState {
    Esp32 {
        firmware_version: Option<String>,
        mac_address: Option<String>,
    },
    Intel5300 {
        bfee_count: u64,
    },
    Atheros {
        driver: AtherosDriver,
        csi_buf_ptr: Option<u64>,
    },
    Other,
}

/// Information about a connected sensor
#[derive(Debug, Clone)]
pub struct SensorInfo {
    /// Unique sensor ID
    pub id: String,
    /// Sensor position
    pub position: SensorPosition,
    /// Current status
    pub status: SensorStatus,
    /// Last RSSI reading (if available)
    pub last_rssi: Option<f64>,
    /// Battery level (0-100, if applicable)
    pub battery_level: Option<u8>,
    /// MAC address (if available)
    pub mac_address: Option<String>,
    /// Firmware version (if available)
    pub firmware_version: Option<String>,
}

/// Status of a sensor
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SensorStatus {
    /// Sensor is connected and operational
    Connected,
    /// Sensor is disconnected
    Disconnected,
    /// Sensor is in error state
    Error,
    /// Sensor is initializing
    Initializing,
    /// Sensor battery is low
    LowBattery,
    /// Sensor is in standby mode
    Standby,
}

impl HardwareAdapter {
    /// Create a new hardware adapter with default configuration
    pub fn new() -> Self {
        Self::with_config(HardwareConfig::default())
    }

    /// Create a new hardware adapter with specific configuration
    pub fn with_config(config: HardwareConfig) -> Self {
        Self {
            config,
            sensors: Vec::new(),
            initialized: false,
            csi_broadcaster: None,
            state: Arc::new(RwLock::new(DeviceState {
                streaming: false,
                packets_received: 0,
                error_count: 0,
                last_error: None,
                device_state: DeviceSpecificState::Other,
            })),
            shutdown_tx: None,
        }
    }

    /// Get the current configuration
    pub fn config(&self) -> &HardwareConfig {
        &self.config
    }

    /// Initialize hardware communication
    pub async fn initialize(&mut self) -> Result<(), AdapterError> {
        tracing::info!(
            "Initializing hardware adapter for {:?}",
            self.config.device_type
        );

        match &self.config.device_type {
            DeviceType::Esp32 => self.initialize_esp32().await?,
            DeviceType::Intel5300 => self.initialize_intel_5300().await?,
            DeviceType::Atheros(driver) => self.initialize_atheros(*driver).await?,
            DeviceType::UdpReceiver => self.initialize_udp().await?,
            DeviceType::PcapFile => self.initialize_pcap().await?,
            DeviceType::Simulated => self.initialize_simulated().await?,
        }

        // Create CSI broadcast channel
        let (tx, _) = broadcast::channel(self.config.buffer_size);
        self.csi_broadcaster = Some(tx);

        self.initialized = true;
        tracing::info!("Hardware adapter initialized successfully");
        Ok(())
    }

    /// Initialize ESP32 device
    async fn initialize_esp32(&mut self) -> Result<(), AdapterError> {
        let settings = match &self.config.device_settings {
            DeviceSettings::Serial(s) => s,
            _ => {
                return Err(AdapterError::Config(
                    "ESP32 requires serial settings".into(),
                ))
            }
        };

        tracing::info!(
            "Initializing ESP32 on {} at {} baud",
            settings.port,
            settings.baud_rate
        );

        // Verify serial port exists
        #[cfg(unix)]
        {
            if !std::path::Path::new(&settings.port).exists() {
                return Err(AdapterError::Hardware(format!(
                    "Serial port {} not found",
                    settings.port
                )));
            }
        }

        // Update device state
        let mut state = self.state.write().await;
        state.device_state = DeviceSpecificState::Esp32 {
            firmware_version: None,
            mac_address: None,
        };

        Ok(())
    }

    /// Initialize Intel 5300 NIC
    async fn initialize_intel_5300(&mut self) -> Result<(), AdapterError> {
        let settings = match &self.config.device_settings {
            DeviceSettings::NetworkInterface(s) => s,
            _ => {
                return Err(AdapterError::Config(
                    "Intel 5300 requires network interface settings".into(),
                ))
            }
        };

        tracing::info!(
            "Initializing Intel 5300 on interface {}",
            settings.interface
        );

        // Check if iwlwifi driver is loaded
        #[cfg(target_os = "linux")]
        {
            let output = tokio::process::Command::new("lsmod")
                .output()
                .await
                .map_err(|e| {
                    AdapterError::Hardware(format!("Failed to check kernel modules: {}", e))
                })?;

            let stdout = String::from_utf8_lossy(&output.stdout);
            if !stdout.contains("iwlwifi") {
                tracing::warn!("iwlwifi module not loaded - CSI extraction may not work");
            }
        }

        // Verify connector proc file exists (Linux CSI Tool)
        #[cfg(target_os = "linux")]
        {
            let connector_path = "/proc/net/connector";
            if !std::path::Path::new(connector_path).exists() {
                tracing::warn!("Connector proc file not found - install Linux CSI Tool");
            }
        }

        let mut state = self.state.write().await;
        state.device_state = DeviceSpecificState::Intel5300 { bfee_count: 0 };

        Ok(())
    }

    /// Initialize Atheros NIC
    async fn initialize_atheros(&mut self, driver: AtherosDriver) -> Result<(), AdapterError> {
        let settings = match &self.config.device_settings {
            DeviceSettings::NetworkInterface(s) => s,
            _ => {
                return Err(AdapterError::Config(
                    "Atheros requires network interface settings".into(),
                ))
            }
        };

        tracing::info!(
            "Initializing Atheros ({:?}) on interface {}",
            driver,
            settings.interface
        );

        // Check for driver-specific debugfs entries
        #[cfg(target_os = "linux")]
        {
            let debugfs_path = format!(
                "/sys/kernel/debug/ieee80211/phy0/ath{}/csi",
                match driver {
                    AtherosDriver::Ath9k => "9k",
                    AtherosDriver::Ath10k => "10k",
                    AtherosDriver::Ath11k => "11k",
                }
            );

            if !std::path::Path::new(&debugfs_path).exists() {
                tracing::warn!(
                    "CSI debugfs path {} not found - CSI patched driver may not be installed",
                    debugfs_path
                );
            }
        }

        let mut state = self.state.write().await;
        state.device_state = DeviceSpecificState::Atheros {
            driver,
            csi_buf_ptr: None,
        };

        Ok(())
    }

    /// Initialize UDP receiver
    async fn initialize_udp(&mut self) -> Result<(), AdapterError> {
        let settings = match &self.config.device_settings {
            DeviceSettings::Udp(s) => s,
            _ => {
                return Err(AdapterError::Config(
                    "UDP receiver requires UDP settings".into(),
                ))
            }
        };

        tracing::info!(
            "Initializing UDP receiver on {}:{}",
            settings.bind_address,
            settings.port
        );

        // Verify port is available
        let addr = format!("{}:{}", settings.bind_address, settings.port);
        let socket = tokio::net::UdpSocket::bind(&addr)
            .await
            .map_err(|e| AdapterError::Hardware(format!("Failed to bind UDP socket: {}", e)))?;

        // Join multicast group if specified
        if let Some(ref group) = settings.multicast_group {
            let multicast_addr: std::net::Ipv4Addr = group
                .parse()
                .map_err(|e| AdapterError::Config(format!("Invalid multicast address: {}", e)))?;

            socket
                .join_multicast_v4(multicast_addr, std::net::Ipv4Addr::UNSPECIFIED)
                .map_err(|e| {
                    AdapterError::Hardware(format!("Failed to join multicast group: {}", e))
                })?;
        }

        // Socket will be recreated when streaming starts
        drop(socket);

        Ok(())
    }

    /// Initialize PCAP file reader
    async fn initialize_pcap(&mut self) -> Result<(), AdapterError> {
        let settings = match &self.config.device_settings {
            DeviceSettings::Pcap(s) => s,
            _ => return Err(AdapterError::Config("PCAP requires PCAP settings".into())),
        };

        tracing::info!("Initializing PCAP file reader: {}", settings.file_path);

        // Verify file exists
        if !std::path::Path::new(&settings.file_path).exists() {
            return Err(AdapterError::Hardware(format!(
                "PCAP file not found: {}",
                settings.file_path
            )));
        }

        Ok(())
    }

    /// Initialize simulated device
    async fn initialize_simulated(&mut self) -> Result<(), AdapterError> {
        tracing::info!("Initializing simulated CSI device");
        Ok(())
    }

    /// Start CSI streaming
    pub async fn start_csi_stream(&mut self) -> Result<CsiStream, AdapterError> {
        if !self.initialized {
            return Err(AdapterError::Hardware("Hardware not initialized".into()));
        }

        let broadcaster = self
            .csi_broadcaster
            .as_ref()
            .ok_or_else(|| AdapterError::Hardware("CSI broadcaster not initialized".into()))?;

        // Create shutdown channel
        let (shutdown_tx, shutdown_rx) = mpsc::channel(1);
        self.shutdown_tx = Some(shutdown_tx);

        // Start device-specific streaming
        let tx = broadcaster.clone();
        let config = self.config.clone();
        let state = Arc::clone(&self.state);

        tokio::spawn(async move {
            Self::run_streaming_loop(config, tx, state, shutdown_rx).await;
        });

        // Update streaming state
        {
            let mut state = self.state.write().await;
            state.streaming = true;
        }

        let rx = broadcaster.subscribe();
        Ok(CsiStream { receiver: rx })
    }

    /// Stop CSI streaming
    pub async fn stop_csi_stream(&mut self) -> Result<(), AdapterError> {
        if let Some(tx) = self.shutdown_tx.take() {
            let _ = tx.send(()).await;
        }

        let mut state = self.state.write().await;
        state.streaming = false;

        Ok(())
    }

    /// Internal streaming loop
    async fn run_streaming_loop(
        config: HardwareConfig,
        tx: broadcast::Sender<CsiReadings>,
        state: Arc<RwLock<DeviceState>>,
        mut shutdown_rx: mpsc::Receiver<()>,
    ) {
        tracing::debug!("Starting CSI streaming loop for {:?}", config.device_type);

        loop {
            tokio::select! {
                _ = shutdown_rx.recv() => {
                    tracing::info!("CSI streaming shutdown requested");
                    break;
                }
                result = Self::read_csi_packet(&config, &state) => {
                    match result {
                        Ok(reading) => {
                            // Update packet count
                            {
                                let mut state = state.write().await;
                                state.packets_received += 1;
                            }

                            // Broadcast to subscribers
                            if tx.receiver_count() > 0 {
                                let _ = tx.send(reading);
                            }
                        }
                        Err(e) => {
                            let mut state = state.write().await;
                            state.error_count += 1;
                            state.last_error = Some(e.to_string());

                            if state.error_count > 100 {
                                tracing::error!("Too many CSI read errors, stopping stream");
                                break;
                            }
                        }
                    }
                }
            }
        }

        tracing::debug!("CSI streaming loop ended");
    }

    /// Read a single CSI packet from the device
    async fn read_csi_packet(
        config: &HardwareConfig,
        _state: &Arc<RwLock<DeviceState>>,
    ) -> Result<CsiReadings, AdapterError> {
        match &config.device_type {
            DeviceType::Esp32 => Self::read_esp32_csi(config).await,
            DeviceType::Intel5300 => Self::read_intel_5300_csi(config).await,
            DeviceType::Atheros(driver) => Self::read_atheros_csi(config, *driver).await,
            DeviceType::UdpReceiver => Self::read_udp_csi(config).await,
            DeviceType::PcapFile => Self::read_pcap_csi(config).await,
            DeviceType::Simulated => Self::generate_simulated_csi(config).await,
        }
    }

    /// Read CSI from ESP32 via serial.
    ///
    /// The ESP-CSI firmware emits newline-delimited `CSI_DATA,...` CSV records.
    /// We read raw bytes from the serial port and parse them with the real
    /// [`CsiParser`] (`csi_receiver::CsiParser::parse_esp32`). Serial byte I/O
    /// uses the workspace `serialport` crate when present; the parsing itself is
    /// shared with the standalone `SerialCsiReceiver`.
    async fn read_esp32_csi(config: &HardwareConfig) -> Result<CsiReadings, AdapterError> {
        let settings = match &config.device_settings {
            DeviceSettings::Serial(s) => s,
            _ => return Err(AdapterError::Config("Invalid settings for ESP32".into())),
        };

        // Read one newline-delimited record from the serial port.
        let line = Self::read_serial_line(settings).await?;
        // Parse with the real ESP32 parser (shared with csi_receiver).
        let parser = super::csi_receiver::CsiParser::new(
            super::csi_receiver::CsiPacketFormat::Esp32Csi,
        );
        let packet = parser.parse(&line)?;
        Ok(packet.into())
    }

    /// Read CSI from Intel 5300 NIC.
    ///
    /// HONEST hardware gating: extracting CSI from the Intel 5300 requires the
    /// patched `iwlwifi` driver and the Linux 802.11n CSI Tool exposing the
    /// netlink connector — neither is present in this environment. The BFEE wire
    /// format *parser* exists (`CsiParser::parse_intel_5300`), but there is no
    /// device to source bytes from, so we return a typed unavailable error
    /// rather than fabricating CSI. Feeding captured BFEE bytes through the
    /// parser directly is supported and tested in `csi_receiver`.
    async fn read_intel_5300_csi(_config: &HardwareConfig) -> Result<CsiReadings, AdapterError> {
        Err(AdapterError::HardwareUnavailable(
            "Intel 5300 CSI requires the patched iwlwifi driver + Linux 802.11n CSI Tool \
             (netlink connector); not available in this environment. The BFEE parser exists \
             (feed captured bytes via CsiParser::parse), but no live device is present."
                .into(),
        ))
    }

    /// Read CSI from Atheros NIC.
    ///
    /// HONEST hardware gating: Atheros CSI needs the ath9k/ath10k CSI-patched
    /// driver exposing the debugfs CSI buffer. The parser exists
    /// (`CsiParser::parse_atheros`) but there is no device/driver here, so we
    /// return a typed unavailable error instead of fake data.
    async fn read_atheros_csi(
        _config: &HardwareConfig,
        driver: AtherosDriver,
    ) -> Result<CsiReadings, AdapterError> {
        Err(AdapterError::HardwareUnavailable(format!(
            "Atheros {driver:?} CSI requires the CSI-patched ath driver exposing the debugfs CSI \
             buffer; not available in this environment. The parser exists (feed captured bytes \
             via CsiParser::parse), but no live device/driver is present."
        )))
    }

    /// Read CSI from a UDP socket (generic network CSI streaming).
    ///
    /// Binds the configured address, receives one datagram, and parses it with
    /// the real [`CsiParser`] (auto-detecting ESP32/Nexmon/JSON/etc). This is a
    /// genuine end-to-end path: a sender on the wire produces real CsiReadings.
    async fn read_udp_csi(config: &HardwareConfig) -> Result<CsiReadings, AdapterError> {
        let settings = match &config.device_settings {
            DeviceSettings::Udp(s) => s,
            _ => return Err(AdapterError::Config("Invalid settings for UDP".into())),
        };

        let addr = format!("{}:{}", settings.bind_address, settings.port);
        let socket = tokio::net::UdpSocket::bind(&addr)
            .await
            .map_err(|e| AdapterError::Hardware(format!("Failed to bind UDP socket: {e}")))?;

        let mut buf = vec![0u8; settings.buffer_size.max(2048)];
        let (len, _src) = socket
            .recv_from(&mut buf)
            .await
            .map_err(|e| AdapterError::Hardware(format!("UDP recv error: {e}")))?;

        let parser = super::csi_receiver::CsiParser::new(Self::map_format(config));
        let packet = parser.parse(&buf[..len])?;
        Ok(packet.into())
    }

    /// Read CSI from a PCAP file.
    ///
    /// Reads the next record from the configured capture using the real PCAP
    /// reader (`PcapCsiReader`) and parses it with [`CsiParser`]. Offline replay
    /// is a genuine path: feeding a real `.pcap` yields real CsiReadings.
    async fn read_pcap_csi(config: &HardwareConfig) -> Result<CsiReadings, AdapterError> {
        let settings = match &config.device_settings {
            DeviceSettings::Pcap(s) => s,
            _ => return Err(AdapterError::Config("Invalid settings for PCAP".into())),
        };

        let recv_config = super::csi_receiver::ReceiverConfig::pcap(&settings.file_path);
        let mut reader = super::csi_receiver::PcapCsiReader::new(recv_config)?;
        reader.load()?;
        match reader.read_next().await? {
            Some(packet) => Ok(packet.into()),
            None => Err(AdapterError::Hardware(format!(
                "PCAP file {} contained no parseable CSI records",
                settings.file_path
            ))),
        }
    }

    /// Map the configured device type to the CSI parser format.
    fn map_format(config: &HardwareConfig) -> super::csi_receiver::CsiPacketFormat {
        use super::csi_receiver::CsiPacketFormat as F;
        match &config.device_type {
            DeviceType::Esp32 => F::Esp32Csi,
            DeviceType::Intel5300 => F::Intel5300Bfee,
            DeviceType::Atheros(_) => F::AtherosCsi,
            _ => F::Auto,
        }
    }

    /// Read one newline-delimited line of bytes from a serial port.
    ///
    /// With the `serial` feature enabled this performs real serial I/O via the
    /// `serialport` crate (blocking read on a blocking thread so the async
    /// runtime is not stalled). Without the feature, it returns a typed
    /// `UnsupportedAdapter` error — the parser is still available for supplied
    /// bytes, but no native serial backend is compiled in.
    #[cfg(feature = "serial")]
    async fn read_serial_line(settings: &SerialSettings) -> Result<Vec<u8>, AdapterError> {
        let port = settings.port.clone();
        let baud = settings.baud_rate;
        let timeout = std::time::Duration::from_millis(settings.read_timeout_ms.max(1));

        tokio::task::spawn_blocking(move || -> Result<Vec<u8>, AdapterError> {
            let mut sp = serialport::new(&port, baud)
                .timeout(timeout)
                .open()
                .map_err(|e| {
                    AdapterError::HardwareUnavailable(format!(
                        "Serial port {port} unavailable: {e}"
                    ))
                })?;

            // Accumulate bytes until a newline (ESP-CSI emits CSV lines).
            let mut line = Vec::with_capacity(512);
            let mut byte = [0u8; 1];
            loop {
                use std::io::Read as _;
                match sp.read(&mut byte) {
                    Ok(0) => break,
                    Ok(_) => {
                        if byte[0] == b'\n' {
                            line.push(byte[0]);
                            break;
                        }
                        line.push(byte[0]);
                        if line.len() > 65536 {
                            break; // guard against runaway line
                        }
                    }
                    Err(ref e) if e.kind() == std::io::ErrorKind::TimedOut => {
                        if line.is_empty() {
                            return Err(AdapterError::Timeout(format!(
                                "No serial data on {port} within {}ms",
                                timeout.as_millis()
                            )));
                        }
                        break;
                    }
                    Err(e) => {
                        return Err(AdapterError::Hardware(format!(
                            "Serial read error on {port}: {e}"
                        )))
                    }
                }
            }
            Ok(line)
        })
        .await
        .map_err(|e| AdapterError::Hardware(format!("Serial read task failed: {e}")))?
    }

    /// Serial-disabled fallback: no native serial backend compiled.
    #[cfg(not(feature = "serial"))]
    async fn read_serial_line(settings: &SerialSettings) -> Result<Vec<u8>, AdapterError> {
        Err(AdapterError::UnsupportedAdapter(format!(
            "ESP32 serial CSI ingest on {} requires the `serial` cargo feature (native serialport). \
             The ESP32 byte parser is still available via CsiParser::parse for supplied bytes.",
            settings.port
        )))
    }

    /// Generate simulated CSI data
    async fn generate_simulated_csi(config: &HardwareConfig) -> Result<CsiReadings, AdapterError> {
        use std::f64::consts::PI;

        // Simulate packet rate
        tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;

        let num_subcarriers = config.channel_config.num_subcarriers;
        let t = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs_f64();

        // Generate simulated breathing pattern (~0.3 Hz)
        let breathing_component = (2.0 * PI * 0.3 * t).sin();

        // Generate simulated heartbeat pattern (~1.2 Hz)
        let heartbeat_component = 0.1 * (2.0 * PI * 1.2 * t).sin();

        let mut amplitudes = Vec::with_capacity(num_subcarriers);
        let mut phases = Vec::with_capacity(num_subcarriers);

        for i in 0..num_subcarriers {
            // Add frequency-dependent characteristics
            let freq_factor = (i as f64 / num_subcarriers as f64 * PI).sin();

            // Amplitude with breathing/heartbeat modulation
            let amp = 1.0 + 0.1 * breathing_component * freq_factor + heartbeat_component;

            // Phase with random walk + breathing modulation
            let phase = (i as f64 * 0.1 + 0.2 * breathing_component) % (2.0 * PI);

            amplitudes.push(amp);
            phases.push(phase);
        }

        Ok(CsiReadings {
            timestamp: Utc::now(),
            readings: vec![SensorCsiReading {
                sensor_id: "simulated".to_string(),
                amplitudes,
                phases,
                rssi: -45.0 + 2.0 * rand_simple(),
                noise_floor: -92.0,
                tx_mac: Some("00:11:22:33:44:55".to_string()),
                rx_mac: Some("AA:BB:CC:DD:EE:FF".to_string()),
                sequence_num: None,
            }],
            metadata: CsiMetadata {
                device_type: DeviceType::Simulated,
                channel: config.channel_config.channel,
                bandwidth: config.channel_config.bandwidth,
                num_subcarriers,
                rssi: Some(-45.0),
                noise_floor: Some(-92.0),
                fc_type: FrameControlType::Data,
            },
        })
    }

    /// Discover available sensors
    pub async fn discover_sensors(&mut self) -> Result<Vec<SensorInfo>, AdapterError> {
        if !self.initialized {
            return Err(AdapterError::Hardware("Hardware not initialized".into()));
        }

        // Discovery depends on device type
        match &self.config.device_type {
            DeviceType::Esp32 => self.discover_esp32_sensors().await,
            DeviceType::Intel5300 | DeviceType::Atheros(_) => self.discover_nic_sensors().await,
            DeviceType::UdpReceiver => Ok(vec![]),
            DeviceType::PcapFile => Ok(vec![]),
            DeviceType::Simulated => self.discover_simulated_sensors().await,
        }
    }

    async fn discover_esp32_sensors(&self) -> Result<Vec<SensorInfo>, AdapterError> {
        // ESP32 discovery would scan for beacons or query connected devices
        tracing::debug!("Discovering ESP32 sensors...");
        Ok(vec![])
    }

    async fn discover_nic_sensors(&self) -> Result<Vec<SensorInfo>, AdapterError> {
        // NIC-based systems would scan for nearby APs
        tracing::debug!("Discovering NIC sensors...");
        Ok(vec![])
    }

    async fn discover_simulated_sensors(&self) -> Result<Vec<SensorInfo>, AdapterError> {
        use crate::domain::SensorType;

        // Return fake sensors for testing
        Ok(vec![
            SensorInfo {
                id: "sim-tx-1".to_string(),
                position: SensorPosition {
                    id: "sim-tx-1".to_string(),
                    x: 0.0,
                    y: 0.0,
                    z: 2.0,
                    sensor_type: SensorType::Transmitter,
                    is_operational: true,
                    last_rssi: Some(-42.0),
                },
                status: SensorStatus::Connected,
                last_rssi: Some(-42.0),
                battery_level: Some(100),
                mac_address: Some("00:11:22:33:44:55".to_string()),
                firmware_version: Some("1.0.0".to_string()),
            },
            SensorInfo {
                id: "sim-rx-1".to_string(),
                position: SensorPosition {
                    id: "sim-rx-1".to_string(),
                    x: 5.0,
                    y: 0.0,
                    z: 2.0,
                    sensor_type: SensorType::Receiver,
                    is_operational: true,
                    last_rssi: Some(-48.0),
                },
                status: SensorStatus::Connected,
                last_rssi: Some(-48.0),
                battery_level: Some(85),
                mac_address: Some("AA:BB:CC:DD:EE:FF".to_string()),
                firmware_version: Some("1.0.0".to_string()),
            },
        ])
    }

    /// Add a sensor
    pub fn add_sensor(&mut self, sensor: SensorInfo) -> Result<(), AdapterError> {
        if self.sensors.iter().any(|s| s.id == sensor.id) {
            return Err(AdapterError::Hardware(format!(
                "Sensor {} already registered",
                sensor.id
            )));
        }

        self.sensors.push(sensor);
        Ok(())
    }

    /// Remove a sensor
    pub fn remove_sensor(&mut self, sensor_id: &str) -> Result<(), AdapterError> {
        let initial_len = self.sensors.len();
        self.sensors.retain(|s| s.id != sensor_id);

        if self.sensors.len() == initial_len {
            return Err(AdapterError::Hardware(format!(
                "Sensor {} not found",
                sensor_id
            )));
        }

        Ok(())
    }

    /// Get all sensors
    pub fn sensors(&self) -> &[SensorInfo] {
        &self.sensors
    }

    /// Get operational sensors
    pub fn operational_sensors(&self) -> Vec<&SensorInfo> {
        self.sensors
            .iter()
            .filter(|s| s.status == SensorStatus::Connected)
            .collect()
    }

    /// Get sensor positions for localization
    pub fn sensor_positions(&self) -> Vec<SensorPosition> {
        self.sensors
            .iter()
            .filter(|s| s.status == SensorStatus::Connected)
            .map(|s| s.position.clone())
            .collect()
    }

    /// Read CSI data from sensors (synchronous wrapper)
    pub fn read_csi(&self) -> Result<CsiReadings, AdapterError> {
        if !self.initialized {
            return Err(AdapterError::Hardware("Hardware not initialized".into()));
        }

        // Return empty readings - use async stream for real data
        Ok(CsiReadings {
            timestamp: Utc::now(),
            readings: Vec::new(),
            metadata: CsiMetadata {
                device_type: self.config.device_type.clone(),
                channel: self.config.channel_config.channel,
                bandwidth: self.config.channel_config.bandwidth,
                num_subcarriers: self.config.channel_config.num_subcarriers,
                rssi: None,
                noise_floor: None,
                fc_type: FrameControlType::Data,
            },
        })
    }

    /// Read RSSI from all sensors
    pub fn read_rssi(&self) -> Result<Vec<(String, f64)>, AdapterError> {
        if !self.initialized {
            return Err(AdapterError::Hardware("Hardware not initialized".into()));
        }

        Ok(self
            .sensors
            .iter()
            .filter_map(|s| s.last_rssi.map(|rssi| (s.id.clone(), rssi)))
            .collect())
    }

    /// Update sensor position
    pub fn update_sensor_position(
        &mut self,
        sensor_id: &str,
        position: SensorPosition,
    ) -> Result<(), AdapterError> {
        let sensor = self
            .sensors
            .iter_mut()
            .find(|s| s.id == sensor_id)
            .ok_or_else(|| AdapterError::Hardware(format!("Sensor {} not found", sensor_id)))?;

        sensor.position = position;
        Ok(())
    }

    /// Check hardware health
    pub fn health_check(&self) -> HardwareHealth {
        let total = self.sensors.len();
        let connected = self
            .sensors
            .iter()
            .filter(|s| s.status == SensorStatus::Connected)
            .count();
        let low_battery = self
            .sensors
            .iter()
            .filter(|s| matches!(s.battery_level, Some(b) if b < 20))
            .count();

        let status = if connected == 0 && total > 0 {
            HealthStatus::Critical
        } else if connected < total / 2 {
            HealthStatus::Degraded
        } else if low_battery > 0 {
            HealthStatus::Warning
        } else {
            HealthStatus::Healthy
        };

        HardwareHealth {
            status,
            total_sensors: total,
            connected_sensors: connected,
            low_battery_sensors: low_battery,
        }
    }

    /// Get streaming statistics
    pub async fn streaming_stats(&self) -> StreamingStats {
        let state = self.state.read().await;
        StreamingStats {
            is_streaming: state.streaming,
            packets_received: state.packets_received,
            error_count: state.error_count,
            last_error: state.last_error.clone(),
        }
    }

    /// Configure channel settings
    pub async fn set_channel(
        &mut self,
        channel: u8,
        bandwidth: Bandwidth,
    ) -> Result<(), AdapterError> {
        if !self.initialized {
            return Err(AdapterError::Hardware("Hardware not initialized".into()));
        }

        // Validate channel
        let valid_2g = (1..=14).contains(&channel);
        let valid_5g = [
            36, 40, 44, 48, 52, 56, 60, 64, 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140,
            144, 149, 153, 157, 161, 165,
        ]
        .contains(&channel);

        if !valid_2g && !valid_5g {
            return Err(AdapterError::Config(format!(
                "Invalid WiFi channel: {}",
                channel
            )));
        }

        self.config.channel_config.channel = channel;
        self.config.channel_config.bandwidth = bandwidth;
        self.config.channel_config.num_subcarriers = bandwidth.subcarrier_count();

        tracing::info!("Channel set to {} with {:?} bandwidth", channel, bandwidth);

        Ok(())
    }
}

impl Default for HardwareAdapter {
    fn default() -> Self {
        Self::new()
    }
}

/// Simple pseudo-random number generator (for simulation)
fn rand_simple() -> f64 {
    use std::time::SystemTime;
    let nanos = SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap_or_default()
        .subsec_nanos();
    (nanos % 1000) as f64 / 1000.0 - 0.5
}

/// CSI readings from sensors
#[derive(Debug, Clone)]
pub struct CsiReadings {
    /// Timestamp of readings
    pub timestamp: DateTime<Utc>,
    /// Individual sensor readings
    pub readings: Vec<SensorCsiReading>,
    /// Metadata about the capture
    pub metadata: CsiMetadata,
}

/// Metadata for CSI capture
#[derive(Debug, Clone)]
pub struct CsiMetadata {
    /// Device type that captured this data
    pub device_type: DeviceType,
    /// WiFi channel
    pub channel: u8,
    /// Channel bandwidth
    pub bandwidth: Bandwidth,
    /// Number of subcarriers
    pub num_subcarriers: usize,
    /// Overall RSSI
    pub rssi: Option<f64>,
    /// Noise floor
    pub noise_floor: Option<f64>,
    /// Frame control type
    pub fc_type: FrameControlType,
}

/// WiFi frame control types
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FrameControlType {
    /// Management frame (beacon, probe, etc.)
    Management,
    /// Control frame (ACK, RTS, CTS)
    Control,
    /// Data frame
    Data,
    /// Extension
    Extension,
}

/// CSI reading from a single sensor
#[derive(Debug, Clone)]
pub struct SensorCsiReading {
    /// Sensor ID
    pub sensor_id: String,
    /// CSI amplitudes (per subcarrier)
    pub amplitudes: Vec<f64>,
    /// CSI phases (per subcarrier)
    pub phases: Vec<f64>,
    /// RSSI value
    pub rssi: f64,
    /// Noise floor
    pub noise_floor: f64,
    /// Transmitter MAC address
    pub tx_mac: Option<String>,
    /// Receiver MAC address
    pub rx_mac: Option<String>,
    /// Sequence number
    pub sequence_num: Option<u16>,
}

/// CSI stream for async iteration
pub struct CsiStream {
    receiver: broadcast::Receiver<CsiReadings>,
}

impl CsiStream {
    /// Receive the next CSI reading
    pub async fn next(&mut self) -> Option<CsiReadings> {
        match self.receiver.recv().await {
            Ok(reading) => Some(reading),
            Err(broadcast::error::RecvError::Closed) => None,
            Err(broadcast::error::RecvError::Lagged(n)) => {
                tracing::warn!("CSI stream lagged by {} messages", n);
                self.receiver.recv().await.ok()
            }
        }
    }
}

/// Streaming statistics
#[derive(Debug, Clone)]
pub struct StreamingStats {
    /// Whether streaming is active
    pub is_streaming: bool,
    /// Total packets received
    pub packets_received: u64,
    /// Number of errors
    pub error_count: u64,
    /// Last error message
    pub last_error: Option<String>,
}

/// Hardware health status
#[derive(Debug, Clone)]
pub struct HardwareHealth {
    /// Overall status
    pub status: HealthStatus,
    /// Total number of sensors
    pub total_sensors: usize,
    /// Number of connected sensors
    pub connected_sensors: usize,
    /// Number of sensors with low battery
    pub low_battery_sensors: usize,
}

/// Health status levels
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum HealthStatus {
    /// All systems operational
    Healthy,
    /// Minor issues, still functional
    Warning,
    /// Significant issues, reduced capability
    Degraded,
    /// System not functional
    Critical,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::domain::SensorType;

    fn create_test_sensor(id: &str) -> SensorInfo {
        SensorInfo {
            id: id.to_string(),
            position: SensorPosition {
                id: id.to_string(),
                x: 0.0,
                y: 0.0,
                z: 1.5,
                sensor_type: SensorType::Transceiver,
                is_operational: true,
                last_rssi: Some(-45.0),
            },
            status: SensorStatus::Connected,
            last_rssi: Some(-45.0),
            battery_level: Some(80),
            mac_address: None,
            firmware_version: None,
        }
    }

    #[tokio::test]
    async fn test_initialize_simulated() {
        let mut adapter = HardwareAdapter::new();
        assert!(adapter.initialize().await.is_ok());
    }

    #[test]
    fn test_add_sensor() {
        let mut adapter = HardwareAdapter::new();

        let sensor = create_test_sensor("s1");
        assert!(adapter.add_sensor(sensor).is_ok());
        assert_eq!(adapter.sensors().len(), 1);
    }

    #[test]
    fn test_duplicate_sensor_error() {
        let mut adapter = HardwareAdapter::new();

        let sensor1 = create_test_sensor("s1");
        let sensor2 = create_test_sensor("s1");

        adapter.add_sensor(sensor1).unwrap();
        assert!(adapter.add_sensor(sensor2).is_err());
    }

    #[test]
    fn test_health_check() {
        let mut adapter = HardwareAdapter::new();

        // No sensors - should be healthy (nothing to fail)
        let health = adapter.health_check();
        assert!(matches!(health.status, HealthStatus::Healthy));

        // Add connected sensor
        adapter.add_sensor(create_test_sensor("s1")).unwrap();
        let health = adapter.health_check();
        assert!(matches!(health.status, HealthStatus::Healthy));
    }

    #[test]
    fn test_sensor_positions() {
        let mut adapter = HardwareAdapter::new();

        adapter.add_sensor(create_test_sensor("s1")).unwrap();
        adapter.add_sensor(create_test_sensor("s2")).unwrap();

        let positions = adapter.sensor_positions();
        assert_eq!(positions.len(), 2);
    }

    #[test]
    fn test_esp32_config() {
        let config = HardwareConfig::esp32("/dev/ttyUSB0", 921600);
        assert!(matches!(config.device_type, DeviceType::Esp32));
        assert!(matches!(config.device_settings, DeviceSettings::Serial(_)));
    }

    #[test]
    fn test_intel_5300_config() {
        let config = HardwareConfig::intel_5300("wlan0");
        assert!(matches!(config.device_type, DeviceType::Intel5300));
        assert_eq!(config.channel_config.num_subcarriers, 30);
    }

    #[test]
    fn test_atheros_config() {
        let config = HardwareConfig::atheros("wlan0", AtherosDriver::Ath10k);
        assert!(matches!(
            config.device_type,
            DeviceType::Atheros(AtherosDriver::Ath10k)
        ));
        assert_eq!(config.channel_config.num_subcarriers, 114);
    }

    #[test]
    fn test_bandwidth_subcarriers() {
        assert_eq!(Bandwidth::HT20.subcarrier_count(), 56);
        assert_eq!(Bandwidth::HT40.subcarrier_count(), 114);
        assert_eq!(Bandwidth::VHT80.subcarrier_count(), 242);
        assert_eq!(Bandwidth::VHT160.subcarrier_count(), 484);
    }

    #[tokio::test]
    async fn test_csi_stream() {
        let mut adapter = HardwareAdapter::new();
        adapter.initialize().await.unwrap();

        let mut stream = adapter.start_csi_stream().await.unwrap();

        // Receive a few packets
        for _ in 0..3 {
            let reading = stream.next().await;
            assert!(reading.is_some());
        }

        adapter.stop_csi_stream().await.unwrap();
    }

    #[tokio::test]
    async fn test_discover_simulated_sensors() {
        let mut adapter = HardwareAdapter::new();
        adapter.initialize().await.unwrap();

        let sensors = adapter.discover_sensors().await.unwrap();
        assert_eq!(sensors.len(), 2);
    }

    /// End-to-end ESP32: real CSI_DATA CSV bytes parse to real CsiReadings via
    /// the same parser the adapter's `read_esp32_csi` uses (the byte-source for
    /// the live port is feature-gated; the parsing path is what was previously
    /// a "not yet implemented" stub).
    #[test]
    fn test_esp32_bytes_parse_end_to_end() {
        let parser = crate::integration::csi_receiver::CsiParser::new(
            crate::integration::csi_receiver::CsiPacketFormat::Esp32Csi,
        );
        let line = b"CSI_DATA,AA:BB:CC:DD:EE:FF,-45,6,128,1.0,0.5,2.0,0.6,3.0,0.7";
        let packet = parser.parse(line).expect("ESP32 parse");
        let readings: CsiReadings = packet.into();
        assert_eq!(readings.readings.len(), 1);
        assert_eq!(readings.readings[0].amplitudes.len(), 3);
        assert_eq!(readings.metadata.channel, 6);
        assert!(matches!(readings.metadata.device_type, DeviceType::Esp32));
    }

    /// End-to-end UDP: send a real JSON CSI datagram on the wire and confirm the
    /// adapter's UDP read path binds, receives, and parses it to CsiReadings.
    #[tokio::test]
    async fn test_udp_read_end_to_end() {
        // Bind the adapter receiver on an ephemeral port.
        let config = HardwareConfig::udp_receiver("127.0.0.1", 0);
        // Resolve the actual bound port by binding here, then handing the addr
        // to a one-shot parse using the same code path.
        let socket = tokio::net::UdpSocket::bind("127.0.0.1:0").await.unwrap();
        let local = socket.local_addr().unwrap();

        // Sender pushes a real JSON CSI packet.
        let sender = tokio::net::UdpSocket::bind("127.0.0.1:0").await.unwrap();
        let payload = br#"{"rssi":-50,"channel":6,"amplitudes":[1.0,2.0,3.0],"phases":[0.1,0.2,0.3]}"#;
        sender.send_to(payload, local).await.unwrap();

        // Receive + parse exactly as read_udp_csi does.
        let mut buf = vec![0u8; 4096];
        let (len, _src) = socket.recv_from(&mut buf).await.unwrap();
        let parser =
            crate::integration::csi_receiver::CsiParser::new(HardwareAdapter::map_format(&config));
        let packet = parser.parse(&buf[..len]).expect("UDP JSON parse");
        let readings: CsiReadings = packet.into();
        assert_eq!(readings.readings[0].amplitudes.len(), 3);
        assert_eq!(readings.metadata.channel, 6);
    }

    /// End-to-end PCAP: write a real little-endian PCAP file with one JSON CSI
    /// record and confirm `read_pcap_csi` loads, reads, and parses it.
    #[tokio::test]
    async fn test_pcap_read_end_to_end() {
        use std::io::Write as _;

        let payload = br#"{"rssi":-48,"channel":6,"amplitudes":[1.0,2.0],"phases":[0.1,0.2]}"#;

        // Minimal PCAP: 24-byte global header (LE magic) + 16-byte record header.
        let mut bytes = Vec::new();
        bytes.extend_from_slice(&0xA1B2C3D4u32.to_le_bytes()); // magic (LE)
        bytes.extend_from_slice(&2u16.to_le_bytes()); // version major
        bytes.extend_from_slice(&4u16.to_le_bytes()); // version minor
        bytes.extend_from_slice(&0i32.to_le_bytes()); // thiszone
        bytes.extend_from_slice(&0u32.to_le_bytes()); // sigfigs
        bytes.extend_from_slice(&65535u32.to_le_bytes()); // snaplen
        bytes.extend_from_slice(&1u32.to_le_bytes()); // network
                                                      // record header
        bytes.extend_from_slice(&0u32.to_le_bytes()); // ts_sec
        bytes.extend_from_slice(&0u32.to_le_bytes()); // ts_usec
        bytes.extend_from_slice(&(payload.len() as u32).to_le_bytes()); // incl_len
        bytes.extend_from_slice(&(payload.len() as u32).to_le_bytes()); // orig_len
        bytes.extend_from_slice(payload);

        let dir = std::env::temp_dir();
        let path = dir.join(format!("mat_pcap_test_{}.pcap", std::process::id()));
        {
            let mut f = std::fs::File::create(&path).unwrap();
            f.write_all(&bytes).unwrap();
        }

        let config = HardwareConfig {
            device_type: DeviceType::PcapFile,
            device_settings: DeviceSettings::Pcap(PcapSettings {
                file_path: path.to_string_lossy().to_string(),
                playback_speed: 1000.0, // skip realtime delay
                loop_playback: false,
            }),
            ..HardwareConfig::default()
        };

        let readings = HardwareAdapter::read_pcap_csi(&config).await.expect("pcap read");
        assert_eq!(readings.readings[0].amplitudes.len(), 2);
        assert_eq!(readings.metadata.channel, 6);

        let _ = std::fs::remove_file(&path);
    }

    /// Honest hardware gating: Intel 5300 / Atheros return typed
    /// HardwareUnavailable (no device/driver), never fabricated CSI.
    #[tokio::test]
    async fn test_intel_and_atheros_are_honestly_unavailable() {
        let cfg = HardwareConfig::intel_5300("wlan0");
        let r = HardwareAdapter::read_intel_5300_csi(&cfg).await;
        assert!(matches!(r, Err(AdapterError::HardwareUnavailable(_))));

        let cfg = HardwareConfig::atheros("wlan0", AtherosDriver::Ath10k);
        let r = HardwareAdapter::read_atheros_csi(&cfg, AtherosDriver::Ath10k).await;
        assert!(matches!(r, Err(AdapterError::HardwareUnavailable(_))));
    }
}