test_enum_task.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * test_enum_task.c
  3. *
  4. * Created on: Feb 5, 2013
  5. * Author: hathach
  6. */
  7. /*
  8. * Software License Agreement (BSD License)
  9. * Copyright (c) 2012, hathach (tinyusb.net)
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without modification,
  13. * are permitted provided that the following conditions are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. * 3. The name of the author may not be used to endorse or promote products
  21. * derived from this software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  24. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  25. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  26. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  27. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  28. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  31. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  32. * OF SUCH DAMAGE.
  33. *
  34. * This file is part of the tiny usb stack.
  35. */
  36. #include "unity.h"
  37. #include "errors.h"
  38. #include "descriptor_test.h"
  39. #include "mock_osal.h"
  40. #include "usbh.h"
  41. #include "mock_hcd.h"
  42. #include "usbh_hcd.h"
  43. #include "mock_tusb_callback.h"
  44. #include "mock_hid_host.h"
  45. extern usbh_device_info_t usbh_device_info_pool[TUSB_CFG_HOST_DEVICE_MAX+1];
  46. extern uint8_t enum_data_buffer[TUSB_CFG_HOST_ENUM_BUFFER_SIZE];
  47. tusb_handle_device_t dev_hdl;
  48. usbh_enumerate_t const enum_connect = {
  49. .core_id = 0,
  50. .hub_addr = 0,
  51. .hub_port = 0,
  52. .speed = TUSB_SPEED_FULL
  53. };
  54. void queue_recv_stub (osal_queue_handle_t const queue_hdl, uint32_t *p_data, uint32_t msec, tusb_error_t *p_error, int num_call);
  55. void semaphore_wait_success_stub(osal_semaphore_handle_t const sem_hdl, uint32_t msec, tusb_error_t *p_error, int num_call);
  56. tusb_error_t control_xfer_stub(uint8_t dev_addr, const tusb_std_request_t * const p_request, uint8_t data[], int num_call);
  57. void setUp(void)
  58. {
  59. memclr_(usbh_device_info_pool, sizeof(usbh_device_info_t)*(TUSB_CFG_HOST_DEVICE_MAX+1));
  60. osal_queue_receive_StubWithCallback(queue_recv_stub);
  61. osal_semaphore_wait_StubWithCallback(semaphore_wait_success_stub);
  62. hcd_pipe_control_xfer_StubWithCallback(control_xfer_stub);
  63. hcd_port_connect_status_ExpectAndReturn(enum_connect.core_id, true);
  64. hcd_pipe_control_open_ExpectAndReturn(0, 8, TUSB_ERROR_NONE);
  65. }
  66. void tearDown(void)
  67. {
  68. }
  69. //--------------------------------------------------------------------+
  70. // STUB & HELPER
  71. //--------------------------------------------------------------------+
  72. void queue_recv_stub (osal_queue_handle_t const queue_hdl, uint32_t *p_data, uint32_t msec, tusb_error_t *p_error, int num_call)
  73. {
  74. (*p_data) = ( *((uint32_t*) &enum_connect) );
  75. (*p_error) = TUSB_ERROR_NONE;
  76. }
  77. void semaphore_wait_success_stub(osal_semaphore_handle_t const sem_hdl, uint32_t msec, tusb_error_t *p_error, int num_call)
  78. {
  79. (*p_error) = TUSB_ERROR_NONE;
  80. }
  81. #define semaphore_wait_timeout_stub(n) semaphore_wait_timeout_##n
  82. #define semaphore_wait_timeout(n) \
  83. void semaphore_wait_timeout_##n(osal_semaphore_handle_t const sem_hdl, uint32_t msec, tusb_error_t *p_error, int num_call) {\
  84. if (num_call >= n)\
  85. (*p_error) = TUSB_ERROR_OSAL_TIMEOUT;\
  86. else \
  87. (*p_error) = TUSB_ERROR_NONE;\
  88. }
  89. semaphore_wait_timeout(0)
  90. semaphore_wait_timeout(1)
  91. semaphore_wait_timeout(2)
  92. semaphore_wait_timeout(3)
  93. semaphore_wait_timeout(4)
  94. semaphore_wait_timeout(5)
  95. semaphore_wait_timeout(6)
  96. semaphore_wait_timeout(7)
  97. semaphore_wait_timeout(8)
  98. tusb_error_t control_xfer_stub(uint8_t dev_addr, const tusb_std_request_t * const p_request, uint8_t data[], int num_call)
  99. {
  100. switch (num_call)
  101. {
  102. case 0: // get 8 bytes of device descriptor
  103. TEST_ASSERT_EQUAL(TUSB_REQUEST_GET_DESCRIPTOR, p_request->bRequest);
  104. TEST_ASSERT_EQUAL(TUSB_DESC_DEVICE, p_request->wValue >> 8);
  105. TEST_ASSERT_EQUAL(8, p_request->wLength);
  106. memcpy(data, &desc_device, p_request->wLength);
  107. break;
  108. case 1: // set device address
  109. TEST_ASSERT_EQUAL(TUSB_REQUEST_SET_ADDRESS, p_request->bRequest);
  110. TEST_ASSERT_EQUAL(p_request->wValue, 1);
  111. break;
  112. case 2: // get full device decriptor for new address
  113. TEST_ASSERT_EQUAL(TUSB_REQUEST_GET_DESCRIPTOR, p_request->bRequest);
  114. TEST_ASSERT_EQUAL(TUSB_DESC_DEVICE, p_request->wValue >> 8);
  115. TEST_ASSERT_EQUAL(18, p_request->wLength);
  116. memcpy(data, &desc_device, p_request->wLength);
  117. break;
  118. case 3: // get 9 bytes of configuration descriptor
  119. TEST_ASSERT_EQUAL(TUSB_REQUEST_GET_DESCRIPTOR, p_request->bRequest);
  120. TEST_ASSERT_EQUAL(TUSB_DESC_CONFIGURATION, p_request->wValue >> 8);
  121. TEST_ASSERT_EQUAL(9, p_request->wLength);
  122. memcpy(data, &desc_configuration, p_request->wLength);
  123. break;
  124. case 4: // get full-length configuration descriptor
  125. TEST_ASSERT_EQUAL(TUSB_REQUEST_GET_DESCRIPTOR, p_request->bRequest);
  126. TEST_ASSERT_EQUAL(TUSB_DESC_CONFIGURATION, p_request->wValue >> 8);
  127. TEST_ASSERT_EQUAL(desc_configuration.configuration.wTotalLength, p_request->wLength);
  128. memcpy(data, &desc_configuration, p_request->wLength);
  129. break;
  130. default:
  131. return TUSB_ERROR_OSAL_TIMEOUT;
  132. }
  133. return TUSB_ERROR_NONE;
  134. }
  135. tusb_error_t hidh_install_stub(uint8_t dev_addr, uint8_t const *descriptor, uint16_t *p_length, int num_call)
  136. {
  137. *p_length = sizeof(tusb_descriptor_interface_t) + sizeof(tusb_hid_descriptor_hid_t) + sizeof(tusb_descriptor_endpoint_t);
  138. return TUSB_ERROR_NONE;
  139. }
  140. //--------------------------------------------------------------------+
  141. // enumeration
  142. //--------------------------------------------------------------------+
  143. void test_addr0_failed_dev_desc(void)
  144. {
  145. osal_semaphore_wait_StubWithCallback(semaphore_wait_timeout_stub(0));
  146. tusbh_device_mount_failed_cb_Expect(TUSB_ERROR_USBH_MOUNT_DEVICE_NOT_RESPOND, NULL);
  147. usbh_enumeration_task();
  148. }
  149. void test_addr0_failed_set_address(void)
  150. {
  151. osal_semaphore_wait_StubWithCallback(semaphore_wait_timeout_stub(1));
  152. tusbh_device_mount_failed_cb_Expect(TUSB_ERROR_USBH_MOUNT_DEVICE_NOT_RESPOND, NULL);
  153. usbh_enumeration_task();
  154. TEST_ASSERT_EQUAL_MEMORY(&desc_device, enum_data_buffer, 8);
  155. }
  156. void test_enum_failed_get_full_dev_desc(void)
  157. {
  158. osal_semaphore_wait_StubWithCallback(semaphore_wait_timeout_stub(2));
  159. hcd_pipe_control_close_ExpectAndReturn(0, TUSB_ERROR_NONE);
  160. hcd_pipe_control_open_ExpectAndReturn(1, desc_device.bMaxPacketSize0, TUSB_ERROR_NONE);
  161. tusbh_device_mount_failed_cb_Expect(TUSB_ERROR_USBH_MOUNT_DEVICE_NOT_RESPOND, NULL);
  162. usbh_enumeration_task();
  163. TEST_ASSERT_EQUAL(TUSB_DEVICE_STATUS_ADDRESSED, usbh_device_info_pool[1].status);
  164. TEST_ASSERT_EQUAL(TUSB_SPEED_FULL, usbh_device_info_pool[1].speed);
  165. TEST_ASSERT_EQUAL(enum_connect.core_id, usbh_device_info_pool[1].core_id);
  166. TEST_ASSERT_EQUAL(enum_connect.hub_addr, usbh_device_info_pool[1].hub_addr);
  167. TEST_ASSERT_EQUAL(enum_connect.hub_port, usbh_device_info_pool[1].hub_port);
  168. }
  169. void test_enum_failed_get_9byte_config_desc(void)
  170. {
  171. osal_semaphore_wait_StubWithCallback(semaphore_wait_timeout_stub(3));
  172. hcd_pipe_control_close_ExpectAndReturn(0, TUSB_ERROR_NONE);
  173. hcd_pipe_control_open_ExpectAndReturn(1, desc_device.bMaxPacketSize0, TUSB_ERROR_NONE);
  174. tusbh_device_attached_cb_ExpectAndReturn((tusb_descriptor_device_t*) enum_data_buffer, 1);
  175. tusbh_device_mount_failed_cb_Expect(TUSB_ERROR_USBH_MOUNT_DEVICE_NOT_RESPOND, NULL);
  176. usbh_enumeration_task();
  177. TEST_ASSERT_EQUAL(desc_device.idVendor, usbh_device_info_pool[1].vendor_id);
  178. TEST_ASSERT_EQUAL(desc_device.idProduct, usbh_device_info_pool[1].product_id);
  179. TEST_ASSERT_EQUAL(desc_device.bNumConfigurations, usbh_device_info_pool[1].configure_count);
  180. }
  181. void test_enum_failed_get_full_config_desc(void)
  182. {
  183. osal_semaphore_wait_StubWithCallback(semaphore_wait_timeout_stub(4));
  184. hcd_pipe_control_close_ExpectAndReturn(0, TUSB_ERROR_NONE);
  185. hcd_pipe_control_open_ExpectAndReturn(1, desc_device.bMaxPacketSize0, TUSB_ERROR_NONE);
  186. tusbh_device_attached_cb_ExpectAndReturn((tusb_descriptor_device_t*) enum_data_buffer, 1);
  187. tusbh_device_mount_failed_cb_Expect(TUSB_ERROR_USBH_MOUNT_DEVICE_NOT_RESPOND, NULL);
  188. usbh_enumeration_task();
  189. }
  190. void class_install_expect(void)
  191. {
  192. hidh_install_subtask_StubWithCallback(hidh_install_stub);
  193. }
  194. void test_enum_parse_config_desc(void)
  195. {
  196. osal_semaphore_wait_StubWithCallback(semaphore_wait_timeout_stub(5));
  197. hcd_pipe_control_close_ExpectAndReturn(0, TUSB_ERROR_NONE);
  198. hcd_pipe_control_open_ExpectAndReturn(1, desc_device.bMaxPacketSize0, TUSB_ERROR_NONE);
  199. tusbh_device_attached_cb_ExpectAndReturn((tusb_descriptor_device_t*) enum_data_buffer, 1);
  200. class_install_expect();
  201. tusbh_device_mount_failed_cb_Expect(TUSB_ERROR_USBH_MOUNT_DEVICE_NOT_RESPOND, NULL); // fail to set configure
  202. usbh_enumeration_task();
  203. TEST_ASSERT_EQUAL(desc_configuration.configuration.bNumInterfaces, usbh_device_info_pool[1].interface_count);
  204. }
  205. void test_enum_set_configure(void)
  206. {
  207. osal_semaphore_wait_StubWithCallback(semaphore_wait_timeout_stub(6));
  208. hcd_pipe_control_close_ExpectAndReturn(0, TUSB_ERROR_NONE);
  209. hcd_pipe_control_open_ExpectAndReturn(1, desc_device.bMaxPacketSize0, TUSB_ERROR_NONE);
  210. tusbh_device_attached_cb_ExpectAndReturn((tusb_descriptor_device_t*) enum_data_buffer, 1);
  211. class_install_expect();
  212. tusbh_device_mount_succeed_cb_Expect(1);
  213. usbh_enumeration_task();
  214. }