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