4 * Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin@yahoo.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * $Id: hccontrol.c,v 1.5 2003/09/05 00:38:24 max Exp $
32 #define L2CAP_SOCKET_CHECKED
33 #include <bluetooth.h>
34 #include <sys/ioctl.h>
35 #include <sys/sysctl.h>
39 #include <netgraph/ng_message.h>
44 #include "hccontrol.h"
47 static int do_hci_command (char const *, int, char **);
48 static struct hci_command * find_hci_command (char const *, struct hci_command *);
49 static int find_hci_nodes (struct nodeinfo **);
50 static void print_hci_command (struct hci_command *);
51 static void usage (void);
56 int numeric_bdaddr = 0;
60 main(int argc, char *argv[])
65 /* Process command line arguments */
66 while ((n = getopt(argc, argv, "n:Nvh")) != -1) {
92 n = do_hci_command(node, argc, argv);
97 /* Create socket and bind it */
99 socket_open(char const *node)
101 struct sockaddr_hci addr;
102 struct ng_btsocket_hci_raw_filter filter;
105 struct nodeinfo *nodes;
107 num = find_hci_nodes(&nodes);
109 errx(7, "Could not find HCI nodes");
112 node = strdup(nodes[0].name);
114 fprintf(stdout, "Using HCI node: %s\n", node);
119 s = socket(PF_BLUETOOTH, SOCK_RAW, BLUETOOTH_PROTO_HCI);
121 err(1, "Could not create socket");
123 memset(&addr, 0, sizeof(addr));
124 addr.hci_len = sizeof(addr);
125 addr.hci_family = AF_BLUETOOTH;
126 strncpy(addr.hci_node, node, sizeof(addr.hci_node));
127 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0)
128 err(2, "Could not bind socket, node=%s", node);
130 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0)
131 err(3, "Could not connect socket, node=%s", node);
133 memset(&filter, 0, sizeof(filter));
134 bit_set(filter.event_mask, NG_HCI_EVENT_COMMAND_COMPL - 1);
135 bit_set(filter.event_mask, NG_HCI_EVENT_COMMAND_STATUS - 1);
136 bit_set(filter.event_mask, NG_HCI_EVENT_INQUIRY_COMPL - 1);
137 bit_set(filter.event_mask, NG_HCI_EVENT_INQUIRY_RESULT - 1);
138 bit_set(filter.event_mask, NG_HCI_EVENT_CON_COMPL - 1);
139 bit_set(filter.event_mask, NG_HCI_EVENT_DISCON_COMPL - 1);
140 bit_set(filter.event_mask, NG_HCI_EVENT_REMOTE_NAME_REQ_COMPL - 1);
141 bit_set(filter.event_mask, NG_HCI_EVENT_READ_REMOTE_FEATURES_COMPL - 1);
142 bit_set(filter.event_mask, NG_HCI_EVENT_READ_REMOTE_VER_INFO_COMPL - 1);
143 bit_set(filter.event_mask, NG_HCI_EVENT_RETURN_LINK_KEYS - 1);
144 bit_set(filter.event_mask, NG_HCI_EVENT_READ_CLOCK_OFFSET_COMPL - 1);
145 bit_set(filter.event_mask, NG_HCI_EVENT_CON_PKT_TYPE_CHANGED - 1);
146 bit_set(filter.event_mask, NG_HCI_EVENT_ROLE_CHANGE - 1);
147 bit_set(filter.event_mask, NG_HCI_EVENT_LE -1);
149 if (setsockopt(s, SOL_HCI_RAW, SO_HCI_RAW_FILTER,
150 (void * const) &filter, sizeof(filter)) < 0)
151 err(4, "Could not setsockopt()");
153 size = (sizeof(mib)/sizeof(mib[0]));
154 if (sysctlnametomib("net.bluetooth.hci.command_timeout",mib,&size) < 0)
155 err(5, "Could not sysctlnametomib()");
157 if (sysctl(mib, sizeof(mib)/sizeof(mib[0]),
158 (void *) &timeout, &size, NULL, 0) < 0)
159 err(6, "Could not sysctl()");
166 /* Execute commands */
168 do_hci_command(char const *node, int argc, char **argv)
171 struct hci_command *c = NULL;
175 if (strcasecmp(cmd, "help") == 0) {
180 fprintf(stdout, "Supported commands:\n");
181 print_hci_command(link_control_commands);
182 print_hci_command(link_policy_commands);
183 print_hci_command(host_controller_baseband_commands);
184 print_hci_command(info_commands);
185 print_hci_command(status_commands);
186 print_hci_command(le_commands);
187 print_hci_command(node_commands);
188 fprintf(stdout, "\nFor more information use " \
198 c = find_hci_command(cmd, link_control_commands);
202 c = find_hci_command(cmd, link_policy_commands);
206 c = find_hci_command(cmd, host_controller_baseband_commands);
210 c = find_hci_command(cmd, info_commands);
214 c = find_hci_command(cmd, status_commands);
218 c = find_hci_command(cmd, le_commands);
223 c = find_hci_command(cmd, node_commands);
225 fprintf(stdout, "Unknown command: \"%s\"\n", cmd);
230 s = socket_open(node);
231 e = (c->handler)(s, -- argc, ++ argv);
242 fprintf(stdout, "Could not execute command \"%s\". %s\n",
243 cmd, strerror(errno));
247 fprintf(stdout, "Usage: %s\n%s\n", c->command, c->description);
250 default: assert(0); break;
255 } /* do_hci_command */
257 /* Try to find command in specified category */
258 static struct hci_command *
259 find_hci_command(char const *command, struct hci_command *category)
261 struct hci_command *c = NULL;
263 for (c = category; c->command != NULL; c++) {
264 char *c_end = strchr(c->command, ' ');
267 int len = c_end - c->command;
269 if (strncasecmp(command, c->command, len) == 0)
271 } else if (strcasecmp(command, c->command) == 0)
276 } /* find_hci_command */
278 /* Find all HCI nodes */
280 find_hci_nodes(struct nodeinfo** nodes)
282 struct ng_btsocket_hci_raw_node_list_names r;
283 struct sockaddr_hci addr;
285 const char * node = "ubt0hci";
287 r.num_names = MAX_NODE_NUM;
288 r.names = (struct nodeinfo*)calloc(MAX_NODE_NUM, sizeof(struct nodeinfo));
290 err(8, "Could not allocate memory");
292 s = socket(PF_BLUETOOTH, SOCK_RAW, BLUETOOTH_PROTO_HCI);
294 err(9, "Could not create socket");
296 memset(&addr, 0, sizeof(addr));
297 addr.hci_len = sizeof(addr);
298 addr.hci_family = AF_BLUETOOTH;
299 strncpy(addr.hci_node, node, sizeof(addr.hci_node));
300 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0)
301 err(10, "Could not bind socket");
303 if (ioctl(s, SIOC_HCI_RAW_NODE_LIST_NAMES, &r, sizeof(r)) < 0)
304 err(11, "Could not get list of HCI nodes");
310 return (r.num_names);
311 } /* find_hci_nodes */
313 /* Print commands in specified category */
315 print_hci_command(struct hci_command *category)
317 struct hci_command *c = NULL;
319 for (c = category; c->command != NULL; c++)
320 fprintf(stdout, "\t%s\n", c->command);
321 } /* print_hci_command */
327 fprintf(stdout, "Usage: hccontrol [-hN] [-n HCI_node_name] cmd [p1] [..]\n");