Skip to contentSkip to conversation

Bluetooth: LE connection fixes & cleanups

PATCH In review Johan Hedberg 2 participants 5 messages Nov 11, 2015

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover Bluetooth: LE connection fixes & cleanups

Hi,

This set of patches simplifies the LE connection logic and fixes outgoing connect attempts through directed advertising.

Johan

----------------------------------------------------------------
Johan Hedberg (3):
Bluetooth: Fix specifying role for LE connections
Bluetooth: Move check for ongoing connect earlier in hci_connect_le()
Bluetooth: Remove conn_unfinished variable from hci_connect_le()

 include/net/bluetooth/hci_core.h |  2 +-
 net/bluetooth/hci_conn.c         | 60 ++++++++++++---------------------------
 net/bluetooth/l2cap_core.c       | 15 +++++-----
 net/bluetooth/mgmt.c             |  3 +-
 4 files changed, 27 insertions(+), 53 deletions(-)
#1 lore raw

PATCH 1/3 Bluetooth: Fix specifying role for LE connections

From: Johan Hedberg <[email protected]>

The hci_connect_le_scan() is (as the name implies) a master/central role API, so it makes no sense in passing a role parameter to it. At the same time this patch also fixes the direct advertising support for LE L2CAP sockets where we now call the more appropriate hci_le_connect() API if slave/peripheral role is desired.

---
 include/net/bluetooth/hci_core.h |  2 +-
 net/bluetooth/hci_conn.c         |  4 ++--
 net/bluetooth/l2cap_core.c       | 15 +++++++--------
 net/bluetooth/mgmt.c             |  3 +--
 4 files changed, 11 insertions(+), 13 deletions(-)
include/net/bluetooth/hci_core.h +1 -1
index 609f4a03899c..55ce209157b1 100644
@@ -877,7 +877,7 @@ struct hci_chan *hci_chan_lookup_handle(struct hci_dev *hdev, __u16 handle);
 
 struct hci_conn *hci_connect_le_scan(struct hci_dev *hdev, bdaddr_t *dst,
 				     u8 dst_type, u8 sec_level,
-				     u16 conn_timeout, u8 role);
+				     u16 conn_timeout);
 struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst,
 				u8 dst_type, u8 sec_level, u16 conn_timeout,
 				u8 role);
net/bluetooth/hci_conn.c +2 -2
index 1ed1e153b3fa..673c2254935b 100644
@@ -988,7 +988,7 @@ static int hci_explicit_conn_params_set(struct hci_dev *hdev,
 /* This function requires the caller holds hdev->lock */
 struct hci_conn *hci_connect_le_scan(struct hci_dev *hdev, bdaddr_t *dst,
 				     u8 dst_type, u8 sec_level,
-				     u16 conn_timeout, u8 role)
+				     u16 conn_timeout)
 {
 	struct hci_conn *conn;
 
@@ -1018,7 +1018,7 @@ struct hci_conn *hci_connect_le_scan(struct hci_dev *hdev, bdaddr_t *dst,
 
 	BT_DBG("requesting refresh of dst_addr");
 
-	conn = hci_conn_add(hdev, LE_LINK, dst, role);
+	conn = hci_conn_add(hdev, LE_LINK, dst, HCI_ROLE_MASTER);
 	if (!conn)
 		return ERR_PTR(-ENOMEM);
 
net/bluetooth/l2cap_core.c +7 -8
index 7c65ee200c29..ffb7b26e02bc 100644
@@ -7099,8 +7099,6 @@ int l2cap_chan_connect(struct l2cap_chan *chan, __le16 psm, u16 cid,
 	chan->dcid = cid;
 
 	if (bdaddr_type_is_le(dst_type)) {
-		u8 role;
-
 		/* Convert from L2CAP channel address type to HCI address type
 		 */
 		if (dst_type == BDADDR_LE_PUBLIC)
@@ -7109,14 +7107,15 @@ int l2cap_chan_connect(struct l2cap_chan *chan, __le16 psm, u16 cid,
 			dst_type = ADDR_LE_DEV_RANDOM;
 
 		if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
-			role = HCI_ROLE_SLAVE;
+			hcon = hci_connect_le(hdev, dst, dst_type,
+					      chan->sec_level,
+					      HCI_LE_CONN_TIMEOUT,
+					      HCI_ROLE_SLAVE);
 		else
-			role = HCI_ROLE_MASTER;
+			hcon = hci_connect_le_scan(hdev, dst, dst_type,
+						   chan->sec_level,
+						   HCI_LE_CONN_TIMEOUT);
 
-		hcon = hci_connect_le_scan(hdev, dst, dst_type,
-					   chan->sec_level,
-					   HCI_LE_CONN_TIMEOUT,
-					   role);
 	} else {
 		u8 auth_type = l2cap_get_auth_type(chan);
 		hcon = hci_connect_acl(hdev, dst, chan->sec_level, auth_type);
net/bluetooth/mgmt.c +1 -2
index e4ad0457547a..eca203e891d2 100644
@@ -3518,8 +3518,7 @@ static int pair_device(struct sock *sk, struct hci_dev *hdev, void *data,
 
 		conn = hci_connect_le_scan(hdev, &cp->addr.bdaddr,
 					   addr_type, sec_level,
-					   HCI_LE_CONN_TIMEOUT,
-					   HCI_ROLE_MASTER);
+					   HCI_LE_CONN_TIMEOUT);
 	}
 
 	if (IS_ERR(conn)) {
Signature · 1 line
2.5.0
#2 lore raw

PATCH 2/3 Bluetooth: Move check for ongoing connect earlier in hci_connect_le()

In reply to Johan Hedberg

From: Johan Hedberg <[email protected]>

This helps simplify the logic in further patches (less cleanups to do in this failure branch).

---
 net/bluetooth/hci_conn.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
net/bluetooth/hci_conn.c +6 -6
index 673c2254935b..08a291dd0f3a 100644
@@ -798,6 +798,12 @@ struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst,
 		return ERR_PTR(-EOPNOTSUPP);
 	}
 
+	/* Since the controller supports only one LE connection attempt at a
+	 * time, we return -EBUSY if there is any connection attempt running.
+	 */
+	if (hci_lookup_le_connect(hdev))
+		return ERR_PTR(-EBUSY);
+
 	/* Some devices send ATT messages as soon as the physical link is
 	 * established. To be able to handle these ATT messages, the user-
 	 * space first establishes the connection and then starts the pairing
@@ -821,12 +827,6 @@ struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst,
 		}
 	}
 
-	/* Since the controller supports only one LE connection attempt at a
-	 * time, we return -EBUSY if there is any connection attempt running.
-	 */
-	if (hci_lookup_le_connect(hdev))
-		return ERR_PTR(-EBUSY);
-
 	/* When given an identity address with existing identity
 	 * resolving key, the connection needs to be established
 	 * to a resolvable random address.
Signature · 1 line
2.5.0
#3 lore raw

PATCH 3/3 Bluetooth: Remove conn_unfinished variable from hci_connect_le()

In reply to Johan Hedberg

From: Johan Hedberg <[email protected]>

The conn_unfinished variable makes the entire logic of hci_connect_le() rather confusing. By restructuring and clarifying the logic we can actually remove the conn_unfinished variable and still keep the same behavior.

---
 net/bluetooth/hci_conn.c | 48 ++++++++++++------------------------------------
 1 file changed, 12 insertions(+), 36 deletions(-)
net/bluetooth/hci_conn.c +12 -36
index 08a291dd0f3a..2d334e07fd77 100644
@@ -785,7 +785,7 @@ struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst,
 				u8 role)
 {
 	struct hci_conn_params *params;
-	struct hci_conn *conn, *conn_unfinished;
+	struct hci_conn *conn;
 	struct smp_irk *irk;
 	struct hci_request req;
 	int err;
@@ -804,27 +804,14 @@ struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst,
 	if (hci_lookup_le_connect(hdev))
 		return ERR_PTR(-EBUSY);
 
-	/* Some devices send ATT messages as soon as the physical link is
-	 * established. To be able to handle these ATT messages, the user-
-	 * space first establishes the connection and then starts the pairing
-	 * process.
-	 *
-	 * So if a hci_conn object already exists for the following connection
-	 * attempt, we simply update pending_sec_level and auth_type fields
-	 * and return the object found.
+	/* If there's already a connection object but it's not in
+	 * scanning state it means it must already be established, in
+	 * which case we can't do anything else except report a failure
+	 * to connect.
 	 */
 	conn = hci_conn_hash_lookup_le(hdev, dst, dst_type);
-	conn_unfinished = NULL;
-	if (conn) {
-		if (conn->state == BT_CONNECT &&
-		    test_bit(HCI_CONN_SCANNING, &conn->flags)) {
-			BT_DBG("will continue unfinished conn %pMR", dst);
-			conn_unfinished = conn;
-		} else {
-			if (conn->pending_sec_level < sec_level)
-				conn->pending_sec_level = sec_level;
-			goto done;
-		}
+	if (conn && !test_bit(HCI_CONN_SCANNING, &conn->flags)) {
+		return ERR_PTR(-EBUSY);
 	}
 
 	/* When given an identity address with existing identity
@@ -842,23 +829,20 @@ struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst,
 		dst_type = ADDR_LE_DEV_RANDOM;
 	}
 
-	if (conn_unfinished) {
-		conn = conn_unfinished;
+	if (conn) {
 		bacpy(&conn->dst, dst);
 	} else {
 		conn = hci_conn_add(hdev, LE_LINK, dst, role);
+		if (!conn)
+			return ERR_PTR(-ENOMEM);
+		hci_conn_hold(conn);
+		conn->pending_sec_level = sec_level;
 	}
 
-	if (!conn)
-		return ERR_PTR(-ENOMEM);
-
 	conn->dst_type = dst_type;
 	conn->sec_level = BT_SECURITY_LOW;
 	conn->conn_timeout = conn_timeout;
 
-	if (!conn_unfinished)
-		conn->pending_sec_level = sec_level;
-
 	hci_req_init(&req, hdev);
 
 	/* Disable advertising if we're active. For master role
@@ -922,14 +906,6 @@ create_conn:
 		return ERR_PTR(err);
 	}
 
-done:
-	/* If this is continuation of connect started by hci_connect_le_scan,
-	 * it already called hci_conn_hold and calling it again would mess the
-	 * counter.
-	 */
-	if (!conn_unfinished)
-		hci_conn_hold(conn);
-
 	return conn;
 }
 
Signature · 1 line
2.5.0
maintainer
#4 lore raw
In reply to Johan Hedberg

Hi Johan,

Quoted 11 lines

This set of patches simplifies the LE connection logic and fixes outgoing connect attempts through directed advertising.

Johan

----------------------------------------------------------------
Johan Hedberg (3):
Bluetooth: Fix specifying role for LE connections
Bluetooth: Move check for ongoing connect earlier in hci_connect_le()
Bluetooth: Remove conn_unfinished variable from hci_connect_le()

Quoted 5 lines
include/net/bluetooth/hci_core.h |  2 +-
net/bluetooth/hci_conn.c         | 60 ++++++++++++---------------------------
net/bluetooth/l2cap_core.c       | 15 +++++-----
net/bluetooth/mgmt.c             |  3 +-
4 files changed, 27 insertions(+), 53 deletions(-)

all 3 patches have been applied to bluetooth-next tree.

Regards

Marcel