From 13e7553d09fef7e57aa8b9aabf518b1cc0760175 Mon Sep 17 00:00:00 2001 From: Th3maz1ng Date: Sun, 23 Apr 2023 20:33:11 +0200 Subject: [PATCH] Added new experimental functions to put the BLE modem to sleep to reduce power consumption --- app/ble/ble_modem.c | 37 +++++++++++++++++++++++++++++++++++++ app/ble/ble_modem.h | 28 ++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/app/ble/ble_modem.c b/app/ble/ble_modem.c index ccd90aa..05034fe 100644 --- a/app/ble/ble_modem.c +++ b/app/ble/ble_modem.c @@ -2,6 +2,7 @@ #include "app_common.h" #include "host/ble_hs.h" #include "FreeRTOS.h" +#include "wm_bt.h" #include "wm_bt_def.h" #include "wm_bt_app.h" #include "wm_bt_util.h" @@ -10,6 +11,8 @@ //Is needed for the BT off workaround #include "wm_wifi.h" +static bool _ble_modem_is_sleeping = false; + bool ble_modem_on(bool bluetoothOnly, bool startService) { int status = BLE_HS_ENOERR; @@ -33,9 +36,18 @@ bool ble_modem_on(bool bluetoothOnly, bool startService) } else { + // We disable the sleep mode if it was enabled + if(!ble_modem_is_sleeping()) + { + if(!ble_modem_wake()) + TLS_BT_APPL_TRACE_ERROR("%s, ble_modem_wake failed"NEW_LINE, __FUNCTION__); + } + + // If we successfully started the modem, we can set it's working mode. if(bluetoothOnly) tls_rf_bt_mode(true); + // Start the ble service if it was asked and if it is not yet started if(startService && !ble_service_is_started()) serviceStartSuccess = ble_service_start(); @@ -56,6 +68,12 @@ bool ble_modem_off(void) return TLS_BT_STATUS_SUCCESS; } + if(ble_modem_is_sleeping()) + { + if(!ble_modem_wake()) + TLS_BT_APPL_TRACE_ERROR("%s, ble_modem_wake failed"NEW_LINE, __FUNCTION__); + } + if(ble_service_is_started()) { serviceStopSuccess = ble_service_stop(); @@ -96,3 +114,22 @@ bool is_ble_modem_on(void) return false; return true; } + +bool ble_modem_sleep(void) +{ + bool status = tls_bt_ctrl_sleep(true); + if(status)_ble_modem_is_sleeping = true; + return status; +} + +bool ble_modem_wake(void) +{ + bool status = tls_bt_ctrl_sleep(false); + if(status)_ble_modem_is_sleeping = false; + return status; +} + +bool ble_modem_is_sleeping(void) +{ + return _ble_modem_is_sleeping; +} diff --git a/app/ble/ble_modem.h b/app/ble/ble_modem.h index 0e3ae25..98bed3c 100644 --- a/app/ble/ble_modem.h +++ b/app/ble/ble_modem.h @@ -29,4 +29,32 @@ bool ble_modem_off(void); */ bool is_ble_modem_on(void); +/** + * @brief Sets the BLE modem to sleep during IDLE times. + * This will lead to a latency penalty of arround 10 seconds, + * upon receiving a new frame but reduces power consumption when using BLE. + * + * @return true if the modem was successfully set to sleep. + * @return false if not + */ +bool ble_modem_sleep(void); + +/** + * @brief Disables the sleep state of the BLE modem. Latency will increase and power + * consumption also. + * + * @return true if the modem woke up successfully + * @return false otherwise + */ +bool ble_modem_wake(void); + +/** + * @brief Returns true if the BLE modem is allowed to sleep during IDLE times, false + * if it is not allowed to sleep. + * + * @return true if it is sleeping or allowed to sleep + * @return false otherwise + */ +bool ble_modem_is_sleeping(void); + #endif //BLE_MODEM_H