Skip to content

Commit

Permalink
Merge pull request #42 from serpilliere/get_port_numbers
Browse files Browse the repository at this point in the history
Add port_numbers API
  • Loading branch information
a1ien authored Oct 17, 2020
2 parents e8863f0 + 67e633e commit 62e814e
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::{
device_handle::DeviceHandle,
fields::{self, Speed},
Error, UsbContext,
error,
};

/// A reference to a USB device.
Expand Down Expand Up @@ -153,4 +154,26 @@ impl<T: UsbContext> Device<T> {
}
)
}

/// Get the list of all port numbers from root for the specified device
pub fn port_numbers(&self) -> Result<Vec<u8>, Error> {
// As per the USB 3.0 specs, the current maximum limit for the depth is 7.
let mut ports = [0;7];

let result = unsafe {
libusb_get_port_numbers(
self.device.as_ptr(),
ports.as_mut_ptr(),
ports.len() as i32
)
};

let ports_number = if result < 0 {
return Err(error::from_libusb(result));
} else {
result
};
Ok(ports[0..ports_number as usize].to_vec())
}

}

0 comments on commit 62e814e

Please sign in to comment.