From 437aba16538befa48ae0136d6944bf8e36b13500 Mon Sep 17 00:00:00 2001 From: chenjianhua Date: Thu, 10 Jul 2025 10:56:06 +0800 Subject: [PATCH] fix(ble/bluedroid): Fixed the connection count limit in multi-connection --- components/bt/host/bluedroid/Kconfig.in | 8 ++++++-- .../common/include/common/bt_target.h | 4 ++++ .../bt/host/bluedroid/stack/gatt/gatt_utils.c | 18 +++++++++--------- .../bluedroid/stack/include/stack/gatt_api.h | 6 +++++- 4 files changed, 24 insertions(+), 12 deletions(-) diff --git a/components/bt/host/bluedroid/Kconfig.in b/components/bt/host/bluedroid/Kconfig.in index a954ffe493..1d4b24b195 100644 --- a/components/bt/host/bluedroid/Kconfig.in +++ b/components/bt/host/bluedroid/Kconfig.in @@ -1210,9 +1210,13 @@ menu "BT DEBUG LOG LEVEL" endmenu #BT DEBUG LOG LEVEL config BT_ACL_CONNECTIONS - int "BT/BLE MAX ACL CONNECTIONS(1~9)" + int "BT/BLE MAX ACL CONNECTIONS" depends on BT_BLUEDROID_ENABLED - range 1 9 + range 1 2 if IDF_TARGET_ESP32C2 + range 1 9 if IDF_TARGET_ESP32 || IDF_TARGET_ESP32C3 || IDF_TARGET_ESP32S3 + range 1 15 if IDF_TARGET_ESP32H2 + range 1 50 + default 2 if IDF_TARGET_ESP32C2 default 4 help Maximum BT/BLE connection count. The ESP32-C3/S3 chip supports a maximum of 10 instances, diff --git a/components/bt/host/bluedroid/common/include/common/bt_target.h b/components/bt/host/bluedroid/common/include/common/bt_target.h index d1dfd1776f..23fca3f3d0 100644 --- a/components/bt/host/bluedroid/common/include/common/bt_target.h +++ b/components/bt/host/bluedroid/common/include/common/bt_target.h @@ -1132,8 +1132,12 @@ /* The number of security records for peer devices. 15 AS Default*/ #ifndef BTM_SEC_MAX_DEVICE_RECORDS +#if (UC_BT_SMP_MAX_BONDS < UC_BT_ACL_CONNECTIONS) +#define BTM_SEC_MAX_DEVICE_RECORDS UC_BT_ACL_CONNECTIONS +#else #define BTM_SEC_MAX_DEVICE_RECORDS UC_BT_SMP_MAX_BONDS #endif +#endif #if BTA_SDP_INCLUDED #define BTM_SDP_SEC_SERVICE_RECORDS 1 diff --git a/components/bt/host/bluedroid/stack/gatt/gatt_utils.c b/components/bt/host/bluedroid/stack/gatt/gatt_utils.c index 0644ab7058..5565169a78 100644 --- a/components/bt/host/bluedroid/stack/gatt/gatt_utils.c +++ b/components/bt/host/bluedroid/stack/gatt/gatt_utils.c @@ -78,7 +78,7 @@ static const UINT8 base_uuid[LEN_UUID_128] = {0xFB, 0x34, 0x9B, 0x5F, 0x80, 0x0 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; -static UINT32 gatt_tcb_id; +static UINT8 gatt_tcb_id[GATT_MAX_PHY_CHANNEL / 8 + 1]; /******************************************************************************* ** @@ -1005,7 +1005,7 @@ UINT8 gatt_find_i_tcb_free(void) UINT8 i = 0, j = GATT_INDEX_INVALID; for (i = 0; i < GATT_MAX_PHY_CHANNEL; i ++) { - if (!((1 << i) & gatt_tcb_id)) { + if (!(gatt_tcb_id[i >> 3] & (1 << (i & 0x7)))) { j = i; break; } @@ -1030,9 +1030,9 @@ tGATT_TCB *gatt_tcb_alloc(UINT8 tcb_idx) /* Add tcb block to list in gatt_cb */ list_append(gatt_cb.p_tcb_list, p_tcb); /* Update tcb id */ - gatt_tcb_id |= 1 << tcb_idx; - } else if(p_tcb) { - osi_free(p_tcb); + gatt_tcb_id[tcb_idx >> 3] |= (1 << (tcb_idx & 0x7)); + } else if (p_tcb) { + osi_free(p_tcb); p_tcb = NULL; } return p_tcb; @@ -1051,7 +1051,7 @@ void gatt_tcb_free( tGATT_TCB *p_tcb) { UINT8 tcb_idx = p_tcb->tcb_idx; if (list_remove(gatt_cb.p_tcb_list, p_tcb)) { - gatt_tcb_id &= ~(1 << tcb_idx); + gatt_tcb_id[tcb_idx >> 3] &= ~(1 << (tcb_idx & 0x7)); } } /******************************************************************************* @@ -1078,9 +1078,9 @@ tGATT_TCB *gatt_allocate_tcb_by_bdaddr(BD_ADDR bda, tBT_TRANSPORT transport) } if (i != GATT_INDEX_INVALID) { p_tcb = gatt_tcb_alloc(i); - if (!p_tcb) { - return NULL; - } + if (!p_tcb) { + return NULL; + } if (allocated) { memset(p_tcb, 0, sizeof(tGATT_TCB)); p_tcb->pending_enc_clcb = fixed_queue_new(QUEUE_SIZE_MAX); diff --git a/components/bt/host/bluedroid/stack/include/stack/gatt_api.h b/components/bt/host/bluedroid/stack/include/stack/gatt_api.h index 6c63db6238..002cb16e33 100644 --- a/components/bt/host/bluedroid/stack/include/stack/gatt_api.h +++ b/components/bt/host/bluedroid/stack/include/stack/gatt_api.h @@ -151,7 +151,11 @@ typedef UINT16 tGATT_DISCONN_REASON; #define GATT_INVALID_CONN_ID 0xFFFF #ifndef GATT_CL_MAX_LCB -#define GATT_CL_MAX_LCB 12 // 22 +#if (GATT_MAX_PHY_CHANNEL > 12) +#define GATT_CL_MAX_LCB GATT_MAX_PHY_CHANNEL +#else +#define GATT_CL_MAX_LCB 12 +#endif #endif #ifndef GATT_MAX_SCCB