ESPHome  2022.9.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/helpers.h"
12 
13 #include <map>
14 #include <memory>
15 
16 #ifdef USE_ESP32
17 
18 #include <esp_gap_ble_api.h>
19 #include <esp_gatts_api.h>
20 
21 namespace esphome {
22 namespace esp32_ble_server {
23 
24 using namespace esp32_ble;
25 
27  public:
28  virtual void on_client_connect(){};
29  virtual void on_client_disconnect(){};
30  virtual void start();
31  virtual void stop();
32 };
33 
34 class BLEServer : public Component {
35  public:
36  void setup() override;
37  void loop() override;
38  void dump_config() override;
39  float get_setup_priority() const override;
40  bool can_proceed() override { return this->can_proceed_; }
41 
42  void teardown();
43 
44  void set_manufacturer(const std::string &manufacturer) { this->manufacturer_ = manufacturer; }
45  void set_model(const std::string &model) { this->model_ = model; }
46 
47  std::shared_ptr<BLEService> create_service(const uint8_t *uuid, bool advertise = false);
48  std::shared_ptr<BLEService> create_service(uint16_t uuid, bool advertise = false);
49  std::shared_ptr<BLEService> create_service(const std::string &uuid, bool advertise = false);
50  std::shared_ptr<BLEService> create_service(ESPBTUUID uuid, bool advertise = false, uint16_t num_handles = 15,
51  uint8_t inst_id = 0);
52 
53  esp_gatt_if_t get_gatts_if() { return this->gatts_if_; }
54  uint32_t get_connected_client_count() { return this->connected_clients_; }
55  const std::map<uint16_t, void *> &get_clients() { return this->clients_; }
56 
57  void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
58 
59  void register_service_component(BLEServiceComponent *component) { this->service_components_.push_back(component); }
60 
61  protected:
62  bool create_device_characteristics_();
63 
64  void add_client_(uint16_t conn_id, void *client) {
65  this->clients_.insert(std::pair<uint16_t, void *>(conn_id, client));
66  }
67  bool remove_client_(uint16_t conn_id) { return this->clients_.erase(conn_id) > 0; }
68 
69  bool can_proceed_{false};
70 
71  std::string manufacturer_;
73  esp_gatt_if_t gatts_if_{0};
74  bool registered_{false};
75 
76  uint32_t connected_clients_{0};
77  std::map<uint16_t, void *> clients_;
78 
79  std::vector<std::shared_ptr<BLEService>> services_;
80  std::shared_ptr<BLEService> device_information_service_;
81 
82  std::vector<BLEServiceComponent *> service_components_;
83 
84  enum State : uint8_t {
85  INIT = 0x00,
89  } state_{INIT};
90 };
91 
92 // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
94 
95 } // namespace esp32_ble_server
96 } // namespace esphome
97 
98 #endif
const std::map< uint16_t, void * > & get_clients()
Definition: ble_server.h:55
void setup()
void loop()
bool remove_client_(uint16_t conn_id)
Definition: ble_server.h:67
std::shared_ptr< BLEService > device_information_service_
Definition: ble_server.h:80
optional< std::string > model_
Definition: ble_server.h:72
void add_client_(uint16_t conn_id, void *client)
Definition: ble_server.h:64
std::vector< BLEServiceComponent * > service_components_
Definition: ble_server.h:82
std::vector< std::shared_ptr< BLEService > > services_
Definition: ble_server.h:79
std::map< uint16_t, void * > clients_
Definition: ble_server.h:77
void register_service_component(BLEServiceComponent *component)
Definition: ble_server.h:59
void set_model(const std::string &model)
Definition: ble_server.h:45
void set_manufacturer(const std::string &manufacturer)
Definition: ble_server.h:44
Definition: a4988.cpp:4