ctrl_client_async_seq.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdint.h>
  7. #include <string.h>
  8. #include "freertos/FreeRTOS.h"
  9. #include "freertos/task.h"
  10. #include "esp_err.h"
  11. #include "esp_log.h"
  12. #include "test_usb_common.h"
  13. #include "ctrl_client.h"
  14. #include "usb/usb_host.h"
  15. #include "unity.h"
  16. #include "test_utils.h"
  17. /*
  18. Implementation of a control transfer client used for USB Host Tests.
  19. - Implemented using sequential call patterns, meaning:
  20. - The entire client is contained within a single task
  21. - All API calls and callbacks are run sequentially
  22. - No critical sections required since everything is sequential
  23. - The control transfer client will:
  24. - Register itself as a client
  25. - Receive USB_HOST_CLIENT_EVENT_NEW_DEV event message, and open the device
  26. - Allocate multiple transfer objects
  27. - Submit a number of control transfers (get configuration descriptor requests)
  28. - Free transfer objects
  29. - Close the device
  30. - Deregister control client
  31. */
  32. #define CTRL_CLIENT_MAX_EVENT_MSGS 5
  33. #define NUM_TRANSFER_OBJ 3
  34. #define MAX_TRANSFER_BYTES 256
  35. const char *CTRL_CLIENT_TAG = "Ctrl Client";
  36. typedef enum {
  37. TEST_STAGE_WAIT_CONN,
  38. TEST_STAGE_DEV_OPEN,
  39. TEST_STAGE_CTRL_XFER,
  40. TEST_STAGE_CTRL_XFER_WAIT,
  41. TEST_STAGE_DEV_CLOSE,
  42. } test_stage_t;
  43. typedef struct {
  44. ctrl_client_test_param_t test_param;
  45. test_stage_t cur_stage;
  46. test_stage_t next_stage;
  47. uint8_t num_xfer_done;
  48. uint8_t num_xfer_sent;
  49. uint8_t dev_addr_to_open;
  50. usb_host_client_handle_t client_hdl;
  51. usb_device_handle_t dev_hdl;
  52. const usb_config_desc_t *config_desc_cached;
  53. } ctrl_client_obj_t;
  54. static void ctrl_transfer_cb(usb_transfer_t *transfer)
  55. {
  56. ctrl_client_obj_t *ctrl_obj = (ctrl_client_obj_t *)transfer->context;
  57. //Check the completed control transfer
  58. TEST_ASSERT_EQUAL(USB_TRANSFER_STATUS_COMPLETED, transfer->status);
  59. TEST_ASSERT_EQUAL(ctrl_obj->config_desc_cached->wTotalLength, transfer->actual_num_bytes - sizeof(usb_setup_packet_t));
  60. ctrl_obj->num_xfer_done++;
  61. if (ctrl_obj->num_xfer_sent < ctrl_obj->test_param.num_ctrl_xfer_to_send) {
  62. ctrl_obj->next_stage = TEST_STAGE_CTRL_XFER;
  63. } else if (ctrl_obj->num_xfer_done == ctrl_obj->test_param.num_ctrl_xfer_to_send) {
  64. ctrl_obj->next_stage = TEST_STAGE_DEV_CLOSE;
  65. }
  66. }
  67. static void ctrl_client_event_cb(const usb_host_client_event_msg_t *event_msg, void *arg)
  68. {
  69. ctrl_client_obj_t *ctrl_obj = (ctrl_client_obj_t *)arg;
  70. switch (event_msg->event) {
  71. case USB_HOST_CLIENT_EVENT_NEW_DEV:
  72. TEST_ASSERT_EQUAL(TEST_STAGE_WAIT_CONN, ctrl_obj->cur_stage);
  73. ctrl_obj->next_stage = TEST_STAGE_DEV_OPEN;
  74. ctrl_obj->dev_addr_to_open = event_msg->new_dev.address;
  75. break;
  76. default:
  77. abort(); //Should never occur in this test
  78. break;
  79. }
  80. }
  81. void ctrl_client_async_seq_task(void *arg)
  82. {
  83. ctrl_client_obj_t ctrl_obj = {0};
  84. memcpy(&ctrl_obj.test_param, arg, sizeof(ctrl_client_test_param_t));
  85. ctrl_obj.cur_stage = TEST_STAGE_WAIT_CONN;
  86. ctrl_obj.next_stage = TEST_STAGE_WAIT_CONN;
  87. //Register client
  88. usb_host_client_config_t client_config = {
  89. .is_synchronous = false,
  90. .max_num_event_msg = CTRL_CLIENT_MAX_EVENT_MSGS,
  91. .async = {
  92. .client_event_callback = ctrl_client_event_cb,
  93. .callback_arg = (void *)&ctrl_obj,
  94. },
  95. };
  96. TEST_ASSERT_EQUAL(ESP_OK, usb_host_client_register(&client_config, &ctrl_obj.client_hdl));
  97. //Allocate transfers
  98. usb_transfer_t *ctrl_xfer[NUM_TRANSFER_OBJ] = {NULL};
  99. for (int i = 0; i < NUM_TRANSFER_OBJ; i++) {
  100. TEST_ASSERT_EQUAL(ESP_OK, usb_host_transfer_alloc(sizeof(usb_setup_packet_t) + MAX_TRANSFER_BYTES, 0, &ctrl_xfer[i]));
  101. ctrl_xfer[i]->callback = ctrl_transfer_cb;
  102. ctrl_xfer[i]->context = (void *)&ctrl_obj;
  103. }
  104. //Wait to be started by main thread
  105. ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
  106. ESP_LOGD(CTRL_CLIENT_TAG, "Starting");
  107. bool exit_loop = false;
  108. bool skip_event_handling = false;
  109. while (!exit_loop) {
  110. if (!skip_event_handling) {
  111. TEST_ASSERT_EQUAL(ESP_OK, usb_host_client_handle_events(ctrl_obj.client_hdl, portMAX_DELAY));
  112. }
  113. skip_event_handling = false;
  114. if (ctrl_obj.cur_stage == ctrl_obj.next_stage) {
  115. continue;
  116. }
  117. ctrl_obj.cur_stage = ctrl_obj.next_stage;
  118. switch (ctrl_obj.next_stage) {
  119. case TEST_STAGE_DEV_OPEN: {
  120. ESP_LOGD(CTRL_CLIENT_TAG, "Open");
  121. //Open the device
  122. TEST_ASSERT_EQUAL(ESP_OK, usb_host_device_open(ctrl_obj.client_hdl, ctrl_obj.dev_addr_to_open, &ctrl_obj.dev_hdl));
  123. //Target our transfers to the device
  124. for (int i = 0; i < NUM_TRANSFER_OBJ; i++) {
  125. ctrl_xfer[i]->device_handle = ctrl_obj.dev_hdl;
  126. }
  127. //Check the VID/PID of the opened device
  128. const usb_device_desc_t *device_desc;
  129. TEST_ASSERT_EQUAL(ESP_OK, usb_host_get_device_descriptor(ctrl_obj.dev_hdl, &device_desc));
  130. TEST_ASSERT_EQUAL(ctrl_obj.test_param.idVendor, device_desc->idVendor);
  131. TEST_ASSERT_EQUAL(ctrl_obj.test_param.idProduct, device_desc->idProduct);
  132. //Cache the active configuration descriptor for later comparison
  133. TEST_ASSERT_EQUAL(ESP_OK, usb_host_get_active_config_descriptor(ctrl_obj.dev_hdl, &ctrl_obj.config_desc_cached));
  134. ctrl_obj.next_stage = TEST_STAGE_CTRL_XFER;
  135. skip_event_handling = true;
  136. break;
  137. }
  138. case TEST_STAGE_CTRL_XFER: {
  139. ESP_LOGD(CTRL_CLIENT_TAG, "Transfer");
  140. //Send a control transfer to get the device's configuration descriptor
  141. usb_transfer_t *transfer = ctrl_xfer[ctrl_obj.num_xfer_sent % NUM_TRANSFER_OBJ];
  142. USB_SETUP_PACKET_INIT_GET_CONFIG_DESC((usb_setup_packet_t *)transfer->data_buffer, 0, MAX_TRANSFER_BYTES);
  143. transfer->num_bytes = sizeof(usb_setup_packet_t) + MAX_TRANSFER_BYTES;
  144. transfer->bEndpointAddress = 0x80;
  145. TEST_ASSERT_EQUAL(ESP_OK, usb_host_transfer_submit_control(ctrl_obj.client_hdl, transfer));
  146. ctrl_obj.num_xfer_sent++;
  147. ctrl_obj.next_stage = TEST_STAGE_CTRL_XFER_WAIT;
  148. skip_event_handling = true;
  149. break;
  150. }
  151. case TEST_STAGE_CTRL_XFER_WAIT: {
  152. //Nothing to do but wait
  153. break;
  154. }
  155. case TEST_STAGE_DEV_CLOSE: {
  156. ESP_LOGD(CTRL_CLIENT_TAG, "Close");
  157. TEST_ASSERT_EQUAL(ESP_OK, usb_host_device_close(ctrl_obj.client_hdl, ctrl_obj.dev_hdl));
  158. exit_loop = true;
  159. break;
  160. }
  161. default:
  162. abort();
  163. break;
  164. }
  165. }
  166. //Free transfers and deregister client
  167. for (int i = 0; i < NUM_TRANSFER_OBJ; i++) {
  168. TEST_ASSERT_EQUAL(ESP_OK, usb_host_transfer_free(ctrl_xfer[i]));
  169. }
  170. TEST_ASSERT_EQUAL(ESP_OK, usb_host_client_deregister(ctrl_obj.client_hdl));
  171. ESP_LOGD(CTRL_CLIENT_TAG, "Done");
  172. vTaskDelete(NULL);
  173. }