Skip to content

Commit

Permalink
Merge branch 'bugfix/fix_ble_creat_conn_fail_enh' into 'master'
Browse files Browse the repository at this point in the history
fix(ble/bluedroid): Fixed BLE create connection fail because of invalid own address type

Closes BLERP-639

See merge request espressif/esp-idf!34895
  • Loading branch information
Isl2017 committed Nov 19, 2024
2 parents 5910fc8 + b65f8a9 commit f420609
Show file tree
Hide file tree
Showing 24 changed files with 312 additions and 122 deletions.
8 changes: 4 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 @@ -1790,8 +1790,8 @@ int bt_mesh_gattc_conn_create(const bt_mesh_addr_t *addr, uint16_t service_uuid)

BTA_DmBleGapPreferExtConnectParamsSet(bt_mesh_gattc_info[i].addr.val, 0x01, &conn_1m_param ,NULL, NULL);

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, TRUE);
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, TRUE, BLE_ADDR_UNKNOWN_TYPE);
#else
/* Min_interval: 15ms
* Max_interval: 15ms
Expand All @@ -1801,8 +1801,8 @@ int bt_mesh_gattc_conn_create(const bt_mesh_addr_t *addr, uint16_t service_uuid)

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);
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);
#endif

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
48 changes: 26 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,8 +67,8 @@ 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 esp_gatt_create_conn)
{
btc_msg_t msg = {0};
btc_ble_gattc_args_t arg;
Expand All @@ -79,34 +79,38 @@ esp_err_t esp_ble_gattc_open(esp_gatt_if_t gattc_if, esp_bd_addr_t remote_bda, e
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, esp_gatt_create_conn.remote_bda, ESP_BD_ADDR_LEN);
arg.open.remote_addr_type = esp_gatt_create_conn.remote_addr_type;
arg.open.is_direct = esp_gatt_create_conn.is_direct;
arg.open.is_aux= esp_gatt_create_conn.is_aux;
arg.open.own_addr_type = esp_gatt_create_conn.own_addr_type;

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 esp_gatt_create_conn;
memcpy(esp_gatt_create_conn.remote_bda, remote_bda, ESP_BD_ADDR_LEN);
esp_gatt_create_conn.remote_addr_type = remote_addr_type;
esp_gatt_create_conn.is_direct = is_direct;
esp_gatt_create_conn.is_aux = false;
esp_gatt_create_conn.own_addr_type = 0xff; //undefined, will use local value
return esp_ble_gattc_enh_open(gattc_if, esp_gatt_create_conn);
}
#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 esp_gatt_create_conn;
memcpy(esp_gatt_create_conn.remote_bda, remote_bda, ESP_BD_ADDR_LEN);
esp_gatt_create_conn.remote_addr_type = remote_addr_type;
esp_gatt_create_conn.is_direct = is_direct;
esp_gatt_create_conn.is_aux = true;
esp_gatt_create_conn.own_addr_type = 0xff; //undefined, will use local value
return esp_ble_gattc_enh_open(gattc_if, esp_gatt_create_conn);
}
#endif // #if (BLE_50_FEATURE_SUPPORT == TRUE)

Expand Down
9 changes: 9 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,15 @@ 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_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: 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
3 changes: 2 additions & 1 deletion components/bt/host/bluedroid/bta/dm/bta_dm_act.c
Original file line number Diff line number Diff line change
Expand Up @@ -6673,7 +6673,8 @@ 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);

}
}
#endif /* #if (GATTC_INCLUDED == TRUE) */
Expand Down
5 changes: 5 additions & 0 deletions components/bt/host/bluedroid/bta/gatt/bta_gattc_act.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ static void bta_gattc_req_cback (UINT16 conn_id, UINT32 trans_id, tGATTS_REQ_TYP
static tBTA_GATTC_FIND_SERVICE_CB bta_gattc_register_service_change_notify(UINT16 conn_id, BD_ADDR remote_bda);

extern void btc_gattc_congest_callback(tBTA_GATTC *param);
extern uint32_t BTM_BleUpdateOwnType(uint8_t *own_bda_type, tBTM_START_ADV_CMPL_CBACK *cb);

static const tGATT_CBACK bta_gattc_cl_cback = {
bta_gattc_conn_cback,
Expand Down Expand Up @@ -336,6 +337,10 @@ void bta_gattc_process_api_open (tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_msg)
UNUSED(p_cb);

if (p_clreg != NULL) {
if (p_msg->api_conn.own_addr_type <= BLE_ADDR_TYPE_MAX) {
// update own address type for creating connection
BTM_BleUpdateOwnType(&p_msg->api_conn.own_addr_type, NULL);
}
if (p_msg->api_conn.is_direct) {
if ((p_clcb = bta_gattc_find_alloc_clcb(p_msg->api_conn.client_if,
p_msg->api_conn.remote_bda,
Expand Down
17 changes: 9 additions & 8 deletions components/bt/host/bluedroid/bta/gatt/bta_gattc_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ void BTA_GATTC_AppDeregister(tBTA_GATTC_IF client_if)

/*******************************************************************************
**
** Function BTA_GATTC_Open
** Function BTA_GATTC_Enh_Open
**
** Description Open a direct connection or add a background auto connection
** bd address
Expand All @@ -142,8 +142,8 @@ void BTA_GATTC_AppDeregister(tBTA_GATTC_IF client_if)
** Returns void
**
*******************************************************************************/
void BTA_GATTC_Open(tBTA_GATTC_IF client_if, BD_ADDR remote_bda, tBTA_ADDR_TYPE remote_addr_type,
BOOLEAN is_direct, tBTA_GATT_TRANSPORT transport, BOOLEAN is_aux)
void BTA_GATTC_Enh_Open(tBTA_GATTC_IF client_if, BD_ADDR remote_bda, tBTA_ADDR_TYPE remote_addr_type,
BOOLEAN is_direct, tBTA_GATT_TRANSPORT transport, BOOLEAN is_aux, tBTA_ADDR_TYPE own_addr_type)
{
tBTA_GATTC_API_OPEN *p_buf;

Expand All @@ -155,6 +155,7 @@ void BTA_GATTC_Open(tBTA_GATTC_IF client_if, BD_ADDR remote_bda, tBTA_ADDR_TYPE
p_buf->transport = transport;
p_buf->is_aux = is_aux;
p_buf->remote_addr_type = remote_addr_type;
p_buf->own_addr_type = own_addr_type;
memcpy(p_buf->remote_bda, remote_bda, BD_ADDR_LEN);


Expand Down Expand Up @@ -472,7 +473,7 @@ void BTA_GATTC_GetGattDb(UINT16 conn_id, UINT16 start_handle, UINT16 end_handle
** Description This function is called to read a characteristics value
**
** Parameters conn_id - connection ID.
** handle - characteritic handle to read.
** handle - characteristic handle to read.
**
** Returns None
**
Expand Down Expand Up @@ -607,7 +608,7 @@ void BTA_GATTC_ReadMultipleVariable(UINT16 conn_id, tBTA_GATTC_MULTI *p_read_mul
**
** Parameters conn_id - connection ID.
** s_handle - start handle.
** e_handle - end hanle
** e_handle - end handle
** uuid - The attribute UUID.
**
** Returns None
Expand Down Expand Up @@ -687,7 +688,7 @@ void BTA_GATTC_WriteCharValue ( UINT16 conn_id,
** Description This function is called to write descriptor value.
**
** Parameters conn_id - connection ID
** handle - descriptor hadle to write.
** handle - descriptor handle to write.
** write_type - write type.
** p_value - the value to be written.
**
Expand Down Expand Up @@ -738,7 +739,7 @@ void BTA_GATTC_WriteCharDescr (UINT16 conn_id,
** Description This function is called to prepare write a characteristic value.
**
** Parameters conn_id - connection ID.
** p_char_id - GATT characteritic ID of the service.
** p_char_id - GATT characteristic ID of the service.
** offset - offset of the write value.
** len: length of the data to be written.
** p_value - the value to be written.
Expand Down Expand Up @@ -781,7 +782,7 @@ void BTA_GATTC_PrepareWrite (UINT16 conn_id, UINT16 handle,
** Description This function is called to prepare write a characteristic descriptor value.
**
** Parameters conn_id - connection ID.
** p_char_descr_id - GATT characteritic descriptor ID of the service.
** p_char_descr_id - GATT characteristic descriptor ID of the service.
** offset - offset of the write value.
** len: length of the data to be written.
** p_value - the value to be written.
Expand Down
5 changes: 3 additions & 2 deletions components/bt/host/bluedroid/bta/gatt/include/bta_gattc_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ typedef enum {
#define BTA_GATTC_WRITE_PREPARE GATT_WRITE_PREPARE
#define BTA_GATTC_INVALID_HANDLE 0

/* internal strucutre for GATTC register API */
/* internal structure for GATTC register API */
typedef struct {
BT_HDR hdr;
tBT_UUID app_uuid;
Expand All @@ -133,6 +133,7 @@ typedef struct {
BOOLEAN is_direct;
BOOLEAN is_aux;
tBTA_TRANSPORT transport;
tBTA_ADDR_TYPE own_addr_type;
} tBTA_GATTC_API_OPEN;

typedef tBTA_GATTC_API_OPEN tBTA_GATTC_API_CANCEL_OPEN;
Expand Down Expand Up @@ -313,7 +314,7 @@ typedef struct {
UINT16 total_char;
UINT16 total_attr;
UINT8 srvc_hdl_chg; /* service handle change indication pending */
UINT16 attr_index; /* cahce NV saving/loading attribute index */
UINT16 attr_index; /* cache NV saving/loading attribute index */

UINT16 mtu;
bool update_incl_srvc;
Expand Down
Loading

0 comments on commit f420609

Please sign in to comment.