Skip to content

Commit

Permalink
Merge pull request #133 from Sainan/allow-narrow-enumeration
Browse files Browse the repository at this point in the history
Add HidApi::reset_devices & HidApi::add_devices
  • Loading branch information
ruabmbua authored Nov 11, 2023
2 parents 78f2738 + c03fc8d commit b69e5db
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "hidapi"
version = "2.4.1"
version = "2.5.0"
authors = [
"Roland Ruckerbauer <[email protected]>",
"Osspial <[email protected]>",
Expand Down
4 changes: 2 additions & 2 deletions src/hidapi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ const STRING_BUF_LEN: usize = 128;
pub struct HidApiBackend;

impl HidApiBackend {
pub fn get_hid_device_info_vector() -> HidResult<Vec<DeviceInfo>> {
pub fn get_hid_device_info_vector(vid: u16, pid: u16) -> HidResult<Vec<DeviceInfo>> {
let mut device_vector = Vec::with_capacity(8);

let enumeration = unsafe { ffi::hid_enumerate(0, 0) };
let enumeration = unsafe { ffi::hid_enumerate(vid, pid) };
{
let mut current_device = enumeration;

Expand Down
29 changes: 23 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,11 @@ impl HidApi {
pub fn new() -> HidResult<Self> {
lazy_init(true)?;

let device_list = HidApiBackend::get_hid_device_info_vector()?;

Ok(HidApi { device_list })
let mut api = HidApi {
device_list: Vec::with_capacity(8),
};
api.add_devices(0, 0)?;
Ok(api)
}

/// Create a new hidapi context, in "do not enumerate" mode.
Expand All @@ -196,13 +198,28 @@ impl HidApi {

/// Refresh devices list and information about them (to access them use
/// `device_list()` method)
/// Identical to `reset_devices()` followed by `add_devices(0, 0)`.
pub fn refresh_devices(&mut self) -> HidResult<()> {
let device_list = HidApiBackend::get_hid_device_info_vector()?;
self.device_list = device_list;
self.reset_devices()?;
self.add_devices(0, 0)?;
Ok(())
}

/// Reset devices list. Intended to be used with the `add_devices` method.
pub fn reset_devices(&mut self) -> HidResult<()> {
self.device_list.clear();
Ok(())
}

/// Indexes devices that match the given VID and PID filters.
/// 0 indicates no filter.
pub fn add_devices(&mut self, vid: u16, pid: u16) -> HidResult<()> {
self.device_list.append(&mut HidApiBackend::get_hid_device_info_vector(vid, pid)?);
Ok(())
}

/// Returns iterator containing information about attached HID devices.
/// Returns iterator containing information about attached HID devices
/// that have been indexed, either by `refresh_devices` or `add_devices`.
pub fn device_list(&self) -> impl Iterator<Item = &DeviceInfo> {
self.device_list.iter()
}
Expand Down
6 changes: 4 additions & 2 deletions src/linux_native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const BUS_SPI: u16 = 0x1C;
pub struct HidApiBackend;

impl HidApiBackend {
pub fn get_hid_device_info_vector() -> HidResult<Vec<DeviceInfo>> {
pub fn get_hid_device_info_vector(vid: u16, pid: u16) -> HidResult<Vec<DeviceInfo>> {
// The C version assumes these can't fail, and they should only fail in case
// of memory allocation issues, at which point maybe we should panic
let mut enumerator = match udev::Enumerator::new() {
Expand All @@ -49,6 +49,8 @@ impl HidApiBackend {
let devices = scan
.filter_map(|device| device_to_hid_device_info(&device))
.flatten()
.filter(|device| vid == 0 || device.vendor_id == vid)
.filter(|device| pid == 0 || device.product_id == pid)
.collect::<Vec<_>>();

Ok(devices)
Expand Down Expand Up @@ -411,7 +413,7 @@ unsafe impl Send for HidDevice {}
// API for the library to call us, or for internal uses
impl HidDevice {
pub(crate) fn open(vid: u16, pid: u16, sn: Option<&str>) -> HidResult<Self> {
for device in HidApiBackend::get_hid_device_info_vector()?
for device in HidApiBackend::get_hid_device_info_vector(0, 0)?
.iter()
.filter(|device| device.vendor_id == vid && device.product_id == pid)
{
Expand Down
4 changes: 2 additions & 2 deletions src/windows_native/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ macro_rules! ensure {

pub struct HidApiBackend;
impl HidApiBackend {
pub fn get_hid_device_info_vector() -> HidResult<Vec<DeviceInfo>> {
Ok(enumerate_devices(0, 0)?)
pub fn get_hid_device_info_vector(vid: u16, pid: u16) -> HidResult<Vec<DeviceInfo>> {
Ok(enumerate_devices(vid, pid)?)
}

pub fn open(vid: u16, pid: u16) -> HidResult<HidDevice> {
Expand Down

0 comments on commit b69e5db

Please sign in to comment.