Skip to content

Commit c4cdb66

Browse files
committed
component/bt: Add lock for SPP slot
1 parent 6acb38a commit c4cdb66

File tree

1 file changed

+36
-2
lines changed
  • components/bt/bluedroid/btc/profile/std/spp

1 file changed

+36
-2
lines changed

components/bt/bluedroid/btc/profile/std/spp/btc_spp.c

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323
#include "esp_spp_api.h"
2424
#include "list.h"
2525

26+
#include "mutex.h"
27+
#include <sys/errno.h>
28+
#include <sys/lock.h>
29+
#include <sys/fcntl.h>
30+
31+
2632
#if (defined BTC_SPP_INCLUDED && BTC_SPP_INCLUDED == TRUE)
2733

2834
typedef struct {
@@ -44,6 +50,7 @@ typedef struct {
4450
} spp_slot_t;
4551
static spp_slot_t *spp_slots[BTA_JV_MAX_RFC_SR_SESSION + 1];
4652
static uint32_t spp_slot_id = 0;
53+
static osi_mutex_t spp_slot_mutex;
4754

4855
static void spp_osi_free(void *p)
4956
{
@@ -127,6 +134,7 @@ static void *btc_spp_rfcomm_inter_cb(tBTA_JV_EVT event, tBTA_JV *p_data, void *u
127134
void *new_user_data = NULL;
128135

129136
uint32_t id = (uintptr_t)user_data;
137+
osi_mutex_lock(&spp_slot_mutex, OSI_MUTEX_MAX_TIMEOUT);
130138
spp_slot_t *slot, *slot_new;
131139
switch (event) {
132140
case BTA_JV_RFCOMM_SRV_OPEN_EVT:
@@ -181,6 +189,7 @@ static void *btc_spp_rfcomm_inter_cb(tBTA_JV_EVT event, tBTA_JV *p_data, void *u
181189
default:
182190
break;
183191
}
192+
osi_mutex_unlock(&spp_slot_mutex);
184193
msg.sig = BTC_SIG_API_CB;
185194
msg.pid = BTC_PID_SPP;
186195
msg.act = event;
@@ -202,6 +211,7 @@ static void btc_spp_dm_inter_cb(tBTA_JV_EVT event, tBTA_JV *p_data, void *user_d
202211

203212
uint32_t id = (uintptr_t)user_data;
204213
spp_slot_t *slot;
214+
osi_mutex_lock(&spp_slot_mutex, OSI_MUTEX_MAX_TIMEOUT);
205215
switch (event) {
206216
case BTA_JV_GET_SCN_EVT:
207217
slot = find_slot_by_id(id);
@@ -248,15 +258,21 @@ static void btc_spp_dm_inter_cb(tBTA_JV_EVT event, tBTA_JV *p_data, void *user_d
248258
}
249259
break;
250260
}
261+
osi_mutex_unlock(&spp_slot_mutex);
251262

252263
}
253264

254265
static void btc_spp_init(void)
255266
{
256267
BTA_JvEnable((tBTA_JV_DM_CBACK *)btc_spp_dm_inter_cb);
268+
if (osi_mutex_new(&spp_slot_mutex) != 0) {
269+
LOG_ERROR("%s osi_mutex_new failed\n", __func__);
270+
}
271+
257272
}
258273
static void btc_spp_uninit(void)
259274
{
275+
osi_mutex_lock(&spp_slot_mutex, OSI_MUTEX_MAX_TIMEOUT);
260276
for (size_t i = 1; i <= BTA_JV_MAX_RFC_SR_SESSION; i++) {
261277
if (spp_slots[i] != NULL && spp_slots[i]->connected) {
262278
BTA_JvRfcommClose(spp_slots[i]->rfc_handle, (void *)spp_slots[i]->id);
@@ -274,6 +290,8 @@ static void btc_spp_uninit(void)
274290
}
275291

276292
BTA_JvDisable();
293+
osi_mutex_unlock(&spp_slot_mutex);
294+
osi_mutex_free(&spp_slot_mutex);
277295
}
278296

279297
static void btc_spp_start_discovery(btc_spp_args_t *arg)
@@ -282,9 +300,11 @@ static void btc_spp_start_discovery(btc_spp_args_t *arg)
282300
}
283301
static void btc_spp_connect(btc_spp_args_t *arg)
284302
{
303+
osi_mutex_lock(&spp_slot_mutex, OSI_MUTEX_MAX_TIMEOUT);
285304
spp_slot_t *slot = malloc_spp_slot();
286305
if (!slot) {
287306
LOG_ERROR("%s unable to malloc RFCOMM slot!", __func__);
307+
osi_mutex_unlock(&spp_slot_mutex);
288308
return;
289309
}
290310
slot->security = arg->connect.sec_mask;
@@ -293,24 +313,29 @@ static void btc_spp_connect(btc_spp_args_t *arg)
293313
memcpy(slot->addr, arg->connect.peer_bd_addr, ESP_BD_ADDR_LEN);
294314
BTA_JvRfcommConnect(arg->connect.sec_mask, arg->connect.role, arg->connect.remote_scn,
295315
arg->connect.peer_bd_addr, (tBTA_JV_RFCOMM_CBACK *)btc_spp_rfcomm_inter_cb, (void *)slot->id);
316+
osi_mutex_unlock(&spp_slot_mutex);
296317
}
297318
static void btc_spp_disconnect(btc_spp_args_t *arg)
298319
{
320+
osi_mutex_lock(&spp_slot_mutex, OSI_MUTEX_MAX_TIMEOUT);
299321
spp_slot_t *slot = find_slot_by_handle(arg->disconnect.handle);
300322
if (!slot) {
301323
LOG_ERROR("%s unable to find RFCOMM slot! disconnect fail!", __func__);
324+
osi_mutex_unlock(&spp_slot_mutex);
302325
return;
303326
}
304327
BTA_JvRfcommClose(arg->disconnect.handle, (void *)slot->id);
305328
btc_disconnect_cb(slot->rfc_handle);
306329
free_spp_slot(slot);
307-
330+
osi_mutex_unlock(&spp_slot_mutex);
308331
}
309332
static void btc_spp_start_srv(btc_spp_args_t *arg)
310333
{
334+
osi_mutex_lock(&spp_slot_mutex, OSI_MUTEX_MAX_TIMEOUT);
311335
spp_slot_t *slot = malloc_spp_slot();
312336
if (!slot) {
313337
LOG_ERROR("%s unable to malloc RFCOMM slot!", __func__);
338+
osi_mutex_unlock(&spp_slot_mutex);
314339
return;
315340
}
316341
slot->security = arg->start_srv.sec_mask;
@@ -320,17 +345,21 @@ static void btc_spp_start_srv(btc_spp_args_t *arg)
320345
strcpy(slot->service_name, arg->start_srv.name);
321346

322347
BTA_JvGetChannelId(BTA_JV_CONN_TYPE_RFCOMM, (void *)slot->id, arg->start_srv.local_scn);
348+
osi_mutex_lock(&spp_slot_mutex, OSI_MUTEX_MAX_TIMEOUT);
323349
}
324350

325351
static void btc_spp_write(btc_spp_args_t *arg)
326352
{
353+
osi_mutex_lock(&spp_slot_mutex, OSI_MUTEX_MAX_TIMEOUT);
327354
spp_slot_t *slot = find_slot_by_handle(arg->write.handle);
328355
if (!slot) {
329356
LOG_ERROR("%s unable to find RFCOMM slot! disconnect fail!", __func__);
357+
osi_mutex_unlock(&spp_slot_mutex);
330358
return;
331359
}
332360
list_append(slot->list, arg->write.p_data);
333361
BTA_JvRfcommWrite(arg->write.handle, slot->id, arg->write.len, arg->write.p_data);
362+
osi_mutex_unlock(&spp_slot_mutex);
334363
}
335364

336365

@@ -461,12 +490,15 @@ void btc_spp_cb_handler(btc_msg_t *msg)
461490
param.write.len = p_data->rfc_write.len;
462491
param.write.cong = p_data->rfc_write.cong;
463492
btc_spp_cb_to_app(ESP_SPP_WRITE_EVT, &param);
493+
osi_mutex_lock(&spp_slot_mutex, OSI_MUTEX_MAX_TIMEOUT);
464494
spp_slot_t *slot = find_slot_by_handle(p_data->rfc_write.handle);
465495
if (!slot) {
466496
LOG_ERROR("%s unable to find RFCOMM slot! disconnect fail!", __func__);
497+
osi_mutex_unlock(&spp_slot_mutex);
467498
break;
468499
}
469500
list_remove(slot->list, list_front(slot->list));
501+
osi_mutex_unlock(&spp_slot_mutex);
470502
break;
471503
case BTA_JV_RFCOMM_CLOSE_EVT:
472504
param.close.status = p_data->rfc_close.status;
@@ -516,9 +548,11 @@ int bta_co_rfc_data_incoming(void *user_data, BT_HDR *p_buf)
516548
msg.act = BTA_JV_RFCOMM_DATA_IND_EVT;
517549

518550
uint32_t id = (uintptr_t)user_data;
551+
osi_mutex_lock(&spp_slot_mutex, OSI_MUTEX_MAX_TIMEOUT);
519552
spp_slot_t *slot = find_slot_by_id(id);
520553
if (!slot) {
521554
LOG_ERROR("%s unable to find RFCOMM slot!", __func__);
555+
osi_mutex_unlock(&spp_slot_mutex);
522556
return 0;
523557
}
524558
p_data.data_ind.handle = slot->rfc_handle;
@@ -529,7 +563,7 @@ int bta_co_rfc_data_incoming(void *user_data, BT_HDR *p_buf)
529563
if (status != BT_STATUS_SUCCESS) {
530564
LOG_ERROR("%s btc_transfer_context failed\n", __func__);
531565
}
532-
// osi_free (p_buf);
566+
osi_mutex_unlock(&spp_slot_mutex);
533567
return 1;
534568
}
535569
int bta_co_rfc_data_outgoing_size(void *user_data, int *size)

0 commit comments

Comments
 (0)