btconfig.c revision 1.6 1 /* $NetBSD: btconfig.c,v 1.6 2007/09/07 18:40:01 plunky Exp $ */
2
3 /*-
4 * Copyright (c) 2006 Itronix Inc.
5 * All rights reserved.
6 *
7 * Written by Iain Hibbert for Itronix Inc.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of Itronix Inc. may not be used to endorse
18 * or promote products derived from this software without specific
19 * prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
25 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28 * ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #include <sys/cdefs.h>
35 __COPYRIGHT("@(#) Copyright (c) 2006 Itronix, Inc.\n"
36 "All rights reserved.\n");
37 __RCSID("$NetBSD: btconfig.c,v 1.6 2007/09/07 18:40:01 plunky Exp $");
38
39 #include <sys/types.h>
40 #include <sys/param.h>
41 #include <sys/socket.h>
42 #include <sys/ioctl.h>
43
44 #include <net/if.h>
45
46 #include <stdio.h>
47 #include <string.h>
48 #include <ctype.h>
49 #include <stdlib.h>
50 #include <util.h>
51 #include <unistd.h>
52 #include <errno.h>
53 #include <err.h>
54 #include <bluetooth.h>
55
56 /* inquiry results storage */
57 struct result {
58 bdaddr_t bdaddr;
59 uint8_t page_scan_rep_mode;
60 uint8_t uclass[HCI_CLASS_SIZE];
61 uint16_t clock_offset;
62 int8_t rssi;
63 };
64
65 int main(int, char *[]);
66 void badarg(const char *);
67 void badparam(const char *);
68 void usage(void);
69 int set_unit(unsigned long);
70 void config_unit(void);
71 void print_info(int);
72 void print_stats(void);
73 void print_class(const char *);
74 void print_voice(int);
75 void tag(const char *);
76 void print_features(const char *, uint8_t *);
77 void do_inquiry(void);
78 void print_result(int, struct result *, int);
79
80 void hci_req(uint16_t, uint8_t , void *, size_t, void *, size_t);
81 #define save_value(opcode, cbuf, clen) hci_req(opcode, 0, cbuf, clen, NULL, 0)
82 #define load_value(opcode, rbuf, rlen) hci_req(opcode, 0, NULL, 0, rbuf, rlen)
83 #define hci_cmd(opcode, cbuf, clen) hci_req(opcode, 0, cbuf, clen, NULL, 0)
84
85 #define MAX_STR_SIZE 0xff
86
87 /* print width */
88 int width = 0;
89 #define MAX_WIDTH 70
90
91 /* global variables */
92 int hci;
93 struct btreq btr;
94
95 /* command line flags */
96 int verbose = 0; /* more info */
97 int lflag = 0; /* list devices */
98 int sflag = 0; /* get/zero stats */
99
100 /* device up/down (flag) */
101 int opt_enable = 0;
102 int opt_reset = 0;
103 #define FLAGS_FMT "\20" \
104 "\001UP" \
105 "\002RUNNING" \
106 "\003XMIT_CMD" \
107 "\004XMIT_ACL" \
108 "\005XMIT_SCO" \
109 "\006INIT_BDADDR" \
110 "\007INIT_BUFFER_SIZE" \
111 "\010INIT_FEATURES"
112
113 /* authorisation (flag) */
114 int opt_auth = 0;
115
116 /* encryption (flag) */
117 int opt_encrypt = 0;
118
119 /* scan enable options (flags) */
120 int opt_pscan = 0;
121 int opt_iscan = 0;
122
123 /* link policy options (flags) */
124 int opt_switch = 0;
125 int opt_hold = 0;
126 int opt_sniff = 0;
127 int opt_park = 0;
128
129 /* class of device (hex value) */
130 int opt_class = 0;
131 uint32_t class;
132
133 /* packet type mask (hex value) */
134 int opt_ptype = 0;
135 uint32_t ptype;
136
137 /* unit name (string) */
138 int opt_name = 0;
139 char name[MAX_STR_SIZE];
140
141 /* pin type */
142 int opt_pin = 0;
143
144 /* Inquiry */
145 int opt_rssi = 0; /* inquiry_with_rssi (flag) */
146 int opt_inquiry = 0;
147 #define INQUIRY_LENGTH 10 /* about 12 seconds */
148 #define INQUIRY_MAX_RESPONSES 10
149
150 /* Voice Settings */
151 int opt_voice = 0;
152 uint32_t voice;
153
154 /* Page Timeout */
155 int opt_pto = 0;
156 uint32_t pto;
157
158 /* set SCO mtu */
159 int opt_scomtu;
160 uint32_t scomtu;
161
162 struct parameter {
163 const char *name;
164 enum { P_SET, P_CLR, P_STR, P_HEX, P_NUM } type;
165 int *opt;
166 void *val;
167 } parameters[] = {
168 { "up", P_SET, &opt_enable, NULL },
169 { "enable", P_SET, &opt_enable, NULL },
170 { "down", P_CLR, &opt_enable, NULL },
171 { "disable", P_CLR, &opt_enable, NULL },
172 { "name", P_STR, &opt_name, name },
173 { "pscan", P_SET, &opt_pscan, NULL },
174 { "-pscan", P_CLR, &opt_pscan, NULL },
175 { "iscan", P_SET, &opt_iscan, NULL },
176 { "-iscan", P_CLR, &opt_iscan, 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 { "rssi", P_SET, &opt_rssi, NULL },
196 { "-rssi", P_CLR, &opt_rssi, NULL },
197 { "reset", P_SET, &opt_reset, NULL },
198 { "voice", P_HEX, &opt_voice, &voice },
199 { "pto", P_NUM, &opt_pto, &pto },
200 { "scomtu", P_NUM, &opt_scomtu, &scomtu },
201 { NULL, 0, NULL, NULL }
202 };
203
204 int
205 main(int ac, char *av[])
206 {
207 int ch;
208 struct parameter *p;
209
210 while ((ch = getopt(ac, av, "hlsvz")) != -1) {
211 switch(ch) {
212 case 'l':
213 lflag = 1;
214 break;
215
216 case 's':
217 sflag = 1;
218 break;
219
220 case 'v':
221 verbose++;
222 break;
223
224 case 'z':
225 sflag = 2;
226 break;
227
228 case 'h':
229 default:
230 usage();
231 }
232 }
233 av += optind;
234 ac -= optind;
235
236 if (lflag && sflag)
237 usage();
238
239 hci = socket(PF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
240 if (hci == -1)
241 err(EXIT_FAILURE, "socket");
242
243 if (ac == 0) {
244 verbose++;
245
246 memset(&btr, 0, sizeof(btr));
247 while (set_unit(SIOCNBTINFO) != -1) {
248 print_info(verbose);
249 print_stats();
250 }
251
252 tag(NULL);
253 } else {
254 strlcpy(btr.btr_name, *av, HCI_DEVNAME_SIZE);
255 av++, ac--;
256
257 if (set_unit(SIOCGBTINFO) < 0)
258 err(EXIT_FAILURE, "%s", btr.btr_name);
259
260 if (ac == 0)
261 verbose += 2;
262
263 while (ac > 0) {
264 for (p = parameters ; ; p++) {
265 if (p->name == NULL)
266 badparam(*av);
267
268 if (strcmp(*av, p->name) == 0)
269 break;
270 }
271
272 switch(p->type) {
273 case P_SET:
274 *(p->opt) = 1;
275 break;
276
277 case P_CLR:
278 *(p->opt) = -1;
279 break;
280
281 case P_STR:
282 if (--ac < 1) badarg(p->name);
283 strlcpy((char *)(p->val), *++av, MAX_STR_SIZE);
284 *(p->opt) = 1;
285 break;
286
287 case P_HEX:
288 if (--ac < 1) badarg(p->name);
289 *(uint32_t *)(p->val) = strtoul(*++av, NULL, 16);
290 *(p->opt) = 1;
291 break;
292
293 case P_NUM:
294 if (--ac < 1) badarg(p->name);
295 *(uint32_t *)(p->val) = strtoul(*++av, NULL, 10);
296 *(p->opt) = 1;
297 break;
298 }
299
300 av++, ac--;
301 }
302
303 config_unit();
304 print_info(verbose);
305 print_stats();
306 do_inquiry();
307 }
308
309 close(hci);
310 return EXIT_SUCCESS;
311 }
312
313 void
314 badparam(const char *param)
315 {
316
317 fprintf(stderr, "unknown parameter '%s'\n", param);
318 exit(EXIT_FAILURE);
319 }
320
321 void
322 badarg(const char *param)
323 {
324
325 fprintf(stderr, "parameter '%s' needs argument\n", param);
326 exit(EXIT_FAILURE);
327 }
328
329 void
330 usage(void)
331 {
332
333 fprintf(stderr, "usage: %s [-svz] [device [parameters]]\n", getprogname());
334 fprintf(stderr, " %s -l\n", getprogname());
335 exit(EXIT_FAILURE);
336 }
337
338 /*
339 * pretty printing feature
340 */
341 void
342 tag(const char *f)
343 {
344
345 if (f == NULL) {
346 if (width > 0)
347 printf("\n");
348
349 width = 0;
350 } else {
351 width += printf("%*s%s",
352 (width == 0 ? (lflag ? 0 : 8) : 1),
353 "", f);
354
355 if (width > MAX_WIDTH) {
356 printf("\n");
357 width = 0;
358 }
359 }
360 }
361
362 /*
363 * basic HCI cmd request function with argument return.
364 *
365 * Normally, this will return on COMMAND_STATUS or COMMAND_COMPLETE for the given
366 * opcode, but if event is given then it will ignore COMMAND_STATUS (unless error)
367 * and wait for the specified event.
368 *
369 * if rbuf/rlen is given, results will be copied into the result buffer for
370 * COMMAND_COMPLETE/event responses.
371 */
372 void
373 hci_req(uint16_t opcode, uint8_t event, void *cbuf, size_t clen, void *rbuf, size_t rlen)
374 {
375 uint8_t msg[sizeof(hci_cmd_hdr_t) + HCI_CMD_PKT_SIZE];
376 hci_event_hdr_t *ep;
377 hci_cmd_hdr_t *cp;
378
379 cp = (hci_cmd_hdr_t *)msg;
380 cp->type = HCI_CMD_PKT;
381 cp->opcode = opcode = htole16(opcode);
382 cp->length = clen = MIN(clen, sizeof(msg) - sizeof(hci_cmd_hdr_t));
383
384 if (clen) memcpy((cp + 1), cbuf, clen);
385
386 if (send(hci, msg, sizeof(hci_cmd_hdr_t) + clen, 0) < 0)
387 err(EXIT_FAILURE, "HCI Send");
388
389 ep = (hci_event_hdr_t *)msg;
390 for(;;) {
391 if (recv(hci, msg, sizeof(msg), 0) < 0) {
392 if (errno == EAGAIN || errno == EINTR)
393 continue;
394
395 err(EXIT_FAILURE, "HCI Recv");
396 }
397
398 if (ep->event == HCI_EVENT_COMMAND_STATUS) {
399 hci_command_status_ep *cs;
400
401 cs = (hci_command_status_ep *)(ep + 1);
402 if (cs->opcode != opcode)
403 continue;
404
405 if (cs->status)
406 errx(EXIT_FAILURE,
407 "HCI cmd (%4.4x) failed (status %d)",
408 opcode, cs->status);
409
410 if (event == 0)
411 break;
412
413 continue;
414 }
415
416 if (ep->event == HCI_EVENT_COMMAND_COMPL) {
417 hci_command_compl_ep *cc;
418 uint8_t *ptr;
419
420 cc = (hci_command_compl_ep *)(ep + 1);
421 if (cc->opcode != opcode)
422 continue;
423
424 if (rbuf == NULL)
425 break;
426
427 ptr = (uint8_t *)(cc + 1);
428 if (*ptr)
429 errx(EXIT_FAILURE,
430 "HCI cmd (%4.4x) failed (status %d)",
431 opcode, *ptr);
432
433 memcpy(rbuf, ++ptr, rlen);
434 break;
435 }
436
437 if (ep->event == event) {
438 if (rbuf == NULL)
439 break;
440
441 memcpy(rbuf, (ep + 1), rlen);
442 break;
443 }
444 }
445 }
446
447 int
448 set_unit(unsigned long cmd)
449 {
450
451 if (ioctl(hci, cmd, &btr) == -1)
452 return -1;
453
454 if (btr.btr_flags & BTF_UP) {
455 struct sockaddr_bt sa;
456
457 sa.bt_len = sizeof(sa);
458 sa.bt_family = AF_BLUETOOTH;
459 bdaddr_copy(&sa.bt_bdaddr, &btr.btr_bdaddr);
460
461 if (bind(hci, (struct sockaddr *)&sa, sizeof(sa)) < 0)
462 err(EXIT_FAILURE, "bind");
463
464 if (connect(hci, (struct sockaddr *)&sa, sizeof(sa)) < 0)
465 err(EXIT_FAILURE, "connect");
466 }
467
468 return 0;
469 }
470
471 /*
472 * apply configuration parameters to unit
473 */
474 void
475 config_unit(void)
476 {
477
478 if (opt_enable) {
479 if (opt_enable > 0)
480 btr.btr_flags |= BTF_UP;
481 else
482 btr.btr_flags &= ~BTF_UP;
483
484 if (ioctl(hci, SIOCSBTFLAGS, &btr) < 0)
485 err(EXIT_FAILURE, "SIOCSBTFLAGS");
486
487 if (set_unit(SIOCGBTINFO) < 0)
488 err(EXIT_FAILURE, "%s", btr.btr_name);
489 }
490
491 if (opt_reset) {
492 hci_cmd(HCI_CMD_RESET, NULL, 0);
493
494 btr.btr_flags |= BTF_INIT;
495 if (ioctl(hci, SIOCSBTFLAGS, &btr) < 0)
496 err(EXIT_FAILURE, "SIOCSBTFLAGS");
497
498 /*
499 * although the reset command will automatically
500 * carry out these commands, we do them manually
501 * just so we can wait for completion.
502 */
503 hci_cmd(HCI_CMD_READ_BDADDR, NULL, 0);
504 hci_cmd(HCI_CMD_READ_BUFFER_SIZE, NULL, 0);
505 hci_cmd(HCI_CMD_READ_LOCAL_FEATURES, NULL, 0);
506
507 if (set_unit(SIOCGBTINFO) < 0)
508 err(EXIT_FAILURE, "%s", btr.btr_name);
509 }
510
511 if (opt_switch || opt_hold || opt_sniff || opt_park) {
512 uint16_t val = btr.btr_link_policy;
513
514 if (opt_switch > 0) val |= HCI_LINK_POLICY_ENABLE_ROLE_SWITCH;
515 if (opt_switch < 0) val &= ~HCI_LINK_POLICY_ENABLE_ROLE_SWITCH;
516 if (opt_hold > 0) val |= HCI_LINK_POLICY_ENABLE_HOLD_MODE;
517 if (opt_hold < 0) val &= ~HCI_LINK_POLICY_ENABLE_HOLD_MODE;
518 if (opt_sniff > 0) val |= HCI_LINK_POLICY_ENABLE_SNIFF_MODE;
519 if (opt_sniff < 0) val &= ~HCI_LINK_POLICY_ENABLE_SNIFF_MODE;
520 if (opt_park > 0) val |= HCI_LINK_POLICY_ENABLE_PARK_MODE;
521 if (opt_park < 0) val &= ~HCI_LINK_POLICY_ENABLE_PARK_MODE;
522
523 btr.btr_link_policy = val;
524 if (ioctl(hci, SIOCSBTPOLICY, &btr) < 0)
525 err(EXIT_FAILURE, "SIOCSBTPOLICY");
526 }
527
528 if (opt_ptype) {
529 btr.btr_packet_type = ptype;
530 if (ioctl(hci, SIOCSBTPTYPE, &btr) < 0)
531 err(EXIT_FAILURE, "SIOCSBTPTYPE");
532 }
533
534 if (opt_pscan || opt_iscan) {
535 uint8_t val;
536
537 load_value(HCI_CMD_READ_SCAN_ENABLE, &val, sizeof(val));
538 if (opt_pscan > 0) val |= HCI_PAGE_SCAN_ENABLE;
539 if (opt_pscan < 0) val &= ~HCI_PAGE_SCAN_ENABLE;
540 if (opt_iscan > 0) val |= HCI_INQUIRY_SCAN_ENABLE;
541 if (opt_iscan < 0) val &= ~HCI_INQUIRY_SCAN_ENABLE;
542 save_value(HCI_CMD_WRITE_SCAN_ENABLE, &val, sizeof(val));
543 }
544
545 if (opt_auth) {
546 uint8_t val = (opt_auth > 0 ? 1 : 0);
547
548 save_value(HCI_CMD_WRITE_AUTH_ENABLE, &val, sizeof(val));
549 }
550
551 if (opt_encrypt) {
552 uint8_t val = (opt_encrypt > 0 ? 1 : 0);
553
554 save_value(HCI_CMD_WRITE_ENCRYPTION_MODE, &val, sizeof(val));
555 }
556
557 if (opt_name)
558 save_value(HCI_CMD_WRITE_LOCAL_NAME, name, HCI_UNIT_NAME_SIZE);
559
560 if (opt_class) {
561 uint8_t val[HCI_CLASS_SIZE];
562
563 val[0] = (class >> 0) & 0xff;
564 val[1] = (class >> 8) & 0xff;
565 val[2] = (class >> 16) & 0xff;
566
567 save_value(HCI_CMD_WRITE_UNIT_CLASS, val, HCI_CLASS_SIZE);
568 }
569
570 if (opt_pin) {
571 uint8_t val;
572
573 if (opt_pin > 0) val = 1;
574 else val = 0;
575
576 save_value(HCI_CMD_WRITE_PIN_TYPE, &val, sizeof(val));
577 }
578
579 if (opt_voice) {
580 uint16_t val;
581
582 val = htole16(voice & 0x03ff);
583 save_value(HCI_CMD_WRITE_VOICE_SETTING, &val, sizeof(val));
584 }
585
586 if (opt_pto) {
587 uint16_t val;
588
589 val = htole16(pto * 8 / 5);
590 save_value(HCI_CMD_WRITE_PAGE_TIMEOUT, &val, sizeof(val));
591 }
592
593 if (opt_scomtu) {
594 if (scomtu > 0xff) {
595 warnx("Invalid SCO mtu %d", scomtu);
596 } else {
597 btr.btr_sco_mtu = scomtu;
598
599 if (ioctl(hci, SIOCSBTSCOMTU, &btr) < 0)
600 warn("SIOCSBTSCOMTU");
601 }
602 }
603
604 if (opt_rssi) {
605 uint8_t val = (opt_rssi > 0 ? 1 : 0);
606
607 save_value(HCI_CMD_WRITE_INQUIRY_MODE, &val, sizeof(val));
608 }
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 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_UNIT_CLASS, buf, HCI_CLASS_SIZE);
647 class = (buf[2] << 16) | (buf[1] << 8) | (buf[0]);
648 print_class("\t");
649
650 load_value(HCI_CMD_READ_LOCAL_NAME, buf, HCI_UNIT_NAME_SIZE);
651 printf("\tname: \"%s\"\n", buf);
652
653 load_value(HCI_CMD_READ_VOICE_SETTING, buf, sizeof(uint16_t));
654 voice = (buf[1] << 8) | buf[0];
655 print_voice(level);
656
657 load_value(HCI_CMD_READ_PIN_TYPE, &val, sizeof(val));
658 printf("\tpin: %s\n", val ? "fixed" : "variable");
659
660 width = printf("\toptions:");
661
662 load_value(HCI_CMD_READ_SCAN_ENABLE, &val, sizeof(val));
663 if (val & HCI_INQUIRY_SCAN_ENABLE) tag("iscan");
664 else if (level > 0) tag("-iscan");
665
666 if (val & HCI_PAGE_SCAN_ENABLE) tag("pscan");
667 else if (level > 0) tag("-pscan");
668
669 load_value(HCI_CMD_READ_AUTH_ENABLE, &val, sizeof(val));
670 if (val) tag("auth");
671 else if (level > 0) tag("-auth");
672
673 load_value(HCI_CMD_READ_ENCRYPTION_MODE, &val, sizeof(val));
674 if (val) tag("encrypt");
675 else if (level > 0) tag("-encrypt");
676
677 val = btr.btr_link_policy;
678 if (val & HCI_LINK_POLICY_ENABLE_ROLE_SWITCH) tag("switch");
679 else if (level > 0) tag("-switch");
680 if (val & HCI_LINK_POLICY_ENABLE_HOLD_MODE) tag("hold");
681 else if (level > 0) tag("-hold");
682 if (val & HCI_LINK_POLICY_ENABLE_SNIFF_MODE) tag("sniff");
683 else if (level > 0) tag("-sniff");
684 if (val & HCI_LINK_POLICY_ENABLE_PARK_MODE) tag("park");
685 else if (level > 0) tag("-park");
686
687 load_value(HCI_CMD_READ_INQUIRY_MODE, &val, sizeof(val));
688 if (val) tag("rssi");
689 else if (level > 0) tag("-rssi");
690
691 tag(NULL);
692
693 if (level-- < 1)
694 return;
695
696 ptype = btr.btr_packet_type;
697 width = printf("\tptype: [0x%04x]", ptype);
698 if (ptype & HCI_PKT_DM1) tag("DM1");
699 if (ptype & HCI_PKT_DH1) tag("DH1");
700 if (ptype & HCI_PKT_DM3) tag("DM3");
701 if (ptype & HCI_PKT_DH3) tag("DH3");
702 if (ptype & HCI_PKT_DM5) tag("DM5");
703 if (ptype & HCI_PKT_DH5) tag("DH5");
704 if ((ptype & HCI_PKT_2MBPS_DH1) == 0) tag("2-DH1");
705 if ((ptype & HCI_PKT_3MBPS_DH1) == 0) tag("3-DH1");
706 if ((ptype & HCI_PKT_2MBPS_DH3) == 0) tag("2-DH3");
707 if ((ptype & HCI_PKT_3MBPS_DH3) == 0) tag("3-DH3");
708 if ((ptype & HCI_PKT_2MBPS_DH5) == 0) tag("2-DH5");
709 if ((ptype & HCI_PKT_3MBPS_DH5) == 0) tag("3-DH5");
710 tag(NULL);
711
712 load_value(HCI_CMD_READ_PAGE_TIMEOUT, &val16, sizeof(val16));
713 printf("\tpage timeout: %d ms\n", val16 * 5 / 8);
714
715 if (level-- < 1)
716 return;
717
718 load_value(HCI_CMD_READ_LOCAL_FEATURES, buf, HCI_FEATURES_SIZE);
719 print_features("\tfeatures:", buf);
720 }
721
722 void
723 print_stats(void)
724 {
725
726 if (sflag == 0)
727 return;
728
729 if (sflag == 1) {
730 if (ioctl(hci, SIOCGBTSTATS, &btr) < 0)
731 err(EXIT_FAILURE, "SIOCGBTSTATS");
732 } else {
733 if (ioctl(hci, SIOCZBTSTATS, &btr) < 0)
734 err(EXIT_FAILURE, "SIOCZBTSTATS");
735 }
736
737 printf( "\tTotal bytes sent %d, recieved %d\n"
738 "\tCommands sent %d, Events received %d\n"
739 "\tACL data packets sent %d, received %d\n"
740 "\tSCO data packets sent %d, received %d\n"
741 "\tInput errors %d, Output errors %d\n",
742 btr.btr_stats.byte_tx, btr.btr_stats.byte_rx,
743 btr.btr_stats.cmd_tx, btr.btr_stats.evt_rx,
744 btr.btr_stats.acl_tx, btr.btr_stats.acl_rx,
745 btr.btr_stats.sco_tx, btr.btr_stats.sco_rx,
746 btr.btr_stats.err_rx, btr.btr_stats.err_tx);
747 }
748
749 void
750 print_features(const char *str, uint8_t *f)
751 {
752
753 width = printf("%s", str);
754
755 /* ------------------- byte 0 --------------------*/
756 if (*f & HCI_LMP_3SLOT) tag("<3 slot>");
757 if (*f & HCI_LMP_5SLOT) tag("<5 slot>");
758 if (*f & HCI_LMP_ENCRYPTION) tag("<encryption>");
759 if (*f & HCI_LMP_SLOT_OFFSET) tag("<slot offset>");
760 if (*f & HCI_LMP_TIMIACCURACY) tag("<timing accuracy>");
761 if (*f & HCI_LMP_ROLE_SWITCH) tag("<role switch>");
762 if (*f & HCI_LMP_HOLD_MODE) tag("<hold mode>");
763 if (*f & HCI_LMP_SNIFF_MODE) tag("<sniff mode>");
764 f++;
765
766 /* ------------------- byte 1 --------------------*/
767 if (*f & HCI_LMP_PARK_MODE) tag("<park mode>");
768 if (*f & HCI_LMP_RSSI) tag("<RSSI>");
769 if (*f & HCI_LMP_CHANNEL_QUALITY) tag("<channel quality>");
770 if (*f & HCI_LMP_SCO_LINK) tag("<SCO link>");
771 if (*f & HCI_LMP_HV2_PKT) tag("<HV2>");
772 if (*f & HCI_LMP_HV3_PKT) tag("<HV3>");
773 if (*f & HCI_LMP_ULAW_LOG) tag("<u-Law log>");
774 if (*f & HCI_LMP_ALAW_LOG) tag("<A-Law log>");
775 f++;
776
777 /* ------------------- byte 1 --------------------*/
778 if (*f & HCI_LMP_CVSD) tag("<CVSD data>");
779 if (*f & HCI_LMP_PAGISCHEME) tag("<paging parameter>");
780 if (*f & HCI_LMP_POWER_CONTROL) tag("<power control>");
781 if (*f & HCI_LMP_TRANSPARENT_SCO) tag("<transparent SCO>");
782 if (*f & HCI_LMP_FLOW_CONTROL_LAG0) tag("<flow control lag 0>");
783 if (*f & HCI_LMP_FLOW_CONTROL_LAG1) tag("<flow control lag 1>");
784 if (*f & HCI_LMP_FLOW_CONTROL_LAG2) tag("<flow control lag 2>");
785 if (*f & HCI_LMP_BC_ENCRYPTION) tag("<broadcast encryption>");
786 f++;
787
788 /* ------------------- byte 3 --------------------*/
789 if (*f & HCI_LMP_EDR_ACL_2MBPS) tag("<EDR ACL 2Mbps>");
790 if (*f & HCI_LMP_EDR_ACL_3MBPS) tag("<EDR ACL 3Mbps>");
791 if (*f & HCI_LMP_ENHANCED_ISCAN) tag("<enhanced inquiry scan>");
792 if (*f & HCI_LMP_INTERLACED_ISCAN) tag("<interlaced inquiry scan>");
793 if (*f & HCI_LMP_INTERLACED_PSCAN) tag("<interlaced page scan>");
794 if (*f & HCI_LMP_RSSI_INQUIRY) tag("<RSSI with inquiry result>");
795 if (*f & HCI_LMP_EV3_PKT) tag("<EV3 packets>");
796 f++;
797
798 /* ------------------- byte 4 --------------------*/
799 if (*f & HCI_LMP_EV4_PKT) tag("<EV4 packets>");
800 if (*f & HCI_LMP_EV5_PKT) tag("<EV5 packets>");
801 if (*f & HCI_LMP_AFH_CAPABLE_SLAVE) tag("<AFH capable slave>");
802 if (*f & HCI_LMP_AFH_CLASS_SLAVE) tag("<AFH class slave>");
803 if (*f & HCI_LMP_3SLOT_EDR_ACL) tag("<3 slot EDR ACL>");
804 f++;
805
806 /* ------------------- byte 5 --------------------*/
807 if (*f & HCI_LMP_5SLOT_EDR_ACL) tag("<5 slot EDR ACL>");
808 if (*f & HCI_LMP_AFH_CAPABLE_MASTER)tag("<AFH capable master>");
809 if (*f & HCI_LMP_AFH_CLASS_MASTER) tag("<AFH class master>");
810 if (*f & HCI_LMP_EDR_eSCO_2MBPS) tag("<EDR eSCO 2Mbps>");
811 if (*f & HCI_LMP_EDR_eSCO_3MBPS) tag("<EDR eSCO 3Mbps>");
812 if (*f & HCI_LMP_3SLOT_EDR_eSCO) tag("<3 slot EDR eSCO>");
813 f++;
814
815 /* ------------------- byte 6 --------------------*/
816 f++;
817
818 /* ------------------- byte 7 --------------------*/
819 if (*f & HCI_LMP_EXTENDED_FEATURES) tag("<extended features>");
820
821 tag(NULL);
822 }
823
824 void
825 print_class(const char *str)
826 {
827 int major, minor;
828
829 major = (class & 0x1f00) >> 8;
830 minor = (class & 0x00fc) >> 2;
831
832 width = printf("%sclass: [0x%6.6x]", str, class);
833
834 switch (major) {
835 case 1: /* Computer */
836 switch (minor) {
837 case 1: tag("Desktop"); break;
838 case 2: tag("Server"); break;
839 case 3: tag("Laptop"); break;
840 case 4: tag("Handheld"); break;
841 case 5: tag("Palm Sized"); break;
842 case 6: tag("Wearable"); break;
843 }
844 tag("Computer");
845 break;
846
847 case 2: /* Phone */
848 switch (minor) {
849 case 1: tag("Cellular Phone"); break;
850 case 2: tag("Cordless Phone"); break;
851 case 3: tag("Smart Phone"); break;
852 case 4: tag("Wired Modem/Phone Gateway"); break;
853 case 5: tag("Common ISDN"); break;
854 default:tag("Phone"); break;
855 }
856 break;
857
858 case 3: /* LAN */
859 tag("LAN");
860 switch ((minor & 0x38) >> 3) {
861 case 0: tag("[Fully available]"); break;
862 case 1: tag("[1-17% utilised]"); break;
863 case 2: tag("[17-33% utilised]"); break;
864 case 3: tag("[33-50% utilised]"); break;
865 case 4: tag("[50-67% utilised]"); break;
866 case 5: tag("[67-83% utilised]"); break;
867 case 6: tag("[83-99% utilised]"); break;
868 case 7: tag("[No service available]"); break;
869 }
870 break;
871
872 case 4: /* Audio/Visual */
873 switch (minor) {
874 case 1: tag("Wearable Headset"); break;
875 case 2: tag("Hands-free Audio"); break;
876 case 4: tag("Microphone"); break;
877 case 5: tag("Loudspeaker"); break;
878 case 6: tag("Headphones"); break;
879 case 7: tag("Portable Audio"); break;
880 case 8: tag("Car Audio"); break;
881 case 9: tag("Set-top Box"); break;
882 case 10: tag("HiFi Audio"); break;
883 case 11: tag("VCR"); break;
884 case 12: tag("Video Camera"); break;
885 case 13: tag("Camcorder"); break;
886 case 14: tag("Video Monitor"); break;
887 case 15: tag("Video Display and Loudspeaker"); break;
888 case 16: tag("Video Conferencing"); break;
889 case 18: tag("A/V [Gaming/Toy]"); break;
890 default: tag("Audio/Visual"); break;
891 }
892 break;
893
894 case 5: /* Peripheral */
895 switch (minor & 0x0f) {
896 case 1: tag("Joystick"); break;
897 case 2: tag("Gamepad"); break;
898 case 3: tag("Remote Control"); break;
899 case 4: tag("Sensing Device"); break;
900 case 5: tag("Digitiser Tablet"); break;
901 case 6: tag("Card Reader"); break;
902 default: tag("Peripheral"); break;
903 }
904
905 if (minor & 0x10) tag("Keyboard");
906 if (minor & 0x20) tag("Mouse");
907 break;
908
909 case 6: /* Imaging */
910 if (minor & 0x20) tag("Printer");
911 if (minor & 0x10) tag("Scanner");
912 if (minor & 0x08) tag("Camera");
913 if (minor & 0x04) tag("Display");
914 if ((minor & 0x3c) == 0) tag("Imaging");
915 break;
916
917 case 7: /* Wearable */
918 switch (minor) {
919 case 1: tag("Wrist Watch"); break;
920 case 2: tag("Pager"); break;
921 case 3: tag("Jacket"); break;
922 case 4: tag("Helmet"); break;
923 case 5: tag("Glasses"); break;
924 default: tag("Wearable"); break;
925 }
926 break;
927
928 case 8: /* Toy */
929 switch (minor) {
930 case 1: tag("Robot"); break;
931 case 2: tag("Vehicle"); break;
932 case 3: tag("Doll / Action Figure"); break;
933 case 4: tag("Controller"); break;
934 case 5: tag("Game"); break;
935 default: tag("Toy"); break;
936 }
937 break;
938
939 default:
940 break;
941 }
942
943 if (class & 0x002000) tag("<Limited Discoverable>");
944 if (class & 0x010000) tag("<Positioning>");
945 if (class & 0x020000) tag("<Networking>");
946 if (class & 0x040000) tag("<Rendering>");
947 if (class & 0x080000) tag("<Capturing>");
948 if (class & 0x100000) tag("<Object Transfer>");
949 if (class & 0x200000) tag("<Audio>");
950 if (class & 0x400000) tag("<Telephony>");
951 if (class & 0x800000) tag("<Information>");
952 tag(NULL);
953 }
954
955 void
956 print_voice(int level)
957 {
958 printf("\tvoice: [0x%4.4x]\n", voice);
959
960 if (level == 0)
961 return;
962
963 printf("\t\tInput Coding: ");
964 switch ((voice & 0x0300) >> 8) {
965 case 0x00: printf("Linear PCM [%d-bit, pos %d]",
966 (voice & 0x0020 ? 16 : 8),
967 (voice & 0x001c) >> 2); break;
968 case 0x01: printf("u-Law"); break;
969 case 0x02: printf("A-Law"); break;
970 case 0x03: printf("unknown"); break;
971 }
972
973 switch ((voice & 0x00c0) >> 6) {
974 case 0x00: printf(", 1's complement"); break;
975 case 0x01: printf(", 2's complement"); break;
976 case 0x02: printf(", sign magnitude"); break;
977 case 0x03: printf(", unsigned"); break;
978 }
979
980 printf("\n\t\tAir Coding: ");
981 switch (voice & 0x0003) {
982 case 0x00: printf("CVSD"); break;
983 case 0x01: printf("u-Law"); break;
984 case 0x02: printf("A-Law"); break;
985 case 0x03: printf("Transparent"); break;
986 }
987
988 printf("\n");
989 }
990
991 void
992 print_result(int num, struct result *r, int rssi)
993 {
994 hci_remote_name_req_cp ncp;
995 hci_remote_name_req_compl_ep nep;
996 #if 0
997 hci_read_remote_features_cp fcp;
998 hci_read_remote_features_compl_ep fep;
999 #endif
1000 struct hostent *hp;
1001
1002 printf("%3d: bdaddr %s",
1003 num,
1004 bt_ntoa(&r->bdaddr, NULL));
1005
1006 hp = bt_gethostbyaddr((const char *)&r->bdaddr, sizeof(bdaddr_t), AF_BLUETOOTH);
1007 if (hp != NULL)
1008 printf(" (%s)", hp->h_name);
1009
1010 printf("\n");
1011
1012 memset(&ncp, 0, sizeof(ncp));
1013 bdaddr_copy(&ncp.bdaddr, &r->bdaddr);
1014 ncp.page_scan_rep_mode = r->page_scan_rep_mode;
1015 ncp.clock_offset = r->clock_offset;
1016
1017 hci_req(HCI_CMD_REMOTE_NAME_REQ,
1018 HCI_EVENT_REMOTE_NAME_REQ_COMPL,
1019 &ncp, sizeof(ncp),
1020 &nep, sizeof(nep));
1021
1022 printf(" : name \"%s\"\n", nep.name);
1023
1024 class = (r->uclass[2] << 16) | (r->uclass[1] << 8) | (r->uclass[0]);
1025 print_class(" : ");
1026
1027 #if 0
1028 hci_req(HCI_CMD_READ_REMOTE_FEATURES,
1029 HCI_EVENT_READ_REMOTE_FEATURES_COMPL,
1030 &fcp, sizeof(fcp),
1031 &fep, sizeof(fep));
1032
1033 print_features(" : features", fep.features);
1034 #endif
1035
1036 printf(" : page scan rep mode 0x%02x\n", r->page_scan_rep_mode);
1037 printf(" : clock offset %d\n", le16toh(r->clock_offset));
1038
1039 if (rssi)
1040 printf(" : rssi %d\n", r->rssi);
1041
1042 printf("\n");
1043 }
1044
1045 void
1046 do_inquiry(void)
1047 {
1048 uint8_t buf[HCI_EVENT_PKT_SIZE];
1049 struct result result[INQUIRY_MAX_RESPONSES];
1050 hci_inquiry_cp inq;
1051 struct hci_filter f;
1052 hci_event_hdr_t *hh;
1053 int i, j, num, rssi;
1054
1055 if (opt_inquiry == 0)
1056 return;
1057
1058 printf("Device Discovery from device: %s ...", btr.btr_name);
1059 fflush(stdout);
1060
1061 memset(&f, 0, sizeof(f));
1062 hci_filter_set(HCI_EVENT_COMMAND_STATUS, &f);
1063 hci_filter_set(HCI_EVENT_COMMAND_COMPL, &f);
1064 hci_filter_set(HCI_EVENT_INQUIRY_RESULT, &f);
1065 hci_filter_set(HCI_EVENT_RSSI_RESULT, &f);
1066 hci_filter_set(HCI_EVENT_INQUIRY_COMPL, &f);
1067 hci_filter_set(HCI_EVENT_REMOTE_NAME_REQ_COMPL, &f);
1068 hci_filter_set(HCI_EVENT_READ_REMOTE_FEATURES_COMPL, &f);
1069 if (setsockopt(hci, BTPROTO_HCI, SO_HCI_EVT_FILTER, &f, sizeof(f)) < 0)
1070 err(EXIT_FAILURE, "Can't set event filter");
1071
1072 /* General Inquiry LAP is 0x9e8b33 */
1073 inq.lap[0] = 0x33;
1074 inq.lap[1] = 0x8b;
1075 inq.lap[2] = 0x9e;
1076 inq.inquiry_length = INQUIRY_LENGTH;
1077 inq.num_responses = INQUIRY_MAX_RESPONSES;
1078
1079 hci_cmd(HCI_CMD_INQUIRY, &inq, sizeof(inq));
1080
1081 num = 0;
1082 rssi = 0;
1083 hh = (hci_event_hdr_t *)buf;
1084
1085 for (;;) {
1086 if (recv(hci, buf, sizeof(buf), 0) <= 0)
1087 err(EXIT_FAILURE, "recv");
1088
1089 if (hh->event == HCI_EVENT_INQUIRY_COMPL)
1090 break;
1091
1092 if (hh->event == HCI_EVENT_INQUIRY_RESULT) {
1093 hci_inquiry_result_ep *ep = (hci_inquiry_result_ep *)(hh + 1);
1094 hci_inquiry_response *ir = (hci_inquiry_response *)(ep + 1);
1095
1096 for (i = 0 ; i < ep->num_responses ; i++) {
1097 if (num == INQUIRY_MAX_RESPONSES)
1098 break;
1099
1100 /* some devices keep responding, ignore dupes */
1101 for (j = 0 ; j < num ; j++)
1102 if (bdaddr_same(&result[j].bdaddr, &ir[i].bdaddr))
1103 break;
1104
1105 if (j < num)
1106 continue;
1107
1108 bdaddr_copy(&result[num].bdaddr, &ir[i].bdaddr);
1109 memcpy(&result[num].uclass, &ir[i].uclass, HCI_CLASS_SIZE);
1110 result[num].page_scan_rep_mode = ir[i].page_scan_rep_mode;
1111 result[num].clock_offset = ir[i].clock_offset;
1112 result[num].rssi = 0;
1113 num++;
1114 printf(".");
1115 fflush(stdout);
1116 }
1117 continue;
1118 }
1119
1120 if (hh->event == HCI_EVENT_RSSI_RESULT) {
1121 hci_rssi_result_ep *ep = (hci_rssi_result_ep *)(hh + 1);
1122 hci_rssi_response *rr = (hci_rssi_response *)(ep + 1);
1123
1124 for (i = 0 ; i < ep->num_responses ; i++) {
1125 if (num == INQUIRY_MAX_RESPONSES)
1126 break;
1127
1128 /* some devices keep responding, ignore dupes */
1129 for (j = 0 ; j < num ; j++)
1130 if (bdaddr_same(&result[j].bdaddr, &rr[i].bdaddr))
1131 break;
1132
1133 if (j < num)
1134 continue;
1135
1136 bdaddr_copy(&result[num].bdaddr, &rr[i].bdaddr);
1137 memcpy(&result[num].uclass, &rr[i].uclass, HCI_CLASS_SIZE);
1138 result[num].page_scan_rep_mode = rr[i].page_scan_rep_mode;
1139 result[num].clock_offset = rr[i].clock_offset;
1140 result[num].rssi = rr[i].rssi;
1141 rssi = 1;
1142 num++;
1143 printf(".");
1144 fflush(stdout);
1145 }
1146 continue;
1147 }
1148 }
1149
1150 printf(" %d response%s\n", num, (num == 1 ? "" : "s"));
1151
1152 for (i = 0 ; i < num ; i++)
1153 print_result(i + 1, &result[i], rssi);
1154 }
1155