ESPHome 2026.5.0
Loading...
Searching...
No Matches
ble_server.cpp
Go to the documentation of this file.
1#include "ble_server.h"
2
4#include "esphome/core/log.h"
7
8#ifdef USE_ESP32
9
10#include <nvs_flash.h>
11#include <freertos/FreeRTOSConfig.h>
12#include <esp_bt_main.h>
13#ifndef CONFIG_ESP_HOSTED_ENABLE_BT_BLUEDROID
14#include <esp_bt.h>
15#endif
16#include <freertos/task.h>
17#include <esp_gap_ble_api.h>
18
20
21static const char *const TAG = "esp32_ble_server";
22
24 if (this->parent_->is_failed()) {
25 this->mark_failed();
26 ESP_LOGE(TAG, "BLE Server was marked failed by ESP32BLE");
27 return;
28 }
29 global_ble_server = this;
30}
31
33 if (!this->parent_->is_active()) {
34 return;
35 }
36 switch (this->state_) {
37 case RUNNING: {
38 // Start all services that are pending to start
39 if (!this->services_to_start_.empty()) {
40 size_t write_idx = 0;
41 for (auto *service : this->services_to_start_) {
42 if (service->is_created()) {
43 service->start(); // Needs to be called once per characteristic in the service
44 }
45 // Remove services that have started or are starting
46 if (!service->is_starting() && !service->is_running()) {
47 this->services_to_start_[write_idx++] = service;
48 }
49 }
50 this->services_to_start_.erase(this->services_to_start_.begin() + write_idx, this->services_to_start_.end());
51 }
52 break;
53 }
54 case INIT: {
55 esp_err_t err = esp_ble_gatts_app_register(0);
56 if (err != ESP_OK) {
57 ESP_LOGE(TAG, "esp_ble_gatts_app_register failed: %d", err);
58 this->mark_failed();
59 return;
60 }
61 this->state_ = REGISTERING;
62 break;
63 }
64 case REGISTERING: {
65 if (this->registered_) {
66 // Create the device information service first so
67 // it is at the top of the GATT table
69 // Create all services previously created
70 for (auto &entry : this->services_) {
71 if (entry.service == this->device_information_service_) {
72 continue;
73 }
74 entry.service->do_create(this);
75 }
76 this->state_ = STARTING_SERVICE;
77 }
78 break;
79 }
80 case STARTING_SERVICE: {
82 this->state_ = RUNNING;
84 ESP_LOGD(TAG, "BLE server setup successfully");
85 } else if (this->device_information_service_->is_created()) {
87 }
88 break;
89 }
90 }
91}
92
93bool BLEServer::can_proceed() { return this->is_running() || !this->parent_->is_active(); }
94
96 if (this->is_running()) {
97 this->parent_->advertising_set_manufacturer_data(this->manufacturer_data_);
98 }
99}
100
101BLEService *BLEServer::create_service(ESPBTUUID uuid, bool advertise, uint16_t num_handles) {
102#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
103 char uuid_buf[esp32_ble::UUID_STR_LEN];
104 uuid.to_str(uuid_buf);
105 ESP_LOGV(TAG, "Creating BLE service - %s", uuid_buf);
106#endif
107 // Calculate the inst_id for the service
108 uint8_t inst_id = 0;
109 for (; inst_id < 0xFF; inst_id++) {
110 if (this->get_service(uuid, inst_id) == nullptr) {
111 break;
112 }
113 }
114 if (inst_id == 0xFF) {
115 char warn_uuid_buf[esp32_ble::UUID_STR_LEN];
116 uuid.to_str(warn_uuid_buf);
117 ESP_LOGW(TAG, "Could not create BLE service %s, too many instances", warn_uuid_buf);
118 return nullptr;
119 }
120 BLEService *service = // NOLINT(cppcoreguidelines-owning-memory)
121 new BLEService(uuid, num_handles, inst_id, advertise);
122 this->services_.push_back({uuid, inst_id, service});
123 if (this->parent_->is_active() && this->registered_) {
124 service->do_create(this);
125 }
126 return service;
127}
128
129void BLEServer::remove_service(ESPBTUUID uuid, uint8_t inst_id) {
130#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
131 char uuid_buf[esp32_ble::UUID_STR_LEN];
132 uuid.to_str(uuid_buf);
133 ESP_LOGV(TAG, "Removing BLE service - %s %d", uuid_buf, inst_id);
134#endif
135 for (auto it = this->services_.begin(); it != this->services_.end(); ++it) {
136 if (it->uuid == uuid && it->inst_id == inst_id) {
137 it->service->do_delete();
138 delete it->service; // NOLINT(cppcoreguidelines-owning-memory)
139 this->services_.erase(it);
140 return;
141 }
142 }
143 char warn_uuid_buf[esp32_ble::UUID_STR_LEN];
144 uuid.to_str(warn_uuid_buf);
145 ESP_LOGW(TAG, "BLE service %s %d does not exist", warn_uuid_buf, inst_id);
146}
147
149 for (auto &entry : this->services_) {
150 if (entry.uuid == uuid && entry.inst_id == inst_id) {
151 return entry.service;
152 }
153 }
154 return nullptr;
155}
156
158 for (auto &entry : this->callbacks_) {
159 if (entry.type == type) {
160 entry.callback(conn_id);
161 }
162 }
163}
164
165void BLEServer::gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if,
166 esp_ble_gatts_cb_param_t *param) {
167 switch (event) {
168 case ESP_GATTS_CONNECT_EVT: {
169 ESP_LOGD(TAG, "BLE Client connected");
170 this->add_client_(param->connect.conn_id);
171 // Resume advertising so additional clients can discover and connect
173 this->parent_->advertising_start();
174 }
175 this->dispatch_callbacks_(CallbackType::ON_CONNECT, param->connect.conn_id);
176 break;
177 }
178 case ESP_GATTS_DISCONNECT_EVT: {
179 ESP_LOGD(TAG, "BLE Client disconnected");
180 this->remove_client_(param->disconnect.conn_id);
181 this->parent_->advertising_start();
182 this->dispatch_callbacks_(CallbackType::ON_DISCONNECT, param->disconnect.conn_id);
183 break;
184 }
185 case ESP_GATTS_REG_EVT: {
186 this->gatts_if_ = gatts_if;
187 this->registered_ = true;
188 break;
189 }
190 default:
191 break;
192 }
193
194 for (auto &entry : this->services_) {
195 entry.service->gatts_event_handler(event, gatts_if, param);
196 }
197}
198
199int8_t BLEServer::find_client_index_(uint16_t conn_id) const {
200 for (uint8_t i = 0; i < this->client_count_; i++) {
201 if (this->clients_[i] == conn_id)
202 return i;
203 }
204 return -1;
205}
206
207void BLEServer::add_client_(uint16_t conn_id) {
208 // Check if already in list
209 if (this->find_client_index_(conn_id) >= 0)
210 return;
211 // Add if there's space
212 if (this->client_count_ < USE_ESP32_BLE_MAX_CONNECTIONS) {
213 this->clients_[this->client_count_++] = conn_id;
214 } else {
215 // This should never happen since max clients is known at compile time
216 ESP_LOGE(TAG, "Client array full");
217 }
218}
219
220void BLEServer::remove_client_(uint16_t conn_id) {
221 int8_t index = this->find_client_index_(conn_id);
222 if (index >= 0) {
223 // Replace with last element and decrement count (client order not preserved)
224 this->clients_[index] = this->clients_[--this->client_count_];
225 }
226}
227
229 // Delete all clients
230 this->client_count_ = 0;
231 // Delete all services
232 for (auto &entry : this->services_) {
233 entry.service->do_delete();
234 }
235 this->registered_ = false;
236 this->state_ = INIT;
237}
238
240
242 ESP_LOGCONFIG(TAG,
243 "ESP32 BLE Server:\n"
244 " Max clients: %u",
245 this->max_clients_);
246}
247
248BLEServer *global_ble_server = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
249
250} // namespace esphome::esp32_ble_server
251
252#endif
void mark_failed()
Mark this component as failed.
ESPDEPRECATED("Use to_str() instead. Removed in 2026.8.0", "2026.2.0") std const char * to_str(std::span< char, UUID_STR_LEN > output) const
Definition ble_uuid.cpp:146
std::vector< CallbackEntry > callbacks_
Definition ble_server.h:91
std::vector< uint8_t > manufacturer_data_
Definition ble_server.h:93
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)
ESPHOME_ALWAYS_INLINE bool is_running()
Definition ble_server.h:34
BLEService * get_service(ESPBTUUID uuid, uint8_t inst_id=0)
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:101
BLEService * create_service(ESPBTUUID uuid, bool advertise=false, uint16_t num_handles=15)
void remove_service(ESPBTUUID uuid, uint8_t inst_id=0)
void add_client_(uint16_t conn_id)
std::vector< ServiceEntry > services_
Definition ble_server.h:100
uint16_t clients_[USE_ESP32_BLE_MAX_CONNECTIONS]
Definition ble_server.h:97
int8_t find_client_index_(uint16_t conn_id) const
void do_create(BLEServer *server)
uint16_t type
constexpr float AFTER_BLUETOOTH
Definition component.h:47