From: Johan Hedberg <johan.hedberg@gmail.com>
To: linux-bluetooth@vger.kernel.org
Subject: [PATCH 07/13] Bluetooth: Introduce hci_req helper to abort a connection
Date: Wed, 21 Oct 2015 18:03:05 +0300 [thread overview]
Message-ID: <1445439791-2151-8-git-send-email-johan.hedberg@gmail.com> (raw)
In-Reply-To: <1445439791-2151-1-git-send-email-johan.hedberg@gmail.com>
From: Johan Hedberg <johan.hedberg@intel.com>
There are several different places needing to make sure that a
connection gets disconnected or canceled. The exact action needed
depends on the connection state, so centralizing this logic can save
quite a lot of code duplication.
Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
---
net/bluetooth/hci_request.c | 86 +++++++++++++++++++++++++++++++++++++++++++++
net/bluetooth/hci_request.h | 4 +++
2 files changed, 90 insertions(+)
diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index b7369220c9ef..b50e51900255 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -564,3 +564,89 @@ void hci_update_background_scan(struct hci_dev *hdev)
if (err && err != -ENODATA)
BT_ERR("Failed to run HCI request: err %d", err);
}
+
+void __hci_abort_conn(struct hci_request *req, struct hci_conn *conn,
+ u8 reason)
+{
+ switch (conn->state) {
+ case BT_CONNECTED:
+ case BT_CONFIG:
+ if (conn->type == AMP_LINK) {
+ struct hci_cp_disconn_phy_link cp;
+
+ cp.phy_handle = HCI_PHY_HANDLE(conn->handle);
+ cp.reason = hci_proto_disconn_ind(conn);
+ hci_req_add(req, HCI_OP_DISCONN_PHY_LINK, sizeof(cp),
+ &cp);
+ } else {
+ struct hci_cp_disconnect dc;
+
+ dc.handle = cpu_to_le16(conn->handle);
+ dc.reason = reason;
+ hci_req_add(req, HCI_OP_DISCONNECT, sizeof(dc), &dc);
+ }
+
+ conn->state = BT_DISCONN;
+
+ break;
+ case BT_CONNECT:
+ if (test_bit(HCI_CONN_SCANNING, &conn->flags))
+ break;
+ if (conn->type == LE_LINK)
+ hci_req_add(req, HCI_OP_LE_CREATE_CONN_CANCEL,
+ 0, NULL);
+ else if (conn->type == ACL_LINK)
+ hci_req_add(req, HCI_OP_CREATE_CONN_CANCEL,
+ 6, &conn->dst);
+ break;
+ case BT_CONNECT2:
+ if (conn->type == ACL_LINK) {
+ struct hci_cp_reject_conn_req rej;
+
+ if (req->hdev->hci_ver < BLUETOOTH_VER_1_2)
+ return;
+
+ bacpy(&rej.bdaddr, &conn->dst);
+ rej.reason = reason;
+
+ hci_req_add(req, HCI_OP_REJECT_CONN_REQ,
+ sizeof(rej), &rej);
+ } else if (conn->type == SCO_LINK || conn->type == ESCO_LINK) {
+ struct hci_cp_reject_sync_conn_req rej;
+
+ bacpy(&rej.bdaddr, &conn->dst);
+ rej.reason = reason;
+
+ hci_req_add(req, HCI_OP_REJECT_SYNC_CONN_REQ,
+ sizeof(rej), &rej);
+ }
+ break;
+ default:
+ conn->state = BT_CLOSED;
+ break;
+ }
+}
+
+static void abort_conn_complete(struct hci_dev *hdev, u8 status, u16 opcode)
+{
+ if (status)
+ BT_DBG("Failed to abort connection: status 0x%2.2x", status);
+}
+
+int hci_abort_conn(struct hci_conn *conn, u8 reason)
+{
+ struct hci_request req;
+ int err;
+
+ hci_req_init(&req, conn->hdev);
+
+ __hci_abort_conn(&req, conn, reason);
+
+ err = hci_req_run(&req, abort_conn_complete);
+ if (err && err != -ENODATA) {
+ BT_ERR("Failed to run HCI request: err %d", err);
+ return err;
+ }
+
+ return 0;
+}
diff --git a/net/bluetooth/hci_request.h b/net/bluetooth/hci_request.h
index bf6df92f42db..25c7f1305dcb 100644
--- a/net/bluetooth/hci_request.h
+++ b/net/bluetooth/hci_request.h
@@ -55,3 +55,7 @@ int hci_update_random_address(struct hci_request *req, bool require_privacy,
void hci_update_background_scan(struct hci_dev *hdev);
void __hci_update_background_scan(struct hci_request *req);
+
+int hci_abort_conn(struct hci_conn *conn, u8 reason);
+void __hci_abort_conn(struct hci_request *req, struct hci_conn *conn,
+ u8 reason);
--
2.5.0
next prev parent reply other threads:[~2015-10-21 15:03 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-10-21 15:02 [PATCH 00/13] Bluetooth: Cleanups & fixes from the UPF Johan Hedberg
2015-10-21 15:02 ` [PATCH 01/13] Bluetooth: Add le_addr_type() helper function Johan Hedberg
2015-10-21 15:03 ` [PATCH 02/13] Bluetooth: Add hci_conn_hash_lookup_le() " Johan Hedberg
2015-10-21 15:03 ` [PATCH 03/13] Bluetooth: Use hci_conn_hash_lookup_le() when possible Johan Hedberg
2015-10-21 15:03 ` [PATCH 04/13] Bluetooth: 6lowpan: " Johan Hedberg
2015-10-21 15:03 ` [PATCH 05/13] Bluetooth: Remove unnecessary indentation in unpair_device() Johan Hedberg
2015-10-21 15:03 ` [PATCH 06/13] Bluetooth: Disable auto-connection parameters when unpairing Johan Hedberg
2015-10-21 15:03 ` Johan Hedberg [this message]
2015-10-21 15:03 ` [PATCH 08/13] Bluetooth: Take advantage of connection abort helpers Johan Hedberg
2015-10-21 15:03 ` [PATCH 09/13] Bluetooth: Remove redundant (and possibly wrong) flag clearing Johan Hedberg
2015-10-21 15:03 ` [PATCH 10/13] Bluetooth: Make hci_disconnect() behave correctly for all states Johan Hedberg
2015-10-21 15:03 ` [PATCH 11/13] Bluetooth: Add hdev helper variable to hci_le_create_connection_cancel Johan Hedberg
2015-10-21 15:03 ` [PATCH 12/13] Bluetooth: Remove unnecessary hci_explicit_connect_lookup function Johan Hedberg
2015-10-21 15:03 ` [PATCH 13/13] Bluetooth: Fix crash in SMP when unpairing Johan Hedberg
2015-10-21 17:02 ` [PATCH 00/13] Bluetooth: Cleanups & fixes from the UPF Marcel Holtmann
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1445439791-2151-8-git-send-email-johan.hedberg@gmail.com \
--to=johan.hedberg@gmail.com \
--cc=linux-bluetooth@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox