Skip to content

Commit

Permalink
Merge branch 'bugfix/fix_gattc_disc_crash' into 'master'
Browse files Browse the repository at this point in the history
Bluedroid: configurable max gattc cache characteristic count

See merge request espressif/esp-idf!19842
  • Loading branch information
wmy-espressif committed Oct 9, 2022
2 parents 10b5382 + 137a7cb commit a5e1924
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 13 deletions.
8 changes: 8 additions & 0 deletions components/bt/host/bluedroid/Kconfig.in
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion components/bt/host/bluedroid/api/esp_gatts_api.c
Expand Up @@ -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;
}

Expand Down
20 changes: 12 additions & 8 deletions components/bt/host/bluedroid/bta/gatt/bta_gattc_cache.c
Expand Up @@ -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) {
Expand Down Expand Up @@ -693,19 +692,24 @@ 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,
p_rec->char_decl_handle,
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");
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
7 changes: 3 additions & 4 deletions components/bt/host/bluedroid/bta/gatt/include/bta_gattc_int.h
Expand Up @@ -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
Expand Down Expand Up @@ -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 */
Expand Down
Expand Up @@ -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
Expand Down
Expand Up @@ -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.
Expand Down

0 comments on commit a5e1924

Please sign in to comment.