ESPHome  2024.3.1
ble_server.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "ble_service.h"
4 #include "ble_characteristic.h"
5 
10 #include "esphome/core/component.h"
11 #include "esphome/core/helpers.h"
13 
14 #include <memory>
15 #include <vector>
16 #include <unordered_map>
17 
18 #ifdef USE_ESP32
19 
20 #include <esp_gap_ble_api.h>
21 #include <esp_gatts_api.h>
22 
23 namespace esphome {
24 namespace esp32_ble_server {
25 
26 using namespace esp32_ble;
27 
29  public:
30  virtual void on_client_connect(){};
31  virtual void on_client_disconnect(){};
32  virtual void start();
33  virtual void stop();
34 };
35 
36 class BLEServer : public Component, public GATTsEventHandler, public BLEStatusEventHandler, public Parented<ESP32BLE> {
37  public:
38  void setup() override;
39  void loop() override;
40  void dump_config() override;
41  float get_setup_priority() const override;
42  bool can_proceed() override;
43 
44  void teardown();
45  bool is_running();
46 
47  void set_manufacturer(const std::string &manufacturer) { this->manufacturer_ = manufacturer; }
48  void set_model(const std::string &model) { this->model_ = model; }
49  void set_manufacturer_data(const std::vector<uint8_t> &data) {
50  this->manufacturer_data_ = data;
51  this->restart_advertising_();
52  }
53 
54  void create_service(ESPBTUUID uuid, bool advertise = false, uint16_t num_handles = 15, uint8_t inst_id = 0);
55  void remove_service(ESPBTUUID uuid);
56  BLEService *get_service(ESPBTUUID uuid);
57 
58  esp_gatt_if_t get_gatts_if() { return this->gatts_if_; }
59  uint32_t get_connected_client_count() { return this->connected_clients_; }
60  const std::unordered_map<uint16_t, void *> &get_clients() { return this->clients_; }
61 
62  void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if,
63  esp_ble_gatts_cb_param_t *param) override;
64 
65  void ble_before_disabled_event_handler() override;
66 
67  void register_service_component(BLEServiceComponent *component) { this->service_components_.push_back(component); }
68 
69  protected:
70  bool create_device_characteristics_();
71  void restart_advertising_();
72 
73  void add_client_(uint16_t conn_id, void *client) { this->clients_.emplace(conn_id, client); }
74  bool remove_client_(uint16_t conn_id) { return this->clients_.erase(conn_id) > 0; }
75 
76  std::string manufacturer_;
78  std::vector<uint8_t> manufacturer_data_;
79  esp_gatt_if_t gatts_if_{0};
80  bool registered_{false};
81 
82  uint32_t connected_clients_{0};
83  std::unordered_map<uint16_t, void *> clients_;
84  std::unordered_map<std::string, BLEService *> services_;
86 
87  std::vector<BLEServiceComponent *> service_components_;
88 
89  enum State : uint8_t {
90  INIT = 0x00,
94  } state_{INIT};
95 };
96 
97 // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
99 
100 } // namespace esp32_ble_server
101 } // namespace esphome
102 
103 #endif
void setup()
void loop()
bool remove_client_(uint16_t conn_id)
Definition: ble_server.h:74
const std::unordered_map< uint16_t, void * > & get_clients()
Definition: ble_server.h:60
std::unordered_map< uint16_t, void * > clients_
Definition: ble_server.h:83
void set_manufacturer_data(const std::vector< uint8_t > &data)
Definition: ble_server.h:49
optional< std::string > model_
Definition: ble_server.h:77
void add_client_(uint16_t conn_id, void *client)
Definition: ble_server.h:73
std::unordered_map< std::string, BLEService * > services_
Definition: ble_server.h:84
std::vector< BLEServiceComponent * > service_components_
Definition: ble_server.h:87
void register_service_component(BLEServiceComponent *component)
Definition: ble_server.h:67
void set_model(const std::string &model)
Definition: ble_server.h:48
void set_manufacturer(const std::string &manufacturer)
Definition: ble_server.h:47
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
Helper class to easily give an object a parent of type T.
Definition: helpers.h:515
std::vector< uint8_t > manufacturer_data_
Definition: ble_server.h:78