182#ifdef CONFIG_ESP_HOSTED_ENABLE_BT_BLUEDROID
185#ifndef CONFIG_ESP_HOSTED_ENABLE_BT_BLUEDROID
186 if (esp_bt_controller_get_status() != ESP_BT_CONTROLLER_STATUS_ENABLED) {
188 if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_IDLE) {
189 esp_bt_controller_config_t cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
190 err = esp_bt_controller_init(&cfg);
192 ESP_LOGE(TAG,
"esp_bt_controller_init failed: %s", esp_err_to_name(err));
195 while (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_IDLE)
198 if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_INITED) {
199 err = esp_bt_controller_enable(ESP_BT_MODE_BLE);
201 ESP_LOGE(TAG,
"esp_bt_controller_enable failed: %s", esp_err_to_name(err));
205 if (esp_bt_controller_get_status() != ESP_BT_CONTROLLER_STATUS_ENABLED) {
206 ESP_LOGE(TAG,
"esp bt controller enable failed");
211 esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT);
213 if (esp_hosted_connect_to_slave() != ESP_OK) {
214 ESP_LOGE(TAG,
"Co-processor transport failed; BLE disabled");
221 esp_hosted_coprocessor_fwver_t fw_ver{};
222 if (esp_hosted_get_coprocessor_fwversion(&fw_ver) != ESP_OK) {
223 ESP_LOGE(TAG,
"Co-processor not responding; BLE disabled. Update its firmware with the esp32_hosted "
227 ESP_LOGD(TAG,
"Co-processor firmware %" PRIu32
".%" PRIu32
".%" PRIu32, fw_ver.major1, fw_ver.minor1, fw_ver.patch1);
229 if (esp_hosted_bt_controller_init() != ESP_OK) {
231 "BT controller init failed; co-processor firmware %" PRIu32
".%" PRIu32
".%" PRIu32
232 " may lack BT support. Update it with the esp32_hosted update component; BLE disabled",
233 fw_ver.major1, fw_ver.minor1, fw_ver.patch1);
237 if (esp_hosted_bt_controller_enable() != ESP_OK) {
239 "BT controller enable failed; co-processor firmware %" PRIu32
".%" PRIu32
".%" PRIu32
240 " may lack BT support. Update it with the esp32_hosted update component; BLE disabled",
241 fw_ver.major1, fw_ver.minor1, fw_ver.patch1);
245 hosted_hci_bluedroid_open();
247 esp_bluedroid_hci_driver_operations_t operations = {
248 .send = hosted_hci_bluedroid_send,
249 .check_send_available = hosted_hci_bluedroid_check_send_available,
250 .register_host_callback = hosted_hci_bluedroid_register_host_callback,
252 esp_bluedroid_attach_hci_driver(&operations);
255 err = esp_bluedroid_init();
257 ESP_LOGE(TAG,
"esp_bluedroid_init failed: %d", err);
260 err = esp_bluedroid_enable();
262 ESP_LOGE(TAG,
"esp_bluedroid_enable failed: %d", err);
266#ifdef ESPHOME_ESP32_BLE_GAP_EVENT_HANDLER_COUNT
269 ESP_LOGE(TAG,
"esp_ble_gap_register_callback failed: %d", err);
274#if defined(USE_ESP32_BLE_SERVER) && defined(ESPHOME_ESP32_BLE_GATTS_EVENT_HANDLER_COUNT)
277 ESP_LOGE(TAG,
"esp_ble_gatts_register_callback failed: %d", err);
282#if defined(USE_ESP32_BLE_CLIENT) && defined(ESPHOME_ESP32_BLE_GATTC_EVENT_HANDLER_COUNT)
285 ESP_LOGE(TAG,
"esp_ble_gattc_register_callback failed: %d", err);
292 constexpr size_t ble_name_max_len = 21;
293 char name_buffer[ble_name_max_len];
294 const char *device_name;
296 if (this->name_ !=
nullptr) {
299 constexpr size_t mac_address_suffix_len = 6;
300 char mac_addr[MAC_ADDRESS_BUFFER_SIZE];
302 const char *mac_suffix_ptr = mac_addr + mac_address_suffix_len;
304 mac_address_suffix_len);
305 device_name = name_buffer;
307 device_name = this->name_;
311 size_t name_len = app_name.
length();
315 memcpy(name_buffer, app_name.c_str(), 13);
316 memcpy(name_buffer + 13, app_name.c_str() + name_len - 7, 7);
318 memcpy(name_buffer, app_name.c_str(), 20);
320 name_buffer[20] =
'\0';
322 memcpy(name_buffer, app_name.c_str(), name_len + 1);
324 device_name = name_buffer;
327 err = esp_ble_gap_set_device_name(device_name);
329 ESP_LOGE(TAG,
"esp_ble_gap_set_device_name failed: %d", err);
333 err = esp_ble_gap_set_security_param(ESP_BLE_SM_IOCAP_MODE, &(this->io_cap_),
sizeof(esp_ble_io_cap_t));
335 ESP_LOGE(TAG,
"esp_ble_gap_set_security_param iocap_mode failed: %d", err);
339#ifdef ESPHOME_ESP32_BLE_EXTENDED_AUTH_PARAMS
340 if (this->max_key_size_) {
341 err = esp_ble_gap_set_security_param(ESP_BLE_SM_MAX_KEY_SIZE, &(this->max_key_size_),
sizeof(uint8_t));
343 ESP_LOGE(TAG,
"esp_ble_gap_set_security_param max_key_size failed: %d", err);
348 if (this->min_key_size_) {
349 err = esp_ble_gap_set_security_param(ESP_BLE_SM_MIN_KEY_SIZE, &(this->min_key_size_),
sizeof(uint8_t));
351 ESP_LOGE(TAG,
"esp_ble_gap_set_security_param min_key_size failed: %d", err);
356 if (this->auth_req_mode_) {
357 err = esp_ble_gap_set_security_param(ESP_BLE_SM_AUTHEN_REQ_MODE, &(this->auth_req_mode_.value()),
358 sizeof(esp_ble_auth_req_t));
360 ESP_LOGE(TAG,
"esp_ble_gap_set_security_param authen_req_mode failed: %d", err);
373#ifdef CONFIG_ESP_HOSTED_ENABLE_BT_BLUEDROID
377 esp_err_t err = esp_bluedroid_disable();
380 if (err != ESP_ERR_INVALID_STATE) {
381 ESP_LOGE(TAG,
"esp_bluedroid_disable failed: %d", err);
384 ESP_LOGD(TAG,
"Already disabled");
386 err = esp_bluedroid_deinit();
389 if (err != ESP_ERR_INVALID_STATE) {
390 ESP_LOGE(TAG,
"esp_bluedroid_deinit failed: %d", err);
393 ESP_LOGD(TAG,
"Already deinitialized");
396#ifndef CONFIG_ESP_HOSTED_ENABLE_BT_BLUEDROID
397 if (esp_bt_controller_get_status() != ESP_BT_CONTROLLER_STATUS_IDLE) {
399 if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_ENABLED) {
400 err = esp_bt_controller_disable();
402 ESP_LOGE(TAG,
"esp_bt_controller_disable failed: %s", esp_err_to_name(err));
405 while (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_ENABLED)
408 if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_INITED) {
409 err = esp_bt_controller_deinit();
411 ESP_LOGE(TAG,
"esp_bt_controller_deinit failed: %s", esp_err_to_name(err));
415 if (esp_bt_controller_get_status() != ESP_BT_CONTROLLER_STATUS_IDLE) {
416 ESP_LOGE(TAG,
"esp bt controller disable failed");
421 if (esp_hosted_bt_controller_disable() != ESP_OK) {
422 ESP_LOGE(TAG,
"esp_hosted_bt_controller_disable failed");
426 if (esp_hosted_bt_controller_deinit(
false) != ESP_OK) {
427 ESP_LOGE(TAG,
"esp_hosted_bt_controller_deinit failed");
431 hosted_hci_bluedroid_close();
438 this->loop_handle_state_transition_not_active_();
442#ifdef USE_ESP32_BLE_ADVERTISING
443 if (this->advertising_ !=
nullptr) {
444 this->advertising_->
loop();
448 BLEEvent *ble_event = this->ble_events_.pop();
449 if (ble_event ==
nullptr)
453 switch (ble_event->
type_) {
454#if defined(USE_ESP32_BLE_SERVER) && defined(ESPHOME_ESP32_BLE_GATTS_EVENT_HANDLER_COUNT)
456 esp_gatts_cb_event_t
event = ble_event->
event_.
gatts.gatts_event;
457 esp_gatt_if_t gatts_if = ble_event->
event_.
gatts.gatts_if;
458 esp_ble_gatts_cb_param_t *param = &ble_event->
event_.
gatts.gatts_param;
459 ESP_LOGV(TAG,
"gatts_event [esp_gatt_if: %d] - %d", gatts_if, event);
460 this->gatts_event_callbacks_.call(event, gatts_if, param);
464#if defined(USE_ESP32_BLE_CLIENT) && defined(ESPHOME_ESP32_BLE_GATTC_EVENT_HANDLER_COUNT)
466 esp_gattc_cb_event_t
event = ble_event->
event_.
gattc.gattc_event;
467 esp_gatt_if_t gattc_if = ble_event->
event_.
gattc.gattc_if;
468 esp_ble_gattc_cb_param_t *param = &ble_event->
event_.
gattc.gattc_param;
469 ESP_LOGV(TAG,
"gattc_event [esp_gatt_if: %d] - %d", gattc_if, event);
470 this->gattc_event_callbacks_.call(event, gattc_if, param);
475 esp_gap_ble_cb_event_t gap_event = ble_event->
event_.
gap.gap_event;
477 case ESP_GAP_BLE_SCAN_RESULT_EVT:
478#ifdef ESPHOME_ESP32_BLE_GAP_SCAN_EVENT_HANDLER_COUNT
479 this->gap_scan_event_callbacks_.call(ble_event->
scan_result());
484 GAP_SCAN_COMPLETE_EVENTS:
486 GAP_ADV_COMPLETE_EVENTS:
488 case ESP_GAP_BLE_READ_RSSI_COMPLETE_EVT:
491 ESP_LOGV(TAG,
"gap_event_handler - %d", gap_event);
492#ifdef ESPHOME_ESP32_BLE_GAP_EVENT_HANDLER_COUNT
494 esp_ble_gap_cb_param_t *param = NULL;
501 GAP_SCAN_COMPLETE_EVENTS:
502 param =
reinterpret_cast<esp_ble_gap_cb_param_t *
>(&ble_event->
event_.
gap.scan_complete);
506 GAP_ADV_COMPLETE_EVENTS:
507 param =
reinterpret_cast<esp_ble_gap_cb_param_t *
>(&ble_event->
event_.
gap.adv_complete);
510 case ESP_GAP_BLE_READ_RSSI_COMPLETE_EVT:
511 param =
reinterpret_cast<esp_ble_gap_cb_param_t *
>(&ble_event->
event_.
gap.read_rssi_complete);
515 param =
reinterpret_cast<esp_ble_gap_cb_param_t *
>(&ble_event->
event_.
gap.security);
523 this->gap_event_callbacks_.call(gap_event, param);
530 ESP_LOGW(TAG,
"Unhandled GAP event type in loop: %d", gap_event);
539 this->ble_event_pool_.release(ble_event);
540 }
while ((ble_event = this->ble_events_.pop()) !=
nullptr);
545 uint16_t dropped = this->ble_events_.get_and_reset_dropped_count();
547 ESP_LOGW(TAG,
"Dropped %u BLE events due to buffer overflow", dropped);