xref: /netbsd-src/usr.sbin/btdevctl/btdevctl.c (revision deb6f0161a9109e7de9b519dc8dfb9478668dcdd)
1 /*	$NetBSD: btdevctl.c,v 1.10 2011/08/27 22:24:14 joerg Exp $	*/
2 
3 /*-
4  * Copyright (c) 2006 Itronix Inc.
5  * All rights reserved.
6  *
7  * Written by Iain Hibbert for Itronix Inc.
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  * 3. The name of Itronix Inc. may not be used to endorse
18  *    or promote products derived from this software without specific
19  *    prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
25  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28  * ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 __COPYRIGHT("@(#) Copyright (c) 2006 The NetBSD Foundation, Inc.\
36   @(#) Copyright (c) 2006 Itronix, Inc.\
37   All rights reserved.");
38 __RCSID("$NetBSD: btdevctl.c,v 1.10 2011/08/27 22:24:14 joerg Exp $");
39 
40 #include <prop/proplib.h>
41 #include <sys/ioctl.h>
42 
43 #include <bluetooth.h>
44 #include <ctype.h>
45 #include <err.h>
46 #include <fcntl.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
50 
51 #include <dev/bluetooth/btdev.h>
52 
53 #include "btdevctl.h"
54 
55 #define BTHUB_PATH		"/dev/bthub"
56 
57 __dead static void usage(void);
58 static char *uppercase(const char *);
59 static int bthub_pioctl(unsigned long, prop_dictionary_t);
60 
61 int
62 main(int argc, char *argv[])
63 {
64 	prop_dictionary_t dev;
65 	prop_object_t obj;
66 	bdaddr_t laddr, raddr;
67 	const char *service, *mode;
68 	int ch, query, verbose, attach, detach, set, none;
69 
70 	bdaddr_copy(&laddr, BDADDR_ANY);
71 	bdaddr_copy(&raddr, BDADDR_ANY);
72 	service = NULL;
73 	mode = NULL;
74 	query = false;
75 	verbose = false;
76 	attach = false;
77 	detach = false;
78 	set = false;
79 	none = false;
80 
81 	while ((ch = getopt(argc, argv, "Aa:Dd:hm:qs:v")) != -1) {
82 		switch (ch) {
83 		case 'A': /* Attach device */
84 			attach = true;
85 			break;
86 
87 		case 'a': /* remote address */
88 			if (!bt_aton(optarg, &raddr)) {
89 				struct hostent  *he = NULL;
90 
91 				if ((he = bt_gethostbyname(optarg)) == NULL)
92 					errx(EXIT_FAILURE, "%s: %s",
93 						optarg, hstrerror(h_errno));
94 
95 				bdaddr_copy(&raddr, (bdaddr_t *)he->h_addr);
96 			}
97 			break;
98 
99 		case 'D': /* Detach device */
100 			detach = true;
101 			break;
102 
103 		case 'd': /* local device address */
104 			if (!bt_devaddr(optarg, &laddr))
105 				err(EXIT_FAILURE, "%s", optarg);
106 
107 			break;
108 
109 		case 'm': /* link mode */
110 			if (strcasecmp(optarg, "none") == 0)
111 				none = true;
112 			else if (strcasecmp(optarg, BTDEVauth) == 0)
113 				mode = BTDEVauth;
114 			else if (strcasecmp(optarg, BTDEVencrypt) == 0)
115 				mode = BTDEVencrypt;
116 			else if (strcasecmp(optarg, BTDEVsecure) == 0)
117 				mode = BTDEVsecure;
118 			else
119 				errx(EXIT_FAILURE, "%s: unknown mode", optarg);
120 
121 			break;
122 
123 		case 'q':
124 			query = true;
125 			break;
126 
127 		case 's': /* service */
128 			service = uppercase(optarg);
129 			break;
130 
131 		case 'v': /* verbose */
132 			verbose = true;
133 			break;
134 
135 		case 'h':
136 		default:
137 			usage();
138 		}
139 	}
140 
141 	argc -= optind;
142 	argv += optind;
143 
144 	if (argc > 0
145 	    || (attach == true && detach == true)
146 	    || bdaddr_any(&laddr)
147 	    || bdaddr_any(&raddr)
148 	    || service == NULL)
149 		usage();
150 
151 	if (attach == false && detach == false)
152 		verbose = true;
153 
154 	dev = db_get(&laddr, &raddr, service);
155 	if (dev == NULL || query == true) {
156 		if (verbose == true)
157 			printf("Performing SDP query for service '%s'..\n", service);
158 
159 		dev = cfg_query(&laddr, &raddr, service);
160 		set = true;
161 	}
162 
163 	if (mode != NULL) {
164 		obj = prop_string_create_cstring_nocopy(mode);
165 		if (obj == NULL || !prop_dictionary_set(dev, BTDEVmode, obj))
166 			errx(EXIT_FAILURE, "proplib failure (%s)", BTDEVmode);
167 
168 		prop_object_release(obj);
169 		set = true;
170 	}
171 
172 	if (none == true) {
173 		prop_dictionary_remove(dev, BTDEVmode);
174 		set = true;
175 	}
176 
177 	if (set == true && !db_set(dev, &laddr, &raddr, service))
178 		errx(EXIT_FAILURE, "service store failed");
179 
180 	/* add binary local-bdaddr */
181 	obj = prop_data_create_data(&laddr, sizeof(laddr));
182 	if (obj == NULL || !prop_dictionary_set(dev, BTDEVladdr, obj))
183 		errx(EXIT_FAILURE, "proplib failure (%s)", BTDEVladdr);
184 
185 	prop_object_release(obj);
186 
187 	/* add binary remote-bdaddr */
188 	obj = prop_data_create_data(&raddr, sizeof(raddr));
189 	if (obj == NULL || !prop_dictionary_set(dev, BTDEVraddr, obj))
190 		errx(EXIT_FAILURE, "proplib failure (%s)", BTDEVraddr);
191 
192 	prop_object_release(obj);
193 
194 	/* add service name */
195 	obj = prop_string_create_cstring(service);
196 	if (obj == NULL || !prop_dictionary_set(dev, BTDEVservice, obj))
197 		errx(EXIT_FAILURE, "proplib failure (%s)", BTDEVservice);
198 
199 	prop_object_release(obj);
200 
201 	if (verbose == true)
202 		cfg_print(dev);
203 
204 	if (attach == true)
205 		bthub_pioctl(BTDEV_ATTACH, dev);
206 
207 	if (detach == true)
208 		bthub_pioctl(BTDEV_DETACH, dev);
209 
210 	exit(EXIT_SUCCESS);
211 }
212 
213 static void
214 usage(void)
215 {
216 
217 	fprintf(stderr,
218 		"usage: %s [-A | -D] [-qv] [-m mode] -a address -d device -s service\n"
219 		"Where:\n"
220 		"\t-A           attach device\n"
221 		"\t-a address   remote device address\n"
222 		"\t-D           detach device\n"
223 		"\t-d device    local device address\n"
224 		"\t-m mode      link mode\n"
225 		"\t-q           force SDP query\n"
226 		"\t-s service   remote service\n"
227 		"\t-v           verbose\n"
228 		"", getprogname());
229 
230 	exit(EXIT_FAILURE);
231 }
232 
233 static char *
234 uppercase(const char *arg)
235 {
236 	char *str, *ptr;
237 
238 	str = strdup(arg);
239 	if (str == NULL)
240 		err(EXIT_FAILURE, "strdup");
241 
242 	for (ptr = str ; *ptr ; ptr++)
243 		*ptr = (char)toupper((int)*ptr);
244 
245 	return str;
246 }
247 
248 static int
249 bthub_pioctl(unsigned long cmd, prop_dictionary_t dict)
250 {
251 	int fd;
252 
253 	fd = open(BTHUB_PATH, O_WRONLY, 0);
254 	if (fd < 0)
255 		err(EXIT_FAILURE, "%s", BTHUB_PATH);
256 
257 	if (prop_dictionary_send_ioctl(dict, fd, cmd))
258 		err(EXIT_FAILURE, "%s", BTHUB_PATH);
259 
260 	close(fd);
261 	return EXIT_SUCCESS;
262 }
263