ESPHome  2024.4.1
ble.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "ble_advertising.h"
4 #include "ble_uuid.h"
5 
8 #include "esphome/core/defines.h"
9 #include "esphome/core/helpers.h"
10 
11 #include "ble_event.h"
12 #include "queue.h"
13 
14 #ifdef USE_ESP32
15 
16 #include <esp_gap_ble_api.h>
17 #include <esp_gattc_api.h>
18 #include <esp_gatts_api.h>
19 
20 namespace esphome {
21 namespace esp32_ble {
22 
23 uint64_t ble_addr_to_uint64(const esp_bd_addr_t address);
24 
25 // NOLINTNEXTLINE(modernize-use-using)
26 typedef struct {
27  void *peer_device;
28  bool connected;
29  uint16_t mtu;
31 
33  IO_CAP_OUT = ESP_IO_CAP_OUT,
34  IO_CAP_IO = ESP_IO_CAP_IO,
35  IO_CAP_IN = ESP_IO_CAP_IN,
36  IO_CAP_NONE = ESP_IO_CAP_NONE,
37  IO_CAP_KBDISP = ESP_IO_CAP_KBDISP,
38 };
39 
51 };
52 
54  public:
55  virtual void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) = 0;
56 };
57 
59  public:
60  virtual void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
61  esp_ble_gattc_cb_param_t *param) = 0;
62 };
63 
65  public:
66  virtual void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if,
67  esp_ble_gatts_cb_param_t *param) = 0;
68 };
69 
71  public:
72  virtual void ble_before_disabled_event_handler() = 0;
73 };
74 
75 class ESP32BLE : public Component {
76  public:
77  void set_io_capability(IoCapability io_capability) { this->io_cap_ = (esp_ble_io_cap_t) io_capability; }
78 
79  void enable();
80  void disable();
81  bool is_active();
82  void setup() override;
83  void loop() override;
84  void dump_config() override;
85  float get_setup_priority() const override;
86 
87  void advertising_start();
88  void advertising_set_service_data(const std::vector<uint8_t> &data);
89  void advertising_set_manufacturer_data(const std::vector<uint8_t> &data);
90  void advertising_add_service_uuid(ESPBTUUID uuid);
91  void advertising_remove_service_uuid(ESPBTUUID uuid);
92 
93  void register_gap_event_handler(GAPEventHandler *handler) { this->gap_event_handlers_.push_back(handler); }
94  void register_gattc_event_handler(GATTcEventHandler *handler) { this->gattc_event_handlers_.push_back(handler); }
95  void register_gatts_event_handler(GATTsEventHandler *handler) { this->gatts_event_handlers_.push_back(handler); }
97  this->ble_status_event_handlers_.push_back(handler);
98  }
99  void set_enable_on_boot(bool enable_on_boot) { this->enable_on_boot_ = enable_on_boot; }
100 
101  protected:
102  static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
103  static void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param);
104  static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param);
105 
106  void real_gatts_event_handler_(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
107  void real_gattc_event_handler_(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param);
108  void real_gap_event_handler_(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param);
109 
110  bool ble_setup_();
111  bool ble_dismantle_();
112  bool ble_pre_setup_();
113  void advertising_init_();
114 
115  std::vector<GAPEventHandler *> gap_event_handlers_;
116  std::vector<GATTcEventHandler *> gattc_event_handlers_;
117  std::vector<GATTsEventHandler *> gatts_event_handlers_;
118  std::vector<BLEStatusEventHandler *> ble_status_event_handlers_;
120 
123  esp_ble_io_cap_t io_cap_{ESP_IO_CAP_NONE};
125 };
126 
127 // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
128 extern ESP32BLE *global_ble;
129 
130 template<typename... Ts> class BLEEnabledCondition : public Condition<Ts...> {
131  public:
132  bool check(Ts... x) override { return global_ble->is_active(); }
133 };
134 
135 template<typename... Ts> class BLEEnableAction : public Action<Ts...> {
136  public:
137  void play(Ts... x) override { global_ble->enable(); }
138 };
139 
140 template<typename... Ts> class BLEDisableAction : public Action<Ts...> {
141  public:
142  void play(Ts... x) override { global_ble->disable(); }
143 };
144 
145 } // namespace esp32_ble
146 } // namespace esphome
147 
148 #endif
void setup()
void play(Ts... x) override
Definition: ble.h:142
void loop()
uint64_t ble_addr_to_uint64(const esp_bd_addr_t address)
Definition: ble.cpp:383
ESP32BLE * global_ble
Definition: ble.cpp:394
uint16_t x
Definition: tt21100.cpp:17
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:115
std::vector< GATTcEventHandler * > gattc_event_handlers_
Definition: ble.h:116
Queue< BLEEvent > ble_events_
Definition: ble.h:121
std::vector< GATTsEventHandler * > gatts_event_handlers_
Definition: ble.h:117
void register_gatts_event_handler(GATTsEventHandler *handler)
Definition: ble.h:95
Base class for all automation conditions.
Definition: automation.h:74
Nothing has been initialized yet.
Definition: ble.h:42
void play(Ts... x) override
Definition: ble.h:137
void register_ble_status_event_handler(BLEStatusEventHandler *handler)
Definition: ble.h:96
std::vector< BLEStatusEventHandler * > ble_status_event_handlers_
Definition: ble.h:118
bool check(Ts... x) override
Definition: ble.h:132
void set_enable_on_boot(bool enable_on_boot)
Definition: ble.h:99
void register_gap_event_handler(GAPEventHandler *handler)
Definition: ble.h:93
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
BLE should be disabled on next loop.
Definition: ble.h:44
void set_io_capability(IoCapability io_capability)
Definition: ble.h:77
BLE should be enabled on next loop.
Definition: ble.h:48
void register_gattc_event_handler(GATTcEventHandler *handler)
Definition: ble.h:94
BLEAdvertising * advertising_
Definition: ble.h:122