Fix crash with bluetooth tracker and esp-idf (#4140)

This commit is contained in:
J. Nick Koston 2022-12-05 09:49:39 -10:00 committed by GitHub
parent 16e523ca68
commit dbbbba3cf8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 5 deletions

View file

@ -238,6 +238,11 @@ async def to_code(config):
if CORE.using_esp_idf: if CORE.using_esp_idf:
add_idf_sdkconfig_option("CONFIG_BT_ENABLED", True) add_idf_sdkconfig_option("CONFIG_BT_ENABLED", True)
# https://github.com/espressif/esp-idf/issues/4101
# https://github.com/espressif/esp-idf/issues/2503
# Match arduino CONFIG_BTU_TASK_STACK_SIZE
# https://github.com/espressif/arduino-esp32/blob/fd72cf46ad6fc1a6de99c1d83ba8eba17d80a4ee/tools/sdk/esp32/sdkconfig#L1866
add_idf_sdkconfig_option("CONFIG_BTU_TASK_STACK_SIZE", 8192)
cg.add_define("USE_OTA_STATE_CALLBACK") # To be notified when an OTA update starts cg.add_define("USE_OTA_STATE_CALLBACK") # To be notified when an OTA update starts

View file

@ -180,7 +180,13 @@ void ESP32BLETracker::loop() {
ESP_LOGE(TAG, "ESP-IDF BLE scan could not restart after 255 attempts, rebooting to restore BLE stack..."); ESP_LOGE(TAG, "ESP-IDF BLE scan could not restart after 255 attempts, rebooting to restore BLE stack...");
App.reboot(); App.reboot();
} }
esp_ble_gap_stop_scanning(); if (xSemaphoreTake(this->scan_end_lock_, 0L)) {
xSemaphoreGive(this->scan_end_lock_);
} else {
ESP_LOGD(TAG, "Stopping scan after failure...");
esp_ble_gap_stop_scanning();
this->cancel_timeout("scan");
}
if (this->scan_start_failed_) { if (this->scan_start_failed_) {
ESP_LOGE(TAG, "Scan start failed: %d", this->scan_start_failed_); ESP_LOGE(TAG, "Scan start failed: %d", this->scan_start_failed_);
this->scan_start_failed_ = ESP_BT_STATUS_SUCCESS; this->scan_start_failed_ = ESP_BT_STATUS_SUCCESS;
@ -199,10 +205,18 @@ void ESP32BLETracker::loop() {
if (promote_to_connecting) { if (promote_to_connecting) {
for (auto *client : this->clients_) { for (auto *client : this->clients_) {
if (client->state() == ClientState::DISCOVERED) { if (client->state() == ClientState::DISCOVERED) {
ESP_LOGD(TAG, "Pausing scan to make connection..."); if (xSemaphoreTake(this->scan_end_lock_, 0L)) {
esp_ble_gap_stop_scanning(); // Scanner is not running since we got the
// We only want to promote one client at a time. // lock, so we can promote the client.
client->set_state(ClientState::READY_TO_CONNECT); xSemaphoreGive(this->scan_end_lock_);
// We only want to promote one client at a time.
// once the scanner is fully stopped.
client->set_state(ClientState::READY_TO_CONNECT);
} else {
ESP_LOGD(TAG, "Pausing scan to make connection...");
esp_ble_gap_stop_scanning();
this->cancel_timeout("scan");
}
break; break;
} }
} }