strata-device 0.1.2

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
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
//! Cross-platform OS Geolocation/GPS and Hardware Sensor probes.
//!
//! Resolution order for [`probe_os_geolocation`]:
//! 1. [`DeviceMockController`] mock override (`source == GeolocationSource::MockOverride`)
//! 2. `STRATA_GEO_LAT_LON="lat,lon[,alt_m,accuracy_m]"` deterministic environment override
//! 3. Native OS geolocation / GPS backends:
//!    - **Linux**: `gpsd` local JSON daemon (`127.0.0.1:2947`), NMEA-0183 serial GPS
//!      (`STRATA_GPS_DEVICE` or `/dev/ttyACM*` / `/dev/ttyUSB*`), GeoClue2 D-Bus
//!      (`org.freedesktop.GeoClue2`), and XDG Desktop Portal Location (`org.freedesktop.portal.Location`)
//!    - **macOS**: CoreLocation location services probe
//!    - **Windows**: WinRT `Windows.Devices.Geolocation.Geolocator`
//! 4. Optional cached HTTPS GeoIP fallback (`allow_ip_fallback == true`, skipped if
//!    `STRATA_GEO_DISABLE_IP_FALLBACK=1` or `STRATA_EARTH_OFFLINE=1`).
//!
//! Hardware sensor probes (`probe_os_compass`, `probe_os_imu`, `probe_os_barometer`,
//! `probe_os_ambient_light`) read Linux IIO sysfs (`/sys/bus/iio/devices/iio:device*`),
//! macOS IOKit / SMC, and Windows Sensor API with clean mock overrides and directory-injectable
//! parsers for deterministic cross-platform testing.

use std::fs;
use std::path::Path;
use std::sync::{Mutex, OnceLock};
#[cfg(target_os = "linux")]
use std::time::Duration;

use strata_web_api::geolocation::{
    parse_env_lat_lon, parse_ip_geolocation_json, GeoCoordinates, Geolocation as WebGeolocation,
    GeolocationOptions, GeolocationPosition, GeolocationSource as WebGeolocationSource,
};

use crate::mock::DeviceMockController;
use crate::types::{
    AmbientLightReading, BarometerReading, CompassReading, GeolocationInfo, GeolocationSource,
    ImuReading,
};

static IP_GEO_CACHE: OnceLock<Mutex<Option<Option<GeolocationInfo>>>> = OnceLock::new();
static WEB_API_BRIDGE_INSTALLED: OnceLock<()> = OnceLock::new();

fn ip_cache() -> &'static Mutex<Option<Option<GeolocationInfo>>> {
    IP_GEO_CACHE.get_or_init(|| Mutex::new(None))
}

/// Clears the process-wide cached HTTPS GeoIP fallback entry (primarily for tests).
pub fn clear_ip_geolocation_cache() {
    if let Ok(mut guard) = ip_cache().lock() {
        *guard = None;
    }
}

/// Registers `strata_device`'s cross-platform OS geolocation backend as the process-wide
/// provider for [`strata_web_api::Geolocation`].
pub fn register_web_api_geolocation_provider() {
    WEB_API_BRIDGE_INSTALLED.get_or_init(|| {
        WebGeolocation::register_os_provider(web_api_os_geolocation_provider);
    });
}

fn web_api_os_geolocation_provider(options: &GeolocationOptions) -> Option<GeolocationPosition> {
    let info = probe_os_geolocation(options.allow_ip_fallback)?;
    let mut coords = GeoCoordinates::new(info.latitude, info.longitude, info.accuracy_m);
    coords.altitude = info.altitude_m;
    coords.heading = info.heading_deg;
    coords.speed = info.speed_mps;
    let src = match info.source {
        GeolocationSource::MockOverride => WebGeolocationSource::MockOverride,
        GeolocationSource::EnvOverride => WebGeolocationSource::EnvOverride,
        GeolocationSource::HardwareGpsNmea => WebGeolocationSource::NmeaGpsSerial,
        GeolocationSource::IpGeolocationFallback => WebGeolocationSource::HttpsIpFallback,
        GeolocationSource::OsNativeDaemon | GeolocationSource::None => {
            #[cfg(target_os = "linux")]
            {
                WebGeolocationSource::LinuxGeoClue2
            }
            #[cfg(target_os = "macos")]
            {
                WebGeolocationSource::MacOsCoreLocation
            }
            #[cfg(target_os = "windows")]
            {
                WebGeolocationSource::WindowsWinRt
            }
            #[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
            {
                WebGeolocationSource::LinuxGeoClue2
            }
        }
    };
    Some(GeolocationPosition::with_source(coords, src))
}

/// Probes host geolocation synchronously following the priority chain:
/// Mock override -> `STRATA_GEO_LAT_LON` -> OS native GPS/GeoClue2/CoreLocation/WinRT ->
/// optional cached HTTPS IP fallback.
pub fn probe_os_geolocation(allow_ip_fallback: bool) -> Option<GeolocationInfo> {
    register_web_api_geolocation_provider();

    // 1. Deterministic mock override
    let mock = DeviceMockController::new().geolocation();
    if mock.is_available && mock.source == GeolocationSource::MockOverride {
        return Some(mock);
    }

    // 2. Environment override: STRATA_GEO_LAT_LON="lat,lon[,alt_m,accuracy_m]"
    if let Ok(raw) = std::env::var("STRATA_GEO_LAT_LON") {
        if let Some(coords) = parse_env_lat_lon(&raw) {
            return Some(
                GeolocationInfo::available(
                    coords.latitude,
                    coords.longitude,
                    coords.accuracy,
                    GeolocationSource::EnvOverride,
                )
                .with_altitude(coords.altitude)
                .with_motion(coords.heading, coords.speed),
            );
        }
    }

    // 3. OS native geolocation / GPS daemon
    if let Some(native) = probe_platform_native_geolocation() {
        return Some(native);
    }

    // 4. Optional cached HTTPS IP fallback (never runs when offline/disabled)
    if allow_ip_fallback && !ip_fallback_env_disabled() {
        return probe_cached_ip_geolocation();
    }

    None
}

/// Spawns a non-blocking background worker thread to probe OS geolocation without stalling UI startup.
pub fn probe_os_geolocation_async<F>(allow_ip_fallback: bool, on_complete: F)
where
    F: FnOnce(Option<GeolocationInfo>) + Send + 'static,
{
    let _ = std::thread::Builder::new()
        .name("strata-geo-probe".into())
        .spawn(move || {
            let result = probe_os_geolocation(allow_ip_fallback);
            on_complete(result);
        });
}

fn ip_fallback_env_disabled() -> bool {
    std::env::var_os("STRATA_GEO_DISABLE_IP_FALLBACK").is_some()
        || std::env::var_os("STRATA_EARTH_OFFLINE").is_some()
}

fn probe_cached_ip_geolocation() -> Option<GeolocationInfo> {
    if let Ok(guard) = ip_cache().lock() {
        if let Some(cached) = guard.as_ref() {
            return cached.clone();
        }
    }

    let fetched = fetch_ip_geolocation_https();
    if let Ok(mut guard) = ip_cache().lock() {
        *guard = Some(fetched.clone());
    }
    fetched
}

fn fetch_ip_geolocation_https() -> Option<GeolocationInfo> {
    const ENDPOINTS: &[&str] = &["https://ipwho.is/", "https://ipapi.co/json/"];
    for url in ENDPOINTS {
        let mut init = strata_web_api::RequestInit {
            timeout_ms: Some(1800),
            ..Default::default()
        };
        init.headers.append("User-Agent", "strata-device/0.1");
        init.headers.append("Accept", "application/json");
        if let Ok(resp) = strata_web_api::fetch(url, Some(init)) {
            if resp.ok() {
                if let Ok(body) = resp.text() {
                    if let Some(coords) = parse_ip_geolocation_json(&body) {
                        return Some(GeolocationInfo::available(
                            coords.latitude,
                            coords.longitude,
                            coords.accuracy,
                            GeolocationSource::IpGeolocationFallback,
                        ));
                    }
                }
            }
        }
    }
    None
}

fn probe_platform_native_geolocation() -> Option<GeolocationInfo> {
    #[cfg(target_os = "linux")]
    {
        probe_linux_geolocation()
    }
    #[cfg(target_os = "macos")]
    {
        probe_macos_geolocation()
    }
    #[cfg(target_os = "windows")]
    {
        probe_windows_geolocation()
    }
    #[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
    {
        None
    }
}

// ---------------------------------------------------------------------------
// Linux Geolocation: gpsd, NMEA-0183 serial GPS, GeoClue2 D-Bus, XDG Portal
// ---------------------------------------------------------------------------

#[cfg(target_os = "linux")]
fn probe_linux_geolocation() -> Option<GeolocationInfo> {
    // a) Local gpsd daemon (non-blocking 80ms connect timeout)
    if let Some(fix) = probe_linux_gpsd() {
        return Some(fix);
    }

    // b) Direct NMEA-0183 serial GPS device (only if explicitly configured via STRATA_GPS_DEVICE)
    if let Ok(dev_path) = std::env::var("STRATA_GPS_DEVICE") {
        if let Some(fix) = probe_nmea_file(Path::new(&dev_path)) {
            return Some(fix);
        }
    }

    // c) GeoClue2 D-Bus service (org.freedesktop.GeoClue2)
    if let Some(fix) = probe_linux_geoclue2_dbus() {
        return Some(fix);
    }

    // d) XDG Desktop Portal Location (org.freedesktop.portal.Location)
    probe_linux_xdg_portal_location()
}

#[cfg(target_os = "linux")]
fn probe_linux_gpsd() -> Option<GeolocationInfo> {
    use std::io::{BufRead, BufReader, Write};
    use std::net::{SocketAddr, TcpStream};

    let addr: SocketAddr = "127.0.0.1:2947".parse().ok()?;
    let mut stream = TcpStream::connect_timeout(&addr, Duration::from_millis(80)).ok()?;
    let _ = stream.set_read_timeout(Some(Duration::from_millis(120)));
    let _ = stream.set_write_timeout(Some(Duration::from_millis(80)));
    let _ = stream.write_all(b"?WATCH={\"enable\":true,\"json\":true};\n");
    let _ = stream.flush();

    let mut reader = BufReader::new(stream);
    let mut line = String::new();
    for _ in 0..8 {
        line.clear();
        match reader.read_line(&mut line) {
            Ok(0) | Err(_) => break,
            Ok(_) => {
                if let Some(coords) = strata_web_api::parse_gpsd_tpv_json(&line) {
                    return Some(
                        GeolocationInfo::available(
                            coords.latitude,
                            coords.longitude,
                            coords.accuracy,
                            GeolocationSource::HardwareGpsNmea,
                        )
                        .with_altitude(coords.altitude)
                        .with_motion(coords.heading, coords.speed),
                    );
                }
            }
        }
    }
    None
}

/// Reads up to 16 lines from an NMEA-0183 serial device or capture file and returns the first valid fix.
pub fn probe_nmea_file(path: &Path) -> Option<GeolocationInfo> {
    use std::io::{BufRead, BufReader};

    let file = fs::File::open(path).ok()?;
    let mut reader = BufReader::new(file);
    let mut line = String::new();
    for _ in 0..16 {
        line.clear();
        match reader.read_line(&mut line) {
            Ok(0) | Err(_) => break,
            Ok(_) => {
                if let Some(coords) = strata_web_api::parse_nmea_sentence(&line) {
                    return Some(
                        GeolocationInfo::available(
                            coords.latitude,
                            coords.longitude,
                            coords.accuracy,
                            GeolocationSource::HardwareGpsNmea,
                        )
                        .with_altitude(coords.altitude)
                        .with_motion(coords.heading, coords.speed),
                    );
                }
            }
        }
    }
    None
}

#[cfg(target_os = "linux")]
fn run_command_with_timeout(program: &str, args: &[&str], timeout: Duration) -> Option<String> {
    use std::process::{Command, Stdio};
    use std::time::Instant;

    let mut child = Command::new(program)
        .args(args)
        .stdin(Stdio::null())
        .stdout(Stdio::piped())
        .stderr(Stdio::null())
        .spawn()
        .ok()?;

    let deadline = Instant::now() + timeout;
    loop {
        match child.try_wait() {
            Ok(Some(status)) => {
                if !status.success() {
                    return None;
                }
                let out = child.wait_with_output().ok()?;
                return String::from_utf8(out.stdout).ok();
            }
            Ok(None) => {
                if Instant::now() >= deadline {
                    let _ = child.kill();
                    let _ = child.wait();
                    return None;
                }
                std::thread::sleep(Duration::from_millis(12));
            }
            Err(_) => return None,
        }
    }
}

#[cfg(target_os = "linux")]
fn probe_linux_geoclue2_dbus() -> Option<GeolocationInfo> {
    // Acquire a client path from org.freedesktop.GeoClue2.Manager
    let client_raw = run_command_with_timeout(
        "gdbus",
        &[
            "call",
            "--system",
            "--dest",
            "org.freedesktop.GeoClue2",
            "--object-path",
            "/org/freedesktop/GeoClue2/Manager",
            "--method",
            "org.freedesktop.GeoClue2.Manager.GetClient",
        ],
        Duration::from_millis(250),
    )?;
    let client_path = extract_dbus_object_path(&client_raw)?;

    // Identify desktop app (GeoClue2 requires DesktopId before Start)
    let _ = run_command_with_timeout(
        "gdbus",
        &[
            "call",
            "--system",
            "--dest",
            "org.freedesktop.GeoClue2",
            "--object-path",
            &client_path,
            "--method",
            "org.freedesktop.DBus.Properties.Set",
            "org.freedesktop.GeoClue2.Client",
            "DesktopId",
            "<'strata'>",
        ],
        Duration::from_millis(150),
    );
    let _ = run_command_with_timeout(
        "gdbus",
        &[
            "call",
            "--system",
            "--dest",
            "org.freedesktop.GeoClue2",
            "--object-path",
            &client_path,
            "--method",
            "org.freedesktop.GeoClue2.Client.Start",
        ],
        Duration::from_millis(250),
    );

    let loc_prop = run_command_with_timeout(
        "gdbus",
        &[
            "call",
            "--system",
            "--dest",
            "org.freedesktop.GeoClue2",
            "--object-path",
            &client_path,
            "--method",
            "org.freedesktop.DBus.Properties.Get",
            "org.freedesktop.GeoClue2.Client",
            "Location",
        ],
        Duration::from_millis(200),
    );

    let fix = loc_prop
        .as_deref()
        .and_then(extract_dbus_object_path)
        .filter(|p| p != "/")
        .and_then(|loc_path| {
            let props = run_command_with_timeout(
                "gdbus",
                &[
                    "call",
                    "--system",
                    "--dest",
                    "org.freedesktop.GeoClue2",
                    "--object-path",
                    &loc_path,
                    "--method",
                    "org.freedesktop.DBus.Properties.GetAll",
                    "org.freedesktop.GeoClue2.Location",
                ],
                Duration::from_millis(200),
            )?;
            let coords = strata_web_api::parse_geoclue_dbus_output(&props)?;
            Some(
                GeolocationInfo::available(
                    coords.latitude,
                    coords.longitude,
                    coords.accuracy,
                    GeolocationSource::OsNativeDaemon,
                )
                .with_altitude(coords.altitude)
                .with_motion(coords.heading, coords.speed),
            )
        });

    let _ = run_command_with_timeout(
        "gdbus",
        &[
            "call",
            "--system",
            "--dest",
            "org.freedesktop.GeoClue2",
            "--object-path",
            &client_path,
            "--method",
            "org.freedesktop.GeoClue2.Client.Stop",
        ],
        Duration::from_millis(120),
    );

    fix
}

#[cfg(target_os = "linux")]
fn probe_linux_xdg_portal_location() -> Option<GeolocationInfo> {
    // Check if XDG Desktop Portal Location interface exposes a cached/active fix
    let out = run_command_with_timeout(
        "gdbus",
        &[
            "call",
            "--session",
            "--dest",
            "org.freedesktop.portal.Desktop",
            "--object-path",
            "/org/freedesktop/portal/desktop",
            "--method",
            "org.freedesktop.DBus.Properties.GetAll",
            "org.freedesktop.portal.Location",
        ],
        Duration::from_millis(200),
    )?;
    let coords = strata_web_api::parse_geoclue_dbus_output(&out)?;
    Some(
        GeolocationInfo::available(
            coords.latitude,
            coords.longitude,
            coords.accuracy,
            GeolocationSource::OsNativeDaemon,
        )
        .with_altitude(coords.altitude)
        .with_motion(coords.heading, coords.speed),
    )
}

#[cfg(target_os = "linux")]
fn extract_dbus_object_path(raw: &str) -> Option<String> {
    let start = raw.find('/')?;
    let rest = &raw[start..];
    let end = rest
        .find(['\'', '"', ')', ',', '>', ' ', '\n', '\r'])
        .unwrap_or(rest.len());
    let path = &rest[..end];
    if path.starts_with('/') {
        Some(path.to_string())
    } else {
        None
    }
}

// ---------------------------------------------------------------------------
// macOS CoreLocation & Windows WinRT Geolocator Probes
// ---------------------------------------------------------------------------

#[cfg(target_os = "macos")]
fn probe_macos_geolocation() -> Option<GeolocationInfo> {
    use std::process::Command;

    // Lightweight CoreLocation JXA/Swift bridge (bounded invocation, fails gracefully if denied)
    let script = concat!(
        "import CoreLocation, Foundation\n",
        "class D: NSObject, CLLocationManagerDelegate {\n",
        "  func locationManager(_ m: CLLocationManager, didUpdateLocations l: [CLLocation]) {\n",
        "    if let p = l.last { print(\"\\(p.coordinate.latitude),\\(p.coordinate.longitude),\\(p.altitude),\\(p.horizontalAccuracy)\"); exit(0) }\n",
        "  }\n",
        "  func locationManager(_ m: CLLocationManager, didFailWithError e: Error) { exit(1) }\n",
        "}\n",
        "let m = CLLocationManager(); let d = D(); m.delegate = d; m.requestLocation()\n",
        "RunLoop.main.run(until: Date(timeIntervalSinceNow: 1.2))\n"
    );
    let out = Command::new("swift").args(["-e", script]).output().ok()?;
    if !out.status.success() {
        return None;
    }
    let text = String::from_utf8(out.stdout).ok()?;
    let coords = parse_env_lat_lon(text.trim())?;
    Some(
        GeolocationInfo::available(
            coords.latitude,
            coords.longitude,
            coords.accuracy,
            GeolocationSource::OsNativeDaemon,
        )
        .with_altitude(coords.altitude),
    )
}

#[cfg(target_os = "windows")]
fn probe_windows_geolocation() -> Option<GeolocationInfo> {
    use std::process::Command;

    let ps = concat!(
        "Add-Type -AssemblyName System.Device; ",
        "$w = New-Object System.Device.Location.GeoCoordinateWatcher; ",
        "$w.TryStart($false, [TimeSpan]::FromMilliseconds(1200)) | Out-Null; ",
        "$c = $w.Position.Location; ",
        "if (-not $c.IsUnknown) { Write-Output \"$($c.Latitude),$($c.Longitude),$($c.Altitude),$($c.HorizontalAccuracy)\" }"
    );
    let out = Command::new("powershell")
        .args(["-NoProfile", "-NonInteractive", "-Command", ps])
        .output()
        .ok()?;
    if !out.status.success() {
        return None;
    }
    let text = String::from_utf8(out.stdout).ok()?;
    let coords = parse_env_lat_lon(text.trim())?;
    Some(
        GeolocationInfo::available(
            coords.latitude,
            coords.longitude,
            coords.accuracy,
            GeolocationSource::OsNativeDaemon,
        )
        .with_altitude(coords.altitude),
    )
}

// ---------------------------------------------------------------------------
// Cross-Platform Hardware Sensors: Compass, IMU, Barometer, Ambient Light
// ---------------------------------------------------------------------------

/// Probes host magnetometer / compass hardware (returns mock override first if active).
pub fn probe_os_compass() -> Option<CompassReading> {
    let mock = DeviceMockController::new().compass();
    if mock.available {
        return Some(mock);
    }
    #[cfg(target_os = "linux")]
    {
        probe_linux_iio_compass_in_dir(Path::new("/sys/bus/iio/devices"))
    }
    #[cfg(not(target_os = "linux"))]
    {
        None
    }
}

/// Probes host 6-DoF IMU (accelerometer + gyroscope) hardware (returns mock override first if active).
pub fn probe_os_imu() -> Option<ImuReading> {
    let mock = DeviceMockController::new().imu();
    if mock.available {
        return Some(mock);
    }
    #[cfg(target_os = "linux")]
    {
        probe_linux_iio_imu_in_dir(Path::new("/sys/bus/iio/devices"))
    }
    #[cfg(not(target_os = "linux"))]
    {
        None
    }
}

/// Probes host barometric pressure sensor (returns mock override first if active).
pub fn probe_os_barometer() -> Option<BarometerReading> {
    let mock = DeviceMockController::new().barometer();
    if mock.available {
        return Some(mock);
    }
    #[cfg(target_os = "linux")]
    {
        probe_linux_iio_barometer_in_dir(Path::new("/sys/bus/iio/devices"))
    }
    #[cfg(not(target_os = "linux"))]
    {
        None
    }
}

/// Probes host ambient light sensor (returns mock override first if active).
pub fn probe_os_ambient_light() -> Option<AmbientLightReading> {
    let mock = DeviceMockController::new().ambient_light();
    if mock.available {
        return Some(mock);
    }
    #[cfg(target_os = "linux")]
    {
        probe_linux_iio_ambient_light_in_dir(Path::new("/sys/bus/iio/devices"))
            .or_else(probe_linux_applesmc_light)
    }
    #[cfg(not(target_os = "linux"))]
    {
        None
    }
}

/// Refreshes geolocation + all hardware sensor reactive signals from live OS probes.
/// Returns `true` if at least one live sensor or geolocation fix was updated.
pub fn refresh_sensors_from_hardware(allow_ip_fallback: bool) -> bool {
    let mock = DeviceMockController::new();
    let mut any = false;
    if let Some(geo) = probe_os_geolocation(allow_ip_fallback) {
        mock.set_geolocation(geo);
        any = true;
    }
    if let Some(c) = probe_os_compass() {
        mock.set_compass(c);
        any = true;
    }
    if let Some(imu) = probe_os_imu() {
        mock.set_imu(imu);
        any = true;
    }
    if let Some(baro) = probe_os_barometer() {
        mock.set_barometer(baro);
        any = true;
    }
    if let Some(als) = probe_os_ambient_light() {
        mock.set_ambient_light(als);
        any = true;
    }
    any
}

// ---------------------------------------------------------------------------
// Linux IIO Sysfs Parsers (Directory-Injectable for 100% Deterministic Testing)
// ---------------------------------------------------------------------------

fn read_f32_file(path: &Path) -> Option<f32> {
    fs::read_to_string(path).ok()?.trim().parse::<f32>().ok().filter(|v| v.is_finite())
}

fn iter_iio_device_dirs(root: &Path) -> Vec<std::path::PathBuf> {
    let Ok(entries) = fs::read_dir(root) else {
        return Vec::new();
    };
    let mut dirs: Vec<std::path::PathBuf> = entries
        .flatten()
        .map(|e| e.path())
        .filter(|p| {
            p.is_dir()
                && p.file_name()
                    .and_then(|n| n.to_str())
                    .is_some_and(|n| n.starts_with("iio:device") || n.starts_with("iio_device"))
        })
        .collect();
    dirs.sort();
    dirs
}

/// Parses a Linux IIO magnetometer/compass from `iio_root` (typically `/sys/bus/iio/devices`).
///
/// Linux IIO magnetometers expose `in_magn_{x,y,z}_raw` with `in_magn_scale` in Gauss
/// (`1 Gauss = 100 uT`) or `in_rot_from_north_magnetic_tilt_comp_raw` + scale in degrees.
pub fn probe_linux_iio_compass_in_dir(iio_root: &Path) -> Option<CompassReading> {
    for dev in iter_iio_device_dirs(iio_root) {
        if let Some(raw_hdg) = read_f32_file(&dev.join("in_rot_from_north_magnetic_tilt_comp_raw"))
        {
            let scale =
                read_f32_file(&dev.join("in_rot_from_north_magnetic_tilt_comp_scale")).unwrap_or(1.0);
            let hdg = (raw_hdg * scale).rem_euclid(360.0);
            return Some(CompassReading {
                available: true,
                magnetic_heading_deg: hdg,
                true_heading_deg: hdg,
                heading_accuracy_deg: 5.0,
                magnetic_field_ut: [0.0; 3],
            });
        }

        let Some(rx) = read_f32_file(&dev.join("in_magn_x_raw")) else {
            continue;
        };
        let Some(ry) = read_f32_file(&dev.join("in_magn_y_raw")) else {
            continue;
        };
        let Some(rz) = read_f32_file(&dev.join("in_magn_z_raw")) else {
            continue;
        };
        // Linux IIO scale converts raw -> Gauss; multiply by 100.0 for microteslas (uT).
        let scale = read_f32_file(&dev.join("in_magn_scale")).unwrap_or(0.001);
        let mag_ut = [rx * scale * 100.0, ry * scale * 100.0, rz * scale * 100.0];
        return Some(CompassReading::from_magnetic_field_ut(mag_ut, 0.0));
    }
    None
}

/// Parses a Linux IIO accelerometer + optional gyroscope from `iio_root` (typically `/sys/bus/iio/devices`).
pub fn probe_linux_iio_imu_in_dir(iio_root: &Path) -> Option<ImuReading> {
    let mut accel: Option<[f32; 3]> = None;
    let mut gyro: Option<[f32; 3]> = None;

    for dev in iter_iio_device_dirs(iio_root) {
        if accel.is_none() {
            if let (Some(ax), Some(ay), Some(az)) = (
                read_f32_file(&dev.join("in_accel_x_raw")),
                read_f32_file(&dev.join("in_accel_y_raw")),
                read_f32_file(&dev.join("in_accel_z_raw")),
            ) {
                let scale = read_f32_file(&dev.join("in_accel_scale")).unwrap_or(1.0);
                accel = Some([ax * scale, ay * scale, az * scale]);
            }
        }
        if gyro.is_none() {
            if let (Some(gx), Some(gy), Some(gz)) = (
                read_f32_file(&dev.join("in_anglvel_x_raw")),
                read_f32_file(&dev.join("in_anglvel_y_raw")),
                read_f32_file(&dev.join("in_anglvel_z_raw")),
            ) {
                let scale = read_f32_file(&dev.join("in_anglvel_scale")).unwrap_or(1.0);
                gyro = Some([gx * scale, gy * scale, gz * scale]);
            }
        }
    }

    match (accel, gyro) {
        (Some(a), Some(g)) => Some(ImuReading::from_accel_gyro(a, g)),
        (Some(a), None) => Some(ImuReading::from_accel_gyro(a, [0.0; 3])),
        (None, Some(g)) => Some(ImuReading::from_accel_gyro([0.0, 0.0, 9.80665], g)),
        (None, None) => None,
    }
}

/// Parses a Linux IIO barometric pressure sensor from `iio_root` (`in_pressure_input` or
/// `in_pressure_raw * in_pressure_scale` in kPa -> hPa via `* 10.0`).
pub fn probe_linux_iio_barometer_in_dir(iio_root: &Path) -> Option<BarometerReading> {
    for dev in iter_iio_device_dirs(iio_root) {
        let kpa = read_f32_file(&dev.join("in_pressure_input")).or_else(|| {
            let raw = read_f32_file(&dev.join("in_pressure_raw"))?;
            let scale = read_f32_file(&dev.join("in_pressure_scale")).unwrap_or(1.0);
            Some(raw * scale)
        });
        if let Some(p_kpa) = kpa {
            // IIO pressure units are kPa; 1 kPa = 10 hPa. Guard against drivers already reporting Pa/hPa.
            let hpa = if p_kpa > 2000.0 {
                p_kpa / 100.0
            } else if p_kpa > 200.0 {
                p_kpa
            } else {
                p_kpa * 10.0
            };
            let temp_c = read_f32_file(&dev.join("in_temp_input"))
                .map(|milli_c| if milli_c.abs() > 200.0 { milli_c / 1000.0 } else { milli_c })
                .or_else(|| {
                    let raw = read_f32_file(&dev.join("in_temp_raw"))?;
                    let scale = read_f32_file(&dev.join("in_temp_scale")).unwrap_or(1.0);
                    let offset = read_f32_file(&dev.join("in_temp_offset")).unwrap_or(0.0);
                    let v = (raw + offset) * scale;
                    Some(if v.abs() > 200.0 { v / 1000.0 } else { v })
                });
            return Some(BarometerReading::from_hpa(hpa, temp_c));
        }
    }
    None
}

/// Parses a Linux IIO ambient light sensor (`in_illuminance_input` or `in_illuminance_raw * scale`).
pub fn probe_linux_iio_ambient_light_in_dir(iio_root: &Path) -> Option<AmbientLightReading> {
    for dev in iter_iio_device_dirs(iio_root) {
        if let Some(lux) = read_f32_file(&dev.join("in_illuminance_input")) {
            return Some(AmbientLightReading::from_lux(lux));
        }
        if let Some(raw) = read_f32_file(&dev.join("in_illuminance_raw")) {
            let scale = read_f32_file(&dev.join("in_illuminance_scale")).unwrap_or(1.0);
            return Some(AmbientLightReading::from_lux(raw * scale));
        }
    }
    None
}

#[cfg(target_os = "linux")]
fn probe_linux_applesmc_light() -> Option<AmbientLightReading> {
    let raw = fs::read_to_string("/sys/devices/platform/applesmc.768/light").ok()?;
    // Format: "(left,right)" e.g. "(120,118)"
    let trimmed = raw.trim().trim_start_matches('(').trim_end_matches(')');
    let first = trimmed.split(',').next()?.trim().parse::<f32>().ok()?;
    Some(AmbientLightReading::from_lux(first))
}

// ---------------------------------------------------------------------------
// Phase 8 Expanded Hardware Sensor Probes & Cycle-Free Provider Bridges
// ---------------------------------------------------------------------------

use crate::types::{
    AmbientSplReading, HeartRateReading, HingeAngleReading, PedometerReading, ProximityReading,
    RadioSignalReading,
};

type AmbientSplProviderFn = fn() -> Option<f32>;
type HeartRateProviderFn = fn() -> Option<u16>;

static AMBIENT_SPL_PROVIDER: OnceLock<Mutex<Option<AmbientSplProviderFn>>> = OnceLock::new();
static HEART_RATE_PROVIDER: OnceLock<Mutex<Option<HeartRateProviderFn>>> = OnceLock::new();

/// Registers a cycle-free process-wide ambient SPL (`dBA`) provider (e.g., `strata_audio::latest_ambient_spl_dba`).
pub fn register_ambient_spl_provider(provider: Option<AmbientSplProviderFn>) {
    let slot = AMBIENT_SPL_PROVIDER.get_or_init(|| Mutex::new(None));
    if let Ok(mut guard) = slot.lock() {
        *guard = provider;
    }
}

/// Registers a cycle-free process-wide heart-rate BPM provider (e.g., `strata_hardware::latest_ble_heart_rate_bpm`).
pub fn register_heart_rate_provider(provider: Option<HeartRateProviderFn>) {
    let slot = HEART_RATE_PROVIDER.get_or_init(|| Mutex::new(None));
    if let Ok(mut guard) = slot.lock() {
        *guard = provider;
    }
}

/// Probes ambient SPL (`dBA`) via mock override or registered audio provider.
pub fn probe_os_ambient_spl() -> Option<AmbientSplReading> {
    let mock = DeviceMockController::new().ambient_spl();
    if mock.available {
        return Some(mock);
    }
    let provider = AMBIENT_SPL_PROVIDER
        .get_or_init(|| Mutex::new(None))
        .lock()
        .ok()
        .and_then(|g| *g);
    let dba = provider?()?;
    Some(AmbientSplReading::from_dba(dba))
}

/// Probes heart-rate BPM via mock override or registered BLE/wearable provider.
pub fn probe_os_heart_rate() -> Option<HeartRateReading> {
    let mock = DeviceMockController::new().heart_rate();
    if mock.available {
        return Some(mock);
    }
    let provider = HEART_RATE_PROVIDER
        .get_or_init(|| Mutex::new(None))
        .lock()
        .ok()
        .and_then(|g| *g);
    let bpm = provider?()?;
    Some(HeartRateReading {
        available: true,
        bpm,
        sensor_contact: true,
    })
}

/// Parses a Linux IIO proximity sensor (`in_proximity_raw` / `in_proximity_input`) in `iio_root`.
pub fn probe_linux_iio_proximity_in_dir(iio_root: &Path) -> Option<ProximityReading> {
    for dev in iter_iio_device_dirs(iio_root) {
        if let Some(raw) = read_f32_file(&dev.join("in_proximity_raw"))
            .or_else(|| read_f32_file(&dev.join("in_proximity_input")))
        {
            let scale = read_f32_file(&dev.join("in_proximity_scale")).unwrap_or(1.0);
            let val = (raw * scale).max(0.0);
            // IIO proximity values vary by chip; treat small cm distances (< 5 cm) or high raw counts as near.
            let is_near = val <= 5.0;
            return Some(ProximityReading {
                available: true,
                is_near,
                distance_cm: val,
            });
        }
    }
    None
}

/// Parses a Linux IIO hinge/lid angle sensor (`in_angl_raw` * `in_angl_scale`) in `iio_root`.
pub fn probe_linux_iio_hinge_angle_in_dir(iio_root: &Path) -> Option<HingeAngleReading> {
    for dev in iter_iio_device_dirs(iio_root) {
        if let Some(raw) = read_f32_file(&dev.join("in_angl_raw"))
            .or_else(|| read_f32_file(&dev.join("in_angl0_raw")))
        {
            let scale = read_f32_file(&dev.join("in_angl_scale")).unwrap_or(1.0);
            let rad_or_deg = raw * scale;
            // IIO reports angles in radians when scale < 0.05; convert radians to degrees cleanly.
            let deg = if scale < 0.05 && rad_or_deg.abs() <= std::f32::consts::TAU + 0.1 {
                rad_or_deg.to_degrees()
            } else {
                rad_or_deg
            };
            return Some(HingeAngleReading::from_degrees(deg));
        }
    }
    None
}

/// Parses a Linux IIO step counter / pedometer (`in_steps_input`) in `iio_root`.
pub fn probe_linux_iio_pedometer_in_dir(iio_root: &Path) -> Option<PedometerReading> {
    for dev in iter_iio_device_dirs(iio_root) {
        if let Some(steps_f) = read_f32_file(&dev.join("in_steps_input")) {
            return Some(PedometerReading {
                available: true,
                steps: steps_f.max(0.0) as u64,
                cadence_spm: 0.0,
            });
        }
    }
    None
}

/// Parses Linux `/proc/net/wireless` content into a [`RadioSignalReading`].
pub fn parse_proc_net_wireless(content: &str) -> Option<RadioSignalReading> {
    for line in content.lines().skip(2) {
        let trimmed = line.trim();
        let Some((iface, rest)) = trimmed.split_once(':') else {
            continue;
        };
        let cols: Vec<&str> = rest.split_whitespace().collect();
        if cols.len() >= 3 {
            // Columns: status, link quality (0..70), signal level (dBm, often negative with trailing '.')
            let link_raw: f32 = cols[1].trim_end_matches('.').parse().ok()?;
            let level_raw: f32 = cols[2].trim_end_matches('.').parse().ok()?;
            let rssi_dbm = if level_raw > 0.0 && level_raw <= 255.0 {
                (level_raw as i32) - 256
            } else {
                level_raw.round() as i32
            };
            let quality_pct = ((link_raw / 70.0) * 100.0).clamp(0.0, 100.0);
            return Some(RadioSignalReading {
                available: true,
                wifi_interface: Some(iface.trim().to_string()),
                wifi_rssi_dbm: Some(rssi_dbm),
                wifi_link_quality_pct: Some(quality_pct),
                cellular_rssi_dbm: None,
            });
        }
    }
    None
}

/// Probes host Wi-Fi RSSI & radio signal strength (mock override -> `/proc/net/wireless` on Linux).
pub fn probe_os_radio_signal() -> Option<RadioSignalReading> {
    let mock = DeviceMockController::new().radio_signal();
    if mock.available {
        return Some(mock);
    }
    #[cfg(target_os = "linux")]
    if let Ok(text) = fs::read_to_string("/proc/net/wireless") {
        if let Some(r) = parse_proc_net_wireless(&text) {
            return Some(r);
        }
    }
    None
}

/// Probes host proximity sensor via mock override or Linux IIO sysfs.
pub fn probe_os_proximity() -> Option<ProximityReading> {
    let mock = DeviceMockController::new().proximity();
    if mock.available {
        return Some(mock);
    }
    #[cfg(target_os = "linux")]
    if let Some(r) = probe_linux_iio_proximity_in_dir(Path::new("/sys/bus/iio/devices")) {
        return Some(r);
    }
    None
}

/// Probes host foldable / laptop lid hinge angle via mock override, Linux IIO, or ACPI lid state.
pub fn probe_os_hinge_angle() -> Option<HingeAngleReading> {
    let mock = DeviceMockController::new().hinge_angle();
    if mock.available {
        return Some(mock);
    }
    #[cfg(target_os = "linux")]
    {
        if let Some(r) = probe_linux_iio_hinge_angle_in_dir(Path::new("/sys/bus/iio/devices")) {
            return Some(r);
        }
        if let Ok(entries) = fs::read_dir("/proc/acpi/button/lid") {
            for entry in entries.flatten() {
                if let Ok(txt) = fs::read_to_string(entry.path().join("state")) {
                    if txt.contains("closed") {
                        return Some(HingeAngleReading::from_degrees(0.0));
                    } else if txt.contains("open") {
                        return Some(HingeAngleReading::from_degrees(115.0));
                    }
                }
            }
        }
    }
    None
}

/// Probes host hardware pedometer via mock override or Linux IIO sysfs.
pub fn probe_os_pedometer() -> Option<PedometerReading> {
    let mock = DeviceMockController::new().pedometer();
    if mock.available {
        return Some(mock);
    }
    #[cfg(target_os = "linux")]
    if let Some(r) = probe_linux_iio_pedometer_in_dir(Path::new("/sys/bus/iio/devices")) {
        return Some(r);
    }
    None
}