test_enum_task.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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.org)
  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_devices[TUSB_CFG_HOST_DEVICE_MAX+1];
  46. extern uint8_t enum_data_buffer[TUSB_CFG_HOST_ENUM_BUFFER_SIZE];
  47. uint8_t dev_addr;
  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_devices, 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. osal_semaphore_reset_Expect( usbh_devices[0].control.sem_hdl );
  65. hcd_pipe_control_open_ExpectAndReturn(0, 8, TUSB_ERROR_NONE);
  66. }
  67. void tearDown(void)
  68. {
  69. }
  70. //--------------------------------------------------------------------+
  71. // STUB & HELPER
  72. //--------------------------------------------------------------------+
  73. 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)
  74. {
  75. (*p_data) = ( *((uint32_t*) &enum_connect) );
  76. (*p_error) = TUSB_ERROR_NONE;
  77. }
  78. void semaphore_wait_success_stub(osal_semaphore_handle_t const sem_hdl, uint32_t msec, tusb_error_t *p_error, int num_call)
  79. {
  80. (*p_error) = TUSB_ERROR_NONE;
  81. }
  82. #define semaphore_wait_timeout_stub(n) semaphore_wait_timeout_##n
  83. #define semaphore_wait_timeout(n) \
  84. void semaphore_wait_timeout_##n(osal_semaphore_handle_t const sem_hdl, uint32_t msec, tusb_error_t *p_error, int num_call) {\
  85. if (num_call >= n)\
  86. (*p_error) = TUSB_ERROR_OSAL_TIMEOUT;\
  87. else \
  88. (*p_error) = TUSB_ERROR_NONE;\
  89. }
  90. semaphore_wait_timeout(0)
  91. semaphore_wait_timeout(1)
  92. semaphore_wait_timeout(2)
  93. semaphore_wait_timeout(3)
  94. semaphore_wait_timeout(4)
  95. semaphore_wait_timeout(5)
  96. semaphore_wait_timeout(6)
  97. semaphore_wait_timeout(7)
  98. semaphore_wait_timeout(8)
  99. tusb_error_t control_xfer_stub(uint8_t dev_addr, const tusb_std_request_t * const p_request, uint8_t data[], int num_call)
  100. {
  101. switch (num_call)
  102. {
  103. case 0: // get 8 bytes of device descriptor
  104. TEST_ASSERT_EQUAL(TUSB_REQUEST_GET_DESCRIPTOR, p_request->bRequest);
  105. TEST_ASSERT_EQUAL(TUSB_DESC_DEVICE, p_request->wValue >> 8);
  106. TEST_ASSERT_EQUAL(8, p_request->wLength);
  107. memcpy(data, &desc_device, p_request->wLength);
  108. break;
  109. case 1: // set device address
  110. TEST_ASSERT_EQUAL(TUSB_REQUEST_SET_ADDRESS, p_request->bRequest);
  111. TEST_ASSERT_EQUAL(p_request->wValue, 1);
  112. break;
  113. case 2: // get full device decriptor for new address
  114. TEST_ASSERT_EQUAL(TUSB_REQUEST_GET_DESCRIPTOR, p_request->bRequest);
  115. TEST_ASSERT_EQUAL(TUSB_DESC_DEVICE, p_request->wValue >> 8);
  116. TEST_ASSERT_EQUAL(18, p_request->wLength);
  117. memcpy(data, &desc_device, p_request->wLength);
  118. break;
  119. case 3: // get 9 bytes of configuration descriptor
  120. TEST_ASSERT_EQUAL(TUSB_REQUEST_GET_DESCRIPTOR, p_request->bRequest);
  121. TEST_ASSERT_EQUAL(TUSB_DESC_CONFIGURATION, p_request->wValue >> 8);
  122. TEST_ASSERT_EQUAL(9, p_request->wLength);
  123. memcpy(data, &desc_configuration, p_request->wLength);
  124. break;
  125. case 4: // get full-length configuration descriptor
  126. TEST_ASSERT_EQUAL(TUSB_REQUEST_GET_DESCRIPTOR, p_request->bRequest);
  127. TEST_ASSERT_EQUAL(TUSB_DESC_CONFIGURATION, p_request->wValue >> 8);
  128. TEST_ASSERT_EQUAL(desc_configuration.configuration.wTotalLength, p_request->wLength);
  129. memcpy(data, &desc_configuration, p_request->wLength);
  130. break;
  131. default:
  132. return TUSB_ERROR_OSAL_TIMEOUT;
  133. }
  134. return TUSB_ERROR_NONE;
  135. }
  136. tusb_error_t hidh_install_stub(uint8_t dev_addr, uint8_t const *descriptor, uint16_t *p_length, int num_call)
  137. {
  138. *p_length = sizeof(tusb_descriptor_interface_t) + sizeof(tusb_hid_descriptor_hid_t) + sizeof(tusb_descriptor_endpoint_t);
  139. return TUSB_ERROR_NONE;
  140. }
  141. //--------------------------------------------------------------------+
  142. // enumeration
  143. //--------------------------------------------------------------------+
  144. void test_addr0_failed_dev_desc(void)
  145. {
  146. osal_semaphore_wait_StubWithCallback(semaphore_wait_timeout_stub(0));
  147. tusbh_device_mount_failed_cb_Expect(TUSB_ERROR_USBH_MOUNT_DEVICE_NOT_RESPOND, NULL);
  148. usbh_enumeration_task();
  149. TEST_ASSERT_EQUAL(TUSB_DEVICE_STATE_ADDRESSED, usbh_devices[0].state);
  150. }
  151. void test_addr0_failed_set_address(void)
  152. {
  153. osal_semaphore_wait_StubWithCallback(semaphore_wait_timeout_stub(1));
  154. tusbh_device_mount_failed_cb_Expect(TUSB_ERROR_USBH_MOUNT_DEVICE_NOT_RESPOND, NULL);
  155. usbh_enumeration_task();
  156. TEST_ASSERT_EQUAL(TUSB_DEVICE_STATE_ADDRESSED, usbh_devices[0].state);
  157. TEST_ASSERT_EQUAL_MEMORY(&desc_device, enum_data_buffer, 8);
  158. }
  159. void test_enum_failed_get_full_dev_desc(void)
  160. {
  161. osal_semaphore_wait_StubWithCallback(semaphore_wait_timeout_stub(2));
  162. hcd_pipe_control_close_ExpectAndReturn(0, TUSB_ERROR_NONE);
  163. osal_semaphore_reset_Expect( usbh_devices[0].control.sem_hdl );
  164. hcd_pipe_control_open_ExpectAndReturn(1, desc_device.bMaxPacketSize0, TUSB_ERROR_NONE);
  165. tusbh_device_mount_failed_cb_Expect(TUSB_ERROR_USBH_MOUNT_DEVICE_NOT_RESPOND, NULL);
  166. usbh_enumeration_task();
  167. TEST_ASSERT_EQUAL(TUSB_DEVICE_STATE_UNPLUG, usbh_devices[0].state);
  168. TEST_ASSERT_EQUAL(TUSB_DEVICE_STATE_ADDRESSED, usbh_devices[1].state);
  169. TEST_ASSERT_EQUAL(TUSB_SPEED_FULL, usbh_devices[1].speed);
  170. TEST_ASSERT_EQUAL(enum_connect.core_id, usbh_devices[1].core_id);
  171. TEST_ASSERT_EQUAL(enum_connect.hub_addr, usbh_devices[1].hub_addr);
  172. TEST_ASSERT_EQUAL(enum_connect.hub_port, usbh_devices[1].hub_port);
  173. }
  174. void test_enum_failed_get_9byte_config_desc(void)
  175. {
  176. osal_semaphore_wait_StubWithCallback(semaphore_wait_timeout_stub(3));
  177. hcd_pipe_control_close_ExpectAndReturn(0, TUSB_ERROR_NONE);
  178. osal_semaphore_reset_Expect( usbh_devices[0].control.sem_hdl );
  179. hcd_pipe_control_open_ExpectAndReturn(1, desc_device.bMaxPacketSize0, TUSB_ERROR_NONE);
  180. tusbh_device_attached_cb_ExpectAndReturn((tusb_descriptor_device_t*) enum_data_buffer, 1);
  181. tusbh_device_mount_failed_cb_Expect(TUSB_ERROR_USBH_MOUNT_DEVICE_NOT_RESPOND, NULL);
  182. usbh_enumeration_task();
  183. TEST_ASSERT_EQUAL(desc_device.idVendor, usbh_devices[1].vendor_id);
  184. TEST_ASSERT_EQUAL(desc_device.idProduct, usbh_devices[1].product_id);
  185. TEST_ASSERT_EQUAL(desc_device.bNumConfigurations, usbh_devices[1].configure_count);
  186. }
  187. void test_enum_failed_get_full_config_desc(void)
  188. {
  189. osal_semaphore_wait_StubWithCallback(semaphore_wait_timeout_stub(4));
  190. hcd_pipe_control_close_ExpectAndReturn(0, TUSB_ERROR_NONE);
  191. osal_semaphore_reset_Expect( usbh_devices[0].control.sem_hdl );
  192. hcd_pipe_control_open_ExpectAndReturn(1, desc_device.bMaxPacketSize0, TUSB_ERROR_NONE);
  193. tusbh_device_attached_cb_ExpectAndReturn((tusb_descriptor_device_t*) enum_data_buffer, 1);
  194. tusbh_device_mount_failed_cb_Expect(TUSB_ERROR_USBH_MOUNT_DEVICE_NOT_RESPOND, NULL);
  195. usbh_enumeration_task();
  196. }
  197. void class_install_expect(void)
  198. {
  199. hidh_open_subtask_StubWithCallback(hidh_install_stub);
  200. }
  201. void test_enum_parse_config_desc(void)
  202. {
  203. osal_semaphore_wait_StubWithCallback(semaphore_wait_timeout_stub(5));
  204. hcd_pipe_control_close_ExpectAndReturn(0, TUSB_ERROR_NONE);
  205. osal_semaphore_reset_Expect( usbh_devices[0].control.sem_hdl );
  206. hcd_pipe_control_open_ExpectAndReturn(1, desc_device.bMaxPacketSize0, TUSB_ERROR_NONE);
  207. tusbh_device_attached_cb_ExpectAndReturn((tusb_descriptor_device_t*) enum_data_buffer, 1);
  208. class_install_expect();
  209. tusbh_device_mount_failed_cb_Expect(TUSB_ERROR_USBH_MOUNT_DEVICE_NOT_RESPOND, NULL); // fail to set configure
  210. usbh_enumeration_task();
  211. TEST_ASSERT_EQUAL(desc_configuration.configuration.bNumInterfaces, usbh_devices[1].interface_count);
  212. }
  213. void test_enum_set_configure(void)
  214. {
  215. osal_semaphore_wait_StubWithCallback(semaphore_wait_timeout_stub(6));
  216. hcd_pipe_control_close_ExpectAndReturn(0, TUSB_ERROR_NONE);
  217. osal_semaphore_reset_Expect( usbh_devices[0].control.sem_hdl );
  218. hcd_pipe_control_open_ExpectAndReturn(1, desc_device.bMaxPacketSize0, TUSB_ERROR_NONE);
  219. tusbh_device_attached_cb_ExpectAndReturn((tusb_descriptor_device_t*) enum_data_buffer, 1);
  220. class_install_expect();
  221. tusbh_device_mount_succeed_cb_Expect(1);
  222. usbh_enumeration_task();
  223. }