diff --git a/cores/esp32/esp32-hal-bt.c b/cores/esp32/esp32-hal-bt.c index 5f1148bd492..1b1a8f400fe 100644 --- a/cores/esp32/esp32-hal-bt.c +++ b/cores/esp32/esp32-hal-bt.c @@ -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 */ diff --git a/cores/esp32/esp32-hal-bt.h b/cores/esp32/esp32-hal-bt.h index 8ab03eea904..76232add271 100644 --- a/cores/esp32/esp32-hal-bt.h +++ b/cores/esp32/esp32-hal-bt.h @@ -31,9 +31,7 @@ typedef enum { BT_MODE_BTDM } bt_mode; -bool btStarted(); bool btStart(); -bool btStartMode(bt_mode mode); bool btStop(); #ifdef __cplusplus diff --git a/cores/esp32/esp32-hal-misc.c b/cores/esp32/esp32-hal-misc.c index 50e2973d27a..1fbfda39ec1 100644 --- a/cores/esp32/esp32-hal-misc.c +++ b/cores/esp32/esp32-hal-misc.c @@ -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" @@ -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 #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) @@ -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(); @@ -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); } diff --git a/libraries/BLE/src/BLEDevice.cpp b/libraries/BLE/src/BLEDevice.cpp index 186b36d6a33..4cbd8ae436b 100644 --- a/libraries/BLE/src/BLEDevice.cpp +++ b/libraries/BLE/src/BLEDevice.cpp @@ -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 #include #include diff --git a/libraries/BluetoothSerial/src/BluetoothSerial.cpp b/libraries/BluetoothSerial/src/BluetoothSerial.cpp index 3d00504c1b1..f9654ed96fb 100644 --- a/libraries/BluetoothSerial/src/BluetoothSerial.cpp +++ b/libraries/BluetoothSerial/src/BluetoothSerial.cpp @@ -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; } @@ -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); } @@ -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; } diff --git a/libraries/SimpleBLE/src/SimpleBLE.cpp b/libraries/SimpleBLE/src/SimpleBLE.cpp index 3c4ed915c05..9b38e937490 100644 --- a/libraries/SimpleBLE/src/SimpleBLE.cpp +++ b/libraries/SimpleBLE/src/SimpleBLE.cpp @@ -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" @@ -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; } @@ -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(); diff --git a/libraries/SimpleBLE/src/SimpleBLE.h b/libraries/SimpleBLE/src/SimpleBLE.h index 23c1cdb593f..1f16e54a185 100644 --- a/libraries/SimpleBLE/src/SimpleBLE.h +++ b/libraries/SimpleBLE/src/SimpleBLE.h @@ -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 #include