Skip to contentSkip to conversation

Bluetooth: Add le_scan_restart

PATCH Superseded Jakub Pawlowski 2 participants 6 messages Jan 14, 2015 – Jan 16, 2015

Subsystems (2)

v8 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

PATCH v8 1/2 Bluetooth: Add le_scan_restart

Currently there is no way to restart le scan, and it's needed in service scan method. The way it work: it disable, and then enable le scan on controller.

---
 include/net/bluetooth/hci_core.h |  3 +++
 net/bluetooth/hci_core.c         | 52 ++++++++++++++++++++++++++++++++++++++++
 net/bluetooth/mgmt.c             |  7 +++++-
 3 files changed, 61 insertions(+), 1 deletion(-)
include/net/bluetooth/hci_core.h +3 -0
index 1f21fe4..2339057 100644
@@ -79,6 +79,7 @@ struct discovery_state {
 	s8			rssi;
 	u16			uuid_count;
 	u8			(*uuids)[16];
+	long			scan_end;
 };
 
 struct hci_conn_hash {
@@ -351,6 +352,7 @@ struct hci_dev {
 	unsigned long		dev_flags;
 
 	struct delayed_work	le_scan_disable;
+	struct delayed_work	le_scan_restart;
 
 	__s8			adv_tx_power;
 	__u8			adv_data[HCI_MAX_AD_LENGTH];
@@ -527,6 +529,7 @@ static inline void hci_discovery_filter_clear(struct hci_dev *hdev)
 	hdev->discovery.uuid_count = 0;
 	kfree(hdev->discovery.uuids);
 	hdev->discovery.uuids = NULL;
+	hdev->discovery.scan_end = 0;
 }
 
 bool hci_discovery_active(struct hci_dev *hdev);
net/bluetooth/hci_core.c +52 -0

88 lines not on this page · raw message

net/bluetooth/mgmt.c +6 -1
index e531da8..847c055b 100644
@@ -3878,9 +3878,14 @@ static void start_discovery_complete(struct hci_dev *hdev, u8 status,
 		break;
 	}
 
-	if (timeout)
+	if (timeout) {
+		if (hdev->discovery.uuid_count > 0 &&
+		    test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks))
+			hdev->discovery.scan_end = jiffies + timeout;
+
 		queue_delayed_work(hdev->workqueue,
 				   &hdev->le_scan_disable, timeout);
+	}
 
 unlock:
 	hci_dev_unlock(hdev);
Signature · 1 line
2.2.0.rc0.207.ga3a616c
#1 lore raw

PATCH v8 2/2 Bluetooth: Add restarting to service discovery

When using LE_SCAN_FILTER_DUP_ENABLE, some controllers would send advertising report from each LE device only once. That means that we don't get any updates on RSSI value, and makes Service Discovery very slow. This patch adds restarting scan when in Service Discovery, and device with filtered uuid is found, but it's not in RSSI range to send event yet. This way if device moves into range, we will quickly get RSSI update.

---
 include/net/bluetooth/hci_core.h |  1 +
 net/bluetooth/mgmt.c             | 44 ++++++++++++++++++++++++++++++++++++----
 2 files changed, 41 insertions(+), 4 deletions(-)
include/net/bluetooth/hci_core.h +1 -0
index 2339057..63cb0e8 100644
@@ -1329,6 +1329,7 @@ void hci_sock_dev_event(struct hci_dev *hdev, int event);
 #define DISCOV_INTERLEAVED_TIMEOUT	5120	/* msec */
 #define DISCOV_INTERLEAVED_INQUIRY_LEN	0x04
 #define DISCOV_BREDR_INQUIRY_LEN	0x08
+#define DISCOV_LE_RESTART_DELAY		200	/* msec */
 
 int mgmt_control(struct sock *sk, struct msghdr *msg, size_t len);
 int mgmt_new_settings(struct hci_dev *hdev);
net/bluetooth/mgmt.c +40 -4

78 lines not on this page · raw message

Signature · 1 line
2.2.0.rc0.207.ga3a616c
#2 lore raw

Re: [PATCH v8 2/2] Bluetooth: Add restarting to service discovery

Hi Jakub,

net/bluetooth/mgmt.c static bool eir_has_uuids(u8 *eir, u16 eir_len, u16 uuid_count, u8 (*uuids)[16]) · line 7178

Quoted 5 lines · Jakub Pawlowski
net/bluetooth/mgmt.c +5 -0
+static void restart_le_scan(struct hci_dev *hdev)
+{
+	/* If controller is not scanning we are done. */
+	if (!test_bit(HCI_LE_SCAN, &hdev->dev_flags))
+		return;

Since this function is only called from mgmt_device_found() can this condition actually ever be true?

net/bluetooth/mgmt.c static bool eir_has_uuids(u8 *eir, u16 eir_len, u16 uuid_count, u8 (*uuids)[16]) · line 7182

Quoted 4 lines
net/bluetooth/mgmt.c +4 -0
+
+	queue_delayed_work(hdev->workqueue, &hdev->le_scan_restart,
+			   msecs_to_jiffies(DISCOV_LE_RESTART_DELAY));
+}

What you should probably be checking for however is that scan_end < jiffies + msecs_to_jiffies(DISCOV_LE_RESTART_DELAY) since then you know that the scanning will have stopped by the tame the restart work gets activated. Or am I thinking wrong here?

net/bluetooth/mgmt.c void mgmt_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, · line 7215

Quoted 5 lines
net/bluetooth/mgmt.c +3 -1
 	if (hdev->discovery.rssi != HCI_RSSI_INVALID &&
-	    (rssi < hdev->discovery.rssi || rssi == HCI_RSSI_INVALID))
+	    (rssi == HCI_RSSI_INVALID ||
+	    (rssi < hdev->discovery.rssi && !test_bit(
+			HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks))))

What I meant by splitting this is that the !test_bit would start on a new line. That way you wouldn't need to break the line right after (

Johan

#3 lore raw

Re: [PATCH v8 2/2] Bluetooth: Add restarting to service discovery

Hi Johan,

Quoted 4 lines · Johan Hedberg

Hi Jakub,

On Wed, Jan 14, 2015, Jakub Pawlowski wrote:

Quoted 8 lines

net/bluetooth/mgmt.c static bool eir_has_uuids(u8 *eir, u16 eir_len, u16 uuid_count, u8 (*uuids)[16]) · line 7178

Quoted 5 lines
net/bluetooth/mgmt.c +5 -0
+static void restart_le_scan(struct hci_dev *hdev)
+{
+     /* If controller is not scanning we are done. */
+     if (!test_bit(HCI_LE_SCAN, &hdev->dev_flags))
+             return;

Since this function is only called from mgmt_device_found() can this condition actually ever be true?

Marcel proposed adding this check there, the rationale is in this mesage:
"""
so here you should check if we are scanning or not and if this extra handling of restarts is actually needed.

In addition you might really want to check that this is the LE scanning phase. I know that checking for
LE_SCAN would do that, but this is a bit dangerous since this code path can also be entered from BR/EDR
inquiry results.
"""
http://article.gmane.org/gmane.linux.bluez.kernel/57005

Quoted 10 lines

net/bluetooth/mgmt.c static bool eir_has_uuids(u8 *eir, u16 eir_len, u16 uuid_count, u8 (*uuids)[16]) · line 7182

Quoted 5 lines
net/bluetooth/mgmt.c +4 -0
+
+     queue_delayed_work(hdev->workqueue, &hdev->le_scan_restart,
+                        msecs_to_jiffies(DISCOV_LE_RESTART_DELAY));
+}

What you should probably be checking for however is that scan_end < jiffies + msecs_to_jiffies(DISCOV_LE_RESTART_DELAY) since then you know that the scanning will have stopped by the tame the restart work gets activated. Or am I thinking wrong here?

le_scan_disable_work that is used to disable scan is canceling le_scan_restart, to make sure we won't try to restart after disabling scan.
Also le_scan_restart_work have check to not start the restart process when the scan is already disabled.

So I think it won't fix any bug. It will however save few cycles by not queuing this work, so I'll add that check.

Quoted 11 lines

net/bluetooth/mgmt.c void mgmt_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, · line 7215

Quoted 6 lines
      if (hdev->discovery.rssi != HCI_RSSI_INVALID &&
net/bluetooth/mgmt.c +3 -1
-         (rssi < hdev->discovery.rssi || rssi == HCI_RSSI_INVALID))
+         (rssi == HCI_RSSI_INVALID ||
+         (rssi < hdev->discovery.rssi && !test_bit(
+                     HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks))))

What I meant by splitting this is that the !test_bit would start on a new line. That way you wouldn't need to break the line right after (

Johan

#4 lore raw
In reply to Jakub Pawlowski

Hi Jakub,

net/bluetooth/hci_core.c static void le_scan_disable_work(struct work_struct *work) · line 2853

Quoted 9 lines · Jakub Pawlowski
net/bluetooth/hci_core.c +9 -0
+static void le_scan_restart_work_complete(struct hci_dev *hdev, u8 status,
+					  u16 opcode)
+{
+	BT_DBG("%s", hdev->name);
+	if (status) {
+		BT_ERR("Failed to restart LE scan: status %d", status);
+	} else if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks) &&
+		 hdev->discovery.scan_end != 0) {
+		long timeout = hdev->discovery.scan_end - jiffies;

To make this consistent with the coding style, could you please format it as:

	if (status) {
		BT_ERR(...);
		return;
	}

	if (!test_bit(DUP_FILT) || !hdev->discovery_scan_end)
		return;

	timeout = ...;

net/bluetooth/hci_core.c static void le_scan_disable_work(struct work_struct *work) · line 2874

Quoted 13 lines
net/bluetooth/hci_core.c +13 -0
+static void le_scan_restart_work(struct work_struct *work)
+{
+	struct hci_dev *hdev = container_of(work, struct hci_dev,
+					    le_scan_restart.work);
+	struct hci_request req;
+	struct hci_cp_le_set_scan_enable cp;
+	int err;
+
+	BT_DBG("%s", hdev->name);
+
+	/* If controller is not scanning we are done. */
+	if (!test_bit(HCI_LE_SCAN, &hdev->dev_flags))
+		return;

Shouldn't you also make sure to cancel this delayed work wherever the code clears the HCI_LE_SCAN flag? (i.e. in hci_event.c)

Johan

#5 lore raw

Hi Johan,

Quoted 26 lines · Johan Hedberg

Hi Jakub,

net/bluetooth/hci_core.c static void le_scan_disable_work(struct work_struct *work) · line 2853

Quoted 9 lines · Jakub Pawlowski
net/bluetooth/hci_core.c +9 -0
+static void le_scan_restart_work_complete(struct hci_dev *hdev, u8 status,
+                                       u16 opcode)
+{
+     BT_DBG("%s", hdev->name);
+     if (status) {
+             BT_ERR("Failed to restart LE scan: status %d", status);
+     } else if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks) &&
+              hdev->discovery.scan_end != 0) {
+             long timeout = hdev->discovery.scan_end - jiffies;

To make this consistent with the coding style, could you please format it as:

        if (status) {
                BT_ERR(...);
                return;
        }

        if (!test_bit(DUP_FILT) || !hdev->discovery_scan_end)
                return;

        timeout = ...;
Quoted 16 lines

net/bluetooth/hci_core.c static void le_scan_disable_work(struct work_struct *work) · line 2874

Quoted 13 lines
net/bluetooth/hci_core.c +13 -0
+static void le_scan_restart_work(struct work_struct *work)
+{
+     struct hci_dev *hdev = container_of(work, struct hci_dev,
+                                         le_scan_restart.work);
+     struct hci_request req;
+     struct hci_cp_le_set_scan_enable cp;
+     int err;
+
+     BT_DBG("%s", hdev->name);
+
+     /* If controller is not scanning we are done. */
+     if (!test_bit(HCI_LE_SCAN, &hdev->dev_flags))
+             return;

Shouldn't you also make sure to cancel this delayed work wherever the code clears the HCI_LE_SCAN flag? (i.e. in hci_event.c)

le_scan_restart_work checks wether HCI_LE_SCAN is set to make sure it won't accidentally 'restart' the scan when it's disabled.
Or did you wanted to cancel this delayed work to make the code faster ? This cancel would be executed only if the DEDUPLICATION quirk is set, so it shouldn't be a gain in speed.

Quoted 2 lines

Johan