Allow ble tracker to subscribe to ota start and stop the scanning (#3800)

This commit is contained in:
Jesse Hills 2022-09-14 16:49:20 +12:00 committed by GitHub
parent a5e3cd1a42
commit f4b0917239
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 36 additions and 12 deletions

View file

@ -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(
{

View file

@ -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 <nvs_flash.h>
#include <freertos/FreeRTOSConfig.h>
@ -15,6 +16,10 @@
#include <esp_gap_ble_api.h>
#include <esp_bt_defs.h>
#ifdef USE_OTA
#include "esphome/components/ota/ota_component.h"
#endif
#ifdef USE_ARDUINO
#include <esp32-hal-bt.h>
#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);
}
}

View file

@ -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")

View file

@ -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<OTABackend> make_ota_backend() {
#ifdef USE_ARDUINO
#ifdef USE_ESP8266
@ -35,6 +37,8 @@ std::unique_ptr<OTABackend> 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) {

View file

@ -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

View file

@ -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