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