Skip to content

Commit 7639f20

Browse files
committed
Merge branch 'bugfix/buff_semaphore_not_free' into 'master'
component/bt: Fixed ble memory leak issue, buff_semaphore was not released. See merge request espressif/esp-idf!9507
2 parents 6333896 + 7d39074 commit 7639f20

File tree

1 file changed

+24
-5
lines changed
  • components/bt/host/bluedroid/stack/l2cap

1 file changed

+24
-5
lines changed

components/bt/host/bluedroid/stack/l2cap/l2c_api.c

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2274,7 +2274,6 @@ UINT16 L2CA_FlushChannel (UINT16 lcid, UINT16 num_to_flush)
22742274
return (num_left);
22752275
}
22762276

2277-
22782277
/******************************************************************************
22792278
**
22802279
** Function update_acl_pkt_num
@@ -2284,54 +2283,66 @@ UINT16 L2CA_FlushChannel (UINT16 lcid, UINT16 num_to_flush)
22842283
** Returns None
22852284
**
22862285
*******************************************************************************/
2287-
22882286
#if BLE_INCLUDED == TRUE
22892287
void l2ble_update_att_acl_pkt_num(UINT8 type, tl2c_buff_param_t *param)
22902288
{
22912289
static SemaphoreHandle_t buff_semaphore = NULL ;
22922290
static INT16 btc_buf;
22932291
static INT16 btu_buf;
22942292

2295-
if(buff_semaphore != NULL){
2296-
xSemaphoreTake(buff_semaphore, 10 / portTICK_PERIOD_MS);
2293+
if(buff_semaphore == NULL && type != L2CA_BUFF_INI){
2294+
L2CAP_TRACE_ERROR("%s buff_semaphore not init", __func__);
2295+
return;
22972296
}
22982297
switch (type)
22992298
{
23002299
case L2CA_ADD_BTC_NUM:{
2300+
xSemaphoreTake(buff_semaphore, portMAX_DELAY);
23012301
btc_buf ++;
2302+
xSemaphoreGive(buff_semaphore);
23022303
break;
23032304
}
23042305
case L2CA_DECREASE_BTC_NUM:{
2306+
xSemaphoreTake(buff_semaphore, portMAX_DELAY);
23052307
btc_buf --;
2308+
xSemaphoreGive(buff_semaphore);
23062309
break;
23072310
}
23082311
case L2CA_ADD_BTU_NUM:{
2312+
xSemaphoreTake(buff_semaphore, portMAX_DELAY);
23092313
btu_buf ++;
2314+
xSemaphoreGive(buff_semaphore);
23102315
break;
23112316
}
23122317
case L2CA_DECREASE_BTU_NUM:{
2318+
xSemaphoreTake(buff_semaphore, portMAX_DELAY);
23132319
btu_buf --;
2320+
xSemaphoreGive(buff_semaphore);
23142321
break;
23152322
}
23162323
case L2CA_GET_ATT_NUM:{
2324+
xSemaphoreTake(buff_semaphore, portMAX_DELAY);
23172325
INT16 att_acl_pkt_num = 0;
23182326
INT16 att_max_num = 0;
23192327
*(param->get_num) = 0;
23202328
UINT8 tcb_idx = param->conn_id;
23212329
tGATT_TCB * p_tcb = gatt_get_tcb_by_idx(tcb_idx);
23222330
if (p_tcb == NULL){
23232331
L2CAP_TRACE_ERROR("%s not found p_tcb", __func__);
2332+
xSemaphoreGive(buff_semaphore);
23242333
break;
23252334
}
23262335
tL2C_LCB * p_lcb = l2cu_find_lcb_by_bd_addr (p_tcb->peer_bda, BT_TRANSPORT_LE);
23272336
if (p_lcb == NULL){
23282337
L2CAP_TRACE_ERROR("%s not found p_lcb", __func__);
2338+
xSemaphoreGive(buff_semaphore);
23292339
break;
23302340
}
23312341
fixed_queue_t * queue = p_lcb->p_fixed_ccbs[L2CAP_ATT_CID - L2CAP_FIRST_FIXED_CHNL]->xmit_hold_q;
23322342
att_max_num = MIN(p_lcb->link_xmit_quota, L2CAP_CACHE_ATT_ACL_NUM);
23332343
if (queue == NULL){
23342344
L2CAP_TRACE_ERROR("%s not found queue", __func__);
2345+
xSemaphoreGive(buff_semaphore);
23352346
break;
23362347
}
23372348
att_acl_pkt_num = fixed_queue_length(queue);
@@ -2340,23 +2351,31 @@ void l2ble_update_att_acl_pkt_num(UINT8 type, tl2c_buff_param_t *param)
23402351
*(param->get_num) = att_max_num - att_acl_pkt_num - (btc_buf + btu_buf);
23412352
}
23422353
}
2354+
xSemaphoreGive(buff_semaphore);
23432355
break;
23442356
}
23452357
case L2CA_BUFF_INI:{
23462358
btc_buf = 0;
23472359
btu_buf = 0;
23482360
buff_semaphore = xSemaphoreCreateBinary();
2361+
if (buff_semaphore == NULL) {
2362+
L2CAP_TRACE_ERROR("%s NO MEMORY", __func__);
2363+
break;
2364+
}
23492365
xSemaphoreGive(buff_semaphore);
23502366
break;
23512367
}
23522368
case L2CA_BUFF_DEINIT:{
2369+
xSemaphoreTake(buff_semaphore, portMAX_DELAY);
23532370
btc_buf = 0;
23542371
btu_buf = 0;
2372+
xSemaphoreGive(buff_semaphore);
2373+
vSemaphoreDelete(buff_semaphore);
2374+
buff_semaphore = NULL;
23552375
break;
23562376
}
23572377
default:
23582378
break;
23592379
}
2360-
xSemaphoreGive(buff_semaphore);
23612380
}
23622381
#endif

0 commit comments

Comments
 (0)