diff --git a/esphome/components/esp32_ble_server/ble_server.cpp b/esphome/components/esp32_ble_server/ble_server.cpp index fc8c9659ff..e5e0ed0fbe 100644 --- a/esphome/components/esp32_ble_server/ble_server.cpp +++ b/esphome/components/esp32_ble_server/ble_server.cpp @@ -165,8 +165,8 @@ void BLEServer::gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t ga ESP_LOGD(TAG, "BLE Client connected"); this->add_client_(param->connect.conn_id, (void *) this); this->connected_clients_++; - for (auto *component : this->service_components_) { - component->on_client_connect(); + for (auto &pair : this->services_) { + pair.second->emit_client_connect(param->connect.conn_id); } break; } @@ -175,8 +175,8 @@ void BLEServer::gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t ga if (this->remove_client_(param->disconnect.conn_id)) this->connected_clients_--; this->parent_->advertising_start(); - for (auto *component : this->service_components_) { - component->on_client_disconnect(); + for (auto &pair : this->services_) { + pair.second->emit_client_disconnect(param->disconnect.conn_id); } break; } diff --git a/esphome/components/esp32_ble_server/ble_server.h b/esphome/components/esp32_ble_server/ble_server.h index b4273f6acb..98be1720b1 100644 --- a/esphome/components/esp32_ble_server/ble_server.h +++ b/esphome/components/esp32_ble_server/ble_server.h @@ -25,14 +25,6 @@ namespace esp32_ble_server { using namespace esp32_ble; -class BLEServiceComponent { - public: - virtual void on_client_connect(){}; - virtual void on_client_disconnect(){}; - virtual void start(); - virtual void stop(); -}; - class BLEServer : public Component, public GATTsEventHandler, public BLEStatusEventHandler, public Parented { public: void setup() override; @@ -64,8 +56,6 @@ class BLEServer : public Component, public GATTsEventHandler, public BLEStatusEv void ble_before_disabled_event_handler() override; - void register_service_component(BLEServiceComponent *component) { this->service_components_.push_back(component); } - protected: static std::string get_service_key(ESPBTUUID uuid, uint8_t inst_id); bool create_device_characteristics_(); @@ -85,8 +75,6 @@ class BLEServer : public Component, public GATTsEventHandler, public BLEStatusEv std::unordered_map services_; BLEService *device_information_service_; - std::vector service_components_; - enum State : uint8_t { INIT = 0x00, REGISTERING, diff --git a/esphome/components/esp32_ble_server/ble_service.cpp b/esphome/components/esp32_ble_server/ble_service.cpp index 368f03fb52..627b17b2ee 100644 --- a/esphome/components/esp32_ble_server/ble_service.cpp +++ b/esphome/components/esp32_ble_server/ble_service.cpp @@ -86,6 +86,13 @@ bool BLEService::do_create_characteristics_() { return true; } +void BLEService::enqueue_start() { + if (this->init_state_ == CREATED) + this->start(); + else + this->should_start_ = true; +} + void BLEService::start() { if (this->do_create_characteristics_()) return; diff --git a/esphome/components/esp32_ble_server/ble_service.h b/esphome/components/esp32_ble_server/ble_service.h index 74aacc2889..f5cff6059e 100644 --- a/esphome/components/esp32_ble_server/ble_service.h +++ b/esphome/components/esp32_ble_server/ble_service.h @@ -42,6 +42,7 @@ class BLEService { void do_delete(); void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param); + void enqueue_start(); void start(); void stop(); @@ -51,6 +52,10 @@ class BLEService { bool is_running() { return this->running_state_ == RUNNING; } bool is_starting() { return this->running_state_ == STARTING; } bool is_deleted() { return this->init_state_ == DELETED; } + void on_client_connect(const std::function &&func) { this->on_client_connect_ = func; } + void on_client_disconnect(const std::function &&func) { this->on_client_disconnect_ = func; } + void emit_client_connect(const uint16_t conn_id) { this->on_client_connect_(conn_id); } + void emit_client_disconnect(const uint16_t conn_id) { this->on_client_disconnect_(conn_id); } protected: std::vector characteristics_; @@ -63,6 +68,8 @@ class BLEService { uint8_t inst_id_; bool advertise_{false}; bool should_start_{false}; + std::function on_client_connect_; + std::function on_client_disconnect_; bool do_create_characteristics_(); void stop_(); diff --git a/esphome/components/esp32_improv/esp32_improv_component.cpp b/esphome/components/esp32_improv/esp32_improv_component.cpp index e0f60329cf..b265a08e4b 100644 --- a/esphome/components/esp32_improv/esp32_improv_component.cpp +++ b/esphome/components/esp32_improv/esp32_improv_component.cpp @@ -76,6 +76,7 @@ void ESP32ImprovComponent::loop() { // Setup the service ESP_LOGD(TAG, "Creating Improv service"); this->service_ = global_ble_server->create_service(ESPBTUUID::from_raw(improv::SERVICE_UUID), true); + this->service_->on_client_disconnect([this]() { this->set_error_(improv::ERROR_NONE); }); this->setup_characteristics(); } @@ -330,8 +331,6 @@ void ESP32ImprovComponent::on_wifi_connect_timeout_() { wifi::global_wifi_component->clear_sta(); } -void ESP32ImprovComponent::on_client_disconnect() { this->set_error_(improv::ERROR_NONE); }; - ESP32ImprovComponent *global_improv_component = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) } // namespace esp32_improv diff --git a/esphome/components/esp32_improv/esp32_improv_component.h b/esphome/components/esp32_improv/esp32_improv_component.h index 3ed377a6ad..97ae16b6b5 100644 --- a/esphome/components/esp32_improv/esp32_improv_component.h +++ b/esphome/components/esp32_improv/esp32_improv_component.h @@ -28,18 +28,17 @@ namespace esp32_improv { using namespace esp32_ble_server; -class ESP32ImprovComponent : public Component, public BLEServiceComponent { +class ESP32ImprovComponent : public Component { public: ESP32ImprovComponent(); void dump_config() override; void loop() override; void setup() override; void setup_characteristics(); - void on_client_disconnect() override; float get_setup_priority() const override; - void start() override; - void stop() override; + void start(); + void stop(); bool is_active() const { return this->state_ != improv::STATE_STOPPED; } #ifdef USE_BINARY_SENSOR diff --git a/tests/test5.yaml b/tests/test5.yaml index f7a34d5a1b..fc1adc8ebe 100644 --- a/tests/test5.yaml +++ b/tests/test5.yaml @@ -294,6 +294,29 @@ esp32_ble: esp32_ble_server: manufacturer: ESPHome model: Test5 + services: + - uuid: 2a24b789-7aab-4535-af3e-ee76a35cc42d + advertise: true + characteristics: + - uuid: cad48e28-7fbe-41cf-bae9-d77a6c233423 + name: "Test Characteristic" + properties: + - READ + - WRITE + - NOTIFY + permissions: + - READ + - WRITE + value: "Hello World" + on_write: + then: + - logger.log: "Characteristic written" + on_read: + then: + - logger.log: "Characteristic read" + on_notify: + then: + - logger.log: "Characteristic notified" esp32_improv: authorizer: io0_button