1 /* $NetBSD: btconfig.c,v 1.28 2017/12/21 09:31:22 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.28 2017/12/21 09:31:22 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 <time.h>
49 #include <unistd.h>
50 #include <util.h>
51
52 __dead static void badarg(const char *);
53 __dead static void badparam(const char *);
54 __dead static void badval(const char *, const char *);
55 __dead static void usage(void);
56 static int set_unit(unsigned long);
57 static void config_unit(void);
58 static void print_val(const char *, const char **, int);
59 static void print_info(int);
60 static void print_stats(void);
61 static void print_class(const char *, uint8_t *);
62 static void print_class0(void);
63 static void print_voice(int);
64 static void tag(const char *);
65 static void print_features0(uint8_t *);
66 static void print_features1(uint8_t *);
67 static void print_features2(uint8_t *);
68 static void print_result(int, struct bt_devinquiry *);
69 static void do_inquiry(void);
70
71 static void hci_req(uint16_t, uint8_t , void *, size_t, void *, size_t);
72 static void save_value(uint16_t, void *, size_t);
73 static void load_value(uint16_t, void *, size_t);
74
75 #define MAX_STR_SIZE 0xff
76
77 /* print width */
78 static int width = 0;
79 #define MAX_WIDTH 70
80
81 /* global variables */
82 static int hci;
83 static struct btreq btr;
84
85 /* command line flags */
86 static int verbose = 0; /* more info */
87 static int lflag = 0; /* list devices */
88 static int sflag = 0; /* get/zero stats */
89
90 /* device up/down (flag) */
91 static int opt_enable = 0;
92 static 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 static int opt_auth = 0;
109
110 /* encryption (flag) */
111 static int opt_encrypt = 0;
112
113 /* scan enable options (flags) */
114 static int opt_pscan = 0;
115 static int opt_iscan = 0;
116
117 /* master role option */
118 static int opt_master = 0;
119
120 /* link policy options (flags) */
121 static int opt_switch = 0;
122 static int opt_hold = 0;
123 static int opt_sniff = 0;
124 static int opt_park = 0;
125
126 /* class of device (hex value) */
127 static int opt_class = 0;
128 static uint32_t class;
129
130 /* packet type mask (hex value) */
131 static int opt_ptype = 0;
132 static uint32_t ptype;
133
134 /* unit name (string) */
135 static int opt_name = 0;
136 static char name[MAX_STR_SIZE];
137
138 /* pin type */
139 static int opt_pin = 0;
140
141 /* Inquiry */
142 static int opt_rssi = 0; /* inquiry_with_rssi (obsolete flag) */
143 static int opt_imode = 0; /* inquiry mode */
144 static int opt_inquiry = 0;
145 #define INQUIRY_LENGTH 10 /* seconds */
146 #define INQUIRY_MAX_RESPONSES 10
147 static const char *imodes[] = { "std", "rssi", "ext", NULL };
148
149 /* Voice Settings */
150 static int opt_voice = 0;
151 static uint32_t voice;
152
153 /* Page Timeout */
154 static int opt_pto = 0;
155 static uint32_t pto;
156
157 /* set SCO mtu */
158 static int opt_scomtu;
159 static uint32_t scomtu;
160
161 static 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
main(int ac,char * av[])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 static void
badparam(const char * param)327 badparam(const char *param)
328 {
329
330 fprintf(stderr, "unknown parameter '%s'\n", param);
331 exit(EXIT_FAILURE);
332 }
333
334 static void
badarg(const char * param)335 badarg(const char *param)
336 {
337
338 fprintf(stderr, "parameter '%s' needs argument\n", param);
339 exit(EXIT_FAILURE);
340 }
341
342 static void
badval(const char * param,const char * value)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 static void
usage(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 static void
tag(const char * f)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 static void
hci_req(uint16_t opcode,uint8_t event,void * cbuf,size_t clen,void * rbuf,size_t rlen)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 static void
save_value(uint16_t opcode,void * cbuf,size_t clen)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 static void
load_value(uint16_t opcode,void * rbuf,size_t rlen)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 static int
set_unit(unsigned long cmd)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 static void
config_unit(void)461 config_unit(void)
462 {
463
464 if (opt_enable) {
465 if (opt_enable > 0)
466 btr.btr_flags |= BTF_UP;
467 else
468 btr.btr_flags &= ~BTF_UP;
469
470 if (ioctl(hci, SIOCSBTFLAGS, &btr) < 0)
471 err(EXIT_FAILURE, "SIOCSBTFLAGS");
472
473 if (set_unit(SIOCGBTINFO) < 0)
474 err(EXIT_FAILURE, "%s", btr.btr_name);
475 }
476
477 if (opt_reset) {
478 static const struct timespec ts = { 0, 100000000 }; /* 100ms */
479
480 btr.btr_flags |= BTF_INIT;
481 if (ioctl(hci, SIOCSBTFLAGS, &btr) < 0)
482 err(EXIT_FAILURE, "SIOCSBTFLAGS");
483
484 hci_req(HCI_CMD_RESET, 0, NULL, 0, NULL, 0);
485
486 do {
487 nanosleep(&ts, NULL);
488 if (ioctl(hci, SIOCGBTINFO, &btr) < 0)
489 err(EXIT_FAILURE, "%s", btr.btr_name);
490 } while ((btr.btr_flags & BTF_INIT) != 0);
491 }
492
493 if (opt_master) {
494 if (opt_master > 0)
495 btr.btr_flags |= BTF_MASTER;
496 else
497 btr.btr_flags &= ~BTF_MASTER;
498
499 if (ioctl(hci, SIOCSBTFLAGS, &btr) < 0)
500 err(EXIT_FAILURE, "SIOCSBTFLAGS");
501 }
502
503 if (opt_switch || opt_hold || opt_sniff || opt_park) {
504 uint16_t val = btr.btr_link_policy;
505
506 if (opt_switch > 0) val |= HCI_LINK_POLICY_ENABLE_ROLE_SWITCH;
507 if (opt_switch < 0) val &= ~HCI_LINK_POLICY_ENABLE_ROLE_SWITCH;
508 if (opt_hold > 0) val |= HCI_LINK_POLICY_ENABLE_HOLD_MODE;
509 if (opt_hold < 0) val &= ~HCI_LINK_POLICY_ENABLE_HOLD_MODE;
510 if (opt_sniff > 0) val |= HCI_LINK_POLICY_ENABLE_SNIFF_MODE;
511 if (opt_sniff < 0) val &= ~HCI_LINK_POLICY_ENABLE_SNIFF_MODE;
512 if (opt_park > 0) val |= HCI_LINK_POLICY_ENABLE_PARK_MODE;
513 if (opt_park < 0) val &= ~HCI_LINK_POLICY_ENABLE_PARK_MODE;
514
515 btr.btr_link_policy = val;
516 if (ioctl(hci, SIOCSBTPOLICY, &btr) < 0)
517 err(EXIT_FAILURE, "SIOCSBTPOLICY");
518 }
519
520 if (opt_ptype) {
521 btr.btr_packet_type = ptype;
522 if (ioctl(hci, SIOCSBTPTYPE, &btr) < 0)
523 err(EXIT_FAILURE, "SIOCSBTPTYPE");
524 }
525
526 if (opt_pscan || opt_iscan) {
527 uint8_t val;
528
529 load_value(HCI_CMD_READ_SCAN_ENABLE, &val, sizeof(val));
530 if (opt_pscan > 0) val |= HCI_PAGE_SCAN_ENABLE;
531 if (opt_pscan < 0) val &= ~HCI_PAGE_SCAN_ENABLE;
532 if (opt_iscan > 0) val |= HCI_INQUIRY_SCAN_ENABLE;
533 if (opt_iscan < 0) val &= ~HCI_INQUIRY_SCAN_ENABLE;
534 save_value(HCI_CMD_WRITE_SCAN_ENABLE, &val, sizeof(val));
535 }
536
537 if (opt_auth) {
538 uint8_t val = (opt_auth > 0 ? 1 : 0);
539
540 save_value(HCI_CMD_WRITE_AUTH_ENABLE, &val, sizeof(val));
541 }
542
543 if (opt_encrypt) {
544 uint8_t val = (opt_encrypt > 0 ? 1 : 0);
545
546 save_value(HCI_CMD_WRITE_ENCRYPTION_MODE, &val, sizeof(val));
547 }
548
549 if (opt_name)
550 save_value(HCI_CMD_WRITE_LOCAL_NAME, name, HCI_UNIT_NAME_SIZE);
551
552 if (opt_class) {
553 uint8_t val[HCI_CLASS_SIZE];
554
555 val[0] = (class >> 0) & 0xff;
556 val[1] = (class >> 8) & 0xff;
557 val[2] = (class >> 16) & 0xff;
558
559 save_value(HCI_CMD_WRITE_UNIT_CLASS, val, HCI_CLASS_SIZE);
560 }
561
562 if (opt_pin) {
563 uint8_t val;
564
565 if (opt_pin > 0) val = 1;
566 else val = 0;
567
568 save_value(HCI_CMD_WRITE_PIN_TYPE, &val, sizeof(val));
569 }
570
571 if (opt_voice) {
572 uint16_t val;
573
574 val = htole16(voice & 0x03ff);
575 save_value(HCI_CMD_WRITE_VOICE_SETTING, &val, sizeof(val));
576 }
577
578 if (opt_pto) {
579 uint16_t val;
580
581 val = htole16(pto * 8 / 5);
582 save_value(HCI_CMD_WRITE_PAGE_TIMEOUT, &val, sizeof(val));
583 }
584
585 if (opt_scomtu) {
586 if (scomtu > 0xff) {
587 warnx("Invalid SCO mtu %d", scomtu);
588 } else {
589 btr.btr_sco_mtu = scomtu;
590
591 if (ioctl(hci, SIOCSBTSCOMTU, &btr) < 0)
592 warn("SIOCSBTSCOMTU");
593 }
594 }
595
596 if (opt_imode | opt_rssi) {
597 uint8_t val = (opt_rssi > 0 ? 1 : 0);
598
599 if (opt_imode)
600 val = opt_imode - 1;
601
602 save_value(HCI_CMD_WRITE_INQUIRY_MODE, &val, sizeof(val));
603 }
604 }
605
606 /*
607 * print value from NULL terminated array given index
608 */
609 static void
print_val(const char * hdr,const char ** argv,int idx)610 print_val(const char *hdr, const char **argv, int idx)
611 {
612 int i = 0;
613
614 while (i < idx && *argv != NULL)
615 i++, argv++;
616
617 printf("\t%s: %s\n", hdr, *argv == NULL ? "unknown" : *argv);
618 }
619
620 /*
621 * Print info for Bluetooth Device with varying verbosity levels
622 */
623 static void
print_info(int level)624 print_info(int level)
625 {
626 uint8_t version, val, buf[MAX_STR_SIZE];
627
628 if (lflag) {
629 tag(btr.btr_name);
630 return;
631 }
632
633 if (level-- < 1)
634 return;
635
636 snprintb((char *)buf, MAX_STR_SIZE, FLAGS_FMT, btr.btr_flags);
637
638 printf("%s: bdaddr %s flags %s\n", btr.btr_name,
639 bt_ntoa(&btr.btr_bdaddr, NULL), buf);
640
641 if (level-- < 1)
642 return;
643
644 printf("\tnum_cmd = %d\n"
645 "\tnum_acl = %d (max %d), acl_mtu = %d\n"
646 "\tnum_sco = %d (max %d), sco_mtu = %d\n",
647 btr.btr_num_cmd,
648 btr.btr_num_acl, btr.btr_max_acl, btr.btr_acl_mtu,
649 btr.btr_num_sco, btr.btr_max_sco, btr.btr_sco_mtu);
650
651 if (level-- < 1 || (btr.btr_flags & BTF_UP) == 0)
652 return;
653
654 load_value(HCI_CMD_READ_LOCAL_VER, buf, 3);
655 printf("\tHCI version: ");
656 switch((version = buf[0])) {
657 case HCI_SPEC_V10: printf("1.0b"); break;
658 case HCI_SPEC_V11: printf("1.1"); break;
659 case HCI_SPEC_V12: printf("1.2"); break;
660 case HCI_SPEC_V20: printf("2.0 + EDR"); break;
661 case HCI_SPEC_V21: printf("2.1 + EDR"); break;
662 case HCI_SPEC_V30: printf("3.0 + HS"); break;
663 case HCI_SPEC_V40: printf("4.0"); break;
664 case HCI_SPEC_V41: printf("4.1"); break;
665 case HCI_SPEC_V42: printf("4.2"); break;
666 case HCI_SPEC_V50: printf("5.0"); break;
667 default: printf("[%d]", version);break;
668 }
669 printf(" rev 0x%04x\n", le16dec(&buf[1]));
670
671 load_value(HCI_CMD_READ_UNIT_CLASS, buf, HCI_CLASS_SIZE);
672 print_class("\tclass:", buf);
673
674 load_value(HCI_CMD_READ_LOCAL_NAME, buf, HCI_UNIT_NAME_SIZE);
675 printf("\tname: \"%s\"\n", buf);
676
677 load_value(HCI_CMD_READ_VOICE_SETTING, buf, sizeof(uint16_t));
678 voice = (buf[1] << 8) | buf[0];
679 print_voice(level);
680
681 load_value(HCI_CMD_READ_PIN_TYPE, &val, sizeof(val));
682 printf("\tpin: %s\n", val ? "fixed" : "variable");
683
684 val = 0;
685 if (version >= HCI_SPEC_V12)
686 load_value(HCI_CMD_READ_INQUIRY_MODE, &val, sizeof(val));
687
688 print_val("inquiry mode", imodes, val);
689
690 width = printf("\toptions:");
691
692 load_value(HCI_CMD_READ_SCAN_ENABLE, &val, sizeof(val));
693 if (val & HCI_INQUIRY_SCAN_ENABLE) tag("iscan");
694 else if (level > 0) tag("-iscan");
695
696 if (val & HCI_PAGE_SCAN_ENABLE) tag("pscan");
697 else if (level > 0) tag("-pscan");
698
699 load_value(HCI_CMD_READ_AUTH_ENABLE, &val, sizeof(val));
700 if (val) tag("auth");
701 else if (level > 0) tag("-auth");
702
703 load_value(HCI_CMD_READ_ENCRYPTION_MODE, &val, sizeof(val));
704 if (val) tag("encrypt");
705 else if (level > 0) tag("-encrypt");
706
707 val = btr.btr_link_policy;
708 if (val & HCI_LINK_POLICY_ENABLE_ROLE_SWITCH) tag("switch");
709 else if (level > 0) tag("-switch");
710 if (val & HCI_LINK_POLICY_ENABLE_HOLD_MODE) tag("hold");
711 else if (level > 0) tag("-hold");
712 if (val & HCI_LINK_POLICY_ENABLE_SNIFF_MODE) tag("sniff");
713 else if (level > 0) tag("-sniff");
714 if (val & HCI_LINK_POLICY_ENABLE_PARK_MODE) tag("park");
715 else if (level > 0) tag("-park");
716
717 tag(NULL);
718
719 if (level-- < 1)
720 return;
721
722 ptype = btr.btr_packet_type;
723 width = printf("\tptype: [0x%04x]", ptype);
724 if (ptype & HCI_PKT_DM1) tag("DM1");
725 if (ptype & HCI_PKT_DH1) tag("DH1");
726 if (ptype & HCI_PKT_DM3) tag("DM3");
727 if (ptype & HCI_PKT_DH3) tag("DH3");
728 if (ptype & HCI_PKT_DM5) tag("DM5");
729 if (ptype & HCI_PKT_DH5) tag("DH5");
730 if ((ptype & HCI_PKT_2MBPS_DH1) == 0) tag("2-DH1");
731 if ((ptype & HCI_PKT_3MBPS_DH1) == 0) tag("3-DH1");
732 if ((ptype & HCI_PKT_2MBPS_DH3) == 0) tag("2-DH3");
733 if ((ptype & HCI_PKT_3MBPS_DH3) == 0) tag("3-DH3");
734 if ((ptype & HCI_PKT_2MBPS_DH5) == 0) tag("2-DH5");
735 if ((ptype & HCI_PKT_3MBPS_DH5) == 0) tag("3-DH5");
736 tag(NULL);
737
738 load_value(HCI_CMD_READ_PAGE_TIMEOUT, buf, sizeof(uint16_t));
739 printf("\tpage timeout: %d ms\n", le16dec(buf) * 5 / 8);
740
741 if (level-- < 1)
742 return;
743
744 if (ioctl(hci, SIOCGBTFEAT, &btr) < 0)
745 err(EXIT_FAILURE, "SIOCGBTFEAT");
746
747 width = printf("\tfeatures:");
748 print_features0(btr.btr_features0);
749 print_features1(btr.btr_features1);
750 print_features2(btr.btr_features2);
751 tag(NULL);
752 }
753
754 static void
print_stats(void)755 print_stats(void)
756 {
757
758 if (sflag == 0)
759 return;
760
761 if (sflag == 1) {
762 if (ioctl(hci, SIOCGBTSTATS, &btr) < 0)
763 err(EXIT_FAILURE, "SIOCGBTSTATS");
764 } else {
765 if (ioctl(hci, SIOCZBTSTATS, &btr) < 0)
766 err(EXIT_FAILURE, "SIOCZBTSTATS");
767 }
768
769 printf( "\tTotal bytes sent %d, received %d\n"
770 "\tCommands sent %d, Events received %d\n"
771 "\tACL data packets sent %d, received %d\n"
772 "\tSCO data packets sent %d, received %d\n"
773 "\tInput errors %d, Output errors %d\n",
774 btr.btr_stats.byte_tx, btr.btr_stats.byte_rx,
775 btr.btr_stats.cmd_tx, btr.btr_stats.evt_rx,
776 btr.btr_stats.acl_tx, btr.btr_stats.acl_rx,
777 btr.btr_stats.sco_tx, btr.btr_stats.sco_rx,
778 btr.btr_stats.err_rx, btr.btr_stats.err_tx);
779 }
780
781 static void
print_features0(uint8_t * f)782 print_features0(uint8_t *f)
783 {
784
785 /* ------------------- byte 0 --------------------*/
786 if (*f & HCI_LMP_3SLOT) tag("<3 slot>");
787 if (*f & HCI_LMP_5SLOT) tag("<5 slot>");
788 if (*f & HCI_LMP_ENCRYPTION) tag("<encryption>");
789 if (*f & HCI_LMP_SLOT_OFFSET) tag("<slot offset>");
790 if (*f & HCI_LMP_TIMIACCURACY) tag("<timing accuracy>");
791 if (*f & HCI_LMP_ROLE_SWITCH) tag("<role switch>");
792 if (*f & HCI_LMP_HOLD_MODE) tag("<hold mode>");
793 if (*f & HCI_LMP_SNIFF_MODE) tag("<sniff mode>");
794 f++;
795
796 /* ------------------- byte 1 --------------------*/
797 if (*f & HCI_LMP_PARK_MODE) tag("<park mode>");
798 if (*f & HCI_LMP_RSSI) tag("<RSSI>");
799 if (*f & HCI_LMP_CHANNEL_QUALITY) tag("<channel quality>");
800 if (*f & HCI_LMP_SCO_LINK) tag("<SCO link>");
801 if (*f & HCI_LMP_HV2_PKT) tag("<HV2>");
802 if (*f & HCI_LMP_HV3_PKT) tag("<HV3>");
803 if (*f & HCI_LMP_ULAW_LOG) tag("<u-Law log>");
804 if (*f & HCI_LMP_ALAW_LOG) tag("<A-Law log>");
805 f++;
806
807 /* ------------------- byte 1 --------------------*/
808 if (*f & HCI_LMP_CVSD) tag("<CVSD data>");
809 if (*f & HCI_LMP_PAGISCHEME) tag("<paging parameter>");
810 if (*f & HCI_LMP_POWER_CONTROL) tag("<power control>");
811 if (*f & HCI_LMP_TRANSPARENT_SCO) tag("<transparent SCO>");
812 if (*f & HCI_LMP_FLOW_CONTROL_LAG0) tag("<flow control lag lsb>");
813 if (*f & HCI_LMP_FLOW_CONTROL_LAG1) tag("<flow control lag mb>");
814 if (*f & HCI_LMP_FLOW_CONTROL_LAG2) tag("<flow control lag msb>");
815 if (*f & HCI_LMP_BC_ENCRYPTION) tag("<broadcast encryption>");
816 f++;
817
818 /* ------------------- byte 3 --------------------*/
819 if (*f & HCI_LMP_EDR_ACL_2MBPS) tag("<EDR ACL 2Mbps>");
820 if (*f & HCI_LMP_EDR_ACL_3MBPS) tag("<EDR ACL 3Mbps>");
821 if (*f & HCI_LMP_ENHANCED_ISCAN) tag("<enhanced inquiry scan>");
822 if (*f & HCI_LMP_INTERLACED_ISCAN) tag("<interlaced inquiry scan>");
823 if (*f & HCI_LMP_INTERLACED_PSCAN) tag("<interlaced page scan>");
824 if (*f & HCI_LMP_RSSI_INQUIRY) tag("<RSSI with inquiry result>");
825 if (*f & HCI_LMP_EV3_PKT) tag("<EV3 packets>");
826 f++;
827
828 /* ------------------- byte 4 --------------------*/
829 if (*f & HCI_LMP_EV4_PKT) tag("<EV4 packets>");
830 if (*f & HCI_LMP_EV5_PKT) tag("<EV5 packets>");
831 if (*f & HCI_LMP_AFH_CAPABLE_SLAVE) tag("<AFH capable slave>");
832 if (*f & HCI_LMP_AFH_CLASS_SLAVE) tag("<AFH class slave>");
833 if (*f & HCI_LMP_BR_EDR_UNSUPPORTED)tag("<BR/EDR not supported>");
834 if (*f & HCI_LMP_LE_CONTROLLER) tag("<LE (controller)>");
835 if (*f & HCI_LMP_3SLOT_EDR_ACL) tag("<3 slot EDR ACL>");
836 f++;
837
838 /* ------------------- byte 5 --------------------*/
839 if (*f & HCI_LMP_5SLOT_EDR_ACL) tag("<5 slot EDR ACL>");
840 if (*f & HCI_LMP_SNIFF_SUBRATING) tag("<sniff subrating>");
841 if (*f & HCI_LMP_PAUSE_ENCRYPTION) tag("<pause encryption>");
842 if (*f & HCI_LMP_AFH_CAPABLE_MASTER)tag("<AFH capable master>");
843 if (*f & HCI_LMP_AFH_CLASS_MASTER) tag("<AFH class master>");
844 if (*f & HCI_LMP_EDR_eSCO_2MBPS) tag("<EDR eSCO 2Mbps>");
845 if (*f & HCI_LMP_EDR_eSCO_3MBPS) tag("<EDR eSCO 3Mbps>");
846 if (*f & HCI_LMP_3SLOT_EDR_eSCO) tag("<3 slot EDR eSCO>");
847 f++;
848
849 /* ------------------- byte 6 --------------------*/
850 if (*f & HCI_LMP_EXTENDED_INQUIRY) tag("<extended inquiry>");
851 if (*f & HCI_LMP_LE_BR_EDR_CONTROLLER)tag("<simultaneous LE & BR/EDR (controller)>");
852 if (*f & HCI_LMP_SIMPLE_PAIRING) tag("<secure simple pairing>");
853 if (*f & HCI_LMP_ENCAPSULATED_PDU) tag("<encapsulated PDU>");
854 if (*f & HCI_LMP_ERRDATA_REPORTING) tag("<errdata reporting>");
855 if (*f & HCI_LMP_NOFLUSH_PB_FLAG) tag("<no flush PB flag>");
856 f++;
857
858 /* ------------------- byte 7 --------------------*/
859 if (*f & HCI_LMP_LINK_SUPERVISION_TO)tag("<link supervision timeout changed>");
860 if (*f & HCI_LMP_INQ_RSP_TX_POWER) tag("<inquiry rsp TX power level>");
861 if (*f & HCI_LMP_ENHANCED_POWER_CONTROL)tag("<enhanced power control>");
862 if (*f & HCI_LMP_EXTENDED_FEATURES) tag("<extended features>");
863 }
864
865 static void
print_features1(uint8_t * f)866 print_features1(uint8_t *f)
867 {
868
869 /* ------------------- byte 0 --------------------*/
870 if (*f & HCI_LMP_SSP) tag("<secure simple pairing (host)>");
871 if (*f & HCI_LMP_LE_HOST) tag("<LE (host)>");
872 if (*f & HCI_LMP_LE_BR_EDR_HOST) tag("<simultaneous LE & BR/EDR (host)>");
873 if (*f & HCI_LMP_SECURE_CONN_HOST) tag("<secure connections (host)>");
874 }
875
876 static void
print_features2(uint8_t * f)877 print_features2(uint8_t *f)
878 {
879 /* ------------------- byte 0 --------------------*/
880 if (*f & HCI_LMP_CONNLESS_MASTER) tag("<connectionless master>");
881 if (*f & HCI_LMP_CONNLESS_SLAVE) tag("<connectionless slave>");
882 if (*f & HCI_LMP_SYNC_TRAIN) tag("<synchronization train>");
883 if (*f & HCI_LMP_SYNC_SCAN) tag("<synchronization scan>");
884 if (*f & HCI_LMP_INQ_RSP_NOTIFY) tag("<inquiry response notification>");
885 if (*f & HCI_LMP_INTERLACE_SCAN) tag("<generalized interlace scan>");
886 if (*f & HCI_LMP_COARSE_CLOCK) tag("<coarse clock adjustment>");
887
888 /* ------------------- byte 1 --------------------*/
889 if (*f & HCI_LMP_SECURE_CONN_CONTROLLER)tag("<secure connections (controller)>");
890 if (*f & HCI_LMP_PING) tag("<ping>");
891 if (*f & HCI_LMP_TRAIN_NUDGING) tag("<train nudging>");
892 }
893
894 static void
print_class(const char * str,uint8_t * uclass)895 print_class(const char *str, uint8_t *uclass)
896 {
897
898 class = (uclass[2] << 16) | (uclass[1] << 8) | uclass[0];
899 width = printf("%s [0x%06x]", str, class);
900
901 switch(__SHIFTOUT(class, __BITS(0, 1))) {
902 case 0: print_class0(); break;
903 default: break;
904 }
905
906 tag(NULL);
907 }
908
909 static void
print_class0(void)910 print_class0(void)
911 {
912
913 switch (__SHIFTOUT(class, __BITS(8, 12))) {
914 case 1: /* Computer */
915 switch (__SHIFTOUT(class, __BITS(2, 7))) {
916 case 1: tag("Desktop workstation"); break;
917 case 2: tag("Server-class computer"); break;
918 case 3: tag("Laptop"); break;
919 case 4: tag("Handheld PC/PDA"); break;
920 case 5: tag("Palm Sized PC/PDA"); break;
921 case 6: tag("Wearable computer"); break;
922 default: tag("Computer"); break;
923 }
924 break;
925
926 case 2: /* Phone */
927 switch (__SHIFTOUT(class, __BITS(2, 7))) {
928 case 1: tag("Cellular Phone"); break;
929 case 2: tag("Cordless Phone"); break;
930 case 3: tag("Smart Phone"); break;
931 case 4: tag("Wired Modem/Phone Gateway"); break;
932 case 5: tag("Common ISDN"); break;
933 default:tag("Phone"); break;
934 }
935 break;
936
937 case 3: /* LAN */
938 tag("LAN");
939 switch (__SHIFTOUT(class, __BITS(5, 7))) {
940 case 0: tag("[Fully available]"); break;
941 case 1: tag("[1-17% utilised]"); break;
942 case 2: tag("[17-33% utilised]"); break;
943 case 3: tag("[33-50% utilised]"); break;
944 case 4: tag("[50-67% utilised]"); break;
945 case 5: tag("[67-83% utilised]"); break;
946 case 6: tag("[83-99% utilised]"); break;
947 case 7: tag("[No service available]"); break;
948 }
949 break;
950
951 case 4: /* Audio/Visual */
952 switch (__SHIFTOUT(class, __BITS(2, 7))) {
953 case 1: tag("Wearable Headset"); break;
954 case 2: tag("Hands-free Audio"); break;
955 case 4: tag("Microphone"); break;
956 case 5: tag("Loudspeaker"); break;
957 case 6: tag("Headphones"); break;
958 case 7: tag("Portable Audio"); break;
959 case 8: tag("Car Audio"); break;
960 case 9: tag("Set-top Box"); break;
961 case 10: tag("HiFi Audio"); break;
962 case 11: tag("VCR"); break;
963 case 12: tag("Video Camera"); break;
964 case 13: tag("Camcorder"); break;
965 case 14: tag("Video Monitor"); break;
966 case 15: tag("Video Display and Loudspeaker"); break;
967 case 16: tag("Video Conferencing"); break;
968 case 18: tag("A/V [Gaming/Toy]"); break;
969 default: tag("Audio/Visual"); break;
970 }
971 break;
972
973 case 5: /* Peripheral */
974 switch (__SHIFTOUT(class, __BITS(2, 5))) {
975 case 1: tag("Joystick"); break;
976 case 2: tag("Gamepad"); break;
977 case 3: tag("Remote Control"); break;
978 case 4: tag("Sensing Device"); break;
979 case 5: tag("Digitiser Tablet"); break;
980 case 6: tag("Card Reader"); break;
981 default: tag("Peripheral"); break;
982 }
983
984 if (class & __BIT(6)) tag("Keyboard");
985 if (class & __BIT(7)) tag("Mouse");
986 break;
987
988 case 6: /* Imaging */
989 if (class & __BIT(4)) tag("Display");
990 if (class & __BIT(5)) tag("Camera");
991 if (class & __BIT(6)) tag("Scanner");
992 if (class & __BIT(7)) tag("Printer");
993 if ((class & __BITS(4, 7)) == 0) tag("Imaging");
994 break;
995
996 case 7: /* Wearable */
997 switch (__SHIFTOUT(class, __BITS(2, 7))) {
998 case 1: tag("Wrist Watch"); break;
999 case 2: tag("Pager"); break;
1000 case 3: tag("Jacket"); break;
1001 case 4: tag("Helmet"); break;
1002 case 5: tag("Glasses"); break;
1003 default: tag("Wearable"); break;
1004 }
1005 break;
1006
1007 case 8: /* Toy */
1008 switch (__SHIFTOUT(class, __BITS(2, 7))) {
1009 case 1: tag("Robot"); break;
1010 case 2: tag("Vehicle"); break;
1011 case 3: tag("Doll / Action Figure"); break;
1012 case 4: tag("Controller"); break;
1013 case 5: tag("Game"); break;
1014 default: tag("Toy"); break;
1015 }
1016 break;
1017
1018 case 9: /* Health */
1019 switch (__SHIFTOUT(class, __BITS(2, 7))) {
1020 case 1: tag("Blood Pressure Monitor"); break;
1021 case 2: tag("Thermometer"); break;
1022 case 3: tag("Weighing Scale"); break;
1023 case 4: tag("Glucose Meter"); break;
1024 case 5: tag("Pulse Oximeter"); break;
1025 case 6: tag("Heart/Pulse Rate Monitor"); break;
1026 case 7: tag("Health Data Display"); break;
1027 default: tag("Health"); break;
1028 }
1029 break;
1030
1031 default:
1032 break;
1033 }
1034
1035 if (class & __BIT(13)) tag("<Limited Discoverable>");
1036 if (class & __BIT(16)) tag("<Positioning>");
1037 if (class & __BIT(17)) tag("<Networking>");
1038 if (class & __BIT(18)) tag("<Rendering>");
1039 if (class & __BIT(19)) tag("<Capturing>");
1040 if (class & __BIT(20)) tag("<Object Transfer>");
1041 if (class & __BIT(21)) tag("<Audio>");
1042 if (class & __BIT(22)) tag("<Telephony>");
1043 if (class & __BIT(23)) tag("<Information>");
1044 }
1045
1046 static void
print_voice(int level)1047 print_voice(int level)
1048 {
1049 printf("\tvoice: [0x%4.4x]\n", voice);
1050
1051 if (level == 0)
1052 return;
1053
1054 printf("\t\tInput Coding: ");
1055 switch ((voice & 0x0300) >> 8) {
1056 case 0x00: printf("Linear PCM [%d-bit, pos %d]",
1057 (voice & 0x0020 ? 16 : 8),
1058 (voice & 0x001c) >> 2); break;
1059 case 0x01: printf("u-Law"); break;
1060 case 0x02: printf("A-Law"); break;
1061 case 0x03: printf("unknown"); break;
1062 }
1063
1064 switch ((voice & 0x00c0) >> 6) {
1065 case 0x00: printf(", 1's complement"); break;
1066 case 0x01: printf(", 2's complement"); break;
1067 case 0x02: printf(", sign magnitude"); break;
1068 case 0x03: printf(", unsigned"); break;
1069 }
1070
1071 printf("\n\t\tAir Coding: ");
1072 switch (voice & 0x0003) {
1073 case 0x00: printf("CVSD"); break;
1074 case 0x01: printf("u-Law"); break;
1075 case 0x02: printf("A-Law"); break;
1076 case 0x03: printf("Transparent"); break;
1077 }
1078
1079 printf("\n");
1080 }
1081
1082 static void
print_result(int num,struct bt_devinquiry * r)1083 print_result(int num, struct bt_devinquiry *r)
1084 {
1085 hci_remote_name_req_cp ncp;
1086 hci_remote_name_req_compl_ep nep;
1087 struct hostent *hp;
1088
1089 printf("%3d: bdaddr %s", num, bt_ntoa(&r->bdaddr, NULL));
1090
1091 hp = bt_gethostbyaddr((const char *)&r->bdaddr, sizeof(bdaddr_t), AF_BLUETOOTH);
1092 if (hp != NULL)
1093 printf(" (%s)", hp->h_name);
1094
1095 printf("\n");
1096
1097 memset(&ncp, 0, sizeof(ncp));
1098 bdaddr_copy(&ncp.bdaddr, &r->bdaddr);
1099 ncp.page_scan_rep_mode = r->pscan_rep_mode;
1100 ncp.clock_offset = r->clock_offset;
1101
1102 hci_req(HCI_CMD_REMOTE_NAME_REQ,
1103 HCI_EVENT_REMOTE_NAME_REQ_COMPL,
1104 &ncp, sizeof(ncp),
1105 &nep, sizeof(nep));
1106
1107 printf(" : name \"%s\"\n", nep.name);
1108 print_class(" : class", r->dev_class);
1109 printf(" : page scan rep mode 0x%02x\n", r->pscan_rep_mode);
1110 printf(" : clock offset %d\n", le16toh(r->clock_offset));
1111 printf(" : rssi %d\n", r->rssi);
1112 printf("\n");
1113 }
1114
1115 static void
do_inquiry(void)1116 do_inquiry(void)
1117 {
1118 struct bt_devinquiry *result;
1119 int i, num;
1120
1121 if (opt_inquiry == 0)
1122 return;
1123
1124 printf("Device Discovery from device: %s ...", btr.btr_name);
1125 fflush(stdout);
1126
1127 num = bt_devinquiry(btr.btr_name, INQUIRY_LENGTH,
1128 INQUIRY_MAX_RESPONSES, &result);
1129
1130 if (num == -1) {
1131 printf("failed\n");
1132 err(EXIT_FAILURE, "%s", btr.btr_name);
1133 }
1134
1135 printf(" %d response%s\n", num, (num == 1 ? "" : "s"));
1136
1137 for (i = 0 ; i < num ; i++)
1138 print_result(i + 1, &result[i]);
1139
1140 free(result);
1141 }
1142