Bluetooth: add hci_restart_le_scan
Subsystems (2)
PATCH v11 1/3 Bluetooth: add hci_restart_le_scan
Currently there is no way to restart le scan. It's needed in preparation for new service scan method. The way it work: it disable, and then enable le scan on controller. During this restart special flag is set to make sure we won't remove disable scan work from workqueue.
--- include/net/bluetooth/hci.h | 8 +++++++ include/net/bluetooth/hci_core.h | 4 ++++ net/bluetooth/hci_core.c | 50 +++++++++++++++++++++++++++++++++++++++- net/bluetooth/hci_event.c | 9 +++++--- 4 files changed, 67 insertions(+), 4 deletions(-)
include/net/bluetooth/hci.h +8 -0
@@ -205,6 +205,14 @@ enum { HCI_LE_SCAN_INTERRUPTED, }; +/* +* BR/EDR and/or LE discovery state flags: the flags defined here should +* represent state of discovery +*/ +enum { +HCI_LE_SCAN_RESTARTING , +}; +/* A mask for the flags that are supposed to remain when a reset happens * or the HCI device is closed. */
include/net/bluetooth/hci_core.h +4 -0
@@ -75,6 +75,7 @@ struct discovery_state {u32 last_adv_flags;u8 last_adv_data[HCI_MAX_AD_LENGTH ];u8 last_adv_data_len; +unsigned long flags; };struct hci_conn_hash {
@@ -337,6 +338,7 @@ struct hci_dev {unsigned long dev_flags;struct delayed_work le_scan_disable; +struct delayed_work le_scan_restart;__s8 adv_tx_power;__u8 adv_data[HCI_MAX_AD_LENGTH ];
@@ -1326,6 +1328,7 @@ void hci_sock_dev_event(struct hci_dev *hdev, int event);#define DISCOV_INTERLEAVED_TIMEOUT 5120/* msec */ #define DISCOV_INTERLEAVED_INQUIRY_LEN 0x04#define DISCOV_BREDR_INQUIRY_LEN 0x08 +#define DISCOV_LE_RESTART_DELAY 300/* msec */ int mgmt_control (struct sock *sk,struct msghdr *msg,size_t len);int mgmt_new_settings (struct hci_dev *hdev);
@@ -1403,6 +1406,7 @@ int hci_update_random_address(struct hci_request *req, bool require_privacy, u8 *own_addr_type);void hci_copy_identity_address (struct hci_dev *hdev,bdaddr_t *bdaddr,u8 *bdaddr_type); +void hci_restart_le_scan (struct hci_dev *hdev);#define SCO_AIRMODE_MASK 0x0003#define SCO_AIRMODE_CVSD 0x0000
net/bluetooth/hci_core.c +49 -1
74 lines not on this page · raw message
net/bluetooth/hci_event.c +6 -3
@@ -1157,10 +1157,13 @@ static void hci_cc_le_set_scan_enable(struct hci_dev *hdev, d->last_adv_data_len, NULL, 0); } -/* Cancel this timer so that we don't try to disable scanning -* when it's already disabled. +/* If HCI_LE_SCAN_RESTARTING is set, don't cancel this timer, +* because we're just restarting scan. Otherwise cancel it so +* that we don't try to disable scanning when it's already +* disabled. */ -cancel_delayed_work (&hdev->le_scan_disable); +if (!test_bit (HCI_LE_SCAN_RESTARTING , &hdev->discovery.flags)) +cancel_delayed_work (&hdev->le_scan_disable);clear_bit (HCI_LE_SCAN , &hdev->dev_flags);
Signature · 1 line
2.2.0.rc0.207.ga3a616c
PATCH v11 2/3 Bluetooth: Extract generic start and stop discovery
This commit extract generic_start_discovery and generic_stop_discovery in preparation for start and stop service discovery. The reason behind that is that both functions will share big part of code, and it would be much easier to maintain just one generic method.
--- net/bluetooth/mgmt.c | 47 +++++++++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 22 deletions(-)
net/bluetooth/mgmt.c +25 -22
@@ -3675,7 +3675,8 @@ done:return err; } -static int mgmt_start_discovery_failed (struct hci_dev *hdev,u8 status) +static int mgmt_start_discovery_failed (struct hci_dev *hdev,u8 status, +u16 opcode) {struct pending_cmd *cmd;u8 type;
@@ -3683,7 +3684,7 @@ static int mgmt_start_discovery_failed(struct hci_dev *hdev, u8 status)hci_discovery_set_state (hdev,DISCOVERY_STOPPED ); - cmd =mgmt_pending_find (MGMT_OP_START_DISCOVERY , hdev); + cmd =mgmt_pending_find (opcode, hdev);if (!cmd)return -ENOENT ;
@@ -3704,7 +3705,8 @@ static void start_discovery_complete(struct hci_dev *hdev, u8 status)if (status) {hci_dev_lock (hdev); -mgmt_start_discovery_failed (hdev, status); +mgmt_start_discovery_failed (hdev, status, +MGMT_OP_START_DISCOVERY );hci_dev_unlock (hdev);return ; }
@@ -3735,8 +3737,8 @@ static void start_discovery_complete(struct hci_dev *hdev, u8 status)queue_delayed_work (hdev->workqueue, &hdev->le_scan_disable, timeout); } -static int start_discovery (struct sock *sk,struct hci_dev *hdev, -void *data,u16 len) +static int generic_start_discovery (struct sock *sk,struct hci_dev *hdev, +void *data,u16 len,u16 opcode) {struct mgmt_cp_start_discovery *cp = data;struct pending_cmd *cmd;
@@ -3754,27 +3756,27 @@ static int start_discovery(struct sock *sk, struct hci_dev *hdev,hci_dev_lock (hdev);if (!hdev_is_powered (hdev)) { - err =cmd_complete (sk, hdev->id,MGMT_OP_START_DISCOVERY , + err =cmd_complete (sk, hdev->id, opcode,MGMT_STATUS_NOT_POWERED , &cp->type,sizeof (cp->type)); goto failed; }if (test_bit (HCI_PERIODIC_INQ , &hdev->dev_flags)) { - err =cmd_complete (sk, hdev->id,MGMT_OP_START_DISCOVERY , + err =cmd_complete (sk, hdev->id, opcode,MGMT_STATUS_BUSY , &cp->type,sizeof (cp->type)); goto failed; }if (hdev->discovery.state !=DISCOVERY_STOPPED ) { - err =cmd_complete (sk, hdev->id,MGMT_OP_START_DISCOVERY , + err =cmd_complete (sk, hdev->id, opcode,MGMT_STATUS_BUSY , &cp->type,sizeof (cp->type)); goto failed; } - cmd =mgmt_pending_add (sk,MGMT_OP_START_DISCOVERY , hdev,NULL ,0 ); + cmd =mgmt_pending_add (sk, opcode, hdev,NULL ,0 );if (!cmd) { err = -ENOMEM ; goto failed;
@@ -3788,16 +3790,14 @@ static int start_discovery(struct sock *sk, struct hci_dev *hdev,case DISCOV_TYPE_BREDR : status =mgmt_bredr_support (hdev);if (status) { - err =cmd_complete (sk, hdev->id, -MGMT_OP_START_DISCOVERY , status, + err =cmd_complete (sk, hdev->id, opcode, status, &cp->type,sizeof (cp->type));mgmt_pending_remove (cmd); goto failed; }if (test_bit (HCI_INQUIRY , &hdev->flags)) { - err =cmd_complete (sk, hdev->id, -MGMT_OP_START_DISCOVERY , + err =cmd_complete (sk, hdev->id, opcode,MGMT_STATUS_BUSY , &cp->type,sizeof (cp->type));mgmt_pending_remove (cmd);
@@ -3816,8 +3816,7 @@ static int start_discovery(struct sock *sk, struct hci_dev *hdev,case DISCOV_TYPE_INTERLEAVED : status =mgmt_le_support (hdev);if (status) { - err =cmd_complete (sk, hdev->id, -MGMT_OP_START_DISCOVERY , status, + err =cmd_complete (sk, hdev->id, opcode, status, &cp->type,sizeof (cp->type));mgmt_pending_remove (cmd); goto failed;
@@ -3825,8 +3824,7 @@ static int start_discovery(struct sock *sk, struct hci_dev *hdev,if (hdev->discovery.type ==DISCOV_TYPE_INTERLEAVED && !test_bit (HCI_BREDR_ENABLED , &hdev->dev_flags)) { - err =cmd_complete (sk, hdev->id, -MGMT_OP_START_DISCOVERY , + err =cmd_complete (sk, hdev->id, opcode,MGMT_STATUS_NOT_SUPPORTED , &cp->type,sizeof (cp->type));mgmt_pending_remove (cmd);
@@ -3840,8 +3838,7 @@ static int start_discovery(struct sock *sk, struct hci_dev *hdev,*/ if (hci_conn_hash_lookup_state (hdev,LE_LINK ,BT_CONNECT )) { - err =cmd_complete (sk, hdev->id, -MGMT_OP_START_DISCOVERY , + err =cmd_complete (sk, hdev->id, opcode,MGMT_STATUS_REJECTED , &cp->type,sizeof (cp->type));
@@ -3867,8 +3864,7 @@ static int start_discovery(struct sock *sk, struct hci_dev *hdev,*/ err =hci_update_random_address (&req, true, &own_addr_type);if (err <0 ) { - err =cmd_complete (sk, hdev->id, -MGMT_OP_START_DISCOVERY , + err =cmd_complete (sk, hdev->id, opcode,MGMT_STATUS_FAILED , &cp->type,sizeof (cp->type));mgmt_pending_remove (cmd);
@@ -3890,7 +3886,7 @@ static int start_discovery(struct sock *sk, struct hci_dev *hdev,break ;default : - err =cmd_complete (sk, hdev->id,MGMT_OP_START_DISCOVERY , + err =cmd_complete (sk, hdev->id, opcode,MGMT_STATUS_INVALID_PARAMS , &cp->type,sizeof (cp->type));mgmt_pending_remove (cmd);
@@ -3908,6 +3904,13 @@ failed:return err; } +static int start_discovery (struct sock *sk,struct hci_dev *hdev, +void *data,u16 len) +{ +return generic_start_discovery (sk, hdev, data, len, +MGMT_OP_START_DISCOVERY ); +} +static int mgmt_stop_discovery_failed (struct hci_dev *hdev,u8 status) {struct pending_cmd *cmd;
Signature · 1 line
2.2.0.rc0.207.ga3a616c
PATCH v11 3/3 Bluetooth: start and stop service discovery
In reply to Jakub PawlowskiThis patch introduces start service discovery method. The reason behind that is to enable users to find specific services in range by UUID. Whole filtering is done in mgmt_device_found.
--- include/net/bluetooth/hci.h | 1 + include/net/bluetooth/hci_core.h | 6 + include/net/bluetooth/mgmt.h | 9 ++ net/bluetooth/hci_core.c | 1 + net/bluetooth/mgmt.c | 277 +++++++++++++++++++++++++++++++++++---- 5 files changed, 267 insertions(+), 27 deletions(-)
include/net/bluetooth/hci.h +1 -0
@@ -211,6 +211,7 @@ enum {*/ enum {HCI_LE_SCAN_RESTARTING , +HCI_SERVICE_FILTER , };/* A mask for the flags that are supposed to remain when a reset happens
include/net/bluetooth/hci_core.h +6 -0
@@ -76,6 +76,11 @@ struct discovery_state {u8 last_adv_data[HCI_MAX_AD_LENGTH ];u8 last_adv_data_len;unsigned long flags; + +/* sd prefix stands for service discovery related properties */ +s8 sd_rssi; +u16 sd_num_uuid; +u8 (*sd_uuid)[16 ]; };struct hci_conn_hash {
@@ -1396,6 +1401,7 @@ void mgmt_new_conn_param(struct hci_dev *hdev, bdaddr_t *bdaddr, u16 max_interval, u16 latency, u16 timeout);void mgmt_reenable_advertising (struct hci_dev *hdev);void mgmt_smp_complete (struct hci_conn *conn,bool complete); +void mgmt_clean_up_service_discovery (struct hci_dev *hdev);u8 hci_le_conn_update (struct hci_conn *conn,u16 min,u16 max,u16 latency,u16 to_multiplier);
include/net/bluetooth/mgmt.h +9 -0
@@ -495,6 +495,15 @@ struct mgmt_cp_set_public_address { } __packed;#define MGMT_SET_PUBLIC_ADDRESS_SIZE 6 +#define MGMT_OP_START_SERVICE_DISCOVERY 0x003A +struct mgmt_cp_start_service_discovery { +__u8 type; +__s8 rssi_threshold; +__le16 num_uuid; +__u8 uuid[0 ][16 ]; +} __packed; +#define MGMT_START_SERVICE_DISCOVERY_SIZE 4 +#define MGMT_EV_CMD_COMPLETE 0x0001struct mgmt_ev_cmd_complete {__le16 opcode;
net/bluetooth/hci_core.c +1 -0
@@ -3853,6 +3853,7 @@ static void le_scan_disable_work(struct work_struct *work)BT_DBG ("%s" , hdev->name);cancel_delayed_work_sync (&hdev->le_scan_restart); +mgmt_clean_up_service_discovery (hdev);hci_req_init (&req, hdev);
net/bluetooth/mgmt.c +250 -27
420 lines not on this page · raw message
Signature · 1 line
2.2.0.rc0.207.ga3a616c
Re: [PATCH v11 3/3] Bluetooth: start and stop service discovery
Hi Jakub,
Quoted 18 lines · Jakub Pawlowski
This patch introduces start service discovery method. The reason behind that is to enable users to find specific services in range by UUID. Whole filtering is done in
mgmt_device_found.Signed-off-by Jakub Pawlowski <[email protected]>--- include/net/bluetooth/hci.h | 1 + include/net/bluetooth/hci_core.h | 6 + include/net/bluetooth/mgmt.h | 9 ++ net/bluetooth/hci_core.c | 1 + net/bluetooth/mgmt.c | 277 +++++++++++++++++++++++++++++++++++---- 5 files changed, 267 insertions(+), 27 deletions(-)include/net/bluetooth/hci.h +0
-0
include/net/bluetooth/hci.h enum { · line 215
Quoted 5 lines
include/net/bluetooth/hci.h +1
-0@@ -211,6 +211,7 @@ enum {*/ enum {HCI_LE_SCAN_RESTARTING , +HCI_SERVICE_FILTER , };
At closer inspection it seems to me that this flag is redundant. Take for example the following if-statement from later on in your patch:
include/net/bluetooth/hci.h enum {
Quoted 6 lines
include/net/bluetooth/hci.h +6
-0+if (!test_bit (HCI_SERVICE_FILTER , &hdev->discovery.flags) || + (hdev->discovery.sd_rssi ==127 && + hdev->discovery.sd_num_uuid ==0 )) { +mgmt_event (MGMT_EV_DEVICE_FOUND , hdev, ev, ev_size,NULL ); +return ; + }
It should be possible to use simply the RSSI and UUID count to select the special "service discovery" behavior. I might have overlooked something though, in which case please enlighten me :)
include/net/bluetooth/hci.h
Quoted 6 lines
include/net/bluetooth/hci.h +5
-0+ +/* sd prefix stands for service discovery related properties */ +s8 sd_rssi; +u16 sd_num_uuid; +u8 (*sd_uuid)[16 ]; };
Minor cosmetic thing I know, but I'd prefer to drop the sd_ prefix from these. I don't think we'll ever end up having similar variables but for a different purpose in the discovery struct (and if we do then we can always add namespacing back).
I've also corrected the parameter naming in mgmt-api.txt to match what we use for other "lists" like Link Keys. The variables here should also reflect that. I'd suggest as follows:
s8 rssi; u16 uuid_count; u8 (*uuids)[16];
include/net/bluetooth/hci.h
Quoted 7 lines
include/net/bluetooth/hci.h +7
-0+#define MGMT_OP_START_SERVICE_DISCOVERY 0x003A +struct mgmt_cp_start_service_discovery { +__u8 type; +__s8 rssi_threshold; +__le16 num_uuid; +__u8 uuid[0 ][16 ]; +} __packed;
Here you should also update the naming. I'm not sure it's worth having the _treshold though since you're using just "rssi" elsewhere.
include/net/bluetooth/hci.h
Quoted 11 lines
include/net/bluetooth/hci.h +11
-0+/* cleans up the state set up by the start_service_discovery function. */ +void mgmt_clean_up_service_discovery (struct hci_dev *hdev) +{ +if (!test_and_clear_bit (HCI_SERVICE_FILTER , &hdev->discovery.flags)) +return ; + +cancel_delayed_work_sync (&hdev->le_scan_restart); +if (hdev->discovery.sd_num_uuid >0 ) +kfree (hdev->discovery.sd_uuid); + hdev->discovery.sd_num_uuid =0 ; +}
Is this function really needed? I mean, what it does is clearly needed, but I'd have just put it into hci_discovery_set_state() when going to DISCOVERY_STOPPED. You could of course keep the code as a helper function but just call it from hci_discovery_set_state(). I might have overlooked something of course but that'd seem the most natural (and simple) place to put this stuff.
Btw, the dependency of num_uuid to the kfree call still looks wrong to me (I asked to change it in an earlier review). The kfree() could be made unconditional as long as you remember to set the uuid pointer to NULL afterwards.
include/net/bluetooth/hci.h
Quoted 12 lines
include/net/bluetooth/hci.h +12
-0+static int init_service_discovery (struct hci_dev *hdev,s8 rssi,u16 num_uuid, +u8 (*uuid)[16 ]) +{ + hdev->discovery.sd_rssi = rssi; + hdev->discovery.sd_num_uuid = num_uuid; + +if (num_uuid >0 ) { + hdev->discovery.sd_uuid =kmalloc (16 * num_uuid,GFP_KERNEL ); +if (!hdev->discovery.sd_uuid) +return -ENOMEM ; +memcpy (hdev->discovery.sd_uuid, uuid,16 * num_uuid); + }
The memcpy and kmalloc calls could be simplified to a single kmemdup call:
hdev->discovery.uuids = kmemdup(uuids, 16 * uuid_count, GFP_KERNEL);
Quoted 23 lines
static int generic_start_discovery(struct sock *sk, struct
hci_dev*hdev,include/net/bluetooth/hci.h +7
-2void *data, u16 len, u16 opcode) { -struct mgmt_cp_start_discovery *cp = data;struct pending_cmd *cmd;struct hci_cp_le_set_scan_param param_cp;struct hci_cp_le_set_scan_enable enable_cp;struct hci_cp_inquiry inq_cp;struct hci_request req;/* General inquiry access code (GIAC) */ +s8 sd_rssi =0 ;u8 lap[3 ] = {0x33 ,0x8b ,0x9e }; -u8 status, own_addr_type; +u8 status, own_addr_type, type; +u8 (*sd_uuid)[16 ] =NULL ; +u16 sd_num_uuid =0 ;int err;BT_DBG ("%s" , hdev->name); +if (opcode ==MGMT_OP_START_SERVICE_DISCOVERY ) { +struct mgmt_cp_start_service_discovery *cp = data; +u16 expected_len, num_uuid_tmp;
include/net/bluetooth/hci.h
Quoted 10 lines
include/net/bluetooth/hci.h +10
-0+ + type = cp->type; + num_uuid_tmp =__le16_to_cpu (cp->num_uuid); + expected_len =sizeof (*cp) + num_uuid_tmp *16 ; + +if (expected_len != len) { +return cmd_complete (sk, hdev->id, opcode, +MGMT_STATUS_INVALID_PARAMS , &type, +sizeof (type)); + }
No need for the { } in a one-statement branch. Also, the num_uuid_tmp variable seems unnecessary here. Can't you just directly assign to your sd_num_uuid variable? (which btw should be renamed to uuid_count)
include/net/bluetooth/hci.h
Quoted 9 lines
include/net/bluetooth/hci.h +9
-0+ sd_rssi = cp->rssi_threshold; + sd_num_uuid = num_uuid_tmp; +if (sd_num_uuid >0 ) + sd_uuid = cp->uuid; + }else { +struct mgmt_cp_start_discovery *cp = data; + + type = cp->type; + }
Since you've got a dedicated branch for each type of discovery we could avoid the sd_num_uuid initialization to 0 upon declaration (something that's in general discouraged whenever possible). The you'd just set it to the __le16_to_cpu() result in the first branch and 0 in the second.
Johan
Hi Jakub,
include/net/bluetooth/hci.h enum { · line 214
Quoted 7 lines · Jakub Pawlowski
include/net/bluetooth/hci.h +7
-0+/* +* BR/EDR and/or LE discovery state flags: the flags defined here should +* represent state of discovery +*/ +enum { +HCI_LE_SCAN_RESTARTING , +};
I think this enum should be as close as possible to the struct discovery_state definition, i.e. right before it in hci_core.h. The hci.h file should be strictly for HCI definitions only, but for historical reasons it also contains some implementation specific stuff.
Also note that the net/ coding style doesn't use an empty first line for multi-line comments.
Quoted 27 lines
include/net/bluetooth/hci_core.h +3
-0@@ -75,6 +75,7 @@ struct discovery_state {u32 last_adv_flags;u8 last_adv_data[HCI_MAX_AD_LENGTH ];u8 last_adv_data_len; +unsigned long flags; };struct hci_conn_hash {@@ -337,6 +338,7 @@ struct hci_dev {unsigned long dev_flags;struct delayed_work le_scan_disable; +struct delayed_work le_scan_restart;__s8 adv_tx_power;__u8 adv_data[HCI_MAX_AD_LENGTH ];@@ -1326,6 +1328,7 @@ void hci_sock_dev_event(struct hci_dev *hdev, int event);#define DISCOV_INTERLEAVED_TIMEOUT 5120/* msec */ #define DISCOV_INTERLEAVED_INQUIRY_LEN 0x04#define DISCOV_BREDR_INQUIRY_LEN 0x08 +#define DISCOV_LE_RESTART_DELAY 300/* msec */ int mgmt_control (struct sock *sk,struct msghdr *msg,size_t len);int mgmt_new_settings (struct hci_dev *hdev);
include/net/bluetooth/hci_core.h int hci_update_random_address(struct hci_request *req, bool require_privacy, · line 1409
Quoted 4 lines
include/net/bluetooth/hci_core.h +1
-0@@ -1403,6 +1406,7 @@ int hci_update_random_address(struct hci_request *req, bool require_privacy, u8 *own_addr_type);void hci_copy_identity_address (struct hci_dev *hdev,bdaddr_t *bdaddr,u8 *bdaddr_type); +void hci_restart_le_scan (struct hci_dev *hdev);
When looking at your later patches (mainly 3/3) this hci_restart_le_scan() functionality is only accessed from within mgmt.c. I'd therefore suggest to simply have it all as part of mgmt.c (as static functions) and only later export it through hci_core.h if there really is a need.
Johan