Skip to content

Commit 139df47

Browse files
committed
feat(usb_cdc): Update vfs read() to comply with POSIX standards
1 parent 24048d8 commit 139df47

File tree

5 files changed

+121
-26
lines changed

5 files changed

+121
-26
lines changed

components/esp_driver_usb_serial_jtag/test_apps/usb_serial_jtag_vfs/main/test_vfs_usb_serial_jtag.c

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,9 @@ TEST_CASE("test select read, write and timeout", "[usb_serial_jtag vfs]")
124124
usb_serial_jtag_driver_uninstall();
125125
}
126126

127-
TEST_CASE("read with usj driver (non-blocking)", "[usb serial jtag vfs]")
127+
/* Test that the read function does not return prematurely when receiving a new line character
128+
*/
129+
TEST_CASE("read does not return on new line character", "[usb serial jtag vfs]")
128130
{
129131
// Send a string with length less than the read requested length
130132
const char in_buffer[] = "!(@*#&(!*@&#((SDasdkjhad\nce"; // read should not early return on \n
@@ -133,8 +135,6 @@ TEST_CASE("read with usj driver (non-blocking)", "[usb serial jtag vfs]")
133135
const size_t out_buffer_len = in_buffer_len - 1; // don't compare the null character at the end of in_buffer string
134136
char out_buffer[out_buffer_len] = {};
135137

136-
// flush_stdin_stdout();
137-
138138
usb_serial_jtag_driver_config_t usj_config = USB_SERIAL_JTAG_DRIVER_CONFIG_DEFAULT();
139139
ESP_ERROR_CHECK(usb_serial_jtag_driver_install(&usj_config));
140140
usb_serial_jtag_vfs_use_driver();
@@ -175,3 +175,34 @@ TEST_CASE("read with usj driver (non-blocking)", "[usb serial jtag vfs]")
175175
ESP_ERROR_CHECK(usb_serial_jtag_driver_uninstall());
176176
vTaskDelay(2); // wait for tasks to exit
177177
}
178+
179+
TEST_CASE("blocking read returns with available data", "[usb serial jtag vfs]")
180+
{
181+
const size_t out_buffer_len = 32;
182+
char out_buffer[out_buffer_len] = {};
183+
184+
usb_serial_jtag_driver_config_t usj_config = USB_SERIAL_JTAG_DRIVER_CONFIG_DEFAULT();
185+
ESP_ERROR_CHECK(usb_serial_jtag_driver_install(&usj_config));
186+
usb_serial_jtag_vfs_use_driver();
187+
188+
usb_serial_jtag_vfs_set_rx_line_endings(ESP_LINE_ENDINGS_LF);
189+
usb_serial_jtag_vfs_set_tx_line_endings(ESP_LINE_ENDINGS_LF);
190+
191+
int flags = fcntl(STDIN_FILENO, F_GETFL, 0);
192+
fcntl(STDIN_FILENO, F_SETFL, flags & (~O_NONBLOCK));
193+
194+
// trigger the test environment to send the test message
195+
char ready_msg[] = "ready to receive\n";
196+
write(fileno(stdout), ready_msg, sizeof(ready_msg));
197+
198+
const int nread = read(STDIN_FILENO, out_buffer, out_buffer_len);
199+
TEST_ASSERT(nread > 0);
200+
TEST_ASSERT(nread < out_buffer_len);
201+
202+
usb_serial_jtag_vfs_use_nonblocking();
203+
fcntl(STDIN_FILENO, F_SETFL, flags);
204+
usb_serial_jtag_vfs_set_rx_line_endings(ESP_LINE_ENDINGS_CRLF);
205+
usb_serial_jtag_vfs_set_tx_line_endings(ESP_LINE_ENDINGS_CRLF);
206+
ESP_ERROR_CHECK(usb_serial_jtag_driver_uninstall());
207+
vTaskDelay(2); // wait for tasks to exit
208+
}

components/esp_driver_usb_serial_jtag/test_apps/usb_serial_jtag_vfs/pytest_usb_serial_jtag_vfs.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,35 @@ def test_usj_vfs_select(dut: Dut, test_message: list) -> None:
2626

2727
@pytest.mark.usb_serial_jtag
2828
@pytest.mark.parametrize(
29-
'port, config',
29+
'port, flash_port, config',
3030
[
31-
pytest.param('/dev/ttyACM1', 'release'),
31+
pytest.param('/dev/serial_ports/ttyACM-esp32', '/dev/serial_ports/ttyUSB-esp32', 'release'),
3232
],
3333
indirect=True,
3434
)
3535
@pytest.mark.parametrize('test_message', ['!(@*#&(!*@&#((SDasdkjhad\nce'])
3636
@idf_parametrize('target', ['esp32s3', 'esp32c3', 'esp32c6', 'esp32h2'], indirect=['target'])
3737
def test_usj_vfs_read(dut: Dut, test_message: list) -> None:
3838
dut.expect_exact('Press ENTER to see the list of tests')
39-
dut.write('"read with usj driver (non-blocking)"')
39+
dut.write('"read does not return on new line character"')
40+
dut.expect_exact('ready to receive', timeout=2)
41+
dut.write(test_message)
42+
dut.expect(r'\d{1} Tests 0 Failures 0 Ignored', timeout=10)
43+
44+
45+
@pytest.mark.usb_serial_jtag
46+
@pytest.mark.parametrize(
47+
'port, flash_port, config',
48+
[
49+
pytest.param('/dev/serial_ports/ttyACM-esp32', '/dev/serial_ports/ttyUSB-esp32', 'release'),
50+
],
51+
indirect=True,
52+
)
53+
@pytest.mark.parametrize('test_message', ['testdata'])
54+
@idf_parametrize('target', ['esp32s3', 'esp32c3', 'esp32c6', 'esp32h2'], indirect=['target'])
55+
def test_usj_vfs_read(dut: Dut, test_message: list) -> None:
56+
dut.expect_exact('Press ENTER to see the list of tests')
57+
dut.write('"blocking read returns with available data"')
4058
dut.expect_exact('ready to receive', timeout=2)
4159
dut.write(test_message)
4260
dut.expect(r'\d{1} Tests 0 Failures 0 Ignored', timeout=10)

components/esp_vfs_console/test_apps/usb_cdc_vfs/main/test_app_main.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ static void test_usb_cdc_select(void)
129129
vTaskDelay(10); // wait for the string to send
130130
}
131131

132+
/**
133+
* @brief Test that the non blocking read is compliant with POSIX standards
134+
*/
132135
static void test_usb_cdc_read_non_blocking(void)
133136
{
134137
test_setup(__func__, sizeof(__func__));
@@ -175,6 +178,44 @@ static void test_usb_cdc_read_non_blocking(void)
175178
TEST_ASSERT(errno != EWOULDBLOCK);
176179
}
177180

181+
/**
182+
* @brief Test that the blocking read will return with the available
183+
* data even if the size is less than the requested size.
184+
*/
185+
static void test_usb_cdc_read_blocking(void)
186+
{
187+
test_setup(__func__, sizeof(__func__));
188+
189+
const size_t out_buffer_len = 32;
190+
char out_buffer[out_buffer_len] = {};
191+
192+
ESP_ERROR_CHECK(esp_vfs_dev_cdcacm_register());
193+
194+
esp_vfs_dev_cdcacm_set_rx_line_endings(ESP_LINE_ENDINGS_LF);
195+
esp_vfs_dev_cdcacm_set_tx_line_endings(ESP_LINE_ENDINGS_LF);
196+
197+
/* make sure blocking mode is enabled */
198+
int flags = fcntl(STDIN_FILENO, F_GETFL, 0);
199+
fcntl(STDIN_FILENO, F_SETFL, flags & (~O_NONBLOCK));
200+
201+
// trigger the test environment to send the test message
202+
char ready_msg[] = "ready to receive\n";
203+
write(fileno(stdout), ready_msg, sizeof(ready_msg));
204+
205+
const int nread = read(STDIN_FILENO, out_buffer, out_buffer_len);
206+
TEST_ASSERT(nread > 0);
207+
TEST_ASSERT(nread < out_buffer_len);
208+
209+
fcntl(STDIN_FILENO, F_SETFL, flags);
210+
esp_vfs_dev_cdcacm_set_rx_line_endings(ESP_LINE_ENDINGS_CRLF);
211+
esp_vfs_dev_cdcacm_set_tx_line_endings(ESP_LINE_ENDINGS_CRLF);
212+
vTaskDelay(2); // wait for tasks to exit
213+
}
214+
215+
/**
216+
* @brief Test that the read function does not prematurely return
217+
* on reception of new line character.
218+
*/
178219
static void test_usb_cdc_read_no_exit_on_newline_reception(void)
179220
{
180221
test_setup(__func__, sizeof(__func__));
@@ -222,9 +263,13 @@ static void test_usb_cdc_read_no_exit_on_newline_reception(void)
222263
vTaskDelay(2); // wait for tasks to exit
223264
}
224265

266+
/* Always make sure that the function calling sequence in the main
267+
* function matches the expected order in the pytest function.
268+
*/
225269
void app_main(void)
226270
{
227271
test_usb_cdc_select();
228272
test_usb_cdc_read_non_blocking();
273+
test_usb_cdc_read_blocking();
229274
test_usb_cdc_read_no_exit_on_newline_reception();
230275
}

components/esp_vfs_console/test_apps/usb_cdc_vfs/pytest_usb_cdc_vfs.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ def test_usb_cdc_vfs_default(dut: Dut, test_message: str) -> None:
2727
dut.expect_exact('send_bytes', timeout=2)
2828
dut.write('abcdefgh')
2929

30+
# test run: test_usb_cdc_read_blocking
31+
dut.expect_exact('test_usb_cdc_read_blocking', timeout=2)
32+
dut.expect_exact('ready to receive', timeout=2)
33+
dut.write('testdata')
34+
3035
# test run: test_usb_cdc_read_no_exit_on_newline_reception
3136
dut.expect_exact('test_usb_cdc_read_no_exit_on_newline_reception', timeout=2)
3237
dut.expect_exact('ready to receive', timeout=2)

components/esp_vfs_console/vfs_cdcacm.c

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -176,35 +176,30 @@ static void cdcacm_return_char(int c)
176176

177177
static ssize_t cdcacm_read(int fd, void *data, size_t size)
178178
{
179-
assert(fd == 0);
179+
assert(fd == USB_CDC_LOCAL_FD);
180180
char *data_c = (char *) data;
181181
ssize_t received = 0;
182182
_lock_acquire_recursive(&s_read_lock);
183183

184184
if (s_blocking) {
185-
while (cdcacm_data_length_in_buffer() < size) {
185+
/* in blocking mode, wait for data. by the POSIX standards,
186+
* the amount of available bytes does not need to match the
187+
* requested size to return */
188+
while (cdcacm_data_length_in_buffer() <= 0) {
186189
xSemaphoreTake(s_rx_semaphore, portMAX_DELAY);
187190
}
188191
} else {
189192
/* process pending interrupts before requesting available data */
190193
esp_usb_console_poll_interrupts();
191194
size = MIN(size, cdcacm_data_length_in_buffer());
192195
}
193-
if (s_rx_mode == ESP_LINE_ENDINGS_CR || s_rx_mode == ESP_LINE_ENDINGS_LF) {
194-
/* This is easy. Just receive, and if needed replace \r by \n. */
195-
received = esp_usb_console_read_buf(data_c, size);
196-
if (s_rx_mode == ESP_LINE_ENDINGS_CR) {
197-
/* Change CRs to newlines */
198-
for (ssize_t i = 0; i < received; i++) {
199-
if (data_c[i] == '\r') {
200-
data_c[i] = '\n';
201-
}
202-
}
203-
}
204-
} else {
205-
while (received < size) {
206-
int c = cdcacm_read_char();
207-
if (c == '\r') {
196+
197+
while (received < size) {
198+
int c = cdcacm_read_char();
199+
if (c == '\r') {
200+
if (s_rx_mode == ESP_LINE_ENDINGS_CR) {
201+
c = '\n';
202+
} else if (s_rx_mode == ESP_LINE_ENDINGS_CRLF) {
208203
/* look ahead */
209204
int c2 = cdcacm_read_char();
210205
if (c2 == NONE) {
@@ -221,11 +216,12 @@ static ssize_t cdcacm_read(int fd, void *data, size_t size)
221216
*/
222217
cdcacm_return_char(c2);
223218
}
224-
} else if (c == NONE) {
225-
break;
226219
}
227-
data_c[received++] = (char) c;
220+
221+
} else if (c == NONE) {
222+
break;
228223
}
224+
data_c[received++] = (char) c;
229225
}
230226
_lock_release_recursive(&s_read_lock);
231227
if (received > 0) {

0 commit comments

Comments
 (0)