Skip to contentSkip to conversation

Bluetooth: Fix advertising address type when toggling connectable

PATCH Superseded [email protected] 2 participants 3 messages Feb 25, 2014

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 Bluetooth: Fix advertising address type when toggling connectable

From: Johan Hedberg <[email protected]>

When the connectable setting is toggled using mgmt_set_connectable the HCI_CONNECTABLE flag will only be set once the related HCI commands succeed. When determining what kind of advertising to do we need to therefore also check whether there is a pending Set Connectable command in addition to the current flag value.

The enable_advertising function was already taking care of this for the advertising type with the help of the get_adv_type function, but was failing to do the same for the address type selection. This patch converts the get_adv_type function to be more generic in that it returns the expected connectable state and updates the enable_advertising function to use the return value both for the advertising type as well as the advertising address type.

---
 net/bluetooth/mgmt.c | 21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)
net/bluetooth/mgmt.c +11 -10
index 25b8b278debd..16782b5860c5 100644
@@ -817,10 +817,9 @@ static void update_class(struct hci_request *req)
 	hci_req_add(req, HCI_OP_WRITE_CLASS_OF_DEV, sizeof(cod), cod);
 }
 
-static u8 get_adv_type(struct hci_dev *hdev)
+static bool get_connectable(struct hci_dev *hdev)
 {
 	struct pending_cmd *cmd;
-	bool connectable;
 
 	/* If there's a pending mgmt command the flag will not yet have
 	 * it's final value, so check for this first.
@@ -828,12 +827,10 @@ static u8 get_adv_type(struct hci_dev *hdev)
 	cmd = mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev);
 	if (cmd) {
 		struct mgmt_mode *cp = cmd->param;
-		connectable = !!cp->val;
-	} else {
-		connectable = test_bit(HCI_CONNECTABLE, &hdev->dev_flags);
+		return cp->val;
 	}
 
-	return connectable ? LE_ADV_IND : LE_ADV_NONCONN_IND;
+	return test_bit(HCI_CONNECTABLE, &hdev->dev_flags);
 }
 
 static void enable_advertising(struct hci_request *req)
@@ -841,17 +838,21 @@ static void enable_advertising(struct hci_request *req)
 	struct hci_dev *hdev = req->hdev;
 	struct hci_cp_le_set_adv_param cp;
 	u8 own_addr_type, enable = 0x01;
-	bool require_privacy;
+	bool connectable;
 
-	require_privacy = !test_bit(HCI_CONNECTABLE, &hdev->dev_flags);
+	connectable = get_connectable(hdev);
 
-	if (hci_update_random_address(req, require_privacy, &own_addr_type) < 0)
+	/* If we're connectable set require_privacy to false as we
+	 * shouldn't use NRPAs in that case. If we're non-connectable
+	 * however we can allow NRPAs to be used.
+	 */
+	if (hci_update_random_address(req, !connectable, &own_addr_type) < 0)
 		return;
 
 	memset(&cp, 0, sizeof(cp));
 	cp.min_interval = __constant_cpu_to_le16(0x0800);
 	cp.max_interval = __constant_cpu_to_le16(0x0800);
-	cp.type = get_adv_type(hdev);
+	cp.type = connectable ? LE_ADV_IND : LE_ADV_NONCONN_IND;
 	cp.own_address_type = own_addr_type;
 	cp.channel_map = hdev->le_adv_channel_map;
 
Signature · 1 line
1.8.5.3
maintainer
#1 lore raw

Hi Johan,

Quoted 57 lines

When the connectable setting is toggled using mgmt_set_connectable the HCI_CONNECTABLE flag will only be set once the related HCI commands succeed. When determining what kind of advertising to do we need to therefore also check whether there is a pending Set Connectable command in addition to the current flag value.

The enable_advertising function was already taking care of this for the advertising type with the help of the get_adv_type function, but was failing to do the same for the address type selection. This patch converts the get_adv_type function to be more generic in that it returns the expected connectable state and updates the enable_advertising function to use the return value both for the advertising type as well as the advertising address type.

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

1 file changed, 11 insertions(+), 10 deletions(-)
net/bluetooth/mgmt.c +4 -7
index 25b8b278debd..16782b5860c5 100644
@@ -817,10 +817,9 @@ static void update_class(struct hci_request *req)
 	hci_req_add(req, HCI_OP_WRITE_CLASS_OF_DEV, sizeof(cod), cod);
 }
 
-static u8 get_adv_type(struct hci_dev *hdev)
+static bool get_connectable(struct hci_dev *hdev)
 {
 	struct pending_cmd *cmd;
-	bool connectable;
 
 	/* If there's a pending mgmt command the flag will not yet have
 	 * it's final value, so check for this first.
@@ -828,12 +827,10 @@ static u8 get_adv_type(struct hci_dev *hdev)
 	cmd = mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev);
 	if (cmd) {
 		struct mgmt_mode *cp = cmd->param;
-		connectable = !!cp->val;
-	} else {
-		connectable = test_bit(HCI_CONNECTABLE, &hdev->dev_flags);
+		return cp->val;
 	}
 
-	return connectable ? LE_ADV_IND : LE_ADV_NONCONN_IND;
+	return test_bit(HCI_CONNECTABLE, &hdev->dev_flags);
 }
 
 static void enable_advertising(struct hci_request *req)
@@ -841,17 +838,21 @@ static void enable_advertising(struct hci_request *req)
 	struct hci_dev *hdev = req->hdev;
 	struct hci_cp_le_set_adv_param cp;
 	u8 own_addr_type, enable = 0x01;
-	bool require_privacy;
+	bool connectable;
 

net/bluetooth/mgmt.c static void enable_advertising(struct hci_request *req) · line 850

Quoted 10 lines
net/bluetooth/mgmt.c +6 -2
-	require_privacy = !test_bit(HCI_CONNECTABLE, &hdev->dev_flags);
+	connectable = get_connectable(hdev);
 
-	if (hci_update_random_address(req, require_privacy, &own_addr_type) < 0)
+	/* If we're connectable set require_privacy to false as we
+	 * shouldn't use NRPAs in that case. If we're non-connectable
+	 * however we can allow NRPAs to be used.
+	 */
+	if (hci_update_random_address(req, !connectable, &own_addr_type) < 0)
 		return;

I really do not want to be pedantic, but this comment is misleading. It is a bit hard to understand. Also I am not sure if it NRPA or URPA. I think using an acronym here is confusing.

	/* Set require_privacy to true only when non-connectable advertising
	 * is used. In that case it is fine to use an unresolvable private
	 * address.
	 */ 

Regards

Marcel

#2 lore raw

Hi Marcel,

Quoted 10 lines · Marcel Holtmann
Quoted 7 lines
net/bluetooth/mgmt.c +5 -1
-	if (hci_update_random_address(req, require_privacy, &own_addr_type) < 0)
+	/* If we're connectable set require_privacy to false as we
+	 * shouldn't use NRPAs in that case. If we're non-connectable
+	 * however we can allow NRPAs to be used.
+	 */
+	if (hci_update_random_address(req, !connectable, &own_addr_type) < 0)
		return;

I really do not want to be pedantic, but this comment is misleading. It is a bit hard to understand. Also I am not sure if it NRPA or URPA.

It's "non-resolvable" according to the core specification.

Quoted 6 lines

I think using an acronym here is confusing.

	/* Set require_privacy to true only when non-connectable advertising
	 * is used. In that case it is fine to use an unresolvable private
	 * address.
	 */ 

v3 has been sent.

Johan