Skip to contentSkip to conversation

Bluetooth: Move local identity address setting to a central place

PATCH Superseded [email protected] 2 participants 8 messages Mar 14, 2014 – Mar 16, 2014

Subsystems (2)

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/4 Bluetooth: Move local identity address setting to a central place

From: Johan Hedberg <[email protected]>

Any time hci_conn_add is used for an LE connection we need to ensure that the local identity address is correctly described in the src and src_type variables. This patch moves setting these values directly into hci_conn_add so that callers don't have to duplicate the effort themselves.

---
 net/bluetooth/hci_conn.c  |  4 ++++
 net/bluetooth/hci_event.c | 16 ----------------
 2 files changed, 4 insertions(+), 16 deletions(-)
net/bluetooth/hci_conn.c +4 -0
index b4809e473a19..1a025b953f19 100644
@@ -401,6 +401,10 @@ struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst)
 	case ACL_LINK:
 		conn->pkt_type = hdev->pkt_type & ACL_PTYPE_MASK;
 		break;
+	case LE_LINK:
+		/* conn->src should reflect the local identity address */
+		hci_copy_identity_address(hdev, &conn->src, &conn->src_type);
+		break;
 	case SCO_LINK:
 		if (lmp_esco_capable(hdev))
 			conn->pkt_type = (hdev->esco_type & SCO_ESCO_MASK) |
net/bluetooth/hci_event.c +0 -16
index e97f1905aa5c..7346ebd4c7d9 100644
@@ -3769,17 +3769,6 @@ static void hci_le_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
 
 		conn->dst_type = ev->bdaddr_type;
 
-		/* The advertising parameters for own address type
-		 * define which source address and source address
-		 * type this connections has.
-		 */
-		if (bacmp(&conn->src, BDADDR_ANY)) {
-			conn->src_type = ADDR_LE_DEV_PUBLIC;
-		} else {
-			bacpy(&conn->src, &hdev->static_addr);
-			conn->src_type = ADDR_LE_DEV_RANDOM;
-		}
-
 		if (ev->role == LE_CONN_ROLE_MASTER) {
 			conn->out = true;
 			conn->link_mode |= HCI_LM_MASTER;
@@ -3821,11 +3810,6 @@ static void hci_le_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
 		cancel_delayed_work(&conn->le_conn_timeout);
 	}
 
-	/* Ensure that the hci_conn contains the identity address type
-	 * regardless of which address the connection was made with.
-	 */
-	hci_copy_identity_address(hdev, &conn->src, &conn->src_type);
-
 	/* Lookup the identity address from the stored connection
 	 * address and address type.
 	 *
Signature · 1 line
1.8.5.3
#1 lore raw

PATCH v3 2/4 Bluetooth: Fix LE responder/initiator address setting

From: Johan Hedberg <[email protected]>

Once directed advertising is brought into the picture simply the lack of an hci_conn object when an le_conn_complete event occurs is no longer a reliable indication that the responder & initiator values need to be set based on our advertising address type.

This patch moves the code for setting these values outside of the "if (!conn)" branch and ensures that they get set for any connection where we are in the slave role.

---
 net/bluetooth/hci_event.c | 26 ++++++++++++++------------
 1 file changed, 14 insertions(+), 12 deletions(-)
net/bluetooth/hci_event.c +14 -12
index 7346ebd4c7d9..7f5178ccae97 100644
@@ -3793,23 +3793,25 @@ static void hci_le_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
 							  &conn->init_addr,
 							  &conn->init_addr_type);
 			}
-		} else {
-			/* Set the responder (our side) address type based on
-			 * the advertising address type.
-			 */
-			conn->resp_addr_type = hdev->adv_addr_type;
-			if (hdev->adv_addr_type == ADDR_LE_DEV_RANDOM)
-				bacpy(&conn->resp_addr, &hdev->random_addr);
-			else
-				bacpy(&conn->resp_addr, &hdev->bdaddr);
-
-			conn->init_addr_type = ev->bdaddr_type;
-			bacpy(&conn->init_addr, &ev->bdaddr);
 		}
 	} else {
 		cancel_delayed_work(&conn->le_conn_timeout);
 	}
 
+	if (!conn->out) {
+		/* Set the responder (our side) address type based on
+		 * the advertising address type.
+		 */
+		conn->resp_addr_type = hdev->adv_addr_type;
+		if (hdev->adv_addr_type == ADDR_LE_DEV_RANDOM)
+			bacpy(&conn->resp_addr, &hdev->random_addr);
+		else
+			bacpy(&conn->resp_addr, &hdev->bdaddr);
+
+		conn->init_addr_type = ev->bdaddr_type;
+		bacpy(&conn->init_addr, &ev->bdaddr);
+	}
+
 	/* Lookup the identity address from the stored connection
 	 * address and address type.
 	 *
Signature · 1 line
1.8.5.3
#2 lore raw

PATCH v3 3/4 Bluetooth: Add error mapping for Directed Advertising Timeout

In reply to [email protected]

From: Johan Hedberg <[email protected]>

When a timeout occurs using directed advertising a 0x3c error gets generated. Since the operation is analogous to conventional connection creation map this to the usual EHOSTDOWN error.

---
 net/bluetooth/lib.c | 1 +
 1 file changed, 1 insertion(+)
net/bluetooth/lib.c +1 -0
index b3fbc73516c4..941ad7530eda 100644
@@ -58,6 +58,7 @@ int bt_to_errno(__u16 code)
 		return EIO;
 
 	case 0x04:
+	case 0x3c:
 		return EHOSTDOWN;
 
 	case 0x05:
Signature · 1 line
1.8.5.3
#3 lore raw

PATCH v3 4/4 Bluetooth: Add directed advertising support through connect()

In reply to [email protected]

From: Johan Hedberg <[email protected]>

When we're in peripheral mode (HCI_ADVERTISING flag is set) the most natural mapping of connect() is to perform directed advertising to the peer device.

This patch does the necessary changes to enable directed advertising and keeps the hci_conn state as BT_CONNECT in a similar way as is done for central or BR/EDR connection initiation.

---
 include/net/bluetooth/hci.h |  1 +
 net/bluetooth/hci_conn.c    | 75 ++++++++++++++++++++++++++++++++++++++++-----
 net/bluetooth/hci_event.c   | 17 ++++++++--
 3 files changed, 83 insertions(+), 10 deletions(-)
include/net/bluetooth/hci.h +1 -0
index be150cf8cd43..4261a67682c0 100644
@@ -367,6 +367,7 @@ enum {
 #define HCI_ERROR_REMOTE_POWER_OFF	0x15
 #define HCI_ERROR_LOCAL_HOST_TERM	0x16
 #define HCI_ERROR_PAIRING_NOT_ALLOWED	0x18
+#define HCI_ERROR_ADVERTISING_TIMEOUT	0x3c
 
 /* Flow control modes */
 #define HCI_FLOW_CTL_MODE_PACKET_BASED	0x00
net/bluetooth/hci_conn.c +67 -8

124 lines not on this page · raw message

net/bluetooth/hci_event.c +15 -2
index 7f5178ccae97..14713b8d9a25 100644
@@ -989,10 +989,23 @@ static void hci_cc_le_set_adv_enable(struct hci_dev *hdev, struct sk_buff *skb)
 	if (!sent)
 		return;
 
+	if (status)
+		return;
+
 	hci_dev_lock(hdev);
 
-	if (!status)
-		mgmt_advertising(hdev, *sent);
+	/* Look for connection initation as peripheral */
+	if (*sent) {
+		struct hci_conn *conn;
+
+		conn = hci_conn_hash_lookup_state(hdev, LE_LINK, BT_CONNECT);
+		if (conn)
+			queue_delayed_work(hdev->workqueue,
+					   &conn->le_conn_timeout,
+					   HCI_LE_CONN_TIMEOUT);
+	}
+
+	mgmt_advertising(hdev, *sent);
 
 	hci_dev_unlock(hdev);
 }
Signature · 1 line
1.8.5.3
maintainer
#4 lore raw

Re: [PATCH v3 4/4] Bluetooth: Add directed advertising support through connect()

Hi Johan,

Quoted 35 lines

When we're in peripheral mode (HCI_ADVERTISING flag is set) the most natural mapping of connect() is to perform directed advertising to the peer device.

This patch does the necessary changes to enable directed advertising and keeps the hci_conn state as BT_CONNECT in a similar way as is done for central or BR/EDR connection initiation.

---

include/net/bluetooth/hci.h |  1 +
net/bluetooth/hci_conn.c    | 75 ++++++++++++++++++++++++++++++++++++++++-----
net/bluetooth/hci_event.c   | 17 ++++++++--
3 files changed, 83 insertions(+), 10 deletions(-)
include/net/bluetooth/hci.h +1 -0
index be150cf8cd43..4261a67682c0 100644
@@ -367,6 +367,7 @@ enum {
 #define HCI_ERROR_REMOTE_POWER_OFF	0x15
 #define HCI_ERROR_LOCAL_HOST_TERM	0x16
 #define HCI_ERROR_PAIRING_NOT_ALLOWED	0x18
+#define HCI_ERROR_ADVERTISING_TIMEOUT	0x3c
 
 /* Flow control modes */
 #define HCI_FLOW_CTL_MODE_PACKET_BASED	0x00
net/bluetooth/hci_conn.c +0 -0
index 1a025b953f19..088ee49097bc 100644
@@ -367,9 +367,18 @@ static void le_conn_timeout(struct work_struct *work)
 {
 	struct hci_conn *conn = container_of(work, struct hci_conn,
 					     le_conn_timeout.work);

net/bluetooth/hci_conn.c static void le_conn_timeout(struct work_struct *work) · line 379

Quoted 10 lines
net/bluetooth/hci_conn.c +7 -0
+	struct hci_dev *hdev = conn->hdev;
 
 	BT_DBG("");
 
+	if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) {
+		u8 enable = 0x00;
+		hci_send_cmd(hdev, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable),
+			     &enable);
+		hci_le_conn_failed(conn, HCI_ERROR_ADVERTISING_TIMEOUT);
+		return;

you need to add a comment why just disabling advertising is the right thing here. And that we later on will actually re-enable it using undirected advertising.

Does it really need to be disabled first. The connection timeout error is not automatically disabling advertising anyway?

Regards

Marcel

#5 lore raw

Re: [PATCH v3 4/4] Bluetooth: Add directed advertising support through connect()

Hi Marcel,

Quoted 39 lines · Marcel Holtmann
Quoted 39 lines

When we're in peripheral mode (HCI_ADVERTISING flag is set) the most natural mapping of connect() is to perform directed advertising to the peer device.

This patch does the necessary changes to enable directed advertising and keeps the hci_conn state as BT_CONNECT in a similar way as is done for central or BR/EDR connection initiation.

---

include/net/bluetooth/hci.h |  1 +
net/bluetooth/hci_conn.c    | 75 ++++++++++++++++++++++++++++++++++++++++-----
net/bluetooth/hci_event.c   | 17 ++++++++--
3 files changed, 83 insertions(+), 10 deletions(-)
include/net/bluetooth/hci.h +1 -0
index be150cf8cd43..4261a67682c0 100644
@@ -367,6 +367,7 @@ enum {
 #define HCI_ERROR_REMOTE_POWER_OFF	0x15
 #define HCI_ERROR_LOCAL_HOST_TERM	0x16
 #define HCI_ERROR_PAIRING_NOT_ALLOWED	0x18
+#define HCI_ERROR_ADVERTISING_TIMEOUT	0x3c
 
 /* Flow control modes */
 #define HCI_FLOW_CTL_MODE_PACKET_BASED	0x00
net/bluetooth/hci_conn.c +1 -0
index 1a025b953f19..088ee49097bc 100644
@@ -367,9 +367,18 @@ static void le_conn_timeout(struct work_struct *work)
 {
 	struct hci_conn *conn = container_of(work, struct hci_conn,
 					     le_conn_timeout.work);
+	struct hci_dev *hdev = conn->hdev;
 
 	BT_DBG("");
 
Quoted 10 lines

net/bluetooth/hci_conn.c static void le_conn_timeout(struct work_struct *work) · line 379

Quoted 6 lines
net/bluetooth/hci_conn.c +6 -0
+	if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) {
+		u8 enable = 0x00;
+		hci_send_cmd(hdev, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable),
+			     &enable);
+		hci_le_conn_failed(conn, HCI_ERROR_ADVERTISING_TIMEOUT);
+		return;

you need to add a comment why just disabling advertising is the right thing here. And that we later on will actually re-enable it using undirected advertising.

Sure, I can add a comment.

Quoted 2 lines

Does it really need to be disabled first. The connection timeout error is not automatically disabling advertising anyway?

You're right that this callback will only get called if we have not gotten any kind of indication from the controller that a connection happened (or failed). This could be because of misbehaving hardware or if we at some point start using low duty cycle directed advertising which doesn't have the 1.28s timeout that normal directed advertising has. I.e. I.e. this timeout is mostly for completeness sake since we also have a timeout for HCI_LE_Create_Connection.

Johan

#6 lore raw

PATCH v4 4/4 Bluetooth: Add directed advertising support through connect()

In reply to [email protected]

From: Johan Hedberg <[email protected]>

When we're in peripheral mode (HCI_ADVERTISING flag is set) the most natural mapping of connect() is to perform directed advertising to the peer device.

This patch does the necessary changes to enable directed advertising and keeps the hci_conn state as BT_CONNECT in a similar way as is done for central or BR/EDR connection initiation.

---
v4: Add clarifying comments for the connection timeout

 include/net/bluetooth/hci.h |  1 +
 net/bluetooth/hci_conn.c    | 80 ++++++++++++++++++++++++++++++++++++++++-----
 net/bluetooth/hci_event.c   | 19 +++++++++--
 3 files changed, 90 insertions(+), 10 deletions(-)
include/net/bluetooth/hci.h +1 -0
index be150cf8cd43..4261a67682c0 100644
@@ -367,6 +367,7 @@ enum {
 #define HCI_ERROR_REMOTE_POWER_OFF	0x15
 #define HCI_ERROR_LOCAL_HOST_TERM	0x16
 #define HCI_ERROR_PAIRING_NOT_ALLOWED	0x18
+#define HCI_ERROR_ADVERTISING_TIMEOUT	0x3c
 
 /* Flow control modes */
 #define HCI_FLOW_CTL_MODE_PACKET_BASED	0x00
net/bluetooth/hci_conn.c +72 -8

129 lines not on this page · raw message

net/bluetooth/hci_event.c +17 -2
index 7f5178ccae97..ec5339a05bb3 100644
@@ -989,10 +989,25 @@ static void hci_cc_le_set_adv_enable(struct hci_dev *hdev, struct sk_buff *skb)
 	if (!sent)
 		return;
 
+	if (status)
+		return;
+
 	hci_dev_lock(hdev);
 
-	if (!status)
-		mgmt_advertising(hdev, *sent);
+	/* If we're doing connection initation as peripheral. Set a
+	 * timeout in case something goes wrong.
+	 */
+	if (*sent) {
+		struct hci_conn *conn;
+
+		conn = hci_conn_hash_lookup_state(hdev, LE_LINK, BT_CONNECT);
+		if (conn)
+			queue_delayed_work(hdev->workqueue,
+					   &conn->le_conn_timeout,
+					   HCI_LE_CONN_TIMEOUT);
+	}
+
+	mgmt_advertising(hdev, *sent);
 
 	hci_dev_unlock(hdev);
 }
Signature · 1 line
1.8.5.3
#7 lore raw

PATCH v5 4/4 Bluetooth: Add directed advertising support through connect()

From: Johan Hedberg <[email protected]>

When we're in peripheral mode (HCI_ADVERTISING flag is set) the most natural mapping of connect() is to perform directed advertising to the peer device.

This patch does the necessary changes to enable directed advertising and keeps the hci_conn state as BT_CONNECT in a similar way as is done for central or BR/EDR connection initiation.

---
v5: Remove min/max adv interval since those should be ignored

 include/net/bluetooth/hci.h |  1 +
 net/bluetooth/hci_conn.c    | 77 ++++++++++++++++++++++++++++++++++++++++-----
 net/bluetooth/hci_event.c   | 19 +++++++++--
 3 files changed, 87 insertions(+), 10 deletions(-)
include/net/bluetooth/hci.h +1 -0
index be150cf8cd43..4261a67682c0 100644
@@ -367,6 +367,7 @@ enum {
 #define HCI_ERROR_REMOTE_POWER_OFF	0x15
 #define HCI_ERROR_LOCAL_HOST_TERM	0x16
 #define HCI_ERROR_PAIRING_NOT_ALLOWED	0x18
+#define HCI_ERROR_ADVERTISING_TIMEOUT	0x3c
 
 /* Flow control modes */
 #define HCI_FLOW_CTL_MODE_PACKET_BASED	0x00
net/bluetooth/hci_conn.c +69 -8

126 lines not on this page · raw message

net/bluetooth/hci_event.c +17 -2
index 7f5178ccae97..ec5339a05bb3 100644
@@ -989,10 +989,25 @@ static void hci_cc_le_set_adv_enable(struct hci_dev *hdev, struct sk_buff *skb)
 	if (!sent)
 		return;
 
+	if (status)
+		return;
+
 	hci_dev_lock(hdev);
 
-	if (!status)
-		mgmt_advertising(hdev, *sent);
+	/* If we're doing connection initation as peripheral. Set a
+	 * timeout in case something goes wrong.
+	 */
+	if (*sent) {
+		struct hci_conn *conn;
+
+		conn = hci_conn_hash_lookup_state(hdev, LE_LINK, BT_CONNECT);
+		if (conn)
+			queue_delayed_work(hdev->workqueue,
+					   &conn->le_conn_timeout,
+					   HCI_LE_CONN_TIMEOUT);
+	}
+
+	mgmt_advertising(hdev, *sent);
 
 	hci_dev_unlock(hdev);
 }
Signature · 1 line
1.8.5.3