usb_host: Update MSC tests for running in CI

This commit is contained in:
Tomas Rezucha 2022-02-11 11:55:13 +01:00
parent 0377fc8d92
commit 3074981dcb
2 changed files with 47 additions and 36 deletions

View File

@ -50,7 +50,7 @@ static const char *TAG = "msc_example";
/**** Kconfig driven Descriptor ****/ /**** Kconfig driven Descriptor ****/
tusb_desc_device_t device_descriptor = { static const tusb_desc_device_t device_descriptor = {
.bLength = sizeof(device_descriptor), .bLength = sizeof(device_descriptor),
.bDescriptorType = TUSB_DESC_DEVICE, .bDescriptorType = TUSB_DESC_DEVICE,
.bcdUSB = 0x0200, .bcdUSB = 0x0200,
@ -67,12 +67,20 @@ tusb_desc_device_t device_descriptor = {
.bNumConfigurations = 0x01 .bNumConfigurations = 0x01
}; };
const uint16_t msc_desc_config_len = TUD_CONFIG_DESC_LEN + CFG_TUD_MSC * TUD_MSC_DESC_LEN;
static const uint8_t msc_desc_configuration[] = {
TUD_CONFIG_DESCRIPTOR(1, 4, 0, msc_desc_config_len, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100),
TUD_MSC_DESCRIPTOR(0, 5, 1, 0x80 | 1, 64),
};
void device_app(void) void device_app(void)
{ {
ESP_LOGI(TAG, "USB initialization"); ESP_LOGI(TAG, "USB initialization");
tinyusb_config_t tusb_cfg = { tinyusb_config_t tusb_cfg = {
.descriptor = &device_descriptor .device_descriptor = &device_descriptor,
.configuration_descriptor = msc_desc_configuration
}; };
ESP_ERROR_CHECK(tinyusb_driver_install(&tusb_cfg)); ESP_ERROR_CHECK(tinyusb_driver_install(&tusb_cfg));
@ -83,7 +91,6 @@ void device_app(void)
} }
} }
// whether host does safe-eject // whether host does safe-eject
static bool ejected = false; static bool ejected = false;
@ -292,4 +299,4 @@ int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16], void *buffer,
return resplen; return resplen;
} }
#endif #endif /* SOC_USB_OTG_SUPPORTED */

View File

@ -50,7 +50,7 @@ static usb_phy_handle_t phy_hdl = NULL;
static void force_conn_state(bool connected, TickType_t delay_ticks) static void force_conn_state(bool connected, TickType_t delay_ticks)
{ {
TEST_ASSERT_NOT_EQUAL(NULL, phy_hdl); TEST_ASSERT(phy_hdl);
if (delay_ticks > 0) { if (delay_ticks > 0) {
//Delay of 0 ticks causes a yield. So skip if delay_ticks is 0. //Delay of 0 ticks causes a yield. So skip if delay_ticks is 0.
vTaskDelay(delay_ticks); vTaskDelay(delay_ticks);
@ -62,7 +62,7 @@ static void msc_event_cb(const msc_host_event_t *event, void *arg)
{ {
if (waiting_for_sudden_disconnect) { if (waiting_for_sudden_disconnect) {
waiting_for_sudden_disconnect = false; waiting_for_sudden_disconnect = false;
TEST_ASSERT(event->event == MSC_DEVICE_DISCONNECTED); TEST_ASSERT_EQUAL(MSC_DEVICE_DISCONNECTED, event->event);
} }
if (event->event == MSC_DEVICE_CONNECTED) { if (event->event == MSC_DEVICE_CONNECTED) {
@ -83,12 +83,12 @@ static void write_read_file(const char *file_path)
ESP_LOGI(TAG, "Writing file"); ESP_LOGI(TAG, "Writing file");
FILE *f = fopen(file_path, "w"); FILE *f = fopen(file_path, "w");
TEST_ASSERT( f != NULL); TEST_ASSERT(f);
fprintf(f, TEST_STRING); fprintf(f, TEST_STRING);
fclose(f); fclose(f);
ESP_LOGI(TAG, "Reading file"); ESP_LOGI(TAG, "Reading file");
TEST_ASSERT( fopen(file_path, "r") != NULL); TEST_ASSERT(fopen(file_path, "r"));
fgets(line, sizeof(line), f); fgets(line, sizeof(line), f);
fclose(f); fclose(f);
// strip newline // strip newline
@ -138,7 +138,7 @@ static void check_file_content(const char *file_path, const char *expected)
{ {
ESP_LOGI(TAG, "Reading %s:", file_path); ESP_LOGI(TAG, "Reading %s:", file_path);
FILE *file = fopen(file_path, "r"); FILE *file = fopen(file_path, "r");
TEST_ASSERT(file != NULL) TEST_ASSERT(file)
char content[200]; char content[200];
fread(content, 1, sizeof(content), file); fread(content, 1, sizeof(content), file);
@ -153,12 +153,12 @@ static void check_sudden_disconnect(void)
ESP_LOGI(TAG, "Creating test.tx"); ESP_LOGI(TAG, "Creating test.tx");
FILE *file = fopen("/usb/test.txt", "w"); FILE *file = fopen("/usb/test.txt", "w");
TEST_ASSERT( file != NULL); TEST_ASSERT(file);
ESP_LOGI(TAG, "Write data"); ESP_LOGI(TAG, "Write data");
TEST_ASSERT( fwrite(data, 1, DATA_SIZE, file) == DATA_SIZE ); TEST_ASSERT_EQUAL(DATA_SIZE, fwrite(data, 1, DATA_SIZE, file));
TEST_ASSERT( fwrite(data, 1, DATA_SIZE, file) == DATA_SIZE ); TEST_ASSERT_EQUAL(DATA_SIZE, fwrite(data, 1, DATA_SIZE, file));
TEST_ASSERT( fflush(file) == 0 ); TEST_ASSERT_EQUAL(0, fflush(file));
ESP_LOGI(TAG, "Trigger a disconnect"); ESP_LOGI(TAG, "Trigger a disconnect");
//Trigger a disconnect //Trigger a disconnect
@ -167,10 +167,10 @@ static void check_sudden_disconnect(void)
// Make sure flag was leared in callback // Make sure flag was leared in callback
vTaskDelay( pdMS_TO_TICKS(100) ); vTaskDelay( pdMS_TO_TICKS(100) );
TEST_ASSERT( waiting_for_sudden_disconnect == false ); TEST_ASSERT_FALSE(waiting_for_sudden_disconnect);
ESP_LOGI(TAG, "Write data after disconnect"); ESP_LOGI(TAG, "Write data after disconnect");
TEST_ASSERT( fwrite(data, 1, DATA_SIZE, file) != DATA_SIZE ); TEST_ASSERT_NOT_EQUAL( DATA_SIZE, fwrite(data, 1, DATA_SIZE, file));
fclose(file); fclose(file);
} }
@ -191,7 +191,7 @@ static void msc_setup(void)
.otg_speed = USB_PHY_SPEED_UNDEFINED, //In Host mode, the speed is determined by the connected device .otg_speed = USB_PHY_SPEED_UNDEFINED, //In Host mode, the speed is determined by the connected device
.gpio_conf = NULL, .gpio_conf = NULL,
}; };
TEST_ASSERT_EQUAL(ESP_OK, usb_new_phy(&phy_config, &phy_hdl)); ESP_OK_ASSERT(usb_new_phy(&phy_config, &phy_hdl));
const usb_host_config_t host_config = { const usb_host_config_t host_config = {
.skip_phy_setup = true, .skip_phy_setup = true,
.intr_flags = ESP_INTR_FLAG_LEVEL1, .intr_flags = ESP_INTR_FLAG_LEVEL1,
@ -212,7 +212,7 @@ static void msc_setup(void)
ESP_LOGI(TAG, "Waiting for USB stick to be connected"); ESP_LOGI(TAG, "Waiting for USB stick to be connected");
msc_host_event_t app_event; msc_host_event_t app_event;
xQueueReceive(app_queue, &app_event, portMAX_DELAY); xQueueReceive(app_queue, &app_event, portMAX_DELAY);
TEST_ASSERT( app_event.event == MSC_DEVICE_CONNECTED ); TEST_ASSERT_EQUAL(MSC_DEVICE_CONNECTED, app_event.event);
uint8_t device_addr = app_event.device.address; uint8_t device_addr = app_event.device.address;
ESP_OK_ASSERT( msc_host_install_device(device_addr, &device) ); ESP_OK_ASSERT( msc_host_install_device(device_addr, &device) );
@ -232,7 +232,7 @@ static void msc_teardown(void)
vSemaphoreDelete(ready_to_deinit_usb); vSemaphoreDelete(ready_to_deinit_usb);
ESP_OK_ASSERT( usb_host_uninstall() ); ESP_OK_ASSERT( usb_host_uninstall() );
//Tear down USB PHY //Tear down USB PHY
TEST_ASSERT_EQUAL(ESP_OK, usb_del_phy(phy_hdl)); ESP_OK_ASSERT(usb_del_phy(phy_hdl));
phy_hdl = NULL; phy_hdl = NULL;
vQueueDelete(app_queue); vQueueDelete(app_queue);
@ -262,35 +262,40 @@ static void erase_storage(void)
} }
} }
static void check_readme_content(void) TEST_CASE("write_and_read_file", "[usb_msc]")
{
msc_setup();
check_file_content("/usb/README.TXT", README_CONTENTS);
msc_teardown();
}
TEST_CASE("Write and read file", "[usb_msc][ignore]")
{ {
msc_setup(); msc_setup();
write_read_file(FILE_NAME); write_read_file(FILE_NAME);
msc_teardown(); msc_teardown();
} }
TEST_CASE("Sudden disconnect", "[usb_msc][ignore]") TEST_CASE("sudden_disconnect", "[usb_msc]")
{ {
msc_setup(); msc_setup();
check_sudden_disconnect(); check_sudden_disconnect();
msc_teardown(); msc_teardown();
} }
void read_write_sectors(void) TEST_CASE("sectors_can_be_written_and_read", "[usb_msc]")
{ {
msc_setup(); msc_setup();
write_read_sectors(); write_read_sectors();
msc_teardown(); msc_teardown();
} }
void check_formatting(void) TEST_CASE("check_README_content", "[usb_msc]")
{
msc_setup();
check_file_content("/usb/README.TXT", README_CONTENTS);
msc_teardown();
}
/**
* @brief USB MSC format testcase
* @attention This testcase deletes all content on the USB MSC device.
* The device must be reset in order to contain the FILE_NAME again.
*/
TEST_CASE("can_be_formated", "[usb_msc]")
{ {
printf("Create file\n"); printf("Create file\n");
msc_setup(); msc_setup();
@ -299,7 +304,7 @@ void check_formatting(void)
printf("File exists after mounting again\n"); printf("File exists after mounting again\n");
msc_setup(); msc_setup();
TEST_ASSERT( file_exists(FILE_NAME) ); TEST_ASSERT(file_exists(FILE_NAME));
printf("Erase storage device\n"); printf("Erase storage device\n");
erase_storage(); erase_storage();
msc_teardown(); msc_teardown();
@ -307,15 +312,14 @@ void check_formatting(void)
printf("Check file does not exist after formatting\n"); printf("Check file does not exist after formatting\n");
mount_config.format_if_mount_failed = true; mount_config.format_if_mount_failed = true;
msc_setup(); msc_setup();
TEST_ASSERT( !file_exists(FILE_NAME) ); TEST_ASSERT_FALSE(file_exists(FILE_NAME));
msc_teardown(); msc_teardown();
mount_config.format_if_mount_failed = false; mount_config.format_if_mount_failed = false;
} }
TEST_CASE_MULTIPLE_DEVICES("Sectors can be written and read", "[usb_msc][ignore]", read_write_sectors, device_app); TEST_CASE("mock_device_app", "[usb_msc_device][ignore]")
{
TEST_CASE_MULTIPLE_DEVICES("Can be Formated", "[usb_msc][ignore]", check_formatting, device_app); device_app();
}
TEST_CASE_MULTIPLE_DEVICES("Check README content", "[usb_msc][ignore]", check_readme_content, device_app);
#endif #endif