183#ifdef CONFIG_ESP_HOSTED_ENABLE_BT_BLUEDROID
186#ifndef CONFIG_ESP_HOSTED_ENABLE_BT_BLUEDROID
187 if (esp_bt_controller_get_status() != ESP_BT_CONTROLLER_STATUS_ENABLED) {
189 if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_IDLE) {
190 esp_bt_controller_config_t cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
191 err = esp_bt_controller_init(&cfg);
193 ESP_LOGE(TAG,
"esp_bt_controller_init failed: %s", esp_err_to_name(err));
196 while (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_IDLE)
199 if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_INITED) {
200 err = esp_bt_controller_enable(ESP_BT_MODE_BLE);
202 ESP_LOGE(TAG,
"esp_bt_controller_enable failed: %s", esp_err_to_name(err));
206 if (esp_bt_controller_get_status() != ESP_BT_CONTROLLER_STATUS_ENABLED) {
207 ESP_LOGE(TAG,
"esp bt controller enable failed");
212 esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT);
214 if (esp_hosted_connect_to_slave() != ESP_OK) {
215 ESP_LOGE(TAG,
"Co-processor transport failed; BLE disabled");
222 esp_hosted_coprocessor_fwver_t fw_ver{};
223 if (esp_hosted_get_coprocessor_fwversion(&fw_ver) != ESP_OK) {
224 ESP_LOGE(TAG,
"Co-processor not responding; BLE disabled. Update its firmware with the esp32_hosted "
228 ESP_LOGD(TAG,
"Co-processor firmware %" PRIu32
".%" PRIu32
".%" PRIu32, fw_ver.major1, fw_ver.minor1, fw_ver.patch1);
230 if (esp_hosted_bt_controller_init() != ESP_OK) {
232 "BT controller init failed; co-processor firmware %" PRIu32
".%" PRIu32
".%" PRIu32
233 " may lack BT support. Update it with the esp32_hosted update component; BLE disabled",
234 fw_ver.major1, fw_ver.minor1, fw_ver.patch1);
238 if (esp_hosted_bt_controller_enable() != ESP_OK) {
240 "BT controller enable failed; co-processor firmware %" PRIu32
".%" PRIu32
".%" PRIu32
241 " may lack BT support. Update it with the esp32_hosted update component; BLE disabled",
242 fw_ver.major1, fw_ver.minor1, fw_ver.patch1);
246 hosted_hci_bluedroid_open();
248 esp_bluedroid_hci_driver_operations_t operations = {
249 .send = hosted_hci_bluedroid_send,
250 .check_send_available = hosted_hci_bluedroid_check_send_available,
251 .register_host_callback = hosted_hci_bluedroid_register_host_callback,
253 esp_bluedroid_attach_hci_driver(&operations);
256 err = esp_bluedroid_init();
258 ESP_LOGE(TAG,
"esp_bluedroid_init failed: %d", err);
261 err = esp_bluedroid_enable();
263 ESP_LOGE(TAG,
"esp_bluedroid_enable failed: %d", err);
267#ifdef ESPHOME_ESP32_BLE_GAP_EVENT_HANDLER_COUNT
270 ESP_LOGE(TAG,
"esp_ble_gap_register_callback failed: %d", err);
275#if defined(USE_ESP32_BLE_SERVER) && defined(ESPHOME_ESP32_BLE_GATTS_EVENT_HANDLER_COUNT)
278 ESP_LOGE(TAG,
"esp_ble_gatts_register_callback failed: %d", err);
283#if defined(USE_ESP32_BLE_CLIENT) && defined(ESPHOME_ESP32_BLE_GATTC_EVENT_HANDLER_COUNT)
286 ESP_LOGE(TAG,
"esp_ble_gattc_register_callback failed: %d", err);
293 constexpr size_t ble_name_max_len = 21;
294 char name_buffer[ble_name_max_len];
295 const char *device_name;
297 if (this->name_ !=
nullptr) {
300 constexpr size_t mac_address_suffix_len = 6;
301 char mac_addr[MAC_ADDRESS_BUFFER_SIZE];
303 const char *mac_suffix_ptr = mac_addr + mac_address_suffix_len;
305 mac_address_suffix_len);
306 device_name = name_buffer;
308 device_name = this->name_;
312 size_t name_len = app_name.
length();
316 memcpy(name_buffer, app_name.c_str(), 13);
317 memcpy(name_buffer + 13, app_name.c_str() + name_len - 7, 7);
319 memcpy(name_buffer, app_name.c_str(), 20);
321 name_buffer[20] =
'\0';
323 memcpy(name_buffer, app_name.c_str(), name_len + 1);
325 device_name = name_buffer;
328 err = esp_ble_gap_set_device_name(device_name);
330 ESP_LOGE(TAG,
"esp_ble_gap_set_device_name failed: %d", err);
334 err = esp_ble_gap_set_security_param(ESP_BLE_SM_IOCAP_MODE, &(this->io_cap_),
sizeof(esp_ble_io_cap_t));
336 ESP_LOGE(TAG,
"esp_ble_gap_set_security_param iocap_mode failed: %d", err);
340#ifdef ESPHOME_ESP32_BLE_EXTENDED_AUTH_PARAMS
341 if (this->max_key_size_) {
342 err = esp_ble_gap_set_security_param(ESP_BLE_SM_MAX_KEY_SIZE, &(this->max_key_size_),
sizeof(uint8_t));
344 ESP_LOGE(TAG,
"esp_ble_gap_set_security_param max_key_size failed: %d", err);
349 if (this->min_key_size_) {
350 err = esp_ble_gap_set_security_param(ESP_BLE_SM_MIN_KEY_SIZE, &(this->min_key_size_),
sizeof(uint8_t));
352 ESP_LOGE(TAG,
"esp_ble_gap_set_security_param min_key_size failed: %d", err);
357 if (this->auth_req_mode_) {
358 err = esp_ble_gap_set_security_param(ESP_BLE_SM_AUTHEN_REQ_MODE, &(this->auth_req_mode_.value()),
359 sizeof(esp_ble_auth_req_t));
361 ESP_LOGE(TAG,
"esp_ble_gap_set_security_param authen_req_mode failed: %d", err);
374#ifdef CONFIG_ESP_HOSTED_ENABLE_BT_BLUEDROID
378 esp_err_t err = esp_bluedroid_disable();
381 if (err != ESP_ERR_INVALID_STATE) {
382 ESP_LOGE(TAG,
"esp_bluedroid_disable failed: %d", err);
385 ESP_LOGD(TAG,
"Already disabled");
387 err = esp_bluedroid_deinit();
390 if (err != ESP_ERR_INVALID_STATE) {
391 ESP_LOGE(TAG,
"esp_bluedroid_deinit failed: %d", err);
394 ESP_LOGD(TAG,
"Already deinitialized");
397#ifndef CONFIG_ESP_HOSTED_ENABLE_BT_BLUEDROID
398 if (esp_bt_controller_get_status() != ESP_BT_CONTROLLER_STATUS_IDLE) {
400 if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_ENABLED) {
401 err = esp_bt_controller_disable();
403 ESP_LOGE(TAG,
"esp_bt_controller_disable failed: %s", esp_err_to_name(err));
406 while (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_ENABLED)
409 if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_INITED) {
410 err = esp_bt_controller_deinit();
412 ESP_LOGE(TAG,
"esp_bt_controller_deinit failed: %s", esp_err_to_name(err));
416 if (esp_bt_controller_get_status() != ESP_BT_CONTROLLER_STATUS_IDLE) {
417 ESP_LOGE(TAG,
"esp bt controller disable failed");
422 if (esp_hosted_bt_controller_disable() != ESP_OK) {
423 ESP_LOGE(TAG,
"esp_hosted_bt_controller_disable failed");
427 if (esp_hosted_bt_controller_deinit(
false) != ESP_OK) {
428 ESP_LOGE(TAG,
"esp_hosted_bt_controller_deinit failed");
432 hosted_hci_bluedroid_close();
439 this->loop_handle_state_transition_not_active_();
443#ifdef USE_ESP32_BLE_ADVERTISING
444 if (this->advertising_ !=
nullptr) {
445 this->advertising_->
loop();
449 BLEEvent *ble_event = this->ble_events_.pop();
450 if (ble_event ==
nullptr)
454 switch (ble_event->
type_) {
455#if defined(USE_ESP32_BLE_SERVER) && defined(ESPHOME_ESP32_BLE_GATTS_EVENT_HANDLER_COUNT)
457 esp_gatts_cb_event_t
event = ble_event->
event_.
gatts.gatts_event;
458 esp_gatt_if_t gatts_if = ble_event->
event_.
gatts.gatts_if;
459 esp_ble_gatts_cb_param_t *param = &ble_event->
event_.
gatts.gatts_param;
460 ESP_LOGV(TAG,
"gatts_event [esp_gatt_if: %d] - %d", gatts_if, event);
461 this->gatts_event_callbacks_.call(event, gatts_if, param);
465#if defined(USE_ESP32_BLE_CLIENT) && defined(ESPHOME_ESP32_BLE_GATTC_EVENT_HANDLER_COUNT)
467 esp_gattc_cb_event_t
event = ble_event->
event_.
gattc.gattc_event;
468 esp_gatt_if_t gattc_if = ble_event->
event_.
gattc.gattc_if;
469 esp_ble_gattc_cb_param_t *param = &ble_event->
event_.
gattc.gattc_param;
470 ESP_LOGV(TAG,
"gattc_event [esp_gatt_if: %d] - %d", gattc_if, event);
471 this->gattc_event_callbacks_.call(event, gattc_if, param);
476 esp_gap_ble_cb_event_t gap_event = ble_event->
event_.
gap.gap_event;
478 case ESP_GAP_BLE_SCAN_RESULT_EVT:
479#ifdef ESPHOME_ESP32_BLE_GAP_SCAN_EVENT_HANDLER_COUNT
480 this->gap_scan_event_callbacks_.call(ble_event->
scan_result());
485 GAP_SCAN_COMPLETE_EVENTS:
487 GAP_ADV_COMPLETE_EVENTS:
489 case ESP_GAP_BLE_READ_RSSI_COMPLETE_EVT:
492 ESP_LOGV(TAG,
"gap_event_handler - %d", gap_event);
493#ifdef ESPHOME_ESP32_BLE_GAP_EVENT_HANDLER_COUNT
495 esp_ble_gap_cb_param_t *param = NULL;
502 GAP_SCAN_COMPLETE_EVENTS:
503 param =
reinterpret_cast<esp_ble_gap_cb_param_t *
>(&ble_event->
event_.
gap.scan_complete);
507 GAP_ADV_COMPLETE_EVENTS:
508 param =
reinterpret_cast<esp_ble_gap_cb_param_t *
>(&ble_event->
event_.
gap.adv_complete);
511 case ESP_GAP_BLE_READ_RSSI_COMPLETE_EVT:
512 param =
reinterpret_cast<esp_ble_gap_cb_param_t *
>(&ble_event->
event_.
gap.read_rssi_complete);
516 param =
reinterpret_cast<esp_ble_gap_cb_param_t *
>(&ble_event->
event_.
gap.security);
524 this->gap_event_callbacks_.call(gap_event, param);
531 ESP_LOGW(TAG,
"Unhandled GAP event type in loop: %d", gap_event);
540 this->ble_event_pool_.release(ble_event);
541 }
while ((ble_event = this->ble_events_.pop()) !=
nullptr);
546 uint16_t dropped = this->ble_events_.get_and_reset_dropped_count();
548 ESP_LOGW(TAG,
"Dropped %u BLE events due to buffer overflow", dropped);