200#ifdef CONFIG_ESP_HOSTED_ENABLE_BT_BLUEDROID
203#ifndef CONFIG_ESP_HOSTED_ENABLE_BT_BLUEDROID
204 if (esp_bt_controller_get_status() != ESP_BT_CONTROLLER_STATUS_ENABLED) {
206 if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_IDLE) {
207 esp_bt_controller_config_t cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
208 err = esp_bt_controller_init(&cfg);
210 ESP_LOGE(TAG,
"esp_bt_controller_init failed: %s", esp_err_to_name(err));
213 while (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_IDLE)
216 if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_INITED) {
217 err = esp_bt_controller_enable(ESP_BT_MODE_BLE);
219 ESP_LOGE(TAG,
"esp_bt_controller_enable failed: %s", esp_err_to_name(err));
223 if (esp_bt_controller_get_status() != ESP_BT_CONTROLLER_STATUS_ENABLED) {
224 ESP_LOGE(TAG,
"esp bt controller enable failed");
229 esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT);
231 if (esp_hosted_connect_to_slave() != ESP_OK) {
232 ESP_LOGE(TAG,
"Co-processor transport failed; BLE disabled");
239 esp_hosted_coprocessor_fwver_t fw_ver{};
240 if (esp_hosted_get_coprocessor_fwversion(&fw_ver) != ESP_OK) {
241 ESP_LOGE(TAG,
"Co-processor not responding; BLE disabled. Update its firmware with the esp32_hosted "
245 ESP_LOGD(TAG,
"Co-processor firmware %" PRIu32
".%" PRIu32
".%" PRIu32, fw_ver.major1, fw_ver.minor1, fw_ver.patch1);
247 if (esp_hosted_bt_controller_init() != ESP_OK) {
249 "BT controller init failed; co-processor firmware %" PRIu32
".%" PRIu32
".%" PRIu32
250 " may lack BT support. Update it with the esp32_hosted update component; BLE disabled",
251 fw_ver.major1, fw_ver.minor1, fw_ver.patch1);
255 if (esp_hosted_bt_controller_enable() != ESP_OK) {
257 "BT controller enable failed; co-processor firmware %" PRIu32
".%" PRIu32
".%" PRIu32
258 " may lack BT support. Update it with the esp32_hosted update component; BLE disabled",
259 fw_ver.major1, fw_ver.minor1, fw_ver.patch1);
263 hosted_hci_bluedroid_open();
265 esp_bluedroid_hci_driver_operations_t operations = {
266 .send = hosted_hci_bluedroid_send,
267 .check_send_available = hosted_hci_bluedroid_check_send_available,
268 .register_host_callback = hosted_hci_bluedroid_register_host_callback,
270 esp_bluedroid_attach_hci_driver(&operations);
273 err = esp_bluedroid_init();
275 ESP_LOGE(TAG,
"esp_bluedroid_init failed: %d", err);
278 err = esp_bluedroid_enable();
280 ESP_LOGE(TAG,
"esp_bluedroid_enable failed: %d", err);
284#ifdef ESPHOME_ESP32_BLE_GAP_EVENT_HANDLER_COUNT
287 ESP_LOGE(TAG,
"esp_ble_gap_register_callback failed: %d", err);
292#if defined(USE_ESP32_BLE_SERVER) && defined(ESPHOME_ESP32_BLE_GATTS_EVENT_HANDLER_COUNT)
295 ESP_LOGE(TAG,
"esp_ble_gatts_register_callback failed: %d", err);
300#if defined(USE_ESP32_BLE_CLIENT) && defined(ESPHOME_ESP32_BLE_GATTC_EVENT_HANDLER_COUNT)
303 ESP_LOGE(TAG,
"esp_ble_gattc_register_callback failed: %d", err);
310 constexpr size_t ble_name_max_len = 21;
311 char name_buffer[ble_name_max_len];
312 const char *device_name;
314 if (this->name_ !=
nullptr) {
317 constexpr size_t mac_address_suffix_len = 6;
318 char mac_addr[MAC_ADDRESS_BUFFER_SIZE];
320 const char *mac_suffix_ptr = mac_addr + mac_address_suffix_len;
322 mac_address_suffix_len);
323 device_name = name_buffer;
325 device_name = this->name_;
329 size_t name_len = app_name.
length();
333 memcpy(name_buffer, app_name.c_str(), 13);
334 memcpy(name_buffer + 13, app_name.c_str() + name_len - 7, 7);
336 memcpy(name_buffer, app_name.c_str(), 20);
338 name_buffer[20] =
'\0';
340 memcpy(name_buffer, app_name.c_str(), name_len + 1);
342 device_name = name_buffer;
345 err = esp_ble_gap_set_device_name(device_name);
347 ESP_LOGE(TAG,
"esp_ble_gap_set_device_name failed: %d", err);
351 err = esp_ble_gap_set_security_param(ESP_BLE_SM_IOCAP_MODE, &(this->io_cap_),
sizeof(esp_ble_io_cap_t));
353 ESP_LOGE(TAG,
"esp_ble_gap_set_security_param iocap_mode failed: %d", err);
357#ifdef ESPHOME_ESP32_BLE_EXTENDED_AUTH_PARAMS
358 if (this->max_key_size_) {
359 err = esp_ble_gap_set_security_param(ESP_BLE_SM_MAX_KEY_SIZE, &(this->max_key_size_),
sizeof(uint8_t));
361 ESP_LOGE(TAG,
"esp_ble_gap_set_security_param max_key_size failed: %d", err);
366 if (this->min_key_size_) {
367 err = esp_ble_gap_set_security_param(ESP_BLE_SM_MIN_KEY_SIZE, &(this->min_key_size_),
sizeof(uint8_t));
369 ESP_LOGE(TAG,
"esp_ble_gap_set_security_param min_key_size failed: %d", err);
374 if (this->auth_req_mode_) {
375 err = esp_ble_gap_set_security_param(ESP_BLE_SM_AUTHEN_REQ_MODE, &(this->auth_req_mode_.value()),
376 sizeof(esp_ble_auth_req_t));
378 ESP_LOGE(TAG,
"esp_ble_gap_set_security_param authen_req_mode failed: %d", err);
391#ifdef CONFIG_ESP_HOSTED_ENABLE_BT_BLUEDROID
395 esp_err_t err = esp_bluedroid_disable();
398 if (err != ESP_ERR_INVALID_STATE) {
399 ESP_LOGE(TAG,
"esp_bluedroid_disable failed: %d", err);
402 ESP_LOGD(TAG,
"Already disabled");
404 err = esp_bluedroid_deinit();
407 if (err != ESP_ERR_INVALID_STATE) {
408 ESP_LOGE(TAG,
"esp_bluedroid_deinit failed: %d", err);
411 ESP_LOGD(TAG,
"Already deinitialized");
414#ifndef CONFIG_ESP_HOSTED_ENABLE_BT_BLUEDROID
415 if (esp_bt_controller_get_status() != ESP_BT_CONTROLLER_STATUS_IDLE) {
417 if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_ENABLED) {
418 err = esp_bt_controller_disable();
420 ESP_LOGE(TAG,
"esp_bt_controller_disable failed: %s", esp_err_to_name(err));
423 while (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_ENABLED)
426 if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_INITED) {
427 err = esp_bt_controller_deinit();
429 ESP_LOGE(TAG,
"esp_bt_controller_deinit failed: %s", esp_err_to_name(err));
433 if (esp_bt_controller_get_status() != ESP_BT_CONTROLLER_STATUS_IDLE) {
434 ESP_LOGE(TAG,
"esp bt controller disable failed");
439 if (esp_hosted_bt_controller_disable() != ESP_OK) {
440 ESP_LOGE(TAG,
"esp_hosted_bt_controller_disable failed");
444 if (esp_hosted_bt_controller_deinit(
false) != ESP_OK) {
445 ESP_LOGE(TAG,
"esp_hosted_bt_controller_deinit failed");
449 hosted_hci_bluedroid_close();
456 this->loop_handle_state_transition_not_active_();
460#ifdef USE_ESP32_BLE_ADVERTISING
461 if (this->advertising_ !=
nullptr) {
462 this->advertising_->
loop();
466 BLEEvent *ble_event = this->ble_events_.pop();
467 if (ble_event ==
nullptr)
471 switch (ble_event->
type_) {
472#if defined(USE_ESP32_BLE_SERVER) && defined(ESPHOME_ESP32_BLE_GATTS_EVENT_HANDLER_COUNT)
474 esp_gatts_cb_event_t
event = ble_event->
event_.
gatts.gatts_event;
475 esp_gatt_if_t gatts_if = ble_event->
event_.
gatts.gatts_if;
476 esp_ble_gatts_cb_param_t *param = &ble_event->
event_.
gatts.gatts_param;
477 ESP_LOGV(TAG,
"gatts_event [esp_gatt_if: %d] - %d", gatts_if, event);
478 this->gatts_event_callbacks_.call(event, gatts_if, param);
482#if defined(USE_ESP32_BLE_CLIENT) && defined(ESPHOME_ESP32_BLE_GATTC_EVENT_HANDLER_COUNT)
484 esp_gattc_cb_event_t
event = ble_event->
event_.
gattc.gattc_event;
485 esp_gatt_if_t gattc_if = ble_event->
event_.
gattc.gattc_if;
486 esp_ble_gattc_cb_param_t *param = &ble_event->
event_.
gattc.gattc_param;
487 ESP_LOGV(TAG,
"gattc_event [esp_gatt_if: %d] - %d", gattc_if, event);
488 this->gattc_event_callbacks_.call(event, gattc_if, param);
493 esp_gap_ble_cb_event_t gap_event = ble_event->
event_.
gap.gap_event;
495 case ESP_GAP_BLE_SCAN_RESULT_EVT:
496#ifdef ESPHOME_ESP32_BLE_GAP_SCAN_EVENT_HANDLER_COUNT
497 this->gap_scan_event_callbacks_.call(ble_event->
scan_result());
502 GAP_SCAN_COMPLETE_EVENTS:
504 GAP_ADV_COMPLETE_EVENTS:
506 case ESP_GAP_BLE_READ_RSSI_COMPLETE_EVT:
509 ESP_LOGV(TAG,
"gap_event_handler - %d", gap_event);
510#ifdef ESPHOME_ESP32_BLE_GAP_EVENT_HANDLER_COUNT
512 esp_ble_gap_cb_param_t *param = NULL;
519 GAP_SCAN_COMPLETE_EVENTS:
520 param =
reinterpret_cast<esp_ble_gap_cb_param_t *
>(&ble_event->
event_.
gap.scan_complete);
524 GAP_ADV_COMPLETE_EVENTS:
525 param =
reinterpret_cast<esp_ble_gap_cb_param_t *
>(&ble_event->
event_.
gap.adv_complete);
528 case ESP_GAP_BLE_READ_RSSI_COMPLETE_EVT:
529 param =
reinterpret_cast<esp_ble_gap_cb_param_t *
>(&ble_event->
event_.
gap.read_rssi_complete);
533 param =
reinterpret_cast<esp_ble_gap_cb_param_t *
>(&ble_event->
event_.
gap.security);
541 this->gap_event_callbacks_.call(gap_event, param);
548 ESP_LOGW(TAG,
"Unhandled GAP event type in loop: %d", gap_event);
557 this->ble_event_pool_.release(ble_event);
558 }
while ((ble_event = this->ble_events_.pop()) !=
nullptr);
563 uint16_t dropped = this->ble_events_.get_and_reset_dropped_count();
565 ESP_LOGW(TAG,
"Dropped %u BLE events due to buffer overflow", dropped);
650 case ESP_GAP_BLE_SCAN_RESULT_EVT:
651 GAP_SCAN_COMPLETE_EVENTS:
653 GAP_ADV_COMPLETE_EVENTS:
655 case ESP_GAP_BLE_READ_RSSI_COMPLETE_EVT:
670 case ESP_GAP_BLE_UPDATE_CONN_PARAMS_EVT: {
671 if (param->update_conn_params.status != ESP_BT_STATUS_SUCCESS) {
672 char mac_s[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
674 ESP_LOGW(TAG,
"[%s] Conn param update failed, status=%d", mac_s, param->update_conn_params.status);
676#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
678 char mac_s[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
680 ESP_LOGV(TAG,
"[%s] Conn params updated: interval=%u (x1.25ms) latency=%u timeout=%u (x10ms)", mac_s,
681 param->update_conn_params.conn_int, param->update_conn_params.latency,
682 param->update_conn_params.timeout);
689 case ESP_GAP_BLE_SET_PKT_LENGTH_COMPLETE_EVT:
690 case ESP_GAP_BLE_PHY_UPDATE_COMPLETE_EVT:
691 case ESP_GAP_BLE_CHANNEL_SELECT_ALGORITHM_EVT:
692 case ESP_GAP_BLE_LOCAL_IR_EVT:
693 case ESP_GAP_BLE_LOCAL_ER_EVT:
699 ESP_LOGW(TAG,
"Ignoring unexpected GAP event type: %d", event);