Skip to contentSkip to conversation

Bluetooth: Advertising cleanups

PATCH In review Johan Hedberg 2 participants 5 messages Nov 30, 2015 – Dec 2, 2015

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

cover Bluetooth: Advertising cleanups

Hi,

Here are a few patches to simplify & clean up advertising handling. I might be doing a bit more similar things before going for refactoring all of the advertising HCI activitity behind hdev->req_workqueue and hci_req_sync().

Johan

----------------------------------------------------------------
Johan Hedberg (2):
Bluetooth: Clean up advertising initialization in powered_update_hci()
Bluetooth: Clean up current advertising instance tracking

 net/bluetooth/hci_core.c    | 12 ++++--
 net/bluetooth/hci_request.c | 98 ++++++++++++--------------------------------
 net/bluetooth/hci_request.h |  8 ++--
 net/bluetooth/mgmt.c        | 10 +++--
 4 files changed, 44 insertions(+), 84 deletions(-)
#1 lore raw

PATCH 1/2 Bluetooth: Clean up advertising initialization in powered_update_hci()

From: Johan Hedberg <[email protected]>

The logic in powered_update_hci() to initialize the advertising data & state is a bit more complicated than it needs to be. It was previously not doing anything if HCI_LE_ENABLED wasn't set, but this was not obvious by quickly looking at the code. Now the conditions for the various actions are more explicit. Another simplification is due to the fact that __hci_req_schedule_adv_instance() takes care of setting hdev->cur_adv_instance so there's no need to set it before calling the function.

---
 net/bluetooth/hci_request.c | 30 ++++++++++++------------------
 1 file changed, 12 insertions(+), 18 deletions(-)
net/bluetooth/hci_request.c +12 -18
index f1529d7740f6..14db777a6bb1 100644
@@ -2181,7 +2181,6 @@ static void discov_off(struct work_struct *work)
 static int powered_update_hci(struct hci_request *req, unsigned long opt)
 {
 	struct hci_dev *hdev = req->hdev;
-	struct adv_info *adv_instance;
 	u8 link_sec;
 
 	hci_dev_lock(hdev);
@@ -2216,32 +2215,27 @@ static int powered_update_hci(struct hci_request *req, unsigned long opt)
 				    sizeof(cp), &cp);
 	}
 
-	if (lmp_le_capable(hdev)) {
+	if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
 		/* Make sure the controller has a good default for
 		 * advertising data. This also applies to the case
 		 * where BR/EDR was toggled during the AUTO_OFF phase.
 		 */
-		if (hci_dev_test_flag(hdev, HCI_LE_ENABLED) &&
-		    (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
-		     list_empty(&hdev->adv_instances))) {
-			__hci_req_update_adv_data(req, HCI_ADV_CURRENT);
-			__hci_req_update_scan_rsp_data(req, HCI_ADV_CURRENT);
-		}
+		if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
+		    list_empty(&hdev->adv_instances)) {
+			__hci_req_update_adv_data(req, 0x00);
+			__hci_req_update_scan_rsp_data(req, 0x00);
+
+			if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
+				__hci_req_enable_advertising(req);
+		} else if (!list_empty(&hdev->adv_instances)) {
+			struct adv_info *adv_instance;
 
-		if (hdev->cur_adv_instance == 0x00 &&
-		    !list_empty(&hdev->adv_instances)) {
 			adv_instance = list_first_entry(&hdev->adv_instances,
 							struct adv_info, list);
-			hdev->cur_adv_instance = adv_instance->instance;
-		}
-
-		if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
-			__hci_req_enable_advertising(req);
-		else if (!list_empty(&hdev->adv_instances) &&
-			 hdev->cur_adv_instance)
 			__hci_req_schedule_adv_instance(req,
-							hdev->cur_adv_instance,
+							adv_instance->instance,
 							true);
+		}
 	}
 
 	link_sec = hci_dev_test_flag(hdev, HCI_LINK_SECURITY);
Signature · 1 line
2.5.0
maintainer
#2 lore raw

Re: [PATCH 1/2] Bluetooth: Clean up advertising initialization in powered_update_hci()

Hi Johan,

Quoted 9 lines

The logic in powered_update_hci() to initialize the advertising data & state is a bit more complicated than it needs to be. It was previously not doing anything if HCI_LE_ENABLED wasn't set, but this was not obvious by quickly looking at the code. Now the conditions for the various actions are more explicit. Another simplification is due to the fact that __hci_req_schedule_adv_instance() takes care of setting hdev->cur_adv_instance so there's no need to set it before calling the function.

Quoted 4 lines

---
net/bluetooth/hci_request.c | 30 ++++++++++++------------------

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

patch has been applied to bluetooth-next tree.

Regards

Marcel

#3 lore raw

PATCH 2/2 Bluetooth: Clean up current advertising instance tracking

In reply to Johan Hedberg

From: Johan Hedberg <[email protected]>

We can simplify a lot of code by making sure hdev->cur_adv_instance is always up-to-date. This allows e.g. the removal of the get_current_adv_instance() helper function and the special HCI_ADV_CURRENT value. This patch also makes selecting instance 0x00 explicit in the various calls where advertising instances aren't enabled, e.g. when HCI_ADVERTISING is set or we've just finished enabling LE.

---
 net/bluetooth/hci_core.c    | 12 +++++---
 net/bluetooth/hci_request.c | 68 ++++++++++-----------------------------------
 net/bluetooth/hci_request.h |  8 ++----
 net/bluetooth/mgmt.c        | 10 ++++---
 4 files changed, 32 insertions(+), 66 deletions(-)
net/bluetooth/hci_core.c +8 -4
index eac3f6fa1272..9fb443a5473a 100644
@@ -1769,7 +1769,7 @@ static void hci_update_scan_state(struct hci_dev *hdev, u8 scan)
 		hci_dev_set_flag(hdev, HCI_BREDR_ENABLED);
 
 		if (hci_dev_test_flag(hdev, HCI_LE_ENABLED))
-			hci_req_update_adv_data(hdev, HCI_ADV_CURRENT);
+			hci_req_update_adv_data(hdev, hdev->cur_adv_instance);
 
 		mgmt_new_settings(hdev);
 	}
@@ -2610,9 +2610,12 @@ int hci_remove_adv_instance(struct hci_dev *hdev, u8 instance)
 
 	BT_DBG("%s removing %dMR", hdev->name, instance);
 
-	if (hdev->cur_adv_instance == instance && hdev->adv_instance_timeout) {
-		cancel_delayed_work(&hdev->adv_instance_expire);
-		hdev->adv_instance_timeout = 0;
+	if (hdev->cur_adv_instance == instance) {
+		if (hdev->adv_instance_timeout) {
+			cancel_delayed_work(&hdev->adv_instance_expire);
+			hdev->adv_instance_timeout = 0;
+		}
+		hdev->cur_adv_instance = 0x00;
 	}
 
 	list_del(&adv_instance->list);
@@ -2639,6 +2642,7 @@ void hci_adv_instances_clear(struct hci_dev *hdev)
 	}
 
 	hdev->adv_instance_cnt = 0;
+	hdev->cur_adv_instance = 0x00;
 }
 
 /* This function requires the caller holds hdev->lock */
net/bluetooth/hci_request.c +15 -53

162 lines not on this page · raw message

net/bluetooth/hci_request.h +3 -5
index a24d3b55094c..64ff8c040d50 100644
@@ -64,14 +64,12 @@ void __hci_req_update_eir(struct hci_request *req);
 void hci_req_add_le_scan_disable(struct hci_request *req);
 void hci_req_add_le_passive_scan(struct hci_request *req);
 
-#define HCI_ADV_CURRENT (-1)
-
 void hci_req_reenable_advertising(struct hci_dev *hdev);
 void __hci_req_enable_advertising(struct hci_request *req);
 void __hci_req_disable_advertising(struct hci_request *req);
-void __hci_req_update_adv_data(struct hci_request *req, int instance);
-int hci_req_update_adv_data(struct hci_dev *hdev, int instance);
-void __hci_req_update_scan_rsp_data(struct hci_request *req, int instance);
+void __hci_req_update_adv_data(struct hci_request *req, u8 instance);
+int hci_req_update_adv_data(struct hci_dev *hdev, u8 instance);
+void __hci_req_update_scan_rsp_data(struct hci_request *req, u8 instance);
 
 int __hci_req_schedule_adv_instance(struct hci_request *req, u8 instance,
 				    bool force);
net/bluetooth/mgmt.c +6 -4
index 03a65e89a7d7..621f6fdd0dd1 100644
@@ -1626,8 +1626,8 @@ static void le_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode)
 		struct hci_request req;
 
 		hci_req_init(&req, hdev);
-		__hci_req_update_adv_data(&req, HCI_ADV_CURRENT);
-		__hci_req_update_scan_rsp_data(&req, HCI_ADV_CURRENT);
+		__hci_req_update_adv_data(&req, 0x00);
+		__hci_req_update_scan_rsp_data(&req, 0x00);
 		hci_req_run(&req, NULL);
 		hci_update_background_scan(hdev);
 	}
@@ -3006,7 +3006,7 @@ static int set_local_name(struct sock *sk, struct hci_dev *hdev, void *data,
 	 * no need to udpate the advertising data here.
 	 */
 	if (lmp_le_capable(hdev))
-		__hci_req_update_scan_rsp_data(&req, HCI_ADV_CURRENT);
+		__hci_req_update_scan_rsp_data(&req, hdev->cur_adv_instance);
 
 	err = hci_req_run(&req, set_name_complete);
 	if (err < 0)
@@ -3799,6 +3799,7 @@ static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data,
 		bool changed;
 
 		if (cp->val) {
+			hdev->cur_adv_instance = 0x00;
 			changed = !hci_dev_test_and_set_flag(hdev, HCI_ADVERTISING);
 			if (cp->val == 0x02)
 				hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
@@ -3846,6 +3847,7 @@ static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data,
 		 * We cannot use update_[adv|scan_rsp]_data() here as the
 		 * HCI_ADVERTISING flag is not yet set.
 		 */
+		hdev->cur_adv_instance = 0x00;
 		__hci_req_update_adv_data(&req, 0x00);
 		__hci_req_update_scan_rsp_data(&req, 0x00);
 		__hci_req_enable_advertising(&req);
@@ -4195,7 +4197,7 @@ static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
 	/* Since only the advertising data flags will change, there
 	 * is no need to update the scan response data.
 	 */
-	__hci_req_update_adv_data(&req, HCI_ADV_CURRENT);
+	__hci_req_update_adv_data(&req, hdev->cur_adv_instance);
 
 	err = hci_req_run(&req, set_bredr_complete);
 	if (err < 0)
Signature · 1 line
2.5.0
maintainer
#4 lore raw

Re: [PATCH 2/2] Bluetooth: Clean up current advertising instance tracking

Hi Johan,

Quoted 8 lines

We can simplify a lot of code by making sure hdev->cur_adv_instance is always up-to-date. This allows e.g. the removal of the get_current_adv_instance() helper function and the special HCI_ADV_CURRENT value. This patch also makes selecting instance 0x00 explicit in the various calls where advertising instances aren't enabled, e.g. when HCI_ADVERTISING is set or we've just finished enabling LE.

Quoted 7 lines

---

net/bluetooth/hci_core.c    | 12 +++++---
net/bluetooth/hci_request.c | 68 ++++++++++-----------------------------------
net/bluetooth/hci_request.h |  8 ++----
net/bluetooth/mgmt.c        | 10 ++++---
4 files changed, 32 insertions(+), 66 deletions(-)

patch has been applied to bluetooth-next tree.

Regards

Marcel