ESPHome  2023.5.5
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 // NOLINTNEXTLINE(modernize-use-using)
22 typedef struct {
23  void *peer_device;
24  bool connected;
25  uint16_t mtu;
27 
29  IO_CAP_OUT = ESP_IO_CAP_OUT,
30  IO_CAP_IO = ESP_IO_CAP_IO,
31  IO_CAP_IN = ESP_IO_CAP_IN,
32  IO_CAP_NONE = ESP_IO_CAP_NONE,
33  IO_CAP_KBDISP = ESP_IO_CAP_KBDISP,
34 };
35 
37  public:
38  virtual void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) = 0;
39 };
40 
42  public:
43  virtual void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
44  esp_ble_gattc_cb_param_t *param) = 0;
45 };
46 
48  public:
49  virtual void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if,
50  esp_ble_gatts_cb_param_t *param) = 0;
51 };
52 
53 class ESP32BLE : public Component {
54  public:
55  void set_io_capability(IoCapability io_capability) { this->io_cap_ = (esp_ble_io_cap_t) io_capability; }
56 
57  void setup() override;
58  void loop() override;
59  void dump_config() override;
60  float get_setup_priority() const override;
61 
62  BLEAdvertising *get_advertising() { return this->advertising_; }
63 
64  void register_gap_event_handler(GAPEventHandler *handler) { this->gap_event_handlers_.push_back(handler); }
65  void register_gattc_event_handler(GATTcEventHandler *handler) { this->gattc_event_handlers_.push_back(handler); }
66  void register_gatts_event_handler(GATTsEventHandler *handler) { this->gatts_event_handlers_.push_back(handler); }
67 
68  protected:
69  static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
70  static void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param);
71  static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param);
72 
73  void real_gatts_event_handler_(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
74  void real_gattc_event_handler_(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param);
75  void real_gap_event_handler_(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param);
76 
77  bool ble_setup_();
78 
79  std::vector<GAPEventHandler *> gap_event_handlers_;
80  std::vector<GATTcEventHandler *> gattc_event_handlers_;
81  std::vector<GATTsEventHandler *> gatts_event_handlers_;
82 
85  esp_ble_io_cap_t io_cap_{ESP_IO_CAP_NONE};
86 };
87 
88 // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
89 extern ESP32BLE *global_ble;
90 
91 } // namespace esp32_ble
92 } // namespace esphome
93 
94 #endif
void setup()
void loop()
ESP32BLE * global_ble
Definition: ble.cpp:247
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:79
std::vector< GATTcEventHandler * > gattc_event_handlers_
Definition: ble.h:80
Queue< BLEEvent > ble_events_
Definition: ble.h:83
std::vector< GATTsEventHandler * > gatts_event_handlers_
Definition: ble.h:81
void register_gatts_event_handler(GATTsEventHandler *handler)
Definition: ble.h:66
BLEAdvertising * get_advertising()
Definition: ble.h:62
void register_gap_event_handler(GAPEventHandler *handler)
Definition: ble.h:64
Definition: a4988.cpp:4
void set_io_capability(IoCapability io_capability)
Definition: ble.h:55
void register_gattc_event_handler(GATTcEventHandler *handler)
Definition: ble.h:65
BLEAdvertising * advertising_
Definition: ble.h:84