ESPHome 2025.10.0-dev
Loading...
Searching...
No Matches
ble.h
Go to the documentation of this file.
1#pragma once
2
3#include "esphome/core/defines.h" // Must be included before conditional includes
4
5#include "ble_uuid.h"
6#include "ble_scan_result.h"
7#ifdef USE_ESP32_BLE_ADVERTISING
8#include "ble_advertising.h"
9#endif
10
11#include <functional>
12#include <span>
13
17
18#include "ble_event.h"
21
22#ifdef USE_ESP32
23
24#include <esp_gap_ble_api.h>
25#include <esp_gattc_api.h>
26#include <esp_gatts_api.h>
27
28namespace esphome::esp32_ble {
29
30// Maximum size of the BLE event queue
31// Increased to absorb the ring buffer capacity from esp32_ble_tracker
32#ifdef USE_PSRAM
33static constexpr uint8_t MAX_BLE_QUEUE_SIZE = 100; // 64 + 36 (ring buffer size with PSRAM)
34#else
35static constexpr uint8_t MAX_BLE_QUEUE_SIZE = 88; // 64 + 24 (ring buffer size without PSRAM)
36#endif
37
38uint64_t ble_addr_to_uint64(const esp_bd_addr_t address);
39
40// NOLINTNEXTLINE(modernize-use-using)
41typedef struct {
44 uint16_t mtu;
46
48 IO_CAP_OUT = ESP_IO_CAP_OUT,
49 IO_CAP_IO = ESP_IO_CAP_IO,
50 IO_CAP_IN = ESP_IO_CAP_IN,
51 IO_CAP_NONE = ESP_IO_CAP_NONE,
52 IO_CAP_KBDISP = ESP_IO_CAP_KBDISP,
53};
54
67
69 public:
70 virtual void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) = 0;
71};
72
74 public:
75 virtual void gap_scan_event_handler(const BLEScanResult &scan_result) = 0;
76};
77
78#ifdef USE_ESP32_BLE_CLIENT
80 public:
81 virtual void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
82 esp_ble_gattc_cb_param_t *param) = 0;
83};
84#endif
85
86#ifdef USE_ESP32_BLE_SERVER
88 public:
89 virtual void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if,
90 esp_ble_gatts_cb_param_t *param) = 0;
91};
92#endif
93
95 public:
97};
98
99class ESP32BLE : public Component {
100 public:
101 void set_io_capability(IoCapability io_capability) { this->io_cap_ = (esp_ble_io_cap_t) io_capability; }
102
103 void set_advertising_cycle_time(uint32_t advertising_cycle_time) {
104 this->advertising_cycle_time_ = advertising_cycle_time;
105 }
106 uint32_t get_advertising_cycle_time() const { return this->advertising_cycle_time_; }
107
108 void enable();
109 void disable();
110 bool is_active();
111 void setup() override;
112 void loop() override;
113 void dump_config() override;
114 float get_setup_priority() const override;
115 void set_name(const std::string &name) { this->name_ = name; }
116
117#ifdef USE_ESP32_BLE_ADVERTISING
118 void advertising_start();
119 void advertising_set_service_data(const std::vector<uint8_t> &data);
120 void advertising_set_manufacturer_data(const std::vector<uint8_t> &data);
121 void advertising_set_appearance(uint16_t appearance) { this->appearance_ = appearance; }
122 void advertising_set_service_data_and_name(std::span<const uint8_t> data, bool include_name);
125 void advertising_register_raw_advertisement_callback(std::function<void(bool)> &&callback);
126#endif
127
128 void register_gap_event_handler(GAPEventHandler *handler) { this->gap_event_handlers_.push_back(handler); }
130 this->gap_scan_event_handlers_.push_back(handler);
131 }
132#ifdef USE_ESP32_BLE_CLIENT
133 void register_gattc_event_handler(GATTcEventHandler *handler) { this->gattc_event_handlers_.push_back(handler); }
134#endif
135#ifdef USE_ESP32_BLE_SERVER
136 void register_gatts_event_handler(GATTsEventHandler *handler) { this->gatts_event_handlers_.push_back(handler); }
137#endif
139 this->ble_status_event_handlers_.push_back(handler);
140 }
141 void set_enable_on_boot(bool enable_on_boot) { this->enable_on_boot_ = enable_on_boot; }
142
143 protected:
144#ifdef USE_ESP32_BLE_SERVER
145 static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
146#endif
147#ifdef USE_ESP32_BLE_CLIENT
148 static void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param);
149#endif
150 static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param);
151
152 bool ble_setup_();
153 bool ble_dismantle_();
154 bool ble_pre_setup_();
155#ifdef USE_ESP32_BLE_ADVERTISING
156 void advertising_init_();
157#endif
158
159 private:
160 template<typename... Args> friend void enqueue_ble_event(Args... args);
161
162 // Vectors (12 bytes each on 32-bit, naturally aligned to 4 bytes)
163 std::vector<GAPEventHandler *> gap_event_handlers_;
164 std::vector<GAPScanEventHandler *> gap_scan_event_handlers_;
165#ifdef USE_ESP32_BLE_CLIENT
166 std::vector<GATTcEventHandler *> gattc_event_handlers_;
167#endif
168#ifdef USE_ESP32_BLE_SERVER
169 std::vector<GATTsEventHandler *> gatts_event_handlers_;
170#endif
171 std::vector<BLEStatusEventHandler *> ble_status_event_handlers_;
172
173 // Large objects (size depends on template parameters, but typically aligned to 4 bytes)
176
177 // optional<string> (typically 16+ bytes on 32-bit, aligned to 4 bytes)
179
180 // 4-byte aligned members
181#ifdef USE_ESP32_BLE_ADVERTISING
182 BLEAdvertising *advertising_{}; // 4 bytes (pointer)
183#endif
184 esp_ble_io_cap_t io_cap_{ESP_IO_CAP_NONE}; // 4 bytes (enum)
185 uint32_t advertising_cycle_time_{}; // 4 bytes
186
187 // 2-byte aligned members
188 uint16_t appearance_{0}; // 2 bytes
189
190 // 1-byte aligned members (grouped together to minimize padding)
191 BLEComponentState state_{BLE_COMPONENT_STATE_OFF}; // 1 byte (uint8_t enum)
192 bool enable_on_boot_{}; // 1 byte
193};
194
195// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
196extern ESP32BLE *global_ble;
197
198template<typename... Ts> class BLEEnabledCondition : public Condition<Ts...> {
199 public:
200 bool check(Ts... x) override { return global_ble->is_active(); }
201};
202
203template<typename... Ts> class BLEEnableAction : public Action<Ts...> {
204 public:
205 void play(Ts... x) override { global_ble->enable(); }
206};
207
208template<typename... Ts> class BLEDisableAction : public Action<Ts...> {
209 public:
210 void play(Ts... x) override { global_ble->disable(); }
211};
212
213} // namespace esphome::esp32_ble
214
215#endif
uint8_t address
Definition bl0906.h:4
Base class for all automation conditions.
Definition automation.h:124
void play(Ts... x) override
Definition ble.h:210
void play(Ts... x) override
Definition ble.h:205
bool check(Ts... x) override
Definition ble.h:200
void advertising_set_manufacturer_data(const std::vector< uint8_t > &data)
Definition ble.cpp:70
void register_gap_event_handler(GAPEventHandler *handler)
Definition ble.h:128
void set_enable_on_boot(bool enable_on_boot)
Definition ble.h:141
static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
Definition ble.cpp:487
void register_gattc_event_handler(GATTcEventHandler *handler)
Definition ble.h:133
void register_gap_scan_event_handler(GAPScanEventHandler *handler)
Definition ble.h:129
static void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param)
Definition ble.cpp:533
friend void enqueue_ble_event(Args... args)
Definition ble.cpp:461
void advertising_register_raw_advertisement_callback(std::function< void(bool)> &&callback)
Definition ble.cpp:98
void advertising_set_service_data_and_name(std::span< const uint8_t > data, bool include_name)
Definition ble.cpp:76
void set_advertising_cycle_time(uint32_t advertising_cycle_time)
Definition ble.h:103
void register_ble_status_event_handler(BLEStatusEventHandler *handler)
Definition ble.h:138
void advertising_add_service_uuid(ESPBTUUID uuid)
Definition ble.cpp:103
void dump_config() override
Definition ble.cpp:541
void loop() override
Definition ble.cpp:292
static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param)
Definition ble.cpp:526
void register_gatts_event_handler(GATTsEventHandler *handler)
Definition ble.h:136
void advertising_set_service_data(const std::vector< uint8_t > &data)
Definition ble.cpp:64
void set_io_capability(IoCapability io_capability)
Definition ble.h:101
void advertising_set_appearance(uint16_t appearance)
Definition ble.h:121
float get_setup_priority() const override
Definition ble.cpp:539
void setup() override
Definition ble.cpp:26
void set_name(const std::string &name)
Definition ble.h:115
uint32_t get_advertising_cycle_time() const
Definition ble.h:106
void advertising_remove_service_uuid(ESPBTUUID uuid)
Definition ble.cpp:109
virtual void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)=0
virtual void gap_scan_event_handler(const BLEScanResult &scan_result)=0
virtual void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param)=0
virtual void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param)=0
ESP32BLE * global_ble
Definition ble.cpp:586
@ BLE_COMPONENT_STATE_DISABLE
BLE should be disabled on next loop.
Definition ble.h:59
@ BLE_COMPONENT_STATE_OFF
Nothing has been initialized yet.
Definition ble.h:57
@ BLE_COMPONENT_STATE_ENABLE
BLE should be enabled on next loop.
Definition ble.h:63
@ BLE_COMPONENT_STATE_DISABLED
BLE is disabled.
Definition ble.h:61
@ BLE_COMPONENT_STATE_ACTIVE
BLE is active.
Definition ble.h:65
uint64_t ble_addr_to_uint64(const esp_bd_addr_t address)
Definition ble.cpp:575
uint16_t x
Definition tt21100.cpp:5