test_enum_task.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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];
  45. extern usbh_device_addr0_t device_addr0;
  46. extern uint8_t enum_data_buffer[TUSB_CFG_HOST_ENUM_BUFFER_SIZE];
  47. tusb_handle_device_t dev_hdl;
  48. pipe_handle_t pipe_addr0 = 12;
  49. usbh_enumerate_t const enum_connect = {
  50. .core_id = 0,
  51. .hub_addr = 0,
  52. .hub_port = 0,
  53. .connect_status = 1
  54. };
  55. 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);
  56. void semaphore_wait_success_stub(osal_semaphore_handle_t const sem_hdl, uint32_t msec, tusb_error_t *p_error, int num_call);
  57. tusb_error_t control_xfer_stub(pipe_handle_t pipe_hdl, const tusb_std_request_t * const p_request, uint8_t data[], int num_call);
  58. void setUp(void)
  59. {
  60. memclr_(usbh_device_info_pool, sizeof(usbh_device_info_t)*TUSB_CFG_HOST_DEVICE_MAX);
  61. osal_queue_receive_StubWithCallback(queue_recv_stub);
  62. osal_semaphore_wait_StubWithCallback(semaphore_wait_success_stub);
  63. hcd_pipe_control_xfer_StubWithCallback(control_xfer_stub);
  64. hcd_port_connect_status_ExpectAndReturn(enum_connect.core_id, true);
  65. hcd_port_speed_ExpectAndReturn(enum_connect.core_id, TUSB_SPEED_FULL);
  66. hcd_addr0_open_IgnoreAndReturn(TUSB_ERROR_NONE);
  67. }
  68. void tearDown(void)
  69. {
  70. }
  71. //--------------------------------------------------------------------+
  72. // STUB & HELPER
  73. //--------------------------------------------------------------------+
  74. 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)
  75. {
  76. (*p_data) = ( *((uint32_t*) &enum_connect) );
  77. (*p_error) = TUSB_ERROR_NONE;
  78. }
  79. void semaphore_wait_success_stub(osal_semaphore_handle_t const sem_hdl, uint32_t msec, tusb_error_t *p_error, int num_call)
  80. {
  81. (*p_error) = TUSB_ERROR_NONE;
  82. }
  83. #define semaphore_wait_timeout_stub(n) semaphore_wait_timeout_##n
  84. #define semaphore_wait_timeout(n) \
  85. void semaphore_wait_timeout_##n(osal_semaphore_handle_t const sem_hdl, uint32_t msec, tusb_error_t *p_error, int num_call) {\
  86. if (num_call >= n)\
  87. (*p_error) = TUSB_ERROR_OSAL_TIMEOUT;\
  88. else \
  89. (*p_error) = TUSB_ERROR_NONE;\
  90. }
  91. semaphore_wait_timeout(0)
  92. semaphore_wait_timeout(1)
  93. semaphore_wait_timeout(2)
  94. semaphore_wait_timeout(3)
  95. semaphore_wait_timeout(4)
  96. tusb_error_t control_xfer_stub(pipe_handle_t pipe_hdl, const tusb_std_request_t * const p_request, uint8_t data[], int num_call)
  97. {
  98. switch (num_call)
  99. {
  100. case 0: // get 8 bytes of device descriptor
  101. TEST_ASSERT_EQUAL(TUSB_REQUEST_GET_DESCRIPTOR, p_request->bRequest);
  102. TEST_ASSERT_EQUAL(TUSB_DESC_DEVICE, p_request->wValue >> 8);
  103. TEST_ASSERT_EQUAL(8, p_request->wLength);
  104. memcpy(data, &desc_device, p_request->wLength);
  105. break;
  106. case 1: // set device address
  107. TEST_ASSERT_EQUAL(TUSB_REQUEST_SET_ADDRESS, p_request->bRequest);
  108. TEST_ASSERT_EQUAL(p_request->wValue, 1);
  109. break;
  110. case 2: // get full device decriptor for new address
  111. TEST_ASSERT_EQUAL(TUSB_REQUEST_GET_DESCRIPTOR, p_request->bRequest);
  112. TEST_ASSERT_EQUAL(TUSB_DESC_DEVICE, p_request->wValue >> 8);
  113. TEST_ASSERT_EQUAL(18, p_request->wLength);
  114. memcpy(data, &desc_device, p_request->wLength);
  115. break;
  116. case 3: // get 9 bytes of configuration descriptor
  117. TEST_ASSERT_EQUAL(TUSB_REQUEST_GET_DESCRIPTOR, p_request->bRequest);
  118. TEST_ASSERT_EQUAL(TUSB_DESC_CONFIGURATION, p_request->wValue >> 8);
  119. TEST_ASSERT_EQUAL(9, p_request->wLength);
  120. memcpy(data, &desc_configuration, p_request->wLength);
  121. break;
  122. case 4: // get full-length configuration descriptor
  123. TEST_ASSERT_EQUAL(TUSB_REQUEST_GET_DESCRIPTOR, p_request->bRequest);
  124. TEST_ASSERT_EQUAL(TUSB_DESC_CONFIGURATION, p_request->wValue >> 8);
  125. TEST_ASSERT_EQUAL(desc_configuration.configuration.wTotalLength, p_request->wLength);
  126. memcpy(data, &desc_configuration, p_request->wLength);
  127. break;
  128. }
  129. return TUSB_ERROR_NONE;
  130. }
  131. //--------------------------------------------------------------------+
  132. // enum connect directed
  133. //--------------------------------------------------------------------+
  134. void test_addr0_failed_dev_desc(void)
  135. {
  136. osal_semaphore_wait_StubWithCallback(semaphore_wait_timeout_stub(0));
  137. tusbh_device_mount_failed_cb_Expect(TUSB_ERROR_USBH_MOUNT_DEVICE_NOT_RESPOND, NULL);
  138. usbh_enumeration_task();
  139. }
  140. void test_addr0_failed_set_address(void)
  141. {
  142. osal_semaphore_wait_StubWithCallback(semaphore_wait_timeout_stub(1));
  143. tusbh_device_mount_failed_cb_Expect(TUSB_ERROR_USBH_MOUNT_DEVICE_NOT_RESPOND, NULL);
  144. usbh_enumeration_task();
  145. TEST_ASSERT_EQUAL_MEMORY(&desc_device, enum_data_buffer, 8);
  146. }
  147. void test_enum_task_connect(void)
  148. {
  149. usbh_enumeration_task();
  150. TEST_ASSERT_EQUAL(TUSB_DEVICE_STATUS_ADDRESSED, usbh_device_info_pool[0].status);
  151. TEST_ASSERT_EQUAL(TUSB_SPEED_FULL, usbh_device_info_pool[0].speed);
  152. TEST_ASSERT_EQUAL(enum_connect.core_id, usbh_device_info_pool[0].core_id);
  153. TEST_ASSERT_EQUAL(enum_connect.hub_addr, usbh_device_info_pool[0].hub_addr);
  154. TEST_ASSERT_EQUAL(enum_connect.hub_port, usbh_device_info_pool[0].hub_port);
  155. // hcd_pipe_control_open_ExpectAndReturn(1, desc_device.bMaxPacketSize0, TUSB_ERROR_NONE);
  156. }
  157. void test_enum_task_disconnect(void)
  158. {
  159. TEST_IGNORE();
  160. }
  161. void test_enum_task_connect_via_hub(void)
  162. {
  163. TEST_IGNORE();
  164. }
  165. void test_enum_task_disconnect_via_hub(void)
  166. {
  167. TEST_IGNORE();
  168. }