ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
ble_server.h
Go to the documentation of this file.
1#pragma once
2
3#include "ble_service.h"
5
11
12#include <memory>
13#include <vector>
14#include <unordered_map>
15#include <functional>
16
17#ifdef USE_ESP32
18
19#include <esp_gatts_api.h>
20
22
23using namespace esp32_ble;
24using namespace bytebuffer;
25
26class BLEServer final : public Component, public Parented<ESP32BLE> {
27 public:
28 void setup() override;
29 void loop() override;
30 void dump_config() override;
31 float get_setup_priority() const override;
32 bool can_proceed() override;
33
34 ESPHOME_ALWAYS_INLINE bool is_running() { return this->parent_->is_active() && this->state_ == RUNNING; }
35
36 void set_manufacturer_data(const std::vector<uint8_t> &data) {
37 this->manufacturer_data_ = data;
39 }
40
46 void set_advertising_required(bool required) { this->advertising_required_ = required; }
47
48 void set_max_clients(uint8_t max_clients) { this->max_clients_ = max_clients; }
49 uint8_t get_max_clients() const { return this->max_clients_; }
50
51 BLEService *create_service(ESPBTUUID uuid, bool advertise = false, uint16_t num_handles = 15);
52 void remove_service(ESPBTUUID uuid, uint8_t inst_id = 0);
53 BLEService *get_service(ESPBTUUID uuid, uint8_t inst_id = 0);
54 void enqueue_start_service(BLEService *service) { this->services_to_start_.push_back(service); }
56
57 esp_gatt_if_t get_gatts_if() { return this->gatts_if_; }
59 const uint16_t *get_clients() const { return this->clients_; }
60 uint8_t get_client_count() const { return this->client_count_; }
61
62 void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
63
65
66#ifdef USE_ESP32_BLE_SERVER_SECURITY
67 void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param);
68
69 void set_pairing_enabled(bool pairing_enabled) { this->pairing_enabled_ = pairing_enabled; }
70#ifdef USE_ESP32_BLE_SERVER_STATIC_PASSKEY
71 void set_passkey(uint32_t passkey) { this->passkey_ = passkey; }
72#endif
74 void passkey_reply(uint64_t address, uint32_t passkey);
76 void numeric_comparison_reply(uint64_t address, bool accept);
77#endif
78 void remove_bond(uint64_t address);
79
80 // Direct callback registration - supports multiple callbacks
81 void on_connect(std::function<void(uint16_t)> &&callback) {
82 this->callbacks_.push_back({CallbackType::ON_CONNECT, std::move(callback)});
83 }
84 void on_disconnect(std::function<void(uint16_t)> &&callback) {
85 this->callbacks_.push_back({CallbackType::ON_DISCONNECT, std::move(callback)});
86 }
87#ifdef USE_ESP32_BLE_SERVER_ON_PASSKEY_REQUEST
88 template<typename F> void add_on_passkey_request_callback(F &&callback) {
89 this->passkey_request_callback_.add(std::forward<F>(callback));
90 }
91#endif
92#ifdef USE_ESP32_BLE_SERVER_ON_PASSKEY_NOTIFICATION
93 template<typename F> void add_on_passkey_notification_callback(F &&callback) {
94 this->passkey_notification_callback_.add(std::forward<F>(callback));
95 }
96#endif
97#ifdef USE_ESP32_BLE_SERVER_ON_NUMERIC_COMPARISON_REQUEST
98 template<typename F> void add_on_numeric_comparison_request_callback(F &&callback) {
99 this->numeric_comparison_request_callback_.add(std::forward<F>(callback));
100 }
101#endif
102
103 protected:
104 enum class CallbackType : uint8_t {
107 };
108
111 std::function<void(uint16_t)> callback;
112 };
113
119
123
124 int8_t find_client_index_(uint16_t conn_id) const;
125 void add_client_(const esp_ble_gatts_cb_param_t::gatts_connect_evt_param &connect);
126#ifdef USE_ESP32_BLE_SERVER_SECURITY
127 bool is_client_address_(const esp_bd_addr_t address) const;
128#endif
129 void remove_client_(uint16_t conn_id);
130 void dispatch_callbacks_(CallbackType type, uint16_t conn_id);
131
132 std::vector<CallbackEntry> callbacks_;
133#ifdef USE_ESP32_BLE_SERVER_ON_PASSKEY_REQUEST
135#endif
136#ifdef USE_ESP32_BLE_SERVER_ON_PASSKEY_NOTIFICATION
138#endif
139#ifdef USE_ESP32_BLE_SERVER_ON_NUMERIC_COMPARISON_REQUEST
141#endif
142
143#ifdef USE_ESP32_BLE_SERVER_STATIC_PASSKEY
145#endif
146 std::vector<uint8_t> manufacturer_data_{};
147 esp_gatt_if_t gatts_if_{0};
148 bool registered_{false};
149#ifdef USE_ESP32_BLE_SERVER_SECURITY
151#endif
154
155 uint16_t clients_[USE_ESP32_BLE_MAX_CONNECTIONS]{};
156#ifdef USE_ESP32_BLE_SERVER_SECURITY
157 // Peer address of each entry in clients_, kept in the same order
158 esp_bd_addr_t client_addresses_[USE_ESP32_BLE_MAX_CONNECTIONS]{};
159#endif
160 uint8_t client_count_{0};
161 uint8_t max_clients_{1};
162 std::vector<ServiceEntry> services_{};
163 std::vector<BLEService *> services_to_start_{};
165
172};
173
174// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
175extern BLEServer *global_ble_server;
176
177} // namespace esphome::esp32_ble_server
178
179#endif
Helper class to easily give an object a parent of type T.
Definition helpers.h:2160
void set_advertising_required(bool required)
Whether this server needs the device to advertise so clients can find and connect to it.
Definition ble_server.h:46
std::vector< CallbackEntry > callbacks_
Definition ble_server.h:132
void add_client_(const esp_ble_gatts_cb_param_t::gatts_connect_evt_param &connect)
CallbackManager< void(uint64_t)> passkey_request_callback_
Definition ble_server.h:134
const uint16_t * get_clients() const
Definition ble_server.h:59
std::vector< uint8_t > manufacturer_data_
Definition ble_server.h:146
esp_bd_addr_t client_addresses_[USE_ESP32_BLE_MAX_CONNECTIONS]
Definition ble_server.h:158
void set_device_information_service(BLEService *service)
Definition ble_server.h:55
void set_passkey(uint32_t passkey)
Definition ble_server.h:71
void add_on_numeric_comparison_request_callback(F &&callback)
Definition ble_server.h:98
void set_manufacturer_data(const std::vector< uint8_t > &data)
Definition ble_server.h:36
void remove_client_(uint16_t conn_id)
float get_setup_priority() const override
enum esphome::esp32_ble_server::BLEServer::State INIT
void dispatch_callbacks_(CallbackType type, uint16_t conn_id)
void add_on_passkey_request_callback(F &&callback)
Definition ble_server.h:88
ESPHOME_ALWAYS_INLINE bool is_running()
Definition ble_server.h:34
void set_pairing_enabled(bool pairing_enabled)
Definition ble_server.h:69
void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
void add_on_passkey_notification_callback(F &&callback)
Definition ble_server.h:93
BLEService * get_service(ESPBTUUID uuid, uint8_t inst_id=0)
void passkey_reply(uint64_t address, uint32_t passkey)
Answer a passkey request from the connected client at address.
void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param)
std::vector< BLEService * > services_to_start_
Definition ble_server.h:163
void enqueue_start_service(BLEService *service)
Definition ble_server.h:54
bool is_client_address_(const esp_bd_addr_t address) const
BLEService * create_service(ESPBTUUID uuid, bool advertise=false, uint16_t num_handles=15)
void remove_service(ESPBTUUID uuid, uint8_t inst_id=0)
std::vector< ServiceEntry > services_
Definition ble_server.h:162
uint16_t clients_[USE_ESP32_BLE_MAX_CONNECTIONS]
Definition ble_server.h:155
CallbackManager< void(uint64_t, uint32_t)> numeric_comparison_request_callback_
Definition ble_server.h:140
int8_t find_client_index_(uint16_t conn_id) const
void numeric_comparison_reply(uint64_t address, bool accept)
Accept or reject a numeric comparison request from the connected client at address.
void on_connect(std::function< void(uint16_t)> &&callback)
Definition ble_server.h:81
void on_disconnect(std::function< void(uint16_t)> &&callback)
Definition ble_server.h:84
void remove_bond(uint64_t address)
void set_max_clients(uint8_t max_clients)
Definition ble_server.h:48
CallbackManager< void(uint64_t, uint32_t)> passkey_notification_callback_
Definition ble_server.h:137
uint8_t type
static void uint32_t
Definition ble_server.h:109
CallbackType type
Definition ble_server.h:110
std::function< void(uint16_t)> callback
Definition ble_server.h:111
Definition ble_server.h:114
ESPBTUUID uuid
Definition ble_server.h:115
BLEService * service
Definition ble_server.h:117
uint8_t inst_id
Definition ble_server.h:116