test_enum_task.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 "usbh.h"
  39. #include "descriptor_test.h"
  40. #include "mock_osal.h"
  41. #include "mock_hcd.h"
  42. #include "mock_usbh_hcd.h"
  43. #include "mock_tusb_callback.h"
  44. extern usbh_device_info_t usbh_device_info_pool[TUSB_CFG_HOST_DEVICE_MAX+1];
  45. extern uint8_t enum_data_buffer[TUSB_CFG_HOST_ENUM_BUFFER_SIZE];
  46. tusb_handle_device_t dev_hdl;
  47. usbh_enumerate_t const enum_connect = {
  48. .core_id = 0,
  49. .hub_addr = 0,
  50. .hub_port = 0,
  51. .speed = TUSB_SPEED_FULL
  52. };
  53. 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);
  54. void semaphore_wait_success_stub(osal_semaphore_handle_t const sem_hdl, uint32_t msec, tusb_error_t *p_error, int num_call);
  55. tusb_error_t control_xfer_stub(uint8_t dev_addr, const tusb_std_request_t * const p_request, uint8_t data[], int num_call);
  56. void setUp(void)
  57. {
  58. memclr_(usbh_device_info_pool, sizeof(usbh_device_info_t)*(TUSB_CFG_HOST_DEVICE_MAX+1));
  59. osal_queue_receive_StubWithCallback(queue_recv_stub);
  60. osal_semaphore_wait_StubWithCallback(semaphore_wait_success_stub);
  61. hcd_pipe_control_xfer_StubWithCallback(control_xfer_stub);
  62. hcd_port_connect_status_ExpectAndReturn(enum_connect.core_id, true);
  63. hcd_pipe_control_open_ExpectAndReturn(0, 8, TUSB_ERROR_NONE);
  64. }
  65. void tearDown(void)
  66. {
  67. }
  68. //--------------------------------------------------------------------+
  69. // STUB & HELPER
  70. //--------------------------------------------------------------------+
  71. 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)
  72. {
  73. (*p_data) = ( *((uint32_t*) &enum_connect) );
  74. (*p_error) = TUSB_ERROR_NONE;
  75. }
  76. void semaphore_wait_success_stub(osal_semaphore_handle_t const sem_hdl, uint32_t msec, tusb_error_t *p_error, int num_call)
  77. {
  78. (*p_error) = TUSB_ERROR_NONE;
  79. }
  80. #define semaphore_wait_timeout_stub(n) semaphore_wait_timeout_##n
  81. #define semaphore_wait_timeout(n) \
  82. void semaphore_wait_timeout_##n(osal_semaphore_handle_t const sem_hdl, uint32_t msec, tusb_error_t *p_error, int num_call) {\
  83. if (num_call >= n)\
  84. (*p_error) = TUSB_ERROR_OSAL_TIMEOUT;\
  85. else \
  86. (*p_error) = TUSB_ERROR_NONE;\
  87. }
  88. semaphore_wait_timeout(0)
  89. semaphore_wait_timeout(1)
  90. semaphore_wait_timeout(2)
  91. semaphore_wait_timeout(3)
  92. semaphore_wait_timeout(4)
  93. semaphore_wait_timeout(5)
  94. semaphore_wait_timeout(6)
  95. semaphore_wait_timeout(7)
  96. semaphore_wait_timeout(8)
  97. tusb_error_t control_xfer_stub(uint8_t dev_addr, const tusb_std_request_t * const p_request, uint8_t data[], int num_call)
  98. {
  99. switch (num_call)
  100. {
  101. case 0: // get 8 bytes of device descriptor
  102. TEST_ASSERT_EQUAL(TUSB_REQUEST_GET_DESCRIPTOR, p_request->bRequest);
  103. TEST_ASSERT_EQUAL(TUSB_DESC_DEVICE, p_request->wValue >> 8);
  104. TEST_ASSERT_EQUAL(8, p_request->wLength);
  105. memcpy(data, &desc_device, p_request->wLength);
  106. break;
  107. case 1: // set device address
  108. TEST_ASSERT_EQUAL(TUSB_REQUEST_SET_ADDRESS, p_request->bRequest);
  109. TEST_ASSERT_EQUAL(p_request->wValue, 1);
  110. break;
  111. case 2: // get full device decriptor for new address
  112. TEST_ASSERT_EQUAL(TUSB_REQUEST_GET_DESCRIPTOR, p_request->bRequest);
  113. TEST_ASSERT_EQUAL(TUSB_DESC_DEVICE, p_request->wValue >> 8);
  114. TEST_ASSERT_EQUAL(18, p_request->wLength);
  115. memcpy(data, &desc_device, p_request->wLength);
  116. break;
  117. case 3: // get 9 bytes of configuration descriptor
  118. TEST_ASSERT_EQUAL(TUSB_REQUEST_GET_DESCRIPTOR, p_request->bRequest);
  119. TEST_ASSERT_EQUAL(TUSB_DESC_CONFIGURATION, p_request->wValue >> 8);
  120. TEST_ASSERT_EQUAL(9, p_request->wLength);
  121. memcpy(data, &desc_configuration, p_request->wLength);
  122. break;
  123. case 4: // get full-length configuration descriptor
  124. TEST_ASSERT_EQUAL(TUSB_REQUEST_GET_DESCRIPTOR, p_request->bRequest);
  125. TEST_ASSERT_EQUAL(TUSB_DESC_CONFIGURATION, p_request->wValue >> 8);
  126. TEST_ASSERT_EQUAL(desc_configuration.configuration.wTotalLength, p_request->wLength);
  127. memcpy(data, &desc_configuration, p_request->wLength);
  128. break;
  129. default:
  130. return TUSB_ERROR_OSAL_TIMEOUT;
  131. }
  132. return TUSB_ERROR_NONE;
  133. }
  134. //--------------------------------------------------------------------+
  135. // enumeration
  136. //--------------------------------------------------------------------+
  137. void test_addr0_failed_dev_desc(void)
  138. {
  139. osal_semaphore_wait_StubWithCallback(semaphore_wait_timeout_stub(0));
  140. tusbh_device_mount_failed_cb_Expect(TUSB_ERROR_USBH_MOUNT_DEVICE_NOT_RESPOND, NULL);
  141. usbh_enumeration_task();
  142. }
  143. void test_addr0_failed_set_address(void)
  144. {
  145. osal_semaphore_wait_StubWithCallback(semaphore_wait_timeout_stub(1));
  146. tusbh_device_mount_failed_cb_Expect(TUSB_ERROR_USBH_MOUNT_DEVICE_NOT_RESPOND, NULL);
  147. usbh_enumeration_task();
  148. TEST_ASSERT_EQUAL_MEMORY(&desc_device, enum_data_buffer, 8);
  149. }
  150. void test_enum_failed_get_full_dev_desc(void)
  151. {
  152. osal_semaphore_wait_StubWithCallback(semaphore_wait_timeout_stub(2));
  153. hcd_pipe_control_open_ExpectAndReturn(1, desc_device.bMaxPacketSize0, TUSB_ERROR_NONE);
  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_STATUS_ADDRESSED, usbh_device_info_pool[1].status);
  157. TEST_ASSERT_EQUAL(TUSB_SPEED_FULL, usbh_device_info_pool[1].speed);
  158. TEST_ASSERT_EQUAL(enum_connect.core_id, usbh_device_info_pool[1].core_id);
  159. TEST_ASSERT_EQUAL(enum_connect.hub_addr, usbh_device_info_pool[1].hub_addr);
  160. TEST_ASSERT_EQUAL(enum_connect.hub_port, usbh_device_info_pool[1].hub_port);
  161. }
  162. void test_enum_failed_get_9byte_config_desc(void)
  163. {
  164. osal_semaphore_wait_StubWithCallback(semaphore_wait_timeout_stub(3));
  165. hcd_pipe_control_open_ExpectAndReturn(1, desc_device.bMaxPacketSize0, TUSB_ERROR_NONE);
  166. tusbh_device_attached_cb_ExpectAndReturn((tusb_descriptor_device_t*) enum_data_buffer, 1);
  167. tusbh_device_mount_failed_cb_Expect(TUSB_ERROR_USBH_MOUNT_DEVICE_NOT_RESPOND, NULL);
  168. usbh_enumeration_task();
  169. TEST_ASSERT_EQUAL(desc_device.idVendor, usbh_device_info_pool[1].vendor_id);
  170. TEST_ASSERT_EQUAL(desc_device.idProduct, usbh_device_info_pool[1].product_id);
  171. TEST_ASSERT_EQUAL(desc_device.bNumConfigurations, usbh_device_info_pool[1].configure_count);
  172. }
  173. void test_enum_failed_get_full_config_desc(void)
  174. {
  175. osal_semaphore_wait_StubWithCallback(semaphore_wait_timeout_stub(4));
  176. hcd_pipe_control_open_ExpectAndReturn(1, desc_device.bMaxPacketSize0, TUSB_ERROR_NONE);
  177. tusbh_device_attached_cb_ExpectAndReturn((tusb_descriptor_device_t*) enum_data_buffer, 1);
  178. tusbh_device_mount_failed_cb_Expect(TUSB_ERROR_USBH_MOUNT_DEVICE_NOT_RESPOND, NULL);
  179. usbh_enumeration_task();
  180. }
  181. void test_enum_parse_config_desc(void)
  182. {
  183. osal_semaphore_wait_StubWithCallback(semaphore_wait_timeout_stub(5));
  184. hcd_pipe_control_open_ExpectAndReturn(1, desc_device.bMaxPacketSize0, TUSB_ERROR_NONE);
  185. tusbh_device_attached_cb_ExpectAndReturn((tusb_descriptor_device_t*) enum_data_buffer, 1);
  186. // hid_install
  187. usbh_enumeration_task();
  188. }