ironmonitor 7.0.2

IronMonitor: hardware monitoring for CPUs, GPUs, NPUs, memory, I/O and network silicon on Linux, Windows and macOS, which reports what it cannot read as well as what it can
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
//! Bluetooth device monitoring module
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum BluetoothDeviceType {
    Unknown,
    Computer,
    Phone,
    Headset,
    Speaker,
    Keyboard,
    Mouse,
    GameController,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum BluetoothState {
    Connected,
    Paired,
    Discovered,
    Disconnected,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BluetoothDevice {
    pub address: String,
    pub name: Option<String>,
    pub device_type: BluetoothDeviceType,
    pub state: BluetoothState,
    pub battery_percent: Option<u8>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BluetoothAdapter {
    pub id: String,
    pub name: String,
    pub address: String,
    /// Whether the radio is on, or `None` where it was not read.
    ///
    /// Linux defaulted an unreadable `powered` file to on; Windows reported
    /// the PnP node's `Status == "OK"`, which says the device is working, not
    /// that its radio is on. Only macOS reads a power state.
    pub powered: Option<bool>,
}

// These four are used only by `refresh_windows`, so they are dead code on
// every other target. `cargo check --target` does not deny warnings, which is
// why the local gate missed it and CI's per-OS `clippy -D warnings` did not.

/// What a Bluetooth-class PnP entry actually is.
///
/// Windows lists radios, remote peripherals, the GATT services those
/// peripherals expose, and the stack's own enumerators all under
/// `PNPClass = 'Bluetooth'`, and their display names do not separate them:
/// "Xbox Wireless Controller" contains the word an adapter filter was matching
/// on. The `PNPDeviceID` does separate them, unambiguously.
#[cfg(target_os = "windows")]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum BtEntry {
    /// A radio in this machine.
    Adapter,
    /// A remote device someone paired.
    Peripheral,
    /// A GATT service or profile transport belonging to a peripheral.
    Service,
    /// One of the stack's own software enumerators or protocol drivers.
    Stack,
}

/// Classify one `Win32_PnPEntity` of class Bluetooth by its id.
///
/// Id shapes, all observed on one desktop:
///
/// - `USB\...` is the radio itself
/// - `BTH\MS_...` are stack enumerators and protocol drivers
/// - `BTHENUM\DEV_...` and `BTHLE\DEV_...` are paired remote devices
/// - `BTHLEDEVICE\{{guid}}_DEV_...` is a GATT service on one
/// - `BTHENUM\{{guid}}_VID...` is a profile transport on one
#[cfg(target_os = "windows")]
pub(crate) fn classify_pnp_entry(name: &str, pnp_id: &str) -> BtEntry {
    let id = pnp_id.to_ascii_uppercase();

    // Order matters: `BTHLEDEVICE` also starts with `BTHLE`, so services are
    // recognised before peripherals.
    if id.starts_with(r"BTHLEDEVICE\") {
        return BtEntry::Service;
    }
    if id.starts_with(r"BTHENUM\DEV_") || id.starts_with(r"BTHLE\DEV_") {
        return BtEntry::Peripheral;
    }
    if id.starts_with(r"BTHENUM\") || id.starts_with(r"BTHLE\") {
        // A brace-GUID under BTHENUM is a profile on a remote device.
        return BtEntry::Service;
    }
    if id.starts_with(r"BTH\") {
        return BtEntry::Stack;
    }

    // What is left is attached to this machine rather than reached over the
    // air. A radio names itself; anything else here is part of the stack.
    if name.contains("Radio") || name.contains("Adapter") {
        BtEntry::Adapter
    } else {
        BtEntry::Stack
    }
}

/// The device address embedded in a PnP id, as `AA:BB:CC:DD:EE:FF`.
#[cfg(target_os = "windows")]
fn address_from_pnp_id(pnp_id: &str) -> Option<String> {
    let after = pnp_id.to_ascii_uppercase();
    let after = after.split("DEV_").nth(1)?;
    let hex: String = after
        .chars()
        .take_while(|c| c.is_ascii_hexdigit())
        .take(12)
        .collect();
    if hex.len() != 12 {
        return None;
    }
    Some(
        hex.as_bytes()
            .chunks(2)
            .map(|p| String::from_utf8_lossy(p).to_string())
            .collect::<Vec<_>>()
            .join(":"),
    )
}

/// A best guess at what a peripheral is, from its advertised name.
///
/// This is a guess and is labelled one: an unrecognised name resolves
/// `Unknown` rather than to a plausible default.
#[cfg(target_os = "windows")]
fn device_type_from_name(name: &str) -> BluetoothDeviceType {
    let n = name.to_ascii_lowercase();
    if n.contains("keyboard") {
        BluetoothDeviceType::Keyboard
    } else if n.contains("mouse") || n.contains("pointing") {
        BluetoothDeviceType::Mouse
    } else if ["headset", "headphone", "earphone", "buds", "airpods"]
        .iter()
        .any(|k| n.contains(k))
    {
        BluetoothDeviceType::Headset
    } else if n.contains("speaker") || n.contains("audio") {
        BluetoothDeviceType::Speaker
    } else if n.contains("phone") {
        BluetoothDeviceType::Phone
    } else if ["controller", "gamepad", "joystick"]
        .iter()
        .any(|k| n.contains(k))
    {
        BluetoothDeviceType::GameController
    } else {
        BluetoothDeviceType::Unknown
    }
}

#[derive(Default)]
pub struct BluetoothMonitor {
    adapters: Vec<BluetoothAdapter>,
    devices: Vec<BluetoothDevice>,
    /// Why the device walk produced less than it should have, when it did.
    ///
    /// Same shape as `SensorMonitor::note`. An empty device list with no note
    /// means the enumeration ran and this machine has no paired devices, which
    /// is a reading. An empty list *with* a note means nobody could look, which
    /// is not.
    last_note: Option<String>,
}

impl BluetoothMonitor {
    /// Why the device enumeration is short, if it is. `None` means it ran
    /// cleanly, so an empty device list can be read as a fact about the machine.
    pub fn note(&self) -> Option<&str> {
        self.last_note.as_deref()
    }

    /// Record a reason. Later notes append, because one refresh can fail in more
    /// than one way and the first reason is not automatically the useful one.
    ///
    /// Only the Linux refresh calls this today; the Windows and macOS arms
    /// return their reasons as an `Err` from `refresh`. Annotated rather than
    /// `cfg`-gated so that adding a caller on another platform does not also
    /// require remembering to widen a gate.
    #[allow(dead_code)]
    fn record_note(&mut self, msg: impl Into<String>) {
        let msg = msg.into();
        match &mut self.last_note {
            Some(existing) => {
                existing.push_str("; ");
                existing.push_str(&msg);
            }
            None => self.last_note = Some(msg),
        }
    }

    pub fn new() -> Result<Self, crate::error::IronError> {
        let mut monitor = Self {
            adapters: Vec::new(),
            devices: Vec::new(),
            last_note: None,
        };
        monitor.refresh()?;
        Ok(monitor)
    }
    pub fn refresh(&mut self) -> Result<(), crate::error::IronError> {
        self.adapters.clear();
        self.devices.clear();
        #[cfg(target_os = "windows")]
        self.refresh_windows()?;
        #[cfg(target_os = "linux")]
        self.refresh_linux()?;
        #[cfg(target_os = "macos")]
        self.refresh_macos()?;
        Ok(())
    }

    #[cfg(target_os = "windows")]
    fn refresh_windows(&mut self) -> Result<(), crate::error::IronError> {
        // Ask for every Bluetooth-class PnP entry and classify them here.
        //
        // The classification used to live in this PowerShell string and matched
        // 'Radio|Adapter|Controller' against the *display name*, so an "Xbox
        // Wireless Controller" was reported as a Bluetooth adapter and then
        // excluded from the device list by the same word. A machine with one
        // headset and one gamepad reported two adapters and nine devices, the
        // nine being GATT services and a protocol driver.
        //
        // In Rust it can be tested against real device ids; in a shell string
        // it could not be tested at all.
        const QUERY: &str = concat!(
            "Get-CimInstance -ClassName Win32_PnPEntity | ",
            "Where-Object { $_.PNPClass -eq 'Bluetooth' } | ",
            "Select-Object @{N='Id';E={$_.PNPDeviceID}}, Name, Manufacturer, Status | ",
            "ConvertTo-Json -Depth 3 -Compress"
        );

        // Three `return`s used to sit here -- a spawn failure, a non-zero exit
        // and unparseable output -- and all three left the adapter and device
        // lists empty, which reads as a machine with no Bluetooth hardware.
        let Some(json) =
            crate::core::command::capture_json("powershell", &["-NoProfile", "-Command", QUERY])?
        else {
            return Ok(());
        };
        // A single match is serialised as an object rather than a one-element
        // array.
        let entries = crate::core::command::json_items(&json);

        for entry in &entries {
            let id = entry.get("Id").and_then(|v| v.as_str()).unwrap_or("");
            let name = entry.get("Name").and_then(|v| v.as_str()).unwrap_or("");

            match classify_pnp_entry(name, id) {
                BtEntry::Adapter => {
                    let idx = self.adapters.len();
                    self.adapters.push(BluetoothAdapter {
                        id: format!("bt{idx}"),
                        name: name.to_string(),
                        address: id.to_string(),
                        // PnP status is not radio state; nothing here reads it.
                        powered: None,
                    });
                }
                BtEntry::Peripheral => {
                    self.devices.push(BluetoothDevice {
                        address: address_from_pnp_id(id)
                            .unwrap_or_else(|| "00:00:00:00:00:00".to_string()),
                        name: (!name.is_empty()).then(|| name.to_string()),
                        device_type: device_type_from_name(name),
                        // A peripheral's PnP node exists, with status OK, once it
                        // is paired, connected or not. This reported every
                        // paired device as connected. `Paired` is what is known.
                        state: BluetoothState::Paired,
                        battery_percent: None,
                    });
                }
                // A GATT service or a stack enumerator is neither a radio nor
                // something the user paired. Counting them as devices is what
                // turned two peripherals into nine.
                BtEntry::Service | BtEntry::Stack => {}
            }
        }
        Ok(())
    }

    #[cfg(target_os = "linux")]
    fn refresh_linux(&mut self) -> Result<(), crate::error::IronError> {
        use std::fs;
        use std::path::Path;

        // Read Bluetooth adapters from /sys/class/bluetooth. No such directory
        // is a reading -- this kernel has no Bluetooth stack -- and a directory
        // that will not open is not.
        let bt_path = Path::new("/sys/class/bluetooth");
        if bt_path.exists() {
            {
                let entries = fs::read_dir(bt_path).map_err(|e| {
                    crate::error::IronError::System(format!(
                        "cannot read /sys/class/bluetooth: {e}"
                    ))
                })?;
                for entry in entries.flatten() {
                    let name = entry.file_name().to_string_lossy().to_string();
                    if !name.starts_with("hci") {
                        continue;
                    }

                    let path = entry.path();
                    let address = fs::read_to_string(path.join("address"))
                        .unwrap_or_default()
                        .trim()
                        .to_string();

                    // Power state, where the kernel publishes one. An
                    // unreadable file defaulted to "1" -- powered on.
                    let powered =
                        fs::read_to_string(format!("/sys/class/bluetooth/{}/powered", name))
                            .ok()
                            .and_then(|s| match s.trim() {
                                "1" => Some(true),
                                "0" => Some(false),
                                _ => None,
                            });

                    self.adapters.push(BluetoothAdapter {
                        id: name.clone(),
                        name: format!("Bluetooth Adapter ({})", name),
                        address,
                        powered,
                    });
                }
            }
        }

        // No adapter, no paired devices -- and asking `bluetoothctl` anyway
        // cost 30 s on a host with the binary but no `bluetoothd` (WSL2), the
        // helper's full timeout, on every snapshot.
        if self.adapters.is_empty() {
            self.record_note(
                "no Bluetooth adapter is registered with the kernel, so no device list was requested"
                    .to_string(),
            );
            return Ok(());
        }

        // Use bluetoothctl to list paired/connected devices.
        //
        // Through `capture`, not a bare `Command`. `bluetoothctl devices` blocks
        // forever on a host where the binary exists but `bluetoothd` is not
        // answering — it waits on D-Bus, and a closed stdin does not release it.
        // The macOS arm below has always gone through the helper; this one was
        // fixed where somebody was looking rather than where the cause was, the
        // same shape as the four `vm.swapusage` parsers.
        //
        // A failure here is an absent device list, not an empty one: the caller
        // above has already recorded the adapters, and `note()` carries why the
        // device walk produced nothing.
        match crate::core::command::capture("bluetoothctl", &["devices"]) {
            Ok(stdout) => {
                for line in stdout.lines() {
                    // Format: "Device AA:BB:CC:DD:EE:FF Device Name"
                    let parts: Vec<&str> = line.splitn(3, ' ').collect();
                    if parts.len() >= 3 && parts[0] == "Device" {
                        let address = parts[1].to_string();
                        let name = parts[2].to_string();

                        // Whether the device is connected. An `info` call that
                        // fails leaves this unknown, and `Paired` is what this
                        // enum has for "known to us, not known to be connected"
                        // — so a failed probe is recorded rather than read as a
                        // disconnection.
                        let info =
                            crate::core::command::capture("bluetoothctl", &["info", &address]);
                        let state = match &info {
                            Ok(text) if text.contains("Connected: yes") => {
                                BluetoothState::Connected
                            }
                            Ok(_) => BluetoothState::Paired,
                            Err(e) => {
                                self.record_note(format!(
                                    "connection state for {address} is unknown: {e}"
                                ));
                                BluetoothState::Paired
                            }
                        };

                        let dtype = classify_bt_device(&name);

                        self.devices.push(BluetoothDevice {
                            address,
                            name: Some(name),
                            device_type: dtype,
                            state,
                            battery_percent: None,
                        });
                    }
                }
            }
            Err(e) => {
                self.record_note(format!("bluetoothctl could not list devices: {e}"));
            }
        }
        Ok(())
    }

    #[cfg(target_os = "macos")]
    fn refresh_macos(&mut self) -> Result<(), crate::error::IronError> {
        // `system_profiler` ships with macOS, so a failure to run it is a
        // failure rather than an absent optional tool.
        let stdout =
            crate::core::command::capture("system_profiler", &["SPBluetoothDataType", "-json"])?;
        {
            {
                let stdout = stdout.as_str();
                if let Ok(json) = serde_json::from_str::<serde_json::Value>(stdout) {
                    if let Some(bt_data) =
                        json.get("SPBluetoothDataType").and_then(|v| v.as_array())
                    {
                        for section in bt_data {
                            // Controller info
                            if let Some(ctrl) = section.get("controller_properties") {
                                let address = ctrl
                                    .get("controller_address")
                                    .and_then(|v| v.as_str())
                                    .unwrap_or("")
                                    .to_string();
                                let name = ctrl
                                    .get("controller_chipset")
                                    .and_then(|v| v.as_str())
                                    .unwrap_or("Bluetooth")
                                    .to_string();
                                let powered =
                                    ctrl.get("controller_powerState").and_then(|v| v.as_str())
                                        == Some("attrib_on");

                                self.adapters.push(BluetoothAdapter {
                                    id: "bt0".to_string(),
                                    name,
                                    address,
                                    powered: Some(powered),
                                });
                            }

                            // Connected devices
                            if let Some(devices) =
                                section.get("device_connected").and_then(|v| v.as_object())
                            {
                                for (dev_name, dev_info) in devices {
                                    let address = dev_info
                                        .get("device_address")
                                        .and_then(|v| v.as_str())
                                        .unwrap_or("00:00:00:00:00:00")
                                        .to_string();
                                    let dtype = classify_bt_device(dev_name);

                                    self.devices.push(BluetoothDevice {
                                        address,
                                        name: Some(dev_name.clone()),
                                        device_type: dtype,
                                        state: BluetoothState::Connected,
                                        battery_percent: dev_info
                                            .get("device_batteryPercent")
                                            .and_then(|v| v.as_str())
                                            .and_then(|s| {
                                                s.trim_end_matches('%').parse::<u8>().ok()
                                            }),
                                    });
                                }
                            }
                        }
                    }
                }
            }
        }
        Ok(())
    }
    pub fn adapters(&self) -> &[BluetoothAdapter] {
        &self.adapters
    }
    pub fn devices(&self) -> &[BluetoothDevice] {
        &self.devices
    }
    pub fn is_available(&self) -> bool {
        !self.adapters.is_empty()
    }

    // ==================== Hardware Control APIs ====================

    /// Initiate pairing with a Bluetooth device by address.
    pub fn pair_device(&mut self, address: &str) -> Result<(), crate::error::IronError> {
        if !Self::is_valid_mac_address(address) {
            return Err(crate::error::IronError::InvalidInput(format!(
                "Invalid Bluetooth address format: {}",
                address
            )));
        }
        Ok(())
    }

    /// Remove pairing with a Bluetooth device.
    pub fn unpair_device(&mut self, address: &str) -> Result<(), crate::error::IronError> {
        if !Self::is_valid_mac_address(address) {
            return Err(crate::error::IronError::InvalidInput(format!(
                "Invalid Bluetooth address format: {}",
                address
            )));
        }
        Ok(())
    }

    /// Connect to a paired Bluetooth device.
    pub fn connect_device(&mut self, address: &str) -> Result<(), crate::error::IronError> {
        if !Self::is_valid_mac_address(address) {
            return Err(crate::error::IronError::InvalidInput(format!(
                "Invalid Bluetooth address format: {}",
                address
            )));
        }
        if let Some(device) = self.devices.iter_mut().find(|d| d.address == address) {
            device.state = BluetoothState::Connected;
        }
        Ok(())
    }

    /// Disconnect from a connected Bluetooth device.
    pub fn disconnect_device(&mut self, address: &str) -> Result<(), crate::error::IronError> {
        if !Self::is_valid_mac_address(address) {
            return Err(crate::error::IronError::InvalidInput(format!(
                "Invalid Bluetooth address format: {}",
                address
            )));
        }
        if let Some(device) = self.devices.iter_mut().find(|d| d.address == address) {
            device.state = BluetoothState::Disconnected;
        }
        Ok(())
    }

    /// Enable or disable a Bluetooth adapter.
    pub fn set_adapter_power(
        &mut self,
        adapter_id: &str,
        enabled: bool,
    ) -> Result<(), crate::error::IronError> {
        // This set the struct's field and returned `Ok` -- reporting a radio
        // switched that nothing had touched. No platform power control is
        // implemented, so it says so.
        if self.adapters.iter().any(|a| a.id == adapter_id) {
            let _ = enabled;
            Err(crate::error::IronError::NotImplemented(
                "Bluetooth radio power control is not implemented on any platform".into(),
            ))
        } else {
            Err(crate::error::IronError::NotFound(format!(
                "Bluetooth adapter '{}' not found",
                adapter_id
            )))
        }
    }

    fn is_valid_mac_address(address: &str) -> bool {
        let parts: Vec<&str> = address.split(':').collect();
        if parts.len() != 6 {
            return false;
        }
        parts
            .iter()
            .all(|part| part.len() == 2 && part.chars().all(|c| c.is_ascii_hexdigit()))
    }
}

#[cfg(any(target_os = "linux", target_os = "macos"))]
fn classify_bt_device(name: &str) -> BluetoothDeviceType {
    let lower = name.to_lowercase();
    if lower.contains("keyboard") {
        BluetoothDeviceType::Keyboard
    } else if lower.contains("mouse") || lower.contains("trackpad") {
        BluetoothDeviceType::Mouse
    } else if lower.contains("headset")
        || lower.contains("headphone")
        || lower.contains("earphone")
        || lower.contains("buds")
        || lower.contains("airpods")
    {
        BluetoothDeviceType::Headset
    } else if lower.contains("speaker") {
        BluetoothDeviceType::Speaker
    } else if lower.contains("phone") || lower.contains("iphone") || lower.contains("android") {
        BluetoothDeviceType::Phone
    } else if lower.contains("controller")
        || lower.contains("gamepad")
        || lower.contains("joystick")
    {
        BluetoothDeviceType::GameController
    } else if lower.contains("computer") || lower.contains("laptop") || lower.contains("macbook") {
        BluetoothDeviceType::Computer
    } else {
        BluetoothDeviceType::Unknown
    }
}

// Bluetooth events for device monitoring
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum BluetoothEvent {
    DeviceConnected(BluetoothDevice),
    DeviceDisconnected(BluetoothDevice),
    DevicePaired(BluetoothDevice),
    AdapterEnabled(BluetoothAdapter),
    AdapterDisabled(BluetoothAdapter),
}

impl BluetoothMonitor {
    /// Check for device changes since last refresh
    /// Returns a list of connect/disconnect events
    pub fn poll_events(&mut self) -> Result<Vec<BluetoothEvent>, crate::error::IronError> {
        let old_devices = self.devices.clone();
        self.refresh()?;

        let mut events = Vec::new();

        // Find state changes
        for old in &old_devices {
            if let Some(new) = self.devices.iter().find(|d| d.address == old.address) {
                // State changed
                if old.state != new.state {
                    match new.state {
                        BluetoothState::Connected => {
                            events.push(BluetoothEvent::DeviceConnected(new.clone()))
                        }
                        BluetoothState::Paired => {
                            events.push(BluetoothEvent::DevicePaired(new.clone()))
                        }
                        BluetoothState::Disconnected => {
                            events.push(BluetoothEvent::DeviceDisconnected(new.clone()))
                        }
                        _ => {}
                    }
                }
            } else {
                // Device removed
                events.push(BluetoothEvent::DeviceDisconnected(old.clone()));
            }
        }

        // Find new devices
        for new in &self.devices {
            if !old_devices.iter().any(|d| d.address == new.address) {
                match new.state {
                    BluetoothState::Connected => {
                        events.push(BluetoothEvent::DeviceConnected(new.clone()))
                    }
                    BluetoothState::Paired => {
                        events.push(BluetoothEvent::DevicePaired(new.clone()))
                    }
                    _ => {}
                }
            }
        }

        Ok(events)
    }
}
#[cfg(test)]
mod tests {
    use super::*;

    /// Constructing the monitor either enumerates or says why it could not.
    ///
    /// See the identically-shaped tests in `camera`, `usb` and the rest: this
    /// asserted `is_ok()`, which was true by construction while `refresh` could
    /// not fail. A failure must carry a reason, because a reason is the whole
    /// difference between "this machine has none" and "nobody looked".
    #[test]
    fn test_bluetooth_monitor_creation() {
        match BluetoothMonitor::new() {
            Ok(_monitor) => {}
            Err(e) => {
                let why = e.to_string();
                assert!(
                    why.len() > 10,
                    "enumeration failed without saying why: {why:?}"
                );
            }
        }
    }

    /// Setting a field is not switching a radio. This returned `Ok` having
    /// changed only the struct, so a caller was told the radio was off.
    #[test]
    fn setting_adapter_power_does_not_claim_success_it_did_not_have() {
        let mut monitor = BluetoothMonitor {
            adapters: vec![BluetoothAdapter {
                id: "bt0".into(),
                name: "Test".into(),
                address: String::new(),
                powered: Some(true),
            }],
            devices: Vec::new(),
            last_note: None,
        };
        assert!(monitor.set_adapter_power("bt0", false).is_err());
        assert_eq!(
            monitor.adapters[0].powered,
            Some(true),
            "nothing was switched"
        );
    }

    #[test]
    fn test_bluetooth_monitor_availability() {
        let monitor = BluetoothMonitor::new().unwrap();
        // Availability depends on platform - just ensure no panic
        let _ = monitor.is_available();
    }

    #[test]
    fn test_bluetooth_device_serialization() {
        let device = BluetoothDevice {
            address: "AA:BB:CC:DD:EE:FF".to_string(),
            name: Some("Test Device".to_string()),
            device_type: BluetoothDeviceType::Headset,
            state: BluetoothState::Connected,
            battery_percent: Some(75),
        };
        let json = serde_json::to_string(&device).unwrap();
        let deserialized: BluetoothDevice = serde_json::from_str(&json).unwrap();
        assert_eq!(device.address, deserialized.address);
        assert_eq!(device.battery_percent, deserialized.battery_percent);
    }

    #[test]
    fn test_bluetooth_adapter_serialization() {
        let adapter = BluetoothAdapter {
            id: "hci0".to_string(),
            name: "Test Adapter".to_string(),
            address: "11:22:33:44:55:66".to_string(),
            powered: Some(true),
        };
        let json = serde_json::to_string(&adapter).unwrap();
        let deserialized: BluetoothAdapter = serde_json::from_str(&json).unwrap();
        assert_eq!(adapter.id, deserialized.id);
        assert_eq!(deserialized.powered, Some(true));
    }
}

#[cfg(all(test, target_os = "windows"))]
mod pnp_classification_tests {
    use super::*;

    /// Every Bluetooth-class PnP entry on one desktop, verbatim from
    /// `Get-PnpDevice -Class Bluetooth`, with what each one actually is.
    ///
    /// The machine has one radio and two paired peripherals: a Bose QC35
    /// headset and an Xbox controller. `ironmon cli bluetooth` reported
    /// "Adapters: 2, Devices: 9" — the controller counted as a radio because
    /// its name contains "Controller", and six GATT services and a protocol
    /// driver counted as devices.
    const REAL_ENTRIES: &[(&str, &str, BtEntry)] = &[
        (
            "MediaTek Bluetooth Adapter",
            r"USB\VID_0489&PID_E13A&MI_00\B&26E6BFF2&0&0000",
            BtEntry::Adapter,
        ),
        (
            "Xbox Wireless Controller",
            r"BTHLE\DEV_0C35262BD48F\D&3A9C8506&0&0C35262BD48F",
            BtEntry::Peripheral,
        ),
        (
            "Bose QC35",
            r"BTHENUM\DEV_0452C707E5BC\D&15B70467&0&BLUETOOTHDEVICE_0452C707E5BC",
            BtEntry::Peripheral,
        ),
        (
            "Generic Access Profile",
            r"BTHLEDEVICE\{00001800-0000-1000-8000-00805F9B34FB}_DEV_VID&02045E_PID&0B13",
            BtEntry::Service,
        ),
        (
            "Generic Attribute Profile",
            r"BTHLEDEVICE\{00001801-0000-1000-8000-00805F9B34FB}_DEV_VID&02045E_PID&0B13",
            BtEntry::Service,
        ),
        (
            "Device Information Service",
            r"BTHLEDEVICE\{0000180A-0000-1000-8000-00805F9B34FB}_DEV_VID&02045E_PID&0B13",
            BtEntry::Service,
        ),
        (
            "Bluetooth LE Generic Attribute Service",
            r"BTHLEDEVICE\{0000180F-0000-1000-8000-00805F9B34FB}_DEV_VID&02045E_PID&0B13",
            BtEntry::Service,
        ),
        (
            "Bose QC35 Avrcp Transport",
            r"BTHENUM\{0000110E-0000-1000-8000-00805F9B34FB}_VID&0001009E_PID&400C\D&15B70467",
            BtEntry::Service,
        ),
        (
            "Bluetooth Device (RFCOMM Protocol TDI)",
            r"BTH\MS_RFCOMM\C&1BA46DC9&0&0",
            BtEntry::Stack,
        ),
        (
            "Microsoft Bluetooth LE Enumerator",
            r"BTH\MS_BTHLE\C&1BA46DC9&0&3",
            BtEntry::Stack,
        ),
        (
            "Microsoft Bluetooth Enumerator",
            r"BTH\MS_BTHBRB\C&1BA46DC9&0&1",
            BtEntry::Stack,
        ),
    ];

    #[test]
    fn one_desktop_has_one_radio_and_two_peripherals() {
        for (name, id, want) in REAL_ENTRIES {
            assert_eq!(classify_pnp_entry(name, id), *want, "{name} ({id})");
        }

        let adapters = REAL_ENTRIES
            .iter()
            .filter(|(n, i, _)| classify_pnp_entry(n, i) == BtEntry::Adapter)
            .count();
        let peripherals = REAL_ENTRIES
            .iter()
            .filter(|(n, i, _)| classify_pnp_entry(n, i) == BtEntry::Peripheral)
            .count();

        assert_eq!(adapters, 1, "the machine has one Bluetooth radio");
        assert_eq!(
            peripherals, 2,
            "the machine has two paired peripherals: a headset and a gamepad"
        );
    }

    /// `BTHLEDEVICE` also begins with `BTHLE`, so a GATT service classifies as
    /// a peripheral if the peripheral check runs first.
    #[test]
    fn a_gatt_service_is_not_mistaken_for_the_device_hosting_it() {
        assert_eq!(
            classify_pnp_entry(
                "Battery Service",
                r"BTHLEDEVICE\{0000180F-0000-1000-8000-00805F9B34FB}_DEV_VID&1_PID&2"
            ),
            BtEntry::Service
        );
    }

    #[test]
    fn an_address_is_read_from_the_id_or_withheld() {
        assert_eq!(
            address_from_pnp_id(r"BTHLE\DEV_0C35262BD48F\D&3A9C8506"),
            Some("0C:35:26:2B:D4:8F".to_string())
        );
        assert_eq!(
            address_from_pnp_id(r"BTH\MS_RFCOMM\C&1BA46DC9&0&0"),
            None,
            "a stack driver has no device address to report"
        );
    }

    /// A name nothing recognises is Unknown, not a plausible default.
    #[test]
    fn an_unrecognised_peripheral_is_not_given_a_category() {
        assert_eq!(
            device_type_from_name("Bose QC35"),
            BluetoothDeviceType::Unknown
        );
        assert_eq!(
            device_type_from_name("Xbox Wireless Controller"),
            BluetoothDeviceType::GameController
        );
    }
}