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