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