]> granicus.if.org Git - esp-idf/commitdiff
VFS: select() on UART should return immediately when data is buffered
authorRoland Dobai <dobai.roland@gmail.com>
Tue, 27 Nov 2018 11:53:19 +0000 (12:53 +0100)
committerRoland Dobai <dobai.roland@gmail.com>
Tue, 27 Nov 2018 12:39:00 +0000 (13:39 +0100)
components/vfs/vfs_uart.c
examples/peripherals/uart_select/main/uart_select_example_main.c

index 594002d78fa07902298f1b849959cfb76e3bc1fe..ebff91a879fe85771fc57ad0af552487894bef58 100644 (file)
@@ -393,6 +393,17 @@ static esp_err_t uart_start_select(int nfds, fd_set *readfds, fd_set *writefds,
     FD_ZERO(writefds);
     FD_ZERO(exceptfds);
 
+    for (int i = 0; i < max_fds; ++i) {
+        if (FD_ISSET(i, _readfds_orig)) {
+            size_t buffered_size;
+            if (uart_get_buffered_data_len(i, &buffered_size) == ESP_OK && buffered_size > 0) {
+                // signalize immediately when data is buffered
+                FD_SET(i, _readfds);
+                esp_vfs_select_triggered(_signal_sem);
+            }
+        }
+    }
+
     portEXIT_CRITICAL(uart_get_selectlock());
     // s_one_select_lock is not released on successfull exit - will be
     // released in uart_end_select()
index 62bca5698ce3b6335f6d3d8836423aa521d721cf..717042f27a7b70a476bd75ee4639ad068efd0e17 100644 (file)
@@ -66,7 +66,10 @@ static void uart_select_task()
                 if (FD_ISSET(fd, &rfds)) {
                     char buf;
                     if (read(fd, &buf, 1) > 0) {
-                            ESP_LOGI(TAG, "Received: %c", buf);
+                        ESP_LOGI(TAG, "Received: %c", buf);
+                        // Note: Only one character was read even the buffer contains more. The other characters will
+                        // be read one-by-one by subsequent calls to select() which will then return immediately
+                        // without timeout.
                     } else {
                         ESP_LOGE(TAG, "UART read error");
                         break;