xref: /netbsd-src/usr.sbin/btdevctl/btdevctl.c (revision fff57c5525bbe431aee7bdb3983954f0627a42cb)
1 /*	$NetBSD: btdevctl.c,v 1.6 2007/11/19 19:49:58 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 Itronix, Inc.\n"
36 	    "All rights reserved.\n");
37 __RCSID("$NetBSD: btdevctl.c,v 1.6 2007/11/19 19:49:58 plunky Exp $");
38 
39 #include <prop/proplib.h>
40 #include <sys/ioctl.h>
41 
42 #include <bluetooth.h>
43 #include <ctype.h>
44 #include <err.h>
45 #include <fcntl.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
49 
50 #include <dev/bluetooth/btdev.h>
51 
52 #include "btdevctl.h"
53 
54 #define BTHUB_PATH		"/dev/bthub"
55 
56 int main(int, char *[]);
57 void usage(void);
58 char *uppercase(const char *);
59 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 		if (dev == NULL)
161 			errx(EXIT_FAILURE, "%s/%s not found", bt_ntoa(&raddr, NULL), service);
162 
163 		set = true;
164 	}
165 
166 	if (mode != NULL) {
167 		obj = prop_string_create_cstring_nocopy(mode);
168 		if (obj == NULL || !prop_dictionary_set(dev, BTDEVmode, obj))
169 			errx(EXIT_FAILURE, "proplib failure (%s)", BTDEVmode);
170 
171 		prop_object_release(obj);
172 		set = true;
173 	}
174 
175 	if (none == true) {
176 		prop_dictionary_remove(dev, BTDEVmode);
177 		set = true;
178 	}
179 
180 	if (set == true && !db_set(dev, &laddr, &raddr, service))
181 		errx(EXIT_FAILURE, "service store failed");
182 
183 	/* add binary local-bdaddr */
184 	obj = prop_data_create_data(&laddr, sizeof(laddr));
185 	if (obj == NULL || !prop_dictionary_set(dev, BTDEVladdr, obj))
186 		errx(EXIT_FAILURE, "proplib failure (%s)", BTDEVladdr);
187 
188 	prop_object_release(obj);
189 
190 	/* add binary remote-bdaddr */
191 	obj = prop_data_create_data(&raddr, sizeof(raddr));
192 	if (obj == NULL || !prop_dictionary_set(dev, BTDEVraddr, obj))
193 		errx(EXIT_FAILURE, "proplib failure (%s)", BTDEVraddr);
194 
195 	prop_object_release(obj);
196 
197 	/* add service name */
198 	obj = prop_string_create_cstring(service);
199 	if (obj == NULL || !prop_dictionary_set(dev, BTDEVservice, obj))
200 		errx(EXIT_FAILURE, "proplib failure (%s)", BTDEVservice);
201 
202 	prop_object_release(obj);
203 
204 	if (verbose == true)
205 		cfg_print(dev);
206 
207 	if (attach == true)
208 		bthub_pioctl(BTDEV_ATTACH, dev);
209 
210 	if (detach == true)
211 		bthub_pioctl(BTDEV_DETACH, dev);
212 
213 	exit(EXIT_SUCCESS);
214 }
215 
216 void
217 usage(void)
218 {
219 
220 	fprintf(stderr,
221 		"usage: %s [-A | -D] [-qv] [-m mode] -a address -d device -s service\n"
222 		"Where:\n"
223 		"\t-A           attach device\n"
224 		"\t-a address   remote device address\n"
225 		"\t-D           detach device\n"
226 		"\t-d device    local device address\n"
227 		"\t-m mode      link mode\n"
228 		"\t-q           force SDP query\n"
229 		"\t-s service   remote service\n"
230 		"\t-v           verbose\n"
231 		"", getprogname());
232 
233 	exit(EXIT_FAILURE);
234 }
235 
236 char *
237 uppercase(const char *arg)
238 {
239 	char *str, *ptr;
240 
241 	str = strdup(arg);
242 	if (str == NULL)
243 		err(EXIT_FAILURE, "strdup");
244 
245 	for (ptr = str ; *ptr ; ptr++)
246 		*ptr = (char)toupper((int)*ptr);
247 
248 	return str;
249 }
250 
251 int
252 bthub_pioctl(unsigned long cmd, prop_dictionary_t dict)
253 {
254 	int fd;
255 
256 	fd = open(BTHUB_PATH, O_WRONLY, 0);
257 	if (fd < 0)
258 		err(EXIT_FAILURE, "%s", BTHUB_PATH);
259 
260 	if (prop_dictionary_send_ioctl(dict, fd, cmd))
261 		err(EXIT_FAILURE, "%s", BTHUB_PATH);
262 
263 	close(fd);
264 	return EXIT_SUCCESS;
265 }
266