1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-28 16:12:24 +01:00

Merge branch 'integration' into memory_api

This commit is contained in:
J. Nick Koston
2025-09-26 00:13:24 -05:00
2 changed files with 28 additions and 25 deletions

View File

@@ -70,11 +70,11 @@ void BLEServer::loop() {
// it is at the top of the GATT table // it is at the top of the GATT table
this->device_information_service_->do_create(this); this->device_information_service_->do_create(this);
// Create all services previously created // Create all services previously created
for (auto &pair : this->services_) { for (auto &entry : this->services_) {
if (pair.second == this->device_information_service_) { if (entry.service == this->device_information_service_) {
continue; continue;
} }
pair.second->do_create(this); entry.service->do_create(this);
} }
this->state_ = STARTING_SERVICE; this->state_ = STARTING_SERVICE;
} }
@@ -118,7 +118,7 @@ BLEService *BLEServer::create_service(ESPBTUUID uuid, bool advertise, uint16_t n
} }
BLEService *service = // NOLINT(cppcoreguidelines-owning-memory) BLEService *service = // NOLINT(cppcoreguidelines-owning-memory)
new BLEService(uuid, num_handles, inst_id, advertise); new BLEService(uuid, num_handles, inst_id, advertise);
this->services_.emplace(BLEServer::get_service_key(uuid, inst_id), service); this->services_.push_back({uuid, inst_id, service});
if (this->parent_->is_active() && this->registered_) { if (this->parent_->is_active() && this->registered_) {
service->do_create(this); service->do_create(this);
} }
@@ -127,26 +127,24 @@ BLEService *BLEServer::create_service(ESPBTUUID uuid, bool advertise, uint16_t n
void BLEServer::remove_service(ESPBTUUID uuid, uint8_t inst_id) { void BLEServer::remove_service(ESPBTUUID uuid, uint8_t inst_id) {
ESP_LOGV(TAG, "Removing BLE service - %s %d", uuid.to_string().c_str(), inst_id); ESP_LOGV(TAG, "Removing BLE service - %s %d", uuid.to_string().c_str(), inst_id);
BLEService *service = this->get_service(uuid, inst_id); for (auto it = this->services_.begin(); it != this->services_.end(); ++it) {
if (service == nullptr) { if (it->uuid == uuid && it->inst_id == inst_id) {
ESP_LOGW(TAG, "BLE service %s %d does not exist", uuid.to_string().c_str(), inst_id); it->service->do_delete();
delete it->service; // NOLINT(cppcoreguidelines-owning-memory)
this->services_.erase(it);
return; return;
} }
service->do_delete(); }
delete service; // NOLINT(cppcoreguidelines-owning-memory) ESP_LOGW(TAG, "BLE service %s %d does not exist", uuid.to_string().c_str(), inst_id);
this->services_.erase(BLEServer::get_service_key(uuid, inst_id));
} }
BLEService *BLEServer::get_service(ESPBTUUID uuid, uint8_t inst_id) { BLEService *BLEServer::get_service(ESPBTUUID uuid, uint8_t inst_id) {
BLEService *service = nullptr; for (auto &entry : this->services_) {
if (this->services_.count(BLEServer::get_service_key(uuid, inst_id)) > 0) { if (entry.uuid == uuid && entry.inst_id == inst_id) {
service = this->services_.at(BLEServer::get_service_key(uuid, inst_id)); return entry.service;
} }
return service; }
} return nullptr;
std::string BLEServer::get_service_key(ESPBTUUID uuid, uint8_t inst_id) {
return uuid.to_string() + std::to_string(inst_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,
@@ -174,8 +172,8 @@ void BLEServer::gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t ga
break; break;
} }
for (const auto &pair : this->services_) { for (auto &entry : this->services_) {
pair.second->gatts_event_handler(event, gatts_if, param); entry.service->gatts_event_handler(event, gatts_if, param);
} }
} }
@@ -183,8 +181,8 @@ void BLEServer::ble_before_disabled_event_handler() {
// Delete all clients // Delete all clients
this->clients_.clear(); this->clients_.clear();
// Delete all services // Delete all services
for (auto &pair : this->services_) { for (auto &entry : this->services_) {
pair.second->do_delete(); entry.service->do_delete();
} }
this->registered_ = false; this->registered_ = false;
this->state_ = INIT; this->state_ = INIT;

View File

@@ -66,7 +66,12 @@ class BLEServer : public Component,
void ble_before_disabled_event_handler() override; void ble_before_disabled_event_handler() override;
protected: protected:
static std::string get_service_key(ESPBTUUID uuid, uint8_t inst_id); struct ServiceEntry {
ESPBTUUID uuid;
uint8_t inst_id;
BLEService *service;
};
void restart_advertising_(); void restart_advertising_();
void add_client_(uint16_t conn_id) { this->clients_.insert(conn_id); } void add_client_(uint16_t conn_id) { this->clients_.insert(conn_id); }
@@ -77,7 +82,7 @@ class BLEServer : public Component,
bool registered_{false}; bool registered_{false};
std::unordered_set<uint16_t> clients_; std::unordered_set<uint16_t> clients_;
std::unordered_map<std::string, BLEService *> services_{}; std::vector<ServiceEntry> services_{};
std::vector<BLEService *> services_to_start_{}; std::vector<BLEService *> services_to_start_{};
BLEService *device_information_service_{}; BLEService *device_information_service_{};