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