diff --git a/components/bt/controller/esp32c3/Kconfig.in b/components/bt/controller/esp32c3/Kconfig.in index fb9920251f..2c162f34ff 100644 --- a/components/bt/controller/esp32c3/Kconfig.in +++ b/components/bt/controller/esp32c3/Kconfig.in @@ -78,15 +78,29 @@ config BT_CTRL_ADV_DUP_FILT_MAX help The maxinum number of suplicate scan filter -config BT_CTRL_HW_CCA - bool "HW CCA check enable" - default n +choice BT_BLE_CCA_MODE + prompt "BLE CCA mode" + default BT_BLE_CCA_MODE_NONE help - It enables HW CCA feature in controller + Define BT BLE CCA mode + + config BT_BLE_CCA_MODE_NONE + bool "NONE" + config BT_BLE_CCA_MODE_HW + bool "Hardware" + config BT_BLE_CCA_MODE_SW + bool "Software" +endchoice + +config BT_BLE_CCA_MODE + int + default 0 if BT_BLE_CCA_MODE_NONE + default 1 if BT_BLE_CCA_MODE_HW + default 2 if BT_BLE_CCA_MODE_SW config BT_CTRL_HW_CCA_VAL int "CCA threshold value" - range 20 60 + range 20 100 default 20 help It is the threshold value of HW CCA, if the value is 30, it means CCA threshold is -30 dBm. diff --git a/components/bt/controller/esp32c3/bt.c b/components/bt/controller/esp32c3/bt.c index 82e05b7837..49e613dd09 100644 --- a/components/bt/controller/esp32c3/bt.c +++ b/components/bt/controller/esp32c3/bt.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -110,7 +110,7 @@ do{\ } while(0) #define OSI_FUNCS_TIME_BLOCKING 0xffffffff -#define OSI_VERSION 0x00010006 +#define OSI_VERSION 0x00010007 #define OSI_MAGIC_VALUE 0xFADEBEAD /* Types definition @@ -192,6 +192,8 @@ struct osi_funcs_t { void (* _esp_hw_power_down)(void); void (* _esp_hw_power_up)(void); void (* _ets_backup_dma_copy)(uint32_t reg, uint32_t mem_addr, uint32_t num, bool to_rem); + void (* _ets_delay_us)(uint32_t us); + void (* _btdm_rom_table_ready)(void); }; @@ -251,6 +253,8 @@ extern void esp_mac_bb_power_up(void); extern void ets_backup_dma_copy(uint32_t reg, uint32_t mem_addr, uint32_t num, bool to_mem); #endif +extern void btdm_cca_feature_enable(void); + extern uint32_t _bt_bss_start; extern uint32_t _bt_bss_end; extern uint32_t _btdm_bss_start; @@ -310,6 +314,7 @@ static void interrupt_off_wrapper(int intr_num); static void btdm_hw_mac_power_up_wrapper(void); static void btdm_hw_mac_power_down_wrapper(void); static void btdm_backup_dma_copy_wrapper(uint32_t reg, uint32_t mem_addr, uint32_t num, bool to_mem); +static void btdm_funcs_table_ready_wrapper(void); static void btdm_slp_tmr_callback(void *arg); @@ -374,6 +379,8 @@ static const struct osi_funcs_t osi_funcs_ro = { ._esp_hw_power_down = btdm_hw_mac_power_down_wrapper, ._esp_hw_power_up = btdm_hw_mac_power_up_wrapper, ._ets_backup_dma_copy = btdm_backup_dma_copy_wrapper, + ._ets_delay_us = esp_rom_delay_us, + ._btdm_rom_table_ready = btdm_funcs_table_ready_wrapper, }; static DRAM_ATTR struct osi_funcs_t *osi_funcs_p; @@ -912,6 +919,13 @@ static void async_wakeup_request_end(int event) return; } +static void btdm_funcs_table_ready_wrapper(void) +{ +#if BT_BLE_CCA_MODE == 2 + btdm_cca_feature_enable(); +#endif +} + static void coex_schm_status_bit_set_wrapper(uint32_t type, uint32_t status) { #if CONFIG_SW_COEXIST_ENABLE @@ -1340,14 +1354,10 @@ esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg) periph_module_enable(PERIPH_BT_MODULE); periph_module_reset(PERIPH_BT_MODULE); - esp_phy_enable(); - s_lp_stat.phy_enabled = 1; - if (btdm_controller_init(cfg) != 0) { err = ESP_ERR_NO_MEM; goto error; } - coex_pti_v2(); btdm_controller_status = ESP_BT_CONTROLLER_STATUS_INITED; @@ -1377,11 +1387,6 @@ static void bt_controller_deinit_internal(void) { periph_module_disable(PERIPH_BT_MODULE); - if (s_lp_stat.phy_enabled) { - esp_phy_disable(); - s_lp_stat.phy_enabled = 0; - } - // deinit low power control resources do { @@ -1475,6 +1480,12 @@ esp_err_t esp_bt_controller_enable(esp_bt_mode_t mode) return ESP_ERR_INVALID_ARG; } + /* Enable PHY when enabling controller to reduce power dissipation after controller init + * Notice the init order: esp_phy_enable() -> bt_bb_v2_init_cmplx() -> coex_pti_v2() + */ + esp_phy_enable(); + s_lp_stat.phy_enabled = 1; + #if CONFIG_SW_COEXIST_ENABLE coex_enable(); #endif @@ -1499,6 +1510,8 @@ esp_err_t esp_bt_controller_enable(esp_bt_mode_t mode) goto error; } + coex_pti_v2(); + btdm_controller_status = ESP_BT_CONTROLLER_STATUS_ENABLED; return ret; @@ -1521,6 +1534,10 @@ error: #if CONFIG_SW_COEXIST_ENABLE coex_disable(); #endif + if (s_lp_stat.phy_enabled) { + esp_phy_disable(); + s_lp_stat.phy_enabled = 0; + } return ret; } @@ -1539,6 +1556,10 @@ esp_err_t esp_bt_controller_disable(void) #if CONFIG_SW_COEXIST_ENABLE coex_disable(); #endif + if (s_lp_stat.phy_enabled) { + esp_phy_disable(); + s_lp_stat.phy_enabled = 0; + } btdm_controller_status = ESP_BT_CONTROLLER_STATUS_INITED; diff --git a/components/bt/controller/lib_esp32 b/components/bt/controller/lib_esp32 index 29e3ff2f39..7bb0d445db 160000 --- a/components/bt/controller/lib_esp32 +++ b/components/bt/controller/lib_esp32 @@ -1 +1 @@ -Subproject commit 29e3ff2f39f16ff71e24bdd25ea87a0306e5b24b +Subproject commit 7bb0d445db3414ce96d21c50ba9249125d41480f diff --git a/components/bt/controller/lib_esp32c3_family b/components/bt/controller/lib_esp32c3_family index b438f60a29..0cfac1b21e 160000 --- a/components/bt/controller/lib_esp32c3_family +++ b/components/bt/controller/lib_esp32c3_family @@ -1 +1 @@ -Subproject commit b438f60a295183e7c67eb42ae05f4580f4b1ced0 +Subproject commit 0cfac1b21ebc995e8e9aa040ab1ab29deee4f580 diff --git a/components/bt/host/bluedroid/Kconfig.in b/components/bt/host/bluedroid/Kconfig.in index ee826bf595..bb9fd7e218 100644 --- a/components/bt/host/bluedroid/Kconfig.in +++ b/components/bt/host/bluedroid/Kconfig.in @@ -1098,3 +1098,17 @@ config BT_BLE_42_FEATURES_SUPPORTED default n help This enables BLE 4.2 features. + +config BT_BLE_FEAT_PERIODIC_ADV_ENH + bool "Enable periodic adv enhancements(adi support)" + depends on (BT_BLUEDROID_ENABLED && BT_BLE_50_FEATURES_SUPPORTED && SOC_ESP_NIMBLE_CONTROLLER) + default n + help + Enable the periodic advertising enhancements + +config BT_BLE_HIGH_DUTY_ADV_INTERVAL + bool "Enable BLE high duty advertising interval feature" + depends on BT_BLUEDROID_ENABLED + default n + help + This enable BLE high duty advertising interval feature diff --git a/components/bt/host/bluedroid/api/esp_gap_ble_api.c b/components/bt/host/bluedroid/api/esp_gap_ble_api.c index b076f11588..d4790fbd6c 100644 --- a/components/bt/host/bluedroid/api/esp_gap_ble_api.c +++ b/components/bt/host/bluedroid/api/esp_gap_ble_api.c @@ -1,16 +1,8 @@ -// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at - -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/* + * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ #include #include "esp_bt_device.h" @@ -146,7 +138,7 @@ esp_err_t esp_ble_gap_update_conn_params(esp_ble_conn_update_params_t *params) if (ESP_BLE_IS_VALID_PARAM(params->min_int, ESP_BLE_CONN_INT_MIN, ESP_BLE_CONN_INT_MAX) && ESP_BLE_IS_VALID_PARAM(params->max_int, ESP_BLE_CONN_INT_MIN, ESP_BLE_CONN_INT_MAX) && ESP_BLE_IS_VALID_PARAM(params->timeout, ESP_BLE_CONN_SUP_TOUT_MIN, ESP_BLE_CONN_SUP_TOUT_MAX) && - (params->latency <= ESP_BLE_CONN_LATENCY_MAX || params->latency == ESP_BLE_CONN_PARAM_UNDEF) && + (params->latency <= ESP_BLE_CONN_LATENCY_MAX) && ((params->timeout * 10) >= ((1 + params->latency) * ((params->max_int * 5) >> 1))) && params->min_int <= params->max_int) { msg.sig = BTC_SIG_API_CALL; @@ -362,7 +354,7 @@ esp_err_t esp_ble_gap_set_prefer_conn_params(esp_bd_addr_t bd_addr, if (ESP_BLE_IS_VALID_PARAM(min_conn_int, ESP_BLE_CONN_INT_MIN, ESP_BLE_CONN_INT_MAX) && ESP_BLE_IS_VALID_PARAM(max_conn_int, ESP_BLE_CONN_INT_MIN, ESP_BLE_CONN_INT_MAX) && ESP_BLE_IS_VALID_PARAM(supervision_tout, ESP_BLE_CONN_SUP_TOUT_MIN, ESP_BLE_CONN_SUP_TOUT_MAX) && - (slave_latency <= ESP_BLE_CONN_LATENCY_MAX || slave_latency == ESP_BLE_CONN_PARAM_UNDEF) && + (slave_latency <= ESP_BLE_CONN_LATENCY_MAX) && ((supervision_tout * 10) >= ((1 + slave_latency) * ((max_conn_int * 5) >> 1))) && min_conn_int <= max_conn_int) { msg.sig = BTC_SIG_API_CALL; @@ -1028,8 +1020,13 @@ esp_err_t esp_ble_gap_periodic_adv_set_params(uint8_t instance, const esp_ble_ga } +#if (CONFIG_BT_BLE_FEAT_PERIODIC_ADV_ENH) +esp_err_t esp_ble_gap_config_periodic_adv_data_raw(uint8_t instance, uint16_t length, + const uint8_t *data, bool only_update_did) +#else esp_err_t esp_ble_gap_config_periodic_adv_data_raw(uint8_t instance, uint16_t length, const uint8_t *data) +#endif { btc_msg_t msg; btc_ble_5_gap_args_t arg; @@ -1043,13 +1040,22 @@ esp_err_t esp_ble_gap_config_periodic_adv_data_raw(uint8_t instance, uint16_t le arg.periodic_adv_cfg_data.instance = instance; arg.periodic_adv_cfg_data.len = length; arg.periodic_adv_cfg_data.data = (uint8_t *)data; +#if (CONFIG_BT_BLE_FEAT_PERIODIC_ADV_ENH) + arg.periodic_adv_cfg_data.only_update_did = only_update_did; +#else + arg.periodic_adv_cfg_data.only_update_did = false; +#endif return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_5_gap_args_t), btc_gap_ble_arg_deep_copy, btc_gap_ble_arg_deep_free) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } +#if (CONFIG_BT_BLE_FEAT_PERIODIC_ADV_ENH) +esp_err_t esp_ble_gap_periodic_adv_start(uint8_t instance,bool include_adi) +#else esp_err_t esp_ble_gap_periodic_adv_start(uint8_t instance) +#endif { btc_msg_t msg; btc_ble_5_gap_args_t arg; @@ -1060,6 +1066,11 @@ esp_err_t esp_ble_gap_periodic_adv_start(uint8_t instance) msg.pid = BTC_PID_GAP_BLE; msg.act = BTC_GAP_BLE_PERIODIC_ADV_START; + #if (CONFIG_BT_BLE_FEAT_PERIODIC_ADV_ENH) + arg.periodic_adv_start.include_adi = include_adi; + #else + arg.periodic_adv_start.include_adi = false; + #endif arg.periodic_adv_start.instance = instance; return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_5_gap_args_t), NULL, NULL) @@ -1274,7 +1285,7 @@ esp_err_t esp_ble_gap_prefer_ext_connect_params_set(esp_bd_addr_t addr, if (ESP_BLE_IS_VALID_PARAM(phy_1m_conn_params->interval_min, ESP_BLE_CONN_INT_MIN, ESP_BLE_CONN_INT_MAX) && ESP_BLE_IS_VALID_PARAM(phy_1m_conn_params->interval_max, ESP_BLE_CONN_INT_MIN, ESP_BLE_CONN_INT_MAX) && ESP_BLE_IS_VALID_PARAM(phy_1m_conn_params->supervision_timeout, ESP_BLE_CONN_SUP_TOUT_MIN, ESP_BLE_CONN_SUP_TOUT_MAX) && - (phy_1m_conn_params->latency <= ESP_BLE_CONN_LATENCY_MAX || phy_1m_conn_params->latency == ESP_BLE_CONN_PARAM_UNDEF) && + (phy_1m_conn_params->latency <= ESP_BLE_CONN_LATENCY_MAX) && ((phy_1m_conn_params->supervision_timeout * 10) >= ((1 + phy_1m_conn_params->latency) * ((phy_1m_conn_params->interval_max * 5) >> 1))) && (phy_1m_conn_params->interval_min <= phy_1m_conn_params->interval_max)) { @@ -1298,7 +1309,7 @@ esp_err_t esp_ble_gap_prefer_ext_connect_params_set(esp_bd_addr_t addr, if (ESP_BLE_IS_VALID_PARAM(phy_2m_conn_params->interval_min, ESP_BLE_CONN_INT_MIN, ESP_BLE_CONN_INT_MAX) && ESP_BLE_IS_VALID_PARAM(phy_2m_conn_params->interval_max, ESP_BLE_CONN_INT_MIN, ESP_BLE_CONN_INT_MAX) && ESP_BLE_IS_VALID_PARAM(phy_2m_conn_params->supervision_timeout, ESP_BLE_CONN_SUP_TOUT_MIN, ESP_BLE_CONN_SUP_TOUT_MAX) && - (phy_2m_conn_params->latency <= ESP_BLE_CONN_LATENCY_MAX || phy_2m_conn_params->latency == ESP_BLE_CONN_PARAM_UNDEF) && + (phy_2m_conn_params->latency <= ESP_BLE_CONN_LATENCY_MAX) && ((phy_2m_conn_params->supervision_timeout * 10) >= ((1 + phy_2m_conn_params->latency) * ((phy_2m_conn_params->interval_max * 5) >> 1))) && (phy_2m_conn_params->interval_min <= phy_2m_conn_params->interval_max)) { @@ -1322,7 +1333,7 @@ esp_err_t esp_ble_gap_prefer_ext_connect_params_set(esp_bd_addr_t addr, if (ESP_BLE_IS_VALID_PARAM(phy_coded_conn_params->interval_min, ESP_BLE_CONN_INT_MIN, ESP_BLE_CONN_INT_MAX) && ESP_BLE_IS_VALID_PARAM(phy_coded_conn_params->interval_max, ESP_BLE_CONN_INT_MIN, ESP_BLE_CONN_INT_MAX) && ESP_BLE_IS_VALID_PARAM(phy_coded_conn_params->supervision_timeout, ESP_BLE_CONN_SUP_TOUT_MIN, ESP_BLE_CONN_SUP_TOUT_MAX) && - (phy_coded_conn_params->latency <= ESP_BLE_CONN_LATENCY_MAX || phy_coded_conn_params->latency == ESP_BLE_CONN_PARAM_UNDEF) && + (phy_coded_conn_params->latency <= ESP_BLE_CONN_LATENCY_MAX) && ((phy_coded_conn_params->supervision_timeout * 10) >= ((1 + phy_coded_conn_params->latency) * ((phy_coded_conn_params->interval_max * 5) >> 1))) && (phy_coded_conn_params->interval_min <= phy_coded_conn_params->interval_max)) { diff --git a/components/bt/host/bluedroid/api/include/api/esp_bt_defs.h b/components/bt/host/bluedroid/api/include/api/esp_bt_defs.h index 48a9007979..e03165f620 100644 --- a/components/bt/host/bluedroid/api/include/api/esp_bt_defs.h +++ b/components/bt/host/bluedroid/api/include/api/esp_bt_defs.h @@ -133,18 +133,20 @@ typedef uint8_t esp_link_key[ESP_BT_OCTET16_LEN]; /* Link Key */ /// Default GATT interface id #define ESP_DEFAULT_GATT_IF 0xff +#if BLE_HIGH_DUTY_ADV_INTERVAL +#define ESP_BLE_PRIM_ADV_INT_MIN 0x000008 /*!< Minimum advertising interval for undirected and low duty cycle directed advertising */ +#else #define ESP_BLE_PRIM_ADV_INT_MIN 0x000020 /*!< Minimum advertising interval for undirected and low duty cycle directed advertising */ +#endif #define ESP_BLE_PRIM_ADV_INT_MAX 0xFFFFFF /*!< Maximum advertising interval for undirected and low duty cycle directed advertising */ #define ESP_BLE_CONN_INT_MIN 0x0006 /*!< relate to BTM_BLE_CONN_INT_MIN in stack/btm_ble_api.h */ #define ESP_BLE_CONN_INT_MAX 0x0C80 /*!< relate to BTM_BLE_CONN_INT_MAX in stack/btm_ble_api.h */ #define ESP_BLE_CONN_LATENCY_MAX 499 /*!< relate to ESP_BLE_CONN_LATENCY_MAX in stack/btm_ble_api.h */ #define ESP_BLE_CONN_SUP_TOUT_MIN 0x000A /*!< relate to BTM_BLE_CONN_SUP_TOUT_MIN in stack/btm_ble_api.h */ #define ESP_BLE_CONN_SUP_TOUT_MAX 0x0C80 /*!< relate to ESP_BLE_CONN_SUP_TOUT_MAX in stack/btm_ble_api.h */ -#define ESP_BLE_CONN_PARAM_UNDEF 0xffff /* use this value when a specific value not to be overwritten */ /* relate to ESP_BLE_CONN_PARAM_UNDEF in stack/btm_ble_api.h */ -#define ESP_BLE_SCAN_PARAM_UNDEF 0xffffffff /* relate to ESP_BLE_SCAN_PARAM_UNDEF in stack/btm_ble_api.h */ /// Check the param is valid or not -#define ESP_BLE_IS_VALID_PARAM(x, min, max) (((x) >= (min) && (x) <= (max)) || ((x) == ESP_BLE_CONN_PARAM_UNDEF)) +#define ESP_BLE_IS_VALID_PARAM(x, min, max) (((x) >= (min) && (x) <= (max)) ) /// UUID type typedef struct { @@ -174,10 +176,10 @@ typedef uint8_t esp_bd_addr_t[ESP_BD_ADDR_LEN]; /// BLE device address type typedef enum { - BLE_ADDR_TYPE_PUBLIC = 0x00, - BLE_ADDR_TYPE_RANDOM = 0x01, - BLE_ADDR_TYPE_RPA_PUBLIC = 0x02, - BLE_ADDR_TYPE_RPA_RANDOM = 0x03, + BLE_ADDR_TYPE_PUBLIC = 0x00, /*!< Public Device Address */ + BLE_ADDR_TYPE_RANDOM = 0x01, /*!< Random Device Address. To set this address, use the function esp_ble_gap_set_rand_addr(esp_bd_addr_t rand_addr) */ + BLE_ADDR_TYPE_RPA_PUBLIC = 0x02, /*!< Resolvable Private Address (RPA) with public identity address */ + BLE_ADDR_TYPE_RPA_RANDOM = 0x03, /*!< Resolvable Private Address (RPA) with random identity address. To set this address, use the function esp_ble_gap_set_rand_addr(esp_bd_addr_t rand_addr) */ } esp_ble_addr_type_t; /// white list address type diff --git a/components/bt/host/bluedroid/api/include/api/esp_gap_ble_api.h b/components/bt/host/bluedroid/api/include/api/esp_gap_ble_api.h index 57b78d033c..047725f26e 100644 --- a/components/bt/host/bluedroid/api/include/api/esp_gap_ble_api.h +++ b/components/bt/host/bluedroid/api/include/api/esp_gap_ble_api.h @@ -1,16 +1,8 @@ -// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at - -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/* + * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ #ifndef __ESP_GAP_BLE_API_H__ #define __ESP_GAP_BLE_API_H__ @@ -141,7 +133,7 @@ typedef enum { ESP_GAP_BLE_SCAN_PARAM_SET_COMPLETE_EVT, /*!< When scan parameters set complete, the event comes */ ESP_GAP_BLE_SCAN_RESULT_EVT, /*!< When one scan result ready, the event comes each time */ ESP_GAP_BLE_ADV_DATA_RAW_SET_COMPLETE_EVT, /*!< When raw advertising data set complete, the event comes */ - ESP_GAP_BLE_SCAN_RSP_DATA_RAW_SET_COMPLETE_EVT, /*!< When raw advertising data set complete, the event comes */ + ESP_GAP_BLE_SCAN_RSP_DATA_RAW_SET_COMPLETE_EVT, /*!< When raw scan response data set complete, the event comes */ ESP_GAP_BLE_ADV_START_COMPLETE_EVT, /*!< When start advertising complete, the event comes */ ESP_GAP_BLE_SCAN_START_COMPLETE_EVT, /*!< When start scan complete, the event comes */ //BLE_INCLUDED @@ -1433,9 +1425,17 @@ esp_err_t esp_ble_gap_update_conn_params(esp_ble_conn_update_params_t *params); esp_err_t esp_ble_gap_set_pkt_data_len(esp_bd_addr_t remote_device, uint16_t tx_data_length); /** - * @brief This function sets the static Random Address and Non-Resolvable Private Address for the application + * @brief This function allows configuring either a Non-Resolvable Private Address or a Static Random Address * - * @param[in] rand_addr: the random address which should be setting + * @param[in] rand_addr: The address to be configured. Refer to the table below for possible address subtypes: + * + * | address [47:46] | Address Type | + * |-----------------|--------------------------| + * | 0b00 | Non-Resolvable Private | + * | | Address | + * |-----------------|--------------------------| + * | 0b11 | Static Random Address | + * |-----------------|--------------------------| * * @return * - ESP_OK : success @@ -1457,7 +1457,7 @@ esp_err_t esp_ble_gap_clear_rand_addr(void); /** - * @brief Enable/disable privacy on the local device + * @brief Enable/disable privacy (including address resolution) on the local device * * @param[in] privacy_enable - enable/disable privacy on remote device. * @@ -1538,6 +1538,7 @@ esp_err_t esp_ble_gap_set_prefer_conn_params(esp_bd_addr_t bd_addr, #endif // #if (BLE_42_FEATURE_SUPPORT == TRUE) /** * @brief Set device name to the local device + * Note: This API don't affect the advertising data * * @param[in] name - device name. * @@ -1586,7 +1587,7 @@ uint8_t *esp_ble_resolve_adv_data(uint8_t *adv_data, uint8_t type, uint8_t *leng * @brief This function is called to set raw advertising data. User need to fill * ADV data by self. * - * @param[in] raw_data : raw advertising data + * @param[in] raw_data : raw advertising data with the format: [Length 1][Data Type 1][Data 1][Length 2][Data Type 2][Data 2] ... * @param[in] raw_data_len : raw advertising data length , less than 31 bytes * * @return @@ -2016,6 +2017,22 @@ esp_err_t esp_ble_gap_ext_adv_set_clear(void); */ esp_err_t esp_ble_gap_periodic_adv_set_params(uint8_t instance, const esp_ble_gap_periodic_adv_params_t *params); +#if (CONFIG_BT_BLE_FEAT_PERIODIC_ADV_ENH) +/** +* @brief This function is used to set the data used in periodic advertising PDUs. +* +* @param[in] instance : identifies the advertising set whose periodic advertising parameters are being configured. +* @param[in] length : the length of periodic data +* @param[in] data : periodic data information +* @param[in] only_update_did : If true, only the Advertising DID of the periodic advertising will be updated, and the length and data parameters will be ignored. +* +* @return - ESP_OK : success +* - other : failed +* +*/ +esp_err_t esp_ble_gap_config_periodic_adv_data_raw(uint8_t instance, uint16_t length, + const uint8_t *data, bool only_update_did); +#else /** * @brief This function is used to set the data used in periodic advertising PDUs. * @@ -2029,6 +2046,21 @@ esp_err_t esp_ble_gap_periodic_adv_set_params(uint8_t instance, const esp_ble_ga */ esp_err_t esp_ble_gap_config_periodic_adv_data_raw(uint8_t instance, uint16_t length, const uint8_t *data); +#endif + +#if (CONFIG_BT_BLE_FEAT_PERIODIC_ADV_ENH) +/** +* @brief This function is used to request the Controller to enable the periodic advertising for the advertising set specified +* +* @param[in] instance : Used to identify an advertising set +* @param[in] include_adi : If true, the ADI (Advertising Data Info) field will be included in AUX_SYNC_IND PDUs +* +* @return - ESP_OK : success +* - other : failed +* +*/ +esp_err_t esp_ble_gap_periodic_adv_start(uint8_t instance,bool include_adi); +#else /** * @brief This function is used to request the Controller to enable the periodic advertising for the advertising set specified * @@ -2039,6 +2071,7 @@ esp_err_t esp_ble_gap_config_periodic_adv_data_raw(uint8_t instance, uint16_t le * */ esp_err_t esp_ble_gap_periodic_adv_start(uint8_t instance); +#endif /** * @brief This function is used to request the Controller to disable the periodic advertising for the advertising set specified diff --git a/components/bt/host/bluedroid/api/include/api/esp_gattc_api.h b/components/bt/host/bluedroid/api/include/api/esp_gattc_api.h index f4b57fec63..43a385aac0 100644 --- a/components/bt/host/bluedroid/api/include/api/esp_gattc_api.h +++ b/components/bt/host/bluedroid/api/include/api/esp_gattc_api.h @@ -367,6 +367,7 @@ esp_err_t esp_ble_gattc_send_mtu_req (esp_gatt_if_t gattc_if, uint16_t conn_id); * @brief This function is called to get service from local cache. * This function report service search result by a callback * event, and followed by a service search complete event. + * Note: 128-bit base UUID will automatically be converted to a 16-bit UUID in the search results. Other types of UUID remain unchanged. * * @param[in] gattc_if: Gatt client access interface. * @param[in] conn_id: connection ID. diff --git a/components/bt/host/bluedroid/api/include/api/esp_gatts_api.h b/components/bt/host/bluedroid/api/include/api/esp_gatts_api.h index 97853cb106..d142e99861 100644 --- a/components/bt/host/bluedroid/api/include/api/esp_gatts_api.h +++ b/components/bt/host/bluedroid/api/include/api/esp_gatts_api.h @@ -473,6 +473,7 @@ esp_err_t esp_ble_gatts_stop_service(uint16_t service_handle); /** * @brief Send indicate or notify to GATT client. * Set param need_confirm as false will send notification, otherwise indication. + * Note: the size of indicate or notify data need less than MTU size,see "esp_ble_gattc_send_mtu_req". * * @param[in] gatts_if: GATT server access interface * @param[in] conn_id - connection id to indicate. diff --git a/components/bt/host/bluedroid/bta/dm/bta_dm_act.c b/components/bt/host/bluedroid/bta/dm/bta_dm_act.c index 179258ee31..cc33129a9d 100644 --- a/components/bt/host/bluedroid/bta/dm/bta_dm_act.c +++ b/components/bt/host/bluedroid/bta/dm/bta_dm_act.c @@ -5047,7 +5047,7 @@ void bta_dm_ble_update_conn_params (tBTA_DM_MSG *p_data) *******************************************************************************/ void bta_dm_ble_disconnect (tBTA_DM_MSG *p_data) { - L2CA_RemoveFixedChnl(L2CAP_ATT_CID, p_data->ble_disconnect.remote_bda); + L2CA_BleDisconnect(p_data->ble_disconnect.remote_bda); } /******************************************************************************* @@ -5669,7 +5669,8 @@ void bta_dm_ble_gap_periodic_adv_cfg_data_raw(tBTA_DM_MSG *p_data) BTM_BlePeriodicAdvCfgDataRaw(p_data->ble_cfg_periodic_adv_data.instance, p_data->ble_cfg_periodic_adv_data.length, - p_data->ble_cfg_periodic_adv_data.data); + p_data->ble_cfg_periodic_adv_data.data, + p_data->ble_cfg_periodic_adv_data.only_update_did); } void bta_dm_ble_gap_periodic_adv_enable(tBTA_DM_MSG *p_data) diff --git a/components/bt/host/bluedroid/bta/dm/bta_dm_api.c b/components/bt/host/bluedroid/bta/dm/bta_dm_api.c index 99a0543ee7..f8c2fd438e 100644 --- a/components/bt/host/bluedroid/bta/dm/bta_dm_api.c +++ b/components/bt/host/bluedroid/bta/dm/bta_dm_api.c @@ -2883,7 +2883,7 @@ void BTA_DmBleGapPeriodicAdvSetParams(UINT8 instance, } void BTA_DmBleGapPeriodicAdvCfgDataRaw(UINT8 instance, UINT16 length, - const UINT8 *data) + const UINT8 *data,bool only_update_did) { tBTA_DM_API_CFG_PERIODIC_ADV_DATA *p_msg; APPL_TRACE_API("%s, Periodic ADV config data raw.", __func__); @@ -2895,6 +2895,7 @@ void BTA_DmBleGapPeriodicAdvCfgDataRaw(UINT8 instance, UINT16 length, p_msg->data = (UINT8 *)(p_msg + 1); memcpy(p_msg->data, data, length); p_msg->data = length != 0 ? (UINT8 *)(p_msg + 1) : NULL; + p_msg->only_update_did = only_update_did; //start sent the msg to the bta system control moudle bta_sys_sendmsg(p_msg); } else { @@ -2903,7 +2904,7 @@ void BTA_DmBleGapPeriodicAdvCfgDataRaw(UINT8 instance, UINT16 length, } -void BTA_DmBleGapPeriodicAdvEnable(BOOLEAN enable, UINT8 instance) +void BTA_DmBleGapPeriodicAdvEnable(UINT8 enable, UINT8 instance) { tBTA_DM_API_ENABLE_PERIODIC_ADV *p_msg; APPL_TRACE_API("%s, Periodic ADV %s.", __func__, enable ? "start" : "stop"); diff --git a/components/bt/host/bluedroid/bta/dm/include/bta_dm_int.h b/components/bt/host/bluedroid/bta/dm/include/bta_dm_int.h index 723f97e941..4083005e26 100644 --- a/components/bt/host/bluedroid/bta/dm/include/bta_dm_int.h +++ b/components/bt/host/bluedroid/bta/dm/include/bta_dm_int.h @@ -945,12 +945,13 @@ typedef struct { UINT8 instance; UINT16 length; UINT8 *data; + BOOLEAN only_update_did; } tBTA_DM_API_CFG_PERIODIC_ADV_DATA; typedef struct { BT_HDR hdr; UINT8 instance; - BOOLEAN enable; + UINT8 enable; } tBTA_DM_API_ENABLE_PERIODIC_ADV; typedef struct { diff --git a/components/bt/host/bluedroid/bta/gatt/bta_gattc_co.c b/components/bt/host/bluedroid/bta/gatt/bta_gattc_co.c index 01b107cee9..3d5394fb1e 100644 --- a/components/bt/host/bluedroid/bta/gatt/bta_gattc_co.c +++ b/components/bt/host/bluedroid/bta/gatt/bta_gattc_co.c @@ -139,7 +139,7 @@ static bool cacheOpen(BD_ADDR bda, bool to_save, UINT8 *index) return ((status == ESP_OK) ? true : false); } -static void cacheReset(BD_ADDR bda) +static void cacheReset(BD_ADDR bda, BOOLEAN update) { char fname[255] = {0}; getFilename(fname, bda); @@ -177,9 +177,16 @@ static void cacheReset(BD_ADDR bda) for(UINT8 i = index; i < (num - 1); i++) { memcpy(&cache_env->cache_addr[i], &cache_env->cache_addr[i+1], sizeof(cache_addr_info_t)); } + //clear the last cache when delete a addr + memset(&cache_env->cache_addr[num-1], 0, sizeof(cache_addr_info_t)); //reduced the number address counter also cache_env->num_addr--; + //don't need to update addr list to nvs flash + if (!update) { + return; + } + //update addr list to nvs flash if(cache_env->num_addr > 0) { //update @@ -376,7 +383,7 @@ void bta_gattc_co_cache_close(BD_ADDR server_bda, UINT16 conn_id) *******************************************************************************/ void bta_gattc_co_cache_reset(BD_ADDR server_bda) { - cacheReset(server_bda); + cacheReset(server_bda, TRUE); } void bta_gattc_co_cache_addr_init(void) @@ -520,26 +527,29 @@ void bta_gattc_co_cache_addr_save(BD_ADDR bd_addr, hash_key_t hash_key) UINT8 index = 0; UINT8 new_index = cache_env->num_addr; UINT8 *p_buf = osi_malloc(MAX_ADDR_LIST_CACHE_BUF); - // check the address list has the same hash key or not - if (bta_gattc_co_find_hash_in_cache(hash_key) != INVALID_ADDR_NUM) { - APPL_TRACE_DEBUG("%s(), the hash key already in the cache list.", __func__); - if ((index = bta_gattc_co_find_addr_in_cache(bd_addr)) != INVALID_ADDR_NUM) { - APPL_TRACE_DEBUG("%s(), the hash bd_addr already in the cache list, index = %x", __func__, index); - //if the bd_addr already in the address list, update the hash key in it. - memcpy(cache_env->cache_addr[index].addr, bd_addr, sizeof(BD_ADDR)); - memcpy(cache_env->cache_addr[index].hash_key, hash_key, sizeof(hash_key_t)); - } else { - //if the bd_addr didn't in the address list, added the bd_addr to the last of the address list. - memcpy(cache_env->cache_addr[new_index].hash_key, hash_key, sizeof(hash_key_t)); - memcpy(cache_env->cache_addr[new_index].addr, bd_addr, sizeof(BD_ADDR)); - cache_env->num_addr++; - } + if (p_buf == NULL) { + APPL_TRACE_ERROR("%s malloc failed!", __func__); + return; + } + // check the address list has the same address or not + // for the same address, it's hash key may be change due to service change + if ((index = bta_gattc_co_find_addr_in_cache(bd_addr)) != INVALID_ADDR_NUM) { + APPL_TRACE_DEBUG("%s the bd_addr already in the cache list, index = %x", __func__, index); + //if the bd_addr already in the address list, update the hash key in it. + memcpy(cache_env->cache_addr[index].addr, bd_addr, sizeof(BD_ADDR)); + memcpy(cache_env->cache_addr[index].hash_key, hash_key, sizeof(hash_key_t)); } else { - APPL_TRACE_DEBUG("%s(), num = %d", __func__, new_index + 1); + if (cache_env->num_addr >= MAX_DEVICE_IN_CACHE) { + APPL_TRACE_WARNING("%s cache list full and remove the oldest addr info", __func__); + cacheReset(cache_env->cache_addr[0].addr, FALSE); + } + new_index = cache_env->num_addr; + assert(new_index < MAX_DEVICE_IN_CACHE); memcpy(cache_env->cache_addr[new_index].addr, bd_addr, sizeof(BD_ADDR)); memcpy(cache_env->cache_addr[new_index].hash_key, hash_key, sizeof(hash_key_t)); cache_env->num_addr++; + APPL_TRACE_DEBUG("%s(), num = %d", __func__, cache_env->num_addr); } nvs_handle_t *fp = &cache_env->addr_fp; @@ -567,10 +577,10 @@ void bta_gattc_co_cache_addr_save(BD_ADDR bd_addr, hash_key_t hash_key) APPL_TRACE_ERROR("%s, Line = %d, nvs flash open fail, err_code = %x", __func__, __LINE__, err_code); } } + //free the buffer after used. osi_free(p_buf); return; - } BOOLEAN bta_gattc_co_cache_new_assoc_list(BD_ADDR src_addr, UINT8 index) diff --git a/components/bt/host/bluedroid/bta/include/bta/bta_api.h b/components/bt/host/bluedroid/bta/include/bta/bta_api.h index f5fae44e1b..0735c7943b 100644 --- a/components/bt/host/bluedroid/bta/include/bta/bta_api.h +++ b/components/bt/host/bluedroid/bta/include/bta/bta_api.h @@ -2947,9 +2947,9 @@ extern void BTA_DmBleGapPeriodicAdvSetParams(UINT8 instance, tBTA_DM_BLE_Periodic_Adv_Params *params); extern void BTA_DmBleGapPeriodicAdvCfgDataRaw(UINT8 instance, UINT16 length, - const UINT8 *data); + const UINT8 *data,BOOLEAN only_update_did); -extern void BTA_DmBleGapPeriodicAdvEnable(BOOLEAN enable, UINT8 instance); +extern void BTA_DmBleGapPeriodicAdvEnable(UINT8 enable, UINT8 instance); extern void BTA_DmBleGapPeriodicAdvCreateSync(tBTA_DM_BLE_Periodic_Sync_Params *params); diff --git a/components/bt/host/bluedroid/btc/profile/std/gap/btc_gap_ble.c b/components/bt/host/bluedroid/btc/profile/std/gap/btc_gap_ble.c index 9780934551..0ade03f199 100644 --- a/components/bt/host/bluedroid/btc/profile/std/gap/btc_gap_ble.c +++ b/components/bt/host/bluedroid/btc/profile/std/gap/btc_gap_ble.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -1836,11 +1836,12 @@ void btc_gap_ble_call_handler(btc_msg_t *msg) BTC_TRACE_DEBUG("BTC_GAP_BLE_CFG_PERIODIC_ADV_DATA_RAW"); BTA_DmBleGapPeriodicAdvCfgDataRaw(arg_5->periodic_adv_cfg_data.instance, arg_5->periodic_adv_cfg_data.len, - (const UINT8 *)arg_5->periodic_adv_cfg_data.data); + (const UINT8 *)arg_5->periodic_adv_cfg_data.data, + arg_5->periodic_adv_cfg_data.only_update_did); break; case BTC_GAP_BLE_PERIODIC_ADV_START: BTC_TRACE_DEBUG("BTC_GAP_BLE_PERIODIC_ADV_START"); - BTA_DmBleGapPeriodicAdvEnable(TRUE, arg_5->periodic_adv_start.instance); + BTA_DmBleGapPeriodicAdvEnable(((arg_5->periodic_adv_start.include_adi)<<1)|0x01, arg_5->periodic_adv_start.instance); break; case BTC_GAP_BLE_PERIODIC_ADV_STOP: BTC_TRACE_DEBUG("BTC_GAP_BLE_PERIODIC_ADV_STOP"); diff --git a/components/bt/host/bluedroid/btc/profile/std/include/btc_gap_ble.h b/components/bt/host/bluedroid/btc/profile/std/include/btc_gap_ble.h index 3721f3dccb..3ff785dcb0 100644 --- a/components/bt/host/bluedroid/btc/profile/std/include/btc_gap_ble.h +++ b/components/bt/host/bluedroid/btc/profile/std/include/btc_gap_ble.h @@ -1,16 +1,8 @@ -// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at - -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/* + * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ #ifndef __BTC_GAP_BLE_H__ #define __BTC_GAP_BLE_H__ @@ -26,7 +18,7 @@ extern tBTA_BLE_ADV_DATA *gl_bta_scan_rsp_data_ptr; #define gl_bta_scan_rsp_data (*gl_bta_scan_rsp_data_ptr) #endif -#define BLE_ISVALID_PARAM(x, min, max) (((x) >= (min) && (x) <= (max)) || ((x) == ESP_BLE_CONN_PARAM_UNDEF)) +#define BLE_ISVALID_PARAM(x, min, max) (((x) >= (min) && (x) <= (max))) typedef enum { #if (BLE_42_FEATURE_SUPPORT == TRUE) @@ -291,9 +283,11 @@ typedef union { uint8_t instance; uint16_t len; uint8_t *data; + bool only_update_did; } periodic_adv_cfg_data; struct periodic_adv_start_args { + bool include_adi; uint8_t instance; } periodic_adv_start; diff --git a/components/bt/host/bluedroid/common/include/common/bluedroid_user_config.h b/components/bt/host/bluedroid/common/include/common/bluedroid_user_config.h index 263500e380..15b5c9bbe4 100644 --- a/components/bt/host/bluedroid/common/include/common/bluedroid_user_config.h +++ b/components/bt/host/bluedroid/common/include/common/bluedroid_user_config.h @@ -125,6 +125,18 @@ #define UC_BT_BLE_42_FEATURES_SUPPORTED FALSE #endif +#ifdef CONFIG_BT_BLE_FEAT_PERIODIC_ADV_ENH +#define UC_BT_BLE_FEAT_PERIODIC_ADV_ENH CONFIG_BT_BLE_FEAT_PERIODIC_ADV_ENH +#else +#define UC_BT_BLE_FEAT_PERIODIC_ADV_ENH FALSE +#endif + +#ifdef CONFIG_BT_BLE_HIGH_DUTY_ADV_INTERVAL +#define UC_BT_BLE_HIGH_DUTY_ADV_INTERVAL CONFIG_BT_BLE_HIGH_DUTY_ADV_INTERVAL +#else +#define UC_BT_BLE_HIGH_DUTY_ADV_INTERVAL FALSE +#endif + //GATTS #ifdef CONFIG_BT_GATTS_ENABLE #define UC_BT_GATTS_ENABLE CONFIG_BT_GATTS_ENABLE diff --git a/components/bt/host/bluedroid/common/include/common/bt_target.h b/components/bt/host/bluedroid/common/include/common/bt_target.h index b76ffcbce5..c7420415cc 100644 --- a/components/bt/host/bluedroid/common/include/common/bt_target.h +++ b/components/bt/host/bluedroid/common/include/common/bt_target.h @@ -183,6 +183,18 @@ #define BLE_42_FEATURE_SUPPORT FALSE #endif +#if (UC_BT_BLE_FEAT_PERIODIC_ADV_ENH == TRUE) +#define BLE_FEAT_PERIODIC_ADV_ENH TRUE +#else +#define BLE_FEAT_PERIODIC_ADV_ENH FALSE +#endif + +#if (UC_BT_BLE_HIGH_DUTY_ADV_INTERVAL == TRUE) +#define BLE_HIGH_DUTY_ADV_INTERVAL TRUE +#else +#define BLE_HIGH_DUTY_ADV_INTERVAL FALSE +#endif + #if (UC_BT_BLE_RPA_SUPPORTED == TRUE) #define CONTROLLER_RPA_LIST_ENABLE TRUE #else diff --git a/components/bt/host/bluedroid/stack/btm/btm_ble_5_gap.c b/components/bt/host/bluedroid/stack/btm/btm_ble_5_gap.c index 0416ce6233..f503d51040 100644 --- a/components/bt/host/bluedroid/stack/btm/btm_ble_5_gap.c +++ b/components/bt/host/bluedroid/stack/btm/btm_ble_5_gap.c @@ -690,7 +690,7 @@ end: return status; } -tBTM_STATUS BTM_BlePeriodicAdvCfgDataRaw(UINT8 instance, UINT16 len, UINT8 *data) +tBTM_STATUS BTM_BlePeriodicAdvCfgDataRaw(UINT8 instance, UINT16 len, UINT8 *data,BOOLEAN only_update_did) { tBTM_STATUS status = BTM_SUCCESS; tHCI_STATUS err = HCI_SUCCESS; @@ -698,6 +698,13 @@ tBTM_STATUS BTM_BlePeriodicAdvCfgDataRaw(UINT8 instance, UINT16 len, UINT8 *data UINT8 operation = 0; UINT16 data_offset = 0; tBTM_BLE_5_GAP_CB_PARAMS cb_params = {0}; + if (only_update_did) + { + len = 0; + data = NULL; + rem_len = 0; + operation = BTM_BLE_ADV_DATA_OP_UNCHANGED_DATA; + } if ((status = btm_ble_ext_adv_set_data_validate(instance, len, data)) != BTM_SUCCESS) { BTM_TRACE_ERROR("%s, invalid extend adv data.", __func__); @@ -708,7 +715,9 @@ tBTM_STATUS BTM_BlePeriodicAdvCfgDataRaw(UINT8 instance, UINT16 len, UINT8 *data UINT8 send_data_len = (rem_len > BTM_BLE_PERIODIC_ADV_DATA_LEN_MAX) ? BTM_BLE_PERIODIC_ADV_DATA_LEN_MAX : rem_len; if (len <= BTM_BLE_EXT_ADV_DATA_LEN_MAX) { - operation = BTM_BLE_ADV_DATA_OP_COMPLETE; + if (!only_update_did) { + operation = BTM_BLE_ADV_DATA_OP_COMPLETE; + } } else { if (rem_len == len) { operation = BTM_BLE_ADV_DATA_OP_FIRST_FRAG; @@ -734,7 +743,7 @@ end: return status; } -tBTM_STATUS BTM_BlePeriodicAdvEnable(UINT8 instance, BOOLEAN enable) +tBTM_STATUS BTM_BlePeriodicAdvEnable(UINT8 instance, UINT8 enable) { tBTM_STATUS status = BTM_SUCCESS; tHCI_STATUS err = HCI_SUCCESS; diff --git a/components/bt/host/bluedroid/stack/btm/btm_ble_gap.c b/components/bt/host/bluedroid/stack/btm/btm_ble_gap.c index 7e32418146..dcfbdca265 100644 --- a/components/bt/host/bluedroid/stack/btm/btm_ble_gap.c +++ b/components/bt/host/bluedroid/stack/btm/btm_ble_gap.c @@ -1000,7 +1000,7 @@ uint32_t BTM_BleUpdateOwnType(uint8_t *own_bda_type, tBTM_START_ADV_CMPL_CBACK * memcpy(btm_cb.ble_ctr_cb.addr_mgnt_cb.private_addr, btm_cb.ble_ctr_cb.addr_mgnt_cb.resolvale_addr, BD_ADDR_LEN); btsnd_hcic_ble_set_random_addr(btm_cb.ble_ctr_cb.addr_mgnt_cb.resolvale_addr); }else { - BTM_TRACE_ERROR ("No random address yet, please set random address and try\n"); + BTM_TRACE_ERROR ("No random address yet, please set random address using API \"esp_ble_gap_set_rand_addr\" and retry\n"); if(cb) { (* cb)(HCI_ERR_ESP_VENDOR_FAIL); } @@ -1049,17 +1049,28 @@ uint32_t BTM_BleUpdateOwnType(uint8_t *own_bda_type, tBTM_START_ADV_CMPL_CBACK * #else uint32_t BTM_BleUpdateOwnType(uint8_t *own_bda_type, tBTM_START_ADV_CMPL_CBACK *cb) { + tBTM_LE_RANDOM_CB *p_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb; + if((*own_bda_type == BLE_ADDR_RANDOM) || (*own_bda_type == BLE_ADDR_RANDOM_ID)) { - if((btm_cb.ble_ctr_cb.addr_mgnt_cb.exist_addr_bit & BTM_BLE_GAP_ADDR_BIT_RANDOM) != BTM_BLE_GAP_ADDR_BIT_RANDOM) { + if((p_cb->exist_addr_bit & BTM_BLE_GAP_ADDR_BIT_RANDOM) != BTM_BLE_GAP_ADDR_BIT_RANDOM) { BTM_TRACE_ERROR("No random address yet, please set random address and try\n"); if(cb) { (* cb)(HCI_ERR_ESP_VENDOR_FAIL); } return BTM_ILLEGAL_VALUE; } + + // If a device is using RPA, it shall also have an Identity Address + if ((*own_bda_type == BLE_ADDR_RANDOM_ID) && BTM_BLE_IS_NON_RESLVE_BDA(p_cb->static_rand_addr)) { + BTM_TRACE_ERROR("No identity address yet, please set static random address and try\n"); + if (cb) { + (* cb)(HCI_ERR_ESP_VENDOR_FAIL); + } + return BTM_ILLEGAL_VALUE; + } } - btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type = *own_bda_type; + p_cb->own_addr_type = *own_bda_type; return BTM_SUCCESS; } diff --git a/components/bt/host/bluedroid/stack/btm/btm_ble_privacy.c b/components/bt/host/bluedroid/stack/btm/btm_ble_privacy.c index 617e598171..6f15dd2d3f 100644 --- a/components/bt/host/bluedroid/stack/btm/btm_ble_privacy.c +++ b/components/bt/host/bluedroid/stack/btm/btm_ble_privacy.c @@ -402,7 +402,6 @@ void btm_ble_set_addr_resolution_enable_complete(UINT8 *p, UINT16 evt_len) tBTM_LE_RANDOM_CB *random_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb; if (!(random_cb && random_cb->set_local_privacy_cback)) { - BTM_TRACE_ERROR("no set local privacy callback found"); return; } diff --git a/components/bt/host/bluedroid/stack/btm/include/btm_ble_int.h b/components/bt/host/bluedroid/stack/btm/include/btm_ble_int.h index 8d52c65fe9..63953eaec7 100644 --- a/components/bt/host/bluedroid/stack/btm/include/btm_ble_int.h +++ b/components/bt/host/bluedroid/stack/btm/include/btm_ble_int.h @@ -84,8 +84,10 @@ typedef UINT8 tBTM_BLE_SEC_REQ_ACT; #define BLE_STATIC_PRIVATE_MSB_MASK 0x3f -#define BLE_RESOLVE_ADDR_MSB 0x40 /* most significant bit, bit7, bit6 is 01 to be resolvable random */ +#define BLE_NON_RESOLVE_ADDR_MSB 0x00 /* most significant bit, bit7, bit6 is 00 to be non-resolvable random */ +#define BLE_RESOLVE_ADDR_MSB 0x40 /* most significant bit, bit7, bit6 is 01 to be resolvable random */ #define BLE_RESOLVE_ADDR_MASK 0xc0 /* bit 6, and bit7 */ +#define BTM_BLE_IS_NON_RESLVE_BDA(x) ((x[0] & BLE_RESOLVE_ADDR_MASK) == BLE_NON_RESOLVE_ADDR_MSB) #define BTM_BLE_IS_RESOLVE_BDA(x) ((x[0] & BLE_RESOLVE_ADDR_MASK) == BLE_RESOLVE_ADDR_MSB) /* LE scan activity bit mask, continue with LE inquiry bits */ diff --git a/components/bt/host/bluedroid/stack/include/stack/btm_ble_api.h b/components/bt/host/bluedroid/stack/include/stack/btm_ble_api.h index e84e2e6dea..ee736e9520 100644 --- a/components/bt/host/bluedroid/stack/include/stack/btm_ble_api.h +++ b/components/bt/host/bluedroid/stack/include/stack/btm_ble_api.h @@ -105,8 +105,12 @@ typedef UINT8 tBTM_BLE_SFP; #endif /* adv parameter boundary values */ -#define BTM_BLE_ADV_INT_MIN 0x0020 -#define BTM_BLE_ADV_INT_MAX 0x4000 +#if BLE_HIGH_DUTY_ADV_INTERVAL +#define BTM_BLE_ADV_INT_MIN 0x0008 /* 5ms */ +#else +#define BTM_BLE_ADV_INT_MIN 0x0020 /* 20ms */ +#endif +#define BTM_BLE_ADV_INT_MAX 0x4000 /* 10240ms */ /* Full scan boundary values */ #define BTM_BLE_ADV_SCAN_FULL_MIN 0x00 @@ -2593,9 +2597,9 @@ tBTM_STATUS BTM_BleExtAdvSetClear(void); tBTM_STATUS BTM_BlePeriodicAdvSetParams(UINT8 instance, tBTM_BLE_Periodic_Adv_Params *params); -tBTM_STATUS BTM_BlePeriodicAdvCfgDataRaw(UINT8 instance, UINT16 len, UINT8 *data); +tBTM_STATUS BTM_BlePeriodicAdvCfgDataRaw(UINT8 instance, UINT16 len, UINT8 *data, BOOLEAN only_update_did); -tBTM_STATUS BTM_BlePeriodicAdvEnable(UINT8 instance, BOOLEAN enable); +tBTM_STATUS BTM_BlePeriodicAdvEnable(UINT8 instance, UINT8 enable); tBTM_STATUS BTM_BlePeriodicAdvCreateSync(tBTM_BLE_Periodic_Sync_Params *params); diff --git a/components/bt/host/bluedroid/stack/include/stack/l2c_api.h b/components/bt/host/bluedroid/stack/include/stack/l2c_api.h index 44d9c862c6..4f916029c1 100644 --- a/components/bt/host/bluedroid/stack/include/stack/l2c_api.h +++ b/components/bt/host/bluedroid/stack/include/stack/l2c_api.h @@ -1214,6 +1214,19 @@ extern BOOLEAN L2CA_EnableUpdateBleConnParams (BD_ADDR rem_bda, BOOLEAN enable); ** *******************************************************************************/ extern UINT8 L2CA_GetBleConnRole (BD_ADDR bd_addr); + +/******************************************************************************* +** +** Function L2CA_BleDisconnect +** +** Description This function use to disconnect LE connection. +** +** Parameters BD Address of remote +** +** Returns TRUE if disconnect successfully. +** +*******************************************************************************/ +extern BOOLEAN L2CA_BleDisconnect (BD_ADDR rem_bda); #endif /* (BLE_INCLUDED == TRUE) */ /******************************************************************************* diff --git a/components/bt/host/bluedroid/stack/l2cap/l2c_api.c b/components/bt/host/bluedroid/stack/l2cap/l2c_api.c index 7c282db09e..f88d58956b 100644 --- a/components/bt/host/bluedroid/stack/l2cap/l2c_api.c +++ b/components/bt/host/bluedroid/stack/l2cap/l2c_api.c @@ -1949,6 +1949,36 @@ BOOLEAN L2CA_RemoveFixedChnl (UINT16 fixed_cid, BD_ADDR rem_bda) return (TRUE); } +#if BLE_INCLUDED == TRUE +BOOLEAN L2CA_BleDisconnect (BD_ADDR rem_bda) +{ + tL2C_LCB *p_lcb; + tGATT_TCB *p_tcb; + + p_lcb = l2cu_find_lcb_by_bd_addr (rem_bda, BT_TRANSPORT_LE); + if (p_lcb == NULL) { + return FALSE; + } + + if (p_lcb->link_state != LST_CONNECTED) { + return FALSE; + } + + p_lcb->disc_reason = HCI_ERR_CONN_CAUSE_LOCAL_HOST; + p_lcb->link_state = LST_DISCONNECTING; + btsnd_hcic_disconnect (p_lcb->handle, HCI_ERR_PEER_USER); + + p_tcb = gatt_find_tcb_by_addr(rem_bda, BT_TRANSPORT_LE); + if (p_tcb == NULL) { + return FALSE; + } + + gatt_set_ch_state(p_tcb, GATT_CH_CLOSING); + + return TRUE; +} +#endif + /******************************************************************************* ** ** Function L2CA_SetFixedChannelTout diff --git a/components/bt/host/bluedroid/stack/l2cap/l2c_ble.c b/components/bt/host/bluedroid/stack/l2cap/l2c_ble.c index e485f70e49..63574432b8 100644 --- a/components/bt/host/bluedroid/stack/l2cap/l2c_ble.c +++ b/components/bt/host/bluedroid/stack/l2cap/l2c_ble.c @@ -1366,7 +1366,7 @@ UINT32 CalConnectParamTimeout(tL2C_LCB *p_lcb) UINT32 timeout = 6; if (p_lcb != NULL){ //1.25 * conn_int *(1+ latency) *32 - timeout = (40 * ( 1 + p_lcb->current_used_conn_latency) * p_lcb->current_used_conn_interval + 1000) / 1000; + timeout = (40 * ( 1 + p_lcb->current_used_conn_latency) * p_lcb->current_used_conn_interval + 1.25 * p_lcb->waiting_update_conn_max_interval + 1000) / 1000; if (timeout < 1){ timeout = 1; }else if (timeout > 120){ diff --git a/components/bt/host/bluedroid/stack/smp/smp_cmac.c b/components/bt/host/bluedroid/stack/smp/smp_cmac.c index 391f538353..e47c56b71f 100644 --- a/components/bt/host/bluedroid/stack/smp/smp_cmac.c +++ b/components/bt/host/bluedroid/stack/smp/smp_cmac.c @@ -133,7 +133,7 @@ static void cmac_aes_cleanup(void) static BOOLEAN cmac_aes_k_calculate(BT_OCTET16 key, UINT8 *p_signature, UINT16 tlen) { tSMP_ENC output; - UINT8 i = 1, err = 0; + UINT16 i = 1, err = 0; UINT8 x[16] = {0}; UINT8 *p_mac; diff --git a/components/bt/include/esp32/include/esp_bt.h b/components/bt/include/esp32/include/esp_bt.h index f048ff37e6..6b7ef15752 100644 --- a/components/bt/include/esp32/include/esp_bt.h +++ b/components/bt/include/esp32/include/esp_bt.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ diff --git a/components/bt/include/esp32c3/include/esp_bt.h b/components/bt/include/esp32c3/include/esp_bt.h index 8ac68e06ec..a7e17f4aa1 100644 --- a/components/bt/include/esp32c3/include/esp_bt.h +++ b/components/bt/include/esp32c3/include/esp_bt.h @@ -19,7 +19,7 @@ extern "C" { #endif #define ESP_BT_CTRL_CONFIG_MAGIC_VAL 0x5A5AA5A5 -#define ESP_BT_CTRL_CONFIG_VERSION 0x02302140 +#define ESP_BT_CTRL_CONFIG_VERSION 0x02307120 #define ESP_BT_HCI_TL_MAGIC_VALUE 0xfadebead #define ESP_BT_HCI_TL_VERSION 0x00010000 @@ -169,6 +169,12 @@ typedef void (* esp_bt_hci_tl_callback_t) (void *arg, uint8_t status); #endif // (CONFIG_BT_BLUEDROID_ENABLED) || (CONFIG_BT_NIMBLE_ENABLED) #endif // (CONFIG_BT_BLE_50_FEATURES_SUPPORTED) || (CONFIG_BT_NIMBLE_50_FEATURE_SUPPORT) +#if defined(CONFIG_BT_BLE_CCA_MODE) +#define BT_BLE_CCA_MODE (CONFIG_BT_BLE_CCA_MODE) +#else +#define BT_BLE_CCA_MODE (0) +#endif + #define AGC_RECORRECT_EN ((BT_CTRL_AGC_RECORRECT_EN << 0) | (BT_CTRL_CODED_AGC_RECORRECT <<1)) #define CFG_MASK_BIT_SCAN_DUPLICATE_OPTION (1<<0) @@ -214,6 +220,7 @@ typedef void (* esp_bt_hci_tl_callback_t) (void *arg, uint8_t status); .scan_backoff_upperlimitmax = BT_CTRL_SCAN_BACKOFF_UPPERLIMITMAX, \ .dup_list_refresh_period = DUPL_SCAN_CACHE_REFRESH_PERIOD, \ .ble_50_feat_supp = BT_CTRL_50_FEATURE_SUPPORT, \ + .ble_cca_mode = BT_BLE_CCA_MODE, \ } #else @@ -284,6 +291,7 @@ typedef struct { uint16_t scan_backoff_upperlimitmax; /*!< scan backoff upperlimitmax value */ uint16_t dup_list_refresh_period; /*!< duplicate scan list refresh time */ bool ble_50_feat_supp; /*!< BLE 5.0 feature support */ + uint8_t ble_cca_mode; /*!< BLE CCA mode */ } esp_bt_controller_config_t; /** diff --git a/examples/bluetooth/bluedroid/ble/gatt_server/main/gatts_demo.c b/examples/bluetooth/bluedroid/ble/gatt_server/main/gatts_demo.c index ea140e8ccc..049def0716 100644 --- a/examples/bluetooth/bluedroid/ble/gatt_server/main/gatts_demo.c +++ b/examples/bluetooth/bluedroid/ble/gatt_server/main/gatts_demo.c @@ -1,10 +1,8 @@ /* - This example code is in the Public Domain (or CC0 licensed, at your option.) - - Unless required by applicable law or agreed to in writing, this - software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - CONDITIONS OF ANY KIND, either express or implied. -*/ + * SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Unlicense OR CC0-1.0 + */ /**************************************************************************** * @@ -75,10 +73,11 @@ static uint8_t adv_config_done = 0; #ifdef CONFIG_SET_RAW_ADV_DATA static uint8_t raw_adv_data[] = { - 0x02, 0x01, 0x06, - 0x02, 0x0a, 0xeb, 0x03, 0x03, 0xab, 0xcd + 0x02, 0x01, 0x06, // Length 2, Data Type 1 (Flags), Data 1 (LE General Discoverable Mode, BR/EDR Not Supported) + 0x02, 0x0a, 0xeb, // Length 2, Data Type 10 (TX power leve), Data 2 (-21) + 0x03, 0x03, 0xab, 0xcd, // Length 3, Data Type 3 (Complete 16-bit Service UUIDs), Data 3 (UUID) }; -static uint8_t raw_scan_rsp_data[] = { +static uint8_t raw_scan_rsp_data[] = { // Length 15, Data Type 9 (Complete Local Name), Data 1 (ESP_GATTS_DEMO) 0x0f, 0x09, 0x45, 0x53, 0x50, 0x5f, 0x47, 0x41, 0x54, 0x54, 0x53, 0x5f, 0x44, 0x45, 0x4d, 0x4f }; diff --git a/examples/bluetooth/bluedroid/ble_50/peroidic_adv/main/periodic_adv_demo.c b/examples/bluetooth/bluedroid/ble_50/peroidic_adv/main/periodic_adv_demo.c index c97bfe029c..d0b79d17a6 100644 --- a/examples/bluetooth/bluedroid/ble_50/peroidic_adv/main/periodic_adv_demo.c +++ b/examples/bluetooth/bluedroid/ble_50/peroidic_adv/main/periodic_adv_demo.c @@ -1,10 +1,8 @@ /* - This example code is in the Public Domain (or CC0 licensed, at your option.) - - Unless required by applicable law or agreed to in writing, this - software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - CONDITIONS OF ANY KIND, either express or implied. -*/ + * SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Unlicense OR CC0-1.0 + */ /**************************************************************************** * @@ -193,9 +191,24 @@ void app_main(void) // start all adv FUNC_SEND_WAIT_SEM(esp_ble_gap_ext_adv_start(NUM_EXT_ADV, &ext_adv[0]), test_sem); + // set periodic adv param FUNC_SEND_WAIT_SEM(esp_ble_gap_periodic_adv_set_params(EXT_ADV_HANDLE, &periodic_adv_params), test_sem); + +#if (CONFIG_BT_BLE_FEAT_PERIODIC_ADV_ENH) + // set periodic adv raw data + FUNC_SEND_WAIT_SEM(esp_ble_gap_config_periodic_adv_data_raw(EXT_ADV_HANDLE, sizeof(periodic_adv_raw_data), &periodic_adv_raw_data[0], false), test_sem); + // start periodic adv, include the ADI field in AUX_SYNC_IND PDUs + FUNC_SEND_WAIT_SEM(esp_ble_gap_periodic_adv_start(EXT_ADV_HANDLE, true), test_sem); + while (1) { + vTaskDelay(2000 / portTICK_PERIOD_MS); + // just update the Advertising DID of the periodic advertising, unchanged data + FUNC_SEND_WAIT_SEM(esp_ble_gap_config_periodic_adv_data_raw(EXT_ADV_HANDLE, 0, NULL, true), test_sem); + } +#else + // set periodic adv raw data FUNC_SEND_WAIT_SEM(esp_ble_gap_config_periodic_adv_data_raw(EXT_ADV_HANDLE, sizeof(periodic_adv_raw_data), &periodic_adv_raw_data[0]), test_sem); FUNC_SEND_WAIT_SEM(esp_ble_gap_periodic_adv_start(EXT_ADV_HANDLE), test_sem); +#endif return; } diff --git a/examples/bluetooth/bluedroid/ble_50/peroidic_adv/tutorial/Periodic_adv_Example_Walkthrough.md b/examples/bluetooth/bluedroid/ble_50/peroidic_adv/tutorial/Periodic_adv_Example_Walkthrough.md index ad843f80d1..4b5ea8a562 100644 --- a/examples/bluetooth/bluedroid/ble_50/peroidic_adv/tutorial/Periodic_adv_Example_Walkthrough.md +++ b/examples/bluetooth/bluedroid/ble_50/peroidic_adv/tutorial/Periodic_adv_Example_Walkthrough.md @@ -94,12 +94,25 @@ a_2m), &raw_ext_adv_data_2m[0]), test_sem); // start all adv FUNC_SEND_WAIT_SEM(esp_ble_gap_ext_adv_start(NUM_EXT_ADV, &ext_adv[0]), test_sem); + + // set periodic adv param + FUNC_SEND_WAIT_SEM(esp_ble_gap_periodic_adv_set_params(EXT_ADV_HANDLE, &periodic_adv_params), test_sem); - FUNC_SEND_WAIT_SEM(esp_ble_gap_periodic_adv_set_params(EXT_ADV_HANDLE, &periodic_adv_params), - test_sem); - FUNC_SEND_WAIT_SEM(esp_ble_gap_config_periodic_adv_data_raw(EXT_ADV_HANDLE, sizeof(periodic_a -dv_raw_data), &periodic_adv_raw_data[0]), test_sem); +#if (CONFIG_BT_BLE_FEAT_PERIODIC_ADV_ENH) + // set periodic adv raw data + FUNC_SEND_WAIT_SEM(esp_ble_gap_config_periodic_adv_data_raw(EXT_ADV_HANDLE, sizeof(periodic_adv_raw_data), &periodic_adv_raw_data[0], false), test_sem); + // start periodic adv, include the ADI field in AUX_SYNC_IND PDUs + FUNC_SEND_WAIT_SEM(esp_ble_gap_periodic_adv_start(EXT_ADV_HANDLE, true), test_sem); + while (1) { + vTaskDelay(2000 / portTICK_PERIOD_MS); + // just update the Advertising DID of the periodic advertising, unchanged data + FUNC_SEND_WAIT_SEM(esp_ble_gap_config_periodic_adv_data_raw(EXT_ADV_HANDLE, 0, NULL, true), test_sem); + } +#else + // set periodic adv raw data + FUNC_SEND_WAIT_SEM(esp_ble_gap_config_periodic_adv_data_raw(EXT_ADV_HANDLE, sizeof(periodic_adv_raw_data), &periodic_adv_raw_data[0]), test_sem); FUNC_SEND_WAIT_SEM(esp_ble_gap_periodic_adv_start(EXT_ADV_HANDLE), test_sem); +#endif return; }