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