From db96edc5d954f0dfd8bd6eac01f1ccb8417b1358 Mon Sep 17 00:00:00 2001 From: zhiweijian Date: Thu, 1 Jun 2017 15:56:36 +0800 Subject: [PATCH] component/bt: Add disconnect & connect func and event - Add disconnect func for gap - Add connect event and disconnect event for gattc --- components/bt/bluedroid/api/esp_gap_ble_api.c | 18 ++++ .../bluedroid/api/include/esp_gap_ble_api.h | 11 ++ .../bt/bluedroid/api/include/esp_gattc_api.h | 102 +++++++++++------- components/bt/bluedroid/bta/dm/bta_dm_act.c | 13 +++ components/bt/bluedroid/bta/dm/bta_dm_api.c | 23 ++++ components/bt/bluedroid/bta/dm/bta_dm_int.h | 8 ++ components/bt/bluedroid/bta/dm/bta_dm_main.c | 1 + .../bt/bluedroid/bta/gatt/bta_gattc_act.c | 39 +++++++ .../bt/bluedroid/bta/gatt/bta_gattc_main.c | 9 ++ .../bt/bluedroid/bta/gatt/bta_gattc_utils.c | 52 +++++++++ components/bt/bluedroid/bta/include/bta_api.h | 11 ++ .../bt/bluedroid/bta/include/bta_gatt_api.h | 18 ++++ .../bt/bluedroid/bta/include/bta_gattc_int.h | 7 +- .../btc/profile/std/gap/btc_gap_ble.c | 7 ++ .../btc/profile/std/gatt/btc_gattc.c | 19 ++++ .../btc/profile/std/include/btc_gap_ble.h | 7 ++ 16 files changed, 304 insertions(+), 41 deletions(-) diff --git a/components/bt/bluedroid/api/esp_gap_ble_api.c b/components/bt/bluedroid/api/esp_gap_ble_api.c index 5c9953916..ccce399ae 100644 --- a/components/bt/bluedroid/api/esp_gap_ble_api.c +++ b/components/bt/bluedroid/api/esp_gap_ble_api.c @@ -363,5 +363,23 @@ esp_err_t esp_ble_confirm_reply(esp_bd_addr_t bd_addr, bool accept) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } +esp_err_t esp_ble_gap_disconnect(esp_bd_addr_t remote_device) +{ + btc_msg_t msg; + btc_ble_gap_args_t arg; + + if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) { + return ESP_ERR_INVALID_STATE; + } + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_GAP_BLE; + msg.act = BTC_GAP_BLE_DISCONNECT_EVT; + memcpy(arg.disconnect.remote_device, remote_device, ESP_BD_ADDR_LEN); + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gap_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + + diff --git a/components/bt/bluedroid/api/include/esp_gap_ble_api.h b/components/bt/bluedroid/api/include/esp_gap_ble_api.h index c0d220919..3e2ab9743 100644 --- a/components/bt/bluedroid/api/include/esp_gap_ble_api.h +++ b/components/bt/bluedroid/api/include/esp_gap_ble_api.h @@ -783,6 +783,17 @@ esp_err_t esp_ble_passkey_reply(esp_bd_addr_t bd_addr, bool accept, uint32_t pas */ esp_err_t esp_ble_confirm_reply(esp_bd_addr_t bd_addr, bool accept); +/** +* @brief This function is to disconnect the physical connection of the peer device +* +* @param[in] remote_device : BD address of the peer device +* +* @return - ESP_OK : success +* - other : failed +* +*/ +esp_err_t esp_ble_gap_disconnect(esp_bd_addr_t remote_device); + #ifdef __cplusplus } #endif diff --git a/components/bt/bluedroid/api/include/esp_gattc_api.h b/components/bt/bluedroid/api/include/esp_gattc_api.h index c2cae797b..c2f948f61 100644 --- a/components/bt/bluedroid/api/include/esp_gattc_api.h +++ b/components/bt/bluedroid/api/include/esp_gattc_api.h @@ -25,45 +25,47 @@ extern "C" { /// GATT Client callback function events typedef enum { - ESP_GATTC_REG_EVT = 0, /*!< When GATT client is registered, the event comes */ - ESP_GATTC_UNREG_EVT = 1, /*!< When GATT client is unregistered, the event comes */ - ESP_GATTC_OPEN_EVT = 2, /*!< When GATT connection is set up, the event comes */ - ESP_GATTC_READ_CHAR_EVT = 3, /*!< When GATT characteristic is read, the event comes */ - ESP_GATTC_WRITE_CHAR_EVT = 4, /*!< When GATT characteristic write operation completes, the event comes */ - ESP_GATTC_CLOSE_EVT = 5, /*!< When GATT connection is closed, the event comes */ - ESP_GATTC_SEARCH_CMPL_EVT = 6, /*!< When GATT service discovery is completed, the event comes */ - ESP_GATTC_SEARCH_RES_EVT = 7, /*!< When GATT service discovery result is got, the event comes */ - ESP_GATTC_READ_DESCR_EVT = 8, /*!< When GATT characteristic descriptor read completes, the event comes */ - ESP_GATTC_WRITE_DESCR_EVT = 9, /*!< When GATT characteristic descriptor write completes, the event comes */ - ESP_GATTC_NOTIFY_EVT = 10, /*!< When GATT notification or indication arrives, the event comes */ - ESP_GATTC_PREP_WRITE_EVT = 11, /*!< When GATT prepare-write operation completes, the event comes */ - ESP_GATTC_EXEC_EVT = 12, /*!< When write execution completes, the event comes */ - ESP_GATTC_ACL_EVT = 13, /*!< When ACL connection is up, the event comes */ - ESP_GATTC_CANCEL_OPEN_EVT = 14, /*!< When GATT client ongoing connection is cancelled, the event comes */ - ESP_GATTC_SRVC_CHG_EVT = 15, /*!< When "service changed" occurs, the event comes */ - ESP_GATTC_ENC_CMPL_CB_EVT = 17, /*!< When encryption procedure completes, the event comes */ - ESP_GATTC_CFG_MTU_EVT = 18, /*!< When configuration of MTU completes, the event comes */ - ESP_GATTC_ADV_DATA_EVT = 19, /*!< When advertising of data, the event comes */ - ESP_GATTC_MULT_ADV_ENB_EVT = 20, /*!< When multi-advertising is enabled, the event comes */ - ESP_GATTC_MULT_ADV_UPD_EVT = 21, /*!< When multi-advertising parameters are updated, the event comes */ - ESP_GATTC_MULT_ADV_DATA_EVT = 22, /*!< When multi-advertising data arrives, the event comes */ - ESP_GATTC_MULT_ADV_DIS_EVT = 23, /*!< When multi-advertising is disabled, the event comes */ - ESP_GATTC_CONGEST_EVT = 24, /*!< When GATT connection congestion comes, the event comes */ - ESP_GATTC_BTH_SCAN_ENB_EVT = 25, /*!< When batch scan is enabled, the event comes */ - ESP_GATTC_BTH_SCAN_CFG_EVT = 26, /*!< When batch scan storage is configured, the event comes */ - ESP_GATTC_BTH_SCAN_RD_EVT = 27, /*!< When Batch scan read event is reported, the event comes */ - ESP_GATTC_BTH_SCAN_THR_EVT = 28, /*!< When Batch scan threshold is set, the event comes */ - ESP_GATTC_BTH_SCAN_PARAM_EVT = 29, /*!< When Batch scan parameters are set, the event comes */ - ESP_GATTC_BTH_SCAN_DIS_EVT = 30, /*!< When Batch scan is disabled, the event comes */ - ESP_GATTC_SCAN_FLT_CFG_EVT = 31, /*!< When Scan filter configuration completes, the event comes */ - ESP_GATTC_SCAN_FLT_PARAM_EVT = 32, /*!< When Scan filter parameters are set, the event comes */ - ESP_GATTC_SCAN_FLT_STATUS_EVT = 33, /*!< When Scan filter status is reported, the event comes */ - ESP_GATTC_ADV_VSC_EVT = 34, /*!< When advertising vendor spec content event is reported, the event comes */ - ESP_GATTC_GET_CHAR_EVT = 35, /*!< When characteristic is got from GATT server, the event comes */ - ESP_GATTC_GET_DESCR_EVT = 36, /*!< When characteristic descriptor is got from GATT server, the event comes */ - ESP_GATTC_GET_INCL_SRVC_EVT = 37, /*!< When included service is got from GATT server, the event comes */ - ESP_GATTC_REG_FOR_NOTIFY_EVT = 38, /*!< When register for notification of a service completes, the event comes */ - ESP_GATTC_UNREG_FOR_NOTIFY_EVT = 39, /*!< When unregister for notification of a service completes, the event comes */ + ESP_GATTC_REG_EVT = 0, /*!< When GATT client is registered, the event comes */ + ESP_GATTC_UNREG_EVT = 1, /*!< When GATT client is unregistered, the event comes */ + ESP_GATTC_OPEN_EVT = 2, /*!< When GATT virtual connection is set up, the event comes */ + ESP_GATTC_READ_CHAR_EVT = 3, /*!< When GATT characteristic is read, the event comes */ + ESP_GATTC_WRITE_CHAR_EVT = 4, /*!< When GATT characteristic write operation completes, the event comes */ + ESP_GATTC_CLOSE_EVT = 5, /*!< When GATT virtual connection is closed, the event comes */ + ESP_GATTC_SEARCH_CMPL_EVT = 6, /*!< When GATT service discovery is completed, the event comes */ + ESP_GATTC_SEARCH_RES_EVT = 7, /*!< When GATT service discovery result is got, the event comes */ + ESP_GATTC_READ_DESCR_EVT = 8, /*!< When GATT characteristic descriptor read completes, the event comes */ + ESP_GATTC_WRITE_DESCR_EVT = 9, /*!< When GATT characteristic descriptor write completes, the event comes */ + ESP_GATTC_NOTIFY_EVT = 10, /*!< When GATT notification or indication arrives, the event comes */ + ESP_GATTC_PREP_WRITE_EVT = 11, /*!< When GATT prepare-write operation completes, the event comes */ + ESP_GATTC_EXEC_EVT = 12, /*!< When write execution completes, the event comes */ + ESP_GATTC_ACL_EVT = 13, /*!< When ACL connection is up, the event comes */ + ESP_GATTC_CANCEL_OPEN_EVT = 14, /*!< When GATT client ongoing connection is cancelled, the event comes */ + ESP_GATTC_SRVC_CHG_EVT = 15, /*!< When "service changed" occurs, the event comes */ + ESP_GATTC_ENC_CMPL_CB_EVT = 17, /*!< When encryption procedure completes, the event comes */ + ESP_GATTC_CFG_MTU_EVT = 18, /*!< When configuration of MTU completes, the event comes */ + ESP_GATTC_ADV_DATA_EVT = 19, /*!< When advertising of data, the event comes */ + ESP_GATTC_MULT_ADV_ENB_EVT = 20, /*!< When multi-advertising is enabled, the event comes */ + ESP_GATTC_MULT_ADV_UPD_EVT = 21, /*!< When multi-advertising parameters are updated, the event comes */ + ESP_GATTC_MULT_ADV_DATA_EVT = 22, /*!< When multi-advertising data arrives, the event comes */ + ESP_GATTC_MULT_ADV_DIS_EVT = 23, /*!< When multi-advertising is disabled, the event comes */ + ESP_GATTC_CONGEST_EVT = 24, /*!< When GATT connection congestion comes, the event comes */ + ESP_GATTC_BTH_SCAN_ENB_EVT = 25, /*!< When batch scan is enabled, the event comes */ + ESP_GATTC_BTH_SCAN_CFG_EVT = 26, /*!< When batch scan storage is configured, the event comes */ + ESP_GATTC_BTH_SCAN_RD_EVT = 27, /*!< When Batch scan read event is reported, the event comes */ + ESP_GATTC_BTH_SCAN_THR_EVT = 28, /*!< When Batch scan threshold is set, the event comes */ + ESP_GATTC_BTH_SCAN_PARAM_EVT = 29, /*!< When Batch scan parameters are set, the event comes */ + ESP_GATTC_BTH_SCAN_DIS_EVT = 30, /*!< When Batch scan is disabled, the event comes */ + ESP_GATTC_SCAN_FLT_CFG_EVT = 31, /*!< When Scan filter configuration completes, the event comes */ + ESP_GATTC_SCAN_FLT_PARAM_EVT = 32, /*!< When Scan filter parameters are set, the event comes */ + ESP_GATTC_SCAN_FLT_STATUS_EVT = 33, /*!< When Scan filter status is reported, the event comes */ + ESP_GATTC_ADV_VSC_EVT = 34, /*!< When advertising vendor spec content event is reported, the event comes */ + ESP_GATTC_GET_CHAR_EVT = 35, /*!< When characteristic is got from GATT server, the event comes */ + ESP_GATTC_GET_DESCR_EVT = 36, /*!< When characteristic descriptor is got from GATT server, the event comes */ + ESP_GATTC_GET_INCL_SRVC_EVT = 37, /*!< When included service is got from GATT server, the event comes */ + ESP_GATTC_REG_FOR_NOTIFY_EVT = 38, /*!< When register for notification of a service completes, the event comes */ + ESP_GATTC_UNREG_FOR_NOTIFY_EVT = 39, /*!< When unregister for notification of a service completes, the event comes */ + ESP_GATTC_CONNECT_EVT = 40, /*!< When the ble physical connection is set up, the event comes */ + ESP_GATTC_DISCONNECT_EVT = 41, /*!< When the ble physical connection disconnected, the event comes */ } esp_gattc_cb_event_t; /// Maximum Transmission Unit used in GATT @@ -241,6 +243,23 @@ typedef union { esp_gatt_id_t char_id; /*!< Characteristic id, include characteristic uuid and other information */ } unreg_for_notify; /*!< Gatt client callback param of ESP_GATTC_UNREG_FOR_NOTIFY_EVT */ + /** + * @brief ESP_GATTC_CONNECT_EVT + */ + struct gattc_connect_evt_param { + esp_gatt_status_t status; /*!< Operation status */ + uint16_t conn_id; /*!< Connection id */ + esp_bd_addr_t remote_bda; /*!< Remote bluetooth device address */ + } connect; /*!< Gatt client callback param of ESP_GATTC_CONNECT_EVT */ + + /** + * @brief ESP_GATTC_DISCONNECT_EVT + */ + struct gattc_disconnect_evt_param { + esp_gatt_status_t status; /*!< Operation status */ + uint16_t conn_id; /*!< Connection id */ + esp_bd_addr_t remote_bda; /*!< Remote bluetooth device address */ + } disconnect; /*!< Gatt client callback param of ESP_GATTC_DISCONNECT_EVT */ } esp_ble_gattc_cb_param_t; /*!< GATT client callback parameter union type */ @@ -311,7 +330,10 @@ esp_err_t esp_ble_gattc_open(esp_gatt_if_t gattc_if, esp_bd_addr_t remote_bda, b /** - * @brief Close a connection to a GATT server. + * @brief Close a virtual connection to a GATT server. gattc maybe have multiple virtual GATT server connections when multiple app_id registed, + * this API only close one virtual GATT server connection. if there exist other virtual GATT server connections, + * it does not disconnect the physical connection. + * if you want to disconnect the physical connection directly, you can use esp_ble_gap_disconnect(esp_bd_addr_t remote_device). * * @param[in] gattc_if: Gatt client access interface. * @param[in] conn_id: connection ID to be closed. diff --git a/components/bt/bluedroid/bta/dm/bta_dm_act.c b/components/bt/bluedroid/bta/dm/bta_dm_act.c index a3e5cf3d4..9e3720996 100644 --- a/components/bt/bluedroid/bta/dm/bta_dm_act.c +++ b/components/bt/bluedroid/bta/dm/bta_dm_act.c @@ -4522,6 +4522,19 @@ void bta_dm_ble_update_conn_params (tBTA_DM_MSG *p_data) APPL_TRACE_ERROR("Update connection parameters failed!"); } } +/******************************************************************************* +** +** Function bta_dm_ble_disconnect +** +** Description This function disconnect the ble connection. +** +** Parameters: +** +*******************************************************************************/ +void bta_dm_ble_disconnect (tBTA_DM_MSG *p_data) +{ + L2CA_RemoveFixedChnl(L2CAP_ATT_CID, p_data->ble_disconnect.remote_bda); +} /******************************************************************************* ** diff --git a/components/bt/bluedroid/bta/dm/bta_dm_api.c b/components/bt/bluedroid/bta/dm/bta_dm_api.c index 0ae3af50a..1e5494353 100644 --- a/components/bt/bluedroid/bta/dm/bta_dm_api.c +++ b/components/bt/bluedroid/bta/dm/bta_dm_api.c @@ -2008,7 +2008,30 @@ void BTA_DmBleUpdateConnectionParams(BD_ADDR bd_addr, UINT16 min_int, UINT16 max bta_sys_sendmsg(p_msg); } } +/******************************************************************************* +** +** Function BTA_DmBleDisconnect +** +** Description Disconnect the ble connection, can only be used when connection is up. +** +** Parameters: bd_addr - BD address of the peer +** +** Returns void +** +*******************************************************************************/ +void BTA_DmBleDisconnect(BD_ADDR bd_addr) +{ + tBTA_DM_API_BLE_DISCONNECT *p_msg; + if ((p_msg = (tBTA_DM_API_BLE_DISCONNECT *) GKI_getbuf(sizeof(tBTA_DM_API_BLE_DISCONNECT))) != NULL) { + memset (p_msg, 0, sizeof(tBTA_DM_API_BLE_DISCONNECT)); + + p_msg->hdr.event = BTA_DM_API_BLE_DISCONNECT_EVT; + bdcpy(p_msg->remote_bda, bd_addr); + + bta_sys_sendmsg(p_msg); + } +} /******************************************************************************* ** ** Function BTA_DmBleSetDataLength diff --git a/components/bt/bluedroid/bta/dm/bta_dm_int.h b/components/bt/bluedroid/bta/dm/bta_dm_int.h index adb5fc033..fc7ca425f 100644 --- a/components/bt/bluedroid/bta/dm/bta_dm_int.h +++ b/components/bt/bluedroid/bta/dm/bta_dm_int.h @@ -136,6 +136,7 @@ enum { BTA_DM_API_BLE_READ_SCAN_REPORTS_EVT, BTA_DM_API_BLE_TRACK_ADVERTISER_EVT, BTA_DM_API_BLE_ENERGY_INFO_EVT, + BTA_DM_API_BLE_DISCONNECT_EVT, #endif @@ -613,6 +614,11 @@ typedef struct { tBTA_BLE_ENERGY_INFO_CBACK *p_energy_info_cback; } tBTA_DM_API_ENERGY_INFO; +typedef struct { + BT_HDR hdr; + BD_ADDR remote_bda; +} tBTA_DM_API_BLE_DISCONNECT; + #endif /* BLE_INCLUDED */ /* data type for BTA_DM_API_REMOVE_ACL_EVT */ @@ -753,6 +759,7 @@ typedef union { tBTA_DM_API_DISABLE_SCAN ble_disable_scan; tBTA_DM_API_TRACK_ADVERTISER ble_track_advert; tBTA_DM_API_ENERGY_INFO ble_energy_info; + tBTA_DM_API_BLE_DISCONNECT ble_disconnect; #endif tBTA_DM_API_REMOVE_ACL remove_acl; @@ -1123,6 +1130,7 @@ extern void bta_dm_ble_set_conn_scan_params (tBTA_DM_MSG *p_data); extern void bta_dm_close_gatt_conn(tBTA_DM_MSG *p_data); extern void bta_dm_ble_observe (tBTA_DM_MSG *p_data); extern void bta_dm_ble_update_conn_params (tBTA_DM_MSG *p_data); +extern void bta_dm_ble_disconnect (tBTA_DM_MSG *p_data); extern void bta_dm_ble_set_rand_address(tBTA_DM_MSG *p_data); extern void bta_dm_ble_stop_advertising(tBTA_DM_MSG *p_data); extern void bta_dm_ble_config_local_privacy (tBTA_DM_MSG *p_data); diff --git a/components/bt/bluedroid/bta/dm/bta_dm_main.c b/components/bt/bluedroid/bta/dm/bta_dm_main.c index d7ebed191..e872e848e 100644 --- a/components/bt/bluedroid/bta/dm/bta_dm_main.c +++ b/components/bt/bluedroid/bta/dm/bta_dm_main.c @@ -131,6 +131,7 @@ const tBTA_DM_ACTION bta_dm_action[BTA_DM_MAX_EVT] = { bta_dm_ble_read_scan_reports, /* BTA_DM_API_BLE_READ_SCAN_REPORTS_EVT */ bta_dm_ble_track_advertiser, /* BTA_DM_API_BLE_TRACK_ADVERTISER_EVT */ bta_dm_ble_get_energy_info, /* BTA_DM_API_BLE_ENERGY_INFO_EVT */ + bta_dm_ble_disconnect, /* BTA_DM_API_BLE_DISCONNECT_EVT */ #endif bta_dm_enable_test_mode, /* BTA_DM_API_ENABLE_TEST_MODE_EVT */ diff --git a/components/bt/bluedroid/bta/gatt/bta_gattc_act.c b/components/bt/bluedroid/bta/gatt/bta_gattc_act.c index dbf490f8c..85ccb0808 100644 --- a/components/bt/bluedroid/bta/gatt/bta_gattc_act.c +++ b/components/bt/bluedroid/bta/gatt/bta_gattc_act.c @@ -681,6 +681,45 @@ void bta_gattc_conn(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data) p_clcb->bta_conn_id, p_clcb->transport, p_clcb->p_srcb->mtu); + + } +} +/******************************************************************************* +** +** Function bta_gattc_conncback +** +** Description receive connection callback from stack +** +** Returns void +** +*******************************************************************************/ +void bta_gattc_conncback(tBTA_GATTC_RCB *p_rcb, tBTA_GATTC_DATA *p_data) +{ + if (p_rcb) { + bta_gattc_send_connect_cback(p_rcb, + BTA_GATT_OK, + p_data->int_conn.remote_bda, + p_data->int_conn.hdr.layer_specific); + + } +} +/******************************************************************************* +** +** Function bta_gattc_disconncback +** +** Description receive disconnection callback from stack +** +** Returns void +** +*******************************************************************************/ +void bta_gattc_disconncback(tBTA_GATTC_RCB *p_rcb, tBTA_GATTC_DATA *p_data) +{ + if (p_rcb) { + bta_gattc_send_disconnect_cback(p_rcb, + BTA_GATT_OK, + p_data->int_conn.remote_bda, + p_data->int_conn.hdr.layer_specific); + } } /******************************************************************************* diff --git a/components/bt/bluedroid/bta/gatt/bta_gattc_main.c b/components/bt/bluedroid/bta/gatt/bta_gattc_main.c index 057e35813..5045ff147 100644 --- a/components/bt/bluedroid/bta/gatt/bta_gattc_main.c +++ b/components/bt/bluedroid/bta/gatt/bta_gattc_main.c @@ -400,7 +400,16 @@ BOOLEAN bta_gattc_hdl_event(BT_HDR *p_msg) default: if (p_msg->event == BTA_GATTC_INT_CONN_EVT) { p_clcb = bta_gattc_find_int_conn_clcb((tBTA_GATTC_DATA *) p_msg); + p_clreg = bta_gattc_cl_get_regcb(((tBTA_GATTC_DATA *)p_msg)->int_conn.client_if); + if (p_clreg != NULL){ + bta_gattc_conncback(p_clreg, (tBTA_GATTC_DATA *) p_msg); + } + } else if (p_msg->event == BTA_GATTC_INT_DISCONN_EVT) { + p_clreg = bta_gattc_cl_get_regcb(((tBTA_GATTC_DATA *)p_msg)->int_conn.client_if); + if (p_clreg != NULL){ + bta_gattc_disconncback(p_clreg, (tBTA_GATTC_DATA *) p_msg); + } p_clcb = bta_gattc_find_int_disconn_clcb((tBTA_GATTC_DATA *) p_msg); } else { p_clcb = bta_gattc_find_clcb_by_conn_id(p_msg->layer_specific); diff --git a/components/bt/bluedroid/bta/gatt/bta_gattc_utils.c b/components/bt/bluedroid/bta/gatt/bta_gattc_utils.c index a9d512b73..0a878e48b 100644 --- a/components/bt/bluedroid/bta/gatt/bta_gattc_utils.c +++ b/components/bt/bluedroid/bta/gatt/bta_gattc_utils.c @@ -770,6 +770,58 @@ void bta_gattc_send_open_cback( tBTA_GATTC_RCB *p_clreg, tBTA_GATT_STATUS status (*p_clreg->p_cback)(BTA_GATTC_OPEN_EVT, &cb_data); } } + +/******************************************************************************* +** +** Function bta_gattc_send_connect_cback +** +** Description send connect callback +** +** Returns +** +*******************************************************************************/ +void bta_gattc_send_connect_cback( tBTA_GATTC_RCB *p_clreg, tBTA_GATT_STATUS status, + BD_ADDR remote_bda, UINT16 conn_id) +{ + tBTA_GATTC cb_data; + + if (p_clreg->p_cback) { + memset(&cb_data, 0, sizeof(tBTA_GATTC)); + + cb_data.connect.status = status; + cb_data.connect.client_if = p_clreg->client_if; + cb_data.connect.conn_id = conn_id; + bdcpy(cb_data.connect.remote_bda, remote_bda); + + (*p_clreg->p_cback)(BTA_GATTC_CONNECT_EVT, &cb_data); + } +} + +/******************************************************************************* +** +** Function bta_gattc_send_disconnect_cback +** +** Description send disconnect callback +** +** Returns +** +*******************************************************************************/ +void bta_gattc_send_disconnect_cback( tBTA_GATTC_RCB *p_clreg, tBTA_GATT_STATUS status, + BD_ADDR remote_bda, UINT16 conn_id) +{ + tBTA_GATTC cb_data; + + if (p_clreg->p_cback) { + memset(&cb_data, 0, sizeof(tBTA_GATTC)); + + cb_data.disconnect.status = status; + cb_data.disconnect.client_if = p_clreg->client_if; + cb_data.disconnect.conn_id = conn_id; + bdcpy(cb_data.disconnect.remote_bda, remote_bda); + + (*p_clreg->p_cback)(BTA_GATTC_DISCONNECT_EVT, &cb_data); + } +} /******************************************************************************* ** ** Function bta_gattc_conn_alloc diff --git a/components/bt/bluedroid/bta/include/bta_api.h b/components/bt/bluedroid/bta/include/bta_api.h index 0c6baa92d..cb3bca8a3 100644 --- a/components/bt/bluedroid/bta/include/bta_api.h +++ b/components/bt/bluedroid/bta/include/bta_api.h @@ -2204,6 +2204,17 @@ extern void BTA_BleDisableAdvInstance(UINT8 inst_id); extern void BTA_DmBleUpdateConnectionParams(BD_ADDR bd_addr, UINT16 min_int, UINT16 max_int, UINT16 latency, UINT16 timeout); +/******************************************************************************* +** +** Function BTA_DmBleDisconnect +** +** Description This function is to disconnect the ble connection +** +** Returns void +** +*******************************************************************************/ +extern void BTA_DmBleDisconnect(BD_ADDR bd_addr); + /******************************************************************************* ** ** Function BTA_DmBleSetDataLength diff --git a/components/bt/bluedroid/bta/include/bta_gatt_api.h b/components/bt/bluedroid/bta/include/bta_gatt_api.h index 8e8459516..1b10aaa78 100644 --- a/components/bt/bluedroid/bta/include/bta_gatt_api.h +++ b/components/bt/bluedroid/bta/include/bta_gatt_api.h @@ -150,6 +150,8 @@ typedef UINT8 tBTA_GATT_STATUS; #define BTA_GATTC_SCAN_FLT_PARAM_EVT 32 /* Param filter event */ #define BTA_GATTC_SCAN_FLT_STATUS_EVT 33 /* Filter status event */ #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 */ typedef UINT8 tBTA_GATTC_EVT; @@ -379,6 +381,20 @@ typedef struct { BD_ADDR remote_bda; } tBTA_GATTC_ENC_CMPL_CB; +typedef struct { + tBTA_GATT_STATUS status; + UINT16 conn_id; + tBTA_GATTC_IF client_if; + BD_ADDR remote_bda; +} tBTA_GATTC_CONNECT; + +typedef struct { + tBTA_GATT_STATUS status; + UINT16 conn_id; + tBTA_GATTC_IF client_if; + BD_ADDR remote_bda; +} tBTA_GATTC_DISCONNECT; + typedef union { tBTA_GATT_STATUS status; @@ -386,7 +402,9 @@ typedef union { tBTA_GATTC_SRVC_RES srvc_res; /* discovery result */ tBTA_GATTC_REG reg_oper; /* registration data */ tBTA_GATTC_OPEN open; + tBTA_GATTC_CONNECT connect; tBTA_GATTC_CLOSE close; + tBTA_GATTC_DISCONNECT disconnect; tBTA_GATTC_READ read; /* read attribute/descriptor data */ tBTA_GATTC_WRITE write; /* write complete data */ tBTA_GATTC_EXEC_CMPL exec_cmpl; /* execute complete */ diff --git a/components/bt/bluedroid/bta/include/bta_gattc_int.h b/components/bt/bluedroid/bta/include/bta_gattc_int.h index d04097b71..20a23e48e 100644 --- a/components/bt/bluedroid/bta/include/bta_gattc_int.h +++ b/components/bt/bluedroid/bta/include/bta_gattc_int.h @@ -441,7 +441,8 @@ extern void bta_gattc_cancel_open_ok(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p extern void bta_gattc_cancel_open_error(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data); extern void bta_gattc_conn(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data); - +extern void bta_gattc_conncback(tBTA_GATTC_RCB *p_rcb, tBTA_GATTC_DATA *p_data); +extern void bta_gattc_disconncback(tBTA_GATTC_RCB *p_rcb, tBTA_GATTC_DATA *p_data); extern void bta_gattc_close(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data); extern void bta_gattc_close_fail(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data); extern void bta_gattc_disc_close(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data); @@ -468,6 +469,10 @@ extern void bta_gattc_init_bk_conn(tBTA_GATTC_API_OPEN *p_data, tBTA_GATTC_RCB * extern void bta_gattc_cancel_bk_conn(tBTA_GATTC_API_CANCEL_OPEN *p_data); extern void bta_gattc_send_open_cback( tBTA_GATTC_RCB *p_clreg, tBTA_GATT_STATUS status, BD_ADDR remote_bda, UINT16 conn_id, tBTA_TRANSPORT transport, UINT16 mtu); +extern void bta_gattc_send_connect_cback( tBTA_GATTC_RCB *p_clreg, tBTA_GATT_STATUS status, + BD_ADDR remote_bda, UINT16 conn_id); +extern void bta_gattc_send_disconnect_cback( tBTA_GATTC_RCB *p_clreg, tBTA_GATT_STATUS status, + BD_ADDR remote_bda, UINT16 conn_id); extern void bta_gattc_process_api_refresh(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_msg); extern void bta_gattc_cfg_mtu(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data); #if BLE_INCLUDED == TRUE diff --git a/components/bt/bluedroid/btc/profile/std/gap/btc_gap_ble.c b/components/bt/bluedroid/btc/profile/std/gap/btc_gap_ble.c index 027e8d4ac..c829184fc 100644 --- a/components/bt/bluedroid/btc/profile/std/gap/btc_gap_ble.c +++ b/components/bt/bluedroid/btc/profile/std/gap/btc_gap_ble.c @@ -668,6 +668,10 @@ static void btc_ble_config_local_privacy(bool privacy_enable) BTA_DmBleConfigLocalPrivacy(privacy_enable); } +static void btc_ble_disconnect(BD_ADDR bd_addr) +{ + BTA_DmBleDisconnect(bd_addr); +} void btc_gap_ble_cb_handler(btc_msg_t *msg) { @@ -974,6 +978,9 @@ void btc_gap_ble_call_handler(btc_msg_t *msg) break; } #endif ///SMP_INCLUDED == TRUE + case BTC_GAP_BLE_DISCONNECT_EVT: + btc_ble_disconnect(arg->disconnect.remote_device); + break; default: break; } diff --git a/components/bt/bluedroid/btc/profile/std/gatt/btc_gattc.c b/components/bt/bluedroid/btc/profile/std/gatt/btc_gattc.c index eaeb904a4..bc8f665a8 100644 --- a/components/bt/bluedroid/btc/profile/std/gatt/btc_gattc.c +++ b/components/bt/bluedroid/btc/profile/std/gatt/btc_gattc.c @@ -684,6 +684,16 @@ void btc_gattc_cb_handler(btc_msg_t *msg) btc_gattc_cb_to_app(ESP_GATTC_OPEN_EVT, gattc_if, ¶m); break; } + case BTA_GATTC_CONNECT_EVT: { + tBTA_GATTC_CONNECT *connect = &arg->connect; + + gattc_if = connect->client_if; + param.connect.status = connect->status; + param.connect.conn_id = BTC_GATT_GET_CONN_ID(connect->conn_id); + memcpy(param.connect.remote_bda, connect->remote_bda, sizeof(esp_bd_addr_t)); + btc_gattc_cb_to_app(ESP_GATTC_CONNECT_EVT, gattc_if, ¶m); + break; + } case BTA_GATTC_CLOSE_EVT: { tBTA_GATTC_CLOSE *close = &arg->close; @@ -695,7 +705,16 @@ void btc_gattc_cb_handler(btc_msg_t *msg) btc_gattc_cb_to_app(ESP_GATTC_CLOSE_EVT, gattc_if, ¶m); break; } + case BTA_GATTC_DISCONNECT_EVT: { + tBTA_GATTC_DISCONNECT *disconnect = &arg->disconnect; + gattc_if = disconnect->client_if; + param.disconnect.status = disconnect->status; + param.disconnect.conn_id = BTC_GATT_GET_CONN_ID(disconnect->conn_id); + memcpy(param.disconnect.remote_bda, disconnect->remote_bda, sizeof(esp_bd_addr_t)); + btc_gattc_cb_to_app(ESP_GATTC_DISCONNECT_EVT, gattc_if, ¶m); + break; + } case BTA_GATTC_CFG_MTU_EVT: { tBTA_GATTC_CFG_MTU *cfg_mtu = &arg->cfg_mtu; diff --git a/components/bt/bluedroid/btc/profile/std/include/btc_gap_ble.h b/components/bt/bluedroid/btc/profile/std/include/btc_gap_ble.h index b26c2b800..78c2db022 100644 --- a/components/bt/bluedroid/btc/profile/std/include/btc_gap_ble.h +++ b/components/bt/bluedroid/btc/profile/std/include/btc_gap_ble.h @@ -39,6 +39,7 @@ typedef enum { BTC_GAP_BLE_SECURITY_RSP_EVT, BTC_GAP_BLE_PASSKEY_REPLY_EVT, BTC_GAP_BLE_CONFIRM_REPLY_EVT, + BTC_GAP_BLE_DISCONNECT_EVT, } btc_gap_ble_act_t; /* btc_ble_gap_args_t */ @@ -115,6 +116,12 @@ typedef union { esp_bd_addr_t bd_addr; bool accept; } enc_comfirm_replay; + + //BTC_GAP_BLE_DISCONNECT_EVT + struct disconnect_args { + esp_bd_addr_t remote_device; + } disconnect; + } btc_ble_gap_args_t; void btc_gap_ble_call_handler(btc_msg_t *msg);