diff --git a/examples/extra_data.rs b/examples/extra_data.rs new file mode 100644 index 00000000..6af4cca7 --- /dev/null +++ b/examples/extra_data.rs @@ -0,0 +1,22 @@ +use cyme::lsusb::profiler; + +fn main() -> Result<(), String> { + // get all system devices - this time with extra data which contain the USBConfiguration, driver data (with udev) + let sp_usb = profiler::get_spusb_with_extra(false).map_err(|e| { + format!("Failed to gather system USB data from libusb, Error({})", e) + })?; + + let devices = sp_usb.flatten_devices(); + + // print all configurations + for device in devices { + device.extra.as_ref().map(|extra| { + println!("Device {} has configurations:", device.name); + for c in extra.configurations.iter() { + println!("{:?}", c); + }; + }); + } + + Ok(()) +} diff --git a/examples/filter_devices.rs b/examples/filter_devices.rs new file mode 100644 index 00000000..79a75a61 --- /dev/null +++ b/examples/filter_devices.rs @@ -0,0 +1,34 @@ +/// This example shows how to use the USBFilter to filter out devices that match a certain criteria +/// +/// See [`USBFilter`] docs for more information +use cyme::lsusb::profiler; +use cyme::usb::ClassCode; +use cyme::system_profiler::USBFilter; + +fn main() -> Result<(), String> { + // get all system devices + let mut sp_usb = profiler::get_spusb(false).map_err(|e| { + format!("Failed to gather system USB data from libusb, Error({})", e) + })?; + + // if one does want the tree, use the utility + let filter = USBFilter { + class: Some(ClassCode::HID), + ..Default::default() + }; + + // will retain only the buses that have devices that match the filter - parent devices such as hubs with a HID device will be retained + filter.retain_buses(&mut sp_usb.buses); + sp_usb.buses.retain(|b| b.devices.as_ref().map_or(false, |d| d.is_empty())); + + // if one does not care about the tree, flatten the devices and do manually + // let hid_devices = sp_usb.flatten_devices().iter().filter(|d| d.class == Some(ClassCode::HID)); + + if sp_usb.buses.is_empty() { + println!("No HID devices found"); + } else { + println!("Found HID devices"); + } + + Ok(()) +} diff --git a/examples/walk_sp_data.rs b/examples/walk_sp_data.rs new file mode 100644 index 00000000..ba4c6f5e --- /dev/null +++ b/examples/walk_sp_data.rs @@ -0,0 +1,32 @@ +use cyme::lsusb::profiler; +use cyme::system_profiler::USBDevice; + +fn recusive_map_devices(device: &USBDevice) { + // the alternate format will print with colour + println!("Device: {:#}", device); + device.devices.as_ref().map(|v| { + for d in v { + recusive_map_devices(d) + } + }); +} + +fn main() -> Result<(), String> { + // get all system devices + let sp_usb = profiler::get_spusb(false).map_err(|e| { + format!("Failed to gather system USB data from libusb, Error({})", e) + })?; + + // SPUSBDataType contains buses... + for bus in sp_usb.buses { + // which may contain devices... + bus.devices.as_ref().map(|devices| { + // to walk all the devices, since each device can have devices attached, call a recursive function + for device in devices { + recusive_map_devices(device); + } + }); + } + + Ok(()) +}