Basic Management interface command handling
Subsystems (2)
Hi,
Here's a set of patches for dealing with basic commands needed for local adapter enumeration and information featching.
I'm not completely sure about the parameter list of read_info (maybe something is missing or too much) but we can fine tune that later as long as the interface is experimental. The basic idea is to avoid too many messages to get the initial set of info about an adapter and then have separate "value X changed" events from the kernel to userspace when individual values change. For the values that can be changed by the user there'll also be individual "set value" commands.
With this set of patches you should be able to get the enumeration (at least somewhat) working with current bluez.git:
echo 1 > /sys/module/bluetooth/parameters/enable_mgmt bluetoothd -n -d -p mgmtops
There's no power control (HCI_UP/HCI_DOWN) available yet, so you'll get more meaningful values in read_info if you do "hciconfig hci0 up" before running bluetoothd.
Johan
PATCH 1/5 Bluetooth: Add error handling for managment command handlers
From: Johan Hedberg <[email protected]>
The command handlers for bluetooth management messaging should be able to report errors (such as memory allocation failures) to the higher levels in the call stack.
--- net/bluetooth/mgmt.c | 11 ++++++++--- 1 files changed, 8 insertions(+), 3 deletions(-)
net/bluetooth/mgmt.c +8 -3
@@ -29,7 +29,7 @@#include <net/bluetooth/hci_core.h> #include <net/bluetooth/mgmt.h> -static void cmd_status (struct sock *sk,u16 cmd,u8 status) +static int cmd_status (struct sock *sk,u16 cmd,u8 status) {struct sk_buff *skb;struct mgmt_hdr *hdr;
@@ -39,7 +39,7 @@ static void cmd_status(struct sock *sk, u16 cmd, u8 status) skb =alloc_skb (sizeof (*hdr) +sizeof (*ev),GFP_ATOMIC );if (!skb) -return ; +return -ENOMEM ; hdr = (void *)skb_put (skb,sizeof (*hdr));
@@ -52,6 +52,8 @@ static void cmd_status(struct sock *sk, u16 cmd, u8 status)if (sock_queue_rcv_skb (sk, skb) <0 )kfree_skb (skb); + +return 0 ; }int mgmt_control (struct sock *sk,struct msghdr *msg,size_t msglen)
@@ -87,10 +89,13 @@ int mgmt_control(struct sock *sk, struct msghdr *msg, size_t msglen)switch (opcode) {default :BT_DBG ("Unknown op %u" , opcode); -cmd_status (sk, opcode,0x01 ); + err =cmd_status (sk, opcode,0x01 );break ; } +if (err <0 ) + goto done; + err = msglen; done:
Signature · 1 line
1.7.2.3
Re: [PATCH 1/5] Bluetooth: Add error handling for managment command handlers
Hi Johan,
Quoted 45 lines · * [email protected] <[email protected]> [2010-12-13 21:07:03 +0200]:
From: Johan Hedberg <[email protected]>
The command handlers for bluetooth management messaging should be able to report errors (such as memory allocation failures) to the higher levels in the call stack.
Signed-off-by Johan Hedberg <[email protected]>--- net/bluetooth/mgmt.c | 11 ++++++++--- 1 files changed, 8 insertions(+), 3 deletions(-)net/bluetooth/mgmt.c +4
-2@@ -29,7 +29,7 @@#include <net/bluetooth/hci_core.h> #include <net/bluetooth/mgmt.h> -static void cmd_status (struct sock *sk,u16 cmd,u8 status) +static int cmd_status (struct sock *sk,u16 cmd,u8 status) {struct sk_buff *skb;struct mgmt_hdr *hdr;@@ -39,7 +39,7 @@ static void cmd_status(struct sock *sk, u16 cmd, u8 status) skb =alloc_skb (sizeof (*hdr) +sizeof (*ev),GFP_ATOMIC );if (!skb) -return ; +return -ENOMEM ; hdr = (void *)skb_put (skb,sizeof (*hdr));@@ -52,6 +52,8 @@ static void cmd_status(struct sock *sk, u16 cmd, u8 status)if (sock_queue_rcv_skb (sk, skb) <0 )kfree_skb (skb); + +return 0 ; }int mgmt_control (struct sock *sk,struct msghdr *msg,size_t msglen)@@ -87,10 +89,13 @@ int mgmt_control(struct sock *sk, struct msghdr *msg, size_t msglen)switch (opcode) {default :
net/bluetooth/mgmt.c int mgmt_control(struct sock *sk, struct msghdr *msg, size_t msglen) · line 99
Quoted 10 lines
net/bluetooth/mgmt.c +4
-1BT_DBG ("Unknown op %u" , opcode); -cmd_status (sk, opcode,0x01 ); + err =cmd_status (sk, opcode,0x01 );break ; } +if (err <0 ) + goto done; + err = msglen;
I think
if (!err) err = msglen;
is better.
Signature · 2 lines
Gustavo F. Padovan http://profusion.mobi
Re: [PATCH 1/5] Bluetooth: Add error handling for managment command handlers
Hi Gustavo,
Quoted 7 lines · Gustavo F. Padovan
Review
Quoted 4 lines
(quoted diff) +3
-0+ if (err < 0) + goto done; + err = msglen;I think
Quoted 5 lines
if (!err) err = msglen;is better.
Agreed. Here's an updated patch. I used "err == 0" since that seems to be more common at least in Marcel's user space projects for non-boolean variable tests.
Johan
Quoted 1 line
From
1b37152df73d9480382263f3e17c3856e8f403f3Mon Sep 17 00:00:00 2001
From: Johan Hedberg <[email protected]>
Date: Fri, 10 Dec 2010 12:06:16 +0200
Subject: [PATCH] Bluetooth: Add error handling for managment command handlers
The command handlers for bluetooth management messaging should be able to report errors (such as memory allocation failures) to the higher levels in the call stack.
--- net/bluetooth/mgmt.c | 11 +++++++---- 1 files changed, 7 insertions(+), 4 deletions(-)
net/bluetooth/mgmt.c +7 -4
@@ -29,7 +29,7 @@#include <net/bluetooth/hci_core.h> #include <net/bluetooth/mgmt.h> -static void cmd_status (struct sock *sk,u16 cmd,u8 status) +static int cmd_status (struct sock *sk,u16 cmd,u8 status) {struct sk_buff *skb;struct mgmt_hdr *hdr;
@@ -39,7 +39,7 @@ static void cmd_status(struct sock *sk, u16 cmd, u8 status) skb =alloc_skb (sizeof (*hdr) +sizeof (*ev),GFP_ATOMIC );if (!skb) -return ; +return -ENOMEM ; hdr = (void *)skb_put (skb,sizeof (*hdr));
@@ -52,6 +52,8 @@ static void cmd_status(struct sock *sk, u16 cmd, u8 status)if (sock_queue_rcv_skb (sk, skb) <0 )kfree_skb (skb); + +return 0 ; }int mgmt_control (struct sock *sk,struct msghdr *msg,size_t msglen)
@@ -87,11 +89,12 @@ int mgmt_control(struct sock *sk, struct msghdr *msg, size_t msglen)switch (opcode) {default :BT_DBG ("Unknown op %u" , opcode); -cmd_status (sk, opcode,0x01 ); + err =cmd_status (sk, opcode,0x01 );break ; } - err = msglen; +if (err ==0 ) + err = msglen; done:kfree (buf);
Signature · 1 line
1.7.2.3
PATCH 2/5 Bluetooth: Add read_version management command
In reply to [email protected]From: Johan Hedberg <[email protected]>
This patch implements the initial read_version command that userspace will use before any other management interface operations.
--- include/net/bluetooth/mgmt.h | 6 ++++++ net/bluetooth/mgmt.c | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 0 deletions(-)
include/net/bluetooth/mgmt.h +6 -0
@@ -27,6 +27,12 @@ struct mgmt_hdr { } __packed;#define MGMT_HDR_SIZE 4 +#define MGMT_OP_READ_VERSION 0x0001 +struct mgmt_rp_read_version { +__u8 version; +__le16 revision; +} __packed; +#define MGMT_EV_CMD_COMPLETE 0x0001struct mgmt_ev_cmd_complete {__le16 opcode;
net/bluetooth/mgmt.c +36 -0
@@ -29,6 +29,39 @@#include <net/bluetooth/hci_core.h> #include <net/bluetooth/mgmt.h> +#define MGMT_VERSION 0 +#define MGMT_REVISION 1 + +static int read_version (struct sock *sk) +{ +struct sk_buff *skb; +struct mgmt_hdr *hdr; +struct mgmt_ev_cmd_complete *ev; +struct mgmt_rp_read_version *rp; + +BT_DBG ("sock %p" , sk); + + skb =alloc_skb (sizeof (*hdr) +sizeof (*ev) +sizeof (*rp),GFP_ATOMIC ); +if (!skb) +return -ENOMEM ; + + hdr = (void *)skb_put (skb,sizeof (*hdr)); + hdr->opcode =cpu_to_le16 (MGMT_EV_CMD_COMPLETE ); + hdr->len =cpu_to_le16 (sizeof (*ev) +sizeof (*rp)); + + ev = (void *)skb_put (skb,sizeof (*ev)); +put_unaligned_le16 (MGMT_OP_READ_VERSION , &ev->opcode); + + rp = (void *)skb_put (skb,sizeof (*rp)); + rp->version =MGMT_VERSION ; +put_unaligned_le16 (MGMT_REVISION , &rp->revision); + +if (sock_queue_rcv_skb (sk, skb) <0 ) +kfree_skb (skb); + +return 0 ; +} +static int cmd_status (struct sock *sk,u16 cmd,u8 status) {struct sk_buff *skb;
@@ -87,6 +120,9 @@ int mgmt_control(struct sock *sk, struct msghdr *msg, size_t msglen) }switch (opcode) { +case MGMT_OP_READ_VERSION : + err =read_version (sk); +break ;default :BT_DBG ("Unknown op %u" , opcode); err =cmd_status (sk, opcode,0x01 );
Signature · 1 line
1.7.2.3
PATCH 3/5 Bluetooth: Add read_index_list management command
In reply to [email protected]From: Johan Hedberg <[email protected]>
This patch implements the read_index_list command through which userspace can get a list of current adapter indices.
--- include/net/bluetooth/mgmt.h | 6 ++++ net/bluetooth/mgmt.c | 53 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 0 deletions(-)
include/net/bluetooth/mgmt.h +6 -0
@@ -33,6 +33,12 @@ struct mgmt_rp_read_version {__le16 revision; } __packed; +#define MGMT_OP_READ_INDEX_LIST 0x0003 +struct mgmt_rp_read_index_list { +__le16 num_controllers; +__le16 index[0 ]; +} __packed; +#define MGMT_EV_CMD_COMPLETE 0x0001struct mgmt_ev_cmd_complete {__le16 opcode;
net/bluetooth/mgmt.c +53 -0
65 lines not on this page · raw message
Signature · 1 line
1.7.2.3
PATCH 4/5 Bluetooth: Add read_info management command
In reply to [email protected]From: Johan Hedberg <[email protected]>
This patch implements the read_info command which is used to fetch basic info about an adapter.
--- include/net/bluetooth/mgmt.h | 19 +++++++++ net/bluetooth/mgmt.c | 90 ++++++++++++++++++++++++++++++++++++++---- 2 files changed, 101 insertions(+), 8 deletions(-)
include/net/bluetooth/mgmt.h +19 -0
@@ -39,6 +39,25 @@ struct mgmt_rp_read_index_list {__le16 index[0 ]; } __packed; +#define MGMT_OP_READ_INFO 0x0004 +struct mgmt_cp_read_info { +__le16 index; +} __packed; +struct mgmt_rp_read_info { +__le16 index; +__u8 type; +__u8 powered; +__u8 discoverable; +__u8 pairable; +__u8 sec_mode; +bdaddr_t bdaddr; +__u8 dev_class[3 ]; +__u8 features[8 ]; +__u16 manufacturer; +__u8 hci_ver; +__u16 hci_rev; +} __packed; +#define MGMT_EV_CMD_COMPLETE 0x0001struct mgmt_ev_cmd_complete {__le16 opcode;
net/bluetooth/mgmt.c +82 -8
120 lines not on this page · raw message
Signature · 1 line
1.7.2.3
PATCH 5/5 Bluetooth: Add management events for controller addition & removal
In reply to [email protected]From: Johan Hedberg <[email protected]>
This patch adds Bluetooth Management interface events for controller addition and removal. The events correspond to the existing HCI_DEV_REG and HCI_DEV_UNREG stack internal events.
--- include/net/bluetooth/hci_core.h | 2 + include/net/bluetooth/mgmt.h | 10 +++++++++ net/bluetooth/hci_core.c | 2 + net/bluetooth/mgmt.c | 41 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 0 deletions(-)
include/net/bluetooth/hci_core.h +2 -0
@@ -662,6 +662,8 @@ void hci_send_to_sock(struct hci_dev *hdev, struct sk_buff *skb);/* Management interface */ int mgmt_control (struct sock *sk,struct msghdr *msg,size_t len); +int mgmt_index_added (u16 index); +int mgmt_index_removed (u16 index);/* HCI info for socket */ #define hci_pi (sk) ((struct hci_pinfo *) sk)
include/net/bluetooth/mgmt.h +10 -0
@@ -75,3 +75,13 @@ struct mgmt_ev_controller_error {__le16 index;__u8 error_code; } __packed; + +#define MGMT_EV_INDEX_ADDED 0x0004 +struct mgmt_ev_index_added { +__le16 index; +} __packed; + +#define MGMT_EV_INDEX_REMOVED 0x0005 +struct mgmt_ev_index_removed { +__le16 index; +} __packed;
net/bluetooth/hci_core.c +2 -0
@@ -960,6 +960,7 @@ int hci_register_dev(struct hci_dev *hdev) } } +mgmt_index_added (hdev->id);hci_notify (hdev,HCI_DEV_REG );return id;
@@ -989,6 +990,7 @@ int hci_unregister_dev(struct hci_dev *hdev)for (i =0 ; i <NUM_REASSEMBLY ; i++)kfree_skb (hdev->reassembly[i]); +mgmt_index_removed (hdev->id);hci_notify (hdev,HCI_DEV_UNREG );if (hdev->rfkill) {
net/bluetooth/mgmt.c +41 -0
44 lines not on this page · raw message
Signature · 1 line
1.7.2.3