hid_read / hid_read_timeout reads more bytes than the report has #210
Labels
No labels
3rdparty
Android
API
Author does not respond
bsd
bug
build system/CI
ci-virtual-device
CMake
Contributions Welcome
Core
dependencies
device-specific
documentation
don't_merge
duplicate
enhancement
Example
Haiku
hidraw
hotplug
info required
invalid
libusb
macOS
Misc
Not an issue
question
Release
Test
Test GUI
Windows
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
gh-libusb/hidapi#210
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
My device has two input reports:
0x01 with a size of 10Bytes
0x02 with a size of 53Bytes
If I use hid_read / hid_read_timeout I get always 53Bytes, also for report 0x01.
But the function hid_get_input_report always returns the correct number of bytes.
This occurs on Windows 7.
This may be a Windows backend limitation.
Not so sure if this is relevant.
https://marc.info/?l=libusb-devel&m=131805687305661&w=2
List: libusb-devel
Subject: Re: [Libusb-devel] How to obtain the output report size?
From: Alan Ott <alan () signal11 ! us>
Date: 2011-10-04 2:30:19
Message-ID: 4E8A6FBB.2080304 () signal11 ! us
[Download RAW message or body]
On 10/03/2011 10:19 PM, Peter Stuge wrote:
Hi Jeremy,
While I concur that HIDAPI is good (I'm the maintainer :) ), there's not
a good way get the report length from HIDAPI either.
The thing is, the report length in that windows record you mentioned is
the length of the longest report of that type that the device
supports, and you must send exactly that many bytes (plus one for the
report number) when you send a report to the device, regardless of
whether or not you're sending a report which is shorter. For example, if
a device has 2 output reports, one 16 bytes and one 4 bytes, you must
send 17 bytes for both. I usually pad with zeroes.
So all that to say there's really no good way to get the data, even on
Windows (unless you only have one report of each type). A userspace
driver of this sort should know what kind of device it's communicating
with anyway. If your code knows the protocol, it should know the length
of the packets, and thus you shouldn't need the library to tell you. I
do understand however, that we sometimes write code for devices which we
don't have any control over and are designed badly (stuff like having
the same VID/PID for devices with different protocols, etc). If this is
the case, maybe we can help you find a different way to differentiate
between similar devices.
Make sure to grab the latest HIDAPI code from git.
Alan.
Currently I see two options:
1.) Execute a dummy read of each input report at device intialization using hid_get_input_report which returns the correct report size and store the report size in an array. This would be a safe implementation, but it would slow down device initialization.
2.) Determine the report size from the report descriptor. This would be the more elegant solution, but would require that the report descriptor reconstructor, which I implemented in #262 would always detemine the correct size. But currently there is one edge case, where this is not the case (a multiple button bit field have the Usage 0).
Just want to post some historical info about HIDAPI why the report desscriptor parser is not
included.
https://sourceforge.net/p/libusb/mailman/message/26230365/
Re: [Libusb-devel] hidapi and HID Report Descriptor parsing
From: Alan Ott <alan@si...> - 2010-09-27 12:04:13
On 09/27/2010 03:50 AM, Ludovic Rousseau wrote:
One argument that is often made is that if you're driving a HID device
directly, then you already know the format of the data, including the
size of each report. It's not strictly necessary to query the device.
Even if you did, what do you do if the device says that its report sizes
are different than you expect?
Even in the Linux kernel, fixed sizes are used for outputting data to
USB devices (sizeof(), hard-coded, etc). I don't think it's a bad thing.
I'm sure many on this mailing list will agree (or will explain why I am
wrong).
I do not. I was able to do some parsing of a report descriptor myself
without too much problem. The code is in HIDAPI in linux/hid.c in a
function called uses_numbered_reports(). My basic parser looks for
Report ID (0x85). I'm just checking for the presence of it. You could
probably modify that to look for Report Size and Report Count fields
ahead of the Input field.
Alan.
Right now my test device has only one input report so I can not test this. Unfortunately my old build environment is gone (Microchip MPLAB 8.x and C18 2.4 or later) so I can not change the FW to test this out. I will see if I can find some other test device.
I believe this is the solution to go.
Reference: answer from Tim Roberts.
On Tue, Jun 15, 2021 at 2:29 AM Tim Roberts wrote:
This is an interesting and quick solution. But I kind of like the other more elegant solution.
This won't work for interrupt-only reports that cannot be read on demand.
@Youw
Now that #249 has been closed after the merge of #451.
Just wondering if we can do something for this issue now.
To be honest I wouldn't want to introduce any additional parsing/interpretation into HIDAPI.
It can be done on top of HIDAPI and I'd rather try to keep the implementation as simple as possible.
In that case, shall we close this issue?
I guess so.