From b65f8a98bd33881e4b9c1bc5b37f9e15f7386553 Mon Sep 17 00:00:00 2001 From: zhanghaipeng Date: Fri, 15 Nov 2024 11:44:38 +0800 Subject: [PATCH] fix(ble/bluedroid): Fixed BLE create connection fail because of invalid own address type --- .../core/bluedroid_host/adapter.c | 8 +- components/bt/host/bluedroid/Kconfig.in | 8 +- .../bt/host/bluedroid/api/esp_gattc_api.c | 48 ++++--- .../bluedroid/api/include/api/esp_gatt_defs.h | 9 ++ .../bluedroid/api/include/api/esp_gattc_api.h | 17 ++- .../bt/host/bluedroid/bta/dm/bta_dm_act.c | 3 +- .../host/bluedroid/bta/gatt/bta_gattc_act.c | 5 + .../host/bluedroid/bta/gatt/bta_gattc_api.c | 17 +-- .../bta/gatt/include/bta_gattc_int.h | 5 +- .../bt/host/bluedroid/bta/hh/bta_hh_le.c | 38 ++--- .../bluedroid/bta/include/bta/bta_gatt_api.h | 24 ++-- .../btc/profile/std/gatt/btc_gattc.c | 7 +- .../btc/profile/std/include/btc_gattc.h | 1 + components/esp_hid/src/ble_hidh.c | 11 +- .../ble/ble_ancs/main/ble_ancs_demo.c | 9 +- .../ble/ble_spp_client/main/spp_client_demo.c | 9 +- .../main/example_ble_client_throughput.c | 9 +- .../ble/gatt_client/main/gattc_demo.c | 9 +- .../Gatt_Client_Example_Walkthrough.md | 11 +- .../main/example_ble_sec_gattc_demo.c | 9 +- .../main/gattc_multi_connect.c | 27 +++- ...nt_Multi_Connection_Example_Walkthrough.md | 131 +++++++++++++----- .../main/ble50_sec_gattc_demo.c | 7 + .../gattc_gatts_coex/main/gattc_gatts_coex.c | 12 +- 24 files changed, 312 insertions(+), 122 deletions(-) diff --git a/components/bt/esp_ble_mesh/core/bluedroid_host/adapter.c b/components/bt/esp_ble_mesh/core/bluedroid_host/adapter.c index d72b83b26396..f83e4731974a 100644 --- a/components/bt/esp_ble_mesh/core/bluedroid_host/adapter.c +++ b/components/bt/esp_ble_mesh/core/bluedroid_host/adapter.c @@ -1777,8 +1777,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 @@ -1788,8 +1788,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; diff --git a/components/bt/host/bluedroid/Kconfig.in b/components/bt/host/bluedroid/Kconfig.in index e5769d195932..dc235c2b9975 100644 --- a/components/bt/host/bluedroid/Kconfig.in +++ b/components/bt/host/bluedroid/Kconfig.in @@ -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" diff --git a/components/bt/host/bluedroid/api/esp_gattc_api.c b/components/bt/host/bluedroid/api/esp_gattc_api.c index 904fb32a0cc3..d59de8ed9d97 100644 --- a/components/bt/host/bluedroid/api/esp_gattc_api.c +++ b/components/bt/host/bluedroid/api/esp_gattc_api.c @@ -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; @@ -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) diff --git a/components/bt/host/bluedroid/api/include/api/esp_gatt_defs.h b/components/bt/host/bluedroid/api/include/api/esp_gatt_defs.h index 77f03e8bf34d..1dfbf861a5cd 100644 --- a/components/bt/host/bluedroid/api/include/api/esp_gatt_defs.h +++ b/components/bt/host/bluedroid/api/include/api/esp_gatt_defs.h @@ -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 diff --git a/components/bt/host/bluedroid/api/include/api/esp_gattc_api.h b/components/bt/host/bluedroid/api/include/api/esp_gattc_api.h index 0c274e203216..442fec2e53d2 100644 --- a/components/bt/host/bluedroid/api/include/api/esp_gattc_api.h +++ b/components/bt/host/bluedroid/api/include/api/esp_gattc_api.h @@ -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 * @@ -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, diff --git a/components/bt/host/bluedroid/bta/dm/bta_dm_act.c b/components/bt/host/bluedroid/bta/dm/bta_dm_act.c index 58d545d3b948..1fe4fa70c420 100644 --- a/components/bt/host/bluedroid/bta/dm/bta_dm_act.c +++ b/components/bt/host/bluedroid/bta/dm/bta_dm_act.c @@ -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) */ 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 543b9b04bf41..bff21212439e 100644 --- a/components/bt/host/bluedroid/bta/gatt/bta_gattc_act.c +++ b/components/bt/host/bluedroid/bta/gatt/bta_gattc_act.c @@ -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, @@ -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, diff --git a/components/bt/host/bluedroid/bta/gatt/bta_gattc_api.c b/components/bt/host/bluedroid/bta/gatt/bta_gattc_api.c index 6e2586d69c0e..87eb48b8913e 100644 --- a/components/bt/host/bluedroid/bta/gatt/bta_gattc_api.c +++ b/components/bt/host/bluedroid/bta/gatt/bta_gattc_api.c @@ -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 @@ -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; @@ -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); @@ -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 ** @@ -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 @@ -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. ** @@ -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. @@ -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. diff --git a/components/bt/host/bluedroid/bta/gatt/include/bta_gattc_int.h b/components/bt/host/bluedroid/bta/gatt/include/bta_gattc_int.h index 108358ca63b8..6d4bd5275a5a 100644 --- a/components/bt/host/bluedroid/bta/gatt/include/bta_gattc_int.h +++ b/components/bt/host/bluedroid/bta/gatt/include/bta_gattc_int.h @@ -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; @@ -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; @@ -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; diff --git a/components/bt/host/bluedroid/bta/hh/bta_hh_le.c b/components/bt/host/bluedroid/bta/hh/bta_hh_le.c index a80314efa6fe..d9cb98a3da66 100644 --- a/components/bt/host/bluedroid/bta/hh/bta_hh_le.c +++ b/components/bt/host/bluedroid/bta/hh/bta_hh_le.c @@ -117,7 +117,7 @@ static void bta_hh_le_hid_report_dbg(tBTA_HH_DEV_CB *p_cb) if (p_cb->hid_srvc[i].in_use) { p_rpt = &p_cb->hid_srvc[i].report[0]; - APPL_TRACE_DEBUG("\t HID serivce inst: %d", i); + APPL_TRACE_DEBUG("\t HID service inst: %d", i); for (j = 0; j < BTA_HH_LE_RPT_MAX; j ++, p_rpt++) { rpt_name = "Unknown"; @@ -334,14 +334,14 @@ void bta_hh_le_open_conn(tBTA_HH_DEV_CB *p_cb, BD_ADDR remote_bda) bta_hh_cb.le_cb_index[BTA_HH_GET_LE_CB_IDX(p_cb->hid_handle)] = p_cb->index; p_cb->in_use = TRUE; - BTA_GATTC_Open(bta_hh_cb.gatt_if, remote_bda, BLE_ADDR_UNKNOWN_TYPE, TRUE, BTA_GATT_TRANSPORT_LE, FALSE); + BTA_GATTC_Enh_Open(bta_hh_cb.gatt_if, remote_bda, BLE_ADDR_UNKNOWN_TYPE, TRUE, BTA_GATT_TRANSPORT_LE, FALSE, BLE_ADDR_UNKNOWN_TYPE); } /******************************************************************************* ** ** Function bta_hh_le_fill_16bits_gatt_id ** -** Description Utility function to fill a GATT ID strucure +** Description Utility function to fill a GATT ID structure ** *******************************************************************************/ void bta_hh_le_fill_16bits_gatt_id(UINT8 inst_id, UINT16 uuid, tBTA_GATT_ID *p_output) @@ -355,7 +355,7 @@ void bta_hh_le_fill_16bits_gatt_id(UINT8 inst_id, UINT16 uuid, tBTA_GATT_ID *p_ ** ** Function bta_hh_le_fill_16bits_srvc_id ** -** Description Utility function to fill a service ID strucure with a 16 bits +** Description Utility function to fill a service ID structure with a 16 bits ** service UUID. ** *******************************************************************************/ @@ -372,7 +372,7 @@ void bta_hh_le_fill_16bits_srvc_id(BOOLEAN is_pri, UINT8 inst_id, UINT16 srvc_uu ** ** Function bta_hh_le_fill_16bits_char_id ** -** Description Utility function to fill a char ID strucure with a 16 bits +** Description Utility function to fill a char ID structure with a 16 bits ** char UUID. ** *******************************************************************************/ @@ -624,7 +624,7 @@ tBTA_HH_STATUS bta_hh_le_read_char_dscrpt(tBTA_HH_DEV_CB *p_cb, UINT16 srvc_uuid ** ** Function bta_hh_le_read_rpt_ref_descr ** -** Description read report refernece descriptors in service discovery process +** Description read report reference descriptors in service discovery process ** *******************************************************************************/ void bta_hh_le_read_rpt_ref_descr(tBTA_HH_DEV_CB *p_dev_cb, tBTA_HH_LE_RPT *p_rpt) @@ -841,7 +841,7 @@ void bta_hh_le_register_input_notif(tBTA_HH_DEV_CB *p_dev_cb, UINT8 srvc_inst, } } /* - else unknow protocol mode */ + else unknown protocol mode */ } } } @@ -1486,7 +1486,7 @@ void bta_hh_le_gatt_disc_cmpl(tBTA_HH_DEV_CB *p_cb, tBTA_HH_STATUS status) { APPL_TRACE_DEBUG("bta_hh_le_gatt_disc_cmpl "); - /* if open sucessful or protocol mode not desired, keep the connection open but inform app */ + /* if open successful or protocol mode not desired, keep the connection open but inform app */ if (status == BTA_HH_OK || status == BTA_HH_ERR_PROTO) { /* assign a special APP ID temp, since device type unknown */ p_cb->app_id = BTA_HH_APP_ID_LE; @@ -1504,7 +1504,7 @@ void bta_hh_le_gatt_disc_cmpl(tBTA_HH_DEV_CB *p_cb, tBTA_HH_STATUS status) ** ** Function bta_hh_le_srvc_expl_srvc ** -** Description This function discover the next avaible HID service. +** Description This function discover the next available HID service. ** ** Parameters: ** @@ -2076,7 +2076,7 @@ void bta_hh_w4_le_read_descr_cmpl(tBTA_HH_DEV_CB *p_dev_cb, tBTA_HH_DATA *p_buf) ** ** Function bta_hh_w4_le_write_cmpl ** -** Description Write charactersitic complete event at W4_CONN st. +** Description Write characteristic complete event at W4_CONN st. ** ** Parameters: ** @@ -2104,7 +2104,7 @@ void bta_hh_w4_le_write_cmpl(tBTA_HH_DEV_CB *p_dev_cb, tBTA_HH_DATA *p_buf) ** ** Function bta_hh_le_write_cmpl ** -** Description Write charactersitic complete event at CONN st. +** Description Write characteristic complete event at CONN st. ** ** Parameters: ** @@ -2162,7 +2162,7 @@ void bta_hh_le_write_cmpl(tBTA_HH_DEV_CB *p_dev_cb, tBTA_HH_DATA *p_buf) ** ** Function bta_hh_le_write_char_descr_cmpl ** -** Description Write charactersitic descriptor complete event +** Description Write characteristic descriptor complete event ** ** Parameters: ** @@ -2216,7 +2216,7 @@ void bta_hh_le_write_char_descr_cmpl(tBTA_HH_DEV_CB *p_dev_cb, tBTA_HH_DATA *p_b ** ** Function bta_hh_le_input_rpt_notify ** -** Description process the notificaton event, most likely for input report. +** Description process the notification event, most likely for input report. ** ** Parameters: ** @@ -2357,7 +2357,7 @@ void bta_hh_gatt_close(tBTA_HH_DEV_CB *p_cb, tBTA_HH_DATA *p_data) ** ** Function bta_hh_le_api_disc_act ** -** Description initaite a Close API to a remote HID device +** Description initiate a Close API to a remote HID device ** ** Returns void ** @@ -2601,7 +2601,7 @@ static void bta_hh_le_add_dev_bg_conn(tBTA_HH_DEV_CB *p_cb, BOOLEAN check_bond) if (/*p_cb->dscp_info.flag & BTA_HH_LE_NORMAL_CONN &&*/ !p_cb->in_bg_conn && to_add) { /* add device into BG connection to accept remote initiated connection */ - BTA_GATTC_Open(bta_hh_cb.gatt_if, p_cb->addr, BLE_ADDR_UNKNOWN_TYPE, FALSE, BTA_GATT_TRANSPORT_LE, FALSE); + BTA_GATTC_Enh_Open(bta_hh_cb.gatt_if, p_cb->addr, BLE_ADDR_UNKNOWN_TYPE, FALSE, BTA_GATT_TRANSPORT_LE, FALSE, BLE_ADDR_UNKNOWN_TYPE); p_cb->in_bg_conn = TRUE; BTA_DmBleSetBgConnType(BTA_DM_BLE_CONN_AUTO, NULL); @@ -2682,7 +2682,7 @@ void bta_hh_le_update_scpp(tBTA_HH_DEV_CB *p_dev_cb, tBTA_HH_DATA *p_buf) if (!p_dev_cb->is_le_device || p_dev_cb->mode != BTA_HH_PROTO_RPT_MODE || p_dev_cb->scps_supported == FALSE) { - APPL_TRACE_ERROR("Can not set ScPP scan paramter as boot host, or remote does not support ScPP "); + APPL_TRACE_ERROR("Can not set ScPP scan parameter as boot host, or remote does not support ScPP "); cback_data.handle = p_dev_cb->hid_handle; cback_data.status = BTA_HH_ERR; @@ -2908,7 +2908,7 @@ static void bta_hh_le_search_scps_chars(tBTA_HH_DEV_CB *p_cb) ** ** Function bta_hh_le_register_scpp_notif ** -** Description register scan parameter refresh notitication complete +** Description register scan parameter refresh notification complete ** ** ** Parameters: @@ -2919,7 +2919,7 @@ static void bta_hh_le_register_scpp_notif(tBTA_HH_DEV_CB *p_dev_cb, tBTA_GATT_ST UINT8 sec_flag = 0; tBTA_GATTC_CHAR_ID char_id; - /* if write scan parameter sucessful */ + /* if write scan parameter successful */ /* if bonded and notification is not enabled, configure the client configuration */ if (status == BTA_GATT_OK && (p_dev_cb->scps_notify & BTA_HH_LE_SCPS_NOTIFY_SPT) != 0 && @@ -2949,7 +2949,7 @@ static void bta_hh_le_register_scpp_notif(tBTA_HH_DEV_CB *p_dev_cb, tBTA_GATT_ST ** ** Function bta_hh_le_register_scpp_notif_cmpl ** -** Description action function to register scan parameter refresh notitication +** Description action function to register scan parameter refresh notification ** ** Parameters: ** diff --git a/components/bt/host/bluedroid/bta/include/bta/bta_gatt_api.h b/components/bt/host/bluedroid/bta/include/bta/bta_gatt_api.h index 183e3acc5ad2..55c4e7074a74 100644 --- a/components/bt/host/bluedroid/bta/include/bta/bta_gatt_api.h +++ b/components/bt/host/bluedroid/bta/include/bta/bta_gatt_api.h @@ -180,7 +180,7 @@ typedef UINT8 tBTA_GATT_STATUS; #define BTA_GATTC_ADV_VSC_EVT 34 /* ADV VSC event */ #define BTA_GATTC_CONNECT_EVT 35 /* GATTC CONNECT event */ #define BTA_GATTC_DISCONNECT_EVT 36 /* GATTC DISCONNECT event */ -#define BTA_GATTC_READ_MULTIPLE_EVT 37 /* GATTC Read mutiple event */ +#define BTA_GATTC_READ_MULTIPLE_EVT 37 /* GATTC Read multiple event */ #define BTA_GATTC_QUEUE_FULL_EVT 38 /* GATTC queue full event */ #define BTA_GATTC_ASSOC_EVT 39 /* GATTC association address event */ #define BTA_GATTC_GET_ADDR_LIST_EVT 40 /* GATTC get address list in the cache event */ @@ -783,9 +783,10 @@ extern void BTA_GATTC_AppRegister(tBT_UUID *p_app_uuid, tBTA_GATTC_CBACK *p_clie *******************************************************************************/ extern 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 @@ -793,13 +794,14 @@ extern void BTA_GATTC_AppDeregister (tBTA_GATTC_IF client_if); ** Parameters client_if: server interface. ** remote_bda: remote device BD address. ** remote_addr_type: remote device BD address type. -** is_direct: direct connection or background auto connection +** is_direct: direct connection or background auto connection. +** own_addr_type: own address type. ** ** Returns void ** *******************************************************************************/ -extern 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); +extern 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); /******************************************************************************* ** @@ -937,8 +939,8 @@ extern void BTA_GATTC_GetGattDb(UINT16 conn_id, UINT16 start_handle, UINT16 end_ ** ** Description This function is called to read a characteristics value ** -** Parameters conn_id - connectino ID. -** handle - characteritic handle to read. +** Parameters conn_id - connection ID. +** handle - characteristic handle to read. ** ** Returns None ** @@ -953,7 +955,7 @@ void BTA_GATTC_ReadCharacteristic(UINT16 conn_id, UINT16 handle, tBTA_GATT_AUTH_ ** ** Parameters conn_id - connection ID. ** s_handle - start handle. -** e_handle - end hanle +** e_handle - end handle ** uuid - The attribute UUID. ** ** Returns None @@ -1072,7 +1074,7 @@ extern tBTA_GATT_STATUS BTA_GATTC_DeregisterForNotifications (tBTA_GATTC_IF ** Description This function is called to prepare write a characteristic value. ** ** Parameters conn_id - connection ID. -** handle - GATT characteritic handle. +** handle - GATT characteristic handle. ** offset - offset of the write value. ** len - length of the data to be written. ** p_value - the value to be written. @@ -1094,7 +1096,7 @@ extern void BTA_GATTC_PrepareWrite (UINT16 conn_id, ** 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. @@ -1238,7 +1240,7 @@ extern void BTA_GATTC_ConfigureMTU (UINT16 conn_id); ** ** Function BTA_GATTS_Init ** -** Description This function is called to initalize GATTS module +** Description This function is called to initialize GATTS module ** ** Parameters None ** diff --git a/components/bt/host/bluedroid/btc/profile/std/gatt/btc_gattc.c b/components/bt/host/bluedroid/btc/profile/std/gatt/btc_gattc.c index e69eed48f32d..94db6dc05a19 100644 --- a/components/bt/host/bluedroid/btc/profile/std/gatt/btc_gattc.c +++ b/components/bt/host/bluedroid/btc/profile/std/gatt/btc_gattc.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 @@ -212,9 +212,10 @@ static void btc_gattc_app_unregister(btc_ble_gattc_args_t *arg) static void btc_gattc_open(btc_ble_gattc_args_t *arg) { tBTA_GATT_TRANSPORT transport = BTA_GATT_TRANSPORT_LE; - BTA_GATTC_Open(arg->open.gattc_if, arg->open.remote_bda, + + BTA_GATTC_Enh_Open(arg->open.gattc_if, arg->open.remote_bda, arg->open.remote_addr_type, arg->open.is_direct, - transport, arg->open.is_aux); + transport, arg->open.is_aux, arg->open.own_addr_type); } static void btc_gattc_close(btc_ble_gattc_args_t *arg) diff --git a/components/bt/host/bluedroid/btc/profile/std/include/btc_gattc.h b/components/bt/host/bluedroid/btc/profile/std/include/btc_gattc.h index eeebb427f223..0a37145b9bd0 100644 --- a/components/bt/host/bluedroid/btc/profile/std/include/btc_gattc.h +++ b/components/bt/host/bluedroid/btc/profile/std/include/btc_gattc.h @@ -57,6 +57,7 @@ typedef union { esp_ble_addr_type_t remote_addr_type; bool is_direct; bool is_aux; + esp_ble_addr_type_t own_addr_type; } open; //BTC_GATTC_ACT_CLOSE, struct close_arg { diff --git a/components/esp_hid/src/ble_hidh.c b/components/esp_hid/src/ble_hidh.c index a11c53868e4c..d5ecc189b3bc 100644 --- a/components/esp_hid/src/ble_hidh.c +++ b/components/esp_hid/src/ble_hidh.c @@ -672,10 +672,17 @@ esp_hidh_dev_t *esp_ble_hidh_dev_open(esp_bd_addr_t bda, esp_ble_addr_type_t add dev->ble.address_type = address_type; dev->ble.appearance = ESP_HID_APPEARANCE_GENERIC; - ret = esp_ble_gattc_open(hid_gattc_if, dev->addr.bda, dev->ble.address_type, true); + esp_ble_gatt_creat_conn_params_t esp_ble_gatt_create_conn; + memcpy(&esp_ble_gatt_create_conn.remote_bda, dev->addr.bda, ESP_BD_ADDR_LEN); + esp_ble_gatt_create_conn.remote_addr_type = dev->ble.address_type; + esp_ble_gatt_create_conn.own_addr_type = BLE_ADDR_TYPE_PUBLIC; + esp_ble_gatt_create_conn.is_direct = true; + esp_ble_gatt_create_conn.is_aux = false; + ret = esp_ble_gattc_enh_open(hid_gattc_if, + esp_ble_gatt_create_conn); if (ret) { esp_hidh_dev_free_inner(dev); - ESP_LOGE(TAG, "esp_ble_gattc_open failed: %d", ret); + ESP_LOGE(TAG, "esp_ble_gattc_enh_open failed: %d", ret); return NULL; } WAIT_CB(); diff --git a/examples/bluetooth/bluedroid/ble/ble_ancs/main/ble_ancs_demo.c b/examples/bluetooth/bluedroid/ble/ble_ancs/main/ble_ancs_demo.c index 39f767097861..80a309c32ed2 100644 --- a/examples/bluetooth/bluedroid/ble/ble_ancs/main/ble_ancs_demo.c +++ b/examples/bluetooth/bluedroid/ble/ble_ancs/main/ble_ancs_demo.c @@ -563,7 +563,14 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_ //ESP_LOGI(BLE_ANCS_TAG, "ESP_GATTC_CONNECT_EVT"); memcpy(gl_profile_tab[PROFILE_A_APP_ID].remote_bda, param->connect.remote_bda, 6); // create gattc virtual connection - esp_ble_gattc_open(gl_profile_tab[PROFILE_A_APP_ID].gattc_if, gl_profile_tab[PROFILE_A_APP_ID].remote_bda, BLE_ADDR_TYPE_RANDOM, true); + esp_ble_gatt_creat_conn_params_t esp_ble_gatt_create_conn; + memcpy(&esp_ble_gatt_create_conn.remote_bda, gl_profile_tab[PROFILE_A_APP_ID].remote_bda, ESP_BD_ADDR_LEN); + esp_ble_gatt_create_conn.remote_addr_type = BLE_ADDR_TYPE_RANDOM; + esp_ble_gatt_create_conn.own_addr_type = BLE_ADDR_TYPE_RPA_PUBLIC; + esp_ble_gatt_create_conn.is_direct = true; + esp_ble_gatt_create_conn.is_aux = false; + esp_ble_gattc_enh_open(gl_profile_tab[PROFILE_A_APP_ID].gattc_if, + esp_ble_gatt_create_conn); break; case ESP_GATTC_DIS_SRVC_CMPL_EVT: ESP_LOGI(BLE_ANCS_TAG, "ESP_GATTC_DIS_SRVC_CMPL_EVT"); diff --git a/examples/bluetooth/bluedroid/ble/ble_spp_client/main/spp_client_demo.c b/examples/bluetooth/bluedroid/ble/ble_spp_client/main/spp_client_demo.c index 29eae66b1ebe..ff43fe2554bd 100644 --- a/examples/bluetooth/bluedroid/ble/ble_spp_client/main/spp_client_demo.c +++ b/examples/bluetooth/bluedroid/ble/ble_spp_client/main/spp_client_demo.c @@ -238,7 +238,14 @@ static void esp_gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *par ESP_LOGI(GATTC_TAG, "Scan stop successfully"); if (is_connect == false) { ESP_LOGI(GATTC_TAG, "Connect to the remote device."); - esp_ble_gattc_open(gl_profile_tab[PROFILE_APP_ID].gattc_if, scan_rst.scan_rst.bda, scan_rst.scan_rst.ble_addr_type, true); + esp_ble_gatt_creat_conn_params_t esp_ble_gatt_create_conn; + memcpy(&esp_ble_gatt_create_conn.remote_bda, scan_rst.scan_rst.bda,ESP_BD_ADDR_LEN); + esp_ble_gatt_create_conn.remote_addr_type = scan_rst.scan_rst.ble_addr_type; + esp_ble_gatt_create_conn.own_addr_type = BLE_ADDR_TYPE_PUBLIC; + esp_ble_gatt_create_conn.is_direct = true; + esp_ble_gatt_create_conn.is_aux = false; + esp_ble_gattc_enh_open(gl_profile_tab[PROFILE_APP_ID].gattc_if, + esp_ble_gatt_create_conn); } break; case ESP_GAP_BLE_SCAN_RESULT_EVT: { diff --git a/examples/bluetooth/bluedroid/ble/ble_throughput/throughput_client/main/example_ble_client_throughput.c b/examples/bluetooth/bluedroid/ble/ble_throughput/throughput_client/main/example_ble_client_throughput.c index 1e0e56fb14ec..8ba19776e95c 100644 --- a/examples/bluetooth/bluedroid/ble/ble_throughput/throughput_client/main/example_ble_client_throughput.c +++ b/examples/bluetooth/bluedroid/ble/ble_throughput/throughput_client/main/example_ble_client_throughput.c @@ -408,7 +408,14 @@ static void esp_gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *par #endif esp_ble_gap_stop_scanning(); - esp_ble_gattc_open(gl_profile_tab[PROFILE_A_APP_ID].gattc_if, scan_result->scan_rst.bda, BLE_ADDR_TYPE_PUBLIC, true); + esp_ble_gatt_creat_conn_params_t esp_ble_gatt_create_conn; + memcpy(&esp_ble_gatt_create_conn.remote_bda, scan_result->scan_rst.bda, ESP_BD_ADDR_LEN); + esp_ble_gatt_create_conn.remote_addr_type = scan_result->scan_rst.ble_addr_type; + esp_ble_gatt_create_conn.own_addr_type = BLE_ADDR_TYPE_PUBLIC; + esp_ble_gatt_create_conn.is_direct = true; + esp_ble_gatt_create_conn.is_aux = false; + esp_ble_gattc_enh_open(gl_profile_tab[PROFILE_A_APP_ID].gattc_if, + esp_ble_gatt_create_conn); } } } diff --git a/examples/bluetooth/bluedroid/ble/gatt_client/main/gattc_demo.c b/examples/bluetooth/bluedroid/ble/gatt_client/main/gattc_demo.c index 6071e3dba8a3..6d1b021180ce 100644 --- a/examples/bluetooth/bluedroid/ble/gatt_client/main/gattc_demo.c +++ b/examples/bluetooth/bluedroid/ble/gatt_client/main/gattc_demo.c @@ -370,7 +370,14 @@ static void esp_gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *par connect = true; ESP_LOGI(GATTC_TAG, "Connect to the remote device"); esp_ble_gap_stop_scanning(); - esp_ble_gattc_open(gl_profile_tab[PROFILE_A_APP_ID].gattc_if, scan_result->scan_rst.bda, scan_result->scan_rst.ble_addr_type, true); + esp_ble_gatt_creat_conn_params_t esp_ble_gatt_create_conn; + memcpy(&esp_ble_gatt_create_conn.remote_bda, scan_result->scan_rst.bda, ESP_BD_ADDR_LEN); + esp_ble_gatt_create_conn.remote_addr_type = scan_result->scan_rst.ble_addr_type; + esp_ble_gatt_create_conn.own_addr_type = BLE_ADDR_TYPE_PUBLIC; + esp_ble_gatt_create_conn.is_direct = true; + esp_ble_gatt_create_conn.is_aux = false; + esp_ble_gattc_enh_open(gl_profile_tab[PROFILE_A_APP_ID].gattc_if, + esp_ble_gatt_create_conn); } } } diff --git a/examples/bluetooth/bluedroid/ble/gatt_client/tutorial/Gatt_Client_Example_Walkthrough.md b/examples/bluetooth/bluedroid/ble/gatt_client/tutorial/Gatt_Client_Example_Walkthrough.md index fa9162251a9a..38470833c031 100644 --- a/examples/bluetooth/bluedroid/ble/gatt_client/tutorial/Gatt_Client_Example_Walkthrough.md +++ b/examples/bluetooth/bluedroid/ble/gatt_client/tutorial/Gatt_Client_Example_Walkthrough.md @@ -372,7 +372,14 @@ We are interested in the `ESP_GAP_SEARCH_INQ_RES_EVT` event, which is called eve connect = true; ESP_LOGI(GATTC_TAG, "connect to the remote device."); esp_ble_gap_stop_scanning(); - esp_ble_gattc_open(gl_profile_tab[PROFILE_A_APP_ID].gattc_if, scan_result->scan_rst.bda, scan_result->scan_rst.ble_addr_type, true); + esp_ble_gatt_creat_conn_params_t esp_ble_gatt_create_conn; + memcpy(&esp_ble_gatt_create_conn.remote_bda, scan_result->scan_rst.bda, ESP_BD_ADDR_LEN); + esp_ble_gatt_create_conn.remote_addr_type = scan_result->scan_rst.ble_addr_type; + esp_ble_gatt_create_conn.own_addr_type = BLE_ADDR_TYPE_PUBLIC; + esp_ble_gatt_create_conn.is_direct = true; + esp_ble_gatt_create_conn.is_aux = false; + esp_ble_gattc_enh_open(gl_profile_tab[PROFILE_A_APP_ID].gattc_if, + esp_ble_gatt_create_conn); } } } @@ -405,7 +412,7 @@ ESP_LOGI(GATTC_TAG, "searched Device Name Len %d", adv_name_len); ESP_LOG_BUFFER_CHAR(GATTC_TAG, adv_name, adv_name_len); ``` -Finally if the remote device name is the same as we have defined above, the local device stops scanning and tries to open a connection to the remote device using the `esp_ble_gattc_open()` function. This function takes as parameters the Application Profile GATT interface, the remote server address and a boolean value. The boolean value is used to indicate if the connection is done directly or if it’s done in the background (auto-connection), at the moment this boolean value must be set to true in order to establish the connection. Notice that the client opens a virtual connection to the server. The virtual connection returns a connection ID. The virtual connection is the connection between the Application Profile and the remote server. Since many Application Profiles can run on one ESP32, there could be many virtual connection opened to the same remote server. There is also the physical connection which is the actual BLE link between the client and the server. Therefore, if the physical connection is disconnected with the `esp_ble_gap_disconnect()` function, all other virtual connections are closed as well. In this example, each Application Profile creates a virtual connection to the same server with the `esp_ble_gattc_open()` function, so when the close function is called, only that connection from the Application Profile is closed, while if the gap disconnect function is called, both connections will be closed. In addition, connect events are propagated to all profiles because it relates to the physical connection, while open events are propagated only to the profile that creates the virtual connection. +Finally if the remote device name is the same as we have defined above, the local device stops scanning and tries to open a connection to the remote device using the `esp_ble_gattc_enh_open()` function. This function takes as parameters the Application Profile GATT interface, the remote server address and a boolean value. The boolean value is used to indicate if the connection is done directly or if it’s done in the background (auto-connection), at the moment this boolean value must be set to true in order to establish the connection. Notice that the client opens a virtual connection to the server. The virtual connection returns a connection ID. The virtual connection is the connection between the Application Profile and the remote server. Since many Application Profiles can run on one ESP32, there could be many virtual connection opened to the same remote server. There is also the physical connection which is the actual BLE link between the client and the server. Therefore, if the physical connection is disconnected with the `esp_ble_gap_disconnect()` function, all other virtual connections are closed as well. In this example, each Application Profile creates a virtual connection to the same server with the `esp_ble_gattc_enh_open()` function, so when the close function is called, only that connection from the Application Profile is closed, while if the gap disconnect function is called, both connections will be closed. In addition, connect events are propagated to all profiles because it relates to the physical connection, while open events are propagated only to the profile that creates the virtual connection. ## Configuring the MTU Size diff --git a/examples/bluetooth/bluedroid/ble/gatt_security_client/main/example_ble_sec_gattc_demo.c b/examples/bluetooth/bluedroid/ble/gatt_security_client/main/example_ble_sec_gattc_demo.c index 76e0fdba3ade..13484d1b994e 100644 --- a/examples/bluetooth/bluedroid/ble/gatt_security_client/main/example_ble_sec_gattc_demo.c +++ b/examples/bluetooth/bluedroid/ble/gatt_security_client/main/example_ble_sec_gattc_demo.c @@ -465,7 +465,14 @@ static void esp_gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *par connect = true; ESP_LOGI(GATTC_TAG, "Connect to the remote device"); esp_ble_gap_stop_scanning(); - esp_ble_gattc_open(gl_profile_tab[PROFILE_A_APP_ID].gattc_if, scan_result->scan_rst.bda, scan_result->scan_rst.ble_addr_type, true); + esp_ble_gatt_creat_conn_params_t esp_ble_gatt_create_conn; + memcpy(&esp_ble_gatt_create_conn.remote_bda, scan_result->scan_rst.bda, ESP_BD_ADDR_LEN); + esp_ble_gatt_create_conn.remote_addr_type = scan_result->scan_rst.ble_addr_type; + esp_ble_gatt_create_conn.own_addr_type = BLE_ADDR_TYPE_RPA_PUBLIC; + esp_ble_gatt_create_conn.is_direct = true; + esp_ble_gatt_create_conn.is_aux = false; + esp_ble_gattc_enh_open(gl_profile_tab[PROFILE_A_APP_ID].gattc_if, + esp_ble_gatt_create_conn); } } } diff --git a/examples/bluetooth/bluedroid/ble/gattc_multi_connect/main/gattc_multi_connect.c b/examples/bluetooth/bluedroid/ble/gattc_multi_connect/main/gattc_multi_connect.c index d07048d6d06d..2307d203fdda 100644 --- a/examples/bluetooth/bluedroid/ble/gattc_multi_connect/main/gattc_multi_connect.c +++ b/examples/bluetooth/bluedroid/ble/gattc_multi_connect/main/gattc_multi_connect.c @@ -817,7 +817,14 @@ static void esp_gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *par conn_device_a = true; ESP_LOGI(GATTC_TAG, "Searched device %s", remote_device_name[0]); esp_ble_gap_stop_scanning(); - esp_ble_gattc_open(gl_profile_tab[PROFILE_A_APP_ID].gattc_if, scan_result->scan_rst.bda, scan_result->scan_rst.ble_addr_type, true); + esp_ble_gatt_creat_conn_params_t esp_ble_gatt_create_conn; + memcpy(&esp_ble_gatt_create_conn.remote_bda, scan_result->scan_rst.bda, ESP_BD_ADDR_LEN); + esp_ble_gatt_create_conn.remote_addr_type = scan_result->scan_rst.ble_addr_type; + esp_ble_gatt_create_conn.own_addr_type = BLE_ADDR_TYPE_PUBLIC; + esp_ble_gatt_create_conn.is_direct = true; + esp_ble_gatt_create_conn.is_aux = false; + esp_ble_gattc_enh_open(gl_profile_tab[PROFILE_A_APP_ID].gattc_if, + esp_ble_gatt_create_conn); Isconnecting = true; } break; @@ -827,7 +834,14 @@ static void esp_gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *par conn_device_b = true; ESP_LOGI(GATTC_TAG, "Searched device %s", remote_device_name[1]); esp_ble_gap_stop_scanning(); - esp_ble_gattc_open(gl_profile_tab[PROFILE_B_APP_ID].gattc_if, scan_result->scan_rst.bda, scan_result->scan_rst.ble_addr_type, true); + esp_ble_gatt_creat_conn_params_t esp_ble_gatt_create_conn; + memcpy(&esp_ble_gatt_create_conn.remote_bda, scan_result->scan_rst.bda, ESP_BD_ADDR_LEN); + esp_ble_gatt_create_conn.remote_addr_type = scan_result->scan_rst.ble_addr_type; + esp_ble_gatt_create_conn.own_addr_type = BLE_ADDR_TYPE_PUBLIC; + esp_ble_gatt_create_conn.is_direct = true; + esp_ble_gatt_create_conn.is_aux = false; + esp_ble_gattc_enh_open(gl_profile_tab[PROFILE_A_APP_ID].gattc_if, + esp_ble_gatt_create_conn); Isconnecting = true; } @@ -837,7 +851,14 @@ static void esp_gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *par conn_device_c = true; ESP_LOGI(GATTC_TAG, "Searched device %s", remote_device_name[2]); esp_ble_gap_stop_scanning(); - esp_ble_gattc_open(gl_profile_tab[PROFILE_C_APP_ID].gattc_if, scan_result->scan_rst.bda, scan_result->scan_rst.ble_addr_type, true); + esp_ble_gatt_creat_conn_params_t esp_ble_gatt_create_conn; + memcpy(&esp_ble_gatt_create_conn.remote_bda, scan_result->scan_rst.bda, ESP_BD_ADDR_LEN); + esp_ble_gatt_create_conn.remote_addr_type = scan_result->scan_rst.ble_addr_type; + esp_ble_gatt_create_conn.own_addr_type = BLE_ADDR_TYPE_PUBLIC; + esp_ble_gatt_create_conn.is_direct = true; + esp_ble_gatt_create_conn.is_aux = false; + esp_ble_gattc_enh_open(gl_profile_tab[PROFILE_A_APP_ID].gattc_if, + esp_ble_gatt_create_conn); Isconnecting = true; } break; diff --git a/examples/bluetooth/bluedroid/ble/gattc_multi_connect/tutorial/Gatt_Client_Multi_Connection_Example_Walkthrough.md b/examples/bluetooth/bluedroid/ble/gattc_multi_connect/tutorial/Gatt_Client_Multi_Connection_Example_Walkthrough.md index b3375db079d3..c946ae697ea8 100644 --- a/examples/bluetooth/bluedroid/ble/gattc_multi_connect/tutorial/Gatt_Client_Multi_Connection_Example_Walkthrough.md +++ b/examples/bluetooth/bluedroid/ble/gattc_multi_connect/tutorial/Gatt_Client_Multi_Connection_Example_Walkthrough.md @@ -116,35 +116,106 @@ if (find_device_1 && find_device_2 && find_device_3 && stop_scan == false { The scan stop triggers an ``ESP_GAP_BLE_SCAN_STOP_COMPLETE_EVT`` event which is used to open a connection to the first remote device. The second and third devices get connected once the client searches for services, gets characteristics and registers for notifications on the first device. This workflow is designed to test that the communication between each remote device is working correctly before trying to connect to the next device or in case of error, skip to the next device. ```c -case ESP_GAP_BLE_SCAN_STOP_COMPLETE_EVT: - if (param->scan_stop_cmpl.status != ESP_BT_STATUS_SUCCESS){ - ESP_LOGE(GATTC_TAG, "Scan stop failed"); + case ESP_GAP_BLE_SCAN_STOP_COMPLETE_EVT: + if (param->scan_stop_cmpl.status != ESP_BT_STATUS_SUCCESS){ + ESP_LOGE(GATTC_TAG, "Scan stop failed"); + break; + } + ESP_LOGI(GATTC_TAG, "Stop scan successfully"); + break; - } - ESP_LOGI(GATTC_TAG, "Stop scan successfully"); - if (!stop_scan){ - ESP_LOGE(GATTC_TAG, "Did not find all devices"); - } - if (find_device_1){ - esp_ble_gattc_open(gl_profile_tab[PROFILE_A_APP_ID].gattc_if, gl_profile_tab[PROFILE_A_APP_ID].remote_bda, true); - } - break; ``` -* The connection is opened with the ``esp_ble_gattc_open()`` function which takes the GATT interface, the remote device address and a boolean value set to true for direct connection or false for background auto connection. To disconnect the physical connection, the GAP API function ``esp_ble_gap_disconnect()`` is used. +* The connection is opened with the ``esp_ble_gattc_enh_open()`` function which takes the GATT interface, the remote device address and a boolean value set to true for direct connection or false for background auto connection. To disconnect the physical connection, the GAP API function ``esp_ble_gap_disconnect()`` is used. - When connecting to the first device, an ``ESP_GATTC_CONNECT_EVT`` event is generated which is forwarded to all profiles. It also triggers an ``ESP_GATTC_OPEN_EVT`` event that is forwarded to the Profile A event handler only, or ``gattc_profile_a_event_handler()`` function. The event checks that the connection is opened successfully, if not, the device is ignored and the client tries to open a connection to the second device: + When connecting to the first device, an ``ESP_GATTC_CONNECT_EVT`` event is generated which is forwarded to all profiles. It also triggers an ``ESP_GATTC_OPEN_EVT`` event that is forwarded to the Profile A event handler only, or ``gattc_profile_a_event_handler()`` function. The event checks that the connection is opened successfully, if not, the device is ignored and the client tries to open a connection to the second device. ```c - case ESP_GATTC_OPEN_EVT: - if (p_data->open.status != ESP_GATT_OK){ - //open failed, ignore the first device, connect the second device - ESP_LOGE(GATTC_TAG, "connect device failed, status %d", p_data->open.status); - if (find_device_2){ - esp_ble_gattc_open(gl_profile_tab[PROFILE_B_APP_ID].gattc_if, gl_profile_tab[PROFILE_B_APP_ID].remote_bda, true); + case ESP_GAP_BLE_SCAN_RESULT_EVT: { + esp_ble_gap_cb_param_t *scan_result = (esp_ble_gap_cb_param_t *)param; + switch (scan_result->scan_rst.search_evt) { + case ESP_GAP_SEARCH_INQ_RES_EVT: + ESP_LOG_BUFFER_HEX(GATTC_TAG, scan_result->scan_rst.bda, 6); + ESP_LOGI(GATTC_TAG, "Searched Adv Data Len %d, Scan Response Len %d", scan_result->scan_rst.adv_data_len, scan_result->scan_rst.scan_rsp_len); + adv_name = esp_ble_resolve_adv_data_by_type(scan_result->scan_rst.ble_adv, + scan_result->scan_rst.adv_data_len + scan_result->scan_rst.scan_rsp_len, + ESP_BLE_AD_TYPE_NAME_CMPL, + &adv_name_len); + ESP_LOGI(GATTC_TAG, "Searched Device Name Len %d", adv_name_len); + ESP_LOG_BUFFER_CHAR(GATTC_TAG, adv_name, adv_name_len); + ESP_LOGI(GATTC_TAG, " "); + if (Isconnecting){ + break; + } + if (conn_device_a && conn_device_b && conn_device_c && !stop_scan_done){ + stop_scan_done = true; + esp_ble_gap_stop_scanning(); + ESP_LOGI(GATTC_TAG, "all devices are connected"); + break; + } + if (adv_name != NULL) { + + if (strlen(remote_device_name[0]) == adv_name_len && strncmp((char *)adv_name, remote_device_name[0], adv_name_len) == 0) { + if (conn_device_a == false) { + conn_device_a = true; + ESP_LOGI(GATTC_TAG, "Searched device %s", remote_device_name[0]); + esp_ble_gap_stop_scanning(); + esp_ble_gatt_creat_conn_params_t esp_ble_gatt_create_conn; + memcpy(&esp_ble_gatt_create_conn.remote_bda, scan_result->scan_rst.bda, ESP_BD_ADDR_LEN); + esp_ble_gatt_create_conn.remote_addr_type = scan_result->scan_rst.ble_addr_type; + esp_ble_gatt_create_conn.own_addr_type = BLE_ADDR_TYPE_PUBLIC; + esp_ble_gatt_create_conn.is_direct = true; + esp_ble_gatt_create_conn.is_aux = false; + esp_ble_gattc_enh_open(gl_profile_tab[PROFILE_A_APP_ID].gattc_if, + esp_ble_gatt_create_conn); + Isconnecting = true; + } + break; + } + else if (strlen(remote_device_name[1]) == adv_name_len && strncmp((char *)adv_name, remote_device_name[1], adv_name_len) == 0) { + if (conn_device_b == false) { + conn_device_b = true; + ESP_LOGI(GATTC_TAG, "Searched device %s", remote_device_name[1]); + esp_ble_gap_stop_scanning(); + esp_ble_gatt_creat_conn_params_t esp_ble_gatt_create_conn; + memcpy(&esp_ble_gatt_create_conn.remote_bda, scan_result->scan_rst.bda, ESP_BD_ADDR_LEN); + esp_ble_gatt_create_conn.remote_addr_type = scan_result->scan_rst.ble_addr_type; + esp_ble_gatt_create_conn.own_addr_type = BLE_ADDR_TYPE_PUBLIC; + esp_ble_gatt_create_conn.is_direct = true; + esp_ble_gatt_create_conn.is_aux = false; + esp_ble_gattc_enh_open(gl_profile_tab[PROFILE_A_APP_ID].gattc_if, + esp_ble_gatt_create_conn); + Isconnecting = true; + + } + } + else if (strlen(remote_device_name[2]) == adv_name_len && strncmp((char *)adv_name, remote_device_name[2], adv_name_len) == 0) { + if (conn_device_c == false) { + conn_device_c = true; + ESP_LOGI(GATTC_TAG, "Searched device %s", remote_device_name[2]); + esp_ble_gap_stop_scanning(); + esp_ble_gatt_creat_conn_params_t esp_ble_gatt_create_conn; + memcpy(&esp_ble_gatt_create_conn.remote_bda, scan_result->scan_rst.bda, ESP_BD_ADDR_LEN); + esp_ble_gatt_create_conn.remote_addr_type = scan_result->scan_rst.ble_addr_type; + esp_ble_gatt_create_conn.own_addr_type = BLE_ADDR_TYPE_PUBLIC; + esp_ble_gatt_create_conn.is_direct = true; + esp_ble_gatt_create_conn.is_aux = false; + esp_ble_gattc_enh_open(gl_profile_tab[PROFILE_A_APP_ID].gattc_if, + esp_ble_gatt_create_conn); + Isconnecting = true; + } + break; + } + } break; + case ESP_GAP_SEARCH_INQ_CMPL_EVT: + break; + default: + break; } + break; + } ``` If the connection is successful the client saves the connection ID, prints the remote device information and configures the MTU size to 200 bytes. @@ -305,35 +376,29 @@ At this point the client has acquired all characteristics from the remote device break; ``` -* If the writing procedure is acknowledged then the remote device has connected successfully and communication is established without error. Immediately, the write procedure generates an ``ESP_GATTC_WRITE_CHAR_EVT`` event which in this example is used to print information and connect to the second remote device: +* If the writing procedure is acknowledged then the remote device has connected successfully and communication is established without error. Immediately, the write procedure generates an ``ESP_GATTC_WRITE_CHAR_EVT`` event which in this example is used to print information and start scan again to connect to the second remote device: ```c - case ESP_GATTC_WRITE_CHAR_EVT: + case ESP_GATTC_WRITE_CHAR_EVT: if (p_data->write.status != ESP_GATT_OK){ ESP_LOGE(GATTC_TAG, "write char failed, error status = %x", p_data->write.status); }else{ ESP_LOGI(GATTC_TAG, "write char success"); } - //connect the second device - if (find_device_2){ - esp_ble_gattc_open(gl_profile_tab[PROFILE_B_APP_ID].gattc_if, gl_profile_tab[PROFILE_B_APP_ID].remote_bda, true); - } + start_scan(); break; ``` * This triggers an open event which is handled by the Profile B event handler. This handler follows the same steps to search for services, get characteristics, register for notifications and write to the characteristic as the first device. The sequence for the second device also ends with an ``ESP_GATTC_WRITE_CHAR_EVT`` event which in turn is used to connect to the third device: ```c - case ESP_GATTC_WRITE_CHAR_EVT: + case ESP_GATTC_WRITE_CHAR_EVT: if (p_data->write.status != ESP_GATT_OK){ - ESP_LOGE(GATTC_TAG, "Write char failed, error status = %x", p_data->write.status); + ESP_LOGE(GATTC_TAG, "write char failed, error status = %x", p_data->write.status); }else{ - ESP_LOGI(GATTC_TAG, "Write char success"); - } - //connect the third device - if (find_device_3){ - esp_ble_gattc_open(gl_profile_tab[PROFILE_C_APP_ID].gattc_if, gl_profile_tab[PROFILE_C_APP_ID].remote_bda, true); + ESP_LOGI(GATTC_TAG, "write char success"); } + start_scan(); break; ``` diff --git a/examples/bluetooth/bluedroid/ble_50/ble50_security_client/main/ble50_sec_gattc_demo.c b/examples/bluetooth/bluedroid/ble_50/ble50_security_client/main/ble50_sec_gattc_demo.c index db69611da174..0c1d0f7be1b8 100644 --- a/examples/bluetooth/bluedroid/ble_50/ble50_security_client/main/ble50_sec_gattc_demo.c +++ b/examples/bluetooth/bluedroid/ble_50/ble50_security_client/main/ble50_sec_gattc_demo.c @@ -503,6 +503,13 @@ static void esp_gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *par esp_ble_gap_prefer_ext_connect_params_set(param->ext_adv_report.params.addr, ESP_BLE_GAP_PHY_1M_PREF_MASK | ESP_BLE_GAP_PHY_2M_PREF_MASK | ESP_BLE_GAP_PHY_CODED_PREF_MASK , &phy_1m_conn_params, &phy_2m_conn_params, &phy_coded_conn_params); + // create gattc virtual connection + esp_ble_gatt_creat_conn_params_t esp_ble_gatt_create_conn; + memcpy(&esp_ble_gatt_create_conn.remote_bda, param->ext_adv_report.params.addr, ESP_BD_ADDR_LEN); + esp_ble_gatt_create_conn.remote_addr_type = param->ext_adv_report.params.addr_type; + esp_ble_gatt_create_conn.own_addr_type = BLE_ADDR_TYPE_PUBLIC; + esp_ble_gatt_create_conn.is_direct = true; + esp_ble_gatt_create_conn.is_aux = true; esp_ble_gattc_aux_open(gl_profile_tab[PROFILE_A_APP_ID].gattc_if, param->ext_adv_report.params.addr, param->ext_adv_report.params.addr_type, true); diff --git a/examples/bluetooth/bluedroid/coex/gattc_gatts_coex/main/gattc_gatts_coex.c b/examples/bluetooth/bluedroid/coex/gattc_gatts_coex/main/gattc_gatts_coex.c index c31a01d6387e..cbcc6519ee51 100644 --- a/examples/bluetooth/bluedroid/coex/gattc_gatts_coex/main/gattc_gatts_coex.c +++ b/examples/bluetooth/bluedroid/coex/gattc_gatts_coex/main/gattc_gatts_coex.c @@ -307,10 +307,14 @@ static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param // Initiate GATT connection with the remote device, // If ble physical connection is set up, ESP_GATTS_CONNECT_EVT and ESP_GATTC_CONNECT_EVT event will come - esp_ble_gattc_open(gattc_profile_tab[GATTC_PROFILE_C_APP_ID].gattc_if, - scan_result->scan_rst.bda, - scan_result->scan_rst.ble_addr_type, - true); + esp_ble_gatt_creat_conn_params_t esp_ble_gatt_create_conn; + memcpy(&esp_ble_gatt_create_conn.remote_bda, scan_result->scan_rst.bda, ESP_BD_ADDR_LEN); + esp_ble_gatt_create_conn.remote_addr_type = scan_result->scan_rst.ble_addr_type; + esp_ble_gatt_create_conn.own_addr_type = BLE_ADDR_TYPE_PUBLIC; + esp_ble_gatt_create_conn.is_direct = true; + esp_ble_gatt_create_conn.is_aux = false; + esp_ble_gattc_enh_open(gattc_profile_tab[GATTC_PROFILE_C_APP_ID].gattc_if, + esp_ble_gatt_create_conn); // Update peer gatt server address memcpy(peer_gatts_addr, scan_result->scan_rst.bda, sizeof(esp_bd_addr_t));