fix(ble): Fix assertion caused by ring buffer read size mismatch expected rx size

This commit is contained in:
xwc
2025-12-25 16:08:22 +08:00
committed by xiongweichao
parent b5aae9a1af
commit d117e8d59b

View File

@@ -161,20 +161,29 @@ static void uhci_rx_done_process_task(void *arg)
{
size_t item_size = 0;
uint8_t *data = NULL;
while (1) {
xSemaphoreTake(uart_env.rx_process_sema, portMAX_DELAY);
data = xRingbufferReceiveUpTo(uart_env.ringbufhandle, &item_size, portMAX_DELAY, uart_env.rx.size);
if (item_size != uart_env.rx.size) {
assert(0);
}
memcpy(uart_env.rx.buf, data, item_size);
vRingbufferReturnItem(uart_env.ringbufhandle, data);
size_t items_waiting = 0;
xSemaphoreTake(uart_env.rx_process_sema, portMAX_DELAY);
while (1) {
data = xRingbufferReceiveUpTo(uart_env.ringbufhandle, &item_size, portMAX_DELAY, uart_env.rx.size);
// prepare next receive
if (uart_env.rx_eof) {
uhci_receive(uart_env.uhci_handle, uart_env.rx_data_buffer, UHCI_MAX_RX_BUFFER_SIZE);
uart_env.rx_eof = false;
}
memcpy(uart_env.rx.buf, data, item_size);
vRingbufferReturnItem(uart_env.ringbufhandle, data);
uart_env.rx.size -= item_size;
uart_env.rx.buf += item_size;
if (uart_env.rx.size) {
vRingbufferGetInfo(uart_env.ringbufhandle, NULL, NULL, NULL, NULL, &items_waiting);
if (items_waiting < uart_env.rx.size) {
ESP_LOGW(tag, "Ring buffer items size less than expected");
}
continue;
}
assert(uart_env.rx.callback != NULL);
esp_bt_hci_tl_callback_t callback = uart_env.rx.callback;
void *arg = uart_env.rx.arg;
@@ -182,13 +191,14 @@ static void uhci_rx_done_process_task(void *arg)
// clear callback pointer
uart_env.rx.callback = NULL;
uart_env.rx.arg = NULL;
uart_env.rx.size = 0;
// call handler
callback(arg, ESP_BT_HCI_TL_STATUS_OK);
// send notification to Bluetooth Controller task
esp_bt_h4tl_eif_io_event_notify(1);
xSemaphoreTake(uart_env.rx_process_sema, portMAX_DELAY);
}
}