1 /* $NetBSD: btconfig.c,v 1.8 2007/11/28 20:16:13 plunky 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.\n" 36 "All rights reserved.\n"); 37 __RCSID("$NetBSD: btconfig.c,v 1.8 2007/11/28 20:16:13 plunky Exp $"); 38 39 #include <sys/ioctl.h> 40 #include <sys/param.h> 41 #include <sys/socket.h> 42 43 #include <bluetooth.h> 44 #include <err.h> 45 #include <errno.h> 46 #include <stdio.h> 47 #include <stdlib.h> 48 #include <string.h> 49 #include <unistd.h> 50 #include <util.h> 51 52 /* inquiry results storage */ 53 struct result { 54 bdaddr_t bdaddr; 55 uint8_t page_scan_rep_mode; 56 uint8_t uclass[HCI_CLASS_SIZE]; 57 uint16_t clock_offset; 58 int8_t rssi; 59 }; 60 61 int main(int, char *[]); 62 void badarg(const char *); 63 void badparam(const char *); 64 void usage(void); 65 int set_unit(unsigned long); 66 void config_unit(void); 67 void print_info(int); 68 void print_stats(void); 69 void print_class(const char *); 70 void print_voice(int); 71 void tag(const char *); 72 void print_features(const char *, uint8_t *); 73 void do_inquiry(void); 74 void print_result(int, struct result *, int); 75 76 void hci_req(uint16_t, uint8_t , void *, size_t, void *, size_t); 77 #define save_value(opcode, cbuf, clen) hci_req(opcode, 0, cbuf, clen, NULL, 0) 78 #define load_value(opcode, rbuf, rlen) hci_req(opcode, 0, NULL, 0, rbuf, rlen) 79 #define hci_cmd(opcode, cbuf, clen) hci_req(opcode, 0, cbuf, clen, NULL, 0) 80 81 #define MAX_STR_SIZE 0xff 82 83 /* print width */ 84 int width = 0; 85 #define MAX_WIDTH 70 86 87 /* global variables */ 88 int hci; 89 struct btreq btr; 90 91 /* command line flags */ 92 int verbose = 0; /* more info */ 93 int lflag = 0; /* list devices */ 94 int sflag = 0; /* get/zero stats */ 95 96 /* device up/down (flag) */ 97 int opt_enable = 0; 98 int opt_reset = 0; 99 #define FLAGS_FMT "\20" \ 100 "\001UP" \ 101 "\002RUNNING" \ 102 "\003XMIT_CMD" \ 103 "\004XMIT_ACL" \ 104 "\005XMIT_SCO" \ 105 "\006INIT_BDADDR" \ 106 "\007INIT_BUFFER_SIZE" \ 107 "\010INIT_FEATURES" \ 108 "\011POWER_UP_NOOP" \ 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 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_UNIT_CLASS, buf, HCI_CLASS_SIZE); 645 class = (buf[2] << 16) | (buf[1] << 8) | (buf[0]); 646 print_class("\t"); 647 648 load_value(HCI_CMD_READ_LOCAL_NAME, buf, HCI_UNIT_NAME_SIZE); 649 printf("\tname: \"%s\"\n", buf); 650 651 load_value(HCI_CMD_READ_VOICE_SETTING, buf, sizeof(uint16_t)); 652 voice = (buf[1] << 8) | buf[0]; 653 print_voice(level); 654 655 load_value(HCI_CMD_READ_PIN_TYPE, &val, sizeof(val)); 656 printf("\tpin: %s\n", val ? "fixed" : "variable"); 657 658 width = printf("\toptions:"); 659 660 load_value(HCI_CMD_READ_SCAN_ENABLE, &val, sizeof(val)); 661 if (val & HCI_INQUIRY_SCAN_ENABLE) tag("iscan"); 662 else if (level > 0) tag("-iscan"); 663 664 if (val & HCI_PAGE_SCAN_ENABLE) tag("pscan"); 665 else if (level > 0) tag("-pscan"); 666 667 load_value(HCI_CMD_READ_AUTH_ENABLE, &val, sizeof(val)); 668 if (val) tag("auth"); 669 else if (level > 0) tag("-auth"); 670 671 load_value(HCI_CMD_READ_ENCRYPTION_MODE, &val, sizeof(val)); 672 if (val) tag("encrypt"); 673 else if (level > 0) tag("-encrypt"); 674 675 val = btr.btr_link_policy; 676 if (val & HCI_LINK_POLICY_ENABLE_ROLE_SWITCH) tag("switch"); 677 else if (level > 0) tag("-switch"); 678 if (val & HCI_LINK_POLICY_ENABLE_HOLD_MODE) tag("hold"); 679 else if (level > 0) tag("-hold"); 680 if (val & HCI_LINK_POLICY_ENABLE_SNIFF_MODE) tag("sniff"); 681 else if (level > 0) tag("-sniff"); 682 if (val & HCI_LINK_POLICY_ENABLE_PARK_MODE) tag("park"); 683 else if (level > 0) tag("-park"); 684 685 /* bt3c(4) locks up if you send this command as it is 1.0b spec 686 * I have ideas about fixing that so it can't happen. 687 * 688 load_value(HCI_CMD_READ_INQUIRY_MODE, &val, sizeof(val)); 689 if (val) tag("rssi"); 690 else if (level > 0) tag("-rssi"); 691 */ 692 693 tag(NULL); 694 695 if (level-- < 1) 696 return; 697 698 ptype = btr.btr_packet_type; 699 width = printf("\tptype: [0x%04x]", ptype); 700 if (ptype & HCI_PKT_DM1) tag("DM1"); 701 if (ptype & HCI_PKT_DH1) tag("DH1"); 702 if (ptype & HCI_PKT_DM3) tag("DM3"); 703 if (ptype & HCI_PKT_DH3) tag("DH3"); 704 if (ptype & HCI_PKT_DM5) tag("DM5"); 705 if (ptype & HCI_PKT_DH5) tag("DH5"); 706 if ((ptype & HCI_PKT_2MBPS_DH1) == 0) tag("2-DH1"); 707 if ((ptype & HCI_PKT_3MBPS_DH1) == 0) tag("3-DH1"); 708 if ((ptype & HCI_PKT_2MBPS_DH3) == 0) tag("2-DH3"); 709 if ((ptype & HCI_PKT_3MBPS_DH3) == 0) tag("3-DH3"); 710 if ((ptype & HCI_PKT_2MBPS_DH5) == 0) tag("2-DH5"); 711 if ((ptype & HCI_PKT_3MBPS_DH5) == 0) tag("3-DH5"); 712 tag(NULL); 713 714 load_value(HCI_CMD_READ_PAGE_TIMEOUT, &val16, sizeof(val16)); 715 printf("\tpage timeout: %d ms\n", val16 * 5 / 8); 716 717 if (level-- < 1) 718 return; 719 720 load_value(HCI_CMD_READ_LOCAL_FEATURES, buf, HCI_FEATURES_SIZE); 721 print_features("\tfeatures:", buf); 722 } 723 724 void 725 print_stats(void) 726 { 727 728 if (sflag == 0) 729 return; 730 731 if (sflag == 1) { 732 if (ioctl(hci, SIOCGBTSTATS, &btr) < 0) 733 err(EXIT_FAILURE, "SIOCGBTSTATS"); 734 } else { 735 if (ioctl(hci, SIOCZBTSTATS, &btr) < 0) 736 err(EXIT_FAILURE, "SIOCZBTSTATS"); 737 } 738 739 printf( "\tTotal bytes sent %d, recieved %d\n" 740 "\tCommands sent %d, Events received %d\n" 741 "\tACL data packets sent %d, received %d\n" 742 "\tSCO data packets sent %d, received %d\n" 743 "\tInput errors %d, Output errors %d\n", 744 btr.btr_stats.byte_tx, btr.btr_stats.byte_rx, 745 btr.btr_stats.cmd_tx, btr.btr_stats.evt_rx, 746 btr.btr_stats.acl_tx, btr.btr_stats.acl_rx, 747 btr.btr_stats.sco_tx, btr.btr_stats.sco_rx, 748 btr.btr_stats.err_rx, btr.btr_stats.err_tx); 749 } 750 751 void 752 print_features(const char *str, uint8_t *f) 753 { 754 755 width = printf("%s", str); 756 757 /* ------------------- byte 0 --------------------*/ 758 if (*f & HCI_LMP_3SLOT) tag("<3 slot>"); 759 if (*f & HCI_LMP_5SLOT) tag("<5 slot>"); 760 if (*f & HCI_LMP_ENCRYPTION) tag("<encryption>"); 761 if (*f & HCI_LMP_SLOT_OFFSET) tag("<slot offset>"); 762 if (*f & HCI_LMP_TIMIACCURACY) tag("<timing accuracy>"); 763 if (*f & HCI_LMP_ROLE_SWITCH) tag("<role switch>"); 764 if (*f & HCI_LMP_HOLD_MODE) tag("<hold mode>"); 765 if (*f & HCI_LMP_SNIFF_MODE) tag("<sniff mode>"); 766 f++; 767 768 /* ------------------- byte 1 --------------------*/ 769 if (*f & HCI_LMP_PARK_MODE) tag("<park mode>"); 770 if (*f & HCI_LMP_RSSI) tag("<RSSI>"); 771 if (*f & HCI_LMP_CHANNEL_QUALITY) tag("<channel quality>"); 772 if (*f & HCI_LMP_SCO_LINK) tag("<SCO link>"); 773 if (*f & HCI_LMP_HV2_PKT) tag("<HV2>"); 774 if (*f & HCI_LMP_HV3_PKT) tag("<HV3>"); 775 if (*f & HCI_LMP_ULAW_LOG) tag("<u-Law log>"); 776 if (*f & HCI_LMP_ALAW_LOG) tag("<A-Law log>"); 777 f++; 778 779 /* ------------------- byte 1 --------------------*/ 780 if (*f & HCI_LMP_CVSD) tag("<CVSD data>"); 781 if (*f & HCI_LMP_PAGISCHEME) tag("<paging parameter>"); 782 if (*f & HCI_LMP_POWER_CONTROL) tag("<power control>"); 783 if (*f & HCI_LMP_TRANSPARENT_SCO) tag("<transparent SCO>"); 784 if (*f & HCI_LMP_FLOW_CONTROL_LAG0) tag("<flow control lag 0>"); 785 if (*f & HCI_LMP_FLOW_CONTROL_LAG1) tag("<flow control lag 1>"); 786 if (*f & HCI_LMP_FLOW_CONTROL_LAG2) tag("<flow control lag 2>"); 787 if (*f & HCI_LMP_BC_ENCRYPTION) tag("<broadcast encryption>"); 788 f++; 789 790 /* ------------------- byte 3 --------------------*/ 791 if (*f & HCI_LMP_EDR_ACL_2MBPS) tag("<EDR ACL 2Mbps>"); 792 if (*f & HCI_LMP_EDR_ACL_3MBPS) tag("<EDR ACL 3Mbps>"); 793 if (*f & HCI_LMP_ENHANCED_ISCAN) tag("<enhanced inquiry scan>"); 794 if (*f & HCI_LMP_INTERLACED_ISCAN) tag("<interlaced inquiry scan>"); 795 if (*f & HCI_LMP_INTERLACED_PSCAN) tag("<interlaced page scan>"); 796 if (*f & HCI_LMP_RSSI_INQUIRY) tag("<RSSI with inquiry result>"); 797 if (*f & HCI_LMP_EV3_PKT) tag("<EV3 packets>"); 798 f++; 799 800 /* ------------------- byte 4 --------------------*/ 801 if (*f & HCI_LMP_EV4_PKT) tag("<EV4 packets>"); 802 if (*f & HCI_LMP_EV5_PKT) tag("<EV5 packets>"); 803 if (*f & HCI_LMP_AFH_CAPABLE_SLAVE) tag("<AFH capable slave>"); 804 if (*f & HCI_LMP_AFH_CLASS_SLAVE) tag("<AFH class slave>"); 805 if (*f & HCI_LMP_3SLOT_EDR_ACL) tag("<3 slot EDR ACL>"); 806 f++; 807 808 /* ------------------- byte 5 --------------------*/ 809 if (*f & HCI_LMP_5SLOT_EDR_ACL) tag("<5 slot EDR ACL>"); 810 if (*f & HCI_LMP_AFH_CAPABLE_MASTER)tag("<AFH capable master>"); 811 if (*f & HCI_LMP_AFH_CLASS_MASTER) tag("<AFH class master>"); 812 if (*f & HCI_LMP_EDR_eSCO_2MBPS) tag("<EDR eSCO 2Mbps>"); 813 if (*f & HCI_LMP_EDR_eSCO_3MBPS) tag("<EDR eSCO 3Mbps>"); 814 if (*f & HCI_LMP_3SLOT_EDR_eSCO) tag("<3 slot EDR eSCO>"); 815 f++; 816 817 /* ------------------- byte 6 --------------------*/ 818 f++; 819 820 /* ------------------- byte 7 --------------------*/ 821 if (*f & HCI_LMP_EXTENDED_FEATURES) tag("<extended features>"); 822 823 tag(NULL); 824 } 825 826 void 827 print_class(const char *str) 828 { 829 int major, minor; 830 831 major = (class & 0x1f00) >> 8; 832 minor = (class & 0x00fc) >> 2; 833 834 width = printf("%sclass: [0x%6.6x]", str, class); 835 836 switch (major) { 837 case 1: /* Computer */ 838 switch (minor) { 839 case 1: tag("Desktop"); break; 840 case 2: tag("Server"); break; 841 case 3: tag("Laptop"); break; 842 case 4: tag("Handheld"); break; 843 case 5: tag("Palm Sized"); break; 844 case 6: tag("Wearable"); break; 845 } 846 tag("Computer"); 847 break; 848 849 case 2: /* Phone */ 850 switch (minor) { 851 case 1: tag("Cellular Phone"); break; 852 case 2: tag("Cordless Phone"); break; 853 case 3: tag("Smart Phone"); break; 854 case 4: tag("Wired Modem/Phone Gateway"); break; 855 case 5: tag("Common ISDN"); break; 856 default:tag("Phone"); break; 857 } 858 break; 859 860 case 3: /* LAN */ 861 tag("LAN"); 862 switch ((minor & 0x38) >> 3) { 863 case 0: tag("[Fully available]"); break; 864 case 1: tag("[1-17% utilised]"); break; 865 case 2: tag("[17-33% utilised]"); break; 866 case 3: tag("[33-50% utilised]"); break; 867 case 4: tag("[50-67% utilised]"); break; 868 case 5: tag("[67-83% utilised]"); break; 869 case 6: tag("[83-99% utilised]"); break; 870 case 7: tag("[No service available]"); break; 871 } 872 break; 873 874 case 4: /* Audio/Visual */ 875 switch (minor) { 876 case 1: tag("Wearable Headset"); break; 877 case 2: tag("Hands-free Audio"); break; 878 case 4: tag("Microphone"); break; 879 case 5: tag("Loudspeaker"); break; 880 case 6: tag("Headphones"); break; 881 case 7: tag("Portable Audio"); break; 882 case 8: tag("Car Audio"); break; 883 case 9: tag("Set-top Box"); break; 884 case 10: tag("HiFi Audio"); break; 885 case 11: tag("VCR"); break; 886 case 12: tag("Video Camera"); break; 887 case 13: tag("Camcorder"); break; 888 case 14: tag("Video Monitor"); break; 889 case 15: tag("Video Display and Loudspeaker"); break; 890 case 16: tag("Video Conferencing"); break; 891 case 18: tag("A/V [Gaming/Toy]"); break; 892 default: tag("Audio/Visual"); break; 893 } 894 break; 895 896 case 5: /* Peripheral */ 897 switch (minor & 0x0f) { 898 case 1: tag("Joystick"); break; 899 case 2: tag("Gamepad"); break; 900 case 3: tag("Remote Control"); break; 901 case 4: tag("Sensing Device"); break; 902 case 5: tag("Digitiser Tablet"); break; 903 case 6: tag("Card Reader"); break; 904 default: tag("Peripheral"); break; 905 } 906 907 if (minor & 0x10) tag("Keyboard"); 908 if (minor & 0x20) tag("Mouse"); 909 break; 910 911 case 6: /* Imaging */ 912 if (minor & 0x20) tag("Printer"); 913 if (minor & 0x10) tag("Scanner"); 914 if (minor & 0x08) tag("Camera"); 915 if (minor & 0x04) tag("Display"); 916 if ((minor & 0x3c) == 0) tag("Imaging"); 917 break; 918 919 case 7: /* Wearable */ 920 switch (minor) { 921 case 1: tag("Wrist Watch"); break; 922 case 2: tag("Pager"); break; 923 case 3: tag("Jacket"); break; 924 case 4: tag("Helmet"); break; 925 case 5: tag("Glasses"); break; 926 default: tag("Wearable"); break; 927 } 928 break; 929 930 case 8: /* Toy */ 931 switch (minor) { 932 case 1: tag("Robot"); break; 933 case 2: tag("Vehicle"); break; 934 case 3: tag("Doll / Action Figure"); break; 935 case 4: tag("Controller"); break; 936 case 5: tag("Game"); break; 937 default: tag("Toy"); break; 938 } 939 break; 940 941 default: 942 break; 943 } 944 945 if (class & 0x002000) tag("<Limited Discoverable>"); 946 if (class & 0x010000) tag("<Positioning>"); 947 if (class & 0x020000) tag("<Networking>"); 948 if (class & 0x040000) tag("<Rendering>"); 949 if (class & 0x080000) tag("<Capturing>"); 950 if (class & 0x100000) tag("<Object Transfer>"); 951 if (class & 0x200000) tag("<Audio>"); 952 if (class & 0x400000) tag("<Telephony>"); 953 if (class & 0x800000) tag("<Information>"); 954 tag(NULL); 955 } 956 957 void 958 print_voice(int level) 959 { 960 printf("\tvoice: [0x%4.4x]\n", voice); 961 962 if (level == 0) 963 return; 964 965 printf("\t\tInput Coding: "); 966 switch ((voice & 0x0300) >> 8) { 967 case 0x00: printf("Linear PCM [%d-bit, pos %d]", 968 (voice & 0x0020 ? 16 : 8), 969 (voice & 0x001c) >> 2); break; 970 case 0x01: printf("u-Law"); break; 971 case 0x02: printf("A-Law"); break; 972 case 0x03: printf("unknown"); break; 973 } 974 975 switch ((voice & 0x00c0) >> 6) { 976 case 0x00: printf(", 1's complement"); break; 977 case 0x01: printf(", 2's complement"); break; 978 case 0x02: printf(", sign magnitude"); break; 979 case 0x03: printf(", unsigned"); break; 980 } 981 982 printf("\n\t\tAir Coding: "); 983 switch (voice & 0x0003) { 984 case 0x00: printf("CVSD"); break; 985 case 0x01: printf("u-Law"); break; 986 case 0x02: printf("A-Law"); break; 987 case 0x03: printf("Transparent"); break; 988 } 989 990 printf("\n"); 991 } 992 993 void 994 print_result(int num, struct result *r, int rssi) 995 { 996 hci_remote_name_req_cp ncp; 997 hci_remote_name_req_compl_ep nep; 998 #if 0 999 hci_read_remote_features_cp fcp; 1000 hci_read_remote_features_compl_ep fep; 1001 #endif 1002 struct hostent *hp; 1003 1004 printf("%3d: bdaddr %s", 1005 num, 1006 bt_ntoa(&r->bdaddr, NULL)); 1007 1008 hp = bt_gethostbyaddr((const char *)&r->bdaddr, sizeof(bdaddr_t), AF_BLUETOOTH); 1009 if (hp != NULL) 1010 printf(" (%s)", hp->h_name); 1011 1012 printf("\n"); 1013 1014 memset(&ncp, 0, sizeof(ncp)); 1015 bdaddr_copy(&ncp.bdaddr, &r->bdaddr); 1016 ncp.page_scan_rep_mode = r->page_scan_rep_mode; 1017 ncp.clock_offset = r->clock_offset; 1018 1019 hci_req(HCI_CMD_REMOTE_NAME_REQ, 1020 HCI_EVENT_REMOTE_NAME_REQ_COMPL, 1021 &ncp, sizeof(ncp), 1022 &nep, sizeof(nep)); 1023 1024 printf(" : name \"%s\"\n", nep.name); 1025 1026 class = (r->uclass[2] << 16) | (r->uclass[1] << 8) | (r->uclass[0]); 1027 print_class(" : "); 1028 1029 #if 0 1030 hci_req(HCI_CMD_READ_REMOTE_FEATURES, 1031 HCI_EVENT_READ_REMOTE_FEATURES_COMPL, 1032 &fcp, sizeof(fcp), 1033 &fep, sizeof(fep)); 1034 1035 print_features(" : features", fep.features); 1036 #endif 1037 1038 printf(" : page scan rep mode 0x%02x\n", r->page_scan_rep_mode); 1039 printf(" : clock offset %d\n", le16toh(r->clock_offset)); 1040 1041 if (rssi) 1042 printf(" : rssi %d\n", r->rssi); 1043 1044 printf("\n"); 1045 } 1046 1047 void 1048 do_inquiry(void) 1049 { 1050 uint8_t buf[HCI_EVENT_PKT_SIZE]; 1051 struct result result[INQUIRY_MAX_RESPONSES]; 1052 hci_inquiry_cp inq; 1053 struct hci_filter f; 1054 hci_event_hdr_t *hh; 1055 int i, j, num, rssi; 1056 1057 if (opt_inquiry == 0) 1058 return; 1059 1060 printf("Device Discovery from device: %s ...", btr.btr_name); 1061 fflush(stdout); 1062 1063 memset(&f, 0, sizeof(f)); 1064 hci_filter_set(HCI_EVENT_COMMAND_STATUS, &f); 1065 hci_filter_set(HCI_EVENT_COMMAND_COMPL, &f); 1066 hci_filter_set(HCI_EVENT_INQUIRY_RESULT, &f); 1067 hci_filter_set(HCI_EVENT_RSSI_RESULT, &f); 1068 hci_filter_set(HCI_EVENT_INQUIRY_COMPL, &f); 1069 hci_filter_set(HCI_EVENT_REMOTE_NAME_REQ_COMPL, &f); 1070 hci_filter_set(HCI_EVENT_READ_REMOTE_FEATURES_COMPL, &f); 1071 if (setsockopt(hci, BTPROTO_HCI, SO_HCI_EVT_FILTER, &f, sizeof(f)) < 0) 1072 err(EXIT_FAILURE, "Can't set event filter"); 1073 1074 /* General Inquiry LAP is 0x9e8b33 */ 1075 inq.lap[0] = 0x33; 1076 inq.lap[1] = 0x8b; 1077 inq.lap[2] = 0x9e; 1078 inq.inquiry_length = INQUIRY_LENGTH; 1079 inq.num_responses = INQUIRY_MAX_RESPONSES; 1080 1081 hci_cmd(HCI_CMD_INQUIRY, &inq, sizeof(inq)); 1082 1083 num = 0; 1084 rssi = 0; 1085 hh = (hci_event_hdr_t *)buf; 1086 1087 for (;;) { 1088 if (recv(hci, buf, sizeof(buf), 0) <= 0) 1089 err(EXIT_FAILURE, "recv"); 1090 1091 if (hh->event == HCI_EVENT_INQUIRY_COMPL) 1092 break; 1093 1094 if (hh->event == HCI_EVENT_INQUIRY_RESULT) { 1095 hci_inquiry_result_ep *ep = (hci_inquiry_result_ep *)(hh + 1); 1096 hci_inquiry_response *ir = (hci_inquiry_response *)(ep + 1); 1097 1098 for (i = 0 ; i < ep->num_responses ; i++) { 1099 if (num == INQUIRY_MAX_RESPONSES) 1100 break; 1101 1102 /* some devices keep responding, ignore dupes */ 1103 for (j = 0 ; j < num ; j++) 1104 if (bdaddr_same(&result[j].bdaddr, &ir[i].bdaddr)) 1105 break; 1106 1107 if (j < num) 1108 continue; 1109 1110 bdaddr_copy(&result[num].bdaddr, &ir[i].bdaddr); 1111 memcpy(&result[num].uclass, &ir[i].uclass, HCI_CLASS_SIZE); 1112 result[num].page_scan_rep_mode = ir[i].page_scan_rep_mode; 1113 result[num].clock_offset = ir[i].clock_offset; 1114 result[num].rssi = 0; 1115 num++; 1116 printf("."); 1117 fflush(stdout); 1118 } 1119 continue; 1120 } 1121 1122 if (hh->event == HCI_EVENT_RSSI_RESULT) { 1123 hci_rssi_result_ep *ep = (hci_rssi_result_ep *)(hh + 1); 1124 hci_rssi_response *rr = (hci_rssi_response *)(ep + 1); 1125 1126 for (i = 0 ; i < ep->num_responses ; i++) { 1127 if (num == INQUIRY_MAX_RESPONSES) 1128 break; 1129 1130 /* some devices keep responding, ignore dupes */ 1131 for (j = 0 ; j < num ; j++) 1132 if (bdaddr_same(&result[j].bdaddr, &rr[i].bdaddr)) 1133 break; 1134 1135 if (j < num) 1136 continue; 1137 1138 bdaddr_copy(&result[num].bdaddr, &rr[i].bdaddr); 1139 memcpy(&result[num].uclass, &rr[i].uclass, HCI_CLASS_SIZE); 1140 result[num].page_scan_rep_mode = rr[i].page_scan_rep_mode; 1141 result[num].clock_offset = rr[i].clock_offset; 1142 result[num].rssi = rr[i].rssi; 1143 rssi = 1; 1144 num++; 1145 printf("."); 1146 fflush(stdout); 1147 } 1148 continue; 1149 } 1150 } 1151 1152 printf(" %d response%s\n", num, (num == 1 ? "" : "s")); 1153 1154 for (i = 0 ; i < num ; i++) 1155 print_result(i + 1, &result[i], rssi); 1156 } 1157