9#ifndef CONFIG_ESP_HOSTED_ENABLE_BT_BLUEDROID
13#include <esp_hosted.h>
14#include <esp_hosted_misc.h>
15#include <esp_hosted_bluedroid.h>
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>
34static const char *
const TAG =
"esp32_ble";
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
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
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
59 ESP_LOGE(TAG,
"BLE could not be prepared for configuration");
65 if (this->enable_on_boot_) {
86#ifdef USE_ESP32_BLE_ADVERTISING
91 this->advertising_->
start();
147 esp_err_t err = nvs_flash_init();
149 ESP_LOGE(TAG,
"nvs_flash_init failed: %d", err);
155#ifdef USE_ESP32_BLE_ADVERTISING
157 if (this->advertising_ !=
nullptr)
159 this->advertising_ =
new BLEAdvertising(this->advertising_cycle_time_);
169#ifndef CONFIG_ESP_HOSTED_ENABLE_BT_BLUEDROID
170 if (esp_bt_controller_get_status() != ESP_BT_CONTROLLER_STATUS_ENABLED) {
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);
176 ESP_LOGE(TAG,
"esp_bt_controller_init failed: %s", esp_err_to_name(err));
179 while (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_IDLE)
182 if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_INITED) {
183 err = esp_bt_controller_enable(ESP_BT_MODE_BLE);
185 ESP_LOGE(TAG,
"esp_bt_controller_enable failed: %s", esp_err_to_name(err));
189 if (esp_bt_controller_get_status() != ESP_BT_CONTROLLER_STATUS_ENABLED) {
190 ESP_LOGE(TAG,
"esp bt controller enable failed");
195 esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT);
197 esp_hosted_connect_to_slave();
199 if (esp_hosted_bt_controller_init() != ESP_OK) {
200 ESP_LOGW(TAG,
"esp_hosted_bt_controller_init failed");
204 if (esp_hosted_bt_controller_enable() != ESP_OK) {
205 ESP_LOGW(TAG,
"esp_hosted_bt_controller_enable failed");
209 hosted_hci_bluedroid_open();
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,
216 esp_bluedroid_attach_hci_driver(&operations);
219 err = esp_bluedroid_init();
221 ESP_LOGE(TAG,
"esp_bluedroid_init failed: %d", err);
224 err = esp_bluedroid_enable();
226 ESP_LOGE(TAG,
"esp_bluedroid_enable failed: %d", err);
230#ifdef ESPHOME_ESP32_BLE_GAP_EVENT_HANDLER_COUNT
233 ESP_LOGE(TAG,
"esp_ble_gap_register_callback failed: %d", err);
238#if defined(USE_ESP32_BLE_SERVER) && defined(ESPHOME_ESP32_BLE_GATTS_EVENT_HANDLER_COUNT)
241 ESP_LOGE(TAG,
"esp_ble_gatts_register_callback failed: %d", err);
246#if defined(USE_ESP32_BLE_CLIENT) && defined(ESPHOME_ESP32_BLE_GATTC_EVENT_HANDLER_COUNT)
249 ESP_LOGE(TAG,
"esp_ble_gattc_register_callback failed: %d", err);
256 constexpr size_t ble_name_max_len = 21;
257 char name_buffer[ble_name_max_len];
258 const char *device_name;
260 if (this->name_ !=
nullptr) {
263 constexpr size_t mac_address_len = 13;
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;
270 mac_address_suffix_len);
271 device_name = name_buffer;
273 device_name = this->name_;
277 size_t name_len = app_name.length();
281 memcpy(name_buffer, app_name.c_str(), 13);
282 memcpy(name_buffer + 13, app_name.c_str() + name_len - 7, 7);
284 memcpy(name_buffer, app_name.c_str(), 20);
286 name_buffer[20] =
'\0';
288 memcpy(name_buffer, app_name.c_str(), name_len + 1);
290 device_name = name_buffer;
293 err = esp_ble_gap_set_device_name(device_name);
295 ESP_LOGE(TAG,
"esp_ble_gap_set_device_name failed: %d", err);
299 err = esp_ble_gap_set_security_param(ESP_BLE_SM_IOCAP_MODE, &(this->io_cap_),
sizeof(uint8_t));
301 ESP_LOGE(TAG,
"esp_ble_gap_set_security_param failed: %d", err);
312 esp_err_t err = esp_bluedroid_disable();
315 if (err != ESP_ERR_INVALID_STATE) {
316 ESP_LOGE(TAG,
"esp_bluedroid_disable failed: %d", err);
319 ESP_LOGD(TAG,
"Already disabled");
321 err = esp_bluedroid_deinit();
324 if (err != ESP_ERR_INVALID_STATE) {
325 ESP_LOGE(TAG,
"esp_bluedroid_deinit failed: %d", err);
328 ESP_LOGD(TAG,
"Already deinitialized");
331#ifndef CONFIG_ESP_HOSTED_ENABLE_BT_BLUEDROID
332 if (esp_bt_controller_get_status() != ESP_BT_CONTROLLER_STATUS_IDLE) {
334 if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_ENABLED) {
335 err = esp_bt_controller_disable();
337 ESP_LOGE(TAG,
"esp_bt_controller_disable failed: %s", esp_err_to_name(err));
340 while (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_ENABLED)
343 if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_INITED) {
344 err = esp_bt_controller_deinit();
346 ESP_LOGE(TAG,
"esp_bt_controller_deinit failed: %s", esp_err_to_name(err));
350 if (esp_bt_controller_get_status() != ESP_BT_CONTROLLER_STATUS_IDLE) {
351 ESP_LOGE(TAG,
"esp bt controller disable failed");
356 if (esp_hosted_bt_controller_disable() != ESP_OK) {
357 ESP_LOGW(TAG,
"esp_hosted_bt_controller_disable failed");
361 if (esp_hosted_bt_controller_deinit(
false) != ESP_OK) {
362 ESP_LOGW(TAG,
"esp_hosted_bt_controller_deinit failed");
366 hosted_hci_bluedroid_close();
372 switch (this->state_) {
377 ESP_LOGD(TAG,
"Disabling");
379#ifdef ESPHOME_ESP32_BLE_BLE_STATUS_EVENT_HANDLER_COUNT
380 for (
auto *ble_event_handler : this->ble_status_event_handlers_) {
381 ble_event_handler->ble_before_disabled_event_handler();
386 ESP_LOGE(TAG,
"Could not be dismantled");
394 ESP_LOGD(TAG,
"Enabling");
398 ESP_LOGE(TAG,
"Could not be set up");
410 BLEEvent *ble_event = this->ble_events_.pop();
411 while (ble_event !=
nullptr) {
412 switch (ble_event->
type_) {
413#if defined(USE_ESP32_BLE_SERVER) && defined(ESPHOME_ESP32_BLE_GATTS_EVENT_HANDLER_COUNT)
415 esp_gatts_cb_event_t
event = ble_event->
event_.
gatts.gatts_event;
416 esp_gatt_if_t gatts_if = ble_event->
event_.
gatts.gatts_if;
417 esp_ble_gatts_cb_param_t *param = &ble_event->
event_.
gatts.gatts_param;
418 ESP_LOGV(TAG,
"gatts_event [esp_gatt_if: %d] - %d", gatts_if, event);
419 for (
auto *gatts_handler : this->gatts_event_handlers_) {
420 gatts_handler->gatts_event_handler(event, gatts_if, param);
425#if defined(USE_ESP32_BLE_CLIENT) && defined(ESPHOME_ESP32_BLE_GATTC_EVENT_HANDLER_COUNT)
427 esp_gattc_cb_event_t
event = ble_event->
event_.
gattc.gattc_event;
428 esp_gatt_if_t gattc_if = ble_event->
event_.
gattc.gattc_if;
429 esp_ble_gattc_cb_param_t *param = &ble_event->
event_.
gattc.gattc_param;
430 ESP_LOGV(TAG,
"gattc_event [esp_gatt_if: %d] - %d", gattc_if, event);
431 for (
auto *gattc_handler : this->gattc_event_handlers_) {
432 gattc_handler->gattc_event_handler(event, gattc_if, param);
438 esp_gap_ble_cb_event_t gap_event = ble_event->
event_.
gap.gap_event;
440 case ESP_GAP_BLE_SCAN_RESULT_EVT:
441#ifdef ESPHOME_ESP32_BLE_GAP_SCAN_EVENT_HANDLER_COUNT
443 for (
auto *scan_handler : this->gap_scan_event_handlers_) {
444 scan_handler->gap_scan_event_handler(ble_event->
scan_result());
450 GAP_SCAN_COMPLETE_EVENTS:
452 GAP_ADV_COMPLETE_EVENTS:
454 case ESP_GAP_BLE_READ_RSSI_COMPLETE_EVT:
457 ESP_LOGV(TAG,
"gap_event_handler - %d", gap_event);
458#ifdef ESPHOME_ESP32_BLE_GAP_EVENT_HANDLER_COUNT
460 esp_ble_gap_cb_param_t *param;
467 GAP_SCAN_COMPLETE_EVENTS:
468 param =
reinterpret_cast<esp_ble_gap_cb_param_t *
>(&ble_event->
event_.
gap.scan_complete);
472 GAP_ADV_COMPLETE_EVENTS:
473 param =
reinterpret_cast<esp_ble_gap_cb_param_t *
>(&ble_event->
event_.
gap.adv_complete);
476 case ESP_GAP_BLE_READ_RSSI_COMPLETE_EVT:
477 param =
reinterpret_cast<esp_ble_gap_cb_param_t *
>(&ble_event->
event_.
gap.read_rssi_complete);
481 param =
reinterpret_cast<esp_ble_gap_cb_param_t *
>(&ble_event->
event_.
gap.security);
489 for (
auto *gap_handler : this->gap_event_handlers_) {
490 gap_handler->gap_event_handler(gap_event, param);
498 ESP_LOGW(TAG,
"Unhandled GAP event type in loop: %d", gap_event);
507 this->ble_event_pool_.release(ble_event);
508 ble_event = this->ble_events_.pop();
510#ifdef USE_ESP32_BLE_ADVERTISING
511 if (this->advertising_ !=
nullptr) {
512 this->advertising_->
loop();
517 uint16_t dropped = this->ble_events_.get_and_reset_dropped_count();
519 ESP_LOGW(TAG,
"Dropped %u BLE events due to buffer overflow", dropped);
525 event->load_gap_event(e, p);
528#ifdef USE_ESP32_BLE_CLIENT
530 event->load_gattc_event(e, i, p);
534#ifdef USE_ESP32_BLE_SERVER
536 event->load_gatts_event(e, i, p);
543 if (event ==
nullptr) {
545 global_ble->ble_events_.increment_dropped_count();
559#ifdef USE_ESP32_BLE_SERVER
560template void enqueue_ble_event(esp_gatts_cb_event_t, esp_gatt_if_t, esp_ble_gatts_cb_param_t *);
562#ifdef USE_ESP32_BLE_CLIENT
563template void enqueue_ble_event(esp_gattc_cb_event_t, esp_gatt_if_t, esp_ble_gattc_cb_param_t *);
570 case ESP_GAP_BLE_SCAN_RESULT_EVT:
571 GAP_SCAN_COMPLETE_EVENTS:
573 GAP_ADV_COMPLETE_EVENTS:
575 case ESP_GAP_BLE_READ_RSSI_COMPLETE_EVT:
584#if defined(USE_SOCKET_SELECT_SUPPORT) && defined(USE_WAKE_LOOP_THREADSAFE)
590 case ESP_GAP_BLE_UPDATE_CONN_PARAMS_EVT:
591 case ESP_GAP_BLE_SET_PKT_LENGTH_COMPLETE_EVT:
592 case ESP_GAP_BLE_PHY_UPDATE_COMPLETE_EVT:
593 case ESP_GAP_BLE_CHANNEL_SELECT_ALGORITHM_EVT:
599 ESP_LOGW(TAG,
"Ignoring unexpected GAP event type: %d", event);
602#ifdef USE_ESP32_BLE_SERVER
604 esp_ble_gatts_cb_param_t *param) {
607#if defined(USE_SOCKET_SELECT_SUPPORT) && defined(USE_WAKE_LOOP_THREADSAFE)
613#ifdef USE_ESP32_BLE_CLIENT
615 esp_ble_gattc_cb_param_t *param) {
618#if defined(USE_SOCKET_SELECT_SUPPORT) && defined(USE_WAKE_LOOP_THREADSAFE)
627 const uint8_t *mac_address = esp_bt_dev_get_address();
629 const char *io_capability_s;
630 switch (this->io_cap_) {
632 io_capability_s =
"display_only";
635 io_capability_s =
"display_yes_no";
638 io_capability_s =
"keyboard_only";
640 case ESP_IO_CAP_NONE:
641 io_capability_s =
"none";
643 case ESP_IO_CAP_KBDISP:
644 io_capability_s =
"keyboard_display";
647 io_capability_s =
"invalid";
655 " IO Capability: %s",
656 mac_s, io_capability_s);
658 ESP_LOGCONFIG(TAG,
"Bluetooth stack is not enabled");
664 u |= uint64_t(
address[0] & 0xFF) << 40;
665 u |= uint64_t(
address[1] & 0xFF) << 32;
666 u |= uint64_t(
address[2] & 0xFF) << 24;
667 u |= uint64_t(
address[3] & 0xFF) << 16;
668 u |= uint64_t(
address[4] & 0xFF) << 8;
669 u |= uint64_t(
address[5] & 0xFF) << 0;
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 add_service_uuid(ESPBTUUID uuid)
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 remove_service_uuid(ESPBTUUID uuid)
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
BLEScanResult scan_result
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)
static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
static void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param)
friend void enqueue_ble_event(Args... args)
void advertising_register_raw_advertisement_callback(std::function< void(bool)> &&callback)
void advertising_set_service_data_and_name(std::span< const uint8_t > data, bool include_name)
void advertising_add_service_uuid(ESPBTUUID uuid)
void dump_config() override
static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param)
void advertising_set_service_data(const std::vector< uint8_t > &data)
float get_setup_priority() const override
void advertising_remove_service_uuid(ESPBTUUID uuid)
void load_ble_event(BLEEvent *event, esp_gap_ble_cb_event_t e, esp_ble_gap_cb_param_t *p)
@ BLE_COMPONENT_STATE_DISABLE
BLE should be disabled on next loop.
@ BLE_COMPONENT_STATE_OFF
Nothing has been initialized yet.
@ BLE_COMPONENT_STATE_ENABLE
BLE should be enabled on next loop.
@ BLE_COMPONENT_STATE_DISABLED
BLE is disabled.
@ BLE_COMPONENT_STATE_ACTIVE
BLE is active.
uint64_t ble_addr_to_uint64(const esp_bd_addr_t address)
void enqueue_ble_event(Args... args)
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.
void format_mac_addr_upper(const uint8_t *mac, char *output)
Format MAC address as XX:XX:XX:XX:XX:XX (uppercase, colon separators)
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.
void IRAM_ATTR HOT delay(uint32_t ms)
Application App
Global storage of Application pointer - only one Application can exist.