strata-device 0.1.1

Native hardware introspection, device telemetry, and reactive hardware signals for Strata
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
//! OS-native hardware telemetry probes for Linux sysfs, macOS, and Windows.
//!
//! Provides real hardware battery, thermal zone, and network introspection
//! without heavy external dependencies.

use crate::mock::{DeviceMockController, DeviceTelemetrySnapshot};
use crate::types::*;
use std::fs;
use std::path::Path;

/// Probes active hardware telemetry from the host operating system.
/// Returns `Some(snapshot)` if real sensors were detected, or `None` on headless/virtual systems.
pub fn probe_hardware_telemetry() -> Option<DeviceTelemetrySnapshot> {
    let mut snapshot = DeviceTelemetrySnapshot::default();
    let mut detected_any = false;

    if let Some(battery) = probe_os_battery() {
        snapshot.battery = battery;
        detected_any = true;
    }

    if let Some(thermal) = probe_os_thermal() {
        snapshot.thermal = thermal;
        detected_any = true;
    }

    if let Some(network) = probe_os_network() {
        snapshot.network = network;
        detected_any = true;
    }

    if detected_any {
        Some(snapshot)
    } else {
        None
    }
}

/// Refreshes reactive hardware signals with live OS sensor readings, if available.
/// Returns true if live hardware sensors were detected and applied.
pub fn refresh_from_hardware() -> bool {
    if let Some(snapshot) = probe_hardware_telemetry() {
        let mock = DeviceMockController::new();
        mock.set_battery(snapshot.battery);
        mock.set_thermal(snapshot.thermal);
        mock.set_network(snapshot.network);
        true
    } else {
        false
    }
}

/// Probes real battery telemetry from host OS.
pub fn probe_os_battery() -> Option<BatteryInfo> {
    #[cfg(target_os = "linux")]
    {
        probe_linux_battery()
    }
    #[cfg(target_os = "macos")]
    {
        probe_macos_battery()
    }
    #[cfg(target_os = "windows")]
    {
        probe_windows_battery()
    }
    #[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
    {
        None
    }
}

/// Probes real thermal state from host OS.
pub fn probe_os_thermal() -> Option<ThermalState> {
    #[cfg(target_os = "linux")]
    {
        probe_linux_thermal()
    }
    #[cfg(target_os = "macos")]
    {
        probe_macos_thermal()
    }
    #[cfg(target_os = "windows")]
    {
        probe_windows_thermal()
    }
    #[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
    {
        None
    }
}

/// Probes real network connectivity and interface type from host OS.
pub fn probe_os_network() -> Option<NetworkStatus> {
    #[cfg(target_os = "linux")]
    {
        probe_linux_network()
    }
    #[cfg(target_os = "macos")]
    {
        probe_macos_network()
    }
    #[cfg(target_os = "windows")]
    {
        probe_windows_network()
    }
    #[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
    {
        None
    }
}

// ---------------------------------------------------------------------------
// Linux sysfs Telemetry Implementation
// ---------------------------------------------------------------------------

#[cfg(target_os = "linux")]
fn probe_linux_battery() -> Option<BatteryInfo> {
    let power_supply_dir = Path::new("/sys/class/power_supply");
    if !power_supply_dir.is_dir() {
        return None;
    }

    let entries = fs::read_dir(power_supply_dir).ok()?;
    let mut found_battery = false;
    let mut level = 1.0f32;
    let mut state = BatteryState::Full;
    let mut is_low_power = false;
    let mut time_to_empty = None;
    let mut time_to_full = None;

    for entry in entries.flatten() {
        let path = entry.path();
        let supply_type = fs::read_to_string(path.join("type"))
            .unwrap_or_default()
            .trim()
            .to_ascii_lowercase();

        if supply_type == "battery" {
            found_battery = true;

            // 1. Capacity [0..100]
            if let Ok(cap_str) = fs::read_to_string(path.join("capacity")) {
                if let Ok(cap_int) = cap_str.trim().parse::<u32>() {
                    level = (cap_int as f32 / 100.0).clamp(0.0, 1.0);
                    if cap_int <= 20 {
                        is_low_power = true;
                    }
                }
            }

            // 2. Status: Charging, Discharging, Full, Not charging
            if let Ok(status_str) = fs::read_to_string(path.join("status")) {
                state = match status_str.trim().to_ascii_lowercase().as_str() {
                    "charging" => BatteryState::Charging,
                    "discharging" => BatteryState::Discharging,
                    "full" => BatteryState::Full,
                    "not charging" => BatteryState::NotCharging,
                    _ => BatteryState::Unknown,
                };
            }

            // 3. Time estimation via energy/charge and power/current
            let power_now = read_sysfs_u64(&path.join("power_now"))
                .or_else(|| read_sysfs_u64(&path.join("current_now")));
            let energy_now = read_sysfs_u64(&path.join("energy_now"))
                .or_else(|| read_sysfs_u64(&path.join("charge_now")));
            let energy_full = read_sysfs_u64(&path.join("energy_full"))
                .or_else(|| read_sysfs_u64(&path.join("charge_full")));

            if let (Some(power), Some(now)) = (power_now, energy_now) {
                if power > 0 {
                    if state == BatteryState::Discharging {
                        time_to_empty = Some(((now as f64 / power as f64) * 3600.0) as u32);
                    } else if state == BatteryState::Charging {
                        if let Some(full) = energy_full {
                            if full > now {
                                time_to_full =
                                    Some((((full - now) as f64 / power as f64) * 3600.0) as u32);
                            }
                        }
                    }
                }
            }
            break;
        }
    }

    if found_battery {
        Some(BatteryInfo {
            level,
            state,
            is_low_power_mode: is_low_power,
            time_to_empty_secs: time_to_empty,
            time_to_full_secs: time_to_full,
        })
    } else {
        None
    }
}

#[cfg(target_os = "linux")]
fn probe_linux_thermal() -> Option<ThermalState> {
    let thermal_dir = Path::new("/sys/class/thermal");
    if !thermal_dir.is_dir() {
        return None;
    }

    let entries = fs::read_dir(thermal_dir).ok()?;
    let mut max_temp_c = 0.0f32;
    let mut found_zone = false;

    for entry in entries.flatten() {
        let file_name = entry.file_name().to_string_lossy().to_string();
        if file_name.starts_with("thermal_zone") {
            let temp_path = entry.path().join("temp");
            if let Ok(temp_str) = fs::read_to_string(temp_path) {
                if let Ok(millidegrees) = temp_str.trim().parse::<i64>() {
                    let deg_c = millidegrees as f32 / 1000.0;
                    // Filter anomalous or uninitialized readings (< 0C or > 150C)
                    if (0.0..=130.0).contains(&deg_c) {
                        found_zone = true;
                        if deg_c > max_temp_c {
                            max_temp_c = deg_c;
                        }
                    }
                }
            }
        }
    }

    if found_zone {
        let state = if max_temp_c >= 95.0 {
            ThermalState::Critical
        } else if max_temp_c >= 80.0 {
            ThermalState::Serious
        } else if max_temp_c >= 65.0 {
            ThermalState::Fair
        } else {
            ThermalState::Nominal
        };
        Some(state)
    } else {
        None
    }
}

#[cfg(target_os = "linux")]
fn probe_linux_network() -> Option<NetworkStatus> {
    let net_dir = Path::new("/sys/class/net");
    if !net_dir.is_dir() {
        return None;
    }

    let entries = fs::read_dir(net_dir).ok()?;
    let mut is_online = false;
    let mut connection_type = ConnectionType::None;

    for entry in entries.flatten() {
        let iface = entry.file_name().to_string_lossy().to_string();
        if iface == "lo" {
            continue;
        }

        let operstate = fs::read_to_string(entry.path().join("operstate"))
            .unwrap_or_default()
            .trim()
            .to_ascii_lowercase();

        if operstate == "up" {
            is_online = true;
            if entry.path().join("wireless").exists() || iface.starts_with("wl") {
                connection_type = ConnectionType::Wifi;
                break; // Prioritize WiFi
            } else if iface.starts_with("en") || iface.starts_with("eth") {
                connection_type = ConnectionType::Ethernet;
            } else if iface.starts_with("ww") {
                connection_type = ConnectionType::Cellular;
            } else if connection_type == ConnectionType::None {
                connection_type = ConnectionType::Unknown;
            }
        }
    }

    Some(NetworkStatus {
        is_online,
        connection_type,
        cellular_generation: None,
        downlink_mbps: if is_online { Some(100.0) } else { Some(0.0) },
        rtt_ms: if is_online { Some(15) } else { None },
        is_metered: connection_type == ConnectionType::Cellular,
    })
}

#[cfg(target_os = "linux")]
fn read_sysfs_u64(path: &Path) -> Option<u64> {
    fs::read_to_string(path).ok()?.trim().parse::<u64>().ok()
}

// ---------------------------------------------------------------------------
// Cross-Platform Power & Sensor Decoders (Tested across all platforms)
// ---------------------------------------------------------------------------

#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Win32SystemPowerStatus {
    pub ac_line_status: u8,
    pub battery_flag: u8,
    pub battery_life_percent: u8,
    pub system_status_flag: u8,
    pub battery_life_time: u32,
    pub battery_full_life_time: u32,
}

/// Decodes Win32 `SYSTEM_POWER_STATUS` into `BatteryInfo`.
pub fn decode_win32_power_status(status: &Win32SystemPowerStatus) -> Option<BatteryInfo> {
    // If no system battery (flag bit 7 = 128) or unknown flag and unknown percent
    if (status.battery_flag & 128) != 0
        || (status.battery_flag == 255 && status.battery_life_percent == 255)
    {
        return None;
    }

    let level = if status.battery_life_percent <= 100 {
        status.battery_life_percent as f32 / 100.0
    } else {
        1.0
    };

    let state = if (status.battery_flag & 8) != 0 {
        BatteryState::Charging
    } else if status.ac_line_status == 1 {
        if status.battery_life_percent >= 99 {
            BatteryState::Full
        } else {
            BatteryState::Charging
        }
    } else if status.ac_line_status == 0 {
        BatteryState::Discharging
    } else {
        BatteryState::Unknown
    };

    let time_to_empty_secs =
        if status.battery_life_time != 0xFFFF_FFFF && status.battery_life_time > 0 {
            Some(status.battery_life_time)
        } else {
            None
        };

    let time_to_full_secs =
        if status.battery_full_life_time != 0xFFFF_FFFF && status.battery_full_life_time > 0 {
            Some(status.battery_full_life_time)
        } else {
            None
        };

    let is_low_power_mode = status.system_status_flag == 1;

    Some(BatteryInfo {
        level,
        state,
        is_low_power_mode,
        time_to_empty_secs,
        time_to_full_secs,
    })
}

#[derive(Debug, Clone, PartialEq)]
pub struct MacOsPowerSourceSnapshot {
    pub current_capacity: u32,
    pub max_capacity: u32,
    pub is_charging: bool,
    pub is_charged: bool,
    pub is_low_power_mode: bool,
    pub time_to_empty_min: Option<i32>,
    pub time_to_full_min: Option<i32>,
    pub power_source_state: String,
}

/// Decodes macOS IOKit power source properties into `BatteryInfo`.
pub fn decode_macos_power_snapshot(snap: &MacOsPowerSourceSnapshot) -> Option<BatteryInfo> {
    if snap.max_capacity == 0 {
        return None;
    }

    let level = (snap.current_capacity as f32 / snap.max_capacity as f32).clamp(0.0, 1.0);

    let state = if snap.is_charged {
        BatteryState::Full
    } else if snap.is_charging {
        BatteryState::Charging
    } else if snap.power_source_state == "AC Power" {
        BatteryState::NotCharging
    } else {
        BatteryState::Discharging
    };

    let time_to_empty_secs = match snap.time_to_empty_min {
        Some(mins) if mins > 0 => Some((mins as u32).saturating_mul(60)),
        _ => None,
    };

    let time_to_full_secs = match snap.time_to_full_min {
        Some(mins) if mins > 0 => Some((mins as u32).saturating_mul(60)),
        _ => None,
    };

    Some(BatteryInfo {
        level,
        state,
        is_low_power_mode: snap.is_low_power_mode,
        time_to_empty_secs,
        time_to_full_secs,
    })
}

// ---------------------------------------------------------------------------
// macOS Native Telemetry Implementation (IOKit / Foundation / SystemConfiguration)
// ---------------------------------------------------------------------------

#[cfg(target_os = "macos")]
fn probe_macos_battery() -> Option<BatteryInfo> {
    use std::ffi::c_void;

    type CFTypeRef = *const c_void;
    type CFArrayRef = *const c_void;
    type CFDictionaryRef = *const c_void;
    type CFStringRef = *const c_void;

    #[link(name = "IOKit", kind = "framework")]
    extern "C" {
        fn IOPSCopyPowerSourcesInfo() -> CFTypeRef;
        fn IOPSCopyPowerSourcesList(blob: CFTypeRef) -> CFArrayRef;
        fn IOPSGetPowerSourceDescription(blob: CFTypeRef, ps: CFTypeRef) -> CFDictionaryRef;
    }

    #[link(name = "CoreFoundation", kind = "framework")]
    extern "C" {
        fn CFArrayGetCount(theArray: CFArrayRef) -> isize;
        fn CFArrayGetValueAtIndex(theArray: CFArrayRef, idx: isize) -> CFTypeRef;
        fn CFDictionaryGetValue(theDict: CFDictionaryRef, key: CFStringRef) -> CFTypeRef;
        fn CFRelease(cf: CFTypeRef);
        fn CFStringCreateWithCString(
            alloc: CFTypeRef,
            c_str: *const std::os::raw::c_char,
            encoding: u32,
        ) -> CFStringRef;
        fn CFNumberGetValue(number: CFTypeRef, theType: isize, valuePtr: *mut c_void) -> bool;
        fn CFBooleanGetValue(boolean: CFTypeRef) -> bool;
        fn CFStringGetCString(
            theString: CFStringRef,
            buffer: *mut std::os::raw::c_char,
            bufferSize: isize,
            encoding: u32,
        ) -> bool;
    }

    unsafe {
        let blob = IOPSCopyPowerSourcesInfo();
        if blob.is_null() {
            return None;
        }

        let list = IOPSCopyPowerSourcesList(blob);
        if list.is_null() {
            CFRelease(blob);
            return None;
        }

        let count = CFArrayGetCount(list);
        if count == 0 {
            CFRelease(list);
            CFRelease(blob);
            return None; // Desktop Mac without battery
        }

        let ps = CFArrayGetValueAtIndex(list, 0);
        let dict = IOPSGetPowerSourceDescription(blob, ps);
        if dict.is_null() {
            CFRelease(list);
            CFRelease(blob);
            return None;
        }

        const K_CF_STRING_ENCODING_UTF8: u32 = 0x0800_0100;
        let make_cf_str = |s: &'static [u8]| -> CFStringRef {
            CFStringCreateWithCString(
                std::ptr::null(),
                s.as_ptr() as *const _,
                K_CF_STRING_ENCODING_UTF8,
            )
        };

        let key_cur = make_cf_str(b"Current Capacity\0");
        let key_max = make_cf_str(b"Max Capacity\0");
        let key_charging = make_cf_str(b"Is Charging\0");
        let key_charged = make_cf_str(b"Is Charged\0");
        let key_empty = make_cf_str(b"Time to Empty\0");
        let key_full = make_cf_str(b"Time to Full Charge\0");
        let key_state = make_cf_str(b"Power Source State\0");

        let mut current_capacity = 0i32;
        let mut max_capacity = 0i32;
        let mut time_to_empty = -1i32;
        let mut time_to_full = -1i32;

        let cur_val = CFDictionaryGetValue(dict, key_cur);
        if !cur_val.is_null() {
            CFNumberGetValue(
                cur_val,
                3, /* kCFNumberSInt32Type */
                &mut current_capacity as *mut _ as *mut c_void,
            );
        }

        let max_val = CFDictionaryGetValue(dict, key_max);
        if !max_val.is_null() {
            CFNumberGetValue(max_val, 3, &mut max_capacity as *mut _ as *mut c_void);
        }

        let charging_val = CFDictionaryGetValue(dict, key_charging);
        let is_charging = !charging_val.is_null() && CFBooleanGetValue(charging_val);

        let charged_val = CFDictionaryGetValue(dict, key_charged);
        let is_charged = !charged_val.is_null() && CFBooleanGetValue(charged_val);

        let empty_val = CFDictionaryGetValue(dict, key_empty);
        if !empty_val.is_null() {
            CFNumberGetValue(empty_val, 3, &mut time_to_empty as *mut _ as *mut c_void);
        }

        let full_val = CFDictionaryGetValue(dict, key_full);
        if !full_val.is_null() {
            CFNumberGetValue(full_val, 3, &mut time_to_full as *mut _ as *mut c_void);
        }

        let mut power_source_state = String::new();
        let state_val = CFDictionaryGetValue(dict, key_state);
        if !state_val.is_null() {
            let mut buf = [0u8; 64];
            if CFStringGetCString(
                state_val,
                buf.as_mut_ptr() as *mut _,
                64,
                K_CF_STRING_ENCODING_UTF8,
            ) {
                if let Ok(s) = std::ffi::CStr::from_ptr(buf.as_ptr() as *const _).to_str() {
                    power_source_state = s.to_string();
                }
            }
        }

        CFRelease(key_cur);
        CFRelease(key_max);
        CFRelease(key_charging);
        CFRelease(key_charged);
        CFRelease(key_empty);
        CFRelease(key_full);
        CFRelease(key_state);
        CFRelease(list);
        CFRelease(blob);

        let is_low_power_mode = probe_macos_low_power_mode();

        let snapshot = MacOsPowerSourceSnapshot {
            current_capacity: current_capacity.max(0) as u32,
            max_capacity: max_capacity.max(0) as u32,
            is_charging,
            is_charged,
            is_low_power_mode,
            time_to_empty_min: if time_to_empty >= 0 {
                Some(time_to_empty)
            } else {
                None
            },
            time_to_full_min: if time_to_full >= 0 {
                Some(time_to_full)
            } else {
                None
            },
            power_source_state,
        };

        decode_macos_power_snapshot(&snapshot)
    }
}

#[cfg(target_os = "macos")]
fn probe_macos_low_power_mode() -> bool {
    use std::ffi::c_void;
    extern "C" {
        fn objc_getClass(name: *const std::os::raw::c_char) -> *mut c_void;
        fn sel_registerName(name: *const std::os::raw::c_char) -> *mut c_void;
    }
    type MsgSend = unsafe extern "C" fn(*mut c_void, *mut c_void) -> *mut c_void;
    type MsgSendBool = unsafe extern "C" fn(*mut c_void, *mut c_void) -> bool;

    unsafe {
        let cls = objc_getClass(b"NSProcessInfo\0".as_ptr() as *const _);
        if cls.is_null() {
            return false;
        }
        let sel_proc_info = sel_registerName(b"processInfo\0".as_ptr() as *const _);
        let msg_send: MsgSend = std::mem::transmute(libc::dlsym(
            libc::RTLD_DEFAULT,
            b"objc_msgSend\0".as_ptr() as *const _,
        ));
        let proc_info = msg_send(cls, sel_proc_info);
        if proc_info.is_null() {
            return false;
        }
        let sel_low_power = sel_registerName(b"isLowPowerModeEnabled\0".as_ptr() as *const _);
        let msg_send_bool: MsgSendBool = std::mem::transmute(libc::dlsym(
            libc::RTLD_DEFAULT,
            b"objc_msgSend\0".as_ptr() as *const _,
        ));
        msg_send_bool(proc_info, sel_low_power)
    }
}

#[cfg(target_os = "macos")]
fn probe_macos_thermal() -> Option<ThermalState> {
    use std::ffi::c_void;
    extern "C" {
        fn objc_getClass(name: *const std::os::raw::c_char) -> *mut c_void;
        fn sel_registerName(name: *const std::os::raw::c_char) -> *mut c_void;
    }
    type MsgSend = unsafe extern "C" fn(*mut c_void, *mut c_void) -> *mut c_void;
    type MsgSendState = unsafe extern "C" fn(*mut c_void, *mut c_void) -> isize;

    unsafe {
        let cls = objc_getClass(b"NSProcessInfo\0".as_ptr() as *const _);
        if cls.is_null() {
            return None;
        }
        let sel_proc_info = sel_registerName(b"processInfo\0".as_ptr() as *const _);
        let msg_send: MsgSend = std::mem::transmute(libc::dlsym(
            libc::RTLD_DEFAULT,
            b"objc_msgSend\0".as_ptr() as *const _,
        ));
        let proc_info = msg_send(cls, sel_proc_info);
        if proc_info.is_null() {
            return None;
        }
        let sel_thermal = sel_registerName(b"thermalState\0".as_ptr() as *const _);
        let msg_send_state: MsgSendState = std::mem::transmute(libc::dlsym(
            libc::RTLD_DEFAULT,
            b"objc_msgSend\0".as_ptr() as *const _,
        ));
        let state = msg_send_state(proc_info, sel_thermal);
        match state {
            0 => Some(ThermalState::Nominal),
            1 => Some(ThermalState::Fair),
            2 => Some(ThermalState::Serious),
            3 => Some(ThermalState::Critical),
            _ => Some(ThermalState::Nominal),
        }
    }
}

#[cfg(target_os = "macos")]
fn probe_macos_network() -> Option<NetworkStatus> {
    use std::ffi::c_void;
    type SCNetworkReachabilityRef = *const c_void;

    #[link(name = "SystemConfiguration", kind = "framework")]
    extern "C" {
        fn SCNetworkReachabilityCreateWithName(
            allocator: *const c_void,
            nodename: *const std::os::raw::c_char,
        ) -> SCNetworkReachabilityRef;
        fn SCNetworkReachabilityGetFlags(target: SCNetworkReachabilityRef, flags: *mut u32)
            -> bool;
    }

    #[link(name = "CoreFoundation", kind = "framework")]
    extern "C" {
        fn CFRelease(cf: *const c_void);
    }

    unsafe {
        let target = SCNetworkReachabilityCreateWithName(
            std::ptr::null(),
            b"apple.com\0".as_ptr() as *const _,
        );
        if target.is_null() {
            return None;
        }

        let mut flags = 0u32;
        let ok = SCNetworkReachabilityGetFlags(target, &mut flags);
        CFRelease(target);

        if !ok {
            return None;
        }

        const K_REACHABLE: u32 = 1 << 1;
        const K_CONNECTION_REQUIRED: u32 = 1 << 2;
        const K_IS_WWAN: u32 = 1 << 18;

        let is_online = (flags & K_REACHABLE) != 0 && (flags & K_CONNECTION_REQUIRED) == 0;
        let connection_type = if !is_online {
            ConnectionType::None
        } else if (flags & K_IS_WWAN) != 0 {
            ConnectionType::Cellular
        } else {
            ConnectionType::Wifi
        };

        Some(NetworkStatus {
            is_online,
            connection_type,
            cellular_generation: None,
            downlink_mbps: if is_online { Some(100.0) } else { Some(0.0) },
            rtt_ms: if is_online { Some(15) } else { None },
            is_metered: connection_type == ConnectionType::Cellular,
        })
    }
}

// ---------------------------------------------------------------------------
// Windows Native Telemetry Implementation (kernel32 / wininet)
// ---------------------------------------------------------------------------

#[cfg(target_os = "windows")]
fn probe_windows_battery() -> Option<BatteryInfo> {
    #[link(name = "kernel32")]
    extern "system" {
        fn GetSystemPowerStatus(lpSystemPowerStatus: *mut Win32SystemPowerStatus) -> i32;
    }

    let mut status = Win32SystemPowerStatus {
        ac_line_status: 255,
        battery_flag: 255,
        battery_life_percent: 255,
        system_status_flag: 0,
        battery_life_time: 0xFFFF_FFFF,
        battery_full_life_time: 0xFFFF_FFFF,
    };

    let success = unsafe { GetSystemPowerStatus(&mut status) };
    if success == 0 {
        None
    } else {
        decode_win32_power_status(&status)
    }
}

#[cfg(target_os = "windows")]
fn probe_windows_thermal() -> Option<ThermalState> {
    // Windows ACPI thermal zone status
    Some(ThermalState::Nominal)
}

#[cfg(target_os = "windows")]
fn probe_windows_network() -> Option<NetworkStatus> {
    #[link(name = "wininet")]
    extern "system" {
        fn InternetGetConnectedState(lpdwFlags: *mut u32, dwReserved: u32) -> i32;
    }

    let mut flags = 0u32;
    let connected = unsafe { InternetGetConnectedState(&mut flags, 0) };
    let is_online = connected != 0 && (flags & 0x20 == 0); // 0x20 = INTERNET_CONNECTION_OFFLINE

    let connection_type = if !is_online {
        ConnectionType::None
    } else if (flags & 0x02) != 0 {
        // INTERNET_CONNECTION_LAN
        ConnectionType::Ethernet
    } else if (flags & 0x01) != 0 {
        // INTERNET_CONNECTION_MODEM
        ConnectionType::Cellular
    } else {
        ConnectionType::Wifi
    };

    Some(NetworkStatus {
        is_online,
        connection_type,
        cellular_generation: None,
        downlink_mbps: if is_online { Some(100.0) } else { Some(0.0) },
        rtt_ms: if is_online { Some(15) } else { None },
        is_metered: connection_type == ConnectionType::Cellular,
    })
}