ESPHome 2026.5.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
38inline uint64_t ble_addr_to_uint64(const esp_bd_addr_t address) {
39 uint64_t u = 0;
40 u |= uint64_t(address[0] & 0xFF) << 40;
41 u |= uint64_t(address[1] & 0xFF) << 32;
42 u |= uint64_t(address[2] & 0xFF) << 24;
43 u |= uint64_t(address[3] & 0xFF) << 16;
44 u |= uint64_t(address[4] & 0xFF) << 8;
45 u |= uint64_t(address[5] & 0xFF) << 0;
46 return u;
47}
48
49// NOLINTNEXTLINE(modernize-use-using)
50typedef struct {
53 uint16_t mtu;
55
57 IO_CAP_OUT = ESP_IO_CAP_OUT,
58 IO_CAP_IO = ESP_IO_CAP_IO,
59 IO_CAP_IN = ESP_IO_CAP_IN,
60 IO_CAP_NONE = ESP_IO_CAP_NONE,
61 IO_CAP_KBDISP = ESP_IO_CAP_KBDISP,
62};
63
64#ifdef ESPHOME_ESP32_BLE_EXTENDED_AUTH_PARAMS
66 AUTH_REQ_NO_BOND = ESP_LE_AUTH_NO_BOND,
67 AUTH_REQ_BOND = ESP_LE_AUTH_BOND,
68 AUTH_REQ_MITM = ESP_LE_AUTH_REQ_MITM,
69 AUTH_REQ_BOND_MITM = ESP_LE_AUTH_REQ_BOND_MITM,
70 AUTH_REQ_SC_ONLY = ESP_LE_AUTH_REQ_SC_ONLY,
71 AUTH_REQ_SC_BOND = ESP_LE_AUTH_REQ_SC_BOND,
72 AUTH_REQ_SC_MITM = ESP_LE_AUTH_REQ_SC_MITM,
73 AUTH_REQ_SC_MITM_BOND = ESP_LE_AUTH_REQ_SC_MITM_BOND,
74};
75#endif
76
89
90class ESP32BLE : public Component {
91 public:
92 void set_io_capability(IoCapability io_capability) { this->io_cap_ = (esp_ble_io_cap_t) io_capability; }
93
94#ifdef ESPHOME_ESP32_BLE_EXTENDED_AUTH_PARAMS
95 void set_max_key_size(uint8_t key_size) { this->max_key_size_ = key_size; }
96 void set_min_key_size(uint8_t key_size) { this->min_key_size_ = key_size; }
97 void set_auth_req(AuthReqMode req) { this->auth_req_mode_ = (esp_ble_auth_req_t) req; }
98#endif
99
100 void set_advertising_cycle_time(uint32_t advertising_cycle_time) {
101 this->advertising_cycle_time_ = advertising_cycle_time;
102 }
103 uint32_t get_advertising_cycle_time() const { return this->advertising_cycle_time_; }
104
105 void enable();
106 void disable();
107 ESPHOME_ALWAYS_INLINE bool is_active() { return this->state_ == BLE_COMPONENT_STATE_ACTIVE; }
108 void setup() override;
109 void loop() override;
110 void dump_config() override;
111 float get_setup_priority() const override;
112 void set_name(const char *name) { this->name_ = name; }
113
114#ifdef USE_ESP32_BLE_ADVERTISING
115 void advertising_start();
116 void advertising_set_service_data(const std::vector<uint8_t> &data);
117 void advertising_set_manufacturer_data(const std::vector<uint8_t> &data);
118 void advertising_set_appearance(uint16_t appearance) { this->appearance_ = appearance; }
119 void advertising_set_service_data_and_name(std::span<const uint8_t> data, bool include_name);
122 void advertising_register_raw_advertisement_callback(std::function<void(bool)> &&callback);
123#endif
124
125#ifdef ESPHOME_ESP32_BLE_GAP_EVENT_HANDLER_COUNT
126 template<typename F> void add_gap_event_callback(F &&callback) {
127 this->gap_event_callbacks_.add(std::forward<F>(callback));
128 }
129#endif
130#ifdef ESPHOME_ESP32_BLE_GAP_SCAN_EVENT_HANDLER_COUNT
131 template<typename F> void add_gap_scan_event_callback(F &&callback) {
132 this->gap_scan_event_callbacks_.add(std::forward<F>(callback));
133 }
134#endif
135#if defined(USE_ESP32_BLE_CLIENT) && defined(ESPHOME_ESP32_BLE_GATTC_EVENT_HANDLER_COUNT)
136 template<typename F> void add_gattc_event_callback(F &&callback) {
137 this->gattc_event_callbacks_.add(std::forward<F>(callback));
138 }
139#endif
140#if defined(USE_ESP32_BLE_SERVER) && defined(ESPHOME_ESP32_BLE_GATTS_EVENT_HANDLER_COUNT)
141 template<typename F> void add_gatts_event_callback(F &&callback) {
142 this->gatts_event_callbacks_.add(std::forward<F>(callback));
143 }
144#endif
145#ifdef ESPHOME_ESP32_BLE_BLE_STATUS_EVENT_HANDLER_COUNT
146 template<typename F> void add_ble_status_event_callback(F &&callback) {
147 this->ble_status_event_callbacks_.add(std::forward<F>(callback));
148 }
149#endif
150 void set_enable_on_boot(bool enable_on_boot) { this->enable_on_boot_ = enable_on_boot; }
151
152 protected:
153#ifdef USE_ESP32_BLE_SERVER
154 static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
155#endif
156#ifdef USE_ESP32_BLE_CLIENT
157 static void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param);
158#endif
159 static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param);
160
161 // Handle DISABLE and ENABLE transitions when not in the ACTIVE state.
162 // Other non-ACTIVE states (e.g. OFF, DISABLED) are currently treated as no-ops.
163 void __attribute__((noinline)) loop_handle_state_transition_not_active_();
164
165 bool ble_setup_();
166 bool ble_dismantle_();
167 bool ble_pre_setup_();
168#ifdef USE_ESP32_BLE_ADVERTISING
169 void advertising_init_();
170#endif
171
172 // BLE uses the core wake_loop_threadsafe() mechanism to wake the main event loop
173 // from BLE tasks. This enables low-latency (~12μs) event processing instead of
174 // waiting for select() timeout (0-16ms). The wake socket is shared with other
175 // components that need this functionality.
176
177 private:
178 template<typename... Args> friend void enqueue_ble_event(Args... args);
179
180#ifdef ESPHOME_ESP32_BLE_GAP_EVENT_HANDLER_COUNT
181 StaticCallbackManager<ESPHOME_ESP32_BLE_GAP_EVENT_HANDLER_COUNT,
182 void(esp_gap_ble_cb_event_t, esp_ble_gap_cb_param_t *)>
183 gap_event_callbacks_;
184#endif
185#ifdef ESPHOME_ESP32_BLE_GAP_SCAN_EVENT_HANDLER_COUNT
186 StaticCallbackManager<ESPHOME_ESP32_BLE_GAP_SCAN_EVENT_HANDLER_COUNT, void(const BLEScanResult &)>
187 gap_scan_event_callbacks_;
188#endif
189#if defined(USE_ESP32_BLE_CLIENT) && defined(ESPHOME_ESP32_BLE_GATTC_EVENT_HANDLER_COUNT)
190 StaticCallbackManager<ESPHOME_ESP32_BLE_GATTC_EVENT_HANDLER_COUNT,
191 void(esp_gattc_cb_event_t, esp_gatt_if_t, esp_ble_gattc_cb_param_t *)>
192 gattc_event_callbacks_;
193#endif
194#if defined(USE_ESP32_BLE_SERVER) && defined(ESPHOME_ESP32_BLE_GATTS_EVENT_HANDLER_COUNT)
195 StaticCallbackManager<ESPHOME_ESP32_BLE_GATTS_EVENT_HANDLER_COUNT,
196 void(esp_gatts_cb_event_t, esp_gatt_if_t, esp_ble_gatts_cb_param_t *)>
197 gatts_event_callbacks_;
198#endif
199#ifdef ESPHOME_ESP32_BLE_BLE_STATUS_EVENT_HANDLER_COUNT
200 StaticCallbackManager<ESPHOME_ESP32_BLE_BLE_STATUS_EVENT_HANDLER_COUNT, void()> ble_status_event_callbacks_;
201#endif
202
203 // Large objects (size depends on template parameters, but typically aligned to 4 bytes)
205 // Pool sized to queue capacity (SIZE-1) because LockFreeQueue<T,N> is a ring
206 // buffer that holds N-1 elements (one slot distinguishes full from empty).
207 // This guarantees allocate() returns nullptr before push() can fail, which:
208 // 1. Prevents leaking a pool slot (the Nth allocate succeeds but push fails)
209 // 2. Avoids needing release() on the producer path after a failed push(),
210 // preserving the SPSC contract on the pool's internal free list
211 esphome::EventPool<BLEEvent, MAX_BLE_QUEUE_SIZE - 1> ble_event_pool_;
212
213 // 4-byte aligned members
214#ifdef USE_ESP32_BLE_ADVERTISING
215 BLEAdvertising *advertising_{}; // 4 bytes (pointer)
216#endif
217 const char *name_{nullptr}; // 4 bytes (pointer to string literal in flash)
218 esp_ble_io_cap_t io_cap_{ESP_IO_CAP_NONE}; // 4 bytes (enum)
219 uint32_t advertising_cycle_time_{}; // 4 bytes
220
221 // 2-byte aligned members
222 uint16_t appearance_{0}; // 2 bytes
223
224 // 1-byte aligned members (grouped together to minimize padding)
225 BLEComponentState state_{BLE_COMPONENT_STATE_OFF}; // 1 byte (uint8_t enum)
226 bool enable_on_boot_{}; // 1 byte
227
228#ifdef ESPHOME_ESP32_BLE_EXTENDED_AUTH_PARAMS
229 optional<esp_ble_auth_req_t> auth_req_mode_;
230
231 uint8_t max_key_size_{0}; // range is 7..16, 0 is unset
232 uint8_t min_key_size_{0}; // range is 7..16, 0 is unset
233#endif
234};
235
236// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
237extern ESP32BLE *global_ble;
238
239template<typename... Ts> class BLEEnabledCondition : public Condition<Ts...> {
240 public:
241 bool check(const Ts &...x) override { return global_ble != nullptr && global_ble->is_active(); }
242};
243
244template<typename... Ts> class BLEEnableAction : public Action<Ts...> {
245 public:
246 void play(const Ts &...x) override {
247 if (global_ble != nullptr)
249 }
250};
251
252template<typename... Ts> class BLEDisableAction : public Action<Ts...> {
253 public:
254 void play(const Ts &...x) override {
255 if (global_ble != nullptr)
257 }
258};
259
260} // namespace esphome::esp32_ble
261
262#endif
uint8_t address
Definition bl0906.h:4
Base class for all automation conditions.
Definition automation.h:459
CallbackManager backed by StaticVector for compile-time-known callback counts.
Definition helpers.h:1900
void play(const Ts &...x) override
Definition ble.h:254
void play(const Ts &...x) override
Definition ble.h:246
bool check(const Ts &...x) override
Definition ble.h:241
void add_gatts_event_callback(F &&callback)
Definition ble.h:141
void advertising_set_manufacturer_data(const std::vector< uint8_t > &data)
Definition ble.cpp:98
void add_gap_scan_event_callback(F &&callback)
Definition ble.h:131
void set_enable_on_boot(bool enable_on_boot)
Definition ble.h:150
static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
Definition ble.cpp:584
void set_auth_req(AuthReqMode req)
Definition ble.h:97
void add_gap_event_callback(F &&callback)
Definition ble.h:126
void set_name(const char *name)
Definition ble.h: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)
Definition ble.cpp:628
friend void enqueue_ble_event(Args... args)
Definition ble.cpp:557
void __attribute__((noinline)) loop_handle_state_transition_not_active_()
void advertising_register_raw_advertisement_callback(std::function< void(bool)> &&callback)
Definition ble.cpp:126
void advertising_set_service_data_and_name(std::span< const uint8_t > data, bool include_name)
Definition ble.cpp:104
void set_advertising_cycle_time(uint32_t advertising_cycle_time)
Definition ble.h:100
void advertising_add_service_uuid(ESPBTUUID uuid)
Definition ble.cpp:131
void dump_config() override
Definition ble.cpp:638
void loop() override
Definition ble.cpp:396
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:619
void add_ble_status_event_callback(F &&callback)
Definition ble.h:146
void set_max_key_size(uint8_t key_size)
Definition ble.h:95
void advertising_set_service_data(const std::vector< uint8_t > &data)
Definition ble.cpp:92
void set_io_capability(IoCapability io_capability)
Definition ble.h:92
void advertising_set_appearance(uint16_t appearance)
Definition ble.h:118
ESPHOME_ALWAYS_INLINE bool is_active()
Definition ble.h:107
float get_setup_priority() const override
Definition ble.cpp:636
void add_gattc_event_callback(F &&callback)
Definition ble.h:136
void setup() override
Definition ble.cpp:56
void set_min_key_size(uint8_t key_size)
Definition ble.h:96
uint32_t get_advertising_cycle_time() const
Definition ble.h:103
void advertising_remove_service_uuid(ESPBTUUID uuid)
Definition ble.cpp:137
ESP32BLE * global_ble
Definition ble.cpp:717
@ BLE_COMPONENT_STATE_DISABLE
BLE should be disabled on next loop.
Definition ble.h:81
@ BLE_COMPONENT_STATE_OFF
Nothing has been initialized yet.
Definition ble.h:79
@ BLE_COMPONENT_STATE_ENABLE
BLE should be enabled on next loop.
Definition ble.h:85
@ BLE_COMPONENT_STATE_DISABLED
BLE is disabled.
Definition ble.h:83
@ BLE_COMPONENT_STATE_ACTIVE
BLE is active.
Definition ble.h:87
uint64_t ble_addr_to_uint64(const esp_bd_addr_t address)
Definition ble.h:38
@ AUTH_REQ_SC_MITM_BOND
Definition ble.h:73
@ AUTH_REQ_BOND_MITM
Definition ble.h:69
const char int const __FlashStringHelper va_list args
Definition log.h:74
static void uint32_t
uint16_t x
Definition tt21100.cpp:5