#![allow(non_snake_case)]
#![allow(non_upper_case_globals)]
#![allow(dead_code)]
use std::mem::size_of;
use std::os::raw::{c_int, c_void};
use std::ptr;
use crate::ported::networkiometer::NetworkIOData;
const VM_LOADAVG: c_int = 2;
#[repr(C)]
struct loadavg {
ldavg: [u32; 3],
fscale: libc::c_long,
}
pub fn Platform_init() -> bool {
true
}
pub fn Platform_done() {
}
pub fn Platform_setBindings() {
}
pub fn Platform_getUptime() -> c_int {
let mut bootTime: libc::timeval = unsafe { std::mem::zeroed() };
let mib: [c_int; 2] = [libc::CTL_KERN, libc::KERN_BOOTTIME];
let mut size = size_of::<libc::timeval>();
let err = unsafe {
libc::sysctl(
mib.as_ptr(),
2,
&mut bootTime as *mut libc::timeval as *mut c_void,
&mut size,
ptr::null(),
0,
)
};
if err != 0 {
return -1;
}
let mut currTime: libc::timeval = unsafe { std::mem::zeroed() };
unsafe { libc::gettimeofday(&mut currTime, ptr::null_mut()) };
(currTime.tv_sec - bootTime.tv_sec) as c_int
}
pub fn Platform_getLoadAverage(one: &mut f64, five: &mut f64, fifteen: &mut f64) {
let mut loadAverage: loadavg = unsafe { std::mem::zeroed() };
let mib: [c_int; 2] = [libc::CTL_VM, VM_LOADAVG];
let mut size = size_of::<loadavg>();
let err = unsafe {
libc::sysctl(
mib.as_ptr(),
2,
&mut loadAverage as *mut loadavg as *mut c_void,
&mut size,
ptr::null(),
0,
)
};
if err != 0 {
*one = 0.0;
*five = 0.0;
*fifteen = 0.0;
return;
}
*one = loadAverage.ldavg[0] as f64 / loadAverage.fscale as f64;
*five = loadAverage.ldavg[1] as f64 / loadAverage.fscale as f64;
*fifteen = loadAverage.ldavg[2] as f64 / loadAverage.fscale as f64;
}
pub fn Platform_getMaxPid() -> libc::pid_t {
i32::MAX
}
pub fn Platform_setCPUValues() {
todo!("port of Platform.c:257")
}
pub fn Platform_setMemoryValues() {
todo!("port of Platform.c:290")
}
pub fn Platform_setSwapValues() {
todo!("port of Platform.c:300")
}
pub fn Platform_getProcessEnv() {
todo!("port of Platform.c:306")
}
pub fn Platform_getProcessLocks() {
todo!("port of Platform.c:360")
}
pub fn Platform_getFileDescriptors() {
todo!("port of Platform.c:365")
}
pub fn Platform_getDiskIO() {
todo!("port of Platform.c:369")
}
pub fn Platform_getNetworkIO(data: &mut NetworkIOData) -> bool {
let mut ifap: *mut libc::ifaddrs = ptr::null_mut();
if unsafe { libc::getifaddrs(&mut ifap) } != 0 {
return false;
}
let mut ifa = ifap;
while !ifa.is_null() {
let cur = unsafe { &*ifa };
ifa = cur.ifa_next;
if cur.ifa_addr.is_null() {
continue;
}
if unsafe { (*cur.ifa_addr).sa_family } as c_int != libc::AF_LINK {
continue;
}
if cur.ifa_flags & libc::IFF_LOOPBACK as libc::c_uint != 0 {
continue;
}
let ifd = cur.ifa_data as *const libc::if_data;
if ifd.is_null() {
continue;
}
let d = unsafe { &*ifd };
data.bytesReceived += d.ifi_ibytes;
data.packetsReceived += d.ifi_ipackets;
data.bytesTransmitted += d.ifi_obytes;
data.packetsTransmitted += d.ifi_opackets;
}
unsafe { libc::freeifaddrs(ifap) };
true
}
pub fn Platform_getBattery() {
todo!("port of Platform.c:449")
}