use crate::mock::{DeviceMockController, DeviceTelemetrySnapshot};
use crate::types::*;
#[cfg(target_os = "linux")]
use std::fs;
#[cfg(target_os = "linux")]
use std::path::Path;
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
}
}
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
}
}
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
}
}
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
}
}
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
}
}
#[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;
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;
}
}
}
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,
};
}
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;
if (0.0..=130.0).contains(°_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; } 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()
}
#[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,
}
pub fn decode_win32_power_status(status: &Win32SystemPowerStatus) -> Option<BatteryInfo> {
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,
}
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,
})
}
#[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; }
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,
&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;
#[link(name = "objc")]
#[link(name = "Foundation", kind = "framework")]
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;
#[link(name = "objc")]
#[link(name = "Foundation", kind = "framework")]
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,
})
}
}
#[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> {
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);
let connection_type = if !is_online {
ConnectionType::None
} else if (flags & 0x02) != 0 {
ConnectionType::Ethernet
} else if (flags & 0x01) != 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,
})
}