Skip to content

Commit

Permalink
Merge branch 'bugfix/fix_ble_creat_conn_fail_enh_v5.4' into 'release/…
Browse files Browse the repository at this point in the history
…v5.4'

fix(ble/bluedroid): Fixed BLE create connection fail because of invalid own address type (v5.4)

See merge request espressif/esp-idf!35019
  • Loading branch information
Jiang Jiang Jian committed Nov 29, 2024
2 parents bbdcabe + 02cc012 commit 3f63fd9
Show file tree
Hide file tree
Showing 34 changed files with 598 additions and 189 deletions.
7 changes: 6 additions & 1 deletion components/bt/controller/esp32/bt.c
Original file line number Diff line number Diff line change
Expand Up @@ -1931,7 +1931,7 @@ esp_err_t esp_bredr_sco_datapath_set(esp_sco_data_path_t data_path)
return ESP_OK;
}

esp_err_t esp_ble_scan_dupilcate_list_flush(void)
esp_err_t esp_ble_scan_duplicate_list_flush(void)
{
if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_ENABLED) {
return ESP_ERR_INVALID_STATE;
Expand All @@ -1940,6 +1940,11 @@ esp_err_t esp_ble_scan_dupilcate_list_flush(void)
return ESP_OK;
}

esp_err_t esp_ble_scan_dupilcate_list_flush(void)
{
return esp_ble_scan_duplicate_list_flush();
}

/**
* This function re-write controller's function,
* As coredump can not show parameters in function which is in a .a file.
Expand Down
6 changes: 0 additions & 6 deletions components/bt/controller/esp32c3/bt.c
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,6 @@ extern int API_vhci_host_register_callback(const vhci_host_callback_t *callback)
extern int ble_txpwr_set(int power_type, uint16_t handle, int power_level);
extern int ble_txpwr_get(int power_type, uint16_t handle);

extern uint16_t l2c_ble_link_get_tx_buf_num(void);
extern void coex_pti_v2(void);

extern bool btdm_deep_sleep_mem_init(void);
Expand Down Expand Up @@ -1877,11 +1876,6 @@ int IRAM_ATTR esp_bt_h4tl_eif_io_event_notify(int event)
return btdm_hci_tl_io_event_post(event);
}

uint16_t esp_bt_get_tx_buf_num(void)
{
return l2c_ble_link_get_tx_buf_num();
}

static void coex_wifi_sleep_set_hook(bool sleep)
{

Expand Down
13 changes: 9 additions & 4 deletions components/bt/esp_ble_mesh/core/bluedroid_host/adapter.c
Original file line number Diff line number Diff line change
Expand Up @@ -1198,6 +1198,7 @@ uint16_t bt_mesh_gattc_get_service_uuid(struct bt_mesh_conn *conn)

int bt_mesh_gattc_conn_create(const bt_mesh_addr_t *addr, uint16_t service_uuid)
{
tBTA_BLE_CONN_PARAMS conn_1m_param = {0};
uint8_t zero[6] = {0};
int i;

Expand Down Expand Up @@ -1251,10 +1252,14 @@ int bt_mesh_gattc_conn_create(const bt_mesh_addr_t *addr, uint16_t service_uuid)
* Slave_latency: 0x0
* Supervision_timeout: 1s
*/
BTA_DmSetBlePrefConnParams(bt_mesh_gattc_info[i].addr.val, 0x18, 0x18, 0x00, 0x64);

BTA_GATTC_Open(bt_mesh_gattc_if, bt_mesh_gattc_info[i].addr.val,
bt_mesh_gattc_info[i].addr.type, true, BTA_GATT_TRANSPORT_LE, FALSE);
conn_1m_param.interval_min = 0x18;
conn_1m_param.interval_max = 0x18;
conn_1m_param.latency = 0;
conn_1m_param.supervision_timeout = 0x64;

BTA_GATTC_Enh_Open(bt_mesh_gattc_if, bt_mesh_gattc_info[i].addr.val,
bt_mesh_gattc_info[i].addr.type, true, BTA_GATT_TRANSPORT_LE, FALSE, BLE_ADDR_UNKNOWN_TYPE,
BTA_BLE_PHY_1M_MASK, &conn_1m_param, NULL, NULL);

return 0;
}
Expand Down
8 changes: 6 additions & 2 deletions components/bt/host/bluedroid/Kconfig.in
Original file line number Diff line number Diff line change
Expand Up @@ -1217,19 +1217,23 @@ config BT_BLE_RPA_TIMEOUT
Default is 900 s (15 minutes). Range is 1 s to 1 hour (3600 s).

config BT_BLE_50_FEATURES_SUPPORTED
bool "Enable BLE 5.0 features"
bool "Enable BLE 5.0 features(please disable BLE 4.2 if enable BLE 5.0)"
depends on (BT_BLE_ENABLED && ((BT_CONTROLLER_ENABLED && SOC_BLE_50_SUPPORTED) || BT_CONTROLLER_DISABLED))
default y
help
Enabling this option activates BLE 5.0 features.
This option is universally supported in chips that support BLE, except for ESP32.
BLE 4.2 and BLE 5.0 cannot be used simultaneously.


config BT_BLE_42_FEATURES_SUPPORTED
bool "Enable BLE 4.2 features"
bool "Enable BLE 4.2 features(please disable BLE 5.0 if enable BLE 4.2)"
depends on (BT_BLE_ENABLED && ((BT_CONTROLLER_ENABLED && SOC_BLE_SUPPORTED) || BT_CONTROLLER_DISABLED))
default n
help
This enables BLE 4.2 features.
This option is universally supported by all ESP chips with BLE capabilities.
BLE 4.2 and BLE 5.0 cannot be used simultaneously.

config BT_BLE_FEAT_PERIODIC_ADV_SYNC_TRANSFER
bool "Enable BLE periodic advertising sync transfer feature"
Expand Down
132 changes: 110 additions & 22 deletions components/bt/host/bluedroid/api/esp_gattc_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,46 +67,134 @@ esp_err_t esp_ble_gattc_app_unregister(esp_gatt_if_t gattc_if)

return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
}
#if (BLE_42_FEATURE_SUPPORT == TRUE)
esp_err_t esp_ble_gattc_open(esp_gatt_if_t gattc_if, esp_bd_addr_t remote_bda, esp_ble_addr_type_t remote_addr_type, bool is_direct)

esp_err_t esp_ble_gattc_enh_open(esp_gatt_if_t gattc_if, esp_ble_gatt_creat_conn_params_t *creat_conn_params)
{
btc_msg_t msg = {0};
btc_ble_gattc_args_t arg;
const esp_ble_conn_params_t *conn_params;

ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);

if (!creat_conn_params) {
return ESP_ERR_INVALID_ARG;
}

msg.sig = BTC_SIG_API_CALL;
msg.pid = BTC_PID_GATTC;
msg.act = BTC_GATTC_ACT_OPEN;
arg.open.gattc_if = gattc_if;
memcpy(arg.open.remote_bda, remote_bda, ESP_BD_ADDR_LEN);
arg.open.remote_addr_type = remote_addr_type;
arg.open.is_direct = is_direct;
arg.open.is_aux = false;
memcpy(arg.open.remote_bda, creat_conn_params->remote_bda, ESP_BD_ADDR_LEN);
arg.open.remote_addr_type = creat_conn_params->remote_addr_type;
arg.open.is_direct = creat_conn_params->is_direct;
arg.open.is_aux= creat_conn_params->is_aux;
arg.open.own_addr_type = creat_conn_params->own_addr_type;
arg.open.phy_mask = creat_conn_params->phy_mask;

// If not aux open, shouldn't set 2M and coded PHY connection params
if (!creat_conn_params->is_aux &&
((creat_conn_params->phy_mask & ESP_BLE_PHY_2M_PREF_MASK) ||
(creat_conn_params->phy_mask & ESP_BLE_PHY_CODED_PREF_MASK))) {
return ESP_ERR_INVALID_ARG;
}

if (creat_conn_params->phy_mask & ESP_BLE_PHY_1M_PREF_MASK) {
if (!creat_conn_params->phy_1m_conn_params) {
return ESP_ERR_INVALID_ARG;
}

conn_params = creat_conn_params->phy_1m_conn_params;
if (ESP_BLE_IS_VALID_PARAM(conn_params->interval_min, ESP_BLE_CONN_INT_MIN, ESP_BLE_CONN_INT_MAX) &&
ESP_BLE_IS_VALID_PARAM(conn_params->interval_max, ESP_BLE_CONN_INT_MIN, ESP_BLE_CONN_INT_MAX) &&
ESP_BLE_IS_VALID_PARAM(conn_params->supervision_timeout, ESP_BLE_CONN_SUP_TOUT_MIN, ESP_BLE_CONN_SUP_TOUT_MAX) &&
(conn_params->latency <= ESP_BLE_CONN_LATENCY_MAX) &&
((conn_params->supervision_timeout * 10) >= ((1 + conn_params->latency) * ((conn_params->interval_max * 5) >> 1))) &&
(conn_params->interval_min <= conn_params->interval_max)) {
memcpy(&arg.open.phy_1m_conn_params, conn_params, sizeof(esp_ble_conn_params_t));
} else {
LOG_ERROR("%s, invalid 1M PHY connection params: min_int = %d, max_int = %d, latency = %d, timeout = %d", __func__,
conn_params->interval_min,
conn_params->interval_max,
conn_params->latency,
conn_params->supervision_timeout);
return ESP_ERR_INVALID_ARG;
}
}

if (creat_conn_params->phy_mask & ESP_BLE_PHY_2M_PREF_MASK) {
if (!creat_conn_params->phy_2m_conn_params) {
return ESP_ERR_INVALID_ARG;
}

conn_params = creat_conn_params->phy_2m_conn_params;
if (ESP_BLE_IS_VALID_PARAM(conn_params->interval_min, ESP_BLE_CONN_INT_MIN, ESP_BLE_CONN_INT_MAX) &&
ESP_BLE_IS_VALID_PARAM(conn_params->interval_max, ESP_BLE_CONN_INT_MIN, ESP_BLE_CONN_INT_MAX) &&
ESP_BLE_IS_VALID_PARAM(conn_params->supervision_timeout, ESP_BLE_CONN_SUP_TOUT_MIN, ESP_BLE_CONN_SUP_TOUT_MAX) &&
(conn_params->latency <= ESP_BLE_CONN_LATENCY_MAX) &&
((conn_params->supervision_timeout * 10) >= ((1 + conn_params->latency) * ((conn_params->interval_max * 5) >> 1))) &&
(conn_params->interval_min <= conn_params->interval_max)) {
memcpy(&arg.open.phy_2m_conn_params, conn_params, sizeof(esp_ble_conn_params_t));
} else {
LOG_ERROR("%s, invalid 2M PHY connection params: min_int = %d, max_int = %d, latency = %d, timeout = %d", __func__,
conn_params->interval_min,
conn_params->interval_max,
conn_params->latency,
conn_params->supervision_timeout);
return ESP_ERR_INVALID_ARG;
}
}

if (creat_conn_params->phy_mask & ESP_BLE_PHY_CODED_PREF_MASK) {
if (!creat_conn_params->phy_coded_conn_params) {
return ESP_ERR_INVALID_ARG;
}

conn_params = creat_conn_params->phy_coded_conn_params;
if (ESP_BLE_IS_VALID_PARAM(conn_params->interval_min, ESP_BLE_CONN_INT_MIN, ESP_BLE_CONN_INT_MAX) &&
ESP_BLE_IS_VALID_PARAM(conn_params->interval_max, ESP_BLE_CONN_INT_MIN, ESP_BLE_CONN_INT_MAX) &&
ESP_BLE_IS_VALID_PARAM(conn_params->supervision_timeout, ESP_BLE_CONN_SUP_TOUT_MIN, ESP_BLE_CONN_SUP_TOUT_MAX) &&
(conn_params->latency <= ESP_BLE_CONN_LATENCY_MAX) &&
((conn_params->supervision_timeout * 10) >= ((1 + conn_params->latency) * ((conn_params->interval_max * 5) >> 1))) &&
(conn_params->interval_min <= conn_params->interval_max)) {
memcpy(&arg.open.phy_coded_conn_params, conn_params, sizeof(esp_ble_conn_params_t));
} else {
LOG_ERROR("%s, invalid Coded PHY connection params: min_int = %d, max_int = %d, latency = %d, timeout = %d", __func__,
conn_params->interval_min,
conn_params->interval_max,
conn_params->latency,
conn_params->supervision_timeout);
return ESP_ERR_INVALID_ARG;
}
}

return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
}

#if (BLE_42_FEATURE_SUPPORT == TRUE)
esp_err_t esp_ble_gattc_open(esp_gatt_if_t gattc_if, esp_bd_addr_t remote_bda, esp_ble_addr_type_t remote_addr_type, bool is_direct)
{
esp_ble_gatt_creat_conn_params_t creat_conn_params = {0};
memcpy(creat_conn_params.remote_bda, remote_bda, ESP_BD_ADDR_LEN);
creat_conn_params.remote_addr_type = remote_addr_type;
creat_conn_params.is_direct = is_direct;
creat_conn_params.is_aux = false;
creat_conn_params.own_addr_type = 0xff; //undefined, will use local value
creat_conn_params.phy_mask = 0x0;
return esp_ble_gattc_enh_open(gattc_if, &creat_conn_params);
}
#endif // #if (BLE_42_FEATURE_SUPPORT == TRUE)

#if (BLE_50_FEATURE_SUPPORT == TRUE)
esp_err_t esp_ble_gattc_aux_open(esp_gatt_if_t gattc_if, esp_bd_addr_t remote_bda, esp_ble_addr_type_t remote_addr_type, bool is_direct)
{
btc_msg_t msg;
btc_ble_gattc_args_t arg;

ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);

msg.sig = BTC_SIG_API_CALL;
msg.pid = BTC_PID_GATTC;
msg.act = BTC_GATTC_ACT_AUX_OPEN;
arg.open.gattc_if = gattc_if;
memcpy(arg.open.remote_bda, remote_bda, ESP_BD_ADDR_LEN);
arg.open.remote_addr_type = remote_addr_type;
arg.open.is_direct = is_direct;
arg.open.is_aux = true;

return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);

esp_ble_gatt_creat_conn_params_t creat_conn_params = {0};
memcpy(creat_conn_params.remote_bda, remote_bda, ESP_BD_ADDR_LEN);
creat_conn_params.remote_addr_type = remote_addr_type;
creat_conn_params.is_direct = is_direct;
creat_conn_params.is_aux = true;
creat_conn_params.own_addr_type = 0xff; //undefined, will use local value
creat_conn_params.phy_mask = 0x0;
return esp_ble_gattc_enh_open(gattc_if, &creat_conn_params);
}
#endif // #if (BLE_50_FEATURE_SUPPORT == TRUE)

Expand Down
19 changes: 19 additions & 0 deletions components/bt/host/bluedroid/api/include/api/esp_bt_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,25 @@ typedef uint8_t esp_link_key[ESP_BT_OCTET16_LEN]; /* Link Key */
#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_PHY_1M_PREF_MASK (1 << 0) /*!< The Host prefers use the LE1M transmitter or receiver PHY */
#define ESP_BLE_PHY_2M_PREF_MASK (1 << 1) /*!< The Host prefers use the LE2M transmitter or receiver PHY */
#define ESP_BLE_PHY_CODED_PREF_MASK (1 << 2) /*!< The Host prefers use the LE CODED transmitter or receiver PHY */
typedef uint8_t esp_ble_phy_mask_t;

/**
* @brief create connection parameters
*/
typedef struct {
uint16_t scan_interval; /*!< Initial scan interval, in units of 0.625ms, the range is 0x0004(2.5ms) to 0xFFFF(10.24s). */
uint16_t scan_window; /*!< Initial scan window, in units of 0.625ms, the range is 0x0004(2.5ms) to 0xFFFF(10.24s). */
uint16_t interval_min; /*!< Minimum connection interval, in units of 1.25ms, the range is 0x0006(7.5ms) to 0x0C80(4s). */
uint16_t interval_max; /*!< Maximum connection interval, in units of 1.25ms, the range is 0x0006(7.5ms) to 0x0C80(4s). */
uint16_t latency; /*!< Connection latency, the range is 0x0000(0) to 0x01F3(499). */
uint16_t supervision_timeout; /*!< Connection supervision timeout, in units of 10ms, the range is from 0x000A(100ms) to 0x0C80(32s). */
uint16_t min_ce_len; /*!< Minimum connection event length, in units of 0.625ms, setting to 0 for no preferred parameters. */
uint16_t max_ce_len; /*!< Maximum connection event length, in units of 0.625ms, setting to 0 for no preferred parameters. */
} esp_ble_conn_params_t;

/// Check the param is valid or not
#define ESP_BLE_IS_VALID_PARAM(x, min, max) (((x) >= (min) && (x) <= (max)) )

Expand Down
13 changes: 13 additions & 0 deletions components/bt/host/bluedroid/api/include/api/esp_gatt_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,19 @@ typedef struct {
esp_bt_uuid_t uuid; /*!< Included service UUID. */
} esp_gattc_incl_svc_elem_t;

/** @brief Represents a creat connection element. */
typedef struct {
esp_bd_addr_t remote_bda; /*!< The Bluetooth address of the remote device */
esp_ble_addr_type_t remote_addr_type; /*!< Address type of the remote device */
bool is_direct; /*!< Direct connection or background auto connection(by now, background auto connection is not supported */
bool is_aux; /*!< Set to true for BLE 5.0 or higher to enable auxiliary connections; set to false for BLE 4.2 or lower. */
esp_ble_addr_type_t own_addr_type; /*!< Specifies the address type used in the connection request. Set to 0xFF if the address type is unknown. */
esp_ble_phy_mask_t phy_mask; /*!< Indicates which PHY connection parameters will be used. When is_aux is false, only the connection params for 1M PHY can be specified */
const esp_ble_conn_params_t *phy_1m_conn_params; /*!< Connection parameters for the LE 1M PHY */
const esp_ble_conn_params_t *phy_2m_conn_params; /*!< Connection parameters for the LE 2M PHY */
const esp_ble_conn_params_t *phy_coded_conn_params; /*!< Connection parameters for the LE Coded PHY */
} esp_ble_gatt_creat_conn_params_t;

#ifdef __cplusplus
}
#endif
17 changes: 16 additions & 1 deletion components/bt/host/bluedroid/api/include/api/esp_gattc_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,20 @@ esp_err_t esp_ble_gattc_app_register(uint16_t app_id);
*/
esp_err_t esp_ble_gattc_app_unregister(esp_gatt_if_t gattc_if);

#if (BLE_42_FEATURE_SUPPORT == TRUE)
/**
* @brief Open a direct connection or add a background auto connection.
* Note: Do not enable both BLE_42_FEATURE_SUPPORT and BLE_50_FEATURE_SUPPORT configuration options simultaneously.
*
* @param[in] gattc_if: GATT client access interface.
* @param[in] esp_gatt_create_conn: Pointer to the structure containing connection parameters.
*
* @return
* - ESP_OK: Operation successful
* - others: Operation failed
*
*/
esp_err_t esp_ble_gattc_enh_open(esp_gatt_if_t gattc_if, esp_ble_gatt_creat_conn_params_t *esp_gatt_create_conn);

/**
* @brief Open a direct connection or add a background auto connection
*
Expand All @@ -328,12 +341,14 @@ esp_err_t esp_ble_gattc_app_unregister(esp_gatt_if_t gattc_if);
* - other: failed
*
*/
#if (BLE_42_FEATURE_SUPPORT == TRUE)
esp_err_t esp_ble_gattc_open(esp_gatt_if_t gattc_if, esp_bd_addr_t remote_bda, esp_ble_addr_type_t remote_addr_type, bool is_direct);
#endif // #if (BLE_42_FEATURE_SUPPORT == TRUE)

#if (BLE_50_FEATURE_SUPPORT == TRUE)
esp_err_t esp_ble_gattc_aux_open(esp_gatt_if_t gattc_if, esp_bd_addr_t remote_bda, esp_ble_addr_type_t remote_addr_type, bool is_direct);
#endif // #if (BLE_50_FEATURE_SUPPORT == TRUE)

/**
* @brief Close the virtual connection to the GATT server. gattc may have multiple virtual GATT server connections when multiple app_id registered,
* this API only close one virtual GATT server connection. if there exist other virtual GATT server connections,
Expand Down
6 changes: 4 additions & 2 deletions components/bt/host/bluedroid/bta/dm/bta_dm_act.c
Original file line number Diff line number Diff line change
Expand Up @@ -6049,7 +6049,7 @@ void bta_dm_ble_gap_set_prefer_ext_conn_params(tBTA_DM_MSG *p_data)
sizeof(tBTA_DM_BLE_CONN_PARAMS));
}

if (conn_params.phy_mask & BTAS_PHY_CODED_MASK) {
if (conn_params.phy_mask & BTA_PHY_CODED_MASK) {
memcpy(&conn_params.phy_coded_conn_params, &p_data->ble_set_per_ext_conn_params.phy_coded_conn_params,
sizeof(tBTA_DM_BLE_CONN_PARAMS));
}
Expand Down Expand Up @@ -6673,7 +6673,9 @@ void btm_dm_start_gatt_discovery (BD_ADDR bd_addr)
btm_dm_start_disc_gatt_services(bta_dm_search_cb.conn_id);
} else {
//TODO need to add addr_type in future
BTA_GATTC_Open(bta_dm_search_cb.client_if, bd_addr, BLE_ADDR_UNKNOWN_TYPE, TRUE, BTA_GATT_TRANSPORT_LE, FALSE);
BTA_GATTC_Enh_Open(bta_dm_search_cb.client_if, bd_addr, BLE_ADDR_UNKNOWN_TYPE, TRUE,
BTA_GATT_TRANSPORT_LE, FALSE, BLE_ADDR_UNKNOWN_TYPE, 0, NULL, NULL, NULL);

}
}
#endif /* #if (GATTC_INCLUDED == TRUE) */
Expand Down
2 changes: 1 addition & 1 deletion components/bt/host/bluedroid/bta/dm/include/bta_dm_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -1040,7 +1040,7 @@ typedef struct {

#define BTA_PHY_1M_MASK (1 << 0)
#define BTA_PHY_2M_MASK (1 << 1)
#define BTAS_PHY_CODED_MASK (1 << 2)
#define BTA_PHY_CODED_MASK (1 << 2)
typedef struct {
BT_HDR hdr;
BD_ADDR bd_addr;
Expand Down
Loading

0 comments on commit 3f63fd9

Please sign in to comment.