Selaa lähdekoodia

esp_system, vfs: fix incomplete blocking reads in vfs_cdcacm

Blocking read from cdcacm VFS could return less bytes than requested.
This didn’t match the behaviour of other VFS drivers, and higher level
code could misbehave.
Ivan Grokhotkov 3 vuotta sitten
vanhempi
sitoutus
bf10146a15

+ 6 - 14
components/esp_system/include/esp_private/usb_console.h

@@ -1,16 +1,8 @@
-// Copyright 2019-2020 Espressif Systems (Shanghai) PTE LTD
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-//     http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
+/*
+ * SPDX-FileCopyrightText: 2019-2022 Espressif Systems (Shanghai) CO LTD
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
 
 #pragma once
 
@@ -62,7 +54,7 @@ ssize_t esp_usb_console_flush(void);
 
 ssize_t esp_usb_console_read_buf(char* buf, size_t buf_size);
 
-bool esp_usb_console_read_available(void);
+ssize_t esp_usb_console_available_for_read(void);
 
 bool esp_usb_console_write_available(void);
 

+ 4 - 4
components/esp_system/port/soc/esp32s2/usb_console.c

@@ -386,7 +386,7 @@ ssize_t esp_usb_console_read_buf(char *buf, size_t buf_size)
     if (s_cdc_acm_device == NULL) {
         return -1;
     }
-    if (!esp_usb_console_read_available()) {
+    if (esp_usb_console_available_for_read() == 0) {
         return 0;
     }
     int bytes_read = cdc_acm_fifo_read(s_cdc_acm_device, (uint8_t*) buf, buf_size);
@@ -414,12 +414,12 @@ esp_err_t esp_usb_console_set_cb(esp_usb_console_cb_t rx_cb, esp_usb_console_cb_
     return ESP_OK;
 }
 
-bool esp_usb_console_read_available(void)
+ssize_t esp_usb_console_available_for_read(void)
 {
     if (s_cdc_acm_device == NULL) {
-        return false;
+        return -1;
     }
-    return cdc_acm_rx_fifo_cnt(s_cdc_acm_device) > 0;
+    return cdc_acm_rx_fifo_cnt(s_cdc_acm_device);
 }
 
 bool esp_usb_console_write_available(void)

+ 8 - 7
components/vfs/vfs_cdcacm.c

@@ -125,15 +125,16 @@ static int cdcacm_read_char(void)
     }
 }
 
-static bool cdcacm_data_in_buffer(void)
+static ssize_t cdcacm_data_length_in_buffer(void)
 {
-    if (s_peek_char != NONE) {
-        return true;
+    ssize_t len = esp_usb_console_available_for_read();
+    if (len < 0) {
+        len = 0;
     }
-    if (esp_usb_console_read_available()) {
-        return true;
+    if (s_peek_char != NONE) {
+        len += 1;
     }
-    return false;
+    return len;
 }
 
 /* Push back a character; it will be returned by next call to cdcacm_read_char */
@@ -150,7 +151,7 @@ static ssize_t cdcacm_read(int fd, void *data, size_t size)
     ssize_t received = 0;
     _lock_acquire_recursive(&s_read_lock);
 
-    while (!cdcacm_data_in_buffer()) {
+    while (cdcacm_data_length_in_buffer() < size) {
         if (!s_blocking) {
             errno = EWOULDBLOCK;
             _lock_release_recursive(&s_read_lock);

+ 0 - 1
tools/ci/check_copyright_ignore.txt

@@ -644,7 +644,6 @@ components/esp_system/include/esp_int_wdt.h
 components/esp_system/include/esp_private/dbg_stubs.h
 components/esp_system/include/esp_private/panic_internal.h
 components/esp_system/include/esp_private/system_internal.h
-components/esp_system/include/esp_private/usb_console.h
 components/esp_system/include/esp_task.h
 components/esp_system/port/arch/riscv/expression_with_stack.c
 components/esp_system/port/arch/xtensa/expression_with_stack.c