Fix markup. Fix backslashes to surive roff.
[netbsd-mini2440.git] / usr.sbin / btdevctl / btdevctl.c
blob68d7dff39da791f4a5d51a38bfd6c2f3b2eb94da
1 /* $NetBSD: btdevctl.c,v 1.7 2008/07/21 13:36:57 lukem Exp $ */
3 /*-
4 * Copyright (c) 2006 Itronix Inc.
5 * All rights reserved.
7 * Written by Iain Hibbert for Itronix Inc.
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.
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.
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.7 2008/07/21 13:36:57 lukem Exp $");
40 #include <prop/proplib.h>
41 #include <sys/ioctl.h>
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>
51 #include <dev/bluetooth/btdev.h>
53 #include "btdevctl.h"
55 #define BTHUB_PATH "/dev/bthub"
57 int main(int, char *[]);
58 void usage(void);
59 char *uppercase(const char *);
60 int bthub_pioctl(unsigned long, prop_dictionary_t);
62 int
63 main(int argc, char *argv[])
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;
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;
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;
88 case 'a': /* remote address */
89 if (!bt_aton(optarg, &raddr)) {
90 struct hostent *he = NULL;
92 if ((he = bt_gethostbyname(optarg)) == NULL)
93 errx(EXIT_FAILURE, "%s: %s",
94 optarg, hstrerror(h_errno));
96 bdaddr_copy(&raddr, (bdaddr_t *)he->h_addr);
98 break;
100 case 'D': /* Detach device */
101 detach = true;
102 break;
104 case 'd': /* local device address */
105 if (!bt_devaddr(optarg, &laddr))
106 err(EXIT_FAILURE, "%s", optarg);
108 break;
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);
122 break;
124 case 'q':
125 query = true;
126 break;
128 case 's': /* service */
129 service = uppercase(optarg);
130 break;
132 case 'v': /* verbose */
133 verbose = true;
134 break;
136 case 'h':
137 default:
138 usage();
142 argc -= optind;
143 argv += optind;
145 if (argc > 0
146 || (attach == true && detach == true)
147 || bdaddr_any(&laddr)
148 || bdaddr_any(&raddr)
149 || service == NULL)
150 usage();
152 if (attach == false && detach == false)
153 verbose = true;
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);
160 dev = cfg_query(&laddr, &raddr, service);
161 if (dev == NULL)
162 errx(EXIT_FAILURE, "%s/%s not found", bt_ntoa(&raddr, NULL), service);
164 set = true;
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);
172 prop_object_release(obj);
173 set = true;
176 if (none == true) {
177 prop_dictionary_remove(dev, BTDEVmode);
178 set = true;
181 if (set == true && !db_set(dev, &laddr, &raddr, service))
182 errx(EXIT_FAILURE, "service store failed");
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);
189 prop_object_release(obj);
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);
196 prop_object_release(obj);
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);
203 prop_object_release(obj);
205 if (verbose == true)
206 cfg_print(dev);
208 if (attach == true)
209 bthub_pioctl(BTDEV_ATTACH, dev);
211 if (detach == true)
212 bthub_pioctl(BTDEV_DETACH, dev);
214 exit(EXIT_SUCCESS);
217 void
218 usage(void)
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());
234 exit(EXIT_FAILURE);
237 char *
238 uppercase(const char *arg)
240 char *str, *ptr;
242 str = strdup(arg);
243 if (str == NULL)
244 err(EXIT_FAILURE, "strdup");
246 for (ptr = str ; *ptr ; ptr++)
247 *ptr = (char)toupper((int)*ptr);
249 return str;
253 bthub_pioctl(unsigned long cmd, prop_dictionary_t dict)
255 int fd;
257 fd = open(BTHUB_PATH, O_WRONLY, 0);
258 if (fd < 0)
259 err(EXIT_FAILURE, "%s", BTHUB_PATH);
261 if (prop_dictionary_send_ioctl(dict, fd, cmd))
262 err(EXIT_FAILURE, "%s", BTHUB_PATH);
264 close(fd);
265 return EXIT_SUCCESS;