ESPHome 2025.8.0b1
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
16
17#include "ble_event.h"
20
21#ifdef USE_ESP32
22
23#include <esp_gap_ble_api.h>
24#include <esp_gattc_api.h>
25#include <esp_gatts_api.h>
26
27namespace esphome::esp32_ble {
28
29// Maximum size of the BLE event queue
30// Increased to absorb the ring buffer capacity from esp32_ble_tracker
31#ifdef USE_PSRAM
32static constexpr uint8_t MAX_BLE_QUEUE_SIZE = 100; // 64 + 36 (ring buffer size with PSRAM)
33#else
34static constexpr uint8_t MAX_BLE_QUEUE_SIZE = 88; // 64 + 24 (ring buffer size without PSRAM)
35#endif
36
37uint64_t ble_addr_to_uint64(const esp_bd_addr_t address);
38
39// NOLINTNEXTLINE(modernize-use-using)
40typedef struct {
43 uint16_t mtu;
45
47 IO_CAP_OUT = ESP_IO_CAP_OUT,
48 IO_CAP_IO = ESP_IO_CAP_IO,
49 IO_CAP_IN = ESP_IO_CAP_IN,
50 IO_CAP_NONE = ESP_IO_CAP_NONE,
51 IO_CAP_KBDISP = ESP_IO_CAP_KBDISP,
52};
53
66
68 public:
69 virtual void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) = 0;
70};
71
73 public:
74 virtual void gap_scan_event_handler(const BLEScanResult &scan_result) = 0;
75};
76
78 public:
79 virtual void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
80 esp_ble_gattc_cb_param_t *param) = 0;
81};
82
84 public:
85 virtual void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if,
86 esp_ble_gatts_cb_param_t *param) = 0;
87};
88
90 public:
92};
93
94class ESP32BLE : public Component {
95 public:
96 void set_io_capability(IoCapability io_capability) { this->io_cap_ = (esp_ble_io_cap_t) io_capability; }
97
98 void set_advertising_cycle_time(uint32_t advertising_cycle_time) {
99 this->advertising_cycle_time_ = advertising_cycle_time;
100 }
101 uint32_t get_advertising_cycle_time() const { return this->advertising_cycle_time_; }
102
103 void enable();
104 void disable();
105 bool is_active();
106 void setup() override;
107 void loop() override;
108 void dump_config() override;
109 float get_setup_priority() const override;
110 void set_name(const std::string &name) { this->name_ = name; }
111
112#ifdef USE_ESP32_BLE_ADVERTISING
113 void advertising_start();
114 void advertising_set_service_data(const std::vector<uint8_t> &data);
115 void advertising_set_manufacturer_data(const std::vector<uint8_t> &data);
116 void advertising_set_appearance(uint16_t appearance) { this->appearance_ = appearance; }
119 void advertising_register_raw_advertisement_callback(std::function<void(bool)> &&callback);
120#endif
121
122 void register_gap_event_handler(GAPEventHandler *handler) { this->gap_event_handlers_.push_back(handler); }
124 this->gap_scan_event_handlers_.push_back(handler);
125 }
126 void register_gattc_event_handler(GATTcEventHandler *handler) { this->gattc_event_handlers_.push_back(handler); }
127 void register_gatts_event_handler(GATTsEventHandler *handler) { this->gatts_event_handlers_.push_back(handler); }
129 this->ble_status_event_handlers_.push_back(handler);
130 }
131 void set_enable_on_boot(bool enable_on_boot) { this->enable_on_boot_ = enable_on_boot; }
132
133 protected:
134 static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
135 static void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param);
136 static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param);
137
138 bool ble_setup_();
139 bool ble_dismantle_();
140 bool ble_pre_setup_();
141#ifdef USE_ESP32_BLE_ADVERTISING
142 void advertising_init_();
143#endif
144
145 private:
146 template<typename... Args> friend void enqueue_ble_event(Args... args);
147
148 // Vectors (12 bytes each on 32-bit, naturally aligned to 4 bytes)
149 std::vector<GAPEventHandler *> gap_event_handlers_;
150 std::vector<GAPScanEventHandler *> gap_scan_event_handlers_;
151 std::vector<GATTcEventHandler *> gattc_event_handlers_;
152 std::vector<GATTsEventHandler *> gatts_event_handlers_;
153 std::vector<BLEStatusEventHandler *> ble_status_event_handlers_;
154
155 // Large objects (size depends on template parameters, but typically aligned to 4 bytes)
158
159 // optional<string> (typically 16+ bytes on 32-bit, aligned to 4 bytes)
161
162 // 4-byte aligned members
163#ifdef USE_ESP32_BLE_ADVERTISING
164 BLEAdvertising *advertising_{}; // 4 bytes (pointer)
165#endif
166 esp_ble_io_cap_t io_cap_{ESP_IO_CAP_NONE}; // 4 bytes (enum)
167 uint32_t advertising_cycle_time_{}; // 4 bytes
168
169 // 2-byte aligned members
170 uint16_t appearance_{0}; // 2 bytes
171
172 // 1-byte aligned members (grouped together to minimize padding)
173 BLEComponentState state_{BLE_COMPONENT_STATE_OFF}; // 1 byte (uint8_t enum)
174 bool enable_on_boot_{}; // 1 byte
175};
176
177// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
178extern ESP32BLE *global_ble;
179
180template<typename... Ts> class BLEEnabledCondition : public Condition<Ts...> {
181 public:
182 bool check(Ts... x) override { return global_ble->is_active(); }
183};
184
185template<typename... Ts> class BLEEnableAction : public Action<Ts...> {
186 public:
187 void play(Ts... x) override { global_ble->enable(); }
188};
189
190template<typename... Ts> class BLEDisableAction : public Action<Ts...> {
191 public:
192 void play(Ts... x) override { global_ble->disable(); }
193};
194
195} // namespace esphome::esp32_ble
196
197#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:192
void play(Ts... x) override
Definition ble.h:187
bool check(Ts... x) override
Definition ble.h:182
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:122
void set_enable_on_boot(bool enable_on_boot)
Definition ble.h:131
static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
Definition ble.cpp:449
void register_gattc_event_handler(GATTcEventHandler *handler)
Definition ble.h:126
void register_gap_scan_event_handler(GAPScanEventHandler *handler)
Definition ble.h:123
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:492
friend void enqueue_ble_event(Args... args)
Definition ble.cpp:427
void advertising_register_raw_advertisement_callback(std::function< void(bool)> &&callback)
Definition ble.cpp:76
void set_advertising_cycle_time(uint32_t advertising_cycle_time)
Definition ble.h:98
void register_ble_status_event_handler(BLEStatusEventHandler *handler)
Definition ble.h:128
void advertising_add_service_uuid(ESPBTUUID uuid)
Definition ble.cpp:81
void dump_config() override
Definition ble.cpp:499
void loop() override
Definition ble.cpp:266
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:487
void register_gatts_event_handler(GATTsEventHandler *handler)
Definition ble.h:127
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:96
void advertising_set_appearance(uint16_t appearance)
Definition ble.h:116
float get_setup_priority() const override
Definition ble.cpp:497
void setup() override
Definition ble.cpp:26
void set_name(const std::string &name)
Definition ble.h:110
uint32_t get_advertising_cycle_time() const
Definition ble.h:101
void advertising_remove_service_uuid(ESPBTUUID uuid)
Definition ble.cpp:87
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:544
@ BLE_COMPONENT_STATE_DISABLE
BLE should be disabled on next loop.
Definition ble.h:58
@ BLE_COMPONENT_STATE_OFF
Nothing has been initialized yet.
Definition ble.h:56
@ BLE_COMPONENT_STATE_ENABLE
BLE should be enabled on next loop.
Definition ble.h:62
@ BLE_COMPONENT_STATE_DISABLED
BLE is disabled.
Definition ble.h:60
@ BLE_COMPONENT_STATE_ACTIVE
BLE is active.
Definition ble.h:64
uint64_t ble_addr_to_uint64(const esp_bd_addr_t address)
Definition ble.cpp:533
uint16_t x
Definition tt21100.cpp:5