@@ -226,6 +226,9 @@ static esp_ble_adv_params_t bt_adv_params = {
226226 .adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY ,
227227};
228228
229+ static bool mod_bt_is_deinit ;
230+ static bool mod_bt_is_conn_restore_available ;
231+
229232/******************************************************************************
230233 DECLARE PRIVATE FUNCTIONS
231234 ******************************************************************************/
@@ -237,6 +240,9 @@ static void close_connection(int32_t conn_id);
237240STATIC void bluetooth_callback_handler (void * arg );
238241STATIC void gattc_char_callback_handler (void * arg );
239242STATIC 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 );
240246
241247/******************************************************************************
242248 DEFINE PUBLIC FUNCTIONS
@@ -263,6 +269,69 @@ void modbt_init0(void) {
263269 mp_obj_list_init ((mp_obj_t )& MP_STATE_PORT (bts_attr_list ), 0 );
264270
265271 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+ }
266335}
267336
268337/******************************************************************************
@@ -282,7 +351,8 @@ static esp_ble_scan_params_t ble_scan_params = {
282351static void close_connection (int32_t conn_id ) {
283352 for (mp_uint_t i = 0 ; i < MP_STATE_PORT (btc_conn_list ).len ; i ++ ) {
284353 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 )) {
286356 connection_obj -> conn_id = -1 ;
287357 mp_obj_list_remove ((void * )& MP_STATE_PORT (btc_conn_list ), connection_obj );
288358 }
@@ -461,6 +531,7 @@ static void gattc_events_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc
461531 // intentional fall through
462532 case ESP_GATTC_CLOSE_EVT :
463533 close_connection (p_data -> close .conn_id );
534+ bt_obj .busy = false;
464535 break ;
465536 default :
466537 break ;
@@ -765,10 +836,23 @@ mp_obj_t bt_deinit(mp_obj_t self_in) {
765836 esp_ble_gap_stop_scanning ();
766837 bt_obj .scanning = false;
767838 }
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+ }
768851 esp_bluedroid_disable ();
769852 esp_bluedroid_deinit ();
770853 esp_bt_controller_disable ();
771854 bt_obj .init = false;
855+ mod_bt_is_conn_restore_available = false;
772856 }
773857 return mp_const_none ;
774858}
@@ -795,6 +879,11 @@ STATIC mp_obj_t bt_start_scan(mp_obj_t self_in, mp_obj_t timeout) {
795879}
796880STATIC MP_DEFINE_CONST_FUN_OBJ_2 (bt_start_scan_obj , bt_start_scan );
797881
882+ static mp_obj_t modbt_start_scan (mp_obj_t timeout )
883+ {
884+ return bt_start_scan (NULL , timeout );
885+ }
886+
798887STATIC mp_obj_t bt_isscanning (mp_obj_t self_in ) {
799888 if (bt_obj .scanning ) {
800889 return mp_const_true ;
@@ -990,6 +1079,11 @@ STATIC mp_obj_t bt_connect(mp_obj_t self_in, mp_obj_t addr) {
9901079}
9911080STATIC MP_DEFINE_CONST_FUN_OBJ_2 (bt_connect_obj , bt_connect );
9921081
1082+ static mp_obj_t modbt_connect (mp_obj_t addr )
1083+ {
1084+ return bt_connect (NULL , addr );
1085+ }
1086+
9931087STATIC mp_obj_t bt_set_advertisement (mp_uint_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
9941088 static const mp_arg_t allowed_args [] = {
9951089 { 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) {
15101604 if (self -> conn_id >= 0 ) {
15111605 esp_ble_gattc_close (bt_obj .gattc_if , self -> conn_id );
15121606 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+ }
15141612 }
15151613 return mp_const_none ;
15161614}
15171615STATIC MP_DEFINE_CONST_FUN_OBJ_1 (bt_conn_disconnect_obj , bt_conn_disconnect );
15181616
1617+ static mp_obj_t modbt_conn_disconnect (mp_obj_t self_in )
1618+ {
1619+ return bt_conn_disconnect (self_in );
1620+ }
1621+
15191622STATIC mp_obj_t bt_conn_services (mp_obj_t self_in ) {
15201623 bt_connection_obj_t * self = self_in ;
15211624 bt_event_result_t bt_event ;
0 commit comments