Patching for BLE co-existence

This commit is contained in:
RoboMagus 2024-11-16 20:51:13 +01:00
parent ec06744bd5
commit ad5967ad56
3 changed files with 44 additions and 19 deletions

View file

@ -149,17 +149,30 @@ bool ESP32BLE::ble_setup_() {
} }
#endif #endif
#ifdef BT_CLASSIC_INCLUDED_PLEASE_DONT_MEM_RELEASE
esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT); esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT);
#endif
err = esp_bluedroid_init(); if (esp_bluedroid_get_status() == ESP_BLUEDROID_STATUS_UNINITIALIZED) {
if (err != ESP_OK) { ESP_LOGD(TAG, "Initializing BlueDroid");
ESP_LOGE(TAG, "esp_bluedroid_init failed: %d", err); err = esp_bluedroid_init();
return false; if (err != ESP_OK) {
ESP_LOGE(TAG, "%s initialize bluedroid failed: %s\n", __func__, esp_err_to_name(err));
return false;
}
} else {
ESP_LOGD(TAG, "BlueDroid Already Initialized!");
} }
err = esp_bluedroid_enable();
if (err != ESP_OK) { if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
ESP_LOGE(TAG, "esp_bluedroid_enable failed: %d", err); ESP_LOGD(TAG, "Enabling BlueDroid");
return false; err = esp_bluedroid_enable();
if (err != ESP_OK) {
ESP_LOGE(TAG, "%s enable bluedroid failed: %s\n", __func__, esp_err_to_name(err));
return false;
}
} else {
ESP_LOGD(TAG, "BlueDroid Already Enabled!");
} }
if (!this->gap_event_handlers_.empty()) { if (!this->gap_event_handlers_.empty()) {

View file

@ -191,3 +191,5 @@ async def to_code(config):
add_idf_sdkconfig_option("CONFIG_BT_LOG_BTC_TRACE_LEVEL_DEBUG", True) add_idf_sdkconfig_option("CONFIG_BT_LOG_BTC_TRACE_LEVEL_DEBUG", True)
add_idf_sdkconfig_option("CONFIG_BT_LOG_BTC_TRACE_LEVEL", 5) add_idf_sdkconfig_option("CONFIG_BT_LOG_BTC_TRACE_LEVEL", 5)
cg.add_define("BT_CLASSIC_INCLUDED_PLEASE_DONT_MEM_RELEASE")

View file

@ -33,9 +33,9 @@ void ESP32BtClassic::setup() {
ESP_LOGE(TAG, "BT Classic could not be set up"); ESP_LOGE(TAG, "BT Classic could not be set up");
this->mark_failed(); this->mark_failed();
#ifdef USE_TEXT_SENSOR #ifdef USE_TEXT_SENSOR
if (last_error_sensor_) { if (last_error_sensor_) {
last_error_sensor_->publish_state("boot"); last_error_sensor_->publish_state("boot");
} }
#endif #endif
return; return;
} }
@ -56,6 +56,12 @@ bool ESP32BtClassic::bt_setup_() {
return false; return false;
} }
#else #else
ESP_LOGI(TAG, "BT_MODE: %d", BT_MODE);
// ESP_BT_MODE_IDLE = 0x00
// ESP_BT_MODE_BLE = 0x01
// ESP_BT_MODE_CLASSIC_BT = 0x02
// ESP_BT_MODE_BTDM = 0x03
if (esp_bt_controller_get_status() != ESP_BT_CONTROLLER_STATUS_ENABLED) { if (esp_bt_controller_get_status() != ESP_BT_CONTROLLER_STATUS_ENABLED) {
// start bt controller // start bt controller
if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_IDLE) { if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_IDLE) {
@ -82,20 +88,24 @@ bool ESP32BtClassic::bt_setup_() {
} }
#endif #endif
ESP_LOGD(TAG, "Initializing BlueDroid");
if (esp_bluedroid_get_status() == ESP_BLUEDROID_STATUS_UNINITIALIZED) { if (esp_bluedroid_get_status() == ESP_BLUEDROID_STATUS_UNINITIALIZED) {
ESP_LOGD(TAG, "Initializing BlueDroid");
if ((err = esp_bluedroid_init()) != ESP_OK) { if ((err = esp_bluedroid_init()) != ESP_OK) {
ESP_LOGE(TAG, "%s initialize bluedroid failed: %s\n", __func__, esp_err_to_name(err)); ESP_LOGE(TAG, "%s initialize bluedroid failed: %s\n", __func__, esp_err_to_name(err));
return false; return false;
} }
} else {
ESP_LOGD(TAG, "BlueDroid Already Initialized!");
} }
ESP_LOGD(TAG, "Enabling BlueDroid");
if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) { if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
ESP_LOGD(TAG, "Enabling BlueDroid");
if ((err = esp_bluedroid_enable()) != ESP_OK) { if ((err = esp_bluedroid_enable()) != ESP_OK) {
ESP_LOGE(TAG, "%s enable bluedroid failed: %s\n", __func__, esp_err_to_name(err)); ESP_LOGE(TAG, "%s enable bluedroid failed: %s\n", __func__, esp_err_to_name(err));
return false; return false;
} }
} else {
ESP_LOGD(TAG, "BlueDroid Already Enabled!");
} }
bool success = gap_startup(); bool success = gap_startup();
@ -156,11 +166,10 @@ void ESP32BtClassic::startScan(const uint64_t u64_addr) {
for (auto *listener : scan_start_listners_) { for (auto *listener : scan_start_listners_) {
listener->on_scan_start(); listener->on_scan_start();
} }
} else {
ESP_LOGE(TAG, "Could not start scan! Error: %s\n BlueDroid status: %d\n Controller status: %d",
esp_err_to_name(result), esp_bluedroid_get_status(), esp_bt_controller_get_status());
} }
else {
ESP_LOGE(TAG, "Could not start scan! Error: %s\n BlueDroid status: %d\n Controller status: %d", esp_err_to_name(result), esp_bluedroid_get_status(), esp_bt_controller_get_status());
}
} }
void ESP32BtClassic::addScan(const bt_scan_item &scan) { void ESP32BtClassic::addScan(const bt_scan_item &scan) {
@ -195,7 +204,7 @@ void ESP32BtClassic::real_gap_event_handler_(esp_bt_gap_cb_event_t event, esp_bt
switch (event) { switch (event) {
case ESP_BT_GAP_READ_REMOTE_NAME_EVT: { case ESP_BT_GAP_READ_REMOTE_NAME_EVT: {
const auto& scan_item = handle_scan_result(param->read_rmt_name); const auto &scan_item = handle_scan_result(param->read_rmt_name);
for (auto *listener : scan_result_listners_) { for (auto *listener : scan_result_listners_) {
listener->on_scan_result(param->read_rmt_name, scan_item); listener->on_scan_result(param->read_rmt_name, scan_item);
@ -233,7 +242,8 @@ optional<bt_scan_item> ESP32BtClassic::handle_scan_result(const rmt_name_result
ESP_LOGD(TAG, "Device '%02X:%02X:%02X:%02X:%02X:%02X' scan result: %s (%d) in %lu ms", EXPAND_MAC_F(result.bda), ESP_LOGD(TAG, "Device '%02X:%02X:%02X:%02X:%02X:%02X' scan result: %s (%d) in %lu ms", EXPAND_MAC_F(result.bda),
esp_bt_status_to_str(result.stat), result.stat, scanDuration); esp_bt_status_to_str(result.stat), result.stat, scanDuration);
ESP_LOGD(TAG, "BlueDroid status: %d\n Controller status: %d", esp_bluedroid_get_status(), esp_bt_controller_get_status()); ESP_LOGD(TAG, "BlueDroid status: %d\n Controller status: %d", esp_bluedroid_get_status(),
esp_bt_controller_get_status());
if (it->scans_remaining == 0) { if (it->scans_remaining == 0) {
ESP_LOGW(TAG, "Device '%02X:%02X:%02X:%02X:%02X:%02X' not found on final scan. Removing from scan list.", ESP_LOGW(TAG, "Device '%02X:%02X:%02X:%02X:%02X:%02X' not found on final scan. Removing from scan list.",