mirror of
https://github.com/esphome/esphome.git
synced 2024-11-10 09:17:46 +01:00
Allow ble tracker to subscribe to ota start and stop the scanning (#3800)
This commit is contained in:
parent
a5e3cd1a42
commit
f4b0917239
6 changed files with 36 additions and 12 deletions
|
@ -239,6 +239,8 @@ 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)
|
||||||
|
|
||||||
|
cg.add_define("USE_OTA_STATE_CALLBACK") # To be notified when an OTA update starts
|
||||||
|
|
||||||
|
|
||||||
ESP32_BLE_START_SCAN_ACTION_SCHEMA = cv.Schema(
|
ESP32_BLE_START_SCAN_ACTION_SCHEMA = cv.Schema(
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
#ifdef USE_ESP32
|
#ifdef USE_ESP32
|
||||||
|
|
||||||
#include "esp32_ble_tracker.h"
|
#include "esp32_ble_tracker.h"
|
||||||
#include "esphome/core/log.h"
|
|
||||||
#include "esphome/core/application.h"
|
#include "esphome/core/application.h"
|
||||||
#include "esphome/core/helpers.h"
|
#include "esphome/core/defines.h"
|
||||||
#include "esphome/core/hal.h"
|
#include "esphome/core/hal.h"
|
||||||
|
#include "esphome/core/helpers.h"
|
||||||
|
#include "esphome/core/log.h"
|
||||||
|
|
||||||
#include <nvs_flash.h>
|
#include <nvs_flash.h>
|
||||||
#include <freertos/FreeRTOSConfig.h>
|
#include <freertos/FreeRTOSConfig.h>
|
||||||
|
@ -15,6 +16,10 @@
|
||||||
#include <esp_gap_ble_api.h>
|
#include <esp_gap_ble_api.h>
|
||||||
#include <esp_bt_defs.h>
|
#include <esp_bt_defs.h>
|
||||||
|
|
||||||
|
#ifdef USE_OTA
|
||||||
|
#include "esphome/components/ota/ota_component.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef USE_ARDUINO
|
#ifdef USE_ARDUINO
|
||||||
#include <esp32-hal-bt.h>
|
#include <esp32-hal-bt.h>
|
||||||
#endif
|
#endif
|
||||||
|
@ -52,8 +57,16 @@ void ESP32BLETracker::setup() {
|
||||||
return;
|
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_) {
|
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)) {
|
if (!connecting && xSemaphoreTake(this->scan_end_lock_, 0L)) {
|
||||||
xSemaphoreGive(this->scan_end_lock_);
|
xSemaphoreGive(this->scan_end_lock_);
|
||||||
if (this->scan_continuous_) {
|
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_) {
|
} else if (xSemaphoreTake(this->scan_end_lock_, 0L) && !this->scanner_idle_) {
|
||||||
xSemaphoreGive(this->scan_end_lock_);
|
xSemaphoreGive(this->scan_end_lock_);
|
||||||
global_esp32_ble_tracker->end_of_scan_();
|
this->end_of_scan_();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -150,7 +163,7 @@ void ESP32BLETracker::loop() {
|
||||||
void ESP32BLETracker::start_scan() {
|
void ESP32BLETracker::start_scan() {
|
||||||
if (xSemaphoreTake(this->scan_end_lock_, 0L)) {
|
if (xSemaphoreTake(this->scan_end_lock_, 0L)) {
|
||||||
xSemaphoreGive(this->scan_end_lock_);
|
xSemaphoreGive(this->scan_end_lock_);
|
||||||
global_esp32_ble_tracker->start_scan_(true);
|
this->start_scan_(true);
|
||||||
} else {
|
} else {
|
||||||
ESP_LOGW(TAG, "Scan requested when a scan is already in progress. Ignoring.");
|
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) {
|
void ESP32BLETracker::real_gap_event_handler_(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) {
|
||||||
switch (event) {
|
switch (event) {
|
||||||
case ESP_GAP_BLE_SCAN_RESULT_EVT:
|
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;
|
break;
|
||||||
case ESP_GAP_BLE_SCAN_PARAM_SET_COMPLETE_EVT:
|
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;
|
break;
|
||||||
case ESP_GAP_BLE_SCAN_START_COMPLETE_EVT:
|
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;
|
break;
|
||||||
case ESP_GAP_BLE_SCAN_STOP_COMPLETE_EVT:
|
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;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
for (auto *client : global_esp32_ble_tracker->clients_) {
|
for (auto *client : this->clients_) {
|
||||||
client->gap_event_handler(event, param);
|
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,
|
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) {
|
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);
|
client->gattc_event_handler(event, gattc_if, param);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -78,6 +78,7 @@ CONFIG_SCHEMA = cv.Schema(
|
||||||
async def to_code(config):
|
async def to_code(config):
|
||||||
var = cg.new_Pvariable(config[CONF_ID])
|
var = cg.new_Pvariable(config[CONF_ID])
|
||||||
cg.add(var.set_port(config[CONF_PORT]))
|
cg.add(var.set_port(config[CONF_PORT]))
|
||||||
|
cg.add_define("USE_OTA")
|
||||||
if CONF_PASSWORD in config:
|
if CONF_PASSWORD in config:
|
||||||
cg.add(var.set_auth_password(config[CONF_PASSWORD]))
|
cg.add(var.set_auth_password(config[CONF_PASSWORD]))
|
||||||
cg.add_define("USE_OTA_PASSWORD")
|
cg.add_define("USE_OTA_PASSWORD")
|
||||||
|
|
|
@ -21,6 +21,8 @@ static const char *const TAG = "ota";
|
||||||
|
|
||||||
static const uint8_t OTA_VERSION_1_0 = 1;
|
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() {
|
std::unique_ptr<OTABackend> make_ota_backend() {
|
||||||
#ifdef USE_ARDUINO
|
#ifdef USE_ARDUINO
|
||||||
#ifdef USE_ESP8266
|
#ifdef USE_ESP8266
|
||||||
|
@ -35,6 +37,8 @@ std::unique_ptr<OTABackend> make_ota_backend() {
|
||||||
#endif // USE_ESP_IDF
|
#endif // USE_ESP_IDF
|
||||||
}
|
}
|
||||||
|
|
||||||
|
OTAComponent::OTAComponent() { global_ota_component = this; }
|
||||||
|
|
||||||
void OTAComponent::setup() {
|
void OTAComponent::setup() {
|
||||||
server_ = socket::socket_ip(SOCK_STREAM, 0);
|
server_ = socket::socket_ip(SOCK_STREAM, 0);
|
||||||
if (server_ == nullptr) {
|
if (server_ == nullptr) {
|
||||||
|
|
|
@ -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.
|
/// OTAComponent provides a simple way to integrate Over-the-Air updates into your app using ArduinoOTA.
|
||||||
class OTAComponent : public Component {
|
class OTAComponent : public Component {
|
||||||
public:
|
public:
|
||||||
|
OTAComponent();
|
||||||
#ifdef USE_OTA_PASSWORD
|
#ifdef USE_OTA_PASSWORD
|
||||||
void set_auth_password(const std::string &password) { password_ = password; }
|
void set_auth_password(const std::string &password) { password_ = password; }
|
||||||
#endif // USE_OTA_PASSWORD
|
#endif // USE_OTA_PASSWORD
|
||||||
|
@ -103,5 +104,7 @@ class OTAComponent : public Component {
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
|
extern OTAComponent *global_ota_component; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||||
|
|
||||||
} // namespace ota
|
} // namespace ota
|
||||||
} // namespace esphome
|
} // namespace esphome
|
||||||
|
|
|
@ -32,6 +32,7 @@
|
||||||
#define USE_MEDIA_PLAYER
|
#define USE_MEDIA_PLAYER
|
||||||
#define USE_MQTT
|
#define USE_MQTT
|
||||||
#define USE_NUMBER
|
#define USE_NUMBER
|
||||||
|
#define USE_OTA
|
||||||
#define USE_OTA_PASSWORD
|
#define USE_OTA_PASSWORD
|
||||||
#define USE_OTA_STATE_CALLBACK
|
#define USE_OTA_STATE_CALLBACK
|
||||||
#define USE_POWER_SUPPLY
|
#define USE_POWER_SUPPLY
|
||||||
|
|
Loading…
Reference in a new issue