Skip to contentSkip to conversation

Bluetooth: Simplify read_adv_features code

PATCH In review Johan Hedberg 2 participants 4 messages Nov 26, 2015

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

PATCH 1/2 Bluetooth: Simplify read_adv_features code

From: Johan Hedberg <[email protected]>

The code in the Read Advertising Features mgmt command handler is unnecessarily complicated. Clean it up and remove unnecessary variables & branches.

---
 net/bluetooth/mgmt.c | 28 ++++++++--------------------
 1 file changed, 8 insertions(+), 20 deletions(-)
net/bluetooth/mgmt.c +8 -20
index 468402ad933c..9ce2bb2fc977 100644
@@ -5788,10 +5788,10 @@ static int read_adv_features(struct sock *sk, struct hci_dev *hdev,
 {
 	struct mgmt_rp_read_adv_features *rp;
 	size_t rp_len;
-	int err, i;
-	bool instance;
+	int err;
 	struct adv_info *adv_instance;
 	u32 supported_flags;
+	u8 *instance;
 
 	BT_DBG("%s", hdev->name);
 
@@ -5801,12 +5801,7 @@ static int read_adv_features(struct sock *sk, struct hci_dev *hdev,
 
 	hci_dev_lock(hdev);
 
-	rp_len = sizeof(*rp);
-
-	instance = hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE);
-	if (instance)
-		rp_len += hdev->adv_instance_cnt;
-
+	rp_len = sizeof(*rp) + hdev->adv_instance_cnt;
 	rp = kmalloc(rp_len, GFP_ATOMIC);
 	if (!rp) {
 		hci_dev_unlock(hdev);
@@ -5819,19 +5814,12 @@ static int read_adv_features(struct sock *sk, struct hci_dev *hdev,
 	rp->max_adv_data_len = HCI_MAX_AD_LENGTH;
 	rp->max_scan_rsp_len = HCI_MAX_AD_LENGTH;
 	rp->max_instances = HCI_MAX_ADV_INSTANCES;
+	rp->num_instances = hdev->adv_instance_cnt;
 
-	if (instance) {
-		i = 0;
-		list_for_each_entry(adv_instance, &hdev->adv_instances, list) {
-			if (i >= hdev->adv_instance_cnt)
-				break;
-
-			rp->instance[i] = adv_instance->instance;
-			i++;
-		}
-		rp->num_instances = hdev->adv_instance_cnt;
-	} else {
-		rp->num_instances = 0;
+	instance = rp->instance;
+	list_for_each_entry(adv_instance, &hdev->adv_instances, list) {
+		*instance = adv_instance->instance;
+		instance++;
 	}
 
 	hci_dev_unlock(hdev);
Signature · 1 line
2.5.0
#1 lore raw

PATCH 2/2 Bluetooth: Remove unnecessary HCI_ADVERTISING_INSTANCE flag

From: Johan Hedberg <[email protected]>

This flag just tells us whether hdev->adv_instances is empty or not. We can equally well use the list_empty() function to get this information.

---
 include/net/bluetooth/hci.h |  1 -
 net/bluetooth/hci_request.c | 19 ++++++++-----------
 net/bluetooth/mgmt.c        |  8 +-------
 3 files changed, 9 insertions(+), 19 deletions(-)
include/net/bluetooth/hci.h +0 -1
index cc2216727655..339ea57be423 100644
@@ -239,7 +239,6 @@ enum {
 	HCI_LE_ENABLED,
 	HCI_ADVERTISING,
 	HCI_ADVERTISING_CONNECTABLE,
-	HCI_ADVERTISING_INSTANCE,
 	HCI_CONNECTABLE,
 	HCI_DISCOVERABLE,
 	HCI_LIMITED_DISCOVERABLE,
net/bluetooth/hci_request.c +8 -11
index 7cc24f1448bd..adfcd6f1d0de 100644
@@ -822,7 +822,7 @@ static u8 get_current_adv_instance(struct hci_dev *hdev)
 	 * setting was set. When neither apply, default to the global settings,
 	 * represented by instance "0".
 	 */
-	if (hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE) &&
+	if (!list_empty(&hdev->adv_instances) &&
 	    !hci_dev_test_flag(hdev, HCI_ADVERTISING))
 		return hdev->cur_adv_instance;
 
@@ -1144,7 +1144,7 @@ void hci_req_reenable_advertising(struct hci_dev *hdev)
 	u8 instance;
 
 	if (!hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
-	    !hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE))
+	    list_empty(&hdev->adv_instances))
 		return;
 
 	instance = get_current_adv_instance(hdev);
@@ -1202,7 +1202,7 @@ int __hci_req_schedule_adv_instance(struct hci_request *req, u8 instance,
 	u16 timeout;
 
 	if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
-	    !hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE))
+	    list_empty(&hdev->adv_instances))
 		return -EPERM;
 
 	if (hdev->adv_instance_timeout)
@@ -1319,10 +1319,8 @@ void hci_req_clear_adv_instance(struct hci_dev *hdev, struct hci_request *req,
 		}
 	}
 
-	if (list_empty(&hdev->adv_instances)) {
+	if (list_empty(&hdev->adv_instances))
 		hdev->cur_adv_instance = 0x00;
-		hci_dev_clear_flag(hdev, HCI_ADVERTISING_INSTANCE);
-	}
 
 	if (!req || !hdev_is_powered(hdev) ||
 	    hci_dev_test_flag(hdev, HCI_ADVERTISING))
@@ -1525,7 +1523,7 @@ static int connectable_update(struct hci_request *req, unsigned long opt)
 
 	/* Update the advertising parameters if necessary */
 	if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
-	    hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE))
+	    !list_empty(&hdev->adv_instances))
 		__hci_req_enable_advertising(req);
 
 	__hci_update_background_scan(req);
@@ -2226,13 +2224,12 @@ static int powered_update_hci(struct hci_request *req, unsigned long opt)
 		 */
 		if (hci_dev_test_flag(hdev, HCI_LE_ENABLED) &&
 		    (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
-		     !hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE))) {
+		     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_INSTANCE) &&
-		    hdev->cur_adv_instance == 0x00 &&
+		if (hdev->cur_adv_instance == 0x00 &&
 		    !list_empty(&hdev->adv_instances)) {
 			adv_instance = list_first_entry(&hdev->adv_instances,
 							struct adv_info, list);
@@ -2241,7 +2238,7 @@ static int powered_update_hci(struct hci_request *req, unsigned long opt)
 
 		if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
 			__hci_req_enable_advertising(req);
-		else if (hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE) &&
+		else if (!list_empty(&hdev->adv_instances) &&
 			 hdev->cur_adv_instance)
 			__hci_req_schedule_adv_instance(req,
 							hdev->cur_adv_instance,
net/bluetooth/mgmt.c +1 -7
index 9ce2bb2fc977..03a65e89a7d7 100644
@@ -3734,7 +3734,6 @@ static void set_advertising_complete(struct hci_dev *hdev, u8 status,
 	 * set up earlier, then re-enable multi-instance advertising.
 	 */
 	if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
-	    !hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE) ||
 	    list_empty(&hdev->adv_instances))
 		goto unlock;
 
@@ -5892,9 +5891,6 @@ static void add_advertising_complete(struct hci_dev *hdev, u8 status,
 
 	cmd = pending_find(MGMT_OP_ADD_ADVERTISING, hdev);
 
-	if (status)
-		hci_dev_clear_flag(hdev, HCI_ADVERTISING_INSTANCE);
-
 	list_for_each_entry_safe(adv_instance, n, &hdev->adv_instances, list) {
 		if (!adv_instance->pending)
 			continue;
@@ -6012,8 +6008,6 @@ static int add_advertising(struct sock *sk, struct hci_dev *hdev,
 	if (hdev->adv_instance_cnt > prev_instance_cnt)
 		mgmt_advertising_added(sk, hdev, cp->instance);
 
-	hci_dev_set_flag(hdev, HCI_ADVERTISING_INSTANCE);
-
 	if (hdev->cur_adv_instance == cp->instance) {
 		/* If the currently advertised instance is being changed then
 		 * cancel the current advertising and schedule the next
@@ -6129,7 +6123,7 @@ static int remove_advertising(struct sock *sk, struct hci_dev *hdev,
 		goto unlock;
 	}
 
-	if (!hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE)) {
+	if (list_empty(&hdev->adv_instances)) {
 		err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADVERTISING,
 				      MGMT_STATUS_INVALID_PARAMS);
 		goto unlock;
Signature · 1 line
2.5.0
maintainer
#2 lore raw

Re: [PATCH 2/2] Bluetooth: Remove unnecessary HCI_ADVERTISING_INSTANCE flag

Hi Johan,

Quoted 10 lines

This flag just tells us whether hdev->adv_instances is empty or not. We can equally well use the list_empty() function to get this information.

---

include/net/bluetooth/hci.h |  1 -
net/bluetooth/hci_request.c | 19 ++++++++-----------
net/bluetooth/mgmt.c        |  8 +-------
3 files changed, 9 insertions(+), 19 deletions(-)

patch has been applied to bluetooth-next tree.

Regards

Marcel

maintainer
#3 lore raw
In reply to Johan Hedberg

Hi Johan,

Quoted 8 lines

The code in the Read Advertising Features mgmt command handler is unnecessarily complicated. Clean it up and remove unnecessary variables & branches.

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

1 file changed, 8 insertions(+), 20 deletions(-)

patch has been applied to bluetooth-next tree.

Regards

Marcel