74
74
#define MOD_BT_GATTS_SUBSCRIBE_EVT (0x0080)
75
75
#define MOD_BT_GATTC_MTU_EVT (0x0100)
76
76
#define MOD_BT_GATTS_MTU_EVT (0x0200)
77
+ #define MOD_BT_GATTS_CLOSE_EVT (0x0400)
77
78
78
79
/******************************************************************************
79
80
DEFINE PRIVATE TYPES
@@ -245,7 +246,7 @@ static esp_ble_adv_params_t bt_adv_params_sec = {
245
246
};
246
247
247
248
248
- static bool mod_bt_is_deinit ;
249
+ static bool mod_bt_allow_resume_deinit ;
249
250
static uint16_t mod_bt_gatts_mtu_restore = 0 ;
250
251
static bool mod_bt_is_conn_restore_available ;
251
252
@@ -288,8 +289,8 @@ void modbt_init0(void) {
288
289
}
289
290
else
290
291
{
291
- //Using only MTU event in group for now
292
- xEventGroupClearBits (bt_event_group , MOD_BT_GATTC_MTU_EVT | MOD_BT_GATTS_MTU_EVT );
292
+ //Using only specific events in group for now
293
+ xEventGroupClearBits (bt_event_group , MOD_BT_GATTC_MTU_EVT | MOD_BT_GATTS_MTU_EVT | MOD_BT_GATTS_DISCONN_EVT | MOD_BT_GATTS_CLOSE_EVT );
293
294
}
294
295
bt_event_group = xEventGroupCreate ();
295
296
@@ -304,13 +305,56 @@ void modbt_init0(void) {
304
305
305
306
esp_bt_controller_mem_release (ESP_BT_MODE_CLASSIC_BT );
306
307
307
- mod_bt_is_deinit = false;
308
+ mod_bt_allow_resume_deinit = false;
308
309
mod_bt_is_conn_restore_available = false;
309
310
}
310
311
312
+ void modbt_deinit (bool allow_reconnect )
313
+ {
314
+ uint16_t timeout = 0 ;
315
+ if (bt_obj .init )
316
+ {
317
+ if (bt_obj .scanning ) {
318
+ esp_ble_gap_stop_scanning ();
319
+ bt_obj .scanning = false;
320
+ }
321
+ /* Allow reconnection flag */
322
+ mod_bt_allow_resume_deinit = allow_reconnect ;
323
+
324
+ bt_connection_obj_t * connection_obj ;
325
+
326
+ for (mp_uint_t i = 0 ; i < MP_STATE_PORT (btc_conn_list ).len ; i ++ )
327
+ {
328
+ // loop through the connections
329
+ connection_obj = ((bt_connection_obj_t * )(MP_STATE_PORT (btc_conn_list ).items [i ]));
330
+ //close connections
331
+ modbt_conn_disconnect (connection_obj );
332
+ }
333
+ while ((MP_STATE_PORT (btc_conn_list ).len > 0 ) && (timeout < 20 ) && !mod_bt_allow_resume_deinit )
334
+ {
335
+ vTaskDelay (50 / portTICK_PERIOD_MS );
336
+ timeout ++ ;
337
+ }
338
+
339
+ if (bt_obj .gatts_conn_id >= 0 )
340
+ {
341
+ bt_obj .advertising = false;
342
+ esp_ble_gatts_close (bt_obj .gatts_if , bt_obj .gatts_conn_id );
343
+ xEventGroupWaitBits (bt_event_group , MOD_BT_GATTS_DISCONN_EVT | MOD_BT_GATTS_CLOSE_EVT , true, true, 1000 /portTICK_PERIOD_MS );
344
+ }
345
+
346
+ esp_bluedroid_disable ();
347
+ esp_bluedroid_deinit ();
348
+ esp_bt_controller_disable ();
349
+ bt_obj .init = false;
350
+ mod_bt_is_conn_restore_available = false;
351
+ xEventGroupClearBits (bt_event_group , MOD_BT_GATTC_MTU_EVT | MOD_BT_GATTS_MTU_EVT | MOD_BT_GATTS_DISCONN_EVT | MOD_BT_GATTS_CLOSE_EVT );
352
+ }
353
+ }
354
+
311
355
void bt_resume (bool reconnect )
312
356
{
313
- if (mod_bt_is_deinit && !bt_obj .init )
357
+ if (mod_bt_allow_resume_deinit && !bt_obj .init )
314
358
{
315
359
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT ();
316
360
esp_bt_controller_init (& bt_cfg );
@@ -367,7 +411,7 @@ void bt_resume(bool reconnect)
367
411
}
368
412
369
413
bt_obj .init = true;
370
- mod_bt_is_deinit = false;
414
+ mod_bt_allow_resume_deinit = false;
371
415
}
372
416
}
373
417
@@ -404,7 +448,7 @@ static void close_connection (int32_t conn_id) {
404
448
for (mp_uint_t i = 0 ; i < MP_STATE_PORT (btc_conn_list ).len ; i ++ ) {
405
449
bt_connection_obj_t * connection_obj = ((bt_connection_obj_t * )(MP_STATE_PORT (btc_conn_list ).items [i ]));
406
450
/* Only reset Conn Id if it is a normal disconnect not module de-init to mark conn obj to be restored */
407
- if (connection_obj -> conn_id == conn_id && (!mod_bt_is_deinit )) {
451
+ if (connection_obj -> conn_id == conn_id && (!mod_bt_allow_resume_deinit )) {
408
452
connection_obj -> conn_id = -1 ;
409
453
mp_obj_list_remove ((void * )& MP_STATE_PORT (btc_conn_list ), connection_obj );
410
454
}
@@ -821,13 +865,16 @@ static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_
821
865
}
822
866
}
823
867
bt_obj .events |= MOD_BT_GATTS_DISCONN_EVT ;
868
+ xEventGroupSetBits (bt_event_group , MOD_BT_GATTS_DISCONN_EVT );
824
869
if (bt_obj .trigger & MOD_BT_GATTS_DISCONN_EVT ) {
825
870
mp_irq_queue_interrupt (bluetooth_callback_handler , (void * )& bt_obj );
826
871
}
827
872
break ;
873
+ case ESP_GATTS_CLOSE_EVT :
874
+ xEventGroupSetBits (bt_event_group , MOD_BT_GATTS_CLOSE_EVT );
875
+ break ;
828
876
case ESP_GATTS_OPEN_EVT :
829
877
case ESP_GATTS_CANCEL_OPEN_EVT :
830
- case ESP_GATTS_CLOSE_EVT :
831
878
case ESP_GATTS_LISTEN_EVT :
832
879
case ESP_GATTS_CONGEST_EVT :
833
880
default :
@@ -972,29 +1019,8 @@ STATIC mp_obj_t bt_init(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw
972
1019
STATIC MP_DEFINE_CONST_FUN_OBJ_KW (bt_init_obj , 1 , bt_init );
973
1020
974
1021
mp_obj_t bt_deinit (mp_obj_t self_in ) {
975
- if (bt_obj .init ) {
976
- if (bt_obj .scanning ) {
977
- esp_ble_gap_stop_scanning ();
978
- bt_obj .scanning = false;
979
- }
980
- /* Indicate module is de-initializing */
981
- mod_bt_is_deinit = true;
982
-
983
- bt_connection_obj_t * connection_obj ;
984
1022
985
- for (mp_uint_t i = 0 ; i < MP_STATE_PORT (btc_conn_list ).len ; i ++ )
986
- {
987
- // loop through the connections
988
- connection_obj = ((bt_connection_obj_t * )(MP_STATE_PORT (btc_conn_list ).items [i ]));
989
- //close connections
990
- modbt_conn_disconnect (connection_obj );
991
- }
992
- esp_bluedroid_disable ();
993
- esp_bluedroid_deinit ();
994
- esp_bt_controller_disable ();
995
- bt_obj .init = false;
996
- mod_bt_is_conn_restore_available = false;
997
- }
1023
+ modbt_deinit (false);
998
1024
return mp_const_none ;
999
1025
}
1000
1026
STATIC MP_DEFINE_CONST_FUN_OBJ_1 (bt_deinit_obj , bt_deinit );
@@ -2041,7 +2067,7 @@ STATIC mp_obj_t bt_conn_disconnect(mp_obj_t self_in) {
2041
2067
esp_ble_gattc_close (bt_obj .gattc_if , self -> conn_id );
2042
2068
esp_ble_gap_disconnect (self -> srv_bda );
2043
2069
/* Only reset Conn Id if it is a normal disconnect not module de-init to mark conn obj to be restored */
2044
- if (!mod_bt_is_deinit )
2070
+ if (!mod_bt_allow_resume_deinit )
2045
2071
{
2046
2072
self -> conn_id = -1 ;
2047
2073
}
0 commit comments