Skip to content

Commit 4c54bf2

Browse files
apanditjhedberg
authored andcommitted
Bluetooth: Add get/set device flags mgmt op
Add the get device flags and set device flags mgmt ops and the device flags changed event. Their behavior is described in detail in mgmt-api.txt in bluez. Sample btmon trace when a HID device is added (trimmed to 75 chars): @ MGMT Command: Unknown (0x0050) plen 11 {0x0001} [hci0] 18:06:14.98 90 c5 13 cd f3 cd 02 01 00 00 00 ........... @ MGMT Event: Unknown (0x002a) plen 15 {0x0004} [hci0] 18:06:14.98 90 c5 13 cd f3 cd 02 01 00 00 00 01 00 00 00 ............... @ MGMT Event: Unknown (0x002a) plen 15 {0x0003} [hci0] 18:06:14.98 90 c5 13 cd f3 cd 02 01 00 00 00 01 00 00 00 ............... @ MGMT Event: Unknown (0x002a) plen 15 {0x0002} [hci0] 18:06:14.98 90 c5 13 cd f3 cd 02 01 00 00 00 01 00 00 00 ............... @ MGMT Event: Command Compl.. (0x0001) plen 10 {0x0001} [hci0] 18:06:14.98 Unknown (0x0050) plen 7 Status: Success (0x00) 90 c5 13 cd f3 cd 02 ....... @ MGMT Command: Add Device (0x0033) plen 8 {0x0001} [hci0] 18:06:14.98 LE Address: CD:F3:CD:13:C5:90 (Static) Action: Auto-connect remote device (0x02) @ MGMT Event: Device Added (0x001a) plen 8 {0x0004} [hci0] 18:06:14.98 LE Address: CD:F3:CD:13:C5:90 (Static) Action: Auto-connect remote device (0x02) @ MGMT Event: Device Added (0x001a) plen 8 {0x0003} [hci0] 18:06:14.98 LE Address: CD:F3:CD:13:C5:90 (Static) Action: Auto-connect remote device (0x02) @ MGMT Event: Device Added (0x001a) plen 8 {0x0002} [hci0] 18:06:14.98 LE Address: CD:F3:CD:13:C5:90 (Static) Action: Auto-connect remote device (0x02) @ MGMT Event: Unknown (0x002a) plen 15 {0x0004} [hci0] 18:06:14.98 90 c5 13 cd f3 cd 02 01 00 00 00 01 00 00 00 ............... @ MGMT Event: Unknown (0x002a) plen 15 {0x0003} [hci0] 18:06:14.98 90 c5 13 cd f3 cd 02 01 00 00 00 01 00 00 00 ............... @ MGMT Event: Unknown (0x002a) plen 15 {0x0002} [hci0] 18:06:14.98 90 c5 13 cd f3 cd 02 01 00 00 00 01 00 00 00 ............... @ MGMT Event: Unknown (0x002a) plen 15 {0x0001} [hci0] 18:06:14.98 90 c5 13 cd f3 cd 02 01 00 00 00 01 00 00 00 ............... Signed-off-by: Abhishek Pandit-Subedi <abhishekpandit@chromium.org> Reviewed-by: Alain Michaud <alainm@chromium.org> Signed-off-by: Marcel Holtmann <marcel@holtmann.org> Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
1 parent a1fc753 commit 4c54bf2

2 files changed

Lines changed: 156 additions & 0 deletions

File tree

‎include/net/bluetooth/mgmt.h‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,27 @@ struct mgmt_rp_set_exp_feature {
720720
#define MGMT_OP_SET_DEF_RUNTIME_CONFIG 0x004e
721721
#define MGMT_SET_DEF_RUNTIME_CONFIG_SIZE 0
722722

723+
#define MGMT_OP_GET_DEVICE_FLAGS 0x004F
724+
#define MGMT_GET_DEVICE_FLAGS_SIZE 7
725+
struct mgmt_cp_get_device_flags {
726+
struct mgmt_addr_info addr;
727+
} __packed;
728+
struct mgmt_rp_get_device_flags {
729+
struct mgmt_addr_info addr;
730+
__le32 supported_flags;
731+
__le32 current_flags;
732+
} __packed;
733+
734+
#define MGMT_OP_SET_DEVICE_FLAGS 0x0050
735+
#define MGMT_SET_DEVICE_FLAGS_SIZE 11
736+
struct mgmt_cp_set_device_flags {
737+
struct mgmt_addr_info addr;
738+
__le32 current_flags;
739+
} __packed;
740+
struct mgmt_rp_set_device_flags {
741+
struct mgmt_addr_info addr;
742+
} __packed;
743+
723744
#define MGMT_EV_CMD_COMPLETE 0x0001
724745
struct mgmt_ev_cmd_complete {
725746
__le16 opcode;
@@ -951,3 +972,10 @@ struct mgmt_ev_exp_feature_changed {
951972
__u8 uuid[16];
952973
__le32 flags;
953974
} __packed;
975+
976+
#define MGMT_EV_DEVICE_FLAGS_CHANGED 0x002a
977+
struct mgmt_ev_device_flags_changed {
978+
struct mgmt_addr_info addr;
979+
__le32 supported_flags;
980+
__le32 current_flags;
981+
} __packed;

‎net/bluetooth/mgmt.c‎

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ static const u16 mgmt_commands[] = {
116116
MGMT_OP_SET_DEF_SYSTEM_CONFIG,
117117
MGMT_OP_READ_DEF_RUNTIME_CONFIG,
118118
MGMT_OP_SET_DEF_RUNTIME_CONFIG,
119+
MGMT_OP_GET_DEVICE_FLAGS,
120+
MGMT_OP_SET_DEVICE_FLAGS,
119121
};
120122

121123
static const u16 mgmt_events[] = {
@@ -156,6 +158,7 @@ static const u16 mgmt_events[] = {
156158
MGMT_EV_EXT_INFO_CHANGED,
157159
MGMT_EV_PHY_CONFIGURATION_CHANGED,
158160
MGMT_EV_EXP_FEATURE_CHANGED,
161+
MGMT_EV_DEVICE_FLAGS_CHANGED,
159162
};
160163

161164
static const u16 mgmt_untrusted_commands[] = {
@@ -3856,6 +3859,120 @@ static int set_exp_feature(struct sock *sk, struct hci_dev *hdev,
38563859
MGMT_STATUS_NOT_SUPPORTED);
38573860
}
38583861

3862+
#define SUPPORTED_DEVICE_FLAGS() ((1U << HCI_CONN_FLAG_MAX) - 1)
3863+
3864+
static int get_device_flags(struct sock *sk, struct hci_dev *hdev, void *data,
3865+
u16 data_len)
3866+
{
3867+
struct mgmt_cp_get_device_flags *cp = data;
3868+
struct mgmt_rp_get_device_flags rp;
3869+
struct bdaddr_list_with_flags *br_params;
3870+
struct hci_conn_params *params;
3871+
u32 supported_flags = SUPPORTED_DEVICE_FLAGS();
3872+
u32 current_flags = 0;
3873+
u8 status = MGMT_STATUS_INVALID_PARAMS;
3874+
3875+
bt_dev_dbg(hdev, "Get device flags %pMR (type 0x%x)\n",
3876+
&cp->addr.bdaddr, cp->addr.type);
3877+
3878+
if (cp->addr.type == BDADDR_BREDR) {
3879+
br_params = hci_bdaddr_list_lookup_with_flags(&hdev->whitelist,
3880+
&cp->addr.bdaddr,
3881+
cp->addr.type);
3882+
if (!br_params)
3883+
goto done;
3884+
3885+
current_flags = br_params->current_flags;
3886+
} else {
3887+
params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr,
3888+
le_addr_type(cp->addr.type));
3889+
3890+
if (!params)
3891+
goto done;
3892+
3893+
current_flags = params->current_flags;
3894+
}
3895+
3896+
bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
3897+
rp.addr.type = cp->addr.type;
3898+
rp.supported_flags = cpu_to_le32(supported_flags);
3899+
rp.current_flags = cpu_to_le32(current_flags);
3900+
3901+
status = MGMT_STATUS_SUCCESS;
3902+
3903+
done:
3904+
return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_DEVICE_FLAGS, status,
3905+
&rp, sizeof(rp));
3906+
}
3907+
3908+
static void device_flags_changed(struct sock *sk, struct hci_dev *hdev,
3909+
bdaddr_t *bdaddr, u8 bdaddr_type,
3910+
u32 supported_flags, u32 current_flags)
3911+
{
3912+
struct mgmt_ev_device_flags_changed ev;
3913+
3914+
bacpy(&ev.addr.bdaddr, bdaddr);
3915+
ev.addr.type = bdaddr_type;
3916+
ev.supported_flags = cpu_to_le32(supported_flags);
3917+
ev.current_flags = cpu_to_le32(current_flags);
3918+
3919+
mgmt_event(MGMT_EV_DEVICE_FLAGS_CHANGED, hdev, &ev, sizeof(ev), sk);
3920+
}
3921+
3922+
static int set_device_flags(struct sock *sk, struct hci_dev *hdev, void *data,
3923+
u16 len)
3924+
{
3925+
struct mgmt_cp_set_device_flags *cp = data;
3926+
struct bdaddr_list_with_flags *br_params;
3927+
struct hci_conn_params *params;
3928+
u8 status = MGMT_STATUS_INVALID_PARAMS;
3929+
u32 supported_flags = SUPPORTED_DEVICE_FLAGS();
3930+
u32 current_flags = __le32_to_cpu(cp->current_flags);
3931+
3932+
bt_dev_dbg(hdev, "Set device flags %pMR (type 0x%x) = 0x%x",
3933+
&cp->addr.bdaddr, cp->addr.type,
3934+
__le32_to_cpu(current_flags));
3935+
3936+
if ((supported_flags | current_flags) != supported_flags) {
3937+
bt_dev_warn(hdev, "Bad flag given (0x%x) vs supported (0x%0x)",
3938+
current_flags, supported_flags);
3939+
goto done;
3940+
}
3941+
3942+
if (cp->addr.type == BDADDR_BREDR) {
3943+
br_params = hci_bdaddr_list_lookup_with_flags(&hdev->whitelist,
3944+
&cp->addr.bdaddr,
3945+
cp->addr.type);
3946+
3947+
if (br_params) {
3948+
br_params->current_flags = current_flags;
3949+
status = MGMT_STATUS_SUCCESS;
3950+
} else {
3951+
bt_dev_warn(hdev, "No such BR/EDR device %pMR (0x%x)",
3952+
&cp->addr.bdaddr, cp->addr.type);
3953+
}
3954+
} else {
3955+
params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr,
3956+
le_addr_type(cp->addr.type));
3957+
if (params) {
3958+
params->current_flags = current_flags;
3959+
status = MGMT_STATUS_SUCCESS;
3960+
} else {
3961+
bt_dev_warn(hdev, "No such LE device %pMR (0x%x)",
3962+
&cp->addr.bdaddr,
3963+
le_addr_type(cp->addr.type));
3964+
}
3965+
}
3966+
3967+
done:
3968+
if (status == MGMT_STATUS_SUCCESS)
3969+
device_flags_changed(sk, hdev, &cp->addr.bdaddr, cp->addr.type,
3970+
supported_flags, current_flags);
3971+
3972+
return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_FLAGS, status,
3973+
&cp->addr, sizeof(cp->addr));
3974+
}
3975+
38593976
static void read_local_oob_data_complete(struct hci_dev *hdev, u8 status,
38603977
u16 opcode, struct sk_buff *skb)
38613978
{
@@ -5973,7 +6090,9 @@ static int add_device(struct sock *sk, struct hci_dev *hdev,
59736090
{
59746091
struct mgmt_cp_add_device *cp = data;
59756092
u8 auto_conn, addr_type;
6093+
struct hci_conn_params *params;
59766094
int err;
6095+
u32 current_flags = 0;
59776096

59786097
bt_dev_dbg(hdev, "sock %p", sk);
59796098

@@ -6041,12 +6160,19 @@ static int add_device(struct sock *sk, struct hci_dev *hdev,
60416160
MGMT_STATUS_FAILED, &cp->addr,
60426161
sizeof(cp->addr));
60436162
goto unlock;
6163+
} else {
6164+
params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr,
6165+
addr_type);
6166+
if (params)
6167+
current_flags = params->current_flags;
60446168
}
60456169

60466170
hci_update_background_scan(hdev);
60476171

60486172
added:
60496173
device_added(sk, hdev, &cp->addr.bdaddr, cp->addr.type, cp->action);
6174+
device_flags_changed(NULL, hdev, &cp->addr.bdaddr, cp->addr.type,
6175+
SUPPORTED_DEVICE_FLAGS(), current_flags);
60506176

60516177
err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
60526178
MGMT_STATUS_SUCCESS, &cp->addr,
@@ -7313,6 +7439,8 @@ static const struct hci_mgmt_handler mgmt_handlers[] = {
73137439
HCI_MGMT_UNTRUSTED },
73147440
{ set_def_runtime_config, MGMT_SET_DEF_RUNTIME_CONFIG_SIZE,
73157441
HCI_MGMT_VAR_LEN },
7442+
{ get_device_flags, MGMT_GET_DEVICE_FLAGS_SIZE },
7443+
{ set_device_flags, MGMT_SET_DEVICE_FLAGS_SIZE },
73167444
};
73177445

73187446
void mgmt_index_added(struct hci_dev *hdev)

0 commit comments

Comments
 (0)