From 605d2225de7729c3b7e98db3e52f992c8b155a9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Rymanowski?= Date: Fri, 31 May 2019 14:10:13 +0200 Subject: [PATCH] nimble/gap: Add helper ble_gap_terminate_with_conn() This is going to be used in following patch --- nimble/host/src/ble_gap.c | 49 +++++++++++++++++++--------------- nimble/host/src/ble_gap_priv.h | 1 + 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/nimble/host/src/ble_gap.c b/nimble/host/src/ble_gap.c index 45bc40186..70690f2c9 100644 --- a/nimble/host/src/ble_gap.c +++ b/nimble/host/src/ble_gap.c @@ -4610,11 +4610,37 @@ ble_gap_conn_active(void) /***************************************************************************** * $terminate connection procedure * *****************************************************************************/ +int +ble_gap_terminate_with_conn(struct ble_hs_conn *conn, uint8_t hci_reason) +{ + uint8_t buf[BLE_HCI_DISCONNECT_CMD_LEN]; + int rc; + + BLE_HS_DBG_ASSERT(ble_hs_locked_by_cur_task()); + if (conn->bhc_flags & BLE_HS_CONN_F_TERMINATING) { + return BLE_HS_EALREADY; + } + + BLE_HS_LOG(INFO, "GAP procedure initiated: terminate connection; " + "conn_handle=%d hci_reason=%d\n", + conn->bhc_handle, hci_reason); + + ble_hs_hci_cmd_build_disconnect(conn->bhc_handle, hci_reason, + buf, sizeof buf); + rc = ble_hs_hci_cmd_tx_empty_ack(BLE_HCI_OP(BLE_HCI_OGF_LINK_CTRL, + BLE_HCI_OCF_DISCONNECT_CMD), + buf, sizeof(buf)); + if (rc != 0) { + return rc; + } + + conn->bhc_flags |= BLE_HS_CONN_F_TERMINATING; + return 0; +} int ble_gap_terminate(uint16_t conn_handle, uint8_t hci_reason) { - uint8_t buf[BLE_HCI_DISCONNECT_CMD_LEN]; struct ble_hs_conn *conn; int rc; @@ -4628,26 +4654,7 @@ ble_gap_terminate(uint16_t conn_handle, uint8_t hci_reason) goto done; } - if (conn->bhc_flags & BLE_HS_CONN_F_TERMINATING) { - rc = BLE_HS_EALREADY; - goto done; - } - - BLE_HS_LOG(INFO, "GAP procedure initiated: terminate connection; " - "conn_handle=%d hci_reason=%d\n", - conn_handle, hci_reason); - - ble_hs_hci_cmd_build_disconnect(conn_handle, hci_reason, - buf, sizeof buf); - rc = ble_hs_hci_cmd_tx_empty_ack(BLE_HCI_OP(BLE_HCI_OGF_LINK_CTRL, - BLE_HCI_OCF_DISCONNECT_CMD), - buf, sizeof(buf)); - if (rc != 0) { - goto done; - } - - conn->bhc_flags |= BLE_HS_CONN_F_TERMINATING; - rc = 0; + rc = ble_gap_terminate_with_conn(conn, hci_reason); done: ble_hs_unlock(); diff --git a/nimble/host/src/ble_gap_priv.h b/nimble/host/src/ble_gap_priv.h index 48a1253fc..a2eff5398 100644 --- a/nimble/host/src/ble_gap_priv.h +++ b/nimble/host/src/ble_gap_priv.h @@ -118,6 +118,7 @@ int ble_gap_master_in_progress(void); void ble_gap_preempt(void); void ble_gap_preempt_done(void); +int ble_gap_terminate_with_conn(struct ble_hs_conn *conn, uint8_t hci_reason); void ble_gap_conn_broken(uint16_t conn_handle, int reason); int32_t ble_gap_timer(void);