diff options
author | markus <markus@FreeBSD.org> | 2006-05-22 17:58:09 +0000 |
---|---|---|
committer | markus <markus@FreeBSD.org> | 2006-05-22 17:58:09 +0000 |
commit | 583587debb36a5d8742e2c0878d7f05293ef8000 (patch) | |
tree | c78652a387f02ed912650bf02a36cec2dd3dbe97 /usr.sbin | |
parent | 83d28e2330e303ab86bb548bc7d4dc0fa8ae3c2b (diff) | |
download | FreeBSD-src-583587debb36a5d8742e2c0878d7f05293ef8000.zip FreeBSD-src-583587debb36a5d8742e2c0878d7f05293ef8000.tar.gz |
- Add HCI node autodetection. As a consequence of this, make the '-n'
parameter optional.
- Add Read_Node_List command which prints a list of available HCI nodes,
their Netgraph IDs and connected hooks
Reviewed by: emax
Approved by: emax
MFC after: 1 week
Diffstat (limited to 'usr.sbin')
-rw-r--r-- | usr.sbin/bluetooth/hccontrol/hccontrol.c | 60 | ||||
-rw-r--r-- | usr.sbin/bluetooth/hccontrol/hccontrol.h | 2 | ||||
-rw-r--r-- | usr.sbin/bluetooth/hccontrol/node.c | 34 |
3 files changed, 89 insertions, 7 deletions
diff --git a/usr.sbin/bluetooth/hccontrol/hccontrol.c b/usr.sbin/bluetooth/hccontrol/hccontrol.c index 0bf5583..c77200e 100644 --- a/usr.sbin/bluetooth/hccontrol/hccontrol.c +++ b/usr.sbin/bluetooth/hccontrol/hccontrol.c @@ -30,10 +30,12 @@ */ #include <bluetooth.h> +#include <sys/ioctl.h> #include <sys/sysctl.h> #include <assert.h> #include <err.h> #include <errno.h> +#include <netgraph/ng_message.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -43,6 +45,7 @@ /* Prototypes */ static int do_hci_command (char const *, int, char **); static struct hci_command * find_hci_command (char const *, struct hci_command *); +static int find_hci_nodes (struct nodeinfo **); static void print_hci_command (struct hci_command *); static void usage (void); @@ -94,13 +97,21 @@ main(int argc, char *argv[]) static int socket_open(char const *node) { - struct sockaddr_hci addr; - struct ng_btsocket_hci_raw_filter filter; - int s, mib[4]; - size_t size; + struct sockaddr_hci addr; + struct ng_btsocket_hci_raw_filter filter; + int s, mib[4]; + size_t size; + struct nodeinfo *nodes; + + if (find_hci_nodes(&nodes) == 0) + err(7, "Could not find HCI nodes"); + + if (node == NULL) { + node = strdup(nodes[0].name); + fprintf(stdout, "Using HCI node: %s\n", node); + } - if (node == NULL) - usage(); + free(nodes); s = socket(PF_BLUETOOTH, SOCK_RAW, BLUETOOTH_PROTO_HCI); if (s < 0) @@ -254,6 +265,41 @@ find_hci_command(char const *command, struct hci_command *category) return (NULL); } /* find_hci_command */ +/* Find all HCI nodes */ +static int +find_hci_nodes(struct nodeinfo** nodes) +{ + struct ng_btsocket_hci_raw_node_list_names r; + struct sockaddr_hci addr; + int s; + const char * node = "ubt0hci"; + + r.num_names = MAX_NODE_NUM; + r.names = (struct nodeinfo*)calloc(MAX_NODE_NUM, sizeof(struct nodeinfo)); + if (r.names == NULL) + err(8, "Could not allocate memory"); + + s = socket(PF_BLUETOOTH, SOCK_RAW, BLUETOOTH_PROTO_HCI); + if (s < 0) + err(9, "Could not create socket"); + + memset(&addr, 0, sizeof(addr)); + addr.hci_len = sizeof(addr); + addr.hci_family = AF_BLUETOOTH; + strncpy(addr.hci_node, node, sizeof(addr.hci_node)); + if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) + err(10, "Could not bind socket"); + + if (ioctl(s, SIOC_HCI_RAW_NODE_LIST_NAMES, &r, sizeof(r)) < 0) + err(11, "Could not get list of HCI nodes"); + + close(s); + + *nodes = r.names; + + return (r.num_names); +} /* find_hci_nodes */ + /* Print commands in specified category */ static void print_hci_command(struct hci_command *category) @@ -268,7 +314,7 @@ print_hci_command(struct hci_command *category) static void usage(void) { - fprintf(stdout, "Usage: hccontrol -n HCI_node_name [-h] cmd [p1] [..]]\n"); + fprintf(stdout, "Usage: hccontrol [-hN] [-n HCI_node_name] cmd [p1] [..]\n"); exit(255); } /* usage */ diff --git a/usr.sbin/bluetooth/hccontrol/hccontrol.h b/usr.sbin/bluetooth/hccontrol/hccontrol.h index 466414f..d471982 100644 --- a/usr.sbin/bluetooth/hccontrol/hccontrol.h +++ b/usr.sbin/bluetooth/hccontrol/hccontrol.h @@ -37,6 +37,8 @@ #define FAILED 2 /* error was reported */ #define USAGE 3 /* invalid parameters */ +#define MAX_NODE_NUM 16 /* max number of nodes */ + struct hci_command { char const *command; char const *description; diff --git a/usr.sbin/bluetooth/hccontrol/node.c b/usr.sbin/bluetooth/hccontrol/node.c index 863d516..ede2153 100644 --- a/usr.sbin/bluetooth/hccontrol/node.c +++ b/usr.sbin/bluetooth/hccontrol/node.c @@ -32,9 +32,11 @@ #include <sys/ioctl.h> #include <bluetooth.h> #include <errno.h> +#include <netgraph/ng_message.h> #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <unistd.h> #include "hccontrol.h" /* Send Read_Node_State command to the node */ @@ -441,6 +443,33 @@ hci_write_node_role_switch(int s, int argc, char **argv) return (OK); } /* hci_write_node_role_switch */ +/* Send Read_Node_List command to the node */ +int +hci_read_node_list(int s, int argc, char **argv) +{ + struct ng_btsocket_hci_raw_node_list_names r; + int i; + + r.num_names = MAX_NODE_NUM; + r.names = (struct nodeinfo*)calloc(MAX_NODE_NUM, sizeof(struct nodeinfo)); + if (r.names == NULL) + return (ERROR); + + if (ioctl(s, SIOC_HCI_RAW_NODE_LIST_NAMES, &r, sizeof(r)) < 0) { + free(r.names); + return (ERROR); + } + + fprintf(stdout, "Name ID Num hooks\n"); + for (i = 0; i < r.num_names; ++i) + fprintf(stdout, "%-15s %08x %9d\n", + r.names[i].name, r.names[i].id, r.names[i].hooks); + + free(r.names); + + return (OK); +} /* hci_read_node_list */ + struct hci_command node_commands[] = { { "read_node_state", @@ -568,6 +597,11 @@ struct hci_command node_commands[] = { &hci_write_node_role_switch }, { +"read_node_list", +"Get a list of HCI nodes, their Netgraph IDs and connected hooks.", +&hci_read_node_list +}, +{ NULL, }}; |