Skip to contentSkip to conversation

Bluetooth: Local name fixes for LE controllers

PATCH New Johan Hedberg 1 participant 5 messages Feb 28, 2013

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

cover Bluetooth: Local name fixes for LE controllers

Hi,

Dependency: Bluetooth: Add HCI transaction framework

This set of patches fixes setting the local name for LE controllers. By ensuring that the right HCI commands get sent (and the wrong ones don't get sent) and by refactoring the way the hci_update_ad() function is used.

Johan

----------------------------------------------------------------
Johan Hedberg (4):
Bluetooth: Fix waiting for EIR update when setting local name
Bluetooth: Handle AD updating through a transaction
Bluetooth: Fix local name setting for LE-only controllers
Bluetooth: Fix setting local name to the existing value

 include/net/bluetooth/hci_core.h |    4 +-
 net/bluetooth/hci_core.c         |   30 +++++------
 net/bluetooth/hci_event.c        |   19 ++++---
 net/bluetooth/mgmt.c             |  101 +++++++++++++++++++++-----------------
 4 files changed, 77 insertions(+), 77 deletions(-)
#1 lore raw

PATCH 1/4 Bluetooth: Fix waiting for EIR update when setting local name

From: Johan Hedberg <[email protected]>

We shouldn't respond to the mgmt_set_local_name command until all related HCI commands have completed. This patch fixes the issue by running the local name HCI command and the EIR update in the same transaction, and returning the mgmt command complete through the transaction complete callback.

The downside of this is that we must set hdev->dev_name before the local name HCI command has completed since otherwise the generated EIR command doesn't contain the new name. This means that we can no-longer reliably detect when the name has really changed and when not. Luckily this only affects scenarios where the mgmt interface is *not* used (e.g. hciconfig) so redundant mgmt_ev_local_name_changed events in these cases are an acceptable drawback.

---
 net/bluetooth/mgmt.c |   86 +++++++++++++++++++++++---------------------------
 1 file changed, 40 insertions(+), 46 deletions(-)
net/bluetooth/mgmt.c +40 -46
index f73c7da..c45625e 100644
@@ -2338,16 +2338,38 @@ static int user_passkey_neg_reply(struct sock *sk, struct hci_dev *hdev,
 				 HCI_OP_USER_PASSKEY_NEG_REPLY, 0);
 }
 
-static void update_name(struct hci_transaction *transaction, const char *name)
+static void update_name(struct hci_transaction *transaction)
 {
+	struct hci_dev *hdev = transaction->hdev;
 	struct hci_cp_write_local_name cp;
 
-	memcpy(cp.name, name, sizeof(cp.name));
+	memcpy(cp.name, hdev->dev_name, sizeof(cp.name));
 
 	hci_transaction_cmd(transaction, HCI_OP_WRITE_LOCAL_NAME,
 			    sizeof(cp), &cp);
 }
 
+static void set_name_complete(struct hci_dev *hdev, u16 opcode, int status)
+{
+	struct mgmt_cp_set_local_name *cp;
+	struct pending_cmd *cmd;
+
+	cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev);
+	if (!cmd)
+		return;
+
+	cp = cmd->param;
+
+	if (status)
+		cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME,
+			   mgmt_status(status));
+	else
+		cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0,
+			     cp, sizeof(*cp));
+
+	mgmt_pending_remove(cmd);
+}
+
 static int set_local_name(struct sock *sk, struct hci_dev *hdev, void *data,
 			  u16 len)
 {
@@ -2382,8 +2404,11 @@ static int set_local_name(struct sock *sk, struct hci_dev *hdev, void *data,
 		goto failed;
 	}
 
-	hci_transaction_init(&transaction, hdev, NULL);
-	update_name(&transaction, cp->name);
+	memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name));
+
+	hci_transaction_init(&transaction, hdev, set_name_complete);
+	update_name(&transaction);
+	update_eir(&transaction);
 	err = hci_transaction_run(&transaction);
 	if (err < 0)
 		mgmt_pending_remove(cmd);
@@ -3190,7 +3215,7 @@ static int powered_update_hci(struct hci_dev *hdev)
 	if (lmp_bredr_capable(hdev)) {
 		set_bredr_scan(&transaction);
 		update_class(&transaction);
-		update_name(&transaction, hdev->dev_name);
+		update_name(&transaction);
 		update_eir(&transaction);
 	}
 
@@ -3759,15 +3784,11 @@ int mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class,
 
 int mgmt_set_local_name_complete(struct hci_dev *hdev, u8 *name, u8 status)
 {
-	struct pending_cmd *cmd;
 	struct mgmt_cp_set_local_name ev;
-	bool changed = false;
-	int err = 0;
+	struct pending_cmd *cmd;
 
-	if (memcmp(name, hdev->dev_name, sizeof(hdev->dev_name)) != 0) {
-		memcpy(hdev->dev_name, name, sizeof(hdev->dev_name));
-		changed = true;
-	}
+	if (status)
+		return 0;
 
 	memset(&ev, 0, sizeof(ev));
 	memcpy(ev.name, name, HCI_MAX_NAME_LENGTH);
@@ -3775,43 +3796,16 @@ int mgmt_set_local_name_complete(struct hci_dev *hdev, u8 *name, u8 status)
 
 	cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev);
 	if (!cmd)
-		goto send_event;
-
-	/* Always assume that either the short or the complete name has
-	 * changed if there was a pending mgmt command */
-	changed = true;
-
-	if (status) {
-		err = cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME,
-				 mgmt_status(status));
-		goto failed;
-	}
-
-	err = cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, &ev,
-			   sizeof(ev));
-	if (err < 0)
-		goto failed;
-
-send_event:
-	if (changed)
-		err = mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev,
-				 sizeof(ev), cmd ? cmd->sk : NULL);
+		memcpy(hdev->dev_name, name, sizeof(hdev->dev_name));
 
-	/* EIR is taken care of separately when powering on the
-	 * adapter so only update them here if this is a name change
-	 * unrelated to power on.
+	/* If this is a HCI command related to powering on the HCI dev
+	 * don't send any mgmt signals.
 	 */
-	if (!test_bit(HCI_INIT, &hdev->flags)) {
-		struct hci_transaction transaction;
-		hci_transaction_init(&transaction, hdev, NULL);
-		update_eir(&transaction);
-		hci_transaction_run(&transaction);
-	}
+	if (!cmd && mgmt_pending_find(MGMT_OP_SET_POWERED, hdev))
+		return 0;
 
-failed:
-	if (cmd)
-		mgmt_pending_remove(cmd);
-	return err;
+	return mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev),
+			  cmd ? cmd->sk : NULL);
 }
 
 int mgmt_read_local_oob_data_reply_complete(struct hci_dev *hdev, u8 *hash,
Signature · 1 line
1.7.10.4
#2 lore raw

PATCH 2/4 Bluetooth: Handle AD updating through a transaction

In reply to Johan Hedberg

From: Johan Hedberg <[email protected]>

For proper control of the AD update and the related HCI commands it's best to run the AD update through a transaction instead of a standalone HCI command. This patch changes the hci_update_ad() function to take a transaction pointer and updates its users appropriately. E.g. the function is no longer called after the init sequence but during stage 3 of the init sequence.

The TX power is read during the init sequence, so we don't need an explicit update whenever it is read and the AD update based on the local name should be done through the local name mgmt handler. The only other user is the update based on enabling advertising. This part is still kept as there is no mgmt API to enable it.

---
 include/net/bluetooth/hci_core.h |    4 ++--
 net/bluetooth/hci_core.c         |   30 +++++++++++-------------------
 net/bluetooth/hci_event.c        |   19 +++++++++----------
 3 files changed, 22 insertions(+), 31 deletions(-)
include/net/bluetooth/hci_core.h +2 -2
index c9827f9..63ad210 100644
@@ -742,8 +742,6 @@ int hci_add_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 *hash,
 								u8 *randomizer);
 int hci_remove_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr);
 
-int hci_update_ad(struct hci_dev *hdev);
-
 void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb);
 
 int hci_recv_frame(struct sk_buff *skb);
@@ -1169,6 +1167,8 @@ struct hci_sec_filter {
 #define hci_req_lock(d)		mutex_lock(&d->req_lock)
 #define hci_req_unlock(d)	mutex_unlock(&d->req_lock)
 
+int hci_update_ad(struct hci_transaction *transaction);
+
 void hci_le_conn_update(struct hci_conn *conn, u16 min, u16 max,
 					u16 latency, u16 to_multiplier);
 void hci_le_start_enc(struct hci_conn *conn, __le16 ediv, __u8 rand[8],
net/bluetooth/hci_core.c +11 -19
index 92b6444..a75a613 100644
@@ -515,8 +515,10 @@ static void hci_init3_req(struct hci_transaction *transaction,
 	if (hdev->commands[5] & 0x10)
 		hci_setup_link_policy(transaction);
 
-	if (lmp_le_capable(hdev))
+	if (lmp_le_capable(hdev)) {
 		hci_set_le_support(transaction);
+		hci_update_ad(transaction);
+	}
 }
 
 static int __hci_init(struct hci_dev *hdev)
@@ -961,39 +963,30 @@ static u8 create_ad(struct hci_dev *hdev, u8 *ptr)
 	return ad_len;
 }
 
-int hci_update_ad(struct hci_dev *hdev)
+int hci_update_ad(struct hci_transaction *transaction)
 {
+	struct hci_dev *hdev = transaction->hdev;
 	struct hci_cp_le_set_adv_data cp;
 	u8 len;
-	int err;
-
-	hci_dev_lock(hdev);
 
-	if (!lmp_le_capable(hdev)) {
-		err = -EINVAL;
-		goto unlock;
-	}
+	if (!lmp_le_capable(hdev))
+		return -EINVAL;
 
 	memset(&cp, 0, sizeof(cp));
 
 	len = create_ad(hdev, cp.data);
 
 	if (hdev->adv_data_len == len &&
-	    memcmp(cp.data, hdev->adv_data, len) == 0) {
-		err = 0;
-		goto unlock;
-	}
+	    memcmp(cp.data, hdev->adv_data, len) == 0)
+		return 0;
 
 	memcpy(hdev->adv_data, cp.data, sizeof(cp.data));
 	hdev->adv_data_len = len;
 
 	cp.length = len;
-	err = hci_send_cmd(hdev, HCI_OP_LE_SET_ADV_DATA, sizeof(cp), &cp);
 
-unlock:
-	hci_dev_unlock(hdev);
-
-	return err;
+	return hci_transaction_cmd(transaction, HCI_OP_LE_SET_ADV_DATA,
+				   sizeof(cp), &cp);
 }
 
 /* ---- HCI ioctl helpers ---- */
@@ -1050,7 +1043,6 @@ int hci_dev_open(__u16 dev)
 		hci_dev_hold(hdev);
 		set_bit(HCI_UP, &hdev->flags);
 		hci_notify(hdev, HCI_DEV_UP);
-		hci_update_ad(hdev);
 		if (!test_bit(HCI_SETUP, &hdev->dev_flags) &&
 		    mgmt_valid_hdev(hdev)) {
 			hci_dev_lock(hdev);
net/bluetooth/hci_event.c +9 -10
index d766cf0..2f70903 100644
@@ -223,9 +223,6 @@ static void hci_cc_write_local_name(struct hci_dev *hdev, struct sk_buff *skb)
 		memcpy(hdev->dev_name, sent, HCI_MAX_NAME_LENGTH);
 
 	hci_dev_unlock(hdev);
-
-	if (!status && !test_bit(HCI_INIT, &hdev->flags))
-		hci_update_ad(hdev);
 }
 
 static void hci_cc_read_local_name(struct hci_dev *hdev, struct sk_buff *skb)
@@ -776,11 +773,8 @@ static void hci_cc_le_read_adv_tx_power(struct hci_dev *hdev,
 
 	BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
 
-	if (!rp->status) {
+	if (!rp->status)
 		hdev->adv_tx_power = rp->tx_power;
-		if (!test_bit(HCI_INIT, &hdev->flags))
-			hci_update_ad(hdev);
-	}
 }
 
 static void hci_cc_user_confirm_reply(struct hci_dev *hdev, struct sk_buff *skb)
@@ -877,10 +871,15 @@ static void hci_cc_le_set_adv_enable(struct hci_dev *hdev, struct sk_buff *skb)
 			clear_bit(HCI_LE_PERIPHERAL, &hdev->dev_flags);
 	}
 
-	hci_dev_unlock(hdev);
+	if (!test_bit(HCI_INIT, &hdev->flags)) {
+		struct hci_transaction transaction;
 
-	if (!test_bit(HCI_INIT, &hdev->flags))
-		hci_update_ad(hdev);
+		hci_transaction_init(&transaction, hdev, NULL);
+		hci_update_ad(&transaction);
+		hci_transaction_run(&transaction);
+	}
+
+	hci_dev_unlock(hdev);
 }
 
 static void hci_cc_le_set_scan_param(struct hci_dev *hdev, struct sk_buff *skb)
Signature · 1 line
1.7.10.4
#3 lore raw

PATCH 3/4 Bluetooth: Fix local name setting for LE-only controllers

In reply to Johan Hedberg

From: Johan Hedberg <[email protected]>

This patch fixes the mgmt_set_local_name command to send the appropriate HCI commands based on BR/EDR support and LE support. Local name and EIR data should only be sent for BR/EDR capable controllers whereas an update to the AD should only happen for LE capable controllers.

---
 net/bluetooth/mgmt.c |    8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
net/bluetooth/mgmt.c +6 -2
index c45625e..b704148 100644
@@ -2407,8 +2407,12 @@ static int set_local_name(struct sock *sk, struct hci_dev *hdev, void *data,
 	memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name));
 
 	hci_transaction_init(&transaction, hdev, set_name_complete);
-	update_name(&transaction);
-	update_eir(&transaction);
+	if (lmp_bredr_capable(hdev)) {
+		update_name(&transaction);
+		update_eir(&transaction);
+	}
+	if (lmp_le_capable(hdev))
+		hci_update_ad(&transaction);
 	err = hci_transaction_run(&transaction);
 	if (err < 0)
 		mgmt_pending_remove(cmd);
Signature · 1 line
1.7.10.4
#4 lore raw

PATCH 4/4 Bluetooth: Fix setting local name to the existing value

In reply to Johan Hedberg

From: Johan Hedberg <[email protected]>

If user space attempts to set the local name to the same value that's already set we should simply return a direct command complete for this mgmt command.

---
 net/bluetooth/mgmt.c |   11 +++++++++++
 1 file changed, 11 insertions(+)
net/bluetooth/mgmt.c +11 -0
index b704148..9ea0505 100644
@@ -2382,6 +2382,17 @@ static int set_local_name(struct sock *sk, struct hci_dev *hdev, void *data,
 
 	hci_dev_lock(hdev);
 
+	/* If the old values are the same as the new ones just return a
+	 * direct command complete event.
+	 */
+	if (!memcmp(hdev->dev_name, cp->name, sizeof(hdev->dev_name)) &&
+	    !memcmp(hdev->short_name, cp->short_name,
+		    sizeof(hdev->short_name))) {
+		err = cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0,
+				   data, len);
+		goto failed;
+	}
+
 	memcpy(hdev->short_name, cp->short_name, sizeof(hdev->short_name));
 
 	if (!hdev_is_powered(hdev)) {
Signature · 1 line
1.7.10.4