Skip to content

Commit

Permalink
change(teardown): Changed key_len to 63 and 511
Browse files Browse the repository at this point in the history
  • Loading branch information
roma-jam committed Dec 18, 2024
1 parent 38493f8 commit 72075c1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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...");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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

0 comments on commit 72075c1

Please sign in to comment.