From 137a7cb5922e636fad1ce1d347b1f402ed926de5 Mon Sep 17 00:00:00 2001 From: chenjianhua Date: Sat, 8 Oct 2022 11:36:43 +0800 Subject: [PATCH] bluedroid: configurable max gattc cache characteristic count --- components/bt/host/bluedroid/Kconfig.in | 8 ++++++++ .../bt/host/bluedroid/api/esp_gatts_api.c | 2 +- .../host/bluedroid/bta/gatt/bta_gattc_cache.c | 20 +++++++++++-------- .../bta/gatt/include/bta_gattc_int.h | 7 +++---- .../include/common/bluedroid_user_config.h | 6 ++++++ .../common/include/common/bt_target.h | 4 ++++ 6 files changed, 34 insertions(+), 13 deletions(-) diff --git a/components/bt/host/bluedroid/Kconfig.in b/components/bt/host/bluedroid/Kconfig.in index dd65fdca3ee..52eb211e4de 100644 --- a/components/bt/host/bluedroid/Kconfig.in +++ b/components/bt/host/bluedroid/Kconfig.in @@ -215,6 +215,14 @@ config BT_GATTC_ENABLE help This option can be close when the app work only on gatt server mode +config BT_GATTC_MAX_CACHE_CHAR + int "Max gattc cache characteristic for discover" + depends on BT_GATTC_ENABLE + range 1 500 + default 40 + help + Maximum GATTC cache characteristic count + config BT_GATTC_CACHE_NVS_FLASH bool "Save gattc cache data to nvs flash" depends on BT_GATTC_ENABLE diff --git a/components/bt/host/bluedroid/api/esp_gatts_api.c b/components/bt/host/bluedroid/api/esp_gatts_api.c index cbeabf6485c..10b82a1c7de 100644 --- a/components/bt/host/bluedroid/api/esp_gatts_api.c +++ b/components/bt/host/bluedroid/api/esp_gatts_api.c @@ -94,7 +94,7 @@ esp_err_t esp_ble_gatts_create_attr_tab(const esp_gatts_attr_db_t *gatts_attr_db ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED); if (max_nb_attr > ESP_GATT_ATTR_HANDLE_MAX) { - LOG_ERROR("%s the number of attribute should not be greater than CONFIG_BT_GATT_MAX_SR_ATTRIBUTES\n"); + LOG_ERROR("The number of attribute should not be greater than CONFIG_BT_GATT_MAX_SR_ATTRIBUTES\n"); return ESP_ERR_INVALID_ARG; } diff --git a/components/bt/host/bluedroid/bta/gatt/bta_gattc_cache.c b/components/bt/host/bluedroid/bta/gatt/bta_gattc_cache.c index ba232cb1d11..d8af854525d 100644 --- a/components/bt/host/bluedroid/bta/gatt/bta_gattc_cache.c +++ b/components/bt/host/bluedroid/bta/gatt/bta_gattc_cache.c @@ -541,7 +541,6 @@ void bta_gattc_start_disc_char_dscp(UINT16 conn_id, tBTA_GATTC_SERV *p_srvc_cb) if (bta_gattc_discover_procedure(conn_id, p_srvc_cb, GATT_DISC_CHAR_DSCPT) != 0) { bta_gattc_char_dscpt_disc_cmpl(conn_id, p_srvc_cb); } - } void bta_gattc_update_include_service(const list_t *services) { @@ -693,7 +692,9 @@ static void bta_gattc_char_dscpt_disc_cmpl(UINT16 conn_id, tBTA_GATTC_SERV *p_sr { tBTA_GATTC_ATTR_REC *p_rec = NULL; - if (--p_srvc_cb->total_char > 0) { + /* Recursive function will cause BTU stack overflow when there are a large number of characteristic + * without descriptor to discover. So replace it with while function */ + while (--p_srvc_cb->total_char > 0) { p_rec = p_srvc_cb->p_srvc_list + (++ p_srvc_cb->cur_char_idx); /* add the next characteristic into cache */ bta_gattc_add_char_to_cache (p_srvc_cb, @@ -701,11 +702,14 @@ static void bta_gattc_char_dscpt_disc_cmpl(UINT16 conn_id, tBTA_GATTC_SERV *p_sr p_rec->s_handle, &p_rec->uuid, p_rec->property); + /* start to discover next characteristic for descriptor */ + if (bta_gattc_discover_procedure(conn_id, p_srvc_cb, GATT_DISC_CHAR_DSCPT) == 0) { + /* send att req and wait for att rsp */ + break; + } + } - /* start discoverying next characteristic for char descriptor */ - bta_gattc_start_disc_char_dscp(conn_id, p_srvc_cb); - } else - /* all characteristic has been explored, start with next service if any */ + if (p_srvc_cb->total_char == 0) /* all characteristic has been explored, start with next service if any */ { #if (defined BTA_GATT_DEBUG && BTA_GATT_DEBUG == TRUE) APPL_TRACE_ERROR("all char has been explored"); @@ -772,7 +776,7 @@ static tBTA_GATT_STATUS bta_gattc_add_srvc_to_list(tBTA_GATTC_SERV *p_srvc_cb, /* allocate bigger buffer ?? */ status = GATT_DB_FULL; - APPL_TRACE_ERROR("service not added, no resources or wrong state"); + APPL_TRACE_ERROR("service not added, no resources or wrong state, see CONFIG_BT_GATTC_MAX_CACHE_CHAR"); } return status; } @@ -814,7 +818,7 @@ static tBTA_GATT_STATUS bta_gattc_add_char_to_list(tBTA_GATTC_SERV *p_srvc_cb, } p_srvc_cb->next_avail_idx ++; } else { - APPL_TRACE_ERROR("char not added, no resources"); + APPL_TRACE_ERROR("char not added, no resources, see CONFIG_BT_GATTC_MAX_CACHE_CHAR"); /* allocate bigger buffer ?? */ status = BTA_GATT_DB_FULL; } diff --git a/components/bt/host/bluedroid/bta/gatt/include/bta_gattc_int.h b/components/bt/host/bluedroid/bta/gatt/include/bta_gattc_int.h index 06a8f5f1877..4262f20b708 100644 --- a/components/bt/host/bluedroid/bta/gatt/include/bta_gattc_int.h +++ b/components/bt/host/bluedroid/bta/gatt/include/bta_gattc_int.h @@ -270,7 +270,6 @@ typedef struct { } tBTA_GATTC_ATTR_REC; -#define BTA_GATTC_MAX_CACHE_CHAR 40 #define BTA_GATTC_ATTR_LIST_SIZE (BTA_GATTC_MAX_CACHE_CHAR * sizeof(tBTA_GATTC_ATTR_REC)) #ifndef BTA_GATTC_CACHE_SRVR_SIZE @@ -305,10 +304,10 @@ typedef struct { tBTA_GATTC_ATTR_REC *p_srvc_list; UINT8 cur_srvc_idx; - UINT8 cur_char_idx; - UINT8 next_avail_idx; + UINT16 cur_char_idx; + UINT16 next_avail_idx; UINT8 total_srvc; - UINT8 total_char; + UINT16 total_char; UINT16 total_attr; UINT8 srvc_hdl_chg; /* service handle change indication pending */ UINT16 attr_index; /* cahce NV saving/loading attribute index */ diff --git a/components/bt/host/bluedroid/common/include/common/bluedroid_user_config.h b/components/bt/host/bluedroid/common/include/common/bluedroid_user_config.h index 6cafa6e41e5..4362cd10fd6 100644 --- a/components/bt/host/bluedroid/common/include/common/bluedroid_user_config.h +++ b/components/bt/host/bluedroid/common/include/common/bluedroid_user_config.h @@ -134,6 +134,12 @@ #endif //GATTC CACHE +#ifdef CONFIG_BT_GATTC_MAX_CACHE_CHAR +#define UC_BT_GATTC_MAX_CACHE_CHAR CONFIG_BT_GATTC_MAX_CACHE_CHAR +#else +#define UC_BT_GATTC_MAX_CACHE_CHAR 40 +#endif + #ifdef CONFIG_BT_GATTC_CACHE_NVS_FLASH #define UC_BT_GATTC_CACHE_NVS_FLASH_ENABLED CONFIG_BT_GATTC_CACHE_NVS_FLASH #else 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 e86e1380d2c..83abc9ba8cf 100644 --- a/components/bt/host/bluedroid/common/include/common/bt_target.h +++ b/components/bt/host/bluedroid/common/include/common/bt_target.h @@ -2247,6 +2247,10 @@ The maximum number of payload octets that the local device can receive in a sing #define BTA_DM_AVOID_A2DP_ROLESWITCH_ON_INQUIRY FALSE #endif +#ifndef BTA_GATTC_MAX_CACHE_CHAR +#define BTA_GATTC_MAX_CACHE_CHAR UC_BT_GATTC_MAX_CACHE_CHAR +#endif + /****************************************************************************** ** ** Tracing: Include trace header file here.