Skip to content

Commit 9232ec5

Browse files
committed
Fix HID buffer lookup
The previous code assumed HID report ids were consecutive. This is not true in the CircuitPython descriptor where report ids are fixed for each report type. Fixes #1617
1 parent 94822ee commit 9232ec5

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

shared-module/usb_hid/Device.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,27 +60,33 @@ void common_hal_usb_hid_device_send_report(usb_hid_device_obj_t *self, uint8_t*
6060
}
6161
}
6262

63+
static usb_hid_device_obj_t* get_hid_device(uint8_t report_id) {
64+
for (uint8_t i = 0; i < USB_HID_NUM_DEVICES; i++) {
65+
if (usb_hid_devices[i].report_id == report_id) {
66+
return &usb_hid_devices[i];
67+
}
68+
}
69+
return NULL;
70+
}
71+
6372
// Callbacks invoked when receive Get_Report request through control endpoint
6473
uint16_t tud_hid_generic_get_report_cb(uint8_t report_id, hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen) {
6574
// only support Input Report
6675
if ( report_type != HID_REPORT_TYPE_INPUT ) return 0;
6776

68-
// index is ID-1
69-
uint8_t idx = ( report_id ? (report_id-1) : 0 );
70-
7177
// fill buffer with current report
72-
memcpy(buffer, usb_hid_devices[idx].report_buffer, reqlen);
78+
memcpy(buffer, get_hid_device(report_id)->report_buffer, reqlen);
7379
return reqlen;
7480
}
7581

7682
// Callbacks invoked when receive Set_Report request through control endpoint
7783
void tud_hid_generic_set_report_cb(uint8_t report_id, hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize) {
78-
// index is ID-1
79-
uint8_t idx = ( report_id ? (report_id-1) : 0 );
84+
usb_hid_device_obj_t* hid_device = get_hid_device(report_id);
8085

8186
if ( report_type == HID_REPORT_TYPE_OUTPUT ) {
8287
// Check if it is Keyboard device
83-
if ( (usb_hid_devices[idx].usage_page == HID_USAGE_PAGE_DESKTOP) && (usb_hid_devices[idx].usage == HID_USAGE_DESKTOP_KEYBOARD) ) {
88+
if (hid_device->usage_page == HID_USAGE_PAGE_DESKTOP &&
89+
hid_device->usage == HID_USAGE_DESKTOP_KEYBOARD) {
8490
// This is LED indicator (CapsLock, NumLock)
8591
// TODO Light up some LED here
8692
}

0 commit comments

Comments
 (0)