Skip to content

Commit 3621aa6

Browse files
committed
Merge branch 'fix/iot_cdc_interface_bug' into 'master'
fix(iot_usb_cdc): fix crash when open not setting's itf See merge request ae_group/esp-iot-solution!1232
2 parents b727a83 + ad32b50 commit 3621aa6

File tree

7 files changed

+34
-15
lines changed

7 files changed

+34
-15
lines changed

components/usb/iot_usbh_cdc/CHANGELOG.md

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

3+
## v1.0.1 - 2025-2-26
4+
5+
### Bug Fixes:
6+
7+
* Fix the cdc can't open the setting's interface cause crash issue.
8+
39
## v1.0.0 - 2024-10-31
410

511
### Break Changes:

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.0"
1+
version: "1.0.1"
22
targets:
33
- esp32s2
44
- esp32s3

components/usb/iot_usbh_cdc/iot_usbh_cdc.c

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -227,8 +227,8 @@ static void usb_event_cb(const usb_host_client_event_msg_t *event_msg, void *arg
227227
const usb_device_desc_t *device_desc;
228228
ESP_ERROR_CHECK(usb_host_get_device_descriptor(current_device, &device_desc));
229229

230-
bool opened = false;
231230
usbh_cdc_t *cdc;
231+
bool if_opened = false;
232232
SLIST_FOREACH(cdc, &p_usbh_cdc_obj->cdc_devices_list, list_entry) {
233233
if ((cdc->vid != CDC_HOST_ANY_VID && cdc->vid != device_desc->idVendor) ||
234234
(cdc->pid != CDC_HOST_ANY_PID && cdc->pid != device_desc->idProduct)) {
@@ -243,14 +243,14 @@ static void usb_event_cb(const usb_host_client_event_msg_t *event_msg, void *arg
243243
if (_cdc_open(cdc) != ESP_OK) {
244244
ESP_LOGE(TAG, "Failed to open cdc device: %d", event_msg->new_dev.address);
245245
} else {
246-
opened = true;
246+
if_opened = true;
247247
ESP_LOGI(TAG, "Opened cdc device: %d", event_msg->new_dev.address);
248248
}
249249
}
250-
if (!opened) {
250+
251+
if (!if_opened) {
251252
usb_host_device_close(p_usbh_cdc_obj->cdc_client_hdl, current_device); // Gracefully continue on error
252253
}
253-
254254
ESP_LOGI(TAG, "New device connected, address: %d", event_msg->new_dev.address);
255255
break;
256256
}
@@ -394,7 +394,9 @@ static void in_xfer_cb(usb_transfer_t *in_xfer)
394394
// if ringbuffer overflow, drop the data
395395
ESP_LOGD(TAG, "in ringbuf full");
396396
} else {
397-
_ringbuf_push(cdc->in_ringbuf_handle, in_xfer->data_buffer, in_xfer->actual_num_bytes, pdMS_TO_TICKS(TIMEOUT_USB_RINGBUF_MS));
397+
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) {
398+
ESP_LOGE(TAG, "in ringbuf push failed");
399+
}
398400
}
399401

400402
usb_host_transfer_submit(in_xfer);
@@ -607,6 +609,10 @@ static esp_err_t _cdc_open(usbh_cdc_t *cdc)
607609
assert(cdc);
608610
ESP_RETURN_ON_FALSE(cdc->state == USBH_CDC_CLOSE, ESP_OK, TAG,);
609611

612+
if (cdc->cbs.connect) {
613+
cdc->cbs.connect((usbh_cdc_handle_t)cdc, cdc->cbs.user_data);
614+
}
615+
610616
// Get Device and Configuration descriptors
611617
const usb_config_desc_t *config_desc;
612618
const usb_device_desc_t *device_desc;
@@ -616,7 +622,7 @@ static esp_err_t _cdc_open(usbh_cdc_t *cdc)
616622
cdc_parsed_info_t cdc_info;
617623
ret = cdc_parse_interface_descriptor(device_desc, config_desc, cdc->intf_idx, &cdc->data.intf_desc, &cdc_info);
618624
if (ret != ESP_OK) {
619-
usb_host_device_close(p_usbh_cdc_obj->cdc_client_hdl, cdc->dev_hdl); // Gracefully continue on error
625+
goto err;
620626
}
621627

622628
_ring_buffer_flush(cdc->in_ringbuf_handle);
@@ -632,14 +638,9 @@ static esp_err_t _cdc_open(usbh_cdc_t *cdc)
632638
cdc->vid = device_desc->idVendor;
633639
cdc->pid = device_desc->idProduct;
634640

635-
if (cdc->cbs.connect) {
636-
cdc->cbs.connect((usbh_cdc_handle_t)cdc, cdc->cbs.user_data);
637-
}
638-
639641
return ESP_OK;
640642

641643
err:
642-
usb_host_device_close(p_usbh_cdc_obj->cdc_client_hdl, cdc->dev_hdl); // Gracefully continue on error
643644
return ret;
644645
}
645646

@@ -719,6 +720,9 @@ esp_err_t usbh_cdc_create(const usbh_cdc_device_config_t *config, usbh_cdc_handl
719720
if (cdc->data.out_xfer_free_sem) {
720721
vSemaphoreDelete(cdc->data.out_xfer_free_sem);
721722
}
723+
if (cdc->dev_hdl) {
724+
usb_host_device_close(p_usbh_cdc_obj->cdc_client_hdl, cdc->dev_hdl);
725+
}
722726
if (cdc) {
723727
free(cdc);
724728
}
@@ -842,7 +846,6 @@ esp_err_t usbh_cdc_desc_print(usbh_cdc_handle_t cdc_handle)
842846
{
843847
ESP_RETURN_ON_FALSE(cdc_handle != NULL, ESP_ERR_INVALID_ARG, TAG, "cdc_handle is NULL");
844848
usbh_cdc_t *cdc = (usbh_cdc_t *) cdc_handle;
845-
ESP_RETURN_ON_FALSE(cdc->state == USBH_CDC_OPEN, ESP_ERR_INVALID_STATE, TAG, "Device is not connected");
846849
ESP_RETURN_ON_FALSE(cdc->dev_hdl != NULL, ESP_ERR_INVALID_STATE, TAG, "Device is not open yet");
847850

848851
const usb_device_desc_t *device_desc;

components/usb/iot_usbh_modem/CHANGELOG.md

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

3+
## v1.1.1 - 2024-11-28
4+
5+
* Add Kconfig MODEM_PRINT_DEVICE_DESCRIPTOR
6+
37
## v1.1.0 - 2024-11-28
48

59
* Support ESP32-P4 with EC20_CE cat.4 module

components/usb/iot_usbh_modem/Kconfig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ menu "IoT USB Host Modem"
8686
help
8787
Set Pin of sim card if have
8888

89+
config MODEM_PRINT_DEVICE_DESCRIPTOR
90+
bool "Print device descriptor"
91+
default y
92+
8993
menu "GPIO Config"
9094
config MODEM_POWER_GPIO
9195
int "Modem power control gpio, set 0 if not use"

components/usb/iot_usbh_modem/src/esp_modem_usb_dte.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,9 @@ static void _usb_conn_callback(usbh_cdc_handle_t cdc_handle, void *arg)
186186
esp_modem_dte_internal_t *esp_dte = (esp_modem_dte_internal_t *)arg;
187187
esp_dte->conn_state = 1;
188188

189+
#if CONFIG_MODEM_PRINT_DEVICE_DESCRIPTOR
189190
usbh_cdc_desc_print(cdc_handle);
191+
#endif
190192

191193
if (esp_dte->conn_callback) {
192194
esp_dte->conn_callback(cdc_handle, NULL);

examples/usb/host/usb_cdc_4g_module/main/idf_component.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ dependencies:
66
json_parser:
77
version: "1.0.3"
88
iot_usbh_modem:
9-
version: "^1.0.0"
9+
version: "^1.1.0"
1010
override_path: "../../../../../components/usb/iot_usbh_modem"
1111
espressif/esp32_s3_usb_otg:
1212
version: "^1.5.1"

0 commit comments

Comments
 (0)