mirror of
https://github.com/esphome/esphome.git
synced 2025-10-04 11:02:19 +01:00
Merge branch 'event_emitter_cleanup' into integration
This commit is contained in:
@@ -147,24 +147,28 @@ BLEService *BLEServer::get_service(ESPBTUUID uuid, uint8_t inst_id) {
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void BLEServer::dispatch_callbacks_(CallbackType type, uint16_t conn_id) {
|
||||||
|
for (auto &entry : this->callbacks_) {
|
||||||
|
if (entry.type == type) {
|
||||||
|
entry.callback(conn_id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void BLEServer::gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if,
|
void BLEServer::gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if,
|
||||||
esp_ble_gatts_cb_param_t *param) {
|
esp_ble_gatts_cb_param_t *param) {
|
||||||
switch (event) {
|
switch (event) {
|
||||||
case ESP_GATTS_CONNECT_EVT: {
|
case ESP_GATTS_CONNECT_EVT: {
|
||||||
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);
|
||||||
for (auto &callback : this->on_connect_callbacks_) {
|
this->dispatch_callbacks_(CallbackType::ON_CONNECT, param->connect.conn_id);
|
||||||
callback(param->connect.conn_id);
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case ESP_GATTS_DISCONNECT_EVT: {
|
case ESP_GATTS_DISCONNECT_EVT: {
|
||||||
ESP_LOGD(TAG, "BLE Client disconnected");
|
ESP_LOGD(TAG, "BLE Client disconnected");
|
||||||
this->remove_client_(param->disconnect.conn_id);
|
this->remove_client_(param->disconnect.conn_id);
|
||||||
this->parent_->advertising_start();
|
this->parent_->advertising_start();
|
||||||
for (auto &callback : this->on_disconnect_callbacks_) {
|
this->dispatch_callbacks_(CallbackType::ON_DISCONNECT, param->disconnect.conn_id);
|
||||||
callback(param->disconnect.conn_id);
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case ESP_GATTS_REG_EVT: {
|
case ESP_GATTS_REG_EVT: {
|
||||||
|
@@ -57,13 +57,23 @@ class BLEServer : public Component, public GATTsEventHandler, public BLEStatusEv
|
|||||||
|
|
||||||
// Direct callback registration - supports multiple callbacks
|
// Direct callback registration - supports multiple callbacks
|
||||||
void on_connect(std::function<void(uint16_t)> &&callback) {
|
void on_connect(std::function<void(uint16_t)> &&callback) {
|
||||||
this->on_connect_callbacks_.push_back(std::move(callback));
|
this->callbacks_.push_back({CallbackType::ON_CONNECT, std::move(callback)});
|
||||||
}
|
}
|
||||||
void on_disconnect(std::function<void(uint16_t)> &&callback) {
|
void on_disconnect(std::function<void(uint16_t)> &&callback) {
|
||||||
this->on_disconnect_callbacks_.push_back(std::move(callback));
|
this->callbacks_.push_back({CallbackType::ON_DISCONNECT, std::move(callback)});
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
enum class CallbackType : uint8_t {
|
||||||
|
ON_CONNECT,
|
||||||
|
ON_DISCONNECT,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct CallbackEntry {
|
||||||
|
CallbackType type;
|
||||||
|
std::function<void(uint16_t)> callback;
|
||||||
|
};
|
||||||
|
|
||||||
struct ServiceEntry {
|
struct ServiceEntry {
|
||||||
ESPBTUUID uuid;
|
ESPBTUUID uuid;
|
||||||
uint8_t inst_id;
|
uint8_t inst_id;
|
||||||
@@ -74,9 +84,9 @@ 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); }
|
||||||
|
void dispatch_callbacks_(CallbackType type, uint16_t conn_id);
|
||||||
|
|
||||||
std::vector<std::function<void(uint16_t)>> on_connect_callbacks_;
|
std::vector<CallbackEntry> callbacks_;
|
||||||
std::vector<std::function<void(uint16_t)>> on_disconnect_callbacks_;
|
|
||||||
|
|
||||||
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};
|
||||||
|
Reference in New Issue
Block a user