msc_client_async_enum.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdint.h>
  7. #include <string.h>
  8. #include <stdlib.h>
  9. #include <sys/param.h>
  10. #include "freertos/FreeRTOS.h"
  11. #include "freertos/task.h"
  12. #include "esp_err.h"
  13. #include "esp_log.h"
  14. #include "test_usb_mock_classes.h"
  15. #include "test_usb_common.h"
  16. #include "msc_client.h"
  17. #include "usb/usb_host.h"
  18. #include "unity.h"
  19. #include "test_utils.h"
  20. /*
  21. Implementation of an asynchronous MSC client used for USB Host enumeration test.
  22. - The MSC client will:
  23. - Register itself as a client
  24. - Receive USB_HOST_CLIENT_EVENT_NEW_DEV event message, and open the device
  25. - Check the device and configuration descriptor of the device
  26. - Check the device's information
  27. - Close device
  28. - Repeat for multiple iterations from waiting connection by forcing a disconnection
  29. - Deregister MSC client
  30. */
  31. #define TEST_ENUM_ITERATIONS 3
  32. typedef enum {
  33. TEST_STAGE_WAIT_CONN,
  34. TEST_STAGE_DEV_OPEN,
  35. TEST_STAGE_CHECK_DEV_DESC,
  36. TEST_STAGE_CHECK_CONFIG_DESC,
  37. TEST_STAGE_CHECK_STR_DESC,
  38. TEST_STAGE_DEV_CLOSE,
  39. } test_stage_t;
  40. typedef struct {
  41. test_stage_t cur_stage;
  42. test_stage_t next_stage;
  43. uint8_t dev_addr_to_open;
  44. usb_host_client_handle_t client_hdl;
  45. usb_device_handle_t dev_hdl;
  46. } msc_client_obj_t;
  47. static void msc_client_event_cb(const usb_host_client_event_msg_t *event_msg, void *arg)
  48. {
  49. msc_client_obj_t *msc_obj = (msc_client_obj_t *)arg;
  50. switch (event_msg->event) {
  51. case USB_HOST_CLIENT_EVENT_NEW_DEV:
  52. TEST_ASSERT_EQUAL(TEST_STAGE_WAIT_CONN, msc_obj->cur_stage);
  53. msc_obj->next_stage = TEST_STAGE_DEV_OPEN;
  54. msc_obj->dev_addr_to_open = event_msg->new_dev.address;
  55. break;
  56. default:
  57. abort(); //Should never occur in this test
  58. break;
  59. }
  60. }
  61. void msc_client_async_enum_task(void *arg)
  62. {
  63. msc_client_obj_t msc_obj;
  64. msc_obj.cur_stage = TEST_STAGE_WAIT_CONN;
  65. msc_obj.next_stage = TEST_STAGE_WAIT_CONN;
  66. msc_obj.client_hdl = NULL;
  67. msc_obj.dev_addr_to_open = 0;
  68. msc_obj.dev_hdl = NULL;
  69. //Register client
  70. usb_host_client_config_t client_config = {
  71. .is_synchronous = false,
  72. .max_num_event_msg = MSC_ASYNC_CLIENT_MAX_EVENT_MSGS,
  73. .async = {
  74. .client_event_callback = msc_client_event_cb,
  75. .callback_arg = (void *)&msc_obj,
  76. },
  77. };
  78. TEST_ASSERT_EQUAL(ESP_OK, usb_host_client_register(&client_config, &msc_obj.client_hdl));
  79. //Wait to be started by main thread
  80. ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
  81. ESP_LOGD(MSC_CLIENT_TAG, "Starting");
  82. bool exit_loop = false;
  83. bool skip_event_handling = false;
  84. int enum_iter = 0;
  85. while (!exit_loop) {
  86. if (!skip_event_handling) {
  87. TEST_ASSERT_EQUAL(ESP_OK, usb_host_client_handle_events(msc_obj.client_hdl, portMAX_DELAY));
  88. }
  89. skip_event_handling = false;
  90. if (msc_obj.cur_stage == msc_obj.next_stage) {
  91. continue;
  92. }
  93. msc_obj.cur_stage = msc_obj.next_stage;
  94. switch (msc_obj.cur_stage) {
  95. case TEST_STAGE_WAIT_CONN: {
  96. //Wait for connection, nothing to do
  97. break;
  98. }
  99. case TEST_STAGE_DEV_OPEN: {
  100. ESP_LOGD(MSC_CLIENT_TAG, "Open");
  101. //Open the device
  102. TEST_ASSERT_EQUAL(ESP_OK, usb_host_device_open(msc_obj.client_hdl, msc_obj.dev_addr_to_open, &msc_obj.dev_hdl));
  103. msc_obj.next_stage = TEST_STAGE_CHECK_DEV_DESC;
  104. skip_event_handling = true; //Need to execute TEST_STAGE_CHECK_DEV_DESC
  105. break;
  106. }
  107. case TEST_STAGE_CHECK_DEV_DESC: {
  108. //Check the device descriptor
  109. const usb_device_desc_t *device_desc;
  110. const usb_device_desc_t *device_desc_ref = (const usb_device_desc_t *)mock_msc_scsi_dev_desc;
  111. TEST_ASSERT_EQUAL(ESP_OK, usb_host_get_device_descriptor(msc_obj.dev_hdl, &device_desc));
  112. TEST_ASSERT_EQUAL(device_desc_ref->bLength, device_desc->bLength);
  113. TEST_ASSERT_EQUAL(0, memcmp(device_desc_ref, device_desc, device_desc_ref->bLength));
  114. msc_obj.next_stage = TEST_STAGE_CHECK_CONFIG_DESC;
  115. skip_event_handling = true; //Need to execute TEST_STAGE_CHECK_CONFIG_DESC
  116. break;
  117. }
  118. case TEST_STAGE_CHECK_CONFIG_DESC: {
  119. //Check the configuration descriptor
  120. const usb_config_desc_t *config_desc;
  121. const usb_config_desc_t *config_desc_ref = (const usb_config_desc_t *)mock_msc_scsi_config_desc;
  122. TEST_ASSERT_EQUAL(ESP_OK, usb_host_get_active_config_descriptor(msc_obj.dev_hdl, &config_desc));
  123. TEST_ASSERT_EQUAL(config_desc_ref->wTotalLength, config_desc->wTotalLength);
  124. TEST_ASSERT_EQUAL(0, memcmp(config_desc_ref, config_desc, config_desc_ref->wTotalLength));
  125. msc_obj.next_stage = TEST_STAGE_CHECK_STR_DESC;
  126. skip_event_handling = true; //Need to execute TEST_STAGE_CHECK_STR_DESC
  127. break;
  128. }
  129. case TEST_STAGE_CHECK_STR_DESC: {
  130. usb_device_info_t dev_info;
  131. TEST_ASSERT_EQUAL(ESP_OK, usb_host_device_info(msc_obj.dev_hdl, &dev_info));
  132. //Check manufacturer string descriptors
  133. const usb_str_desc_t *manu_str_desc_ref = (const usb_str_desc_t *)mock_msc_scsi_str_desc_manu;
  134. const usb_str_desc_t *product_str_desc_ref = (const usb_str_desc_t *)mock_msc_scsi_str_desc_prod;
  135. const usb_str_desc_t *ser_num_str_desc_ref = (const usb_str_desc_t *)mock_msc_scsi_str_desc_ser_num;
  136. TEST_ASSERT_EQUAL(manu_str_desc_ref->bLength, dev_info.str_desc_manufacturer->bLength);
  137. TEST_ASSERT_EQUAL(product_str_desc_ref->bLength, dev_info.str_desc_product->bLength);
  138. TEST_ASSERT_EQUAL(ser_num_str_desc_ref->bLength, dev_info.str_desc_serial_num->bLength);
  139. TEST_ASSERT_EQUAL(0, memcmp(manu_str_desc_ref, dev_info.str_desc_manufacturer , manu_str_desc_ref->bLength));
  140. TEST_ASSERT_EQUAL(0, memcmp(product_str_desc_ref, dev_info.str_desc_product , manu_str_desc_ref->bLength));
  141. TEST_ASSERT_EQUAL(0, memcmp(ser_num_str_desc_ref, dev_info.str_desc_serial_num , manu_str_desc_ref->bLength));
  142. //Get dev info and compare
  143. msc_obj.next_stage = TEST_STAGE_DEV_CLOSE;
  144. skip_event_handling = true; //Need to execute TEST_STAGE_DEV_CLOSE
  145. break;
  146. }
  147. case TEST_STAGE_DEV_CLOSE: {
  148. ESP_LOGD(MSC_CLIENT_TAG, "Close");
  149. TEST_ASSERT_EQUAL(ESP_OK, usb_host_device_close(msc_obj.client_hdl, msc_obj.dev_hdl));
  150. enum_iter++;
  151. if (enum_iter < TEST_ENUM_ITERATIONS) {
  152. //Start the next test iteration by disconnecting the device, then going back to TEST_STAGE_WAIT_CONN stage
  153. test_usb_set_phy_state(false, 0);
  154. test_usb_set_phy_state(true, 0);
  155. msc_obj.next_stage = TEST_STAGE_WAIT_CONN;
  156. skip_event_handling = true; //Need to execute TEST_STAGE_WAIT_CONN
  157. } else {
  158. exit_loop = true;
  159. }
  160. break;
  161. }
  162. default:
  163. abort();
  164. break;
  165. }
  166. }
  167. //Free transfers and deregister the client
  168. TEST_ASSERT_EQUAL(ESP_OK, usb_host_client_deregister(msc_obj.client_hdl));
  169. ESP_LOGD(MSC_CLIENT_TAG, "Done");
  170. vTaskDelete(NULL);
  171. }