-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feature(cdc_teardown): Added iteration test #106
Draft
roma-jam
wants to merge
3
commits into
master
Choose a base branch
from
feature/cdc_acm_teardown_test
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# The following lines of boilerplate have to be in your project's | ||
# CMakeLists in this exact order for cmake to work correctly | ||
cmake_minimum_required(VERSION 3.16) | ||
include($ENV{IDF_PATH}/tools/cmake/project.cmake) | ||
|
||
# "Trim" the build. Include the minimal set of components, main, and anything it depends on. | ||
set(COMPONENTS main) | ||
|
||
project(test_app_teardown_device) |
4 changes: 4 additions & 0 deletions
4
device/esp_tinyusb/test_apps/teardown_device/main/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
idf_component_register(SRC_DIRS . | ||
INCLUDE_DIRS . | ||
REQUIRES unity | ||
WHOLE_ARCHIVE) |
5 changes: 5 additions & 0 deletions
5
device/esp_tinyusb/test_apps/teardown_device/main/idf_component.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
## IDF Component Manager Manifest File | ||
dependencies: | ||
espressif/esp_tinyusb: | ||
version: "*" | ||
override_path: "../../../" |
62 changes: 62 additions & 0 deletions
62
device/esp_tinyusb/test_apps/teardown_device/main/test_app_main.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include <stdio.h> | ||
#include <string.h> | ||
#include "unity.h" | ||
#include "unity_test_runner.h" | ||
#include "unity_test_utils_memory.h" | ||
|
||
void app_main(void) | ||
{ | ||
/* | ||
_ _ _ | ||
| | (_) | | | ||
___ ___ _ __ | |_ _ _ __ _ _ _ _ ___| |__ | ||
/ _ \/ __| '_ \| __| | '_ \| | | | | | / __| '_ \ | ||
| __/\__ \ |_) | |_| | | | | |_| | |_| \__ \ |_) | | ||
\___||___/ .__/ \__|_|_| |_|\__, |\__,_|___/_.__/ | ||
| |______ __/ | | ||
|_|______| |___/ | ||
_____ _____ _____ _____ | ||
|_ _| ___/ ___|_ _| | ||
| | | |__ \ `--. | | | ||
| | | __| `--. \ | | | ||
| | | |___/\__/ / | | | ||
\_/ \____/\____/ \_/ | ||
*/ | ||
|
||
printf(" _ _ _ \n"); | ||
printf(" | | (_) | | \n"); | ||
printf(" ___ ___ _ __ | |_ _ _ __ _ _ _ _ ___| |__ \n"); | ||
printf(" / _ \\/ __| '_ \\| __| | '_ \\| | | | | | / __| '_ \\ \n"); | ||
printf("| __/\\__ \\ |_) | |_| | | | | |_| | |_| \\__ \\ |_) |\n"); | ||
printf(" \\___||___/ .__/ \\__|_|_| |_|\\__, |\\__,_|___/_.__/ \n"); | ||
printf(" | |______ __/ | \n"); | ||
printf(" |_|______| |___/ \n"); | ||
printf(" _____ _____ _____ _____ \n"); | ||
printf("|_ _| ___/ ___|_ _| \n"); | ||
printf(" | | | |__ \\ `--. | | \n"); | ||
printf(" | | | __| `--. \\ | | \n"); | ||
printf(" | | | |___/\\__/ / | | \n"); | ||
printf(" \\_/ \\____/\\____/ \\_/ \n"); | ||
|
||
unity_utils_setup_heap_record(80); | ||
unity_utils_set_leak_level(128); | ||
unity_run_menu(); | ||
} | ||
|
||
/* setUp runs before every test */ | ||
void setUp(void) | ||
{ | ||
unity_utils_record_free_mem(); | ||
} | ||
|
||
/* tearDown runs after every test */ | ||
void tearDown(void) | ||
{ | ||
unity_utils_evaluate_leaks(); | ||
} |
213 changes: 213 additions & 0 deletions
213
device/esp_tinyusb/test_apps/teardown_device/main/test_teardown.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,213 @@ | ||||||||
/* | ||||||||
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD | ||||||||
* | ||||||||
* SPDX-License-Identifier: Apache-2.0 | ||||||||
*/ | ||||||||
|
||||||||
#include "soc/soc_caps.h" | ||||||||
#if SOC_USB_OTG_SUPPORTED | ||||||||
|
||||||||
// | ||||||||
#include <stdio.h> | ||||||||
#include <string.h> | ||||||||
// | ||||||||
#include "freertos/FreeRTOS.h" | ||||||||
#include "freertos/task.h" | ||||||||
#include "freertos/semphr.h" | ||||||||
// | ||||||||
#include "esp_system.h" | ||||||||
#include "esp_log.h" | ||||||||
#include "esp_err.h" | ||||||||
// | ||||||||
#include "unity.h" | ||||||||
#include "tinyusb.h" | ||||||||
#include "tusb_cdc_acm.h" | ||||||||
|
||||||||
static const char *TAG = "teardown"; | ||||||||
|
||||||||
SemaphoreHandle_t wait_mount = NULL; | ||||||||
SemaphoreHandle_t wait_terminal = NULL; | ||||||||
SemaphoreHandle_t wait_command = NULL; | ||||||||
|
||||||||
static uint8_t rx_buf[CONFIG_TINYUSB_CDC_RX_BUFSIZE + 1]; | ||||||||
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_ATTACH_TIMEOUT_MS 5000 | ||||||||
#define TEARDOWN_COMMAND_TIMEOUT_MS 5000 | ||||||||
#define TEARDOWN_AMOUNT 4 | ||||||||
|
||||||||
static const tusb_desc_device_t cdc_device_descriptor = { | ||||||||
.bLength = sizeof(cdc_device_descriptor), | ||||||||
.bDescriptorType = TUSB_DESC_DEVICE, | ||||||||
.bcdUSB = 0x0200, | ||||||||
.bDeviceClass = TUSB_CLASS_MISC, | ||||||||
.bDeviceSubClass = MISC_SUBCLASS_COMMON, | ||||||||
.bDeviceProtocol = MISC_PROTOCOL_IAD, | ||||||||
.bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE, | ||||||||
.idVendor = USB_ESPRESSIF_VID, | ||||||||
.idProduct = 0x4002, | ||||||||
.bcdDevice = 0x0100, | ||||||||
.iManufacturer = 0x01, | ||||||||
.iProduct = 0x02, | ||||||||
.iSerialNumber = 0x03, | ||||||||
.bNumConfigurations = 0x01 | ||||||||
}; | ||||||||
|
||||||||
static const uint16_t cdc_desc_config_len = TUD_CONFIG_DESC_LEN + CFG_TUD_CDC * TUD_CDC_DESC_LEN; | ||||||||
static const uint8_t cdc_desc_configuration[] = { | ||||||||
TUD_CONFIG_DESCRIPTOR(1, 2, 0, cdc_desc_config_len, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100), | ||||||||
TUD_CDC_DESCRIPTOR(0, 4, 0x81, 8, 0x02, 0x82, (TUD_OPT_HIGH_SPEED ? 512 : 64)), | ||||||||
}; | ||||||||
|
||||||||
#if (TUD_OPT_HIGH_SPEED) | ||||||||
static const tusb_desc_device_qualifier_t device_qualifier = { | ||||||||
.bLength = sizeof(tusb_desc_device_qualifier_t), | ||||||||
.bDescriptorType = TUSB_DESC_DEVICE_QUALIFIER, | ||||||||
.bcdUSB = 0x0200, | ||||||||
.bDeviceClass = TUSB_CLASS_MISC, | ||||||||
.bDeviceSubClass = MISC_SUBCLASS_COMMON, | ||||||||
.bDeviceProtocol = MISC_PROTOCOL_IAD, | ||||||||
.bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE, | ||||||||
.bNumConfigurations = 0x01, | ||||||||
.bReserved = 0 | ||||||||
}; | ||||||||
#endif // TUD_OPT_HIGH_SPEED | ||||||||
|
||||||||
static void tinyusb_cdc_rx_callback(int itf, cdcacm_event_t *event) | ||||||||
{ | ||||||||
// Something was received | ||||||||
xSemaphoreGive(wait_command); | ||||||||
} | ||||||||
|
||||||||
/** | ||||||||
* @brief CDC device line change callback | ||||||||
* | ||||||||
* CDC device signals, that the DTR, RTS states changed | ||||||||
* | ||||||||
* @param[in] itf CDC device index | ||||||||
* @param[in] event CDC event type | ||||||||
*/ | ||||||||
void tinyusb_cdc_line_state_changed_callback(int itf, cdcacm_event_t *event) | ||||||||
{ | ||||||||
int dtr = event->line_state_changed_data.dtr; | ||||||||
int rts = event->line_state_changed_data.rts; | ||||||||
ESP_LOGD(TAG, "Line state changed on channel %d: DTR:%d, RTS:%d", itf, dtr, rts); | ||||||||
|
||||||||
// Terminal: | ||||||||
// dtr==1 && rts==1 - connected | ||||||||
// dtr==0 && rts==0 - disconnected | ||||||||
xSemaphoreGive(wait_terminal); | ||||||||
} | ||||||||
|
||||||||
// Invoked when device is mounted | ||||||||
void tud_mount_cb(void) | ||||||||
{ | ||||||||
xSemaphoreGive(wait_mount); | ||||||||
} | ||||||||
|
||||||||
/** | ||||||||
* @brief TinyUSB Teardown specific testcase | ||||||||
* | ||||||||
* Scenario: | ||||||||
* - Installs the tinyUSB driver via esp-tinyusb wrapper with 1xCDC class device | ||||||||
* - Awaits device configuration be the Host (TEARDOWN_ATTACH_TIMEOUT_MS) | ||||||||
* - Awaits the terminal connection (TEARDOWN_ATTACH_TIMEOUT_MS) | ||||||||
* - Expects the command sequence from the Host (TEARDOWN_COMMAND_TIMEOUT_MS) | ||||||||
* - Replies with the response sequence to the Host | ||||||||
* - Awaits terminal disconnection (TEARDOWN_ATTACH_TIMEOUT_MS) | ||||||||
* - Teardowns the tinyUSB driver via esp-tinyusb wrapper | ||||||||
* - Repeats the steps from the step.1 N times (where N = TEARDOWN_AMOUNT) | ||||||||
* - Verifies amount of attempts and memory leakage (attempts should be 0) | ||||||||
* | ||||||||
* command sequence[] = ep_size * 0xAA | ||||||||
* response sequence[] = ep_size * 0x55 | ||||||||
* | ||||||||
* Hint: Values 0xAA and 0x55 were selected to verify the buffer memory integrity, | ||||||||
* as the 0xAA and 0x55 are the inversion of each other and the data bits in the same position changes from 1 to 0 in every transaction. | ||||||||
*/ | ||||||||
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(); | ||||||||
TEST_ASSERT_NOT_EQUAL(NULL, wait_command); | ||||||||
wait_terminal = xSemaphoreCreateBinary(); | ||||||||
TEST_ASSERT_NOT_EQUAL(NULL, wait_terminal); | ||||||||
|
||||||||
// Prep reply | ||||||||
for (int i = 0; i < TEARDOWN_CMD_RPL_SIZE; i++) { | ||||||||
tx_buf[i] = TEARDOWN_RPL_KEY; | ||||||||
} | ||||||||
|
||||||||
// TinyUSB driver configuration | ||||||||
const tinyusb_config_t tusb_cfg = { | ||||||||
.device_descriptor = &cdc_device_descriptor, | ||||||||
.string_descriptor = NULL, | ||||||||
.string_descriptor_count = 0, | ||||||||
.external_phy = false, | ||||||||
#if (TUD_OPT_HIGH_SPEED) | ||||||||
.fs_configuration_descriptor = cdc_desc_configuration, | ||||||||
.hs_configuration_descriptor = cdc_desc_configuration, | ||||||||
.qualifier_descriptor = &device_qualifier, | ||||||||
#else | ||||||||
.configuration_descriptor = cdc_desc_configuration, | ||||||||
#endif // TUD_OPT_HIGH_SPEED | ||||||||
}; | ||||||||
|
||||||||
// TinyUSB ACM Driver configuration | ||||||||
const tinyusb_config_cdcacm_t acm_cfg = { | ||||||||
.usb_dev = TINYUSB_USBDEV_0, | ||||||||
.cdc_port = TINYUSB_CDC_ACM_0, | ||||||||
.rx_unread_buf_sz = 64, | ||||||||
.callback_rx = &tinyusb_cdc_rx_callback, | ||||||||
.callback_rx_wanted_char = NULL, | ||||||||
.callback_line_state_changed = &tinyusb_cdc_line_state_changed_callback, | ||||||||
.callback_line_coding_changed = NULL | ||||||||
}; | ||||||||
|
||||||||
int attempts = TEARDOWN_AMOUNT; | ||||||||
while (attempts) { | ||||||||
TEST_ASSERT_EQUAL(ESP_OK, tinyusb_driver_install(&tusb_cfg)); | ||||||||
// Init CDC 0 | ||||||||
TEST_ASSERT_FALSE(tusb_cdc_acm_initialized(TINYUSB_CDC_ACM_0)); | ||||||||
TEST_ASSERT_EQUAL(ESP_OK, tusb_cdc_acm_init(&acm_cfg)); | ||||||||
TEST_ASSERT_TRUE(tusb_cdc_acm_initialized(TINYUSB_CDC_ACM_0)); | ||||||||
// Wait for the usb event | ||||||||
ESP_LOGD(TAG, "wait dev mounted..."); | ||||||||
TEST_ASSERT_EQUAL(pdTRUE, xSemaphoreTake(wait_mount, pdMS_TO_TICKS(TEARDOWN_ATTACH_TIMEOUT_MS))); | ||||||||
ESP_LOGD(TAG, "wait terminal connection..."); | ||||||||
TEST_ASSERT_EQUAL(pdTRUE, xSemaphoreTake(wait_terminal, pdMS_TO_TICKS(TEARDOWN_ATTACH_TIMEOUT_MS))); | ||||||||
// 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..."); | ||||||||
TEST_ASSERT_EQUAL(TEARDOWN_CMD_RPL_SIZE, tinyusb_cdcacm_write_queue(TINYUSB_CDC_ACM_0, tx_buf, TEARDOWN_CMD_RPL_SIZE)); | ||||||||
TEST_ASSERT_EQUAL(ESP_OK, tinyusb_cdcacm_write_flush(TINYUSB_CDC_ACM_0, pdMS_TO_TICKS(1000))); | ||||||||
ESP_LOGD(TAG, "wait for terminal disconnection"); | ||||||||
TEST_ASSERT_EQUAL(pdTRUE, xSemaphoreTake(wait_terminal, pdMS_TO_TICKS(TEARDOWN_ATTACH_TIMEOUT_MS))); | ||||||||
// Teardown | ||||||||
attempts--; | ||||||||
TEST_ASSERT_EQUAL(ESP_OK, tinyusb_cdcacm_unregister_callback(TINYUSB_CDC_ACM_0, CDC_EVENT_RX)); | ||||||||
TEST_ASSERT_EQUAL(ESP_OK, tinyusb_cdcacm_unregister_callback(TINYUSB_CDC_ACM_0, CDC_EVENT_LINE_STATE_CHANGED)); | ||||||||
TEST_ASSERT_EQUAL(ESP_OK, tusb_cdc_acm_deinit(TINYUSB_CDC_ACM_0)); | ||||||||
TEST_ASSERT_EQUAL(ESP_OK, tinyusb_driver_uninstall()); | ||||||||
} | ||||||||
// Remove primitives | ||||||||
vSemaphoreDelete(wait_mount); | ||||||||
vSemaphoreDelete(wait_command); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Delete also the |
||||||||
// All attempts should be completed | ||||||||
TEST_ASSERT_EQUAL(0, attempts); | ||||||||
} | ||||||||
|
||||||||
#endif |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use
EACH_EQUAL
macro to compare an array to a single valueThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the idea!