1 /* $NetBSD: btdevctl.c,v 1.11 2020/06/07 00:12:00 thorpej 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.11 2020/06/07 00:12:00 thorpej 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
main(int argc,char * argv[])62 main(int argc, char *argv[])
63 {
64 prop_dictionary_t dev;
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 set = true;
160 }
161
162 if (mode != NULL) {
163 if (!prop_dictionary_set_string_nocopy(dev, BTDEVmode, mode))
164 errx(EXIT_FAILURE, "proplib failure (%s)", BTDEVmode);
165 set = true;
166 }
167
168 if (none == true) {
169 prop_dictionary_remove(dev, BTDEVmode);
170 set = true;
171 }
172
173 if (set == true && !db_set(dev, &laddr, &raddr, service))
174 errx(EXIT_FAILURE, "service store failed");
175
176 /* add binary local-bdaddr */
177 if (!prop_dictionary_set_data(dev, BTDEVladdr, &laddr, sizeof(laddr)))
178 errx(EXIT_FAILURE, "proplib failure (%s)", BTDEVladdr);
179
180 /* add binary remote-bdaddr */
181 if (!prop_dictionary_set_data(dev, BTDEVraddr, &raddr, sizeof(raddr)))
182 errx(EXIT_FAILURE, "proplib failure (%s)", BTDEVraddr);
183
184 /* add service name */
185 if (!prop_dictionary_set_string(dev, BTDEVservice, service))
186 errx(EXIT_FAILURE, "proplib failure (%s)", BTDEVservice);
187
188 if (verbose == true)
189 cfg_print(dev);
190
191 if (attach == true)
192 bthub_pioctl(BTDEV_ATTACH, dev);
193
194 if (detach == true)
195 bthub_pioctl(BTDEV_DETACH, dev);
196
197 exit(EXIT_SUCCESS);
198 }
199
200 static void
usage(void)201 usage(void)
202 {
203
204 fprintf(stderr,
205 "usage: %s [-A | -D] [-qv] [-m mode] -a address -d device -s service\n"
206 "Where:\n"
207 "\t-A attach device\n"
208 "\t-a address remote device address\n"
209 "\t-D detach device\n"
210 "\t-d device local device address\n"
211 "\t-m mode link mode\n"
212 "\t-q force SDP query\n"
213 "\t-s service remote service\n"
214 "\t-v verbose\n"
215 "", getprogname());
216
217 exit(EXIT_FAILURE);
218 }
219
220 static char *
uppercase(const char * arg)221 uppercase(const char *arg)
222 {
223 char *str, *ptr;
224
225 str = strdup(arg);
226 if (str == NULL)
227 err(EXIT_FAILURE, "strdup");
228
229 for (ptr = str ; *ptr ; ptr++)
230 *ptr = (char)toupper((int)*ptr);
231
232 return str;
233 }
234
235 static int
bthub_pioctl(unsigned long cmd,prop_dictionary_t dict)236 bthub_pioctl(unsigned long cmd, prop_dictionary_t dict)
237 {
238 int fd;
239
240 fd = open(BTHUB_PATH, O_WRONLY, 0);
241 if (fd < 0)
242 err(EXIT_FAILURE, "%s", BTHUB_PATH);
243
244 if (prop_dictionary_send_ioctl(dict, fd, cmd))
245 err(EXIT_FAILURE, "%s", BTHUB_PATH);
246
247 close(fd);
248 return EXIT_SUCCESS;
249 }
250