Skip to content

Refactor BT enable #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: bt_enable_refac
Choose a base branch
from
165 changes: 68 additions & 97 deletions cores/esp32/esp32-hal-bt.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,123 +12,94 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "sdkconfig.h"
#include "soc/soc_caps.h"
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BLUEDROID_ENABLED) && SOC_BT_SUPPORTED

#include "esp_bt.h"
#include "esp_bt_main.h"
#include "esp_bt_device.h"
#include "esp32-hal-bt.h"
#include "esp32-hal-log.h"

#if SOC_BT_SUPPORTED
#ifdef CONFIG_BT_ENABLED
static bool _bt_initialized = false;
static bool _bt_enabled = false;

#if CONFIG_IDF_TARGET_ESP32
bool btInUse() {
return true;
return _bt_initialized && _bt_enabled;
}
#else
// user may want to change it to free resources
__attribute__((weak)) bool btInUse() {
return true;
}
#endif

#include "esp_bt.h"
bool btStart() {
esp_err_t err;

#ifdef CONFIG_BTDM_CONTROLLER_MODE_BTDM
#define BT_MODE ESP_BT_MODE_BTDM
#elif defined(CONFIG_BTDM_CONTROLLER_MODE_BR_EDR_ONLY)
#define BT_MODE ESP_BT_MODE_CLASSIC_BT
#else
#define BT_MODE ESP_BT_MODE_BLE
#endif
if(_bt_enabled) {
return true;
}

bool btStarted() {
return (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_ENABLED);
}
if(!_bt_initialized) {
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();

bool btStart() {
return btStartMode(BT_MODE);
}
err = esp_bt_controller_init(&bt_cfg);
if(err != ESP_OK) {
log_e("BT controller initialize failed: %s", esp_err_to_name(err));
return false;
}

bool btStartMode(bt_mode mode) {
esp_bt_mode_t esp_bt_mode;
esp_bt_controller_config_t cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
#if CONFIG_IDF_TARGET_ESP32
switch (mode) {
case BT_MODE_BLE: esp_bt_mode = ESP_BT_MODE_BLE; break;
case BT_MODE_CLASSIC_BT: esp_bt_mode = ESP_BT_MODE_CLASSIC_BT; break;
case BT_MODE_BTDM: esp_bt_mode = ESP_BT_MODE_BTDM; break;
default: esp_bt_mode = BT_MODE; break;
}
// esp_bt_controller_enable(MODE) This mode must be equal as the mode in “cfg” of esp_bt_controller_init().
cfg.mode = esp_bt_mode;
if (cfg.mode == ESP_BT_MODE_CLASSIC_BT) {
esp_bt_controller_mem_release(ESP_BT_MODE_BLE);
}
#else
// other esp variants dont support BT-classic / DM.
esp_bt_mode = BT_MODE;
#endif

if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_ENABLED) {
return true;
}
esp_err_t ret;
if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_IDLE) {
if ((ret = esp_bt_controller_init(&cfg)) != ESP_OK) {
log_e("initialize controller failed: %s", esp_err_to_name(ret));
return false;
_bt_initialized = true;
}

err = esp_bt_controller_enable(ESP_BT_MODE_BTDM);
if(err != ESP_OK) {
log_e("BT controller enable failed: %s", esp_err_to_name(err));
return false;
}
while (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_IDLE) {}
}
if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_INITED) {
if ((ret = esp_bt_controller_enable(esp_bt_mode)) != ESP_OK) {
log_e("BT Enable mode=%d failed %s", BT_MODE, esp_err_to_name(ret));
return false;

err = esp_bluedroid_init();
if(err != ESP_OK) {
log_e("Bluedroid initialize failed: %s", esp_err_to_name(err));
esp_bt_controller_disable();
return false;
}

err = esp_bluedroid_enable();
if(err != ESP_OK) {
log_e("Bluedroid enable failed: %s", esp_err_to_name(err));
esp_bluedroid_deinit();
esp_bt_controller_disable();
return false;
}
}
if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_ENABLED) {

_bt_enabled = true;
return true;
}
log_e("BT Start failed");
return false;
}

bool btStop() {
if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_IDLE) {
return true;
}
if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_ENABLED) {
if (esp_bt_controller_disable()) {
log_e("BT Disable failed");
return false;
}
while (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_ENABLED);
}
if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_INITED) {
if (esp_bt_controller_deinit()) {
log_e("BT deint failed");
return false;
esp_err_t err;

if(!_bt_enabled) {
return true;
}
vTaskDelay(1);
if (esp_bt_controller_get_status() != ESP_BT_CONTROLLER_STATUS_IDLE) {
return false;

err = esp_bluedroid_disable();
if(err != ESP_OK) {
log_e("Bluedroid disable failed: %s", esp_err_to_name(err));
return false;
}
return true;
}
log_e("BT Stop failed");
return false;
}

#else // CONFIG_BT_ENABLED
bool btStarted() {
return false;
}
err = esp_bluedroid_deinit();
if(err != ESP_OK) {
log_e("Bluedroid deinitialize failed: %s", esp_err_to_name(err));
return false;
}

bool btStart() {
return false;
}
err = esp_bt_controller_disable();
if(err != ESP_OK) {
log_e("BT controller disable failed: %s", esp_err_to_name(err));
return false;
}

bool btStop() {
return false;
_bt_enabled = false;
return true;
}

#endif /* CONFIG_BT_ENABLED */

#endif /* SOC_BT_SUPPORTED */
#endif /* CONFIG_BT_ENABLED CONFIG_BLUEDROID_ENABLED SOC_BT_SUPPORTED */
2 changes: 0 additions & 2 deletions cores/esp32/esp32-hal-bt.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ typedef enum {
BT_MODE_BTDM
} bt_mode;

bool btStarted();
bool btStart();
bool btStartMode(bt_mode mode);
bool btStop();

#ifdef __cplusplus
Expand Down
31 changes: 15 additions & 16 deletions cores/esp32/esp32-hal-misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

#include "sdkconfig.h"
#include "soc/soc_caps.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_attr.h"
Expand All @@ -25,9 +26,20 @@
#include "esp_ota_ops.h"
#endif //CONFIG_APP_ROLLBACK_ENABLE
#include "esp_private/startup_internal.h"
#if defined(CONFIG_BT_ENABLED) && SOC_BT_SUPPORTED
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BLUEDROID_ENABLED) && SOC_BT_SUPPORTED
#include "esp_bt.h"
#endif //CONFIG_BT_ENABLED
#if CONFIG_IDF_TARGET_ESP32
bool btInUse() __attribute__((weak));
bool btInUse() {
return true;
}
#else
bool btInUse() __attribute__((weak));
bool btInUse() {
return false;
}
#endif
#endif //CONFIG_BLUEDROID_ENABLED
#include <sys/time.h>
#include "soc/rtc.h"
#if !defined(CONFIG_IDF_TARGET_ESP32C2) && !defined(CONFIG_IDF_TARGET_ESP32C6) && !defined(CONFIG_IDF_TARGET_ESP32H2) && !defined(CONFIG_IDF_TARGET_ESP32P4)
Expand Down Expand Up @@ -243,19 +255,6 @@ bool verifyRollbackLater() {
}
#endif

#ifdef CONFIG_BT_ENABLED
#if CONFIG_IDF_TARGET_ESP32
//overwritten in esp32-hal-bt.c
bool btInUse() __attribute__((weak));
bool btInUse() {
return false;
}
#else
//from esp32-hal-bt.c
extern bool btInUse();
#endif
#endif

#if CONFIG_SPIRAM_SUPPORT || CONFIG_SPIRAM
ESP_SYSTEM_INIT_FN(init_psram_new, CORE, BIT(0), 99) {
psramInit();
Expand Down Expand Up @@ -305,7 +304,7 @@ void initArduino() {
if (err) {
log_e("Failed to initialize NVS! Error: %u", err);
}
#if defined(CONFIG_BT_ENABLED) && SOC_BT_SUPPORTED
#if defined(CONFIG_BLUEDROID_ENABLED) && SOC_BT_SUPPORTED
if (!btInUse()) {
esp_bt_controller_mem_release(ESP_BT_MODE_BTDM);
}
Expand Down
2 changes: 1 addition & 1 deletion libraries/BLE/src/BLEDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#if SOC_BLE_SUPPORTED

#include "sdkconfig.h"
#if defined(CONFIG_BLUEDROID_ENABLED)
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BLUEDROID_ENABLED)
#include <freertos/FreeRTOS.h>
#include <freertos/event_groups.h>
#include <freertos/task.h>
Expand Down
6 changes: 3 additions & 3 deletions libraries/BluetoothSerial/src/BluetoothSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ static bool _init_bt(const char *deviceName, bt_mode mode) {
}
}

if (!btStarted() && !btStartMode(mode)) {
if (!btStart()) {
log_e("initialize controller failed");
return false;
}
Expand Down Expand Up @@ -729,7 +729,7 @@ static bool _init_bt(const char *deviceName, bt_mode mode) {
}

static bool _stop_bt() {
if (btStarted()) {
if (btStart()) {
if (_spp_client) {
esp_spp_disconnect(_spp_client);
}
Expand Down Expand Up @@ -1115,7 +1115,7 @@ bool BluetoothSerial::isReady(bool checkMaster, int timeout) {
log_e("Master mode is not active. Call begin(localName, true) to enable Master mode");
return false;
}
if (!btStarted()) {
if (!btStart()) {
log_e("BT is not initialized. Call begin() first");
return false;
}
Expand Down
10 changes: 4 additions & 6 deletions libraries/SimpleBLE/src/SimpleBLE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "sdkconfig.h"

#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BLUEDROID_ENABLED)

#include "SimpleBLE.h"
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BLUEDROID_ENABLED) && SOC_BT_SUPPORTED

#include "esp32-hal-log.h"

#include "esp_bt.h"
Expand Down Expand Up @@ -62,7 +60,7 @@ static void _on_gap(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
}

static bool _init_gap(const char *name) {
if (!btStarted() && !btStart()) {
if (!btStart()) {
log_e("btStart failed");
return false;
}
Expand Down Expand Up @@ -95,7 +93,7 @@ static bool _init_gap(const char *name) {
}

static bool _stop_gap() {
if (btStarted()) {
if (btStart()) {
esp_bluedroid_disable();
esp_bluedroid_deinit();
btStop();
Expand Down
4 changes: 2 additions & 2 deletions libraries/SimpleBLE/src/SimpleBLE.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
#define _SIMPLE_BLE_H_

#include "sdkconfig.h"

#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BLUEDROID_ENABLED)
#include "soc/soc_caps.h"
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BLUEDROID_ENABLED) && SOC_BT_SUPPORTED

#include <cstdint>
#include <cstdio>
Expand Down
Loading