1 1.28 plunky /* $NetBSD: btconfig.c,v 1.28 2017/12/21 09:31:22 plunky Exp $ */ 2 1.1 gdamore 3 1.1 gdamore /*- 4 1.1 gdamore * Copyright (c) 2006 Itronix Inc. 5 1.1 gdamore * All rights reserved. 6 1.1 gdamore * 7 1.1 gdamore * Written by Iain Hibbert for Itronix Inc. 8 1.1 gdamore * 9 1.1 gdamore * Redistribution and use in source and binary forms, with or without 10 1.1 gdamore * modification, are permitted provided that the following conditions 11 1.1 gdamore * are met: 12 1.1 gdamore * 1. Redistributions of source code must retain the above copyright 13 1.1 gdamore * notice, this list of conditions and the following disclaimer. 14 1.1 gdamore * 2. Redistributions in binary form must reproduce the above copyright 15 1.1 gdamore * notice, this list of conditions and the following disclaimer in the 16 1.1 gdamore * documentation and/or other materials provided with the distribution. 17 1.1 gdamore * 3. The name of Itronix Inc. may not be used to endorse 18 1.1 gdamore * or promote products derived from this software without specific 19 1.1 gdamore * prior written permission. 20 1.1 gdamore * 21 1.1 gdamore * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND 22 1.1 gdamore * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 23 1.1 gdamore * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24 1.1 gdamore * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY 25 1.1 gdamore * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 26 1.1 gdamore * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 27 1.1 gdamore * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 28 1.1 gdamore * ON ANY THEORY OF LIABILITY, WHETHER IN 29 1.1 gdamore * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 1.1 gdamore * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 1.1 gdamore * POSSIBILITY OF SUCH DAMAGE. 32 1.1 gdamore */ 33 1.1 gdamore 34 1.1 gdamore #include <sys/cdefs.h> 35 1.13 lukem __COPYRIGHT("@(#) Copyright (c) 2006 Itronix, Inc. All rights reserved."); 36 1.28 plunky __RCSID("$NetBSD: btconfig.c,v 1.28 2017/12/21 09:31:22 plunky Exp $"); 37 1.1 gdamore 38 1.7 plunky #include <sys/ioctl.h> 39 1.1 gdamore #include <sys/param.h> 40 1.1 gdamore #include <sys/socket.h> 41 1.1 gdamore 42 1.7 plunky #include <bluetooth.h> 43 1.7 plunky #include <err.h> 44 1.7 plunky #include <errno.h> 45 1.1 gdamore #include <stdio.h> 46 1.7 plunky #include <stdlib.h> 47 1.1 gdamore #include <string.h> 48 1.24 plunky #include <time.h> 49 1.7 plunky #include <unistd.h> 50 1.1 gdamore #include <util.h> 51 1.1 gdamore 52 1.25 joerg __dead static void badarg(const char *); 53 1.25 joerg __dead static void badparam(const char *); 54 1.25 joerg __dead static void badval(const char *, const char *); 55 1.25 joerg __dead static void usage(void); 56 1.25 joerg static int set_unit(unsigned long); 57 1.25 joerg static void config_unit(void); 58 1.25 joerg static void print_val(const char *, const char **, int); 59 1.25 joerg static void print_info(int); 60 1.25 joerg static void print_stats(void); 61 1.25 joerg static void print_class(const char *, uint8_t *); 62 1.25 joerg static void print_class0(void); 63 1.25 joerg static void print_voice(int); 64 1.25 joerg static void tag(const char *); 65 1.25 joerg static void print_features0(uint8_t *); 66 1.25 joerg static void print_features1(uint8_t *); 67 1.27 plunky static void print_features2(uint8_t *); 68 1.25 joerg static void print_result(int, struct bt_devinquiry *); 69 1.25 joerg static void do_inquiry(void); 70 1.25 joerg 71 1.25 joerg static void hci_req(uint16_t, uint8_t , void *, size_t, void *, size_t); 72 1.25 joerg static void save_value(uint16_t, void *, size_t); 73 1.25 joerg static void load_value(uint16_t, void *, size_t); 74 1.1 gdamore 75 1.1 gdamore #define MAX_STR_SIZE 0xff 76 1.1 gdamore 77 1.1 gdamore /* print width */ 78 1.25 joerg static int width = 0; 79 1.1 gdamore #define MAX_WIDTH 70 80 1.1 gdamore 81 1.1 gdamore /* global variables */ 82 1.25 joerg static int hci; 83 1.25 joerg static struct btreq btr; 84 1.1 gdamore 85 1.1 gdamore /* command line flags */ 86 1.25 joerg static int verbose = 0; /* more info */ 87 1.25 joerg static int lflag = 0; /* list devices */ 88 1.25 joerg static int sflag = 0; /* get/zero stats */ 89 1.1 gdamore 90 1.1 gdamore /* device up/down (flag) */ 91 1.25 joerg static int opt_enable = 0; 92 1.25 joerg static int opt_reset = 0; 93 1.1 gdamore #define FLAGS_FMT "\20" \ 94 1.1 gdamore "\001UP" \ 95 1.1 gdamore "\002RUNNING" \ 96 1.1 gdamore "\003XMIT_CMD" \ 97 1.1 gdamore "\004XMIT_ACL" \ 98 1.1 gdamore "\005XMIT_SCO" \ 99 1.1 gdamore "\006INIT_BDADDR" \ 100 1.1 gdamore "\007INIT_BUFFER_SIZE" \ 101 1.8 plunky "\010INIT_FEATURES" \ 102 1.8 plunky "\011POWER_UP_NOOP" \ 103 1.10 plunky "\012INIT_COMMANDS" \ 104 1.14 plunky "\013MASTER" \ 105 1.8 plunky "" 106 1.1 gdamore 107 1.1 gdamore /* authorisation (flag) */ 108 1.25 joerg static int opt_auth = 0; 109 1.1 gdamore 110 1.1 gdamore /* encryption (flag) */ 111 1.25 joerg static int opt_encrypt = 0; 112 1.1 gdamore 113 1.1 gdamore /* scan enable options (flags) */ 114 1.25 joerg static int opt_pscan = 0; 115 1.25 joerg static int opt_iscan = 0; 116 1.1 gdamore 117 1.14 plunky /* master role option */ 118 1.25 joerg static int opt_master = 0; 119 1.14 plunky 120 1.1 gdamore /* link policy options (flags) */ 121 1.25 joerg static int opt_switch = 0; 122 1.25 joerg static int opt_hold = 0; 123 1.25 joerg static int opt_sniff = 0; 124 1.25 joerg static int opt_park = 0; 125 1.1 gdamore 126 1.1 gdamore /* class of device (hex value) */ 127 1.25 joerg static int opt_class = 0; 128 1.25 joerg static uint32_t class; 129 1.1 gdamore 130 1.1 gdamore /* packet type mask (hex value) */ 131 1.25 joerg static int opt_ptype = 0; 132 1.25 joerg static uint32_t ptype; 133 1.1 gdamore 134 1.1 gdamore /* unit name (string) */ 135 1.25 joerg static int opt_name = 0; 136 1.25 joerg static char name[MAX_STR_SIZE]; 137 1.1 gdamore 138 1.1 gdamore /* pin type */ 139 1.25 joerg static int opt_pin = 0; 140 1.1 gdamore 141 1.1 gdamore /* Inquiry */ 142 1.25 joerg static int opt_rssi = 0; /* inquiry_with_rssi (obsolete flag) */ 143 1.25 joerg static int opt_imode = 0; /* inquiry mode */ 144 1.25 joerg static int opt_inquiry = 0; 145 1.21 plunky #define INQUIRY_LENGTH 10 /* seconds */ 146 1.1 gdamore #define INQUIRY_MAX_RESPONSES 10 147 1.25 joerg static const char *imodes[] = { "std", "rssi", "ext", NULL }; 148 1.1 gdamore 149 1.1 gdamore /* Voice Settings */ 150 1.25 joerg static int opt_voice = 0; 151 1.25 joerg static uint32_t voice; 152 1.1 gdamore 153 1.2 tron /* Page Timeout */ 154 1.25 joerg static int opt_pto = 0; 155 1.25 joerg static uint32_t pto; 156 1.2 tron 157 1.3 plunky /* set SCO mtu */ 158 1.25 joerg static int opt_scomtu; 159 1.25 joerg static uint32_t scomtu; 160 1.3 plunky 161 1.25 joerg static struct parameter { 162 1.1 gdamore const char *name; 163 1.15 plunky enum { P_SET, P_CLR, P_STR, P_HEX, P_NUM, P_VAL } type; 164 1.1 gdamore int *opt; 165 1.1 gdamore void *val; 166 1.1 gdamore } parameters[] = { 167 1.1 gdamore { "up", P_SET, &opt_enable, NULL }, 168 1.1 gdamore { "enable", P_SET, &opt_enable, NULL }, 169 1.1 gdamore { "down", P_CLR, &opt_enable, NULL }, 170 1.1 gdamore { "disable", P_CLR, &opt_enable, NULL }, 171 1.1 gdamore { "name", P_STR, &opt_name, name }, 172 1.1 gdamore { "pscan", P_SET, &opt_pscan, NULL }, 173 1.1 gdamore { "-pscan", P_CLR, &opt_pscan, NULL }, 174 1.1 gdamore { "iscan", P_SET, &opt_iscan, NULL }, 175 1.1 gdamore { "-iscan", P_CLR, &opt_iscan, NULL }, 176 1.14 plunky { "master", P_SET, &opt_master, NULL }, 177 1.14 plunky { "-master", P_CLR, &opt_master, NULL }, 178 1.1 gdamore { "switch", P_SET, &opt_switch, NULL }, 179 1.1 gdamore { "-switch", P_CLR, &opt_switch, NULL }, 180 1.1 gdamore { "hold", P_SET, &opt_hold, NULL }, 181 1.1 gdamore { "-hold", P_CLR, &opt_hold, NULL }, 182 1.1 gdamore { "sniff", P_SET, &opt_sniff, NULL }, 183 1.1 gdamore { "-sniff", P_CLR, &opt_sniff, NULL }, 184 1.1 gdamore { "park", P_SET, &opt_park, NULL }, 185 1.1 gdamore { "-park", P_CLR, &opt_park, NULL }, 186 1.1 gdamore { "auth", P_SET, &opt_auth, NULL }, 187 1.1 gdamore { "-auth", P_CLR, &opt_auth, NULL }, 188 1.1 gdamore { "encrypt", P_SET, &opt_encrypt, NULL }, 189 1.1 gdamore { "-encrypt", P_CLR, &opt_encrypt, NULL }, 190 1.1 gdamore { "ptype", P_HEX, &opt_ptype, &ptype }, 191 1.1 gdamore { "class", P_HEX, &opt_class, &class }, 192 1.1 gdamore { "fixed", P_SET, &opt_pin, NULL }, 193 1.1 gdamore { "variable", P_CLR, &opt_pin, NULL }, 194 1.1 gdamore { "inq", P_SET, &opt_inquiry, NULL }, 195 1.1 gdamore { "inquiry", P_SET, &opt_inquiry, NULL }, 196 1.15 plunky { "imode", P_VAL, &opt_imode, imodes }, 197 1.6 plunky { "rssi", P_SET, &opt_rssi, NULL }, 198 1.6 plunky { "-rssi", P_CLR, &opt_rssi, NULL }, 199 1.1 gdamore { "reset", P_SET, &opt_reset, NULL }, 200 1.1 gdamore { "voice", P_HEX, &opt_voice, &voice }, 201 1.2 tron { "pto", P_NUM, &opt_pto, &pto }, 202 1.3 plunky { "scomtu", P_NUM, &opt_scomtu, &scomtu }, 203 1.5 plunky { NULL, 0, NULL, NULL } 204 1.1 gdamore }; 205 1.1 gdamore 206 1.1 gdamore int 207 1.1 gdamore main(int ac, char *av[]) 208 1.1 gdamore { 209 1.1 gdamore int ch; 210 1.1 gdamore struct parameter *p; 211 1.1 gdamore 212 1.1 gdamore while ((ch = getopt(ac, av, "hlsvz")) != -1) { 213 1.1 gdamore switch(ch) { 214 1.1 gdamore case 'l': 215 1.1 gdamore lflag = 1; 216 1.1 gdamore break; 217 1.1 gdamore 218 1.1 gdamore case 's': 219 1.1 gdamore sflag = 1; 220 1.1 gdamore break; 221 1.1 gdamore 222 1.1 gdamore case 'v': 223 1.1 gdamore verbose++; 224 1.1 gdamore break; 225 1.1 gdamore 226 1.1 gdamore case 'z': 227 1.1 gdamore sflag = 2; 228 1.1 gdamore break; 229 1.1 gdamore 230 1.1 gdamore case 'h': 231 1.1 gdamore default: 232 1.1 gdamore usage(); 233 1.1 gdamore } 234 1.1 gdamore } 235 1.1 gdamore av += optind; 236 1.1 gdamore ac -= optind; 237 1.1 gdamore 238 1.1 gdamore if (lflag && sflag) 239 1.1 gdamore usage(); 240 1.1 gdamore 241 1.1 gdamore hci = socket(PF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI); 242 1.1 gdamore if (hci == -1) 243 1.1 gdamore err(EXIT_FAILURE, "socket"); 244 1.1 gdamore 245 1.1 gdamore if (ac == 0) { 246 1.1 gdamore verbose++; 247 1.1 gdamore 248 1.1 gdamore memset(&btr, 0, sizeof(btr)); 249 1.1 gdamore while (set_unit(SIOCNBTINFO) != -1) { 250 1.1 gdamore print_info(verbose); 251 1.1 gdamore print_stats(); 252 1.1 gdamore } 253 1.1 gdamore 254 1.1 gdamore tag(NULL); 255 1.1 gdamore } else { 256 1.1 gdamore strlcpy(btr.btr_name, *av, HCI_DEVNAME_SIZE); 257 1.1 gdamore av++, ac--; 258 1.1 gdamore 259 1.1 gdamore if (set_unit(SIOCGBTINFO) < 0) 260 1.1 gdamore err(EXIT_FAILURE, "%s", btr.btr_name); 261 1.1 gdamore 262 1.1 gdamore if (ac == 0) 263 1.1 gdamore verbose += 2; 264 1.1 gdamore 265 1.1 gdamore while (ac > 0) { 266 1.1 gdamore for (p = parameters ; ; p++) { 267 1.1 gdamore if (p->name == NULL) 268 1.1 gdamore badparam(*av); 269 1.1 gdamore 270 1.1 gdamore if (strcmp(*av, p->name) == 0) 271 1.1 gdamore break; 272 1.1 gdamore } 273 1.1 gdamore 274 1.1 gdamore switch(p->type) { 275 1.1 gdamore case P_SET: 276 1.1 gdamore *(p->opt) = 1; 277 1.1 gdamore break; 278 1.1 gdamore 279 1.1 gdamore case P_CLR: 280 1.1 gdamore *(p->opt) = -1; 281 1.1 gdamore break; 282 1.1 gdamore 283 1.1 gdamore case P_STR: 284 1.1 gdamore if (--ac < 1) badarg(p->name); 285 1.1 gdamore strlcpy((char *)(p->val), *++av, MAX_STR_SIZE); 286 1.1 gdamore *(p->opt) = 1; 287 1.1 gdamore break; 288 1.1 gdamore 289 1.1 gdamore case P_HEX: 290 1.1 gdamore if (--ac < 1) badarg(p->name); 291 1.5 plunky *(uint32_t *)(p->val) = strtoul(*++av, NULL, 16); 292 1.1 gdamore *(p->opt) = 1; 293 1.1 gdamore break; 294 1.2 tron 295 1.2 tron case P_NUM: 296 1.2 tron if (--ac < 1) badarg(p->name); 297 1.5 plunky *(uint32_t *)(p->val) = strtoul(*++av, NULL, 10); 298 1.2 tron *(p->opt) = 1; 299 1.2 tron break; 300 1.15 plunky 301 1.15 plunky case P_VAL: 302 1.15 plunky if (--ac < 1) badarg(p->name); 303 1.15 plunky ++av; 304 1.15 plunky ch = 0; 305 1.15 plunky do { 306 1.15 plunky if (((char **)(p->val))[ch] == NULL) 307 1.15 plunky badval(p->name, *av); 308 1.15 plunky } while (strcmp(((char **)(p->val))[ch++], *av)); 309 1.15 plunky *(p->opt) = ch; 310 1.15 plunky break; 311 1.1 gdamore } 312 1.1 gdamore 313 1.1 gdamore av++, ac--; 314 1.1 gdamore } 315 1.1 gdamore 316 1.1 gdamore config_unit(); 317 1.1 gdamore print_info(verbose); 318 1.1 gdamore print_stats(); 319 1.1 gdamore do_inquiry(); 320 1.1 gdamore } 321 1.1 gdamore 322 1.1 gdamore close(hci); 323 1.1 gdamore return EXIT_SUCCESS; 324 1.1 gdamore } 325 1.1 gdamore 326 1.25 joerg static void 327 1.1 gdamore badparam(const char *param) 328 1.1 gdamore { 329 1.1 gdamore 330 1.1 gdamore fprintf(stderr, "unknown parameter '%s'\n", param); 331 1.1 gdamore exit(EXIT_FAILURE); 332 1.1 gdamore } 333 1.1 gdamore 334 1.25 joerg static void 335 1.1 gdamore badarg(const char *param) 336 1.1 gdamore { 337 1.1 gdamore 338 1.1 gdamore fprintf(stderr, "parameter '%s' needs argument\n", param); 339 1.1 gdamore exit(EXIT_FAILURE); 340 1.1 gdamore } 341 1.1 gdamore 342 1.25 joerg static void 343 1.15 plunky badval(const char *param, const char *value) 344 1.15 plunky { 345 1.15 plunky 346 1.15 plunky fprintf(stderr, "bad value '%s' for parameter '%s'\n", value, param); 347 1.15 plunky exit(EXIT_FAILURE); 348 1.15 plunky } 349 1.15 plunky 350 1.25 joerg static void 351 1.1 gdamore usage(void) 352 1.1 gdamore { 353 1.1 gdamore 354 1.1 gdamore fprintf(stderr, "usage: %s [-svz] [device [parameters]]\n", getprogname()); 355 1.1 gdamore fprintf(stderr, " %s -l\n", getprogname()); 356 1.1 gdamore exit(EXIT_FAILURE); 357 1.1 gdamore } 358 1.1 gdamore 359 1.1 gdamore /* 360 1.1 gdamore * pretty printing feature 361 1.1 gdamore */ 362 1.25 joerg static void 363 1.1 gdamore tag(const char *f) 364 1.1 gdamore { 365 1.1 gdamore 366 1.1 gdamore if (f == NULL) { 367 1.1 gdamore if (width > 0) 368 1.1 gdamore printf("\n"); 369 1.1 gdamore 370 1.1 gdamore width = 0; 371 1.1 gdamore } else { 372 1.1 gdamore width += printf("%*s%s", 373 1.1 gdamore (width == 0 ? (lflag ? 0 : 8) : 1), 374 1.1 gdamore "", f); 375 1.1 gdamore 376 1.1 gdamore if (width > MAX_WIDTH) { 377 1.1 gdamore printf("\n"); 378 1.1 gdamore width = 0; 379 1.1 gdamore } 380 1.1 gdamore } 381 1.1 gdamore } 382 1.1 gdamore 383 1.1 gdamore /* 384 1.21 plunky * basic HCI request wrapper with error check 385 1.1 gdamore */ 386 1.25 joerg static void 387 1.21 plunky hci_req(uint16_t opcode, uint8_t event, void *cbuf, size_t clen, 388 1.21 plunky void *rbuf, size_t rlen) 389 1.1 gdamore { 390 1.21 plunky struct bt_devreq req; 391 1.19 plunky 392 1.21 plunky req.opcode = opcode; 393 1.21 plunky req.event = event; 394 1.21 plunky req.cparam = cbuf; 395 1.21 plunky req.clen = clen; 396 1.21 plunky req.rparam = rbuf; 397 1.21 plunky req.rlen = rlen; 398 1.21 plunky 399 1.21 plunky if (bt_devreq(hci, &req, 10) == -1) 400 1.21 plunky err(EXIT_FAILURE, "cmd (%02x|%03x)", 401 1.21 plunky HCI_OGF(opcode), HCI_OCF(opcode)); 402 1.21 plunky 403 1.21 plunky if (event == 0 && rlen > 0 && ((uint8_t *)rbuf)[0] != 0) 404 1.21 plunky errx(EXIT_FAILURE, "cmd (%02x|%03x): status 0x%02x", 405 1.21 plunky HCI_OGF(opcode), HCI_OCF(opcode), ((uint8_t *)rbuf)[0]); 406 1.21 plunky } 407 1.19 plunky 408 1.21 plunky /* 409 1.21 plunky * write value to device with opcode. 410 1.21 plunky * provide a small response buffer so that the status can be checked 411 1.21 plunky */ 412 1.25 joerg static void 413 1.21 plunky save_value(uint16_t opcode, void *cbuf, size_t clen) 414 1.21 plunky { 415 1.21 plunky uint8_t buf[1]; 416 1.19 plunky 417 1.21 plunky hci_req(opcode, 0, cbuf, clen, buf, sizeof(buf)); 418 1.21 plunky } 419 1.19 plunky 420 1.21 plunky /* 421 1.21 plunky * read value from device with opcode. 422 1.21 plunky * use our own buffer and only return the value from the response packet 423 1.21 plunky */ 424 1.25 joerg static void 425 1.21 plunky load_value(uint16_t opcode, void *rbuf, size_t rlen) 426 1.21 plunky { 427 1.21 plunky uint8_t buf[UINT8_MAX]; 428 1.1 gdamore 429 1.21 plunky hci_req(opcode, 0, NULL, 0, buf, sizeof(buf)); 430 1.21 plunky memcpy(rbuf, buf + 1, rlen); 431 1.1 gdamore } 432 1.1 gdamore 433 1.25 joerg static int 434 1.1 gdamore set_unit(unsigned long cmd) 435 1.1 gdamore { 436 1.1 gdamore 437 1.1 gdamore if (ioctl(hci, cmd, &btr) == -1) 438 1.1 gdamore return -1; 439 1.1 gdamore 440 1.1 gdamore if (btr.btr_flags & BTF_UP) { 441 1.1 gdamore struct sockaddr_bt sa; 442 1.1 gdamore 443 1.1 gdamore sa.bt_len = sizeof(sa); 444 1.1 gdamore sa.bt_family = AF_BLUETOOTH; 445 1.1 gdamore bdaddr_copy(&sa.bt_bdaddr, &btr.btr_bdaddr); 446 1.1 gdamore 447 1.1 gdamore if (bind(hci, (struct sockaddr *)&sa, sizeof(sa)) < 0) 448 1.1 gdamore err(EXIT_FAILURE, "bind"); 449 1.1 gdamore 450 1.1 gdamore if (connect(hci, (struct sockaddr *)&sa, sizeof(sa)) < 0) 451 1.1 gdamore err(EXIT_FAILURE, "connect"); 452 1.1 gdamore } 453 1.1 gdamore 454 1.1 gdamore return 0; 455 1.1 gdamore } 456 1.1 gdamore 457 1.1 gdamore /* 458 1.1 gdamore * apply configuration parameters to unit 459 1.1 gdamore */ 460 1.25 joerg static void 461 1.1 gdamore config_unit(void) 462 1.1 gdamore { 463 1.1 gdamore 464 1.19 plunky if (opt_enable) { 465 1.19 plunky if (opt_enable > 0) 466 1.19 plunky btr.btr_flags |= BTF_UP; 467 1.19 plunky else 468 1.19 plunky btr.btr_flags &= ~BTF_UP; 469 1.1 gdamore 470 1.1 gdamore if (ioctl(hci, SIOCSBTFLAGS, &btr) < 0) 471 1.1 gdamore err(EXIT_FAILURE, "SIOCSBTFLAGS"); 472 1.1 gdamore 473 1.1 gdamore if (set_unit(SIOCGBTINFO) < 0) 474 1.1 gdamore err(EXIT_FAILURE, "%s", btr.btr_name); 475 1.1 gdamore } 476 1.1 gdamore 477 1.19 plunky if (opt_reset) { 478 1.24 plunky static const struct timespec ts = { 0, 100000000 }; /* 100ms */ 479 1.1 gdamore 480 1.19 plunky btr.btr_flags |= BTF_INIT; 481 1.1 gdamore if (ioctl(hci, SIOCSBTFLAGS, &btr) < 0) 482 1.1 gdamore err(EXIT_FAILURE, "SIOCSBTFLAGS"); 483 1.1 gdamore 484 1.24 plunky hci_req(HCI_CMD_RESET, 0, NULL, 0, NULL, 0); 485 1.19 plunky 486 1.24 plunky do { 487 1.24 plunky nanosleep(&ts, NULL); 488 1.24 plunky if (ioctl(hci, SIOCGBTINFO, &btr) < 0) 489 1.24 plunky err(EXIT_FAILURE, "%s", btr.btr_name); 490 1.24 plunky } while ((btr.btr_flags & BTF_INIT) != 0); 491 1.1 gdamore } 492 1.1 gdamore 493 1.14 plunky if (opt_master) { 494 1.14 plunky if (opt_master > 0) 495 1.14 plunky btr.btr_flags |= BTF_MASTER; 496 1.14 plunky else 497 1.14 plunky btr.btr_flags &= ~BTF_MASTER; 498 1.14 plunky 499 1.14 plunky if (ioctl(hci, SIOCSBTFLAGS, &btr) < 0) 500 1.14 plunky err(EXIT_FAILURE, "SIOCSBTFLAGS"); 501 1.14 plunky } 502 1.14 plunky 503 1.1 gdamore if (opt_switch || opt_hold || opt_sniff || opt_park) { 504 1.1 gdamore uint16_t val = btr.btr_link_policy; 505 1.1 gdamore 506 1.1 gdamore if (opt_switch > 0) val |= HCI_LINK_POLICY_ENABLE_ROLE_SWITCH; 507 1.1 gdamore if (opt_switch < 0) val &= ~HCI_LINK_POLICY_ENABLE_ROLE_SWITCH; 508 1.1 gdamore if (opt_hold > 0) val |= HCI_LINK_POLICY_ENABLE_HOLD_MODE; 509 1.1 gdamore if (opt_hold < 0) val &= ~HCI_LINK_POLICY_ENABLE_HOLD_MODE; 510 1.1 gdamore if (opt_sniff > 0) val |= HCI_LINK_POLICY_ENABLE_SNIFF_MODE; 511 1.1 gdamore if (opt_sniff < 0) val &= ~HCI_LINK_POLICY_ENABLE_SNIFF_MODE; 512 1.1 gdamore if (opt_park > 0) val |= HCI_LINK_POLICY_ENABLE_PARK_MODE; 513 1.1 gdamore if (opt_park < 0) val &= ~HCI_LINK_POLICY_ENABLE_PARK_MODE; 514 1.1 gdamore 515 1.1 gdamore btr.btr_link_policy = val; 516 1.1 gdamore if (ioctl(hci, SIOCSBTPOLICY, &btr) < 0) 517 1.1 gdamore err(EXIT_FAILURE, "SIOCSBTPOLICY"); 518 1.1 gdamore } 519 1.1 gdamore 520 1.1 gdamore if (opt_ptype) { 521 1.1 gdamore btr.btr_packet_type = ptype; 522 1.1 gdamore if (ioctl(hci, SIOCSBTPTYPE, &btr) < 0) 523 1.1 gdamore err(EXIT_FAILURE, "SIOCSBTPTYPE"); 524 1.1 gdamore } 525 1.1 gdamore 526 1.1 gdamore if (opt_pscan || opt_iscan) { 527 1.1 gdamore uint8_t val; 528 1.1 gdamore 529 1.1 gdamore load_value(HCI_CMD_READ_SCAN_ENABLE, &val, sizeof(val)); 530 1.1 gdamore if (opt_pscan > 0) val |= HCI_PAGE_SCAN_ENABLE; 531 1.1 gdamore if (opt_pscan < 0) val &= ~HCI_PAGE_SCAN_ENABLE; 532 1.1 gdamore if (opt_iscan > 0) val |= HCI_INQUIRY_SCAN_ENABLE; 533 1.1 gdamore if (opt_iscan < 0) val &= ~HCI_INQUIRY_SCAN_ENABLE; 534 1.1 gdamore save_value(HCI_CMD_WRITE_SCAN_ENABLE, &val, sizeof(val)); 535 1.1 gdamore } 536 1.1 gdamore 537 1.1 gdamore if (opt_auth) { 538 1.1 gdamore uint8_t val = (opt_auth > 0 ? 1 : 0); 539 1.1 gdamore 540 1.1 gdamore save_value(HCI_CMD_WRITE_AUTH_ENABLE, &val, sizeof(val)); 541 1.1 gdamore } 542 1.1 gdamore 543 1.1 gdamore if (opt_encrypt) { 544 1.1 gdamore uint8_t val = (opt_encrypt > 0 ? 1 : 0); 545 1.1 gdamore 546 1.1 gdamore save_value(HCI_CMD_WRITE_ENCRYPTION_MODE, &val, sizeof(val)); 547 1.1 gdamore } 548 1.1 gdamore 549 1.1 gdamore if (opt_name) 550 1.1 gdamore save_value(HCI_CMD_WRITE_LOCAL_NAME, name, HCI_UNIT_NAME_SIZE); 551 1.1 gdamore 552 1.1 gdamore if (opt_class) { 553 1.1 gdamore uint8_t val[HCI_CLASS_SIZE]; 554 1.1 gdamore 555 1.1 gdamore val[0] = (class >> 0) & 0xff; 556 1.1 gdamore val[1] = (class >> 8) & 0xff; 557 1.1 gdamore val[2] = (class >> 16) & 0xff; 558 1.1 gdamore 559 1.1 gdamore save_value(HCI_CMD_WRITE_UNIT_CLASS, val, HCI_CLASS_SIZE); 560 1.1 gdamore } 561 1.1 gdamore 562 1.1 gdamore if (opt_pin) { 563 1.1 gdamore uint8_t val; 564 1.1 gdamore 565 1.1 gdamore if (opt_pin > 0) val = 1; 566 1.1 gdamore else val = 0; 567 1.1 gdamore 568 1.1 gdamore save_value(HCI_CMD_WRITE_PIN_TYPE, &val, sizeof(val)); 569 1.1 gdamore } 570 1.1 gdamore 571 1.1 gdamore if (opt_voice) { 572 1.1 gdamore uint16_t val; 573 1.1 gdamore 574 1.1 gdamore val = htole16(voice & 0x03ff); 575 1.1 gdamore save_value(HCI_CMD_WRITE_VOICE_SETTING, &val, sizeof(val)); 576 1.1 gdamore } 577 1.2 tron 578 1.2 tron if (opt_pto) { 579 1.2 tron uint16_t val; 580 1.2 tron 581 1.2 tron val = htole16(pto * 8 / 5); 582 1.2 tron save_value(HCI_CMD_WRITE_PAGE_TIMEOUT, &val, sizeof(val)); 583 1.2 tron } 584 1.3 plunky 585 1.3 plunky if (opt_scomtu) { 586 1.5 plunky if (scomtu > 0xff) { 587 1.3 plunky warnx("Invalid SCO mtu %d", scomtu); 588 1.3 plunky } else { 589 1.3 plunky btr.btr_sco_mtu = scomtu; 590 1.3 plunky 591 1.3 plunky if (ioctl(hci, SIOCSBTSCOMTU, &btr) < 0) 592 1.3 plunky warn("SIOCSBTSCOMTU"); 593 1.3 plunky } 594 1.3 plunky } 595 1.6 plunky 596 1.15 plunky if (opt_imode | opt_rssi) { 597 1.6 plunky uint8_t val = (opt_rssi > 0 ? 1 : 0); 598 1.6 plunky 599 1.15 plunky if (opt_imode) 600 1.15 plunky val = opt_imode - 1; 601 1.15 plunky 602 1.6 plunky save_value(HCI_CMD_WRITE_INQUIRY_MODE, &val, sizeof(val)); 603 1.6 plunky } 604 1.1 gdamore } 605 1.1 gdamore 606 1.1 gdamore /* 607 1.15 plunky * print value from NULL terminated array given index 608 1.15 plunky */ 609 1.25 joerg static void 610 1.15 plunky print_val(const char *hdr, const char **argv, int idx) 611 1.15 plunky { 612 1.15 plunky int i = 0; 613 1.15 plunky 614 1.15 plunky while (i < idx && *argv != NULL) 615 1.15 plunky i++, argv++; 616 1.15 plunky 617 1.15 plunky printf("\t%s: %s\n", hdr, *argv == NULL ? "unknown" : *argv); 618 1.15 plunky } 619 1.15 plunky 620 1.15 plunky /* 621 1.1 gdamore * Print info for Bluetooth Device with varying verbosity levels 622 1.1 gdamore */ 623 1.25 joerg static void 624 1.1 gdamore print_info(int level) 625 1.1 gdamore { 626 1.9 plunky uint8_t version, val, buf[MAX_STR_SIZE]; 627 1.1 gdamore 628 1.1 gdamore if (lflag) { 629 1.1 gdamore tag(btr.btr_name); 630 1.1 gdamore return; 631 1.1 gdamore } 632 1.1 gdamore 633 1.1 gdamore if (level-- < 1) 634 1.1 gdamore return; 635 1.1 gdamore 636 1.1 gdamore snprintb((char *)buf, MAX_STR_SIZE, FLAGS_FMT, btr.btr_flags); 637 1.1 gdamore 638 1.1 gdamore printf("%s: bdaddr %s flags %s\n", btr.btr_name, 639 1.1 gdamore bt_ntoa(&btr.btr_bdaddr, NULL), buf); 640 1.1 gdamore 641 1.1 gdamore if (level-- < 1) 642 1.1 gdamore return; 643 1.1 gdamore 644 1.1 gdamore printf("\tnum_cmd = %d\n" 645 1.23 plunky "\tnum_acl = %d (max %d), acl_mtu = %d\n" 646 1.23 plunky "\tnum_sco = %d (max %d), sco_mtu = %d\n", 647 1.1 gdamore btr.btr_num_cmd, 648 1.23 plunky btr.btr_num_acl, btr.btr_max_acl, btr.btr_acl_mtu, 649 1.23 plunky btr.btr_num_sco, btr.btr_max_sco, btr.btr_sco_mtu); 650 1.1 gdamore 651 1.1 gdamore if (level-- < 1 || (btr.btr_flags & BTF_UP) == 0) 652 1.1 gdamore return; 653 1.1 gdamore 654 1.28 plunky load_value(HCI_CMD_READ_LOCAL_VER, buf, 3); 655 1.9 plunky printf("\tHCI version: "); 656 1.28 plunky switch((version = buf[0])) { 657 1.28 plunky case HCI_SPEC_V10: printf("1.0b"); break; 658 1.28 plunky case HCI_SPEC_V11: printf("1.1"); break; 659 1.28 plunky case HCI_SPEC_V12: printf("1.2"); break; 660 1.28 plunky case HCI_SPEC_V20: printf("2.0 + EDR"); break; 661 1.28 plunky case HCI_SPEC_V21: printf("2.1 + EDR"); break; 662 1.28 plunky case HCI_SPEC_V30: printf("3.0 + HS"); break; 663 1.28 plunky case HCI_SPEC_V40: printf("4.0"); break; 664 1.28 plunky case HCI_SPEC_V41: printf("4.1"); break; 665 1.28 plunky case HCI_SPEC_V42: printf("4.2"); break; 666 1.28 plunky case HCI_SPEC_V50: printf("5.0"); break; 667 1.28 plunky default: printf("[%d]", version);break; 668 1.9 plunky } 669 1.28 plunky printf(" rev 0x%04x\n", le16dec(&buf[1])); 670 1.9 plunky 671 1.1 gdamore load_value(HCI_CMD_READ_UNIT_CLASS, buf, HCI_CLASS_SIZE); 672 1.20 plunky print_class("\tclass:", buf); 673 1.1 gdamore 674 1.1 gdamore load_value(HCI_CMD_READ_LOCAL_NAME, buf, HCI_UNIT_NAME_SIZE); 675 1.1 gdamore printf("\tname: \"%s\"\n", buf); 676 1.1 gdamore 677 1.1 gdamore load_value(HCI_CMD_READ_VOICE_SETTING, buf, sizeof(uint16_t)); 678 1.1 gdamore voice = (buf[1] << 8) | buf[0]; 679 1.1 gdamore print_voice(level); 680 1.1 gdamore 681 1.1 gdamore load_value(HCI_CMD_READ_PIN_TYPE, &val, sizeof(val)); 682 1.1 gdamore printf("\tpin: %s\n", val ? "fixed" : "variable"); 683 1.1 gdamore 684 1.15 plunky val = 0; 685 1.15 plunky if (version >= HCI_SPEC_V12) 686 1.15 plunky load_value(HCI_CMD_READ_INQUIRY_MODE, &val, sizeof(val)); 687 1.15 plunky 688 1.15 plunky print_val("inquiry mode", imodes, val); 689 1.15 plunky 690 1.1 gdamore width = printf("\toptions:"); 691 1.1 gdamore 692 1.1 gdamore load_value(HCI_CMD_READ_SCAN_ENABLE, &val, sizeof(val)); 693 1.1 gdamore if (val & HCI_INQUIRY_SCAN_ENABLE) tag("iscan"); 694 1.1 gdamore else if (level > 0) tag("-iscan"); 695 1.1 gdamore 696 1.1 gdamore if (val & HCI_PAGE_SCAN_ENABLE) tag("pscan"); 697 1.1 gdamore else if (level > 0) tag("-pscan"); 698 1.1 gdamore 699 1.1 gdamore load_value(HCI_CMD_READ_AUTH_ENABLE, &val, sizeof(val)); 700 1.1 gdamore if (val) tag("auth"); 701 1.1 gdamore else if (level > 0) tag("-auth"); 702 1.1 gdamore 703 1.1 gdamore load_value(HCI_CMD_READ_ENCRYPTION_MODE, &val, sizeof(val)); 704 1.1 gdamore if (val) tag("encrypt"); 705 1.1 gdamore else if (level > 0) tag("-encrypt"); 706 1.1 gdamore 707 1.1 gdamore val = btr.btr_link_policy; 708 1.1 gdamore if (val & HCI_LINK_POLICY_ENABLE_ROLE_SWITCH) tag("switch"); 709 1.1 gdamore else if (level > 0) tag("-switch"); 710 1.1 gdamore if (val & HCI_LINK_POLICY_ENABLE_HOLD_MODE) tag("hold"); 711 1.1 gdamore else if (level > 0) tag("-hold"); 712 1.1 gdamore if (val & HCI_LINK_POLICY_ENABLE_SNIFF_MODE) tag("sniff"); 713 1.1 gdamore else if (level > 0) tag("-sniff"); 714 1.1 gdamore if (val & HCI_LINK_POLICY_ENABLE_PARK_MODE) tag("park"); 715 1.1 gdamore else if (level > 0) tag("-park"); 716 1.1 gdamore 717 1.1 gdamore tag(NULL); 718 1.1 gdamore 719 1.1 gdamore if (level-- < 1) 720 1.1 gdamore return; 721 1.1 gdamore 722 1.2 tron ptype = btr.btr_packet_type; 723 1.2 tron width = printf("\tptype: [0x%04x]", ptype); 724 1.2 tron if (ptype & HCI_PKT_DM1) tag("DM1"); 725 1.2 tron if (ptype & HCI_PKT_DH1) tag("DH1"); 726 1.2 tron if (ptype & HCI_PKT_DM3) tag("DM3"); 727 1.2 tron if (ptype & HCI_PKT_DH3) tag("DH3"); 728 1.2 tron if (ptype & HCI_PKT_DM5) tag("DM5"); 729 1.2 tron if (ptype & HCI_PKT_DH5) tag("DH5"); 730 1.2 tron if ((ptype & HCI_PKT_2MBPS_DH1) == 0) tag("2-DH1"); 731 1.2 tron if ((ptype & HCI_PKT_3MBPS_DH1) == 0) tag("3-DH1"); 732 1.2 tron if ((ptype & HCI_PKT_2MBPS_DH3) == 0) tag("2-DH3"); 733 1.2 tron if ((ptype & HCI_PKT_3MBPS_DH3) == 0) tag("3-DH3"); 734 1.2 tron if ((ptype & HCI_PKT_2MBPS_DH5) == 0) tag("2-DH5"); 735 1.2 tron if ((ptype & HCI_PKT_3MBPS_DH5) == 0) tag("3-DH5"); 736 1.2 tron tag(NULL); 737 1.2 tron 738 1.22 plunky load_value(HCI_CMD_READ_PAGE_TIMEOUT, buf, sizeof(uint16_t)); 739 1.22 plunky printf("\tpage timeout: %d ms\n", le16dec(buf) * 5 / 8); 740 1.1 gdamore 741 1.1 gdamore if (level-- < 1) 742 1.1 gdamore return; 743 1.1 gdamore 744 1.23 plunky if (ioctl(hci, SIOCGBTFEAT, &btr) < 0) 745 1.23 plunky err(EXIT_FAILURE, "SIOCGBTFEAT"); 746 1.21 plunky 747 1.23 plunky width = printf("\tfeatures:"); 748 1.23 plunky print_features0(btr.btr_features0); 749 1.23 plunky print_features1(btr.btr_features1); 750 1.27 plunky print_features2(btr.btr_features2); 751 1.23 plunky tag(NULL); 752 1.1 gdamore } 753 1.1 gdamore 754 1.25 joerg static void 755 1.1 gdamore print_stats(void) 756 1.1 gdamore { 757 1.1 gdamore 758 1.1 gdamore if (sflag == 0) 759 1.1 gdamore return; 760 1.1 gdamore 761 1.1 gdamore if (sflag == 1) { 762 1.1 gdamore if (ioctl(hci, SIOCGBTSTATS, &btr) < 0) 763 1.1 gdamore err(EXIT_FAILURE, "SIOCGBTSTATS"); 764 1.1 gdamore } else { 765 1.1 gdamore if (ioctl(hci, SIOCZBTSTATS, &btr) < 0) 766 1.1 gdamore err(EXIT_FAILURE, "SIOCZBTSTATS"); 767 1.1 gdamore } 768 1.1 gdamore 769 1.26 mbalmer printf( "\tTotal bytes sent %d, received %d\n" 770 1.1 gdamore "\tCommands sent %d, Events received %d\n" 771 1.1 gdamore "\tACL data packets sent %d, received %d\n" 772 1.1 gdamore "\tSCO data packets sent %d, received %d\n" 773 1.1 gdamore "\tInput errors %d, Output errors %d\n", 774 1.1 gdamore btr.btr_stats.byte_tx, btr.btr_stats.byte_rx, 775 1.1 gdamore btr.btr_stats.cmd_tx, btr.btr_stats.evt_rx, 776 1.1 gdamore btr.btr_stats.acl_tx, btr.btr_stats.acl_rx, 777 1.1 gdamore btr.btr_stats.sco_tx, btr.btr_stats.sco_rx, 778 1.1 gdamore btr.btr_stats.err_rx, btr.btr_stats.err_tx); 779 1.1 gdamore } 780 1.1 gdamore 781 1.25 joerg static void 782 1.17 plunky print_features0(uint8_t *f) 783 1.17 plunky { 784 1.1 gdamore 785 1.1 gdamore /* ------------------- byte 0 --------------------*/ 786 1.1 gdamore if (*f & HCI_LMP_3SLOT) tag("<3 slot>"); 787 1.1 gdamore if (*f & HCI_LMP_5SLOT) tag("<5 slot>"); 788 1.1 gdamore if (*f & HCI_LMP_ENCRYPTION) tag("<encryption>"); 789 1.1 gdamore if (*f & HCI_LMP_SLOT_OFFSET) tag("<slot offset>"); 790 1.1 gdamore if (*f & HCI_LMP_TIMIACCURACY) tag("<timing accuracy>"); 791 1.1 gdamore if (*f & HCI_LMP_ROLE_SWITCH) tag("<role switch>"); 792 1.1 gdamore if (*f & HCI_LMP_HOLD_MODE) tag("<hold mode>"); 793 1.1 gdamore if (*f & HCI_LMP_SNIFF_MODE) tag("<sniff mode>"); 794 1.1 gdamore f++; 795 1.1 gdamore 796 1.1 gdamore /* ------------------- byte 1 --------------------*/ 797 1.1 gdamore if (*f & HCI_LMP_PARK_MODE) tag("<park mode>"); 798 1.1 gdamore if (*f & HCI_LMP_RSSI) tag("<RSSI>"); 799 1.1 gdamore if (*f & HCI_LMP_CHANNEL_QUALITY) tag("<channel quality>"); 800 1.1 gdamore if (*f & HCI_LMP_SCO_LINK) tag("<SCO link>"); 801 1.1 gdamore if (*f & HCI_LMP_HV2_PKT) tag("<HV2>"); 802 1.1 gdamore if (*f & HCI_LMP_HV3_PKT) tag("<HV3>"); 803 1.1 gdamore if (*f & HCI_LMP_ULAW_LOG) tag("<u-Law log>"); 804 1.1 gdamore if (*f & HCI_LMP_ALAW_LOG) tag("<A-Law log>"); 805 1.1 gdamore f++; 806 1.1 gdamore 807 1.1 gdamore /* ------------------- byte 1 --------------------*/ 808 1.1 gdamore if (*f & HCI_LMP_CVSD) tag("<CVSD data>"); 809 1.1 gdamore if (*f & HCI_LMP_PAGISCHEME) tag("<paging parameter>"); 810 1.1 gdamore if (*f & HCI_LMP_POWER_CONTROL) tag("<power control>"); 811 1.1 gdamore if (*f & HCI_LMP_TRANSPARENT_SCO) tag("<transparent SCO>"); 812 1.27 plunky if (*f & HCI_LMP_FLOW_CONTROL_LAG0) tag("<flow control lag lsb>"); 813 1.27 plunky if (*f & HCI_LMP_FLOW_CONTROL_LAG1) tag("<flow control lag mb>"); 814 1.27 plunky if (*f & HCI_LMP_FLOW_CONTROL_LAG2) tag("<flow control lag msb>"); 815 1.1 gdamore if (*f & HCI_LMP_BC_ENCRYPTION) tag("<broadcast encryption>"); 816 1.1 gdamore f++; 817 1.1 gdamore 818 1.1 gdamore /* ------------------- byte 3 --------------------*/ 819 1.1 gdamore if (*f & HCI_LMP_EDR_ACL_2MBPS) tag("<EDR ACL 2Mbps>"); 820 1.1 gdamore if (*f & HCI_LMP_EDR_ACL_3MBPS) tag("<EDR ACL 3Mbps>"); 821 1.1 gdamore if (*f & HCI_LMP_ENHANCED_ISCAN) tag("<enhanced inquiry scan>"); 822 1.1 gdamore if (*f & HCI_LMP_INTERLACED_ISCAN) tag("<interlaced inquiry scan>"); 823 1.1 gdamore if (*f & HCI_LMP_INTERLACED_PSCAN) tag("<interlaced page scan>"); 824 1.1 gdamore if (*f & HCI_LMP_RSSI_INQUIRY) tag("<RSSI with inquiry result>"); 825 1.1 gdamore if (*f & HCI_LMP_EV3_PKT) tag("<EV3 packets>"); 826 1.1 gdamore f++; 827 1.1 gdamore 828 1.1 gdamore /* ------------------- byte 4 --------------------*/ 829 1.1 gdamore if (*f & HCI_LMP_EV4_PKT) tag("<EV4 packets>"); 830 1.1 gdamore if (*f & HCI_LMP_EV5_PKT) tag("<EV5 packets>"); 831 1.1 gdamore if (*f & HCI_LMP_AFH_CAPABLE_SLAVE) tag("<AFH capable slave>"); 832 1.1 gdamore if (*f & HCI_LMP_AFH_CLASS_SLAVE) tag("<AFH class slave>"); 833 1.27 plunky if (*f & HCI_LMP_BR_EDR_UNSUPPORTED)tag("<BR/EDR not supported>"); 834 1.27 plunky if (*f & HCI_LMP_LE_CONTROLLER) tag("<LE (controller)>"); 835 1.1 gdamore if (*f & HCI_LMP_3SLOT_EDR_ACL) tag("<3 slot EDR ACL>"); 836 1.1 gdamore f++; 837 1.1 gdamore 838 1.1 gdamore /* ------------------- byte 5 --------------------*/ 839 1.1 gdamore if (*f & HCI_LMP_5SLOT_EDR_ACL) tag("<5 slot EDR ACL>"); 840 1.11 plunky if (*f & HCI_LMP_SNIFF_SUBRATING) tag("<sniff subrating>"); 841 1.11 plunky if (*f & HCI_LMP_PAUSE_ENCRYPTION) tag("<pause encryption>"); 842 1.1 gdamore if (*f & HCI_LMP_AFH_CAPABLE_MASTER)tag("<AFH capable master>"); 843 1.1 gdamore if (*f & HCI_LMP_AFH_CLASS_MASTER) tag("<AFH class master>"); 844 1.1 gdamore if (*f & HCI_LMP_EDR_eSCO_2MBPS) tag("<EDR eSCO 2Mbps>"); 845 1.1 gdamore if (*f & HCI_LMP_EDR_eSCO_3MBPS) tag("<EDR eSCO 3Mbps>"); 846 1.1 gdamore if (*f & HCI_LMP_3SLOT_EDR_eSCO) tag("<3 slot EDR eSCO>"); 847 1.1 gdamore f++; 848 1.1 gdamore 849 1.1 gdamore /* ------------------- byte 6 --------------------*/ 850 1.11 plunky if (*f & HCI_LMP_EXTENDED_INQUIRY) tag("<extended inquiry>"); 851 1.27 plunky if (*f & HCI_LMP_LE_BR_EDR_CONTROLLER)tag("<simultaneous LE & BR/EDR (controller)>"); 852 1.11 plunky if (*f & HCI_LMP_SIMPLE_PAIRING) tag("<secure simple pairing>"); 853 1.11 plunky if (*f & HCI_LMP_ENCAPSULATED_PDU) tag("<encapsulated PDU>"); 854 1.11 plunky if (*f & HCI_LMP_ERRDATA_REPORTING) tag("<errdata reporting>"); 855 1.11 plunky if (*f & HCI_LMP_NOFLUSH_PB_FLAG) tag("<no flush PB flag>"); 856 1.1 gdamore f++; 857 1.1 gdamore 858 1.1 gdamore /* ------------------- byte 7 --------------------*/ 859 1.11 plunky if (*f & HCI_LMP_LINK_SUPERVISION_TO)tag("<link supervision timeout changed>"); 860 1.11 plunky if (*f & HCI_LMP_INQ_RSP_TX_POWER) tag("<inquiry rsp TX power level>"); 861 1.17 plunky if (*f & HCI_LMP_ENHANCED_POWER_CONTROL)tag("<enhanced power control>"); 862 1.1 gdamore if (*f & HCI_LMP_EXTENDED_FEATURES) tag("<extended features>"); 863 1.17 plunky } 864 1.1 gdamore 865 1.25 joerg static void 866 1.17 plunky print_features1(uint8_t *f) 867 1.17 plunky { 868 1.17 plunky 869 1.17 plunky /* ------------------- byte 0 --------------------*/ 870 1.27 plunky if (*f & HCI_LMP_SSP) tag("<secure simple pairing (host)>"); 871 1.27 plunky if (*f & HCI_LMP_LE_HOST) tag("<LE (host)>"); 872 1.27 plunky if (*f & HCI_LMP_LE_BR_EDR_HOST) tag("<simultaneous LE & BR/EDR (host)>"); 873 1.27 plunky if (*f & HCI_LMP_SECURE_CONN_HOST) tag("<secure connections (host)>"); 874 1.27 plunky } 875 1.27 plunky 876 1.27 plunky static void 877 1.27 plunky print_features2(uint8_t *f) 878 1.27 plunky { 879 1.27 plunky /* ------------------- byte 0 --------------------*/ 880 1.27 plunky if (*f & HCI_LMP_CONNLESS_MASTER) tag("<connectionless master>"); 881 1.27 plunky if (*f & HCI_LMP_CONNLESS_SLAVE) tag("<connectionless slave>"); 882 1.27 plunky if (*f & HCI_LMP_SYNC_TRAIN) tag("<synchronization train>"); 883 1.27 plunky if (*f & HCI_LMP_SYNC_SCAN) tag("<synchronization scan>"); 884 1.27 plunky if (*f & HCI_LMP_INQ_RSP_NOTIFY) tag("<inquiry response notification>"); 885 1.27 plunky if (*f & HCI_LMP_INTERLACE_SCAN) tag("<generalized interlace scan>"); 886 1.27 plunky if (*f & HCI_LMP_COARSE_CLOCK) tag("<coarse clock adjustment>"); 887 1.27 plunky 888 1.27 plunky /* ------------------- byte 1 --------------------*/ 889 1.27 plunky if (*f & HCI_LMP_SECURE_CONN_CONTROLLER)tag("<secure connections (controller)>"); 890 1.27 plunky if (*f & HCI_LMP_PING) tag("<ping>"); 891 1.27 plunky if (*f & HCI_LMP_TRAIN_NUDGING) tag("<train nudging>"); 892 1.1 gdamore } 893 1.1 gdamore 894 1.25 joerg static void 895 1.20 plunky print_class(const char *str, uint8_t *uclass) 896 1.1 gdamore { 897 1.1 gdamore 898 1.20 plunky class = (uclass[2] << 16) | (uclass[1] << 8) | uclass[0]; 899 1.20 plunky width = printf("%s [0x%06x]", str, class); 900 1.1 gdamore 901 1.20 plunky switch(__SHIFTOUT(class, __BITS(0, 1))) { 902 1.20 plunky case 0: print_class0(); break; 903 1.20 plunky default: break; 904 1.20 plunky } 905 1.20 plunky 906 1.20 plunky tag(NULL); 907 1.20 plunky } 908 1.20 plunky 909 1.25 joerg static void 910 1.20 plunky print_class0(void) 911 1.20 plunky { 912 1.1 gdamore 913 1.20 plunky switch (__SHIFTOUT(class, __BITS(8, 12))) { 914 1.1 gdamore case 1: /* Computer */ 915 1.20 plunky switch (__SHIFTOUT(class, __BITS(2, 7))) { 916 1.20 plunky case 1: tag("Desktop workstation"); break; 917 1.20 plunky case 2: tag("Server-class computer"); break; 918 1.1 gdamore case 3: tag("Laptop"); break; 919 1.20 plunky case 4: tag("Handheld PC/PDA"); break; 920 1.20 plunky case 5: tag("Palm Sized PC/PDA"); break; 921 1.20 plunky case 6: tag("Wearable computer"); break; 922 1.20 plunky default: tag("Computer"); break; 923 1.1 gdamore } 924 1.1 gdamore break; 925 1.1 gdamore 926 1.1 gdamore case 2: /* Phone */ 927 1.20 plunky switch (__SHIFTOUT(class, __BITS(2, 7))) { 928 1.1 gdamore case 1: tag("Cellular Phone"); break; 929 1.1 gdamore case 2: tag("Cordless Phone"); break; 930 1.1 gdamore case 3: tag("Smart Phone"); break; 931 1.1 gdamore case 4: tag("Wired Modem/Phone Gateway"); break; 932 1.1 gdamore case 5: tag("Common ISDN"); break; 933 1.1 gdamore default:tag("Phone"); break; 934 1.1 gdamore } 935 1.1 gdamore break; 936 1.1 gdamore 937 1.1 gdamore case 3: /* LAN */ 938 1.1 gdamore tag("LAN"); 939 1.20 plunky switch (__SHIFTOUT(class, __BITS(5, 7))) { 940 1.1 gdamore case 0: tag("[Fully available]"); break; 941 1.1 gdamore case 1: tag("[1-17% utilised]"); break; 942 1.1 gdamore case 2: tag("[17-33% utilised]"); break; 943 1.1 gdamore case 3: tag("[33-50% utilised]"); break; 944 1.1 gdamore case 4: tag("[50-67% utilised]"); break; 945 1.1 gdamore case 5: tag("[67-83% utilised]"); break; 946 1.1 gdamore case 6: tag("[83-99% utilised]"); break; 947 1.1 gdamore case 7: tag("[No service available]"); break; 948 1.1 gdamore } 949 1.1 gdamore break; 950 1.1 gdamore 951 1.1 gdamore case 4: /* Audio/Visual */ 952 1.20 plunky switch (__SHIFTOUT(class, __BITS(2, 7))) { 953 1.1 gdamore case 1: tag("Wearable Headset"); break; 954 1.1 gdamore case 2: tag("Hands-free Audio"); break; 955 1.1 gdamore case 4: tag("Microphone"); break; 956 1.1 gdamore case 5: tag("Loudspeaker"); break; 957 1.1 gdamore case 6: tag("Headphones"); break; 958 1.1 gdamore case 7: tag("Portable Audio"); break; 959 1.1 gdamore case 8: tag("Car Audio"); break; 960 1.1 gdamore case 9: tag("Set-top Box"); break; 961 1.1 gdamore case 10: tag("HiFi Audio"); break; 962 1.1 gdamore case 11: tag("VCR"); break; 963 1.1 gdamore case 12: tag("Video Camera"); break; 964 1.1 gdamore case 13: tag("Camcorder"); break; 965 1.1 gdamore case 14: tag("Video Monitor"); break; 966 1.1 gdamore case 15: tag("Video Display and Loudspeaker"); break; 967 1.1 gdamore case 16: tag("Video Conferencing"); break; 968 1.1 gdamore case 18: tag("A/V [Gaming/Toy]"); break; 969 1.1 gdamore default: tag("Audio/Visual"); break; 970 1.1 gdamore } 971 1.1 gdamore break; 972 1.1 gdamore 973 1.1 gdamore case 5: /* Peripheral */ 974 1.20 plunky switch (__SHIFTOUT(class, __BITS(2, 5))) { 975 1.1 gdamore case 1: tag("Joystick"); break; 976 1.1 gdamore case 2: tag("Gamepad"); break; 977 1.1 gdamore case 3: tag("Remote Control"); break; 978 1.1 gdamore case 4: tag("Sensing Device"); break; 979 1.1 gdamore case 5: tag("Digitiser Tablet"); break; 980 1.1 gdamore case 6: tag("Card Reader"); break; 981 1.1 gdamore default: tag("Peripheral"); break; 982 1.1 gdamore } 983 1.1 gdamore 984 1.20 plunky if (class & __BIT(6)) tag("Keyboard"); 985 1.20 plunky if (class & __BIT(7)) tag("Mouse"); 986 1.1 gdamore break; 987 1.1 gdamore 988 1.1 gdamore case 6: /* Imaging */ 989 1.20 plunky if (class & __BIT(4)) tag("Display"); 990 1.20 plunky if (class & __BIT(5)) tag("Camera"); 991 1.20 plunky if (class & __BIT(6)) tag("Scanner"); 992 1.20 plunky if (class & __BIT(7)) tag("Printer"); 993 1.20 plunky if ((class & __BITS(4, 7)) == 0) tag("Imaging"); 994 1.1 gdamore break; 995 1.1 gdamore 996 1.1 gdamore case 7: /* Wearable */ 997 1.20 plunky switch (__SHIFTOUT(class, __BITS(2, 7))) { 998 1.1 gdamore case 1: tag("Wrist Watch"); break; 999 1.1 gdamore case 2: tag("Pager"); break; 1000 1.1 gdamore case 3: tag("Jacket"); break; 1001 1.1 gdamore case 4: tag("Helmet"); break; 1002 1.1 gdamore case 5: tag("Glasses"); break; 1003 1.1 gdamore default: tag("Wearable"); break; 1004 1.1 gdamore } 1005 1.1 gdamore break; 1006 1.1 gdamore 1007 1.1 gdamore case 8: /* Toy */ 1008 1.20 plunky switch (__SHIFTOUT(class, __BITS(2, 7))) { 1009 1.1 gdamore case 1: tag("Robot"); break; 1010 1.1 gdamore case 2: tag("Vehicle"); break; 1011 1.1 gdamore case 3: tag("Doll / Action Figure"); break; 1012 1.1 gdamore case 4: tag("Controller"); break; 1013 1.1 gdamore case 5: tag("Game"); break; 1014 1.1 gdamore default: tag("Toy"); break; 1015 1.1 gdamore } 1016 1.1 gdamore break; 1017 1.1 gdamore 1018 1.20 plunky case 9: /* Health */ 1019 1.20 plunky switch (__SHIFTOUT(class, __BITS(2, 7))) { 1020 1.20 plunky case 1: tag("Blood Pressure Monitor"); break; 1021 1.20 plunky case 2: tag("Thermometer"); break; 1022 1.20 plunky case 3: tag("Weighing Scale"); break; 1023 1.20 plunky case 4: tag("Glucose Meter"); break; 1024 1.20 plunky case 5: tag("Pulse Oximeter"); break; 1025 1.20 plunky case 6: tag("Heart/Pulse Rate Monitor"); break; 1026 1.20 plunky case 7: tag("Health Data Display"); break; 1027 1.20 plunky default: tag("Health"); break; 1028 1.20 plunky } 1029 1.20 plunky break; 1030 1.20 plunky 1031 1.1 gdamore default: 1032 1.1 gdamore break; 1033 1.1 gdamore } 1034 1.1 gdamore 1035 1.20 plunky if (class & __BIT(13)) tag("<Limited Discoverable>"); 1036 1.20 plunky if (class & __BIT(16)) tag("<Positioning>"); 1037 1.20 plunky if (class & __BIT(17)) tag("<Networking>"); 1038 1.20 plunky if (class & __BIT(18)) tag("<Rendering>"); 1039 1.20 plunky if (class & __BIT(19)) tag("<Capturing>"); 1040 1.20 plunky if (class & __BIT(20)) tag("<Object Transfer>"); 1041 1.20 plunky if (class & __BIT(21)) tag("<Audio>"); 1042 1.20 plunky if (class & __BIT(22)) tag("<Telephony>"); 1043 1.20 plunky if (class & __BIT(23)) tag("<Information>"); 1044 1.1 gdamore } 1045 1.1 gdamore 1046 1.25 joerg static void 1047 1.1 gdamore print_voice(int level) 1048 1.1 gdamore { 1049 1.19 plunky printf("\tvoice: [0x%4.4x]\n", voice); 1050 1.1 gdamore 1051 1.1 gdamore if (level == 0) 1052 1.1 gdamore return; 1053 1.1 gdamore 1054 1.1 gdamore printf("\t\tInput Coding: "); 1055 1.1 gdamore switch ((voice & 0x0300) >> 8) { 1056 1.1 gdamore case 0x00: printf("Linear PCM [%d-bit, pos %d]", 1057 1.19 plunky (voice & 0x0020 ? 16 : 8), 1058 1.19 plunky (voice & 0x001c) >> 2); break; 1059 1.1 gdamore case 0x01: printf("u-Law"); break; 1060 1.1 gdamore case 0x02: printf("A-Law"); break; 1061 1.1 gdamore case 0x03: printf("unknown"); break; 1062 1.1 gdamore } 1063 1.1 gdamore 1064 1.1 gdamore switch ((voice & 0x00c0) >> 6) { 1065 1.1 gdamore case 0x00: printf(", 1's complement"); break; 1066 1.1 gdamore case 0x01: printf(", 2's complement"); break; 1067 1.1 gdamore case 0x02: printf(", sign magnitude"); break; 1068 1.1 gdamore case 0x03: printf(", unsigned"); break; 1069 1.1 gdamore } 1070 1.1 gdamore 1071 1.1 gdamore printf("\n\t\tAir Coding: "); 1072 1.1 gdamore switch (voice & 0x0003) { 1073 1.1 gdamore case 0x00: printf("CVSD"); break; 1074 1.1 gdamore case 0x01: printf("u-Law"); break; 1075 1.1 gdamore case 0x02: printf("A-Law"); break; 1076 1.1 gdamore case 0x03: printf("Transparent"); break; 1077 1.1 gdamore } 1078 1.1 gdamore 1079 1.1 gdamore printf("\n"); 1080 1.1 gdamore } 1081 1.1 gdamore 1082 1.25 joerg static void 1083 1.21 plunky print_result(int num, struct bt_devinquiry *r) 1084 1.1 gdamore { 1085 1.1 gdamore hci_remote_name_req_cp ncp; 1086 1.1 gdamore hci_remote_name_req_compl_ep nep; 1087 1.1 gdamore struct hostent *hp; 1088 1.1 gdamore 1089 1.21 plunky printf("%3d: bdaddr %s", num, bt_ntoa(&r->bdaddr, NULL)); 1090 1.1 gdamore 1091 1.1 gdamore hp = bt_gethostbyaddr((const char *)&r->bdaddr, sizeof(bdaddr_t), AF_BLUETOOTH); 1092 1.1 gdamore if (hp != NULL) 1093 1.1 gdamore printf(" (%s)", hp->h_name); 1094 1.1 gdamore 1095 1.1 gdamore printf("\n"); 1096 1.1 gdamore 1097 1.6 plunky memset(&ncp, 0, sizeof(ncp)); 1098 1.1 gdamore bdaddr_copy(&ncp.bdaddr, &r->bdaddr); 1099 1.21 plunky ncp.page_scan_rep_mode = r->pscan_rep_mode; 1100 1.1 gdamore ncp.clock_offset = r->clock_offset; 1101 1.1 gdamore 1102 1.1 gdamore hci_req(HCI_CMD_REMOTE_NAME_REQ, 1103 1.1 gdamore HCI_EVENT_REMOTE_NAME_REQ_COMPL, 1104 1.1 gdamore &ncp, sizeof(ncp), 1105 1.1 gdamore &nep, sizeof(nep)); 1106 1.1 gdamore 1107 1.1 gdamore printf(" : name \"%s\"\n", nep.name); 1108 1.21 plunky print_class(" : class", r->dev_class); 1109 1.21 plunky printf(" : page scan rep mode 0x%02x\n", r->pscan_rep_mode); 1110 1.1 gdamore printf(" : clock offset %d\n", le16toh(r->clock_offset)); 1111 1.21 plunky printf(" : rssi %d\n", r->rssi); 1112 1.1 gdamore printf("\n"); 1113 1.1 gdamore } 1114 1.1 gdamore 1115 1.25 joerg static void 1116 1.1 gdamore do_inquiry(void) 1117 1.1 gdamore { 1118 1.21 plunky struct bt_devinquiry *result; 1119 1.21 plunky int i, num; 1120 1.1 gdamore 1121 1.1 gdamore if (opt_inquiry == 0) 1122 1.1 gdamore return; 1123 1.1 gdamore 1124 1.1 gdamore printf("Device Discovery from device: %s ...", btr.btr_name); 1125 1.1 gdamore fflush(stdout); 1126 1.1 gdamore 1127 1.21 plunky num = bt_devinquiry(btr.btr_name, INQUIRY_LENGTH, 1128 1.21 plunky INQUIRY_MAX_RESPONSES, &result); 1129 1.19 plunky 1130 1.21 plunky if (num == -1) { 1131 1.21 plunky printf("failed\n"); 1132 1.21 plunky err(EXIT_FAILURE, "%s", btr.btr_name); 1133 1.1 gdamore } 1134 1.1 gdamore 1135 1.1 gdamore printf(" %d response%s\n", num, (num == 1 ? "" : "s")); 1136 1.1 gdamore 1137 1.1 gdamore for (i = 0 ; i < num ; i++) 1138 1.21 plunky print_result(i + 1, &result[i]); 1139 1.21 plunky 1140 1.21 plunky free(result); 1141 1.1 gdamore } 1142