Skip to content

Handle HID OUT reports with no report ID #7806

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

Merged
merged 3 commits into from
Mar 29, 2023

Conversation

dhalbert
Copy link
Collaborator

Raw HID OUT reports (to the peripheral from the host), with no report ID, were not being parsed correctly. The code assumed the first byte was the report id. However, this is not always the case.

Instead, see if there is a defined HID device with report ID 0, whose report length matches the incoming report. If so, assume the incoming report is for that device.

The long-term fix is for TinyUSB to know which reports are for which report ID's, but that requires hathach/tinyusb#1095 to be addressed.

Tested with hidapitester on Linux:

sudo /home/halbert/bin/hidapitester --vidpid 239a/80f2 --open --length 64 --send-output 7,8,9
Opening device, vid/pid: 0x239A/0x80F2
Writing output report of 64-bytes...wrote 64 bytes:
 07 08 09 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Closing device

On the CPy side:

>>> import rawread
None
None
None
b'\x07\x08\t\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
None

test program (rawread.py):

import usb_hid
import time

d = usb_hid.devices[0]

while True:
    print(d.get_last_received_report())
    time.sleep(2)

boot.py:

import usb_hid

RAWHID_REPORT_DESCRIPTOR = bytes((
    0x06, 0x00, 0xFF,  # Usage Page (Vendor Defined 0xFF00)
    0x09, 0x01,        # Usage (0x01)
    0xA1, 0x01,        # Collection (Application)
    0x09, 0x02,        #   Usage (0x02)
    0x15, 0x00,        #   Logical Minimum (0)
    0x26, 0xFF, 0x00,  #   Logical Maximum (255)
    0x75, 0x08,        #   Report Size (8)
    0x95, 0x40,        #   Report Count (64)
    0x81, 0x02,        #   Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
    0x09, 0x03,        #   Usage (0x03)
    0x15, 0x00,        #   Logical Minimum (0)
    0x26, 0xFF, 0x00,  #   Logical Maximum (255)
    0x75, 0x08,        #   Report Size (8)
    0x95, 0x40,        #   Report Count (64)
    0x91, 0x02,        #   Output (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile)
    0xC0,              # End Collection
))

raw_hid = usb_hid.Device(
    report_descriptor=RAWHID_REPORT_DESCRIPTOR,
    usage_page=0xFF00,
    usage=0x01,
    report_ids=(0,),
    in_report_lengths=(64,),
    out_report_lengths=(64,),
)

usb_hid.enable((raw_hid,))
#usb_hid.enable((usb_hid.Device.KEYBOARD,))

@dhalbert
Copy link
Collaborator Author

I also tested existing support for OUT reports, using Keyboard, and sending Keycode.CAPS_LOCK, and I see the Keyboard.led_status still changing as it should.

tannewt
tannewt previously approved these changes Mar 28, 2023
Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me! Thank you!

@dhalbert dhalbert requested a review from tannewt March 28, 2023 20:49
@dhalbert
Copy link
Collaborator Author

@tannewt I managed to get the builds to fit by casting some more optimizer spells.

Copy link

@jepler jepler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@dhalbert dhalbert merged commit 5909cea into adafruit:main Mar 29, 2023
@dhalbert dhalbert deleted the no-report-id-out-reports branch March 29, 2023 19:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants