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