1/*- 2 * hccontrol.c 3 * 4 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 5 * 6 * Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin@yahoo.com> 7 * All rights reserved. 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 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 * 30 * $Id: hccontrol.c,v 1.5 2003/09/05 00:38:24 max Exp $ 31 * $FreeBSD: stable/11/usr.sbin/bluetooth/hccontrol/hccontrol.c 330449 2018-03-05 07:26:05Z eadler $ 32 */ 33 34#define L2CAP_SOCKET_CHECKED 35#include <bluetooth.h> 36#include <sys/ioctl.h> 37#include <sys/sysctl.h> 38#include <assert.h> 39#include <err.h> 40#include <errno.h> 41#include <netgraph/ng_message.h> 42#include <stdio.h> 43#include <stdlib.h> 44#include <string.h> 45#include <unistd.h> 46#include "hccontrol.h" 47 48/* Prototypes */ 49static int do_hci_command (char const *, int, char **); 50static struct hci_command * find_hci_command (char const *, struct hci_command *); 51static int find_hci_nodes (struct nodeinfo **); 52static void print_hci_command (struct hci_command *); 53static void usage (void); 54 55/* Globals */ 56int verbose = 0; 57int timeout; 58int numeric_bdaddr = 0; 59 60/* Main */ 61int 62main(int argc, char *argv[]) 63{ 64 char *node = NULL; 65 int n; 66 67 /* Process command line arguments */ 68 while ((n = getopt(argc, argv, "n:Nvh")) != -1) { 69 switch (n) { 70 case 'n': 71 node = optarg; 72 break; 73 74 case 'N': 75 numeric_bdaddr = 1; 76 break; 77 78 case 'v': 79 verbose = 1; 80 break; 81 82 case 'h': 83 default: 84 usage(); 85 } 86 } 87 88 argc -= optind; 89 argv += optind; 90 91 if (*argv == NULL) 92 usage(); 93 94 n = do_hci_command(node, argc, argv); 95 96 return (n); 97} /* main */ 98 99/* Create socket and bind it */ 100static int 101socket_open(char const *node) 102{ 103 struct sockaddr_hci addr; 104 struct ng_btsocket_hci_raw_filter filter; 105 int s, mib[4], num; 106 size_t size; 107 struct nodeinfo *nodes; 108 109 num = find_hci_nodes(&nodes); 110 if (num == 0) 111 errx(7, "Could not find HCI nodes"); 112 113 if (node == NULL) { 114 node = strdup(nodes[0].name); 115 if (num > 1) 116 fprintf(stdout, "Using HCI node: %s\n", node); 117 } 118 119 free(nodes); 120 121 s = socket(PF_BLUETOOTH, SOCK_RAW, BLUETOOTH_PROTO_HCI); 122 if (s < 0) 123 err(1, "Could not create socket"); 124 125 memset(&addr, 0, sizeof(addr)); 126 addr.hci_len = sizeof(addr); 127 addr.hci_family = AF_BLUETOOTH; 128 strncpy(addr.hci_node, node, sizeof(addr.hci_node)); 129 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) 130 err(2, "Could not bind socket, node=%s", node); 131 132 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) 133 err(3, "Could not connect socket, node=%s", node); 134 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 */ 169static int 170do_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 } 230execute: 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 */ 260static struct hci_command * 261find_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 */ 281static int 282find_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 */ 316static void 317print_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 */ 326static void 327usage(void) 328{ 329 fprintf(stdout, "Usage: hccontrol [-hN] [-n HCI_node_name] cmd [p1] [..]\n"); 330 exit(255); 331} /* usage */ 332 333