diff --git a/components/esp_system/test_apps/.build-test-rules.yml b/components/esp_system/test_apps/.build-test-rules.yml index 76aa754ff1..b35ef8434a 100644 --- a/components/esp_system/test_apps/.build-test-rules.yml +++ b/components/esp_system/test_apps/.build-test-rules.yml @@ -3,6 +3,7 @@ components/esp_system/test_apps/console: disable: - if: CONFIG_NAME == "serial_jtag_only" and SOC_USB_SERIAL_JTAG_SUPPORTED != 1 + - if: CONFIG_NAME == "simple" and IDF_TARGET != "esp32" components/esp_system/test_apps/esp_system_unity_tests: disable: diff --git a/components/esp_system/test_apps/console/main/test_app_main.c b/components/esp_system/test_apps/console/main/test_app_main.c index 0740cdfc50..f19736243d 100644 --- a/components/esp_system/test_apps/console/main/test_app_main.c +++ b/components/esp_system/test_apps/console/main/test_app_main.c @@ -4,6 +4,10 @@ * SPDX-License-Identifier: Apache-2.0 */ #include +#include +#include +#include +#include #include "sdkconfig.h" #include "esp_rom_uart.h" @@ -45,6 +49,24 @@ static void console_none_print(void) } #endif +#if CONFIG_VFS_SUPPORT_IO +static void console_open_close_check(void) +{ + printf("Opening /dev/console\n"); + int fd = open("/dev/console", O_RDWR); + assert(fd >= 0 && "Could not open file"); + + const char *msg = "This should be printed to stdout\n"; + + write(fd, msg, strlen(msg)); + + printf("Closing /dev/console\n"); + close(fd); + + printf("This should be printed to stdout\n"); +} +#endif // CONFIG_VFS_SUPPORT_IO + void app_main(void) { printf("Hello World\n"); @@ -52,4 +74,9 @@ void app_main(void) #if CONFIG_ESP_CONSOLE_NONE console_none_print(); #endif // CONFIG_ESP_CONSOLE_NONE + +#if CONFIG_VFS_SUPPORT_IO + console_open_close_check(); +#endif // CONFIG_VFS_SUPPORT_IO + } diff --git a/components/esp_system/test_apps/console/pytest_esp_system_console_tests.py b/components/esp_system/test_apps/console/pytest_esp_system_console_tests.py index 06432a8158..2625d583ac 100644 --- a/components/esp_system/test_apps/console/pytest_esp_system_console_tests.py +++ b/components/esp_system/test_apps/console/pytest_esp_system_console_tests.py @@ -38,12 +38,46 @@ def test_esp_system_console_no_output_uart(dut: Dut) -> None: @pytest.mark.usb_serial_jtag @pytest.mark.parametrize( - 'port, config', + 'port, flash_port, config', [ - pytest.param('/dev/serial_ports/ttyACM-esp32', 'serial_jtag_only', marks=JTAG_SERIAL_MARKS), + pytest.param('/dev/serial_ports/ttyACM-esp32', '/dev/serial_ports/ttyUSB-esp32', 'serial_jtag_only', marks=JTAG_SERIAL_MARKS), ], indirect=True, ) def test_esp_system_console_only_serial_jtag(dut: Dut) -> None: dut.expect('2nd stage bootloader') dut.expect('Hello World') + dut.expect('Opening /dev/console') + dut.expect('This should be printed to stdout') + dut.expect('Closing /dev/console') + dut.expect('This should be printed to stdout') + + +@pytest.mark.usb_serial_jtag +@pytest.mark.parametrize( + 'port, flash_port, config', + [ + pytest.param('/dev/serial_ports/ttyACM-esp32', '/dev/serial_ports/ttyUSB-esp32', 'serial_jtag_only_no_vfs', marks=JTAG_SERIAL_MARKS), + ], + indirect=True, +) +def test_esp_system_console_only_serial_jtag_no_vfs(dut: Dut) -> None: + dut.expect('2nd stage bootloader') + dut.expect('Hello World') + + +@pytest.mark.generic +@pytest.mark.parametrize( + 'config', + [ + pytest.param('simple', marks=pytest.mark.supported_targets), + ], + indirect=True +) +def test_esp_system_console_correct_open_and_close(dut: Dut) -> None: + dut.expect('2nd stage bootloader') + dut.expect('Hello World') + dut.expect('Opening /dev/console') + dut.expect('This should be printed to stdout') + dut.expect('Closing /dev/console') + dut.expect('This should be printed to stdout') diff --git a/components/esp_system/test_apps/console/sdkconfig.ci.simple b/components/esp_system/test_apps/console/sdkconfig.ci.simple new file mode 100644 index 0000000000..e69de29bb2 diff --git a/components/esp_vfs_console/vfs_console.c b/components/esp_vfs_console/vfs_console.c index 980514c1d4..bfcb984f00 100644 --- a/components/esp_vfs_console/vfs_console.c +++ b/components/esp_vfs_console/vfs_console.c @@ -15,6 +15,7 @@ #include "esp_vfs_console.h" #include "sdkconfig.h" #include "esp_private/startup_internal.h" +#include #define STRINGIFY(s) STRINGIFY2(s) #define STRINGIFY2(s) #s @@ -43,8 +44,18 @@ const static esp_vfs_t *primary_vfs = NULL; static vfs_console_context_t vfs_console = {0}; +static size_t s_open_count = 0; + int console_open(const char * path, int flags, int mode) { + if (s_open_count > 0) { + // Underlying fd is already open, so just increment the open count + // and return the same fd + + s_open_count++; + return 0; + } + // Primary port open #if CONFIG_ESP_CONSOLE_UART vfs_console.fd_primary = open("/dev/uart/"STRINGIFY(CONFIG_ESP_CONSOLE_UART_NUM), flags, mode); @@ -58,6 +69,8 @@ int console_open(const char * path, int flags, int mode) #if CONFIG_ESP_CONSOLE_SECONDARY_USB_SERIAL_JTAG vfs_console.fd_secondary = open("/dev/secondary", flags, mode); #endif + + s_open_count++; return 0; } @@ -78,6 +91,18 @@ int console_fstat(int fd, struct stat * st) int console_close(int fd) { + if (s_open_count == 0) { + errno = EBADF; + return -1; + } + + s_open_count--; + + // We don't actually close the underlying fd until the open count reaches 0 + if (s_open_count > 0) { + return 0; + } + // All function calls are to primary, except from write and close, which will be forwarded to both primary and secondary. close(vfs_console.fd_primary); #if CONFIG_ESP_CONSOLE_SECONDARY_USB_SERIAL_JTAG