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