ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
ble_client_base.cpp
Go to the documentation of this file.
1#include "ble_client_base.h"
2
4#include "esphome/core/log.h"
5
6#ifdef USE_ESP32
7
8#include <esp_gap_ble_api.h>
9#include <esp_gatt_defs.h>
10#include <esp_gattc_api.h>
11
13
14static const char *const TAG = "esp32_ble_client";
15
16// Intermediate connection parameters for standard operation
17// ESP-IDF defaults (12.5-15ms) are too slow for stable connections through WiFi-based BLE proxies,
18// causing disconnections. These medium parameters balance responsiveness with bandwidth usage.
19static constexpr uint16_t MEDIUM_MIN_CONN_INTERVAL = 0x07; // 7 * 1.25ms = 8.75ms
20static constexpr uint16_t MEDIUM_MAX_CONN_INTERVAL = 0x09; // 9 * 1.25ms = 11.25ms
21// The timeout value was increased from 6s to 8s to address stability issues observed
22// in certain BLE devices when operating through WiFi-based BLE proxies. The longer
23// timeout reduces the likelihood of disconnections during periods of high latency.
24static constexpr uint16_t MEDIUM_CONN_TIMEOUT = 800; // 800 * 10ms = 8s
25
26// Fastest connection parameters for devices with short discovery timeouts
27static constexpr uint16_t FAST_MIN_CONN_INTERVAL = 0x06; // 6 * 1.25ms = 7.5ms (BLE minimum)
28static constexpr uint16_t FAST_MAX_CONN_INTERVAL = 0x06; // 6 * 1.25ms = 7.5ms
29static constexpr uint16_t FAST_CONN_TIMEOUT = 1000; // 1000 * 10ms = 10s
30static constexpr uint32_t DISCONNECTING_TIMEOUT = 10000; // 10s
31static const esp_bt_uuid_t NOTIFY_DESC_UUID = {
32 .len = ESP_UUID_LEN_16,
33 .uuid =
34 {
35 .uuid16 = ESP_GATT_UUID_CHAR_CLIENT_CONFIG,
36 },
37};
38
40 static uint8_t connection_index = 0;
41 this->connection_index_ = connection_index++;
42}
43
45 ESP_LOGV(TAG, "[%d] [%s] Set state %d", this->connection_index_, this->address_str_, (int) st);
46 ESPBTClient::set_state(st);
47}
48
50 if (!esp32_ble::global_ble->is_active()) {
51 this->set_state(espbt::ClientState::INIT);
52 return;
53 }
54 if (this->state() == espbt::ClientState::INIT) {
55 auto ret = esp_ble_gattc_app_register(this->app_id);
56 if (ret) {
57 ESP_LOGE(TAG, "gattc app register failed. app_id=%d code=%d", this->app_id, ret);
58 this->mark_failed();
59 }
60 this->set_state(espbt::ClientState::IDLE);
61 }
62 // If idle, we can disable the loop as connect()
63 // will enable it again when a connection is needed.
64 else if (this->state() == espbt::ClientState::IDLE) {
65 this->disable_loop();
66 } else if (this->state() == espbt::ClientState::DISCONNECTING &&
67 (millis() - this->disconnecting_started_) > DISCONNECTING_TIMEOUT) {
68 ESP_LOGE(TAG, "[%d] [%s] Timeout waiting for CLOSE_EVT after disconnect, forcing IDLE", this->connection_index_,
69 this->address_str_);
70 // release_services() must be called before set_idle_() — if we entered DISCONNECTING
71 // via unconditional_disconnect() (which doesn't call release_services()), and ESP-IDF
72 // never delivered CLOSE_EVT/DISCONNECT_EVT, services would leak without this call.
73 this->release_services();
74 this->set_idle_();
75 this->on_disconnect_complete(ESP_GATT_CONN_TIMEOUT);
76 }
77}
78
80
82 ESP_LOGCONFIG(TAG,
83 " Address: %s\n"
84 " Auto-Connect: %s\n"
85 " State: %s",
86 this->address_str(), TRUEFALSE(this->auto_connect_), espbt::client_state_to_string(this->state()));
87 if (this->status_ == ESP_GATT_NO_RESOURCES) {
88 ESP_LOGE(TAG, " Failed due to no resources. Try to reduce number of BLE clients in config.");
89 } else if (this->status_ != ESP_GATT_OK) {
90 ESP_LOGW(TAG, " Failed due to error code %d", this->status_);
91 }
92}
93
94#ifdef USE_ESP32_BLE_DEVICE
96 if (!this->auto_connect_)
97 return false;
98 if (this->address_ == 0 || device.address_uint64() != this->address_)
99 return false;
100 if (this->state() != espbt::ClientState::IDLE)
101 return false;
102
103 this->log_event_("Found device");
104 if (ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG)
106
107 this->set_state(espbt::ClientState::DISCOVERED);
108 this->set_address(device.address_uint64());
109 this->remote_addr_type_ = device.get_address_type();
110 return true;
111}
112#endif
113
115 // Prevent duplicate connection attempts or connecting while still disconnecting
116 if (this->state() == espbt::ClientState::CONNECTING || this->state() == espbt::ClientState::CONNECTED ||
117 this->state() == espbt::ClientState::ESTABLISHED) {
118 ESP_LOGW(TAG, "[%d] [%s] Connection already in progress, state=%s", this->connection_index_, this->address_str_,
120 return;
121 } else if (this->state() == espbt::ClientState::DISCONNECTING) {
122 ESP_LOGW(TAG, "[%d] [%s] Cannot connect, still waiting for CLOSE_EVT to complete disconnect",
123 this->connection_index_, this->address_str_);
124 return;
125 }
126 ESP_LOGI(TAG, "[%d] [%s] 0x%02x Connecting", this->connection_index_, this->address_str_, this->remote_addr_type_);
127 this->paired_ = false;
128 // Enable loop for state processing
129 this->enable_loop();
130 // Immediately transition to CONNECTING to prevent duplicate connection attempts
131 this->set_state(espbt::ClientState::CONNECTING);
132
133 // Determine connection parameters based on connection type
134 if (this->connection_type_ == espbt::ConnectionType::V3_WITHOUT_CACHE) {
135 // V3 without cache needs fast params for service discovery
136 this->set_conn_params_(FAST_MIN_CONN_INTERVAL, FAST_MAX_CONN_INTERVAL, 0, FAST_CONN_TIMEOUT, "fast");
137 } else if (this->connection_type_ == espbt::ConnectionType::V3_WITH_CACHE) {
138 // V3 with cache can use medium params
139 this->set_conn_params_(MEDIUM_MIN_CONN_INTERVAL, MEDIUM_MAX_CONN_INTERVAL, 0, MEDIUM_CONN_TIMEOUT, "medium");
140 }
141 // For V1/Legacy, don't set params - use ESP-IDF defaults
142
143 // Open the connection
144 auto ret = esp_ble_gattc_open(this->gattc_if_, this->remote_bda_, this->remote_addr_type_, true);
145 this->handle_connection_result_(ret);
146}
147
148esp_err_t BLEClientBase::pair() { return esp_ble_set_encryption(this->remote_bda_, ESP_BLE_SEC_ENCRYPT); }
149
151 if (this->state() == espbt::ClientState::IDLE || this->state() == espbt::ClientState::DISCONNECTING) {
152 ESP_LOGI(TAG, "[%d] [%s] Disconnect requested, but already %s", this->connection_index_, this->address_str_,
154 return;
155 }
156 if (this->state() == espbt::ClientState::CONNECTING || this->conn_id_ == UNSET_CONN_ID) {
157 ESP_LOGD(TAG, "[%d] [%s] Disconnect before connected, disconnect scheduled", this->connection_index_,
158 this->address_str_);
159 this->want_disconnect_ = true;
160 return;
161 }
163}
164
166 // Disconnect without checking the state.
167 ESP_LOGI(TAG, "[%d] [%s] Disconnecting (conn_id: %d).", this->connection_index_, this->address_str_, this->conn_id_);
168 if (this->state() == espbt::ClientState::DISCONNECTING) {
169 this->log_error_("Already disconnecting");
170 return;
171 }
172 if (this->conn_id_ == UNSET_CONN_ID) {
173 this->log_error_("conn id unset, cannot disconnect");
174 return;
175 }
176 auto err = esp_ble_gattc_close(this->gattc_if_, this->conn_id_);
177 if (err != ESP_OK) {
178 //
179 // This is a fatal error, but we can't do anything about it
180 // and it likely means the BLE stack is in a bad state.
181 //
182 // In the future we might consider App.reboot() here since
183 // the BLE stack is in an indeterminate state.
184 //
185 this->log_gattc_warning_("esp_ble_gattc_close", err);
186 }
187
188 if (this->state() == espbt::ClientState::DISCOVERED) {
189 this->set_address(0);
190 this->set_state(espbt::ClientState::IDLE);
191 } else {
192 this->set_disconnecting_();
193 }
194}
195
197#ifdef USE_ESP32_BLE_DEVICE
198 for (auto &svc : this->services_)
199 delete svc; // NOLINT(cppcoreguidelines-owning-memory)
200 this->services_.clear();
201#endif
202#ifndef CONFIG_BT_GATTC_CACHE_NVS_FLASH
203 esp_ble_gattc_cache_clean(this->remote_bda_);
204#endif
205}
206
207void BLEClientBase::log_event_(const char *name) {
208 ESP_LOGD(TAG, "[%d] [%s] %s", this->connection_index_, this->address_str_, name);
209}
210
212 ESP_LOGD(TAG, "[%d] [%s] ESP_GATTC_%s_EVT", this->connection_index_, this->address_str_, name);
213}
214
216 // Data transfer events are logged at VERBOSE level because logging to UART creates
217 // delays that cause timing issues during time-sensitive BLE operations. This is
218 // especially problematic during pairing or firmware updates which require rapid
219 // writes to many characteristics - the log spam can cause these operations to fail.
220 ESP_LOGV(TAG, "[%d] [%s] ESP_GATTC_%s_EVT", this->connection_index_, this->address_str_, name);
221}
222
223void BLEClientBase::log_gattc_warning_(const char *operation, esp_gatt_status_t status) {
224 ESP_LOGW(TAG, "[%d] [%s] %s error, status=%d", this->connection_index_, this->address_str_, operation, status);
225}
226
227void BLEClientBase::log_gattc_warning_(const char *operation, esp_err_t err) {
228 ESP_LOGW(TAG, "[%d] [%s] %s error, status=%d", this->connection_index_, this->address_str_, operation, err);
229}
230
231void BLEClientBase::log_connection_params_(const char *param_type) {
232 ESP_LOGD(TAG, "[%d] [%s] %s conn params", this->connection_index_, this->address_str_, param_type);
233}
234
236 if (ret) {
237 this->log_gattc_warning_("esp_ble_gattc_open", ret);
238 // Don't use set_idle_() here — CONNECT_EVT never fired so conn_id_ is still UNSET_CONN_ID.
239 this->set_state(espbt::ClientState::IDLE);
240 }
241}
242
244 ESP_LOGE(TAG, "[%d] [%s] %s", this->connection_index_, this->address_str_, message);
245}
246
247void BLEClientBase::log_error_(const char *message, int code) {
248 ESP_LOGE(TAG, "[%d] [%s] %s=%d", this->connection_index_, this->address_str_, message, code);
249}
250
252 ESP_LOGW(TAG, "[%d] [%s] %s", this->connection_index_, this->address_str_, message);
253}
254
255esp_err_t BLEClientBase::update_conn_params_(uint16_t min_interval, uint16_t max_interval, uint16_t latency,
256 uint16_t timeout, const char *param_type) {
257 esp_ble_conn_update_params_t conn_params = {{0}};
258 memcpy(conn_params.bda, this->remote_bda_, sizeof(esp_bd_addr_t));
259 conn_params.min_int = min_interval;
260 conn_params.max_int = max_interval;
261 conn_params.latency = latency;
262 conn_params.timeout = timeout;
263 this->log_connection_params_(param_type);
264 esp_err_t err = esp_ble_gap_update_conn_params(&conn_params);
265 if (err != ESP_OK) {
266 this->log_gattc_warning_("esp_ble_gap_update_conn_params", err);
267 }
268 return err;
269}
270
271void BLEClientBase::set_conn_params_(uint16_t min_interval, uint16_t max_interval, uint16_t latency, uint16_t timeout,
272 const char *param_type) {
273 // Set preferred connection parameters before connecting
274 // These will be used when establishing the connection
275 this->log_connection_params_(param_type);
276 esp_err_t err = esp_ble_gap_set_prefer_conn_params(this->remote_bda_, min_interval, max_interval, latency, timeout);
277 if (err != ESP_OK) {
278 this->log_gattc_warning_("esp_ble_gap_set_prefer_conn_params", err);
279 }
280}
281
282bool BLEClientBase::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t esp_gattc_if,
283 esp_ble_gattc_cb_param_t *param) {
284 if (event == ESP_GATTC_REG_EVT && this->app_id != param->reg.app_id)
285 return false;
286 if (event != ESP_GATTC_REG_EVT && esp_gattc_if != ESP_GATT_IF_NONE && esp_gattc_if != this->gattc_if_)
287 return false;
288
289 ESP_LOGV(TAG, "[%d] [%s] gattc_event_handler: event=%d gattc_if=%d", this->connection_index_, this->address_str_,
290 event, esp_gattc_if);
291
292 switch (event) {
293 case ESP_GATTC_REG_EVT: {
294 if (param->reg.status == ESP_GATT_OK) {
295 ESP_LOGV(TAG, "[%d] [%s] gattc registered app id %d", this->connection_index_, this->address_str_,
296 this->app_id);
297 this->gattc_if_ = esp_gattc_if;
298 } else {
299 this->log_error_("gattc app registration failed status", param->reg.status);
300 this->status_ = param->reg.status;
301 this->mark_failed();
302 }
303 break;
304 }
305 case ESP_GATTC_OPEN_EVT: {
306 if (!this->check_addr(param->open.remote_bda))
307 return false;
308 this->log_gattc_lifecycle_event_("OPEN");
309 // conn_id was already set in ESP_GATTC_CONNECT_EVT
310 this->service_count_ = 0;
311
312 // ESP-IDF's BLE stack may send ESP_GATTC_OPEN_EVT after esp_ble_gattc_open() returns an
313 // error, if the error occurred at the BTA/GATT layer. This can result in the event
314 // arriving after we've already transitioned to IDLE state.
315 if (this->state() == espbt::ClientState::IDLE) {
316 ESP_LOGD(TAG, "[%d] [%s] ESP_GATTC_OPEN_EVT in IDLE state (status=%d), ignoring", this->connection_index_,
317 this->address_str_, param->open.status);
318 break;
319 }
320
321 if (this->state() != espbt::ClientState::CONNECTING) {
322 // This should not happen but lets log it in case it does
323 // because it means we have a bad assumption about how the
324 // ESP BT stack works.
325 ESP_LOGE(TAG, "[%d] [%s] ESP_GATTC_OPEN_EVT in %s state (status=%d)", this->connection_index_,
326 this->address_str_, espbt::client_state_to_string(this->state()), param->open.status);
327 }
328 if (param->open.status != ESP_GATT_OK && param->open.status != ESP_GATT_ALREADY_OPEN) {
329 this->log_gattc_warning_("Connection open", param->open.status);
330 // Connection was never established so CLOSE_EVT may not follow
331 this->set_idle_();
332 break;
333 }
334 if (this->want_disconnect_) {
335 // Disconnect was requested after connecting started,
336 // but before the connection was established. Now that we have
337 // this->conn_id_ set, we can disconnect it.
338 // Don't reset conn_id_ here — CLOSE_EVT needs it to match and call set_idle_().
340 break;
341 }
342 // MTU negotiation already started in ESP_GATTC_CONNECT_EVT
343 this->set_state(espbt::ClientState::CONNECTED);
344 ESP_LOGI(TAG, "[%d] [%s] Connection open", this->connection_index_, this->address_str_);
345 if (this->connection_type_ == espbt::ConnectionType::V3_WITH_CACHE) {
346 // Cached connections already connected with medium parameters, no update needed
347 // only set our state, subclients might have more stuff to do yet.
348 this->set_state_internal_(espbt::ClientState::ESTABLISHED);
349 break;
350 }
351 // For V3_WITHOUT_CACHE, we already set fast params before connecting
352 // No need to update them again here
353 this->log_event_("Searching for services");
354 esp_ble_gattc_search_service(esp_gattc_if, param->open.conn_id, nullptr);
355 break;
356 }
357 case ESP_GATTC_CONNECT_EVT: {
358 if (!this->check_addr(param->connect.remote_bda))
359 return false;
360 this->log_gattc_lifecycle_event_("CONNECT");
361 this->conn_id_ = param->connect.conn_id;
362 // Start MTU negotiation immediately as recommended by ESP-IDF examples
363 // (gatt_client, ble_throughput) which call esp_ble_gattc_send_mtu_req in
364 // ESP_GATTC_CONNECT_EVT instead of waiting for ESP_GATTC_OPEN_EVT.
365 // This saves ~3ms in the connection process.
366 auto ret = esp_ble_gattc_send_mtu_req(this->gattc_if_, param->connect.conn_id);
367 if (ret) {
368 this->log_gattc_warning_("esp_ble_gattc_send_mtu_req", ret);
369 }
370 break;
371 }
372 case ESP_GATTC_DISCONNECT_EVT: {
373 if (!this->check_addr(param->disconnect.remote_bda))
374 return false;
375 // Check if we were disconnected while waiting for service discovery
376 if (param->disconnect.reason == ESP_GATT_CONN_TERMINATE_PEER_USER &&
377 this->state() == espbt::ClientState::CONNECTED) {
378 this->log_warning_("Remote closed during discovery");
379 } else {
380 ESP_LOGD(TAG, "[%d] [%s] ESP_GATTC_DISCONNECT_EVT, reason 0x%02x", this->connection_index_, this->address_str_,
381 param->disconnect.reason);
382 }
383 // For active disconnects (esp_ble_gattc_close), CLOSE_EVT arrives before
384 // DISCONNECT_EVT. If CLOSE_EVT already transitioned us to IDLE, don't go
385 // backwards to DISCONNECTING — the connection is already fully cleaned up.
386 if (this->state() == espbt::ClientState::IDLE) {
387 this->log_event_("DISCONNECT_EVT after CLOSE_EVT, already IDLE");
388 break;
389 }
390 // For passive disconnects (remote device disconnected or link lost),
391 // DISCONNECT_EVT arrives first. Don't transition to IDLE yet — wait for
392 // CLOSE_EVT to ensure the controller has fully freed resources (L2CAP
393 // channels, ATT resources, HCI connection handle). Transitioning to IDLE
394 // here would allow reconnection before cleanup is complete, causing the
395 // controller to reject the new connection (status=133) or crash with
396 // ASSERT_PARAM in lld_evt.c.
397 this->release_services();
398 this->set_disconnecting_();
399 break;
400 }
401
402 case ESP_GATTC_CFG_MTU_EVT: {
403 if (this->conn_id_ != param->cfg_mtu.conn_id)
404 return false;
405 if (param->cfg_mtu.status != ESP_GATT_OK) {
406 ESP_LOGW(TAG, "[%d] [%s] cfg_mtu failed, mtu %d, status %d", this->connection_index_, this->address_str_,
407 param->cfg_mtu.mtu, param->cfg_mtu.status);
408 // No state change required here - disconnect event will follow if needed.
409 break;
410 }
411 ESP_LOGD(TAG, "[%d] [%s] cfg_mtu status %d, mtu %d", this->connection_index_, this->address_str_,
412 param->cfg_mtu.status, param->cfg_mtu.mtu);
413 this->mtu_ = param->cfg_mtu.mtu;
414 break;
415 }
416 case ESP_GATTC_CLOSE_EVT: {
417 if (this->conn_id_ != param->close.conn_id)
418 return false;
419 this->log_gattc_lifecycle_event_("CLOSE");
420 this->release_services();
421 this->set_idle_();
422 this->on_disconnect_complete(param->close.reason);
423 break;
424 }
425 case ESP_GATTC_SEARCH_RES_EVT: {
426 if (this->conn_id_ != param->search_res.conn_id)
427 return false;
428 this->service_count_++;
429 if (this->connection_type_ == espbt::ConnectionType::V3_WITHOUT_CACHE) {
430 // V3 clients don't need services initialized since
431 // as they use the ESP APIs to get services.
432 break;
433 }
434#ifdef USE_ESP32_BLE_DEVICE
435 BLEService *ble_service = new BLEService(); // NOLINT(cppcoreguidelines-owning-memory)
436 ble_service->uuid = espbt::ESPBTUUID::from_uuid(param->search_res.srvc_id.uuid);
437 ble_service->start_handle = param->search_res.start_handle;
438 ble_service->end_handle = param->search_res.end_handle;
439 ble_service->client = this;
440 this->services_.push_back(ble_service);
441#endif
442 break;
443 }
444 case ESP_GATTC_SEARCH_CMPL_EVT: {
445 if (this->conn_id_ != param->search_cmpl.conn_id)
446 return false;
447 this->log_gattc_lifecycle_event_("SEARCH_CMPL");
448 // For V3_WITHOUT_CACHE, switch back to medium connection parameters after service discovery
449 // This balances performance with bandwidth usage after the critical discovery phase
450 if (this->connection_type_ == espbt::ConnectionType::V3_WITHOUT_CACHE) {
451 this->update_conn_params_(MEDIUM_MIN_CONN_INTERVAL, MEDIUM_MAX_CONN_INTERVAL, 0, MEDIUM_CONN_TIMEOUT, "medium");
452 } else if (this->connection_type_ != espbt::ConnectionType::V3_WITH_CACHE) {
453#ifdef USE_ESP32_BLE_DEVICE
454#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
455 for (auto &svc : this->services_) {
456 char uuid_buf[espbt::UUID_STR_LEN];
457 svc->uuid.to_str(uuid_buf);
458 ESP_LOGV(TAG, "[%d] [%s] Service UUID: %s", this->connection_index_, this->address_str_, uuid_buf);
459 ESP_LOGV(TAG, "[%d] [%s] start_handle: 0x%x end_handle: 0x%x", this->connection_index_, this->address_str_,
460 svc->start_handle, svc->end_handle);
461 }
462#endif
463#endif
464 }
465 ESP_LOGI(TAG, "[%d] [%s] Service discovery complete", this->connection_index_, this->address_str_);
466 this->set_state_internal_(espbt::ClientState::ESTABLISHED);
467 break;
468 }
469 case ESP_GATTC_READ_DESCR_EVT: {
470 if (this->conn_id_ != param->write.conn_id)
471 return false;
472 this->log_gattc_data_event_("READ_DESCR");
473 break;
474 }
475 case ESP_GATTC_WRITE_DESCR_EVT: {
476 if (this->conn_id_ != param->write.conn_id)
477 return false;
478 this->log_gattc_data_event_("WRITE_DESCR");
479 break;
480 }
481 case ESP_GATTC_WRITE_CHAR_EVT: {
482 if (this->conn_id_ != param->write.conn_id)
483 return false;
484 this->log_gattc_data_event_("WRITE_CHAR");
485 break;
486 }
487 case ESP_GATTC_READ_CHAR_EVT: {
488 if (this->conn_id_ != param->read.conn_id)
489 return false;
490 this->log_gattc_data_event_("READ_CHAR");
491 break;
492 }
493 case ESP_GATTC_NOTIFY_EVT: {
494 if (this->conn_id_ != param->notify.conn_id)
495 return false;
496 this->log_gattc_data_event_("NOTIFY");
497 break;
498 }
499 case ESP_GATTC_REG_FOR_NOTIFY_EVT: {
500 this->log_gattc_data_event_("REG_FOR_NOTIFY");
501 if (this->connection_type_ == espbt::ConnectionType::V3_WITH_CACHE ||
502 this->connection_type_ == espbt::ConnectionType::V3_WITHOUT_CACHE) {
503 // Client is responsible for flipping the descriptor value
504 // when using the cache
505 break;
506 }
507 esp_gattc_descr_elem_t desc_result;
508 uint16_t count = 1;
509 esp_gatt_status_t descr_status = esp_ble_gattc_get_descr_by_char_handle(
510 this->gattc_if_, this->conn_id_, param->reg_for_notify.handle, NOTIFY_DESC_UUID, &desc_result, &count);
511 if (descr_status != ESP_GATT_OK) {
512 this->log_gattc_warning_("esp_ble_gattc_get_descr_by_char_handle", descr_status);
513 break;
514 }
515 esp_gattc_char_elem_t char_result;
516 esp_gatt_status_t char_status =
517 esp_ble_gattc_get_all_char(this->gattc_if_, this->conn_id_, param->reg_for_notify.handle,
518 param->reg_for_notify.handle, &char_result, &count, 0);
519 if (char_status != ESP_GATT_OK) {
520 this->log_gattc_warning_("esp_ble_gattc_get_all_char", char_status);
521 break;
522 }
523
524 /*
525 1 = notify
526 2 = indicate
527 */
528 uint16_t notify_en = char_result.properties & ESP_GATT_CHAR_PROP_BIT_NOTIFY ? 1 : 2;
529 esp_err_t status =
530 esp_ble_gattc_write_char_descr(this->gattc_if_, this->conn_id_, desc_result.handle, sizeof(notify_en),
531 (uint8_t *) &notify_en, ESP_GATT_WRITE_TYPE_RSP, ESP_GATT_AUTH_REQ_NONE);
532 ESP_LOGV(TAG, "Wrote notify descriptor %d, properties=%d", notify_en, char_result.properties);
533 if (status) {
534 this->log_gattc_warning_("esp_ble_gattc_write_char_descr", status);
535 }
536 break;
537 }
538
539 case ESP_GATTC_UNREG_FOR_NOTIFY_EVT: {
540 this->log_gattc_data_event_("UNREG_FOR_NOTIFY");
541 break;
542 }
543
544 default:
545 // Unknown events logged at VERBOSE to avoid UART delays during time-sensitive operations
546 ESP_LOGV(TAG, "[%d] [%s] Event %d", this->connection_index_, this->address_str_, event);
547 break;
548 }
549 return true;
550}
551
552// clients can't call defer() directly since it's protected.
553void BLEClientBase::run_later(std::function<void()> &&f) { // NOLINT
554 this->defer(std::move(f));
555}
556
557void BLEClientBase::gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) {
558 switch (event) {
559 // This event is sent by the server when it requests security
560 case ESP_GAP_BLE_SEC_REQ_EVT:
561 if (!this->check_addr(param->ble_security.auth_cmpl.bd_addr))
562 return;
563 ESP_LOGV(TAG, "[%d] [%s] ESP_GAP_BLE_SEC_REQ_EVT %x", this->connection_index_, this->address_str_, event);
564 esp_ble_gap_security_rsp(param->ble_security.ble_req.bd_addr, true);
565 break;
566 // This event is sent once authentication has completed
567 case ESP_GAP_BLE_AUTH_CMPL_EVT:
568 if (!this->check_addr(param->ble_security.auth_cmpl.bd_addr))
569 return;
570 char addr_str[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
571 format_mac_addr_upper(param->ble_security.auth_cmpl.bd_addr, addr_str);
572 ESP_LOGI(TAG, "[%d] [%s] auth complete addr: %s", this->connection_index_, this->address_str_, addr_str);
573 if (!param->ble_security.auth_cmpl.success) {
574 this->log_error_("auth fail reason", param->ble_security.auth_cmpl.fail_reason);
575 } else {
576 this->paired_ = true;
577 ESP_LOGD(TAG, "[%d] [%s] auth success type = %d mode = %d", this->connection_index_, this->address_str_,
578 param->ble_security.auth_cmpl.addr_type, param->ble_security.auth_cmpl.auth_mode);
579 }
580 break;
581
582 // There are other events we'll want to implement at some point to support things like pass key
583 // https://github.com/espressif/esp-idf/blob/cba69dd088344ed9d26739f04736ae7a37541b3a/examples/bluetooth/bluedroid/ble/gatt_security_client/tutorial/Gatt_Security_Client_Example_Walkthrough.md
584 default:
585 break;
586 }
587}
588
589// Parse GATT values into a float for a sensor.
590// Ref: https://www.bluetooth.com/specifications/assigned-numbers/format-types/
591float BLEClientBase::parse_char_value(uint8_t *value, uint16_t length) {
592 // A length of one means a single octet value.
593 if (length == 0)
594 return 0;
595 if (length == 1)
596 return (float) ((uint8_t) value[0]);
597
598 switch (value[0]) {
599 case 0x1: // boolean.
600 case 0x2: // 2bit.
601 case 0x3: // nibble.
602 case 0x4: // uint8.
603 return (float) ((uint8_t) value[1]);
604 case 0x5: // uint12.
605 case 0x6: // uint16.
606 if (length > 2) {
607 return (float) encode_uint16(value[1], value[2]);
608 }
609 [[fallthrough]];
610 case 0x7: // uint24.
611 if (length > 3) {
612 return (float) encode_uint24(value[1], value[2], value[3]);
613 }
614 [[fallthrough]];
615 case 0x8: // uint32.
616 if (length > 4) {
617 return (float) encode_uint32(value[1], value[2], value[3], value[4]);
618 }
619 [[fallthrough]];
620 case 0xC: // int8.
621 return (float) ((int8_t) value[1]);
622 case 0xD: // int12.
623 case 0xE: // int16.
624 if (length > 2) {
625 return (float) ((int16_t) (value[1] << 8) + (int16_t) value[2]);
626 }
627 [[fallthrough]];
628 case 0xF: // int24.
629 if (length > 3) {
630 return (float) ((int32_t) (value[1] << 16) + (int32_t) (value[2] << 8) + (int32_t) (value[3]));
631 }
632 [[fallthrough]];
633 case 0x10: // int32.
634 if (length > 4) {
635 return (float) ((int32_t) (value[1] << 24) + (int32_t) (value[2] << 16) + (int32_t) (value[3] << 8) +
636 (int32_t) (value[4]));
637 }
638 }
639 ESP_LOGW(TAG, "[%d] [%s] Cannot parse characteristic value of type 0x%x length %d", this->connection_index_,
640 this->address_str_, value[0], length);
641 return NAN;
642}
643
644#ifdef USE_ESP32_BLE_DEVICE
646 for (auto *svc : this->services_) {
647 if (svc->uuid == uuid)
648 return svc;
649 }
650 return nullptr;
651}
652
653BLEService *BLEClientBase::get_service(uint16_t uuid) { return this->get_service(espbt::ESPBTUUID::from_uint16(uuid)); }
654
656 auto *svc = this->get_service(service);
657 if (svc == nullptr)
658 return nullptr;
659 return svc->get_characteristic(chr);
660}
661
662BLECharacteristic *BLEClientBase::get_characteristic(uint16_t service, uint16_t chr) {
663 return this->get_characteristic(espbt::ESPBTUUID::from_uint16(service), espbt::ESPBTUUID::from_uint16(chr));
664}
665
667 for (auto *svc : this->services_) {
668 if (!svc->parsed)
669 svc->parse_characteristics();
670 for (auto *chr : svc->characteristics) {
671 if (chr->handle == handle)
672 return chr;
673 }
674 }
675 return nullptr;
676}
677
679 auto *chr = this->get_characteristic(handle);
680 if (chr != nullptr) {
681 if (!chr->parsed)
682 chr->parse_descriptors();
683 for (auto &desc : chr->descriptors) {
684 if (desc->uuid.get_uuid().uuid.uuid16 == ESP_GATT_UUID_CHAR_CLIENT_CONFIG)
685 return desc;
686 }
687 }
688 return nullptr;
689}
690
692 auto *svc = this->get_service(service);
693 if (svc == nullptr)
694 return nullptr;
695 auto *ch = svc->get_characteristic(chr);
696 if (ch == nullptr)
697 return nullptr;
698 return ch->get_descriptor(descr);
699}
700
701BLEDescriptor *BLEClientBase::get_descriptor(uint16_t service, uint16_t chr, uint16_t descr) {
702 return this->get_descriptor(espbt::ESPBTUUID::from_uint16(service), espbt::ESPBTUUID::from_uint16(chr),
703 espbt::ESPBTUUID::from_uint16(descr));
704}
705
707 for (auto *svc : this->services_) {
708 if (!svc->parsed)
709 svc->parse_characteristics();
710 for (auto *chr : svc->characteristics) {
711 if (!chr->parsed)
712 chr->parse_descriptors();
713 for (auto *desc : chr->descriptors) {
714 if (desc->handle == handle)
715 return desc;
716 }
717 }
718 }
719 return nullptr;
720}
721#endif // USE_ESP32_BLE_DEVICE
722
723} // namespace esphome::esp32_ble_client
724
725#endif // USE_ESP32
uint8_t status
Definition bl0942.h:8
void mark_failed()
Mark this component as failed.
ESPDEPRECATED("Use const char* overload instead. Removed in 2026.7.0", "2026.1.0") void defer(const std voi defer)(const char *name, std::function< void()> &&f)
Defer a callback to the next loop() call.
Definition component.h:543
void enable_loop()
Enable this component's loop.
Definition component.h:246
void disable_loop()
Disable this component's loop.
ESPDEPRECATED("set_retry is deprecated and will be removed in 2026.8.0. Use set_timeout or set_interval instead.", "2026.2.0") void set_retry(const std uint32_t uint8_t std::function< RetryResult(uint8_t)> && f
Definition component.h:437
std::vector< BLEService * > services_
char address_str_[MAC_ADDRESS_PRETTY_BUFFER_SIZE]
void log_gattc_warning_(const char *operation, esp_gatt_status_t status)
void log_connection_params_(const char *param_type)
BLEDescriptor * get_descriptor(espbt::ESPBTUUID service, espbt::ESPBTUUID chr, espbt::ESPBTUUID descr)
void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) override
void set_state(espbt::ClientState st) override
esp_err_t update_conn_params_(uint16_t min_interval, uint16_t max_interval, uint16_t latency, uint16_t timeout, const char *param_type)
BLECharacteristic * get_characteristic(espbt::ESPBTUUID service, espbt::ESPBTUUID chr)
virtual void set_address(uint64_t address)
void set_idle_()
Transition to IDLE and reset conn_id — call when the connection is fully dead.
void run_later(std::function< void()> &&f)
virtual void on_disconnect_complete(esp_err_t reason)
Hook called once a connection has been fully torn down (after release_services() and set_idle_()),...
BLEService * get_service(espbt::ESPBTUUID uuid)
void set_disconnecting_()
Transition to DISCONNECTING and start the safety timeout.
bool gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) override
bool parse_device(const espbt::ESPBTDevice &device) override
float parse_char_value(uint8_t *value, uint16_t length)
BLEDescriptor * get_config_descriptor(uint16_t handle)
void set_conn_params_(uint16_t min_interval, uint16_t max_interval, uint16_t latency, uint16_t timeout, const char *param_type)
void print_bt_device_info(const ESPBTDevice &device)
void set_state_internal_(ClientState st)
Set state without IDLE handling - use for direct state transitions.
esp_ble_addr_type_t get_address_type() const
const LogString * message
Definition component.cpp:35
ESP32BLETracker * global_esp32_ble_tracker
const char * client_state_to_string(ClientState state)
ESP32BLE * global_ble
Definition ble.cpp:718
constexpr float AFTER_BLUETOOTH
Definition component.h:47
constexpr uint32_t encode_uint24(uint8_t byte1, uint8_t byte2, uint8_t byte3)
Encode a 24-bit value given three bytes in most to least significant byte order.
Definition helpers.h:863
constexpr uint32_t encode_uint32(uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4)
Encode a 32-bit value given four bytes in most to least significant byte order.
Definition helpers.h:867
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
Definition helpers.h:859
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:28
char * format_mac_addr_upper(const uint8_t *mac, char *output)
Format MAC address as XX:XX:XX:XX:XX:XX (uppercase, colon separators)
Definition helpers.h:1453
static void uint32_t
uint16_t length
Definition tt21100.cpp:0
spi_device_handle_t handle