Skip to content

Commit 7ae93db

Browse files
committed
Merge branch 'bugfix/fix_ble_gattc_app_unregister_v5.0' into 'release/v5.0'
Bugfix/fix ble gattc app unregister (v5.0) See merge request espressif/esp-idf!32441
2 parents 745cd37 + 915c312 commit 7ae93db

File tree

4 files changed

+31
-15
lines changed

4 files changed

+31
-15
lines changed

components/bt/host/bluedroid/api/include/api/esp_bt_main.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -40,7 +40,10 @@ esp_bluedroid_status_t esp_bluedroid_get_status(void);
4040
esp_err_t esp_bluedroid_enable(void);
4141

4242
/**
43-
* @brief Disable bluetooth, must prior to esp_bluedroid_deinit().
43+
* @brief Disable Bluetooth, must be called prior to esp_bluedroid_deinit().
44+
*
45+
* @note Before calling this API, ensure that all activities related to
46+
* the application, such as connections, scans, etc., are properly closed.
4447
*
4548
* @return
4649
* - ESP_OK : Succeed

components/bt/host/bluedroid/api/include/api/esp_gattc_api.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,10 +299,14 @@ esp_err_t esp_ble_gattc_app_register(uint16_t app_id);
299299

300300
/**
301301
* @brief This function is called to unregister an application
302-
* from GATTC module.
302+
* from the GATTC module.
303303
*
304304
* @param[in] gattc_if: Gatt client access interface.
305305
*
306+
* @note Before calling this API, ensure that all activities
307+
* related to the application, such as connections, scans, ADV,
308+
* are properly closed.
309+
*
306310
* @return
307311
* - ESP_OK: success
308312
* - other: failed
@@ -608,7 +612,7 @@ esp_gatt_status_t esp_ble_gattc_get_db(esp_gatt_if_t gattc_if, uint16_t conn_id,
608612
*
609613
* @param[in] gattc_if: Gatt client access interface.
610614
* @param[in] conn_id : connection ID.
611-
* @param[in] handle : characteritic handle to read.
615+
* @param[in] handle : characteristic handle to read.
612616
* @param[in] auth_req : authenticate request type
613617
*
614618
* @return

components/bt/host/bluedroid/bta/gatt/bta_gattc_act.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ static void bta_gattc_cmpl_sendmsg(UINT16 conn_id, tGATTC_OPTYPE op,
6363
tGATT_CL_COMPLETE *p_data);
6464
static void bta_gattc_pop_command_to_send(tBTA_GATTC_CLCB *p_clcb);
6565

66-
static void bta_gattc_deregister_cmpl(tBTA_GATTC_RCB *p_clreg);
66+
void bta_gattc_deregister_cmpl(tBTA_GATTC_RCB *p_clreg);
6767
static void bta_gattc_enc_cmpl_cback(tGATT_IF gattc_if, BD_ADDR bda);
6868
static void bta_gattc_cong_cback (UINT16 conn_id, BOOLEAN congested);
6969
static void bta_gattc_req_cback (UINT16 conn_id, UINT32 trans_id, tGATTS_REQ_TYPE type, tGATTS_DATA *p_data);
@@ -154,7 +154,7 @@ void bta_gattc_disable(tBTA_GATTC_CB *p_cb)
154154
APPL_TRACE_DEBUG("bta_gattc_disable");
155155

156156
if (p_cb->state != BTA_GATTC_STATE_ENABLED) {
157-
APPL_TRACE_ERROR("not enabled or disable in pogress");
157+
APPL_TRACE_ERROR("not enabled or disable in progress");
158158
return;
159159
}
160160

@@ -227,7 +227,7 @@ void bta_gattc_register(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_data)
227227
if ((p_buf = (tBTA_GATTC_INT_START_IF *) osi_malloc(sizeof(tBTA_GATTC_INT_START_IF))) != NULL) {
228228
p_buf->hdr.event = BTA_GATTC_INT_START_IF_EVT;
229229
p_buf->client_if = p_cb->cl_rcb[i].client_if;
230-
APPL_TRACE_DEBUG("GATTC getbuf sucess.\n");
230+
APPL_TRACE_DEBUG("GATTC getbuf success.\n");
231231
bta_sys_sendmsg(p_buf);
232232
status = BTA_GATT_OK;
233233
} else {
@@ -841,6 +841,9 @@ void bta_gattc_close(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
841841
(* p_cback)(BTA_GATTC_CLOSE_EVT, (tBTA_GATTC *)&cb_data);
842842
}
843843

844+
// Please note that BTA_GATTC_CLOSE_EVT will run in the BTC task.
845+
// because bta_gattc_deregister_cmpl did not execute as expected(this is a known issue),
846+
// we will run it again in bta_gattc_clcb_dealloc_by_conn_id.
844847
if (p_clreg->num_clcb == 0 && p_clreg->dereg_pending) {
845848
bta_gattc_deregister_cmpl(p_clreg);
846849
}
@@ -1672,7 +1675,7 @@ void bta_gattc_fail(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
16721675
** Returns void
16731676
**
16741677
*******************************************************************************/
1675-
static void bta_gattc_deregister_cmpl(tBTA_GATTC_RCB *p_clreg)
1678+
void bta_gattc_deregister_cmpl(tBTA_GATTC_RCB *p_clreg)
16761679
{
16771680
tBTA_GATTC_CB *p_cb = &bta_gattc_cb;
16781681
tBTA_GATTC_IF client_if = p_clreg->client_if;
@@ -2118,7 +2121,7 @@ void bta_gattc_process_indicate(UINT16 conn_id, tGATTC_OPTYPE op, tGATT_CL_COMPL
21182121
bta_gattc_proc_other_indication(p_clcb, op, p_data, &notify);
21192122
}
21202123
} else if (op == GATTC_OPTYPE_INDICATION) {
2121-
/* no one intersted and need ack? */
2124+
/* no one interested and need ack? */
21222125
APPL_TRACE_DEBUG("%s no one interested, ack now", __func__);
21232126
GATTC_SendHandleValueConfirm(conn_id, handle);
21242127
}
@@ -2235,7 +2238,7 @@ static void bta_gattc_req_cback (UINT16 conn_id, UINT32 trans_id, tGATTS_REQ_TYP
22352238
**
22362239
** Function bta_gattc_init_clcb_conn
22372240
**
2238-
** Description Initaite a BTA CLCB connection
2241+
** Description Initiate a BTA CLCB connection
22392242
**
22402243
** Returns void
22412244
**
@@ -2252,7 +2255,7 @@ void bta_gattc_init_clcb_conn(UINT8 cif, BD_ADDR remote_bda)
22522255
return;
22532256
}
22542257

2255-
/* initaite a new connection here */
2258+
/* initiate a new connection here */
22562259
if ((p_clcb = bta_gattc_clcb_alloc(cif, remote_bda, BTA_GATT_TRANSPORT_LE)) != NULL) {
22572260
gattc_data.hdr.layer_specific = p_clcb->bta_conn_id = conn_id;
22582261

components/bt/host/bluedroid/bta/gatt/bta_gattc_utils.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ UINT8 bta_gattc_num_reg_app(void)
160160
**
161161
** Function bta_gattc_find_clcb_by_cif
162162
**
163-
** Description get clcb by client interface and remote bd adddress
163+
** Description get clcb by client interface and remote bd address
164164
**
165165
** Returns pointer to the clcb
166166
**
@@ -322,12 +322,18 @@ void bta_gattc_clcb_dealloc(tBTA_GATTC_CLCB *p_clcb)
322322
}
323323
}
324324

325+
extern void bta_gattc_deregister_cmpl(tBTA_GATTC_RCB *p_clreg);
325326
void bta_gattc_clcb_dealloc_by_conn_id(UINT16 conn_id)
326327
{
327328
tBTA_GATTC_CLCB *p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id);
328329

329330
if (p_clcb) {
331+
tBTA_GATTC_RCB *p_clreg = p_clcb->p_rcb;
330332
bta_gattc_clcb_dealloc(p_clcb);
333+
// there is a workaround: if there is no connect, we will reset it.
334+
if (p_clreg && p_clreg->num_clcb == 0 && p_clreg->dereg_pending) {
335+
bta_gattc_deregister_cmpl(p_clreg);
336+
}
331337
}
332338
}
333339

@@ -517,15 +523,15 @@ BOOLEAN bta_gattc_enqueue(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
517523
cmd_data->api_write.p_value = (UINT8 *)(cmd_data + 1);
518524
memcpy(cmd_data->api_write.p_value, p_data->api_write.p_value, len);
519525
} else {
520-
APPL_TRACE_ERROR("%s(), line = %d, alloc fail, no memery.", __func__, __LINE__);
526+
APPL_TRACE_ERROR("%s(), line = %d, alloc fail, no memory.", __func__, __LINE__);
521527
return FALSE;
522528
}
523529
} else {
524530
if ((cmd_data = (tBTA_GATTC_DATA *)osi_malloc(sizeof(tBTA_GATTC_DATA))) != NULL) {
525531
memset(cmd_data, 0, sizeof(tBTA_GATTC_DATA));
526532
memcpy(cmd_data, p_data, sizeof(tBTA_GATTC_DATA));
527533
} else {
528-
APPL_TRACE_ERROR("%s(), line = %d, alloc fail, no memery.", __func__, __LINE__);
534+
APPL_TRACE_ERROR("%s(), line = %d, alloc fail, no memory.", __func__, __LINE__);
529535
return FALSE;
530536
}
531537
}
@@ -919,7 +925,7 @@ BOOLEAN bta_gattc_conn_dealloc(BD_ADDR remote_bda)
919925
**
920926
** Function bta_gattc_find_int_conn_clcb
921927
**
922-
** Description try to locate a clcb when an internal connecion event arrives.
928+
** Description try to locate a clcb when an internal connection event arrives.
923929
**
924930
** Returns pointer to the clcb
925931
**

0 commit comments

Comments
 (0)