xref: /netbsd-src/usr.sbin/btdevctl/btdevctl.c (revision bcd1eb151dd460c33b0aaeef288c719648c890e3)
1*bcd1eb15Sthorpej /*	$NetBSD: btdevctl.c,v 1.11 2020/06/07 00:12:00 thorpej Exp $	*/
2885b13c5Splunky 
3885b13c5Splunky /*-
4885b13c5Splunky  * Copyright (c) 2006 Itronix Inc.
5885b13c5Splunky  * All rights reserved.
6885b13c5Splunky  *
7885b13c5Splunky  * Written by Iain Hibbert for Itronix Inc.
8885b13c5Splunky  *
9885b13c5Splunky  * Redistribution and use in source and binary forms, with or without
10885b13c5Splunky  * modification, are permitted provided that the following conditions
11885b13c5Splunky  * are met:
12885b13c5Splunky  * 1. Redistributions of source code must retain the above copyright
13885b13c5Splunky  *    notice, this list of conditions and the following disclaimer.
14885b13c5Splunky  * 2. Redistributions in binary form must reproduce the above copyright
15885b13c5Splunky  *    notice, this list of conditions and the following disclaimer in the
16885b13c5Splunky  *    documentation and/or other materials provided with the distribution.
17885b13c5Splunky  * 3. The name of Itronix Inc. may not be used to endorse
18885b13c5Splunky  *    or promote products derived from this software without specific
19885b13c5Splunky  *    prior written permission.
20885b13c5Splunky  *
21885b13c5Splunky  * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
22885b13c5Splunky  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23885b13c5Splunky  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24885b13c5Splunky  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
25885b13c5Splunky  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26885b13c5Splunky  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27885b13c5Splunky  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28885b13c5Splunky  * ON ANY THEORY OF LIABILITY, WHETHER IN
29885b13c5Splunky  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30885b13c5Splunky  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31885b13c5Splunky  * POSSIBILITY OF SUCH DAMAGE.
32885b13c5Splunky  */
33885b13c5Splunky 
34885b13c5Splunky #include <sys/cdefs.h>
35170631f4Splunky __COPYRIGHT("@(#) Copyright (c) 2006 The NetBSD Foundation, Inc.\
36170631f4Splunky   @(#) Copyright (c) 2006 Itronix, Inc.\
37170631f4Splunky   All rights reserved.");
38*bcd1eb15Sthorpej __RCSID("$NetBSD: btdevctl.c,v 1.11 2020/06/07 00:12:00 thorpej Exp $");
39885b13c5Splunky 
40885b13c5Splunky #include <prop/proplib.h>
414f1cbddcSplunky #include <sys/ioctl.h>
42885b13c5Splunky 
434f1cbddcSplunky #include <bluetooth.h>
444f1cbddcSplunky #include <ctype.h>
45885b13c5Splunky #include <err.h>
46885b13c5Splunky #include <fcntl.h>
47885b13c5Splunky #include <stdlib.h>
48885b13c5Splunky #include <string.h>
49885b13c5Splunky #include <unistd.h>
50885b13c5Splunky 
514f1cbddcSplunky #include <dev/bluetooth/btdev.h>
524f1cbddcSplunky 
53885b13c5Splunky #include "btdevctl.h"
54885b13c5Splunky 
554f1cbddcSplunky #define BTHUB_PATH		"/dev/bthub"
56885b13c5Splunky 
578756b978Sjoerg __dead static void usage(void);
588756b978Sjoerg static char *uppercase(const char *);
598756b978Sjoerg static int bthub_pioctl(unsigned long, prop_dictionary_t);
60885b13c5Splunky 
61885b13c5Splunky int
main(int argc,char * argv[])62885b13c5Splunky main(int argc, char *argv[])
63885b13c5Splunky {
644f1cbddcSplunky 	prop_dictionary_t dev;
654f1cbddcSplunky 	bdaddr_t laddr, raddr;
66f5db72e7Splunky 	const char *service, *mode;
67f5db72e7Splunky 	int ch, query, verbose, attach, detach, set, none;
68885b13c5Splunky 
694f1cbddcSplunky 	bdaddr_copy(&laddr, BDADDR_ANY);
704f1cbddcSplunky 	bdaddr_copy(&raddr, BDADDR_ANY);
714f1cbddcSplunky 	service = NULL;
72f5db72e7Splunky 	mode = NULL;
735e809e89Spavel 	query = false;
745e809e89Spavel 	verbose = false;
755e809e89Spavel 	attach = false;
765e809e89Spavel 	detach = false;
775e809e89Spavel 	set = false;
785e809e89Spavel 	none = false;
794f1cbddcSplunky 
80f5db72e7Splunky 	while ((ch = getopt(argc, argv, "Aa:Dd:hm:qs:v")) != -1) {
814f1cbddcSplunky 		switch (ch) {
824f1cbddcSplunky 		case 'A': /* Attach device */
835e809e89Spavel 			attach = true;
844f1cbddcSplunky 			break;
854f1cbddcSplunky 
864f1cbddcSplunky 		case 'a': /* remote address */
874f1cbddcSplunky 			if (!bt_aton(optarg, &raddr)) {
884f1cbddcSplunky 				struct hostent  *he = NULL;
894f1cbddcSplunky 
904f1cbddcSplunky 				if ((he = bt_gethostbyname(optarg)) == NULL)
914f1cbddcSplunky 					errx(EXIT_FAILURE, "%s: %s",
924f1cbddcSplunky 						optarg, hstrerror(h_errno));
934f1cbddcSplunky 
944f1cbddcSplunky 				bdaddr_copy(&raddr, (bdaddr_t *)he->h_addr);
954f1cbddcSplunky 			}
964f1cbddcSplunky 			break;
974f1cbddcSplunky 
984f1cbddcSplunky 		case 'D': /* Detach device */
995e809e89Spavel 			detach = true;
1004f1cbddcSplunky 			break;
1014f1cbddcSplunky 
1024f1cbddcSplunky 		case 'd': /* local device address */
1034f1cbddcSplunky 			if (!bt_devaddr(optarg, &laddr))
1044f1cbddcSplunky 				err(EXIT_FAILURE, "%s", optarg);
1054f1cbddcSplunky 
1064f1cbddcSplunky 			break;
1074f1cbddcSplunky 
108f5db72e7Splunky 		case 'm': /* link mode */
109f5db72e7Splunky 			if (strcasecmp(optarg, "none") == 0)
1105e809e89Spavel 				none = true;
111f5db72e7Splunky 			else if (strcasecmp(optarg, BTDEVauth) == 0)
112f5db72e7Splunky 				mode = BTDEVauth;
113f5db72e7Splunky 			else if (strcasecmp(optarg, BTDEVencrypt) == 0)
114f5db72e7Splunky 				mode = BTDEVencrypt;
115f5db72e7Splunky 			else if (strcasecmp(optarg, BTDEVsecure) == 0)
116f5db72e7Splunky 				mode = BTDEVsecure;
117f5db72e7Splunky 			else
118b0d7f48fSplunky 				errx(EXIT_FAILURE, "%s: unknown mode", optarg);
119f5db72e7Splunky 
120f5db72e7Splunky 			break;
121f5db72e7Splunky 
1224f1cbddcSplunky 		case 'q':
1235e809e89Spavel 			query = true;
1244f1cbddcSplunky 			break;
1254f1cbddcSplunky 
1264f1cbddcSplunky 		case 's': /* service */
1274f1cbddcSplunky 			service = uppercase(optarg);
1284f1cbddcSplunky 			break;
1294f1cbddcSplunky 
1304f1cbddcSplunky 		case 'v': /* verbose */
1315e809e89Spavel 			verbose = true;
1324f1cbddcSplunky 			break;
1334f1cbddcSplunky 
1344f1cbddcSplunky 		case 'h':
1354f1cbddcSplunky 		default:
1364f1cbddcSplunky 			usage();
1374f1cbddcSplunky 		}
1384f1cbddcSplunky 	}
1394f1cbddcSplunky 
1404f1cbddcSplunky 	argc -= optind;
1414f1cbddcSplunky 	argv += optind;
1424f1cbddcSplunky 
1434f1cbddcSplunky 	if (argc > 0
1445e809e89Spavel 	    || (attach == true && detach == true)
1454f1cbddcSplunky 	    || bdaddr_any(&laddr)
1464f1cbddcSplunky 	    || bdaddr_any(&raddr)
1474f1cbddcSplunky 	    || service == NULL)
148885b13c5Splunky 		usage();
149885b13c5Splunky 
1505e809e89Spavel 	if (attach == false && detach == false)
1515e809e89Spavel 		verbose = true;
152885b13c5Splunky 
1534f1cbddcSplunky 	dev = db_get(&laddr, &raddr, service);
1545e809e89Spavel 	if (dev == NULL || query == true) {
1555e809e89Spavel 		if (verbose == true)
1564f1cbddcSplunky 			printf("Performing SDP query for service '%s'..\n", service);
1574f1cbddcSplunky 
1584f1cbddcSplunky 		dev = cfg_query(&laddr, &raddr, service);
1595e809e89Spavel 		set = true;
160885b13c5Splunky 	}
161885b13c5Splunky 
162f5db72e7Splunky 	if (mode != NULL) {
163*bcd1eb15Sthorpej 		if (!prop_dictionary_set_string_nocopy(dev, BTDEVmode, mode))
164f5db72e7Splunky 			errx(EXIT_FAILURE, "proplib failure (%s)", BTDEVmode);
1655e809e89Spavel 		set = true;
166f5db72e7Splunky 	}
167f5db72e7Splunky 
1685e809e89Spavel 	if (none == true) {
169f5db72e7Splunky 		prop_dictionary_remove(dev, BTDEVmode);
1705e809e89Spavel 		set = true;
171f5db72e7Splunky 	}
172f5db72e7Splunky 
1735e809e89Spavel 	if (set == true && !db_set(dev, &laddr, &raddr, service))
174f5db72e7Splunky 		errx(EXIT_FAILURE, "service store failed");
175f5db72e7Splunky 
1764f1cbddcSplunky 	/* add binary local-bdaddr */
177*bcd1eb15Sthorpej 	if (!prop_dictionary_set_data(dev, BTDEVladdr, &laddr, sizeof(laddr)))
1784f1cbddcSplunky 		errx(EXIT_FAILURE, "proplib failure (%s)", BTDEVladdr);
1794f1cbddcSplunky 
1804f1cbddcSplunky 	/* add binary remote-bdaddr */
181*bcd1eb15Sthorpej 	if (!prop_dictionary_set_data(dev, BTDEVraddr, &raddr, sizeof(raddr)))
1824f1cbddcSplunky 		errx(EXIT_FAILURE, "proplib failure (%s)", BTDEVraddr);
1834f1cbddcSplunky 
184ed4d8d47Splunky 	/* add service name */
185*bcd1eb15Sthorpej 	if (!prop_dictionary_set_string(dev, BTDEVservice, service))
186ed4d8d47Splunky 		errx(EXIT_FAILURE, "proplib failure (%s)", BTDEVservice);
187ed4d8d47Splunky 
1885e809e89Spavel 	if (verbose == true)
1894f1cbddcSplunky 		cfg_print(dev);
1904f1cbddcSplunky 
1915e809e89Spavel 	if (attach == true)
1924f1cbddcSplunky 		bthub_pioctl(BTDEV_ATTACH, dev);
1934f1cbddcSplunky 
1945e809e89Spavel 	if (detach == true)
1954f1cbddcSplunky 		bthub_pioctl(BTDEV_DETACH, dev);
1964f1cbddcSplunky 
1974f1cbddcSplunky 	exit(EXIT_SUCCESS);
198885b13c5Splunky }
199885b13c5Splunky 
2008756b978Sjoerg static void
usage(void)2014f1cbddcSplunky usage(void)
202885b13c5Splunky {
203885b13c5Splunky 
2044f1cbddcSplunky 	fprintf(stderr,
205f5db72e7Splunky 		"usage: %s [-A | -D] [-qv] [-m mode] -a address -d device -s service\n"
2064f1cbddcSplunky 		"Where:\n"
2074f1cbddcSplunky 		"\t-A           attach device\n"
2084f1cbddcSplunky 		"\t-a address   remote device address\n"
2094f1cbddcSplunky 		"\t-D           detach device\n"
2104f1cbddcSplunky 		"\t-d device    local device address\n"
211f5db72e7Splunky 		"\t-m mode      link mode\n"
2124f1cbddcSplunky 		"\t-q           force SDP query\n"
2134f1cbddcSplunky 		"\t-s service   remote service\n"
2144f1cbddcSplunky 		"\t-v           verbose\n"
2154f1cbddcSplunky 		"", getprogname());
216885b13c5Splunky 
2174f1cbddcSplunky 	exit(EXIT_FAILURE);
218885b13c5Splunky }
219885b13c5Splunky 
2208756b978Sjoerg static char *
uppercase(const char * arg)2214f1cbddcSplunky uppercase(const char *arg)
2224f1cbddcSplunky {
2234f1cbddcSplunky 	char *str, *ptr;
224885b13c5Splunky 
2254f1cbddcSplunky 	str = strdup(arg);
2264f1cbddcSplunky 	if (str == NULL)
2274f1cbddcSplunky 		err(EXIT_FAILURE, "strdup");
228885b13c5Splunky 
2294f1cbddcSplunky 	for (ptr = str ; *ptr ; ptr++)
2304f1cbddcSplunky 		*ptr = (char)toupper((int)*ptr);
231885b13c5Splunky 
2324f1cbddcSplunky 	return str;
233885b13c5Splunky }
234885b13c5Splunky 
2358756b978Sjoerg static int
bthub_pioctl(unsigned long cmd,prop_dictionary_t dict)2364f1cbddcSplunky bthub_pioctl(unsigned long cmd, prop_dictionary_t dict)
237885b13c5Splunky {
238885b13c5Splunky 	int fd;
239885b13c5Splunky 
2404f1cbddcSplunky 	fd = open(BTHUB_PATH, O_WRONLY, 0);
241885b13c5Splunky 	if (fd < 0)
2424f1cbddcSplunky 		err(EXIT_FAILURE, "%s", BTHUB_PATH);
243885b13c5Splunky 
2444f1cbddcSplunky 	if (prop_dictionary_send_ioctl(dict, fd, cmd))
2454f1cbddcSplunky 		err(EXIT_FAILURE, "%s", BTHUB_PATH);
2464f1cbddcSplunky 
247885b13c5Splunky 	close(fd);
2484f1cbddcSplunky 	return EXIT_SUCCESS;
249885b13c5Splunky }
250