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