diff --git a/tests/test/host/hid/test_hidh_keyboard.c b/tests/test/host/hid/test_hidh_keyboard.c index b437c9cab..12f3d7e4b 100644 --- a/tests/test/host/hid/test_hidh_keyboard.c +++ b/tests/test/host/hid/test_hidh_keyboard.c @@ -119,9 +119,11 @@ void test_keyboard_open_ok(void) TEST_ASSERT_EQUAL(TUSB_ERROR_NONE, hidh_keyboard_open_subtask(dev_addr, (uint8_t*) p_kbd_interface_desc, &length)); tusbh_device_get_state_IgnoreAndReturn(TUSB_DEVICE_STATE_CONFIGURED); - TEST_ASSERT_EQUAL(2, tusbh_hid_keyboard_no_instances(dev_addr)); // init instance to 1 + TEST_ASSERT_EQUAL(2, tusbh_hid_keyboard_no_instances(dev_addr)); // init set instance number to 1 TEST_ASSERT_PIPE_HANDLE(pipe_hdl, p_hidh_kbd_interface->pipe_in); TEST_ASSERT_EQUAL(8, p_hidh_kbd_interface->report_size); + TEST_ASSERT_EQUAL(sizeof(tusb_descriptor_interface_t) + sizeof(tusb_hid_descriptor_hid_t) + sizeof(tusb_descriptor_endpoint_t), + length); } void test_keyboard_init(void) diff --git a/tinyusb/class/hid_host_keyboard.c b/tinyusb/class/hid_host_keyboard.c index 2647f9bca..43031d6eb 100644 --- a/tinyusb/class/hid_host_keyboard.c +++ b/tinyusb/class/hid_host_keyboard.c @@ -101,17 +101,24 @@ void hidh_keyboard_init(void) tusb_error_t hidh_keyboard_open_subtask(uint8_t dev_addr, uint8_t const *descriptor, uint16_t *p_length) { - descriptor += *descriptor; // skip interface - descriptor += *descriptor; // TODO skip HID, only support std keyboard + uint8_t *p_desc = descriptor; + + p_desc += p_desc[DESCRIPTOR_OFFSET_LENGTH]; // skip interface + p_desc += p_desc[DESCRIPTOR_OFFSET_LENGTH]; // TODO skip HID, only support std keyboard + + // set ASAP, in case of error, p_length has to be not zero to prevent infinite re-open + (*p_length) = p_desc - descriptor; keyboard_interface_t *p_kbd = get_kbd_instance(dev_addr, keyboard_data[dev_addr-1].instance_count); - ASSERT_INT(TUSB_DESC_ENDPOINT, descriptor[DESCRIPTOR_OFFSET_TYPE], TUSB_ERROR_INVALID_PARA); + ASSERT_INT(TUSB_DESC_ENDPOINT, p_desc[DESCRIPTOR_OFFSET_TYPE], TUSB_ERROR_INVALID_PARA); - p_kbd->pipe_in = hcd_pipe_open(dev_addr, (tusb_descriptor_endpoint_t*) descriptor, TUSB_CLASS_HID); + p_kbd->pipe_in = hcd_pipe_open(dev_addr, (tusb_descriptor_endpoint_t*) p_desc, TUSB_CLASS_HID); + + p_desc += p_desc[DESCRIPTOR_OFFSET_LENGTH]; // advance endpoint descriptor + (*p_length) = p_desc - descriptor; ASSERT (pipehandle_is_valid(p_kbd->pipe_in), TUSB_ERROR_HCD_FAILED); - keyboard_data[dev_addr-1].instance_count++; return TUSB_ERROR_NONE;