Skip to contentSkip to conversation

Bluetooth: add hci_restart_le_scan

PATCH Superseded Jakub Pawlowski 2 participants 12 messages Nov 20, 2014

Subsystems (2)

v7 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

PATCH v7 1/3 Bluetooth: add hci_restart_le_scan

Currently there is no way to restart le scan. It's needed in preparation for new service scan method. The way it work: it disable, and then enable le scan on controller. During this restart special flag is set to make sure we won't remove disable scan work from workqueue.

---
 include/net/bluetooth/hci.h      |  1 +
 include/net/bluetooth/hci_core.h |  2 ++
 net/bluetooth/hci_core.c         | 42 ++++++++++++++++++++++++++++++++++++++++
 net/bluetooth/hci_event.c        |  9 ++++++---
 4 files changed, 51 insertions(+), 3 deletions(-)
include/net/bluetooth/hci.h +1 -0
index e56f909..2f0bce2 100644
@@ -203,6 +203,7 @@ enum {
 	HCI_FAST_CONNECTABLE,
 	HCI_BREDR_ENABLED,
 	HCI_LE_SCAN_INTERRUPTED,
+	HCI_LE_SCAN_RESTARTING,
 };
 
 /* A mask for the flags that are supposed to remain when a reset happens
include/net/bluetooth/hci_core.h +2 -0
index 396c098..36211f9 100644
@@ -337,6 +337,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];
@@ -1403,6 +1404,7 @@ int hci_update_random_address(struct hci_request *req, bool require_privacy,
 			      u8 *own_addr_type);
 void hci_copy_identity_address(struct hci_dev *hdev, bdaddr_t *bdaddr,
 			       u8 *bdaddr_type);
+void hci_restart_le_scan(struct hci_dev *hdev, unsigned long delay);
 
 #define SCO_AIRMODE_MASK       0x0003
 #define SCO_AIRMODE_CVSD       0x0000
net/bluetooth/hci_core.c +42 -0

66 lines not on this page · raw message

net/bluetooth/hci_event.c +6 -3
index bd0a801..ed4d5e1 100644
@@ -1157,10 +1157,13 @@ static void hci_cc_le_set_scan_enable(struct hci_dev *hdev,
 					  d->last_adv_data_len, NULL, 0);
 		}
 
-		/* Cancel this timer so that we don't try to disable scanning
-		 * when it's already disabled.
+		/* If HCI_LE_SCAN_RESTARTING is set, don't cancel this timer,
+		 * because we're just restarting scan. Otherwise cancel it so
+		 * that we don't try to disable scanning when it's already
+		 * disabled.
 		 */
-		cancel_delayed_work(&hdev->le_scan_disable);
+		if (!test_bit(HCI_LE_SCAN_RESTARTING, &hdev->dev_flags))
+			cancel_delayed_work(&hdev->le_scan_disable);
 
 		clear_bit(HCI_LE_SCAN, &hdev->dev_flags);
 
Signature · 1 line
2.1.0.rc2.206.gedb03e5
#1 lore raw

PATCH v7 2/3 Bluetooth: Extract generic start and stop discovery

This commit extract generic_start_discovery and generic_stop_discovery in preparation for start and stop service discovery. The reason behind that is that both functions will share big part of code, and it would be much easier to maintain just one generic method.

---
 net/bluetooth/mgmt.c | 147 ++++++++++++++++++++++++++++++---------------------
 1 file changed, 86 insertions(+), 61 deletions(-)
net/bluetooth/mgmt.c +86 -61
index cbeef5f..f650d30 100644
@@ -3675,7 +3675,8 @@ done:
 	return err;
 }
 
-static int mgmt_start_discovery_failed(struct hci_dev *hdev, u8 status)
+static int mgmt_start_discovery_failed(struct hci_dev *hdev, u8 status,
+				       uint16_t opcode)
 {
 	struct pending_cmd *cmd;
 	u8 type;
@@ -3683,7 +3684,7 @@ static int mgmt_start_discovery_failed(struct hci_dev *hdev, u8 status)
 
 	hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
 
-	cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev);
+	cmd = mgmt_pending_find(opcode, hdev);
 	if (!cmd)
 		return -ENOENT;
 
@@ -3696,7 +3697,8 @@ static int mgmt_start_discovery_failed(struct hci_dev *hdev, u8 status)
 	return err;
 }
 
-static void start_discovery_complete(struct hci_dev *hdev, u8 status)
+static void generic_start_discovery_complete(struct hci_dev *hdev, u8 status,
+					     uint16_t opcode)
 {
 	unsigned long timeout = 0;
 
@@ -3704,7 +3706,7 @@ static void start_discovery_complete(struct hci_dev *hdev, u8 status)
 
 	if (status) {
 		hci_dev_lock(hdev);
-		mgmt_start_discovery_failed(hdev, status);
+		mgmt_start_discovery_failed(hdev, status, opcode);
 		hci_dev_unlock(hdev);
 		return;
 	}
@@ -3735,8 +3737,13 @@ static void start_discovery_complete(struct hci_dev *hdev, u8 status)
 	queue_delayed_work(hdev->workqueue, &hdev->le_scan_disable, timeout);
 }
 
-static int start_discovery(struct sock *sk, struct hci_dev *hdev,
-			   void *data, u16 len)
+static void start_discovery_complete(struct hci_dev *hdev, u8 status)
+{
+	generic_start_discovery_complete(hdev, status, MGMT_OP_START_DISCOVERY);
+}
+
+static int generic_start_discovery(struct sock *sk, struct hci_dev *hdev,
+				   void *data, u16 len, uint16_t opcode)
 {
 	struct mgmt_cp_start_discovery *cp = data;
 	struct pending_cmd *cmd;
@@ -3746,41 +3753,41 @@ static int start_discovery(struct sock *sk, struct hci_dev *hdev,
 	struct hci_request req;
 	/* General inquiry access code (GIAC) */
 	u8 lap[3] = { 0x33, 0x8b, 0x9e };
-	u8 status, own_addr_type;
+	u8 status, own_addr_type, type;
 	int err;
 
 	BT_DBG("%s", hdev->name);
 
+	type = cp->type;
+
 	hci_dev_lock(hdev);
 
 	if (!hdev_is_powered(hdev)) {
-		err = cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY,
-				   MGMT_STATUS_NOT_POWERED,
-				   &cp->type, sizeof(cp->type));
+		err = cmd_complete(sk, hdev->id, opcode,
+				   MGMT_STATUS_NOT_POWERED, &type,
+				   sizeof(type));
 		goto failed;
 	}
 
 	if (test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags)) {
-		err = cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY,
-				   MGMT_STATUS_BUSY, &cp->type,
-				   sizeof(cp->type));
+		err = cmd_complete(sk, hdev->id, opcode,
+				   MGMT_STATUS_BUSY, &type, sizeof(type));
 		goto failed;
 	}
 
 	if (hdev->discovery.state != DISCOVERY_STOPPED) {
-		err = cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY,
-				   MGMT_STATUS_BUSY, &cp->type,
-				   sizeof(cp->type));
+		err = cmd_complete(sk, hdev->id, opcode,
+				   MGMT_STATUS_BUSY, &type, sizeof(type));
 		goto failed;
 	}
 
-	cmd = mgmt_pending_add(sk, MGMT_OP_START_DISCOVERY, hdev, NULL, 0);
+	cmd = mgmt_pending_add(sk, opcode, hdev, NULL, 0);
 	if (!cmd) {
 		err = -ENOMEM;
 		goto failed;
 	}
 
-	hdev->discovery.type = cp->type;
+	hdev->discovery.type = type;
 
 	hci_req_init(&req, hdev);
 
@@ -3788,18 +3795,16 @@ static int start_discovery(struct sock *sk, struct hci_dev *hdev,
 	case DISCOV_TYPE_BREDR:
 		status = mgmt_bredr_support(hdev);
 		if (status) {
-			err = cmd_complete(sk, hdev->id,
-					   MGMT_OP_START_DISCOVERY, status,
-					   &cp->type, sizeof(cp->type));
+			err = cmd_complete(sk, hdev->id, opcode, status, &type,
+					   sizeof(type));
 			mgmt_pending_remove(cmd);
 			goto failed;
 		}
 
 		if (test_bit(HCI_INQUIRY, &hdev->flags)) {
-			err = cmd_complete(sk, hdev->id,
-					   MGMT_OP_START_DISCOVERY,
-					   MGMT_STATUS_BUSY, &cp->type,
-					   sizeof(cp->type));
+			err = cmd_complete(sk, hdev->id, opcode,
+					   MGMT_STATUS_BUSY, &type,
+					   sizeof(type));
 			mgmt_pending_remove(cmd);
 			goto failed;
 		}
@@ -3816,19 +3821,17 @@ static int start_discovery(struct sock *sk, struct hci_dev *hdev,
 	case DISCOV_TYPE_INTERLEAVED:
 		status = mgmt_le_support(hdev);
 		if (status) {
-			err = cmd_complete(sk, hdev->id,
-					   MGMT_OP_START_DISCOVERY, status,
-					   &cp->type, sizeof(cp->type));
+			err = cmd_complete(sk, hdev->id, opcode, status, &type,
+					   sizeof(type));
 			mgmt_pending_remove(cmd);
 			goto failed;
 		}
 
 		if (hdev->discovery.type == DISCOV_TYPE_INTERLEAVED &&
 		    !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) {
-			err = cmd_complete(sk, hdev->id,
-					   MGMT_OP_START_DISCOVERY,
-					   MGMT_STATUS_NOT_SUPPORTED,
-					   &cp->type, sizeof(cp->type));
+			err = cmd_complete(sk, hdev->id, opcode,
+					   MGMT_STATUS_NOT_SUPPORTED, &type,
+					   sizeof(type));
 			mgmt_pending_remove(cmd);
 			goto failed;
 		}
@@ -3840,11 +3843,9 @@ static int start_discovery(struct sock *sk, struct hci_dev *hdev,
 			 */
 			if (hci_conn_hash_lookup_state(hdev, LE_LINK,
 						       BT_CONNECT)) {
-				err = cmd_complete(sk, hdev->id,
-						   MGMT_OP_START_DISCOVERY,
-						   MGMT_STATUS_REJECTED,
-						   &cp->type,
-						   sizeof(cp->type));
+				err = cmd_complete(sk, hdev->id, opcode,
+						   MGMT_STATUS_REJECTED, &type,
+						   sizeof(type));
 				mgmt_pending_remove(cmd);
 				goto failed;
 			}
@@ -3867,10 +3868,9 @@ static int start_discovery(struct sock *sk, struct hci_dev *hdev,
 		 */
 		err = hci_update_random_address(&req, true, &own_addr_type);
 		if (err < 0) {
-			err = cmd_complete(sk, hdev->id,
-					   MGMT_OP_START_DISCOVERY,
-					   MGMT_STATUS_FAILED,
-					   &cp->type, sizeof(cp->type));
+			err = cmd_complete(sk, hdev->id, opcode,
+					   MGMT_STATUS_FAILED, &type,
+					   sizeof(type));
 			mgmt_pending_remove(cmd);
 			goto failed;
 		}
@@ -3890,14 +3890,15 @@ static int start_discovery(struct sock *sk, struct hci_dev *hdev,
 		break;
 
 	default:
-		err = cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY,
-				   MGMT_STATUS_INVALID_PARAMS,
-				   &cp->type, sizeof(cp->type));
+		err = cmd_complete(sk, hdev->id, opcode,
+				   MGMT_STATUS_INVALID_PARAMS, &type,
+				   sizeof(type));
 		mgmt_pending_remove(cmd);
 		goto failed;
 	}
 
 	err = hci_req_run(&req, start_discovery_complete);
+
 	if (err < 0)
 		mgmt_pending_remove(cmd);
 	else
@@ -3908,12 +3909,20 @@ failed:
 	return err;
 }
 
-static int mgmt_stop_discovery_failed(struct hci_dev *hdev, u8 status)
+static int start_discovery(struct sock *sk, struct hci_dev *hdev,
+			   void *data, u16 len)
+{
+	return generic_start_discovery(sk, hdev, data, len,
+				       MGMT_OP_START_DISCOVERY);
+}
+
+static int mgmt_stop_discovery_failed(struct hci_dev *hdev, u8 status,
+				      uint16_t opcode)
 {
 	struct pending_cmd *cmd;
 	int err;
 
-	cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev);
+	cmd = mgmt_pending_find(opcode, hdev);
 	if (!cmd)
 		return -ENOENT;
 
@@ -3924,14 +3933,15 @@ static int mgmt_stop_discovery_failed(struct hci_dev *hdev, u8 status)
 	return err;
 }
 
-static void stop_discovery_complete(struct hci_dev *hdev, u8 status)
+static void generic_stop_discovery_complete(struct hci_dev *hdev, u8 status,
+					    uint16_t opcode)
 {
 	BT_DBG("status %d", status);
 
 	hci_dev_lock(hdev);
 
 	if (status) {
-		mgmt_stop_discovery_failed(hdev, status);
+		mgmt_stop_discovery_failed(hdev, status, opcode);
 		goto unlock;
 	}
 
@@ -3941,33 +3951,40 @@ unlock:
 	hci_dev_unlock(hdev);
 }
 
-static int stop_discovery(struct sock *sk, struct hci_dev *hdev, void *data,
-			  u16 len)
+static void stop_discovery_complete(struct hci_dev *hdev, u8 status)
+{
+	generic_stop_discovery_complete(hdev, status, MGMT_OP_STOP_DISCOVERY);
+}
+
+static int generic_stop_discovery(struct sock *sk, struct hci_dev *hdev,
+				  void *data, u16 len, uint16_t opcode)
 {
-	struct mgmt_cp_stop_discovery *mgmt_cp = data;
 	struct pending_cmd *cmd;
 	struct hci_request req;
+	struct mgmt_cp_stop_discovery *mgmt_cp = data;
 	int err;
+	u8 type;
 
 	BT_DBG("%s", hdev->name);
 
+	type = mgmt_cp->type;
+
 	hci_dev_lock(hdev);
 
 	if (!hci_discovery_active(hdev)) {
-		err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY,
-				   MGMT_STATUS_REJECTED, &mgmt_cp->type,
-				   sizeof(mgmt_cp->type));
+		err = cmd_complete(sk, hdev->id, opcode,
+				   MGMT_STATUS_REJECTED, &type, sizeof(type));
 		goto unlock;
 	}
 
-	if (hdev->discovery.type != mgmt_cp->type) {
-		err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY,
-				   MGMT_STATUS_INVALID_PARAMS, &mgmt_cp->type,
-				   sizeof(mgmt_cp->type));
+	if (hdev->discovery.type != type) {
+		err = cmd_complete(sk, hdev->id, opcode,
+				   MGMT_STATUS_INVALID_PARAMS, &type,
+				   sizeof(type));
 		goto unlock;
 	}
 
-	cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, NULL, 0);
+	cmd = mgmt_pending_add(sk, opcode, hdev, NULL, 0);
 	if (!cmd) {
 		err = -ENOMEM;
 		goto unlock;
@@ -3978,6 +3995,7 @@ static int stop_discovery(struct sock *sk, struct hci_dev *hdev, void *data,
 	hci_stop_discovery(&req);
 
 	err = hci_req_run(&req, stop_discovery_complete);
+
 	if (!err) {
 		hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
 		goto unlock;
@@ -3987,8 +4005,8 @@ static int stop_discovery(struct sock *sk, struct hci_dev *hdev, void *data,
 
 	/* If no HCI commands were sent we're done */
 	if (err == -ENODATA) {
-		err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, 0,
-				   &mgmt_cp->type, sizeof(mgmt_cp->type));
+		err = cmd_complete(sk, hdev->id, opcode, 0,
+				   &type, sizeof(type));
 		hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
 	}
 
@@ -3997,6 +4015,13 @@ unlock:
 	return err;
 }
 
+static int stop_discovery(struct sock *sk, struct hci_dev *hdev, void *data,
+			  u16 len)
+{
+	return generic_stop_discovery(sk, hdev, data, len,
+				      MGMT_OP_STOP_DISCOVERY);
+}
+
 static int confirm_name(struct sock *sk, struct hci_dev *hdev, void *data,
 			u16 len)
 {
Signature · 1 line
2.1.0.rc2.206.gedb03e5
#6 lore raw

PATCH v7 3/3 Bluetooth: start and stop service discovery

In reply to Jakub Pawlowski

This patch introduces start service discovery and stop service discovery methods. The reason behind that is to enable users to find specific services in range by UUID. Whole filtering is done in mgmt_device_found.

---
 include/net/bluetooth/hci.h      |   1 +
 include/net/bluetooth/hci_core.h |  11 ++
 include/net/bluetooth/mgmt.h     |  24 ++++
 net/bluetooth/hci_core.c         |   1 +
 net/bluetooth/mgmt.c             | 296 +++++++++++++++++++++++++++++++++++++--
 5 files changed, 324 insertions(+), 9 deletions(-)
include/net/bluetooth/hci.h +1 -0
index 2f0bce2..97b0893 100644
@@ -204,6 +204,7 @@ enum {
 	HCI_BREDR_ENABLED,
 	HCI_LE_SCAN_INTERRUPTED,
 	HCI_LE_SCAN_RESTARTING,
+	HCI_SERVICE_FILTER,
 };
 
 /* A mask for the flags that are supposed to remain when a reset happens
include/net/bluetooth/hci_core.h +11 -0
index 36211f9..39009f2 100644
@@ -68,6 +68,7 @@ struct discovery_state {
 	struct list_head	all;	/* All devices found during inquiry */
 	struct list_head	unknown;	/* Name state not known */
 	struct list_head	resolve;	/* Name needs to be resolved */
+	struct list_head	uuid_filter;
 	__u32			timestamp;
 	bdaddr_t		last_adv_addr;
 	u8			last_adv_addr_type;
@@ -146,6 +147,12 @@ struct oob_data {
 	u8 rand256[16];
 };
 
+struct uuid_filter {
+	struct list_head list;
+	s8 rssi;
+	u8 uuid[16];
+};
+
 #define HCI_MAX_SHORT_NAME_LENGTH	10
 
 /* Default LE RPA expiry time, 15 minutes */
@@ -502,6 +509,7 @@ static inline void discovery_init(struct hci_dev *hdev)
 	INIT_LIST_HEAD(&hdev->discovery.all);
 	INIT_LIST_HEAD(&hdev->discovery.unknown);
 	INIT_LIST_HEAD(&hdev->discovery.resolve);
+	INIT_LIST_HEAD(&hdev->discovery.uuid_filter);
 }
 
 bool hci_discovery_active(struct hci_dev *hdev);
@@ -1328,6 +1336,8 @@ void hci_sock_dev_event(struct hci_dev *hdev, int event);
 #define DISCOV_INTERLEAVED_INQUIRY_LEN	0x04
 #define DISCOV_BREDR_INQUIRY_LEN	0x08
 
+#define DISCOV_LE_RESTART_DELAY msecs_to_jiffies(300)
+
 int mgmt_control(struct sock *sk, struct msghdr *msg, size_t len);
 int mgmt_new_settings(struct hci_dev *hdev);
 void mgmt_index_added(struct hci_dev *hdev);
@@ -1394,6 +1404,7 @@ void mgmt_new_conn_param(struct hci_dev *hdev, bdaddr_t *bdaddr,
 			 u16 max_interval, u16 latency, u16 timeout);
 void mgmt_reenable_advertising(struct hci_dev *hdev);
 void mgmt_smp_complete(struct hci_conn *conn, bool complete);
+void mgmt_clean_up_service_discovery(struct hci_dev *hdev);
 
 u8 hci_le_conn_update(struct hci_conn *conn, u16 min, u16 max, u16 latency,
 		      u16 to_multiplier);
include/net/bluetooth/mgmt.h +24 -0
index b391fd6..202caf7 100644
@@ -495,6 +495,30 @@ struct mgmt_cp_set_public_address {
 } __packed;
 #define MGMT_SET_PUBLIC_ADDRESS_SIZE	6
 
+#define MGMT_OP_START_SERVICE_DISCOVERY	0x003A
+#define MGMT_START_SERVICE_DISCOVERY_SIZE 1
+
+#define MGMT_RANGE_NONE			0x00
+#define MGMT_RANGE_RSSI			0x01
+#define MGMT_RANGE_PATHLOSS		0x02
+
+struct mgmt_uuid_filter {
+	__s8 rssi;
+	__u8 uuid[16];
+} __packed;
+
+struct mgmt_cp_start_service_discovery {
+	__u8 type;
+	__le16 filter_count;
+	struct mgmt_uuid_filter filter[0];
+} __packed;
+
+#define MGMT_OP_STOP_SERVICE_DISCOVERY	0x003B
+struct mgmt_cp_stop_service_discovery {
+	__u8 type;
+} __packed;
+#define MGMT_STOP_SERVICE_DISCOVERY_SIZE 1
+
 #define MGMT_EV_CMD_COMPLETE		0x0001
 struct mgmt_ev_cmd_complete {
 	__le16	opcode;
net/bluetooth/hci_core.c +1 -0
index 858cf54..fab6755 100644
@@ -3853,6 +3853,7 @@ static void le_scan_disable_work(struct work_struct *work)
 	BT_DBG("%s", hdev->name);
 
 	cancel_delayed_work_sync(&hdev->le_scan_restart);
+	mgmt_clean_up_service_discovery(hdev);
 
 	hci_req_init(&req, hdev);
 
net/bluetooth/mgmt.c +287 -9

401 lines not on this page · raw message

Signature · 1 line
2.1.0.rc2.206.gedb03e5
#7 lore raw
In reply to Jakub Pawlowski

Hi Jakub,

Quoted 68 lines · Jakub Pawlowski

Currently there is no way to restart le scan. It's needed in preparation for new service scan method. The way it work: it disable, and then enable le scan on controller. During this restart special flag is set to make sure we won't remove disable scan work from workqueue.

---
 include/net/bluetooth/hci.h      |  1 +
 include/net/bluetooth/hci_core.h |  2 ++
 net/bluetooth/hci_core.c         | 42 ++++++++++++++++++++++++++++++++++++++++
 net/bluetooth/hci_event.c        |  9 ++++++---
 4 files changed, 51 insertions(+), 3 deletions(-)
include/net/bluetooth/hci.h +1 -0
index e56f909..2f0bce2 100644
@@ -203,6 +203,7 @@ enum {
        HCI_FAST_CONNECTABLE,
        HCI_BREDR_ENABLED,
        HCI_LE_SCAN_INTERRUPTED,
+       HCI_LE_SCAN_RESTARTING,
 };
 
 /* A mask for the flags that are supposed to remain when a reset happens
include/net/bluetooth/hci_core.h +2 -0
index 396c098..36211f9 100644
@@ -337,6 +337,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];
@@ -1403,6 +1404,7 @@ int hci_update_random_address(struct hci_request *req, bool require_privacy,
                              u8 *own_addr_type);
 void hci_copy_identity_address(struct hci_dev *hdev, bdaddr_t *bdaddr,
                               u8 *bdaddr_type);
+void hci_restart_le_scan(struct hci_dev *hdev, unsigned long delay);
 
 #define SCO_AIRMODE_MASK       0x0003
 #define SCO_AIRMODE_CVSD       0x0000
net/bluetooth/hci_core.c +3 -0
index 5c319a4..858cf54 100644
@@ -2557,6 +2557,7 @@ static int hci_dev_do_close(struct hci_dev *hdev)
                cancel_delayed_work(&hdev->service_cache);
 
        cancel_delayed_work_sync(&hdev->le_scan_disable);
+       cancel_delayed_work_sync(&hdev->le_scan_restart);
 
        if (test_bit(HCI_MGMT, &hdev->dev_flags))
                cancel_delayed_work_sync(&hdev->rpa_expired);
@@ -3851,6 +3852,8 @@ static void le_scan_disable_work(struct work_struct *work)
 
        BT_DBG("%s", hdev->name);
 
+       cancel_delayed_work_sync(&hdev->le_scan_restart);
+
        hci_req_init(&req, hdev);
 
        hci_req_add_le_scan_disable(&req);

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

Quoted 7 lines
net/bluetooth/hci_core.c +4 -0
@@ -3860,6 +3863,44 @@ static void le_scan_disable_work(struct work_struct *work)
                BT_ERR("Disable LE scanning request failed: err %d", err);
 }
 
+void hci_restart_le_scan(struct hci_dev *hdev, unsigned long delay)
+{
+       queue_delayed_work(hdev->workqueue, &hdev->le_scan_restart, delay);
+}

Since you're exposing this as a public API, does it make sense to check here if a delayed work has already been scheduled? I.e. is it OK if this function gets called several times in succession with different delay values?

Quoted 69 lines
net/bluetooth/hci_core.c +35 -0
+
+static void le_scan_restart_work_complete(struct hci_dev *hdev, u8 status)
+{
+       clear_bit(HCI_LE_SCAN_RESTARTING, &hdev->dev_flags);
+
+       if (status)
+               BT_ERR("Failed to restart LE scanning: status %d", status);
+}
+
+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);
+
+       hci_req_init(&req, hdev);
+
+       hci_req_add_le_scan_disable(&req);
+
+       memset(&cp, 0, sizeof(cp));
+       cp.enable = LE_SCAN_ENABLE;
+       hci_req_add(&req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(cp), &cp);
+
+       set_bit(HCI_LE_SCAN_RESTARTING, &hdev->dev_flags);
+
+       err = hci_req_run(&req, le_scan_restart_work_complete);
+       if (err)
+               BT_ERR("Disable LE scanning request failed: err %d", err);
+}
+
 static void set_random_addr(struct hci_request *req, bdaddr_t *rpa)
 {
        struct hci_dev *hdev = req->hdev;
@@ -4037,6 +4078,7 @@ struct hci_dev *hci_alloc_dev(void)
        INIT_DELAYED_WORK(&hdev->power_off, hci_power_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_restart, le_scan_restart_work);
 
        skb_queue_head_init(&hdev->rx_q);
        skb_queue_head_init(&hdev->cmd_q);
net/bluetooth/hci_event.c +6 -4
index bd0a801..ed4d5e1 100644
@@ -1157,10 +1157,13 @@ static void hci_cc_le_set_scan_enable(struct hci_dev *hdev,
                                          d->last_adv_data_len, NULL, 0);
                }
 
-               /* Cancel this timer so that we don't try to disable scanning
-                * when it's already disabled.
+               /* If HCI_LE_SCAN_RESTARTING is set, don't cancel this timer,
+                * because we're just restarting scan. Otherwise cancel it so
+                * that we don't try to disable scanning when it's already
+                * disabled.
                 */
-               cancel_delayed_work(&hdev->le_scan_disable);
+               if (!test_bit(HCI_LE_SCAN_RESTARTING, &hdev->dev_flags))
+                       cancel_delayed_work(&hdev->le_scan_disable);
 
                clear_bit(HCI_LE_SCAN, &hdev->dev_flags);
 
--

2.1.0.rc2.206.gedb03e5

Quoted 4 lines

--
To unsubscribe from this list: send the line "unsubscribe linux-bluetooth" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Thanks,
Arman

#2 lore raw

Re: [PATCH v7 2/3] Bluetooth: Extract generic start and stop discovery

In reply to Jakub Pawlowski

Hi Jakub,

Quoted 35 lines · Jakub Pawlowski

This commit extract generic_start_discovery and generic_stop_discovery in preparation for start and stop service discovery. The reason behind that is that both functions will share big part of code, and it would be much easier to maintain just one generic method.

---
 net/bluetooth/mgmt.c | 147 ++++++++++++++++++++++++++++++---------------------
 1 file changed, 86 insertions(+), 61 deletions(-)
net/bluetooth/mgmt.c +3 -2
index cbeef5f..f650d30 100644
@@ -3675,7 +3675,8 @@ done:
        return err;
 }
 
-static int mgmt_start_discovery_failed(struct hci_dev *hdev, u8 status)
+static int mgmt_start_discovery_failed(struct hci_dev *hdev, u8 status,
+                                      uint16_t opcode)
 {
        struct pending_cmd *cmd;
        u8 type;
@@ -3683,7 +3684,7 @@ static int mgmt_start_discovery_failed(struct hci_dev *hdev, u8 status)
 
        hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
 
-       cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev);
+       cmd = mgmt_pending_find(opcode, hdev);
        if (!cmd)
                return -ENOENT;
 

net/bluetooth/mgmt.c static int mgmt_start_discovery_failed(struct hci_dev *hdev, u8 status) · line 3701

Quoted 6 lines
net/bluetooth/mgmt.c +2 -1
@@ -3696,7 +3697,8 @@ static int mgmt_start_discovery_failed(struct hci_dev *hdev, u8 status)
        return err;
 }
 
-static void start_discovery_complete(struct hci_dev *hdev, u8 status)
+static void generic_start_discovery_complete(struct hci_dev *hdev, u8 status,
+                                            uint16_t opcode)

I'm not that big on calling these generic functions "generic_*". Let's try to come up with something better.

Quoted 30 lines
net/bluetooth/mgmt.c +8 -3
 {
        unsigned long timeout = 0;
 
@@ -3704,7 +3706,7 @@ static void start_discovery_complete(struct hci_dev *hdev, u8 status)
 
        if (status) {
                hci_dev_lock(hdev);
-               mgmt_start_discovery_failed(hdev, status);
+               mgmt_start_discovery_failed(hdev, status, opcode);
                hci_dev_unlock(hdev);
                return;
        }
@@ -3735,8 +3737,13 @@ static void start_discovery_complete(struct hci_dev *hdev, u8 status)
        queue_delayed_work(hdev->workqueue, &hdev->le_scan_disable, timeout);
 }
 
-static int start_discovery(struct sock *sk, struct hci_dev *hdev,
-                          void *data, u16 len)
+static void start_discovery_complete(struct hci_dev *hdev, u8 status)
+{
+       generic_start_discovery_complete(hdev, status, MGMT_OP_START_DISCOVERY);
+}
+
+static int generic_start_discovery(struct sock *sk, struct hci_dev *hdev,
+                                  void *data, u16 len, uint16_t opcode)
 {
        struct mgmt_cp_start_discovery *cp = data;
        struct pending_cmd *cmd;
@@ -3746,41 +3753,41 @@ static int start_discovery(struct sock *sk, struct hci_dev *hdev,
        struct hci_request req;

net/bluetooth/mgmt.c static int start_discovery(struct sock *sk, struct hci_dev *hdev, · line 3762

Quoted 10 lines
net/bluetooth/mgmt.c +3 -1
        /* General inquiry access code (GIAC) */
        u8 lap[3] = { 0x33, 0x8b, 0x9e };
-       u8 status, own_addr_type;
+       u8 status, own_addr_type, type;
        int err;
 
        BT_DBG("%s", hdev->name);
 
+       type = cp->type;
+

I don't see the point of this local variable & assignment. Why not just leave it as it is, "cp->type" isn't that wordy.

Quoted 188 lines
net/bluetooth/mgmt.c +44 -43
        hci_dev_lock(hdev);
 
        if (!hdev_is_powered(hdev)) {
-               err = cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY,
-                                  MGMT_STATUS_NOT_POWERED,
-                                  &cp->type, sizeof(cp->type));
+               err = cmd_complete(sk, hdev->id, opcode,
+                                  MGMT_STATUS_NOT_POWERED, &type,
+                                  sizeof(type));
                goto failed;
        }
 
        if (test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags)) {
-               err = cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY,
-                                  MGMT_STATUS_BUSY, &cp->type,
-                                  sizeof(cp->type));
+               err = cmd_complete(sk, hdev->id, opcode,
+                                  MGMT_STATUS_BUSY, &type, sizeof(type));
                goto failed;
        }
 
        if (hdev->discovery.state != DISCOVERY_STOPPED) {
-               err = cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY,
-                                  MGMT_STATUS_BUSY, &cp->type,
-                                  sizeof(cp->type));
+               err = cmd_complete(sk, hdev->id, opcode,
+                                  MGMT_STATUS_BUSY, &type, sizeof(type));
                goto failed;
        }
 
-       cmd = mgmt_pending_add(sk, MGMT_OP_START_DISCOVERY, hdev, NULL, 0);
+       cmd = mgmt_pending_add(sk, opcode, hdev, NULL, 0);
        if (!cmd) {
                err = -ENOMEM;
                goto failed;
        }
 
-       hdev->discovery.type = cp->type;
+       hdev->discovery.type = type;
 
        hci_req_init(&req, hdev);
 
@@ -3788,18 +3795,16 @@ static int start_discovery(struct sock *sk, struct hci_dev *hdev,
        case DISCOV_TYPE_BREDR:
                status = mgmt_bredr_support(hdev);
                if (status) {
-                       err = cmd_complete(sk, hdev->id,
-                                          MGMT_OP_START_DISCOVERY, status,
-                                          &cp->type, sizeof(cp->type));
+                       err = cmd_complete(sk, hdev->id, opcode, status, &type,
+                                          sizeof(type));
                        mgmt_pending_remove(cmd);
                        goto failed;
                }
 
                if (test_bit(HCI_INQUIRY, &hdev->flags)) {
-                       err = cmd_complete(sk, hdev->id,
-                                          MGMT_OP_START_DISCOVERY,
-                                          MGMT_STATUS_BUSY, &cp->type,
-                                          sizeof(cp->type));
+                       err = cmd_complete(sk, hdev->id, opcode,
+                                          MGMT_STATUS_BUSY, &type,
+                                          sizeof(type));
                        mgmt_pending_remove(cmd);
                        goto failed;
                }
@@ -3816,19 +3821,17 @@ static int start_discovery(struct sock *sk, struct hci_dev *hdev,
        case DISCOV_TYPE_INTERLEAVED:
                status = mgmt_le_support(hdev);
                if (status) {
-                       err = cmd_complete(sk, hdev->id,
-                                          MGMT_OP_START_DISCOVERY, status,
-                                          &cp->type, sizeof(cp->type));
+                       err = cmd_complete(sk, hdev->id, opcode, status, &type,
+                                          sizeof(type));
                        mgmt_pending_remove(cmd);
                        goto failed;
                }
 
                if (hdev->discovery.type == DISCOV_TYPE_INTERLEAVED &&
                    !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) {
-                       err = cmd_complete(sk, hdev->id,
-                                          MGMT_OP_START_DISCOVERY,
-                                          MGMT_STATUS_NOT_SUPPORTED,
-                                          &cp->type, sizeof(cp->type));
+                       err = cmd_complete(sk, hdev->id, opcode,
+                                          MGMT_STATUS_NOT_SUPPORTED, &type,
+                                          sizeof(type));
                        mgmt_pending_remove(cmd);
                        goto failed;
                }
@@ -3840,11 +3843,9 @@ static int start_discovery(struct sock *sk, struct hci_dev *hdev,
                         */
                        if (hci_conn_hash_lookup_state(hdev, LE_LINK,
                                                       BT_CONNECT)) {
-                               err = cmd_complete(sk, hdev->id,
-                                                  MGMT_OP_START_DISCOVERY,
-                                                  MGMT_STATUS_REJECTED,
-                                                  &cp->type,
-                                                  sizeof(cp->type));
+                               err = cmd_complete(sk, hdev->id, opcode,
+                                                  MGMT_STATUS_REJECTED, &type,
+                                                  sizeof(type));
                                mgmt_pending_remove(cmd);
                                goto failed;
                        }
@@ -3867,10 +3868,9 @@ static int start_discovery(struct sock *sk, struct hci_dev *hdev,
                 */
                err = hci_update_random_address(&req, true, &own_addr_type);
                if (err < 0) {
-                       err = cmd_complete(sk, hdev->id,
-                                          MGMT_OP_START_DISCOVERY,
-                                          MGMT_STATUS_FAILED,
-                                          &cp->type, sizeof(cp->type));
+                       err = cmd_complete(sk, hdev->id, opcode,
+                                          MGMT_STATUS_FAILED, &type,
+                                          sizeof(type));
                        mgmt_pending_remove(cmd);
                        goto failed;
                }
@@ -3890,14 +3890,15 @@ static int start_discovery(struct sock *sk, struct hci_dev *hdev,
                break;
 
        default:
-               err = cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY,
-                                  MGMT_STATUS_INVALID_PARAMS,
-                                  &cp->type, sizeof(cp->type));
+               err = cmd_complete(sk, hdev->id, opcode,
+                                  MGMT_STATUS_INVALID_PARAMS, &type,
+                                  sizeof(type));
                mgmt_pending_remove(cmd);
                goto failed;
        }
 
        err = hci_req_run(&req, start_discovery_complete);
+
        if (err < 0)
                mgmt_pending_remove(cmd);
        else
@@ -3908,12 +3909,20 @@ failed:
        return err;
 }
 
-static int mgmt_stop_discovery_failed(struct hci_dev *hdev, u8 status)
+static int start_discovery(struct sock *sk, struct hci_dev *hdev,
+                          void *data, u16 len)
+{
+       return generic_start_discovery(sk, hdev, data, len,
+                                      MGMT_OP_START_DISCOVERY);
+}
+
+static int mgmt_stop_discovery_failed(struct hci_dev *hdev, u8 status,
+                                     uint16_t opcode)
 {
        struct pending_cmd *cmd;
        int err;
 
-       cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev);
+       cmd = mgmt_pending_find(opcode, hdev);
        if (!cmd)
                return -ENOENT;
 
@@ -3924,14 +3933,15 @@ static int mgmt_stop_discovery_failed(struct hci_dev *hdev, u8 status)
        return err;
 }
 
-static void stop_discovery_complete(struct hci_dev *hdev, u8 status)
+static void generic_stop_discovery_complete(struct hci_dev *hdev, u8 status,
+                                           uint16_t opcode)
 {
        BT_DBG("status %d", status);
 
        hci_dev_lock(hdev);
 
        if (status) {
-               mgmt_stop_discovery_failed(hdev, status);
+               mgmt_stop_discovery_failed(hdev, status, opcode);
                goto unlock;
        }
 
@@ -3941,33 +3951,40 @@ unlock:
        hci_dev_unlock(hdev);
 }
 
-static int stop_discovery(struct sock *sk, struct hci_dev *hdev, void *data,
-                         u16 len)
+static void stop_discovery_complete(struct hci_dev *hdev, u8 status)
+{

net/bluetooth/mgmt.c unlock: · line 3964

Quoted 10 lines
net/bluetooth/mgmt.c +6 -1
+       generic_stop_discovery_complete(hdev, status, MGMT_OP_STOP_DISCOVERY);
+}
+
+static int generic_stop_discovery(struct sock *sk, struct hci_dev *hdev,
+                                 void *data, u16 len, uint16_t opcode)
 {
-       struct mgmt_cp_stop_discovery *mgmt_cp = data;
        struct pending_cmd *cmd;
        struct hci_request req;
+       struct mgmt_cp_stop_discovery *mgmt_cp = data;

Why move this?

net/bluetooth/mgmt.c unlock: · line 3967

Quoted 3 lines
net/bluetooth/mgmt.c +1 -0
        int err;
+       u8 type;
 

Again, there's no need for this variable.

Quoted 67 lines
net/bluetooth/mgmt.c +19 -11
        BT_DBG("%s", hdev->name);
 
+       type = mgmt_cp->type;
+
        hci_dev_lock(hdev);
 
        if (!hci_discovery_active(hdev)) {
-               err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY,
-                                  MGMT_STATUS_REJECTED, &mgmt_cp->type,
-                                  sizeof(mgmt_cp->type));
+               err = cmd_complete(sk, hdev->id, opcode,
+                                  MGMT_STATUS_REJECTED, &type, sizeof(type));
                goto unlock;
        }
 
-       if (hdev->discovery.type != mgmt_cp->type) {
-               err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY,
-                                  MGMT_STATUS_INVALID_PARAMS, &mgmt_cp->type,
-                                  sizeof(mgmt_cp->type));
+       if (hdev->discovery.type != type) {
+               err = cmd_complete(sk, hdev->id, opcode,
+                                  MGMT_STATUS_INVALID_PARAMS, &type,
+                                  sizeof(type));
                goto unlock;
        }
 
-       cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, NULL, 0);
+       cmd = mgmt_pending_add(sk, opcode, hdev, NULL, 0);
        if (!cmd) {
                err = -ENOMEM;
                goto unlock;
@@ -3978,6 +3995,7 @@ static int stop_discovery(struct sock *sk, struct hci_dev *hdev, void *data,
        hci_stop_discovery(&req);
 
        err = hci_req_run(&req, stop_discovery_complete);
+
        if (!err) {
                hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
                goto unlock;
@@ -3987,8 +4005,8 @@ static int stop_discovery(struct sock *sk, struct hci_dev *hdev, void *data,
 
        /* If no HCI commands were sent we're done */
        if (err == -ENODATA) {
-               err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, 0,
-                                  &mgmt_cp->type, sizeof(mgmt_cp->type));
+               err = cmd_complete(sk, hdev->id, opcode, 0,
+                                  &type, sizeof(type));
                hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
        }
 
@@ -3997,6 +4015,13 @@ unlock:
        return err;
 }
 
+static int stop_discovery(struct sock *sk, struct hci_dev *hdev, void *data,
+                         u16 len)
+{
+       return generic_stop_discovery(sk, hdev, data, len,
+                                     MGMT_OP_STOP_DISCOVERY);
+}
+
 static int confirm_name(struct sock *sk, struct hci_dev *hdev, void *data,
                        u16 len)
 {
--

2.1.0.rc2.206.gedb03e5

Quoted 4 lines

--
To unsubscribe from this list: send the line "unsubscribe linux-bluetooth" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Overall I think this patch looks good, except for a few nits that I commented on above.

Thanks,
Arman

#8 lore raw
In reply to Arman Uguray
Quoted 73 lines · Arman Uguray

Hi Jakub,

Quoted 71 lines · Jakub Pawlowski

Currently there is no way to restart le scan. It's needed in preparation for new service scan method. The way it work: it disable, and then enable le scan on controller. During this restart special flag is set to make sure we won't remove disable scan work from workqueue.

---
 include/net/bluetooth/hci.h      |  1 +
 include/net/bluetooth/hci_core.h |  2 ++
 net/bluetooth/hci_core.c         | 42 ++++++++++++++++++++++++++++++++++++++++
 net/bluetooth/hci_event.c        |  9 ++++++---
 4 files changed, 51 insertions(+), 3 deletions(-)
include/net/bluetooth/hci.h +1 -0
index e56f909..2f0bce2 100644
@@ -203,6 +203,7 @@ enum {
        HCI_FAST_CONNECTABLE,
        HCI_BREDR_ENABLED,
        HCI_LE_SCAN_INTERRUPTED,
+       HCI_LE_SCAN_RESTARTING,
 };
 
 /* A mask for the flags that are supposed to remain when a reset happens
include/net/bluetooth/hci_core.h +2 -0
index 396c098..36211f9 100644
@@ -337,6 +337,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];
@@ -1403,6 +1404,7 @@ int hci_update_random_address(struct hci_request *req, bool require_privacy,
                              u8 *own_addr_type);
 void hci_copy_identity_address(struct hci_dev *hdev, bdaddr_t *bdaddr,
                               u8 *bdaddr_type);
+void hci_restart_le_scan(struct hci_dev *hdev, unsigned long delay);
 
 #define SCO_AIRMODE_MASK       0x0003
 #define SCO_AIRMODE_CVSD       0x0000
net/bluetooth/hci_core.c +3 -0
index 5c319a4..858cf54 100644
@@ -2557,6 +2557,7 @@ static int hci_dev_do_close(struct hci_dev *hdev)
                cancel_delayed_work(&hdev->service_cache);
 
        cancel_delayed_work_sync(&hdev->le_scan_disable);
+       cancel_delayed_work_sync(&hdev->le_scan_restart);
 
        if (test_bit(HCI_MGMT, &hdev->dev_flags))
                cancel_delayed_work_sync(&hdev->rpa_expired);
@@ -3851,6 +3852,8 @@ static void le_scan_disable_work(struct work_struct *work)
 
        BT_DBG("%s", hdev->name);
 
+       cancel_delayed_work_sync(&hdev->le_scan_restart);
+
        hci_req_init(&req, hdev);
 
        hci_req_add_le_scan_disable(&req);
@@ -3860,6 +3863,44 @@ static void le_scan_disable_work(struct work_struct *work)
                BT_ERR("Disable LE scanning request failed: err %d", err);
 }
 
Quoted 9 lines

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

Quoted 4 lines
net/bluetooth/hci_core.c +4 -0
+void hci_restart_le_scan(struct hci_dev *hdev, unsigned long delay)
+{
+       queue_delayed_work(hdev->workqueue, &hdev->le_scan_restart, delay);
+}

Since you're exposing this as a public API, does it make sense to check here if a delayed work has already been scheduled? I.e. is it OK if this function gets called several times in succession with different delay values?

Next patch introduces DISCOV_LE_RESTART_DELAY msecs_to_jiffies(300) which is the only delay value used to schedule le_scan_restart right now. I might change the implementation to:

void hci_restart_le_scan(struct hci_dev *hdev)

{
       queue_delayed_work(hdev->workqueue, &hdev->le_scan_restart,

DISCOV_LE_RESTART_DELAY);
}

Quoted 77 lines
Quoted 70 lines
net/bluetooth/hci_core.c +35 -0
+
+static void le_scan_restart_work_complete(struct hci_dev *hdev, u8 status)
+{
+       clear_bit(HCI_LE_SCAN_RESTARTING, &hdev->dev_flags);
+
+       if (status)
+               BT_ERR("Failed to restart LE scanning: status %d", status);
+}
+
+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);
+
+       hci_req_init(&req, hdev);
+
+       hci_req_add_le_scan_disable(&req);
+
+       memset(&cp, 0, sizeof(cp));
+       cp.enable = LE_SCAN_ENABLE;
+       hci_req_add(&req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(cp), &cp);
+
+       set_bit(HCI_LE_SCAN_RESTARTING, &hdev->dev_flags);
+
+       err = hci_req_run(&req, le_scan_restart_work_complete);
+       if (err)
+               BT_ERR("Disable LE scanning request failed: err %d", err);
+}
+
 static void set_random_addr(struct hci_request *req, bdaddr_t *rpa)
 {
        struct hci_dev *hdev = req->hdev;
@@ -4037,6 +4078,7 @@ struct hci_dev *hci_alloc_dev(void)
        INIT_DELAYED_WORK(&hdev->power_off, hci_power_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_restart, le_scan_restart_work);
 
        skb_queue_head_init(&hdev->rx_q);
        skb_queue_head_init(&hdev->cmd_q);
net/bluetooth/hci_event.c +6 -4
index bd0a801..ed4d5e1 100644
@@ -1157,10 +1157,13 @@ static void hci_cc_le_set_scan_enable(struct hci_dev *hdev,
                                          d->last_adv_data_len, NULL, 0);
                }
 
-               /* Cancel this timer so that we don't try to disable scanning
-                * when it's already disabled.
+               /* If HCI_LE_SCAN_RESTARTING is set, don't cancel this timer,
+                * because we're just restarting scan. Otherwise cancel it so
+                * that we don't try to disable scanning when it's already
+                * disabled.
                 */
-               cancel_delayed_work(&hdev->le_scan_disable);
+               if (!test_bit(HCI_LE_SCAN_RESTARTING, &hdev->dev_flags))
+                       cancel_delayed_work(&hdev->le_scan_disable);
 
                clear_bit(HCI_LE_SCAN, &hdev->dev_flags);
 
--

2.1.0.rc2.206.gedb03e5

Quoted 4 lines

--
To unsubscribe from this list: send the line "unsubscribe linux-bluetooth" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Thanks,
Arman

#3 lore raw

Re: [PATCH v7 2/3] Bluetooth: Extract generic start and stop discovery

In reply to Arman Uguray
Quoted 83 lines · Arman Uguray

Hi Jakub,

Quoted 35 lines · Jakub Pawlowski

This commit extract generic_start_discovery and generic_stop_discovery in preparation for start and stop service discovery. The reason behind that is that both functions will share big part of code, and it would be much easier to maintain just one generic method.

---
 net/bluetooth/mgmt.c | 147 ++++++++++++++++++++++++++++++---------------------
 1 file changed, 86 insertions(+), 61 deletions(-)
net/bluetooth/mgmt.c +3 -2
index cbeef5f..f650d30 100644
@@ -3675,7 +3675,8 @@ done:
        return err;
 }
 
-static int mgmt_start_discovery_failed(struct hci_dev *hdev, u8 status)
+static int mgmt_start_discovery_failed(struct hci_dev *hdev, u8 status,
+                                      uint16_t opcode)
 {
        struct pending_cmd *cmd;
        u8 type;
@@ -3683,7 +3684,7 @@ static int mgmt_start_discovery_failed(struct hci_dev *hdev, u8 status)
 
        hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
 
-       cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev);
+       cmd = mgmt_pending_find(opcode, hdev);
        if (!cmd)
                return -ENOENT;
 

net/bluetooth/mgmt.c static int mgmt_start_discovery_failed(struct hci_dev *hdev, u8 status) · line 3701

Quoted 6 lines
net/bluetooth/mgmt.c +2 -1
@@ -3696,7 +3697,8 @@ static int mgmt_start_discovery_failed(struct hci_dev *hdev, u8 status)
        return err;
 }
 
-static void start_discovery_complete(struct hci_dev *hdev, u8 status)
+static void generic_start_discovery_complete(struct hci_dev *hdev, u8 status,
+                                            uint16_t opcode)

I'm not that big on calling these generic functions "generic_*". Let's try to come up with something better.

Quoted 36 lines
net/bluetooth/mgmt.c +9 -4
 {
        unsigned long timeout = 0;
 
@@ -3704,7 +3706,7 @@ static void start_discovery_complete(struct hci_dev *hdev, u8 status)
 
        if (status) {
                hci_dev_lock(hdev);
-               mgmt_start_discovery_failed(hdev, status);
+               mgmt_start_discovery_failed(hdev, status, opcode);
                hci_dev_unlock(hdev);
                return;
        }
@@ -3735,8 +3737,13 @@ static void start_discovery_complete(struct hci_dev *hdev, u8 status)
        queue_delayed_work(hdev->workqueue, &hdev->le_scan_disable, timeout);
 }
 
-static int start_discovery(struct sock *sk, struct hci_dev *hdev,
-                          void *data, u16 len)
+static void start_discovery_complete(struct hci_dev *hdev, u8 status)
+{
+       generic_start_discovery_complete(hdev, status, MGMT_OP_START_DISCOVERY);
+}
+
+static int generic_start_discovery(struct sock *sk, struct hci_dev *hdev,
+                                  void *data, u16 len, uint16_t opcode)
 {
        struct mgmt_cp_start_discovery *cp = data;
        struct pending_cmd *cmd;
@@ -3746,41 +3753,41 @@ static int start_discovery(struct sock *sk, struct hci_dev *hdev,
        struct hci_request req;
        /* General inquiry access code (GIAC) */
        u8 lap[3] = { 0x33, 0x8b, 0x9e };
-       u8 status, own_addr_type;
+       u8 status, own_addr_type, type;
        int err;
 
Quoted 7 lines

net/bluetooth/mgmt.c static int start_discovery(struct sock *sk, struct hci_dev *hdev, · line 3762

Quoted 4 lines
net/bluetooth/mgmt.c +2 -0
        BT_DBG("%s", hdev->name);
 
+       type = cp->type;
+

I don't see the point of this local variable & assignment. Why not just leave it as it is, "cp->type" isn't that wordy.

So this patch is preparation for next patch which will have if/else statement here that would pick proper type for different cases. The next patch is already big so I wanted to make this change from cp->type to type in this one. If that's a bad idea I'll move that to next patch.

Quoted 185 lines
Quoted 185 lines
net/bluetooth/mgmt.c +42 -41
        hci_dev_lock(hdev);
 
        if (!hdev_is_powered(hdev)) {
-               err = cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY,
-                                  MGMT_STATUS_NOT_POWERED,
-                                  &cp->type, sizeof(cp->type));
+               err = cmd_complete(sk, hdev->id, opcode,
+                                  MGMT_STATUS_NOT_POWERED, &type,
+                                  sizeof(type));
                goto failed;
        }
 
        if (test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags)) {
-               err = cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY,
-                                  MGMT_STATUS_BUSY, &cp->type,
-                                  sizeof(cp->type));
+               err = cmd_complete(sk, hdev->id, opcode,
+                                  MGMT_STATUS_BUSY, &type, sizeof(type));
                goto failed;
        }
 
        if (hdev->discovery.state != DISCOVERY_STOPPED) {
-               err = cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY,
-                                  MGMT_STATUS_BUSY, &cp->type,
-                                  sizeof(cp->type));
+               err = cmd_complete(sk, hdev->id, opcode,
+                                  MGMT_STATUS_BUSY, &type, sizeof(type));
                goto failed;
        }
 
-       cmd = mgmt_pending_add(sk, MGMT_OP_START_DISCOVERY, hdev, NULL, 0);
+       cmd = mgmt_pending_add(sk, opcode, hdev, NULL, 0);
        if (!cmd) {
                err = -ENOMEM;
                goto failed;
        }
 
-       hdev->discovery.type = cp->type;
+       hdev->discovery.type = type;
 
        hci_req_init(&req, hdev);
 
@@ -3788,18 +3795,16 @@ static int start_discovery(struct sock *sk, struct hci_dev *hdev,
        case DISCOV_TYPE_BREDR:
                status = mgmt_bredr_support(hdev);
                if (status) {
-                       err = cmd_complete(sk, hdev->id,
-                                          MGMT_OP_START_DISCOVERY, status,
-                                          &cp->type, sizeof(cp->type));
+                       err = cmd_complete(sk, hdev->id, opcode, status, &type,
+                                          sizeof(type));
                        mgmt_pending_remove(cmd);
                        goto failed;
                }
 
                if (test_bit(HCI_INQUIRY, &hdev->flags)) {
-                       err = cmd_complete(sk, hdev->id,
-                                          MGMT_OP_START_DISCOVERY,
-                                          MGMT_STATUS_BUSY, &cp->type,
-                                          sizeof(cp->type));
+                       err = cmd_complete(sk, hdev->id, opcode,
+                                          MGMT_STATUS_BUSY, &type,
+                                          sizeof(type));
                        mgmt_pending_remove(cmd);
                        goto failed;
                }
@@ -3816,19 +3821,17 @@ static int start_discovery(struct sock *sk, struct hci_dev *hdev,
        case DISCOV_TYPE_INTERLEAVED:
                status = mgmt_le_support(hdev);
                if (status) {
-                       err = cmd_complete(sk, hdev->id,
-                                          MGMT_OP_START_DISCOVERY, status,
-                                          &cp->type, sizeof(cp->type));
+                       err = cmd_complete(sk, hdev->id, opcode, status, &type,
+                                          sizeof(type));
                        mgmt_pending_remove(cmd);
                        goto failed;
                }
 
                if (hdev->discovery.type == DISCOV_TYPE_INTERLEAVED &&
                    !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) {
-                       err = cmd_complete(sk, hdev->id,
-                                          MGMT_OP_START_DISCOVERY,
-                                          MGMT_STATUS_NOT_SUPPORTED,
-                                          &cp->type, sizeof(cp->type));
+                       err = cmd_complete(sk, hdev->id, opcode,
+                                          MGMT_STATUS_NOT_SUPPORTED, &type,
+                                          sizeof(type));
                        mgmt_pending_remove(cmd);
                        goto failed;
                }
@@ -3840,11 +3843,9 @@ static int start_discovery(struct sock *sk, struct hci_dev *hdev,
                         */
                        if (hci_conn_hash_lookup_state(hdev, LE_LINK,
                                                       BT_CONNECT)) {
-                               err = cmd_complete(sk, hdev->id,
-                                                  MGMT_OP_START_DISCOVERY,
-                                                  MGMT_STATUS_REJECTED,
-                                                  &cp->type,
-                                                  sizeof(cp->type));
+                               err = cmd_complete(sk, hdev->id, opcode,
+                                                  MGMT_STATUS_REJECTED, &type,
+                                                  sizeof(type));
                                mgmt_pending_remove(cmd);
                                goto failed;
                        }
@@ -3867,10 +3868,9 @@ static int start_discovery(struct sock *sk, struct hci_dev *hdev,
                 */
                err = hci_update_random_address(&req, true, &own_addr_type);
                if (err < 0) {
-                       err = cmd_complete(sk, hdev->id,
-                                          MGMT_OP_START_DISCOVERY,
-                                          MGMT_STATUS_FAILED,
-                                          &cp->type, sizeof(cp->type));
+                       err = cmd_complete(sk, hdev->id, opcode,
+                                          MGMT_STATUS_FAILED, &type,
+                                          sizeof(type));
                        mgmt_pending_remove(cmd);
                        goto failed;
                }
@@ -3890,14 +3890,15 @@ static int start_discovery(struct sock *sk, struct hci_dev *hdev,
                break;
 
        default:
-               err = cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY,
-                                  MGMT_STATUS_INVALID_PARAMS,
-                                  &cp->type, sizeof(cp->type));
+               err = cmd_complete(sk, hdev->id, opcode,
+                                  MGMT_STATUS_INVALID_PARAMS, &type,
+                                  sizeof(type));
                mgmt_pending_remove(cmd);
                goto failed;
        }
 
        err = hci_req_run(&req, start_discovery_complete);
+
        if (err < 0)
                mgmt_pending_remove(cmd);
        else
@@ -3908,12 +3909,20 @@ failed:
        return err;
 }
 
-static int mgmt_stop_discovery_failed(struct hci_dev *hdev, u8 status)
+static int start_discovery(struct sock *sk, struct hci_dev *hdev,
+                          void *data, u16 len)
+{
+       return generic_start_discovery(sk, hdev, data, len,
+                                      MGMT_OP_START_DISCOVERY);
+}
+
+static int mgmt_stop_discovery_failed(struct hci_dev *hdev, u8 status,
+                                     uint16_t opcode)
 {
        struct pending_cmd *cmd;
        int err;
 
-       cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev);
+       cmd = mgmt_pending_find(opcode, hdev);
        if (!cmd)
                return -ENOENT;
 
@@ -3924,14 +3933,15 @@ static int mgmt_stop_discovery_failed(struct hci_dev *hdev, u8 status)
        return err;
 }
 
-static void stop_discovery_complete(struct hci_dev *hdev, u8 status)
+static void generic_stop_discovery_complete(struct hci_dev *hdev, u8 status,
+                                           uint16_t opcode)
 {
        BT_DBG("status %d", status);
 
        hci_dev_lock(hdev);
 
        if (status) {
-               mgmt_stop_discovery_failed(hdev, status);
+               mgmt_stop_discovery_failed(hdev, status, opcode);
                goto unlock;
        }
 
@@ -3941,33 +3951,40 @@ unlock:
        hci_dev_unlock(hdev);
 }
 
Quoted 16 lines
Quoted 4 lines
net/bluetooth/mgmt.c +2 -2
-static int stop_discovery(struct sock *sk, struct hci_dev *hdev, void *data,
-                         u16 len)
+static void stop_discovery_complete(struct hci_dev *hdev, u8 status)
+{

net/bluetooth/mgmt.c unlock: · line 3964

Quoted 10 lines
net/bluetooth/mgmt.c +6 -1
+       generic_stop_discovery_complete(hdev, status, MGMT_OP_STOP_DISCOVERY);
+}
+
+static int generic_stop_discovery(struct sock *sk, struct hci_dev *hdev,
+                                 void *data, u16 len, uint16_t opcode)
 {
-       struct mgmt_cp_stop_discovery *mgmt_cp = data;
        struct pending_cmd *cmd;
        struct hci_request req;
+       struct mgmt_cp_stop_discovery *mgmt_cp = data;

Why move this?

I'll fix that

Quoted 6 lines

net/bluetooth/mgmt.c unlock: · line 3967

Quoted 4 lines
net/bluetooth/mgmt.c +1 -0
        int err;
+       u8 type;
 

Again, there's no need for this variable.

same as aboce - this patch is preparation for next patch which will have if/else statement here that would pick proper type for different cases.

Quoted 78 lines
Quoted 68 lines
net/bluetooth/mgmt.c +19 -11
        BT_DBG("%s", hdev->name);
 
+       type = mgmt_cp->type;
+
        hci_dev_lock(hdev);
 
        if (!hci_discovery_active(hdev)) {
-               err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY,
-                                  MGMT_STATUS_REJECTED, &mgmt_cp->type,
-                                  sizeof(mgmt_cp->type));
+               err = cmd_complete(sk, hdev->id, opcode,
+                                  MGMT_STATUS_REJECTED, &type, sizeof(type));
                goto unlock;
        }
 
-       if (hdev->discovery.type != mgmt_cp->type) {
-               err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY,
-                                  MGMT_STATUS_INVALID_PARAMS, &mgmt_cp->type,
-                                  sizeof(mgmt_cp->type));
+       if (hdev->discovery.type != type) {
+               err = cmd_complete(sk, hdev->id, opcode,
+                                  MGMT_STATUS_INVALID_PARAMS, &type,
+                                  sizeof(type));
                goto unlock;
        }
 
-       cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, NULL, 0);
+       cmd = mgmt_pending_add(sk, opcode, hdev, NULL, 0);
        if (!cmd) {
                err = -ENOMEM;
                goto unlock;
@@ -3978,6 +3995,7 @@ static int stop_discovery(struct sock *sk, struct hci_dev *hdev, void *data,
        hci_stop_discovery(&req);
 
        err = hci_req_run(&req, stop_discovery_complete);
+
        if (!err) {
                hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
                goto unlock;
@@ -3987,8 +4005,8 @@ static int stop_discovery(struct sock *sk, struct hci_dev *hdev, void *data,
 
        /* If no HCI commands were sent we're done */
        if (err == -ENODATA) {
-               err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, 0,
-                                  &mgmt_cp->type, sizeof(mgmt_cp->type));
+               err = cmd_complete(sk, hdev->id, opcode, 0,
+                                  &type, sizeof(type));
                hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
        }
 
@@ -3997,6 +4015,13 @@ unlock:
        return err;
 }
 
+static int stop_discovery(struct sock *sk, struct hci_dev *hdev, void *data,
+                         u16 len)
+{
+       return generic_stop_discovery(sk, hdev, data, len,
+                                     MGMT_OP_STOP_DISCOVERY);
+}
+
 static int confirm_name(struct sock *sk, struct hci_dev *hdev, void *data,
                        u16 len)
 {
--

2.1.0.rc2.206.gedb03e5

Quoted 4 lines

--
To unsubscribe from this list: send the line "unsubscribe linux-bluetooth" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Overall I think this patch looks good, except for a few nits that I commented on above.

Thanks,
Arman

#9 lore raw
In reply to Jakub Pawlowski

Hi Jakub,

Quoted 89 lines · Jakub Pawlowski
Quoted 73 lines · Arman Uguray

Hi Jakub,

Quoted 71 lines · Jakub Pawlowski

Currently there is no way to restart le scan. It's needed in preparation for new service scan method. The way it work: it disable, and then enable le scan on controller. During this restart special flag is set to make sure we won't remove disable scan work from workqueue.

---
 include/net/bluetooth/hci.h      |  1 +
 include/net/bluetooth/hci_core.h |  2 ++
 net/bluetooth/hci_core.c         | 42 ++++++++++++++++++++++++++++++++++++++++
 net/bluetooth/hci_event.c        |  9 ++++++---
 4 files changed, 51 insertions(+), 3 deletions(-)
include/net/bluetooth/hci.h +1 -0
index e56f909..2f0bce2 100644
@@ -203,6 +203,7 @@ enum {
        HCI_FAST_CONNECTABLE,
        HCI_BREDR_ENABLED,
        HCI_LE_SCAN_INTERRUPTED,
+       HCI_LE_SCAN_RESTARTING,
 };
 
 /* A mask for the flags that are supposed to remain when a reset happens
include/net/bluetooth/hci_core.h +2 -0
index 396c098..36211f9 100644
@@ -337,6 +337,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];
@@ -1403,6 +1404,7 @@ int hci_update_random_address(struct hci_request *req, bool require_privacy,
                              u8 *own_addr_type);
 void hci_copy_identity_address(struct hci_dev *hdev, bdaddr_t *bdaddr,
                               u8 *bdaddr_type);
+void hci_restart_le_scan(struct hci_dev *hdev, unsigned long delay);
 
 #define SCO_AIRMODE_MASK       0x0003
 #define SCO_AIRMODE_CVSD       0x0000
net/bluetooth/hci_core.c +3 -0
index 5c319a4..858cf54 100644
@@ -2557,6 +2557,7 @@ static int hci_dev_do_close(struct hci_dev *hdev)
                cancel_delayed_work(&hdev->service_cache);
 
        cancel_delayed_work_sync(&hdev->le_scan_disable);
+       cancel_delayed_work_sync(&hdev->le_scan_restart);
 
        if (test_bit(HCI_MGMT, &hdev->dev_flags))
                cancel_delayed_work_sync(&hdev->rpa_expired);
@@ -3851,6 +3852,8 @@ static void le_scan_disable_work(struct work_struct *work)
 
        BT_DBG("%s", hdev->name);
 
+       cancel_delayed_work_sync(&hdev->le_scan_restart);
+
        hci_req_init(&req, hdev);
 
        hci_req_add_le_scan_disable(&req);
@@ -3860,6 +3863,44 @@ static void le_scan_disable_work(struct work_struct *work)
                BT_ERR("Disable LE scanning request failed: err %d", err);
 }
 
Quoted 9 lines

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

Quoted 4 lines
net/bluetooth/hci_core.c +4 -0
+void hci_restart_le_scan(struct hci_dev *hdev, unsigned long delay)
+{
+       queue_delayed_work(hdev->workqueue, &hdev->le_scan_restart, delay);
+}

Since you're exposing this as a public API, does it make sense to check here if a delayed work has already been scheduled? I.e. is it OK if this function gets called several times in succession with different delay values?

Next patch introduces DISCOV_LE_RESTART_DELAY msecs_to_jiffies(300) which is the only delay value used to schedule le_scan_restart right now. I might change the implementation to:

Quoted 6 lines

void hci_restart_le_scan(struct hci_dev *hdev)

{
       queue_delayed_work(hdev->workqueue, &hdev->le_scan_restart,

DISCOV_LE_RESTART_DELAY);
}

Sure but even with a consistent delay value, is it OK to repeatedly call this function from other layers? Since this is publicly exposed from hci_core.h, any code within the subsystem can make calls to this. So you should make sure that it doesn't break this, or cover that edge case inside this function, if necessary.

Quoted 70 lines
Quoted 70 lines
Quoted 70 lines
net/bluetooth/hci_core.c +35 -0
+
+static void le_scan_restart_work_complete(struct hci_dev *hdev, u8 status)
+{
+       clear_bit(HCI_LE_SCAN_RESTARTING, &hdev->dev_flags);
+
+       if (status)
+               BT_ERR("Failed to restart LE scanning: status %d", status);
+}
+
+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);
+
+       hci_req_init(&req, hdev);
+
+       hci_req_add_le_scan_disable(&req);
+
+       memset(&cp, 0, sizeof(cp));
+       cp.enable = LE_SCAN_ENABLE;
+       hci_req_add(&req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(cp), &cp);
+
+       set_bit(HCI_LE_SCAN_RESTARTING, &hdev->dev_flags);
+
+       err = hci_req_run(&req, le_scan_restart_work_complete);
+       if (err)
+               BT_ERR("Disable LE scanning request failed: err %d", err);
+}
+
 static void set_random_addr(struct hci_request *req, bdaddr_t *rpa)
 {
        struct hci_dev *hdev = req->hdev;
@@ -4037,6 +4078,7 @@ struct hci_dev *hci_alloc_dev(void)
        INIT_DELAYED_WORK(&hdev->power_off, hci_power_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_restart, le_scan_restart_work);
 
        skb_queue_head_init(&hdev->rx_q);
        skb_queue_head_init(&hdev->cmd_q);
net/bluetooth/hci_event.c +6 -4
index bd0a801..ed4d5e1 100644
@@ -1157,10 +1157,13 @@ static void hci_cc_le_set_scan_enable(struct hci_dev *hdev,
                                          d->last_adv_data_len, NULL, 0);
                }
 
-               /* Cancel this timer so that we don't try to disable scanning
-                * when it's already disabled.
+               /* If HCI_LE_SCAN_RESTARTING is set, don't cancel this timer,
+                * because we're just restarting scan. Otherwise cancel it so
+                * that we don't try to disable scanning when it's already
+                * disabled.
                 */
-               cancel_delayed_work(&hdev->le_scan_disable);
+               if (!test_bit(HCI_LE_SCAN_RESTARTING, &hdev->dev_flags))
+                       cancel_delayed_work(&hdev->le_scan_disable);
 
                clear_bit(HCI_LE_SCAN, &hdev->dev_flags);
 
--

2.1.0.rc2.206.gedb03e5

Quoted 7 lines
Quoted 7 lines
Quoted 4 lines

--
To unsubscribe from this list: send the line "unsubscribe linux-bluetooth" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Thanks,
Arman

Arman

#4 lore raw

Re: [PATCH v7 2/3] Bluetooth: Extract generic start and stop discovery

In reply to Jakub Pawlowski

Hi Jakub,

Quoted 93 lines · Jakub Pawlowski
Quoted 83 lines · Arman Uguray

Hi Jakub,

Quoted 35 lines · Jakub Pawlowski

This commit extract generic_start_discovery and generic_stop_discovery in preparation for start and stop service discovery. The reason behind that is that both functions will share big part of code, and it would be much easier to maintain just one generic method.

---
 net/bluetooth/mgmt.c | 147 ++++++++++++++++++++++++++++++---------------------
 1 file changed, 86 insertions(+), 61 deletions(-)
net/bluetooth/mgmt.c +3 -2
index cbeef5f..f650d30 100644
@@ -3675,7 +3675,8 @@ done:
        return err;
 }
 
-static int mgmt_start_discovery_failed(struct hci_dev *hdev, u8 status)
+static int mgmt_start_discovery_failed(struct hci_dev *hdev, u8 status,
+                                      uint16_t opcode)
 {
        struct pending_cmd *cmd;
        u8 type;
@@ -3683,7 +3684,7 @@ static int mgmt_start_discovery_failed(struct hci_dev *hdev, u8 status)
 
        hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
 
-       cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev);
+       cmd = mgmt_pending_find(opcode, hdev);
        if (!cmd)
                return -ENOENT;
 

net/bluetooth/mgmt.c static int mgmt_start_discovery_failed(struct hci_dev *hdev, u8 status) · line 3701

Quoted 6 lines
net/bluetooth/mgmt.c +2 -1
@@ -3696,7 +3697,8 @@ static int mgmt_start_discovery_failed(struct hci_dev *hdev, u8 status)
        return err;
 }
 
-static void start_discovery_complete(struct hci_dev *hdev, u8 status)
+static void generic_start_discovery_complete(struct hci_dev *hdev, u8 status,
+                                            uint16_t opcode)

I'm not that big on calling these generic functions "generic_*". Let's try to come up with something better.

Quoted 36 lines
net/bluetooth/mgmt.c +9 -4
 {
        unsigned long timeout = 0;
 
@@ -3704,7 +3706,7 @@ static void start_discovery_complete(struct hci_dev *hdev, u8 status)
 
        if (status) {
                hci_dev_lock(hdev);
-               mgmt_start_discovery_failed(hdev, status);
+               mgmt_start_discovery_failed(hdev, status, opcode);
                hci_dev_unlock(hdev);
                return;
        }
@@ -3735,8 +3737,13 @@ static void start_discovery_complete(struct hci_dev *hdev, u8 status)
        queue_delayed_work(hdev->workqueue, &hdev->le_scan_disable, timeout);
 }
 
-static int start_discovery(struct sock *sk, struct hci_dev *hdev,
-                          void *data, u16 len)
+static void start_discovery_complete(struct hci_dev *hdev, u8 status)
+{
+       generic_start_discovery_complete(hdev, status, MGMT_OP_START_DISCOVERY);
+}
+
+static int generic_start_discovery(struct sock *sk, struct hci_dev *hdev,
+                                  void *data, u16 len, uint16_t opcode)
 {
        struct mgmt_cp_start_discovery *cp = data;
        struct pending_cmd *cmd;
@@ -3746,41 +3753,41 @@ static int start_discovery(struct sock *sk, struct hci_dev *hdev,
        struct hci_request req;
        /* General inquiry access code (GIAC) */
        u8 lap[3] = { 0x33, 0x8b, 0x9e };
-       u8 status, own_addr_type;
+       u8 status, own_addr_type, type;
        int err;
 
Quoted 7 lines

net/bluetooth/mgmt.c static int start_discovery(struct sock *sk, struct hci_dev *hdev, · line 3762

Quoted 4 lines
net/bluetooth/mgmt.c +2 -0
        BT_DBG("%s", hdev->name);
 
+       type = cp->type;
+

I don't see the point of this local variable & assignment. Why not just leave it as it is, "cp->type" isn't that wordy.

Quoted 6 lines

So this patch is preparation for next patch which will have if/else statement here that would pick proper type for different cases. The next patch is already big so I wanted to make this change from cp->type to type in this one. If that's a bad idea I'll move that to next patch.

Yeah, from here it just looks like a random refactor. I would just put it into the next patch. Or if you think that the next patch is too big, maybe think about how you can make it smaller by separating the logical steps?

Quoted 208 lines
Quoted 185 lines
Quoted 185 lines
net/bluetooth/mgmt.c +42 -41
        hci_dev_lock(hdev);
 
        if (!hdev_is_powered(hdev)) {
-               err = cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY,
-                                  MGMT_STATUS_NOT_POWERED,
-                                  &cp->type, sizeof(cp->type));
+               err = cmd_complete(sk, hdev->id, opcode,
+                                  MGMT_STATUS_NOT_POWERED, &type,
+                                  sizeof(type));
                goto failed;
        }
 
        if (test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags)) {
-               err = cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY,
-                                  MGMT_STATUS_BUSY, &cp->type,
-                                  sizeof(cp->type));
+               err = cmd_complete(sk, hdev->id, opcode,
+                                  MGMT_STATUS_BUSY, &type, sizeof(type));
                goto failed;
        }
 
        if (hdev->discovery.state != DISCOVERY_STOPPED) {
-               err = cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY,
-                                  MGMT_STATUS_BUSY, &cp->type,
-                                  sizeof(cp->type));
+               err = cmd_complete(sk, hdev->id, opcode,
+                                  MGMT_STATUS_BUSY, &type, sizeof(type));
                goto failed;
        }
 
-       cmd = mgmt_pending_add(sk, MGMT_OP_START_DISCOVERY, hdev, NULL, 0);
+       cmd = mgmt_pending_add(sk, opcode, hdev, NULL, 0);
        if (!cmd) {
                err = -ENOMEM;
                goto failed;
        }
 
-       hdev->discovery.type = cp->type;
+       hdev->discovery.type = type;
 
        hci_req_init(&req, hdev);
 
@@ -3788,18 +3795,16 @@ static int start_discovery(struct sock *sk, struct hci_dev *hdev,
        case DISCOV_TYPE_BREDR:
                status = mgmt_bredr_support(hdev);
                if (status) {
-                       err = cmd_complete(sk, hdev->id,
-                                          MGMT_OP_START_DISCOVERY, status,
-                                          &cp->type, sizeof(cp->type));
+                       err = cmd_complete(sk, hdev->id, opcode, status, &type,
+                                          sizeof(type));
                        mgmt_pending_remove(cmd);
                        goto failed;
                }
 
                if (test_bit(HCI_INQUIRY, &hdev->flags)) {
-                       err = cmd_complete(sk, hdev->id,
-                                          MGMT_OP_START_DISCOVERY,
-                                          MGMT_STATUS_BUSY, &cp->type,
-                                          sizeof(cp->type));
+                       err = cmd_complete(sk, hdev->id, opcode,
+                                          MGMT_STATUS_BUSY, &type,
+                                          sizeof(type));
                        mgmt_pending_remove(cmd);
                        goto failed;
                }
@@ -3816,19 +3821,17 @@ static int start_discovery(struct sock *sk, struct hci_dev *hdev,
        case DISCOV_TYPE_INTERLEAVED:
                status = mgmt_le_support(hdev);
                if (status) {
-                       err = cmd_complete(sk, hdev->id,
-                                          MGMT_OP_START_DISCOVERY, status,
-                                          &cp->type, sizeof(cp->type));
+                       err = cmd_complete(sk, hdev->id, opcode, status, &type,
+                                          sizeof(type));
                        mgmt_pending_remove(cmd);
                        goto failed;
                }
 
                if (hdev->discovery.type == DISCOV_TYPE_INTERLEAVED &&
                    !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) {
-                       err = cmd_complete(sk, hdev->id,
-                                          MGMT_OP_START_DISCOVERY,
-                                          MGMT_STATUS_NOT_SUPPORTED,
-                                          &cp->type, sizeof(cp->type));
+                       err = cmd_complete(sk, hdev->id, opcode,
+                                          MGMT_STATUS_NOT_SUPPORTED, &type,
+                                          sizeof(type));
                        mgmt_pending_remove(cmd);
                        goto failed;
                }
@@ -3840,11 +3843,9 @@ static int start_discovery(struct sock *sk, struct hci_dev *hdev,
                         */
                        if (hci_conn_hash_lookup_state(hdev, LE_LINK,
                                                       BT_CONNECT)) {
-                               err = cmd_complete(sk, hdev->id,
-                                                  MGMT_OP_START_DISCOVERY,
-                                                  MGMT_STATUS_REJECTED,
-                                                  &cp->type,
-                                                  sizeof(cp->type));
+                               err = cmd_complete(sk, hdev->id, opcode,
+                                                  MGMT_STATUS_REJECTED, &type,
+                                                  sizeof(type));
                                mgmt_pending_remove(cmd);
                                goto failed;
                        }
@@ -3867,10 +3868,9 @@ static int start_discovery(struct sock *sk, struct hci_dev *hdev,
                 */
                err = hci_update_random_address(&req, true, &own_addr_type);
                if (err < 0) {
-                       err = cmd_complete(sk, hdev->id,
-                                          MGMT_OP_START_DISCOVERY,
-                                          MGMT_STATUS_FAILED,
-                                          &cp->type, sizeof(cp->type));
+                       err = cmd_complete(sk, hdev->id, opcode,
+                                          MGMT_STATUS_FAILED, &type,
+                                          sizeof(type));
                        mgmt_pending_remove(cmd);
                        goto failed;
                }
@@ -3890,14 +3890,15 @@ static int start_discovery(struct sock *sk, struct hci_dev *hdev,
                break;
 
        default:
-               err = cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY,
-                                  MGMT_STATUS_INVALID_PARAMS,
-                                  &cp->type, sizeof(cp->type));
+               err = cmd_complete(sk, hdev->id, opcode,
+                                  MGMT_STATUS_INVALID_PARAMS, &type,
+                                  sizeof(type));
                mgmt_pending_remove(cmd);
                goto failed;
        }
 
        err = hci_req_run(&req, start_discovery_complete);
+
        if (err < 0)
                mgmt_pending_remove(cmd);
        else
@@ -3908,12 +3909,20 @@ failed:
        return err;
 }
 
-static int mgmt_stop_discovery_failed(struct hci_dev *hdev, u8 status)
+static int start_discovery(struct sock *sk, struct hci_dev *hdev,
+                          void *data, u16 len)
+{
+       return generic_start_discovery(sk, hdev, data, len,
+                                      MGMT_OP_START_DISCOVERY);
+}
+
+static int mgmt_stop_discovery_failed(struct hci_dev *hdev, u8 status,
+                                     uint16_t opcode)
 {
        struct pending_cmd *cmd;
        int err;
 
-       cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev);
+       cmd = mgmt_pending_find(opcode, hdev);
        if (!cmd)
                return -ENOENT;
 
@@ -3924,14 +3933,15 @@ static int mgmt_stop_discovery_failed(struct hci_dev *hdev, u8 status)
        return err;
 }
 
-static void stop_discovery_complete(struct hci_dev *hdev, u8 status)
+static void generic_stop_discovery_complete(struct hci_dev *hdev, u8 status,
+                                           uint16_t opcode)
 {
        BT_DBG("status %d", status);
 
        hci_dev_lock(hdev);
 
        if (status) {
-               mgmt_stop_discovery_failed(hdev, status);
+               mgmt_stop_discovery_failed(hdev, status, opcode);
                goto unlock;
        }
 
@@ -3941,33 +3951,40 @@ unlock:
        hci_dev_unlock(hdev);
 }
 
Quoted 16 lines
Quoted 4 lines
net/bluetooth/mgmt.c +2 -2
-static int stop_discovery(struct sock *sk, struct hci_dev *hdev, void *data,
-                         u16 len)
+static void stop_discovery_complete(struct hci_dev *hdev, u8 status)
+{

net/bluetooth/mgmt.c unlock: · line 3964

Quoted 10 lines
net/bluetooth/mgmt.c +6 -1
+       generic_stop_discovery_complete(hdev, status, MGMT_OP_STOP_DISCOVERY);
+}
+
+static int generic_stop_discovery(struct sock *sk, struct hci_dev *hdev,
+                                 void *data, u16 len, uint16_t opcode)
 {
-       struct mgmt_cp_stop_discovery *mgmt_cp = data;
        struct pending_cmd *cmd;
        struct hci_request req;
+       struct mgmt_cp_stop_discovery *mgmt_cp = data;

Why move this?

I'll fix that

Quoted 5 lines
Quoted 4 lines
net/bluetooth/mgmt.c +1 -0
        int err;
+       u8 type;
 
Quoted 84 lines
Quoted 1 line

Again, there's no need for this variable.

same as aboce - this patch is preparation for next patch which will have if/else statement here that would pick proper type for different cases.

Quoted 78 lines
Quoted 68 lines
net/bluetooth/mgmt.c +19 -11
        BT_DBG("%s", hdev->name);
 
+       type = mgmt_cp->type;
+
        hci_dev_lock(hdev);
 
        if (!hci_discovery_active(hdev)) {
-               err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY,
-                                  MGMT_STATUS_REJECTED, &mgmt_cp->type,
-                                  sizeof(mgmt_cp->type));
+               err = cmd_complete(sk, hdev->id, opcode,
+                                  MGMT_STATUS_REJECTED, &type, sizeof(type));
                goto unlock;
        }
 
-       if (hdev->discovery.type != mgmt_cp->type) {
-               err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY,
-                                  MGMT_STATUS_INVALID_PARAMS, &mgmt_cp->type,
-                                  sizeof(mgmt_cp->type));
+       if (hdev->discovery.type != type) {
+               err = cmd_complete(sk, hdev->id, opcode,
+                                  MGMT_STATUS_INVALID_PARAMS, &type,
+                                  sizeof(type));
                goto unlock;
        }
 
-       cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, NULL, 0);
+       cmd = mgmt_pending_add(sk, opcode, hdev, NULL, 0);
        if (!cmd) {
                err = -ENOMEM;
                goto unlock;
@@ -3978,6 +3995,7 @@ static int stop_discovery(struct sock *sk, struct hci_dev *hdev, void *data,
        hci_stop_discovery(&req);
 
        err = hci_req_run(&req, stop_discovery_complete);
+
        if (!err) {
                hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
                goto unlock;
@@ -3987,8 +4005,8 @@ static int stop_discovery(struct sock *sk, struct hci_dev *hdev, void *data,
 
        /* If no HCI commands were sent we're done */
        if (err == -ENODATA) {
-               err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, 0,
-                                  &mgmt_cp->type, sizeof(mgmt_cp->type));
+               err = cmd_complete(sk, hdev->id, opcode, 0,
+                                  &type, sizeof(type));
                hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
        }
 
@@ -3997,6 +4015,13 @@ unlock:
        return err;
 }
 
+static int stop_discovery(struct sock *sk, struct hci_dev *hdev, void *data,
+                         u16 len)
+{
+       return generic_stop_discovery(sk, hdev, data, len,
+                                     MGMT_OP_STOP_DISCOVERY);
+}
+
 static int confirm_name(struct sock *sk, struct hci_dev *hdev, void *data,
                        u16 len)
 {
--

2.1.0.rc2.206.gedb03e5

Quoted 4 lines

--
To unsubscribe from this list: send the line "unsubscribe linux-bluetooth" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Overall I think this patch looks good, except for a few nits that I commented on above.

Thanks,
Arman

Thanks,
Arman

#10 lore raw
In reply to Arman Uguray
Quoted 98 lines · Arman Uguray

Hi Jakub,

Quoted 89 lines · Jakub Pawlowski
Quoted 73 lines · Arman Uguray

Hi Jakub,

Quoted 71 lines · Jakub Pawlowski

Currently there is no way to restart le scan. It's needed in preparation for new service scan method. The way it work: it disable, and then enable le scan on controller. During this restart special flag is set to make sure we won't remove disable scan work from workqueue.

---
 include/net/bluetooth/hci.h      |  1 +
 include/net/bluetooth/hci_core.h |  2 ++
 net/bluetooth/hci_core.c         | 42 ++++++++++++++++++++++++++++++++++++++++
 net/bluetooth/hci_event.c        |  9 ++++++---
 4 files changed, 51 insertions(+), 3 deletions(-)
include/net/bluetooth/hci.h +1 -0
index e56f909..2f0bce2 100644
@@ -203,6 +203,7 @@ enum {
        HCI_FAST_CONNECTABLE,
        HCI_BREDR_ENABLED,
        HCI_LE_SCAN_INTERRUPTED,
+       HCI_LE_SCAN_RESTARTING,
 };
 
 /* A mask for the flags that are supposed to remain when a reset happens
include/net/bluetooth/hci_core.h +2 -0
index 396c098..36211f9 100644
@@ -337,6 +337,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];
@@ -1403,6 +1404,7 @@ int hci_update_random_address(struct hci_request *req, bool require_privacy,
                              u8 *own_addr_type);
 void hci_copy_identity_address(struct hci_dev *hdev, bdaddr_t *bdaddr,
                               u8 *bdaddr_type);
+void hci_restart_le_scan(struct hci_dev *hdev, unsigned long delay);
 
 #define SCO_AIRMODE_MASK       0x0003
 #define SCO_AIRMODE_CVSD       0x0000
net/bluetooth/hci_core.c +3 -0
index 5c319a4..858cf54 100644
@@ -2557,6 +2557,7 @@ static int hci_dev_do_close(struct hci_dev *hdev)
                cancel_delayed_work(&hdev->service_cache);
 
        cancel_delayed_work_sync(&hdev->le_scan_disable);
+       cancel_delayed_work_sync(&hdev->le_scan_restart);
 
        if (test_bit(HCI_MGMT, &hdev->dev_flags))
                cancel_delayed_work_sync(&hdev->rpa_expired);
@@ -3851,6 +3852,8 @@ static void le_scan_disable_work(struct work_struct *work)
 
        BT_DBG("%s", hdev->name);
 
+       cancel_delayed_work_sync(&hdev->le_scan_restart);
+
        hci_req_init(&req, hdev);
 
        hci_req_add_le_scan_disable(&req);
@@ -3860,6 +3863,44 @@ static void le_scan_disable_work(struct work_struct *work)
                BT_ERR("Disable LE scanning request failed: err %d", err);
 }
 
Quoted 9 lines

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

Quoted 4 lines
net/bluetooth/hci_core.c +4 -0
+void hci_restart_le_scan(struct hci_dev *hdev, unsigned long delay)
+{
+       queue_delayed_work(hdev->workqueue, &hdev->le_scan_restart, delay);
+}

Since you're exposing this as a public API, does it make sense to check here if a delayed work has already been scheduled? I.e. is it OK if this function gets called several times in succession with different delay values?

Next patch introduces DISCOV_LE_RESTART_DELAY msecs_to_jiffies(300) which is the only delay value used to schedule le_scan_restart right now. I might change the implementation to:

Quoted 6 lines

void hci_restart_le_scan(struct hci_dev *hdev)

{
       queue_delayed_work(hdev->workqueue, &hdev->le_scan_restart,

DISCOV_LE_RESTART_DELAY);
}

Quoted 5 lines

Sure but even with a consistent delay value, is it OK to repeatedly call this function from other layers? Since this is publicly exposed from hci_core.h, any code within the subsystem can make calls to this. So you should make sure that it doesn't break this, or cover that edge case inside this function, if necessary.

So queue_delayed_work when you call it first time just schedule job and returns 0, if you call it again it will return nozero value if structure was already waiting in the queue.
I actually depend on this behaviour to make sure I don't restart scan too often, maximum once every DISCOV_LE_RESTART_DELAY.

Quoted 80 lines
Quoted 71 lines
Quoted 71 lines
Quoted 71 lines
net/bluetooth/hci_core.c +35 -0
+
+static void le_scan_restart_work_complete(struct hci_dev *hdev, u8 status)
+{
+       clear_bit(HCI_LE_SCAN_RESTARTING, &hdev->dev_flags);
+
+       if (status)
+               BT_ERR("Failed to restart LE scanning: status %d", status);
+}
+
+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);
+
+       hci_req_init(&req, hdev);
+
+       hci_req_add_le_scan_disable(&req);
+
+       memset(&cp, 0, sizeof(cp));
+       cp.enable = LE_SCAN_ENABLE;
+       hci_req_add(&req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(cp), &cp);
+
+       set_bit(HCI_LE_SCAN_RESTARTING, &hdev->dev_flags);
+
+       err = hci_req_run(&req, le_scan_restart_work_complete);
+       if (err)
+               BT_ERR("Disable LE scanning request failed: err %d", err);
+}
+
 static void set_random_addr(struct hci_request *req, bdaddr_t *rpa)
 {
        struct hci_dev *hdev = req->hdev;
@@ -4037,6 +4078,7 @@ struct hci_dev *hci_alloc_dev(void)
        INIT_DELAYED_WORK(&hdev->power_off, hci_power_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_restart, le_scan_restart_work);
 
        skb_queue_head_init(&hdev->rx_q);
        skb_queue_head_init(&hdev->cmd_q);
net/bluetooth/hci_event.c +6 -4
index bd0a801..ed4d5e1 100644
@@ -1157,10 +1157,13 @@ static void hci_cc_le_set_scan_enable(struct hci_dev *hdev,
                                          d->last_adv_data_len, NULL, 0);
                }
 
-               /* Cancel this timer so that we don't try to disable scanning
-                * when it's already disabled.
+               /* If HCI_LE_SCAN_RESTARTING is set, don't cancel this timer,
+                * because we're just restarting scan. Otherwise cancel it so
+                * that we don't try to disable scanning when it's already
+                * disabled.
                 */
-               cancel_delayed_work(&hdev->le_scan_disable);
+               if (!test_bit(HCI_LE_SCAN_RESTARTING, &hdev->dev_flags))
+                       cancel_delayed_work(&hdev->le_scan_disable);
 
                clear_bit(HCI_LE_SCAN, &hdev->dev_flags);
 
--

2.1.0.rc2.206.gedb03e5

Quoted 7 lines
Quoted 7 lines
Quoted 4 lines

--
To unsubscribe from this list: send the line "unsubscribe linux-bluetooth" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Thanks,
Arman

Arman

#5 lore raw

Re: [PATCH v7 2/3] Bluetooth: Extract generic start and stop discovery

In reply to Arman Uguray
Quoted 102 lines · Arman Uguray

Hi Jakub,

Quoted 93 lines · Jakub Pawlowski
Quoted 83 lines · Arman Uguray

Hi Jakub,

Quoted 35 lines · Jakub Pawlowski

This commit extract generic_start_discovery and generic_stop_discovery in preparation for start and stop service discovery. The reason behind that is that both functions will share big part of code, and it would be much easier to maintain just one generic method.

---
 net/bluetooth/mgmt.c | 147 ++++++++++++++++++++++++++++++---------------------
 1 file changed, 86 insertions(+), 61 deletions(-)
net/bluetooth/mgmt.c +3 -2
index cbeef5f..f650d30 100644
@@ -3675,7 +3675,8 @@ done:
        return err;
 }
 
-static int mgmt_start_discovery_failed(struct hci_dev *hdev, u8 status)
+static int mgmt_start_discovery_failed(struct hci_dev *hdev, u8 status,
+                                      uint16_t opcode)
 {
        struct pending_cmd *cmd;
        u8 type;
@@ -3683,7 +3684,7 @@ static int mgmt_start_discovery_failed(struct hci_dev *hdev, u8 status)
 
        hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
 
-       cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev);
+       cmd = mgmt_pending_find(opcode, hdev);
        if (!cmd)
                return -ENOENT;
 

net/bluetooth/mgmt.c static int mgmt_start_discovery_failed(struct hci_dev *hdev, u8 status) · line 3701

Quoted 6 lines
net/bluetooth/mgmt.c +2 -1
@@ -3696,7 +3697,8 @@ static int mgmt_start_discovery_failed(struct hci_dev *hdev, u8 status)
        return err;
 }
 
-static void start_discovery_complete(struct hci_dev *hdev, u8 status)
+static void generic_start_discovery_complete(struct hci_dev *hdev, u8 status,
+                                            uint16_t opcode)

I'm not that big on calling these generic functions "generic_*". Let's try to come up with something better.

Quoted 36 lines
net/bluetooth/mgmt.c +9 -4
 {
        unsigned long timeout = 0;
 
@@ -3704,7 +3706,7 @@ static void start_discovery_complete(struct hci_dev *hdev, u8 status)
 
        if (status) {
                hci_dev_lock(hdev);
-               mgmt_start_discovery_failed(hdev, status);
+               mgmt_start_discovery_failed(hdev, status, opcode);
                hci_dev_unlock(hdev);
                return;
        }
@@ -3735,8 +3737,13 @@ static void start_discovery_complete(struct hci_dev *hdev, u8 status)
        queue_delayed_work(hdev->workqueue, &hdev->le_scan_disable, timeout);
 }
 
-static int start_discovery(struct sock *sk, struct hci_dev *hdev,
-                          void *data, u16 len)
+static void start_discovery_complete(struct hci_dev *hdev, u8 status)
+{
+       generic_start_discovery_complete(hdev, status, MGMT_OP_START_DISCOVERY);
+}
+
+static int generic_start_discovery(struct sock *sk, struct hci_dev *hdev,
+                                  void *data, u16 len, uint16_t opcode)
 {
        struct mgmt_cp_start_discovery *cp = data;
        struct pending_cmd *cmd;
@@ -3746,41 +3753,41 @@ static int start_discovery(struct sock *sk, struct hci_dev *hdev,
        struct hci_request req;
        /* General inquiry access code (GIAC) */
        u8 lap[3] = { 0x33, 0x8b, 0x9e };
-       u8 status, own_addr_type;
+       u8 status, own_addr_type, type;
        int err;
 
Quoted 7 lines

net/bluetooth/mgmt.c static int start_discovery(struct sock *sk, struct hci_dev *hdev, · line 3762

Quoted 4 lines
net/bluetooth/mgmt.c +2 -0
        BT_DBG("%s", hdev->name);
 
+       type = cp->type;
+

I don't see the point of this local variable & assignment. Why not just leave it as it is, "cp->type" isn't that wordy.

Quoted 6 lines

So this patch is preparation for next patch which will have if/else statement here that would pick proper type for different cases. The next patch is already big so I wanted to make this change from cp->type to type in this one. If that's a bad idea I'll move that to next patch.

Quoted 4 lines

Yeah, from here it just looks like a random refactor. I would just put it into the next patch. Or if you think that the next patch is too big, maybe think about how you can make it smaller by separating the logical steps?

Ok, I'll move it to next patch

Quoted 296 lines
Quoted 209 lines
Quoted 186 lines
Quoted 186 lines
net/bluetooth/mgmt.c +42 -41
        hci_dev_lock(hdev);
 
        if (!hdev_is_powered(hdev)) {
-               err = cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY,
-                                  MGMT_STATUS_NOT_POWERED,
-                                  &cp->type, sizeof(cp->type));
+               err = cmd_complete(sk, hdev->id, opcode,
+                                  MGMT_STATUS_NOT_POWERED, &type,
+                                  sizeof(type));
                goto failed;
        }
 
        if (test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags)) {
-               err = cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY,
-                                  MGMT_STATUS_BUSY, &cp->type,
-                                  sizeof(cp->type));
+               err = cmd_complete(sk, hdev->id, opcode,
+                                  MGMT_STATUS_BUSY, &type, sizeof(type));
                goto failed;
        }
 
        if (hdev->discovery.state != DISCOVERY_STOPPED) {
-               err = cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY,
-                                  MGMT_STATUS_BUSY, &cp->type,
-                                  sizeof(cp->type));
+               err = cmd_complete(sk, hdev->id, opcode,
+                                  MGMT_STATUS_BUSY, &type, sizeof(type));
                goto failed;
        }
 
-       cmd = mgmt_pending_add(sk, MGMT_OP_START_DISCOVERY, hdev, NULL, 0);
+       cmd = mgmt_pending_add(sk, opcode, hdev, NULL, 0);
        if (!cmd) {
                err = -ENOMEM;
                goto failed;
        }
 
-       hdev->discovery.type = cp->type;
+       hdev->discovery.type = type;
 
        hci_req_init(&req, hdev);
 
@@ -3788,18 +3795,16 @@ static int start_discovery(struct sock *sk, struct hci_dev *hdev,
        case DISCOV_TYPE_BREDR:
                status = mgmt_bredr_support(hdev);
                if (status) {
-                       err = cmd_complete(sk, hdev->id,
-                                          MGMT_OP_START_DISCOVERY, status,
-                                          &cp->type, sizeof(cp->type));
+                       err = cmd_complete(sk, hdev->id, opcode, status, &type,
+                                          sizeof(type));
                        mgmt_pending_remove(cmd);
                        goto failed;
                }
 
                if (test_bit(HCI_INQUIRY, &hdev->flags)) {
-                       err = cmd_complete(sk, hdev->id,
-                                          MGMT_OP_START_DISCOVERY,
-                                          MGMT_STATUS_BUSY, &cp->type,
-                                          sizeof(cp->type));
+                       err = cmd_complete(sk, hdev->id, opcode,
+                                          MGMT_STATUS_BUSY, &type,
+                                          sizeof(type));
                        mgmt_pending_remove(cmd);
                        goto failed;
                }
@@ -3816,19 +3821,17 @@ static int start_discovery(struct sock *sk, struct hci_dev *hdev,
        case DISCOV_TYPE_INTERLEAVED:
                status = mgmt_le_support(hdev);
                if (status) {
-                       err = cmd_complete(sk, hdev->id,
-                                          MGMT_OP_START_DISCOVERY, status,
-                                          &cp->type, sizeof(cp->type));
+                       err = cmd_complete(sk, hdev->id, opcode, status, &type,
+                                          sizeof(type));
                        mgmt_pending_remove(cmd);
                        goto failed;
                }
 
                if (hdev->discovery.type == DISCOV_TYPE_INTERLEAVED &&
                    !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) {
-                       err = cmd_complete(sk, hdev->id,
-                                          MGMT_OP_START_DISCOVERY,
-                                          MGMT_STATUS_NOT_SUPPORTED,
-                                          &cp->type, sizeof(cp->type));
+                       err = cmd_complete(sk, hdev->id, opcode,
+                                          MGMT_STATUS_NOT_SUPPORTED, &type,
+                                          sizeof(type));
                        mgmt_pending_remove(cmd);
                        goto failed;
                }
@@ -3840,11 +3843,9 @@ static int start_discovery(struct sock *sk, struct hci_dev *hdev,
                         */
                        if (hci_conn_hash_lookup_state(hdev, LE_LINK,
                                                       BT_CONNECT)) {
-                               err = cmd_complete(sk, hdev->id,
-                                                  MGMT_OP_START_DISCOVERY,
-                                                  MGMT_STATUS_REJECTED,
-                                                  &cp->type,
-                                                  sizeof(cp->type));
+                               err = cmd_complete(sk, hdev->id, opcode,
+                                                  MGMT_STATUS_REJECTED, &type,
+                                                  sizeof(type));
                                mgmt_pending_remove(cmd);
                                goto failed;
                        }
@@ -3867,10 +3868,9 @@ static int start_discovery(struct sock *sk, struct hci_dev *hdev,
                 */
                err = hci_update_random_address(&req, true, &own_addr_type);
                if (err < 0) {
-                       err = cmd_complete(sk, hdev->id,
-                                          MGMT_OP_START_DISCOVERY,
-                                          MGMT_STATUS_FAILED,
-                                          &cp->type, sizeof(cp->type));
+                       err = cmd_complete(sk, hdev->id, opcode,
+                                          MGMT_STATUS_FAILED, &type,
+                                          sizeof(type));
                        mgmt_pending_remove(cmd);
                        goto failed;
                }
@@ -3890,14 +3890,15 @@ static int start_discovery(struct sock *sk, struct hci_dev *hdev,
                break;
 
        default:
-               err = cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY,
-                                  MGMT_STATUS_INVALID_PARAMS,
-                                  &cp->type, sizeof(cp->type));
+               err = cmd_complete(sk, hdev->id, opcode,
+                                  MGMT_STATUS_INVALID_PARAMS, &type,
+                                  sizeof(type));
                mgmt_pending_remove(cmd);
                goto failed;
        }
 
        err = hci_req_run(&req, start_discovery_complete);
+
        if (err < 0)
                mgmt_pending_remove(cmd);
        else
@@ -3908,12 +3909,20 @@ failed:
        return err;
 }
 
-static int mgmt_stop_discovery_failed(struct hci_dev *hdev, u8 status)
+static int start_discovery(struct sock *sk, struct hci_dev *hdev,
+                          void *data, u16 len)
+{
+       return generic_start_discovery(sk, hdev, data, len,
+                                      MGMT_OP_START_DISCOVERY);
+}
+
+static int mgmt_stop_discovery_failed(struct hci_dev *hdev, u8 status,
+                                     uint16_t opcode)
 {
        struct pending_cmd *cmd;
        int err;
 
-       cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev);
+       cmd = mgmt_pending_find(opcode, hdev);
        if (!cmd)
                return -ENOENT;
 
@@ -3924,14 +3933,15 @@ static int mgmt_stop_discovery_failed(struct hci_dev *hdev, u8 status)
        return err;
 }
 
-static void stop_discovery_complete(struct hci_dev *hdev, u8 status)
+static void generic_stop_discovery_complete(struct hci_dev *hdev, u8 status,
+                                           uint16_t opcode)
 {
        BT_DBG("status %d", status);
 
        hci_dev_lock(hdev);
 
        if (status) {
-               mgmt_stop_discovery_failed(hdev, status);
+               mgmt_stop_discovery_failed(hdev, status, opcode);
                goto unlock;
        }
 
@@ -3941,33 +3951,40 @@ unlock:
        hci_dev_unlock(hdev);
 }
 
Quoted 16 lines
Quoted 4 lines
net/bluetooth/mgmt.c +2 -2
-static int stop_discovery(struct sock *sk, struct hci_dev *hdev, void *data,
-                         u16 len)
+static void stop_discovery_complete(struct hci_dev *hdev, u8 status)
+{

net/bluetooth/mgmt.c unlock: · line 3964

Quoted 10 lines
net/bluetooth/mgmt.c +6 -1
+       generic_stop_discovery_complete(hdev, status, MGMT_OP_STOP_DISCOVERY);
+}
+
+static int generic_stop_discovery(struct sock *sk, struct hci_dev *hdev,
+                                 void *data, u16 len, uint16_t opcode)
 {
-       struct mgmt_cp_stop_discovery *mgmt_cp = data;
        struct pending_cmd *cmd;
        struct hci_request req;
+       struct mgmt_cp_stop_discovery *mgmt_cp = data;

Why move this?

I'll fix that

Quoted 5 lines
Quoted 4 lines
net/bluetooth/mgmt.c +1 -0
        int err;
+       u8 type;
 
Quoted 84 lines
Quoted 1 line

Again, there's no need for this variable.

same as aboce - this patch is preparation for next patch which will have if/else statement here that would pick proper type for different cases.

Quoted 78 lines
Quoted 68 lines
net/bluetooth/mgmt.c +19 -11
        BT_DBG("%s", hdev->name);
 
+       type = mgmt_cp->type;
+
        hci_dev_lock(hdev);
 
        if (!hci_discovery_active(hdev)) {
-               err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY,
-                                  MGMT_STATUS_REJECTED, &mgmt_cp->type,
-                                  sizeof(mgmt_cp->type));
+               err = cmd_complete(sk, hdev->id, opcode,
+                                  MGMT_STATUS_REJECTED, &type, sizeof(type));
                goto unlock;
        }
 
-       if (hdev->discovery.type != mgmt_cp->type) {
-               err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY,
-                                  MGMT_STATUS_INVALID_PARAMS, &mgmt_cp->type,
-                                  sizeof(mgmt_cp->type));
+       if (hdev->discovery.type != type) {
+               err = cmd_complete(sk, hdev->id, opcode,
+                                  MGMT_STATUS_INVALID_PARAMS, &type,
+                                  sizeof(type));
                goto unlock;
        }
 
-       cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, NULL, 0);
+       cmd = mgmt_pending_add(sk, opcode, hdev, NULL, 0);
        if (!cmd) {
                err = -ENOMEM;
                goto unlock;
@@ -3978,6 +3995,7 @@ static int stop_discovery(struct sock *sk, struct hci_dev *hdev, void *data,
        hci_stop_discovery(&req);
 
        err = hci_req_run(&req, stop_discovery_complete);
+
        if (!err) {
                hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
                goto unlock;
@@ -3987,8 +4005,8 @@ static int stop_discovery(struct sock *sk, struct hci_dev *hdev, void *data,
 
        /* If no HCI commands were sent we're done */
        if (err == -ENODATA) {
-               err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, 0,
-                                  &mgmt_cp->type, sizeof(mgmt_cp->type));
+               err = cmd_complete(sk, hdev->id, opcode, 0,
+                                  &type, sizeof(type));
                hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
        }
 
@@ -3997,6 +4015,13 @@ unlock:
        return err;
 }
 
+static int stop_discovery(struct sock *sk, struct hci_dev *hdev, void *data,
+                         u16 len)
+{
+       return generic_stop_discovery(sk, hdev, data, len,
+                                     MGMT_OP_STOP_DISCOVERY);
+}
+
 static int confirm_name(struct sock *sk, struct hci_dev *hdev, void *data,
                        u16 len)
 {
--

2.1.0.rc2.206.gedb03e5

Quoted 4 lines

--
To unsubscribe from this list: send the line "unsubscribe linux-bluetooth" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Overall I think this patch looks good, except for a few nits that I commented on above.

Thanks,
Arman

Thanks,
Arman

#11 lore raw
In reply to Jakub Pawlowski

Hi Jakub,

Quoted 106 lines · Jakub Pawlowski
Quoted 98 lines · Arman Uguray

Hi Jakub,

Quoted 89 lines · Jakub Pawlowski
Quoted 73 lines · Arman Uguray

Hi Jakub,

Quoted 71 lines · Jakub Pawlowski

Currently there is no way to restart le scan. It's needed in preparation for new service scan method. The way it work: it disable, and then enable le scan on controller. During this restart special flag is set to make sure we won't remove disable scan work from workqueue.

---
 include/net/bluetooth/hci.h      |  1 +
 include/net/bluetooth/hci_core.h |  2 ++
 net/bluetooth/hci_core.c         | 42 ++++++++++++++++++++++++++++++++++++++++
 net/bluetooth/hci_event.c        |  9 ++++++---
 4 files changed, 51 insertions(+), 3 deletions(-)
include/net/bluetooth/hci.h +1 -0
index e56f909..2f0bce2 100644
@@ -203,6 +203,7 @@ enum {
        HCI_FAST_CONNECTABLE,
        HCI_BREDR_ENABLED,
        HCI_LE_SCAN_INTERRUPTED,
+       HCI_LE_SCAN_RESTARTING,
 };
 
 /* A mask for the flags that are supposed to remain when a reset happens
include/net/bluetooth/hci_core.h +2 -0
index 396c098..36211f9 100644
@@ -337,6 +337,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];
@@ -1403,6 +1404,7 @@ int hci_update_random_address(struct hci_request *req, bool require_privacy,
                              u8 *own_addr_type);
 void hci_copy_identity_address(struct hci_dev *hdev, bdaddr_t *bdaddr,
                               u8 *bdaddr_type);
+void hci_restart_le_scan(struct hci_dev *hdev, unsigned long delay);
 
 #define SCO_AIRMODE_MASK       0x0003
 #define SCO_AIRMODE_CVSD       0x0000
net/bluetooth/hci_core.c +3 -0
index 5c319a4..858cf54 100644
@@ -2557,6 +2557,7 @@ static int hci_dev_do_close(struct hci_dev *hdev)
                cancel_delayed_work(&hdev->service_cache);
 
        cancel_delayed_work_sync(&hdev->le_scan_disable);
+       cancel_delayed_work_sync(&hdev->le_scan_restart);
 
        if (test_bit(HCI_MGMT, &hdev->dev_flags))
                cancel_delayed_work_sync(&hdev->rpa_expired);
@@ -3851,6 +3852,8 @@ static void le_scan_disable_work(struct work_struct *work)
 
        BT_DBG("%s", hdev->name);
 
+       cancel_delayed_work_sync(&hdev->le_scan_restart);
+
        hci_req_init(&req, hdev);
 
        hci_req_add_le_scan_disable(&req);
@@ -3860,6 +3863,44 @@ static void le_scan_disable_work(struct work_struct *work)
                BT_ERR("Disable LE scanning request failed: err %d", err);
 }
 
Quoted 9 lines

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

Quoted 4 lines
net/bluetooth/hci_core.c +4 -0
+void hci_restart_le_scan(struct hci_dev *hdev, unsigned long delay)
+{
+       queue_delayed_work(hdev->workqueue, &hdev->le_scan_restart, delay);
+}

Since you're exposing this as a public API, does it make sense to check here if a delayed work has already been scheduled? I.e. is it OK if this function gets called several times in succession with different delay values?

Next patch introduces DISCOV_LE_RESTART_DELAY msecs_to_jiffies(300) which is the only delay value used to schedule le_scan_restart right now. I might change the implementation to:

Quoted 6 lines

void hci_restart_le_scan(struct hci_dev *hdev)

{
       queue_delayed_work(hdev->workqueue, &hdev->le_scan_restart,

DISCOV_LE_RESTART_DELAY);
}

Quoted 5 lines

Sure but even with a consistent delay value, is it OK to repeatedly call this function from other layers? Since this is publicly exposed from hci_core.h, any code within the subsystem can make calls to this. So you should make sure that it doesn't break this, or cover that edge case inside this function, if necessary.

Quoted 6 lines

So queue_delayed_work when you call it first time just schedule job and returns 0, if you call it again it will return nozero value if structure was already waiting in the queue.
I actually depend on this behaviour to make sure I don't restart scan too often, maximum once every DISCOV_LE_RESTART_DELAY.

Cool, that's what I wanted to confirm. In that case I'm OK with this.

Quoted 79 lines
Quoted 79 lines
Quoted 78 lines
Quoted 78 lines
Quoted 71 lines
net/bluetooth/hci_core.c +35 -0
+
+static void le_scan_restart_work_complete(struct hci_dev *hdev, u8 status)
+{
+       clear_bit(HCI_LE_SCAN_RESTARTING, &hdev->dev_flags);
+
+       if (status)
+               BT_ERR("Failed to restart LE scanning: status %d", status);
+}
+
+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);
+
+       hci_req_init(&req, hdev);
+
+       hci_req_add_le_scan_disable(&req);
+
+       memset(&cp, 0, sizeof(cp));
+       cp.enable = LE_SCAN_ENABLE;
+       hci_req_add(&req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(cp), &cp);
+
+       set_bit(HCI_LE_SCAN_RESTARTING, &hdev->dev_flags);
+
+       err = hci_req_run(&req, le_scan_restart_work_complete);
+       if (err)
+               BT_ERR("Disable LE scanning request failed: err %d", err);
+}
+
 static void set_random_addr(struct hci_request *req, bdaddr_t *rpa)
 {
        struct hci_dev *hdev = req->hdev;
@@ -4037,6 +4078,7 @@ struct hci_dev *hci_alloc_dev(void)
        INIT_DELAYED_WORK(&hdev->power_off, hci_power_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_restart, le_scan_restart_work);
 
        skb_queue_head_init(&hdev->rx_q);
        skb_queue_head_init(&hdev->cmd_q);
net/bluetooth/hci_event.c +6 -4
index bd0a801..ed4d5e1 100644
@@ -1157,10 +1157,13 @@ static void hci_cc_le_set_scan_enable(struct hci_dev *hdev,
                                          d->last_adv_data_len, NULL, 0);
                }
 
-               /* Cancel this timer so that we don't try to disable scanning
-                * when it's already disabled.
+               /* If HCI_LE_SCAN_RESTARTING is set, don't cancel this timer,
+                * because we're just restarting scan. Otherwise cancel it so
+                * that we don't try to disable scanning when it's already
+                * disabled.
                 */
-               cancel_delayed_work(&hdev->le_scan_disable);
+               if (!test_bit(HCI_LE_SCAN_RESTARTING, &hdev->dev_flags))
+                       cancel_delayed_work(&hdev->le_scan_disable);
 
                clear_bit(HCI_LE_SCAN, &hdev->dev_flags);
 
--

2.1.0.rc2.206.gedb03e5

Quoted 4 lines

--
To unsubscribe from this list: send the line "unsubscribe linux-bluetooth" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Thanks,
Arman

Quoted 1 line
Quoted 1 line

Arman

Arman