refactor(usb): Split test device descriptors from mock class files

Previously, descriptors of the test devices were stored direclty in the mock
device files (e.g., "mock_[hid|msc].[h|c]"). This commit splits out the device
descriptors to separate files (e.g., "dev_[hid|msc].c") along with getter
functions.

Users that want to run the tests locally on a different device simply need to
update the "dev_[hid|msc].c" file for their device.
This commit is contained in:
Darian Leung
2024-05-08 02:56:16 +08:00
committed by BOT
parent 12f07d846b
commit c66f46cb77
24 changed files with 1123 additions and 516 deletions

View File

@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@@ -15,77 +15,18 @@
const char *MSC_CLIENT_TAG = "MSC Client";
const usb_device_desc_t mock_msc_scsi_dev_desc = {
.bLength = USB_DEVICE_DESC_SIZE,
.bDescriptorType = USB_B_DESCRIPTOR_TYPE_DEVICE,
.bcdUSB = MOCK_MSC_SCSI_USB_VERSION,
.bDeviceClass = USB_CLASS_PER_INTERFACE,
.bDeviceSubClass = 0,
.bDeviceProtocol = 0,
.bMaxPacketSize0 = MOCK_MSC_SCSI_DEV_DFLT_EP_MPS,
.idVendor = MOCK_MSC_SCSI_DEV_ID_VENDOR,
.idProduct = MOCK_MSC_SCSI_DEV_ID_PRODUCT,
.bcdDevice = MOCK_MSC_SCSI_DEV_VERSION,
.iManufacturer = 1,
.iProduct = 2,
.iSerialNumber = 3,
.bNumConfigurations = 1,
};
#define MOCK_MSC_SCSI_WTOTALLENGTH (USB_CONFIG_DESC_SIZE + USB_INTF_DESC_SIZE + 2*USB_EP_DESC_SIZE)
static const usb_config_desc_t mock_msc_config_desc = {
.bLength = USB_CONFIG_DESC_SIZE,
.bDescriptorType = USB_B_DESCRIPTOR_TYPE_CONFIGURATION,
.wTotalLength = MOCK_MSC_SCSI_WTOTALLENGTH,
.bNumInterfaces = 1,
.bConfigurationValue = 1,
.iConfiguration = 0,
.bmAttributes = 0x80,
.bMaxPower = 0x70, // 224mA
};
static const usb_intf_desc_t mock_msc_intf_desc = {
.bLength = USB_INTF_DESC_SIZE,
.bDescriptorType = USB_B_DESCRIPTOR_TYPE_INTERFACE,
.bInterfaceNumber = MOCK_MSC_SCSI_INTF_NUMBER,
.bAlternateSetting = MOCK_MSC_SCSI_INTF_ALT_SETTING,
.bNumEndpoints = 2,
.bInterfaceClass = USB_CLASS_MASS_STORAGE,
.bInterfaceSubClass = 0x06, // SCSI
.bInterfaceProtocol = 0x50, // Bulk only
.iInterface = 0,
};
uint8_t mock_msc_scsi_config_desc[255];
uint16_t mock_msc_scsi_str_desc_manu[128];
uint16_t mock_msc_scsi_str_desc_prod[128];
uint16_t mock_msc_scsi_str_desc_ser_num[128];
const usb_ep_desc_t mock_msc_scsi_bulk_out_ep_desc = {
.bLength = sizeof(usb_ep_desc_t),
.bDescriptorType = USB_B_DESCRIPTOR_TYPE_ENDPOINT,
.bEndpointAddress = MOCK_MSC_SCSI_BULK_OUT_EP_ADDR, // EP 1 OUT
.bmAttributes = USB_BM_ATTRIBUTES_XFER_BULK,
.wMaxPacketSize = MOCK_MSC_SCSI_BULK_EP_MPS, // MPS of 64 bytes
.bInterval = 0,
};
const usb_ep_desc_t mock_msc_scsi_bulk_in_ep_desc = {
.bLength = sizeof(usb_ep_desc_t),
.bDescriptorType = USB_B_DESCRIPTOR_TYPE_ENDPOINT,
.bEndpointAddress = MOCK_MSC_SCSI_BULK_IN_EP_ADDR,
.bmAttributes = USB_BM_ATTRIBUTES_XFER_BULK,
.wMaxPacketSize = MOCK_MSC_SCSI_BULK_EP_MPS, // MPS of 64 bytes
.bInterval = 0,
};
void mock_msc_scsi_init_cbw(mock_msc_bulk_cbw_t *cbw, bool is_read, int offset, int num_sectors, uint32_t tag)
void mock_msc_scsi_init_cbw(mock_msc_bulk_cbw_t *cbw,
bool is_read,
unsigned int offset,
unsigned int num_sectors,
unsigned int sector_size,
uint32_t tag)
{
cbw->dCBWSignature = 0x43425355; // Fixed value
cbw->dCBWTag = tag; // Random value that is echoed back
cbw->dCBWDataTransferLength = num_sectors * MOCK_MSC_SCSI_SECTOR_SIZE;
cbw->dCBWDataTransferLength = num_sectors * sector_size;
cbw->bmCBWFlags = (is_read) ? (1 << 7) : 0; // If this is a read, set the direction flag
cbw->bCBWLUN = MOCK_MSC_SCSI_LUN;
cbw->bCBWLUN = 0;
cbw->bCBWCBLength = 10; // The length of the SCSI command
// Initialize SCSI CMD as READ10 or WRITE 10
cbw->CBWCB.opcode = (is_read) ? 0x28 : 0x2A; // SCSI CMD READ10 or WRITE10
@@ -121,38 +62,3 @@ bool mock_msc_scsi_check_csw(mock_msc_bulk_csw_t *csw, uint32_t tag_expect)
}
return no_issues;
}
void mock_msc_scsi_init_reference_descriptors(void)
{
// Configuration descriptor
uint8_t *dest_ptr = mock_msc_scsi_config_desc;
memcpy(dest_ptr, (void*)&mock_msc_config_desc, sizeof(mock_msc_config_desc));
dest_ptr += USB_CONFIG_DESC_SIZE;
memcpy(dest_ptr, (void*)&mock_msc_intf_desc, sizeof(mock_msc_intf_desc));
dest_ptr += USB_INTF_DESC_SIZE;
memcpy(dest_ptr, (void*)&mock_msc_scsi_bulk_in_ep_desc, sizeof(mock_msc_scsi_bulk_in_ep_desc));
dest_ptr += USB_EP_DESC_SIZE;
memcpy(dest_ptr, (void*)&mock_msc_scsi_bulk_out_ep_desc, sizeof(mock_msc_scsi_bulk_out_ep_desc));
// String descriptors
const char *str = MOCK_MSC_SCSI_STRING_1;
uint8_t chr_count = strlen(str);
mock_msc_scsi_str_desc_manu[0] = (USB_B_DESCRIPTOR_TYPE_STRING << 8) | (2 * chr_count + 2); // first byte is length (including header), second byte is string type
for (uint8_t i = 0; i < chr_count; i++) {
mock_msc_scsi_str_desc_manu[1 + i] = str[i];
}
str = MOCK_MSC_SCSI_STRING_2;
chr_count = strlen(str);
mock_msc_scsi_str_desc_prod[0] = (USB_B_DESCRIPTOR_TYPE_STRING << 8) | (2 * chr_count + 2); // first byte is length (including header), second byte is string type
for (uint8_t i = 0; i < chr_count; i++) {
mock_msc_scsi_str_desc_prod[1 + i] = str[i];
}
str = MOCK_MSC_SCSI_STRING_3;
chr_count = strlen(str);
mock_msc_scsi_str_desc_ser_num[0] = (USB_B_DESCRIPTOR_TYPE_STRING << 8) | (2 * chr_count + 2); // first byte is length (including header), second byte is string type
for (uint8_t i = 0; i < chr_count; i++) {
mock_msc_scsi_str_desc_ser_num[1 + i] = str[i];
}
}