Skip to content

Commit e5e1e7f

Browse files
mcchou-crjhedberg
authored andcommitted
Bluetooth: Add handler of MGMT_OP_READ_ADV_MONITOR_FEATURES
This adds the request handler of MGMT_OP_READ_ADV_MONITOR_FEATURES command. Since the controller-based monitoring is not yet in place, this report only the supported features but not the enabled features. The following test was performed. - Issuing btmgmt advmon-features. Signed-off-by: Miao-chen Chou <mcchou@chromium.org> Signed-off-by: Marcel Holtmann <marcel@holtmann.org> Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
1 parent 7fceb17 commit e5e1e7f

5 files changed

Lines changed: 97 additions & 1 deletion

File tree

‎include/net/bluetooth/hci_core.h‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#ifndef __HCI_CORE_H
2626
#define __HCI_CORE_H
2727

28+
#include <linux/idr.h>
2829
#include <linux/leds.h>
2930
#include <linux/rculist.h>
3031

@@ -237,6 +238,24 @@ struct adv_info {
237238
#define HCI_MAX_ADV_INSTANCES 5
238239
#define HCI_DEFAULT_ADV_DURATION 2
239240

241+
struct adv_pattern {
242+
struct list_head list;
243+
__u8 ad_type;
244+
__u8 offset;
245+
__u8 length;
246+
__u8 value[HCI_MAX_AD_LENGTH];
247+
};
248+
249+
struct adv_monitor {
250+
struct list_head patterns;
251+
bool active;
252+
__u16 handle;
253+
};
254+
255+
#define HCI_MIN_ADV_MONITOR_HANDLE 1
256+
#define HCI_MAX_ADV_MONITOR_NUM_HANDLES 32
257+
#define HCI_MAX_ADV_MONITOR_NUM_PATTERNS 16
258+
240259
#define HCI_MAX_SHORT_NAME_LENGTH 10
241260

242261
/* Min encryption key size to match with SMP */
@@ -511,6 +530,9 @@ struct hci_dev {
511530
__u16 adv_instance_timeout;
512531
struct delayed_work adv_instance_expire;
513532

533+
struct idr adv_monitors_idr;
534+
unsigned int adv_monitors_cnt;
535+
514536
__u8 irk[16];
515537
__u32 rpa_timeout;
516538
struct delayed_work rpa_expired;
@@ -1258,6 +1280,8 @@ int hci_add_adv_instance(struct hci_dev *hdev, u8 instance, u32 flags,
12581280
int hci_remove_adv_instance(struct hci_dev *hdev, u8 instance);
12591281
void hci_adv_instances_set_rpa_expired(struct hci_dev *hdev, bool rpa_expired);
12601282

1283+
void hci_adv_monitors_clear(struct hci_dev *hdev);
1284+
12611285
void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb);
12621286

12631287
void hci_init_sysfs(struct hci_dev *hdev);

‎net/bluetooth/hci_core.c‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
/* Bluetooth HCI core. */
2727

2828
#include <linux/export.h>
29-
#include <linux/idr.h>
3029
#include <linux/rfkill.h>
3130
#include <linux/debugfs.h>
3231
#include <linux/crypto.h>
@@ -2996,6 +2995,12 @@ int hci_add_adv_instance(struct hci_dev *hdev, u8 instance, u32 flags,
29962995
return 0;
29972996
}
29982997

2998+
/* This function requires the caller holds hdev->lock */
2999+
void hci_adv_monitors_clear(struct hci_dev *hdev)
3000+
{
3001+
idr_destroy(&hdev->adv_monitors_idr);
3002+
}
3003+
29993004
struct bdaddr_list *hci_bdaddr_list_lookup(struct list_head *bdaddr_list,
30003005
bdaddr_t *bdaddr, u8 type)
30013006
{
@@ -3646,6 +3651,8 @@ int hci_register_dev(struct hci_dev *hdev)
36463651

36473652
queue_work(hdev->req_workqueue, &hdev->power_on);
36483653

3654+
idr_init(&hdev->adv_monitors_idr);
3655+
36493656
return id;
36503657

36513658
err_wqueue:
@@ -3716,6 +3723,7 @@ void hci_unregister_dev(struct hci_dev *hdev)
37163723
hci_smp_irks_clear(hdev);
37173724
hci_remote_oob_data_clear(hdev);
37183725
hci_adv_instances_clear(hdev);
3726+
hci_adv_monitors_clear(hdev);
37193727
hci_bdaddr_list_clear(&hdev->le_white_list);
37203728
hci_bdaddr_list_clear(&hdev->le_resolv_list);
37213729
hci_conn_params_clear_all(hdev);

‎net/bluetooth/mgmt.c‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "smp.h"
3838
#include "mgmt_util.h"
3939
#include "mgmt_config.h"
40+
#include "msft.h"
4041

4142
#define MGMT_VERSION 1
4243
#define MGMT_REVISION 17
@@ -118,6 +119,7 @@ static const u16 mgmt_commands[] = {
118119
MGMT_OP_SET_DEF_RUNTIME_CONFIG,
119120
MGMT_OP_GET_DEVICE_FLAGS,
120121
MGMT_OP_SET_DEVICE_FLAGS,
122+
MGMT_OP_READ_ADV_MONITOR_FEATURES,
121123
};
122124

123125
static const u16 mgmt_events[] = {
@@ -3973,6 +3975,51 @@ static int set_device_flags(struct sock *sk, struct hci_dev *hdev, void *data,
39733975
&cp->addr, sizeof(cp->addr));
39743976
}
39753977

3978+
static int read_adv_mon_features(struct sock *sk, struct hci_dev *hdev,
3979+
void *data, u16 len)
3980+
{
3981+
struct adv_monitor *monitor = NULL;
3982+
struct mgmt_rp_read_adv_monitor_features *rp = NULL;
3983+
int handle;
3984+
size_t rp_size = 0;
3985+
__u32 supported = 0;
3986+
__u16 num_handles = 0;
3987+
__u16 handles[HCI_MAX_ADV_MONITOR_NUM_HANDLES];
3988+
3989+
BT_DBG("request for %s", hdev->name);
3990+
3991+
hci_dev_lock(hdev);
3992+
3993+
if (msft_get_features(hdev) & MSFT_FEATURE_MASK_LE_ADV_MONITOR)
3994+
supported |= MGMT_ADV_MONITOR_FEATURE_MASK_OR_PATTERNS;
3995+
3996+
idr_for_each_entry(&hdev->adv_monitors_idr, monitor, handle) {
3997+
handles[num_handles++] = monitor->handle;
3998+
}
3999+
4000+
hci_dev_unlock(hdev);
4001+
4002+
rp_size = sizeof(*rp) + (num_handles * sizeof(u16));
4003+
rp = kmalloc(rp_size, GFP_KERNEL);
4004+
if (!rp)
4005+
return -ENOMEM;
4006+
4007+
/* Once controller-based monitoring is in place, the enabled_features
4008+
* should reflect the use.
4009+
*/
4010+
rp->supported_features = cpu_to_le32(supported);
4011+
rp->enabled_features = 0;
4012+
rp->max_num_handles = cpu_to_le16(HCI_MAX_ADV_MONITOR_NUM_HANDLES);
4013+
rp->max_num_patterns = HCI_MAX_ADV_MONITOR_NUM_PATTERNS;
4014+
rp->num_handles = cpu_to_le16(num_handles);
4015+
if (num_handles)
4016+
memcpy(&rp->handles, &handles, (num_handles * sizeof(u16)));
4017+
4018+
return mgmt_cmd_complete(sk, hdev->id,
4019+
MGMT_OP_READ_ADV_MONITOR_FEATURES,
4020+
MGMT_STATUS_SUCCESS, rp, rp_size);
4021+
}
4022+
39764023
static void read_local_oob_data_complete(struct hci_dev *hdev, u8 status,
39774024
u16 opcode, struct sk_buff *skb)
39784025
{
@@ -7441,6 +7488,7 @@ static const struct hci_mgmt_handler mgmt_handlers[] = {
74417488
HCI_MGMT_VAR_LEN },
74427489
{ get_device_flags, MGMT_GET_DEVICE_FLAGS_SIZE },
74437490
{ set_device_flags, MGMT_SET_DEVICE_FLAGS_SIZE },
7491+
{ read_adv_mon_features, MGMT_READ_ADV_MONITOR_FEATURES_SIZE },
74447492
};
74457493

74467494
void mgmt_index_added(struct hci_dev *hdev)

‎net/bluetooth/msft.c‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,10 @@ void msft_vendor_evt(struct hci_dev *hdev, struct sk_buff *skb)
139139

140140
bt_dev_dbg(hdev, "MSFT vendor event %u", event);
141141
}
142+
143+
__u64 msft_get_features(struct hci_dev *hdev)
144+
{
145+
struct msft_data *msft = hdev->msft_data;
146+
147+
return msft ? msft->features : 0;
148+
}

‎net/bluetooth/msft.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,25 @@
33
* Copyright (C) 2020 Google Corporation
44
*/
55

6+
#define MSFT_FEATURE_MASK_BREDR_RSSI_MONITOR BIT(0)
7+
#define MSFT_FEATURE_MASK_LE_CONN_RSSI_MONITOR BIT(1)
8+
#define MSFT_FEATURE_MASK_LE_ADV_RSSI_MONITOR BIT(2)
9+
#define MSFT_FEATURE_MASK_LE_ADV_MONITOR BIT(3)
10+
#define MSFT_FEATURE_MASK_CURVE_VALIDITY BIT(4)
11+
#define MSFT_FEATURE_MASK_CONCURRENT_ADV_MONITOR BIT(5)
12+
613
#if IS_ENABLED(CONFIG_BT_MSFTEXT)
714

815
void msft_do_open(struct hci_dev *hdev);
916
void msft_do_close(struct hci_dev *hdev);
1017
void msft_vendor_evt(struct hci_dev *hdev, struct sk_buff *skb);
18+
__u64 msft_get_features(struct hci_dev *hdev);
1119

1220
#else
1321

1422
static inline void msft_do_open(struct hci_dev *hdev) {}
1523
static inline void msft_do_close(struct hci_dev *hdev) {}
1624
static inline void msft_vendor_evt(struct hci_dev *hdev, struct sk_buff *skb) {}
25+
static inline __u64 msft_get_features(struct hci_dev *hdev) { return 0; }
1726

1827
#endif

0 commit comments

Comments
 (0)