ESPHome 2026.3.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
55#ifdef ESPHOME_ESP32_BLE_EXTENDED_AUTH_PARAMS
57 AUTH_REQ_NO_BOND = ESP_LE_AUTH_NO_BOND,
58 AUTH_REQ_BOND = ESP_LE_AUTH_BOND,
59 AUTH_REQ_MITM = ESP_LE_AUTH_REQ_MITM,
60 AUTH_REQ_BOND_MITM = ESP_LE_AUTH_REQ_BOND_MITM,
61 AUTH_REQ_SC_ONLY = ESP_LE_AUTH_REQ_SC_ONLY,
62 AUTH_REQ_SC_BOND = ESP_LE_AUTH_REQ_SC_BOND,
63 AUTH_REQ_SC_MITM = ESP_LE_AUTH_REQ_SC_MITM,
64 AUTH_REQ_SC_MITM_BOND = ESP_LE_AUTH_REQ_SC_MITM_BOND,
65};
66#endif
67
80
82 public:
83 virtual void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) = 0;
84};
85
87 public:
88 virtual void gap_scan_event_handler(const BLEScanResult &scan_result) = 0;
89};
90
91#ifdef USE_ESP32_BLE_CLIENT
93 public:
94 virtual void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
95 esp_ble_gattc_cb_param_t *param) = 0;
96};
97#endif
98
99#ifdef USE_ESP32_BLE_SERVER
101 public:
102 virtual void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if,
103 esp_ble_gatts_cb_param_t *param) = 0;
104};
105#endif
106
108 public:
110};
111
112class ESP32BLE : public Component {
113 public:
114 void set_io_capability(IoCapability io_capability) { this->io_cap_ = (esp_ble_io_cap_t) io_capability; }
115
116#ifdef ESPHOME_ESP32_BLE_EXTENDED_AUTH_PARAMS
117 void set_max_key_size(uint8_t key_size) { this->max_key_size_ = key_size; }
118 void set_min_key_size(uint8_t key_size) { this->min_key_size_ = key_size; }
119 void set_auth_req(AuthReqMode req) { this->auth_req_mode_ = (esp_ble_auth_req_t) req; }
120#endif
121
122 void set_advertising_cycle_time(uint32_t advertising_cycle_time) {
123 this->advertising_cycle_time_ = advertising_cycle_time;
124 }
125 uint32_t get_advertising_cycle_time() const { return this->advertising_cycle_time_; }
126
127 void enable();
128 void disable();
129 bool is_active();
130 void setup() override;
131 void loop() override;
132 void dump_config() override;
133 float get_setup_priority() const override;
134 void set_name(const char *name) { this->name_ = name; }
135
136#ifdef USE_ESP32_BLE_ADVERTISING
137 void advertising_start();
138 void advertising_set_service_data(const std::vector<uint8_t> &data);
139 void advertising_set_manufacturer_data(const std::vector<uint8_t> &data);
140 void advertising_set_appearance(uint16_t appearance) { this->appearance_ = appearance; }
141 void advertising_set_service_data_and_name(std::span<const uint8_t> data, bool include_name);
144 void advertising_register_raw_advertisement_callback(std::function<void(bool)> &&callback);
145#endif
146
147#ifdef ESPHOME_ESP32_BLE_GAP_EVENT_HANDLER_COUNT
148 void register_gap_event_handler(GAPEventHandler *handler) { this->gap_event_handlers_.push_back(handler); }
149#endif
150#ifdef ESPHOME_ESP32_BLE_GAP_SCAN_EVENT_HANDLER_COUNT
152 this->gap_scan_event_handlers_.push_back(handler);
153 }
154#endif
155#if defined(USE_ESP32_BLE_CLIENT) && defined(ESPHOME_ESP32_BLE_GATTC_EVENT_HANDLER_COUNT)
156 void register_gattc_event_handler(GATTcEventHandler *handler) { this->gattc_event_handlers_.push_back(handler); }
157#endif
158#if defined(USE_ESP32_BLE_SERVER) && defined(ESPHOME_ESP32_BLE_GATTS_EVENT_HANDLER_COUNT)
159 void register_gatts_event_handler(GATTsEventHandler *handler) { this->gatts_event_handlers_.push_back(handler); }
160#endif
161#ifdef ESPHOME_ESP32_BLE_BLE_STATUS_EVENT_HANDLER_COUNT
163 this->ble_status_event_handlers_.push_back(handler);
164 }
165#endif
166 void set_enable_on_boot(bool enable_on_boot) { this->enable_on_boot_ = enable_on_boot; }
167
168 protected:
169#ifdef USE_ESP32_BLE_SERVER
170 static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
171#endif
172#ifdef USE_ESP32_BLE_CLIENT
173 static void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param);
174#endif
175 static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param);
176
177 // Handle DISABLE and ENABLE transitions when not in the ACTIVE state.
178 // Other non-ACTIVE states (e.g. OFF, DISABLED) are currently treated as no-ops.
179 void __attribute__((noinline)) loop_handle_state_transition_not_active_();
180
181 bool ble_setup_();
182 bool ble_dismantle_();
183 bool ble_pre_setup_();
184#ifdef USE_ESP32_BLE_ADVERTISING
185 void advertising_init_();
186#endif
187
188 // BLE uses the core wake_loop_threadsafe() mechanism to wake the main event loop
189 // from BLE tasks. This enables low-latency (~12μs) event processing instead of
190 // waiting for select() timeout (0-16ms). The wake socket is shared with other
191 // components that need this functionality.
192
193 private:
194 template<typename... Args> friend void enqueue_ble_event(Args... args);
195
196 // Handler vectors - use StaticVector when counts are known at compile time
197#ifdef ESPHOME_ESP32_BLE_GAP_EVENT_HANDLER_COUNT
199#endif
200#ifdef ESPHOME_ESP32_BLE_GAP_SCAN_EVENT_HANDLER_COUNT
202#endif
203#if defined(USE_ESP32_BLE_CLIENT) && defined(ESPHOME_ESP32_BLE_GATTC_EVENT_HANDLER_COUNT)
205#endif
206#if defined(USE_ESP32_BLE_SERVER) && defined(ESPHOME_ESP32_BLE_GATTS_EVENT_HANDLER_COUNT)
208#endif
209#ifdef ESPHOME_ESP32_BLE_BLE_STATUS_EVENT_HANDLER_COUNT
211#endif
212
213 // Large objects (size depends on template parameters, but typically aligned to 4 bytes)
216
217 // 4-byte aligned members
218#ifdef USE_ESP32_BLE_ADVERTISING
219 BLEAdvertising *advertising_{}; // 4 bytes (pointer)
220#endif
221 const char *name_{nullptr}; // 4 bytes (pointer to string literal in flash)
222 esp_ble_io_cap_t io_cap_{ESP_IO_CAP_NONE}; // 4 bytes (enum)
223 uint32_t advertising_cycle_time_{}; // 4 bytes
224
225 // 2-byte aligned members
226 uint16_t appearance_{0}; // 2 bytes
227
228 // 1-byte aligned members (grouped together to minimize padding)
229 BLEComponentState state_{BLE_COMPONENT_STATE_OFF}; // 1 byte (uint8_t enum)
230 bool enable_on_boot_{}; // 1 byte
231
232#ifdef ESPHOME_ESP32_BLE_EXTENDED_AUTH_PARAMS
233 optional<esp_ble_auth_req_t> auth_req_mode_;
234
235 uint8_t max_key_size_{0}; // range is 7..16, 0 is unset
236 uint8_t min_key_size_{0}; // range is 7..16, 0 is unset
237#endif
238};
239
240// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
241extern ESP32BLE *global_ble;
242
243template<typename... Ts> class BLEEnabledCondition : public Condition<Ts...> {
244 public:
245 bool check(const Ts &...x) override { return global_ble != nullptr && global_ble->is_active(); }
246};
247
248template<typename... Ts> class BLEEnableAction : public Action<Ts...> {
249 public:
250 void play(const Ts &...x) override {
251 if (global_ble != nullptr)
253 }
254};
255
256template<typename... Ts> class BLEDisableAction : public Action<Ts...> {
257 public:
258 void play(const Ts &...x) override {
259 if (global_ble != nullptr)
261 }
262};
263
264} // namespace esphome::esp32_ble
265
266#endif
uint8_t address
Definition bl0906.h:4
Base class for all automation conditions.
Definition automation.h:304
Minimal static vector - saves memory by avoiding std::vector overhead.
Definition helpers.h:209
void play(const Ts &...x) override
Definition ble.h:258
void play(const Ts &...x) override
Definition ble.h:250
bool check(const Ts &...x) override
Definition ble.h:245
void advertising_set_manufacturer_data(const std::vector< uint8_t > &data)
Definition ble.cpp:100
void register_gap_event_handler(GAPEventHandler *handler)
Definition ble.h:148
void set_enable_on_boot(bool enable_on_boot)
Definition ble.h:166
static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
Definition ble.cpp:591
void set_auth_req(AuthReqMode req)
Definition ble.h:119
void register_gattc_event_handler(GATTcEventHandler *handler)
Definition ble.h:156
void register_gap_scan_event_handler(GAPScanEventHandler *handler)
Definition ble.h:151
void set_name(const char *name)
Definition ble.h:134
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:639
friend void enqueue_ble_event(Args... args)
Definition ble.cpp:565
void __attribute__((noinline)) loop_handle_state_transition_not_active_()
void advertising_register_raw_advertisement_callback(std::function< void(bool)> &&callback)
Definition ble.cpp:128
void advertising_set_service_data_and_name(std::span< const uint8_t > data, bool include_name)
Definition ble.cpp:106
void set_advertising_cycle_time(uint32_t advertising_cycle_time)
Definition ble.h:122
void register_ble_status_event_handler(BLEStatusEventHandler *handler)
Definition ble.h:162
void advertising_add_service_uuid(ESPBTUUID uuid)
Definition ble.cpp:133
void dump_config() override
Definition ble.cpp:651
void loop() override
Definition ble.cpp:398
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:628
void register_gatts_event_handler(GATTsEventHandler *handler)
Definition ble.h:159
void set_max_key_size(uint8_t key_size)
Definition ble.h:117
void advertising_set_service_data(const std::vector< uint8_t > &data)
Definition ble.cpp:94
void set_io_capability(IoCapability io_capability)
Definition ble.h:114
void advertising_set_appearance(uint16_t appearance)
Definition ble.h:140
float get_setup_priority() const override
Definition ble.cpp:649
void setup() override
Definition ble.cpp:56
void set_min_key_size(uint8_t key_size)
Definition ble.h:118
uint32_t get_advertising_cycle_time() const
Definition ble.h:125
void advertising_remove_service_uuid(ESPBTUUID uuid)
Definition ble.cpp:139
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:741
@ BLE_COMPONENT_STATE_DISABLE
BLE should be disabled on next loop.
Definition ble.h:72
@ BLE_COMPONENT_STATE_OFF
Nothing has been initialized yet.
Definition ble.h:70
@ BLE_COMPONENT_STATE_ENABLE
BLE should be enabled on next loop.
Definition ble.h:76
@ BLE_COMPONENT_STATE_DISABLED
BLE is disabled.
Definition ble.h:74
@ BLE_COMPONENT_STATE_ACTIVE
BLE is active.
Definition ble.h:78
uint64_t ble_addr_to_uint64(const esp_bd_addr_t address)
Definition ble.cpp:730
@ AUTH_REQ_SC_MITM_BOND
Definition ble.h:64
@ AUTH_REQ_BOND_MITM
Definition ble.h:60
uint16_t x
Definition tt21100.cpp:5