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 disconnection 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     - Allocate IN and OUT transfer objects for MSC SCSI transfers
29     - Trigger a single MSC SCSI transfer
30         - Split the data stage into multiple transfers (so that the endpoint multiple queued up transfers)
31         - Cause a disconnection mid-way through the data stage
32     - All of the transfers should be automatically deqeueud
33     - Then a USB_HOST_CLIENT_EVENT_DEV_GONE event should occur afterwards
34     - Free transfer objects
35     - Close device
36     - Deregister MSC client
37 */
38 
39 #define TEST_DCONN_ITERATIONS   3
40 
41 typedef enum {
42     TEST_STAGE_WAIT_CONN,
43     TEST_STAGE_DEV_OPEN,
44     TEST_STAGE_MSC_RESET,
45     TEST_STAGE_MSC_CBW,
46     TEST_STAGE_MSC_DATA_DCONN,
47     TEST_STAGE_DEV_CLOSE,
48 } test_stage_t;
49 
50 typedef struct {
51     msc_client_test_param_t test_param;
52     test_stage_t cur_stage;
53     test_stage_t next_stage;
54     uint8_t dev_addr_to_open;
55     usb_host_client_handle_t client_hdl;
56     usb_device_handle_t dev_hdl;
57     int num_data_transfers;
58     int event_count;
59 } msc_client_obj_t;
60 
msc_reset_cbw_transfer_cb(usb_transfer_t * transfer)61 static void msc_reset_cbw_transfer_cb(usb_transfer_t *transfer)
62 {
63     msc_client_obj_t *msc_obj = (msc_client_obj_t *)transfer->context;
64     //We expect the reset and CBW transfers to complete with no issues
65     TEST_ASSERT_EQUAL(USB_TRANSFER_STATUS_COMPLETED, transfer->status);
66     TEST_ASSERT_EQUAL(transfer->num_bytes, transfer->actual_num_bytes);
67     switch (msc_obj->cur_stage) {
68         case TEST_STAGE_MSC_RESET:
69             msc_obj->next_stage = TEST_STAGE_MSC_CBW;
70             break;
71         case TEST_STAGE_MSC_CBW:
72             msc_obj->next_stage = TEST_STAGE_MSC_DATA_DCONN;
73             break;
74         default:
75             abort();
76             break;
77     }
78 }
79 
msc_data_transfer_cb(usb_transfer_t * transfer)80 static void msc_data_transfer_cb(usb_transfer_t *transfer)
81 {
82     //The data stage should have either completed, or failed due to the disconnection.
83     TEST_ASSERT(transfer->status == USB_TRANSFER_STATUS_COMPLETED || transfer->status == USB_TRANSFER_STATUS_NO_DEVICE);
84     if (transfer->status == USB_TRANSFER_STATUS_COMPLETED) {
85         TEST_ASSERT_EQUAL(transfer->num_bytes, transfer->actual_num_bytes);
86     } else {
87         TEST_ASSERT_EQUAL(0, transfer->actual_num_bytes);
88     }
89     msc_client_obj_t *msc_obj = (msc_client_obj_t *)transfer->context;
90     msc_obj->event_count++;
91     //If all transfers dequeued and device gone event occurred. Go to next stage
92     if (msc_obj->event_count >= msc_obj->num_data_transfers + 1) {
93         msc_obj->next_stage = TEST_STAGE_DEV_CLOSE;
94     }
95 }
96 
msc_client_event_cb(const usb_host_client_event_msg_t * event_msg,void * arg)97 static void msc_client_event_cb(const usb_host_client_event_msg_t *event_msg, void *arg)
98 {
99     msc_client_obj_t *msc_obj = (msc_client_obj_t *)arg;
100     switch (event_msg->event) {
101         case USB_HOST_CLIENT_EVENT_NEW_DEV:
102             TEST_ASSERT_EQUAL(TEST_STAGE_WAIT_CONN, msc_obj->cur_stage);
103             msc_obj->next_stage = TEST_STAGE_DEV_OPEN;
104             msc_obj->dev_addr_to_open = event_msg->new_dev.address;
105             break;
106         case USB_HOST_CLIENT_EVENT_DEV_GONE:
107             msc_obj->event_count++;
108             //If all transfers dequeued and device gone event occurred. Go to next stage
109             if (msc_obj->event_count >= msc_obj->num_data_transfers + 1) {
110                 msc_obj->next_stage = TEST_STAGE_DEV_CLOSE;
111             }
112             break;
113         default:
114             abort();    //Should never occur in this test
115             break;
116     }
117 }
118 
msc_client_async_dconn_task(void * arg)119 void msc_client_async_dconn_task(void *arg)
120 {
121     msc_client_obj_t msc_obj;
122     memcpy(&msc_obj.test_param, arg, sizeof(msc_client_test_param_t));
123     msc_obj.cur_stage = TEST_STAGE_WAIT_CONN;
124     msc_obj.next_stage = TEST_STAGE_WAIT_CONN;
125     msc_obj.dev_addr_to_open = 0;
126     msc_obj.client_hdl = NULL;
127     msc_obj.dev_hdl = NULL;
128     msc_obj.num_data_transfers = msc_obj.test_param.num_sectors_per_xfer / MOCK_MSC_SCSI_SECTOR_SIZE;
129     msc_obj.event_count = 0;
130 
131     //Register client
132     usb_host_client_config_t client_config = {
133         .is_synchronous = false,
134         .max_num_event_msg = MSC_ASYNC_CLIENT_MAX_EVENT_MSGS,
135         .async = {
136             .client_event_callback = msc_client_event_cb,
137             .callback_arg = (void *)&msc_obj,
138         },
139     };
140     TEST_ASSERT_EQUAL(ESP_OK, usb_host_client_register(&client_config, &msc_obj.client_hdl));
141 
142     //Allocate transfers
143     usb_transfer_t *xfer_out;   //Must be large enough to contain CBW and MSC reset control transfer
144     usb_transfer_t *xfer_in[msc_obj.num_data_transfers];   //We manually split the data stage into multiple transfers
145     size_t xfer_out_size = MAX(sizeof(mock_msc_bulk_cbw_t), sizeof(usb_setup_packet_t));
146     size_t xfer_in_size = MOCK_MSC_SCSI_SECTOR_SIZE;
147     TEST_ASSERT_EQUAL(ESP_OK, usb_host_transfer_alloc(xfer_out_size, 0, &xfer_out));
148     xfer_out->context = (void *)&msc_obj;
149     for (int i = 0; i < msc_obj.num_data_transfers; i++) {
150         TEST_ASSERT_EQUAL(ESP_OK, usb_host_transfer_alloc(xfer_in_size, 0, &xfer_in[i]));
151         xfer_in[i]->context = (void *)&msc_obj;
152     }
153 
154     //Wait to be started by main thread
155     ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
156     ESP_LOGD(MSC_CLIENT_TAG, "Starting");
157 
158     bool exit_loop = false;
159     bool skip_event_handling = false;
160     int dconn_iter = 0;
161     while (!exit_loop) {
162         if (!skip_event_handling) {
163             TEST_ASSERT_EQUAL(ESP_OK, usb_host_client_handle_events(msc_obj.client_hdl, portMAX_DELAY));
164         }
165         skip_event_handling = false;
166         if (msc_obj.cur_stage == msc_obj.next_stage) {
167             continue;
168         }
169         msc_obj.cur_stage = msc_obj.next_stage;
170 
171         switch (msc_obj.cur_stage) {
172             case TEST_STAGE_WAIT_CONN: {
173                 //Nothing to do while waiting for connection
174                 break;
175             }
176             case TEST_STAGE_DEV_OPEN: {
177                 ESP_LOGD(MSC_CLIENT_TAG, "Open");
178                 //Open the device
179                 TEST_ASSERT_EQUAL(ESP_OK, usb_host_device_open(msc_obj.client_hdl, msc_obj.dev_addr_to_open, &msc_obj.dev_hdl));
180                 //Target our transfers to the device
181                 xfer_out->device_handle = msc_obj.dev_hdl;
182                 xfer_out->callback = msc_reset_cbw_transfer_cb;
183                 for (int i = 0; i < msc_obj.num_data_transfers; i++) {
184                     xfer_in[i]->device_handle = msc_obj.dev_hdl;
185                     xfer_in[i]->callback = msc_data_transfer_cb;
186                 }
187                 //Check the VID/PID of the opened device
188                 const usb_device_desc_t *device_desc;
189                 TEST_ASSERT_EQUAL(ESP_OK, usb_host_get_device_descriptor(msc_obj.dev_hdl, &device_desc));
190                 TEST_ASSERT_EQUAL(msc_obj.test_param.idVendor, device_desc->idVendor);
191                 TEST_ASSERT_EQUAL(msc_obj.test_param.idProduct, device_desc->idProduct);
192                 //Claim the MSC interface
193                 TEST_ASSERT_EQUAL(ESP_OK, usb_host_interface_claim(msc_obj.client_hdl, msc_obj.dev_hdl, MOCK_MSC_SCSI_INTF_NUMBER, MOCK_MSC_SCSI_INTF_ALT_SETTING));
194                 msc_obj.next_stage = TEST_STAGE_MSC_RESET;
195                 skip_event_handling = true; //Need to execute TEST_STAGE_MSC_RESET
196                 break;
197             }
198             case TEST_STAGE_MSC_RESET: {
199                 ESP_LOGD(MSC_CLIENT_TAG, "MSC Reset");
200                 //Send an MSC SCSI interface reset
201                 MOCK_MSC_SCSI_REQ_INIT_RESET((usb_setup_packet_t *)xfer_out->data_buffer, MOCK_MSC_SCSI_INTF_NUMBER);
202                 xfer_out->num_bytes = sizeof(usb_setup_packet_t);
203                 xfer_out->bEndpointAddress = 0;
204                 TEST_ASSERT_EQUAL(ESP_OK, usb_host_transfer_submit_control(msc_obj.client_hdl, xfer_out));
205                 //Next stage set from transfer callback
206                 break;
207             }
208             case TEST_STAGE_MSC_CBW: {
209                 ESP_LOGD(MSC_CLIENT_TAG, "CBW");
210                 mock_msc_scsi_init_cbw((mock_msc_bulk_cbw_t *)xfer_out->data_buffer, true, 0, msc_obj.test_param.num_sectors_per_xfer, msc_obj.test_param.msc_scsi_xfer_tag);
211                 xfer_out->num_bytes = sizeof(mock_msc_bulk_cbw_t);
212                 xfer_out->bEndpointAddress = MOCK_MSC_SCSI_BULK_OUT_EP_ADDR;
213                 TEST_ASSERT_EQUAL(ESP_OK, usb_host_transfer_submit(xfer_out));
214                 //Next stage set from transfer callback
215                 break;
216             }
217             case TEST_STAGE_MSC_DATA_DCONN: {
218                 ESP_LOGD(MSC_CLIENT_TAG, "Data and disconnect");
219                 //Setup the Data IN transfers
220                 for (int i = 0; i < msc_obj.num_data_transfers; i++) {
221                     xfer_in[i]->num_bytes = usb_round_up_to_mps(MOCK_MSC_SCSI_SECTOR_SIZE, MOCK_MSC_SCSI_BULK_EP_MPS);
222                     xfer_in[i]->bEndpointAddress = MOCK_MSC_SCSI_BULK_IN_EP_ADDR;
223                 }
224                 //Submit those transfers
225                 for (int i = 0; i < msc_obj.num_data_transfers; i++) {
226                     TEST_ASSERT_EQUAL(ESP_OK, usb_host_transfer_submit(xfer_in[i]));
227                 }
228                 //Trigger a disconnect
229                 test_usb_set_phy_state(false, 0);
230                 //Next stage set from transfer callback
231                 break;
232             }
233             case TEST_STAGE_DEV_CLOSE: {
234                 ESP_LOGD(MSC_CLIENT_TAG, "Close");
235                 TEST_ASSERT_EQUAL(ESP_OK, usb_host_interface_release(msc_obj.client_hdl, msc_obj.dev_hdl, MOCK_MSC_SCSI_INTF_NUMBER));
236                 TEST_ASSERT_EQUAL(ESP_OK, usb_host_device_close(msc_obj.client_hdl, msc_obj.dev_hdl));
237                 dconn_iter++;
238                 if (dconn_iter < TEST_DCONN_ITERATIONS) {
239                     //Start the next test iteration by going back to TEST_STAGE_WAIT_CONN and reenabling connections
240                     msc_obj.next_stage = TEST_STAGE_WAIT_CONN;
241                     skip_event_handling = true; //Need to execute TEST_STAGE_WAIT_CONN
242                     test_usb_set_phy_state(true, 0);
243                 } else {
244                     exit_loop = true;
245                 }
246                 break;
247             }
248             default:
249                 abort();
250                 break;
251         }
252     }
253     //Free transfers
254     TEST_ASSERT_EQUAL(ESP_OK, usb_host_transfer_free(xfer_out));
255     for (int i = 0; i < msc_obj.num_data_transfers; i++) {
256         TEST_ASSERT_EQUAL(ESP_OK, usb_host_transfer_free(xfer_in[i]));
257     }
258     //Deregister the client
259     TEST_ASSERT_EQUAL(ESP_OK, usb_host_client_deregister(msc_obj.client_hdl));
260     ESP_LOGD(MSC_CLIENT_TAG, "Done");
261     vTaskDelete(NULL);
262 }
263