Skip to content

Commit 90c9ffa

Browse files
committed
component/bt: free timer resources after using them
Bluedroid use a set of timer function pairs such as btu_start_timer/btu_stop_timer, btu_sys_start_timer/btu_sys_stop_timer for use, in a lack of timer release functions. Thus the timers may be exhausted after some event sequence such as repetition of connection/reconnection with different devices. The maximum timer number used in bluedroid is given by ALARM_CBS_NUM which is 30 for now. This bugfix borrowed some update from bluedroid in Andoroid 7.0, which add timer release functions which promote the recycle of timer resources.
1 parent daa8cfa commit 90c9ffa

File tree

23 files changed

+231
-56
lines changed

23 files changed

+231
-56
lines changed

components/bt/bluedroid/bta/av/bta_av_act.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,6 +1268,11 @@ void bta_av_disable(tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
12681268
hdr.layer_specific = xx + 1;
12691269
bta_av_api_deregister((tBTA_AV_DATA *)&hdr);
12701270
}
1271+
1272+
bta_sys_free_timer(&p_cb->sig_tmr);
1273+
memset(&p_cb->sig_tmr, 0, sizeof(TIMER_LIST_ENT));
1274+
bta_sys_free_timer(&p_cb->acp_sig_tmr);
1275+
memset(&p_cb->acp_sig_tmr, 0, sizeof(TIMER_LIST_ENT));
12711276
}
12721277

12731278
/*******************************************************************************

components/bt/bluedroid/bta/dm/bta_dm_act.c

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,47 @@ void bta_dm_enable(tBTA_DM_MSG *p_data)
282282
}
283283
}
284284

285+
/*******************************************************************************
286+
*
287+
* Function bta_dm_init_cb
288+
*
289+
* Description Initializes the bta_dm_cb control block
290+
*
291+
*
292+
* Returns void
293+
*
294+
******************************************************************************/
295+
void bta_dm_init_cb(void)
296+
{
297+
memset(&bta_dm_cb, 0, sizeof(bta_dm_cb));
298+
}
299+
300+
/*******************************************************************************
301+
*
302+
* Function bta_dm_deinit_cb
303+
*
304+
* Description De-initializes the bta_dm_cb control block
305+
*
306+
*
307+
* Returns void
308+
*
309+
******************************************************************************/
310+
void bta_dm_deinit_cb(void)
311+
{
312+
bta_sys_free_timer(&bta_dm_cb.disable_timer);
313+
#if ( BTA_EIR_CANNED_UUID_LIST != TRUE )
314+
bta_sys_free_timer(&bta_dm_cb.app_ready_timer);
315+
#endif
316+
#if BTM_SSR_INCLUDED == TRUE
317+
for (size_t i = 0; i < BTA_DM_NUM_PM_TIMER; i++) {
318+
for (size_t j = 0; j < BTA_DM_PM_MODE_TIMER_MAX; j++) {
319+
bta_sys_free_timer(&bta_dm_cb.pm_timer[i].timer[j]);
320+
}
321+
}
322+
#endif
323+
memset(&bta_dm_cb, 0, sizeof(bta_dm_cb));
324+
}
325+
285326
/*******************************************************************************
286327
**
287328
** Function bta_dm_sys_hw_cback
@@ -318,7 +359,15 @@ static void bta_dm_sys_hw_cback( tBTA_SYS_HW_EVT status )
318359
}
319360

320361
/* reinitialize the control block */
321-
memset(&bta_dm_cb, 0, sizeof(bta_dm_cb));
362+
bta_dm_deinit_cb();
363+
364+
bta_sys_free_timer(&bta_dm_search_cb.search_timer);
365+
#if ((defined BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
366+
#if ((defined BTA_GATT_INCLUDED) && (BTA_GATT_INCLUDED == TRUE) && SDP_INCLUDED == TRUE)
367+
bta_sys_free_timer(&bta_dm_search_cb.gatt_close_timer);
368+
#endif
369+
#endif
370+
memset(&bta_dm_search_cb, 0x00, sizeof(bta_dm_search_cb));
322371

323372
/* unregister from SYS */
324373
bta_sys_hw_unregister( BTA_SYS_HW_BLUETOOTH );
@@ -332,11 +381,18 @@ static void bta_dm_sys_hw_cback( tBTA_SYS_HW_EVT status )
332381
/* save security callback */
333382
temp_cback = bta_dm_cb.p_sec_cback;
334383
/* make sure the control block is properly initialized */
335-
memset(&bta_dm_cb, 0, sizeof(bta_dm_cb));
384+
bta_dm_init_cb();
385+
336386
/* and retrieve the callback */
337387
bta_dm_cb.p_sec_cback = temp_cback;
338388
bta_dm_cb.is_bta_dm_active = TRUE;
339389

390+
bta_sys_free_timer(&bta_dm_search_cb.search_timer);
391+
#if ((defined BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
392+
#if ((defined BTA_GATT_INCLUDED) && (BTA_GATT_INCLUDED == TRUE) && SDP_INCLUDED == TRUE)
393+
bta_sys_free_timer(&bta_dm_search_cb.gatt_close_timer);
394+
#endif
395+
#endif
340396
/* hw is ready, go on with BTA DM initialization */
341397
memset(&bta_dm_search_cb, 0x00, sizeof(bta_dm_search_cb));
342398
#if (BTM_SSR_INCLUDED == TRUE)

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

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@
4848

4949
#if GATTC_INCLUDED == TRUE && BLE_INCLUDED == TRUE
5050

51-
static osi_mutex_t write_ccc_mutex;
52-
5351
/*****************************************************************************
5452
** Constants
5553
*****************************************************************************/
@@ -129,9 +127,9 @@ static void bta_gattc_enable(tBTA_GATTC_CB *p_cb)
129127
memset(&bta_gattc_cb, 0, sizeof(tBTA_GATTC_CB));
130128
p_cb->state = BTA_GATTC_STATE_ENABLED;
131129
// Create a write ccc mutex when the gatt client enable
132-
osi_mutex_new(&write_ccc_mutex);
130+
osi_mutex_new(&bta_gattc_cb.write_ccc_mutex);
133131
} else {
134-
APPL_TRACE_DEBUG("GATTC is arelady enabled");
132+
APPL_TRACE_DEBUG("GATTC is already enabled");
135133
}
136134
}
137135

@@ -157,7 +155,7 @@ void bta_gattc_disable(tBTA_GATTC_CB *p_cb)
157155
return;
158156
}
159157
// Free the write ccc mutex when the gatt client disable
160-
osi_mutex_free(&write_ccc_mutex);
158+
osi_mutex_free(&bta_gattc_cb.write_ccc_mutex);
161159

162160
for (i = 0; i < BTA_GATTC_CL_MAX; i ++) {
163161
if (p_cb->cl_rcb[i].in_use) {
@@ -1644,16 +1642,16 @@ static void bta_gattc_conn_cback(tGATT_IF gattc_if, BD_ADDR bda, UINT16 conn_id,
16441642
else if ((transport == BT_TRANSPORT_LE) && (connected == FALSE) && (p_conn != NULL)){
16451643
p_conn->service_change_ccc_written = FALSE;
16461644
if (p_conn->ccc_timer_used == TRUE){
1647-
assert(write_ccc_mutex != NULL);
1648-
osi_mutex_lock(&write_ccc_mutex, OSI_MUTEX_MAX_TIMEOUT);
1645+
assert(bta_gattc_cb.write_ccc_mutex != NULL);
1646+
osi_mutex_lock(&bta_gattc_cb.write_ccc_mutex, OSI_MUTEX_MAX_TIMEOUT);
16491647

16501648
if (p_conn->service_change_ccc_timer.param != 0) {
16511649
osi_free((void *)p_conn->service_change_ccc_timer.param);
16521650
p_conn->service_change_ccc_timer.param = (TIMER_PARAM_TYPE)0;
16531651
}
16541652
bta_sys_stop_timer(&(p_conn->service_change_ccc_timer));
16551653
p_conn->ccc_timer_used = FALSE;
1656-
osi_mutex_unlock(&write_ccc_mutex);
1654+
osi_mutex_unlock(&bta_gattc_cb.write_ccc_mutex);
16571655
}
16581656
}
16591657

@@ -2354,22 +2352,22 @@ static void bta_gattc_wait4_service_change_ccc_cback (TIMER_LIST_ENT *p_tle)
23542352
BOOLEAN start_ccc_timer = FALSE;
23552353
UINT32 new_timeout;
23562354

2357-
assert(write_ccc_mutex != NULL);
2358-
osi_mutex_lock(&write_ccc_mutex, OSI_MUTEX_MAX_TIMEOUT);
2355+
assert(bta_gattc_cb.write_ccc_mutex != NULL);
2356+
osi_mutex_lock(&bta_gattc_cb.write_ccc_mutex, OSI_MUTEX_MAX_TIMEOUT);
23592357

23602358
tBTA_GATTC_WAIT_CCC_TIMER *p_timer_param = (tBTA_GATTC_WAIT_CCC_TIMER*) p_tle->param;
23612359
p_tle->param = (TIMER_PARAM_TYPE)0;
23622360
if (p_timer_param == NULL){
23632361
APPL_TRACE_ERROR("p_timer_param is NULL in %s\n", __func__);
2364-
osi_mutex_unlock(&write_ccc_mutex);
2362+
osi_mutex_unlock(&bta_gattc_cb.write_ccc_mutex);
23652363
return;
23662364
}
23672365

23682366
tBTA_GATTC_CONN *p_conn = bta_gattc_conn_find(p_timer_param->remote_bda);
23692367
if (p_conn == NULL){
23702368
APPL_TRACE_ERROR("p_conn is NULL in %s\n", __func__);
23712369
osi_free(p_timer_param);
2372-
osi_mutex_unlock(&write_ccc_mutex);
2370+
osi_mutex_unlock(&bta_gattc_cb.write_ccc_mutex);
23732371
return;
23742372
}
23752373

@@ -2401,7 +2399,7 @@ static void bta_gattc_wait4_service_change_ccc_cback (TIMER_LIST_ENT *p_tle)
24012399
}
24022400

24032401
osi_free(p_timer_param);
2404-
osi_mutex_unlock(&write_ccc_mutex);
2402+
osi_mutex_unlock(&bta_gattc_cb.write_ccc_mutex);
24052403
}
24062404

24072405
#endif

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,10 @@ BOOLEAN bta_gattc_conn_dealloc(BD_ADDR remote_bda)
862862
if (p_conn != NULL) {
863863
p_conn->in_use = FALSE;
864864
memset(p_conn->remote_bda, 0, BD_ADDR_LEN);
865+
osi_mutex_lock(&bta_gattc_cb.write_ccc_mutex, OSI_MUTEX_MAX_TIMEOUT);
866+
bta_sys_free_timer(&p_conn->service_change_ccc_timer);
867+
p_conn->ccc_timer_used = FALSE;
868+
osi_mutex_unlock(&bta_gattc_cb.write_ccc_mutex);
865869
return TRUE;
866870
}
867871
return FALSE;

components/bt/bluedroid/bta/include/bta_gattc_int.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "bta_gattc_ci.h"
3131
#include "bta_gattc_co.h"
3232
#include "fixed_queue.h"
33+
#include "mutex.h"
3334

3435
/*****************************************************************************
3536
** Constants and data types
@@ -357,8 +358,8 @@ enum {
357358
};
358359

359360
typedef struct {
360-
UINT8 state;
361-
361+
UINT8 state;
362+
osi_mutex_t write_ccc_mutex;
362363
tBTA_GATTC_CONN conn_track[BTA_GATTC_CONN_MAX];
363364
tBTA_GATTC_BG_TCK bg_track[BTA_GATTC_KNOWN_SR_MAX];
364365
tBTA_GATTC_RCB cl_rcb[BTA_GATTC_CL_MAX];

components/bt/bluedroid/bta/include/bta_sys.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ extern UINT16 bta_sys_get_sys_features(void);
224224
extern void bta_sys_sendmsg(void *p_msg);
225225
extern void bta_sys_start_timer(TIMER_LIST_ENT *p_tle, UINT16 type, INT32 timeout_ms);
226226
extern void bta_sys_stop_timer(TIMER_LIST_ENT *p_tle);
227+
extern void bta_sys_free_timer(TIMER_LIST_ENT *p_tle);
227228
extern void bta_sys_disable(tBTA_SYS_HW_MODULE module);
228229
extern UINT32 bta_sys_get_remaining_ticks(TIMER_LIST_ENT *p_target_tle);
229230

components/bt/bluedroid/bta/sys/bta_sys_main.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,28 @@ void bta_sys_stop_timer(TIMER_LIST_ENT *p_tle)
657657
osi_alarm_cancel(alarm);
658658
}
659659

660+
/*******************************************************************************
661+
**
662+
** Function bta_sys_free_timer
663+
**
664+
** Description Stop and free a BTA timer.
665+
**
666+
** Returns void
667+
**
668+
*******************************************************************************/
669+
void bta_sys_free_timer(TIMER_LIST_ENT *p_tle)
670+
{
671+
assert(p_tle != NULL);
672+
673+
osi_alarm_t *alarm = hash_map_get(bta_alarm_hash_map, p_tle);
674+
if (alarm == NULL) {
675+
LOG_DEBUG("%s expected alarm was not in bta alarm hash map.", __func__);
676+
return;
677+
}
678+
osi_alarm_cancel(alarm);
679+
hash_map_erase(bta_alarm_hash_map, p_tle);
680+
}
681+
660682
/*******************************************************************************
661683
**
662684
** Function bta_sys_disable

components/bt/bluedroid/stack/avdt/avdt_ccb.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ void avdt_ccb_dealloc(tAVDT_CCB *p_ccb, tAVDT_CCB_EVT *p_data)
407407
UNUSED(p_data);
408408

409409
AVDT_TRACE_DEBUG("avdt_ccb_dealloc %d\n", avdt_ccb_to_idx(p_ccb));
410-
btu_stop_timer(&p_ccb->timer_entry);
410+
btu_free_timer(&p_ccb->timer_entry);
411411
fixed_queue_free(p_ccb->cmd_q, NULL);
412412
fixed_queue_free(p_ccb->rsp_q, NULL);
413413
memset(p_ccb, 0, sizeof(tAVDT_CCB));

components/bt/bluedroid/stack/avdt/avdt_scb.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ void avdt_scb_dealloc(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
644644
UNUSED(p_data);
645645

646646
AVDT_TRACE_DEBUG("avdt_scb_dealloc hdl=%d\n", avdt_scb_to_hdl(p_scb));
647-
btu_stop_timer(&p_scb->timer_entry);
647+
btu_free_timer(&p_scb->timer_entry);
648648

649649
#if AVDT_MULTIPLEXING == TRUE
650650
/* free fragments we're holding, if any; it shouldn't happen */

components/bt/bluedroid/stack/btm/btm_ble_gap.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3740,6 +3740,9 @@ void btm_ble_init (void)
37403740

37413741
BTM_TRACE_DEBUG("%s", __func__);
37423742

3743+
btu_free_timer(&p_cb->obs_timer_ent);
3744+
btu_free_timer(&p_cb->scan_timer_ent);
3745+
btu_free_timer(&p_cb->inq_var.fast_adv_timer);
37433746
memset(p_cb, 0, sizeof(tBTM_BLE_CB));
37443747
memset(&(btm_cb.cmn_ble_vsc_cb), 0 , sizeof(tBTM_BLE_VSC_CB));
37453748
btm_cb.cmn_ble_vsc_cb.values_read = FALSE;

0 commit comments

Comments
 (0)