ESPHome  2023.8.3
ble.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "ble_advertising.h"
4 
6 #include "esphome/core/defines.h"
7 #include "esphome/core/helpers.h"
8 
9 #include "queue.h"
10 #include "ble_event.h"
11 
12 #ifdef USE_ESP32
13 
14 #include <esp_gap_ble_api.h>
15 #include <esp_gatts_api.h>
16 #include <esp_gattc_api.h>
17 
18 namespace esphome {
19 namespace esp32_ble {
20 
21 uint64_t ble_addr_to_uint64(const esp_bd_addr_t address);
22 
23 // NOLINTNEXTLINE(modernize-use-using)
24 typedef struct {
25  void *peer_device;
26  bool connected;
27  uint16_t mtu;
29 
31  IO_CAP_OUT = ESP_IO_CAP_OUT,
32  IO_CAP_IO = ESP_IO_CAP_IO,
33  IO_CAP_IN = ESP_IO_CAP_IN,
34  IO_CAP_NONE = ESP_IO_CAP_NONE,
35  IO_CAP_KBDISP = ESP_IO_CAP_KBDISP,
36 };
37 
39  public:
40  virtual void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) = 0;
41 };
42 
44  public:
45  virtual void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
46  esp_ble_gattc_cb_param_t *param) = 0;
47 };
48 
50  public:
51  virtual void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if,
52  esp_ble_gatts_cb_param_t *param) = 0;
53 };
54 
55 class ESP32BLE : public Component {
56  public:
57  void set_io_capability(IoCapability io_capability) { this->io_cap_ = (esp_ble_io_cap_t) io_capability; }
58 
59  void setup() override;
60  void loop() override;
61  void dump_config() override;
62  float get_setup_priority() const override;
63 
64  BLEAdvertising *get_advertising() { return this->advertising_; }
65 
66  void register_gap_event_handler(GAPEventHandler *handler) { this->gap_event_handlers_.push_back(handler); }
67  void register_gattc_event_handler(GATTcEventHandler *handler) { this->gattc_event_handlers_.push_back(handler); }
68  void register_gatts_event_handler(GATTsEventHandler *handler) { this->gatts_event_handlers_.push_back(handler); }
69 
70  protected:
71  static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
72  static void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param);
73  static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param);
74 
75  void real_gatts_event_handler_(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
76  void real_gattc_event_handler_(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param);
77  void real_gap_event_handler_(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param);
78 
79  bool ble_setup_();
80 
81  std::vector<GAPEventHandler *> gap_event_handlers_;
82  std::vector<GATTcEventHandler *> gattc_event_handlers_;
83  std::vector<GATTsEventHandler *> gatts_event_handlers_;
84 
87  esp_ble_io_cap_t io_cap_{ESP_IO_CAP_NONE};
88 };
89 
90 // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
91 extern ESP32BLE *global_ble;
92 
93 } // namespace esp32_ble
94 } // namespace esphome
95 
96 #endif
void setup()
void loop()
uint64_t ble_addr_to_uint64(const esp_bd_addr_t address)
Definition: ble.cpp:247
ESP32BLE * global_ble
Definition: ble.cpp:258
virtual void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)=0
std::vector< GAPEventHandler * > gap_event_handlers_
Definition: ble.h:81
std::vector< GATTcEventHandler * > gattc_event_handlers_
Definition: ble.h:82
Queue< BLEEvent > ble_events_
Definition: ble.h:85
std::vector< GATTsEventHandler * > gatts_event_handlers_
Definition: ble.h:83
void register_gatts_event_handler(GATTsEventHandler *handler)
Definition: ble.h:68
BLEAdvertising * get_advertising()
Definition: ble.h:64
void register_gap_event_handler(GAPEventHandler *handler)
Definition: ble.h:66
void set_io_capability(IoCapability io_capability)
Definition: ble.h:57
void register_gattc_event_handler(GATTcEventHandler *handler)
Definition: ble.h:67
BLEAdvertising * advertising_
Definition: ble.h:86