From c2a4dd0c681c2228ec161f2ba3bacd5f2d43b5f1 Mon Sep 17 00:00:00 2001 From: Roland Dobai Date: Tue, 14 Jan 2020 13:48:36 +0100 Subject: [PATCH] VFS: Check in select() if the UART driver is installed or not Closes https://github.com/espressif/esp-idf/issues/4627 --- components/driver/include/driver/uart.h | 11 +++++++++++ components/driver/uart.c | 9 ++++++--- components/vfs/vfs_uart.c | 8 ++++++++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/components/driver/include/driver/uart.h b/components/driver/include/driver/uart.h index 57f4f7904..b0596a8cf 100644 --- a/components/driver/include/driver/uart.h +++ b/components/driver/include/driver/uart.h @@ -109,6 +109,17 @@ esp_err_t uart_driver_install(uart_port_t uart_num, int rx_buffer_size, int tx_b */ esp_err_t uart_driver_delete(uart_port_t uart_num); +/** + * @brief Checks whether the driver is installed or not + * + * @param uart_num UART port number, the max port number is (UART_NUM_MAX -1). + * + * @return + * - true driver is installed + * - false driver is not installed + */ +bool uart_is_driver_installed(uart_port_t uart_num); + /** * @brief Set UART data bits. * diff --git a/components/driver/uart.c b/components/driver/uart.c index 9750ac486..3fe1862c7 100644 --- a/components/driver/uart.c +++ b/components/driver/uart.c @@ -1355,8 +1355,6 @@ err: return r; } -int a = 0; - //Make sure no other tasks are still using UART before you call this function esp_err_t uart_driver_delete(uart_port_t uart_num) { @@ -1410,6 +1408,11 @@ esp_err_t uart_driver_delete(uart_port_t uart_num) return ESP_OK; } +bool uart_is_driver_installed(uart_port_t uart_num) +{ + return uart_num < UART_NUM_MAX && (p_uart_obj[uart_num] != NULL); +} + void uart_set_select_notif_callback(uart_port_t uart_num, uart_select_notif_callback_t uart_select_notif_callback) { if (uart_num < UART_NUM_MAX && p_uart_obj[uart_num]) { @@ -1538,4 +1541,4 @@ esp_err_t uart_set_loop_back(uart_port_t uart_num, bool loop_back_en) UART_CHECK((uart_num < UART_NUM_MAX), "uart_num error", ESP_ERR_INVALID_ARG); uart_hal_set_loop_back(&(uart_context[uart_num].hal), loop_back_en); return ESP_OK; -} \ No newline at end of file +} diff --git a/components/vfs/vfs_uart.c b/components/vfs/vfs_uart.c index 28e0d6f79..dc430b82b 100644 --- a/components/vfs/vfs_uart.c +++ b/components/vfs/vfs_uart.c @@ -433,6 +433,14 @@ static esp_err_t uart_start_select(int nfds, fd_set *readfds, fd_set *writefds, const int max_fds = MIN(nfds, UART_NUM); *end_select_args = NULL; + for (int i = 0; i < max_fds; ++i) { + if (FD_ISSET(i, readfds) || FD_ISSET(i, writefds) || FD_ISSET(i, exceptfds)) { + if (!uart_is_driver_installed(i)) { + return ESP_ERR_INVALID_STATE; + } + } + } + uart_select_args_t *args = malloc(sizeof(uart_select_args_t)); if (args == NULL) {