From 0a1f705fdabc0088b3738319a402cd56829ef9de Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 12 Dec 2022 14:57:12 -1000 Subject: [PATCH] Speed up bluetooth proxy connections when using esp-idf (#4171) --- esphome/components/bluetooth_proxy/__init__.py | 8 ++++++++ esphome/components/esp32/__init__.py | 2 +- esphome/components/esp32_ble_client/ble_client_base.cpp | 2 ++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/esphome/components/bluetooth_proxy/__init__.py b/esphome/components/bluetooth_proxy/__init__.py index 9c566c56a5..bec1579d8e 100644 --- a/esphome/components/bluetooth_proxy/__init__.py +++ b/esphome/components/bluetooth_proxy/__init__.py @@ -2,11 +2,13 @@ from esphome.components import esp32_ble_tracker, esp32_ble_client import esphome.config_validation as cv import esphome.codegen as cg from esphome.const import CONF_ACTIVE, CONF_ID +from esphome.components.esp32 import add_idf_sdkconfig_option AUTO_LOAD = ["esp32_ble_client", "esp32_ble_tracker"] DEPENDENCIES = ["api", "esp32"] CODEOWNERS = ["@jesserockz"] +CONF_CACHE_SERVICES = "cache_services" CONF_CONNECTIONS = "connections" MAX_CONNECTIONS = 3 @@ -47,6 +49,9 @@ CONFIG_SCHEMA = cv.All( { cv.GenerateID(): cv.declare_id(BluetoothProxy), cv.Optional(CONF_ACTIVE, default=False): cv.boolean, + cv.SplitDefault(CONF_CACHE_SERVICES, esp32_idf=True): cv.All( + cv.only_with_esp_idf, cv.boolean + ), cv.Optional(CONF_CONNECTIONS): cv.All( cv.ensure_list(CONNECTION_SCHEMA), cv.Length(min=1, max=MAX_CONNECTIONS), @@ -72,4 +77,7 @@ async def to_code(config): cg.add(var.register_connection(connection_var)) await esp32_ble_tracker.register_client(connection_var, connection_conf) + if config.get(CONF_CACHE_SERVICES): + add_idf_sdkconfig_option("CONFIG_BT_GATTC_CACHE_NVS_FLASH", True) + cg.add_define("USE_BLUETOOTH_PROXY") diff --git a/esphome/components/esp32/__init__.py b/esphome/components/esp32/__init__.py index 75dc68020f..3989b62842 100644 --- a/esphome/components/esp32/__init__.py +++ b/esphome/components/esp32/__init__.py @@ -398,11 +398,11 @@ spiffs, data, spiffs, 0x391000, 0x00F000 IDF_PARTITIONS_CSV = """\ # Name, Type, SubType, Offset, Size, Flags -nvs, data, nvs, , 0x4000, otadata, data, ota, , 0x2000, phy_init, data, phy, , 0x1000, app0, app, ota_0, , 0x1C0000, app1, app, ota_1, , 0x1C0000, +nvs, data, nvs, , 0x6d000, """ diff --git a/esphome/components/esp32_ble_client/ble_client_base.cpp b/esphome/components/esp32_ble_client/ble_client_base.cpp index 658f6c464e..dd6fc94127 100644 --- a/esphome/components/esp32_ble_client/ble_client_base.cpp +++ b/esphome/components/esp32_ble_client/ble_client_base.cpp @@ -95,7 +95,9 @@ void BLEClientBase::release_services() { for (auto &svc : this->services_) delete svc; // NOLINT(cppcoreguidelines-owning-memory) this->services_.clear(); +#ifndef CONFIG_BT_GATTC_CACHE_NVS_FLASH esp_ble_gattc_cache_clean(this->remote_bda_); +#endif } bool BLEClientBase::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t esp_gattc_if,