diff --git a/esphome/components/esp32_ble_tracker/__init__.py b/esphome/components/esp32_ble_tracker/__init__.py index f70fea0639..6d7868d4c5 100644 --- a/esphome/components/esp32_ble_tracker/__init__.py +++ b/esphome/components/esp32_ble_tracker/__init__.py @@ -239,6 +239,8 @@ async def to_code(config): if CORE.using_esp_idf: add_idf_sdkconfig_option("CONFIG_BT_ENABLED", True) + cg.add_define("USE_OTA_STATE_CALLBACK") # To be notified when an OTA update starts + ESP32_BLE_START_SCAN_ACTION_SCHEMA = cv.Schema( { diff --git a/esphome/components/esp32_ble_tracker/esp32_ble_tracker.cpp b/esphome/components/esp32_ble_tracker/esp32_ble_tracker.cpp index 68d88eb1e8..f7e51a8ab3 100644 --- a/esphome/components/esp32_ble_tracker/esp32_ble_tracker.cpp +++ b/esphome/components/esp32_ble_tracker/esp32_ble_tracker.cpp @@ -1,10 +1,11 @@ #ifdef USE_ESP32 #include "esp32_ble_tracker.h" -#include "esphome/core/log.h" #include "esphome/core/application.h" -#include "esphome/core/helpers.h" +#include "esphome/core/defines.h" #include "esphome/core/hal.h" +#include "esphome/core/helpers.h" +#include "esphome/core/log.h" #include #include @@ -15,6 +16,10 @@ #include #include +#ifdef USE_OTA +#include "esphome/components/ota/ota_component.h" +#endif + #ifdef USE_ARDUINO #include #endif @@ -52,8 +57,16 @@ void ESP32BLETracker::setup() { return; } +#ifdef USE_OTA + ota::global_ota_component->add_on_state_callback([this](ota::OTAState state, float progress, uint8_t error) { + if (state == ota::OTA_STARTED) { + this->stop_scan(); + } + }); +#endif + if (this->scan_continuous_) { - global_esp32_ble_tracker->start_scan_(true); + this->start_scan_(true); } } @@ -83,10 +96,10 @@ void ESP32BLETracker::loop() { if (!connecting && xSemaphoreTake(this->scan_end_lock_, 0L)) { xSemaphoreGive(this->scan_end_lock_); if (this->scan_continuous_) { - global_esp32_ble_tracker->start_scan_(false); + this->start_scan_(false); } else if (xSemaphoreTake(this->scan_end_lock_, 0L) && !this->scanner_idle_) { xSemaphoreGive(this->scan_end_lock_); - global_esp32_ble_tracker->end_of_scan_(); + this->end_of_scan_(); return; } } @@ -150,7 +163,7 @@ void ESP32BLETracker::loop() { void ESP32BLETracker::start_scan() { if (xSemaphoreTake(this->scan_end_lock_, 0L)) { xSemaphoreGive(this->scan_end_lock_); - global_esp32_ble_tracker->start_scan_(true); + this->start_scan_(true); } else { ESP_LOGW(TAG, "Scan requested when a scan is already in progress. Ignoring."); } @@ -299,21 +312,21 @@ void ESP32BLETracker::gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_ga void ESP32BLETracker::real_gap_event_handler_(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) { switch (event) { case ESP_GAP_BLE_SCAN_RESULT_EVT: - global_esp32_ble_tracker->gap_scan_result_(param->scan_rst); + this->gap_scan_result_(param->scan_rst); break; case ESP_GAP_BLE_SCAN_PARAM_SET_COMPLETE_EVT: - global_esp32_ble_tracker->gap_scan_set_param_complete_(param->scan_param_cmpl); + this->gap_scan_set_param_complete_(param->scan_param_cmpl); break; case ESP_GAP_BLE_SCAN_START_COMPLETE_EVT: - global_esp32_ble_tracker->gap_scan_start_complete_(param->scan_start_cmpl); + this->gap_scan_start_complete_(param->scan_start_cmpl); break; case ESP_GAP_BLE_SCAN_STOP_COMPLETE_EVT: - global_esp32_ble_tracker->gap_scan_stop_complete_(param->scan_stop_cmpl); + this->gap_scan_stop_complete_(param->scan_stop_cmpl); break; default: break; } - for (auto *client : global_esp32_ble_tracker->clients_) { + for (auto *client : this->clients_) { client->gap_event_handler(event, param); } } @@ -351,7 +364,7 @@ void ESP32BLETracker::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_i void ESP32BLETracker::real_gattc_event_handler_(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) { - for (auto *client : global_esp32_ble_tracker->clients_) { + for (auto *client : this->clients_) { client->gattc_event_handler(event, gattc_if, param); } } diff --git a/esphome/components/ota/__init__.py b/esphome/components/ota/__init__.py index b3d3b7ad23..1bc4012ce2 100644 --- a/esphome/components/ota/__init__.py +++ b/esphome/components/ota/__init__.py @@ -78,6 +78,7 @@ CONFIG_SCHEMA = cv.Schema( async def to_code(config): var = cg.new_Pvariable(config[CONF_ID]) cg.add(var.set_port(config[CONF_PORT])) + cg.add_define("USE_OTA") if CONF_PASSWORD in config: cg.add(var.set_auth_password(config[CONF_PASSWORD])) cg.add_define("USE_OTA_PASSWORD") diff --git a/esphome/components/ota/ota_component.cpp b/esphome/components/ota/ota_component.cpp index 25c9f2e912..a02d64cd08 100644 --- a/esphome/components/ota/ota_component.cpp +++ b/esphome/components/ota/ota_component.cpp @@ -21,6 +21,8 @@ static const char *const TAG = "ota"; static const uint8_t OTA_VERSION_1_0 = 1; +OTAComponent *global_ota_component = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) + std::unique_ptr make_ota_backend() { #ifdef USE_ARDUINO #ifdef USE_ESP8266 @@ -35,6 +37,8 @@ std::unique_ptr make_ota_backend() { #endif // USE_ESP_IDF } +OTAComponent::OTAComponent() { global_ota_component = this; } + void OTAComponent::setup() { server_ = socket::socket_ip(SOCK_STREAM, 0); if (server_ == nullptr) { diff --git a/esphome/components/ota/ota_component.h b/esphome/components/ota/ota_component.h index d178805168..9a1c92f727 100644 --- a/esphome/components/ota/ota_component.h +++ b/esphome/components/ota/ota_component.h @@ -41,6 +41,7 @@ enum OTAState { OTA_COMPLETED = 0, OTA_STARTED, OTA_IN_PROGRESS, OTA_ERROR }; /// OTAComponent provides a simple way to integrate Over-the-Air updates into your app using ArduinoOTA. class OTAComponent : public Component { public: + OTAComponent(); #ifdef USE_OTA_PASSWORD void set_auth_password(const std::string &password) { password_ = password; } #endif // USE_OTA_PASSWORD @@ -103,5 +104,7 @@ class OTAComponent : public Component { #endif }; +extern OTAComponent *global_ota_component; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) + } // namespace ota } // namespace esphome diff --git a/esphome/core/defines.h b/esphome/core/defines.h index 90676c421e..cea53b8545 100644 --- a/esphome/core/defines.h +++ b/esphome/core/defines.h @@ -32,6 +32,7 @@ #define USE_MEDIA_PLAYER #define USE_MQTT #define USE_NUMBER +#define USE_OTA #define USE_OTA_PASSWORD #define USE_OTA_STATE_CALLBACK #define USE_POWER_SUPPLY