ESPHome  2024.12.2
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  void set_name(const std::string &name) { this->name_ = name; }
94 
95  void advertising_start();
96  void advertising_set_service_data(const std::vector<uint8_t> &data);
97  void advertising_set_manufacturer_data(const std::vector<uint8_t> &data);
98  void advertising_add_service_uuid(ESPBTUUID uuid);
99  void advertising_remove_service_uuid(ESPBTUUID uuid);
100  void advertising_register_raw_advertisement_callback(std::function<void(bool)> &&callback);
101 
102  void register_gap_event_handler(GAPEventHandler *handler) { this->gap_event_handlers_.push_back(handler); }
103  void register_gattc_event_handler(GATTcEventHandler *handler) { this->gattc_event_handlers_.push_back(handler); }
104  void register_gatts_event_handler(GATTsEventHandler *handler) { this->gatts_event_handlers_.push_back(handler); }
106  this->ble_status_event_handlers_.push_back(handler);
107  }
108  void set_enable_on_boot(bool enable_on_boot) { this->enable_on_boot_ = enable_on_boot; }
109 
110  protected:
111  static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
112  static void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param);
113  static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param);
114 
115  void real_gatts_event_handler_(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
116  void real_gattc_event_handler_(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param);
117  void real_gap_event_handler_(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param);
118 
119  bool ble_setup_();
120  bool ble_dismantle_();
121  bool ble_pre_setup_();
122  void advertising_init_();
123 
124  std::vector<GAPEventHandler *> gap_event_handlers_;
125  std::vector<GATTcEventHandler *> gattc_event_handlers_;
126  std::vector<GATTsEventHandler *> gatts_event_handlers_;
127  std::vector<BLEStatusEventHandler *> ble_status_event_handlers_;
129 
132  esp_ble_io_cap_t io_cap_{ESP_IO_CAP_NONE};
136 };
137 
138 // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
139 extern ESP32BLE *global_ble;
140 
141 template<typename... Ts> class BLEEnabledCondition : public Condition<Ts...> {
142  public:
143  bool check(Ts... x) override { return global_ble->is_active(); }
144 };
145 
146 template<typename... Ts> class BLEEnableAction : public Action<Ts...> {
147  public:
148  void play(Ts... x) override { global_ble->enable(); }
149 };
150 
151 template<typename... Ts> class BLEDisableAction : public Action<Ts...> {
152  public:
153  void play(Ts... x) override { global_ble->disable(); }
154 };
155 
156 } // namespace esp32_ble
157 } // namespace esphome
158 
159 #endif
void setup()
void play(Ts... x) override
Definition: ble.h:153
const char * name
Definition: stm32flash.h:78
void loop()
uint64_t ble_addr_to_uint64(const esp_bd_addr_t address)
Definition: ble.cpp:427
ESP32BLE * global_ble
Definition: ble.cpp:438
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:124
std::vector< GATTcEventHandler * > gattc_event_handlers_
Definition: ble.h:125
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:130
uint32_t advertising_cycle_time_
Definition: ble.h:133
std::vector< GATTsEventHandler * > gatts_event_handlers_
Definition: ble.h:126
void register_gatts_event_handler(GATTsEventHandler *handler)
Definition: ble.h:104
optional< std::string > name_
Definition: ble.h:135
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:148
void register_ble_status_event_handler(BLEStatusEventHandler *handler)
Definition: ble.h:105
std::vector< BLEStatusEventHandler * > ble_status_event_handlers_
Definition: ble.h:127
bool check(Ts... x) override
Definition: ble.h:143
void set_enable_on_boot(bool enable_on_boot)
Definition: ble.h:108
void register_gap_event_handler(GAPEventHandler *handler)
Definition: ble.h:102
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 set_name(const std::string &name)
Definition: ble.h:93
void register_gattc_event_handler(GATTcEventHandler *handler)
Definition: ble.h:103
BLEAdvertising * advertising_
Definition: ble.h:131