Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hid_enumerate specify multiple VID/PID combos. #225

Open
FabianKopp opened this issue Dec 24, 2020 · 12 comments
Open

hid_enumerate specify multiple VID/PID combos. #225

FabianKopp opened this issue Dec 24, 2020 · 12 comments
Labels
API API change, Version 1 stuff Core Related to common codes like hidapi.h enhancement New feature or request

Comments

@FabianKopp
Copy link

As of now you can only specify one pair of VID PID in the hid_enumerate function. Is it possible to have a version where you can supply multiple sets of VID PIDs to the function? This would be of great use for anyone scanning for multiple products.

@Youw
Copy link
Member

Youw commented Dec 25, 2020

use hid_enumerate with (0, 0) VID/PID - it will give you all the devices, so you can filter that list on your own.

@FabianKopp
Copy link
Author

FabianKopp commented Dec 25, 2020

Right, but it turns out that running hid_enumerate(0,0) repeatedly is very bad and causes many issues on user's computers. For example it can cause clicking sounds on microphones and make other HID devices unusable. It is better to filter within the enumerate function than to run a full getDescriptor request on every single hid device on the computer.

@Youw
Copy link
Member

Youw commented Dec 25, 2020

Not repeatedly. Enumerate once - search/filter all the devices you need in that list.

@FabianKopp
Copy link
Author

But if you have a program and device where you have to repeatedly check for when the device was inserted or removed on the fly this does not work.

@Youw
Copy link
Member

Youw commented Dec 25, 2020

I'm confused. Checking for hotplug/unplug events is entirely different problem, then you originally described (finding multiple devices with a single hid_enumerate).

@FabianKopp
Copy link
Author

FabianKopp commented Dec 25, 2020

One detects plug and unplug events by repeatedly enumerating the the devices and checking the difference between enumerations

@Youw
Copy link
Member

Youw commented Dec 25, 2020

And how passing multiple VIDs/PIDs into hid_enumerate help with "audio clicks" each time you call it?

@FabianKopp
Copy link
Author

When you do hid_enumerate(0,0) it executes getDescriptor requests on every single usb device on your computer even devices such as microphones that I'm not interested in. Resource limited devices with weak CPUs such a microphones cannot do their regular job and respond to the request at the same time. When the microphone receives the request it stops streaming audio data briefly so that it can respond. This can be heard as a short "pop" or "click". If we call hid_enumerate without wildcards and only specify which devices we care about, it avoids sending this disruptive getDescriptor request to sensitive devices such as microphones, etc.

@todbot
Copy link
Contributor

todbot commented Dec 25, 2020

In general, I would advise against using hid_enumerate() to detect insertion/removal events because it's so taxing to the USB bus system.

Instead, use an OS-specific USB insertion/removal event system. I don't know of a hidapi-like cross-platform C-based API for doing this, but you can check out the Nodejs package node-usb-detection to see the platform-specific code they use for Windows (RegisterDeviceNotificationA()), MacOS (IONotificationPortCreate()), and Linux (udev_monitor_receive_device()): https://github.com/MadLittleMods/node-usb-detection/tree/master/src

Another thing to note, there is a delay between device inserted and when it's available to hidapi, as the OS has to hook in the USB and HID driver. I've seen this delay be up to several seconds on MacOS and Windows.

@Youw
Copy link
Member

Youw commented Dec 25, 2020

Very good point from Tod.

Thanks for the explanation @FabianKopp, I think I understand your problem now.

it executes getDescriptor requests on every single usb device

  1. So I presume, it is not called when you enumerate the devices with a specific PID/VID?
  2. There is no direct call to what you call a getDescriptor in windows implementation, after the device is enumerated. All I see is HidD_GetPreparsedData, HidP_GetCaps and HidD_Get***String calls. Can you track down which one of those actually causing the negative "clicks" effect you mentioned?

@FabianKopp
Copy link
Author

FabianKopp commented Dec 25, 2020

Its pretty much everything after this filtering line: if ((vendor_id == 0x0 || attrib.VendorID == vendor_id) && (product_id == 0x0 || attrib.ProductID == product_id)) { Its HidD_GetPreparsedData, HidP_GetCaps, HidD_GetSerialNumberString, HidD_GetManufacturerString, HidD_GetProductString. These calls are taxing to the usb device and can be avoided on other devices by specifying the vid/pid you are looking for and ignoring the rest. I just think it would really helpful to be able to specify multiple vid/pids in case you are looking for 2 or more types of devices.

@Youw
Copy link
Member

Youw commented Dec 26, 2020

I'm asking, because I was thinking a more general aproach: enumerate function, that accepts a filter function over devices attrigutes (or even over a hid_device_info), but I guess it wouldn't help your case, since only VID/PID is available for filtering, without causing negative effect on your audio device(s).

@mcuee mcuee added the enhancement New feature or request label Jan 1, 2021
@mcuee mcuee added the Core Related to common codes like hidapi.h label Jul 18, 2021
@Youw Youw added the API API change, Version 1 stuff label Mar 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API API change, Version 1 stuff Core Related to common codes like hidapi.h enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants