summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhathach <[email protected]>2013-03-25 20:57:27 +0700
committerhathach <[email protected]>2013-03-25 20:57:27 +0700
commita2031e6218c783c5bdd4cc37ede4f32ad19d979d (patch)
tree6dc7720679e59e9bb83acafbd109005e7a451a9f
parentf5e7644a1e05f92149ce4fe3e16ed64120fb46bf (diff)
refractor hidh_keyboard_open
-rw-r--r--tests/test/host/hid/test_hidh_keyboard.c25
-rw-r--r--tinyusb/class/hid_host_keyboard.c24
-rw-r--r--tinyusb/class/hid_host_keyboard.h1
3 files changed, 34 insertions, 16 deletions
diff --git a/tests/test/host/hid/test_hidh_keyboard.c b/tests/test/host/hid/test_hidh_keyboard.c
index 12f3d7e4b..3f915919a 100644
--- a/tests/test/host/hid/test_hidh_keyboard.c
+++ b/tests/test/host/hid/test_hidh_keyboard.c
@@ -108,30 +108,37 @@ void test_keyboard_no_instances_invalid_para(void)
TEST_ASSERT_EQUAL(0, tusbh_hid_keyboard_no_instances(dev_addr));
}
+void test_keyboard_init(void)
+{
+ hidh_keyboard_init();
+
+ for(uint32_t i=0; i < sizeof(hidh_keyboard_info_t)*TUSB_CFG_HOST_DEVICE_MAX; i++)
+ TEST_ASSERT_EQUAL(0, ((uint8_t*)keyboard_data) [i]);
+}
+
void test_keyboard_open_ok(void)
{
uint16_t length=0;
- pipe_handle_t pipe_hdl = {.dev_addr = dev_addr, .xfer_type = TUSB_XFER_INTERRUPT, .index = 1};
+ pipe_handle_t pipe_hdl = {.dev_addr = dev_addr, .xfer_type = TUSB_XFER_INTERRUPT, .index = 2};
+ keyboard_interface_t* p_new_instance = &keyboard_data[dev_addr-1].instance[instance_num+1];
hcd_pipe_open_ExpectAndReturn(dev_addr, p_kdb_endpoint_desc, TUSB_CLASS_HID, pipe_hdl);
//------------- Code Under TEST -------------//
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 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_PIPE_HANDLE(pipe_hdl, p_new_instance->pipe_in);
+ TEST_ASSERT_EQUAL(8, p_new_instance->report_size);
TEST_ASSERT_EQUAL(sizeof(tusb_descriptor_interface_t) + sizeof(tusb_hid_descriptor_hid_t) + sizeof(tusb_descriptor_endpoint_t),
length);
+
+ tusbh_device_get_state_IgnoreAndReturn(TUSB_DEVICE_STATE_CONFIGURED);
+ TEST_ASSERT_EQUAL(2, tusbh_hid_keyboard_no_instances(dev_addr)); // init set instance number to 1
}
-void test_keyboard_init(void)
+void test_keyboard_close(void)
{
- hidh_keyboard_init();
- for(uint32_t i=0; i < sizeof(hidh_keyboard_info_t)*TUSB_CFG_HOST_DEVICE_MAX; i++)
- TEST_ASSERT_EQUAL(0, ((uint8_t*)keyboard_data) [i]);
}
//--------------------------------------------------------------------+
diff --git a/tinyusb/class/hid_host_keyboard.c b/tinyusb/class/hid_host_keyboard.c
index f37ca3507..984a427d4 100644
--- a/tinyusb/class/hid_host_keyboard.c
+++ b/tinyusb/class/hid_host_keyboard.c
@@ -101,27 +101,39 @@ void hidh_keyboard_init(void)
tusb_error_t hidh_keyboard_open_subtask(uint8_t dev_addr, uint8_t const *descriptor, uint16_t *p_length)
{
+ keyboard_interface_t *p_keyboard = get_kbd_instance(dev_addr, keyboard_data[dev_addr-1].instance_count);
uint8_t const *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
+ (*p_length) = p_desc - descriptor; // set ASAP, in case of error, p_length has to be not zero to prevent infinite re-open
- // set ASAP, in case of error, p_length has to be not zero to prevent infinite re-open
- (*p_length) = p_desc - descriptor;
+ //------------- HID descriptor -------------//
+ tusb_hid_descriptor_hid_t* const p_desc_hid = (tusb_hid_descriptor_hid_t* const) p_desc;
+ ASSERT_INT(HID_DESC_HID, p_desc_hid->bDescriptorType, TUSB_ERROR_INVALID_PARA);
- keyboard_interface_t *p_kbd = get_kbd_instance(dev_addr, keyboard_data[dev_addr-1].instance_count);
+ p_desc += p_desc[DESCRIPTOR_OFFSET_LENGTH]; // TODO skip HID, only support std keyboard
+ (*p_length) = p_desc - descriptor;
+ //------------- Endpoint Descriptor -------------//
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*) p_desc, TUSB_CLASS_HID);
+ p_keyboard->pipe_in = hcd_pipe_open(dev_addr, (tusb_descriptor_endpoint_t*) p_desc, TUSB_CLASS_HID);
+
+ p_keyboard->report_size = ( ((tusb_descriptor_endpoint_t*) p_desc)->wMaxPacketSize & (BIT_(12)-1) );
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);
+ ASSERT (pipehandle_is_valid(p_keyboard->pipe_in), TUSB_ERROR_HCD_FAILED);
+
keyboard_data[dev_addr-1].instance_count++;
return TUSB_ERROR_NONE;
}
+void hidh_keyboard_close(uint8_t dev_addr)
+{
+
+}
+
#endif
diff --git a/tinyusb/class/hid_host_keyboard.h b/tinyusb/class/hid_host_keyboard.h
index f07595dcf..4c5b19ac9 100644
--- a/tinyusb/class/hid_host_keyboard.h
+++ b/tinyusb/class/hid_host_keyboard.h
@@ -88,7 +88,6 @@ typedef struct {
} hidh_keyboard_info_t;
void hidh_keyboard_init(void);
-tusb_error_t hidh_keyboard_install(uint8_t dev_addr, uint8_t const *descriptor) ATTR_WARN_UNUSED_RESULT;
tusb_error_t hidh_keyboard_open_subtask(uint8_t dev_addr, uint8_t const *descriptor, uint16_t *p_length) ATTR_WARN_UNUSED_RESULT;
void hidh_keyboard_close(uint8_t dev_addr);