@@ -236,6 +236,7 @@ static void gap_events_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_para
236236static void gattc_events_handler (esp_gattc_cb_event_t event , esp_gatt_if_t gattc_if , esp_ble_gattc_cb_param_t * param );
237237static void gatts_event_handler (esp_gatts_cb_event_t event , esp_gatt_if_t gatts_if , esp_ble_gatts_cb_param_t * param );
238238static void close_connection (int32_t conn_id );
239+ static bool modem_sleep (bool enable );
239240
240241STATIC void bluetooth_callback_handler (void * arg );
241242STATIC void gattc_char_callback_handler (void * arg );
@@ -359,6 +360,36 @@ static void close_connection (int32_t conn_id) {
359360 }
360361}
361362
363+ static bool modem_sleep (bool enable )
364+ {
365+ bool ret = true;
366+ if (enable )
367+ {
368+ /* Enable Modem Sleep */
369+ if (ESP_OK != esp_bt_sleep_enable ())
370+ {
371+ /* Failed*/
372+ ret = false;
373+ }
374+ }
375+ else
376+ {
377+ /* Disable Modem Sleep */
378+ if (esp_bt_sleep_disable ())
379+ {
380+ /* Failed*/
381+ ret = false;
382+ }
383+ /* Wakeup the modem is it is sleeping */
384+ if (esp_bt_controller_is_sleeping () && ret )
385+ {
386+ esp_bt_controller_wakeup_request ();
387+ }
388+ }
389+
390+ return ret ;
391+ }
392+
362393static bt_char_obj_t * find_gattc_char (int32_t conn_id , uint16_t char_handle ) {
363394 for (mp_uint_t i = 0 ; i < MP_STATE_PORT (btc_conn_list ).len ; i ++ ) {
364395 // search through the connections
@@ -787,13 +818,34 @@ static mp_obj_t bt_init_helper(bt_obj_t *self, const mp_arg_val_t *args) {
787818
788819 bt_obj .gatts_conn_id = -1 ;
789820
821+
822+
823+ /* Set BLE modem sleep flag*/
824+ if (args [2 ].u_obj != MP_OBJ_NULL ) {
825+ if (mp_obj_is_true (args [2 ].u_obj ))
826+ {
827+ if (!modem_sleep (true))
828+ {
829+ nlr_raise (mp_obj_new_exception_msg (& mp_type_OSError , "Failed to Enable Bluetooth modem Sleep" ));
830+ }
831+ }
832+ else
833+ {
834+ if (!modem_sleep (false))
835+ {
836+ nlr_raise (mp_obj_new_exception_msg (& mp_type_OSError , "Failed to Disable Bluetooth modem Sleep" ));
837+ }
838+ }
839+ }
840+
790841 return mp_const_none ;
791842}
792843
793844STATIC const mp_arg_t bt_init_args [] = {
794845 { MP_QSTR_id , MP_ARG_INT , {.u_int = 0 } },
795846 { MP_QSTR_mode , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = E_BT_STACK_MODE_BLE } },
796847 { MP_QSTR_antenna , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_obj = MP_OBJ_NULL } },
848+ { MP_QSTR_modem_sleep , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_obj = MP_OBJ_NULL } },
797849};
798850STATIC mp_obj_t bt_make_new (const mp_obj_type_t * type , mp_uint_t n_args , mp_uint_t n_kw , const mp_obj_t * all_args ) {
799851 // parse args
@@ -899,20 +951,9 @@ STATIC mp_obj_t bt_modem_sleep(mp_uint_t n_args, const mp_obj_t *args) {
899951 {
900952 if (n_args > 1 )
901953 {
902- if (mp_obj_is_true (args [1 ]))
954+ if (! modem_sleep ( mp_obj_is_true (args [1 ]) ))
903955 {
904- /* Enable Modem Sleep */
905- esp_bt_sleep_enable ();
906- }
907- else
908- {
909- /* Disable Modem Sleep */
910- esp_bt_sleep_disable ();
911- /* Wakeup the modem is it is sleeping */
912- if (esp_bt_controller_is_sleeping ())
913- {
914- esp_bt_controller_wakeup_request ();
915- }
956+ nlr_raise (mp_obj_new_exception_msg (& mp_type_OSError , mpexception_os_operation_failed ));
916957 }
917958 }
918959 else
0 commit comments