Skip to content

Commit

Permalink
a few more examples
Browse files Browse the repository at this point in the history
  • Loading branch information
tuna-f1sh committed Oct 4, 2023
1 parent 78eee26 commit 3986cbe
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
22 changes: 22 additions & 0 deletions examples/extra_data.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
34 changes: 34 additions & 0 deletions examples/filter_devices.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
32 changes: 32 additions & 0 deletions examples/walk_sp_data.rs
Original file line number Diff line number Diff line change
@@ -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(())
}

0 comments on commit 3986cbe

Please sign in to comment.