From e2af75b5e41a391a42f2474e6ca9a3648a8fba6e Mon Sep 17 00:00:00 2001 From: zwj Date: Thu, 18 Feb 2021 16:25:02 +0800 Subject: [PATCH 1/5] add BLE connection establishment retry --- components/bt/host/bluedroid/Kconfig.in | 8 + .../include/common/bluedroid_user_config.h | 7 + .../common/include/common/bt_target.h | 12 + .../host/bluedroid/stack/btm/btm_ble_5_gap.c | 232 ++++++++++-------- .../bt/host/bluedroid/stack/btm/btm_ble_gap.c | 12 + .../bt/host/bluedroid/stack/btm/btm_main.c | 11 + .../bluedroid/stack/l2cap/include/l2c_int.h | 3 + .../bt/host/bluedroid/stack/l2cap/l2c_ble.c | 17 +- .../bt/host/bluedroid/stack/l2cap/l2c_link.c | 37 ++- .../bt/host/bluedroid/stack/l2cap/l2c_utils.c | 24 ++ 10 files changed, 262 insertions(+), 101 deletions(-) diff --git a/components/bt/host/bluedroid/Kconfig.in b/components/bt/host/bluedroid/Kconfig.in index 522a3821b4..e5ba243bbe 100644 --- a/components/bt/host/bluedroid/Kconfig.in +++ b/components/bt/host/bluedroid/Kconfig.in @@ -193,6 +193,14 @@ config BT_GATTC_CACHE_NVS_FLASH help This select can save gattc cache data to nvs flash +config BT_GATTC_CONNECT_RETRY_COUNT + int "The number of attempts to reconnect if the connection establishment failed" + depends on BT_GATTC_ENABLE + range 1 7 + default 3 + help + The number of attempts to reconnect if the connection establishment failed + config BT_BLE_SMP_ENABLE bool "Include BLE security module(SMP)" depends on BT_BLE_ENABLED 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 a286389fbc..93804a08b9 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 @@ -134,6 +134,13 @@ #define UC_BT_GATTC_CACHE_NVS_FLASH_ENABLED FALSE #endif +#ifdef CONFIG_BT_GATTC_CONNECT_RETRY_COUNT +#define UC_BT_GATTC_CONNECT_RETRY_COUNT CONFIG_BT_GATTC_CONNECT_RETRY_COUNT +#else +#define UC_BT_GATTC_CONNECT_RETRY_COUNT 0 +#endif + + //SMP #ifdef CONFIG_BT_SMP_ENABLE #define UC_BT_SMP_ENABLE CONFIG_BT_SMP_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 d3b991a7f0..c376462122 100644 --- a/components/bt/host/bluedroid/common/include/common/bt_target.h +++ b/components/bt/host/bluedroid/common/include/common/bt_target.h @@ -202,6 +202,18 @@ #define GATTC_CACHE_NVS FALSE #endif /* UC_BT_GATTC_ENABLE && UC_BT_GATTC_CACHE_NVS_FLASH_ENABLED */ +#if (UC_BT_GATTC_ENABLE && UC_BT_GATTC_CONNECT_RETRY_COUNT) +#define GATTC_CONNECT_RETRY_COUNT UC_BT_GATTC_CONNECT_RETRY_COUNT +#else +#define GATTC_CONNECT_RETRY_COUNT 0 +#endif /* UC_BT_GATTC_ENABLE && UC_BT_GATTC_CONNECT_RETRY_COUNT */ + +#if (GATTC_CONNECT_RETRY_COUNT > 0) +#define GATTC_CONNECT_RETRY_EN TRUE +#else +#define GATTC_CONNECT_RETRY_EN FALSE +#endif + #if (UC_BT_SMP_ENABLE) #define SMP_INCLUDED TRUE #if (BLE_INCLUDED == TRUE) 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 dd66fc74fd..d9210ce967 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 @@ -23,9 +23,23 @@ tBTM_BLE_EXTENDED_CB extend_adv_cb; tBTM_BLE_5_HCI_CBACK ble_5_hci_cb; +#define INVALID_VALUE 0XFF + static tBTM_STATUS btm_ble_ext_adv_params_validate(tBTM_BLE_GAP_EXT_ADV_PARAMS *params); static tBTM_STATUS btm_ble_ext_adv_set_data_validate(UINT8 instance, UINT16 len, UINT8 *data); +typedef struct { + uint16_t ter_con_handle; + bool invalid; + UINT8 instance; + int duration; + int max_events; + uint8_t retry_count; +} tBTM_EXT_ADV_RECORD; + +tBTM_EXT_ADV_RECORD adv_record[MAX_BLE_ADV_INSTANCE] = {0}; +extern void btm_ble_inter_set(bool extble_inter); + static char *btm_ble_hci_status_to_str(tHCI_STATUS status) { switch(status) { @@ -176,6 +190,16 @@ static char *btm_ble_hci_status_to_str(tHCI_STATUS status) return NULL; } +void btm_ble_extendadvcb_init(void) +{ + memset(&extend_adv_cb, 0, sizeof(tBTM_BLE_EXTENDED_CB)); +} + +void btm_ble_advrecod_init(void) +{ + memset(&adv_record[0], 0, sizeof(tBTM_EXT_ADV_RECORD)*MAX_BLE_ADV_INSTANCE); +} + void BTM_BleGapRegisterCallback(tBTM_BLE_5_HCI_CBACK cb) { if (cb) { @@ -185,6 +209,15 @@ void BTM_BleGapRegisterCallback(tBTM_BLE_5_HCI_CBACK cb) } } +void BTM_ExtBleCallbackTrigger(tBTM_BLE_5_GAP_EVENT event, tBTM_BLE_5_GAP_CB_PARAMS *params) +{ + if(params && params->status == BTM_SUCCESS) { + btm_ble_inter_set(true); + } + if (ble_5_hci_cb) { + ble_5_hci_cb(event, params); + } +} tBTM_STATUS BTM_BleReadPhy(BD_ADDR bd_addr, UINT8 *tx_phy, UINT8 *rx_phy) { @@ -221,9 +254,9 @@ tBTM_STATUS BTM_BleSetPreferDefaultPhy(UINT8 tx_phy_mask, UINT8 rx_phy_mask) } cb_params.set_perf_def_phy.status = err; - if (ble_5_hci_cb) { - ble_5_hci_cb(BTM_BLE_5_GAP_SET_PREFERED_DEFAULT_PHY_COMPLETE_EVT, &cb_params); - } + + BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_SET_PREFERED_DEFAULT_PHY_COMPLETE_EVT, &cb_params); + return status; } @@ -247,9 +280,7 @@ tBTM_STATUS BTM_BleSetPreferPhy(BD_ADDR bd_addr, UINT8 all_phys, UINT8 tx_phy_ma if (!btsnd_hcic_ble_set_phy(p_lcb->handle, all_phys, tx_phy_mask, rx_phy_mask, phy_options)) { cb_params.status = BTM_ILLEGAL_VALUE; - if (ble_5_hci_cb) { - ble_5_hci_cb(BTM_BLE_5_GAP_SET_PREFERED_PHY_COMPLETE_EVT, &cb_params); - } + BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_SET_PREFERED_PHY_COMPLETE_EVT, &cb_params); } @@ -295,9 +326,7 @@ tBTM_STATUS BTM_BleSetExtendedAdvRandaddr(UINT8 instance, BD_ADDR rand_addr) end: cb_params.status = status; - if (ble_5_hci_cb) { - ble_5_hci_cb(BTM_BLE_5_GAP_EXT_ADV_SET_RAND_ADDR_COMPLETE_EVT, &cb_params); - } + BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_EXT_ADV_SET_RAND_ADDR_COMPLETE_EVT, &cb_params); return status; @@ -351,10 +380,7 @@ tBTM_STATUS BTM_BleSetExtendedAdvParams(UINT8 instance, tBTM_BLE_GAP_EXT_ADV_PAR end: cb_params.status = status; - - if (ble_5_hci_cb) { - ble_5_hci_cb(BTM_BLE_5_GAP_EXT_ADV_SET_PARAMS_COMPLETE_EVT, &cb_params); - } + BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_EXT_ADV_SET_PARAMS_COMPLETE_EVT, &cb_params); return status; } @@ -405,10 +431,7 @@ tBTM_STATUS BTM_BleConfigExtendedAdvDataRaw(BOOLEAN is_scan_rsp, UINT8 instance, end: cb_params.status = status; - - if (ble_5_hci_cb) { - ble_5_hci_cb(is_scan_rsp ? BTM_BLE_5_GAP_EXT_SCAN_RSP_DATA_SET_COMPLETE_EVT : BTM_BLE_5_GAP_EXT_ADV_DATA_SET_COMPLETE_EVT, &cb_params); - } + BTM_ExtBleCallbackTrigger(is_scan_rsp ? BTM_BLE_5_GAP_EXT_SCAN_RSP_DATA_SET_COMPLETE_EVT : BTM_BLE_5_GAP_EXT_ADV_DATA_SET_COMPLETE_EVT, &cb_params); return status; } @@ -475,17 +498,72 @@ end: for (int i = 0; i < MAX_BLE_ADV_INSTANCE; i++) { extend_adv_cb.inst[i].configured = false; } + // disable all ext adv + if(num == 0) { + + for (uint8_t i = 0; i < MAX_BLE_ADV_INSTANCE; i++) + { + adv_record[i].invalid = false; + adv_record[i].instance = INVALID_VALUE; + adv_record[i].duration = INVALID_VALUE; + adv_record[i].max_events = INVALID_VALUE; + adv_record[i].retry_count = 0; + } + } else { + for (uint8_t i = 0; i < num; i++) + { + uint8_t index = ext_adv[i].instance; + adv_record[index].invalid = false; + adv_record[index].instance = INVALID_VALUE; + adv_record[index].duration = INVALID_VALUE; + adv_record[index].max_events = INVALID_VALUE; + adv_record[index].retry_count = 0; + } + } + } + // start extend adv success, save the adv information + if(enable && status == BTM_SUCCESS) { + for (uint8_t i = 0; i < num; i++) + { + uint8_t index = ext_adv[i].instance; + adv_record[index].invalid = true; + adv_record[index].instance = ext_adv[i].instance; + adv_record[index].duration = ext_adv[i].duration; + adv_record[index].max_events = ext_adv[i].max_events; + adv_record[index].retry_count = 0; + } } cb_params.status = status; - - if (ble_5_hci_cb) { - ble_5_hci_cb(enable ? BTM_BLE_5_GAP_EXT_ADV_START_COMPLETE_EVT : BTM_BLE_5_GAP_EXT_ADV_STOP_COMPLETE_EVT, &cb_params); - } + BTM_ExtBleCallbackTrigger(enable ? BTM_BLE_5_GAP_EXT_ADV_START_COMPLETE_EVT : BTM_BLE_5_GAP_EXT_ADV_STOP_COMPLETE_EVT, &cb_params); return status; } +tBTM_STATUS BTM_BleStartExtAdvRestart(uint8_t con_handle) +{ + tBTM_BLE_EXT_ADV ext_adv; + uint8_t index = INVALID_VALUE; + for (uint8_t i = 0; i < MAX_BLE_ADV_INSTANCE; i++) + { + if(adv_record[i].ter_con_handle == con_handle) { + index = i; + break; + } + } + + if((index >= MAX_BLE_ADV_INSTANCE) || (!adv_record[index].invalid) || (adv_record[index].retry_count > GATTC_CONNECT_RETRY_COUNT)) { + return BTM_WRONG_MODE; + } + + adv_record[index].retry_count ++; + BTM_TRACE_DEBUG("remote device did not reveive aux connect response, retatrt the extend adv to reconnect, adv handle %d con_handle %d\n", index, con_handle); + ext_adv.instance = adv_record[index].instance; + ext_adv.duration = adv_record[index].duration; + ext_adv.max_events = adv_record[index].max_events; + return BTM_BleStartExtAdv(true, 1, &ext_adv); +} + tBTM_STATUS BTM_BleExtAdvSetRemove(UINT8 instance) { tBTM_STATUS status = BTM_SUCCESS; @@ -507,9 +585,7 @@ end: cb_params.status = status; - if (ble_5_hci_cb) { - ble_5_hci_cb(BTM_BLE_5_GAP_EXT_ADV_SET_REMOVE_COMPLETE_EVT, &cb_params); - } + BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_EXT_ADV_SET_REMOVE_COMPLETE_EVT, &cb_params); return status; } @@ -527,9 +603,7 @@ tBTM_STATUS BTM_BleExtAdvSetClear(void) cb_params.status = status; - if (ble_5_hci_cb) { - ble_5_hci_cb(BTM_BLE_5_GAP_EXT_ADV_SET_CLEAR_COMPLETE_EVT, &cb_params); - } + BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_EXT_ADV_SET_CLEAR_COMPLETE_EVT, &cb_params); return status; } @@ -567,9 +641,7 @@ end: cb_params.status = status; - if (ble_5_hci_cb) { - ble_5_hci_cb(BTM_BLE_5_GAP_PERIODIC_ADV_SET_PARAMS_COMPLETE_EVT, &cb_params); - } + BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_PERIODIC_ADV_SET_PARAMS_COMPLETE_EVT, &cb_params); return status; } @@ -620,9 +692,7 @@ tBTM_STATUS BTM_BlePeriodicAdvCfgDataRaw(UINT8 instance, UINT16 len, UINT8 *data cb_params.status = status; - if (ble_5_hci_cb) { - ble_5_hci_cb(BTM_BLE_5_GAP_PERIODIC_ADV_DATA_SET_COMPLETE_EVT, &cb_params); - } + BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_PERIODIC_ADV_DATA_SET_COMPLETE_EVT, &cb_params); return status; } @@ -648,9 +718,7 @@ end: cb_params.status = status; - if (ble_5_hci_cb) { - ble_5_hci_cb(enable ? BTM_BLE_5_GAP_PERIODIC_ADV_START_COMPLETE_EVT : BTM_BLE_5_GAP_PERIODIC_ADV_STOP_COMPLETE_EVT, &cb_params); - } + BTM_ExtBleCallbackTrigger(enable ? BTM_BLE_5_GAP_PERIODIC_ADV_START_COMPLETE_EVT : BTM_BLE_5_GAP_PERIODIC_ADV_STOP_COMPLETE_EVT, &cb_params); return status; @@ -683,12 +751,11 @@ tBTM_STATUS BTM_BlePeriodicAdvCreateSync(tBTM_BLE_Periodic_Sync_Params *params) } end: - if((status != BTM_SUCCESS) && ble_5_hci_cb) { + if(status != BTM_SUCCESS) { cb_params.status = status; - ble_5_hci_cb(BTM_BLE_5_GAP_PERIODIC_ADV_CREATE_SYNC_COMPLETE_EVT, &cb_params); + BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_PERIODIC_ADV_CREATE_SYNC_COMPLETE_EVT, &cb_params); } - return status; } void btm_set_phy_callback(UINT8 status) @@ -696,9 +763,7 @@ void btm_set_phy_callback(UINT8 status) tBTM_BLE_5_GAP_CB_PARAMS cb_params = {0}; cb_params.status = status; - if (ble_5_hci_cb) { - ble_5_hci_cb(BTM_BLE_5_GAP_SET_PREFERED_PHY_COMPLETE_EVT, &cb_params); - } + BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_SET_PREFERED_PHY_COMPLETE_EVT, &cb_params); } void btm_create_sync_callback(UINT8 status) @@ -706,9 +771,7 @@ void btm_create_sync_callback(UINT8 status) tBTM_BLE_5_GAP_CB_PARAMS cb_params = {0}; cb_params.status = status; - if (ble_5_hci_cb) { - ble_5_hci_cb(BTM_BLE_5_GAP_PERIODIC_ADV_CREATE_SYNC_COMPLETE_EVT, &cb_params); - } + BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_PERIODIC_ADV_CREATE_SYNC_COMPLETE_EVT, &cb_params); } void btm_read_phy_callback(uint8_t hci_status, uint16_t conn_handle, uint8_t tx_phy, uint8_t rx_phy) @@ -727,9 +790,7 @@ void btm_read_phy_callback(uint8_t hci_status, uint16_t conn_handle, uint8_t tx_ cb_params.read_phy.tx_phy = tx_phy; cb_params.read_phy.rx_phy = rx_phy; - if (ble_5_hci_cb) { - ble_5_hci_cb(BTM_BLE_5_GAP_READ_PHY_COMPLETE_EVT, &cb_params); - } + BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_READ_PHY_COMPLETE_EVT, &cb_params); } tBTM_STATUS BTM_BlePeriodicAdvSyncCancel(void) @@ -745,9 +806,7 @@ tBTM_STATUS BTM_BlePeriodicAdvSyncCancel(void) cb_params.status = status; - if (ble_5_hci_cb) { - ble_5_hci_cb(BTM_BLE_5_GAP_PERIODIC_ADV_SYNC_CANCEL_COMPLETE_EVT, &cb_params); - } + BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_PERIODIC_ADV_SYNC_CANCEL_COMPLETE_EVT, &cb_params); return status; } @@ -765,9 +824,7 @@ tBTM_STATUS BTM_BlePeriodicAdvSyncTerm(UINT16 sync_handle) cb_params.status = status; - if (ble_5_hci_cb) { - ble_5_hci_cb(BTM_BLE_5_GAP_PERIODIC_ADV_SYNC_TERMINATE_COMPLETE_EVT, &cb_params); - } + BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_PERIODIC_ADV_SYNC_TERMINATE_COMPLETE_EVT, &cb_params); return status; } @@ -791,9 +848,7 @@ tBTM_STATUS BTM_BlePeriodicAdvAddDevToList(tBLE_ADDR_TYPE addr_type, BD_ADDR add end: cb_params.status = status; - if (ble_5_hci_cb) { - ble_5_hci_cb(BTM_BLE_5_GAP_PERIODIC_ADV_ADD_DEV_COMPLETE_EVT, &cb_params); - } + BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_PERIODIC_ADV_ADD_DEV_COMPLETE_EVT, &cb_params); return status; } @@ -818,9 +873,7 @@ tBTM_STATUS BTM_BlePeriodicAdvRemoveDevFromList(tBLE_ADDR_TYPE addr_type, BD_ADD end: cb_params.status = status; - if (ble_5_hci_cb) { - ble_5_hci_cb(BTM_BLE_5_GAP_PERIODIC_ADV_REMOVE_DEV_COMPLETE_EVT, &cb_params); - } + BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_PERIODIC_ADV_REMOVE_DEV_COMPLETE_EVT, &cb_params); return status; } @@ -836,9 +889,7 @@ tBTM_STATUS BTM_BlePeriodicAdvClearDev(void) } cb_params.status = status; - if (ble_5_hci_cb) { - ble_5_hci_cb(BTM_BLE_5_GAP_PERIODIC_ADV_CLEAR_DEV_COMPLETE_EVT, &cb_params); - } + BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_PERIODIC_ADV_CLEAR_DEV_COMPLETE_EVT, &cb_params); return status; } @@ -887,9 +938,7 @@ end: cb_params.status = status; - if (ble_5_hci_cb) { - ble_5_hci_cb(BTM_BLE_5_GAP_SET_EXT_SCAN_PARAMS_COMPLETE_EVT, &cb_params); - } + BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_SET_EXT_SCAN_PARAMS_COMPLETE_EVT, &cb_params); return cb_params.status; } @@ -915,9 +964,7 @@ end: cb_params.status = status; - if (ble_5_hci_cb) { - ble_5_hci_cb(enable ? BTM_BLE_5_GAP_EXT_SCAN_START_COMPLETE_EVT : BTM_BLE_5_GAP_EXT_SCAN_STOP_COMPLETE_EVT, &cb_params); - } + BTM_ExtBleCallbackTrigger(enable ? BTM_BLE_5_GAP_EXT_SCAN_START_COMPLETE_EVT : BTM_BLE_5_GAP_EXT_SCAN_STOP_COMPLETE_EVT, &cb_params); return status; } @@ -1041,20 +1088,15 @@ void btm_ble_update_phy_evt(tBTM_BLE_UPDATE_PHY *params) memcpy(cb_params.phy_update.addr, p_lcb->remote_bd_addr, BD_ADDR_LEN); // If the user has register the callback function, should callback it to the application. - if (ble_5_hci_cb) { - ble_5_hci_cb(BTM_BLE_5_GAP_PHY_UPDATE_COMPLETE_EVT, &cb_params); - } + + BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_PHY_UPDATE_COMPLETE_EVT, &cb_params); return; } void btm_ble_scan_timeout_evt(void) { - if (ble_5_hci_cb) { - ble_5_hci_cb(BTM_BLE_5_GAP_SCAN_TIMEOUT_EVT, NULL); - } - - return; + BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_SCAN_TIMEOUT_EVT, NULL); } void btm_ble_adv_set_terminated_evt(tBTM_BLE_ADV_TERMINAT *params) @@ -1066,12 +1108,18 @@ void btm_ble_adv_set_terminated_evt(tBTM_BLE_ADV_TERMINAT *params) return; } + // adv terminated due to connection, save the adv handle and connection handle + if(params->completed_event == 0x00) { + adv_record[params->adv_handle].ter_con_handle = params->conn_handle; + } else { + adv_record[params->adv_handle].ter_con_handle = INVALID_VALUE; + adv_record[params->adv_handle].invalid = false; + } + memcpy(&cb_params.adv_term, params, sizeof(tBTM_BLE_ADV_TERMINAT)); // If the user has register the callback function, should callback it to the application. - if (ble_5_hci_cb) { - ble_5_hci_cb(BTM_BLE_5_GAP_ADV_TERMINATED_EVT, &cb_params); - } + BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_ADV_TERMINATED_EVT, &cb_params); return; } @@ -1088,9 +1136,7 @@ void btm_ble_ext_adv_report_evt(tBTM_BLE_EXT_ADV_REPORT *params) memcpy(&cb_params.ext_adv_report, params, sizeof(tBTM_BLE_EXT_ADV_REPORT)); // If the user has register the callback function, should callback it to the application. - if (ble_5_hci_cb) { - ble_5_hci_cb(BTM_BLE_5_GAP_EXT_ADV_REPORT_EVT, &cb_params); - } + BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_EXT_ADV_REPORT_EVT, &cb_params); return; @@ -1108,9 +1154,7 @@ void btm_ble_scan_req_received_evt(tBTM_BLE_SCAN_REQ_RECEIVED *params) memcpy(&cb_params.scan_req, params, sizeof(tBTM_BLE_SCAN_REQ_RECEIVED)); // If the user has register the callback function, should callback it to the application. - if (ble_5_hci_cb) { - ble_5_hci_cb(BTM_BLE_5_GAP_SCAN_REQ_RECEIVED_EVT, &cb_params); - } + BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_SCAN_REQ_RECEIVED_EVT, &cb_params); return; } @@ -1127,9 +1171,7 @@ void btm_ble_channel_select_algorithm_evt(tBTM_BLE_CHANNEL_SEL_ALG *params) memcpy(&cb_params.channel_sel, params, sizeof(tBTM_BLE_CHANNEL_SEL_ALG)); // If the user has register the callback function, should callback it to the application. - if (ble_5_hci_cb) { - ble_5_hci_cb(BTM_BLE_5_GAP_CHANNEL_SELETE_ALGORITHM_EVT, &cb_params); - } + BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_CHANNEL_SELETE_ALGORITHM_EVT, &cb_params); return; } @@ -1146,9 +1188,7 @@ void btm_ble_periodic_adv_report_evt(tBTM_PERIOD_ADV_REPORT *params) memcpy(&cb_params.period_adv_report, params, sizeof(tBTM_PERIOD_ADV_REPORT)); // If the user has register the callback function, should callback it to the application. - if (ble_5_hci_cb) { - ble_5_hci_cb(BTM_BLE_5_GAP_PERIODIC_ADV_REPORT_EVT, &cb_params); - } + BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_PERIODIC_ADV_REPORT_EVT, &cb_params); return; @@ -1166,9 +1206,7 @@ void btm_ble_periodic_adv_sync_lost_evt(tBTM_BLE_PERIOD_ADV_SYNC_LOST *params) memcpy(&cb_params.sync_lost, params, sizeof(tBTM_BLE_PERIOD_ADV_SYNC_LOST)); // If the user has register the callback function, should callback it to the application. - if (ble_5_hci_cb) { - ble_5_hci_cb(BTM_BLE_5_GAP_PERIODIC_ADV_SYNC_LOST_EVT, &cb_params); - } + BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_PERIODIC_ADV_SYNC_LOST_EVT, &cb_params); return; @@ -1186,9 +1224,7 @@ void btm_ble_periodic_adv_sync_establish_evt(tBTM_BLE_PERIOD_ADV_SYNC_ESTAB *par memcpy(&cb_params.sync_estab, params, sizeof(tBTM_BLE_PERIOD_ADV_SYNC_ESTAB)); // If the user has register the callback function, should callback it to the application. - if (ble_5_hci_cb) { - ble_5_hci_cb(BTM_BLE_5_GAP_PERIODIC_ADV_SYNC_ESTAB_EVT, &cb_params); - } + BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_PERIODIC_ADV_SYNC_ESTAB_EVT, &cb_params); return; 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 8a7382df6a..0a2245915b 100644 --- a/components/bt/host/bluedroid/stack/btm/btm_ble_gap.c +++ b/components/bt/host/bluedroid/stack/btm/btm_ble_gap.c @@ -88,6 +88,18 @@ static void btm_ble_stop_discover(void); #define BTM_BLE_SEL_CONN_RESULT 0x04 #define BTM_BLE_DISCO_RESULT 0x08 +static bool is_ble50_inter = false; + +void btm_ble_inter_set(bool extble_inter) +{ + is_ble50_inter = extble_inter; +} + +bool btm_ble_inter_get(void) +{ + return is_ble50_inter; +} + /* LE states combo bit to check */ const UINT8 btm_le_state_combo_tbl[BTM_BLE_STATE_MAX][BTM_BLE_STATE_MAX][2] = { {/* single state support */ diff --git a/components/bt/host/bluedroid/stack/btm/btm_main.c b/components/bt/host/bluedroid/stack/btm/btm_main.c index 9c13240739..5ddfa85a99 100644 --- a/components/bt/host/bluedroid/stack/btm/btm_main.c +++ b/components/bt/host/bluedroid/stack/btm/btm_main.c @@ -37,6 +37,12 @@ tBTM_CB btm_cb; tBTM_CB *btm_cb_ptr; #endif +#if (BLE_50_FEATURE_SUPPORT == TRUE) +extern void btm_ble_extendadvcb_init(void); +extern void btm_ble_advrecod_init(void); +#endif + + /******************************************************************************* ** ** Function btm_init @@ -80,6 +86,11 @@ void btm_init (void) btm_ble_sem_init(); #endif btm_sec_dev_init(); +#if (BLE_50_FEATURE_SUPPORT == TRUE) + btm_ble_extendadvcb_init(); + btm_ble_advrecod_init(); +#endif + } diff --git a/components/bt/host/bluedroid/stack/l2cap/include/l2c_int.h b/components/bt/host/bluedroid/stack/l2cap/include/l2c_int.h index 66e221dd53..3c293b79d2 100644 --- a/components/bt/host/bluedroid/stack/l2cap/include/l2c_int.h +++ b/components/bt/host/bluedroid/stack/l2cap/include/l2c_int.h @@ -455,6 +455,9 @@ typedef struct t_l2c_linkcb { /* connection parameters update order: waiting_update_conn_xx -> updating_conn_xx -> current_used_conn_xx */ + /* create connection retry count*/ + UINT8 retry_create_con; + UINT32 start_time_s; #endif #if (L2CAP_ROUND_ROBIN_CHANNEL_SERVICE == TRUE) diff --git a/components/bt/host/bluedroid/stack/l2cap/l2c_ble.c b/components/bt/host/bluedroid/stack/l2cap/l2c_ble.c index 2507d6a275..db7a051950 100644 --- a/components/bt/host/bluedroid/stack/l2cap/l2c_ble.c +++ b/components/bt/host/bluedroid/stack/l2cap/l2c_ble.c @@ -51,6 +51,7 @@ const tHCI_ExtConnParams ext_conn_params = { #endif // #if (BLE_50_FEATURE_SUPPORT == TRUE) static BOOLEAN l2cble_start_conn_update (tL2C_LCB *p_lcb); +extern int64_t esp_system_get_time(void); /******************************************************************************* ** @@ -847,6 +848,18 @@ BOOLEAN l2cble_init_direct_conn (tL2C_LCB *p_lcb) L2CAP_TRACE_ERROR("initate direct connection fail, topology limitation"); return FALSE; } + uint32_t link_timeout = L2CAP_BLE_LINK_CONNECT_TOUT; + if(GATTC_CONNECT_RETRY_COUNT) { + if(!p_lcb->retry_create_con) { + p_lcb->start_time_s = (esp_system_get_time()/1000); + } + uint32_t current_time = (esp_system_get_time()/1000); + link_timeout = (L2CAP_BLE_LINK_CONNECT_TOUT*1000 - (current_time - p_lcb->start_time_s))/1000; + + if(link_timeout == 0 || link_timeout > L2CAP_BLE_LINK_CONNECT_TOUT) { + link_timeout = L2CAP_BLE_LINK_CONNECT_TOUT; + } + } if (!p_lcb->is_aux) { if (!btsnd_hcic_ble_create_ll_conn (scan_int,/* UINT16 scan_int */ @@ -872,7 +885,7 @@ BOOLEAN l2cble_init_direct_conn (tL2C_LCB *p_lcb) p_lcb->link_state = LST_CONNECTING; l2cb.is_ble_connecting = TRUE; memcpy (l2cb.ble_connecting_bda, p_lcb->remote_bd_addr, BD_ADDR_LEN); - btu_start_timer (&p_lcb->timer_entry, BTU_TTYPE_L2CAP_LINK, L2CAP_BLE_LINK_CONNECT_TOUT); + btu_start_timer (&p_lcb->timer_entry, BTU_TTYPE_L2CAP_LINK, link_timeout); btm_ble_set_conn_st (BLE_DIR_CONN); return (TRUE); @@ -899,7 +912,7 @@ BOOLEAN l2cble_init_direct_conn (tL2C_LCB *p_lcb) p_lcb->link_state = LST_CONNECTING; l2cb.is_ble_connecting = TRUE; memcpy (l2cb.ble_connecting_bda, p_lcb->remote_bd_addr, BD_ADDR_LEN); - btu_start_timer (&p_lcb->timer_entry, BTU_TTYPE_L2CAP_LINK, L2CAP_BLE_LINK_CONNECT_TOUT); + btu_start_timer (&p_lcb->timer_entry, BTU_TTYPE_L2CAP_LINK, link_timeout); btm_ble_set_conn_st (BLE_DIR_CONN); if(!btsnd_hcic_ble_create_ext_conn(&aux_conn)) { l2cu_release_lcb (p_lcb); diff --git a/components/bt/host/bluedroid/stack/l2cap/l2c_link.c b/components/bt/host/bluedroid/stack/l2cap/l2c_link.c index 2791c4c175..fabf032abe 100644 --- a/components/bt/host/bluedroid/stack/l2cap/l2c_link.c +++ b/components/bt/host/bluedroid/stack/l2cap/l2c_link.c @@ -42,6 +42,11 @@ static BOOLEAN l2c_link_send_to_lower (tL2C_LCB *p_lcb, BT_HDR *p_buf); +#if (BLE_50_FEATURE_SUPPORT == TRUE) +extern tBTM_STATUS BTM_BleStartExtAdvRestart(uint8_t handle); +#endif// #if (BLE_50_FEATURE_SUPPORT == TRUE) +extern bool btm_ble_inter_get(void); + /******************************************************************************* ** ** Function l2c_link_hci_conn_req @@ -333,7 +338,6 @@ void l2c_link_sec_comp (BD_ADDR p_bda, tBT_TRANSPORT transport, void *p_ref_data } } - /******************************************************************************* ** ** Function l2c_link_hci_disc_comp @@ -456,7 +460,38 @@ BOOLEAN l2c_link_hci_disc_comp (UINT16 handle, UINT8 reason) } p_lcb->p_pending_ccb = NULL; +#if (BLE_INCLUDED == TRUE && GATTC_CONNECT_RETRY_EN == TRUE) + if(reason == HCI_ERR_CONN_FAILED_ESTABLISHMENT && p_lcb->transport == BT_TRANSPORT_LE) { + if(p_lcb->link_role == HCI_ROLE_MASTER && p_lcb->retry_create_con < GATTC_CONNECT_RETRY_COUNT) { + L2CAP_TRACE_DEBUG("master retry connect, retry count %d reason 0x%x\n", p_lcb->retry_create_con, reason); + p_lcb->retry_create_con ++; + // create connection retry + if (l2cu_create_conn(p_lcb, BT_TRANSPORT_LE)) { + btm_acl_removed (p_lcb->remote_bd_addr, BT_TRANSPORT_LE); + lcb_is_free = FALSE; /* still using this lcb */ + } + } + + #if (BLE_50_FEATURE_SUPPORT == TRUE) + if(btm_ble_inter_get() && p_lcb->link_role == HCI_ROLE_SLAVE && p_lcb->retry_create_con < GATTC_CONNECT_RETRY_COUNT) { + p_lcb->retry_create_con ++; + L2CAP_TRACE_DEBUG("slave restart extend adv, retry count %d reason 0x%x\n", p_lcb->retry_create_con, reason); + BTM_BleStartExtAdvRestart(handle); + } + #endif // #if (BLE_50_FEATURE_SUPPORT == TRUE) + + #if (BLE_42_FEATURE_SUPPORT == TRUE) + if(!btm_ble_inter_get() && p_lcb->link_role == HCI_ROLE_SLAVE && p_lcb->retry_create_con < GATTC_CONNECT_RETRY_COUNT) { + p_lcb->retry_create_con ++; + L2CAP_TRACE_DEBUG("slave resatrt adv, retry count %d reason 0x%x\n", p_lcb->retry_create_con, reason); + btm_ble_start_adv(); + } + #endif // #if (BLE_42_FEATURE_SUPPORT == TRUE) + } + + +#endif // #if (BLE_INCLUDED == TRUE) /* Release the LCB */ if (lcb_is_free) { l2cu_release_lcb (p_lcb); diff --git a/components/bt/host/bluedroid/stack/l2cap/l2c_utils.c b/components/bt/host/bluedroid/stack/l2cap/l2c_utils.c index a82d8172cb..2eaefe85b9 100644 --- a/components/bt/host/bluedroid/stack/l2cap/l2c_utils.c +++ b/components/bt/host/bluedroid/stack/l2cap/l2c_utils.c @@ -158,6 +158,10 @@ void l2cu_release_lcb (tL2C_LCB *p_lcb) p_lcb->in_use = FALSE; p_lcb->is_bonding = FALSE; +#if (BLE_INCLUDED == TRUE) + p_lcb->retry_create_con = 0; + p_lcb->start_time_s = 0; +#endif // #if (BLE_INCLUDED == TRUE) /* Stop and release timers */ btu_free_timer (&p_lcb->timer_entry); @@ -335,6 +339,26 @@ tL2C_LCB *l2cu_find_free_lcb (void) return (NULL); } +uint8_t l2cu_plcb_active_count(void) +{ + list_node_t *p_node = NULL; + tL2C_LCB *p_lcb = NULL; + uint8_t active_count = 0; + for (p_node = list_begin(l2cb.p_lcb_pool); p_node; p_node = list_next(p_node)) { + p_lcb = list_node(p_node); + if (p_lcb && p_lcb->in_use) { + active_count ++; + } + } + if (active_count >= MAX_L2CAP_CHANNELS) { + L2CAP_TRACE_ERROR("error active count"); + active_count = 0; + } + L2CAP_TRACE_DEBUG("plcb active count %d", active_count); + return active_count; + +} + /******************************************************************************* ** ** Function l2cu_get_conn_role From fe2f3bfe1e35d994efcedbaa8bc494c87adf71ba Mon Sep 17 00:00:00 2001 From: zwj Date: Wed, 7 Apr 2021 16:07:17 +0800 Subject: [PATCH 2/5] fix ble 5.0 SMP failed --- .../host/bluedroid/stack/btm/btm_ble_5_gap.c | 55 +++- .../bt/host/bluedroid/stack/btm/btm_ble_gap.c | 255 ++++++++++-------- .../stack/include/stack/btm_ble_api.h | 3 - .../bt/host/bluedroid/stack/l2cap/l2c_ble.c | 19 +- 4 files changed, 207 insertions(+), 125 deletions(-) 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 d9210ce967..8a532a2faa 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 @@ -24,7 +24,10 @@ tBTM_BLE_EXTENDED_CB extend_adv_cb; tBTM_BLE_5_HCI_CBACK ble_5_hci_cb; #define INVALID_VALUE 0XFF - +extern BOOLEAN BTM_GetLocalResolvablePrivateAddr(BD_ADDR bda); +extern void BTM_UpdateAddrInfor(uint8_t addr_type, BD_ADDR bda); +extern void BTM_BleSetStaticAddr(BD_ADDR rand_addr); +extern uint32_t BTM_BleUpdateOwnType(uint8_t *own_bda_type, tBTM_START_ADV_CMPL_CBACK *cb); static tBTM_STATUS btm_ble_ext_adv_params_validate(tBTM_BLE_GAP_EXT_ADV_PARAMS *params); static tBTM_STATUS btm_ble_ext_adv_set_data_validate(UINT8 instance, UINT16 len, UINT8 *data); @@ -316,6 +319,12 @@ tBTM_STATUS BTM_BleSetExtendedAdvRandaddr(UINT8 instance, BD_ADDR rand_addr) BTM_TRACE_ERROR("%s, fail to send the hci command, the error code = %s(0x%x)", __func__, btm_ble_hci_status_to_str(err), err); status = BTM_ILLEGAL_VALUE; + } else { + // set random address success, update address infor + if(extend_adv_cb.inst[instance].configured && extend_adv_cb.inst[instance].connetable) { + BTM_BleSetStaticAddr(rand_addr); + BTM_UpdateAddrInfor(BLE_ADDR_RANDOM, rand_addr); + } } } else { BTM_TRACE_ERROR("%s invalid random address", __func__); @@ -336,6 +345,8 @@ tBTM_STATUS BTM_BleSetExtendedAdvParams(UINT8 instance, tBTM_BLE_GAP_EXT_ADV_PAR tBTM_STATUS status = BTM_SUCCESS; tHCI_STATUS err = HCI_SUCCESS; tBTM_BLE_5_GAP_CB_PARAMS cb_params = {0}; + bool use_rpa_addr = false; + BD_ADDR rand_addr; if (instance >= MAX_BLE_ADV_INSTANCE) { status = BTM_ILLEGAL_VALUE; @@ -364,6 +375,15 @@ tBTM_STATUS BTM_BleSetExtendedAdvParams(UINT8 instance, tBTM_BLE_GAP_EXT_ADV_PAR } else { extend_adv_cb.inst[instance].legacy_pdu = false; } + // if own_addr_type == BLE_ADDR_PUBLIC_ID or BLE_ADDR_RANDOM_ID, + if((params->own_addr_type == BLE_ADDR_PUBLIC_ID || params->own_addr_type == BLE_ADDR_RANDOM_ID) && BTM_GetLocalResolvablePrivateAddr(rand_addr)) { + params->own_addr_type = BLE_ADDR_RANDOM; + use_rpa_addr = true; + } else if(params->own_addr_type == BLE_ADDR_PUBLIC_ID){ + params->own_addr_type = BLE_ADDR_PUBLIC; + } else if (params->own_addr_type == BLE_ADDR_RANDOM_ID) { + params->own_addr_type = BLE_ADDR_RANDOM; + } if ((err = btsnd_hcic_ble_set_ext_adv_params(instance, params->type, params->interval_min, params->interval_max, params->channel_map, params->own_addr_type, params->peer_addr_type, @@ -372,13 +392,22 @@ tBTM_STATUS BTM_BleSetExtendedAdvParams(UINT8 instance, tBTM_BLE_GAP_EXT_ADV_PAR params->secondary_phy, params->sid, params->scan_req_notif)) != HCI_SUCCESS) { BTM_TRACE_ERROR("LE EA SetParams: cmd err=0x%x", err); status = BTM_ILLEGAL_VALUE; - goto end; + goto end; } extend_adv_cb.inst[instance].configured = true; end: - + if(use_rpa_addr) { + // update RPA address + if((err = btsnd_hcic_ble_set_extend_rand_address(instance, rand_addr)) != HCI_SUCCESS) { + BTM_TRACE_ERROR("LE EA SetParams: cmd err=0x%x", err); + status = BTM_ILLEGAL_VALUE; + } else { + // set addr success, update address infor + BTM_UpdateAddrInfor(BLE_ADDR_RANDOM, rand_addr); + } + } cb_params.status = status; BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_EXT_ADV_SET_PARAMS_COMPLETE_EVT, &cb_params); @@ -579,6 +608,12 @@ tBTM_STATUS BTM_BleExtAdvSetRemove(UINT8 instance) if ((err = btsnd_hcic_ble_remove_adv_set(instance)) != HCI_SUCCESS) { BTM_TRACE_ERROR("LE EAS Rm: cmd err=0x%x", err); status = BTM_ILLEGAL_VALUE; + } else { + extend_adv_cb.inst[instance].configured = false; + extend_adv_cb.inst[instance].legacy_pdu = false; + extend_adv_cb.inst[instance].directed = false; + extend_adv_cb.inst[instance].scannable = false; + extend_adv_cb.inst[instance].connetable = false; } end: @@ -599,6 +634,14 @@ tBTM_STATUS BTM_BleExtAdvSetClear(void) if ((err = btsnd_hcic_ble_clear_adv_set()) != HCI_SUCCESS) { BTM_TRACE_ERROR("LE EAS Clr: cmd err=0x%x", err); status = BTM_ILLEGAL_VALUE; + } else { + for (uint8_t i = 0; i < MAX_BLE_ADV_INSTANCE; i++) { + extend_adv_cb.inst[i].configured = false; + extend_adv_cb.inst[i].legacy_pdu = false; + extend_adv_cb.inst[i].directed = false; + extend_adv_cb.inst[i].scannable = false; + extend_adv_cb.inst[i].connetable = false; + } } cb_params.status = status; @@ -926,6 +969,12 @@ tBTM_STATUS BTM_BleSetExtendedScanParams(tBTM_BLE_EXT_SCAN_PARAMS *params) phy_count++; } + if (BTM_BleUpdateOwnType(¶ms->own_addr_type, NULL) != 0 ) { + status = BTM_ILLEGAL_VALUE; + BTM_TRACE_ERROR("LE UpdateOwnType err"); + goto end; + } + extend_adv_cb.scan_duplicate = params->scan_duplicate; if ((err = btsnd_hcic_ble_set_ext_scan_params(params->own_addr_type, params->filter_policy, phy_mask, phy_count, 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 0a2245915b..96b0118e78 100644 --- a/components/bt/host/bluedroid/stack/btm/btm_ble_gap.c +++ b/components/bt/host/bluedroid/stack/btm/btm_ble_gap.c @@ -82,6 +82,7 @@ static UINT8 btm_set_conn_mode_adv_init_addr(tBTM_BLE_INQ_CB *p_cb, tBLE_ADDR_TYPE *p_own_addr_type); static void btm_ble_stop_observe(void); static void btm_ble_stop_discover(void); +uint32_t BTM_BleUpdateOwnType(uint8_t *own_bda_type, tBTM_START_ADV_CMPL_CBACK *cb); #define BTM_BLE_INQ_RESULT 0x01 #define BTM_BLE_OBS_RESULT 0x02 @@ -920,6 +921,140 @@ BOOLEAN BTM_BleConfigPrivacy(BOOLEAN privacy_mode, tBTM_SET_LOCAL_PRIVACY_CBACK #endif } +/******************************************************************************* +** +** Function BTMGetLocalResolvablePrivateAddr +** +** Description This function is called to get local RPA address +** +** Parameters bda: address pointer. +** +** +*******************************************************************************/ + +BOOLEAN BTM_GetLocalResolvablePrivateAddr(BD_ADDR bda) +{ + tBTM_LE_RANDOM_CB *p_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb; + BTM_TRACE_DEBUG ("get owm resolvable random address"); + + if (bda) { + /* if privacy disabled, return false */ + if ((p_cb->exist_addr_bit & BTM_BLE_GAP_ADDR_BIT_RESOLVABLE) == BTM_BLE_GAP_ADDR_BIT_RESOLVABLE) { + memcpy(bda, p_cb->resolvale_addr, BD_ADDR_LEN); + BTM_TRACE_DEBUG("own resolvable random address: 0x%02x:%02x:%02x:%02x:%02x:%02x", + p_cb->resolvale_addr[0], p_cb->resolvale_addr[1], + p_cb->resolvale_addr[2], p_cb->resolvale_addr[3], + p_cb->resolvale_addr[4], p_cb->resolvale_addr[5]); + return TRUE; + } + + return FALSE; + } + + return FALSE; +} + +/******************************************************************************* +** +** Function BTM_UpdateAddrInfor +** +** Description This function is called to update address information +** +** Parameters addr_type: address type +** bda: address pointer. +** +** +*******************************************************************************/ +void BTM_UpdateAddrInfor(uint8_t addr_type, BD_ADDR bda) +{ + btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type = addr_type; + memcpy(btm_cb.ble_ctr_cb.addr_mgnt_cb.private_addr, bda, BD_ADDR_LEN); +} + +/******************************************************************************* +** +** Function BTM_BleSetStaticAddr +** +** Description This function is called to save random address +** +** Parameters rand_addr: address pointer. +** +** +*******************************************************************************/ +void BTM_BleSetStaticAddr(BD_ADDR rand_addr) +{ + memcpy(btm_cb.ble_ctr_cb.addr_mgnt_cb.static_rand_addr, rand_addr, BD_ADDR_LEN); + btm_cb.ble_ctr_cb.addr_mgnt_cb.exist_addr_bit |= BTM_BLE_GAP_ADDR_BIT_RANDOM; +} + +uint32_t BTM_BleUpdateOwnType(uint8_t *own_bda_type, tBTM_START_ADV_CMPL_CBACK *cb) +{ + if(*own_bda_type == BLE_ADDR_RANDOM) { + if((btm_cb.ble_ctr_cb.addr_mgnt_cb.exist_addr_bit & BTM_BLE_GAP_ADDR_BIT_RANDOM) == BTM_BLE_GAP_ADDR_BIT_RANDOM) { + //close privacy + #if BLE_PRIVACY_SPT == TRUE + if (btm_cb.ble_ctr_cb.privacy_mode != BTM_PRIVACY_NONE) { + BTM_BleConfigPrivacy(FALSE, NULL); + } + #endif + btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type = BLE_ADDR_RANDOM; + memcpy(btm_cb.ble_ctr_cb.addr_mgnt_cb.private_addr, btm_cb.ble_ctr_cb.addr_mgnt_cb.static_rand_addr, BD_ADDR_LEN); + // set address to controller + btsnd_hcic_ble_set_random_addr(btm_cb.ble_ctr_cb.addr_mgnt_cb.static_rand_addr); + + } else if((btm_cb.ble_ctr_cb.addr_mgnt_cb.exist_addr_bit & BTM_BLE_GAP_ADDR_BIT_RESOLVABLE) == BTM_BLE_GAP_ADDR_BIT_RESOLVABLE) { + btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type = BLE_ADDR_RANDOM; + 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"); + if(cb) { + (* cb)(HCI_ERR_ESP_VENDOR_FAIL); + } + return BTM_ILLEGAL_VALUE; + } + } else if(*own_bda_type == BLE_ADDR_PUBLIC_ID || *own_bda_type == BLE_ADDR_RANDOM_ID) { + if((btm_cb.ble_ctr_cb.addr_mgnt_cb.exist_addr_bit & BTM_BLE_GAP_ADDR_BIT_RESOLVABLE) == BTM_BLE_GAP_ADDR_BIT_RESOLVABLE) { + *own_bda_type = BLE_ADDR_RANDOM; + btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type = BLE_ADDR_RANDOM; + 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 { + #if BLE_PRIVACY_SPT == TRUE + if(btm_cb.ble_ctr_cb.privacy_mode != BTM_PRIVACY_NONE) { + BTM_TRACE_ERROR ("Error state\n"); + if(cb) { + (* cb)(HCI_ERR_ESP_VENDOR_FAIL); + } + return BTM_ILLEGAL_VALUE; + } + #endif + if(*own_bda_type == BLE_ADDR_PUBLIC_ID) { + *own_bda_type = BLE_ADDR_PUBLIC; + btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type = BLE_ADDR_PUBLIC; + } else { //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) { + *own_bda_type = BLE_ADDR_RANDOM; + btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type = BLE_ADDR_RANDOM; + memcpy(btm_cb.ble_ctr_cb.addr_mgnt_cb.private_addr, btm_cb.ble_ctr_cb.addr_mgnt_cb.static_rand_addr, BD_ADDR_LEN); + btsnd_hcic_ble_set_random_addr(btm_cb.ble_ctr_cb.addr_mgnt_cb.static_rand_addr); + } else { + BTM_TRACE_ERROR ("No RPA and no random address yet, please set RPA or random address and try\n"); + if(cb) { + (* cb)(HCI_ERR_ESP_VENDOR_FAIL); + } + return BTM_ILLEGAL_VALUE; + } + } + } + } else { + btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type = BLE_ADDR_PUBLIC; + } + + return BTM_SUCCESS; +} + + /******************************************************************************* ** ** Function BTM_BleConfigLocalIcon @@ -1357,69 +1492,9 @@ tBTM_STATUS BTM_BleSetAdvParamsAll(UINT16 adv_int_min, UINT16 adv_int_max, UINT8 if (!controller_get_interface()->supports_ble()) { return BTM_ILLEGAL_VALUE; } - - if(own_bda_type == BLE_ADDR_RANDOM) { - if((btm_cb.ble_ctr_cb.addr_mgnt_cb.exist_addr_bit & BTM_BLE_GAP_ADDR_BIT_RANDOM) == BTM_BLE_GAP_ADDR_BIT_RANDOM) { - //close privacy - #if BLE_PRIVACY_SPT == TRUE - if (btm_cb.ble_ctr_cb.privacy_mode != BTM_PRIVACY_NONE) { - BTM_BleConfigPrivacy(FALSE, NULL); - } - #endif - btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type = BLE_ADDR_RANDOM; - memcpy(btm_cb.ble_ctr_cb.addr_mgnt_cb.private_addr, btm_cb.ble_ctr_cb.addr_mgnt_cb.static_rand_addr, BD_ADDR_LEN); - // set address to controller - btsnd_hcic_ble_set_random_addr(btm_cb.ble_ctr_cb.addr_mgnt_cb.static_rand_addr); - - } else if((btm_cb.ble_ctr_cb.addr_mgnt_cb.exist_addr_bit & BTM_BLE_GAP_ADDR_BIT_RESOLVABLE) == BTM_BLE_GAP_ADDR_BIT_RESOLVABLE) { - btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type = BLE_ADDR_RANDOM; - 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"); - if(adv_cb) { - (* adv_cb)(HCI_ERR_ESP_VENDOR_FAIL); - } - return BTM_ILLEGAL_VALUE; - } - } else if(own_bda_type == BLE_ADDR_PUBLIC_ID || own_bda_type == BLE_ADDR_RANDOM_ID) { - if((btm_cb.ble_ctr_cb.addr_mgnt_cb.exist_addr_bit & BTM_BLE_GAP_ADDR_BIT_RESOLVABLE) == BTM_BLE_GAP_ADDR_BIT_RESOLVABLE) { - own_bda_type = BLE_ADDR_RANDOM; - btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type = BLE_ADDR_RANDOM; - 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 { - #if BLE_PRIVACY_SPT == TRUE - if(btm_cb.ble_ctr_cb.privacy_mode != BTM_PRIVACY_NONE) { - BTM_TRACE_ERROR ("Error state\n"); - if(adv_cb) { - (* adv_cb)(HCI_ERR_ESP_VENDOR_FAIL); - } - return BTM_ILLEGAL_VALUE; - } - #endif - if(own_bda_type == BLE_ADDR_PUBLIC_ID) { - own_bda_type = BLE_ADDR_PUBLIC; - btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type = BLE_ADDR_PUBLIC; - } else { //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) { - own_bda_type = BLE_ADDR_RANDOM; - btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type = BLE_ADDR_RANDOM; - memcpy(btm_cb.ble_ctr_cb.addr_mgnt_cb.private_addr, btm_cb.ble_ctr_cb.addr_mgnt_cb.static_rand_addr, BD_ADDR_LEN); - btsnd_hcic_ble_set_random_addr(btm_cb.ble_ctr_cb.addr_mgnt_cb.static_rand_addr); - } else { - BTM_TRACE_ERROR ("No RPA and no random address yet, please set RPA or random address and try\n"); - if(adv_cb) { - (* adv_cb)(HCI_ERR_ESP_VENDOR_FAIL); - } - return BTM_ILLEGAL_VALUE; - } - } - } - } else { - btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type = BLE_ADDR_PUBLIC; + if (BTM_BleUpdateOwnType(&own_bda_type, adv_cb) != 0) { + return BTM_ILLEGAL_VALUE; } - if (!BTM_BLE_ISVALID_PARAM(adv_int_min, BTM_BLE_ADV_INT_MIN, BTM_BLE_ADV_INT_MAX) || !BTM_BLE_ISVALID_PARAM(adv_int_max, BTM_BLE_ADV_INT_MIN, BTM_BLE_ADV_INT_MAX)) { BTM_TRACE_ERROR ("adv_int_min or adv_int_max is invalid\n"); @@ -1591,61 +1666,9 @@ tBTM_STATUS BTM_BleSetScanFilterParams(tGATT_IF client_if, UINT32 scan_interval, if (!controller_get_interface()->supports_ble()) { return BTM_ILLEGAL_VALUE; } - - if(addr_type_own == BLE_ADDR_RANDOM) { - if((btm_cb.ble_ctr_cb.addr_mgnt_cb.exist_addr_bit & BTM_BLE_GAP_ADDR_BIT_RANDOM) == BTM_BLE_GAP_ADDR_BIT_RANDOM) { - //close privacy - #if BLE_PRIVACY_SPT == TRUE - if (btm_cb.ble_ctr_cb.privacy_mode != BTM_PRIVACY_NONE) { - BTM_BleConfigPrivacy(FALSE, NULL); - } - #endif - btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type = BLE_ADDR_RANDOM; - memcpy(btm_cb.ble_ctr_cb.addr_mgnt_cb.private_addr, btm_cb.ble_ctr_cb.addr_mgnt_cb.static_rand_addr, BD_ADDR_LEN); - // set address to controller - btsnd_hcic_ble_set_random_addr(btm_cb.ble_ctr_cb.addr_mgnt_cb.static_rand_addr); - - } else if((btm_cb.ble_ctr_cb.addr_mgnt_cb.exist_addr_bit & BTM_BLE_GAP_ADDR_BIT_RESOLVABLE) == BTM_BLE_GAP_ADDR_BIT_RESOLVABLE) { - btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type = BLE_ADDR_RANDOM; - 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"); - return BTM_ILLEGAL_VALUE; - } - } else if(addr_type_own == BLE_ADDR_PUBLIC_ID || addr_type_own == BLE_ADDR_RANDOM_ID) { - if((btm_cb.ble_ctr_cb.addr_mgnt_cb.exist_addr_bit & BTM_BLE_GAP_ADDR_BIT_RESOLVABLE) == BTM_BLE_GAP_ADDR_BIT_RESOLVABLE) { - addr_type_own = BLE_ADDR_RANDOM; - btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type = BLE_ADDR_RANDOM; - 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 { - #if BLE_PRIVACY_SPT == TRUE - if(btm_cb.ble_ctr_cb.privacy_mode != BTM_PRIVACY_NONE) { - BTM_TRACE_ERROR ("Error state\n"); - return BTM_ILLEGAL_VALUE; - } - #endif - if(addr_type_own == BLE_ADDR_PUBLIC_ID) { - addr_type_own = BLE_ADDR_PUBLIC; - btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type = BLE_ADDR_PUBLIC; - } else { - //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) { - addr_type_own = BLE_ADDR_RANDOM; - btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type = BLE_ADDR_RANDOM; - memcpy(btm_cb.ble_ctr_cb.addr_mgnt_cb.private_addr, btm_cb.ble_ctr_cb.addr_mgnt_cb.static_rand_addr, BD_ADDR_LEN); - btsnd_hcic_ble_set_random_addr(btm_cb.ble_ctr_cb.addr_mgnt_cb.static_rand_addr); - } else { - BTM_TRACE_ERROR ("No RPA and no random address yet, please set RPA or random address and try\n"); - return BTM_ILLEGAL_VALUE; - } - } - } - } else { - btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type = BLE_ADDR_PUBLIC; + if (BTM_BleUpdateOwnType(&addr_type_own, NULL) != 0) { + return BTM_ILLEGAL_VALUE; } - /* If not supporting extended scan support, use the older range for checking */ if (btm_cb.cmn_ble_vsc_cb.extended_scan_support == 0) { max_scan_interval = BTM_BLE_SCAN_INT_MAX; 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 14588433d2..47cd2adb69 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 @@ -700,8 +700,6 @@ typedef void (tBTM_BLE_PF_PARAM_CBACK) (tBTM_BLE_PF_ACTION action_type, #define MAX_BLE_ADV_INSTANCE 10 typedef struct { UINT8 inst_id; - BOOLEAN in_use; - UINT8 adv_evt; BOOLEAN configured; BOOLEAN legacy_pdu; @@ -714,7 +712,6 @@ typedef struct { typedef struct { tBTM_BLE_EXTENDED_INST inst[MAX_BLE_ADV_INSTANCE]; /* dynamic array to store adv instance */ UINT8 scan_duplicate; - tBTM_BLE_MULTI_ADV_OPQ op_q; } tBTM_BLE_EXTENDED_CB; #define BTM_BLE_GAP_SET_EXT_ADV_PROP_CONNECTABLE (1 << 0) diff --git a/components/bt/host/bluedroid/stack/l2cap/l2c_ble.c b/components/bt/host/bluedroid/stack/l2cap/l2c_ble.c index db7a051950..8f760deb17 100644 --- a/components/bt/host/bluedroid/stack/l2cap/l2c_ble.c +++ b/components/bt/host/bluedroid/stack/l2cap/l2c_ble.c @@ -802,7 +802,6 @@ BOOLEAN l2cble_init_direct_conn (tL2C_LCB *p_lcb) #if ( (defined BLE_PRIVACY_SPT) && (BLE_PRIVACY_SPT == TRUE)) own_addr_type = btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type; - #if (!CONTROLLER_RPA_LIST_ENABLE) if(dev_rec_exist) { // if the current address information is valid, get the real address information @@ -826,7 +825,7 @@ BOOLEAN l2cble_init_direct_conn (tL2C_LCB *p_lcb) #endif // (!CONTROLLER_RPA_LIST_ENABLE) -#if CONTROLLER_RPA_LIST_ENABLE +#if (CONTROLLER_RPA_LIST_ENABLE && CONFIG_BT_CTRL_ESP32) if (p_dev_rec->ble.in_controller_list & BTM_RESOLVING_LIST_BIT) { if (btm_cb.ble_ctr_cb.privacy_mode >= BTM_PRIVACY_1_2) { @@ -840,7 +839,6 @@ BOOLEAN l2cble_init_direct_conn (tL2C_LCB *p_lcb) } #endif // CONTROLLER_RPA_LIST_ENABLE - #endif // (defined BLE_PRIVACY_SPT) && (BLE_PRIVACY_SPT == TRUE) if (!btm_ble_topology_check(BTM_BLE_STATE_INIT)) { @@ -892,6 +890,21 @@ BOOLEAN l2cble_init_direct_conn (tL2C_LCB *p_lcb) } } else { #if (BLE_50_FEATURE_SUPPORT == TRUE) + + /* + * 0x00 Public Device Address + * 0x01 Random Device Address + * 0x02 Public Identity Address (corresponds to Resolved Private Address) + * 0x03 Random (static) Identity Address (corresponds to Resolved Private Address) + * 0xFF No address provided (anonymous advertisement) + */ + + if ((peer_addr_type & BLE_ADDR_RANDOM) == BLE_ADDR_RANDOM) { + peer_addr_type = BLE_ADDR_RANDOM; + } else { + peer_addr_type = BLE_ADDR_PUBLIC; + } + tHCI_CreatExtConn aux_conn = {0}; aux_conn.filter_policy = FALSE; aux_conn.own_addr_type = own_addr_type; From 5717f8f2fb4e7d48e1d07a56aa0f1f5d19b5e84f Mon Sep 17 00:00:00 2001 From: zwj Date: Mon, 12 Apr 2021 21:19:03 +0800 Subject: [PATCH 3/5] update con state when getting connection cancle complete --- .../bt/host/bluedroid/stack/btm/btm_ble.c | 24 +++++++++++++++++++ .../bluedroid/stack/btm/include/btm_ble_int.h | 1 + .../bt/host/bluedroid/stack/btu/btu_hcif.c | 3 +++ 3 files changed, 28 insertions(+) diff --git a/components/bt/host/bluedroid/stack/btm/btm_ble.c b/components/bt/host/bluedroid/stack/btm/btm_ble.c index 3205df4d60..54ffcaa111 100644 --- a/components/bt/host/bluedroid/stack/btm/btm_ble.c +++ b/components/bt/host/bluedroid/stack/btm/btm_ble.c @@ -2076,6 +2076,30 @@ void btm_ble_create_ll_conn_complete (UINT8 status) btm_ble_update_mode_operation(HCI_ROLE_UNKNOWN, NULL, status); } } + +/***************************************************************************** +** Function btm_ble_create_conn_cancel_complete +** +** Description LE connection cancel complete. +** +******************************************************************************/ +void btm_ble_create_conn_cancel_complete (UINT8 *p) +{ + UINT8 status; + + STREAM_TO_UINT8 (status, p); + + switch (status) { + case HCI_SUCCESS: + if (btm_ble_get_conn_st() == BLE_CONN_CANCEL) { + btm_ble_set_conn_st (BLE_CONN_IDLE); + } + break; + default: + break; + } +} + /***************************************************************************** ** Function btm_proc_smp_cback ** 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 e1af9a58df..017eb89223 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 @@ -413,6 +413,7 @@ tBTM_STATUS btm_ble_start_adv(void); tBTM_STATUS btm_ble_stop_adv(void); tBTM_STATUS btm_ble_start_scan(void); void btm_ble_create_ll_conn_complete (UINT8 status); +void btm_ble_create_conn_cancel_complete (UINT8 *p); /* LE security function from btm_sec.c */ #if SMP_INCLUDED == TRUE diff --git a/components/bt/host/bluedroid/stack/btu/btu_hcif.c b/components/bt/host/bluedroid/stack/btu/btu_hcif.c index 4212ba6cc9..82e786a164 100644 --- a/components/bt/host/bluedroid/stack/btu/btu_hcif.c +++ b/components/bt/host/bluedroid/stack/btu/btu_hcif.c @@ -1071,6 +1071,9 @@ static void btu_hcif_hdl_command_complete (UINT16 opcode, UINT8 *p, UINT16 evt_l case HCI_BLE_TEST_END: btm_ble_test_command_complete(p); break; + case HCI_BLE_CREATE_CONN_CANCEL: + btm_ble_create_conn_cancel_complete(p); + break; #if (defined BLE_PRIVACY_SPT && BLE_PRIVACY_SPT == TRUE) case HCI_BLE_ADD_DEV_RESOLVING_LIST: From 67b09d3db5c7160fe9ae21be59da0bc93fef2e63 Mon Sep 17 00:00:00 2001 From: zwj Date: Tue, 13 Apr 2021 16:53:13 +0800 Subject: [PATCH 4/5] add option to enable multi-connection --- components/bt/host/bluedroid/Kconfig.in | 7 +++++++ components/bt/host/bluedroid/bta/gatt/bta_gattc_act.c | 3 ++- .../bt/host/bluedroid/bta/gatt/bta_gattc_cache.c | 2 ++ .../common/include/common/bluedroid_user_config.h | 6 ++++++ .../host/bluedroid/common/include/common/bt_target.h | 10 ++++++++++ components/bt/host/bluedroid/stack/smp/smp_act.c | 4 ++++ components/bt/host/bluedroid/stack/smp/smp_utils.c | 2 ++ 7 files changed, 33 insertions(+), 1 deletion(-) diff --git a/components/bt/host/bluedroid/Kconfig.in b/components/bt/host/bluedroid/Kconfig.in index e5ba243bbe..d4a47c02b6 100644 --- a/components/bt/host/bluedroid/Kconfig.in +++ b/components/bt/host/bluedroid/Kconfig.in @@ -971,6 +971,13 @@ config BT_ACL_CONNECTIONS help Maximum BT/BLE connection count +config BT_MULTI_CONNECTION_ENBALE + bool "Enable BLE multi-conections" + depends on BT_BLUEDROID_ENABLED + default y + help + Enable this option if there are multiple connections + config BT_ALLOCATION_FROM_SPIRAM_FIRST bool "BT/BLE will first malloc the memory from the PSRAM" depends on BT_BLUEDROID_ENABLED diff --git a/components/bt/host/bluedroid/bta/gatt/bta_gattc_act.c b/components/bt/host/bluedroid/bta/gatt/bta_gattc_act.c index 87c1a9a0bd..9f5417bf76 100644 --- a/components/bt/host/bluedroid/bta/gatt/bta_gattc_act.c +++ b/components/bt/host/bluedroid/bta/gatt/bta_gattc_act.c @@ -973,10 +973,11 @@ void bta_gattc_start_discover(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data) p_clcb->p_srcb->srvc_hdl_chg = FALSE; p_clcb->p_srcb->update_count = 0; p_clcb->p_srcb->state = BTA_GATTC_SERV_DISC_ACT; - + #if (BT_MULTI_CONNECTION_ENBALE == FALSE) if (p_clcb->transport == BTA_TRANSPORT_LE) { L2CA_EnableUpdateBleConnParams(p_clcb->p_srcb->server_bda, FALSE); } + #endif /* set all srcb related clcb into discovery ST */ bta_gattc_set_discover_st(p_clcb->p_srcb); diff --git a/components/bt/host/bluedroid/bta/gatt/bta_gattc_cache.c b/components/bt/host/bluedroid/bta/gatt/bta_gattc_cache.c index 0781986de7..ba232cb1d1 100644 --- a/components/bt/host/bluedroid/bta/gatt/bta_gattc_cache.c +++ b/components/bt/host/bluedroid/bta/gatt/bta_gattc_cache.c @@ -616,9 +616,11 @@ static void bta_gattc_explore_srvc(UINT16 conn_id, tBTA_GATTC_SERV *p_srvc_cb) //server discover end, update connection parameters #if BLE_INCLUDED == TRUE + #if (BT_MULTI_CONNECTION_ENBALE == FALSE) if (p_clcb->transport == BTA_TRANSPORT_LE) { L2CA_EnableUpdateBleConnParams(p_clcb->p_srcb->server_bda, TRUE); } + #endif //discover service complete, trigger callback tBTA_GATTC cb_data; cb_data.dis_cmpl.status = p_clcb->status; 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 93804a08b9..4edca2bacb 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 @@ -210,6 +210,12 @@ #define UC_BT_ACL_CONNECTIONS 5 #endif +#ifdef CONFIG_BT_MULTI_CONNECTION_ENBALE +#define UC_BT_MULTI_CONNECTION_ENBALE CONFIG_BT_MULTI_CONNECTION_ENBALE +#else +#define UC_BT_MULTI_CONNECTION_ENBALE FALSE +#endif + //BT_BLE_ESTAB_LINK_CONN_TOUT #ifdef CONFIG_BT_BLE_ESTAB_LINK_CONN_TOUT #define UC_BT_BLE_ESTAB_LINK_CONN_TOUT CONFIG_BT_BLE_ESTAB_LINK_CONN_TOUT 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 c376462122..fa16eea4fa 100644 --- a/components/bt/host/bluedroid/common/include/common/bt_target.h +++ b/components/bt/host/bluedroid/common/include/common/bt_target.h @@ -249,6 +249,16 @@ #define GATT_MAX_PHY_CHANNEL UC_BT_ACL_CONNECTIONS #endif /* UC_BT_ACL_CONNECTIONS */ +#ifdef UC_BT_MULTI_CONNECTION_ENBALE +#define BT_MULTI_CONNECTION_ENBALE UC_BT_MULTI_CONNECTION_ENBALE +#endif + +#if(BT_MULTI_CONNECTION_ENBALE && (CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3)) +#define BLE_CE_LEN_MIN 5 +#else +#define BLE_CE_LEN_MIN 0 +#endif + #ifdef UC_BT_BLE_ESTAB_LINK_CONN_TOUT #define BLE_ESTABLISH_LINK_CONNECTION_TIMEOUT UC_BT_BLE_ESTAB_LINK_CONN_TOUT #endif diff --git a/components/bt/host/bluedroid/stack/smp/smp_act.c b/components/bt/host/bluedroid/stack/smp/smp_act.c index 0ff45ad4aa..63f02c1e59 100644 --- a/components/bt/host/bluedroid/stack/smp/smp_act.c +++ b/components/bt/host/bluedroid/stack/smp/smp_act.c @@ -1540,9 +1540,11 @@ void smp_idle_terminate(tSMP_CB *p_cb, tSMP_INT_DATA *p_data) *******************************************************************************/ void smp_fast_conn_param(tSMP_CB *p_cb, tSMP_INT_DATA *p_data) { +#if (BT_MULTI_CONNECTION_ENBALE == FALSE) if(p_cb->role == BTM_ROLE_MASTER) { L2CA_EnableUpdateBleConnParams(p_cb->pairing_bda, FALSE); } +#endif #if (SMP_SLAVE_CON_PARAMS_UPD_ENABLE == TRUE) else { tBTM_SEC_DEV_REC *p_rec = btm_find_dev (p_cb->pairing_bda); @@ -1554,7 +1556,9 @@ void smp_fast_conn_param(tSMP_CB *p_cb, tSMP_INT_DATA *p_data) some peripherals are not able to revert to fast connection parameters during the start of service discovery. Connection paramter updates get enabled again once service discovery completes. */ + #if (BT_MULTI_CONNECTION_ENBALE == FALSE) L2CA_EnableUpdateBleConnParams(p_cb->pairing_bda, FALSE); + #endif } #endif } diff --git a/components/bt/host/bluedroid/stack/smp/smp_utils.c b/components/bt/host/bluedroid/stack/smp/smp_utils.c index 24b70cad9a..a9b4bf1a12 100644 --- a/components/bt/host/bluedroid/stack/smp/smp_utils.c +++ b/components/bt/host/bluedroid/stack/smp/smp_utils.c @@ -1021,7 +1021,9 @@ void smp_proc_pairing_cmpl(tSMP_CB *p_cb) //clear flag p_rec->ble.skip_update_conn_param = false; } else { + #if (BT_MULTI_CONNECTION_ENBALE == FALSE) L2CA_EnableUpdateBleConnParams(p_cb->pairing_bda, TRUE); + #endif } } From 2a7df3bcd8bc85409cd39063bd14c47f12b54e35 Mon Sep 17 00:00:00 2001 From: zwj Date: Tue, 2 Mar 2021 14:29:48 +0800 Subject: [PATCH 5/5] optimize C3 multi-connection --- components/bt/controller/lib | 2 +- .../bt/host/bluedroid/stack/hcic/hciblecmds.c | 12 ++++++------ .../bluedroid/stack/l2cap/include/l2c_int.h | 1 + .../bt/host/bluedroid/stack/l2cap/l2c_ble.c | 18 +++++++++++------- components/bt/include/esp32c3/include/esp_bt.h | 5 ++++- 5 files changed, 23 insertions(+), 15 deletions(-) diff --git a/components/bt/controller/lib b/components/bt/controller/lib index 2b7816260e..bc5bc7bb52 160000 --- a/components/bt/controller/lib +++ b/components/bt/controller/lib @@ -1 +1 @@ -Subproject commit 2b7816260ecd12710b983b7c859d42fce4f7d979 +Subproject commit bc5bc7bb523663d8ac07d50ac04eabf62acd2b1b diff --git a/components/bt/host/bluedroid/stack/hcic/hciblecmds.c b/components/bt/host/bluedroid/stack/hcic/hciblecmds.c index ba025baeed..bda68015df 100644 --- a/components/bt/host/bluedroid/stack/hcic/hciblecmds.c +++ b/components/bt/host/bluedroid/stack/hcic/hciblecmds.c @@ -1568,8 +1568,8 @@ BOOLEAN btsnd_hcic_ble_create_ext_conn(tHCI_CreatExtConn *p_conn) UINT16_TO_STREAM(pp, params->conn_interval_max); UINT16_TO_STREAM(pp, params->conn_latency); UINT16_TO_STREAM(pp, params->sup_timeout); - UINT16_TO_STREAM(pp, params->min_ce_len); - UINT16_TO_STREAM(pp, params->max_ce_len); + UINT16_TO_STREAM(pp, BLE_CE_LEN_MIN); + UINT16_TO_STREAM(pp, BLE_CE_LEN_MIN); } if (p_conn->init_phy_mask & 0x02) { @@ -1580,8 +1580,8 @@ BOOLEAN btsnd_hcic_ble_create_ext_conn(tHCI_CreatExtConn *p_conn) UINT16_TO_STREAM(pp, params->conn_interval_max); UINT16_TO_STREAM(pp, params->conn_latency); UINT16_TO_STREAM(pp, params->sup_timeout); - UINT16_TO_STREAM(pp, params->min_ce_len); - UINT16_TO_STREAM(pp, params->max_ce_len); + UINT16_TO_STREAM(pp, BLE_CE_LEN_MIN); + UINT16_TO_STREAM(pp, BLE_CE_LEN_MIN); } if (p_conn->init_phy_mask & 0x04) { @@ -1592,8 +1592,8 @@ BOOLEAN btsnd_hcic_ble_create_ext_conn(tHCI_CreatExtConn *p_conn) UINT16_TO_STREAM(pp, params->conn_interval_max); UINT16_TO_STREAM(pp, params->conn_latency); UINT16_TO_STREAM(pp, params->sup_timeout); - UINT16_TO_STREAM(pp, params->min_ce_len); - UINT16_TO_STREAM(pp, params->max_ce_len); + UINT16_TO_STREAM(pp, BLE_CE_LEN_MIN); + UINT16_TO_STREAM(pp, BLE_CE_LEN_MIN); } btu_hcif_send_cmd(LOCAL_BR_EDR_CONTROLLER_ID, p); diff --git a/components/bt/host/bluedroid/stack/l2cap/include/l2c_int.h b/components/bt/host/bluedroid/stack/l2cap/include/l2c_int.h index 3c293b79d2..a504b3d34b 100644 --- a/components/bt/host/bluedroid/stack/l2cap/include/l2c_int.h +++ b/components/bt/host/bluedroid/stack/l2cap/include/l2c_int.h @@ -600,6 +600,7 @@ extern BOOLEAN l2cu_start_post_bond_timer (UINT16 handle); extern void l2cu_release_lcb (tL2C_LCB *p_lcb); extern tL2C_LCB *l2cu_find_lcb_by_bd_addr (BD_ADDR p_bd_addr, tBT_TRANSPORT transport); extern tL2C_LCB *l2cu_find_lcb_by_handle (UINT16 handle); +extern uint8_t l2cu_plcb_active_count(void); extern void l2cu_update_lcb_4_bonding (BD_ADDR p_bd_addr, BOOLEAN is_bonding); extern UINT8 l2cu_get_conn_role (tL2C_LCB *p_this_lcb); diff --git a/components/bt/host/bluedroid/stack/l2cap/l2c_ble.c b/components/bt/host/bluedroid/stack/l2cap/l2c_ble.c index 8f760deb17..b0dd90a99f 100644 --- a/components/bt/host/bluedroid/stack/l2cap/l2c_ble.c +++ b/components/bt/host/bluedroid/stack/l2cap/l2c_ble.c @@ -205,6 +205,10 @@ BOOLEAN L2CA_EnableUpdateBleConnParams (BD_ADDR rem_bda, BOOLEAN enable) if (p_lcb->current_used_conn_interval <= BTM_BLE_CONN_INT_MAX_DEF && (p_lcb->conn_update_mask & L2C_BLE_CONN_UPDATE_DISABLE) == 0){ return (FALSE); } + bool is_disable = (p_lcb->conn_update_mask & L2C_BLE_CONN_UPDATE_DISABLE); + if(l2cu_plcb_active_count() >1 && !(enable && is_disable)) { + return FALSE; + } if (enable) { p_lcb->conn_update_mask &= ~L2C_BLE_CONN_UPDATE_DISABLE; @@ -364,7 +368,7 @@ void l2cble_scanner_conn_comp (UINT16 handle, BD_ADDR bda, tBLE_ADDR_TYPE type, p_dev_rec->conn_params.max_conn_int, p_dev_rec->conn_params.slave_latency, p_dev_rec->conn_params.supervision_tout, - 0, 0); + BLE_CE_LEN_MIN, BLE_CE_LEN_MIN); } /* Tell BTM Acl management about the link */ @@ -525,7 +529,7 @@ static BOOLEAN l2cble_start_conn_update (tL2C_LCB *p_lcb) #endif ) { btsnd_hcic_ble_upd_ll_conn_params(p_lcb->handle, min_conn_int, max_conn_int, - slave_latency, supervision_tout, 0, 0); + slave_latency, supervision_tout, BLE_CE_LEN_MIN, BLE_CE_LEN_MIN); } else { l2cu_send_peer_ble_par_req (p_lcb, min_conn_int, max_conn_int, slave_latency, supervision_tout); } @@ -553,7 +557,7 @@ static BOOLEAN l2cble_start_conn_update (tL2C_LCB *p_lcb) #endif ) { btsnd_hcic_ble_upd_ll_conn_params(p_lcb->handle, p_lcb->waiting_update_conn_min_interval, - p_lcb->waiting_update_conn_max_interval, p_lcb->waiting_update_conn_latency, p_lcb->waiting_update_conn_timeout, 0, 0); + p_lcb->waiting_update_conn_max_interval, p_lcb->waiting_update_conn_latency, p_lcb->waiting_update_conn_timeout, BLE_CE_LEN_MIN, BLE_CE_LEN_MIN); } else { l2cu_send_peer_ble_par_req (p_lcb, p_lcb->waiting_update_conn_min_interval, p_lcb->waiting_update_conn_max_interval, p_lcb->waiting_update_conn_latency, p_lcb->waiting_update_conn_timeout); @@ -874,8 +878,8 @@ BOOLEAN l2cble_init_direct_conn (tL2C_LCB *p_lcb) p_dev_rec->conn_params.slave_latency : BTM_BLE_CONN_SLAVE_LATENCY_DEF), /* UINT16 conn_latency */ (UINT16) ((p_dev_rec->conn_params.supervision_tout != BTM_BLE_CONN_PARAM_UNDEF) ? p_dev_rec->conn_params.supervision_tout : BTM_BLE_CONN_TIMEOUT_DEF), /* conn_timeout */ - 0, /* UINT16 min_len */ - 0)) { /* UINT16 max_len */ + BLE_CE_LEN_MIN, /* UINT16 min_len */ + BLE_CE_LEN_MIN)) { /* UINT16 max_len */ l2cu_release_lcb (p_lcb); L2CAP_TRACE_ERROR("initate direct connection fail, no resources"); return (FALSE); @@ -1130,12 +1134,12 @@ void l2cble_process_rc_param_request_evt(UINT16 handle, UINT16 int_min, UINT16 i /* if update is enabled, always accept connection parameter update */ if ((p_lcb->conn_update_mask & L2C_BLE_CONN_UPDATE_DISABLE) == 0) { p_lcb->conn_update_mask |= L2C_BLE_UPDATE_PENDING; - btsnd_hcic_ble_rc_param_req_reply(handle, int_min, int_max, latency, timeout, 0, 0); + btsnd_hcic_ble_rc_param_req_reply(handle, int_min, int_max, latency, timeout, BLE_CE_LEN_MIN, BLE_CE_LEN_MIN); }else { /* always accept connection parameters request which is sent by itself */ if (int_max == BTM_BLE_CONN_INT_MIN) { p_lcb->conn_update_mask |= L2C_BLE_UPDATE_PENDING; - btsnd_hcic_ble_rc_param_req_reply(handle, int_min, int_max, latency, timeout, 0, 0); + btsnd_hcic_ble_rc_param_req_reply(handle, int_min, int_max, latency, timeout, BLE_CE_LEN_MIN, BLE_CE_LEN_MIN); }else { L2CAP_TRACE_EVENT ("L2CAP - LE - update currently disabled"); p_lcb->conn_update_mask |= L2C_BLE_NEW_CONN_PARAM; diff --git a/components/bt/include/esp32c3/include/esp_bt.h b/components/bt/include/esp32c3/include/esp_bt.h index 7a33d605bc..812efc83f2 100644 --- a/components/bt/include/esp32c3/include/esp_bt.h +++ b/components/bt/include/esp32c3/include/esp_bt.h @@ -26,7 +26,7 @@ extern "C" { #endif #define ESP_BT_CTRL_CONFIG_MAGIC_VAL 0x5A5AA5A5 -#define ESP_BT_CTRL_CONFIG_VERSION 0x02103080 +#define ESP_BT_CTRL_CONFIG_VERSION 0x02103310 #define ESP_BT_HCI_TL_MAGIC_VALUE 0xfadebead #define ESP_BT_HCI_TL_VERSION 0x00010000 @@ -96,6 +96,7 @@ enum { #ifdef CONFIG_BT_ENABLED #define BT_CTRL_BLE_MAX_ACT_LIMIT 10 //Maximum BLE activity limitation +#define SLAVE_CE_LEN_MIN_DEFAULT 5 #ifdef CONFIG_BT_CTRL_SCAN_DUPL_TYPE #define SCAN_DUPLICATE_TYPE_VALUE CONFIG_BT_CTRL_SCAN_DUPL_TYPE @@ -161,6 +162,7 @@ enum { .mesh_adv_size = MESH_DUPLICATE_SCAN_CACHE_SIZE, \ .coex_phy_coded_tx_rx_time_limit = CONFIG_BT_CTRL_COEX_PHY_CODED_TX_RX_TLIM_EFF, \ .hw_target_code = BLE_HW_TARGET_CODE_ESP32C3_CHIP_ECO0, \ + .slave_ce_len_min = SLAVE_CE_LEN_MIN_DEFAULT, \ }; #else @@ -225,6 +227,7 @@ typedef struct { uint16_t mesh_adv_size; /*!< Mesh adv size for scan duplicate */ uint8_t coex_phy_coded_tx_rx_time_limit; /*!< limit on max tx/rx time in case of connection using CODED-PHY with Wi-Fi coexistence */ uint32_t hw_target_code; /*!< hardware target */ + uint8_t slave_ce_len_min; } esp_bt_controller_config_t; /**