hccontrol.c revision 158834
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: head/usr.sbin/bluetooth/hccontrol/hccontrol.c 158834 2006-05-22 17:58:09Z markus $ 30 */ 31 32#include <bluetooth.h> 33#include <sys/ioctl.h> 34#include <sys/sysctl.h> 35#include <assert.h> 36#include <err.h> 37#include <errno.h> 38#include <netgraph/ng_message.h> 39#include <stdio.h> 40#include <stdlib.h> 41#include <string.h> 42#include <unistd.h> 43#include "hccontrol.h" 44 45/* Prototypes */ 46static int do_hci_command (char const *, int, char **); 47static struct hci_command * find_hci_command (char const *, struct hci_command *); 48static int find_hci_nodes (struct nodeinfo **); 49static void print_hci_command (struct hci_command *); 50static void usage (void); 51 52/* Globals */ 53int verbose = 0; 54int timeout; 55int numeric_bdaddr = 0; 56 57/* Main */ 58int 59main(int argc, char *argv[]) 60{ 61 char *node = NULL; 62 int n; 63 64 /* Process command line arguments */ 65 while ((n = getopt(argc, argv, "n:Nvh")) != -1) { 66 switch (n) { 67 case 'n': 68 node = optarg; 69 break; 70 71 case 'N': 72 numeric_bdaddr = 1; 73 break; 74 75 case 'v': 76 verbose = 1; 77 break; 78 79 case 'h': 80 default: 81 usage(); 82 } 83 } 84 85 argc -= optind; 86 argv += optind; 87 88 if (*argv == NULL) 89 usage(); 90 91 n = do_hci_command(node, argc, argv); 92 93 return (n); 94} /* main */ 95 96/* Create socket and bind it */ 97static int 98socket_open(char const *node) 99{ 100 struct sockaddr_hci addr; 101 struct ng_btsocket_hci_raw_filter filter; 102 int s, mib[4]; 103 size_t size; 104 struct nodeinfo *nodes; 105 106 if (find_hci_nodes(&nodes) == 0) 107 err(7, "Could not find HCI nodes"); 108 109 if (node == NULL) { 110 node = strdup(nodes[0].name); 111 fprintf(stdout, "Using HCI node: %s\n", node); 112 } 113 114 free(nodes); 115 116 s = socket(PF_BLUETOOTH, SOCK_RAW, BLUETOOTH_PROTO_HCI); 117 if (s < 0) 118 err(1, "Could not create socket"); 119 120 memset(&addr, 0, sizeof(addr)); 121 addr.hci_len = sizeof(addr); 122 addr.hci_family = AF_BLUETOOTH; 123 strncpy(addr.hci_node, node, sizeof(addr.hci_node)); 124 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) 125 err(2, "Could not bind socket, node=%s", node); 126 127 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) 128 err(3, "Could not connect socket, node=%s", node); 129 130 memset(&filter, 0, sizeof(filter)); 131 bit_set(filter.event_mask, NG_HCI_EVENT_COMMAND_COMPL - 1); 132 bit_set(filter.event_mask, NG_HCI_EVENT_COMMAND_STATUS - 1); 133 bit_set(filter.event_mask, NG_HCI_EVENT_INQUIRY_COMPL - 1); 134 bit_set(filter.event_mask, NG_HCI_EVENT_INQUIRY_RESULT - 1); 135 bit_set(filter.event_mask, NG_HCI_EVENT_CON_COMPL - 1); 136 bit_set(filter.event_mask, NG_HCI_EVENT_DISCON_COMPL - 1); 137 bit_set(filter.event_mask, NG_HCI_EVENT_REMOTE_NAME_REQ_COMPL - 1); 138 bit_set(filter.event_mask, NG_HCI_EVENT_READ_REMOTE_FEATURES_COMPL - 1); 139 bit_set(filter.event_mask, NG_HCI_EVENT_READ_REMOTE_VER_INFO_COMPL - 1); 140 bit_set(filter.event_mask, NG_HCI_EVENT_RETURN_LINK_KEYS - 1); 141 bit_set(filter.event_mask, NG_HCI_EVENT_READ_CLOCK_OFFSET_COMPL - 1); 142 bit_set(filter.event_mask, NG_HCI_EVENT_CON_PKT_TYPE_CHANGED - 1); 143 bit_set(filter.event_mask, NG_HCI_EVENT_ROLE_CHANGE - 1); 144 145 if (setsockopt(s, SOL_HCI_RAW, SO_HCI_RAW_FILTER, 146 (void * const) &filter, sizeof(filter)) < 0) 147 err(4, "Could not setsockopt()"); 148 149 size = (sizeof(mib)/sizeof(mib[0])); 150 if (sysctlnametomib("net.bluetooth.hci.command_timeout",mib,&size) < 0) 151 err(5, "Could not sysctlnametomib()"); 152 153 if (sysctl(mib, sizeof(mib)/sizeof(mib[0]), 154 (void *) &timeout, &size, NULL, 0) < 0) 155 err(6, "Could not sysctl()"); 156 157 timeout ++; 158 159 return (s); 160} /* socket_open */ 161 162/* Execute commands */ 163static int 164do_hci_command(char const *node, int argc, char **argv) 165{ 166 char *cmd = argv[0]; 167 struct hci_command *c = NULL; 168 int s, e, help; 169 170 help = 0; 171 if (strcasecmp(cmd, "help") == 0) { 172 argc --; 173 argv ++; 174 175 if (argc <= 0) { 176 fprintf(stdout, "Supported commands:\n"); 177 print_hci_command(link_control_commands); 178 print_hci_command(link_policy_commands); 179 print_hci_command(host_controller_baseband_commands); 180 print_hci_command(info_commands); 181 print_hci_command(status_commands); 182 print_hci_command(node_commands); 183 fprintf(stdout, "\nFor more information use " \ 184 "'help command'\n"); 185 186 return (OK); 187 } 188 189 help = 1; 190 cmd = argv[0]; 191 } 192 193 c = find_hci_command(cmd, link_control_commands); 194 if (c != NULL) 195 goto execute; 196 197 c = find_hci_command(cmd, link_policy_commands); 198 if (c != NULL) 199 goto execute; 200 201 c = find_hci_command(cmd, host_controller_baseband_commands); 202 if (c != NULL) 203 goto execute; 204 205 c = find_hci_command(cmd, info_commands); 206 if (c != NULL) 207 goto execute; 208 209 c = find_hci_command(cmd, status_commands); 210 if (c != NULL) 211 goto execute; 212 213 c = find_hci_command(cmd, node_commands); 214 if (c == NULL) { 215 fprintf(stdout, "Unknown command: \"%s\"\n", cmd); 216 return (ERROR); 217 } 218execute: 219 if (!help) { 220 s = socket_open(node); 221 e = (c->handler)(s, -- argc, ++ argv); 222 close(s); 223 } else 224 e = USAGE; 225 226 switch (e) { 227 case OK: 228 case FAILED: 229 break; 230 231 case ERROR: 232 fprintf(stdout, "Could not execute command \"%s\". %s\n", 233 cmd, strerror(errno)); 234 break; 235 236 case USAGE: 237 fprintf(stdout, "Usage: %s\n%s\n", c->command, c->description); 238 break; 239 240 default: assert(0); break; 241 } 242 243 244 return (e); 245} /* do_hci_command */ 246 247/* Try to find command in specified category */ 248static struct hci_command * 249find_hci_command(char const *command, struct hci_command *category) 250{ 251 struct hci_command *c = NULL; 252 253 for (c = category; c->command != NULL; c++) { 254 char *c_end = strchr(c->command, ' '); 255 256 if (c_end != NULL) { 257 int len = c_end - c->command; 258 259 if (strncasecmp(command, c->command, len) == 0) 260 return (c); 261 } else if (strcasecmp(command, c->command) == 0) 262 return (c); 263 } 264 265 return (NULL); 266} /* find_hci_command */ 267 268/* Find all HCI nodes */ 269static int 270find_hci_nodes(struct nodeinfo** nodes) 271{ 272 struct ng_btsocket_hci_raw_node_list_names r; 273 struct sockaddr_hci addr; 274 int s; 275 const char * node = "ubt0hci"; 276 277 r.num_names = MAX_NODE_NUM; 278 r.names = (struct nodeinfo*)calloc(MAX_NODE_NUM, sizeof(struct nodeinfo)); 279 if (r.names == NULL) 280 err(8, "Could not allocate memory"); 281 282 s = socket(PF_BLUETOOTH, SOCK_RAW, BLUETOOTH_PROTO_HCI); 283 if (s < 0) 284 err(9, "Could not create socket"); 285 286 memset(&addr, 0, sizeof(addr)); 287 addr.hci_len = sizeof(addr); 288 addr.hci_family = AF_BLUETOOTH; 289 strncpy(addr.hci_node, node, sizeof(addr.hci_node)); 290 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) 291 err(10, "Could not bind socket"); 292 293 if (ioctl(s, SIOC_HCI_RAW_NODE_LIST_NAMES, &r, sizeof(r)) < 0) 294 err(11, "Could not get list of HCI nodes"); 295 296 close(s); 297 298 *nodes = r.names; 299 300 return (r.num_names); 301} /* find_hci_nodes */ 302 303/* Print commands in specified category */ 304static void 305print_hci_command(struct hci_command *category) 306{ 307 struct hci_command *c = NULL; 308 309 for (c = category; c->command != NULL; c++) 310 fprintf(stdout, "\t%s\n", c->command); 311} /* print_hci_command */ 312 313/* Usage */ 314static void 315usage(void) 316{ 317 fprintf(stdout, "Usage: hccontrol [-hN] [-n HCI_node_name] cmd [p1] [..]\n"); 318 exit(255); 319} /* usage */ 320 321