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
HIDAPI testing device with known good firmware, including using Virtual HID device for CI Testing #478
The alternative: uhid.
In one of the issues there was a sample. The sample implementation actually has several issues/bugs, but the overall aproach is fine.
changed the title [-]Consider using umockdev hidraw device for testing under Linux[/-][+]Consider using umockdev or uhid hidraw device for testing under Linux[/+]on May 11, 2023
changed the title [-]Consider using umockdev or uhid hidraw device for testing under Linux[/-][+]Using Virtual HID device for CI Testing -- using umockdev or uhid hidraw or others under Linux[/+]on May 14, 2023
BTW, other than CI, it is also good to see if we have some known good HID device or FW to test HIDAPI.
For libusb, here is the reference, it can be the same for USB HID device development. But for Bluetooth/BLE and even I2C/SPI HID device, we need different tools.
Arm Cortex M MCU based development board like ST STM32 (eg: Bluepill and Blackpill, Discovery or Nucleo boards), Microchip SAM MCU, Raspberry Pi Pico, etc
Other USB MCU like Microhip PIC or AVR, TI MSP 430, etc
Linux USB Gadget devices (eg: using Raspberry Pi Zero or Raspberry Pi 4B/400)
libusb-1.0.26 has the umockdev based testing under Linux
Other than vendor provided USB stacks (sometimes with proprietary licenses), there are also Opensource MCU USB stacks.
Just wondering what is the test device you are using to test HIDAPI?
For me I usually use whatever HID device I have with me like the following for general test using hidtest.
Logitech USB Receiver
Plantronics USB Headset
My laptop has built-in I2C HID device (Synaptics Touch Pad)
USB Mouse/Keyboard
Bluetooth Mouse (Microsoft 3600 and maybe others)
Logitech Dual Action USB Gamepad
Wireless Xbox Gamepad (old version)
ESP32S based BLE HID example
For more real tests with hidapitester or simple test codes. I do not know much about the FW development other than simple modifications of the USB PIC USB codes and maybe a little bit with USB AVRs.
Microchip PIC18F87J50 with custom HID FW (8 bit USB PIC18 MCU). I have USB PIC24 and PIC32 board as well but seldom use them.
Cypress EZ-USB FX2LP breadout board
Arduino Leonardo and Pro Micro based HID example (8 bit USB AVR MCU)
Arduino M0 clone (Atmel SAMD21)
Teensy 2.0 and 2.0+ based HID example (8 bit USB AVR MCU)
Various STM32 MCU demo boards
Raspberry Pi Pico using C++ or MicroPython or CircuitPython
ESP32C3/S2/S3 based boards using C or MicroPython or CircuitPython
BBC Micro:bit v2 (Nordic nRF52833) using C or MicroPython or CircuitPython
Then I use some HID devices to test hidapi indirectly using avrdude and OpenOCD (CMSIS DAP v1 compliant adapters).
Unfortunately I don't have any devices dedicated for testing, only a few of my peripherals, and a few devices from my employer (I'm under NDA not to disclose what those are), which have a few fixed input/output/interrupt reports, that I usually use for some sanity testing.
changed the title [-]Using Virtual HID device for CI Testing -- using umockdev or uhid hidraw or others under Linux[/-][+]HIDAPI testing device with known good firmware, including using Virtual HID device for CI Testing[/+]on May 14, 2023
TeensyRawHID -- can be used by different Teensy boards (USB AVR and Arm Cortex M MCUs)
two tinyusb examples which can be used by many ARM core MCU development boards including STM32 and Raspberry Pi Pico, as well as non-ARM core MCUs like ESP32 S2/S3 or MSP430
I have done the modification of the generic HID FW to add report ID (Input 1, Output 2, Feature OUT 3, Feature IN 4). I used that to test libusb Windows HID backend many year ago during the initial stage of the libusb Windows backend development.
Simple test using hidapitester for the Microchip Simple HID demo (which is the base for the FW used in hidtest). I am using the mla version for the 8bit/16bit USB PICs.
I think for HIDAPI testing, maybe FX3 is a bit overkill but you may want to get cheap EZ-USB FX2LP breakout from online marketplace like AliExpress at about US$5. For example, I have the following board.
I just did a simple modification of Jan Axelson's fx2hid example to extend the Input/Output report size to 64 bytes.
Edit: take note that in order to develop high speed HID USB device using FX2LP with wMaxPacketSize > 64 Bytes, we can not use EP1IN or EP1OUT, as their buf size is only 64 bytes. We should probably use EP2 and EP6 (buffer size equals to 512 Bytes or 1024 Bytes). So the modification is not as simple. But if we just need to get report size >64 bytes, then it is still possible to use EP1 IN and EP1 OUT. We need to handle the fragamentation in firmware.
I have had problems with 64-byte reports with report IDs. Not sure if this is a MCU buffer limitation (which is 64 bytes usually), an issue with hidapi, or something else.
Here is a modified set of above code that does work, up to 63 byte reports:
# rawhid_code.pyimporttimeimportusb_hidimportadafruit_hidcustom_device=adafruit_hid.find_device(usb_hid.devices, usage_page=0xff00, usage=0x01)
print("custom_device: %04x %04x"% (custom_device.usage_page, custom_device.usage) )
whileTrue:
out_report=custom_device.get_last_received_report(2) # out from computerifout_report:
print("len:",len(out_report),["%02x"%xforxinout_report])
time.sleep(0.5)
print("sending copy on reportid 1")
in_report=bytearray(out_report) # copy in case we want to modifycustom_device.send_report(in_report, 1); # in to computer
I have had problems with 64-byte reports with report IDs. Not sure if this is a MCU buffer limitation (which is 64 bytes usually), an issue with hidapi, or something else.
Interesting and this is exactly the same problem I have with my USB PIC using Jan Axelson's generic HID example as well. 63 bytes works but not 64 bytes. Firmware_generic_hid4_63B.zip
I think there are a few potential reasons.
MCU FW problem, one possible cause is ZLP related issues.
hidapitester issue (see below)
hidapi issue
combination of the above, espeically 1) and 2).
Note that hidapitester is given -l 64 to make a report that is REPORT_COUNT+1 long.
This is one potential issue of hidapitester I would like to check with you and @Youw. I am thinking this should be worked-around inside hidapitest for Windows (report ID 0 needs to be in the buffer but not really appear in the USB BUS. That is probably why we need one more bytes of bufer than the length of report size. I believe this is not necessary for Linux and Windows. I could be wrong though.
Note that hidapitester is given -l 64 to make a report that is REPORT_COUNT+1 long.
This is one potential issue of hidapitester I would like to check with you and @Youw.
This reflects how hidapi works, where the first byte of the send buffer is the reportId if it exists, which I believe came from how libusb works. It's always bugged me and has led to so much confusion, but I'm not sure how we could get around it.
This reflects how hidapi works, where the first byte of the send buffer is the reportId if it exists, which I believe came from how libusb works. It's always bugged me and has led to so much confusion, but I'm not sure how we could get around it.
But from a cross-platform API point of view, we may need to revist to align with Linux and macOS, which means we need to focus on the data on the USB bus and we need to drop the extra 0 in front when the report ID is 0 for Windows. 6fcb0bb
Same for Output report. Right now we may report one more byte than real data on the bus due to Windows driver behavior, when the report ID is 0.
All in all, we may need to align Windows HIDAPI behavior along with Linux and macOS. Need to check with the libusb backend as well.
There are many confusions under libusb Windows HID backend as well. I think there are more problems with libusb Windows HID backend (libusb project recommends people to use HIDAPI and not libusb, so it is a legacy thingy).
Unfortunately I've never had good test device(s) to check all: input/output/feature/interrupt, with and w/o report ID and with different lengths.
All being said, I think we need to focus to make the behavior (e.g. in terms of the return length, etc.) be consistent on all platforms, specifically to match the documentation.
Please take a look when you got the time. Thanks.
I'm a little lost in the context and worst of all - cannot properly check it locally...
All being said, I think we need to focus to make the behavior (e.g. in terms of the return length, etc.) be consistent on all platforms, specifically to match the documentation.
Yes I agree.
Please take a look when you got the time. Thanks.
I'm a little lost in the context and worst of all - cannot properly check it locally...
I understand. I will try to summarize and create another issue. And hopefully you can get one or two of the test devices without too much difficulties.
Activity
mcuee commentedon Oct 31, 2022
Ref: libusb project uses umockdev for Linux testing using a container.
https://github.com/libusb/libusb/blob/master/tests/umockdev.c
https://github.com/libusb/libusb/blob/master/.github/workflows/linux.yml
Youw commentedon Mar 12, 2023
The alternative:
uhid.In one of the issues there was a sample. The sample implementation actually has several issues/bugs, but the overall aproach is fine.
[-]Consider using umockdev hidraw device for testing under Linux[/-][+]Consider using umockdev or uhid hidraw device for testing under Linux[/+][-]Consider using umockdev or uhid hidraw device for testing under Linux[/-][+]Using Virtual HID device for CI Testing -- using umockdev or uhid hidraw or others under Linux[/+]mcuee commentedon May 14, 2023
I am not so sure if this is possible or not.
https://github.com/vpelletier/python-functionfs
mcuee commentedon May 14, 2023
BTW, other than CI, it is also good to see if we have some known good HID device or FW to test HIDAPI.
For libusb, here is the reference, it can be the same for USB HID device development. But for Bluetooth/BLE and even I2C/SPI HID device, we need different tools.
https://github.com/libusb/libusb/wiki/FAQ#do-you-have-a-list-of-known-good-devices-to-test-libusb
Other than vendor provided USB stacks (sometimes with proprietary licenses), there are also Opensource MCU USB stacks.
Just wondering what is the test device you are using to test HIDAPI?
For me I usually use whatever HID device I have with me like the following for general test using hidtest.
For more real tests with hidapitester or simple test codes. I do not know much about the FW development other than simple modifications of the USB PIC USB codes and maybe a little bit with USB AVRs.
Then I use some HID devices to test hidapi indirectly using avrdude and OpenOCD (CMSIS DAP v1 compliant adapters).
Youw commentedon May 14, 2023
Unfortunately I don't have any devices dedicated for testing, only a few of my peripherals, and a few devices from my employer (I'm under NDA not to disclose what those are), which have a few fixed input/output/interrupt reports, that I usually use for some sanity testing.
Youw commentedon May 14, 2023
Yeah, would be great to have 1-2 FW samples for some well known simple/cheap device to be able to test most (all?) of the HIDAPI.
[-]Using Virtual HID device for CI Testing -- using umockdev or uhid hidraw or others under Linux[/-][+]HIDAPI testing device with known good firmware, including using Virtual HID device for CI Testing[/+]mcuee commentedon May 14, 2023
@todbot has some test firmware for hidapitester which can be used for hidapi testing as well.
https://github.com/todbot/hidapitester/tree/master/test_hardware
TinyUSB
https://github.com/hathach/tinyusb
node-hid has some examples as well.
https://github.com/node-hid/node-hid#examples
mcuee commentedon May 14, 2023
Jan Axelson has quite some HID examples for things like Cypress EZ-USB FX2LP and Microchip USB PIC.
http://janaxelson.com/hidpage.htm
mcuee commentedon May 14, 2023
Simple test for Jan Axelson FX2HID example (0925:1234) -- loop back of Input/Output report (no report ID) using hidapitest.
For this FW, I have to explicitly send output report ID 0.
Her USB PIC generic HID example has feature report loopback as well.
I have done the modification of the generic HID FW to add report ID (Input 1, Output 2, Feature OUT 3, Feature IN 4). I used that to test libusb Windows HID backend many year ago during the initial stage of the libusb Windows backend development.
mcuee commentedon May 14, 2023
Simple test using hidapitester for the Microchip Simple HID demo (which is the base for the FW used in hidtest). I am using the mla version for the 8bit/16bit USB PICs.
mcuee commentedon May 14, 2023
@Youw
For libusb testing, I usually recommend Cypress EZ-USB FX3 (SuperSpeed). Then EZ-USB FX2LP (High-Speed).
https://github.com/libusb/libusb/wiki/FAQ#do-you-have-a-list-of-known-good-devices-to-test-libusb
I think for HIDAPI testing, maybe FX3 is a bit overkill but you may want to get cheap EZ-USB FX2LP breakout from online marketplace like AliExpress at about US$5. For example, I have the following board.
I just did a simple modification of Jan Axelson's fx2hid example to extend the Input/Output report size to 64 bytes.
fx2hid_64bytes.zip
Target.zip
Edit: take note that in order to develop high speed HID USB device using FX2LP with wMaxPacketSize > 64 Bytes, we can not use EP1IN or EP1OUT, as their buf size is only 64 bytes. We should probably use EP2 and EP6 (buffer size equals to 512 Bytes or 1024 Bytes). So the modification is not as simple. But if we just need to get report size >64 bytes, then it is still possible to use EP1 IN and EP1 OUT. We need to handle the fragamentation in firmware.
17 remaining items
mcuee commentedon Jun 11, 2023
@todbot
Yes you are right. The following works.
Relevant discussion here.
todbot commentedon Jun 14, 2023
I have had problems with 64-byte reports with report IDs. Not sure if this is a MCU buffer limitation (which is 64 bytes usually), an issue with hidapi, or something else.
Here is a modified set of above code that does work, up to 63 byte reports:
And running hidapitester:
Note that hidapitester is given
-l 64to make a report that is REPORT_COUNT+1 long.When run, the CircuitPython device outputs this on its REPL:
mcuee commentedon Jun 14, 2023
@todbot
Thanks a lot for the help.
Interesting and this is exactly the same problem I have with my USB PIC using Jan Axelson's generic HID example as well. 63 bytes works but not 64 bytes.
Firmware_generic_hid4_63B.zip
I think there are a few potential reasons.
This is one potential issue of hidapitester I would like to check with you and @Youw. I am thinking this should be worked-around inside hidapitest for Windows (report ID 0 needs to be in the buffer but not really appear in the USB BUS. That is probably why we need one more bytes of bufer than the length of report size. I believe this is not necessary for Linux and Windows. I could be wrong though.
todbot commentedon Jun 15, 2023
This reflects how
hidapiworks, where the first byte of the send buffer is the reportId if it exists, which I believe came from how libusb works. It's always bugged me and has led to so much confusion, but I'm not sure how we could get around it.mcuee commentedon Jun 15, 2023
@Youw
Please take a look when you got the time. Thanks.
mcuee commentedon Jun 15, 2023
@Youw and @todbot and @JoergAtGithub
I think we have gone through this report length previously. Maybe we have to revisit them.
I think the following PR fixes the issue for reading back the input report and it is correct.
I think we need to revisit for Feature report. I think the following commit is kind of correct if we look at from Windows driver point of view.
https://sourceforge.net/p/libusb/mailman/message/37303650/
But from a cross-platform API point of view, we may need to revist to align with Linux and macOS, which means we need to focus on the data on the USB bus and we need to drop the extra 0 in front when the report ID is 0 for Windows.
6fcb0bb
Same for Output report. Right now we may report one more byte than real data on the bus due to Windows driver behavior, when the report ID is 0.
All in all, we may need to align Windows HIDAPI behavior along with Linux and macOS. Need to check with the libusb backend as well.
Another related old PR for macOS.
mcuee commentedon Jun 15, 2023
There are many confusions under libusb Windows HID backend as well. I think there are more problems with libusb Windows HID backend (libusb project recommends people to use HIDAPI and not libusb, so it is a legacy thingy).
There is another interesting issue there as well (hopefully not affecting HIDAPI).
mcuee commentedon Jun 15, 2023
@tormodvolden
FYI as well. I believe libusb/libusb#1217 is incorrect.
Youw commentedon Jun 15, 2023
Unfortunately I've never had good test device(s) to check all: input/output/feature/interrupt, with and w/o report ID and with different lengths.
All being said, I think we need to focus to make the behavior (e.g. in terms of the return length, etc.) be consistent on all platforms, specifically to match the documentation.
I'm a little lost in the context and worst of all - cannot properly check it locally...
mcuee commentedon Jun 16, 2023
I will recommend the following devices which will work with the examples here. They are of relative low cost from online stores like AliExpress.
https://github.com/todbot/hidapitester/tree/master/test_hardware
Yes I agree.
I understand. I will try to summarize and create another issue. And hopefully you can get one or two of the test devices without too much difficulties.
Youw commentedon Jun 16, 2023
Hm, I actually have an Arduino somewhere. Haven't used it for years. Maybe I could use it.
Now it is only the question if finding fime for it.
mcuee commentedon Jun 16, 2023
No problem, take your time.
The Arduino Uno and Maga 2560 (official version and some clones) come with ATmega16U2. It is a bit tricky to get the rawhid example working.
https://github.com/NicoHood/HID/wiki/RawHID-API
https://github.com/NicoHood/HoodLoader2
Arduino Leonardo or Arduino Micro or Pro Micro or clones come with ATmega32U4 and then you should have no issues to get rawhid example working.
https://github.com/todbot/hidapitester/tree/master/test_hardware/ProMicroRawHID
mcuee commentedon Jun 16, 2023
I am going to move this to discussion and create a new issue to make this one clean. Sorry I have put too much info here and people may get confused.