7474#define MOD_BT_GATTS_SUBSCRIBE_EVT (0x0080)
7575#define MOD_BT_GATTC_MTU_EVT (0x0100)
7676#define MOD_BT_GATTS_MTU_EVT (0x0200)
77+ #define MOD_BT_GATTS_CLOSE_EVT (0x0400)
7778
7879/******************************************************************************
7980 DEFINE PRIVATE TYPES
@@ -245,7 +246,7 @@ static esp_ble_adv_params_t bt_adv_params_sec = {
245246};
246247
247248
248- static bool mod_bt_is_deinit ;
249+ static bool mod_bt_allow_resume_deinit ;
249250static uint16_t mod_bt_gatts_mtu_restore = 0 ;
250251static bool mod_bt_is_conn_restore_available ;
251252
@@ -288,8 +289,8 @@ void modbt_init0(void) {
288289 }
289290 else
290291 {
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 );
293294 }
294295 bt_event_group = xEventGroupCreate ();
295296
@@ -304,13 +305,56 @@ void modbt_init0(void) {
304305
305306 esp_bt_controller_mem_release (ESP_BT_MODE_CLASSIC_BT );
306307
307- mod_bt_is_deinit = false;
308+ mod_bt_allow_resume_deinit = false;
308309 mod_bt_is_conn_restore_available = false;
309310}
310311
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+
311355void bt_resume (bool reconnect )
312356{
313- if (mod_bt_is_deinit && !bt_obj .init )
357+ if (mod_bt_allow_resume_deinit && !bt_obj .init )
314358 {
315359 esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT ();
316360 esp_bt_controller_init (& bt_cfg );
@@ -367,7 +411,7 @@ void bt_resume(bool reconnect)
367411 }
368412
369413 bt_obj .init = true;
370- mod_bt_is_deinit = false;
414+ mod_bt_allow_resume_deinit = false;
371415 }
372416}
373417
@@ -404,7 +448,7 @@ static void close_connection (int32_t conn_id) {
404448 for (mp_uint_t i = 0 ; i < MP_STATE_PORT (btc_conn_list ).len ; i ++ ) {
405449 bt_connection_obj_t * connection_obj = ((bt_connection_obj_t * )(MP_STATE_PORT (btc_conn_list ).items [i ]));
406450 /* 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 )) {
408452 connection_obj -> conn_id = -1 ;
409453 mp_obj_list_remove ((void * )& MP_STATE_PORT (btc_conn_list ), connection_obj );
410454 }
@@ -821,13 +865,16 @@ static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_
821865 }
822866 }
823867 bt_obj .events |= MOD_BT_GATTS_DISCONN_EVT ;
868+ xEventGroupSetBits (bt_event_group , MOD_BT_GATTS_DISCONN_EVT );
824869 if (bt_obj .trigger & MOD_BT_GATTS_DISCONN_EVT ) {
825870 mp_irq_queue_interrupt (bluetooth_callback_handler , (void * )& bt_obj );
826871 }
827872 break ;
873+ case ESP_GATTS_CLOSE_EVT :
874+ xEventGroupSetBits (bt_event_group , MOD_BT_GATTS_CLOSE_EVT );
875+ break ;
828876 case ESP_GATTS_OPEN_EVT :
829877 case ESP_GATTS_CANCEL_OPEN_EVT :
830- case ESP_GATTS_CLOSE_EVT :
831878 case ESP_GATTS_LISTEN_EVT :
832879 case ESP_GATTS_CONGEST_EVT :
833880 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
9721019STATIC MP_DEFINE_CONST_FUN_OBJ_KW (bt_init_obj , 1 , bt_init );
9731020
9741021mp_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 ;
9841022
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);
9981024 return mp_const_none ;
9991025}
10001026STATIC 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) {
20412067 esp_ble_gattc_close (bt_obj .gattc_if , self -> conn_id );
20422068 esp_ble_gap_disconnect (self -> srv_bda );
20432069 /* 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 )
20452071 {
20462072 self -> conn_id = -1 ;
20472073 }
0 commit comments