1use crate::mock::{DeviceMockController, DeviceTelemetrySnapshot};
7use crate::types::*;
8#[cfg(target_os = "linux")]
9use std::fs;
10#[cfg(target_os = "linux")]
11use std::path::Path;
12
13pub fn probe_hardware_telemetry() -> Option<DeviceTelemetrySnapshot> {
16 let mut snapshot = DeviceTelemetrySnapshot::default();
17 let mut detected_any = false;
18
19 if let Some(battery) = probe_os_battery() {
20 snapshot.battery = battery;
21 detected_any = true;
22 }
23
24 if let Some(thermal) = probe_os_thermal() {
25 snapshot.thermal = thermal;
26 detected_any = true;
27 }
28
29 if let Some(network) = probe_os_network() {
30 snapshot.network = network;
31 detected_any = true;
32 }
33
34 if detected_any {
35 Some(snapshot)
36 } else {
37 None
38 }
39}
40
41pub fn refresh_from_hardware() -> bool {
44 if let Some(snapshot) = probe_hardware_telemetry() {
45 let mock = DeviceMockController::new();
46 mock.set_battery(snapshot.battery);
47 mock.set_thermal(snapshot.thermal);
48 mock.set_network(snapshot.network);
49 true
50 } else {
51 false
52 }
53}
54
55pub fn probe_os_battery() -> Option<BatteryInfo> {
57 #[cfg(target_os = "linux")]
58 {
59 probe_linux_battery()
60 }
61 #[cfg(target_os = "macos")]
62 {
63 probe_macos_battery()
64 }
65 #[cfg(target_os = "windows")]
66 {
67 probe_windows_battery()
68 }
69 #[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
70 {
71 None
72 }
73}
74
75pub fn probe_os_thermal() -> Option<ThermalState> {
77 #[cfg(target_os = "linux")]
78 {
79 probe_linux_thermal()
80 }
81 #[cfg(target_os = "macos")]
82 {
83 probe_macos_thermal()
84 }
85 #[cfg(target_os = "windows")]
86 {
87 probe_windows_thermal()
88 }
89 #[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
90 {
91 None
92 }
93}
94
95pub fn probe_os_network() -> Option<NetworkStatus> {
97 #[cfg(target_os = "linux")]
98 {
99 probe_linux_network()
100 }
101 #[cfg(target_os = "macos")]
102 {
103 probe_macos_network()
104 }
105 #[cfg(target_os = "windows")]
106 {
107 probe_windows_network()
108 }
109 #[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
110 {
111 None
112 }
113}
114
115#[cfg(target_os = "linux")]
120fn probe_linux_battery() -> Option<BatteryInfo> {
121 let power_supply_dir = Path::new("/sys/class/power_supply");
122 if !power_supply_dir.is_dir() {
123 return None;
124 }
125
126 let entries = fs::read_dir(power_supply_dir).ok()?;
127 let mut found_battery = false;
128 let mut level = 1.0f32;
129 let mut state = BatteryState::Full;
130 let mut is_low_power = false;
131 let mut time_to_empty = None;
132 let mut time_to_full = None;
133
134 for entry in entries.flatten() {
135 let path = entry.path();
136 let supply_type = fs::read_to_string(path.join("type"))
137 .unwrap_or_default()
138 .trim()
139 .to_ascii_lowercase();
140
141 if supply_type == "battery" {
142 found_battery = true;
143
144 if let Ok(cap_str) = fs::read_to_string(path.join("capacity")) {
146 if let Ok(cap_int) = cap_str.trim().parse::<u32>() {
147 level = (cap_int as f32 / 100.0).clamp(0.0, 1.0);
148 if cap_int <= 20 {
149 is_low_power = true;
150 }
151 }
152 }
153
154 if let Ok(status_str) = fs::read_to_string(path.join("status")) {
156 state = match status_str.trim().to_ascii_lowercase().as_str() {
157 "charging" => BatteryState::Charging,
158 "discharging" => BatteryState::Discharging,
159 "full" => BatteryState::Full,
160 "not charging" => BatteryState::NotCharging,
161 _ => BatteryState::Unknown,
162 };
163 }
164
165 let power_now = read_sysfs_u64(&path.join("power_now"))
167 .or_else(|| read_sysfs_u64(&path.join("current_now")));
168 let energy_now = read_sysfs_u64(&path.join("energy_now"))
169 .or_else(|| read_sysfs_u64(&path.join("charge_now")));
170 let energy_full = read_sysfs_u64(&path.join("energy_full"))
171 .or_else(|| read_sysfs_u64(&path.join("charge_full")));
172
173 if let (Some(power), Some(now)) = (power_now, energy_now) {
174 if power > 0 {
175 if state == BatteryState::Discharging {
176 time_to_empty = Some(((now as f64 / power as f64) * 3600.0) as u32);
177 } else if state == BatteryState::Charging {
178 if let Some(full) = energy_full {
179 if full > now {
180 time_to_full =
181 Some((((full - now) as f64 / power as f64) * 3600.0) as u32);
182 }
183 }
184 }
185 }
186 }
187 break;
188 }
189 }
190
191 if found_battery {
192 Some(BatteryInfo {
193 level,
194 state,
195 is_low_power_mode: is_low_power,
196 time_to_empty_secs: time_to_empty,
197 time_to_full_secs: time_to_full,
198 })
199 } else {
200 None
201 }
202}
203
204#[cfg(target_os = "linux")]
205fn probe_linux_thermal() -> Option<ThermalState> {
206 let thermal_dir = Path::new("/sys/class/thermal");
207 if !thermal_dir.is_dir() {
208 return None;
209 }
210
211 let entries = fs::read_dir(thermal_dir).ok()?;
212 let mut max_temp_c = 0.0f32;
213 let mut found_zone = false;
214
215 for entry in entries.flatten() {
216 let file_name = entry.file_name().to_string_lossy().to_string();
217 if file_name.starts_with("thermal_zone") {
218 let temp_path = entry.path().join("temp");
219 if let Ok(temp_str) = fs::read_to_string(temp_path) {
220 if let Ok(millidegrees) = temp_str.trim().parse::<i64>() {
221 let deg_c = millidegrees as f32 / 1000.0;
222 if (0.0..=130.0).contains(°_c) {
224 found_zone = true;
225 if deg_c > max_temp_c {
226 max_temp_c = deg_c;
227 }
228 }
229 }
230 }
231 }
232 }
233
234 if found_zone {
235 let state = if max_temp_c >= 95.0 {
236 ThermalState::Critical
237 } else if max_temp_c >= 80.0 {
238 ThermalState::Serious
239 } else if max_temp_c >= 65.0 {
240 ThermalState::Fair
241 } else {
242 ThermalState::Nominal
243 };
244 Some(state)
245 } else {
246 None
247 }
248}
249
250#[cfg(target_os = "linux")]
251fn probe_linux_network() -> Option<NetworkStatus> {
252 let net_dir = Path::new("/sys/class/net");
253 if !net_dir.is_dir() {
254 return None;
255 }
256
257 let entries = fs::read_dir(net_dir).ok()?;
258 let mut is_online = false;
259 let mut connection_type = ConnectionType::None;
260
261 for entry in entries.flatten() {
262 let iface = entry.file_name().to_string_lossy().to_string();
263 if iface == "lo" {
264 continue;
265 }
266
267 let operstate = fs::read_to_string(entry.path().join("operstate"))
268 .unwrap_or_default()
269 .trim()
270 .to_ascii_lowercase();
271
272 if operstate == "up" {
273 is_online = true;
274 if entry.path().join("wireless").exists() || iface.starts_with("wl") {
275 connection_type = ConnectionType::Wifi;
276 break; } else if iface.starts_with("en") || iface.starts_with("eth") {
278 connection_type = ConnectionType::Ethernet;
279 } else if iface.starts_with("ww") {
280 connection_type = ConnectionType::Cellular;
281 } else if connection_type == ConnectionType::None {
282 connection_type = ConnectionType::Unknown;
283 }
284 }
285 }
286
287 Some(NetworkStatus {
288 is_online,
289 connection_type,
290 cellular_generation: None,
291 downlink_mbps: if is_online { Some(100.0) } else { Some(0.0) },
292 rtt_ms: if is_online { Some(15) } else { None },
293 is_metered: connection_type == ConnectionType::Cellular,
294 })
295}
296
297#[cfg(target_os = "linux")]
298fn read_sysfs_u64(path: &Path) -> Option<u64> {
299 fs::read_to_string(path).ok()?.trim().parse::<u64>().ok()
300}
301
302#[repr(C)]
307#[derive(Debug, Clone, Copy, PartialEq, Eq)]
308pub struct Win32SystemPowerStatus {
309 pub ac_line_status: u8,
310 pub battery_flag: u8,
311 pub battery_life_percent: u8,
312 pub system_status_flag: u8,
313 pub battery_life_time: u32,
314 pub battery_full_life_time: u32,
315}
316
317pub fn decode_win32_power_status(status: &Win32SystemPowerStatus) -> Option<BatteryInfo> {
319 if (status.battery_flag & 128) != 0
321 || (status.battery_flag == 255 && status.battery_life_percent == 255)
322 {
323 return None;
324 }
325
326 let level = if status.battery_life_percent <= 100 {
327 status.battery_life_percent as f32 / 100.0
328 } else {
329 1.0
330 };
331
332 let state = if (status.battery_flag & 8) != 0 {
333 BatteryState::Charging
334 } else if status.ac_line_status == 1 {
335 if status.battery_life_percent >= 99 {
336 BatteryState::Full
337 } else {
338 BatteryState::Charging
339 }
340 } else if status.ac_line_status == 0 {
341 BatteryState::Discharging
342 } else {
343 BatteryState::Unknown
344 };
345
346 let time_to_empty_secs =
347 if status.battery_life_time != 0xFFFF_FFFF && status.battery_life_time > 0 {
348 Some(status.battery_life_time)
349 } else {
350 None
351 };
352
353 let time_to_full_secs =
354 if status.battery_full_life_time != 0xFFFF_FFFF && status.battery_full_life_time > 0 {
355 Some(status.battery_full_life_time)
356 } else {
357 None
358 };
359
360 let is_low_power_mode = status.system_status_flag == 1;
361
362 Some(BatteryInfo {
363 level,
364 state,
365 is_low_power_mode,
366 time_to_empty_secs,
367 time_to_full_secs,
368 })
369}
370
371#[derive(Debug, Clone, PartialEq)]
372pub struct MacOsPowerSourceSnapshot {
373 pub current_capacity: u32,
374 pub max_capacity: u32,
375 pub is_charging: bool,
376 pub is_charged: bool,
377 pub is_low_power_mode: bool,
378 pub time_to_empty_min: Option<i32>,
379 pub time_to_full_min: Option<i32>,
380 pub power_source_state: String,
381}
382
383pub fn decode_macos_power_snapshot(snap: &MacOsPowerSourceSnapshot) -> Option<BatteryInfo> {
385 if snap.max_capacity == 0 {
386 return None;
387 }
388
389 let level = (snap.current_capacity as f32 / snap.max_capacity as f32).clamp(0.0, 1.0);
390
391 let state = if snap.is_charged {
392 BatteryState::Full
393 } else if snap.is_charging {
394 BatteryState::Charging
395 } else if snap.power_source_state == "AC Power" {
396 BatteryState::NotCharging
397 } else {
398 BatteryState::Discharging
399 };
400
401 let time_to_empty_secs = match snap.time_to_empty_min {
402 Some(mins) if mins > 0 => Some((mins as u32).saturating_mul(60)),
403 _ => None,
404 };
405
406 let time_to_full_secs = match snap.time_to_full_min {
407 Some(mins) if mins > 0 => Some((mins as u32).saturating_mul(60)),
408 _ => None,
409 };
410
411 Some(BatteryInfo {
412 level,
413 state,
414 is_low_power_mode: snap.is_low_power_mode,
415 time_to_empty_secs,
416 time_to_full_secs,
417 })
418}
419
420#[cfg(target_os = "macos")]
425fn probe_macos_battery() -> Option<BatteryInfo> {
426 use std::ffi::c_void;
427
428 type CFTypeRef = *const c_void;
429 type CFArrayRef = *const c_void;
430 type CFDictionaryRef = *const c_void;
431 type CFStringRef = *const c_void;
432
433 #[link(name = "IOKit", kind = "framework")]
434 extern "C" {
435 fn IOPSCopyPowerSourcesInfo() -> CFTypeRef;
436 fn IOPSCopyPowerSourcesList(blob: CFTypeRef) -> CFArrayRef;
437 fn IOPSGetPowerSourceDescription(blob: CFTypeRef, ps: CFTypeRef) -> CFDictionaryRef;
438 }
439
440 #[link(name = "CoreFoundation", kind = "framework")]
441 extern "C" {
442 fn CFArrayGetCount(theArray: CFArrayRef) -> isize;
443 fn CFArrayGetValueAtIndex(theArray: CFArrayRef, idx: isize) -> CFTypeRef;
444 fn CFDictionaryGetValue(theDict: CFDictionaryRef, key: CFStringRef) -> CFTypeRef;
445 fn CFRelease(cf: CFTypeRef);
446 fn CFStringCreateWithCString(
447 alloc: CFTypeRef,
448 c_str: *const std::os::raw::c_char,
449 encoding: u32,
450 ) -> CFStringRef;
451 fn CFNumberGetValue(number: CFTypeRef, theType: isize, valuePtr: *mut c_void) -> bool;
452 fn CFBooleanGetValue(boolean: CFTypeRef) -> bool;
453 fn CFStringGetCString(
454 theString: CFStringRef,
455 buffer: *mut std::os::raw::c_char,
456 bufferSize: isize,
457 encoding: u32,
458 ) -> bool;
459 }
460
461 unsafe {
462 let blob = IOPSCopyPowerSourcesInfo();
463 if blob.is_null() {
464 return None;
465 }
466
467 let list = IOPSCopyPowerSourcesList(blob);
468 if list.is_null() {
469 CFRelease(blob);
470 return None;
471 }
472
473 let count = CFArrayGetCount(list);
474 if count == 0 {
475 CFRelease(list);
476 CFRelease(blob);
477 return None; }
479
480 let ps = CFArrayGetValueAtIndex(list, 0);
481 let dict = IOPSGetPowerSourceDescription(blob, ps);
482 if dict.is_null() {
483 CFRelease(list);
484 CFRelease(blob);
485 return None;
486 }
487
488 const K_CF_STRING_ENCODING_UTF8: u32 = 0x0800_0100;
489 let make_cf_str = |s: &'static [u8]| -> CFStringRef {
490 CFStringCreateWithCString(
491 std::ptr::null(),
492 s.as_ptr() as *const _,
493 K_CF_STRING_ENCODING_UTF8,
494 )
495 };
496
497 let key_cur = make_cf_str(b"Current Capacity\0");
498 let key_max = make_cf_str(b"Max Capacity\0");
499 let key_charging = make_cf_str(b"Is Charging\0");
500 let key_charged = make_cf_str(b"Is Charged\0");
501 let key_empty = make_cf_str(b"Time to Empty\0");
502 let key_full = make_cf_str(b"Time to Full Charge\0");
503 let key_state = make_cf_str(b"Power Source State\0");
504
505 let mut current_capacity = 0i32;
506 let mut max_capacity = 0i32;
507 let mut time_to_empty = -1i32;
508 let mut time_to_full = -1i32;
509
510 let cur_val = CFDictionaryGetValue(dict, key_cur);
511 if !cur_val.is_null() {
512 CFNumberGetValue(
513 cur_val,
514 3, &mut current_capacity as *mut _ as *mut c_void,
516 );
517 }
518
519 let max_val = CFDictionaryGetValue(dict, key_max);
520 if !max_val.is_null() {
521 CFNumberGetValue(max_val, 3, &mut max_capacity as *mut _ as *mut c_void);
522 }
523
524 let charging_val = CFDictionaryGetValue(dict, key_charging);
525 let is_charging = !charging_val.is_null() && CFBooleanGetValue(charging_val);
526
527 let charged_val = CFDictionaryGetValue(dict, key_charged);
528 let is_charged = !charged_val.is_null() && CFBooleanGetValue(charged_val);
529
530 let empty_val = CFDictionaryGetValue(dict, key_empty);
531 if !empty_val.is_null() {
532 CFNumberGetValue(empty_val, 3, &mut time_to_empty as *mut _ as *mut c_void);
533 }
534
535 let full_val = CFDictionaryGetValue(dict, key_full);
536 if !full_val.is_null() {
537 CFNumberGetValue(full_val, 3, &mut time_to_full as *mut _ as *mut c_void);
538 }
539
540 let mut power_source_state = String::new();
541 let state_val = CFDictionaryGetValue(dict, key_state);
542 if !state_val.is_null() {
543 let mut buf = [0u8; 64];
544 if CFStringGetCString(
545 state_val,
546 buf.as_mut_ptr() as *mut _,
547 64,
548 K_CF_STRING_ENCODING_UTF8,
549 ) {
550 if let Ok(s) = std::ffi::CStr::from_ptr(buf.as_ptr() as *const _).to_str() {
551 power_source_state = s.to_string();
552 }
553 }
554 }
555
556 CFRelease(key_cur);
557 CFRelease(key_max);
558 CFRelease(key_charging);
559 CFRelease(key_charged);
560 CFRelease(key_empty);
561 CFRelease(key_full);
562 CFRelease(key_state);
563 CFRelease(list);
564 CFRelease(blob);
565
566 let is_low_power_mode = probe_macos_low_power_mode();
567
568 let snapshot = MacOsPowerSourceSnapshot {
569 current_capacity: current_capacity.max(0) as u32,
570 max_capacity: max_capacity.max(0) as u32,
571 is_charging,
572 is_charged,
573 is_low_power_mode,
574 time_to_empty_min: if time_to_empty >= 0 {
575 Some(time_to_empty)
576 } else {
577 None
578 },
579 time_to_full_min: if time_to_full >= 0 {
580 Some(time_to_full)
581 } else {
582 None
583 },
584 power_source_state,
585 };
586
587 decode_macos_power_snapshot(&snapshot)
588 }
589}
590
591#[cfg(target_os = "macos")]
592fn probe_macos_low_power_mode() -> bool {
593 use std::ffi::c_void;
594 #[link(name = "objc")]
595 #[link(name = "Foundation", kind = "framework")]
596 extern "C" {
597 fn objc_getClass(name: *const std::os::raw::c_char) -> *mut c_void;
598 fn sel_registerName(name: *const std::os::raw::c_char) -> *mut c_void;
599 }
600 type MsgSend = unsafe extern "C" fn(*mut c_void, *mut c_void) -> *mut c_void;
601 type MsgSendBool = unsafe extern "C" fn(*mut c_void, *mut c_void) -> bool;
602
603 unsafe {
604 let cls = objc_getClass(b"NSProcessInfo\0".as_ptr() as *const _);
605 if cls.is_null() {
606 return false;
607 }
608 let sel_proc_info = sel_registerName(b"processInfo\0".as_ptr() as *const _);
609 let msg_send: MsgSend = std::mem::transmute(libc::dlsym(
610 libc::RTLD_DEFAULT,
611 b"objc_msgSend\0".as_ptr() as *const _,
612 ));
613 let proc_info = msg_send(cls, sel_proc_info);
614 if proc_info.is_null() {
615 return false;
616 }
617 let sel_low_power = sel_registerName(b"isLowPowerModeEnabled\0".as_ptr() as *const _);
618 let msg_send_bool: MsgSendBool = std::mem::transmute(libc::dlsym(
619 libc::RTLD_DEFAULT,
620 b"objc_msgSend\0".as_ptr() as *const _,
621 ));
622 msg_send_bool(proc_info, sel_low_power)
623 }
624}
625
626#[cfg(target_os = "macos")]
627fn probe_macos_thermal() -> Option<ThermalState> {
628 use std::ffi::c_void;
629 #[link(name = "objc")]
630 #[link(name = "Foundation", kind = "framework")]
631 extern "C" {
632 fn objc_getClass(name: *const std::os::raw::c_char) -> *mut c_void;
633 fn sel_registerName(name: *const std::os::raw::c_char) -> *mut c_void;
634 }
635 type MsgSend = unsafe extern "C" fn(*mut c_void, *mut c_void) -> *mut c_void;
636 type MsgSendState = unsafe extern "C" fn(*mut c_void, *mut c_void) -> isize;
637
638 unsafe {
639 let cls = objc_getClass(b"NSProcessInfo\0".as_ptr() as *const _);
640 if cls.is_null() {
641 return None;
642 }
643 let sel_proc_info = sel_registerName(b"processInfo\0".as_ptr() as *const _);
644 let msg_send: MsgSend = std::mem::transmute(libc::dlsym(
645 libc::RTLD_DEFAULT,
646 b"objc_msgSend\0".as_ptr() as *const _,
647 ));
648 let proc_info = msg_send(cls, sel_proc_info);
649 if proc_info.is_null() {
650 return None;
651 }
652 let sel_thermal = sel_registerName(b"thermalState\0".as_ptr() as *const _);
653 let msg_send_state: MsgSendState = std::mem::transmute(libc::dlsym(
654 libc::RTLD_DEFAULT,
655 b"objc_msgSend\0".as_ptr() as *const _,
656 ));
657 let state = msg_send_state(proc_info, sel_thermal);
658 match state {
659 0 => Some(ThermalState::Nominal),
660 1 => Some(ThermalState::Fair),
661 2 => Some(ThermalState::Serious),
662 3 => Some(ThermalState::Critical),
663 _ => Some(ThermalState::Nominal),
664 }
665 }
666}
667
668#[cfg(target_os = "macos")]
669fn probe_macos_network() -> Option<NetworkStatus> {
670 use std::ffi::c_void;
671 type SCNetworkReachabilityRef = *const c_void;
672
673 #[link(name = "SystemConfiguration", kind = "framework")]
674 extern "C" {
675 fn SCNetworkReachabilityCreateWithName(
676 allocator: *const c_void,
677 nodename: *const std::os::raw::c_char,
678 ) -> SCNetworkReachabilityRef;
679 fn SCNetworkReachabilityGetFlags(target: SCNetworkReachabilityRef, flags: *mut u32)
680 -> bool;
681 }
682
683 #[link(name = "CoreFoundation", kind = "framework")]
684 extern "C" {
685 fn CFRelease(cf: *const c_void);
686 }
687
688 unsafe {
689 let target = SCNetworkReachabilityCreateWithName(
690 std::ptr::null(),
691 b"apple.com\0".as_ptr() as *const _,
692 );
693 if target.is_null() {
694 return None;
695 }
696
697 let mut flags = 0u32;
698 let ok = SCNetworkReachabilityGetFlags(target, &mut flags);
699 CFRelease(target);
700
701 if !ok {
702 return None;
703 }
704
705 const K_REACHABLE: u32 = 1 << 1;
706 const K_CONNECTION_REQUIRED: u32 = 1 << 2;
707 const K_IS_WWAN: u32 = 1 << 18;
708
709 let is_online = (flags & K_REACHABLE) != 0 && (flags & K_CONNECTION_REQUIRED) == 0;
710 let connection_type = if !is_online {
711 ConnectionType::None
712 } else if (flags & K_IS_WWAN) != 0 {
713 ConnectionType::Cellular
714 } else {
715 ConnectionType::Wifi
716 };
717
718 Some(NetworkStatus {
719 is_online,
720 connection_type,
721 cellular_generation: None,
722 downlink_mbps: if is_online { Some(100.0) } else { Some(0.0) },
723 rtt_ms: if is_online { Some(15) } else { None },
724 is_metered: connection_type == ConnectionType::Cellular,
725 })
726 }
727}
728
729#[cfg(target_os = "windows")]
734fn probe_windows_battery() -> Option<BatteryInfo> {
735 #[link(name = "kernel32")]
736 extern "system" {
737 fn GetSystemPowerStatus(lpSystemPowerStatus: *mut Win32SystemPowerStatus) -> i32;
738 }
739
740 let mut status = Win32SystemPowerStatus {
741 ac_line_status: 255,
742 battery_flag: 255,
743 battery_life_percent: 255,
744 system_status_flag: 0,
745 battery_life_time: 0xFFFF_FFFF,
746 battery_full_life_time: 0xFFFF_FFFF,
747 };
748
749 let success = unsafe { GetSystemPowerStatus(&mut status) };
750 if success == 0 {
751 None
752 } else {
753 decode_win32_power_status(&status)
754 }
755}
756
757#[cfg(target_os = "windows")]
758fn probe_windows_thermal() -> Option<ThermalState> {
759 Some(ThermalState::Nominal)
761}
762
763#[cfg(target_os = "windows")]
764fn probe_windows_network() -> Option<NetworkStatus> {
765 #[link(name = "wininet")]
766 extern "system" {
767 fn InternetGetConnectedState(lpdwFlags: *mut u32, dwReserved: u32) -> i32;
768 }
769
770 let mut flags = 0u32;
771 let connected = unsafe { InternetGetConnectedState(&mut flags, 0) };
772 let is_online = connected != 0 && (flags & 0x20 == 0); let connection_type = if !is_online {
775 ConnectionType::None
776 } else if (flags & 0x02) != 0 {
777 ConnectionType::Ethernet
779 } else if (flags & 0x01) != 0 {
780 ConnectionType::Cellular
782 } else {
783 ConnectionType::Wifi
784 };
785
786 Some(NetworkStatus {
787 is_online,
788 connection_type,
789 cellular_generation: None,
790 downlink_mbps: if is_online { Some(100.0) } else { Some(0.0) },
791 rtt_ms: if is_online { Some(15) } else { None },
792 is_metered: connection_type == ConnectionType::Cellular,
793 })
794}