mirror of
https://github.com/esphome/esphome.git
synced 2025-09-04 20:32:21 +01:00
Inherit esp32_ble_beacon
from esp32_ble
(#6908)
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
172a358d01
commit
5d5f3276e9
@@ -7,10 +7,10 @@ from esphome.components.esp32 import add_idf_sdkconfig_option, get_esp32_variant
|
||||
|
||||
DEPENDENCIES = ["esp32"]
|
||||
CODEOWNERS = ["@jesserockz", "@Rapsssito"]
|
||||
CONFLICTS_WITH = ["esp32_ble_beacon"]
|
||||
|
||||
CONF_BLE_ID = "ble_id"
|
||||
CONF_IO_CAPABILITY = "io_capability"
|
||||
CONF_ADVERTISING_CYCLE_TIME = "advertising_cycle_time"
|
||||
|
||||
NO_BLUETOOTH_VARIANTS = [const.VARIANT_ESP32S2]
|
||||
|
||||
@@ -34,6 +34,19 @@ IO_CAPABILITY = {
|
||||
"display_yes_no": IoCapability.IO_CAP_IO,
|
||||
}
|
||||
|
||||
esp_power_level_t = cg.global_ns.enum("esp_power_level_t")
|
||||
|
||||
TX_POWER_LEVELS = {
|
||||
-12: esp_power_level_t.ESP_PWR_LVL_N12,
|
||||
-9: esp_power_level_t.ESP_PWR_LVL_N9,
|
||||
-6: esp_power_level_t.ESP_PWR_LVL_N6,
|
||||
-3: esp_power_level_t.ESP_PWR_LVL_N3,
|
||||
0: esp_power_level_t.ESP_PWR_LVL_N0,
|
||||
3: esp_power_level_t.ESP_PWR_LVL_P3,
|
||||
6: esp_power_level_t.ESP_PWR_LVL_P6,
|
||||
9: esp_power_level_t.ESP_PWR_LVL_P9,
|
||||
}
|
||||
|
||||
CONFIG_SCHEMA = cv.Schema(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(ESP32BLE),
|
||||
@@ -41,6 +54,9 @@ CONFIG_SCHEMA = cv.Schema(
|
||||
IO_CAPABILITY, lower=True
|
||||
),
|
||||
cv.Optional(CONF_ENABLE_ON_BOOT, default=True): cv.boolean,
|
||||
cv.Optional(
|
||||
CONF_ADVERTISING_CYCLE_TIME, default="10s"
|
||||
): cv.positive_time_period_milliseconds,
|
||||
}
|
||||
).extend(cv.COMPONENT_SCHEMA)
|
||||
|
||||
@@ -58,6 +74,7 @@ async def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
cg.add(var.set_enable_on_boot(config[CONF_ENABLE_ON_BOOT]))
|
||||
cg.add(var.set_io_capability(config[CONF_IO_CAPABILITY]))
|
||||
cg.add(var.set_advertising_cycle_time(config[CONF_ADVERTISING_CYCLE_TIME]))
|
||||
await cg.register_component(var, config)
|
||||
|
||||
if CORE.using_esp_idf:
|
||||
|
@@ -78,6 +78,11 @@ void ESP32BLE::advertising_set_manufacturer_data(const std::vector<uint8_t> &dat
|
||||
this->advertising_start();
|
||||
}
|
||||
|
||||
void ESP32BLE::advertising_register_raw_advertisement_callback(std::function<void(bool)> &&callback) {
|
||||
this->advertising_init_();
|
||||
this->advertising_->register_raw_advertisement_callback(std::move(callback));
|
||||
}
|
||||
|
||||
void ESP32BLE::advertising_add_service_uuid(ESPBTUUID uuid) {
|
||||
this->advertising_init_();
|
||||
this->advertising_->add_service_uuid(uuid);
|
||||
@@ -102,7 +107,7 @@ bool ESP32BLE::ble_pre_setup_() {
|
||||
void ESP32BLE::advertising_init_() {
|
||||
if (this->advertising_ != nullptr)
|
||||
return;
|
||||
this->advertising_ = new BLEAdvertising(); // NOLINT(cppcoreguidelines-owning-memory)
|
||||
this->advertising_ = new BLEAdvertising(this->advertising_cycle_time_); // NOLINT(cppcoreguidelines-owning-memory)
|
||||
|
||||
this->advertising_->set_scan_response(true);
|
||||
this->advertising_->set_min_preferred_interval(0x06);
|
||||
@@ -312,6 +317,9 @@ void ESP32BLE::loop() {
|
||||
delete ble_event; // NOLINT(cppcoreguidelines-owning-memory)
|
||||
ble_event = this->ble_events_.pop();
|
||||
}
|
||||
if (this->advertising_ != nullptr) {
|
||||
this->advertising_->loop();
|
||||
}
|
||||
}
|
||||
|
||||
void ESP32BLE::gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) {
|
||||
|
@@ -3,6 +3,8 @@
|
||||
#include "ble_advertising.h"
|
||||
#include "ble_uuid.h"
|
||||
|
||||
#include <functional>
|
||||
|
||||
#include "esphome/core/automation.h"
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/defines.h"
|
||||
@@ -76,6 +78,11 @@ class ESP32BLE : public Component {
|
||||
public:
|
||||
void set_io_capability(IoCapability io_capability) { this->io_cap_ = (esp_ble_io_cap_t) io_capability; }
|
||||
|
||||
void set_advertising_cycle_time(uint32_t advertising_cycle_time) {
|
||||
this->advertising_cycle_time_ = advertising_cycle_time;
|
||||
}
|
||||
uint32_t get_advertising_cycle_time() const { return this->advertising_cycle_time_; }
|
||||
|
||||
void enable();
|
||||
void disable();
|
||||
bool is_active();
|
||||
@@ -89,6 +96,7 @@ class ESP32BLE : public Component {
|
||||
void advertising_set_manufacturer_data(const std::vector<uint8_t> &data);
|
||||
void advertising_add_service_uuid(ESPBTUUID uuid);
|
||||
void advertising_remove_service_uuid(ESPBTUUID uuid);
|
||||
void advertising_register_raw_advertisement_callback(std::function<void(bool)> &&callback);
|
||||
|
||||
void register_gap_event_handler(GAPEventHandler *handler) { this->gap_event_handlers_.push_back(handler); }
|
||||
void register_gattc_event_handler(GATTcEventHandler *handler) { this->gattc_event_handlers_.push_back(handler); }
|
||||
@@ -121,6 +129,7 @@ class ESP32BLE : public Component {
|
||||
Queue<BLEEvent> ble_events_;
|
||||
BLEAdvertising *advertising_;
|
||||
esp_ble_io_cap_t io_cap_{ESP_IO_CAP_NONE};
|
||||
uint32_t advertising_cycle_time_;
|
||||
bool enable_on_boot_;
|
||||
};
|
||||
|
||||
|
@@ -10,9 +10,9 @@
|
||||
namespace esphome {
|
||||
namespace esp32_ble {
|
||||
|
||||
static const char *const TAG = "esp32_ble";
|
||||
static const char *const TAG = "esp32_ble.advertising";
|
||||
|
||||
BLEAdvertising::BLEAdvertising() {
|
||||
BLEAdvertising::BLEAdvertising(uint32_t advertising_cycle_time) : advertising_cycle_time_(advertising_cycle_time) {
|
||||
this->advertising_data_.set_scan_rsp = false;
|
||||
this->advertising_data_.include_name = true;
|
||||
this->advertising_data_.include_txpower = true;
|
||||
@@ -64,7 +64,7 @@ void BLEAdvertising::set_manufacturer_data(const std::vector<uint8_t> &data) {
|
||||
}
|
||||
}
|
||||
|
||||
void BLEAdvertising::start() {
|
||||
esp_err_t BLEAdvertising::services_advertisement_() {
|
||||
int num_services = this->advertising_uuids_.size();
|
||||
if (num_services == 0) {
|
||||
this->advertising_data_.service_uuid_len = 0;
|
||||
@@ -87,8 +87,8 @@ void BLEAdvertising::start() {
|
||||
this->advertising_data_.include_txpower = !this->scan_response_;
|
||||
err = esp_ble_gap_config_adv_data(&this->advertising_data_);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "esp_ble_gap_config_adv_data failed (Advertising): %d", err);
|
||||
return;
|
||||
ESP_LOGE(TAG, "esp_ble_gap_config_adv_data failed (Advertising): %s", esp_err_to_name(err));
|
||||
return err;
|
||||
}
|
||||
|
||||
if (this->scan_response_) {
|
||||
@@ -101,8 +101,8 @@ void BLEAdvertising::start() {
|
||||
this->scan_response_data_.flag = 0;
|
||||
err = esp_ble_gap_config_adv_data(&this->scan_response_data_);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "esp_ble_gap_config_adv_data failed (Scan response): %d", err);
|
||||
return;
|
||||
ESP_LOGE(TAG, "esp_ble_gap_config_adv_data failed (Scan response): %s", esp_err_to_name(err));
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,8 +113,18 @@ void BLEAdvertising::start() {
|
||||
|
||||
err = esp_ble_gap_start_advertising(&this->advertising_params_);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "esp_ble_gap_start_advertising failed: %d", err);
|
||||
return;
|
||||
ESP_LOGE(TAG, "esp_ble_gap_start_advertising failed: %s", esp_err_to_name(err));
|
||||
return err;
|
||||
}
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
void BLEAdvertising::start() {
|
||||
if (this->current_adv_index_ == -1) {
|
||||
this->services_advertisement_();
|
||||
} else {
|
||||
this->raw_advertisements_callbacks_[this->current_adv_index_](true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -124,6 +134,29 @@ void BLEAdvertising::stop() {
|
||||
ESP_LOGE(TAG, "esp_ble_gap_stop_advertising failed: %d", err);
|
||||
return;
|
||||
}
|
||||
if (this->current_adv_index_ != -1) {
|
||||
this->raw_advertisements_callbacks_[this->current_adv_index_](false);
|
||||
}
|
||||
}
|
||||
|
||||
void BLEAdvertising::loop() {
|
||||
if (this->raw_advertisements_callbacks_.empty()) {
|
||||
return;
|
||||
}
|
||||
const uint32_t now = millis();
|
||||
if (now - this->last_advertisement_time_ > this->advertising_cycle_time_) {
|
||||
this->stop();
|
||||
this->current_adv_index_ += 1;
|
||||
if (this->current_adv_index_ >= this->raw_advertisements_callbacks_.size()) {
|
||||
this->current_adv_index_ = -1;
|
||||
}
|
||||
this->start();
|
||||
this->last_advertisement_time_ = now;
|
||||
}
|
||||
}
|
||||
|
||||
void BLEAdvertising::register_raw_advertisement_callback(std::function<void(bool)> &&callback) {
|
||||
this->raw_advertisements_callbacks_.push_back(std::move(callback));
|
||||
}
|
||||
|
||||
} // namespace esp32_ble
|
||||
|
@@ -1,20 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <functional>
|
||||
#include <vector>
|
||||
|
||||
#ifdef USE_ESP32
|
||||
|
||||
#include <esp_bt.h>
|
||||
#include <esp_gap_ble_api.h>
|
||||
#include <esp_gatts_api.h>
|
||||
|
||||
namespace esphome {
|
||||
namespace esp32_ble {
|
||||
|
||||
using raw_adv_data_t = struct {
|
||||
uint8_t *data;
|
||||
size_t length;
|
||||
esp_power_level_t power_level;
|
||||
};
|
||||
|
||||
class ESPBTUUID;
|
||||
|
||||
class BLEAdvertising {
|
||||
public:
|
||||
BLEAdvertising();
|
||||
BLEAdvertising(uint32_t advertising_cycle_time);
|
||||
|
||||
void loop();
|
||||
|
||||
void add_service_uuid(ESPBTUUID uuid);
|
||||
void remove_service_uuid(ESPBTUUID uuid);
|
||||
@@ -22,16 +33,25 @@ class BLEAdvertising {
|
||||
void set_min_preferred_interval(uint16_t interval) { this->advertising_data_.min_interval = interval; }
|
||||
void set_manufacturer_data(const std::vector<uint8_t> &data);
|
||||
void set_service_data(const std::vector<uint8_t> &data);
|
||||
void register_raw_advertisement_callback(std::function<void(bool)> &&callback);
|
||||
|
||||
void start();
|
||||
void stop();
|
||||
|
||||
protected:
|
||||
esp_err_t services_advertisement_();
|
||||
|
||||
bool scan_response_;
|
||||
esp_ble_adv_data_t advertising_data_;
|
||||
esp_ble_adv_data_t scan_response_data_;
|
||||
esp_ble_adv_params_t advertising_params_;
|
||||
std::vector<ESPBTUUID> advertising_uuids_;
|
||||
|
||||
std::vector<std::function<void(bool)>> raw_advertisements_callbacks_;
|
||||
|
||||
const uint32_t advertising_cycle_time_;
|
||||
uint32_t last_advertisement_time_{0};
|
||||
int8_t current_adv_index_{-1}; // -1 means standard scan response
|
||||
};
|
||||
|
||||
} // namespace esp32_ble
|
||||
|
Reference in New Issue
Block a user