Bluetooth: add virtual hci command
Subsystems (2)
PATCH 1/2 Bluetooth: add virtual hci command
This patch adds virtual HCI command HCI_OP_VIRT_LE_SCAN_DISABLED. It's called virtual, because it'll never be sent to the controller. It can be added to asynchronous hci_request, and when time comes for its execution, it can do some logic and decide either to transform itself into real packet, or send nothing.
Logic for HCI_OP_VIRT_LE_SCAN_DISABLED is as follows: if at time of execution scan is disabled, do nothing. If scan is enabled, disable it. It is needed because scan state migth change between moment when we schedule command and execution time. Later patches will use this new command to fix some error scenarios.
--- include/net/bluetooth/hci.h | 2 + include/net/bluetooth/hci_core.h | 1 + net/bluetooth/hci_core.c | 86 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+)
include/net/bluetooth/hci.h +2 -0
@@ -1961,6 +1961,8 @@ struct hci_ev_si_security {__u8 incoming; } __packed; +#define HCI_OP_VIRT_LE_SCAN_DISABLE 0xFF0b +/* ---- HCI Packet structures ---- */ #define HCI_COMMAND_HDR_SIZE 3#define HCI_EVENT_HDR_SIZE 2
include/net/bluetooth/hci_core.h +1 -0
@@ -322,6 +322,7 @@ struct hci_dev {struct delayed_work service_cache;struct delayed_work cmd_timer; +struct work_struct cmd_timer_cancel;struct work_struct rx_work;struct work_struct cmd_work;
net/bluetooth/hci_core.c +86 -0
110 lines not on this page · raw message
Signature · 1 line
2.5.0
PATCH 2/2 Bluetooth: fix connect race condition
Right now, when calling Device1.Connect() on two LE devices using RPA at same time, second call to Connect() will fail every few times. It is due to race condition inside kernel, that happens when adding devices to whitelist. This patch fixes that.
Each time device is added to whitelist, __hci_update_background_scan will be called. Scan state will be checked, and two commands will be scheduled, but not executed yet: LE_SET_SCAN_PARAM and LE_SET_SCAN_ENABLE. Then during execution time, if those commands were scheduled twice, first two commands will succeed, but later two will fail, because LE_SET_SCAN_PARAM must be executed when scan is disabled, and fist two commands left it enabled.
In order to solve that, HCI_OP_VIRT_LE_SCAN_DISABLE must be used. It'll check the scan state at the runtime, instead of schedule time, and will stop the scan if necessary.
--- net/bluetooth/hci_request.c | 22 ++++++++++++++++------ net/bluetooth/mgmt.c | 1 - 2 files changed, 16 insertions(+), 7 deletions(-)
net/bluetooth/hci_request.c +16 -6
@@ -150,6 +150,19 @@ void hci_req_add_le_scan_disable(struct hci_request *req)hci_req_add (req,HCI_OP_LE_SET_SCAN_ENABLE ,sizeof (cp), &cp); } +void hci_req_add_virt_le_scan_disable (struct hci_request *req) +{ +struct hci_cp_le_set_scan_enable cp; + +memset (&cp,0 ,sizeof (cp)); + cp.enable =LE_SCAN_DISABLE ; + +/* Instead of sending real scan disable packet, send virtual one, +* that will check and stop scan at time of running command. +*/ +hci_req_add (req,HCI_OP_VIRT_LE_SCAN_DISABLE ,sizeof (cp), &cp); +} +static void add_to_white_list (struct hci_request *req,struct hci_conn_params *params) {
@@ -258,6 +271,9 @@ void hci_req_add_le_passive_scan(struct hci_request *req)u8 own_addr_type;u8 filter_policy; +/* HCI_OP_LE_SET_SCAN_PARAM must be run when scan is disabled. */ +hci_req_add_virt_le_scan_disable (req); +/* Set require_privacy to false since no SCAN_REQ are send * during passive scanning. Not using an non-resolvable address * here is important so that peer devices using direct
@@ -531,12 +547,6 @@ void __hci_update_background_scan(struct hci_request *req)if (hci_lookup_le_connect (hdev))return ; -/* If controller is currently scanning, we stop it to ensure we -* don't miss any advertising (due to duplicates filter). -*/ -if (hci_dev_test_flag (hdev,HCI_LE_SCAN )) -hci_req_add_le_scan_disable (req); -hci_req_add_le_passive_scan (req);BT_DBG ("%s starting background scanning" , hdev->name);
net/bluetooth/mgmt.c +0 -1
@@ -5045,7 +5045,6 @@ static int set_scan_params(struct sock *sk, struct hci_dev *hdev,hci_req_init (&req, hdev); -hci_req_add_le_scan_disable (&req);hci_req_add_le_passive_scan (&req);hci_req_run (&req,NULL );
Signature · 1 line
2.5.0