Skip to contentSkip to conversation

Bluetooth: Some fixes and full peripheral role support

PATCH In review Johan Hedberg 5 participants 29 messages Oct 24, 2012 – Oct 25, 2012

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

cover Bluetooth: Some fixes and full peripheral role support

Hi,

This set of patches contains some basic fixes to SSP and LE enablement HCI commands as well as the two missing patches from my previous set, i.e. implementation of mgmt_set_le for peripheral mode as well as the writing of advertising data.

Johan

#1 lore raw

PATCH 1/6 Bluetooth: Fix setting host feature bits for SSP

From: Johan Hedberg <[email protected]>

When we get a successful command complete for HCI_Write_SSP_Mode we need to update the host feature bits for the hdev struct accordingly.

---
 net/bluetooth/hci_event.c |   13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
net/bluetooth/hci_event.c +10 -3
index aae8053..dc60d31 100644
@@ -440,7 +440,7 @@ static void hci_cc_host_buffer_size(struct hci_dev *hdev, struct sk_buff *skb)
 static void hci_cc_write_ssp_mode(struct hci_dev *hdev, struct sk_buff *skb)
 {
 	__u8 status = *((__u8 *) skb->data);
-	void *sent;
+	struct hci_cp_write_ssp_mode *sent;
 
 	BT_DBG("%s status 0x%2.2x", hdev->name, status);
 
@@ -448,10 +448,17 @@ static void hci_cc_write_ssp_mode(struct hci_dev *hdev, struct sk_buff *skb)
 	if (!sent)
 		return;
 
+	if (!status) {
+		if (sent->mode)
+			hdev->host_features[0] |= LMP_HOST_SSP;
+		else
+			hdev->host_features[0] &= ~LMP_HOST_SSP;
+	}
+
 	if (test_bit(HCI_MGMT, &hdev->dev_flags))
-		mgmt_ssp_enable_complete(hdev, *((u8 *) sent), status);
+		mgmt_ssp_enable_complete(hdev, sent->mode, status);
 	else if (!status) {
-		if (*((u8 *) sent))
+		if (sent->mode)
 			set_bit(HCI_SSP_ENABLED, &hdev->dev_flags);
 		else
 			clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags);
Signature · 1 line
1.7.10.4
#3 lore raw

PATCH 2/6 Bluetooth: Fix sending unnecessary HCI_Write_SSP_Mode command

In reply to Johan Hedberg

From: Johan Hedberg <[email protected]>

This patch fixes sending an unnecessary HCI_Write_SSP_Mode command if the command has already been sent as part of the default HCI init sequence.

---
 include/net/bluetooth/hci_core.h |    1 +
 net/bluetooth/mgmt.c             |    3 ++-
 2 files changed, 3 insertions(+), 1 deletion(-)
include/net/bluetooth/hci_core.h +1 -0
index 6642b3c..b3490c6 100644
@@ -770,6 +770,7 @@ void hci_conn_del_sysfs(struct hci_conn *conn);
 #define lmp_ext_feat_capable(dev)  ((dev)->features[7] & LMP_EXTFEATURES)
 
 /* ----- Extended LMP capabilities ----- */
+#define lmp_host_ssp_capable(dev)  ((dev)->host_features[0] & LMP_HOST_SSP)
 #define lmp_host_le_capable(dev)   ((dev)->host_features[0] & LMP_HOST_LE)
 #define lmp_host_le_br_capable(dev) ((dev)->host_features[0] & LMP_HOST_LE_BREDR)
 
net/bluetooth/mgmt.c +2 -1
index 0ee150e..02dc5f8 100644
@@ -2914,7 +2914,8 @@ int mgmt_powered(struct hci_dev *hdev, u8 powered)
 	mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match);
 
 	if (powered) {
-		if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) {
+		if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags) &&
+		    !lmp_host_ssp_capable(hdev)) {
 			u8 ssp = 1;
 
 			hci_send_cmd(hdev, HCI_OP_WRITE_SSP_MODE, 1, &ssp);
Signature · 1 line
1.7.10.4
#5 lore raw

PATCH 3/6 Bluetooth: Fix sending unnecessary HCI_LE_Host_Enable

In reply to Johan Hedberg

From: Johan Hedberg <[email protected]>

This patch fixes sending an unnecessary HCI_LE_Host_Enable command if the command has already been sent as part of the default HCI init sequence.

---
 net/bluetooth/mgmt.c |   10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
net/bluetooth/mgmt.c +8 -2
index 02dc5f8..03dc176 100644
@@ -2927,8 +2927,14 @@ int mgmt_powered(struct hci_dev *hdev, u8 powered)
 			cp.le = 1;
 			cp.simul = !!lmp_le_br_capable(hdev);
 
-			hci_send_cmd(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED,
-				     sizeof(cp), &cp);
+			/* Check first if we already have the right
+			 * host state (host features set)
+			 */
+			if (cp.le != !!lmp_host_le_capable(hdev) ||
+			    cp.simul != !!lmp_host_le_br_capable(hdev))
+				hci_send_cmd(hdev,
+					     HCI_OP_WRITE_LE_HOST_SUPPORTED,
+					     sizeof(cp), &cp);
 		}
 
 		if (lmp_bredr_capable(hdev)) {
Signature · 1 line
1.7.10.4
#11 lore raw

PATCH 4/6 Bluetooth: Fix unnecessary EIR update during powering on

In reply to Johan Hedberg

From: Johan Hedberg <[email protected]>

When powered on the EIR data gets updated as the last step by mgmt. Therefore avoid an update when getting a local name update as that's part of the normal HCI init sequence.

---
 net/bluetooth/mgmt.c |    7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
net/bluetooth/mgmt.c +6 -1
index 03dc176..9fe386f 100644
@@ -3523,7 +3523,12 @@ send_event:
 		err = mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev,
 				 sizeof(ev), cmd ? cmd->sk : NULL);
 
-	update_eir(hdev);
+	/* 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 (!test_bit(HCI_INIT, &hdev->flags))
+		update_eir(hdev);
 
 failed:
 	if (cmd)
Signature · 1 line
1.7.10.4
#14 lore raw

PATCH 5/6 Bluetooth: mgmt: Add support for switching to LE peripheral mode

In reply to Johan Hedberg

From: Johan Hedberg <[email protected]>

This patch extends the mgmt_set_le command to allow for a new value (0x02) to mean that LE should be enabled together with advertising enabled. This paves the way for acting in a fully functional LE peripheral role. The change to the mgmt_set_le command is fully backwards compatible as the value 0x01 means LE central role (which was the old behavior).

---
 include/net/bluetooth/hci.h      |    2 +
 include/net/bluetooth/hci_core.h |    1 +
 include/net/bluetooth/mgmt.h     |    6 ++
 net/bluetooth/hci_event.c        |   57 +++++++++++++
 net/bluetooth/mgmt.c             |  166 +++++++++++++++++++++++++++++++-------
 5 files changed, 204 insertions(+), 28 deletions(-)
include/net/bluetooth/hci.h +2 -0
index 6c414f4..a633da0 100644
@@ -939,6 +939,8 @@ struct hci_rp_le_read_adv_tx_power {
 	__s8	tx_power;
 } __packed;
 
+#define HCI_OP_LE_SET_ADV_ENABLE	0x200a
+
 #define HCI_OP_LE_SET_SCAN_PARAM	0x200b
 struct hci_cp_le_set_scan_param {
 	__u8    type;
include/net/bluetooth/hci_core.h +1 -0
index b3490c6..9d7e94e 100644
@@ -1083,6 +1083,7 @@ int mgmt_set_local_name_complete(struct hci_dev *hdev, u8 *name, u8 status);
 int mgmt_read_local_oob_data_reply_complete(struct hci_dev *hdev, u8 *hash,
 					    u8 *randomizer, u8 status);
 int mgmt_le_enable_complete(struct hci_dev *hdev, u8 enable, u8 status);
+int mgmt_le_adv_enable_complete(struct hci_dev *hdev, u8 enable, u8 status);
 int mgmt_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
 		      u8 addr_type, u8 *dev_class, s8 rssi, u8 cfm_name,
 		      u8 ssp, u8 *eir, u16 eir_len);
include/net/bluetooth/mgmt.h +6 -0
index 22980a7..26c9a4d 100644
@@ -92,6 +92,7 @@ struct mgmt_rp_read_index_list {
 #define MGMT_SETTING_BREDR		0x00000080
 #define MGMT_SETTING_HS			0x00000100
 #define MGMT_SETTING_LE			0x00000200
+#define MGMT_SETTING_PERIPHERAL		0x00000400
 
 #define MGMT_OP_READ_INFO		0x0004
 #define MGMT_READ_INFO_SIZE		0
@@ -134,6 +135,11 @@ struct mgmt_cp_set_discoverable {
 #define MGMT_OP_SET_HS			0x000C
 
 #define MGMT_OP_SET_LE			0x000D
+
+#define MGMT_LE_OFF			0x00
+#define MGMT_LE_CENTRAL			0x01
+#define MGMT_LE_PERIPHERAL		0x02
+
 #define MGMT_OP_SET_DEV_CLASS		0x000E
 struct mgmt_cp_set_dev_class {
 	__u8	major;
net/bluetooth/hci_event.c +57 -0

84 lines not on this page · raw message

net/bluetooth/mgmt.c +138 -28

240 lines not on this page · raw message

Signature · 1 line
1.7.10.4
#27 lore raw

PATCH 6/6 Bluetooth: Add support for setting LE advertising data

In reply to Johan Hedberg

From: Johan Hedberg <[email protected]>

This patch adds support for setting basing LE advertising data. The three elements supported for now are the advertising flags, the TX power and the friendly name.

---
 include/net/bluetooth/hci.h      |   15 ++++++
 include/net/bluetooth/hci_core.h |    2 +
 net/bluetooth/hci_event.c        |    9 ++++
 net/bluetooth/mgmt.c             |  102 ++++++++++++++++++++++++++++++++++++--
 4 files changed, 125 insertions(+), 3 deletions(-)
include/net/bluetooth/hci.h +15 -0
index a633da0..f850e94 100644
@@ -335,6 +335,13 @@ enum {
 #define EIR_SSP_RAND_R		0x0F /* Simple Pairing Randomizer R */
 #define EIR_DEVICE_ID		0x10 /* device ID */
 
+/* Low Energy Advertising Flags */
+#define LE_AD_LIMITED		0x01 /* Limited Discoverable */
+#define LE_AD_GENERAL		0x02 /* General Discoverable */
+#define LE_AD_NO_BREDR		0x04 /* BR/EDR not supported */
+#define LE_AD_SIM_LE_BREDR_CTRL	0x08 /* Simultaneous LE & BR/EDR Controller */
+#define LE_AD_SIM_LE_BREDR_HOST	0x10 /* Simultaneous LE & BR/EDR Host */
+
 /* -----  HCI Commands ---- */
 #define HCI_OP_NOP			0x0000
 
@@ -939,6 +946,14 @@ struct hci_rp_le_read_adv_tx_power {
 	__s8	tx_power;
 } __packed;
 
+#define HCI_MAX_AD_LENGTH		31
+
+#define HCI_OP_LE_SET_ADV_DATA		0x2008
+struct hci_cp_le_set_adv_data {
+	__u8		length;
+	__u8		data[HCI_MAX_AD_LENGTH];
+} __packed;
+
 #define HCI_OP_LE_SET_ADV_ENABLE	0x200a
 
 #define HCI_OP_LE_SET_SCAN_PARAM	0x200b
include/net/bluetooth/hci_core.h +2 -0
index 9d7e94e..0981d3c 100644
@@ -279,6 +279,8 @@ struct hci_dev {
 	struct le_scan_params	le_scan_params;
 
 	__s8			adv_tx_power;
+	__u8			adv_data[HCI_MAX_AD_LENGTH];
+	__u8			adv_data_len;
 
 	int (*open)(struct hci_dev *hdev);
 	int (*close)(struct hci_dev *hdev);
net/bluetooth/hci_event.c +9 -0
index ed6b1e2..3691251 100644
@@ -1108,6 +1108,15 @@ static void hci_cc_le_read_adv_tx_power(struct hci_dev *hdev,
 	if (!rp->status)
 		hdev->adv_tx_power = rp->tx_power;
 
+	if (hdev->adv_data_len > 0) {
+		struct hci_cp_le_set_adv_data cp;
+
+		memcpy(cp.data, hdev->adv_data, sizeof(cp.data));
+		cp.length = cpu_to_le16(hdev->adv_data_len);
+
+		hci_send_cmd(hdev, HCI_OP_LE_SET_ADV_DATA, sizeof(cp), &cp);
+	}
+
 	hci_req_complete(hdev, HCI_OP_LE_READ_ADV_TX_POWER, rp->status);
 }
 
net/bluetooth/mgmt.c +99 -3

143 lines not on this page · raw message

Signature · 1 line
1.7.10.4
maintainer Acked-by on v1 1/6
#2 lore raw

Re: [PATCH 1/6] Bluetooth: Fix setting host feature bits for SSP

In reply to Johan Hedberg

Hi Johan,

Quoted 7 lines

When we get a successful command complete for HCI_Write_SSP_Mode we need to update the host feature bits for the hdev struct accordingly.

---
 net/bluetooth/hci_event.c |   13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

Regards

Marcel

maintainer Acked-by on v1 2/6
#4 lore raw

Re: [PATCH 2/6] Bluetooth: Fix sending unnecessary HCI_Write_SSP_Mode command

In reply to Johan Hedberg

Hi Johan,

Quoted 9 lines

This patch fixes sending an unnecessary HCI_Write_SSP_Mode command if the command has already been sent as part of the default HCI init sequence.

---
 include/net/bluetooth/hci_core.h |    1 +
 net/bluetooth/mgmt.c             |    3 ++-
 2 files changed, 3 insertions(+), 1 deletion(-)

Regards

Marcel

maintainer Acked-by on v1 3/6
#6 lore raw

Re: [PATCH 3/6] Bluetooth: Fix sending unnecessary HCI_LE_Host_Enable

In reply to Johan Hedberg

Hi Johan,

Quoted 8 lines

This patch fixes sending an unnecessary HCI_LE_Host_Enable command if the command has already been sent as part of the default HCI init sequence.

---
 net/bluetooth/mgmt.c |   10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

Regards

Marcel

maintainer Acked-by on v1 4/6
#12 lore raw

Re: [PATCH 4/6] Bluetooth: Fix unnecessary EIR update during powering on

In reply to Johan Hedberg

Hi Johan,

Quoted 8 lines

When powered on the EIR data gets updated as the last step by mgmt. Therefore avoid an update when getting a local name update as that's part of the normal HCI init sequence.

---
 net/bluetooth/mgmt.c |    7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

Regards

Marcel

maintainer
#28 lore raw
In reply to Johan Hedberg

Hi Johan,

Quoted 4 lines

This set of patches contains some basic fixes to SSP and LE enablement HCI commands as well as the two missing patches from my previous set, i.e. implementation of mgmt_set_le for peripheral mode as well as the writing of advertising data.

patches 1-4 look good to me. With 5-6, I would hold off a little bit until we get this all figured out.

However we might should merge some of the basic HCI constants and structs you need for advertisement handling. So that Aloisio's and your patches do not conflict that much.

Regards

Marcel

#15 lore raw

Re: [PATCH 5/6] Bluetooth: mgmt: Add support for switching to LE peripheral mode

In reply to Johan Hedberg

Hi Johan,

Quoted 17 lines · Johan Hedberg
net/bluetooth/hci_event.c +9 -0
index dc60d31..ed6b1e2 100644
@@ -790,9 +790,24 @@ static void hci_set_le_support(struct hci_dev *hdev)
 		cp.simul = !!lmp_le_br_capable(hdev);
 	}
 
+	/* If the host features don't reflect the desired state for LE
+	 * then send the write_le_host_supported command. The command
+	 * complete handler for it will take care of any necessary
+	 * subsequent commands like set_adv_enable.
+	 *
+	 * If the host features for LE are already correct and
+	 * peripheral mode is enabled directly send the le_set_adv
+	 * command. The value of &cp.le is used so that advertising will
+	 * not be enabled in the exceptional case that LE for some

net/bluetooth/hci_event.c static void hci_set_le_support(struct hci_dev *hdev) · line 811

Quoted 10 lines
net/bluetooth/hci_event.c +6 -0
+	 * reason isn't enabled - something that should only be possible
+	 * if someone is doing direct raw access to HCI.
+	 */
 	if (cp.le != !!lmp_host_le_capable(hdev))
 		hci_send_cmd(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(cp),
 			     &cp);
+	else if (test_bit(HCI_LE_PERIPHERAL, &hdev->dev_flags))
+		hci_send_cmd(hdev, HCI_OP_LE_SET_ADV_ENABLE, sizeof(cp.le),
+			     &cp.le);
 }

I agree with Marcel, and one point that worried me was this unconditional advertising, I feel that we should be smarter about when to start advertising, for example, here we are not taking into account the user's visiblity settings.

Cheers,

Signature · 1 line
Vinicius
#16 lore raw

Re: [PATCH 5/6] Bluetooth: mgmt: Add support for switching to LE peripheral mode

Hi Vinicius,

Quoted 25 lines · Vinicius Costa Gomes
Quoted 24 lines · Johan Hedberg
net/bluetooth/hci_event.c +13 -0
index dc60d31..ed6b1e2 100644
@@ -790,9 +790,24 @@ static void hci_set_le_support(struct hci_dev *hdev)
 		cp.simul = !!lmp_le_br_capable(hdev);
 	}
 
+	/* If the host features don't reflect the desired state for LE
+	 * then send the write_le_host_supported command. The command
+	 * complete handler for it will take care of any necessary
+	 * subsequent commands like set_adv_enable.
+	 *
+	 * If the host features for LE are already correct and
+	 * peripheral mode is enabled directly send the le_set_adv
+	 * command. The value of &cp.le is used so that advertising will
+	 * not be enabled in the exceptional case that LE for some
+	 * reason isn't enabled - something that should only be possible
+	 * if someone is doing direct raw access to HCI.
+	 */
 	if (cp.le != !!lmp_host_le_capable(hdev))
 		hci_send_cmd(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(cp),
 			     &cp);
+	else if (test_bit(HCI_LE_PERIPHERAL, &hdev->dev_flags))
Quoted 8 lines

net/bluetooth/hci_event.c static void hci_set_le_support(struct hci_dev *hdev) · line 811

Quoted 3 lines
net/bluetooth/hci_event.c +2 -0
+		hci_send_cmd(hdev, HCI_OP_LE_SET_ADV_ENABLE, sizeof(cp.le),
+			     &cp.le);
 }

I agree with Marcel, and one point that worried me was this unconditional advertising, I feel that we should be smarter about when to start advertising, for example, here we are not taking into account the user's visiblity settings.

If you're a peripheral and you're not connectable/discoverable then what good does it do to have Bluetooth enabled at all? Since a peripheral can't scan or initiate connections you can't really do anything. So if a higher layer doesn't want us advertising it could either set LE to off or central role or even power off the adapter completely.

Johan

#17 lore raw

Re: [PATCH 5/6] Bluetooth: mgmt: Add support for switching to LE peripheral mode

Hi Johan,

Quoted 37 lines · Johan Hedberg

Hi Vinicius,

Quoted 25 lines · Vinicius Costa Gomes
Quoted 24 lines · Johan Hedberg
net/bluetooth/hci_event.c +13 -0
index dc60d31..ed6b1e2 100644
@@ -790,9 +790,24 @@ static void hci_set_le_support(struct hci_dev *hdev)
 		cp.simul = !!lmp_le_br_capable(hdev);
 	}
 
+	/* If the host features don't reflect the desired state for LE
+	 * then send the write_le_host_supported command. The command
+	 * complete handler for it will take care of any necessary
+	 * subsequent commands like set_adv_enable.
+	 *
+	 * If the host features for LE are already correct and
+	 * peripheral mode is enabled directly send the le_set_adv
+	 * command. The value of &cp.le is used so that advertising will
+	 * not be enabled in the exceptional case that LE for some
+	 * reason isn't enabled - something that should only be possible
+	 * if someone is doing direct raw access to HCI.
+	 */
 	if (cp.le != !!lmp_host_le_capable(hdev))
 		hci_send_cmd(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(cp),
 			     &cp);
+	else if (test_bit(HCI_LE_PERIPHERAL, &hdev->dev_flags))
Quoted 8 lines

net/bluetooth/hci_event.c static void hci_set_le_support(struct hci_dev *hdev) · line 811

Quoted 3 lines
net/bluetooth/hci_event.c +2 -0
+		hci_send_cmd(hdev, HCI_OP_LE_SET_ADV_ENABLE, sizeof(cp.le),
+			     &cp.le);
 }

I agree with Marcel, and one point that worried me was this unconditional advertising, I feel that we should be smarter about when to start advertising, for example, here we are not taking into account the user's visiblity settings.

Quoted 5 lines

If you're a peripheral and you're not connectable/discoverable then what good does it do to have Bluetooth enabled at all? Since a peripheral can't scan or initiate connections you can't really do anything. So if a higher layer doesn't want us advertising it could either set LE to off or central role or even power off the adapter completely.

And what about the device that receive these advertising events? It won't be able to do anything with them. And we would be increasing the risk of interfering with the communication of others.

For me, the LE peripheral mode setting has a longer lifetime than (and is orthogonal to) the Discoverable/Connectable settings. So, I would enable Peripheral mode once during the lifetime of my session, and control the type and whether I am advertising using the Connectable/Discoverable settings (perhaps even from the Apps that are listening for connections).

Having to change the role of the device to the Central role or turning the controller off (which would mean to re-do a lot of the initialization when I turn it on again) to stop advertising seems, at least, not intuitive.

Then, the only way to send not Discoverable nor Connectable advertising events would be with the Broadcaster role.

Quoted 2 lines

Johan

Cheers,

Signature · 1 line
Vinicius
maintainer
#18 lore raw

Re: [PATCH 5/6] Bluetooth: mgmt: Add support for switching to LE peripheral mode

Hi Vinicius,

Quoted 50 lines
Quoted 34 lines
Quoted 25 lines · Johan Hedberg
Quoted 24 lines
net/bluetooth/hci_event.c +13 -0
index dc60d31..ed6b1e2 100644
@@ -790,9 +790,24 @@ static void hci_set_le_support(struct hci_dev *hdev)
 		cp.simul = !!lmp_le_br_capable(hdev);
 	}
 
+	/* If the host features don't reflect the desired state for LE
+	 * then send the write_le_host_supported command. The command
+	 * complete handler for it will take care of any necessary
+	 * subsequent commands like set_adv_enable.
+	 *
+	 * If the host features for LE are already correct and
+	 * peripheral mode is enabled directly send the le_set_adv
+	 * command. The value of &cp.le is used so that advertising will
+	 * not be enabled in the exceptional case that LE for some
+	 * reason isn't enabled - something that should only be possible
+	 * if someone is doing direct raw access to HCI.
+	 */
 	if (cp.le != !!lmp_host_le_capable(hdev))
 		hci_send_cmd(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(cp),
 			     &cp);
+	else if (test_bit(HCI_LE_PERIPHERAL, &hdev->dev_flags))
Quoted 8 lines

net/bluetooth/hci_event.c static void hci_set_le_support(struct hci_dev *hdev) · line 811

Quoted 3 lines
net/bluetooth/hci_event.c +2 -0
+		hci_send_cmd(hdev, HCI_OP_LE_SET_ADV_ENABLE, sizeof(cp.le),
+			     &cp.le);
 }

I agree with Marcel, and one point that worried me was this unconditional advertising, I feel that we should be smarter about when to start advertising, for example, here we are not taking into account the user's visiblity settings.

Quoted 5 lines

If you're a peripheral and you're not connectable/discoverable then what good does it do to have Bluetooth enabled at all? Since a peripheral can't scan or initiate connections you can't really do anything. So if a higher layer doesn't want us advertising it could either set LE to off or central role or even power off the adapter completely.

And what about the device that receive these advertising events? It won't be able to do anything with them. And we would be increasing the risk of interfering with the communication of others.

For me, the LE peripheral mode setting has a longer lifetime than (and is orthogonal to) the Discoverable/Connectable settings. So, I would enable Peripheral mode once during the lifetime of my session, and control the type and whether I am advertising using the Connectable/Discoverable settings (perhaps even from the Apps that are listening for connections).

Quoted 7 lines

Having to change the role of the device to the Central role or turning the controller off (which would mean to re-do a lot of the initialization when I turn it on again) to stop advertising seems, at least, not intuitive.

Then, the only way to send not Discoverable nor Connectable advertising events would be with the Broadcaster role.

that is actually not my worries. That is something that bluetoothd will handle for the applications. And I do not want to intermix connectable and discoverable from BR/EDR. These are two different things.

The way I see it currently is that peripheral mode is tight to an application that is current providing a service. Or even a plugin inside bluetoothd.

I am currently almost leading with Johan here that once peripheral mode is enabled, we should just advertise. If you do not want to advertise, then just disable peripheral mode.

The one thing I want to have figured out before we go ahead with peripheral support is of course broadcaster and observer support.

Regards

Marcel

#19 lore raw

Re: [PATCH 5/6] Bluetooth: mgmt: Add support for switching to LE peripheral mode

Hi Marcel,

Quoted 57 lines · Marcel Holtmann

Hi Vinicius,

Quoted 55 lines
Quoted 34 lines
Quoted 25 lines · Johan Hedberg
Quoted 24 lines
net/bluetooth/hci_event.c +13 -0
index dc60d31..ed6b1e2 100644
@@ -790,9 +790,24 @@ static void hci_set_le_support(struct hci_dev *hdev)
 		cp.simul = !!lmp_le_br_capable(hdev);
 	}
 
+	/* If the host features don't reflect the desired state for LE
+	 * then send the write_le_host_supported command. The command
+	 * complete handler for it will take care of any necessary
+	 * subsequent commands like set_adv_enable.
+	 *
+	 * If the host features for LE are already correct and
+	 * peripheral mode is enabled directly send the le_set_adv
+	 * command. The value of &cp.le is used so that advertising will
+	 * not be enabled in the exceptional case that LE for some
+	 * reason isn't enabled - something that should only be possible
+	 * if someone is doing direct raw access to HCI.
+	 */
 	if (cp.le != !!lmp_host_le_capable(hdev))
 		hci_send_cmd(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(cp),
 			     &cp);
+	else if (test_bit(HCI_LE_PERIPHERAL, &hdev->dev_flags))
Quoted 8 lines

net/bluetooth/hci_event.c static void hci_set_le_support(struct hci_dev *hdev) · line 811

Quoted 3 lines
net/bluetooth/hci_event.c +2 -0
+		hci_send_cmd(hdev, HCI_OP_LE_SET_ADV_ENABLE, sizeof(cp.le),
+			     &cp.le);
 }

I agree with Marcel, and one point that worried me was this unconditional advertising, I feel that we should be smarter about when to start advertising, for example, here we are not taking into account the user's visiblity settings.

Quoted 5 lines

If you're a peripheral and you're not connectable/discoverable then what good does it do to have Bluetooth enabled at all? Since a peripheral can't scan or initiate connections you can't really do anything. So if a higher layer doesn't want us advertising it could either set LE to off or central role or even power off the adapter completely.

And what about the device that receive these advertising events? It won't be able to do anything with them. And we would be increasing the risk of interfering with the communication of others.

For me, the LE peripheral mode setting has a longer lifetime than (and is orthogonal to) the Discoverable/Connectable settings. So, I would enable Peripheral mode once during the lifetime of my session, and control the type and whether I am advertising using the Connectable/Discoverable settings (perhaps even from the Apps that are listening for connections).

Having to change the role of the device to the Central role or turning the controller off (which would mean to re-do a lot of the initialization when I turn it on again) to stop advertising seems, at least, not intuitive.

Quoted 6 lines
Quoted 2 lines

Then, the only way to send not Discoverable nor Connectable advertising events would be with the Broadcaster role.

that is actually not my worries. That is something that bluetoothd will handle for the applications. And I do not want to intermix connectable and discoverable from BR/EDR. These are two different things.

Ok. This is new to me. So, what information will we use to set the advertising type and the Flags field in the advertising event (think general/limited discoverable modes)?

Quoted 4 lines

The way I see it currently is that peripheral mode is tight to an application that is current providing a service. Or even a plugin inside bluetoothd.

Makes total sense.

Quoted 4 lines

I am currently almost leading with Johan here that once peripheral mode is enabled, we should just advertise. If you do not want to advertise, then just disable peripheral mode.

Ok. I think I got it. I was with the impression that the Peripheral bit was something like a capability, something that I would set and forget. In reality, it is something meant to be more transient. I would argue that this confusion alone is a point in favor of having a separate command for it. But I guess it is too late now.

So, my worry of not sending useless advertising events would be addressed by bluetoothd. Sounds good enough.

Quoted 10 lines

The one thing I want to have figured out before we go ahead with peripheral support is of course broadcaster and observer support.

Regards

Marcel

Cheers,

Signature · 1 line
Vinicius
#20 lore raw

Re: [PATCH 5/6] Bluetooth: mgmt: Add support for switching to LE peripheral mode

Hi Vinicius,

Quoted 3 lines · Vinicius Costa Gomes

So, what information will we use to set the advertising type and the Flags field in the advertising event (think general/limited discoverable modes)?

Take a look at the create_ad() function in the last patch of this set. Setting the right flags value is really quite simple. The advertising type could be selected in a similar way, i.e. through hdev->dev_flags.

Johan

#21 lore raw

Re: [PATCH 5/6] Bluetooth: mgmt: Add support for switching to LE peripheral mode

Hi Johan,

Quoted 10 lines · Johan Hedberg

Hi Vinicius,

Quoted 3 lines · Vinicius Costa Gomes

So, what information will we use to set the advertising type and the Flags field in the advertising event (think general/limited discoverable modes)?

Take a look at the create_ad() function in the last patch of this set. Setting the right flags value is really quite simple. The advertising type could be selected in a similar way, i.e. through hdev->dev_flags.

I don't see any use now, but "actual" LE peripherals allow to use directed advertising when for instance, they need to send an ATT notification just to a specific device, and not let anyone else connect to them. Do you see an easy way of selecting an specific device to advertise to?

The issue here is that once LE Peripheral is enabled, any device can connect to you (e.g. other previously bonded devices will for sure attempt to reconnect). If you want to be connected by a specific device, you need to use directed advertising from the beginning.

Also is LE advertising re-enabled when a connection is established (remember it is disabled automatically by the controller after connection is established)? And after an HCI Reset ?

Regards,

Signature · 3 lines
Anderson Lizardo
Instituto Nokia de Tecnologia - INdT
Manaus - Brazil
#22 lore raw

Re: [PATCH 5/6] Bluetooth: mgmt: Add support for switching to LE peripheral mode

Hi Lizardo,

Quoted 18 lines · Anderson Lizardo
Quoted 10 lines · Johan Hedberg

Hi Vinicius,

Quoted 3 lines · Vinicius Costa Gomes

So, what information will we use to set the advertising type and the Flags field in the advertising event (think general/limited discoverable modes)?

Take a look at the create_ad() function in the last patch of this set. Setting the right flags value is really quite simple. The advertising type could be selected in a similar way, i.e. through hdev->dev_flags.

I don't see any use now, but "actual" LE peripherals allow to use directed advertising when for instance, they need to send an ATT notification just to a specific device, and not let anyone else connect to them. Do you see an easy way of selecting an specific device to advertise to?

Quoted 4 lines

The issue here is that once LE Peripheral is enabled, any device can connect to you (e.g. other previously bonded devices will for sure attempt to reconnect). If you want to be connected by a specific device, you need to use directed advertising from the beginning.

This will need some more thinking. We could add a new mgmt command for that, or if you wanna go crazy why not map this to the L2CAP socket interface's connect() system call. I.e. if LE GAP is in peripheral mode instead of doing HCI_LE_Create_Connection or just failing with "not allowed" a socket connect() would simply trigger directed advertising to the device in question and deliver the successful connection in the same way as a central role triggered connect would do (unless we time out waiting for the remote device to connect). Now that I think of this second option it actually sounds quite natural and not so crazy after all :)

Quoted 3 lines

Also is LE advertising re-enabled when a connection is established (remember it is disabled automatically by the controller after connection is established)? And after an HCI Reset ?

The "after a connection" part is still missing and I'll have that as a separate patch not to grow the already quite big patch. As for reset, right now that's only sent when power-cycling the adapter and the HCI init sequence then takes care of enabling advertising.

Johan

#23 lore raw

Re: [PATCH 5/6] Bluetooth: mgmt: Add support for switching to LE peripheral mode

Hi,

Quoted 10 lines · Johan Hedberg

This will need some more thinking. We could add a new mgmt command for that, or if you wanna go crazy why not map this to the L2CAP socket interface's connect() system call. I.e. if LE GAP is in peripheral mode instead of doing HCI_LE_Create_Connection or just failing with "not allowed" a socket connect() would simply trigger directed advertising to the device in question and deliver the successful connection in the same way as a central role triggered connect would do (unless we time out waiting for the remote device to connect). Now that I think of this second option it actually sounds quite natural and not so crazy after all :)

There's on problem with this though: we'd still be undirected connectable between doing mgmt_set_le(peripheral) and issuing the socket connect(). So maybe we might need to introduce a mgmt_set_le_connectable command and a "le-connectable" setting after all (that could only be set in peripheral mode).

Johan

#24 lore raw

Re: [PATCH 5/6] Bluetooth: mgmt: Add support for switching to LE peripheral mode

Hi,

Quoted 14 lines · Johan Hedberg

Hi,

Quoted 10 lines · Johan Hedberg

This will need some more thinking. We could add a new mgmt command for that, or if you wanna go crazy why not map this to the L2CAP socket interface's connect() system call. I.e. if LE GAP is in peripheral mode instead of doing HCI_LE_Create_Connection or just failing with "not allowed" a socket connect() would simply trigger directed advertising to the device in question and deliver the successful connection in the same way as a central role triggered connect would do (unless we time out waiting for the remote device to connect). Now that I think of this second option it actually sounds quite natural and not so crazy after all :)

Quoted 5 lines

There's on problem with this though: we'd still be undirected connectable between doing mgmt_set_le(peripheral) and issuing the socket connect(). So maybe we might need to introduce a mgmt_set_le_connectable command and a "le-connectable" setting after all (that could only be set in peripheral mode).

Why not sending undirected connectable events when there's an active listen()?

Quoted 2 lines

Johan

Cheers,

Signature · 1 line
Vinicius
#7 lore raw

Re: [PATCH 3/6] Bluetooth: Fix sending unnecessary HCI_LE_Host_Enable

In reply to Johan Hedberg

Hi Johan,

Quoted 16 lines · Johan Hedberg

From: Johan Hedberg <[email protected]>

This patch fixes sending an unnecessary HCI_LE_Host_Enable command if the command has already been sent as part of the default HCI init sequence.

---
 net/bluetooth/mgmt.c |   10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
net/bluetooth/mgmt.c +0 -0
index 02dc5f8..03dc176 100644

net/bluetooth/mgmt.c int mgmt_powered(struct hci_dev *hdev, u8 powered) · line 2934

Quoted 10 lines
net/bluetooth/mgmt.c +5 -2
@@ -2927,8 +2927,14 @@ int mgmt_powered(struct hci_dev *hdev, u8 powered)
 			cp.le = 1;
 			cp.simul = !!lmp_le_br_capable(hdev);
 
-			hci_send_cmd(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED,
-				     sizeof(cp), &cp);
+			/* Check first if we already have the right
+			 * host state (host features set)
+			 */
+			if (cp.le != !!lmp_host_le_capable(hdev) ||
+			    cp.simul != !!lmp_host_le_br_capable(hdev))

Shouldn't the !! be part of the macro itself? Looks we will be using !! always with these macros.

Gustavo

#25 lore raw

Re: [PATCH 5/6] Bluetooth: mgmt: Add support for switching to LE peripheral mode

In reply to Vinicius Costa Gomes

Hi Vinicius,

Quoted 12 lines · Vinicius Costa Gomes
Quoted 12 lines · Johan Hedberg
Quoted 10 lines

This will need some more thinking. We could add a new mgmt command for that, or if you wanna go crazy why not map this to the L2CAP socket interface's connect() system call. I.e. if LE GAP is in peripheral mode instead of doing HCI_LE_Create_Connection or just failing with "not allowed" a socket connect() would simply trigger directed advertising to the device in question and deliver the successful connection in the same way as a central role triggered connect would do (unless we time out waiting for the remote device to connect). Now that I think of this second option it actually sounds quite natural and not so crazy after all :)

Quoted 8 lines
Quoted 5 lines

There's on problem with this though: we'd still be undirected connectable between doing mgmt_set_le(peripheral) and issuing the socket connect(). So maybe we might need to introduce a mgmt_set_le_connectable command and a "le-connectable" setting after all (that could only be set in peripheral mode).

Why not sending undirected connectable events when there's an active listen()?

But we always have that, don't we? (the GATT server socket). Or are you saying that bluetoothd could close its GATT server socket before switching to peripheral mode and that would ensure that the kernel doesn't enable connectable advertising?

Johan

maintainer
#8 lore raw

Re: [PATCH 3/6] Bluetooth: Fix sending unnecessary HCI_LE_Host_Enable

In reply to Gustavo Padovan

Hi Gustavo,

Quoted 19 lines
Quoted 19 lines

From: Johan Hedberg <[email protected]>

This patch fixes sending an unnecessary HCI_LE_Host_Enable command if the command has already been sent as part of the default HCI init sequence.

---
 net/bluetooth/mgmt.c |   10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
net/bluetooth/mgmt.c +0 -0
index 02dc5f8..03dc176 100644
@@ -2927,8 +2927,14 @@ int mgmt_powered(struct hci_dev *hdev, u8 powered)
 			cp.le = 1;
 			cp.simul = !!lmp_le_br_capable(hdev);
 
Quoted 10 lines

net/bluetooth/mgmt.c int mgmt_powered(struct hci_dev *hdev, u8 powered) · line 2934

Quoted 7 lines
net/bluetooth/mgmt.c +5 -2
-			hci_send_cmd(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED,
-				     sizeof(cp), &cp);
+			/* Check first if we already have the right
+			 * host state (host features set)
+			 */
+			if (cp.le != !!lmp_host_le_capable(hdev) ||
+			    cp.simul != !!lmp_host_le_br_capable(hdev))

Shouldn't the !! be part of the macro itself? Looks we will be using !! always with these macros.

we could do that actually. However I prefer to do that with a separate patch.

Regards

Marcel

#13 lore raw

Re: [PATCH 4/6] Bluetooth: Fix unnecessary EIR update during powering on

In reply to Johan Hedberg

Hi Johan,

Quoted 10 lines · Johan Hedberg

From: Johan Hedberg <[email protected]>

When powered on the EIR data gets updated as the last step by mgmt. Therefore avoid an update when getting a local name update as that's part of the normal HCI init sequence.

---
 net/bluetooth/mgmt.c |    7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

Patch 1, 2 and 4 have been applied to bluetooth-next. Thanks.

Gustavo

#9 lore raw

Re: [PATCH 3/6] Bluetooth: Fix sending unnecessary HCI_LE_Host_Enable

In reply to Marcel Holtmann

Hi,

Quoted 30 lines · Marcel Holtmann
Quoted 19 lines
Quoted 19 lines

From: Johan Hedberg <[email protected]>

This patch fixes sending an unnecessary HCI_LE_Host_Enable command if the command has already been sent as part of the default HCI init sequence.

---
 net/bluetooth/mgmt.c |   10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
net/bluetooth/mgmt.c +0 -0
index 02dc5f8..03dc176 100644
@@ -2927,8 +2927,14 @@ int mgmt_powered(struct hci_dev *hdev, u8 powered)
 			cp.le = 1;
 			cp.simul = !!lmp_le_br_capable(hdev);
 
Quoted 10 lines

net/bluetooth/mgmt.c int mgmt_powered(struct hci_dev *hdev, u8 powered) · line 2934

Quoted 7 lines
net/bluetooth/mgmt.c +5 -2
-			hci_send_cmd(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED,
-				     sizeof(cp), &cp);
+			/* Check first if we already have the right
+			 * host state (host features set)
+			 */
+			if (cp.le != !!lmp_host_le_capable(hdev) ||
+			    cp.simul != !!lmp_host_le_br_capable(hdev))

Shouldn't the !! be part of the macro itself? Looks we will be using !! always with these macros.

Quoted 2 lines

we could do that actually. However I prefer to do that with a separate patch.

I was thinking of the same earlier, and yes a separate patch to convert all of the feature test macros would be the cleanest.

Johan

#10 lore raw

Re: [PATCH 3/6] Bluetooth: Fix sending unnecessary HCI_LE_Host_Enable

Hi,

Quoted 33 lines · Johan Hedberg

Hi,

Quoted 30 lines · Marcel Holtmann
Quoted 29 lines
Quoted 16 lines

From: Johan Hedberg <[email protected]>

This patch fixes sending an unnecessary HCI_LE_Host_Enable command if the command has already been sent as part of the default HCI init sequence.

---
 net/bluetooth/mgmt.c |   10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
net/bluetooth/mgmt.c +0 -0
index 02dc5f8..03dc176 100644

net/bluetooth/mgmt.c int mgmt_powered(struct hci_dev *hdev, u8 powered) · line 2934

Quoted 10 lines
net/bluetooth/mgmt.c +5 -2
@@ -2927,8 +2927,14 @@ int mgmt_powered(struct hci_dev *hdev, u8 powered)
 			cp.le = 1;
 			cp.simul = !!lmp_le_br_capable(hdev);
 
-			hci_send_cmd(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED,
-				     sizeof(cp), &cp);
+			/* Check first if we already have the right
+			 * host state (host features set)
+			 */
+			if (cp.le != !!lmp_host_le_capable(hdev) ||
+			    cp.simul != !!lmp_host_le_br_capable(hdev))

Shouldn't the !! be part of the macro itself? Looks we will be using !! always with these macros.

Quoted 5 lines
Quoted 2 lines

we could do that actually. However I prefer to do that with a separate patch.

I was thinking of the same earlier, and yes a separate patch to convert all of the feature test macros would be the cleanest.

I didn't say it needed to be in the same patch. Anyway, I applied this patch now, I'll do the macro's conversion later today.

Gustavo

#26 lore raw

Re: [PATCH 5/6] Bluetooth: mgmt: Add support for switching to LE peripheral mode

In reply to Johan Hedberg

Hi Johan,

Quoted 21 lines · Johan Hedberg

Hi Vinicius,

Quoted 18 lines · Vinicius Costa Gomes
Quoted 17 lines · Johan Hedberg
Quoted 10 lines

This will need some more thinking. We could add a new mgmt command for that, or if you wanna go crazy why not map this to the L2CAP socket interface's connect() system call. I.e. if LE GAP is in peripheral mode instead of doing HCI_LE_Create_Connection or just failing with "not allowed" a socket connect() would simply trigger directed advertising to the device in question and deliver the successful connection in the same way as a central role triggered connect would do (unless we time out waiting for the remote device to connect). Now that I think of this second option it actually sounds quite natural and not so crazy after all :)

There's on problem with this though: we'd still be undirected connectable between doing mgmt_set_le(peripheral) and issuing the socket connect(). So maybe we might need to introduce a mgmt_set_le_connectable command and a "le-connectable" setting after all (that could only be set in peripheral mode).

Quoted 7 lines
Quoted 2 lines

Why not sending undirected connectable events when there's an active listen()?

But we always have that, don't we? (the GATT server socket). Or are you saying that bluetoothd could close its GATT server socket before switching to peripheral mode and that would ensure that the kernel doesn't enable connectable advertising?

Right now bluetoothd always has a listen() active, yes. But remember that that listen() was added only to help testing, in the case that BlueZ always initiates connections that listen() is not needed.

What I am thinking is having more control about when to advertise, for example, each profile that needs the peripheral role would increase the reference of a listener, when all the references are dropped I stop advertising.

Quoted 2 lines

Johan

Cheers,

Signature · 1 line
Vinicius