mirror of
https://github.com/esphome/esphome.git
synced 2025-10-19 10:13:49 +01:00
[esp32_ble] Add support for hosted BLE (#11167)
This commit is contained in:
@@ -16,7 +16,9 @@
|
|||||||
|
|
||||||
#include "bluetooth_connection.h"
|
#include "bluetooth_connection.h"
|
||||||
|
|
||||||
|
#ifndef CONFIG_ESP_HOSTED_ENABLE_BT_BLUEDROID
|
||||||
#include <esp_bt.h>
|
#include <esp_bt.h>
|
||||||
|
#endif
|
||||||
#include <esp_bt_device.h>
|
#include <esp_bt_device.h>
|
||||||
|
|
||||||
namespace esphome::bluetooth_proxy {
|
namespace esphome::bluetooth_proxy {
|
||||||
|
@@ -387,6 +387,15 @@ def final_validation(config):
|
|||||||
max_connections = config.get(CONF_MAX_CONNECTIONS, DEFAULT_MAX_CONNECTIONS)
|
max_connections = config.get(CONF_MAX_CONNECTIONS, DEFAULT_MAX_CONNECTIONS)
|
||||||
validate_connection_slots(max_connections)
|
validate_connection_slots(max_connections)
|
||||||
|
|
||||||
|
# Check if hosted bluetooth is being used
|
||||||
|
if "esp32_hosted" in full_config:
|
||||||
|
add_idf_sdkconfig_option("CONFIG_BT_CLASSIC_ENABLED", False)
|
||||||
|
add_idf_sdkconfig_option("CONFIG_BT_BLE_ENABLED", True)
|
||||||
|
add_idf_sdkconfig_option("CONFIG_BT_BLUEDROID_ENABLED", True)
|
||||||
|
add_idf_sdkconfig_option("CONFIG_BT_CONTROLLER_DISABLED", True)
|
||||||
|
add_idf_sdkconfig_option("CONFIG_ESP_HOSTED_ENABLE_BT_BLUEDROID", True)
|
||||||
|
add_idf_sdkconfig_option("CONFIG_ESP_HOSTED_BLUEDROID_HCI_VHCI", True)
|
||||||
|
|
||||||
# Check if BLE Server is needed
|
# Check if BLE Server is needed
|
||||||
has_ble_server = "esp32_ble_server" in full_config
|
has_ble_server = "esp32_ble_server" in full_config
|
||||||
|
|
||||||
|
@@ -6,7 +6,15 @@
|
|||||||
#include "esphome/core/helpers.h"
|
#include "esphome/core/helpers.h"
|
||||||
#include "esphome/core/log.h"
|
#include "esphome/core/log.h"
|
||||||
|
|
||||||
|
#ifndef CONFIG_ESP_HOSTED_ENABLE_BT_BLUEDROID
|
||||||
#include <esp_bt.h>
|
#include <esp_bt.h>
|
||||||
|
#else
|
||||||
|
extern "C" {
|
||||||
|
#include <esp_hosted.h>
|
||||||
|
#include <esp_hosted_misc.h>
|
||||||
|
#include <esp_hosted_bluedroid.h>
|
||||||
|
}
|
||||||
|
#endif
|
||||||
#include <esp_bt_device.h>
|
#include <esp_bt_device.h>
|
||||||
#include <esp_bt_main.h>
|
#include <esp_bt_main.h>
|
||||||
#include <esp_gap_ble_api.h>
|
#include <esp_gap_ble_api.h>
|
||||||
@@ -136,6 +144,7 @@ void ESP32BLE::advertising_init_() {
|
|||||||
|
|
||||||
bool ESP32BLE::ble_setup_() {
|
bool ESP32BLE::ble_setup_() {
|
||||||
esp_err_t err;
|
esp_err_t err;
|
||||||
|
#ifndef CONFIG_ESP_HOSTED_ENABLE_BT_BLUEDROID
|
||||||
#ifdef USE_ARDUINO
|
#ifdef USE_ARDUINO
|
||||||
if (!btStart()) {
|
if (!btStart()) {
|
||||||
ESP_LOGE(TAG, "btStart failed: %d", esp_bt_controller_get_status());
|
ESP_LOGE(TAG, "btStart failed: %d", esp_bt_controller_get_status());
|
||||||
@@ -169,6 +178,28 @@ bool ESP32BLE::ble_setup_() {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT);
|
esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT);
|
||||||
|
#else
|
||||||
|
esp_hosted_connect_to_slave(); // NOLINT
|
||||||
|
|
||||||
|
if (esp_hosted_bt_controller_init() != ESP_OK) {
|
||||||
|
ESP_LOGW(TAG, "esp_hosted_bt_controller_init failed");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (esp_hosted_bt_controller_enable() != ESP_OK) {
|
||||||
|
ESP_LOGW(TAG, "esp_hosted_bt_controller_enable failed");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
hosted_hci_bluedroid_open();
|
||||||
|
|
||||||
|
esp_bluedroid_hci_driver_operations_t operations = {
|
||||||
|
.send = hosted_hci_bluedroid_send,
|
||||||
|
.check_send_available = hosted_hci_bluedroid_check_send_available,
|
||||||
|
.register_host_callback = hosted_hci_bluedroid_register_host_callback,
|
||||||
|
};
|
||||||
|
esp_bluedroid_attach_hci_driver(&operations);
|
||||||
|
#endif
|
||||||
|
|
||||||
err = esp_bluedroid_init();
|
err = esp_bluedroid_init();
|
||||||
if (err != ESP_OK) {
|
if (err != ESP_OK) {
|
||||||
@@ -257,6 +288,7 @@ bool ESP32BLE::ble_dismantle_() {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifndef CONFIG_ESP_HOSTED_ENABLE_BT_BLUEDROID
|
||||||
#ifdef USE_ARDUINO
|
#ifdef USE_ARDUINO
|
||||||
if (!btStop()) {
|
if (!btStop()) {
|
||||||
ESP_LOGE(TAG, "btStop failed: %d", esp_bt_controller_get_status());
|
ESP_LOGE(TAG, "btStop failed: %d", esp_bt_controller_get_status());
|
||||||
@@ -286,6 +318,19 @@ bool ESP32BLE::ble_dismantle_() {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
#else
|
||||||
|
if (esp_hosted_bt_controller_disable() != ESP_OK) {
|
||||||
|
ESP_LOGW(TAG, "esp_hosted_bt_controller_disable failed");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (esp_hosted_bt_controller_deinit(false) != ESP_OK) {
|
||||||
|
ESP_LOGW(TAG, "esp_hosted_bt_controller_deinit failed");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
hosted_hci_bluedroid_close();
|
||||||
#endif
|
#endif
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@@ -10,7 +10,9 @@
|
|||||||
#ifdef USE_ESP32
|
#ifdef USE_ESP32
|
||||||
#ifdef USE_ESP32_BLE_ADVERTISING
|
#ifdef USE_ESP32_BLE_ADVERTISING
|
||||||
|
|
||||||
|
#ifndef CONFIG_ESP_HOSTED_ENABLE_BT_BLUEDROID
|
||||||
#include <esp_bt.h>
|
#include <esp_bt.h>
|
||||||
|
#endif
|
||||||
#include <esp_gap_ble_api.h>
|
#include <esp_gap_ble_api.h>
|
||||||
#include <esp_gatts_api.h>
|
#include <esp_gatts_api.h>
|
||||||
|
|
||||||
|
@@ -3,7 +3,9 @@
|
|||||||
|
|
||||||
#ifdef USE_ESP32
|
#ifdef USE_ESP32
|
||||||
|
|
||||||
|
#ifndef CONFIG_ESP_HOSTED_ENABLE_BT_BLUEDROID
|
||||||
#include <esp_bt.h>
|
#include <esp_bt.h>
|
||||||
|
#endif
|
||||||
#include <esp_bt_main.h>
|
#include <esp_bt_main.h>
|
||||||
#include <esp_gap_ble_api.h>
|
#include <esp_gap_ble_api.h>
|
||||||
#include <freertos/FreeRTOS.h>
|
#include <freertos/FreeRTOS.h>
|
||||||
|
@@ -5,7 +5,9 @@
|
|||||||
|
|
||||||
#ifdef USE_ESP32
|
#ifdef USE_ESP32
|
||||||
|
|
||||||
|
#ifndef CONFIG_ESP_HOSTED_ENABLE_BT_BLUEDROID
|
||||||
#include <esp_bt.h>
|
#include <esp_bt.h>
|
||||||
|
#endif
|
||||||
#include <esp_gap_ble_api.h>
|
#include <esp_gap_ble_api.h>
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
|
@@ -10,7 +10,9 @@
|
|||||||
#include <nvs_flash.h>
|
#include <nvs_flash.h>
|
||||||
#include <freertos/FreeRTOSConfig.h>
|
#include <freertos/FreeRTOSConfig.h>
|
||||||
#include <esp_bt_main.h>
|
#include <esp_bt_main.h>
|
||||||
|
#ifndef CONFIG_ESP_HOSTED_ENABLE_BT_BLUEDROID
|
||||||
#include <esp_bt.h>
|
#include <esp_bt.h>
|
||||||
|
#endif
|
||||||
#include <freertos/task.h>
|
#include <freertos/task.h>
|
||||||
#include <esp_gap_ble_api.h>
|
#include <esp_gap_ble_api.h>
|
||||||
|
|
||||||
|
@@ -7,7 +7,9 @@
|
|||||||
#include "esphome/core/helpers.h"
|
#include "esphome/core/helpers.h"
|
||||||
#include "esphome/core/log.h"
|
#include "esphome/core/log.h"
|
||||||
|
|
||||||
|
#ifndef CONFIG_ESP_HOSTED_ENABLE_BT_BLUEDROID
|
||||||
#include <esp_bt.h>
|
#include <esp_bt.h>
|
||||||
|
#endif
|
||||||
#include <esp_bt_defs.h>
|
#include <esp_bt_defs.h>
|
||||||
#include <esp_bt_main.h>
|
#include <esp_bt_main.h>
|
||||||
#include <esp_gap_ble_api.h>
|
#include <esp_gap_ble_api.h>
|
||||||
@@ -845,6 +847,7 @@ void ESP32BLETracker::log_unexpected_state_(const char *operation, ScannerState
|
|||||||
|
|
||||||
#ifdef USE_ESP32_BLE_SOFTWARE_COEXISTENCE
|
#ifdef USE_ESP32_BLE_SOFTWARE_COEXISTENCE
|
||||||
void ESP32BLETracker::update_coex_preference_(bool force_ble) {
|
void ESP32BLETracker::update_coex_preference_(bool force_ble) {
|
||||||
|
#ifndef CONFIG_ESP_HOSTED_ENABLE_BT_BLUEDROID
|
||||||
if (force_ble && !this->coex_prefer_ble_) {
|
if (force_ble && !this->coex_prefer_ble_) {
|
||||||
ESP_LOGD(TAG, "Setting coexistence to Bluetooth to make connection.");
|
ESP_LOGD(TAG, "Setting coexistence to Bluetooth to make connection.");
|
||||||
this->coex_prefer_ble_ = true;
|
this->coex_prefer_ble_ = true;
|
||||||
@@ -854,6 +857,7 @@ void ESP32BLETracker::update_coex_preference_(bool force_ble) {
|
|||||||
this->coex_prefer_ble_ = false;
|
this->coex_prefer_ble_ = false;
|
||||||
esp_coex_preference_set(ESP_COEX_PREFER_BALANCE); // Reset to default
|
esp_coex_preference_set(ESP_COEX_PREFER_BALANCE); // Reset to default
|
||||||
}
|
}
|
||||||
|
#endif // CONFIG_ESP_HOSTED_ENABLE_BT_BLUEDROID
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
19
tests/components/bluetooth_proxy/test.esp32-p4-idf.yaml
Normal file
19
tests/components/bluetooth_proxy/test.esp32-p4-idf.yaml
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
<<: !include common.yaml
|
||||||
|
|
||||||
|
esp32_ble_tracker:
|
||||||
|
max_connections: 9
|
||||||
|
|
||||||
|
bluetooth_proxy:
|
||||||
|
active: true
|
||||||
|
connection_slots: 9
|
||||||
|
|
||||||
|
esp32_hosted:
|
||||||
|
active_high: true
|
||||||
|
variant: ESP32C6
|
||||||
|
reset_pin: GPIO54
|
||||||
|
cmd_pin: GPIO19
|
||||||
|
clk_pin: GPIO18
|
||||||
|
d0_pin: GPIO14
|
||||||
|
d1_pin: GPIO15
|
||||||
|
d2_pin: GPIO16
|
||||||
|
d3_pin: GPIO17
|
Reference in New Issue
Block a user