Skip to content

Commit b2c2321

Browse files
committed
fix(modem_4g): fix iot_usbh_cdc memory overrun issue
1 parent 77acf37 commit b2c2321

File tree

8 files changed

+51
-16
lines changed

8 files changed

+51
-16
lines changed

components/usb/iot_usbh_cdc/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# ChangeLog
22

3+
## v1.0.3 - 2025-3-27
4+
5+
### Bug Fixes:
6+
7+
* Fixed an incorrect `ring_buffer pop` request value of 0.
8+
* Fixed uninitialized structure issue.
9+
310
## v1.0.2 - 2025-3-13
411

512
### Bug Fixes:

components/usb/iot_usbh_cdc/idf_component.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: "1.0.2"
1+
version: "1.0.3"
22
targets:
33
- esp32s2
44
- esp32s3

components/usb/iot_usbh_cdc/iot_usbh_cdc.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,8 @@ static esp_err_t _ringbuf_pop(RingbufHandle_t ringbuf_hdl, uint8_t *buf, size_t
110110
static esp_err_t _ringbuf_push(RingbufHandle_t ringbuf_hdl, const uint8_t *buf, size_t write_bytes, TickType_t ticks_to_wait)
111111
{
112112
int res = xRingbufferSend(ringbuf_hdl, buf, write_bytes, ticks_to_wait);
113-
114113
if (res != pdTRUE) {
115-
ESP_LOGW(TAG, "The in buffer is too small, the data has been lost");
114+
ESP_LOGW(TAG, "The ringbuffer is full, the data has been lost");
116115
return ESP_FAIL;
117116
}
118117
return ESP_OK;
@@ -392,8 +391,9 @@ static void in_xfer_cb(usb_transfer_t *in_xfer)
392391
if (data_len + in_xfer->actual_num_bytes >= cdc->in_ringbuf_size) {
393392
// TODO: add notify cb for user
394393
// if ringbuffer overflow, drop the data
395-
ESP_LOGD(TAG, "in ringbuf full");
394+
ESP_LOGW(TAG, "CDC in ringbuf is full!");
396395
} else {
396+
ESP_LOG_BUFFER_HEXDUMP(TAG, in_xfer->data_buffer, in_xfer->actual_num_bytes, ESP_LOG_DEBUG);
397397
if (_ringbuf_push(cdc->in_ringbuf_handle, in_xfer->data_buffer, in_xfer->actual_num_bytes, pdMS_TO_TICKS(TIMEOUT_USB_RINGBUF_MS)) != ESP_OK) {
398398
ESP_LOGE(TAG, "in ringbuf push failed");
399399
}
@@ -619,7 +619,7 @@ static esp_err_t _cdc_open(usbh_cdc_t *cdc)
619619
ESP_ERROR_CHECK(usb_host_get_active_config_descriptor(cdc->dev_hdl, &config_desc));
620620
ESP_ERROR_CHECK(usb_host_get_device_descriptor(cdc->dev_hdl, &device_desc));
621621

622-
cdc_parsed_info_t cdc_info;
622+
cdc_parsed_info_t cdc_info = {0};
623623
ret = cdc_parse_interface_descriptor(device_desc, config_desc, cdc->intf_idx, &cdc->data.intf_desc, &cdc_info);
624624
if (ret != ESP_OK) {
625625
goto err;
@@ -795,12 +795,12 @@ esp_err_t usbh_cdc_read_bytes(usbh_cdc_handle_t cdc_handle, const uint8_t *buf,
795795
usbh_cdc_t *cdc = (usbh_cdc_t *) cdc_handle;
796796
ESP_GOTO_ON_FALSE(cdc->state == USBH_CDC_OPEN, ESP_ERR_INVALID_STATE, fail, TAG, "Device is not connected");
797797

798-
size_t data_len = _get_ringbuf_len(cdc->in_ringbuf_handle);
799-
if (data_len > *length) {
800-
data_len = *length;
798+
size_t data_len = *length;
799+
if (data_len > CONFIG_IN_RINGBUFFER_SIZE) {
800+
data_len = CONFIG_IN_RINGBUFFER_SIZE;
801801
}
802802

803-
ret = _ringbuf_pop(cdc->in_ringbuf_handle, (uint8_t *)buf, data_len, length, pdMS_TO_TICKS(TIMEOUT_USB_RINGBUF_MS));
803+
ret = _ringbuf_pop(cdc->in_ringbuf_handle, (uint8_t *)buf, data_len, length, ticks_to_wait);
804804
if (ret != ESP_OK) {
805805
ESP_LOGD(TAG, "cdc read failed");
806806
*length = 0;

components/usb/iot_usbh_modem/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# ChangeLog
22

3+
## v1.1.4 - 2024-3-20
4+
5+
* Support 4g module `YM310_X09`
6+
* Add a debug print
7+
38
## v1.1.3 - 2024-3-20
49

510
* Support 4g module `EG25_GL` and `AIR780E`

components/usb/iot_usbh_modem/Kconfig

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,22 @@ menu "IoT USB Host Modem"
2626
depends on n
2727
config MODEM_TARGET_BG96_MA
2828
bool "BG96MA"
29-
depends on n
3029
config MODEM_TARGET_MC610_EU
3130
bool "MC610_EU"
3231
config MODEM_TARGET_EC20_CE
3332
bool "EC20_CE"
3433
config MODEM_TARGET_EG25_GL
3534
bool "EC25_GL"
35+
config MODEM_TARGET_YM310_X09
36+
bool "YM310_X09"
37+
config MODEM_TARGET_SIM7600E
38+
bool "SIM7600E"
39+
config MODEM_TARGET_A7670E
40+
bool "A7670E"
41+
config MODEM_TARGET_SIM7070G
42+
bool "SIM7070G"
43+
config MODEM_TARGET_SIM7080
44+
bool "SIM7080"
3645
endchoice
3746

3847
config MODEM_TARGET_NAME
@@ -45,13 +54,19 @@ menu "IoT USB Host Modem"
4554
default "MC610_EU" if MODEM_TARGET_MC610_EU
4655
default "EC20_CE" if MODEM_TARGET_EC20_CE
4756
default "EG25_GL" if MODEM_TARGET_EG25_GL
57+
default "YM310_X09" if MODEM_TARGET_YM310_X09
58+
default "SIM7600E" if MODEM_TARGET_SIM7600E
59+
default "A7670E" if MODEM_TARGET_A7670E
60+
default "SIM7070G" if MODEM_TARGET_SIM7070G
61+
default "SIM7080G" if MODEM_TARGET_SIM7080G
4862
default "User Defined" if MODEM_TARGET_USER
4963

5064
config MODEM_SUPPORT_SECONDARY_AT_PORT
5165
bool "Enable Dual CDC Mode"
52-
depends on (MODEM_TARGET_MC610_EU || MODEM_TARGET_USER || MODEM_TARGET_EG25_GL)
66+
depends on (MODEM_TARGET_MC610_EU || MODEM_TARGET_USER || MODEM_TARGET_EG25_GL || MODEM_TARGET_A7670E)
5367
default y if MODEM_TARGET_MC610_EU
5468
default y if MODEM_TARGET_EG25_GL
69+
default y if MODEM_TARGET_A7670E
5570
default n if MODEM_TARGET_USER
5671

5772
menu "USB CDC interface config"
@@ -66,6 +81,12 @@ menu "IoT USB Host Modem"
6681
default 0x02 if MODEM_TARGET_EC20_CE
6782
default 0x03 if MODEM_TARGET_AIR780_E
6883
default 0x03 if MODEM_TARGET_EG25_GL
84+
default 0x03 if MODEM_TARGET_YM310_X09
85+
default 0x02 if MODEM_TARGET_BG96_MA
86+
default 0x03 if MODEM_TARGET_SIM7600E
87+
default 0x04 if MODEM_TARGET_A7670E
88+
default 0x02 if MODEM_TARGET_SIM7070G
89+
default 0x02 if MODEM_TARGET_SIM7080G
6990
default 0x00 if MODEM_TARGET_USER
7091
help
7192
USB CDC interface (eg.0x00) used for send AT/data to device
@@ -76,6 +97,7 @@ menu "IoT USB Host Modem"
7697
default 0x01 if MODEM_TARGET_USER
7798
default 0x05 if MODEM_TARGET_MC610_EU
7899
default 0x02 if MODEM_TARGET_EG25_GL
100+
default 0x05 if MODEM_TARGET_A7670E
79101
help
80102
USB CDC interface 2 (eg.0x01) used for send AT/data to device
81103
endmenu

components/usb/iot_usbh_modem/idf_component.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: "1.1.3"
1+
version: "1.1.4"
22
targets:
33
- esp32s2
44
- esp32s3

components/usb/iot_usbh_modem/src/esp_modem_dce.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ esp_err_t esp_modem_dce_default_destroy(esp_modem_dce_t *dce)
101101
esp_err_t esp_modem_dce_handle_response_default(esp_modem_dce_t *dce, const char *line)
102102
{
103103
esp_err_t err = ESP_FAIL;
104+
ESP_LOGD(TAG, "Received line:%s\n", line);
104105
if (strstr(line, MODEM_RESULT_CODE_SUCCESS)) {
105106
err = esp_modem_process_command_done(dce, ESP_MODEM_STATE_SUCCESS);
106107
} else if (strstr(line, MODEM_RESULT_CODE_ERROR)) {

examples/usb/host/usb_cdc_basic/main/usb_cdc_basic_main.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -92,9 +92,9 @@ void app_main(void)
9292
usbh_cdc_create(&dev_config, &handle[0]);
9393

9494
#if (EXAMPLE_BULK_ITF_NUM > 1)
95-
config.itf_num = 1;
96-
config.cbs = {0};
97-
ESP_LOGI(TAG, "Open interface num: %d with first USB CDC Device", config.itf_num);
95+
dev_config.itf_num = 3;
96+
memset(&dev_config.cbs, 0, sizeof(dev_config.cbs));
97+
ESP_LOGI(TAG, "Open interface num: %d with first USB CDC Device", dev_config.itf_num);
9898
usbh_cdc_create(&dev_config, &handle[1]);
9999
#endif
100100
/*!< Wait for the USB device to be connected */

0 commit comments

Comments
 (0)