From 72075c1c85492cbd390c48f41f0f7fe1ee325ba5 Mon Sep 17 00:00:00 2001 From: Roman Leonov Date: Wed, 18 Dec 2024 18:00:07 +0100 Subject: [PATCH] change(teardown): Changed key_len to 63 and 511 --- .../teardown_device/main/test_teardown.c | 17 ++++++------ .../teardown_device/pytest_teardown_device.py | 27 ++++++++----------- 2 files changed, 19 insertions(+), 25 deletions(-) diff --git a/device/esp_tinyusb/test_apps/teardown_device/main/test_teardown.c b/device/esp_tinyusb/test_apps/teardown_device/main/test_teardown.c index a176f6ba..28e43ada 100644 --- a/device/esp_tinyusb/test_apps/teardown_device/main/test_teardown.c +++ b/device/esp_tinyusb/test_apps/teardown_device/main/test_teardown.c @@ -34,7 +34,7 @@ static uint8_t tx_buf[CONFIG_TINYUSB_CDC_TX_BUFSIZE + 1] = { 0 }; #define TEARDOWN_CMD_KEY 0xAA #define TEARDOWN_RPL_KEY 0x55 -#define TEARDOWN_CMD_RPL_SIZE ((TUD_OPT_HIGH_SPEED ? 512 : 64)) +#define TEARDOWN_CMD_RPL_SIZE ((TUD_OPT_HIGH_SPEED ? 512 : 64) - 1) #define TEARDOWN_ATTACH_TIMEOUT_MS 2000 #define TEARDOWN_COMMAND_TIMEOUT_MS 5000 #define TEARDOWN_AMOUNT 10 @@ -78,14 +78,7 @@ static const tusb_desc_device_qualifier_t device_qualifier = { static void tinyusb_cdc_rx_callback(int itf, cdcacm_event_t *event) { - size_t rx_size = 0; - TEST_ASSERT_EQUAL(ESP_OK, tinyusb_cdcacm_read(itf, rx_buf, CONFIG_TINYUSB_CDC_RX_BUFSIZE, &rx_size)); - for (int i = 0; i < TEARDOWN_CMD_RPL_SIZE; i++) { - if (rx_buf[i] != TEARDOWN_CMD_KEY) { - return; - } - } - // Key command received + // Something was received xSemaphoreGive(wait_command); } @@ -120,6 +113,8 @@ void tud_mount_cb(void) */ TEST_CASE("tinyusb_teardown", "[esp_tinyusb][teardown]") { + size_t rx_size = 0; + wait_mount = xSemaphoreCreateBinary(); TEST_ASSERT_NOT_EQUAL(NULL, wait_mount); wait_command = xSemaphoreCreateBinary(); @@ -174,6 +169,10 @@ TEST_CASE("tinyusb_teardown", "[esp_tinyusb][teardown]") // Wait for the command ESP_LOGD(TAG, "wait command..."); TEST_ASSERT_EQUAL(pdTRUE, xSemaphoreTake(wait_command, pdMS_TO_TICKS(TEARDOWN_COMMAND_TIMEOUT_MS))); + TEST_ASSERT_EQUAL(ESP_OK, tinyusb_cdcacm_read(TINYUSB_CDC_ACM_0, rx_buf, CONFIG_TINYUSB_CDC_RX_BUFSIZE, &rx_size)); + for (int i = 0; i < TEARDOWN_CMD_RPL_SIZE; i++) { + TEST_ASSERT_EQUAL(TEARDOWN_CMD_KEY, rx_buf[i]); + } ESP_LOGD(TAG, "command received"); // Reply the response sequence ESP_LOGD(TAG, "send response..."); diff --git a/device/esp_tinyusb/test_apps/teardown_device/pytest_teardown_device.py b/device/esp_tinyusb/test_apps/teardown_device/pytest_teardown_device.py index d72b022b..bb4916b8 100644 --- a/device/esp_tinyusb/test_apps/teardown_device/pytest_teardown_device.py +++ b/device/esp_tinyusb/test_apps/teardown_device/pytest_teardown_device.py @@ -29,13 +29,13 @@ def wait_for_tusb_cdc(vid, pid, timeout=30): sleep(0.5) # Check every 0.5 seconds return None -def teardown_device(ep_size, amount): +def teardown_device(key_len, amount): TUSB_VID = 0x303A # Espressif TinyUSB VID TUSB_PID = 0x4002 # Espressif TinyUSB VID # Command to send and expected response - COMMAND = b'\xAA' * ep_size - EXPECTED_RESPONSE = b'\x55' * ep_size + COMMAND = b'\xAA' * key_len + EXPECTED_RESPONSE = b'\x55' * key_len # Number of iterations, must be equal to ITERATIONS in the test application ITERATIONS = amount @@ -55,18 +55,12 @@ def teardown_device(ep_size, amount): with serial.Serial(port=tusb_cdc, baudrate=9600, timeout=1) as cdc: print(f"Opened port: {tusb_cdc}") # Send the key command - cdc.write(COMMAND) - - # Wait until all data is transmitted - cdc.flush() - print(f"Sent {len(COMMAND)}: {COMMAND.hex().upper()}") - - # Clear buffers - cdc.reset_input_buffer() # Clear input buffer - cdc.reset_output_buffer() # Clear output buffe - + res = cdc.write(COMMAND) + assert res == key_len # Wait for the response res = cdc.readline() + assert len(res) == key_len + print(f"Sent {len(COMMAND)}: {COMMAND.hex().upper()}") print(f"Received {len(res)}: {res.hex().upper()}") # Check if the response matches the expected response @@ -92,8 +86,9 @@ def test_usb_teardown_device(dut) -> None: dut.expect_exact('TinyUSB: TinyUSB Driver installed') sleep(2) # Some time for the OS to enumerate our USB device if dut.target == 'esp32p4': - ep_size = 512 + MPS = 512 else: - ep_size = 64 - teardown_device(ep_size, 10) # Teardown tusb device 10 times + MPS = 64 + MPS -= 1 # On Linux, the serial port kept opened, while len==MPS, https://github.com/pyserial/pyserial/issues/753 + teardown_device(MPS, 10) # Teardown tusb device 10 times