ESPHome 2026.2.3
Loading...
Searching...
No Matches
ble.cpp
Go to the documentation of this file.
1#include "ble.h"
2
3#ifdef USE_ESP32
4
7#include "esphome/core/log.h"
8
9#ifndef CONFIG_ESP_HOSTED_ENABLE_BT_BLUEDROID
10#include <esp_bt.h>
11#else
12extern "C" {
13#include <esp_hosted.h>
14#include <esp_hosted_misc.h>
15#include <esp_hosted_bluedroid.h>
16}
17#endif
18#include <esp_bt_device.h>
19#include <esp_bt_main.h>
20#include <esp_gap_ble_api.h>
21#include <freertos/FreeRTOS.h>
22#include <freertos/FreeRTOSConfig.h>
23#include <freertos/task.h>
24#include <nvs_flash.h>
25
26#ifdef USE_ARDUINO
27// Prevent Arduino from releasing BT memory at startup (esp32-hal-misc.c).
28// Without this, esp_bt_controller_init() fails with ESP_ERR_INVALID_STATE.
29extern "C" bool btInUse() { return true; } // NOLINT(readability-identifier-naming)
30#endif
31
33
34static const char *const TAG = "esp32_ble";
35
36// GAP event groups for deduplication across gap_event_handler and dispatch_gap_event_
37#define GAP_SCAN_COMPLETE_EVENTS \
38 case ESP_GAP_BLE_SCAN_PARAM_SET_COMPLETE_EVT: \
39 case ESP_GAP_BLE_SCAN_START_COMPLETE_EVT: \
40 case ESP_GAP_BLE_SCAN_STOP_COMPLETE_EVT
41
42#define GAP_ADV_COMPLETE_EVENTS \
43 case ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT: \
44 case ESP_GAP_BLE_SCAN_RSP_DATA_SET_COMPLETE_EVT: \
45 case ESP_GAP_BLE_ADV_DATA_RAW_SET_COMPLETE_EVT: \
46 case ESP_GAP_BLE_ADV_START_COMPLETE_EVT: \
47 case ESP_GAP_BLE_ADV_STOP_COMPLETE_EVT
48
49#define GAP_SECURITY_EVENTS \
50 case ESP_GAP_BLE_AUTH_CMPL_EVT: \
51 case ESP_GAP_BLE_SEC_REQ_EVT: \
52 case ESP_GAP_BLE_PASSKEY_NOTIF_EVT: \
53 case ESP_GAP_BLE_PASSKEY_REQ_EVT: \
54 case ESP_GAP_BLE_NC_REQ_EVT
55
57 global_ble = this;
58 if (!ble_pre_setup_()) {
59 ESP_LOGE(TAG, "BLE could not be prepared for configuration");
60 this->mark_failed();
61 return;
62 }
63
64 this->state_ = BLE_COMPONENT_STATE_DISABLED;
65 if (this->enable_on_boot_) {
66 this->enable();
67 }
68}
69
71 if (this->state_ != BLE_COMPONENT_STATE_DISABLED)
72 return;
73
74 this->state_ = BLE_COMPONENT_STATE_ENABLE;
75}
76
78 if (this->state_ == BLE_COMPONENT_STATE_DISABLED)
79 return;
80
81 this->state_ = BLE_COMPONENT_STATE_DISABLE;
82}
83
84bool ESP32BLE::is_active() { return this->state_ == BLE_COMPONENT_STATE_ACTIVE; }
85
86#ifdef USE_ESP32_BLE_ADVERTISING
88 this->advertising_init_();
89 if (!this->is_active())
90 return;
91 this->advertising_->start();
92}
93
94void ESP32BLE::advertising_set_service_data(const std::vector<uint8_t> &data) {
95 this->advertising_init_();
96 this->advertising_->set_service_data(data);
97 this->advertising_start();
98}
99
100void ESP32BLE::advertising_set_manufacturer_data(const std::vector<uint8_t> &data) {
101 this->advertising_init_();
102 this->advertising_->set_manufacturer_data(data);
103 this->advertising_start();
104}
105
106void ESP32BLE::advertising_set_service_data_and_name(std::span<const uint8_t> data, bool include_name) {
107 // This method atomically updates both service data and device name inclusion in BLE advertising.
108 // When include_name is true, the device name is included in the advertising packet making it
109 // visible to passive BLE scanners. When false, the name is only visible in scan response
110 // (requires active scanning). This atomic operation ensures we only restart advertising once
111 // when changing both properties, avoiding the brief gap that would occur with separate calls.
112
113 this->advertising_init_();
114
115 if (include_name) {
116 // When including name, clear service data first to avoid packet overflow
117 this->advertising_->set_service_data(std::span<const uint8_t>{});
118 this->advertising_->set_include_name(true);
119 } else {
120 // When including service data, clear name first to avoid packet overflow
121 this->advertising_->set_include_name(false);
122 this->advertising_->set_service_data(data);
123 }
124
125 this->advertising_start();
126}
127
128void ESP32BLE::advertising_register_raw_advertisement_callback(std::function<void(bool)> &&callback) {
129 this->advertising_init_();
130 this->advertising_->register_raw_advertisement_callback(std::move(callback));
131}
132
134 this->advertising_init_();
135 this->advertising_->add_service_uuid(uuid);
136 this->advertising_start();
137}
138
140 this->advertising_init_();
141 this->advertising_->remove_service_uuid(uuid);
142 this->advertising_start();
143}
144#endif
145
147 esp_err_t err = nvs_flash_init();
148 if (err != ESP_OK) {
149 ESP_LOGE(TAG, "nvs_flash_init failed: %d", err);
150 return false;
151 }
152 return true;
153}
154
155#ifdef USE_ESP32_BLE_ADVERTISING
157 if (this->advertising_ != nullptr)
158 return;
159 this->advertising_ = new BLEAdvertising(this->advertising_cycle_time_); // NOLINT(cppcoreguidelines-owning-memory)
160
161 this->advertising_->set_scan_response(true);
162 this->advertising_->set_min_preferred_interval(0x06);
163 this->advertising_->set_appearance(this->appearance_);
164}
165#endif
166
168 esp_err_t err;
169#ifndef CONFIG_ESP_HOSTED_ENABLE_BT_BLUEDROID
170 if (esp_bt_controller_get_status() != ESP_BT_CONTROLLER_STATUS_ENABLED) {
171 // start bt controller
172 if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_IDLE) {
173 esp_bt_controller_config_t cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
174 err = esp_bt_controller_init(&cfg);
175 if (err != ESP_OK) {
176 ESP_LOGE(TAG, "esp_bt_controller_init failed: %s", esp_err_to_name(err));
177 return false;
178 }
179 while (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_IDLE)
180 ;
181 }
182 if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_INITED) {
183 err = esp_bt_controller_enable(ESP_BT_MODE_BLE);
184 if (err != ESP_OK) {
185 ESP_LOGE(TAG, "esp_bt_controller_enable failed: %s", esp_err_to_name(err));
186 return false;
187 }
188 }
189 if (esp_bt_controller_get_status() != ESP_BT_CONTROLLER_STATUS_ENABLED) {
190 ESP_LOGE(TAG, "esp bt controller enable failed");
191 return false;
192 }
193 }
194
195 esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT);
196#else
197 esp_hosted_connect_to_slave(); // NOLINT
198
199 if (esp_hosted_bt_controller_init() != ESP_OK) {
200 ESP_LOGW(TAG, "esp_hosted_bt_controller_init failed");
201 return false;
202 }
203
204 if (esp_hosted_bt_controller_enable() != ESP_OK) {
205 ESP_LOGW(TAG, "esp_hosted_bt_controller_enable failed");
206 return false;
207 }
208
209 hosted_hci_bluedroid_open();
210
211 esp_bluedroid_hci_driver_operations_t operations = {
212 .send = hosted_hci_bluedroid_send,
213 .check_send_available = hosted_hci_bluedroid_check_send_available,
214 .register_host_callback = hosted_hci_bluedroid_register_host_callback,
215 };
216 esp_bluedroid_attach_hci_driver(&operations);
217#endif
218
219 err = esp_bluedroid_init();
220 if (err != ESP_OK) {
221 ESP_LOGE(TAG, "esp_bluedroid_init failed: %d", err);
222 return false;
223 }
224 err = esp_bluedroid_enable();
225 if (err != ESP_OK) {
226 ESP_LOGE(TAG, "esp_bluedroid_enable failed: %d", err);
227 return false;
228 }
229
230#ifdef ESPHOME_ESP32_BLE_GAP_EVENT_HANDLER_COUNT
231 err = esp_ble_gap_register_callback(ESP32BLE::gap_event_handler);
232 if (err != ESP_OK) {
233 ESP_LOGE(TAG, "esp_ble_gap_register_callback failed: %d", err);
234 return false;
235 }
236#endif
237
238#if defined(USE_ESP32_BLE_SERVER) && defined(ESPHOME_ESP32_BLE_GATTS_EVENT_HANDLER_COUNT)
239 err = esp_ble_gatts_register_callback(ESP32BLE::gatts_event_handler);
240 if (err != ESP_OK) {
241 ESP_LOGE(TAG, "esp_ble_gatts_register_callback failed: %d", err);
242 return false;
243 }
244#endif
245
246#if defined(USE_ESP32_BLE_CLIENT) && defined(ESPHOME_ESP32_BLE_GATTC_EVENT_HANDLER_COUNT)
247 err = esp_ble_gattc_register_callback(ESP32BLE::gattc_event_handler);
248 if (err != ESP_OK) {
249 ESP_LOGE(TAG, "esp_ble_gattc_register_callback failed: %d", err);
250 return false;
251 }
252#endif
253
254 // BLE device names are limited to 20 characters
255 // Buffer: 20 chars + null terminator
256 constexpr size_t ble_name_max_len = 21;
257 char name_buffer[ble_name_max_len];
258 const char *device_name;
259
260 if (this->name_ != nullptr) {
262 // MAC address length: 12 hex chars + null terminator
263 constexpr size_t mac_address_len = 13;
264 // MAC address suffix length (last 6 characters of 12-char MAC address string)
265 constexpr size_t mac_address_suffix_len = 6;
266 char mac_addr[mac_address_len];
268 const char *mac_suffix_ptr = mac_addr + mac_address_suffix_len;
269 make_name_with_suffix_to(name_buffer, sizeof(name_buffer), this->name_, strlen(this->name_), '-', mac_suffix_ptr,
270 mac_address_suffix_len);
271 device_name = name_buffer;
272 } else {
273 device_name = this->name_;
274 }
275 } else {
276 const std::string &app_name = App.get_name();
277 size_t name_len = app_name.length();
278 if (name_len > 20) {
280 // Keep first 13 chars and last 7 chars (MAC suffix), remove middle
281 memcpy(name_buffer, app_name.c_str(), 13);
282 memcpy(name_buffer + 13, app_name.c_str() + name_len - 7, 7);
283 } else {
284 memcpy(name_buffer, app_name.c_str(), 20);
285 }
286 name_buffer[20] = '\0';
287 } else {
288 memcpy(name_buffer, app_name.c_str(), name_len + 1); // Include null terminator
289 }
290 device_name = name_buffer;
291 }
292
293 err = esp_ble_gap_set_device_name(device_name);
294 if (err != ESP_OK) {
295 ESP_LOGE(TAG, "esp_ble_gap_set_device_name failed: %d", err);
296 return false;
297 }
298
299 err = esp_ble_gap_set_security_param(ESP_BLE_SM_IOCAP_MODE, &(this->io_cap_), sizeof(uint8_t));
300 if (err != ESP_OK) {
301 ESP_LOGE(TAG, "esp_ble_gap_set_security_param failed: %d", err);
302 return false;
303 }
304
305 // BLE takes some time to be fully set up, 200ms should be more than enough
306 delay(200); // NOLINT
307
308 return true;
309}
310
312 esp_err_t err = esp_bluedroid_disable();
313 if (err != ESP_OK) {
314 // ESP_ERR_INVALID_STATE means Bluedroid is already disabled, which is fine
315 if (err != ESP_ERR_INVALID_STATE) {
316 ESP_LOGE(TAG, "esp_bluedroid_disable failed: %d", err);
317 return false;
318 }
319 ESP_LOGD(TAG, "Already disabled");
320 }
321 err = esp_bluedroid_deinit();
322 if (err != ESP_OK) {
323 // ESP_ERR_INVALID_STATE means Bluedroid is already deinitialized, which is fine
324 if (err != ESP_ERR_INVALID_STATE) {
325 ESP_LOGE(TAG, "esp_bluedroid_deinit failed: %d", err);
326 return false;
327 }
328 ESP_LOGD(TAG, "Already deinitialized");
329 }
330
331#ifndef CONFIG_ESP_HOSTED_ENABLE_BT_BLUEDROID
332 if (esp_bt_controller_get_status() != ESP_BT_CONTROLLER_STATUS_IDLE) {
333 // stop bt controller
334 if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_ENABLED) {
335 err = esp_bt_controller_disable();
336 if (err != ESP_OK) {
337 ESP_LOGE(TAG, "esp_bt_controller_disable failed: %s", esp_err_to_name(err));
338 return false;
339 }
340 while (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_ENABLED)
341 ;
342 }
343 if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_INITED) {
344 err = esp_bt_controller_deinit();
345 if (err != ESP_OK) {
346 ESP_LOGE(TAG, "esp_bt_controller_deinit failed: %s", esp_err_to_name(err));
347 return false;
348 }
349 }
350 if (esp_bt_controller_get_status() != ESP_BT_CONTROLLER_STATUS_IDLE) {
351 ESP_LOGE(TAG, "esp bt controller disable failed");
352 return false;
353 }
354 }
355#else
356 if (esp_hosted_bt_controller_disable() != ESP_OK) {
357 ESP_LOGW(TAG, "esp_hosted_bt_controller_disable failed");
358 return false;
359 }
360
361 if (esp_hosted_bt_controller_deinit(false) != ESP_OK) {
362 ESP_LOGW(TAG, "esp_hosted_bt_controller_deinit failed");
363 return false;
364 }
365
366 hosted_hci_bluedroid_close();
367#endif
368 return true;
369}
370
372 if (this->state_ != BLE_COMPONENT_STATE_ACTIVE) {
373 this->loop_handle_state_transition_not_active_();
374 return;
375 }
376
377 BLEEvent *ble_event = this->ble_events_.pop();
378 while (ble_event != nullptr) {
379 switch (ble_event->type_) {
380#if defined(USE_ESP32_BLE_SERVER) && defined(ESPHOME_ESP32_BLE_GATTS_EVENT_HANDLER_COUNT)
381 case BLEEvent::GATTS: {
382 esp_gatts_cb_event_t event = ble_event->event_.gatts.gatts_event;
383 esp_gatt_if_t gatts_if = ble_event->event_.gatts.gatts_if;
384 esp_ble_gatts_cb_param_t *param = &ble_event->event_.gatts.gatts_param;
385 ESP_LOGV(TAG, "gatts_event [esp_gatt_if: %d] - %d", gatts_if, event);
386 for (auto *gatts_handler : this->gatts_event_handlers_) {
387 gatts_handler->gatts_event_handler(event, gatts_if, param);
388 }
389 break;
390 }
391#endif
392#if defined(USE_ESP32_BLE_CLIENT) && defined(ESPHOME_ESP32_BLE_GATTC_EVENT_HANDLER_COUNT)
393 case BLEEvent::GATTC: {
394 esp_gattc_cb_event_t event = ble_event->event_.gattc.gattc_event;
395 esp_gatt_if_t gattc_if = ble_event->event_.gattc.gattc_if;
396 esp_ble_gattc_cb_param_t *param = &ble_event->event_.gattc.gattc_param;
397 ESP_LOGV(TAG, "gattc_event [esp_gatt_if: %d] - %d", gattc_if, event);
398 for (auto *gattc_handler : this->gattc_event_handlers_) {
399 gattc_handler->gattc_event_handler(event, gattc_if, param);
400 }
401 break;
402 }
403#endif
404 case BLEEvent::GAP: {
405 esp_gap_ble_cb_event_t gap_event = ble_event->event_.gap.gap_event;
406 switch (gap_event) {
407 case ESP_GAP_BLE_SCAN_RESULT_EVT:
408#ifdef ESPHOME_ESP32_BLE_GAP_SCAN_EVENT_HANDLER_COUNT
409 // Use the new scan event handler - no memcpy!
410 for (auto *scan_handler : this->gap_scan_event_handlers_) {
411 scan_handler->gap_scan_event_handler(ble_event->scan_result());
412 }
413#endif
414 break;
415
416 // Scan complete events
417 GAP_SCAN_COMPLETE_EVENTS:
418 // Advertising complete events
419 GAP_ADV_COMPLETE_EVENTS:
420 // RSSI complete event
421 case ESP_GAP_BLE_READ_RSSI_COMPLETE_EVT:
422 // Security events
423 GAP_SECURITY_EVENTS:
424 ESP_LOGV(TAG, "gap_event_handler - %d", gap_event);
425#ifdef ESPHOME_ESP32_BLE_GAP_EVENT_HANDLER_COUNT
426 {
427 esp_ble_gap_cb_param_t *param;
428 // clang-format off
429 switch (gap_event) {
430 // All three scan complete events have the same structure with just status
431 // The scan_complete struct matches ESP-IDF's layout exactly, so this reinterpret_cast is safe
432 // This is verified at compile-time by static_assert checks in ble_event.h
433 // The struct already contains our copy of the status (copied in BLEEvent constructor)
434 GAP_SCAN_COMPLETE_EVENTS:
435 param = reinterpret_cast<esp_ble_gap_cb_param_t *>(&ble_event->event_.gap.scan_complete);
436 break;
437
438 // All advertising complete events have the same structure with just status
439 GAP_ADV_COMPLETE_EVENTS:
440 param = reinterpret_cast<esp_ble_gap_cb_param_t *>(&ble_event->event_.gap.adv_complete);
441 break;
442
443 case ESP_GAP_BLE_READ_RSSI_COMPLETE_EVT:
444 param = reinterpret_cast<esp_ble_gap_cb_param_t *>(&ble_event->event_.gap.read_rssi_complete);
445 break;
446
447 GAP_SECURITY_EVENTS:
448 param = reinterpret_cast<esp_ble_gap_cb_param_t *>(&ble_event->event_.gap.security);
449 break;
450
451 default:
452 break;
453 }
454 // clang-format on
455 // Dispatch to all registered handlers
456 for (auto *gap_handler : this->gap_event_handlers_) {
457 gap_handler->gap_event_handler(gap_event, param);
458 }
459 }
460#endif
461 break;
462
463 default:
464 // Unknown/unhandled event
465 ESP_LOGW(TAG, "Unhandled GAP event type in loop: %d", gap_event);
466 break;
467 }
468 break;
469 }
470 default:
471 break;
472 }
473 // Return the event to the pool
474 this->ble_event_pool_.release(ble_event);
475 ble_event = this->ble_events_.pop();
476 }
477#ifdef USE_ESP32_BLE_ADVERTISING
478 if (this->advertising_ != nullptr) {
479 this->advertising_->loop();
480 }
481#endif
482
483 // Log dropped events periodically
484 uint16_t dropped = this->ble_events_.get_and_reset_dropped_count();
485 if (dropped > 0) {
486 ESP_LOGW(TAG, "Dropped %u BLE events due to buffer overflow", dropped);
487 }
488}
489
490void ESP32BLE::loop_handle_state_transition_not_active_() {
491 // Caller ensures state_ != ACTIVE
492 if (this->state_ == BLE_COMPONENT_STATE_DISABLE) {
493 ESP_LOGD(TAG, "Disabling");
494
495#ifdef ESPHOME_ESP32_BLE_BLE_STATUS_EVENT_HANDLER_COUNT
496 for (auto *ble_event_handler : this->ble_status_event_handlers_) {
497 ble_event_handler->ble_before_disabled_event_handler();
498 }
499#endif
500
501 if (!ble_dismantle_()) {
502 ESP_LOGE(TAG, "Could not be dismantled");
503 this->mark_failed();
504 return;
505 }
506 this->state_ = BLE_COMPONENT_STATE_DISABLED;
507 } else if (this->state_ == BLE_COMPONENT_STATE_ENABLE) {
508 ESP_LOGD(TAG, "Enabling");
509 this->state_ = BLE_COMPONENT_STATE_OFF;
510
511 if (!ble_setup_()) {
512 ESP_LOGE(TAG, "Could not be set up");
513 this->mark_failed();
514 return;
515 }
516
517 this->state_ = BLE_COMPONENT_STATE_ACTIVE;
518 }
519}
520
521// Helper function to load new event data based on type
522void load_ble_event(BLEEvent *event, esp_gap_ble_cb_event_t e, esp_ble_gap_cb_param_t *p) {
523 event->load_gap_event(e, p);
524}
525
526#ifdef USE_ESP32_BLE_CLIENT
527void load_ble_event(BLEEvent *event, esp_gattc_cb_event_t e, esp_gatt_if_t i, esp_ble_gattc_cb_param_t *p) {
528 event->load_gattc_event(e, i, p);
529}
530#endif
531
532#ifdef USE_ESP32_BLE_SERVER
533void load_ble_event(BLEEvent *event, esp_gatts_cb_event_t e, esp_gatt_if_t i, esp_ble_gatts_cb_param_t *p) {
534 event->load_gatts_event(e, i, p);
535}
536#endif
537
538template<typename... Args> void enqueue_ble_event(Args... args) {
539 // Allocate an event from the pool
540 BLEEvent *event = global_ble->ble_event_pool_.allocate();
541 if (event == nullptr) {
542 // No events available - queue is full or we're out of memory
543 global_ble->ble_events_.increment_dropped_count();
544 return;
545 }
546
547 // Load new event data (replaces previous event)
548 load_ble_event(event, args...);
549
550 // Push the event to the queue
551 global_ble->ble_events_.push(event);
552 // Push always succeeds because we're the only producer and the pool ensures we never exceed queue size
553}
554
555// Explicit template instantiations for the friend function
556template void enqueue_ble_event(esp_gap_ble_cb_event_t, esp_ble_gap_cb_param_t *);
557#ifdef USE_ESP32_BLE_SERVER
558template void enqueue_ble_event(esp_gatts_cb_event_t, esp_gatt_if_t, esp_ble_gatts_cb_param_t *);
559#endif
560#ifdef USE_ESP32_BLE_CLIENT
561template void enqueue_ble_event(esp_gattc_cb_event_t, esp_gatt_if_t, esp_ble_gattc_cb_param_t *);
562#endif
563
564void ESP32BLE::gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) {
565 switch (event) {
566 // Queue GAP events that components need to handle
567 // Scanning events - used by esp32_ble_tracker
568 case ESP_GAP_BLE_SCAN_RESULT_EVT:
569 GAP_SCAN_COMPLETE_EVENTS:
570 // Advertising events - used by esp32_ble_beacon and esp32_ble server
571 GAP_ADV_COMPLETE_EVENTS:
572 // Connection events - used by ble_client
573 case ESP_GAP_BLE_READ_RSSI_COMPLETE_EVT:
574 enqueue_ble_event(event, param);
575 return;
576
577 // Security events - used by ble_client and bluetooth_proxy
578 // These are rare but interactive (pairing/bonding), so notify immediately
579 GAP_SECURITY_EVENTS:
580 enqueue_ble_event(event, param);
581 // Wake up main loop to process security event immediately
582#if defined(USE_SOCKET_SELECT_SUPPORT) && defined(USE_WAKE_LOOP_THREADSAFE)
584#endif
585 return;
586
587 // Ignore these GAP events as they are not relevant for our use case
588 case ESP_GAP_BLE_UPDATE_CONN_PARAMS_EVT:
589 case ESP_GAP_BLE_SET_PKT_LENGTH_COMPLETE_EVT:
590 case ESP_GAP_BLE_PHY_UPDATE_COMPLETE_EVT: // BLE 5.0 PHY update complete
591 case ESP_GAP_BLE_CHANNEL_SELECT_ALGORITHM_EVT: // BLE 5.0 channel selection algorithm
592 return;
593
594 default:
595 break;
596 }
597 ESP_LOGW(TAG, "Ignoring unexpected GAP event type: %d", event);
598}
599
600#ifdef USE_ESP32_BLE_SERVER
601void ESP32BLE::gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if,
602 esp_ble_gatts_cb_param_t *param) {
603 enqueue_ble_event(event, gatts_if, param);
604 // Wake up main loop to process GATT event immediately
605#if defined(USE_SOCKET_SELECT_SUPPORT) && defined(USE_WAKE_LOOP_THREADSAFE)
607#endif
608}
609#endif
610
611#ifdef USE_ESP32_BLE_CLIENT
612void ESP32BLE::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
613 esp_ble_gattc_cb_param_t *param) {
614 enqueue_ble_event(event, gattc_if, param);
615 // Wake up main loop to process GATT event immediately
616#if defined(USE_SOCKET_SELECT_SUPPORT) && defined(USE_WAKE_LOOP_THREADSAFE)
618#endif
619}
620#endif
621
623
625 const uint8_t *mac_address = esp_bt_dev_get_address();
626 if (mac_address) {
627 const char *io_capability_s;
628 switch (this->io_cap_) {
629 case ESP_IO_CAP_OUT:
630 io_capability_s = "display_only";
631 break;
632 case ESP_IO_CAP_IO:
633 io_capability_s = "display_yes_no";
634 break;
635 case ESP_IO_CAP_IN:
636 io_capability_s = "keyboard_only";
637 break;
638 case ESP_IO_CAP_NONE:
639 io_capability_s = "none";
640 break;
641 case ESP_IO_CAP_KBDISP:
642 io_capability_s = "keyboard_display";
643 break;
644 default:
645 io_capability_s = "invalid";
646 break;
647 }
648 char mac_s[18];
649 format_mac_addr_upper(mac_address, mac_s);
650 ESP_LOGCONFIG(TAG,
651 "BLE:\n"
652 " MAC address: %s\n"
653 " IO Capability: %s",
654 mac_s, io_capability_s);
655 } else {
656 ESP_LOGCONFIG(TAG, "Bluetooth stack is not enabled");
657 }
658}
659
660uint64_t ble_addr_to_uint64(const esp_bd_addr_t address) {
661 uint64_t u = 0;
662 u |= uint64_t(address[0] & 0xFF) << 40;
663 u |= uint64_t(address[1] & 0xFF) << 32;
664 u |= uint64_t(address[2] & 0xFF) << 24;
665 u |= uint64_t(address[3] & 0xFF) << 16;
666 u |= uint64_t(address[4] & 0xFF) << 8;
667 u |= uint64_t(address[5] & 0xFF) << 0;
668 return u;
669}
670
671ESP32BLE *global_ble = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
672
673} // namespace esphome::esp32_ble
674
675#endif
uint8_t address
Definition bl0906.h:4
bool btInUse()
Definition ble.cpp:29
void wake_loop_threadsafe()
Wake the main event loop from a FreeRTOS task Thread-safe, can be called from task context to immedia...
bool is_name_add_mac_suffix_enabled() const
const std::string & get_name() const
Get the name of this Application set by pre_setup().
virtual void mark_failed()
Mark this component as failed.
void set_manufacturer_data(const std::vector< uint8_t > &data)
void set_scan_response(bool scan_response)
void set_include_name(bool include_name)
void set_min_preferred_interval(uint16_t interval)
void set_service_data(const std::vector< uint8_t > &data)
void register_raw_advertisement_callback(std::function< void(bool)> &&callback)
void set_appearance(uint16_t appearance)
union esphome::esp32_ble::BLEEvent::@78 event_
struct esphome::esp32_ble::BLEEvent::@78::gatts_event gatts
struct esphome::esp32_ble::BLEEvent::@78::gap_event gap
struct esphome::esp32_ble::BLEEvent::@78::gattc_event gattc
void advertising_set_manufacturer_data(const std::vector< uint8_t > &data)
Definition ble.cpp:100
static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
Definition ble.cpp:564
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:612
friend void enqueue_ble_event(Args... args)
Definition ble.cpp:538
void advertising_register_raw_advertisement_callback(std::function< void(bool)> &&callback)
Definition ble.cpp:128
void advertising_set_service_data_and_name(std::span< const uint8_t > data, bool include_name)
Definition ble.cpp:106
void advertising_add_service_uuid(ESPBTUUID uuid)
Definition ble.cpp:133
void dump_config() override
Definition ble.cpp:624
void loop() override
Definition ble.cpp:371
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:601
void advertising_set_service_data(const std::vector< uint8_t > &data)
Definition ble.cpp:94
float get_setup_priority() const override
Definition ble.cpp:622
void setup() override
Definition ble.cpp:56
void advertising_remove_service_uuid(ESPBTUUID uuid)
Definition ble.cpp:139
ESP32BLE * global_ble
Definition ble.cpp:671
void load_ble_event(BLEEvent *event, esp_gap_ble_cb_event_t e, esp_ble_gap_cb_param_t *p)
Definition ble.cpp:522
@ 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:660
void enqueue_ble_event(Args... args)
Definition ble.cpp:538
size_t make_name_with_suffix_to(char *buffer, size_t buffer_size, const char *name, size_t name_len, char sep, const char *suffix_ptr, size_t suffix_len)
Zero-allocation version: format name + separator + suffix directly into buffer.
Definition helpers.cpp:261
void get_mac_address_into_buffer(std::span< char, MAC_ADDRESS_BUFFER_SIZE > buf)
Get the device MAC address into the given buffer, in lowercase hex notation.
Definition helpers.cpp:813
void IRAM_ATTR HOT delay(uint32_t ms)
Definition core.cpp:26
Application App
Global storage of Application pointer - only one Application can exist.
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:1045