switchtec-user-sys
A Rust wrapper for the switchtec-user
library.
Details and usage instructions for the switchtec-user
library can be found here.
Example Usage
Get pciswitch device name and temperature
Example using the switchtec_name
and switchtec_die_temp
functions provided by the switchtec-user
library
use std::env;
use switchtec_user_sys::{switchtec_die_temp, switchtec_name, SwitchtecDevice, CStrExt};
fn main() -> anyhow::Result<()> {
let path = env::args()
.skip(1)
.next()
.unwrap_or_else(|| "/dev/pciswitch0".to_owned());
let device = SwitchtecDevice::new(path).open()?;
let (device_name, temperature) = unsafe {
let temp = switchtec_die_temp(*device);
let name = switchtec_name(*device).as_string()?;
(name, temp)
};
println!("Temperature for {device_name}: {temperature}");
Ok(())
}
Get status for each port for a pciswitch device
A more complex example using an out-value struct with switchtec_status
use std::env;
use std::io;
use std::ptr;
use switchtec_user_sys::{switchtec_status, switchtec_status_free, SwitchtecDevice};
fn main() -> anyhow::Result<()> {
let path: std::path::PathBuf = "/dev/pciswitch1".into();
let device = SwitchtecDevice::new(&path).open()?;
let mut status: *mut switchtec_user_sys::switchtec_status = ptr::null_mut();
let per_port_status = unsafe {
let port_count = switchtec_status(*device, ptr::addr_of_mut!(status));
let resp = if status.is_null() || port_count.is_negative() {
Err(io::Error::new(io::ErrorKind::Other, format!("Unknown error")))
} else {
let statuses: Vec<_> = std::slice::from_raw_parts(status, port_count as usize)
.iter()
.take(port_count as usize)
.copied()
.collect();
Ok(statuses)
};
switchtec_status_free(status as *mut _, port_count);
resp
}?;
println!("{per_port_status:#?}");
Ok(())
}
License
switchtec-user-sys
is both MIT and Apache License, Version 2.0 licensed, as found
in the LICENSE-MIT and LICENSE-APACHE files.