1
0
mirror of https://git.FreeBSD.org/src.git synced 2024-12-17 10:26:15 +00:00

- 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
This commit is contained in:
Markus Brueffer 2006-05-22 17:58:09 +00:00
parent 6c68b224b0
commit fc5806eda9
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=158834
3 changed files with 89 additions and 7 deletions

View File

@ -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 (node == NULL)
usage();
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);
}
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 */

View File

@ -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;

View File

@ -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,
}};