Просмотр исходного кода

add usbh_device_info_t to return device status
add usbh_init and test code
replace usbh_device_is_plugged in hid_host and test_hid_host with usbh_device_info_t

hathach 13 лет назад
Родитель
Сommit
06f923c7bb

+ 11 - 9
tests/test/host/test_hid_host_keyboard.c

@@ -94,13 +94,13 @@ void tearDown(void)
 //--------------------------------------------------------------------+
 void test_keyboard_no_instances_invalid_para(void)
 {
-  usbh_device_is_plugged_IgnoreAndReturn(false);
+  tusbh_device_status_get_IgnoreAndReturn(0);
   TEST_ASSERT_EQUAL(0, tusbh_hid_keyboard_no_instances(TUSB_CFG_HOST_DEVICE_MAX));
 }
 
 void test_keyboard_install_ok(void)
 {
-  usbh_device_is_plugged_IgnoreAndReturn(true);
+  tusbh_device_status_get_IgnoreAndReturn(TUSB_DEVICE_STATUS_READY);
 
   TEST_ASSERT_EQUAL(0, tusbh_hid_keyboard_no_instances(device_hdl));
   TEST_ASSERT_EQUAL(TUSB_ERROR_NONE, class_hid_keyboard_install(device_hdl, (uint8_t*) &kbd_descriptor));
@@ -110,7 +110,7 @@ void test_keyboard_install_ok(void)
 void test_keyboard_init(void)
 {
   class_hid_keyboard_info_t keyboard_info_zero[TUSB_CFG_HOST_DEVICE_MAX];
-  memset(&keyboard_info_zero, 0, sizeof(class_hid_keyboard_info_t)*TUSB_CFG_HOST_DEVICE_MAX);
+  memset(keyboard_info_zero, 0, sizeof(class_hid_keyboard_info_t)*TUSB_CFG_HOST_DEVICE_MAX);
 
   class_hid_keyboard_init();
 
@@ -149,22 +149,24 @@ pipe_status_t pipe_status_get_stub(pipe_handle_t pipe_hdl, int num_call)
 
 void test_keyboard_get_invalid_para()
 {
-  usbh_device_is_plugged_IgnoreAndReturn(false);
+  tusbh_device_status_get_IgnoreAndReturn(TUSB_DEVICE_STATUS_READY);
   TEST_ASSERT_EQUAL(TUSB_ERROR_INVALID_PARA, tusbh_hid_keyboard_get(0, 0, NULL));
-  TEST_ASSERT_EQUAL(TUSB_ERROR_INVALID_PARA, tusbh_hid_keyboard_get(TUSB_CFG_HOST_DEVICE_MAX, 0, &report));
-  TEST_ASSERT_EQUAL(TUSB_ERROR_INVALID_PARA, tusbh_hid_keyboard_get(0, TUSB_CFG_HOST_HID_KEYBOARD_NO_INSTANCES_PER_DEVICE, &report));
+
+  tusbh_device_status_get_IgnoreAndReturn(0);
+  TEST_ASSERT_EQUAL(TUSB_ERROR_DEVICE_NOT_READY, tusbh_hid_keyboard_get(TUSB_CFG_HOST_DEVICE_MAX, 0, &report));
+  TEST_ASSERT_EQUAL(TUSB_ERROR_DEVICE_NOT_READY, tusbh_hid_keyboard_get(0, TUSB_CFG_HOST_HID_KEYBOARD_NO_INSTANCES_PER_DEVICE, &report));
 }
 
 void test_keyboard_get_class_not_supported()
 {
-  usbh_device_is_plugged_IgnoreAndReturn(true);
+  tusbh_device_status_get_IgnoreAndReturn(TUSB_DEVICE_STATUS_READY);
   keyboard_info_pool[device_hdl].instance[0].pipe_in = 0;
   TEST_ASSERT_EQUAL(TUSB_ERROR_CLASS_DEVICE_DONT_SUPPORT, tusbh_hid_keyboard_get(device_hdl, instance_num, &report));
 }
 
 void test_keyboard_get_report_not_available()
 {
-  usbh_device_is_plugged_IgnoreAndReturn(true);
+  tusbh_device_status_get_IgnoreAndReturn(TUSB_DEVICE_STATUS_READY);
 
   usbh_pipe_status_get_IgnoreAndReturn(PIPE_STATUS_BUSY);
   TEST_ASSERT_EQUAL(TUSB_ERROR_CLASS_DATA_NOT_AVAILABLE, tusbh_hid_keyboard_get(device_hdl, instance_num, &report));
@@ -175,7 +177,7 @@ void test_keyboard_get_report_not_available()
 
 void test_keyboard_get_ok()
 {
-  usbh_device_is_plugged_IgnoreAndReturn(true);
+  tusbh_device_status_get_IgnoreAndReturn(TUSB_DEVICE_STATUS_READY);
   usbh_pipe_status_get_StubWithCallback(pipe_status_get_stub);
 
   TEST_ASSERT_EQUAL(TUSB_ERROR_NONE, tusbh_hid_keyboard_get(device_hdl, instance_num, &report));

+ 25 - 2
tests/test/host/test_usbd_host.c

@@ -38,15 +38,38 @@
 #include "unity.h"
 #include "usbd_host.h"
 
+extern usbh_device_info_t device_info_pool[TUSB_CFG_HOST_DEVICE_MAX];
+tusb_handle_device_t dev_hdl;
 void setUp(void)
 {
+  dev_hdl = 0;
+  device_info_pool[dev_hdl].status = TUSB_DEVICE_STATUS_READY;
 }
 
 void tearDown(void)
 {
 }
 
-void test_()
+void test_usbh_init(void)
 {
-  // TEST_IGNORE();
+  usbh_device_info_t device_info_zero[TUSB_CFG_HOST_DEVICE_MAX];
+  memset(device_info_zero, 0, sizeof(usbh_device_info_t)*TUSB_CFG_HOST_DEVICE_MAX);
+
+  usbh_init();
+
+  TEST_ASSERT_EQUAL_MEMORY(device_info_zero, device_info_pool, sizeof(usbh_device_info_t)*TUSB_CFG_HOST_DEVICE_MAX);
+}
+
+void test_usbh_status_get_fail(void)
+{
+  usbh_init();
+  TEST_ASSERT_EQUAL( 0, tusbh_device_status_get(TUSB_CFG_HOST_DEVICE_MAX) );
+  TEST_ASSERT_EQUAL( TUSB_DEVICE_STATUS_UNPLUG, tusbh_device_status_get(dev_hdl) );
 }
+
+void test_usbh_status_get_succeed(void)
+{
+  device_info_pool[dev_hdl].status = TUSB_DEVICE_STATUS_READY;
+  TEST_ASSERT_EQUAL( TUSB_DEVICE_STATUS_READY, tusbh_device_status_get(dev_hdl) );
+}
+

+ 7 - 6
tinyusb/class/hid_host.c

@@ -35,7 +35,7 @@
  * This file is part of the tiny usb stack.
  */
 
-#include "common/common.h"
+#include "tusb_option.h"
 
 #if defined TUSB_CFG_HOST && defined DEVICE_CLASS_HID
 
@@ -44,6 +44,7 @@
 //--------------------------------------------------------------------+
 // INCLUDE
 //--------------------------------------------------------------------+
+#include "common/common.h"
 #include "hid_host.h"
 
 //--------------------------------------------------------------------+
@@ -57,13 +58,13 @@ STATIC_ class_hid_keyboard_info_t keyboard_info_pool[TUSB_CFG_HOST_DEVICE_MAX];
 
 
 //--------------------------------------------------------------------+
-// PUBLIC API
+// PUBLIC API (Parameter Verification is required)
 //--------------------------------------------------------------------+
 tusb_error_t tusbh_hid_keyboard_get(tusb_handle_device_t const device_hdl, uint8_t instance_num, tusb_keyboard_report_t * const report)
 {
   keyboard_interface_t *p_kbd;
 
-  ASSERT(usbh_device_is_plugged(device_hdl), TUSB_ERROR_INVALID_PARA);
+  ASSERT_INT(TUSB_DEVICE_STATUS_READY, tusbh_device_status_get(device_hdl), TUSB_ERROR_DEVICE_NOT_READY);
   ASSERT_PTR(report, TUSB_ERROR_INVALID_PARA);
   ASSERT(instance_num < TUSB_CFG_HOST_HID_KEYBOARD_NO_INSTANCES_PER_DEVICE, TUSB_ERROR_INVALID_PARA);
 
@@ -80,13 +81,13 @@ tusb_error_t tusbh_hid_keyboard_get(tusb_handle_device_t const device_hdl, uint8
 
 uint8_t tusbh_hid_keyboard_no_instances(tusb_handle_device_t const device_hdl)
 {
-  ASSERT(usbh_device_is_plugged(device_hdl), 0);
+  ASSERT_INT(TUSB_DEVICE_STATUS_READY, tusbh_device_status_get(device_hdl), 0);
 
   return keyboard_info_pool[device_hdl].instance_count;
 }
 
 //--------------------------------------------------------------------+
-// CLASS-USBD API
+// CLASS-USBD API (don't require to verify parameters)
 //--------------------------------------------------------------------+
 void class_hid_keyboard_init(void)
 {
@@ -95,7 +96,7 @@ void class_hid_keyboard_init(void)
 
 tusb_error_t class_hid_keyboard_install(uint8_t const dev_addr, uint8_t const *descriptor)
 {
-  keyboard_info_pool[0].instance_count++;
+  keyboard_info_pool[dev_addr].instance_count++;
 
   return TUSB_ERROR_NONE;
 }

+ 1 - 0
tinyusb/common/errors.h

@@ -62,6 +62,7 @@
 #define ERROR_TABLE(ENTRY) \
     ENTRY(TUSB_ERROR_NONE)\
     ENTRY(TUSB_ERROR_INVALID_PARA)\
+    ENTRY(TUSB_ERROR_DEVICE_NOT_READY)\
     ENTRY(TUSB_ERROR_CLASS_DEVICE_DONT_SUPPORT)\
     ENTRY(TUSB_ERROR_CLASS_DATA_NOT_AVAILABLE)\
     ENTRY(TUSB_ERROR_OSAL_TIMEOUT)\

+ 35 - 2
tinyusb/host/usbd_host.c

@@ -35,11 +35,44 @@
  * This file is part of the tiny usb stack.
  */
 
-#include "usbd_host.h"
+#include "tusb_option.h"
 
 #ifdef TUSB_CFG_HOST
 
-usbh_device_info_t usbh_device_pool[TUSB_CFG_HOST_DEVICE_MAX];
+#define _TINY_USB_SOURCE_FILE_
+
+//--------------------------------------------------------------------+
+// INCLUDE
+//--------------------------------------------------------------------+
+#include "common/common.h"
+#include "usbd_host.h"
+
+//--------------------------------------------------------------------+
+// MACRO CONSTANT TYPEDEF
+//--------------------------------------------------------------------+
+
+
+//--------------------------------------------------------------------+
+// INTERNAL OBJECT & FUNCTION DECLARATION
+//--------------------------------------------------------------------+
+STATIC_ usbh_device_info_t device_info_pool[TUSB_CFG_HOST_DEVICE_MAX];
+
+//--------------------------------------------------------------------+
+// PUBLIC API (Parameter Verification is required)
+//--------------------------------------------------------------------+
+tusbh_device_status_t tusbh_device_status_get (tusb_handle_device_t const device_hdl)
+{
+  ASSERT(device_hdl < TUSB_CFG_HOST_DEVICE_MAX, 0);
+  return device_info_pool[device_hdl].status;
+}
+
+//--------------------------------------------------------------------+
+// CLASS-USBD API (don't require to verify parameters)
+//--------------------------------------------------------------------+
+void usbh_init(void)
+{
+  memset(device_info_pool, 0, sizeof(usbh_device_info_t)*TUSB_CFG_HOST_DEVICE_MAX);
+}
 
 #if 0
 tusb_error_t tusbh_keyboard_open(tusb_handle_device_t device_hdl, uint8_t configure_num, tusb_handle_keyboard_t *keyboard_hdl)

+ 14 - 2
tinyusb/host/usbd_host.h

@@ -98,10 +98,21 @@ enum {
   TUSB_FLAGS_CLASS_VENDOR_SPECIFIC      = BIT_(31)
 };
 
+/// Device Status
+enum {
+  TUSB_DEVICE_STATUS_UNPLUG = 0,
+  TUSB_DEVICE_STATUS_READY = BIT_(0),
+
+  TUSB_DEVICE_STATUS_REMOVING = BIT_(2),
+  TUSB_DEVICE_STATUS_SAFE_REMOVE = BIT_(3),
+};
+
+typedef uint8_t  tusbh_device_status_t;
 typedef uint32_t tusbh_flag_class_t;
 
-typedef struct {
+typedef struct { // TODO internal structure
   uint8_t core_id;
+  tusbh_device_status_t status;
   pipe_handle_t pipe_control;
 
 #if 0 // TODO allow configure for vendor/product
@@ -134,6 +145,7 @@ typedef uint32_t tusb_handle_device_t;
 void         tusbh_device_mounting_cb (tusb_error_t const error, tusb_handle_device_t const device_hdl);
 void         tusbh_device_mounted_cb (tusb_error_t const error, tusb_handle_device_t const device_hdl);
 tusb_error_t tusbh_configuration_set     (tusb_handle_device_t const device_hdl, uint8_t const configure_number) ATTR_WARN_UNUSED_RESULT;
+tusbh_device_status_t tusbh_device_status_get (tusb_handle_device_t const device_hdl) ATTR_WARN_UNUSED_RESULT;
 
 
 //--------------------------------------------------------------------+
@@ -141,7 +153,7 @@ tusb_error_t tusbh_configuration_set     (tusb_handle_device_t const device_hdl,
 //--------------------------------------------------------------------+
 #ifdef _TINY_USB_SOURCE_FILE_
 
-bool          usbh_device_is_plugged(tusb_handle_device_t const device_hdl) ATTR_WARN_UNUSED_RESULT;
+void usbh_init(void);
 pipe_status_t usbh_pipe_status_get(pipe_handle_t pipe_hdl) ATTR_WARN_UNUSED_RESULT;
 
 #endif