Skip to content

Commit b3dc905

Browse files
committed
Add locking to GAP and GATTC
1 parent a4711b0 commit b3dc905

File tree

5 files changed

+83
-33
lines changed

5 files changed

+83
-33
lines changed

include/esp32/esp32_bt_internal.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ int esp32_bt_gatts_event(const struct ble_gap_event *event, void *arg);
3737

3838
extern uint8_t own_addr_type;
3939

40+
void esp32_bt_rlock(void);
41+
void esp32_bt_runlock(void);
42+
4043
#ifdef __cplusplus
4144
}
4245
#endif

src/esp32/esp32_bt.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ static bool s_inited = false;
4242
static bool s_should_be_running = false;
4343
static TaskHandle_t s_host_task_handle;
4444
static SemaphoreHandle_t s_sem = NULL;
45+
static struct mgos_rlock_type *s_lock = NULL;
4546

4647
enum esp32_bt_state {
4748
ESP32_BT_STOPPED = 0,
@@ -239,13 +240,23 @@ static void esp32_bt_host_task(void *param) {
239240
}
240241
}
241242

243+
void esp32_bt_rlock(void) {
244+
mgos_rlock(s_lock);
245+
}
246+
247+
void esp32_bt_runlock(void) {
248+
mgos_runlock(s_lock);
249+
}
250+
242251
extern void ble_store_config_init(void);
243252

244253
static bool esp32_bt_init(void) {
245254
if (s_inited) {
246255
return true;
247256
}
248257

258+
s_lock = mgos_rlock_create();
259+
249260
bool ret = false;
250261

251262
if (mgos_sys_config_get_bt_dev_name() != NULL) {

src/esp32/esp32_bt_gap.c

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,9 @@ bool mgos_bt_gap_scan(const struct mgos_bt_gap_scan_opts *opts) {
174174
}
175175

176176
static int esp32_bt_gap_event(struct ble_gap_event *ev, void *arg) {
177+
int ret = 0;
177178
LOG(LL_DEBUG, ("GAP Event %d", ev->type));
179+
esp32_bt_rlock();
178180
switch (ev->type) {
179181
// Forward GATTS events to the GATTS handler.
180182
case BLE_GAP_EVENT_CONNECT:
@@ -187,7 +189,7 @@ static int esp32_bt_gap_event(struct ble_gap_event *ev, void *arg) {
187189
case BLE_GAP_EVENT_SUBSCRIBE:
188190
case BLE_GAP_EVENT_NOTIFY_TX:
189191
case BLE_GAP_EVENT_MTU:
190-
esp32_bt_gatts_event(ev, arg);
192+
ret = esp32_bt_gatts_event(ev, arg);
191193
break;
192194
case BLE_GAP_EVENT_ADV_COMPLETE:
193195
s_advertising = false;
@@ -206,10 +208,11 @@ static int esp32_bt_gap_event(struct ble_gap_event *ev, void *arg) {
206208
}
207209
// Return BLE_GAP_REPEAT_PAIRING_RETRY to indicate that the host should
208210
// continue with the pairing operation.
209-
return BLE_GAP_REPEAT_PAIRING_RETRY;
211+
ret = BLE_GAP_REPEAT_PAIRING_RETRY;
210212
}
211213
}
212-
return 0;
214+
esp32_bt_runlock();
215+
return ret;
213216
}
214217

215218
bool esp32_bt_gap_start_advertising(void) {
@@ -303,9 +306,13 @@ bool esp32_bt_gap_start_advertising(void) {
303306
}
304307

305308
static bool esp32_bt_gap_stop_advertising(void) {
306-
if (!s_advertising) return true;
307-
bool res = (ble_gap_adv_stop() == 0);
309+
esp32_bt_rlock();
310+
bool res = !s_advertising;
311+
if (res) goto out;
312+
res = (ble_gap_adv_stop() == 0);
308313
if (res) s_advertising = false;
314+
out:
315+
esp32_bt_runlock();
309316
return res;
310317
}
311318

@@ -330,7 +337,10 @@ bool mgos_bt_gap_set_scan_rsp_data(struct mg_str scan_rsp_data) {
330337
}
331338

332339
bool mgos_bt_gap_set_adv_enable(bool adv_enable) {
340+
esp32_bt_rlock();
333341
s_adv_enable = adv_enable;
334-
return (s_adv_enable ? esp32_bt_gap_start_advertising()
335-
: esp32_bt_gap_stop_advertising());
342+
bool res = (s_adv_enable ? esp32_bt_gap_start_advertising()
343+
: esp32_bt_gap_stop_advertising());
344+
esp32_bt_rlock();
345+
return res;
336346
}

src/esp32/esp32_bt_gattc.c

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -141,18 +141,24 @@ static void esp32_bt_gattc_finish_discovery(struct esp32_bt_gattc_conn *conn,
141141

142142
static int esp32_bt_gattc_event(struct ble_gap_event *ev, void *arg) {
143143
char buf1[MGOS_BT_UUID_STR_LEN];
144+
esp32_bt_rlock();
144145
struct esp32_bt_gattc_conn *conn = validate_conn(arg);
145-
if (conn == NULL) return BLE_ATT_ERR_UNLIKELY;
146+
if (conn == NULL) {
147+
esp32_bt_runlock();
148+
return BLE_ATT_ERR_UNLIKELY;
149+
}
146150
LOG(LL_DEBUG, ("GATTC EV %d", ev->type));
147151
switch (ev->type) {
148152
case BLE_GAP_EVENT_CONNECT: {
149153
uint16_t conn_id = ev->connect.conn_handle;
150154
conn->gc.conn_id = conn_id;
151155
struct ble_gap_conn_desc cd;
152156
ble_gap_conn_find(conn_id, &cd);
153-
LOG(LL_INFO, ("CONNECT %s ch %d st %d",
157+
int8_t conn_rssi = 0;
158+
ble_gap_conn_rssi(conn_id, &conn_rssi);
159+
LOG(LL_INFO, ("CONNECT %s ch %d st %d rssi %d",
154160
esp32_bt_addr_to_str(&cd.peer_ota_addr, buf1), conn_id,
155-
ev->connect.status));
161+
ev->connect.status, -conn_rssi));
156162
ble_gattc_exchange_mtu(conn_id, esp32_bt_gattc_mtu_event, conn);
157163
break;
158164
}
@@ -179,6 +185,7 @@ static int esp32_bt_gattc_event(struct ble_gap_event *ev, void *arg) {
179185
break;
180186
}
181187
}
188+
esp32_bt_runlock();
182189
return 0;
183190
}
184191

@@ -262,8 +269,12 @@ static int esp32_bt_gattc_disc_dsc_ev(uint16_t conn_id,
262269
void *arg) {
263270
int ret = 0;
264271
char buf[MGOS_BT_UUID_STR_LEN];
272+
esp32_bt_rlock();
265273
struct esp32_bt_gattc_conn *conn = validate_conn(arg);
266-
if (conn == NULL) return BLE_ATT_ERR_UNLIKELY;
274+
if (conn == NULL) {
275+
esp32_bt_runlock();
276+
return BLE_ATT_ERR_UNLIKELY;
277+
}
267278
switch (err->status) {
268279
case 0:
269280
LOG(LL_DEBUG, ("DISC_DSC ch %d uuid %s h %d", conn_id,
@@ -282,6 +293,7 @@ static int esp32_bt_gattc_disc_dsc_ev(uint16_t conn_id,
282293
esp32_bt_gattc_finish_discovery(conn, false /* ok */);
283294
}
284295
}
296+
esp32_bt_runlock();
285297
return ret;
286298
}
287299

@@ -291,8 +303,12 @@ static int esp32_bt_gattc_disc_chr_ev(uint16_t conn_id,
291303
void *arg) {
292304
int ret = 0;
293305
char buf[MGOS_BT_UUID_STR_LEN];
306+
esp32_bt_rlock();
294307
struct esp32_bt_gattc_conn *conn = validate_conn(arg);
295-
if (conn == NULL) return BLE_ATT_ERR_UNLIKELY;
308+
if (conn == NULL) {
309+
esp32_bt_runlock();
310+
return BLE_ATT_ERR_UNLIKELY;
311+
}
296312
switch (err->status) {
297313
case 0:
298314
LOG(LL_DEBUG, ("DISC_CHR ch %d uuid %s dh %d vh %d", conn_id,
@@ -316,6 +332,7 @@ static int esp32_bt_gattc_disc_chr_ev(uint16_t conn_id,
316332
esp32_bt_gattc_finish_discovery(conn, false /* ok */);
317333
}
318334
}
335+
esp32_bt_runlock();
319336
return ret;
320337
}
321338

@@ -325,8 +342,12 @@ static int esp32_bt_gattc_disc_svc_ev(uint16_t conn_id,
325342
void *arg) {
326343
int ret = 0;
327344
char buf[MGOS_BT_UUID_STR_LEN];
345+
esp32_bt_rlock();
328346
struct esp32_bt_gattc_conn *conn = validate_conn(arg);
329-
if (conn == NULL) return BLE_ATT_ERR_UNLIKELY;
347+
if (conn == NULL) {
348+
esp32_bt_runlock();
349+
return BLE_ATT_ERR_UNLIKELY;
350+
}
330351
switch (err->status) {
331352
case 0:
332353
LOG(LL_DEBUG, ("DISC_SVC ch %d uuid %s sh %d eh %d", conn_id,
@@ -354,6 +375,7 @@ static int esp32_bt_gattc_disc_svc_ev(uint16_t conn_id,
354375
esp32_bt_gattc_finish_discovery(conn, false /* ok */);
355376
}
356377
}
378+
esp32_bt_runlock();
357379
return ret;
358380
}
359381

@@ -364,19 +386,27 @@ static void esp32_bt_gattc_invoke_fd(void *arg) {
364386
}
365387

366388
bool mgos_bt_gattc_discover(uint16_t conn_id) {
389+
int ret = false;
390+
esp32_bt_rlock();
367391
struct esp32_bt_gattc_conn *conn = find_conn_by_id(conn_id);
368-
if (conn == NULL) return false;
369-
if (!conn->connected || conn->disc_in_progress) return false;
392+
if (conn == NULL) goto out;
393+
if (!conn->connected || conn->disc_in_progress) goto out;
370394
if (conn->disc_done) {
371395
conn->disc_in_progress = true;
372396
mgos_invoke_cb(esp32_bt_gattc_invoke_fd, conn, false /* from_isr */);
373-
return true;
397+
ret = true;
398+
goto out;
374399
}
375400
conn->disc_in_progress = true;
376401
if (ble_gattc_disc_all_svcs(conn_id, esp32_bt_gattc_disc_svc_ev, conn) != 0) {
377402
conn->disc_in_progress = false;
403+
ret = false;
404+
} else {
405+
ret = true;
378406
}
379-
return true;
407+
out:
408+
esp32_bt_runlock();
409+
return ret;
380410
}
381411

382412
bool mgos_bt_gattc_disconnect(uint16_t conn_id) {

src/esp32/esp32_bt_gatts.c

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ struct esp32_bt_gatts_connection_entry {
104104
SLIST_ENTRY(esp32_bt_gatts_connection_entry) next;
105105
};
106106

107-
struct mgos_rlock_type *s_lock;
108107
static SLIST_HEAD(s_svcs, esp32_bt_gatts_service_entry) s_svcs =
109108
SLIST_HEAD_INITIALIZER(s_svcs);
110109
static SLIST_HEAD(s_conns, esp32_bt_gatts_connection_entry) s_conns =
@@ -300,24 +299,24 @@ static void esp32_bt_gatts_create_sessions(
300299

301300
int mgos_bt_gatts_get_num_connections(void) {
302301
int num = 0;
303-
mgos_rlock(s_lock);
302+
esp32_bt_rlock();
304303
struct esp32_bt_gatts_connection_entry *ce;
305304
SLIST_FOREACH(ce, &s_conns, next) num++;
306-
mgos_runlock(s_lock);
305+
esp32_bt_runlock();
307306
return num;
308307
}
309308

310309
bool mgos_bt_gatts_is_send_queue_empty(void) {
311310
bool res = true;
312-
mgos_rlock(s_lock);
311+
esp32_bt_rlock();
313312
struct esp32_bt_gatts_connection_entry *ce;
314313
SLIST_FOREACH(ce, &s_conns, next) {
315314
if (!STAILQ_EMPTY(&ce->pending_inds)) {
316315
res = false;
317316
break;
318317
}
319318
}
320-
mgos_runlock(s_lock);
319+
esp32_bt_runlock();
321320
return res;
322321
}
323322

@@ -358,7 +357,7 @@ int esp32_bt_gatts_event(const struct ble_gap_event *ev, void *arg) {
358357
char buf1[MGOS_BT_UUID_STR_LEN], buf2[MGOS_BT_UUID_STR_LEN];
359358
LOG(LL_DEBUG,
360359
("GATTS EV %d hf %d", ev->type, (int) mgos_get_free_heap_size()));
361-
mgos_rlock(s_lock);
360+
esp32_bt_rlock();
362361
switch (ev->type) {
363362
case BLE_GAP_EVENT_CONNECT: {
364363
uint16_t conn_id = ev->connect.conn_handle;
@@ -525,7 +524,7 @@ int esp32_bt_gatts_event(const struct ble_gap_event *ev, void *arg) {
525524
break;
526525
}
527526
}
528-
mgos_runlock(s_lock);
527+
esp32_bt_runlock();
529528
return ret;
530529
}
531530

@@ -820,9 +819,9 @@ bool mgos_bt_gatts_register_service(const char *svc_uuid,
820819
bsvc->characteristics = se->ble_chars;
821820
se->ble_svc_def[1].type = BLE_GATT_SVC_TYPE_END;
822821

823-
mgos_rlock(s_lock);
822+
esp32_bt_rlock();
824823
SLIST_INSERT_HEAD(&s_svcs, se, next);
825-
mgos_runlock(s_lock);
824+
esp32_bt_runlock();
826825
esp32_bt_restart();
827826
res = true;
828827
out:
@@ -867,7 +866,7 @@ void mgos_bt_gatts_send_resp_data(struct mgos_bt_gatts_conn *gsc,
867866
struct mgos_bt_gatts_read_arg *ra,
868867
struct mg_str data) {
869868
if (data.len == 0) return;
870-
mgos_rlock(s_lock);
869+
esp32_bt_rlock();
871870
struct esp32_bt_gatts_session_entry *sse = find_session_by_gsc(gsc);
872871
if (sse == NULL) goto out;
873872
struct esp32_bt_gatts_pending_read *pr = NULL;
@@ -883,7 +882,7 @@ void mgos_bt_gatts_send_resp_data(struct mgos_bt_gatts_conn *gsc,
883882
}
884883
mbuf_append(&pr->data, data.p, data.len);
885884
out:
886-
mgos_runlock(s_lock);
885+
esp32_bt_runlock();
887886
}
888887

889888
void mgos_bt_gatts_notify(struct mgos_bt_gatts_conn *gsc,
@@ -897,11 +896,11 @@ void mgos_bt_gatts_notify(struct mgos_bt_gatts_conn *gsc,
897896
pi->handle = handle;
898897
pi->is_ind = (mode == MGOS_BT_GATT_NOTIFY_MODE_INDICATE);
899898
pi->value = mg_strdup(data);
900-
mgos_rlock(s_lock);
899+
esp32_bt_rlock();
901900
STAILQ_INSERT_TAIL(&sse->ce->pending_inds, pi, next);
902901
sse->ce->ind_queue_len++;
903902
esp32_bt_gatts_send_next_ind_locked(sse->ce);
904-
mgos_runlock(s_lock);
903+
esp32_bt_runlock();
905904
}
906905

907906
void mgos_bt_gatts_notify_uuid(struct mgos_bt_gatts_conn *gsc,
@@ -927,8 +926,5 @@ bool esp32_bt_gatts_start(void) {
927926

928927
bool esp32_bt_gatts_init(void) {
929928
ble_hs_cfg.gatts_register_cb = esp32_gatts_register_cb;
930-
if (s_lock == NULL) {
931-
s_lock = mgos_rlock_create();
932-
}
933929
return true;
934930
}

0 commit comments

Comments
 (0)