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