@@ -226,6 +226,9 @@ static esp_ble_adv_params_t bt_adv_params = {
226
226
.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY ,
227
227
};
228
228
229
+ static bool mod_bt_is_deinit ;
230
+ static bool mod_bt_is_conn_restore_available ;
231
+
229
232
/******************************************************************************
230
233
DECLARE PRIVATE FUNCTIONS
231
234
******************************************************************************/
@@ -237,6 +240,9 @@ static void close_connection(int32_t conn_id);
237
240
STATIC void bluetooth_callback_handler (void * arg );
238
241
STATIC void gattc_char_callback_handler (void * arg );
239
242
STATIC void gatts_char_callback_handler (void * arg );
243
+ static mp_obj_t modbt_start_scan (mp_obj_t timeout );
244
+ static mp_obj_t modbt_conn_disconnect (mp_obj_t self_in );
245
+ static mp_obj_t modbt_connect (mp_obj_t addr );
240
246
241
247
/******************************************************************************
242
248
DEFINE PUBLIC FUNCTIONS
@@ -263,6 +269,69 @@ void modbt_init0(void) {
263
269
mp_obj_list_init ((mp_obj_t )& MP_STATE_PORT (bts_attr_list ), 0 );
264
270
265
271
esp_bt_controller_mem_release (ESP_BT_MODE_CLASSIC_BT );
272
+
273
+ mod_bt_is_deinit = false;
274
+ mod_bt_is_conn_restore_available = false;
275
+ }
276
+
277
+ void bt_resume (bool reconnect )
278
+ {
279
+ if (mod_bt_is_deinit && !bt_obj .init )
280
+ {
281
+ esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT ();
282
+ esp_bt_controller_init (& bt_cfg );
283
+
284
+ esp_bt_controller_enable (ESP_BT_MODE_BLE );
285
+
286
+ if (ESP_OK != esp_bluedroid_init ()) {
287
+ nlr_raise (mp_obj_new_exception_msg (& mp_type_OSError , "Bluetooth init failed" ));
288
+ }
289
+ if (ESP_OK != esp_bluedroid_enable ()) {
290
+ nlr_raise (mp_obj_new_exception_msg (& mp_type_OSError , "Bluetooth enable failed" ));
291
+ }
292
+
293
+ esp_ble_gattc_app_register (MOD_BT_CLIENT_APP_ID );
294
+ esp_ble_gatts_app_register (MOD_BT_SERVER_APP_ID );
295
+
296
+ esp_ble_gatt_set_local_mtu (200 );
297
+
298
+ bt_connection_obj_t * connection_obj = NULL ;
299
+
300
+ if (MP_STATE_PORT (btc_conn_list ).len > 0 )
301
+ {
302
+ /* Get the Last gattc connection obj before sleep */
303
+ connection_obj = ((bt_connection_obj_t * )(MP_STATE_PORT (btc_conn_list ).items [MP_STATE_PORT (btc_conn_list ).len - 1 ]));
304
+ }
305
+
306
+ if (reconnect )
307
+ {
308
+ /* Check if there was a gattc connection Active before sleep */
309
+ if (connection_obj != NULL ) {
310
+ if (connection_obj -> conn_id >= 0 ) {
311
+ /* Enable Scan */
312
+ modbt_start_scan (MP_OBJ_NEW_SMALL_INT (-1 ));
313
+ mp_hal_delay_ms (50 );
314
+ while (!bt_obj .scanning ){
315
+ /* Wait for scanning to start */
316
+ }
317
+ /* re-connect to Last Connection */
318
+ mp_obj_list_remove ((void * )& MP_STATE_PORT (btc_conn_list ), connection_obj );
319
+ mp_obj_list_append ((void * )& MP_STATE_PORT (btc_conn_list ), modbt_connect (mp_obj_new_bytes ((const byte * )connection_obj -> srv_bda , 6 )));
320
+
321
+ mod_bt_is_conn_restore_available = true;
322
+ }
323
+ }
324
+
325
+ /* See if there was an averstisment active before Sleep */
326
+ if (bt_obj .advertising )
327
+ {
328
+ esp_ble_gap_start_advertising (& bt_adv_params );
329
+ }
330
+ }
331
+
332
+ bt_obj .init = true;
333
+ mod_bt_is_deinit = false;
334
+ }
266
335
}
267
336
268
337
/******************************************************************************
@@ -282,7 +351,8 @@ static esp_ble_scan_params_t ble_scan_params = {
282
351
static void close_connection (int32_t conn_id ) {
283
352
for (mp_uint_t i = 0 ; i < MP_STATE_PORT (btc_conn_list ).len ; i ++ ) {
284
353
bt_connection_obj_t * connection_obj = ((bt_connection_obj_t * )(MP_STATE_PORT (btc_conn_list ).items [i ]));
285
- if (connection_obj -> conn_id == conn_id ) {
354
+ /* Only reset Conn Id if it is a normal disconnect not module de-init to mark conn obj to be restored */
355
+ if (connection_obj -> conn_id == conn_id && (!mod_bt_is_deinit )) {
286
356
connection_obj -> conn_id = -1 ;
287
357
mp_obj_list_remove ((void * )& MP_STATE_PORT (btc_conn_list ), connection_obj );
288
358
}
@@ -461,6 +531,7 @@ static void gattc_events_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc
461
531
// intentional fall through
462
532
case ESP_GATTC_CLOSE_EVT :
463
533
close_connection (p_data -> close .conn_id );
534
+ bt_obj .busy = false;
464
535
break ;
465
536
default :
466
537
break ;
@@ -765,10 +836,23 @@ mp_obj_t bt_deinit(mp_obj_t self_in) {
765
836
esp_ble_gap_stop_scanning ();
766
837
bt_obj .scanning = false;
767
838
}
839
+ /* Indicate module is de-initializing */
840
+ mod_bt_is_deinit = true;
841
+
842
+ bt_connection_obj_t * connection_obj ;
843
+
844
+ for (mp_uint_t i = 0 ; i < MP_STATE_PORT (btc_conn_list ).len ; i ++ )
845
+ {
846
+ // loop through the connections
847
+ connection_obj = ((bt_connection_obj_t * )(MP_STATE_PORT (btc_conn_list ).items [i ]));
848
+ //close connections
849
+ modbt_conn_disconnect (connection_obj );
850
+ }
768
851
esp_bluedroid_disable ();
769
852
esp_bluedroid_deinit ();
770
853
esp_bt_controller_disable ();
771
854
bt_obj .init = false;
855
+ mod_bt_is_conn_restore_available = false;
772
856
}
773
857
return mp_const_none ;
774
858
}
@@ -795,6 +879,11 @@ STATIC mp_obj_t bt_start_scan(mp_obj_t self_in, mp_obj_t timeout) {
795
879
}
796
880
STATIC MP_DEFINE_CONST_FUN_OBJ_2 (bt_start_scan_obj , bt_start_scan );
797
881
882
+ static mp_obj_t modbt_start_scan (mp_obj_t timeout )
883
+ {
884
+ return bt_start_scan (NULL , timeout );
885
+ }
886
+
798
887
STATIC mp_obj_t bt_isscanning (mp_obj_t self_in ) {
799
888
if (bt_obj .scanning ) {
800
889
return mp_const_true ;
@@ -990,6 +1079,11 @@ STATIC mp_obj_t bt_connect(mp_obj_t self_in, mp_obj_t addr) {
990
1079
}
991
1080
STATIC MP_DEFINE_CONST_FUN_OBJ_2 (bt_connect_obj , bt_connect );
992
1081
1082
+ static mp_obj_t modbt_connect (mp_obj_t addr )
1083
+ {
1084
+ return bt_connect (NULL , addr );
1085
+ }
1086
+
993
1087
STATIC mp_obj_t bt_set_advertisement (mp_uint_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
994
1088
static const mp_arg_t allowed_args [] = {
995
1089
{ MP_QSTR_name , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_obj = mp_const_none } },
@@ -1510,12 +1604,21 @@ STATIC mp_obj_t bt_conn_disconnect(mp_obj_t self_in) {
1510
1604
if (self -> conn_id >= 0 ) {
1511
1605
esp_ble_gattc_close (bt_obj .gattc_if , self -> conn_id );
1512
1606
esp_ble_gap_disconnect (self -> srv_bda );
1513
- self -> conn_id = -1 ;
1607
+ /* Only reset Conn Id if it is a normal disconnect not module de-init to mark conn obj to be restored */
1608
+ if (!mod_bt_is_deinit )
1609
+ {
1610
+ self -> conn_id = -1 ;
1611
+ }
1514
1612
}
1515
1613
return mp_const_none ;
1516
1614
}
1517
1615
STATIC MP_DEFINE_CONST_FUN_OBJ_1 (bt_conn_disconnect_obj , bt_conn_disconnect );
1518
1616
1617
+ static mp_obj_t modbt_conn_disconnect (mp_obj_t self_in )
1618
+ {
1619
+ return bt_conn_disconnect (self_in );
1620
+ }
1621
+
1519
1622
STATIC mp_obj_t bt_conn_services (mp_obj_t self_in ) {
1520
1623
bt_connection_obj_t * self = self_in ;
1521
1624
bt_event_result_t bt_event ;
0 commit comments