205#ifdef CONFIG_ESP_HOSTED_ENABLE_BT_BLUEDROID
208#ifndef CONFIG_ESP_HOSTED_ENABLE_BT_BLUEDROID
209 if (esp_bt_controller_get_status() != ESP_BT_CONTROLLER_STATUS_ENABLED) {
211 if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_IDLE) {
212 esp_bt_controller_config_t cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
213 err = esp_bt_controller_init(&cfg);
215 ESP_LOGE(TAG,
"esp_bt_controller_init failed: %s", esp_err_to_name(err));
218 while (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_IDLE)
221 if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_INITED) {
222 err = esp_bt_controller_enable(ESP_BT_MODE_BLE);
224 ESP_LOGE(TAG,
"esp_bt_controller_enable failed: %s", esp_err_to_name(err));
228 if (esp_bt_controller_get_status() != ESP_BT_CONTROLLER_STATUS_ENABLED) {
229 ESP_LOGE(TAG,
"esp bt controller enable failed");
234 esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT);
236 if (esp_hosted_connect_to_slave() != ESP_OK) {
237 ESP_LOGE(TAG,
"Co-processor transport failed; BLE disabled");
244 esp_hosted_coprocessor_fwver_t fw_ver{};
245 if (esp_hosted_get_coprocessor_fwversion(&fw_ver) != ESP_OK) {
246 ESP_LOGE(TAG,
"Co-processor not responding; BLE disabled. Update its firmware with the esp32_hosted "
250 ESP_LOGD(TAG,
"Co-processor firmware %" PRIu32
".%" PRIu32
".%" PRIu32, fw_ver.major1, fw_ver.minor1, fw_ver.patch1);
252 if (esp_hosted_bt_controller_init() != ESP_OK) {
254 "BT controller init failed; co-processor firmware %" PRIu32
".%" PRIu32
".%" PRIu32
255 " may lack BT support. Update it with the esp32_hosted update component; BLE disabled",
256 fw_ver.major1, fw_ver.minor1, fw_ver.patch1);
260 if (esp_hosted_bt_controller_enable() != ESP_OK) {
262 "BT controller enable failed; co-processor firmware %" PRIu32
".%" PRIu32
".%" PRIu32
263 " may lack BT support. Update it with the esp32_hosted update component; BLE disabled",
264 fw_ver.major1, fw_ver.minor1, fw_ver.patch1);
268 hosted_hci_bluedroid_open();
270 esp_bluedroid_hci_driver_operations_t operations = {
271 .send = hosted_hci_bluedroid_send,
272 .check_send_available = hosted_hci_bluedroid_check_send_available,
273 .register_host_callback = hosted_hci_bluedroid_register_host_callback,
275 esp_bluedroid_attach_hci_driver(&operations);
278 err = esp_bluedroid_init();
280 ESP_LOGE(TAG,
"esp_bluedroid_init failed: %d", err);
283 err = esp_bluedroid_enable();
285 ESP_LOGE(TAG,
"esp_bluedroid_enable failed: %d", err);
289#ifdef ESPHOME_ESP32_BLE_GAP_EVENT_HANDLER_COUNT
292 ESP_LOGE(TAG,
"esp_ble_gap_register_callback failed: %d", err);
297#if defined(USE_ESP32_BLE_SERVER) && defined(ESPHOME_ESP32_BLE_GATTS_EVENT_HANDLER_COUNT)
300 ESP_LOGE(TAG,
"esp_ble_gatts_register_callback failed: %d", err);
305#if defined(USE_ESP32_BLE_CLIENT) && defined(ESPHOME_ESP32_BLE_GATTC_EVENT_HANDLER_COUNT)
308 ESP_LOGE(TAG,
"esp_ble_gattc_register_callback failed: %d", err);
315 constexpr size_t ble_name_max_len = 21;
316 char name_buffer[ble_name_max_len];
317 const char *device_name;
319 if (this->name_ !=
nullptr) {
322 constexpr size_t mac_address_suffix_len = 6;
323 char mac_addr[MAC_ADDRESS_BUFFER_SIZE];
325 const char *mac_suffix_ptr = mac_addr + mac_address_suffix_len;
327 mac_address_suffix_len);
328 device_name = name_buffer;
330 device_name = this->name_;
334 size_t name_len = app_name.
length();
338 memcpy(name_buffer, app_name.c_str(), 13);
339 memcpy(name_buffer + 13, app_name.c_str() + name_len - 7, 7);
341 memcpy(name_buffer, app_name.c_str(), 20);
343 name_buffer[20] =
'\0';
345 memcpy(name_buffer, app_name.c_str(), name_len + 1);
347 device_name = name_buffer;
350 err = esp_ble_gap_set_device_name(device_name);
352 ESP_LOGE(TAG,
"esp_ble_gap_set_device_name failed: %d", err);
356 err = esp_ble_gap_set_security_param(ESP_BLE_SM_IOCAP_MODE, &(this->io_cap_),
sizeof(esp_ble_io_cap_t));
358 ESP_LOGE(TAG,
"esp_ble_gap_set_security_param iocap_mode failed: %d", err);
362#ifdef ESPHOME_ESP32_BLE_EXTENDED_AUTH_PARAMS
363 if (this->max_key_size_) {
364 err = esp_ble_gap_set_security_param(ESP_BLE_SM_MAX_KEY_SIZE, &(this->max_key_size_),
sizeof(uint8_t));
366 ESP_LOGE(TAG,
"esp_ble_gap_set_security_param max_key_size failed: %d", err);
371 if (this->min_key_size_) {
372 err = esp_ble_gap_set_security_param(ESP_BLE_SM_MIN_KEY_SIZE, &(this->min_key_size_),
sizeof(uint8_t));
374 ESP_LOGE(TAG,
"esp_ble_gap_set_security_param min_key_size failed: %d", err);
379 if (this->auth_req_mode_) {
380 err = esp_ble_gap_set_security_param(ESP_BLE_SM_AUTHEN_REQ_MODE, &(this->auth_req_mode_.value()),
381 sizeof(esp_ble_auth_req_t));
383 ESP_LOGE(TAG,
"esp_ble_gap_set_security_param authen_req_mode failed: %d", err);
396#ifdef CONFIG_ESP_HOSTED_ENABLE_BT_BLUEDROID
400 esp_err_t err = esp_bluedroid_disable();
403 if (err != ESP_ERR_INVALID_STATE) {
404 ESP_LOGE(TAG,
"esp_bluedroid_disable failed: %d", err);
407 ESP_LOGD(TAG,
"Already disabled");
409 err = esp_bluedroid_deinit();
412 if (err != ESP_ERR_INVALID_STATE) {
413 ESP_LOGE(TAG,
"esp_bluedroid_deinit failed: %d", err);
416 ESP_LOGD(TAG,
"Already deinitialized");
419#ifndef CONFIG_ESP_HOSTED_ENABLE_BT_BLUEDROID
420 if (esp_bt_controller_get_status() != ESP_BT_CONTROLLER_STATUS_IDLE) {
422 if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_ENABLED) {
423 err = esp_bt_controller_disable();
425 ESP_LOGE(TAG,
"esp_bt_controller_disable failed: %s", esp_err_to_name(err));
428 while (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_ENABLED)
431 if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_INITED) {
432 err = esp_bt_controller_deinit();
434 ESP_LOGE(TAG,
"esp_bt_controller_deinit failed: %s", esp_err_to_name(err));
438 if (esp_bt_controller_get_status() != ESP_BT_CONTROLLER_STATUS_IDLE) {
439 ESP_LOGE(TAG,
"esp bt controller disable failed");
444 if (esp_hosted_bt_controller_disable() != ESP_OK) {
445 ESP_LOGE(TAG,
"esp_hosted_bt_controller_disable failed");
449 if (esp_hosted_bt_controller_deinit(
false) != ESP_OK) {
450 ESP_LOGE(TAG,
"esp_hosted_bt_controller_deinit failed");
454 hosted_hci_bluedroid_close();
461 this->loop_handle_state_transition_not_active_();
465#ifdef USE_ESP32_BLE_ADVERTISING
466 if (this->advertising_ !=
nullptr) {
467 this->advertising_->
loop();
471 BLEEvent *ble_event = this->ble_events_.pop();
472 if (ble_event ==
nullptr)
476 switch (ble_event->
type_) {
477#if defined(USE_ESP32_BLE_SERVER) && defined(ESPHOME_ESP32_BLE_GATTS_EVENT_HANDLER_COUNT)
479 esp_gatts_cb_event_t
event = ble_event->
event_.
gatts.gatts_event;
480 esp_gatt_if_t gatts_if = ble_event->
event_.
gatts.gatts_if;
481 esp_ble_gatts_cb_param_t *param = &ble_event->
event_.
gatts.gatts_param;
482 ESP_LOGV(TAG,
"gatts_event [esp_gatt_if: %d] - %d", gatts_if, event);
483 this->gatts_event_callbacks_.call(event, gatts_if, param);
487#if defined(USE_ESP32_BLE_CLIENT) && defined(ESPHOME_ESP32_BLE_GATTC_EVENT_HANDLER_COUNT)
489 esp_gattc_cb_event_t
event = ble_event->
event_.
gattc.gattc_event;
490 esp_gatt_if_t gattc_if = ble_event->
event_.
gattc.gattc_if;
491 esp_ble_gattc_cb_param_t *param = &ble_event->
event_.
gattc.gattc_param;
492 ESP_LOGV(TAG,
"gattc_event [esp_gatt_if: %d] - %d", gattc_if, event);
493 this->gattc_event_callbacks_.call(event, gattc_if, param);
498 esp_gap_ble_cb_event_t gap_event = ble_event->
event_.
gap.gap_event;
500 case ESP_GAP_BLE_SCAN_RESULT_EVT:
501#ifdef ESPHOME_ESP32_BLE_GAP_SCAN_EVENT_HANDLER_COUNT
502 this->gap_scan_event_callbacks_.call(ble_event->
scan_result());
507 GAP_SCAN_COMPLETE_EVENTS:
509 GAP_ADV_COMPLETE_EVENTS:
511 case ESP_GAP_BLE_READ_RSSI_COMPLETE_EVT:
514 ESP_LOGV(TAG,
"gap_event_handler - %d", gap_event);
515#ifdef ESPHOME_ESP32_BLE_GAP_EVENT_HANDLER_COUNT
517 esp_ble_gap_cb_param_t *param = NULL;
524 GAP_SCAN_COMPLETE_EVENTS:
525 param =
reinterpret_cast<esp_ble_gap_cb_param_t *
>(&ble_event->
event_.
gap.scan_complete);
529 GAP_ADV_COMPLETE_EVENTS:
530 param =
reinterpret_cast<esp_ble_gap_cb_param_t *
>(&ble_event->
event_.
gap.adv_complete);
533 case ESP_GAP_BLE_READ_RSSI_COMPLETE_EVT:
534 param =
reinterpret_cast<esp_ble_gap_cb_param_t *
>(&ble_event->
event_.
gap.read_rssi_complete);
538 param =
reinterpret_cast<esp_ble_gap_cb_param_t *
>(&ble_event->
event_.
gap.security);
546 this->gap_event_callbacks_.call(gap_event, param);
553 ESP_LOGW(TAG,
"Unhandled GAP event type in loop: %d", gap_event);
562 this->ble_event_pool_.release(ble_event);
563 }
while ((ble_event = this->ble_events_.pop()) !=
nullptr);
568 uint16_t dropped = this->ble_events_.get_and_reset_dropped_count();
570 ESP_LOGW(TAG,
"Dropped %u BLE events due to buffer overflow", dropped);
659 case ESP_GAP_BLE_SCAN_RESULT_EVT:
660 GAP_SCAN_COMPLETE_EVENTS:
662 GAP_ADV_COMPLETE_EVENTS:
664 case ESP_GAP_BLE_READ_RSSI_COMPLETE_EVT:
679 case ESP_GAP_BLE_UPDATE_CONN_PARAMS_EVT: {
680 if (param->update_conn_params.status != ESP_BT_STATUS_SUCCESS) {
681 char mac_s[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
683 ESP_LOGW(TAG,
"[%s] Conn param update failed, status=%d", mac_s, param->update_conn_params.status);
685#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
687 char mac_s[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
689 ESP_LOGV(TAG,
"[%s] Conn params updated: interval=%u (x1.25ms) latency=%u timeout=%u (x10ms)", mac_s,
690 param->update_conn_params.conn_int, param->update_conn_params.latency,
691 param->update_conn_params.timeout);
698 case ESP_GAP_BLE_SET_PKT_LENGTH_COMPLETE_EVT:
699 case ESP_GAP_BLE_PHY_UPDATE_COMPLETE_EVT:
700 case ESP_GAP_BLE_CHANNEL_SELECT_ALGORITHM_EVT:
701 case ESP_GAP_BLE_LOCAL_IR_EVT:
702 case ESP_GAP_BLE_LOCAL_ER_EVT:
708 ESP_LOGW(TAG,
"Ignoring unexpected GAP event type: %d", event);