fix(esp_stdio): remap console select fd bits to sink device

Map logical /dev/console fds to the primary sink fd for select.
This avoids probing UART1/UART2 for stdout/stderr and keeps UART select state consistent.
Add stdio regression checks for logical fd mapping and symmetric uart_end_select teardown.
This commit is contained in:
Tomáš Rohlínek
2026-04-01 15:33:45 +02:00
parent e041542092
commit 59179490a2
4 changed files with 192 additions and 8 deletions
@@ -19,6 +19,7 @@
#include <fcntl.h>
#include <unistd.h>
#include <sys/select.h>
#if CONFIG_ESP_CONSOLE_NONE
/* Set up UART on UART_0 (console) to be able to
@@ -171,6 +172,37 @@ static void stdio_fd_init_check(void)
fprintf(stderr, "STDIO_TEST:STREAM:STDERR\n");
fflush(stderr);
}
#if CONFIG_VFS_SUPPORT_SELECT && CONFIG_ESP_CONSOLE_UART
static void stdio_select_fd_mapping_check(void)
{
bool driver_installed_here = false;
esp_err_t err = uart_driver_install(CONFIG_ESP_CONSOLE_UART_NUM, 256, 0, 0, NULL, 0);
if (err == ESP_OK) {
driver_installed_here = true;
} else {
assert(err == ESP_ERR_INVALID_STATE);
}
fd_set writefds;
FD_ZERO(&writefds);
FD_SET(fileno(stdout), &writefds);
FD_SET(fileno(stderr), &writefds);
struct timeval tv = {
.tv_sec = 0,
.tv_usec = 0,
};
errno = 0;
int ret = select(fileno(stderr) + 1, NULL, &writefds, NULL, &tv);
assert(ret >= 0);
if (driver_installed_here) {
assert(uart_driver_delete(CONFIG_ESP_CONSOLE_UART_NUM) == ESP_OK);
}
printf("STDIO_TEST:SELECT:LOGICAL_FD_MAP_OK\n");
}
#endif
#endif // CONFIG_VFS_SUPPORT_IO
void app_main(void)
@@ -188,6 +220,9 @@ void app_main(void)
close(fd);
stdio_fd_init_check();
#if CONFIG_VFS_SUPPORT_SELECT && CONFIG_ESP_CONSOLE_UART
stdio_select_fd_mapping_check();
#endif
#endif // CONFIG_VFS_SUPPORT_IO
#if CONFIG_ESP_CONSOLE_NONE