From a7597ac11e19c28f13e995464d5ff90305463e3d Mon Sep 17 00:00:00 2001 From: Zhao Wei Liang Date: Fri, 5 Dec 2025 09:51:40 +0800 Subject: [PATCH 01/12] fix(ble): fixed the assert issue when using tinycrypt on ESP32-C2 (cherry picked from commit ac5d14d9544a596fc600993e4e0783d3987b64d7) Co-authored-by: zhaoweiliang --- components/bt/controller/esp32c2/bt.c | 34 ++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/components/bt/controller/esp32c2/bt.c b/components/bt/controller/esp32c2/bt.c index c9b227a5f7..104503d8e2 100644 --- a/components/bt/controller/esp32c2/bt.c +++ b/components/bt/controller/esp32c2/bt.c @@ -232,6 +232,10 @@ static void esp_bt_ctrl_log_partition_get_and_erase_first_block(void); #if CONFIG_FREERTOS_USE_TICKLESS_IDLE static bool esp_bt_check_wakeup_by_bt(void); #endif // CONFIG_FREERTOS_USE_TICKLESS_IDLE +#if (CONFIG_BT_CONTROLLER_ONLY) && (CONFIG_BT_LE_SM_SC) && (!CONFIG_BT_LE_CRYPTO_STACK_MBEDTLS) +#include "tinycrypt/ecc.h" +static int ecc_rand_func(uint8_t *dst, unsigned int len); +#endif // (CONFIG_BT_CONTROLLER_ONLY) && (CONFIG_BT_LE_SM_SC) && (!CONFIG_BT_LE_CRYPTO_STACK_MBEDTLS) /* Local variable definition *************************************************************************** */ @@ -1024,6 +1028,9 @@ esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg) ESP_LOGW(NIMBLE_PORT_LOG_TAG, "hci transport init failed %d", ret); goto free_controller; } +#if (CONFIG_BT_CONTROLLER_ONLY) && (CONFIG_BT_LE_SM_SC) && (!CONFIG_BT_LE_CRYPTO_STACK_MBEDTLS) + uECC_set_rng(ecc_rand_func); +#endif // (CONFIG_BT_CONTROLLER_ONLY) && (CONFIG_BT_LE_SM_SC) && (!CONFIG_BT_LE_CRYPTO_STACK_MBEDTLS) return ESP_OK; free_controller: hci_transport_deinit(); @@ -1457,6 +1464,27 @@ static mbedtls_ecp_keypair keypair; #if CONFIG_BT_LE_SM_SC #include "tinycrypt/cmac_mode.h" #include "tinycrypt/ecc_dh.h" + + +/* Used by uECC to get random data */ +static int ecc_rand_func(uint8_t *dst, unsigned int len) +{ + int offset_cnt = 0; + uint8_t *u8ptr = dst; + uint64_t random_64 = 0; + + while(len > 0) { + random64 = (uint64_t)esp_random(); + random64 = (random64 << 32)| (uint64_t)esp_random();; + offset_cnt = len < sizeof(uint64_t) ? len : sizeof(uint64_t); + memcpy(u8ptr, &random64, offset_cnt); + len -= offset_cnt; + u8ptr += offset_cnt; + } + + return 1; +} + #endif // CONFIG_BT_LE_SM_SC #endif // CONFIG_BT_LE_CRYPTO_STACK_MBEDTLS @@ -1548,11 +1576,11 @@ exit: } #else - if (uECC_valid_public_key(pk, &curve_secp256r1) < 0) { + if (uECC_valid_public_key(pk, uECC_secp256r1()) < 0) { return BLE_SM_KEY_ERR; } - rc = uECC_shared_secret(pk, priv, dh, &curve_secp256r1); + rc = uECC_shared_secret(pk, priv, dh, uECC_secp256r1()); if (rc == TC_CRYPTO_FAIL) { return BLE_SM_KEY_ERR; } @@ -1628,7 +1656,7 @@ int ble_sm_alg_gen_key_pair(uint8_t *pub, uint8_t *priv) return BLE_SM_KEY_ERR; } #else - if (uECC_make_key(pk, priv, &curve_secp256r1) != TC_CRYPTO_SUCCESS) { + if (uECC_make_key(pk, priv, uECC_secp256r1()) != TC_CRYPTO_SUCCESS) { return BLE_SM_KEY_ERR; } #endif // CONFIG_BT_LE_CRYPTO_STACK_MBEDTLS From c42a382a705d227608985c3453c1f94ed5ea127d Mon Sep 17 00:00:00 2001 From: zhaoweiliang Date: Tue, 2 Dec 2025 19:09:08 +0800 Subject: [PATCH 02/12] change(ble): [AUTO_MR] Update lib_esp32h2 to faf10639 --- components/bt/controller/lib_esp32h2/esp32h2-bt-lib | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/bt/controller/lib_esp32h2/esp32h2-bt-lib b/components/bt/controller/lib_esp32h2/esp32h2-bt-lib index a568fa5a03..33a575696b 160000 --- a/components/bt/controller/lib_esp32h2/esp32h2-bt-lib +++ b/components/bt/controller/lib_esp32h2/esp32h2-bt-lib @@ -1 +1 @@ -Subproject commit a568fa5a03545dd133a301ccdbae99afa3692923 +Subproject commit 33a575696b811a255dfb68bbea1370ea5e95265f From 4eb392586fae6732eb1460bbf926b2628edae62c Mon Sep 17 00:00:00 2001 From: zhaoweiliang Date: Tue, 2 Dec 2025 19:09:08 +0800 Subject: [PATCH 03/12] change(ble): [AUTO_MR] Update lib_esp32c5 to faf10639 --- components/bt/controller/lib_esp32c5/esp32c5-bt-lib | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/bt/controller/lib_esp32c5/esp32c5-bt-lib b/components/bt/controller/lib_esp32c5/esp32c5-bt-lib index a5feba34e1..cc21d43f5c 160000 --- a/components/bt/controller/lib_esp32c5/esp32c5-bt-lib +++ b/components/bt/controller/lib_esp32c5/esp32c5-bt-lib @@ -1 +1 @@ -Subproject commit a5feba34e144c82374fb9818108d6efc5216dc1f +Subproject commit cc21d43f5c3c3203125a13329391ba3b3c5ab518 From fb5882d86c54b12508e0d5c6602061fca5700a90 Mon Sep 17 00:00:00 2001 From: zhaoweiliang Date: Tue, 2 Dec 2025 19:09:08 +0800 Subject: [PATCH 04/12] change(ble): [AUTO_MR] Update lib_esp32c6 to faf10639 --- components/bt/controller/lib_esp32c6/esp32c6-bt-lib | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/bt/controller/lib_esp32c6/esp32c6-bt-lib b/components/bt/controller/lib_esp32c6/esp32c6-bt-lib index cf7c287226..237c0ce2eb 160000 --- a/components/bt/controller/lib_esp32c6/esp32c6-bt-lib +++ b/components/bt/controller/lib_esp32c6/esp32c6-bt-lib @@ -1 +1 @@ -Subproject commit cf7c287226c2c8575dc1cb9896d54a14a18d1dd4 +Subproject commit 237c0ce2eb75becf4890d96897105174659e07b5 From eefc31f4ae45fa0942f43cc43683d7954ee3b803 Mon Sep 17 00:00:00 2001 From: zhaoweiliang Date: Fri, 28 Nov 2025 20:31:30 +0800 Subject: [PATCH 05/12] feat(ble): add CONFIG_BT_LE_CTRL_DL_ITVL_PHY_SYNC_EN config on ESP32-H2 --- components/bt/controller/esp32h2/Kconfig.in | 8 ++++ components/bt/controller/esp32h2/bt.c | 39 +++++++++++++++++-- components/bt/controller/esp32h2/esp_bt_cfg.h | 7 ++++ .../bt/include/esp32h2/include/esp_bt.h | 4 +- 4 files changed, 54 insertions(+), 4 deletions(-) diff --git a/components/bt/controller/esp32h2/Kconfig.in b/components/bt/controller/esp32h2/Kconfig.in index c3d7df55e0..47820f717c 100644 --- a/components/bt/controller/esp32h2/Kconfig.in +++ b/components/bt/controller/esp32h2/Kconfig.in @@ -989,3 +989,11 @@ config BT_LE_CTRL_SLV_FAST_RX_CONN_DATA_EN When this option is enabled, the Controller continues receiving PDUs In the next connection event instead of entering latency After a data packet is received. + +config BT_LE_CTRL_DL_ITVL_PHY_SYNC_EN + bool "Enables the automatic initiation of a data length update(Experimental)." + default n + help + When this option is enabled, the Controller automatically initiates a data length update + Using the appropriate data length parameters + When a PHY update or a connection interval update occurs. diff --git a/components/bt/controller/esp32h2/bt.c b/components/bt/controller/esp32h2/bt.c index 6ea4185a27..c51cd40c95 100644 --- a/components/bt/controller/esp32h2/bt.c +++ b/components/bt/controller/esp32h2/bt.c @@ -187,6 +187,7 @@ extern int r_ble_get_npl_element_info(esp_bt_controller_config_t *cfg, ble_npl_c extern char *ble_controller_get_compile_version(void); extern int esp_ble_register_bb_funcs(void); extern void esp_ble_unregister_bb_funcs(void); +extern bool esp_ble_controller_lib_check(void); extern uint32_t _bt_bss_start; extern uint32_t _bt_bss_end; extern uint32_t _bt_controller_bss_start; @@ -226,6 +227,10 @@ static void esp_bt_ctrl_log_partition_get_and_erase_first_block(void); #if CONFIG_FREERTOS_USE_TICKLESS_IDLE static bool esp_bt_check_wakeup_by_bt(void); #endif // CONFIG_FREERTOS_USE_TICKLESS_IDLE +#if (CONFIG_BT_CONTROLLER_ONLY) && (CONFIG_BT_LE_SM_SC) && (!CONFIG_BT_LE_CRYPTO_STACK_MBEDTLS) +#include "tinycrypt/ecc.h" +static int ecc_rand_func(uint8_t *dst, unsigned int len); +#endif // (CONFIG_BT_CONTROLLER_ONLY) && (CONFIG_BT_LE_SM_SC) && (!CONFIG_BT_LE_CRYPTO_STACK_MBEDTLS) /* Local variable definition *************************************************************************** */ @@ -1009,6 +1014,7 @@ static void ble_rtc_clk_init(esp_bt_controller_config_t *cfg) esp_bt_rtc_slow_clk_select(s_bt_lpclk_src); } + esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg) { uint8_t mac[6]; @@ -1144,6 +1150,13 @@ esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg) goto free_controller; } + if (!esp_ble_controller_lib_check()) { + ESP_LOGW(NIMBLE_PORT_LOG_TAG, "Controller lib version mismatch!"); + } + +#if (CONFIG_BT_CONTROLLER_ONLY) && (CONFIG_BT_LE_SM_SC) && (!CONFIG_BT_LE_CRYPTO_STACK_MBEDTLS) + uECC_set_rng(ecc_rand_func); +#endif // (CONFIG_BT_CONTROLLER_ONLY) && (CONFIG_BT_LE_SM_SC) && (!CONFIG_BT_LE_CRYPTO_STACK_MBEDTLS) return ESP_OK; free_controller: hci_transport_deinit(); @@ -1614,6 +1627,25 @@ static mbedtls_ecp_keypair keypair; #if CONFIG_BT_LE_SM_SC #include "tinycrypt/cmac_mode.h" #include "tinycrypt/ecc_dh.h" + +/* Used by uECC to get random data */ +static int ecc_rand_func(uint8_t *dst, unsigned int len) +{ + int offset_cnt = 0; + uint8_t *u8ptr = dst; + uint64_t random_64 = 0; + + while(len > 0) { + random64 = (uint64_t)esp_random(); + random64 = (random64 << 32)| (uint64_t)esp_random();; + offset_cnt = len < sizeof(uint64_t) ? len : sizeof(uint64_t); + memcpy(u8ptr, &random64, offset_cnt); + len -= offset_cnt; + u8ptr += offset_cnt; + } + + return 1; +} #endif // CONFIG_BT_LE_SM_SC #endif // CONFIG_BT_LE_CRYPTO_STACK_MBEDTLS @@ -1705,11 +1737,11 @@ exit: } #else - if (uECC_valid_public_key(pk, &curve_secp256r1) < 0) { + if (uECC_valid_public_key(pk, uECC_secp256r1()) < 0) { return BLE_SM_KEY_ERR; } - rc = uECC_shared_secret(pk, priv, dh, &curve_secp256r1); + rc = uECC_shared_secret(pk, priv, dh, uECC_secp256r1()); if (rc == TC_CRYPTO_FAIL) { return BLE_SM_KEY_ERR; } @@ -1785,7 +1817,7 @@ int ble_sm_alg_gen_key_pair(uint8_t *pub, uint8_t *priv) return BLE_SM_KEY_ERR; } #else - if (uECC_make_key(pk, priv, &curve_secp256r1) != TC_CRYPTO_SUCCESS) { + if (uECC_make_key(pk, priv, uECC_secp256r1()) != TC_CRYPTO_SUCCESS) { return BLE_SM_KEY_ERR; } #endif // CONFIG_BT_LE_CRYPTO_STACK_MBEDTLS @@ -1801,6 +1833,7 @@ int ble_sm_alg_gen_key_pair(uint8_t *pub, uint8_t *priv) #endif // CONFIG_BT_LE_SM_LEGACY || CONFIG_BT_LE_SM_SC #endif // (!CONFIG_BT_NIMBLE_ENABLED) && (CONFIG_BT_CONTROLLER_ENABLED) + #if CONFIG_BT_LE_DEBUG_REMAIN_SCENE_ENABLED #include "esp_gdbstub.h" #endif // CONFIG_BT_LE_DEBUG_REMAIN_SCENE_ENABLED diff --git a/components/bt/controller/esp32h2/esp_bt_cfg.h b/components/bt/controller/esp32h2/esp_bt_cfg.h index 4dbe2f2fe8..20f2ec7edb 100644 --- a/components/bt/controller/esp32h2/esp_bt_cfg.h +++ b/components/bt/controller/esp32h2/esp_bt_cfg.h @@ -215,6 +215,13 @@ extern "C" { #define DEFAULT_BT_LE_CTRL_SLV_FAST_RX_CONN_DATA_EN (0) #endif + +#if defined(CONFIG_BT_LE_CTRL_DL_ITVL_PHY_SYNC_EN) +#define DEFAULT_BT_LE_CTRL_DL_ITVL_PHY_SYNC_EN (CONFIG_BT_LE_CTRL_DL_ITVL_PHY_SYNC_EN) +#else +#define DEFAULT_BT_LE_CTRL_DL_ITVL_PHY_SYNC_EN (0) +#endif + #ifdef CONFIG_BT_LE_HCI_INTERFACE_USE_UART #define HCI_UART_EN CONFIG_BT_LE_HCI_INTERFACE_USE_UART #else diff --git a/components/bt/include/esp32h2/include/esp_bt.h b/components/bt/include/esp32h2/include/esp_bt.h index a171b52a6b..f3d06ac7c5 100644 --- a/components/bt/include/esp32h2/include/esp_bt.h +++ b/components/bt/include/esp32h2/include/esp_bt.h @@ -161,7 +161,7 @@ esp_err_t esp_ble_tx_power_set_enhanced(esp_ble_enhanced_power_type_t power_type */ esp_power_level_t esp_ble_tx_power_get_enhanced(esp_ble_enhanced_power_type_t power_type, uint16_t handle); -#define CONFIG_VERSION 0x20251104 +#define CONFIG_VERSION 0x20251125 #define CONFIG_MAGIC 0x5A5AA5A5 /** @@ -238,6 +238,7 @@ typedef struct { uint8_t conn_rsv_cnt; /*!< BLE conn state machine reserve count number */ uint8_t priority_level_cfg; /*!< The option for priority level configuration */ uint8_t slv_fst_rx_lat_en; /*!< The option for enabling slave fast PDU reception during latency. */ + uint8_t dl_itvl_phy_sync_en; /*!< The option for automatically initiate the data length update when phy update or connect interval update. */ uint32_t config_magic; /*!< Configuration magic value */ } esp_bt_controller_config_t; @@ -302,6 +303,7 @@ typedef struct { .conn_rsv_cnt = BLE_LL_CONN_SM_RESERVE_CNT_N, \ .priority_level_cfg = BT_LL_CTRL_PRIO_LVL_CFG, \ .slv_fst_rx_lat_en = DEFAULT_BT_LE_CTRL_SLV_FAST_RX_CONN_DATA_EN, \ + .dl_itvl_phy_sync_en = DEFAULT_BT_LE_CTRL_DL_ITVL_PHY_SYNC_EN, \ .config_magic = CONFIG_MAGIC, \ } From 0b89e031d8c734423741b7f4699ddaac10cec564 Mon Sep 17 00:00:00 2001 From: zhaoweiliang Date: Tue, 2 Dec 2025 20:07:34 +0800 Subject: [PATCH 06/12] feat(ble): add CONFIG_BT_LE_CTRL_DL_ITVL_PHY_SYNC_EN config on ESP32-C6 --- components/bt/controller/esp32c6/Kconfig.in | 8 ++++ components/bt/controller/esp32c6/bt.c | 44 ++++++++++++++++--- components/bt/controller/esp32c6/esp_bt_cfg.h | 8 +++- .../bt/include/esp32c6/include/esp_bt.h | 7 ++- 4 files changed, 57 insertions(+), 10 deletions(-) diff --git a/components/bt/controller/esp32c6/Kconfig.in b/components/bt/controller/esp32c6/Kconfig.in index bffab12e66..58fc31715d 100644 --- a/components/bt/controller/esp32c6/Kconfig.in +++ b/components/bt/controller/esp32c6/Kconfig.in @@ -985,3 +985,11 @@ config BT_LE_CTRL_SLV_FAST_RX_CONN_DATA_EN When this option is enabled, the Controller continues receiving PDUs In the next connection event instead of entering latency After a data packet is received. + +config BT_LE_CTRL_DL_ITVL_PHY_SYNC_EN + bool "Enables the automatic initiation of a data length update(Experimental)." + default n + help + When this option is enabled, the Controller automatically initiates a data length update + Using the appropriate data length parameters + When a PHY update or a connection interval update occurs. diff --git a/components/bt/controller/esp32c6/bt.c b/components/bt/controller/esp32c6/bt.c index a442f8d85c..de8d8f51fc 100644 --- a/components/bt/controller/esp32c6/bt.c +++ b/components/bt/controller/esp32c6/bt.c @@ -34,8 +34,8 @@ #include "os/endian.h" #include "esp_bt.h" -#include "esp_intr_alloc.h" #include "ble_priv.h" +#include "esp_intr_alloc.h" #include "esp_sleep.h" #include "esp_pm.h" #ifdef CONFIG_ESP_PHY_ENABLED @@ -133,7 +133,6 @@ typedef union { }; uint32_t val; } bt_wakeup_params_t; - /* External functions or variables ************************************************************************ */ @@ -192,6 +191,7 @@ extern int r_ble_get_npl_element_info(esp_bt_controller_config_t *cfg, ble_npl_c extern char *ble_controller_get_compile_version(void); extern int esp_ble_register_bb_funcs(void); extern void esp_ble_unregister_bb_funcs(void); +extern bool esp_ble_controller_lib_check(void); extern uint32_t _bt_bss_start; extern uint32_t _bt_bss_end; extern uint32_t _bt_controller_bss_start; @@ -233,6 +233,11 @@ static void esp_bt_ctrl_log_partition_get_and_erase_first_block(void); #if CONFIG_FREERTOS_USE_TICKLESS_IDLE static bool esp_bt_check_wakeup_by_bt(void); #endif // CONFIG_FREERTOS_USE_TICKLESS_IDLE + +#if (CONFIG_BT_CONTROLLER_ONLY) && (CONFIG_BT_LE_SM_SC) && (!CONFIG_BT_LE_CRYPTO_STACK_MBEDTLS) +#include "tinycrypt/ecc.h" +static int ecc_rand_func(uint8_t *dst, unsigned int len); +#endif // (CONFIG_BT_CONTROLLER_ONLY) && (CONFIG_BT_LE_SM_SC) && (!CONFIG_BT_LE_CRYPTO_STACK_MBEDTLS) /* Local variable definition *************************************************************************** */ @@ -1055,6 +1060,7 @@ static void ble_rtc_clk_init(esp_bt_controller_config_t *cfg) esp_bt_rtc_slow_clk_select(s_bt_lpclk_src); } + esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg) { uint8_t mac[6]; @@ -1191,6 +1197,13 @@ esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg) goto free_controller; } + if (!esp_ble_controller_lib_check()) { + ESP_LOGW(NIMBLE_PORT_LOG_TAG, "Controller lib version mismatch!"); + } + +#if (CONFIG_BT_CONTROLLER_ONLY) && (CONFIG_BT_LE_SM_SC) && (!CONFIG_BT_LE_CRYPTO_STACK_MBEDTLS) + uECC_set_rng(ecc_rand_func); +#endif // (CONFIG_BT_CONTROLLER_ONLY) && (CONFIG_BT_LE_SM_SC) && (!CONFIG_BT_LE_CRYPTO_STACK_MBEDTLS) return ESP_OK; free_controller: hci_transport_deinit(); @@ -1443,7 +1456,6 @@ esp_err_t esp_bt_mem_release(esp_bt_mode_t mode) return ret; } - esp_bt_controller_status_t esp_bt_controller_get_status(void) { return ble_controller_status; @@ -1664,6 +1676,25 @@ static mbedtls_ecp_keypair keypair; #if CONFIG_BT_LE_SM_SC #include "tinycrypt/cmac_mode.h" #include "tinycrypt/ecc_dh.h" + +/* Used by uECC to get random data */ +static int ecc_rand_func(uint8_t *dst, unsigned int len) +{ + int offset_cnt = 0; + uint8_t *u8ptr = dst; + uint64_t random_64 = 0; + + while(len > 0) { + random64 = (uint64_t)esp_random(); + random64 = (random64 << 32)| (uint64_t)esp_random();; + offset_cnt = len < sizeof(uint64_t) ? len : sizeof(uint64_t); + memcpy(u8ptr, &random64, offset_cnt); + len -= offset_cnt; + u8ptr += offset_cnt; + } + + return 1; +} #endif // CONFIG_BT_LE_SM_SC #endif // CONFIG_BT_LE_CRYPTO_STACK_MBEDTLS @@ -1755,11 +1786,11 @@ exit: } #else - if (uECC_valid_public_key(pk, &curve_secp256r1) < 0) { + if (uECC_valid_public_key(pk, uECC_secp256r1()) < 0) { return BLE_SM_KEY_ERR; } - rc = uECC_shared_secret(pk, priv, dh, &curve_secp256r1); + rc = uECC_shared_secret(pk, priv, dh, uECC_secp256r1()); if (rc == TC_CRYPTO_FAIL) { return BLE_SM_KEY_ERR; } @@ -1835,7 +1866,7 @@ int ble_sm_alg_gen_key_pair(uint8_t *pub, uint8_t *priv) return BLE_SM_KEY_ERR; } #else - if (uECC_make_key(pk, priv, &curve_secp256r1) != TC_CRYPTO_SUCCESS) { + if (uECC_make_key(pk, priv, uECC_secp256r1()) != TC_CRYPTO_SUCCESS) { return BLE_SM_KEY_ERR; } #endif // CONFIG_BT_LE_CRYPTO_STACK_MBEDTLS @@ -1851,7 +1882,6 @@ int ble_sm_alg_gen_key_pair(uint8_t *pub, uint8_t *priv) #endif // CONFIG_BT_LE_SM_LEGACY || CONFIG_BT_LE_SM_SC #endif // (!CONFIG_BT_NIMBLE_ENABLED) && (CONFIG_BT_CONTROLLER_ENABLED) - #if CONFIG_BT_LE_DEBUG_REMAIN_SCENE_ENABLED #include "esp_gdbstub.h" #endif // CONFIG_BT_LE_DEBUG_REMAIN_SCENE_ENABLED diff --git a/components/bt/controller/esp32c6/esp_bt_cfg.h b/components/bt/controller/esp32c6/esp_bt_cfg.h index 71f7f9d194..e5d378ad9b 100644 --- a/components/bt/controller/esp32c6/esp_bt_cfg.h +++ b/components/bt/controller/esp32c6/esp_bt_cfg.h @@ -218,6 +218,13 @@ extern "C" { #define DEFAULT_BT_LE_CTRL_SLV_FAST_RX_CONN_DATA_EN (0) #endif + +#if defined(CONFIG_BT_LE_CTRL_DL_ITVL_PHY_SYNC_EN) +#define DEFAULT_BT_LE_CTRL_DL_ITVL_PHY_SYNC_EN (CONFIG_BT_LE_CTRL_DL_ITVL_PHY_SYNC_EN) +#else +#define DEFAULT_BT_LE_CTRL_DL_ITVL_PHY_SYNC_EN (0) +#endif + #ifdef CONFIG_BT_LE_HCI_INTERFACE_USE_UART #define HCI_UART_EN CONFIG_BT_LE_HCI_INTERFACE_USE_UART #else @@ -307,7 +314,6 @@ extern "C" { #define RUN_QA_TEST (0) #define NIMBLE_DISABLE_SCAN_BACKOFF (0) #define BT_LL_CTRL_PRIO_LVL_CFG ((CONFIG_BT_LE_DFT_SYNC_SCHED_PRIO_LEVEL << 4) | (CONFIG_BT_LE_DFT_PERIODIC_ADV_SCHED_PRIO_LEVEL << 2) | CONFIG_BT_LE_DFT_ADV_SCHED_PRIO_LEVEL) - #ifdef __cplusplus } #endif diff --git a/components/bt/include/esp32c6/include/esp_bt.h b/components/bt/include/esp32c6/include/esp_bt.h index 3ccdeb7cb7..5771335a99 100644 --- a/components/bt/include/esp32c6/include/esp_bt.h +++ b/components/bt/include/esp32c6/include/esp_bt.h @@ -156,7 +156,7 @@ esp_err_t esp_ble_tx_power_set_enhanced(esp_ble_enhanced_power_type_t power_type */ esp_power_level_t esp_ble_tx_power_get_enhanced(esp_ble_enhanced_power_type_t power_type, uint16_t handle); -#define CONFIG_VERSION 0x20251104 +#define CONFIG_VERSION 0x20251125 #define CONFIG_MAGIC 0x5A5AA5A5 /** @@ -236,6 +236,7 @@ typedef struct { uint8_t conn_rsv_cnt; /*!< BLE conn state machine reserve count number */ uint8_t priority_level_cfg; /*!< The option for priority level configuration */ uint8_t slv_fst_rx_lat_en; /*!< The option for enabling slave fast PDU reception during latency. */ + uint8_t dl_itvl_phy_sync_en; /*!< The option for automatically initiate the data length update when phy update or connect interval update. */ uint32_t config_magic; /*!< Magic number for configuration validation */ } esp_bt_controller_config_t; @@ -301,7 +302,8 @@ typedef struct { .adv_rsv_cnt = BLE_LL_ADV_SM_RESERVE_CNT_N, \ .conn_rsv_cnt = BLE_LL_CONN_SM_RESERVE_CNT_N, \ .priority_level_cfg = BT_LL_CTRL_PRIO_LVL_CFG, \ - .slv_fst_rx_lat_en = DEFAULT_BT_LE_CTRL_SLV_FAST_RX_CONN_DATA_EN, \ + .slv_fst_rx_lat_en = DEFAULT_BT_LE_CTRL_SLV_FAST_RX_CONN_DATA_EN, \ + .dl_itvl_phy_sync_en = DEFAULT_BT_LE_CTRL_DL_ITVL_PHY_SYNC_EN, \ .config_magic = CONFIG_MAGIC, \ } #elif CONFIG_IDF_TARGET_ESP32C61 @@ -365,6 +367,7 @@ typedef struct { .conn_rsv_cnt = BLE_LL_CONN_SM_RESERVE_CNT_N, \ .priority_level_cfg = BT_LL_CTRL_PRIO_LVL_CFG, \ .slv_fst_rx_lat_en = DEFAULT_BT_LE_CTRL_SLV_FAST_RX_CONN_DATA_EN, \ + .dl_itvl_phy_sync_en = DEFAULT_BT_LE_CTRL_DL_ITVL_PHY_SYNC_EN, \ .config_magic = CONFIG_MAGIC, \ } #endif From 80b7c2b0f15b2210f255219cf3d1db1c062bd9d8 Mon Sep 17 00:00:00 2001 From: zhaoweiliang Date: Tue, 2 Dec 2025 20:11:57 +0800 Subject: [PATCH 07/12] feat(ble): add CONFIG_BT_LE_CTRL_DL_ITVL_PHY_SYNC_EN config on ESP32-C5 --- components/bt/controller/esp32c5/Kconfig.in | 8 ++++ components/bt/controller/esp32c5/bt.c | 38 +++++++++++++++++-- components/bt/controller/esp32c5/esp_bt_cfg.h | 7 ++++ components/bt/controller/esp32h2/bt.c | 1 - .../bt/include/esp32c5/include/esp_bt.h | 4 +- 5 files changed, 53 insertions(+), 5 deletions(-) diff --git a/components/bt/controller/esp32c5/Kconfig.in b/components/bt/controller/esp32c5/Kconfig.in index cfcef62e17..dbd2a8e212 100644 --- a/components/bt/controller/esp32c5/Kconfig.in +++ b/components/bt/controller/esp32c5/Kconfig.in @@ -951,3 +951,11 @@ config BT_LE_CTRL_SLV_FAST_RX_CONN_DATA_EN When this option is enabled, the Controller continues receiving PDUs In the next connection event instead of entering latency After a data packet is received. + +config BT_LE_CTRL_DL_ITVL_PHY_SYNC_EN + bool "Enables the automatic initiation of a data length update(Experimental)." + default n + help + When this option is enabled, the Controller automatically initiates a data length update + Using the appropriate data length parameters + When a PHY update or a connection interval update occurs. diff --git a/components/bt/controller/esp32c5/bt.c b/components/bt/controller/esp32c5/bt.c index 7453a6917f..6354eb9928 100644 --- a/components/bt/controller/esp32c5/bt.c +++ b/components/bt/controller/esp32c5/bt.c @@ -180,6 +180,7 @@ extern int r_ble_get_npl_element_info(esp_bt_controller_config_t *cfg, ble_npl_c extern char *ble_controller_get_compile_version(void); extern int esp_ble_register_bb_funcs(void); extern void esp_ble_unregister_bb_funcs(void); +extern bool esp_ble_controller_lib_check(void); extern uint32_t _bt_bss_start; extern uint32_t _bt_bss_end; extern uint32_t _bt_controller_bss_start; @@ -218,6 +219,10 @@ static void esp_bt_ctrl_log_partition_get_and_erase_first_block(void); #if CONFIG_FREERTOS_USE_TICKLESS_IDLE static bool esp_bt_check_wakeup_by_bt(void); #endif // CONFIG_FREERTOS_USE_TICKLESS_IDLE +#if (CONFIG_BT_CONTROLLER_ONLY) && (CONFIG_BT_LE_SM_SC) && (!CONFIG_BT_LE_CRYPTO_STACK_MBEDTLS) +#include "tinycrypt/ecc.h" +static int ecc_rand_func(uint8_t *dst, unsigned int len); +#endif // (CONFIG_BT_CONTROLLER_ONLY) && (CONFIG_BT_LE_SM_SC) && (!CONFIG_BT_LE_CRYPTO_STACK_MBEDTLS) /* Local variable definition *************************************************************************** */ @@ -1121,6 +1126,13 @@ esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg) goto free_controller; } + if (!esp_ble_controller_lib_check()) { + ESP_LOGW(NIMBLE_PORT_LOG_TAG, "Controller lib version mismatch!"); + } + +#if (CONFIG_BT_CONTROLLER_ONLY) && (CONFIG_BT_LE_SM_SC) && (!CONFIG_BT_LE_CRYPTO_STACK_MBEDTLS) + uECC_set_rng(ecc_rand_func); +#endif // (CONFIG_BT_CONTROLLER_ONLY) && (CONFIG_BT_LE_SM_SC) && (!CONFIG_BT_LE_CRYPTO_STACK_MBEDTLS) return ESP_OK; free_controller: hci_transport_deinit(); @@ -1594,6 +1606,26 @@ static mbedtls_ecp_keypair keypair; #if CONFIG_BT_LE_SM_SC #include "tinycrypt/cmac_mode.h" #include "tinycrypt/ecc_dh.h" + +/* Used by uECC to get random data */ +static int ecc_rand_func(uint8_t *dst, unsigned int len) +{ + int offset_cnt = 0; + uint8_t *u8ptr = dst; + uint64_t random_64 = 0; + + while(len > 0) { + random64 = (uint64_t)esp_random(); + random64 = (random64 << 32)| (uint64_t)esp_random();; + offset_cnt = len < sizeof(uint64_t) ? len : sizeof(uint64_t); + memcpy(u8ptr, &random64, offset_cnt); + len -= offset_cnt; + u8ptr += offset_cnt; + } + + return 1; +} + #endif // CONFIG_BT_LE_SM_SC #endif // CONFIG_BT_LE_CRYPTO_STACK_MBEDTLS @@ -1685,11 +1717,11 @@ exit: } #else - if (uECC_valid_public_key(pk, &curve_secp256r1) < 0) { + if (uECC_valid_public_key(pk, uECC_secp256r1()) < 0) { return BLE_SM_KEY_ERR; } - rc = uECC_shared_secret(pk, priv, dh, &curve_secp256r1); + rc = uECC_shared_secret(pk, priv, dh, uECC_secp256r1()); if (rc == TC_CRYPTO_FAIL) { return BLE_SM_KEY_ERR; } @@ -1765,7 +1797,7 @@ int ble_sm_alg_gen_key_pair(uint8_t *pub, uint8_t *priv) return BLE_SM_KEY_ERR; } #else - if (uECC_make_key(pk, priv, &curve_secp256r1) != TC_CRYPTO_SUCCESS) { + if (uECC_make_key(pk, priv, uECC_secp256r1()) != TC_CRYPTO_SUCCESS) { return BLE_SM_KEY_ERR; } #endif // CONFIG_BT_LE_CRYPTO_STACK_MBEDTLS diff --git a/components/bt/controller/esp32c5/esp_bt_cfg.h b/components/bt/controller/esp32c5/esp_bt_cfg.h index 4dbe2f2fe8..20f2ec7edb 100644 --- a/components/bt/controller/esp32c5/esp_bt_cfg.h +++ b/components/bt/controller/esp32c5/esp_bt_cfg.h @@ -215,6 +215,13 @@ extern "C" { #define DEFAULT_BT_LE_CTRL_SLV_FAST_RX_CONN_DATA_EN (0) #endif + +#if defined(CONFIG_BT_LE_CTRL_DL_ITVL_PHY_SYNC_EN) +#define DEFAULT_BT_LE_CTRL_DL_ITVL_PHY_SYNC_EN (CONFIG_BT_LE_CTRL_DL_ITVL_PHY_SYNC_EN) +#else +#define DEFAULT_BT_LE_CTRL_DL_ITVL_PHY_SYNC_EN (0) +#endif + #ifdef CONFIG_BT_LE_HCI_INTERFACE_USE_UART #define HCI_UART_EN CONFIG_BT_LE_HCI_INTERFACE_USE_UART #else diff --git a/components/bt/controller/esp32h2/bt.c b/components/bt/controller/esp32h2/bt.c index c51cd40c95..ffa419c6e0 100644 --- a/components/bt/controller/esp32h2/bt.c +++ b/components/bt/controller/esp32h2/bt.c @@ -1014,7 +1014,6 @@ static void ble_rtc_clk_init(esp_bt_controller_config_t *cfg) esp_bt_rtc_slow_clk_select(s_bt_lpclk_src); } - esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg) { uint8_t mac[6]; diff --git a/components/bt/include/esp32c5/include/esp_bt.h b/components/bt/include/esp32c5/include/esp_bt.h index b44c861e72..478c4df11a 100644 --- a/components/bt/include/esp32c5/include/esp_bt.h +++ b/components/bt/include/esp32c5/include/esp_bt.h @@ -159,7 +159,7 @@ esp_err_t esp_ble_tx_power_set_enhanced(esp_ble_enhanced_power_type_t power_type */ esp_power_level_t esp_ble_tx_power_get_enhanced(esp_ble_enhanced_power_type_t power_type, uint16_t handle); -#define CONFIG_VERSION 0x20251104 +#define CONFIG_VERSION 0x20251125 #define CONFIG_MAGIC 0x5A5AA5A5 /** @@ -236,6 +236,7 @@ typedef struct { uint8_t conn_rsv_cnt; /*!< BLE conn state machine reserve count number */ uint8_t priority_level_cfg; /*!< The option for priority level configuration */ uint8_t slv_fst_rx_lat_en; /*!< The option for enabling slave fast PDU reception during latency. */ + uint8_t dl_itvl_phy_sync_en; /*!< The option for automatically initiate the data length update when phy update or connect interval update. */ uint32_t config_magic; /*!< Magic number for configuration validation */ } esp_bt_controller_config_t; @@ -299,6 +300,7 @@ typedef struct { .conn_rsv_cnt = BLE_LL_CONN_SM_RESERVE_CNT_N, \ .priority_level_cfg = BT_LL_CTRL_PRIO_LVL_CFG, \ .slv_fst_rx_lat_en = DEFAULT_BT_LE_CTRL_SLV_FAST_RX_CONN_DATA_EN, \ + .dl_itvl_phy_sync_en = DEFAULT_BT_LE_CTRL_DL_ITVL_PHY_SYNC_EN, \ .config_magic = CONFIG_MAGIC, \ } From 3d45deb23a5758c0c310fbedd09953d5faab7593 Mon Sep 17 00:00:00 2001 From: zhaoweiliang Date: Wed, 31 Dec 2025 11:45:14 +0800 Subject: [PATCH 08/12] change(ble): [AUTO_MR] Update lib_esp32h2 to 2406161c --- components/bt/controller/lib_esp32h2/esp32h2-bt-lib | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/bt/controller/lib_esp32h2/esp32h2-bt-lib b/components/bt/controller/lib_esp32h2/esp32h2-bt-lib index 33a575696b..bfbcfa6ee4 160000 --- a/components/bt/controller/lib_esp32h2/esp32h2-bt-lib +++ b/components/bt/controller/lib_esp32h2/esp32h2-bt-lib @@ -1 +1 @@ -Subproject commit 33a575696b811a255dfb68bbea1370ea5e95265f +Subproject commit bfbcfa6ee46f4e2e323d9ddf6069984d6f836aa4 From 9a7d360faaa0adbbc76479664eaf4cfa3b72eeea Mon Sep 17 00:00:00 2001 From: zhaoweiliang Date: Wed, 31 Dec 2025 11:45:14 +0800 Subject: [PATCH 09/12] change(ble): [AUTO_MR] Update lib_esp32c5 to 2406161c --- components/bt/controller/lib_esp32c5/esp32c5-bt-lib | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/bt/controller/lib_esp32c5/esp32c5-bt-lib b/components/bt/controller/lib_esp32c5/esp32c5-bt-lib index cc21d43f5c..955958046d 160000 --- a/components/bt/controller/lib_esp32c5/esp32c5-bt-lib +++ b/components/bt/controller/lib_esp32c5/esp32c5-bt-lib @@ -1 +1 @@ -Subproject commit cc21d43f5c3c3203125a13329391ba3b3c5ab518 +Subproject commit 955958046d6306726cf10ff420805e33c429e0e8 From e047f40baa224cbf66c9130c0eb58c337523c357 Mon Sep 17 00:00:00 2001 From: zhaoweiliang Date: Wed, 31 Dec 2025 11:45:14 +0800 Subject: [PATCH 10/12] change(ble): [AUTO_MR] Update lib_esp32c6 to 2406161c --- components/bt/controller/lib_esp32c6/esp32c6-bt-lib | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/bt/controller/lib_esp32c6/esp32c6-bt-lib b/components/bt/controller/lib_esp32c6/esp32c6-bt-lib index 237c0ce2eb..8175cc3655 160000 --- a/components/bt/controller/lib_esp32c6/esp32c6-bt-lib +++ b/components/bt/controller/lib_esp32c6/esp32c6-bt-lib @@ -1 +1 @@ -Subproject commit 237c0ce2eb75becf4890d96897105174659e07b5 +Subproject commit 8175cc3655be5a08d85dd38149d3dd79d7c1fe49 From 86613d225f0964204d8b5799d3042d1b4f417c70 Mon Sep 17 00:00:00 2001 From: zhaoweiliang Date: Wed, 31 Dec 2025 12:04:37 +0800 Subject: [PATCH 11/12] fix(ble): fixed build error on ESP32-C6 --- components/bt/controller/esp32c2/bt.c | 4 +++- components/bt/controller/esp32c6/bt.c | 4 +++- components/bt/controller/esp32h2/bt.c | 4 +++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/components/bt/controller/esp32c2/bt.c b/components/bt/controller/esp32c2/bt.c index 104503d8e2..41149cd8e5 100644 --- a/components/bt/controller/esp32c2/bt.c +++ b/components/bt/controller/esp32c2/bt.c @@ -1466,12 +1466,13 @@ static mbedtls_ecp_keypair keypair; #include "tinycrypt/ecc_dh.h" +#if CONFIG_BT_CONTROLLER_ONLY /* Used by uECC to get random data */ static int ecc_rand_func(uint8_t *dst, unsigned int len) { int offset_cnt = 0; uint8_t *u8ptr = dst; - uint64_t random_64 = 0; + uint64_t random64 = 0; while(len > 0) { random64 = (uint64_t)esp_random(); @@ -1485,6 +1486,7 @@ static int ecc_rand_func(uint8_t *dst, unsigned int len) return 1; } +#endif // CONFIG_BT_CONTROLLER_ONLY #endif // CONFIG_BT_LE_SM_SC #endif // CONFIG_BT_LE_CRYPTO_STACK_MBEDTLS diff --git a/components/bt/controller/esp32c6/bt.c b/components/bt/controller/esp32c6/bt.c index de8d8f51fc..018f06ff6f 100644 --- a/components/bt/controller/esp32c6/bt.c +++ b/components/bt/controller/esp32c6/bt.c @@ -1677,12 +1677,13 @@ static mbedtls_ecp_keypair keypair; #include "tinycrypt/cmac_mode.h" #include "tinycrypt/ecc_dh.h" +#if CONFIG_BT_CONTROLLER_ONLY /* Used by uECC to get random data */ static int ecc_rand_func(uint8_t *dst, unsigned int len) { int offset_cnt = 0; uint8_t *u8ptr = dst; - uint64_t random_64 = 0; + uint64_t random64 = 0; while(len > 0) { random64 = (uint64_t)esp_random(); @@ -1695,6 +1696,7 @@ static int ecc_rand_func(uint8_t *dst, unsigned int len) return 1; } +#endif // CONFIG_BT_CONTROLLER_ONLY #endif // CONFIG_BT_LE_SM_SC #endif // CONFIG_BT_LE_CRYPTO_STACK_MBEDTLS diff --git a/components/bt/controller/esp32h2/bt.c b/components/bt/controller/esp32h2/bt.c index ffa419c6e0..9fb20c0f57 100644 --- a/components/bt/controller/esp32h2/bt.c +++ b/components/bt/controller/esp32h2/bt.c @@ -1627,12 +1627,13 @@ static mbedtls_ecp_keypair keypair; #include "tinycrypt/cmac_mode.h" #include "tinycrypt/ecc_dh.h" +#if CONFIG_BT_CONTROLLER_ONLY /* Used by uECC to get random data */ static int ecc_rand_func(uint8_t *dst, unsigned int len) { int offset_cnt = 0; uint8_t *u8ptr = dst; - uint64_t random_64 = 0; + uint64_t random64 = 0; while(len > 0) { random64 = (uint64_t)esp_random(); @@ -1645,6 +1646,7 @@ static int ecc_rand_func(uint8_t *dst, unsigned int len) return 1; } +#endif // CONFIG_BT_CONTROLLER_ONLY #endif // CONFIG_BT_LE_SM_SC #endif // CONFIG_BT_LE_CRYPTO_STACK_MBEDTLS From 823ebb1ce35b86ffe2da8af720c65ecc0ab650ed Mon Sep 17 00:00:00 2001 From: zhaoweiliang Date: Wed, 31 Dec 2025 12:06:41 +0800 Subject: [PATCH 12/12] fix(ble): fixed build error on ESP32-C5 --- components/bt/controller/esp32c5/bt.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/components/bt/controller/esp32c5/bt.c b/components/bt/controller/esp32c5/bt.c index 6354eb9928..2b1945ccd9 100644 --- a/components/bt/controller/esp32c5/bt.c +++ b/components/bt/controller/esp32c5/bt.c @@ -1606,13 +1606,13 @@ static mbedtls_ecp_keypair keypair; #if CONFIG_BT_LE_SM_SC #include "tinycrypt/cmac_mode.h" #include "tinycrypt/ecc_dh.h" - +#if CONFIG_BT_CONTROLLER_ONLY /* Used by uECC to get random data */ static int ecc_rand_func(uint8_t *dst, unsigned int len) { int offset_cnt = 0; uint8_t *u8ptr = dst; - uint64_t random_64 = 0; + uint64_t random64 = 0; while(len > 0) { random64 = (uint64_t)esp_random(); @@ -1625,7 +1625,7 @@ static int ecc_rand_func(uint8_t *dst, unsigned int len) return 1; } - +#endif // CONFIG_BT_CONTROLLER_ONLY #endif // CONFIG_BT_LE_SM_SC #endif // CONFIG_BT_LE_CRYPTO_STACK_MBEDTLS