Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2d28cfe7 authored by Jakub Pawlowski's avatar Jakub Pawlowski Committed by Marcel Holtmann
Browse files

Bluetooth: Add le_scan_restart work for LE scan restarting



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.

During the restart, we must remember when the scan was started, and
it's duration, to later re-schedule the le_scan_disable work, that was
stopped during the stop scan phase.

Signed-off-by: default avatarJakub Pawlowski <jpawlowski@google.com>
Signed-off-by: default avatarMarcel Holtmann <marcel@holtmann.org>
parent 3251ca33
Loading
Loading
Loading
Loading
+5 −0
Original line number Original line Diff line number Diff line
@@ -79,6 +79,8 @@ struct discovery_state {
	s8			rssi;
	s8			rssi;
	u16			uuid_count;
	u16			uuid_count;
	u8			(*uuids)[16];
	u8			(*uuids)[16];
	unsigned long		scan_start;
	unsigned long		scan_duration;
};
};


struct hci_conn_hash {
struct hci_conn_hash {
@@ -354,6 +356,7 @@ struct hci_dev {
	unsigned long		dev_flags;
	unsigned long		dev_flags;


	struct delayed_work	le_scan_disable;
	struct delayed_work	le_scan_disable;
	struct delayed_work	le_scan_restart;


	__s8			adv_tx_power;
	__s8			adv_tx_power;
	__u8			adv_data[HCI_MAX_AD_LENGTH];
	__u8			adv_data[HCI_MAX_AD_LENGTH];
@@ -531,6 +534,8 @@ static inline void hci_discovery_filter_clear(struct hci_dev *hdev)
	hdev->discovery.uuid_count = 0;
	hdev->discovery.uuid_count = 0;
	kfree(hdev->discovery.uuids);
	kfree(hdev->discovery.uuids);
	hdev->discovery.uuids = NULL;
	hdev->discovery.uuids = NULL;
	hdev->discovery.scan_start = 0;
	hdev->discovery.scan_duration = 0;
}
}


bool hci_discovery_active(struct hci_dev *hdev);
bool hci_discovery_active(struct hci_dev *hdev);
+74 −0
Original line number Original line Diff line number Diff line
@@ -1617,6 +1617,7 @@ static int hci_dev_do_close(struct hci_dev *hdev)
		cancel_delayed_work(&hdev->service_cache);
		cancel_delayed_work(&hdev->service_cache);


	cancel_delayed_work_sync(&hdev->le_scan_disable);
	cancel_delayed_work_sync(&hdev->le_scan_disable);
	cancel_delayed_work_sync(&hdev->le_scan_restart);


	if (test_bit(HCI_MGMT, &hdev->dev_flags))
	if (test_bit(HCI_MGMT, &hdev->dev_flags))
		cancel_delayed_work_sync(&hdev->rpa_expired);
		cancel_delayed_work_sync(&hdev->rpa_expired);
@@ -2830,6 +2831,8 @@ static void le_scan_disable_work_complete(struct hci_dev *hdev, u8 status,
		return;
		return;
	}
	}


	hdev->discovery.scan_start = 0;

	switch (hdev->discovery.type) {
	switch (hdev->discovery.type) {
	case DISCOV_TYPE_LE:
	case DISCOV_TYPE_LE:
		hci_dev_lock(hdev);
		hci_dev_lock(hdev);
@@ -2869,6 +2872,8 @@ static void le_scan_disable_work(struct work_struct *work)


	BT_DBG("%s", hdev->name);
	BT_DBG("%s", hdev->name);


	cancel_delayed_work_sync(&hdev->le_scan_restart);

	hci_req_init(&req, hdev);
	hci_req_init(&req, hdev);


	hci_req_add_le_scan_disable(&req);
	hci_req_add_le_scan_disable(&req);
@@ -2878,6 +2883,74 @@ static void le_scan_disable_work(struct work_struct *work)
		BT_ERR("Disable LE scanning request failed: err %d", err);
		BT_ERR("Disable LE scanning request failed: err %d", err);
}
}


static void le_scan_restart_work_complete(struct hci_dev *hdev, u8 status,
					  u16 opcode)
{
	unsigned long timeout, duration, scan_start, now;

	BT_DBG("%s", hdev->name);

	if (status) {
		BT_ERR("Failed to restart LE scan: status %d", status);
		return;
	}

	if (!test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks) ||
	    !hdev->discovery.scan_start)
		return;

	/* When the scan was started, hdev->le_scan_disable has been queued
	 * after duration from scan_start. During scan restart this job
	 * has been canceled, and we need to queue it again after proper
	 * timeout, to make sure that scan does not run indefinitely.
	 */
	duration = hdev->discovery.scan_duration;
	scan_start = hdev->discovery.scan_start;
	now = jiffies;
	if (now - scan_start <= duration) {
		int elapsed;

		if (now >= scan_start)
			elapsed = now - scan_start;
		else
			elapsed = ULONG_MAX - scan_start + now;

		timeout = duration - elapsed;
	} else {
		timeout = 0;
	}
	queue_delayed_work(hdev->workqueue,
			   &hdev->le_scan_disable, timeout);
}

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;

	hci_req_init(&req, hdev);

	hci_req_add_le_scan_disable(&req);

	memset(&cp, 0, sizeof(cp));
	cp.enable = LE_SCAN_ENABLE;
	cp.filter_dup = LE_SCAN_FILTER_DUP_ENABLE;
	hci_req_add(&req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(cp), &cp);

	err = hci_req_run(&req, le_scan_restart_work_complete);
	if (err)
		BT_ERR("Restart LE scan request failed: err %d", err);
}

/* Copy the Identity Address of the controller.
/* Copy the Identity Address of the controller.
 *
 *
 * If the controller has a public BD_ADDR, then by default use that one.
 * If the controller has a public BD_ADDR, then by default use that one.
@@ -2974,6 +3047,7 @@ struct hci_dev *hci_alloc_dev(void)
	INIT_DELAYED_WORK(&hdev->power_off, hci_power_off);
	INIT_DELAYED_WORK(&hdev->power_off, hci_power_off);
	INIT_DELAYED_WORK(&hdev->discov_off, hci_discov_off);
	INIT_DELAYED_WORK(&hdev->discov_off, hci_discov_off);
	INIT_DELAYED_WORK(&hdev->le_scan_disable, le_scan_disable_work);
	INIT_DELAYED_WORK(&hdev->le_scan_disable, le_scan_disable_work);
	INIT_DELAYED_WORK(&hdev->le_scan_restart, le_scan_restart_work);


	skb_queue_head_init(&hdev->rx_q);
	skb_queue_head_init(&hdev->rx_q);
	skb_queue_head_init(&hdev->cmd_q);
	skb_queue_head_init(&hdev->cmd_q);
+18 −1
Original line number Original line Diff line number Diff line
@@ -3896,6 +3896,9 @@ static void start_discovery_complete(struct hci_dev *hdev, u8 status,


	hci_discovery_set_state(hdev, DISCOVERY_FINDING);
	hci_discovery_set_state(hdev, DISCOVERY_FINDING);


	/* If the scan involves LE scan, pick proper timeout to schedule
	 * hdev->le_scan_disable that will stop it.
	 */
	switch (hdev->discovery.type) {
	switch (hdev->discovery.type) {
	case DISCOV_TYPE_LE:
	case DISCOV_TYPE_LE:
		timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
		timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
@@ -3912,9 +3915,23 @@ static void start_discovery_complete(struct hci_dev *hdev, u8 status,
		break;
		break;
	}
	}


	if (timeout)
	if (timeout) {
		/* When service discovery is used and the controller has
		 * a strict duplicate filter, it is important to remember
		 * the start and duration of the scan. This is required
		 * for restarting scanning during the discovery phase.
		 */
		if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER,
			     &hdev->quirks) &&
		    (hdev->discovery.uuid_count > 0 ||
		     hdev->discovery.rssi != HCI_RSSI_INVALID)) {
			hdev->discovery.scan_start = jiffies;
			hdev->discovery.scan_duration = timeout;
		}

		queue_delayed_work(hdev->workqueue,
		queue_delayed_work(hdev->workqueue,
				   &hdev->le_scan_disable, timeout);
				   &hdev->le_scan_disable, timeout);
	}


unlock:
unlock:
	hci_dev_unlock(hdev);
	hci_dev_unlock(hdev);