use std::env;
use std::ffi::{CStr, CString};
use std::os::unix::ffi::OsStrExt;
use std::path::PathBuf;
use switchtec_user_sys::{
switchtec_close, switchtec_dev, switchtec_die_temp, switchtec_open, switchtec_strerror,
};
fn get_temperature(dev: *mut switchtec_dev) -> f32 {
unsafe { switchtec_die_temp(dev) }
}
fn main() -> anyhow::Result<()> {
let path: PathBuf = env::args()
.skip(1)
.next()
.unwrap_or_else(|| "/dev/pciswitch0".to_owned())
.into();
let cpath = CString::new(path.as_os_str().as_bytes())?;
let dev = unsafe { switchtec_open(cpath.as_ptr()) };
if dev.is_null() {
let err = unsafe { CStr::from_ptr(switchtec_strerror()).to_owned() };
anyhow::bail!(
"Unable to open switchtec device at {}: {}",
path.display(),
err.into_string()?
);
}
let temp = get_temperature(dev);
println!("Temp: {}C", temp / 100.0);
unsafe {
switchtec_close(dev);
}
Ok(())
}