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