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

Get vendorId & productId from hid_device or path #431

Closed
Julusian opened this issue Jun 19, 2022 · 1 comment · Fixed by #432
Closed

Get vendorId & productId from hid_device or path #431

Julusian opened this issue Jun 19, 2022 · 1 comment · Fixed by #432
Labels
enhancement New feature or request

Comments

@Julusian
Copy link
Contributor

I maintain a couple of nodejs libraries (such as elgato-stream-deck) which use this library via node-hid.

In those I expose a method which takes in a path to the device and opens it (such as function openStreamDeck(devicePath: string): StreamDeck), returning a class to interact with the device.
But to do this, I need to check the productId of the hid device to determine the model being opened. In this streamdeck library there are 6 models of compatible device, each with a slightly different protocol.

My current method for this is to do a hid_enumerate before opening the device to find the productId, but this is horribly inefficient.
Ideally there would be a method to get the hid_device_info for a specific path, or be able to get the productId and vendorId from a hid_device
I might be able to achieve the same with hid_get_manufacturer_string and hid_get_product_string, but I suspect that two of the models will report the same name, even though their protocol is completely different.

I can try and implement one of these, but would like to discuss what the api should look like first.

It looks like for windows there is already a hid_device_info stored inside hid_device.
macos has the create_device_info_with_usage function that could be used to create and store a hid_device_info, and at the same time the hid_get_manufacturer_string and related methods could be updated to copy off that struct like windows is doing.
linux looks like it will take a bit of work to refactor parts of hid_enumerate to be reusable, but it doesnt look like there is anything stopping it from being done. As another bonus, get_device_string could be removed.
libusb also looks like it will need a bit of refactoring to be compatible.

So as windows already has a hid_device_info stored inside hid_device, I think it makes sense to replicate this for all backends.
Then one of these methods could be added to access some more of the information it stores:

  • int HID_API_EXPORT HID_API_CALL hid_get_device_ids(hid_device *dev, unsigned short *vendor_id, unsinged short *product_id);
  • struct hid_device_info HID_API_EXPORT HID_API_CALL hid_get_device_info(hid_device *dev);
  • or something else?
@Youw
Copy link
Member

Youw commented Jun 19, 2022

Thanks for the analysis.

struct hid_device_info HID_API_EXPORT * HID_API_CALL hid_get_device_info(hid_device *dev);

I think this would be the way to go - way more flexible in a long term.

libusb also looks like it will need a bit of refactoring to be compatible.

That should be almost trivial. libusb has all the API for it.

linux looks like it will take a bit of work

That I agree. Right now linux/hidraw backend uses udev to gather all the information about the device, and I believe that long-term that is not a good approach. hidraw is a kernel API, so we better use kernel API to gather the information on the device, i.e. sysfs. The most difficulty is the variety of protocols (USB/Bluetooth/I2C/etc.) that needs to be tested with sysfs, and personally I only have USB HID devices that I can reliebly test.

@Youw Youw closed this as completed Jun 19, 2022
@Youw Youw reopened this Jun 19, 2022
@Youw Youw added the enhancement New feature or request label Jun 20, 2022
@Youw Youw closed this as completed in #432 Aug 13, 2022
Youw pushed a commit that referenced this issue Aug 13, 2022
- new API function: `struct hid_device_info * hid_get_device_info(hid_device *dev);` to get `hid_device_info` after the device already opened;
- reused existing implementation on Windows and macOS from enumeration routines to have the implementation;
- refactored libusb implementation to have a shared routine for `hid_enumerate` and `hid_get_device_info`;
- refactored hidraw implementation to have a shared routine for `hid_enumerate` and `hid_get_device_info`;

Resolves: #431
Closes: #164
Closes: #163
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants