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 <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_common.h"
16 #include "test_usb_mock_classes.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 MSC client used for USB Host Tests
24
25 - Implemented using sequential call patterns, meaning:
26 - The entire client is contained within a single task
27 - All API calls and callbacks are run sequentially
28 - No critical sections required since everything is sequential
29 - The MSC client will:
30 - Register itself as a client
31 - Receive USB_HOST_CLIENT_EVENT_NEW_DEV event message, and open the device
32 - Allocate IN and OUT transfer objects for MSC SCSI transfers
33 - Iterate through multiple MSC SCSI block reads
34 - Free transfer objects
35 - Close device
36 - Deregister MSC client
37 */
38
39 typedef enum {
40 TEST_STAGE_WAIT_CONN,
41 TEST_STAGE_DEV_OPEN,
42 TEST_STAGE_MSC_RESET,
43 TEST_STAGE_MSC_CBW,
44 TEST_STAGE_MSC_DATA,
45 TEST_STAGE_MSC_CSW,
46 TEST_STAGE_DEV_CLOSE,
47 } test_stage_t;
48
49 typedef struct {
50 msc_client_test_param_t test_param;
51 test_stage_t cur_stage;
52 test_stage_t next_stage;
53 uint8_t dev_addr_to_open;
54 usb_host_client_handle_t client_hdl;
55 usb_device_handle_t dev_hdl;
56 int num_sectors_read;
57 } msc_client_obj_t;
58
msc_transfer_cb(usb_transfer_t * transfer)59 static void msc_transfer_cb(usb_transfer_t *transfer)
60 {
61 msc_client_obj_t *msc_obj = (msc_client_obj_t *)transfer->context;
62 switch (msc_obj->cur_stage) {
63 case TEST_STAGE_MSC_RESET: {
64 //Check MSC SCSI interface reset
65 TEST_ASSERT_EQUAL(USB_TRANSFER_STATUS_COMPLETED, transfer->status);
66 TEST_ASSERT_EQUAL(transfer->num_bytes, transfer->actual_num_bytes);
67 msc_obj->next_stage = TEST_STAGE_MSC_CBW;
68 break;
69 }
70 case TEST_STAGE_MSC_CBW: {
71 //Check MSC SCSI CBW transfer
72 TEST_ASSERT_EQUAL(USB_TRANSFER_STATUS_COMPLETED, transfer->status);
73 TEST_ASSERT_EQUAL(sizeof(mock_msc_bulk_cbw_t), transfer->actual_num_bytes);
74 msc_obj->next_stage = TEST_STAGE_MSC_DATA;
75 break;
76 }
77 case TEST_STAGE_MSC_DATA: {
78 //Check MSC SCSI data IN transfer
79 TEST_ASSERT_EQUAL(USB_TRANSFER_STATUS_COMPLETED, transfer->status);
80 TEST_ASSERT_EQUAL(MOCK_MSC_SCSI_SECTOR_SIZE * msc_obj->test_param.num_sectors_per_xfer, transfer->actual_num_bytes);
81 msc_obj->next_stage = TEST_STAGE_MSC_CSW;
82 break;
83 }
84 case TEST_STAGE_MSC_CSW: {
85 //Check MSC SCSI CSW transfer
86 TEST_ASSERT_EQUAL(USB_TRANSFER_STATUS_COMPLETED, transfer->status);
87 TEST_ASSERT_EQUAL(true, mock_msc_scsi_check_csw((mock_msc_bulk_csw_t *)transfer->data_buffer, msc_obj->test_param.msc_scsi_xfer_tag));
88 msc_obj->num_sectors_read += msc_obj->test_param.num_sectors_per_xfer;
89 if (msc_obj->num_sectors_read < msc_obj->test_param.num_sectors_to_read) {
90 msc_obj->next_stage = TEST_STAGE_MSC_CBW;
91 } else {
92 msc_obj->next_stage = TEST_STAGE_DEV_CLOSE;
93 }
94 break;
95 }
96 default: {
97 abort();
98 break;
99 }
100 }
101 }
102
msc_client_event_cb(const usb_host_client_event_msg_t * event_msg,void * arg)103 static void msc_client_event_cb(const usb_host_client_event_msg_t *event_msg, void *arg)
104 {
105 msc_client_obj_t *msc_obj = (msc_client_obj_t *)arg;
106 switch (event_msg->event) {
107 case USB_HOST_CLIENT_EVENT_NEW_DEV:
108 TEST_ASSERT_EQUAL(TEST_STAGE_WAIT_CONN, msc_obj->cur_stage);
109 msc_obj->next_stage = TEST_STAGE_DEV_OPEN;
110 msc_obj->dev_addr_to_open = event_msg->new_dev.address;
111 break;
112 default:
113 abort(); //Should never occur in this test
114 break;
115
116 }
117 }
118
msc_client_async_seq_task(void * arg)119 void msc_client_async_seq_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.client_hdl = NULL;
126 msc_obj.dev_addr_to_open = 0;
127 msc_obj.dev_hdl = NULL;
128 msc_obj.num_sectors_read = 0;
129
130 //Register client
131 usb_host_client_config_t client_config = {
132 .is_synchronous = false,
133 .max_num_event_msg = MSC_ASYNC_CLIENT_MAX_EVENT_MSGS,
134 .async = {
135 .client_event_callback = msc_client_event_cb,
136 .callback_arg = (void *)&msc_obj,
137 },
138 };
139 TEST_ASSERT_EQUAL(ESP_OK, usb_host_client_register(&client_config, &msc_obj.client_hdl));
140
141 //Allocate transfers
142 usb_transfer_t *xfer_out = NULL; //Must be large enough to contain CBW and MSC reset control transfer
143 usb_transfer_t *xfer_in = NULL; //Must be large enough to contain CSW and Data
144 size_t out_worst_case_size = MAX(sizeof(mock_msc_bulk_cbw_t), sizeof(usb_setup_packet_t));
145 size_t in_worst_case_size = usb_round_up_to_mps(MAX(MOCK_MSC_SCSI_SECTOR_SIZE * msc_obj.test_param.num_sectors_per_xfer, sizeof(mock_msc_bulk_csw_t)), MOCK_MSC_SCSI_BULK_EP_MPS);
146 TEST_ASSERT_EQUAL(ESP_OK, usb_host_transfer_alloc(out_worst_case_size, 0, &xfer_out));
147 TEST_ASSERT_EQUAL(ESP_OK, usb_host_transfer_alloc(in_worst_case_size, 0, &xfer_in));
148 xfer_out->callback = msc_transfer_cb;
149 xfer_in->callback = msc_transfer_cb;
150 xfer_out->context = (void *)&msc_obj;
151 xfer_in->context = (void *)&msc_obj;
152
153 //Wait to be started by main thread
154 ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
155 ESP_LOGD(MSC_CLIENT_TAG, "Starting");
156
157 bool exit_loop = false;
158 bool skip_event_handling = false;
159 while (!exit_loop) {
160 if (!skip_event_handling) {
161 TEST_ASSERT_EQUAL(ESP_OK, usb_host_client_handle_events(msc_obj.client_hdl, portMAX_DELAY));
162 }
163 skip_event_handling = false;
164 if (msc_obj.cur_stage == msc_obj.next_stage) {
165 continue;
166 }
167 msc_obj.cur_stage = msc_obj.next_stage;
168
169 switch (msc_obj.cur_stage) {
170 case TEST_STAGE_DEV_OPEN: {
171 ESP_LOGD(MSC_CLIENT_TAG, "Open");
172 //Open the device
173 TEST_ASSERT_EQUAL(ESP_OK, usb_host_device_open(msc_obj.client_hdl, msc_obj.dev_addr_to_open, &msc_obj.dev_hdl));
174 //Target our transfers to the device
175 xfer_out->device_handle = msc_obj.dev_hdl;
176 xfer_in->device_handle = msc_obj.dev_hdl;
177 //Check the VID/PID of the opened device
178 const usb_device_desc_t *device_desc;
179 TEST_ASSERT_EQUAL(ESP_OK, usb_host_get_device_descriptor(msc_obj.dev_hdl, &device_desc));
180 TEST_ASSERT_EQUAL(msc_obj.test_param.idVendor, device_desc->idVendor);
181 TEST_ASSERT_EQUAL(msc_obj.test_param.idProduct, device_desc->idProduct);
182 //Claim the MSC interface
183 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));
184 msc_obj.next_stage = TEST_STAGE_MSC_RESET;
185 skip_event_handling = true; //Need to execute TEST_STAGE_MSC_RESET
186 break;
187 }
188 case TEST_STAGE_MSC_RESET: {
189 ESP_LOGD(MSC_CLIENT_TAG, "MSC Reset");
190 //Send an MSC SCSI interface reset
191 MOCK_MSC_SCSI_REQ_INIT_RESET((usb_setup_packet_t *)xfer_out->data_buffer, MOCK_MSC_SCSI_INTF_NUMBER);
192 xfer_out->num_bytes = sizeof(usb_setup_packet_t);
193 xfer_out->bEndpointAddress = 0;
194 TEST_ASSERT_EQUAL(ESP_OK, usb_host_transfer_submit_control(msc_obj.client_hdl, xfer_out));
195 //Next stage set from transfer callback
196 break;
197 }
198 case TEST_STAGE_MSC_CBW: {
199 ESP_LOGD(MSC_CLIENT_TAG, "CBW");
200 mock_msc_scsi_init_cbw((mock_msc_bulk_cbw_t *)xfer_out->data_buffer, true, msc_obj.next_stage, msc_obj.test_param.num_sectors_per_xfer, msc_obj.test_param.msc_scsi_xfer_tag);
201 xfer_out->num_bytes = sizeof(mock_msc_bulk_cbw_t);
202 xfer_out->bEndpointAddress = MOCK_MSC_SCSI_BULK_OUT_EP_ADDR;
203 TEST_ASSERT_EQUAL(ESP_OK, usb_host_transfer_submit(xfer_out));
204 //Next stage set from transfer callback
205 break;
206 }
207 case TEST_STAGE_MSC_DATA: {
208 ESP_LOGD(MSC_CLIENT_TAG, "Data");
209 xfer_in->num_bytes = usb_round_up_to_mps(MOCK_MSC_SCSI_SECTOR_SIZE * msc_obj.test_param.num_sectors_per_xfer, MOCK_MSC_SCSI_BULK_EP_MPS);
210 xfer_in->bEndpointAddress = MOCK_MSC_SCSI_BULK_IN_EP_ADDR;
211 TEST_ASSERT_EQUAL(ESP_OK, usb_host_transfer_submit(xfer_in));
212 //Next stage set from transfer callback
213 break;
214 }
215 case TEST_STAGE_MSC_CSW: {
216 ESP_LOGD(MSC_CLIENT_TAG, "CSW");
217 xfer_in->num_bytes = usb_round_up_to_mps(sizeof(mock_msc_bulk_csw_t), MOCK_MSC_SCSI_BULK_EP_MPS);
218 xfer_in->bEndpointAddress = MOCK_MSC_SCSI_BULK_IN_EP_ADDR;
219 TEST_ASSERT_EQUAL(ESP_OK, usb_host_transfer_submit(xfer_in));
220 //Next stage set from transfer callback
221 break;
222 }
223 case TEST_STAGE_DEV_CLOSE: {
224 ESP_LOGD(MSC_CLIENT_TAG, "Close");
225 TEST_ASSERT_EQUAL(ESP_OK, usb_host_interface_release(msc_obj.client_hdl, msc_obj.dev_hdl, MOCK_MSC_SCSI_INTF_NUMBER));
226 TEST_ASSERT_EQUAL(ESP_OK, usb_host_device_close(msc_obj.client_hdl, msc_obj.dev_hdl));
227 exit_loop = true;
228 break;
229 }
230 default:
231 abort();
232 break;
233 }
234 }
235 //Free transfers and deregister the client
236 TEST_ASSERT_EQUAL(ESP_OK, usb_host_transfer_free(xfer_out));
237 TEST_ASSERT_EQUAL(ESP_OK, usb_host_transfer_free(xfer_in));
238 TEST_ASSERT_EQUAL(ESP_OK, usb_host_client_deregister(msc_obj.client_hdl));
239 ESP_LOGD(MSC_CLIENT_TAG, "Done");
240 vTaskDelete(NULL);
241 }
242