4 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
6 * Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin@yahoo.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * $Id: hccontrol.c,v 1.5 2003/09/05 00:38:24 max Exp $
34 #define L2CAP_SOCKET_CHECKED
35 #include <bluetooth.h>
36 #include <sys/ioctl.h>
37 #include <sys/sysctl.h>
41 #include <netgraph/ng_message.h>
46 #include "hccontrol.h"
49 static int do_hci_command (char const *, int, char **);
50 static struct hci_command * find_hci_command (char const *, struct hci_command *);
51 static int find_hci_nodes (struct nodeinfo **);
52 static void print_hci_command (struct hci_command *);
53 static void usage (void);
58 int numeric_bdaddr = 0;
62 main(int argc, char *argv[])
67 /* Process command line arguments */
68 while ((n = getopt(argc, argv, "n:Nvh")) != -1) {
94 n = do_hci_command(node, argc, argv);
99 /* Create socket and bind it */
101 socket_open(char const *node)
103 struct sockaddr_hci addr;
104 struct ng_btsocket_hci_raw_filter filter;
107 struct nodeinfo *nodes;
109 num = find_hci_nodes(&nodes);
111 errx(7, "Could not find HCI nodes");
114 node = strdup(nodes[0].name);
116 fprintf(stdout, "Using HCI node: %s\n", node);
121 s = socket(PF_BLUETOOTH, SOCK_RAW, BLUETOOTH_PROTO_HCI);
123 err(1, "Could not create socket");
125 memset(&addr, 0, sizeof(addr));
126 addr.hci_len = sizeof(addr);
127 addr.hci_family = AF_BLUETOOTH;
128 strncpy(addr.hci_node, node, sizeof(addr.hci_node));
129 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0)
130 err(2, "Could not bind socket, node=%s", node);
132 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0)
133 err(3, "Could not connect socket, node=%s", node);
135 memset(&filter, 0, sizeof(filter));
136 bit_set(filter.event_mask, NG_HCI_EVENT_COMMAND_COMPL - 1);
137 bit_set(filter.event_mask, NG_HCI_EVENT_COMMAND_STATUS - 1);
138 bit_set(filter.event_mask, NG_HCI_EVENT_INQUIRY_COMPL - 1);
139 bit_set(filter.event_mask, NG_HCI_EVENT_INQUIRY_RESULT - 1);
140 bit_set(filter.event_mask, NG_HCI_EVENT_CON_COMPL - 1);
141 bit_set(filter.event_mask, NG_HCI_EVENT_DISCON_COMPL - 1);
142 bit_set(filter.event_mask, NG_HCI_EVENT_REMOTE_NAME_REQ_COMPL - 1);
143 bit_set(filter.event_mask, NG_HCI_EVENT_READ_REMOTE_FEATURES_COMPL - 1);
144 bit_set(filter.event_mask, NG_HCI_EVENT_READ_REMOTE_VER_INFO_COMPL - 1);
145 bit_set(filter.event_mask, NG_HCI_EVENT_RETURN_LINK_KEYS - 1);
146 bit_set(filter.event_mask, NG_HCI_EVENT_READ_CLOCK_OFFSET_COMPL - 1);
147 bit_set(filter.event_mask, NG_HCI_EVENT_CON_PKT_TYPE_CHANGED - 1);
148 bit_set(filter.event_mask, NG_HCI_EVENT_ROLE_CHANGE - 1);
149 bit_set(filter.event_mask, NG_HCI_EVENT_LE -1);
151 if (setsockopt(s, SOL_HCI_RAW, SO_HCI_RAW_FILTER,
152 (void * const) &filter, sizeof(filter)) < 0)
153 err(4, "Could not setsockopt()");
155 size = (sizeof(mib)/sizeof(mib[0]));
156 if (sysctlnametomib("net.bluetooth.hci.command_timeout",mib,&size) < 0)
157 err(5, "Could not sysctlnametomib()");
159 if (sysctl(mib, sizeof(mib)/sizeof(mib[0]),
160 (void *) &timeout, &size, NULL, 0) < 0)
161 err(6, "Could not sysctl()");
168 /* Execute commands */
170 do_hci_command(char const *node, int argc, char **argv)
173 struct hci_command *c = NULL;
177 if (strcasecmp(cmd, "help") == 0) {
182 fprintf(stdout, "Supported commands:\n");
183 print_hci_command(link_control_commands);
184 print_hci_command(link_policy_commands);
185 print_hci_command(host_controller_baseband_commands);
186 print_hci_command(info_commands);
187 print_hci_command(status_commands);
188 print_hci_command(le_commands);
189 print_hci_command(node_commands);
190 fprintf(stdout, "\nFor more information use " \
200 c = find_hci_command(cmd, link_control_commands);
204 c = find_hci_command(cmd, link_policy_commands);
208 c = find_hci_command(cmd, host_controller_baseband_commands);
212 c = find_hci_command(cmd, info_commands);
216 c = find_hci_command(cmd, status_commands);
220 c = find_hci_command(cmd, le_commands);
225 c = find_hci_command(cmd, node_commands);
227 fprintf(stdout, "Unknown command: \"%s\"\n", cmd);
232 s = socket_open(node);
233 e = (c->handler)(s, -- argc, ++ argv);
244 fprintf(stdout, "Could not execute command \"%s\". %s\n",
245 cmd, strerror(errno));
249 fprintf(stdout, "Usage: %s\n%s\n", c->command, c->description);
252 default: assert(0); break;
257 } /* do_hci_command */
259 /* Try to find command in specified category */
260 static struct hci_command *
261 find_hci_command(char const *command, struct hci_command *category)
263 struct hci_command *c = NULL;
265 for (c = category; c->command != NULL; c++) {
266 char *c_end = strchr(c->command, ' ');
269 int len = c_end - c->command;
271 if (strncasecmp(command, c->command, len) == 0)
273 } else if (strcasecmp(command, c->command) == 0)
278 } /* find_hci_command */
280 /* Find all HCI nodes */
282 find_hci_nodes(struct nodeinfo** nodes)
284 struct ng_btsocket_hci_raw_node_list_names r;
285 struct sockaddr_hci addr;
287 const char * node = "ubt0hci";
289 r.num_names = MAX_NODE_NUM;
290 r.names = (struct nodeinfo*)calloc(MAX_NODE_NUM, sizeof(struct nodeinfo));
292 err(8, "Could not allocate memory");
294 s = socket(PF_BLUETOOTH, SOCK_RAW, BLUETOOTH_PROTO_HCI);
296 err(9, "Could not create socket");
298 memset(&addr, 0, sizeof(addr));
299 addr.hci_len = sizeof(addr);
300 addr.hci_family = AF_BLUETOOTH;
301 strncpy(addr.hci_node, node, sizeof(addr.hci_node));
302 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0)
303 err(10, "Could not bind socket");
305 if (ioctl(s, SIOC_HCI_RAW_NODE_LIST_NAMES, &r, sizeof(r)) < 0)
306 err(11, "Could not get list of HCI nodes");
312 return (r.num_names);
313 } /* find_hci_nodes */
315 /* Print commands in specified category */
317 print_hci_command(struct hci_command *category)
319 struct hci_command *c = NULL;
321 for (c = category; c->command != NULL; c++)
322 fprintf(stdout, "\t%s\n", c->command);
323 } /* print_hci_command */
329 fprintf(stdout, "Usage: hccontrol [-hN] [-n HCI_node_name] cmd [p1] [..]\n");