Skip to contentSkip to conversation

Bluetooth: Fix connecting devices during LE device discovery

PATCH In review [email protected] 2 participants 6 messages Jul 7, 2014

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

PATCH v3 1/3 Bluetooth: Fix connecting devices during LE device discovery

From: Johan Hedberg <[email protected]>

If we have devices set as to be connected we should connect to them even during normal discovery if we get a connectable advertising event. If we also have HCI_CONNECTABLE set we should connect ADV_DIRECT_IND events even to devices that we don't have in our pend_le_conns list. This patch implements such behavior by passing the advertising report type to check_pending_le_conn() and calls that function regardless of what type of scanning we are doing.

---
 net/bluetooth/hci_event.c | 47 +++++++++++++++++++++++++++++------------------
 1 file changed, 29 insertions(+), 18 deletions(-)
net/bluetooth/hci_event.c +29 -18
index 57837cad9919..8a36abb3cb06 100644
@@ -4229,13 +4229,30 @@ static void hci_le_conn_update_complete_evt(struct hci_dev *hdev,
 
 /* This function requires the caller holds hdev->lock */
 static bool check_pending_le_conn(struct hci_dev *hdev, bdaddr_t *addr,
-				  u8 addr_type)
+				  u8 addr_type, u8 adv_type)
 {
 	struct hci_conn *conn;
 
+	/* If the event is not connectable don't proceed further */
+	if (adv_type != LE_ADV_IND && adv_type != LE_ADV_DIRECT_IND)
+		return false;
+
+	/* Ignore if the device is blocked */
+	if (hci_blacklist_lookup(hdev, addr, addr_type))
+		return false;
+
+	/* If we're connectable, always connect any ADV_DIRECT_IND event */
+	if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags) &&
+	    adv_type == LE_ADV_DIRECT_IND)
+		goto connect;
+
+	/* If we're not connectable only connect devices that we have in
+	 * our pend_le_conns list.
+	 */
 	if (!hci_pend_le_action_lookup(&hdev->pend_le_conns, addr, addr_type))
 		return false;
 
+connect:
 	conn = hci_connect_le(hdev, addr, addr_type, BT_SECURITY_LOW,
 			      HCI_AT_NO_BONDING, HCI_LE_AUTOCONN_TIMEOUT);
 	if (!IS_ERR(conn))
@@ -4260,32 +4277,26 @@ static void process_adv_report(struct hci_dev *hdev, u8 type, bdaddr_t *bdaddr,
 			       u8 bdaddr_type, s8 rssi, u8 *data, u8 len)
 {
 	struct discovery_state *d = &hdev->discovery;
+	struct smp_irk *irk;
 	bool match;
 	u32 flags;
 
+	/* Check if we need to convert to identity address */
+	irk = hci_get_irk(hdev, bdaddr, bdaddr_type);
+	if (irk) {
+		bdaddr = &irk->bdaddr;
+		bdaddr_type = irk->addr_type;
+	}
+
+	/* Check if we have been requested to connect to this device */
+	check_pending_le_conn(hdev, bdaddr, bdaddr_type, type);
+
 	/* Passive scanning shouldn't trigger any device found events,
 	 * except for devices marked as CONN_REPORT for which we do send
 	 * device found events.
 	 */
 	if (hdev->le_scan_type == LE_SCAN_PASSIVE) {
 		struct hci_conn_params *param;
-		struct smp_irk *irk;
-
-		/* Check if we need to convert to identity address */
-		irk = hci_get_irk(hdev, bdaddr, bdaddr_type);
-		if (irk) {
-			bdaddr = &irk->bdaddr;
-			bdaddr_type = irk->addr_type;
-		}
-
-		/* Ignore if the device is blocked */
-		if (hci_blacklist_lookup(hdev, bdaddr, bdaddr_type))
-			return;
-
-		if (type == LE_ADV_IND || type == LE_ADV_DIRECT_IND) {
-			if (check_pending_le_conn(hdev, bdaddr, bdaddr_type))
-				return;
-		}
 
 		if (type == LE_ADV_DIRECT_IND)
 			return;
Signature · 1 line
1.9.3
#1 lore raw

PATCH v3 2/3 Bluetooth: Remove redundant IRK lookup in mgmt_device_found()

From: Johan Hedberg <[email protected]>

Now that we have the process_adv_report() function doing the IRK lookup and updating the bdaddr we don't need to do this anymore in mgmt.c in the mgmt_device_found() function.

---
 net/bluetooth/mgmt.c | 12 ++----------
 1 file changed, 2 insertions(+), 10 deletions(-)
net/bluetooth/mgmt.c +2 -10
index be91d55c258b..40244fc0e326 100644
@@ -6717,7 +6717,6 @@ void mgmt_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
 {
 	char buf[512];
 	struct mgmt_ev_device_found *ev = (void *) buf;
-	struct smp_irk *irk;
 	size_t ev_size;
 
 	/* Don't send events for a non-kernel initiated discovery. With
@@ -6739,15 +6738,8 @@ void mgmt_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
 
 	memset(buf, 0, sizeof(buf));
 
-	irk = hci_get_irk(hdev, bdaddr, addr_type);
-	if (irk) {
-		bacpy(&ev->addr.bdaddr, &irk->bdaddr);
-		ev->addr.type = link_to_bdaddr(link_type, irk->addr_type);
-	} else {
-		bacpy(&ev->addr.bdaddr, bdaddr);
-		ev->addr.type = link_to_bdaddr(link_type, addr_type);
-	}
-
+	bacpy(&ev->addr.bdaddr, bdaddr);
+	ev->addr.type = link_to_bdaddr(link_type, addr_type);
 	ev->rssi = rssi;
 	ev->flags = cpu_to_le32(flags);
 
Signature · 1 line
1.9.3
maintainer
#2 lore raw

Re: [PATCH v3 2/3] Bluetooth: Remove redundant IRK lookup in mgmt_device_found()

Hi Johan,

Quoted 8 lines

Now that we have the process_adv_report() function doing the IRK lookup and updating the bdaddr we don't need to do this anymore in mgmt.c in the mgmt_device_found() function.

---
net/bluetooth/mgmt.c | 12 ++----------

1 file changed, 2 insertions(+), 10 deletions(-)

patch has been applied to bluetooth-next tree.

Regards

Marcel

#3 lore raw

PATCH v3 3/3 Bluetooth: Remove unnecessary return value from check_pending_le_conn

In reply to [email protected]

From: Johan Hedberg <[email protected]>

Since only the caller of this function doesn't care about the return value anymore let's just remove it.

---
 net/bluetooth/hci_event.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
net/bluetooth/hci_event.c +5 -7
index 8a36abb3cb06..84f4659f136e 100644
@@ -4228,18 +4228,18 @@ static void hci_le_conn_update_complete_evt(struct hci_dev *hdev,
 }
 
 /* This function requires the caller holds hdev->lock */
-static bool check_pending_le_conn(struct hci_dev *hdev, bdaddr_t *addr,
+static void check_pending_le_conn(struct hci_dev *hdev, bdaddr_t *addr,
 				  u8 addr_type, u8 adv_type)
 {
 	struct hci_conn *conn;
 
 	/* If the event is not connectable don't proceed further */
 	if (adv_type != LE_ADV_IND && adv_type != LE_ADV_DIRECT_IND)
-		return false;
+		return;
 
 	/* Ignore if the device is blocked */
 	if (hci_blacklist_lookup(hdev, addr, addr_type))
-		return false;
+		return;
 
 	/* If we're connectable, always connect any ADV_DIRECT_IND event */
 	if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags) &&
@@ -4250,13 +4250,13 @@ static bool check_pending_le_conn(struct hci_dev *hdev, bdaddr_t *addr,
 	 * our pend_le_conns list.
 	 */
 	if (!hci_pend_le_action_lookup(&hdev->pend_le_conns, addr, addr_type))
-		return false;
+		return;
 
 connect:
 	conn = hci_connect_le(hdev, addr, addr_type, BT_SECURITY_LOW,
 			      HCI_AT_NO_BONDING, HCI_LE_AUTOCONN_TIMEOUT);
 	if (!IS_ERR(conn))
-		return true;
+		return;
 
 	switch (PTR_ERR(conn)) {
 	case -EBUSY:
@@ -4269,8 +4269,6 @@ connect:
 	default:
 		BT_DBG("Failed to connect: err %ld", PTR_ERR(conn));
 	}
-
-	return true;
 }
 
 static void process_adv_report(struct hci_dev *hdev, u8 type, bdaddr_t *bdaddr,
Signature · 1 line
1.9.3
maintainer
#4 lore raw

Re: [PATCH v3 3/3] Bluetooth: Remove unnecessary return value from check_pending_le_conn

Hi Johan,

Quoted 7 lines

Since only the caller of this function doesn't care about the return value anymore let's just remove it.

---
net/bluetooth/hci_event.c | 12 +++++-------

1 file changed, 5 insertions(+), 7 deletions(-)

patch has been applied to bluetooth-next tree.

Regards

Marcel

maintainer
#5 lore raw
In reply to [email protected]

Hi Johan,

Quoted 8 lines

If we have devices set as to be connected we should connect to them even during normal discovery if we get a connectable advertising event. If we also have HCI_CONNECTABLE set we should connect ADV_DIRECT_IND events even to devices that we don't have in our pend_le_conns list. This patch implements such behavior by passing the advertising report type to check_pending_le_conn() and calls that function regardless of what type of scanning we are doing.

Quoted 4 lines

---
net/bluetooth/hci_event.c | 47 +++++++++++++++++++++++++++++------------------

1 file changed, 29 insertions(+), 18 deletions(-)

patch has been applied to bluetooth-next tree.

Regards

Marcel