btdevctl.c revision 1.8 1 /* $NetBSD: btdevctl.c,v 1.8 2009/05/12 18:39:20 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.8 2009/05/12 18:39:20 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 if (dev == NULL)
162 errx(EXIT_FAILURE, "%s/%s not found", bt_ntoa(&raddr, NULL), service);
163
164 set = true;
165 }
166
167 if (mode != NULL) {
168 obj = prop_string_create_cstring_nocopy(mode);
169 if (obj == NULL || !prop_dictionary_set(dev, BTDEVmode, obj))
170 errx(EXIT_FAILURE, "proplib failure (%s)", BTDEVmode);
171
172 prop_object_release(obj);
173 set = true;
174 }
175
176 if (none == true) {
177 prop_dictionary_remove(dev, BTDEVmode);
178 set = true;
179 }
180
181 if (set == true && !db_set(dev, &laddr, &raddr, service))
182 errx(EXIT_FAILURE, "service store failed");
183
184 /* add binary local-bdaddr */
185 obj = prop_data_create_data(&laddr, sizeof(laddr));
186 if (obj == NULL || !prop_dictionary_set(dev, BTDEVladdr, obj))
187 errx(EXIT_FAILURE, "proplib failure (%s)", BTDEVladdr);
188
189 prop_object_release(obj);
190
191 /* add binary remote-bdaddr */
192 obj = prop_data_create_data(&raddr, sizeof(raddr));
193 if (obj == NULL || !prop_dictionary_set(dev, BTDEVraddr, obj))
194 errx(EXIT_FAILURE, "proplib failure (%s)", BTDEVraddr);
195
196 prop_object_release(obj);
197
198 /* add service name */
199 obj = prop_string_create_cstring(service);
200 if (obj == NULL || !prop_dictionary_set(dev, BTDEVservice, obj))
201 errx(EXIT_FAILURE, "proplib failure (%s)", BTDEVservice);
202
203 prop_object_release(obj);
204
205 if (verbose == true)
206 cfg_print(dev);
207
208 if (attach == true)
209 bthub_pioctl(BTDEV_ATTACH, dev);
210
211 if (detach == true)
212 bthub_pioctl(BTDEV_DETACH, dev);
213
214 exit(EXIT_SUCCESS);
215 }
216
217 void
218 usage(void)
219 {
220
221 fprintf(stderr,
222 "usage: %s [-A | -D] [-qv] [-m mode] -a address -d device -s service\n"
223 "Where:\n"
224 "\t-A attach device\n"
225 "\t-a address remote device address\n"
226 "\t-D detach device\n"
227 "\t-d device local device address\n"
228 "\t-m mode link mode\n"
229 "\t-q force SDP query\n"
230 "\t-s service remote service\n"
231 "\t-v verbose\n"
232 "", getprogname());
233
234 exit(EXIT_FAILURE);
235 }
236
237 char *
238 uppercase(const char *arg)
239 {
240 char *str, *ptr;
241
242 str = strdup(arg);
243 if (str == NULL)
244 err(EXIT_FAILURE, "strdup");
245
246 for (ptr = str ; *ptr ; ptr++)
247 *ptr = (char)toupper((int)*ptr);
248
249 return str;
250 }
251
252 int
253 bthub_pioctl(unsigned long cmd, prop_dictionary_t dict)
254 {
255 int fd;
256
257 fd = open(BTHUB_PATH, O_WRONLY, 0);
258 if (fd < 0)
259 err(EXIT_FAILURE, "%s", BTHUB_PATH);
260
261 if (prop_dictionary_send_ioctl(dict, fd, cmd))
262 err(EXIT_FAILURE, "%s", BTHUB_PATH);
263
264 close(fd);
265 return EXIT_SUCCESS;
266 }
267