211#ifdef CONFIG_BT_CONTROLLER_DISABLED
214#ifndef CONFIG_BT_CONTROLLER_DISABLED
215 if (esp_bt_controller_get_status() != ESP_BT_CONTROLLER_STATUS_ENABLED) {
217 if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_IDLE) {
218 esp_bt_controller_config_t cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
219 err = esp_bt_controller_init(&cfg);
221 ESP_LOGE(TAG,
"esp_bt_controller_init failed: %s", esp_err_to_name(err));
224 while (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_IDLE)
227 if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_INITED) {
228 err = esp_bt_controller_enable(ESP_BT_MODE_BLE);
230 ESP_LOGE(TAG,
"esp_bt_controller_enable failed: %s", esp_err_to_name(err));
234 if (esp_bt_controller_get_status() != ESP_BT_CONTROLLER_STATUS_ENABLED) {
235 ESP_LOGE(TAG,
"esp bt controller enable failed");
240 esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT);
242 if (esp_hosted_connect_to_slave() != ESP_OK) {
243 ESP_LOGE(TAG,
"Co-processor transport failed; BLE disabled");
249 esp_hosted_coprocessor_fwver_t fw_ver{};
250 if (esp_hosted_get_coprocessor_fwversion(&fw_ver) != ESP_OK) {
251 ESP_LOGE(TAG,
"Co-processor not responding; BLE disabled. Update its firmware with the esp32_hosted "
255 ESP_LOGD(TAG,
"Co-processor firmware %" PRIu32
".%" PRIu32
".%" PRIu32, fw_ver.major1, fw_ver.minor1, fw_ver.patch1);
257#ifdef CONFIG_ESP_HOSTED_HOST_FEAT_BT
258 esp_hosted_bt_host_stack_cfg_t bt_cfg{};
259 bt_cfg.stack = ESP_HOSTED_BT_HOST_STACK_BLUEDROID;
260 bt_cfg.bring_up_controller =
true;
261 bt_cfg.controller_ready_timeout_ms = EH_BT_CTRL_DEFAULT_READY_TIMEOUT_MS;
262 if (esp_hosted_bt_host_stack_setup(&bt_cfg) != ESP_OK) {
264 "BT controller bring-up failed; co-processor firmware %" PRIu32
".%" PRIu32
".%" PRIu32
265 " may lack BT support. Update it with the esp32_hosted update component; BLE disabled",
266 fw_ver.major1, fw_ver.minor1, fw_ver.patch1);
270 if (esp_hosted_bt_controller_init() != ESP_OK) {
272 "BT controller init failed; co-processor firmware %" PRIu32
".%" PRIu32
".%" PRIu32
273 " may lack BT support. Update it with the esp32_hosted update component; BLE disabled",
274 fw_ver.major1, fw_ver.minor1, fw_ver.patch1);
278 if (esp_hosted_bt_controller_enable() != ESP_OK) {
280 "BT controller enable failed; co-processor firmware %" PRIu32
".%" PRIu32
".%" PRIu32
281 " may lack BT support. Update it with the esp32_hosted update component; BLE disabled",
282 fw_ver.major1, fw_ver.minor1, fw_ver.patch1);
286 hosted_hci_bluedroid_open();
288 esp_bluedroid_hci_driver_operations_t operations = {
289 .send = hosted_hci_bluedroid_send,
290 .check_send_available = hosted_hci_bluedroid_check_send_available,
291 .register_host_callback = hosted_hci_bluedroid_register_host_callback,
293 esp_bluedroid_attach_hci_driver(&operations);
297 err = esp_bluedroid_init();
299 ESP_LOGE(TAG,
"esp_bluedroid_init failed: %d", err);
302 err = esp_bluedroid_enable();
304 ESP_LOGE(TAG,
"esp_bluedroid_enable failed: %d", err);
308#ifdef ESPHOME_ESP32_BLE_GAP_EVENT_HANDLER_COUNT
311 ESP_LOGE(TAG,
"esp_ble_gap_register_callback failed: %d", err);
316#if defined(USE_ESP32_BLE_SERVER) && defined(ESPHOME_ESP32_BLE_GATTS_EVENT_HANDLER_COUNT)
319 ESP_LOGE(TAG,
"esp_ble_gatts_register_callback failed: %d", err);
324#if defined(USE_ESP32_BLE_CLIENT) && defined(ESPHOME_ESP32_BLE_GATTC_EVENT_HANDLER_COUNT)
327 ESP_LOGE(TAG,
"esp_ble_gattc_register_callback failed: %d", err);
334 constexpr size_t ble_name_max_len = 21;
335 char name_buffer[ble_name_max_len];
336 const char *device_name;
338 if (this->name_ !=
nullptr) {
341 constexpr size_t mac_address_suffix_len = 6;
342 char mac_addr[MAC_ADDRESS_BUFFER_SIZE];
344 const char *mac_suffix_ptr = mac_addr + mac_address_suffix_len;
346 mac_address_suffix_len);
347 device_name = name_buffer;
349 device_name = this->name_;
353 size_t name_len = app_name.
length();
357 memcpy(name_buffer, app_name.c_str(), 13);
358 memcpy(name_buffer + 13, app_name.c_str() + name_len - 7, 7);
360 memcpy(name_buffer, app_name.c_str(), 20);
362 name_buffer[20] =
'\0';
364 memcpy(name_buffer, app_name.c_str(), name_len + 1);
366 device_name = name_buffer;
369 err = esp_ble_gap_set_device_name(device_name);
371 ESP_LOGE(TAG,
"esp_ble_gap_set_device_name failed: %d", err);
375 err = esp_ble_gap_set_security_param(ESP_BLE_SM_IOCAP_MODE, &(this->io_cap_),
sizeof(esp_ble_io_cap_t));
377 ESP_LOGE(TAG,
"esp_ble_gap_set_security_param iocap_mode failed: %d", err);
381#ifdef ESPHOME_ESP32_BLE_EXTENDED_AUTH_PARAMS
382 if (this->max_key_size_) {
383 err = esp_ble_gap_set_security_param(ESP_BLE_SM_MAX_KEY_SIZE, &(this->max_key_size_),
sizeof(uint8_t));
385 ESP_LOGE(TAG,
"esp_ble_gap_set_security_param max_key_size failed: %d", err);
390 if (this->min_key_size_) {
391 err = esp_ble_gap_set_security_param(ESP_BLE_SM_MIN_KEY_SIZE, &(this->min_key_size_),
sizeof(uint8_t));
393 ESP_LOGE(TAG,
"esp_ble_gap_set_security_param min_key_size failed: %d", err);
398 if (this->auth_req_mode_) {
399 err = esp_ble_gap_set_security_param(ESP_BLE_SM_AUTHEN_REQ_MODE, &(this->auth_req_mode_.value()),
400 sizeof(esp_ble_auth_req_t));
402 ESP_LOGE(TAG,
"esp_ble_gap_set_security_param authen_req_mode failed: %d", err);
415#ifdef CONFIG_BT_CONTROLLER_DISABLED
419 esp_err_t err = esp_bluedroid_disable();
422 if (err != ESP_ERR_INVALID_STATE) {
423 ESP_LOGE(TAG,
"esp_bluedroid_disable failed: %d", err);
426 ESP_LOGD(TAG,
"Already disabled");
428 err = esp_bluedroid_deinit();
431 if (err != ESP_ERR_INVALID_STATE) {
432 ESP_LOGE(TAG,
"esp_bluedroid_deinit failed: %d", err);
435 ESP_LOGD(TAG,
"Already deinitialized");
438#ifndef CONFIG_BT_CONTROLLER_DISABLED
439 if (esp_bt_controller_get_status() != ESP_BT_CONTROLLER_STATUS_IDLE) {
441 if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_ENABLED) {
442 err = esp_bt_controller_disable();
444 ESP_LOGE(TAG,
"esp_bt_controller_disable failed: %s", esp_err_to_name(err));
447 while (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_ENABLED)
450 if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_INITED) {
451 err = esp_bt_controller_deinit();
453 ESP_LOGE(TAG,
"esp_bt_controller_deinit failed: %s", esp_err_to_name(err));
457 if (esp_bt_controller_get_status() != ESP_BT_CONTROLLER_STATUS_IDLE) {
458 ESP_LOGE(TAG,
"esp bt controller disable failed");
462#elif defined(CONFIG_ESP_HOSTED_HOST_FEAT_BT)
463 if (esp_hosted_bt_host_stack_teardown() != ESP_OK) {
464 ESP_LOGE(TAG,
"esp_hosted_bt_host_stack_teardown failed");
468 if (esp_hosted_bt_controller_disable() != ESP_OK) {
469 ESP_LOGE(TAG,
"esp_hosted_bt_controller_disable failed");
473 if (esp_hosted_bt_controller_deinit(
false) != ESP_OK) {
474 ESP_LOGE(TAG,
"esp_hosted_bt_controller_deinit failed");
478 hosted_hci_bluedroid_close();
485 this->loop_handle_state_transition_not_active_();
489#ifdef USE_ESP32_BLE_ADVERTISING
490 if (this->advertising_ !=
nullptr) {
491 this->advertising_->
loop();
495 BLEEvent *ble_event = this->ble_events_.pop();
496 if (ble_event ==
nullptr)
500 switch (ble_event->
type_) {
501#if defined(USE_ESP32_BLE_SERVER) && defined(ESPHOME_ESP32_BLE_GATTS_EVENT_HANDLER_COUNT)
503 esp_gatts_cb_event_t
event = ble_event->
event_.
gatts.gatts_event;
504 esp_gatt_if_t gatts_if = ble_event->
event_.
gatts.gatts_if;
505 esp_ble_gatts_cb_param_t *param = &ble_event->
event_.
gatts.gatts_param;
506 ESP_LOGV(TAG,
"gatts_event [esp_gatt_if: %d] - %d", gatts_if, event);
507 this->gatts_event_callbacks_.call(event, gatts_if, param);
511#if defined(USE_ESP32_BLE_CLIENT) && defined(ESPHOME_ESP32_BLE_GATTC_EVENT_HANDLER_COUNT)
513 esp_gattc_cb_event_t
event = ble_event->
event_.
gattc.gattc_event;
514 esp_gatt_if_t gattc_if = ble_event->
event_.
gattc.gattc_if;
515 esp_ble_gattc_cb_param_t *param = &ble_event->
event_.
gattc.gattc_param;
516 ESP_LOGV(TAG,
"gattc_event [esp_gatt_if: %d] - %d", gattc_if, event);
517 this->gattc_event_callbacks_.call(event, gattc_if, param);
522 esp_gap_ble_cb_event_t gap_event = ble_event->
event_.
gap.gap_event;
524 case ESP_GAP_BLE_SCAN_RESULT_EVT:
525#ifdef ESPHOME_ESP32_BLE_GAP_SCAN_EVENT_HANDLER_COUNT
526 this->gap_scan_event_callbacks_.call(ble_event->
scan_result());
531 GAP_SCAN_COMPLETE_EVENTS:
533 GAP_ADV_COMPLETE_EVENTS:
535 case ESP_GAP_BLE_READ_RSSI_COMPLETE_EVT:
538 ESP_LOGV(TAG,
"gap_event_handler - %d", gap_event);
539#ifdef ESPHOME_ESP32_BLE_GAP_EVENT_HANDLER_COUNT
541 esp_ble_gap_cb_param_t *param = NULL;
548 GAP_SCAN_COMPLETE_EVENTS:
549 param =
reinterpret_cast<esp_ble_gap_cb_param_t *
>(&ble_event->
event_.
gap.scan_complete);
553 GAP_ADV_COMPLETE_EVENTS:
554 param =
reinterpret_cast<esp_ble_gap_cb_param_t *
>(&ble_event->
event_.
gap.adv_complete);
557 case ESP_GAP_BLE_READ_RSSI_COMPLETE_EVT:
558 param =
reinterpret_cast<esp_ble_gap_cb_param_t *
>(&ble_event->
event_.
gap.read_rssi_complete);
562 param =
reinterpret_cast<esp_ble_gap_cb_param_t *
>(&ble_event->
event_.
gap.security);
570 this->gap_event_callbacks_.call(gap_event, param);
577 ESP_LOGW(TAG,
"Unhandled GAP event type in loop: %d", gap_event);
586 this->ble_event_pool_.release(ble_event);
587 }
while ((ble_event = this->ble_events_.pop()) !=
nullptr);
592 uint16_t dropped = this->ble_events_.get_and_reset_dropped_count();
594 ESP_LOGW(TAG,
"Dropped %u BLE events due to buffer overflow", dropped);
683 case ESP_GAP_BLE_SCAN_RESULT_EVT:
684 GAP_SCAN_COMPLETE_EVENTS:
686 GAP_ADV_COMPLETE_EVENTS:
688 case ESP_GAP_BLE_READ_RSSI_COMPLETE_EVT:
703 case ESP_GAP_BLE_UPDATE_CONN_PARAMS_EVT: {
704 if (param->update_conn_params.status != ESP_BT_STATUS_SUCCESS) {
705 char mac_s[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
707 ESP_LOGW(TAG,
"[%s] Conn param update failed, status=%d", mac_s, param->update_conn_params.status);
709#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
711 char mac_s[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
713 ESP_LOGV(TAG,
"[%s] Conn params updated: interval=%u (x1.25ms) latency=%u timeout=%u (x10ms)", mac_s,
714 param->update_conn_params.conn_int, param->update_conn_params.latency,
715 param->update_conn_params.timeout);
722 case ESP_GAP_BLE_SET_PKT_LENGTH_COMPLETE_EVT:
723 case ESP_GAP_BLE_PHY_UPDATE_COMPLETE_EVT:
724 case ESP_GAP_BLE_CHANNEL_SELECT_ALGORITHM_EVT:
725 case ESP_GAP_BLE_LOCAL_IR_EVT:
726 case ESP_GAP_BLE_LOCAL_ER_EVT:
732 ESP_LOGW(TAG,
"Ignoring unexpected GAP event type: %d", event);