1 /*
2  * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <stdio.h>
8 #include <stdbool.h>
9 #include "freertos/FreeRTOS.h"
10 #include "freertos/semphr.h"
11 #include "unity.h"
12 #include "test_utils.h"
13 #include "test_usb_mock_classes.h"
14 #include "test_hcd_common.h"
15 
16 // --------------------------------------------------- Test Cases ------------------------------------------------------
17 
mock_msc_reset_req(hcd_pipe_handle_t default_pipe)18 static void mock_msc_reset_req(hcd_pipe_handle_t default_pipe)
19 {
20     //Create URB
21     urb_t *urb = test_hcd_alloc_urb(0, sizeof(usb_setup_packet_t));
22     usb_setup_packet_t *setup_pkt = (usb_setup_packet_t *)urb->transfer.data_buffer;
23     MOCK_MSC_SCSI_REQ_INIT_RESET(setup_pkt, MOCK_MSC_SCSI_INTF_NUMBER);
24     urb->transfer.num_bytes = sizeof(usb_setup_packet_t);
25     //Enqueue, wait, dequeue, and check URB
26     TEST_ASSERT_EQUAL(ESP_OK, hcd_urb_enqueue(default_pipe, urb));
27     test_hcd_expect_pipe_event(default_pipe, HCD_PIPE_EVENT_URB_DONE);
28     TEST_ASSERT_EQUAL(urb, hcd_urb_dequeue(default_pipe));
29     TEST_ASSERT_EQUAL(USB_TRANSFER_STATUS_COMPLETED, urb->transfer.status);
30     //Free URB
31     test_hcd_free_urb(urb);
32 }
33 
34 /*
35 Test HCD bulk pipe URBs
36 
37 Purpose:
38     - Test that a bulk pipe can be created
39     - URBs can be created and enqueued to the bulk pipe pipe
40     - Bulk pipe returns HCD_PIPE_EVENT_URB_DONE for completed URBs
41     - Test utilizes a bare bones (i.e., mock) MSC class using SCSI commands
42 
43 Procedure:
44     - Setup HCD and wait for connection
45     - Allocate default pipe and enumerate the device
46     - Allocate separate URBS for CBW, Data, and CSW transfers of the MSC class
47     - Read TEST_NUM_SECTORS_TOTAL number of sectors for the mass storage device
48     - Expect HCD_PIPE_EVENT_URB_DONE for each URB
49     - Deallocate URBs
50     - Teardown
51 */
52 
53 #define TEST_NUM_SECTORS_TOTAL          10
54 #define TEST_NUM_SECTORS_PER_XFER       2
55 
56 TEST_CASE("Test HCD bulk pipe URBs", "[hcd][ignore]")
57 {
58     hcd_port_handle_t port_hdl = test_hcd_setup();  //Setup the HCD and port
59     usb_speed_t port_speed = test_hcd_wait_for_conn(port_hdl);  //Trigger a connection
60     vTaskDelay(pdMS_TO_TICKS(100)); //Short delay send of SOF (for FS) or EOPs (for LS)
61 
62     //Enumerate and reset MSC SCSI device
63     hcd_pipe_handle_t default_pipe = test_hcd_pipe_alloc(port_hdl, NULL, 0, port_speed); //Create a default pipe (using a NULL EP descriptor)
64     uint8_t dev_addr = test_hcd_enum_device(default_pipe);
65     mock_msc_reset_req(default_pipe);
66 
67     //Create BULK IN and BULK OUT pipes for SCSI
68     hcd_pipe_handle_t bulk_out_pipe = test_hcd_pipe_alloc(port_hdl, &mock_msc_scsi_bulk_out_ep_desc, dev_addr, port_speed);
69     hcd_pipe_handle_t bulk_in_pipe = test_hcd_pipe_alloc(port_hdl, &mock_msc_scsi_bulk_in_ep_desc, dev_addr, port_speed);
70     //Create URBs for CBW, Data, and CSW transport. IN Buffer sizes are rounded up to nearest MPS
71     urb_t *urb_cbw = test_hcd_alloc_urb(0, sizeof(mock_msc_bulk_cbw_t));
72     urb_t *urb_data = test_hcd_alloc_urb(0, TEST_NUM_SECTORS_PER_XFER * MOCK_MSC_SCSI_SECTOR_SIZE);
73     urb_t *urb_csw = test_hcd_alloc_urb(0, sizeof(mock_msc_bulk_csw_t) + (mock_msc_scsi_bulk_in_ep_desc.wMaxPacketSize - (sizeof(mock_msc_bulk_csw_t) % mock_msc_scsi_bulk_in_ep_desc.wMaxPacketSize)));
74     urb_cbw->transfer.num_bytes = sizeof(mock_msc_bulk_cbw_t);
75     urb_data->transfer.num_bytes = TEST_NUM_SECTORS_PER_XFER * MOCK_MSC_SCSI_SECTOR_SIZE;
76     urb_csw->transfer.num_bytes = sizeof(mock_msc_bulk_csw_t) + (mock_msc_scsi_bulk_in_ep_desc.wMaxPacketSize - (sizeof(mock_msc_bulk_csw_t) % mock_msc_scsi_bulk_in_ep_desc.wMaxPacketSize));
77 
78     for (int block_num = 0; block_num < TEST_NUM_SECTORS_TOTAL; block_num += TEST_NUM_SECTORS_PER_XFER) {
79         //Initialize CBW URB, then send it on the BULK OUT pipe
80         mock_msc_scsi_init_cbw((mock_msc_bulk_cbw_t *)urb_cbw->transfer.data_buffer, true, block_num, TEST_NUM_SECTORS_PER_XFER, 0xAAAAAAAA);
81         TEST_ASSERT_EQUAL(ESP_OK, hcd_urb_enqueue(bulk_out_pipe, urb_cbw));
82         test_hcd_expect_pipe_event(bulk_out_pipe, HCD_PIPE_EVENT_URB_DONE);
83         TEST_ASSERT_EQUAL(urb_cbw, hcd_urb_dequeue(bulk_out_pipe));
84         TEST_ASSERT_EQUAL(USB_TRANSFER_STATUS_COMPLETED, urb_cbw->transfer.status);
85         //Read data through BULK IN pipe
86         TEST_ASSERT_EQUAL(ESP_OK, hcd_urb_enqueue(bulk_in_pipe, urb_data));
87         test_hcd_expect_pipe_event(bulk_in_pipe, HCD_PIPE_EVENT_URB_DONE);
88         TEST_ASSERT_EQUAL(urb_data, hcd_urb_dequeue(bulk_in_pipe));
89         TEST_ASSERT_EQUAL(USB_TRANSFER_STATUS_COMPLETED, urb_data->transfer.status);
90         //Read the CSW through BULK IN pipe
91         TEST_ASSERT_EQUAL(ESP_OK, hcd_urb_enqueue(bulk_in_pipe, urb_csw));
92         test_hcd_expect_pipe_event(bulk_in_pipe, HCD_PIPE_EVENT_URB_DONE);
93         TEST_ASSERT_EQUAL(urb_csw, hcd_urb_dequeue(bulk_in_pipe));
94         TEST_ASSERT_EQUAL(USB_TRANSFER_STATUS_COMPLETED, urb_data->transfer.status);
95         TEST_ASSERT_EQUAL(sizeof(mock_msc_bulk_csw_t), urb_csw->transfer.actual_num_bytes);
96         TEST_ASSERT_EQUAL(true, mock_msc_scsi_check_csw((mock_msc_bulk_csw_t *)urb_csw->transfer.data_buffer, 0xAAAAAAAA));
97         //Print the read data
98         printf("Block %d to %d:\n", block_num, block_num + TEST_NUM_SECTORS_PER_XFER);
99         for (int i = 0; i < urb_data->transfer.actual_num_bytes; i++) {
100             printf("0x%02x,", ((char *)urb_data->transfer.data_buffer)[i]);
101         }
102         printf("\n\n");
103     }
104 
105     test_hcd_free_urb(urb_cbw);
106     test_hcd_free_urb(urb_data);
107     test_hcd_free_urb(urb_csw);
108     test_hcd_pipe_free(bulk_out_pipe);
109     test_hcd_pipe_free(bulk_in_pipe);
110     test_hcd_pipe_free(default_pipe);
111     //Cleanup
112     test_hcd_wait_for_disconn(port_hdl, false);
113     test_hcd_teardown(port_hdl);
114 }
115