xref: /netbsd-src/usr.sbin/btdevctl/btdevctl.c (revision 88fcb00c0357f2d7c1774f86a352637bfda96184)
1 /*	$NetBSD: btdevctl.c,v 1.9 2011/03/20 19:46:13 plunky 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.9 2011/03/20 19:46:13 plunky 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 int main(int, char *[]);
58 void usage(void);
59 char *uppercase(const char *);
60 int bthub_pioctl(unsigned long, prop_dictionary_t);
61 
62 int
63 main(int argc, char *argv[])
64 {
65 	prop_dictionary_t dev;
66 	prop_object_t obj;
67 	bdaddr_t laddr, raddr;
68 	const char *service, *mode;
69 	int ch, query, verbose, attach, detach, set, none;
70 
71 	bdaddr_copy(&laddr, BDADDR_ANY);
72 	bdaddr_copy(&raddr, BDADDR_ANY);
73 	service = NULL;
74 	mode = NULL;
75 	query = false;
76 	verbose = false;
77 	attach = false;
78 	detach = false;
79 	set = false;
80 	none = false;
81 
82 	while ((ch = getopt(argc, argv, "Aa:Dd:hm:qs:v")) != -1) {
83 		switch (ch) {
84 		case 'A': /* Attach device */
85 			attach = true;
86 			break;
87 
88 		case 'a': /* remote address */
89 			if (!bt_aton(optarg, &raddr)) {
90 				struct hostent  *he = NULL;
91 
92 				if ((he = bt_gethostbyname(optarg)) == NULL)
93 					errx(EXIT_FAILURE, "%s: %s",
94 						optarg, hstrerror(h_errno));
95 
96 				bdaddr_copy(&raddr, (bdaddr_t *)he->h_addr);
97 			}
98 			break;
99 
100 		case 'D': /* Detach device */
101 			detach = true;
102 			break;
103 
104 		case 'd': /* local device address */
105 			if (!bt_devaddr(optarg, &laddr))
106 				err(EXIT_FAILURE, "%s", optarg);
107 
108 			break;
109 
110 		case 'm': /* link mode */
111 			if (strcasecmp(optarg, "none") == 0)
112 				none = true;
113 			else if (strcasecmp(optarg, BTDEVauth) == 0)
114 				mode = BTDEVauth;
115 			else if (strcasecmp(optarg, BTDEVencrypt) == 0)
116 				mode = BTDEVencrypt;
117 			else if (strcasecmp(optarg, BTDEVsecure) == 0)
118 				mode = BTDEVsecure;
119 			else
120 				errx(EXIT_FAILURE, "%s: unknown mode", optarg);
121 
122 			break;
123 
124 		case 'q':
125 			query = true;
126 			break;
127 
128 		case 's': /* service */
129 			service = uppercase(optarg);
130 			break;
131 
132 		case 'v': /* verbose */
133 			verbose = true;
134 			break;
135 
136 		case 'h':
137 		default:
138 			usage();
139 		}
140 	}
141 
142 	argc -= optind;
143 	argv += optind;
144 
145 	if (argc > 0
146 	    || (attach == true && detach == true)
147 	    || bdaddr_any(&laddr)
148 	    || bdaddr_any(&raddr)
149 	    || service == NULL)
150 		usage();
151 
152 	if (attach == false && detach == false)
153 		verbose = true;
154 
155 	dev = db_get(&laddr, &raddr, service);
156 	if (dev == NULL || query == true) {
157 		if (verbose == true)
158 			printf("Performing SDP query for service '%s'..\n", service);
159 
160 		dev = cfg_query(&laddr, &raddr, service);
161 		set = true;
162 	}
163 
164 	if (mode != NULL) {
165 		obj = prop_string_create_cstring_nocopy(mode);
166 		if (obj == NULL || !prop_dictionary_set(dev, BTDEVmode, obj))
167 			errx(EXIT_FAILURE, "proplib failure (%s)", BTDEVmode);
168 
169 		prop_object_release(obj);
170 		set = true;
171 	}
172 
173 	if (none == true) {
174 		prop_dictionary_remove(dev, BTDEVmode);
175 		set = true;
176 	}
177 
178 	if (set == true && !db_set(dev, &laddr, &raddr, service))
179 		errx(EXIT_FAILURE, "service store failed");
180 
181 	/* add binary local-bdaddr */
182 	obj = prop_data_create_data(&laddr, sizeof(laddr));
183 	if (obj == NULL || !prop_dictionary_set(dev, BTDEVladdr, obj))
184 		errx(EXIT_FAILURE, "proplib failure (%s)", BTDEVladdr);
185 
186 	prop_object_release(obj);
187 
188 	/* add binary remote-bdaddr */
189 	obj = prop_data_create_data(&raddr, sizeof(raddr));
190 	if (obj == NULL || !prop_dictionary_set(dev, BTDEVraddr, obj))
191 		errx(EXIT_FAILURE, "proplib failure (%s)", BTDEVraddr);
192 
193 	prop_object_release(obj);
194 
195 	/* add service name */
196 	obj = prop_string_create_cstring(service);
197 	if (obj == NULL || !prop_dictionary_set(dev, BTDEVservice, obj))
198 		errx(EXIT_FAILURE, "proplib failure (%s)", BTDEVservice);
199 
200 	prop_object_release(obj);
201 
202 	if (verbose == true)
203 		cfg_print(dev);
204 
205 	if (attach == true)
206 		bthub_pioctl(BTDEV_ATTACH, dev);
207 
208 	if (detach == true)
209 		bthub_pioctl(BTDEV_DETACH, dev);
210 
211 	exit(EXIT_SUCCESS);
212 }
213 
214 void
215 usage(void)
216 {
217 
218 	fprintf(stderr,
219 		"usage: %s [-A | -D] [-qv] [-m mode] -a address -d device -s service\n"
220 		"Where:\n"
221 		"\t-A           attach device\n"
222 		"\t-a address   remote device address\n"
223 		"\t-D           detach device\n"
224 		"\t-d device    local device address\n"
225 		"\t-m mode      link mode\n"
226 		"\t-q           force SDP query\n"
227 		"\t-s service   remote service\n"
228 		"\t-v           verbose\n"
229 		"", getprogname());
230 
231 	exit(EXIT_FAILURE);
232 }
233 
234 char *
235 uppercase(const char *arg)
236 {
237 	char *str, *ptr;
238 
239 	str = strdup(arg);
240 	if (str == NULL)
241 		err(EXIT_FAILURE, "strdup");
242 
243 	for (ptr = str ; *ptr ; ptr++)
244 		*ptr = (char)toupper((int)*ptr);
245 
246 	return str;
247 }
248 
249 int
250 bthub_pioctl(unsigned long cmd, prop_dictionary_t dict)
251 {
252 	int fd;
253 
254 	fd = open(BTHUB_PATH, O_WRONLY, 0);
255 	if (fd < 0)
256 		err(EXIT_FAILURE, "%s", BTHUB_PATH);
257 
258 	if (prop_dictionary_send_ioctl(dict, fd, cmd))
259 		err(EXIT_FAILURE, "%s", BTHUB_PATH);
260 
261 	close(fd);
262 	return EXIT_SUCCESS;
263 }
264