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