ESPHome 2025.12.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#ifdef ESPHOME_ESP32_BLE_GAP_EVENT_HANDLER_COUNT
129 void register_gap_event_handler(GAPEventHandler *handler) { this->gap_event_handlers_.push_back(handler); }
130#endif
131#ifdef ESPHOME_ESP32_BLE_GAP_SCAN_EVENT_HANDLER_COUNT
133 this->gap_scan_event_handlers_.push_back(handler);
134 }
135#endif
136#if defined(USE_ESP32_BLE_CLIENT) && defined(ESPHOME_ESP32_BLE_GATTC_EVENT_HANDLER_COUNT)
137 void register_gattc_event_handler(GATTcEventHandler *handler) { this->gattc_event_handlers_.push_back(handler); }
138#endif
139#if defined(USE_ESP32_BLE_SERVER) && defined(ESPHOME_ESP32_BLE_GATTS_EVENT_HANDLER_COUNT)
140 void register_gatts_event_handler(GATTsEventHandler *handler) { this->gatts_event_handlers_.push_back(handler); }
141#endif
142#ifdef ESPHOME_ESP32_BLE_BLE_STATUS_EVENT_HANDLER_COUNT
144 this->ble_status_event_handlers_.push_back(handler);
145 }
146#endif
147 void set_enable_on_boot(bool enable_on_boot) { this->enable_on_boot_ = enable_on_boot; }
148
149 protected:
150#ifdef USE_ESP32_BLE_SERVER
151 static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
152#endif
153#ifdef USE_ESP32_BLE_CLIENT
154 static void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param);
155#endif
156 static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param);
157
158 bool ble_setup_();
159 bool ble_dismantle_();
160 bool ble_pre_setup_();
161#ifdef USE_ESP32_BLE_ADVERTISING
162 void advertising_init_();
163#endif
164
165 // BLE uses the core wake_loop_threadsafe() mechanism to wake the main event loop
166 // from BLE tasks. This enables low-latency (~12μs) event processing instead of
167 // waiting for select() timeout (0-16ms). The wake socket is shared with other
168 // components that need this functionality.
169
170 private:
171 template<typename... Args> friend void enqueue_ble_event(Args... args);
172
173 // Handler vectors - use StaticVector when counts are known at compile time
174#ifdef ESPHOME_ESP32_BLE_GAP_EVENT_HANDLER_COUNT
176#endif
177#ifdef ESPHOME_ESP32_BLE_GAP_SCAN_EVENT_HANDLER_COUNT
179#endif
180#if defined(USE_ESP32_BLE_CLIENT) && defined(ESPHOME_ESP32_BLE_GATTC_EVENT_HANDLER_COUNT)
182#endif
183#if defined(USE_ESP32_BLE_SERVER) && defined(ESPHOME_ESP32_BLE_GATTS_EVENT_HANDLER_COUNT)
185#endif
186#ifdef ESPHOME_ESP32_BLE_BLE_STATUS_EVENT_HANDLER_COUNT
188#endif
189
190 // Large objects (size depends on template parameters, but typically aligned to 4 bytes)
193
194 // optional<string> (typically 16+ bytes on 32-bit, aligned to 4 bytes)
196
197 // 4-byte aligned members
198#ifdef USE_ESP32_BLE_ADVERTISING
199 BLEAdvertising *advertising_{}; // 4 bytes (pointer)
200#endif
201 esp_ble_io_cap_t io_cap_{ESP_IO_CAP_NONE}; // 4 bytes (enum)
202 uint32_t advertising_cycle_time_{}; // 4 bytes
203
204 // 2-byte aligned members
205 uint16_t appearance_{0}; // 2 bytes
206
207 // 1-byte aligned members (grouped together to minimize padding)
208 BLEComponentState state_{BLE_COMPONENT_STATE_OFF}; // 1 byte (uint8_t enum)
209 bool enable_on_boot_{}; // 1 byte
210};
211
212// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
213extern ESP32BLE *global_ble;
214
215template<typename... Ts> class BLEEnabledCondition : public Condition<Ts...> {
216 public:
217 bool check(const Ts &...x) override { return global_ble->is_active(); }
218};
219
220template<typename... Ts> class BLEEnableAction : public Action<Ts...> {
221 public:
222 void play(const Ts &...x) override { global_ble->enable(); }
223};
224
225template<typename... Ts> class BLEDisableAction : public Action<Ts...> {
226 public:
227 void play(const Ts &...x) override { global_ble->disable(); }
228};
229
230} // namespace esphome::esp32_ble
231
232#endif
uint8_t address
Definition bl0906.h:4
Base class for all automation conditions.
Definition automation.h:148
Minimal static vector - saves memory by avoiding std::vector overhead.
Definition helpers.h:115
void play(const Ts &...x) override
Definition ble.h:227
void play(const Ts &...x) override
Definition ble.h:222
bool check(const Ts &...x) override
Definition ble.h:217
void advertising_set_manufacturer_data(const std::vector< uint8_t > &data)
Definition ble.cpp:98
void register_gap_event_handler(GAPEventHandler *handler)
Definition ble.h:129
void set_enable_on_boot(bool enable_on_boot)
Definition ble.h:147
static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
Definition ble.cpp:553
void register_gattc_event_handler(GATTcEventHandler *handler)
Definition ble.h:137
void register_gap_scan_event_handler(GAPScanEventHandler *handler)
Definition ble.h:132
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:601
friend void enqueue_ble_event(Args... args)
Definition ble.cpp:527
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:103
void register_ble_status_event_handler(BLEStatusEventHandler *handler)
Definition ble.h:143
void advertising_add_service_uuid(ESPBTUUID uuid)
Definition ble.cpp:131
void dump_config() override
Definition ble.cpp:613
void loop() override
Definition ble.cpp:358
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:590
void register_gatts_event_handler(GATTsEventHandler *handler)
Definition ble.h:140
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:101
void advertising_set_appearance(uint16_t appearance)
Definition ble.h:121
float get_setup_priority() const override
Definition ble.cpp:611
void setup() override
Definition ble.cpp:54
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:137
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:660
@ 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:649
uint16_t x
Definition tt21100.cpp:5