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