mirror of
https://github.com/esphome/esphome.git
synced 2024-11-28 17:54:13 +01:00
Remove BLEServiceComponent redundancy
This commit is contained in:
parent
0913772e26
commit
4282918581
7 changed files with 45 additions and 22 deletions
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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<ESP32BLE> {
|
||||
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<std::string, BLEService *> services_;
|
||||
BLEService *device_information_service_;
|
||||
|
||||
std::vector<BLEServiceComponent *> service_components_;
|
||||
|
||||
enum State : uint8_t {
|
||||
INIT = 0x00,
|
||||
REGISTERING,
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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<void(const uint16_t)> &&func) { this->on_client_connect_ = func; }
|
||||
void on_client_disconnect(const std::function<void(const uint16_t)> &&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<BLECharacteristic *> characteristics_;
|
||||
|
@ -63,6 +68,8 @@ class BLEService {
|
|||
uint8_t inst_id_;
|
||||
bool advertise_{false};
|
||||
bool should_start_{false};
|
||||
std::function<void(const uint16_t)> on_client_connect_;
|
||||
std::function<void(const uint16_t)> on_client_disconnect_;
|
||||
|
||||
bool do_create_characteristics_();
|
||||
void stop_();
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue