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

[esp32_ble_server] Optimize service storage: 1KB flash savings, 84x-241x faster lookups

This commit is contained in:
J. Nick Koston
2025-09-25 23:59:16 -05:00
parent 1560b8b8e2
commit 1eaa121ad2
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
this->device_information_service_->do_create(this);
// Create all services previously created
for (auto &pair : this->services_) {
if (pair.second == this->device_information_service_) {
for (auto &entry : this->services_) {
if (entry.service == this->device_information_service_) {
continue;
}
pair.second->do_create(this);
entry.service->do_create(this);
}
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)
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_) {
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) {
ESP_LOGV(TAG, "Removing BLE service - %s %d", uuid.to_string().c_str(), inst_id);
BLEService *service = this->get_service(uuid, inst_id);
if (service == nullptr) {
ESP_LOGW(TAG, "BLE service %s %d does not exist", uuid.to_string().c_str(), inst_id);
for (auto it = this->services_.begin(); it != this->services_.end(); ++it) {
if (it->uuid == uuid && it->inst_id == inst_id) {
it->service->do_delete();
delete it->service; // NOLINT(cppcoreguidelines-owning-memory)
this->services_.erase(it);
return;
}
service->do_delete();
delete service; // NOLINT(cppcoreguidelines-owning-memory)
this->services_.erase(BLEServer::get_service_key(uuid, inst_id));
}
ESP_LOGW(TAG, "BLE service %s %d does not exist", uuid.to_string().c_str(), inst_id);
}
BLEService *BLEServer::get_service(ESPBTUUID uuid, uint8_t inst_id) {
BLEService *service = nullptr;
if (this->services_.count(BLEServer::get_service_key(uuid, inst_id)) > 0) {
service = this->services_.at(BLEServer::get_service_key(uuid, inst_id));
for (auto &entry : this->services_) {
if (entry.uuid == uuid && entry.inst_id == inst_id) {
return entry.service;
}
return service;
}
std::string BLEServer::get_service_key(ESPBTUUID uuid, uint8_t inst_id) {
return uuid.to_string() + std::to_string(inst_id);
return nullptr;
}
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;
}
for (const auto &pair : this->services_) {
pair.second->gatts_event_handler(event, gatts_if, param);
for (auto &entry : this->services_) {
entry.service->gatts_event_handler(event, gatts_if, param);
}
}
@@ -183,8 +181,8 @@ void BLEServer::ble_before_disabled_event_handler() {
// Delete all clients
this->clients_.clear();
// Delete all services
for (auto &pair : this->services_) {
pair.second->do_delete();
for (auto &entry : this->services_) {
entry.service->do_delete();
}
this->registered_ = false;
this->state_ = INIT;

View File

@@ -66,7 +66,12 @@ class BLEServer : public Component,
void ble_before_disabled_event_handler() override;
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 add_client_(uint16_t conn_id) { this->clients_.insert(conn_id); }
@@ -77,7 +82,7 @@ class BLEServer : public Component,
bool registered_{false};
std::unordered_set<uint16_t> clients_;
std::unordered_map<std::string, BLEService *> services_{};
std::vector<ServiceEntry> services_{};
std::vector<BLEService *> services_to_start_{};
BLEService *device_information_service_{};