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