Skip to content

Commit

Permalink
Merge branch 'bugfix/fix_some_ble_bugs_cjh_v5.3' into 'release/v5.3'
Browse files Browse the repository at this point in the history
Fixed some BLE bugs 240812 (backport v5.3)

See merge request espressif/esp-idf!32792
  • Loading branch information
Isl2017 committed Sep 5, 2024
2 parents cd1da85 + 023aada commit 1720dc7
Show file tree
Hide file tree
Showing 17 changed files with 118 additions and 94 deletions.
14 changes: 14 additions & 0 deletions components/bt/common/include/bt_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#define OSI_INITIAL_TRACE_LEVEL UC_BT_LOG_OSI_TRACE_LEVEL
#define BLUFI_INITIAL_TRACE_LEVEL UC_BT_LOG_BLUFI_TRACE_LEVEL

// MEMORY
#if UC_BT_BLE_DYNAMIC_ENV_MEMORY
#define BT_BLE_DYNAMIC_ENV_MEMORY TRUE
#define BTC_DYNAMIC_MEMORY TRUE
Expand All @@ -64,6 +65,19 @@
#define BT_BLE_DYNAMIC_ENV_MEMORY FALSE
#endif

#if UC_HEAP_ALLOCATION_FROM_SPIRAM_FIRST
#define HEAP_ALLOCATION_FROM_SPIRAM_FIRST TRUE
#else
#define HEAP_ALLOCATION_FROM_SPIRAM_FIRST FALSE
#endif

#if UC_BT_ABORT_WHEN_ALLOCATION_FAILS
#define HEAP_ALLOCATION_FAILS_ABORT TRUE
#else
#define HEAP_ALLOCATION_FAILS_ABORT FALSE
#endif

// HCI LOG
#if UC_BT_HCI_LOG_DEBUG_EN
#define BT_HCI_LOG_INCLUDED UC_BT_HCI_LOG_DEBUG_EN
#else
Expand Down
15 changes: 14 additions & 1 deletion components/bt/common/include/bt_user_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,26 @@
#define UC_BT_BLUFI_ENABLE FALSE
#endif

//MEMORY DEBUG
//MEMORY
#ifdef CONFIG_BT_BLUEDROID_MEM_DEBUG
#define UC_BT_BLUEDROID_MEM_DEBUG TRUE
#else
#define UC_BT_BLUEDROID_MEM_DEBUG FALSE
#endif

#ifdef CONFIG_BT_ALLOCATION_FROM_SPIRAM_FIRST
#define UC_HEAP_ALLOCATION_FROM_SPIRAM_FIRST CONFIG_BT_ALLOCATION_FROM_SPIRAM_FIRST
#else
#define UC_HEAP_ALLOCATION_FROM_SPIRAM_FIRST FALSE
#endif

#ifdef CONFIG_BT_ABORT_WHEN_ALLOCATION_FAILS
#define UC_BT_ABORT_WHEN_ALLOCATION_FAILS CONFIG_BT_ABORT_WHEN_ALLOCATION_FAILS
#else
#define UC_BT_ABORT_WHEN_ALLOCATION_FAILS FALSE
#endif

//HCI LOG
#ifdef CONFIG_BT_HCI_LOG_DEBUG_EN
#define UC_BT_HCI_LOG_DEBUG_EN TRUE
#else
Expand Down
51 changes: 18 additions & 33 deletions components/bt/common/osi/allocator.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,48 +213,33 @@ char *osi_strdup(const char *str)

void *osi_malloc_func(size_t size)
{
#if HEAP_MEMORY_DEBUG
void *p;
#if HEAP_ALLOCATION_FROM_SPIRAM_FIRST
p = heap_caps_malloc_prefer(size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL);
#else
p = malloc(size);
#endif /* #if HEAP_ALLOCATION_FROM_SPIRAM_FIRST */
osi_mem_dbg_record(p, size, __func__, __LINE__);
void *p = osi_malloc_base(size);

if (size != 0 && p == NULL) {
OSI_TRACE_ERROR("malloc failed (caller=%p size=%u)\n", __builtin_return_address(0), size);
#if HEAP_ALLOCATION_FAILS_ABORT
assert(0);
#endif
}

return p;
#else
#if HEAP_ALLOCATION_FROM_SPIRAM_FIRST
return heap_caps_malloc_prefer(size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL);
#else
return malloc(size);
#endif /* #if HEAP_ALLOCATION_FROM_SPIRAM_FIRST */
#endif /* #if HEAP_MEMORY_DEBUG */
}

void *osi_calloc_func(size_t size)
{
#if HEAP_MEMORY_DEBUG
void *p;
#if HEAP_ALLOCATION_FROM_SPIRAM_FIRST
p = heap_caps_calloc_prefer(1, size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL);
#else
p = calloc(1, size);
#endif /* #if HEAP_ALLOCATION_FROM_SPIRAM_FIRST */
osi_mem_dbg_record(p, size, __func__, __LINE__);
void *p = osi_calloc_base(size);

if (size != 0 && p == NULL) {
OSI_TRACE_ERROR("calloc failed (caller=%p size=%u)\n", __builtin_return_address(0), size);
#if HEAP_ALLOCATION_FAILS_ABORT
assert(0);
#endif
}

return p;
#else
#if HEAP_ALLOCATION_FROM_SPIRAM_FIRST
return heap_caps_calloc_prefer(1, size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL);
#else
return calloc(1, size);
#endif /* #if HEAP_ALLOCATION_FROM_SPIRAM_FIRST */
#endif /* #if HEAP_MEMORY_DEBUG */
}

void osi_free_func(void *ptr)
{
#if HEAP_MEMORY_DEBUG
osi_mem_dbg_clean(ptr, __func__, __LINE__);
#endif
free(ptr);
}
13 changes: 9 additions & 4 deletions components/bt/common/osi/include/osi/allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,18 @@ do { \

#else

// Memory alloc function without print and assertion
#if HEAP_ALLOCATION_FROM_SPIRAM_FIRST
#define osi_malloc(size) heap_caps_malloc_prefer(size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL)
#define osi_calloc(size) heap_caps_calloc_prefer(1, size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL)
#define osi_malloc_base(size) heap_caps_malloc_prefer(size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL)
#define osi_calloc_base(size) heap_caps_calloc_prefer(1, size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL)
#else
#define osi_malloc(size) malloc((size))
#define osi_calloc(size) calloc(1, (size))
#define osi_malloc_base(size) malloc((size))
#define osi_calloc_base(size) calloc(1, (size))
#endif /* #if HEAP_ALLOCATION_FROM_SPIRAM_FIRST */

// Memory alloc function with print and assertion when fails
#define osi_malloc(size) osi_malloc_func((size))
#define osi_calloc(size) osi_calloc_func((size))
#define osi_free(p) free((p))

#endif /* HEAP_MEMORY_DEBUG */
Expand Down
4 changes: 2 additions & 2 deletions components/bt/common/osi/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -271,10 +271,10 @@ osi_thread_t *osi_thread_create(const char *name, size_t stack_size, int priorit
}

for (int i = 0; i < thread->work_queue_num; i++) {
if (thread->work_queues[i]) {
if (thread->work_queues && thread->work_queues[i]) {
osi_work_queue_delete(thread->work_queues[i]);
thread->work_queues[i] = NULL;
}
thread->work_queues[i] = NULL;
}

if (thread->work_queues) {
Expand Down
7 changes: 7 additions & 0 deletions components/bt/host/bluedroid/Kconfig.in
Original file line number Diff line number Diff line change
Expand Up @@ -1223,3 +1223,10 @@ config BT_BLE_HIGH_DUTY_ADV_INTERVAL
default n
help
This enable BLE high duty advertising interval feature

config BT_ABORT_WHEN_ALLOCATION_FAILS
bool "Abort when memory allocation fails in BT/BLE stack"
depends on BT_BLUEDROID_ENABLED
default n
help
This enables abort when memory allocation fails
8 changes: 8 additions & 0 deletions components/bt/host/bluedroid/api/include/api/esp_bt_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ typedef enum {
ESP_BT_STATUS_HCI_CONN_TOUT_DUE_TO_MIC_FAILURE,
ESP_BT_STATUS_HCI_CONN_FAILED_ESTABLISHMENT,
ESP_BT_STATUS_HCI_MAC_CONNECTION_FAILED,
ESP_BT_STATUS_HCI_CCA_REJECTED,
ESP_BT_STATUS_HCI_TYPE0_SUBMAP_NOT_DEFINED,
ESP_BT_STATUS_HCI_UNKNOWN_ADV_ID,
ESP_BT_STATUS_HCI_LIMIT_REACHED,
ESP_BT_STATUS_HCI_OPT_CANCEL_BY_HOST,
ESP_BT_STATUS_HCI_PKT_TOO_LONG,
ESP_BT_STATUS_HCI_TOO_LATE,
ESP_BT_STATUS_HCI_TOO_EARLY,
} esp_bt_status_t;


Expand Down
10 changes: 7 additions & 3 deletions components/bt/host/bluedroid/btc/core/btc_util.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -352,7 +352,7 @@ esp_bt_status_t btc_hci_to_esp_status(uint8_t hci_status)
esp_bt_status_t btc_btm_status_to_esp_status (uint8_t btm_status)
{
esp_bt_status_t esp_status = ESP_BT_STATUS_FAIL;
switch(btm_status){
switch(btm_status) {
case BTM_SUCCESS:
esp_status = ESP_BT_STATUS_SUCCESS;
break;
Expand Down Expand Up @@ -381,7 +381,11 @@ esp_bt_status_t btc_btm_status_to_esp_status (uint8_t btm_status)
esp_status = ESP_BT_STATUS_FAIL;
break;
default:
esp_status = ESP_BT_STATUS_FAIL;
if (btm_status & BTM_HCI_ERROR) {
esp_status = ESP_BT_STATUS_BASE_FOR_HCI_ERR | (btm_status & 0x7F);
} else {
esp_status = ESP_BT_STATUS_FAIL;
}
break;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,20 +381,6 @@
* Memory reference
**********************************************************/

//MEMORY ALLOCATOR
#ifdef CONFIG_BT_ALLOCATION_FROM_SPIRAM_FIRST
#define UC_HEAP_ALLOCATION_FROM_SPIRAM_FIRST CONFIG_BT_ALLOCATION_FROM_SPIRAM_FIRST
#else
#define UC_HEAP_ALLOCATION_FROM_SPIRAM_FIRST FALSE
#endif

//MEMORY DEBUG
#ifdef CONFIG_BT_BLUEDROID_MEM_DEBUG
#define UC_BT_BLUEDROID_MEM_DEBUG CONFIG_BT_BLUEDROID_MEM_DEBUG
#else
#define UC_BT_BLUEDROID_MEM_DEBUG FALSE
#endif

//ESP COEXIST VSC
#ifdef CONFIG_BT_BLUEDROID_ESP_COEX_VSC
#define UC_BT_BLUEDROID_ESP_COEX_VSC CONFIG_BT_BLUEDROID_ESP_COEX_VSC
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2378,12 +2378,6 @@ The maximum number of payload octets that the local device can receive in a sing
#define BTSNOOP_MEM FALSE
#endif

#if UC_HEAP_ALLOCATION_FROM_SPIRAM_FIRST
#define HEAP_ALLOCATION_FROM_SPIRAM_FIRST TRUE
#else
#define HEAP_ALLOCATION_FROM_SPIRAM_FIRST FALSE
#endif

#include "common/bt_trace.h"

#endif /* BT_TARGET_H */
2 changes: 1 addition & 1 deletion components/bt/host/bluedroid/hci/hci_hal_h4.c
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ static int host_recv_pkt_cb(uint8_t *data, uint16_t len)
}
#endif
pkt_size = BT_PKT_LINKED_HDR_SIZE + BT_HDR_SIZE + len;
linked_pkt = (pkt_linked_item_t *) osi_calloc(pkt_size);
linked_pkt = (pkt_linked_item_t *) osi_calloc_base(pkt_size);
if (!linked_pkt) {
#if (BLE_ADV_REPORT_FLOW_CONTROL == TRUE)
hci_adv_credits_consumed(1);
Expand Down
2 changes: 1 addition & 1 deletion components/bt/host/bluedroid/stack/btm/btm_ble.c
Original file line number Diff line number Diff line change
Expand Up @@ -2148,7 +2148,7 @@ void btm_ble_create_ll_conn_complete (UINT8 status)
tL2C_LCB *p_lcb = l2cu_find_lcb_by_bd_addr(l2cb.ble_connecting_bda, BT_TRANSPORT_LE);
/* Do not remove lcb if an LE link is already up as a peripheral */
if (p_lcb != NULL &&
!(p_lcb->link_role == HCI_ROLE_SLAVE && BTM_ACL_IS_CONNECTED(l2cb.ble_connecting_bda))) {
!(p_lcb->link_role == HCI_ROLE_SLAVE && BTM_LE_ACL_IS_CONNECTED(l2cb.ble_connecting_bda))) {
p_lcb->disc_reason = L2CAP_CONN_CANCEL;
l2cu_release_lcb (p_lcb);
}
Expand Down
Loading

0 comments on commit 1720dc7

Please sign in to comment.