xref: /freebsd/usr.sbin/bluetooth/hccontrol/hccontrol.c (revision aa1a8ff2d6dbc51ef058f46f3db5a8bb77967145)
1 /*-
2  * hccontrol.c
3  *
4  * SPDX-License-Identifier: BSD-2-Clause
5  *
6  * Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin@yahoo.com>
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
17  *
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
28  * SUCH DAMAGE.
29  *
30  * $Id: hccontrol.c,v 1.5 2003/09/05 00:38:24 max Exp $
31  */
32 
33 #define L2CAP_SOCKET_CHECKED
34 #include <bluetooth.h>
35 #include <sys/ioctl.h>
36 #include <sys/param.h>
37 #include <sys/sysctl.h>
38 #include <assert.h>
39 #include <err.h>
40 #include <errno.h>
41 #include <netgraph/ng_message.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46 #include "hccontrol.h"
47 
48 /* Prototypes */
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);
54 
55 /* Globals */
56 int	 verbose = 0;
57 int	 timeout;
58 int	 numeric_bdaddr = 0;
59 
60 /* Main */
61 int
62 main(int argc, char *argv[])
63 {
64 	char	*node = NULL;
65 	int	 n;
66 
67 	/* Process command line arguments */
68 	while ((n = getopt(argc, argv, "n:Nvh")) != -1) {
69 		switch (n) {
70 		case 'n':
71 			node = optarg;
72 			break;
73 
74 		case 'N':
75 			numeric_bdaddr = 1;
76 			break;
77 
78 		case 'v':
79 			verbose = 1;
80 			break;
81 
82 		case 'h':
83 		default:
84 			usage();
85 		}
86 	}
87 
88 	argc -= optind;
89 	argv += optind;
90 
91 	if (*argv == NULL)
92 		usage();
93 
94 	n = do_hci_command(node, argc, argv);
95 
96 	return (n);
97 } /* main */
98 
99 /* Create socket and bind it */
100 static int
101 socket_open(char const *node)
102 {
103 	struct sockaddr_hci			 addr;
104 	struct ng_btsocket_hci_raw_filter	 filter;
105 	int					 s, mib[4], num;
106 	size_t					 size;
107 	struct nodeinfo 			*nodes;
108 	char                                    *lnode = NULL;
109 
110 	num = find_hci_nodes(&nodes);
111 	if (num == 0)
112 		errx(7, "Could not find HCI nodes");
113 
114 	if (node == NULL) {
115 		node = lnode = strdup(nodes[0].name);
116 		if (num > 1)
117 			fprintf(stdout, "Using HCI node: %s\n", node);
118 	}
119 
120 	free(nodes);
121 
122 	s = socket(PF_BLUETOOTH, SOCK_RAW, BLUETOOTH_PROTO_HCI);
123 	if (s < 0)
124 		err(1, "Could not create socket");
125 
126 	memset(&addr, 0, sizeof(addr));
127 	addr.hci_len = sizeof(addr);
128 	addr.hci_family = AF_BLUETOOTH;
129 	strncpy(addr.hci_node, node, sizeof(addr.hci_node));
130 	if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0)
131 		err(2, "Could not bind socket, node=%s", node);
132 
133 	if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0)
134 		err(3, "Could not connect socket, node=%s", node);
135 
136 	free(lnode);
137 	memset(&filter, 0, sizeof(filter));
138 	bit_set(filter.event_mask, NG_HCI_EVENT_COMMAND_COMPL - 1);
139 	bit_set(filter.event_mask, NG_HCI_EVENT_COMMAND_STATUS - 1);
140 	bit_set(filter.event_mask, NG_HCI_EVENT_INQUIRY_COMPL - 1);
141 	bit_set(filter.event_mask, NG_HCI_EVENT_INQUIRY_RESULT - 1);
142 	bit_set(filter.event_mask, NG_HCI_EVENT_CON_COMPL - 1);
143 	bit_set(filter.event_mask, NG_HCI_EVENT_DISCON_COMPL - 1);
144 	bit_set(filter.event_mask, NG_HCI_EVENT_REMOTE_NAME_REQ_COMPL - 1);
145 	bit_set(filter.event_mask, NG_HCI_EVENT_READ_REMOTE_FEATURES_COMPL - 1);
146 	bit_set(filter.event_mask, NG_HCI_EVENT_READ_REMOTE_VER_INFO_COMPL - 1);
147 	bit_set(filter.event_mask, NG_HCI_EVENT_RETURN_LINK_KEYS - 1);
148 	bit_set(filter.event_mask, NG_HCI_EVENT_READ_CLOCK_OFFSET_COMPL - 1);
149 	bit_set(filter.event_mask, NG_HCI_EVENT_CON_PKT_TYPE_CHANGED - 1);
150 	bit_set(filter.event_mask, NG_HCI_EVENT_ROLE_CHANGE - 1);
151 	bit_set(filter.event_mask, NG_HCI_EVENT_LE -1);
152 
153 	if (setsockopt(s, SOL_HCI_RAW, SO_HCI_RAW_FILTER,
154 			(void * const) &filter, sizeof(filter)) < 0)
155 		err(4, "Could not setsockopt()");
156 
157 	size = nitems(mib);
158 	if (sysctlnametomib("net.bluetooth.hci.command_timeout",mib,&size) < 0)
159 		err(5, "Could not sysctlnametomib()");
160 
161 	if (sysctl(mib, nitems(mib),
162 			(void *) &timeout, &size, NULL, 0) < 0)
163 		err(6, "Could not sysctl()");
164 
165 	timeout ++;
166 
167 	return (s);
168 } /* socket_open */
169 
170 /* Execute commands */
171 static int
172 do_hci_command(char const *node, int argc, char **argv)
173 {
174 	char			*cmd = argv[0];
175 	struct hci_command	*c = NULL;
176 	int			 s, e, help;
177 
178 	help = 0;
179 	if (strcasecmp(cmd, "help") == 0) {
180 		argc --;
181 		argv ++;
182 
183 		if (argc <= 0) {
184 			fprintf(stdout, "Supported commands:\n");
185 			print_hci_command(link_control_commands);
186 			print_hci_command(link_policy_commands);
187 			print_hci_command(host_controller_baseband_commands);
188 			print_hci_command(info_commands);
189 			print_hci_command(status_commands);
190 			print_hci_command(le_commands);
191 			print_hci_command(node_commands);
192 			fprintf(stdout, "\nFor more information use " \
193 				"'help command'\n");
194 
195 			return (OK);
196 		}
197 
198 		help = 1;
199 		cmd = argv[0];
200 	}
201 
202 	c = find_hci_command(cmd, link_control_commands);
203 	if (c != NULL)
204 		goto execute;
205 
206 	c = find_hci_command(cmd, link_policy_commands);
207 	if (c != NULL)
208 		goto execute;
209 
210 	c = find_hci_command(cmd, host_controller_baseband_commands);
211 	if (c != NULL)
212 		goto execute;
213 
214 	c = find_hci_command(cmd, info_commands);
215 	if (c != NULL)
216 		goto execute;
217 
218 	c = find_hci_command(cmd, status_commands);
219 	if (c != NULL)
220 		goto execute;
221 
222 	c = find_hci_command(cmd, le_commands);
223 	if (c != NULL)
224 		goto execute;
225 
226 
227 	c = find_hci_command(cmd, node_commands);
228 	if (c == NULL) {
229 		fprintf(stdout, "Unknown command: \"%s\"\n", cmd);
230 		return (ERROR);
231 	}
232 execute:
233 	if (!help) {
234 		s = socket_open(node);
235 		e = (c->handler)(s, -- argc, ++ argv);
236 		close(s);
237 	} else
238 		e = USAGE;
239 
240 	switch (e) {
241 	case OK:
242 	case FAILED:
243 		break;
244 
245 	case ERROR:
246 		fprintf(stdout, "Could not execute command \"%s\". %s\n",
247 			cmd, strerror(errno));
248 		break;
249 
250 	case USAGE:
251 		fprintf(stdout, "Usage: %s\n%s\n", c->command, c->description);
252 		break;
253 
254 	default: assert(0); break;
255 	}
256 
257 
258 	return (e);
259 } /* do_hci_command */
260 
261 /* Try to find command in specified category */
262 static struct hci_command *
263 find_hci_command(char const *command, struct hci_command *category)
264 {
265 	struct hci_command	*c = NULL;
266 
267 	for (c = category; c->command != NULL; c++) {
268 		char 	*c_end = strchr(c->command, ' ');
269 
270 		if (c_end != NULL) {
271 			int	len = c_end - c->command;
272 
273 			if (strncasecmp(command, c->command, len) == 0)
274 				return (c);
275 		} else if (strcasecmp(command, c->command) == 0)
276 				return (c);
277 	}
278 
279 	return (NULL);
280 } /* find_hci_command */
281 
282 /* Find all HCI nodes */
283 static int
284 find_hci_nodes(struct nodeinfo** nodes)
285 {
286 	struct ng_btsocket_hci_raw_node_list_names	r;
287 	struct sockaddr_hci				addr;
288 	int						s;
289 	const char *					node = "ubt0hci";
290 
291 	r.num_names = MAX_NODE_NUM;
292 	r.names = (struct nodeinfo*)calloc(MAX_NODE_NUM, sizeof(struct nodeinfo));
293 	if (r.names == NULL)
294 		err(8, "Could not allocate memory");
295 
296 	s = socket(PF_BLUETOOTH, SOCK_RAW, BLUETOOTH_PROTO_HCI);
297 	if (s < 0)
298 		err(9, "Could not create socket");
299 
300 	memset(&addr, 0, sizeof(addr));
301 	addr.hci_len = sizeof(addr);
302 	addr.hci_family = AF_BLUETOOTH;
303 	strncpy(addr.hci_node, node, sizeof(addr.hci_node));
304 	if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0)
305 		err(10, "Could not bind socket");
306 
307 	if (ioctl(s, SIOC_HCI_RAW_NODE_LIST_NAMES, &r, sizeof(r)) < 0)
308 		err(11, "Could not get list of HCI nodes");
309 
310 	close(s);
311 
312 	*nodes = r.names;
313 
314 	return (r.num_names);
315 } /* find_hci_nodes */
316 
317 /* Print commands in specified category */
318 static void
319 print_hci_command(struct hci_command *category)
320 {
321 	struct hci_command	*c = NULL;
322 
323 	for (c = category; c->command != NULL; c++)
324 		fprintf(stdout, "\t%s\n", c->command);
325 } /* print_hci_command */
326 
327 /* Usage */
328 static void
329 usage(void)
330 {
331 	fprintf(stdout, "Usage: hccontrol [-hN] [-n HCI_node_name] cmd [p1] [..]\n");
332 	exit(255);
333 } /* usage */
334 
335