1 /* 2 * hccontrol.c 3 * 4 * Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin@yahoo.com> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 * $Id: hccontrol.c,v 1.5 2003/09/05 00:38:24 max Exp $ 29 * $FreeBSD$ 30 */ 31 32 #define L2CAP_SOCKET_CHECKED 33 #include <bluetooth.h> 34 #include <sys/ioctl.h> 35 #include <sys/sysctl.h> 36 #include <assert.h> 37 #include <err.h> 38 #include <errno.h> 39 #include <netgraph/ng_message.h> 40 #include <stdio.h> 41 #include <stdlib.h> 42 #include <string.h> 43 #include <unistd.h> 44 #include "hccontrol.h" 45 46 /* Prototypes */ 47 static int do_hci_command (char const *, int, char **); 48 static struct hci_command * find_hci_command (char const *, struct hci_command *); 49 static int find_hci_nodes (struct nodeinfo **); 50 static void print_hci_command (struct hci_command *); 51 static void usage (void); 52 53 /* Globals */ 54 int verbose = 0; 55 int timeout; 56 int numeric_bdaddr = 0; 57 58 /* Main */ 59 int 60 main(int argc, char *argv[]) 61 { 62 char *node = NULL; 63 int n; 64 65 /* Process command line arguments */ 66 while ((n = getopt(argc, argv, "n:Nvh")) != -1) { 67 switch (n) { 68 case 'n': 69 node = optarg; 70 break; 71 72 case 'N': 73 numeric_bdaddr = 1; 74 break; 75 76 case 'v': 77 verbose = 1; 78 break; 79 80 case 'h': 81 default: 82 usage(); 83 } 84 } 85 86 argc -= optind; 87 argv += optind; 88 89 if (*argv == NULL) 90 usage(); 91 92 n = do_hci_command(node, argc, argv); 93 94 return (n); 95 } /* main */ 96 97 /* Create socket and bind it */ 98 static int 99 socket_open(char const *node) 100 { 101 struct sockaddr_hci addr; 102 struct ng_btsocket_hci_raw_filter filter; 103 int s, mib[4], num; 104 size_t size; 105 struct nodeinfo *nodes; 106 char *lnode = NULL; 107 108 num = find_hci_nodes(&nodes); 109 if (num == 0) 110 errx(7, "Could not find HCI nodes"); 111 112 if (node == NULL) { 113 node = lnode = strdup(nodes[0].name); 114 if (num > 1) 115 fprintf(stdout, "Using HCI node: %s\n", node); 116 } 117 118 free(nodes); 119 120 s = socket(PF_BLUETOOTH, SOCK_RAW, BLUETOOTH_PROTO_HCI); 121 if (s < 0) 122 err(1, "Could not create socket"); 123 124 memset(&addr, 0, sizeof(addr)); 125 addr.hci_len = sizeof(addr); 126 addr.hci_family = AF_BLUETOOTH; 127 strncpy(addr.hci_node, node, sizeof(addr.hci_node)); 128 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) 129 err(2, "Could not bind socket, node=%s", node); 130 131 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) 132 err(3, "Could not connect socket, node=%s", node); 133 134 free(lnode); 135 memset(&filter, 0, sizeof(filter)); 136 bit_set(filter.event_mask, NG_HCI_EVENT_COMMAND_COMPL - 1); 137 bit_set(filter.event_mask, NG_HCI_EVENT_COMMAND_STATUS - 1); 138 bit_set(filter.event_mask, NG_HCI_EVENT_INQUIRY_COMPL - 1); 139 bit_set(filter.event_mask, NG_HCI_EVENT_INQUIRY_RESULT - 1); 140 bit_set(filter.event_mask, NG_HCI_EVENT_CON_COMPL - 1); 141 bit_set(filter.event_mask, NG_HCI_EVENT_DISCON_COMPL - 1); 142 bit_set(filter.event_mask, NG_HCI_EVENT_REMOTE_NAME_REQ_COMPL - 1); 143 bit_set(filter.event_mask, NG_HCI_EVENT_READ_REMOTE_FEATURES_COMPL - 1); 144 bit_set(filter.event_mask, NG_HCI_EVENT_READ_REMOTE_VER_INFO_COMPL - 1); 145 bit_set(filter.event_mask, NG_HCI_EVENT_RETURN_LINK_KEYS - 1); 146 bit_set(filter.event_mask, NG_HCI_EVENT_READ_CLOCK_OFFSET_COMPL - 1); 147 bit_set(filter.event_mask, NG_HCI_EVENT_CON_PKT_TYPE_CHANGED - 1); 148 bit_set(filter.event_mask, NG_HCI_EVENT_ROLE_CHANGE - 1); 149 bit_set(filter.event_mask, NG_HCI_EVENT_LE -1); 150 151 if (setsockopt(s, SOL_HCI_RAW, SO_HCI_RAW_FILTER, 152 (void * const) &filter, sizeof(filter)) < 0) 153 err(4, "Could not setsockopt()"); 154 155 size = (sizeof(mib)/sizeof(mib[0])); 156 if (sysctlnametomib("net.bluetooth.hci.command_timeout",mib,&size) < 0) 157 err(5, "Could not sysctlnametomib()"); 158 159 if (sysctl(mib, sizeof(mib)/sizeof(mib[0]), 160 (void *) &timeout, &size, NULL, 0) < 0) 161 err(6, "Could not sysctl()"); 162 163 timeout ++; 164 165 return (s); 166 } /* socket_open */ 167 168 /* Execute commands */ 169 static int 170 do_hci_command(char const *node, int argc, char **argv) 171 { 172 char *cmd = argv[0]; 173 struct hci_command *c = NULL; 174 int s, e, help; 175 176 help = 0; 177 if (strcasecmp(cmd, "help") == 0) { 178 argc --; 179 argv ++; 180 181 if (argc <= 0) { 182 fprintf(stdout, "Supported commands:\n"); 183 print_hci_command(link_control_commands); 184 print_hci_command(link_policy_commands); 185 print_hci_command(host_controller_baseband_commands); 186 print_hci_command(info_commands); 187 print_hci_command(status_commands); 188 print_hci_command(le_commands); 189 print_hci_command(node_commands); 190 fprintf(stdout, "\nFor more information use " \ 191 "'help command'\n"); 192 193 return (OK); 194 } 195 196 help = 1; 197 cmd = argv[0]; 198 } 199 200 c = find_hci_command(cmd, link_control_commands); 201 if (c != NULL) 202 goto execute; 203 204 c = find_hci_command(cmd, link_policy_commands); 205 if (c != NULL) 206 goto execute; 207 208 c = find_hci_command(cmd, host_controller_baseband_commands); 209 if (c != NULL) 210 goto execute; 211 212 c = find_hci_command(cmd, info_commands); 213 if (c != NULL) 214 goto execute; 215 216 c = find_hci_command(cmd, status_commands); 217 if (c != NULL) 218 goto execute; 219 220 c = find_hci_command(cmd, le_commands); 221 if (c != NULL) 222 goto execute; 223 224 225 c = find_hci_command(cmd, node_commands); 226 if (c == NULL) { 227 fprintf(stdout, "Unknown command: \"%s\"\n", cmd); 228 return (ERROR); 229 } 230 execute: 231 if (!help) { 232 s = socket_open(node); 233 e = (c->handler)(s, -- argc, ++ argv); 234 close(s); 235 } else 236 e = USAGE; 237 238 switch (e) { 239 case OK: 240 case FAILED: 241 break; 242 243 case ERROR: 244 fprintf(stdout, "Could not execute command \"%s\". %s\n", 245 cmd, strerror(errno)); 246 break; 247 248 case USAGE: 249 fprintf(stdout, "Usage: %s\n%s\n", c->command, c->description); 250 break; 251 252 default: assert(0); break; 253 } 254 255 256 return (e); 257 } /* do_hci_command */ 258 259 /* Try to find command in specified category */ 260 static struct hci_command * 261 find_hci_command(char const *command, struct hci_command *category) 262 { 263 struct hci_command *c = NULL; 264 265 for (c = category; c->command != NULL; c++) { 266 char *c_end = strchr(c->command, ' '); 267 268 if (c_end != NULL) { 269 int len = c_end - c->command; 270 271 if (strncasecmp(command, c->command, len) == 0) 272 return (c); 273 } else if (strcasecmp(command, c->command) == 0) 274 return (c); 275 } 276 277 return (NULL); 278 } /* find_hci_command */ 279 280 /* Find all HCI nodes */ 281 static int 282 find_hci_nodes(struct nodeinfo** nodes) 283 { 284 struct ng_btsocket_hci_raw_node_list_names r; 285 struct sockaddr_hci addr; 286 int s; 287 const char * node = "ubt0hci"; 288 289 r.num_names = MAX_NODE_NUM; 290 r.names = (struct nodeinfo*)calloc(MAX_NODE_NUM, sizeof(struct nodeinfo)); 291 if (r.names == NULL) 292 err(8, "Could not allocate memory"); 293 294 s = socket(PF_BLUETOOTH, SOCK_RAW, BLUETOOTH_PROTO_HCI); 295 if (s < 0) 296 err(9, "Could not create socket"); 297 298 memset(&addr, 0, sizeof(addr)); 299 addr.hci_len = sizeof(addr); 300 addr.hci_family = AF_BLUETOOTH; 301 strncpy(addr.hci_node, node, sizeof(addr.hci_node)); 302 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) 303 err(10, "Could not bind socket"); 304 305 if (ioctl(s, SIOC_HCI_RAW_NODE_LIST_NAMES, &r, sizeof(r)) < 0) 306 err(11, "Could not get list of HCI nodes"); 307 308 close(s); 309 310 *nodes = r.names; 311 312 return (r.num_names); 313 } /* find_hci_nodes */ 314 315 /* Print commands in specified category */ 316 static void 317 print_hci_command(struct hci_command *category) 318 { 319 struct hci_command *c = NULL; 320 321 for (c = category; c->command != NULL; c++) 322 fprintf(stdout, "\t%s\n", c->command); 323 } /* print_hci_command */ 324 325 /* Usage */ 326 static void 327 usage(void) 328 { 329 fprintf(stdout, "Usage: hccontrol [-hN] [-n HCI_node_name] cmd [p1] [..]\n"); 330 exit(255); 331 } /* usage */ 332 333