1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-03 18:42:23 +01:00
This commit is contained in:
J. Nick Koston
2025-09-29 18:15:25 -05:00
parent 9ff838bf35
commit 43d8e213f6
6 changed files with 23 additions and 16 deletions

View File

@@ -160,7 +160,6 @@ esphome/components/esp_ldo/* @clydebarrow
esphome/components/espnow/* @jesserockz esphome/components/espnow/* @jesserockz
esphome/components/ethernet_info/* @gtjadsonsantos esphome/components/ethernet_info/* @gtjadsonsantos
esphome/components/event/* @nohat esphome/components/event/* @nohat
esphome/components/event_emitter/* @Rapsssito
esphome/components/exposure_notifications/* @OttoWinter esphome/components/exposure_notifications/* @OttoWinter
esphome/components/ezo/* @ssieb esphome/components/ezo/* @ssieb
esphome/components/ezo_pmp/* @carlos-sarmiento esphome/components/ezo_pmp/* @carlos-sarmiento

View File

@@ -209,7 +209,7 @@ void BLECharacteristic::gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt
break; // For some reason you can request a read but not want a response break; // For some reason you can request a read but not want a response
if (this->on_read_callback_) { if (this->on_read_callback_) {
this->on_read_callback_(param->read.conn_id); (*this->on_read_callback_)(param->read.conn_id);
} }
uint16_t max_offset = 22; uint16_t max_offset = 22;
@@ -279,7 +279,7 @@ void BLECharacteristic::gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt
if (!param->write.is_prep) { if (!param->write.is_prep) {
if (this->on_write_callback_) { if (this->on_write_callback_) {
this->on_write_callback_(this->value_, param->write.conn_id); (*this->on_write_callback_)(this->value_, param->write.conn_id);
} }
} }
@@ -292,7 +292,7 @@ void BLECharacteristic::gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt
this->write_event_ = false; this->write_event_ = false;
if (param->exec_write.exec_write_flag == ESP_GATT_PREP_WRITE_EXEC) { if (param->exec_write.exec_write_flag == ESP_GATT_PREP_WRITE_EXEC) {
if (this->on_write_callback_) { if (this->on_write_callback_) {
this->on_write_callback_(this->value_, param->exec_write.conn_id); (*this->on_write_callback_)(this->value_, param->exec_write.conn_id);
} }
} }
esp_err_t err = esp_err_t err =

View File

@@ -75,7 +75,8 @@ void BLEDescriptor::gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_
this->value_.attr_len = param->write.len; this->value_.attr_len = param->write.len;
memcpy(this->value_.attr_value, param->write.value, param->write.len); memcpy(this->value_.attr_value, param->write.value, param->write.len);
if (this->on_write_callback_) { if (this->on_write_callback_) {
this->on_write_callback_(std::span<const uint8_t>(param->write.value, param->write.len), param->write.conn_id); (*this->on_write_callback_)(std::span<const uint8_t>(param->write.value, param->write.len),
param->write.conn_id);
} }
break; break;
} }

View File

@@ -8,13 +8,14 @@
#include <esp_gatt_defs.h> #include <esp_gatt_defs.h>
#include <esp_gatts_api.h> #include <esp_gatts_api.h>
#include <span> #include <span>
#include <functional>
#include <memory>
namespace esphome { namespace esphome {
namespace esp32_ble_server { namespace esp32_ble_server {
using namespace esp32_ble; using namespace esp32_ble;
using namespace bytebuffer; using namespace bytebuffer;
using namespace event_emitter;
class BLECharacteristic; class BLECharacteristic;
@@ -34,9 +35,10 @@ class BLEDescriptor {
bool is_created() { return this->state_ == CREATED; } bool is_created() { return this->state_ == CREATED; }
bool is_failed() { return this->state_ == FAILED; } bool is_failed() { return this->state_ == FAILED; }
// Direct callback registration // Direct callback registration - only allocates when callback is set
void on_write(std::function<void(std::span<const uint8_t>, uint16_t)> &&callback) { void on_write(std::function<void(std::span<const uint8_t>, uint16_t)> &&callback) {
this->on_write_callback_ = std::move(callback); this->on_write_callback_ =
std::make_unique<std::function<void(std::span<const uint8_t>, uint16_t)>>(std::move(callback));
} }
protected: protected:
@@ -46,7 +48,7 @@ class BLEDescriptor {
esp_attr_value_t value_{}; esp_attr_value_t value_{};
std::function<void(std::span<const uint8_t>, uint16_t)> on_write_callback_{nullptr}; std::unique_ptr<std::function<void(std::span<const uint8_t>, uint16_t)>> on_write_callback_;
esp_gatt_perm_t permissions_{}; esp_gatt_perm_t permissions_{};

View File

@@ -154,7 +154,7 @@ void BLEServer::gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t ga
ESP_LOGD(TAG, "BLE Client connected"); ESP_LOGD(TAG, "BLE Client connected");
this->add_client_(param->connect.conn_id); this->add_client_(param->connect.conn_id);
if (this->on_connect_callback_) { if (this->on_connect_callback_) {
this->on_connect_callback_(param->connect.conn_id); (*this->on_connect_callback_)(param->connect.conn_id);
} }
break; break;
} }
@@ -163,7 +163,7 @@ void BLEServer::gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t ga
this->remove_client_(param->disconnect.conn_id); this->remove_client_(param->disconnect.conn_id);
this->parent_->advertising_start(); this->parent_->advertising_start();
if (this->on_disconnect_callback_) { if (this->on_disconnect_callback_) {
this->on_disconnect_callback_(param->disconnect.conn_id); (*this->on_disconnect_callback_)(param->disconnect.conn_id);
} }
break; break;
} }

View File

@@ -13,6 +13,7 @@
#include <vector> #include <vector>
#include <unordered_map> #include <unordered_map>
#include <unordered_set> #include <unordered_set>
#include <functional>
#ifdef USE_ESP32 #ifdef USE_ESP32
@@ -55,9 +56,13 @@ class BLEServer : public Component, public GATTsEventHandler, public BLEStatusEv
void ble_before_disabled_event_handler() override; void ble_before_disabled_event_handler() override;
// Direct callback registration // Direct callback registration - only allocates when callback is set
void on_connect(std::function<void(uint16_t)> &&callback) { this->on_connect_callback_ = std::move(callback); } void on_connect(std::function<void(uint16_t)> &&callback) {
void on_disconnect(std::function<void(uint16_t)> &&callback) { this->on_disconnect_callback_ = std::move(callback); } this->on_connect_callback_ = std::make_unique<std::function<void(uint16_t)>>(std::move(callback));
}
void on_disconnect(std::function<void(uint16_t)> &&callback) {
this->on_disconnect_callback_ = std::make_unique<std::function<void(uint16_t)>>(std::move(callback));
}
protected: protected:
struct ServiceEntry { struct ServiceEntry {
@@ -71,8 +76,8 @@ class BLEServer : public Component, public GATTsEventHandler, public BLEStatusEv
void add_client_(uint16_t conn_id) { this->clients_.insert(conn_id); } void add_client_(uint16_t conn_id) { this->clients_.insert(conn_id); }
void remove_client_(uint16_t conn_id) { this->clients_.erase(conn_id); } void remove_client_(uint16_t conn_id) { this->clients_.erase(conn_id); }
std::function<void(uint16_t)> on_connect_callback_{nullptr}; std::unique_ptr<std::function<void(uint16_t)>> on_connect_callback_;
std::function<void(uint16_t)> on_disconnect_callback_{nullptr}; std::unique_ptr<std::function<void(uint16_t)>> on_disconnect_callback_;
std::vector<uint8_t> manufacturer_data_{}; std::vector<uint8_t> manufacturer_data_{};
esp_gatt_if_t gatts_if_{0}; esp_gatt_if_t gatts_if_{0};