Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.

Commit dc4bc94

Browse files
author
iwahdan88
committed
esp32/modbt: included an init param to enable/disable modem sleep
1 parent caa3669 commit dc4bc94

File tree

1 file changed

+54
-13
lines changed

1 file changed

+54
-13
lines changed

esp32/mods/modbt.c

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ static void gap_events_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_para
236236
static void gattc_events_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param);
237237
static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
238238
static void close_connection(int32_t conn_id);
239+
static bool modem_sleep(bool enable);
239240

240241
STATIC void bluetooth_callback_handler(void *arg);
241242
STATIC 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+
362393
static 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

793844
STATIC 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
};
798850
STATIC 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

Comments
 (0)