LE auto connection and connection parameters
Subsystems (2)
v4 cover LE auto connection and connection parameters
Hi all,
This v4 is the same patch set from v3 but rebased on top of current bluetooth-next tree.
Below follows v3 cover letter:
The main changes from the previous version are:
* Debugfs interface to add auto connect address instead of new mgmt commands.
* We always stop LE scanning in favor of connection establishment even if the controller supports scanning and connection at the same time.
* Background scanning is now controlled in one single place (hci_update_ background_scan function).
* RCU was replaced by hdev->lock to protect hdev->le_conn_params list. After all the changes since the original version of this patch set, I realized we always operate on hdev->le_conn_params with hdev->lock held so there is no point in use RCU to protect this list.
In order to test the LE auto connection mechanism please follow the instructions below.
To add a new auto connection address we write on le_auto_conn file following the format <address> <address type> <auto_connect>.
The <address type> values are:
* 0 for public address
* 1 for random address
The <auto_connect> values are (for more details see struct hci_conn_params):
* 0 for disabled
* 1 for always
* 2 for link loss
So for instance, if you want the kernel autonomously establishes connections with device AA:BB:CC:DD:EE:FF (public address) every time the device enters in connectable mode (starts advertising), you should run the command:
$ echo "AA:BB:CC:DD:EE:FF 0 1" > /sys/kernel/debug/bluetooth/hci0/le_auto_conn
To get the list of connection parameters configured in kernel, read the le_auto_conn file:
$ cat /sys/kernel/debug/bluetooth/hci0/le_auto_conn
Finally, to clear the connection parameters list, write an empty string:
$ echo "" > /sys/kernel/debug/bluetooth/hci0/le_auto_conn
BR,
Andre
Andre Guedes (12):
Bluetooth: Save connection interval parameters in hci_conn
Bluetooth: Group list_head fields from strcut hci_dev together
Bluetooth: Introduce connection parameters list
Bluetooth: Use connection parameters if any
Bluetooth: Stop scanning on LE connection
Bluetooth: Introduce hdev->pend_le_conn list
Bluetooth: Introduce LE auto connection infrastructure
Bluetooth: Re-enable background scan in case of error
Bluetooth: Temporarily stop background scanning on discovery
Bluetooth: Auto connection and power on
Bleutooth: Add support for auto connect options
Bluetooth: Add le_auto_conn file on debugfs
include/net/bluetooth/hci_core.h | 43 +++++- net/bluetooth/hci_conn.c | 39 ++++- net/bluetooth/hci_core.c | 318 +++++++++++++++++++++++++++++++++++++++ net/bluetooth/hci_event.c | 60 ++++++++ net/bluetooth/mgmt.c | 25 ++- 5 files changed, 473 insertions(+), 12 deletions(-)
Signature · 1 line
1.8.4.2
RFC v4 1/12 Bluetooth: Save connection interval parameters in hci_conn
This patch creates two new fields in struct hci_conn to save the minimum and maximum connection interval values used to establish the connection this object represents.
This change is required in order to know what parameters the connection is currently using.
--- include/net/bluetooth/hci_core.h | 2 ++ net/bluetooth/hci_conn.c | 6 ++++-- 2 files changed, 6 insertions(+), 2 deletions(-)
include/net/bluetooth/hci_core.h +2 -0
@@ -332,6 +332,8 @@ struct hci_conn {__u8 passkey_entered;__u16 disc_timeout;__u16 setting; +__u16 le_conn_min_interval; +__u16 le_conn_max_interval;unsigned long flags;__u8 remote_cap;
net/bluetooth/hci_conn.c +4 -2
@@ -558,8 +558,8 @@ static int hci_create_le_conn(struct hci_conn *conn)bacpy (&cp.peer_addr, &conn->dst); cp.peer_addr_type = conn->dst_type; cp.own_address_type = conn->src_type; - cp.conn_interval_min =cpu_to_le16 (hdev->le_conn_min_interval); - cp.conn_interval_max =cpu_to_le16 (hdev->le_conn_max_interval); + cp.conn_interval_min =cpu_to_le16 (conn->le_conn_min_interval); + cp.conn_interval_max =cpu_to_le16 (conn->le_conn_max_interval); cp.supervision_timeout =__constant_cpu_to_le16 (0x002a ); cp.min_ce_len =__constant_cpu_to_le16 (0x0000 ); cp.max_ce_len =__constant_cpu_to_le16 (0x0000 );
@@ -624,6 +624,8 @@ static struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst, conn->sec_level =BT_SECURITY_LOW ; conn->pending_sec_level = sec_level; conn->auth_type = auth_type; + conn->le_conn_min_interval = hdev->le_conn_min_interval; + conn->le_conn_max_interval = hdev->le_conn_max_interval; err =hci_create_le_conn (conn);if (err)
Signature · 1 line
1.8.4.2
RFC v4 2/12 Bluetooth: Group list_head fields from strcut hci_dev together
In reply to Andre GuedesThis patch groups the list_head fields from struct hci_dev together and removes empty lines between them.
--- include/net/bluetooth/hci_core.h | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)
include/net/bluetooth/hci_core.h +2 -5
@@ -257,18 +257,15 @@ struct hci_dev {__u32 req_status;__u32 req_result; -struct list_head mgmt_pending;struct discovery_state discovery;struct hci_conn_hash conn_hash; -struct list_head blacklist; +struct list_head mgmt_pending; +struct list_head blacklist;struct list_head uuids; -struct list_head link_keys; -struct list_head long_term_keys; -struct list_head remote_oob_data;struct hci_dev_stats stat;
Signature · 1 line
1.8.4.2
RFC v4 3/12 Bluetooth: Introduce connection parameters list
In reply to Andre GuedesThis patch adds to hdev the connection parameters list (hdev->le_ conn_params). The elements from this list (struct hci_conn_params) contains the connection parameters (for now, minimum and maximum connection interval) that should be used during the connection establishment.
The struct hci_conn_params also defines the 'auto_connect' field which will be used to implement the auto connection mechanism.
Moreover, this patch adds helper functions to manipulate hdev->le_ conn_params list. Some of these functions are also declared in hci_core.h since they will be used outside hci_core.c in upcoming patches.
--- include/net/bluetooth/hci_core.h | 25 +++++++++++++ net/bluetooth/hci_core.c | 80 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+)
include/net/bluetooth/hci_core.h +25 -0
@@ -267,6 +267,7 @@ struct hci_dev {struct list_head link_keys;struct list_head long_term_keys;struct list_head remote_oob_data; +struct list_head le_conn_params;struct hci_dev_stats stat;
@@ -371,6 +372,22 @@ struct hci_chan {__u8 state; }; +struct hci_conn_params { +struct list_head list; + +bdaddr_t addr; +u8 addr_type; + +enum { +HCI_AUTO_CONN_DISABLED , +HCI_AUTO_CONN_ALWAYS , +HCI_AUTO_CONN_LINK_LOSS , + } auto_connect; + +u16 conn_min_interval; +u16 conn_max_interval; +}; +extern struct list_head hci_dev_list;extern struct list_head hci_cb_list;extern rwlock_t hci_dev_list_lock;
@@ -739,6 +756,14 @@ int hci_blacklist_clear(struct hci_dev *hdev);int hci_blacklist_add (struct hci_dev *hdev,bdaddr_t *bdaddr,u8 type);int hci_blacklist_del (struct hci_dev *hdev,bdaddr_t *bdaddr,u8 type); +struct hci_conn_params *hci_conn_params_lookup (struct hci_dev *hdev, +bdaddr_t *addr,u8 addr_type); +void hci_conn_params_add (struct hci_dev *hdev,bdaddr_t *addr,u8 addr_type, +u8 auto_connect,u16 conn_min_interval, +u16 conn_max_interval); +void hci_conn_params_del (struct hci_dev *hdev,bdaddr_t *addr,u8 addr_type); +void hci_conn_params_clear (struct hci_dev *hdev); +int hci_uuids_clear (struct hci_dev *hdev);int hci_link_keys_clear (struct hci_dev *hdev);
net/bluetooth/hci_core.c +80 -0
98 lines not on this page · raw message
Signature · 1 line
1.8.4.2
RFC v4 4/12 Bluetooth: Use connection parameters if any
In reply to Andre GuedesThis patch changes hci_connect_le() so it uses the connection parameters specified for the certain device. If no parameters were configured, we use the default values.
--- net/bluetooth/hci_conn.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)
net/bluetooth/hci_conn.c +10 -2
@@ -578,6 +578,7 @@ static int hci_create_le_conn(struct hci_conn *conn)static struct hci_conn *hci_connect_le (struct hci_dev *hdev,bdaddr_t *dst,u8 dst_type,u8 sec_level,u8 auth_type) { +struct hci_conn_params *params;struct hci_conn *conn;int err;
@@ -624,8 +625,15 @@ static struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst, conn->sec_level =BT_SECURITY_LOW ; conn->pending_sec_level = sec_level; conn->auth_type = auth_type; - conn->le_conn_min_interval = hdev->le_conn_min_interval; - conn->le_conn_max_interval = hdev->le_conn_max_interval; + + params =hci_conn_params_lookup (hdev, &conn->dst, conn->dst_type); +if (params) { + conn->le_conn_min_interval = params->conn_min_interval; + conn->le_conn_max_interval = params->conn_max_interval; + }else { + conn->le_conn_min_interval = hdev->le_conn_min_interval; + conn->le_conn_max_interval = hdev->le_conn_max_interval; + } err =hci_create_le_conn (conn);if (err)
Signature · 1 line
1.8.4.2
RFC v4 5/12 Bluetooth: Stop scanning on LE connection
In reply to Andre GuedesSome LE controllers don't support scanning and creating a connection at the same time. So we should always stop scanning in order to establish the connection.
Since we may prematurely stop the discovery procedure in favor of the connection establishment, we should also cancel hdev->le_scan_ disable delayed work and set the discovery state to DISCOVERY_STOPPED.
This change does a small improvement since it is not mandatory the user stops scanning before connecting anymore. Moreover, this change is required by upcoming LE auto connection mechanism in order to work properly with controllers that don't support background scanning and connection establishment at the same time.
In future, we might want to do a small optimization by checking if controller is able to scan and connect at the same time. For now, we want the simplest approach so we always stop scanning (even if the controller is able to carry out both operations).
--- net/bluetooth/hci_conn.c | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-)
net/bluetooth/hci_conn.c +22 -1
@@ -518,8 +518,17 @@ static void create_le_conn_complete(struct hci_dev *hdev, u8 status) {struct hci_conn *conn; -if (status ==0 ) +if (status ==0 ) { +/* If the discovery procedure was running, we prematurely +* stopped it. So we have to change the discovery state. +*/ +if (hdev->discovery.state ==DISCOVERY_FINDING ) { +cancel_delayed_work (&hdev->le_scan_disable); +hci_discovery_set_state (hdev,DISCOVERY_STOPPED ); + } +return ; + }BT_ERR ("HCI request failed to create LE connection: status 0x%2.2x" , status);
@@ -552,6 +561,18 @@ static int hci_create_le_conn(struct hci_conn *conn)hci_req_init (&req, hdev); +/* If controller is scanning, we stop it since some controllers are +* not able to scan and connect at the same time. +*/ +if (test_bit (HCI_LE_SCAN , &hdev->dev_flags)) { +struct hci_cp_le_set_scan_enable enable_cp; + +memset (&enable_cp,0 ,sizeof (enable_cp)); + enable_cp.enable =LE_SCAN_DISABLE ; +hci_req_add (&req,HCI_OP_LE_SET_SCAN_ENABLE ,sizeof (enable_cp), + &enable_cp); + } +memset (&cp,0 ,sizeof (cp)); cp.scan_interval =cpu_to_le16 (hdev->le_scan_interval); cp.scan_window =cpu_to_le16 (hdev->le_scan_window);
Signature · 1 line
1.8.4.2
Re: [RFC v4 05/12] Bluetooth: Stop scanning on LE connection
Hi Andre,
Quoted 56 lines · Andre Guedes
Some LE controllers don't support scanning and creating a connection at the same time. So we should always stop scanning in order to establish the connection.
Since we may prematurely stop the discovery procedure in favor of the connection establishment, we should also cancel
hdev->le_scan_disable delayed work and set the discovery state toDISCOVERY_STOPPED.This change does a small improvement since it is not mandatory the user stops scanning before connecting anymore. Moreover, this change is required by upcoming LE auto connection mechanism in order to work properly with controllers that don't support background scanning and connection establishment at the same time.
In future, we might want to do a small optimization by checking if controller is able to scan and connect at the same time. For now, we want the simplest approach so we always stop scanning (even if the controller is able to carry out both operations).
Signed-off-by Andre Guedes <[email protected]>--- net/bluetooth/hci_conn.c | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-)net/bluetooth/hci_conn.c +15
-1@@ -518,8 +518,17 @@ static void create_le_conn_complete(struct hci_dev *hdev, u8 status) {struct hci_conn *conn; -if (status ==0 ) +if (status ==0 ) { +/* If the discovery procedure was running, we prematurely +* stopped it. So we have to change the discovery state. +*/ +if (hdev->discovery.state ==DISCOVERY_FINDING ) { +cancel_delayed_work (&hdev->le_scan_disable); +hci_discovery_set_state (hdev,DISCOVERY_STOPPED ); + } +return ; + }BT_ERR ("HCI request failed to create LE connection: status 0x%2.2x" , status);@@ -552,6 +561,18 @@ static int hci_create_le_conn(struct hci_conn *conn)hci_req_init (&req, hdev); +/* If controller is scanning, we stop it since some controllers are +* not able to scan and connect at the same time. +*/ +if (test_bit (HCI_LE_SCAN , &hdev->dev_flags)) { +struct hci_cp_le_set_scan_enable enable_cp;
net/bluetooth/hci_conn.c static int hci_create_le_conn(struct hci_conn *conn) · line 578
Quoted 10 lines
net/bluetooth/hci_conn.c +7
-0+ +memset (&enable_cp,0 ,sizeof (enable_cp)); + enable_cp.enable =LE_SCAN_DISABLE ; +hci_req_add (&req,HCI_OP_LE_SET_SCAN_ENABLE ,sizeof (enable_cp), + &enable_cp); + } +memset (&cp,0 ,sizeof (cp)); cp.scan_interval =cpu_to_le16 (hdev->le_scan_interval); cp.scan_window =cpu_to_le16 (hdev->le_scan_window);
The way this all hangs together with a discovery process started through mgmt_start_discovery feels a bit flimsy to me. Particularly, have you ensured that everything is fine if you've got inquiry ongoing when you do hci_discovery_set_state(hdev, DISCOVERY_STOPPED). Also, are there any risks of race conditions here, e.g. is it fine to let the cancel_delayed_work() call be in create_le_conn_complete() instead of doing it in hci_create_le_conn()?
What also makes this hard to track is that the condition you're testing for first is the HCI_LE_SCAN bit, but then later you look at discovery.state == DISCOVERY_FINDING. For the casual reader there's no direct indication of how these two are releated for the various types of discovery that are possible (LE-only, BR/EDR-only and interleaved).
I don't mean to say that this is a nack for the patch, but I'd like to know that you've considered and tested this kind of cases. I had to spend quite some time looking through the existing code and this patch and still couldn't arrive at absolute confidence of its correctness, meaning there should hopefully be some room for simplification.
Johan
Re: [RFC v4 05/12] Bluetooth: Stop scanning on LE connection
Hi Johan,
Sorry the delay to reply, I was out of the office last week.
Quoted 70 lines · Johan Hedberg
Hi Andre,
Quoted 56 lines · Andre Guedes
Some LE controllers don't support scanning and creating a connection at the same time. So we should always stop scanning in order to establish the connection.
Since we may prematurely stop the discovery procedure in favor of the connection establishment, we should also cancel
hdev->le_scan_disable delayed work and set the discovery state toDISCOVERY_STOPPED.This change does a small improvement since it is not mandatory the user stops scanning before connecting anymore. Moreover, this change is required by upcoming LE auto connection mechanism in order to work properly with controllers that don't support background scanning and connection establishment at the same time.
In future, we might want to do a small optimization by checking if controller is able to scan and connect at the same time. For now, we want the simplest approach so we always stop scanning (even if the controller is able to carry out both operations).
Signed-off-by Andre Guedes <[email protected]>--- net/bluetooth/hci_conn.c | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-)net/bluetooth/hci_conn.c +15
-1@@ -518,8 +518,17 @@ static void create_le_conn_complete(struct hci_dev *hdev, u8 status) {struct hci_conn *conn; -if (status ==0 ) +if (status ==0 ) { +/* If the discovery procedure was running, we prematurely +* stopped it. So we have to change the discovery state. +*/ +if (hdev->discovery.state ==DISCOVERY_FINDING ) { +cancel_delayed_work (&hdev->le_scan_disable); +hci_discovery_set_state (hdev,DISCOVERY_STOPPED ); + } +return ; + }BT_ERR ("HCI request failed to create LE connection: status 0x%2.2x" , status);@@ -552,6 +561,18 @@ static int hci_create_le_conn(struct hci_conn *conn)hci_req_init (&req, hdev); +/* If controller is scanning, we stop it since some controllers are +* not able to scan and connect at the same time. +*/ +if (test_bit (HCI_LE_SCAN , &hdev->dev_flags)) { +struct hci_cp_le_set_scan_enable enable_cp;net/bluetooth/hci_conn.c static int hci_create_le_conn(struct hci_conn *conn) · line 578
Quoted 10 lines
net/bluetooth/hci_conn.c +7
-0+ +memset (&enable_cp,0 ,sizeof (enable_cp)); + enable_cp.enable =LE_SCAN_DISABLE ; +hci_req_add (&req,HCI_OP_LE_SET_SCAN_ENABLE ,sizeof (enable_cp), + &enable_cp); + } +memset (&cp,0 ,sizeof (cp)); cp.scan_interval =cpu_to_le16 (hdev->le_scan_interval); cp.scan_window =cpu_to_le16 (hdev->le_scan_window);
Quoted 7 lines
The way this all hangs together with a discovery process started through
mgmt_start_discoveryfeels a bit flimsy to me. Particularly, have you ensured that everything is fine if you've got inquiry ongoing when you dohci_discovery_set_state(hdev, DISCOVERY_STOPPED). Also, are there any risks of race conditions here, e.g. is it fine to let thecancel_delayed_work()call be increate_le_conn_complete()instead of doing it inhci_create_le_conn()?
Yes, I did lots of testing, including the inquiry test you mentioned, and I didn't find any issues.
I failed to see any race conditons since cancel_delayed_work() does not block. So it is fine to call cancel_delayed_work() in create_le_conn_complete() as well as in hci_create_le_conn().
Quoted 5 lines
What also makes this hard to track is that the condition you're testing for first is the
HCI_LE_SCANbit, but then later you look at discovery.state ==DISCOVERY_FINDING. For the casual reader there's no direct indication of how these two are releated for the various types of discovery that are possible (LE-only, BR/EDR-only and interleaved).
Yes, I see your point. However, we can't check HCI_LE_SCAN in create_le_conn_complete() because the flag was already cleared in hci_cc_le_set_scan_enable().
Quoted 5 lines
I don't mean to say that this is a nack for the patch, but I'd like to know that you've considered and tested this kind of cases. I had to spend quite some time looking through the existing code and this patch and still couldn't arrive at absolute confidence of its correctness, meaning there should hopefully be some room for simplification.
So I think we can do some simplification by moving this discovery handling from create_le_conn_complete() to hci_create_le_conn(), at least I think this code will become a bit easier to follow. What do you think?
Regards,
Andre
Re: [RFC v4 05/12] Bluetooth: Stop scanning on LE connection
Hi Andre,
Quoted 47 lines · Andre Guedes
Quoted 39 lines
Quoted 28 lines
net/bluetooth/hci_conn.c +15
-1@@ -518,8 +518,17 @@ static void create_le_conn_complete(struct hci_dev *hdev, u8 status) {struct hci_conn *conn; -if (status ==0 ) +if (status ==0 ) { +/* If the discovery procedure was running, we prematurely +* stopped it. So we have to change the discovery state. +*/ +if (hdev->discovery.state ==DISCOVERY_FINDING ) { +cancel_delayed_work (&hdev->le_scan_disable); +hci_discovery_set_state (hdev,DISCOVERY_STOPPED ); + } +return ; + }BT_ERR ("HCI request failed to create LE connection: status 0x%2.2x" , status);@@ -552,6 +561,18 @@ static int hci_create_le_conn(struct hci_conn *conn)hci_req_init (&req, hdev); +/* If controller is scanning, we stop it since some controllers are +* not able to scan and connect at the same time. +*/ +if (test_bit (HCI_LE_SCAN , &hdev->dev_flags)) { +struct hci_cp_le_set_scan_enable enable_cp;net/bluetooth/hci_conn.c static int hci_create_le_conn(struct hci_conn *conn) · line 578
Quoted 10 lines
net/bluetooth/hci_conn.c +7
-0+ +memset (&enable_cp,0 ,sizeof (enable_cp)); + enable_cp.enable =LE_SCAN_DISABLE ; +hci_req_add (&req,HCI_OP_LE_SET_SCAN_ENABLE ,sizeof (enable_cp), + &enable_cp); + } +memset (&cp,0 ,sizeof (cp)); cp.scan_interval =cpu_to_le16 (hdev->le_scan_interval); cp.scan_window =cpu_to_le16 (hdev->le_scan_window);Quoted 7 lines
The way this all hangs together with a discovery process started through
mgmt_start_discoveryfeels a bit flimsy to me. Particularly, have you ensured that everything is fine if you've got inquiry ongoing when you dohci_discovery_set_state(hdev, DISCOVERY_STOPPED). Also, are there any risks of race conditions here, e.g. is it fine to let thecancel_delayed_work()call be increate_le_conn_complete()instead of doing it inhci_create_le_conn()?
Quoted 6 lines
Yes, I did lots of testing, including the inquiry test you mentioned, and I didn't find any issues.
I failed to see any race conditons since
cancel_delayed_work()does not block. So it is fine to callcancel_delayed_work()increate_le_conn_complete()as well as inhci_create_le_conn().
I presume you'd want to prevent the delayed work from getting called as soon as you've entered the if (test_bit(HCI_LE_SCAN)) section in hci_create_le_conn? In theory it might get called before create_le_conn_complete. In such a scenario le_scan_disable_work_complete could e.g. trigger inquiry before create_le_conn_complete is called, couldn't it?
Quoted 9 lines
Quoted 5 lines
What also makes this hard to track is that the condition you're testing for first is the
HCI_LE_SCANbit, but then later you look at discovery.state ==DISCOVERY_FINDING. For the casual reader there's no direct indication of how these two are releated for the various types of discovery that are possible (LE-only, BR/EDR-only and interleaved).Yes, I see your point. However, we can't check
HCI_LE_SCANincreate_le_conn_complete()because the flag was already cleared inhci_cc_le_set_scan_enable().
Understood.
Quoted 10 lines
Quoted 5 lines
I don't mean to say that this is a nack for the patch, but I'd like to know that you've considered and tested this kind of cases. I had to spend quite some time looking through the existing code and this patch and still couldn't arrive at absolute confidence of its correctness, meaning there should hopefully be some room for simplification.
So I think we can do some simplification by moving this discovery handling from
create_le_conn_complete()tohci_create_le_conn(), at least I think this code will become a bit easier to follow. What do you think?
I think it would at least close the potential race I described above, i.e. the delayed work getting triggered before create_le_conn_complete.
Johan
Re: [RFC v4 05/12] Bluetooth: Stop scanning on LE connection
Hi Johan,
Quoted 57 lines · Johan Hedberg
Hi Andre,
Quoted 47 lines · Andre Guedes
Quoted 39 lines
Quoted 28 lines
net/bluetooth/hci_conn.c +15
-1@@ -518,8 +518,17 @@ static void create_le_conn_complete(struct hci_dev *hdev, u8 status) {struct hci_conn *conn; -if (status ==0 ) +if (status ==0 ) { +/* If the discovery procedure was running, we prematurely +* stopped it. So we have to change the discovery state. +*/ +if (hdev->discovery.state ==DISCOVERY_FINDING ) { +cancel_delayed_work (&hdev->le_scan_disable); +hci_discovery_set_state (hdev,DISCOVERY_STOPPED ); + } +return ; + }BT_ERR ("HCI request failed to create LE connection: status 0x%2.2x" , status);@@ -552,6 +561,18 @@ static int hci_create_le_conn(struct hci_conn *conn)hci_req_init (&req, hdev); +/* If controller is scanning, we stop it since some controllers are +* not able to scan and connect at the same time. +*/ +if (test_bit (HCI_LE_SCAN , &hdev->dev_flags)) { +struct hci_cp_le_set_scan_enable enable_cp;net/bluetooth/hci_conn.c static int hci_create_le_conn(struct hci_conn *conn) · line 578
Quoted 10 lines
net/bluetooth/hci_conn.c +7
-0+ +memset (&enable_cp,0 ,sizeof (enable_cp)); + enable_cp.enable =LE_SCAN_DISABLE ; +hci_req_add (&req,HCI_OP_LE_SET_SCAN_ENABLE ,sizeof (enable_cp), + &enable_cp); + } +memset (&cp,0 ,sizeof (cp)); cp.scan_interval =cpu_to_le16 (hdev->le_scan_interval); cp.scan_window =cpu_to_le16 (hdev->le_scan_window);Quoted 7 lines
The way this all hangs together with a discovery process started through
mgmt_start_discoveryfeels a bit flimsy to me. Particularly, have you ensured that everything is fine if you've got inquiry ongoing when you dohci_discovery_set_state(hdev, DISCOVERY_STOPPED). Also, are there any risks of race conditions here, e.g. is it fine to let thecancel_delayed_work()call be increate_le_conn_complete()instead of doing it inhci_create_le_conn()?Quoted 6 lines
Yes, I did lots of testing, including the inquiry test you mentioned, and I didn't find any issues.
I failed to see any race conditons since
cancel_delayed_work()does not block. So it is fine to callcancel_delayed_work()increate_le_conn_complete()as well as inhci_create_le_conn().
Quoted 6 lines
I presume you'd want to prevent the delayed work from getting called as soon as you've entered the if (
test_bit(HCI_LE_SCAN)) section inhci_create_le_conn? In theory it might get called beforecreate_le_conn_complete. In such a scenariole_scan_disable_work_completecould e.g. trigger inquiry beforecreate_le_conn_completeis called, couldn't it?
Yes, this scenario can indeed happen in a very short window. However, it doesn't seem to be a big issue. If that happen then the discovery procedure keeps running normally and the connection attempt fails. If it is a auto connection then the kernel will retry in a near future. If it a user connection, it will get a busy error.
Quoted 18 lines
Quoted 9 lines
Quoted 5 lines
What also makes this hard to track is that the condition you're testing for first is the
HCI_LE_SCANbit, but then later you look at discovery.state ==DISCOVERY_FINDING. For the casual reader there's no direct indication of how these two are releated for the various types of discovery that are possible (LE-only, BR/EDR-only and interleaved).Yes, I see your point. However, we can't check
HCI_LE_SCANincreate_le_conn_complete()because the flag was already cleared inhci_cc_le_set_scan_enable().Understood.
Quoted 6 lines
Quoted 5 lines
I don't mean to say that this is a nack for the patch, but I'd like to know that you've considered and tested this kind of cases. I had to spend quite some time looking through the existing code and this patch and still couldn't arrive at absolute confidence of its correctness, meaning there should hopefully be some room for simplification.
Quoted 7 lines
Quoted 4 lines
So I think we can do some simplification by moving this discovery handling from
create_le_conn_complete()tohci_create_le_conn(), at least I think this code will become a bit easier to follow. What do you think?I think it would at least close the potential race I described above, i.e. the delayed work getting triggered before
create_le_conn_complete.
I realized this approach is not good enough since we'll be changing discovery state before being sure the HCI command executed successfully. This could let the discovery state inconsistent.
So after spending some time thinking about this issue, I believe the better way to handle this is taking it in two steps. First, we simply stop scanning and handle the discovery state. Then we create the LE connection.
I'm already working on this "two steps" approach and I should send the v5 patch set very soon.
Regards,
Andre
RFC v4 6/12 Bluetooth: Introduce hdev->pend_le_conn list
In reply to Andre GuedesThis patch introduces the hdev->pend_le_conn list which holds the device addresses the kernel should autonomously connect. It also introduces some helper functions to manipulate the list.
The list and helper functions will be used by the next patch which implements the LE auto connection infrastructure.
--- include/net/bluetooth/hci_core.h | 7 +++++ net/bluetooth/hci_core.c | 68 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+)
include/net/bluetooth/hci_core.h +7 -0
@@ -268,6 +268,7 @@ struct hci_dev {struct list_head long_term_keys;struct list_head remote_oob_data;struct list_head le_conn_params; +struct list_head pend_le_conns;struct hci_dev_stats stat;
@@ -764,6 +765,12 @@ void hci_conn_params_add(struct hci_dev *hdev, bdaddr_t *addr, u8 addr_type,void hci_conn_params_del (struct hci_dev *hdev,bdaddr_t *addr,u8 addr_type);void hci_conn_params_clear (struct hci_dev *hdev); +struct bdaddr_list *hci_pend_le_conn_lookup (struct hci_dev *hdev, +bdaddr_t *addr,u8 addr_type); +void hci_pend_le_conn_add (struct hci_dev *hdev,bdaddr_t *addr,u8 addr_type); +void hci_pend_le_conn_del (struct hci_dev *hdev,bdaddr_t *addr,u8 addr_type); +void hci_pend_le_conns_clear (struct hci_dev *hdev); +int hci_uuids_clear (struct hci_dev *hdev);int hci_link_keys_clear (struct hci_dev *hdev);
net/bluetooth/hci_core.c +68 -0
86 lines not on this page · raw message
Signature · 1 line
1.8.4.2
RFC v4 7/12 Bluetooth: Introduce LE auto connection infrastructure
In reply to Andre GuedesThis patch introduces the LE auto connection infrastructure. This infrastructure will be used to implement the auto_connect options from hci_conn_params.
In summary, the auto connection mechanism works as follows: Once the first pending LE connection is created, the background scanning is started. When the target device is found in range, the kernel autonomously starts the connection attempt. If connection is established successfully, that pending LE connection is deleted and the background is stopped.
To achieve that, this patch introduces the hci_update_background_scan() which controls the background scanning state. This function starts or stops the background scanning based on the hdev->pend_le_conns list. If there is no pending LE connection, the background scanning is stopped. Otherwise, we start the background scanning.
Then, every time a pending LE connection is added we call hci_update_ background_scan() so the background scanning is started (in case it is not already running). Likewise, every time a pending LE connection is deleted we call hci_update_background_scan() so the background scanning is stopped (in case this was the last pending LE connection) or it is started again (in case we have more pending LE connections). This way the background scanning keeps running until all pending LE connection are established.
When a device with pending connection is in range, we establish the connection and delete the pending connection.
--- include/net/bluetooth/hci_core.h | 2 + net/bluetooth/hci_core.c | 86 +++++++++++++++++++++++++++++++++++++++- net/bluetooth/hci_event.c | 41 +++++++++++++++++++ 3 files changed, 127 insertions(+), 2 deletions(-)
include/net/bluetooth/hci_core.h +2 -0
@@ -771,6 +771,8 @@ void hci_pend_le_conn_add(struct hci_dev *hdev, bdaddr_t *addr, u8 addr_type);void hci_pend_le_conn_del (struct hci_dev *hdev,bdaddr_t *addr,u8 addr_type);void hci_pend_le_conns_clear (struct hci_dev *hdev); +void hci_update_background_scan (struct hci_dev *hdev); +int hci_uuids_clear (struct hci_dev *hdev);int hci_link_keys_clear (struct hci_dev *hdev);
net/bluetooth/hci_core.c +84 -2
112 lines not on this page · raw message
net/bluetooth/hci_event.c +41 -0
66 lines not on this page · raw message
Signature · 1 line
1.8.4.2
RFC v4 8/12 Bluetooth: Re-enable background scan in case of error
In reply to Andre GuedesSince we temporarily stop the background scanning in favor of connection, we should re-enable it in case something goes wrong with connection establishment. So this patch adds a hci_update_ background_scan() call in create_le_conn_complete() and hci_le_ conn_complete_evt() error flow.
--- net/bluetooth/hci_conn.c | 2 ++ net/bluetooth/hci_event.c | 1 + 2 files changed, 3 insertions(+)
net/bluetooth/hci_conn.c +2 -0
@@ -548,6 +548,8 @@ static void create_le_conn_complete(struct hci_dev *hdev, u8 status)hci_conn_del (conn); +hci_update_background_scan (hdev); + done:hci_dev_unlock (hdev); }
net/bluetooth/hci_event.c +1 -0
@@ -3522,6 +3522,7 @@ static void hci_le_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)hci_proto_connect_cfm (conn, ev->status); conn->state =BT_CLOSED ;hci_conn_del (conn); +hci_update_background_scan (hdev); goto unlock; }
Signature · 1 line
1.8.4.2
RFC v4 9/12 Bluetooth: Temporarily stop background scanning on discovery
In reply to Andre GuedesIf the user send a mgmt start discovery command while the background scanning is running, we should temporarily stop it. Once the discovery finishes, we start the background scanning again.
--- net/bluetooth/hci_core.c | 2 ++ net/bluetooth/mgmt.c | 12 ++++++++---- 2 files changed, 10 insertions(+), 4 deletions(-)
net/bluetooth/hci_core.c +2 -0
@@ -1498,6 +1498,8 @@ void hci_discovery_set_state(struct hci_dev *hdev, int state)switch (state) {case DISCOVERY_STOPPED : +hci_update_background_scan (hdev); +if (hdev->discovery.state !=DISCOVERY_STARTING )mgmt_discovering (hdev,0 );break ;
net/bluetooth/mgmt.c +8 -4
@@ -3280,11 +3280,15 @@ static int start_discovery(struct sock *sk, struct hci_dev *hdev, goto failed; } +/* If controller is scanning, it means the background scanning +* is running. Thus, we should temporarily stop it in order to +* set the discovery scanning parameters. +*/ if (test_bit (HCI_LE_SCAN , &hdev->dev_flags)) { - err =cmd_status (sk, hdev->id,MGMT_OP_START_DISCOVERY , -MGMT_STATUS_BUSY ); -mgmt_pending_remove (cmd); - goto failed; +memset (&enable_cp,0 ,sizeof (enable_cp)); + enable_cp.enable =LE_SCAN_DISABLE ; +hci_req_add (&req,HCI_OP_LE_SET_SCAN_ENABLE , +sizeof (enable_cp), &enable_cp); }memset (¶m_cp,0 ,sizeof (param_cp));
Signature · 1 line
1.8.4.2
RFC v4 10/12 Bluetooth: Auto connection and power on
In reply to Andre GuedesWhen hdev is closed (e.g. Mgmt power off command, RFKILL or controller is reset), the ongoing active connections are silently dropped by the controller (no Disconnection Complete Event is sent to host). For that reason, the devices that require HCI_AUTO_CONN_ALWAYS are not added to hdev->pend_le_conns list and they won't auto connect.
So to fix this issue, during hdev closing, we remove all pending LE connections. After adapter is powered on, we add a pending LE connection for each HCI_AUTO_CONN_ALWAYS address.
This way, the auto connection mechanism works propely after a power off and power on sequence as well as RFKILL block/unblock.
--- net/bluetooth/hci_core.c | 1 + net/bluetooth/mgmt.c | 13 +++++++++++++ 2 files changed, 14 insertions(+)
net/bluetooth/hci_core.c +1 -0
@@ -1969,6 +1969,7 @@ static int hci_dev_do_close(struct hci_dev *hdev)hci_dev_lock (hdev);hci_inquiry_cache_flush (hdev);hci_conn_hash_flush (hdev); +hci_pend_le_conns_clear (hdev);hci_dev_unlock (hdev);hci_notify (hdev,HCI_DEV_DOWN );
net/bluetooth/mgmt.c +13 -0
@@ -4247,6 +4247,17 @@ void mgmt_index_removed(struct hci_dev *hdev)mgmt_event (MGMT_EV_INDEX_REMOVED , hdev,NULL ,0 ,NULL ); } +/* This function requires the caller holds hdev->lock */ +static void restart_le_auto_conns (struct hci_dev *hdev) +{ +struct hci_conn_params *p; + +list_for_each_entry (p, &hdev->le_conn_params, list) { +if (p->auto_connect ==HCI_AUTO_CONN_ALWAYS ) +hci_pend_le_conn_add (hdev, &p->addr, p->addr_type); + } +} +static void powered_complete (struct hci_dev *hdev,u8 status) {struct cmd_lookup match = {NULL , hdev };
@@ -4255,6 +4266,8 @@ static void powered_complete(struct hci_dev *hdev, u8 status)hci_dev_lock (hdev); +restart_le_auto_conns (hdev); +mgmt_pending_foreach (MGMT_OP_SET_POWERED , hdev, settings_rsp, &match);new_settings (hdev, match.sk);
Signature · 1 line
1.8.4.2
RFC v4 11/12 Bleutooth: Add support for auto connect options
In reply to Andre GuedesThis patch adds support for the HCI_AUTO_CONN_ALWAYS and HCI_AUTO_ CONN_LINK_LOSS options from struct hci_conn_params.
The HCI_AUTO_CONN_ALWAYS option configures the kernel to always re- establish the connection, no matter the reason the connection was terminated. This feature is required by some LE profiles such as HID over GATT, Health Thermometer and Blood Pressure. These profiles require the host autonomously connect to the device as soon as it enters in connectable mode (start advertising) so the device is able to delivery notifications or indications.
The BT_AUTO_CONN_LINK_LOSS option configures the kernel to re- establish the connection in case the connection was terminated due to a link loss. This feature is required by the majority of LE profiles such as Proximity, Find Me, Cycling Speed and Cadence and Time.
--- net/bluetooth/hci_event.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
net/bluetooth/hci_event.c +18 -0
@@ -1780,6 +1780,7 @@ static void hci_disconn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb) {struct hci_ev_disconn_complete *ev = (void *) skb->data;u8 reason =hci_to_mgmt_reason (ev->reason); +struct hci_conn_params *params;struct hci_conn *conn;u8 type;
@@ -1806,6 +1807,23 @@ static void hci_disconn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)if (conn->type ==ACL_LINK && conn->flush_key)hci_remove_link_key (hdev, &conn->dst); + params =hci_conn_params_lookup (hdev, &conn->dst, conn->dst_type); +if (params) { +switch (params->auto_connect) { +case HCI_AUTO_CONN_LINK_LOSS : +if (ev->reason !=HCI_ERROR_CONNECTION_TIMEOUT ) +break ; +/* Fall through */ + +case HCI_AUTO_CONN_ALWAYS : +hci_pend_le_conn_add (hdev, &conn->dst, conn->dst_type); +break ; + +default : +break ; + } + } + type = conn->type;hci_proto_disconn_cfm (conn, ev->reason);
Signature · 1 line
1.8.4.2
RFC v4 12/12 Bluetooth: Add le_auto_conn file on debugfs
In reply to Andre GuedesThis patch adds to debugfs the le_auto_conn file. This file will be used to test LE auto connection infrastructure.
To add a new auto connection address we write on le_auto_conn file following the format <address> <address type> <auto_connect>.
The <address type> values are:
* 0 for public address
* 1 for random address
The <auto_connect> values are (for more details see struct hci_ conn_params):
* 0 for disabled
* 1 for always
* 2 for link loss
So for instance, if you want the kernel autonomously establishes connections with device AA:BB:CC:DD:EE:FF (public address) every time the device enters in connectable mode (starts advertising), you should run the command:
$ echo "AA:BB:CC:DD:EE:FF 0 1" > /sys/kernel/debug/bluetooth/hci0/le_auto_conn
To get the list of connection parameters configured in kernel, read the le_auto_conn file:
$ cat /sys/kernel/debug/bluetooth/hci0/le_auto_conn
Finally, to clear the connection parameters list, write an empty string:
$ echo "" > /sys/kernel/debug/bluetooth/hci0/le_auto_conn
This file is created only if LE is enabled.
--- net/bluetooth/hci_core.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+)
net/bluetooth/hci_core.c +85 -0
@@ -636,6 +636,89 @@ static int conn_max_interval_get(void *data, u64 *val)DEFINE_SIMPLE_ATTRIBUTE (conn_max_interval_fops, conn_max_interval_get, conn_max_interval_set,"%llu\n" ); +static int le_auto_conn_show (struct seq_file *sf,void *ptr) +{ +struct hci_dev *hdev = sf->private; +struct hci_conn_params *p; + +hci_dev_lock (hdev); + +list_for_each_entry (p, &hdev->le_conn_params, list) { +seq_printf (sf,"%pMR %u %u\n" , &p->addr, p->addr_type, + p->auto_connect); + } + +hci_dev_unlock (hdev); + +return 0 ; +} + +static int le_auto_conn_open (struct inode *inode,struct file *file) +{ +return single_open (file, le_auto_conn_show, inode->i_private); +} + +static ssize_t le_auto_conn_write (struct file *file,const char __user *data, +size_t count,loff_t *offset) +{ +struct seq_file *sf = file->private_data; +struct hci_dev *hdev = sf->private; +u8 auto_connect; +bdaddr_t addr; +u8 addr_type; +char *buf; +int n; + +/* Don't allow partial write */ +if (*offset !=0 ) +return -EINVAL ; + +/* If empty string, clear the connection parameters and pending LE +* connection list. +*/ +if (count ==1 ) { +hci_dev_lock (hdev); +hci_conn_params_clear (hdev); +hci_pend_le_conns_clear (hdev); +hci_dev_unlock (hdev); +return count; + } + + buf =kzalloc (count,GFP_KERNEL ); +if (!buf) +return -ENOMEM ; + +if (copy_from_user (buf, data, count)) { +kfree (buf); +return -EFAULT ; + } + + n =sscanf (buf,"%hhx:%hhx:%hhx:%hhx:%hhx:%hhx %hhu %hhu" , &addr.b[5 ], + &addr.b[4 ], &addr.b[3 ], &addr.b[2 ], &addr.b[1 ], &addr.b[0 ], + &addr_type, &auto_connect); +if (n !=8 ) { +kfree (buf); +return -EINVAL ; + } + +hci_dev_lock (hdev); +hci_conn_params_add (hdev, &addr, addr_type, auto_connect, + hdev->le_conn_min_interval, + hdev->le_conn_max_interval); +hci_dev_unlock (hdev); + +kfree (buf); +return count; +} + +static const struct file_operations le_auto_conn_fops = { + .open = le_auto_conn_open, + .read = seq_read, + .write = le_auto_conn_write, + .llseek = seq_lseek, + .release = single_release, +}; +/* ---- HCI requests ---- */ static void hci_req_sync_complete (struct hci_dev *hdev,u8 result)
@@ -1406,6 +1489,8 @@ static int __hci_init(struct hci_dev *hdev) hdev, &conn_min_interval_fops);debugfs_create_file ("conn_max_interval" ,0644 , hdev->debugfs, hdev, &conn_max_interval_fops); +debugfs_create_file ("le_auto_conn" ,0644 , hdev->debugfs, hdev, + &le_auto_conn_fops); }return 0 ;
Signature · 1 line
1.8.4.2