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