1 /* $NetBSD: btdevctl.c,v 1.2 2006/09/10 15:45:56 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.2 2006/09/10 15:45:56 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 if (verbose == TRUE) 165 cfg_print(dev); 166 167 if (attach == TRUE) 168 bthub_pioctl(BTDEV_ATTACH, dev); 169 170 if (detach == TRUE) 171 bthub_pioctl(BTDEV_DETACH, dev); 172 173 exit(EXIT_SUCCESS); 174 } 175 176 void 177 usage(void) 178 { 179 180 fprintf(stderr, 181 "usage: %s [-A | -D] [-qv] -a address -d device -s service\n" 182 "Where:\n" 183 "\t-A attach device\n" 184 "\t-a address remote device address\n" 185 "\t-D detach device\n" 186 "\t-d device local device address\n" 187 "\t-q force SDP query\n" 188 "\t-s service remote service\n" 189 "\t-v verbose\n" 190 "", getprogname()); 191 192 exit(EXIT_FAILURE); 193 } 194 195 char * 196 uppercase(const char *arg) 197 { 198 char *str, *ptr; 199 200 str = strdup(arg); 201 if (str == NULL) 202 err(EXIT_FAILURE, "strdup"); 203 204 for (ptr = str ; *ptr ; ptr++) 205 *ptr = (char)toupper((int)*ptr); 206 207 return str; 208 } 209 210 int 211 bthub_pioctl(unsigned long cmd, prop_dictionary_t dict) 212 { 213 int fd; 214 215 fd = open(BTHUB_PATH, O_WRONLY, 0); 216 if (fd < 0) 217 err(EXIT_FAILURE, "%s", BTHUB_PATH); 218 219 if (prop_dictionary_send_ioctl(dict, fd, cmd)) 220 err(EXIT_FAILURE, "%s", BTHUB_PATH); 221 222 close(fd); 223 return EXIT_SUCCESS; 224 } 225