You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Need API to get the HID Report Descriptor or a similar data structure #249
CoreRelated to common codes like hidapi.hWindowsRelated to Windows backendenhancementNew feature or requesthidrawRelated to Linux/hidraw backendlibusbRelated to libusb backendmacOSRelated to macOS backend
We need a function to read the HID Report Descriptor or an equal data structure with information about the fields in the different HID reports.
Background:
For the cross platform Mixxx DJ software we need a solution, to allow the end user to map HID controller buttons, faders, knobs, RGB LEDs, etc. These are different on any DJ controller. Therefore this mapping it must be customizeable by end users.
As input we need information about the field, e.g.: a 12 bit field at position n of Input report with ID0x02 with a logical minimum of -2048 and a maximum of +2047.
In my eyes it'd make sense to directly store this descriptor either
by (optionally) allocating memory linked through a new field in struct hid_device_info (simpler) or
by postponing allocation of found device structs here until the size of the report descriptor is found, allocating the base size along with the descriptor size and appending an indefinite size field to struct hid_device_info (feels more elegant)
That said, it might be nice or meaningful to somehow incorporate this quite nice descriptor to string parser based on which I'm momentarily trying to make a generic parser to help extract meaningful values from given reports without having to know the report layout beforehand (primarily trying to make it work with my use cases, but for simple devices should be fine).
Thanks for that info @tschiemer. I have not looked into what it would take to implement this. If you have a branch of hidapi you are working on, please open a pull request for this upstream repository and I'd be happy to test it with my Native Instruments Traktor Kontrol S4 Mk3.
Actually, it somewhat comes for free as the basic usage page and usage are extracted from the report descriptor
While this indeed already available and easy to get for libusb and hidraw backends, one of the first things about HIDAPI is being cross-platform.
I agree that this would be very useful, but Windows backend doesn't have an API to get a descriptor at all.
The descriptor could be partially reconstructed, but still, it is not an easy task.
I know one successfull attempt to reconstruct a HID descriptor using WinAPI calls, but that one is written on C# (https://github.com/todbot/win-hid-dump is using it). If someone could port it to C - I'd gladly introduce a new API (to get HID descriptor) for all platforms/backends.
There's also the approach to use the generic WinUSB driver (comes with Win7 or newer) instead of Windows High-Level-HID-API: #61 (comment)
As far as I understood it, the WinUSB driver gives a userspace Windows program full access to an USB device, but locks it exclusive.
gives a userspace Windows program full access to an USB device, but locks it exclusive
Yeah, and someone actually needs to make a driver for each specific device. And the problem not even with making a driver - a template inf file can be generated. But signing a driver properly - that requires some effort.
And you'd need to compile a libusb backend on Windows, which isn't compilable of-the-box yet.
@Be-ing so not quite a hidapi branch, but as a proof of concept I implemented this puredata external to parse input reports based on the report descriptor, works for me on macos and a rpi4/linux with a joystick and mouse (see releases for binaries).
It's a bit of a mess as it uses libusb to find devices according to search criteria (more than hidapi provides out of the box, but meaningful in my opinion and based on hidapi-libusb internals) and reads/parses the report descriptor but relies on hidapi to actually receive input reports (and thus on linux the device listing is set to detach devices as in the hidapi driver..) - should have had a deeper look at hidapi's (or hidraw) interface before..
At the moment only the raw values as given in the input report are extracted without making use of any of the logical/physical min/max, unit, exponential etc values but adding that shouldn't be too big of a deal..
Could not test this with any feature/output reports or more complicated report descriptors, as I have neither the devices nor much more capacity..
The hid report parser just creates a datastructure that's more easy to handle but I would guess it won't work for all descriptors (collection items are somewhat ignored; note that the handling of output and feature items likely will need some fixing in the parser..).
As external it "publishes" any changed values (usage page, usage, value), similar to the apple HIDIOManager value callbacks.
I guess construction of output reports can be achieved by inverting the logic of the value extractor.
I agree that this would be very useful, but Windows backend doesn't have an API to get a descriptor at all.
The descriptor could be partially reconstructed, but still, it is not an easy task.
I know one successfull attempt to reconstruct a HID descriptor using WinAPI calls, but that one is written on C# (https://github.com/todbot/win-hid-dump is using it). If someone could port it to C - I'd gladly introduce a new API (to get HID descriptor) for all platforms/backends.
I implemented such a reconstructor in C and will start a Pull Request for HIDAPI soon. Stay tuned!
I think libusb HID backend has already the codes to partially reconstruct the descriptors. Some from the USB HUB driver, some from the USB HID driver. But that may not be suitable for HIDAPI as it is also for bluetooth devices.
While this indeed already available and easy to get for libusb and hidraw backends, one of the first things about HIDAPI is being cross-platform.
I agree that this would be very useful, but Windows backend doesn't have an API to get a descriptor at all.
The descriptor could be partially reconstructed, but still, it is not an easy task.
I know one successfull attempt to reconstruct a HID descriptor using WinAPI calls, but that one is written on C# (https://github.com/todbot/win-hid-dump is using it). If someone could port it to C - I'd gladly introduce a new API (to get HID descriptor) for all platforms/backends.
#322 has been merged. So I apply the two patches from the macOS get-descriptor branch and here is the result. The way I apply the patch may not be so correct but the patching is successful.
Only one output is a bit strange.
Device Found
type: 046d c52b
path: DevSrvsID:4294969637
serial_number:
Manufacturer: Logitech
Product: USB Receiver
Release: 2410
Interface: 0
Usage (page): 0x6 (0x1)
Report Descriptor: not available.
Can I somehow help to get the get_descriptor branch merged?
Actually you can:
One of my biggest conserns is rd_item_byte. A classical linked list of a 1-byte items is one of the least efficient data structures possible.
Right now the byte_list is defined as: struct rd_item_byte *byte_list = NULL;.
A much better aproach would be to have something like:
in this case we still going to have convenient rd_append/write... functions, but no dynamic allocations per-byte.
My second consern is the implementation of hid_winapi_descriptor_reconstruct_pp_data.
As a human being I refuse to read/understand/check it line-by-line, and because it was written by enother human being it may contain accidental issues.
So I think we will have it mostly as is (except my consern above), but run it thru some static analyzers (we have setup project for coverity, and run unit-tests we have with ASAN enabled (maybe even have it enabled for our CI).
If no issues found (or after all issues fixed) - that will be the last item in my checklist for that branch.
I will look, how I can adapt the proposed rd_buffer approach!
To have static code checkers in CI, makes sense to me. I wonder, how it will handle the preparsed_data itself, where the memory allocation happens in WIN32 API code and only a pointer to the struct of variable size is returned. Everything else is under our control, and therefore, should be fixable.
CoreRelated to common codes like hidapi.hWindowsRelated to Windows backendenhancementNew feature or requesthidrawRelated to Linux/hidraw backendlibusbRelated to libusb backendmacOSRelated to macOS backend
Activity
Be-ing commentedon Feb 26, 2021
Potentially helpful links:
https://github.com/DIGImend/usbhid-dump
https://github.com/DIGImend/hidrd
Those are both licensed under the GPL, so they couldn't be used directly in hidapi unless the maintainers are willing to change the hidapi license. Nevertheless they could be useful examples to reference.
https://eleccelerator.com/tutorial-about-usb-hid-report-descriptors/
Youw commentedon Feb 28, 2021
Go check:
https://github.com/todbot/win-hid-dump
https://github.com/todbot/mac-hid-dump
tschiemer commentedon Mar 16, 2021
Actually, it somewhat comes for free as the basic usage page and usage are extracted from the report descriptor which is read here:
hidapi/libusb/hid.c
Line 644 in 0ea5e95
In my eyes it'd make sense to directly store this descriptor either
struct hid_device_info(simpler) orstruct hid_device_info(feels more elegant)That said, it might be nice or meaningful to somehow incorporate this quite nice descriptor to string parser based on which I'm momentarily trying to make a generic parser to help extract meaningful values from given reports without having to know the report layout beforehand (primarily trying to make it work with my use cases, but for simple devices should be fine).
Be-ing commentedon Mar 16, 2021
Thanks for that info @tschiemer. I have not looked into what it would take to implement this. If you have a branch of hidapi you are working on, please open a pull request for this upstream repository and I'd be happy to test it with my Native Instruments Traktor Kontrol S4 Mk3.
tschiemer commentedon Mar 16, 2021
Ok, can see to it, but might take a moment
Youw commentedon Mar 16, 2021
While this indeed already available and easy to get for libusb and hidraw backends, one of the first things about HIDAPI is being cross-platform.
I agree that this would be very useful, but Windows backend doesn't have an API to get a descriptor at all.
The descriptor could be partially reconstructed, but still, it is not an easy task.
I know one successfull attempt to reconstruct a HID descriptor using WinAPI calls, but that one is written on C# (https://github.com/todbot/win-hid-dump is using it). If someone could port it to C - I'd gladly introduce a new API (to get HID descriptor) for all platforms/backends.
JoergAtGithub commentedon Mar 16, 2021
There's also the approach to use the generic WinUSB driver (comes with Win7 or newer) instead of Windows High-Level-HID-API: #61 (comment)
As far as I understood it, the WinUSB driver gives a userspace Windows program full access to an USB device, but locks it exclusive.
Youw commentedon Mar 16, 2021
Yeah, and someone actually needs to make a driver for each specific device. And the problem not even with making a driver - a template inf file can be generated. But signing a driver properly - that requires some effort.
And you'd need to compile a libusb backend on Windows, which isn't compilable of-the-box yet.
JoergAtGithub commentedon Mar 16, 2021
Ok, that doesn't sounds like what we're looking for.
Be-ing commentedon Mar 17, 2021
I think it could be a good idea to start with libusb or hidraw to figure out what the API should be then implement it for other backends.
tschiemer commentedon Mar 22, 2021
@Be-ing so not quite a hidapi branch, but as a proof of concept I implemented this puredata external to parse input reports based on the report descriptor, works for me on macos and a rpi4/linux with a joystick and mouse (see releases for binaries).
It's a bit of a mess as it uses libusb to find devices according to search criteria (more than hidapi provides out of the box, but meaningful in my opinion and based on hidapi-libusb internals) and reads/parses the report descriptor but relies on hidapi to actually receive input reports (and thus on linux the device listing is set to detach devices as in the hidapi driver..) - should have had a deeper look at hidapi's (or hidraw) interface before..
At the moment only the raw values as given in the input report are extracted without making use of any of the logical/physical min/max, unit, exponential etc values but adding that shouldn't be too big of a deal..
Could not test this with any feature/output reports or more complicated report descriptors, as I have neither the devices nor much more capacity..
The hid report parser just creates a datastructure that's more easy to handle but I would guess it won't work for all descriptors (collection items are somewhat ignored; note that the handling of output and feature items likely will need some fixing in the parser..).
As external it "publishes" any changed values (usage page, usage, value), similar to the apple HIDIOManager value callbacks.
I guess construction of output reports can be achieved by inverting the logic of the value extractor.
Maybe this is helpful in some form
JoergAtGithub commentedon Apr 6, 2021
I implemented such a reconstructor in C and will start a Pull Request for HIDAPI soon. Stay tuned!
JoergAtGithub commentedon Apr 10, 2021
I started PR #262 with a C implementation of the Windows Report-Descriptor code.
mcuee commentedon Jun 13, 2021
I think libusb HID backend has already the codes to partially reconstruct the descriptors. Some from the USB HUB driver, some from the USB HID driver. But that may not be suitable for HIDAPI as it is also for bluetooth devices.
https://github.com/libusb/libusb/blob/master/libusb/os/windows_winusb.c#L3728
static int hid_open(int sub_api, struct libusb_device_handle *dev_handle)
113 remaining items
mcuee commentedon Aug 25, 2021
#322 has been merged. So I apply the two patches from the macOS get-descriptor branch and here is the result. The way I apply the patch may not be so correct but the patching is successful.
Only one output is a bit strange.
mcuee commentedon Aug 25, 2021
@Youw The issue goes away once I run with sudo. Not so sure why.
Youw commentedon Aug 25, 2021
Not sure either.
Does
IOHIDDeviceOpen(dev->device_handle, kIOHIDOptionsTypeNone);makes any difference?mcuee commentedon Aug 25, 2021
Indeed that makes the difference. With the change, no need to use sudo.
Youw commentedon Aug 25, 2021
That means that there is another application/daemon running in the background that has that device opened.
mcuee commentedon Mar 17, 2022
Just the latest test result from the get-descriptor branch under Windows.
Click for the run log
JoergAtGithub commentedon May 31, 2022
Can I somehow help to get the
get_descriptorbranch merged?Youw commentedon May 31, 2022
Get me fired from my current/main job :D
Youw commentedon Aug 15, 2022
Actually you can:
One of my biggest conserns is
rd_item_byte. A classical linked list of a 1-byte items is one of the least efficient data structures possible.Right now the
byte_listis defined as:struct rd_item_byte *byte_list = NULL;.A much better aproach would be to have something like:
in this case we still going to have convenient
rd_append/write...functions, but no dynamic allocations per-byte.My second consern is the implementation of
hid_winapi_descriptor_reconstruct_pp_data.As a human being I refuse to read/understand/check it line-by-line, and because it was written by enother human being it may contain accidental issues.
So I think we will have it mostly as is (except my consern above), but run it thru some static analyzers (we have setup project for coverity, and run unit-tests we have with ASAN enabled (maybe even have it enabled for our CI).
If no issues found (or after all issues fixed) - that will be the last item in my checklist for that branch.
JoergAtGithub commentedon Aug 15, 2022
I will look, how I can adapt the proposed rd_buffer approach!
To have static code checkers in CI, makes sense to me. I wonder, how it will handle the preparsed_data itself, where the memory allocation happens in WIN32 API code and only a pointer to the struct of variable size is returned. Everything else is under our control, and therefore, should be fixable.
mcuee commentedon Jan 19, 2023
Just another test results.
run log for hidtest
mcuee commentedon Feb 22, 2023
One more test results with latest commit.
run log for hidtest
mcuee commentedon Feb 22, 2023
Comparison with libusbK kList utility for the Mi Wireless Mouse USB Receiver, by attaching libusb0.sys filter to the device.
mcuee commentedon Mar 12, 2023
Great that this PR has finally been merged.