No empty .Rs/.Re
[netbsd-mini2440.git] / usr.sbin / btconfig / btconfig.c
blob388461d1cdad05e5742f619b241d303db997835c
1 /* $NetBSD: btconfig.c,v 1.21 2009/10/08 19:50:03 plunky Exp $ */
3 /*-
4 * Copyright (c) 2006 Itronix Inc.
5 * All rights reserved.
7 * Written by Iain Hibbert for Itronix Inc.
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.
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.
34 #include <sys/cdefs.h>
35 __COPYRIGHT("@(#) Copyright (c) 2006 Itronix, Inc. All rights reserved.");
36 __RCSID("$NetBSD: btconfig.c,v 1.21 2009/10/08 19:50:03 plunky Exp $");
38 #include <sys/ioctl.h>
39 #include <sys/param.h>
40 #include <sys/socket.h>
42 #include <bluetooth.h>
43 #include <err.h>
44 #include <errno.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
49 #include <util.h>
51 int main(int, char *[]);
52 void badarg(const char *);
53 void badparam(const char *);
54 void badval(const char *, const char *);
55 void usage(void);
56 int set_unit(unsigned long);
57 void config_unit(void);
58 void print_val(const char *, const char **, int);
59 void print_info(int);
60 void print_stats(void);
61 void print_class(const char *, uint8_t *);
62 void print_class0(void);
63 void print_voice(int);
64 void tag(const char *);
65 void print_features(const char *, uint8_t, uint8_t *);
66 void print_features0(uint8_t *);
67 void print_features1(uint8_t *);
68 void print_result(int, struct bt_devinquiry *);
69 void do_inquiry(void);
71 void hci_req(uint16_t, uint8_t , void *, size_t, void *, size_t);
72 void save_value(uint16_t, void *, size_t);
73 void load_value(uint16_t, void *, size_t);
75 #define MAX_STR_SIZE 0xff
77 /* print width */
78 int width = 0;
79 #define MAX_WIDTH 70
81 /* global variables */
82 int hci;
83 struct btreq btr;
85 /* command line flags */
86 int verbose = 0; /* more info */
87 int lflag = 0; /* list devices */
88 int sflag = 0; /* get/zero stats */
90 /* device up/down (flag) */
91 int opt_enable = 0;
92 int opt_reset = 0;
93 #define FLAGS_FMT "\20" \
94 "\001UP" \
95 "\002RUNNING" \
96 "\003XMIT_CMD" \
97 "\004XMIT_ACL" \
98 "\005XMIT_SCO" \
99 "\006INIT_BDADDR" \
100 "\007INIT_BUFFER_SIZE" \
101 "\010INIT_FEATURES" \
102 "\011POWER_UP_NOOP" \
103 "\012INIT_COMMANDS" \
104 "\013MASTER" \
107 /* authorisation (flag) */
108 int opt_auth = 0;
110 /* encryption (flag) */
111 int opt_encrypt = 0;
113 /* scan enable options (flags) */
114 int opt_pscan = 0;
115 int opt_iscan = 0;
117 /* master role option */
118 int opt_master = 0;
120 /* link policy options (flags) */
121 int opt_switch = 0;
122 int opt_hold = 0;
123 int opt_sniff = 0;
124 int opt_park = 0;
126 /* class of device (hex value) */
127 int opt_class = 0;
128 uint32_t class;
130 /* packet type mask (hex value) */
131 int opt_ptype = 0;
132 uint32_t ptype;
134 /* unit name (string) */
135 int opt_name = 0;
136 char name[MAX_STR_SIZE];
138 /* pin type */
139 int opt_pin = 0;
141 /* Inquiry */
142 int opt_rssi = 0; /* inquiry_with_rssi (obsolete flag) */
143 int opt_imode = 0; /* inquiry mode */
144 int opt_inquiry = 0;
145 #define INQUIRY_LENGTH 10 /* seconds */
146 #define INQUIRY_MAX_RESPONSES 10
147 const char *imodes[] = { "std", "rssi", "ext", NULL };
149 /* Voice Settings */
150 int opt_voice = 0;
151 uint32_t voice;
153 /* Page Timeout */
154 int opt_pto = 0;
155 uint32_t pto;
157 /* set SCO mtu */
158 int opt_scomtu;
159 uint32_t scomtu;
161 struct parameter {
162 const char *name;
163 enum { P_SET, P_CLR, P_STR, P_HEX, P_NUM, P_VAL } 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 { "master", P_SET, &opt_master, NULL },
177 { "-master", P_CLR, &opt_master, NULL },
178 { "switch", P_SET, &opt_switch, NULL },
179 { "-switch", P_CLR, &opt_switch, NULL },
180 { "hold", P_SET, &opt_hold, NULL },
181 { "-hold", P_CLR, &opt_hold, NULL },
182 { "sniff", P_SET, &opt_sniff, NULL },
183 { "-sniff", P_CLR, &opt_sniff, NULL },
184 { "park", P_SET, &opt_park, NULL },
185 { "-park", P_CLR, &opt_park, NULL },
186 { "auth", P_SET, &opt_auth, NULL },
187 { "-auth", P_CLR, &opt_auth, NULL },
188 { "encrypt", P_SET, &opt_encrypt, NULL },
189 { "-encrypt", P_CLR, &opt_encrypt, NULL },
190 { "ptype", P_HEX, &opt_ptype, &ptype },
191 { "class", P_HEX, &opt_class, &class },
192 { "fixed", P_SET, &opt_pin, NULL },
193 { "variable", P_CLR, &opt_pin, NULL },
194 { "inq", P_SET, &opt_inquiry, NULL },
195 { "inquiry", P_SET, &opt_inquiry, NULL },
196 { "imode", P_VAL, &opt_imode, imodes },
197 { "rssi", P_SET, &opt_rssi, NULL },
198 { "-rssi", P_CLR, &opt_rssi, NULL },
199 { "reset", P_SET, &opt_reset, NULL },
200 { "voice", P_HEX, &opt_voice, &voice },
201 { "pto", P_NUM, &opt_pto, &pto },
202 { "scomtu", P_NUM, &opt_scomtu, &scomtu },
203 { NULL, 0, NULL, NULL }
207 main(int ac, char *av[])
209 int ch;
210 struct parameter *p;
212 while ((ch = getopt(ac, av, "hlsvz")) != -1) {
213 switch(ch) {
214 case 'l':
215 lflag = 1;
216 break;
218 case 's':
219 sflag = 1;
220 break;
222 case 'v':
223 verbose++;
224 break;
226 case 'z':
227 sflag = 2;
228 break;
230 case 'h':
231 default:
232 usage();
235 av += optind;
236 ac -= optind;
238 if (lflag && sflag)
239 usage();
241 hci = socket(PF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
242 if (hci == -1)
243 err(EXIT_FAILURE, "socket");
245 if (ac == 0) {
246 verbose++;
248 memset(&btr, 0, sizeof(btr));
249 while (set_unit(SIOCNBTINFO) != -1) {
250 print_info(verbose);
251 print_stats();
254 tag(NULL);
255 } else {
256 strlcpy(btr.btr_name, *av, HCI_DEVNAME_SIZE);
257 av++, ac--;
259 if (set_unit(SIOCGBTINFO) < 0)
260 err(EXIT_FAILURE, "%s", btr.btr_name);
262 if (ac == 0)
263 verbose += 2;
265 while (ac > 0) {
266 for (p = parameters ; ; p++) {
267 if (p->name == NULL)
268 badparam(*av);
270 if (strcmp(*av, p->name) == 0)
271 break;
274 switch(p->type) {
275 case P_SET:
276 *(p->opt) = 1;
277 break;
279 case P_CLR:
280 *(p->opt) = -1;
281 break;
283 case P_STR:
284 if (--ac < 1) badarg(p->name);
285 strlcpy((char *)(p->val), *++av, MAX_STR_SIZE);
286 *(p->opt) = 1;
287 break;
289 case P_HEX:
290 if (--ac < 1) badarg(p->name);
291 *(uint32_t *)(p->val) = strtoul(*++av, NULL, 16);
292 *(p->opt) = 1;
293 break;
295 case P_NUM:
296 if (--ac < 1) badarg(p->name);
297 *(uint32_t *)(p->val) = strtoul(*++av, NULL, 10);
298 *(p->opt) = 1;
299 break;
301 case P_VAL:
302 if (--ac < 1) badarg(p->name);
303 ++av;
304 ch = 0;
305 do {
306 if (((char **)(p->val))[ch] == NULL)
307 badval(p->name, *av);
308 } while (strcmp(((char **)(p->val))[ch++], *av));
309 *(p->opt) = ch;
310 break;
313 av++, ac--;
316 config_unit();
317 print_info(verbose);
318 print_stats();
319 do_inquiry();
322 close(hci);
323 return EXIT_SUCCESS;
326 void
327 badparam(const char *param)
330 fprintf(stderr, "unknown parameter '%s'\n", param);
331 exit(EXIT_FAILURE);
334 void
335 badarg(const char *param)
338 fprintf(stderr, "parameter '%s' needs argument\n", param);
339 exit(EXIT_FAILURE);
342 void
343 badval(const char *param, const char *value)
346 fprintf(stderr, "bad value '%s' for parameter '%s'\n", value, param);
347 exit(EXIT_FAILURE);
350 void
351 usage(void)
354 fprintf(stderr, "usage: %s [-svz] [device [parameters]]\n", getprogname());
355 fprintf(stderr, " %s -l\n", getprogname());
356 exit(EXIT_FAILURE);
360 * pretty printing feature
362 void
363 tag(const char *f)
366 if (f == NULL) {
367 if (width > 0)
368 printf("\n");
370 width = 0;
371 } else {
372 width += printf("%*s%s",
373 (width == 0 ? (lflag ? 0 : 8) : 1),
374 "", f);
376 if (width > MAX_WIDTH) {
377 printf("\n");
378 width = 0;
384 * basic HCI request wrapper with error check
386 void
387 hci_req(uint16_t opcode, uint8_t event, void *cbuf, size_t clen,
388 void *rbuf, size_t rlen)
390 struct bt_devreq req;
392 req.opcode = opcode;
393 req.event = event;
394 req.cparam = cbuf;
395 req.clen = clen;
396 req.rparam = rbuf;
397 req.rlen = rlen;
399 if (bt_devreq(hci, &req, 10) == -1)
400 err(EXIT_FAILURE, "cmd (%02x|%03x)",
401 HCI_OGF(opcode), HCI_OCF(opcode));
403 if (event == 0 && rlen > 0 && ((uint8_t *)rbuf)[0] != 0)
404 errx(EXIT_FAILURE, "cmd (%02x|%03x): status 0x%02x",
405 HCI_OGF(opcode), HCI_OCF(opcode), ((uint8_t *)rbuf)[0]);
409 * write value to device with opcode.
410 * provide a small response buffer so that the status can be checked
412 void
413 save_value(uint16_t opcode, void *cbuf, size_t clen)
415 uint8_t buf[1];
417 hci_req(opcode, 0, cbuf, clen, buf, sizeof(buf));
421 * read value from device with opcode.
422 * use our own buffer and only return the value from the response packet
424 void
425 load_value(uint16_t opcode, void *rbuf, size_t rlen)
427 uint8_t buf[UINT8_MAX];
429 hci_req(opcode, 0, NULL, 0, buf, sizeof(buf));
430 memcpy(rbuf, buf + 1, rlen);
434 set_unit(unsigned long cmd)
437 if (ioctl(hci, cmd, &btr) == -1)
438 return -1;
440 if (btr.btr_flags & BTF_UP) {
441 struct sockaddr_bt sa;
443 sa.bt_len = sizeof(sa);
444 sa.bt_family = AF_BLUETOOTH;
445 bdaddr_copy(&sa.bt_bdaddr, &btr.btr_bdaddr);
447 if (bind(hci, (struct sockaddr *)&sa, sizeof(sa)) < 0)
448 err(EXIT_FAILURE, "bind");
450 if (connect(hci, (struct sockaddr *)&sa, sizeof(sa)) < 0)
451 err(EXIT_FAILURE, "connect");
454 return 0;
458 * apply configuration parameters to unit
460 void
461 config_unit(void)
464 if (opt_enable) {
465 if (opt_enable > 0)
466 btr.btr_flags |= BTF_UP;
467 else
468 btr.btr_flags &= ~BTF_UP;
470 if (ioctl(hci, SIOCSBTFLAGS, &btr) < 0)
471 err(EXIT_FAILURE, "SIOCSBTFLAGS");
473 if (set_unit(SIOCGBTINFO) < 0)
474 err(EXIT_FAILURE, "%s", btr.btr_name);
477 if (opt_reset) {
478 hci_req(HCI_CMD_RESET, 0, NULL, 0, NULL, 0);
480 btr.btr_flags |= BTF_INIT;
481 if (ioctl(hci, SIOCSBTFLAGS, &btr) < 0)
482 err(EXIT_FAILURE, "SIOCSBTFLAGS");
485 * although the reset command will automatically
486 * carry out these commands, we do them manually
487 * just so we can wait for completion.
489 hci_req(HCI_CMD_READ_BDADDR, 0, NULL, 0, NULL, 0);
490 hci_req(HCI_CMD_READ_BUFFER_SIZE, 0, NULL, 0, NULL, 0);
491 hci_req(HCI_CMD_READ_LOCAL_FEATURES, 0, NULL, 0, NULL, 0);
493 if (set_unit(SIOCGBTINFO) < 0)
494 err(EXIT_FAILURE, "%s", btr.btr_name);
497 if (opt_master) {
498 if (opt_master > 0)
499 btr.btr_flags |= BTF_MASTER;
500 else
501 btr.btr_flags &= ~BTF_MASTER;
503 if (ioctl(hci, SIOCSBTFLAGS, &btr) < 0)
504 err(EXIT_FAILURE, "SIOCSBTFLAGS");
507 if (opt_switch || opt_hold || opt_sniff || opt_park) {
508 uint16_t val = btr.btr_link_policy;
510 if (opt_switch > 0) val |= HCI_LINK_POLICY_ENABLE_ROLE_SWITCH;
511 if (opt_switch < 0) val &= ~HCI_LINK_POLICY_ENABLE_ROLE_SWITCH;
512 if (opt_hold > 0) val |= HCI_LINK_POLICY_ENABLE_HOLD_MODE;
513 if (opt_hold < 0) val &= ~HCI_LINK_POLICY_ENABLE_HOLD_MODE;
514 if (opt_sniff > 0) val |= HCI_LINK_POLICY_ENABLE_SNIFF_MODE;
515 if (opt_sniff < 0) val &= ~HCI_LINK_POLICY_ENABLE_SNIFF_MODE;
516 if (opt_park > 0) val |= HCI_LINK_POLICY_ENABLE_PARK_MODE;
517 if (opt_park < 0) val &= ~HCI_LINK_POLICY_ENABLE_PARK_MODE;
519 btr.btr_link_policy = val;
520 if (ioctl(hci, SIOCSBTPOLICY, &btr) < 0)
521 err(EXIT_FAILURE, "SIOCSBTPOLICY");
524 if (opt_ptype) {
525 btr.btr_packet_type = ptype;
526 if (ioctl(hci, SIOCSBTPTYPE, &btr) < 0)
527 err(EXIT_FAILURE, "SIOCSBTPTYPE");
530 if (opt_pscan || opt_iscan) {
531 uint8_t val;
533 load_value(HCI_CMD_READ_SCAN_ENABLE, &val, sizeof(val));
534 if (opt_pscan > 0) val |= HCI_PAGE_SCAN_ENABLE;
535 if (opt_pscan < 0) val &= ~HCI_PAGE_SCAN_ENABLE;
536 if (opt_iscan > 0) val |= HCI_INQUIRY_SCAN_ENABLE;
537 if (opt_iscan < 0) val &= ~HCI_INQUIRY_SCAN_ENABLE;
538 save_value(HCI_CMD_WRITE_SCAN_ENABLE, &val, sizeof(val));
541 if (opt_auth) {
542 uint8_t val = (opt_auth > 0 ? 1 : 0);
544 save_value(HCI_CMD_WRITE_AUTH_ENABLE, &val, sizeof(val));
547 if (opt_encrypt) {
548 uint8_t val = (opt_encrypt > 0 ? 1 : 0);
550 save_value(HCI_CMD_WRITE_ENCRYPTION_MODE, &val, sizeof(val));
553 if (opt_name)
554 save_value(HCI_CMD_WRITE_LOCAL_NAME, name, HCI_UNIT_NAME_SIZE);
556 if (opt_class) {
557 uint8_t val[HCI_CLASS_SIZE];
559 val[0] = (class >> 0) & 0xff;
560 val[1] = (class >> 8) & 0xff;
561 val[2] = (class >> 16) & 0xff;
563 save_value(HCI_CMD_WRITE_UNIT_CLASS, val, HCI_CLASS_SIZE);
566 if (opt_pin) {
567 uint8_t val;
569 if (opt_pin > 0) val = 1;
570 else val = 0;
572 save_value(HCI_CMD_WRITE_PIN_TYPE, &val, sizeof(val));
575 if (opt_voice) {
576 uint16_t val;
578 val = htole16(voice & 0x03ff);
579 save_value(HCI_CMD_WRITE_VOICE_SETTING, &val, sizeof(val));
582 if (opt_pto) {
583 uint16_t val;
585 val = htole16(pto * 8 / 5);
586 save_value(HCI_CMD_WRITE_PAGE_TIMEOUT, &val, sizeof(val));
589 if (opt_scomtu) {
590 if (scomtu > 0xff) {
591 warnx("Invalid SCO mtu %d", scomtu);
592 } else {
593 btr.btr_sco_mtu = scomtu;
595 if (ioctl(hci, SIOCSBTSCOMTU, &btr) < 0)
596 warn("SIOCSBTSCOMTU");
600 if (opt_imode | opt_rssi) {
601 uint8_t val = (opt_rssi > 0 ? 1 : 0);
603 if (opt_imode)
604 val = opt_imode - 1;
606 save_value(HCI_CMD_WRITE_INQUIRY_MODE, &val, sizeof(val));
611 * print value from NULL terminated array given index
613 void
614 print_val(const char *hdr, const char **argv, int idx)
616 int i = 0;
618 while (i < idx && *argv != NULL)
619 i++, argv++;
621 printf("\t%s: %s\n", hdr, *argv == NULL ? "unknown" : *argv);
625 * Print info for Bluetooth Device with varying verbosity levels
627 void
628 print_info(int level)
630 uint8_t version, val, buf[MAX_STR_SIZE];
632 if (lflag) {
633 tag(btr.btr_name);
634 return;
637 if (level-- < 1)
638 return;
640 snprintb((char *)buf, MAX_STR_SIZE, FLAGS_FMT, btr.btr_flags);
642 printf("%s: bdaddr %s flags %s\n", btr.btr_name,
643 bt_ntoa(&btr.btr_bdaddr, NULL), buf);
645 if (level-- < 1)
646 return;
648 printf("\tnum_cmd = %d\n"
649 "\tnum_acl = %d, acl_mtu = %d\n"
650 "\tnum_sco = %d, sco_mtu = %d\n",
651 btr.btr_num_cmd,
652 btr.btr_num_acl, btr.btr_acl_mtu,
653 btr.btr_num_sco, btr.btr_sco_mtu);
655 if (level-- < 1 || (btr.btr_flags & BTF_UP) == 0)
656 return;
658 load_value(HCI_CMD_READ_LOCAL_VER, &version, sizeof(version));
659 printf("\tHCI version: ");
660 switch(version) {
661 case HCI_SPEC_V10: printf("1.0b\n"); break;
662 case HCI_SPEC_V11: printf("1.1\n"); break;
663 case HCI_SPEC_V12: printf("1.2\n"); break;
664 case HCI_SPEC_V20: printf("2.0 + EDR\n"); break;
665 case HCI_SPEC_V21: printf("2.1 + EDR\n"); break;
666 case HCI_SPEC_V30: printf("3.0 + HS\n"); break;
667 default: printf("unknown\n"); break;
670 load_value(HCI_CMD_READ_UNIT_CLASS, buf, HCI_CLASS_SIZE);
671 print_class("\tclass:", buf);
673 load_value(HCI_CMD_READ_LOCAL_NAME, buf, HCI_UNIT_NAME_SIZE);
674 printf("\tname: \"%s\"\n", buf);
676 load_value(HCI_CMD_READ_VOICE_SETTING, buf, sizeof(uint16_t));
677 voice = (buf[1] << 8) | buf[0];
678 print_voice(level);
680 load_value(HCI_CMD_READ_PIN_TYPE, &val, sizeof(val));
681 printf("\tpin: %s\n", val ? "fixed" : "variable");
683 val = 0;
684 if (version >= HCI_SPEC_V12)
685 load_value(HCI_CMD_READ_INQUIRY_MODE, &val, sizeof(val));
687 print_val("inquiry mode", imodes, val);
689 width = printf("\toptions:");
691 load_value(HCI_CMD_READ_SCAN_ENABLE, &val, sizeof(val));
692 if (val & HCI_INQUIRY_SCAN_ENABLE) tag("iscan");
693 else if (level > 0) tag("-iscan");
695 if (val & HCI_PAGE_SCAN_ENABLE) tag("pscan");
696 else if (level > 0) tag("-pscan");
698 load_value(HCI_CMD_READ_AUTH_ENABLE, &val, sizeof(val));
699 if (val) tag("auth");
700 else if (level > 0) tag("-auth");
702 load_value(HCI_CMD_READ_ENCRYPTION_MODE, &val, sizeof(val));
703 if (val) tag("encrypt");
704 else if (level > 0) tag("-encrypt");
706 val = btr.btr_link_policy;
707 if (val & HCI_LINK_POLICY_ENABLE_ROLE_SWITCH) tag("switch");
708 else if (level > 0) tag("-switch");
709 if (val & HCI_LINK_POLICY_ENABLE_HOLD_MODE) tag("hold");
710 else if (level > 0) tag("-hold");
711 if (val & HCI_LINK_POLICY_ENABLE_SNIFF_MODE) tag("sniff");
712 else if (level > 0) tag("-sniff");
713 if (val & HCI_LINK_POLICY_ENABLE_PARK_MODE) tag("park");
714 else if (level > 0) tag("-park");
716 tag(NULL);
718 if (level-- < 1)
719 return;
721 ptype = btr.btr_packet_type;
722 width = printf("\tptype: [0x%04x]", ptype);
723 if (ptype & HCI_PKT_DM1) tag("DM1");
724 if (ptype & HCI_PKT_DH1) tag("DH1");
725 if (ptype & HCI_PKT_DM3) tag("DM3");
726 if (ptype & HCI_PKT_DH3) tag("DH3");
727 if (ptype & HCI_PKT_DM5) tag("DM5");
728 if (ptype & HCI_PKT_DH5) tag("DH5");
729 if ((ptype & HCI_PKT_2MBPS_DH1) == 0) tag("2-DH1");
730 if ((ptype & HCI_PKT_3MBPS_DH1) == 0) tag("3-DH1");
731 if ((ptype & HCI_PKT_2MBPS_DH3) == 0) tag("2-DH3");
732 if ((ptype & HCI_PKT_3MBPS_DH3) == 0) tag("3-DH3");
733 if ((ptype & HCI_PKT_2MBPS_DH5) == 0) tag("2-DH5");
734 if ((ptype & HCI_PKT_3MBPS_DH5) == 0) tag("3-DH5");
735 tag(NULL);
737 load_value(HCI_CMD_READ_PAGE_TIMEOUT, buf, sizeof(uint16_t));
738 printf("\tpage timeout: %d ms\n", le16dec(buf) * 5 / 8);
740 if (level-- < 1)
741 return;
743 load_value(HCI_CMD_READ_LOCAL_FEATURES, buf, HCI_FEATURES_SIZE);
744 if ((buf[7] & HCI_LMP_EXTENDED_FEATURES) == 0) {
745 print_features("\tfeatures:", 0, buf);
746 } else {
747 hci_read_local_extended_features_rp rp;
749 rp.page = 0;
751 do {
752 hci_req(HCI_CMD_READ_LOCAL_EXTENDED_FEATURES, 0,
753 &rp.page, sizeof(rp.page), &rp, sizeof(rp));
755 print_features("\tfeatures (page %d):",
756 rp.page, rp.features);
757 } while (rp.page++ < rp.max_page);
761 void
762 print_stats(void)
765 if (sflag == 0)
766 return;
768 if (sflag == 1) {
769 if (ioctl(hci, SIOCGBTSTATS, &btr) < 0)
770 err(EXIT_FAILURE, "SIOCGBTSTATS");
771 } else {
772 if (ioctl(hci, SIOCZBTSTATS, &btr) < 0)
773 err(EXIT_FAILURE, "SIOCZBTSTATS");
776 printf( "\tTotal bytes sent %d, recieved %d\n"
777 "\tCommands sent %d, Events received %d\n"
778 "\tACL data packets sent %d, received %d\n"
779 "\tSCO data packets sent %d, received %d\n"
780 "\tInput errors %d, Output errors %d\n",
781 btr.btr_stats.byte_tx, btr.btr_stats.byte_rx,
782 btr.btr_stats.cmd_tx, btr.btr_stats.evt_rx,
783 btr.btr_stats.acl_tx, btr.btr_stats.acl_rx,
784 btr.btr_stats.sco_tx, btr.btr_stats.sco_rx,
785 btr.btr_stats.err_rx, btr.btr_stats.err_tx);
788 void
789 print_features(const char *fmt, uint8_t page, uint8_t *f)
792 width = printf(fmt, page);
794 switch(page) {
795 case 0: print_features0(f); break;
796 case 1: print_features1(f); break;
797 default: break;
800 tag(NULL);
803 void
804 print_features0(uint8_t *f)
807 /* ------------------- byte 0 --------------------*/
808 if (*f & HCI_LMP_3SLOT) tag("<3 slot>");
809 if (*f & HCI_LMP_5SLOT) tag("<5 slot>");
810 if (*f & HCI_LMP_ENCRYPTION) tag("<encryption>");
811 if (*f & HCI_LMP_SLOT_OFFSET) tag("<slot offset>");
812 if (*f & HCI_LMP_TIMIACCURACY) tag("<timing accuracy>");
813 if (*f & HCI_LMP_ROLE_SWITCH) tag("<role switch>");
814 if (*f & HCI_LMP_HOLD_MODE) tag("<hold mode>");
815 if (*f & HCI_LMP_SNIFF_MODE) tag("<sniff mode>");
816 f++;
818 /* ------------------- byte 1 --------------------*/
819 if (*f & HCI_LMP_PARK_MODE) tag("<park mode>");
820 if (*f & HCI_LMP_RSSI) tag("<RSSI>");
821 if (*f & HCI_LMP_CHANNEL_QUALITY) tag("<channel quality>");
822 if (*f & HCI_LMP_SCO_LINK) tag("<SCO link>");
823 if (*f & HCI_LMP_HV2_PKT) tag("<HV2>");
824 if (*f & HCI_LMP_HV3_PKT) tag("<HV3>");
825 if (*f & HCI_LMP_ULAW_LOG) tag("<u-Law log>");
826 if (*f & HCI_LMP_ALAW_LOG) tag("<A-Law log>");
827 f++;
829 /* ------------------- byte 1 --------------------*/
830 if (*f & HCI_LMP_CVSD) tag("<CVSD data>");
831 if (*f & HCI_LMP_PAGISCHEME) tag("<paging parameter>");
832 if (*f & HCI_LMP_POWER_CONTROL) tag("<power control>");
833 if (*f & HCI_LMP_TRANSPARENT_SCO) tag("<transparent SCO>");
834 if (*f & HCI_LMP_FLOW_CONTROL_LAG0) tag("<flow control lag 0>");
835 if (*f & HCI_LMP_FLOW_CONTROL_LAG1) tag("<flow control lag 1>");
836 if (*f & HCI_LMP_FLOW_CONTROL_LAG2) tag("<flow control lag 2>");
837 if (*f & HCI_LMP_BC_ENCRYPTION) tag("<broadcast encryption>");
838 f++;
840 /* ------------------- byte 3 --------------------*/
841 if (*f & HCI_LMP_EDR_ACL_2MBPS) tag("<EDR ACL 2Mbps>");
842 if (*f & HCI_LMP_EDR_ACL_3MBPS) tag("<EDR ACL 3Mbps>");
843 if (*f & HCI_LMP_ENHANCED_ISCAN) tag("<enhanced inquiry scan>");
844 if (*f & HCI_LMP_INTERLACED_ISCAN) tag("<interlaced inquiry scan>");
845 if (*f & HCI_LMP_INTERLACED_PSCAN) tag("<interlaced page scan>");
846 if (*f & HCI_LMP_RSSI_INQUIRY) tag("<RSSI with inquiry result>");
847 if (*f & HCI_LMP_EV3_PKT) tag("<EV3 packets>");
848 f++;
850 /* ------------------- byte 4 --------------------*/
851 if (*f & HCI_LMP_EV4_PKT) tag("<EV4 packets>");
852 if (*f & HCI_LMP_EV5_PKT) tag("<EV5 packets>");
853 if (*f & HCI_LMP_AFH_CAPABLE_SLAVE) tag("<AFH capable slave>");
854 if (*f & HCI_LMP_AFH_CLASS_SLAVE) tag("<AFH class slave>");
855 if (*f & HCI_LMP_3SLOT_EDR_ACL) tag("<3 slot EDR ACL>");
856 f++;
858 /* ------------------- byte 5 --------------------*/
859 if (*f & HCI_LMP_5SLOT_EDR_ACL) tag("<5 slot EDR ACL>");
860 if (*f & HCI_LMP_SNIFF_SUBRATING) tag("<sniff subrating>");
861 if (*f & HCI_LMP_PAUSE_ENCRYPTION) tag("<pause encryption>");
862 if (*f & HCI_LMP_AFH_CAPABLE_MASTER)tag("<AFH capable master>");
863 if (*f & HCI_LMP_AFH_CLASS_MASTER) tag("<AFH class master>");
864 if (*f & HCI_LMP_EDR_eSCO_2MBPS) tag("<EDR eSCO 2Mbps>");
865 if (*f & HCI_LMP_EDR_eSCO_3MBPS) tag("<EDR eSCO 3Mbps>");
866 if (*f & HCI_LMP_3SLOT_EDR_eSCO) tag("<3 slot EDR eSCO>");
867 f++;
869 /* ------------------- byte 6 --------------------*/
870 if (*f & HCI_LMP_EXTENDED_INQUIRY) tag("<extended inquiry>");
871 if (*f & HCI_LMP_SIMPLE_PAIRING) tag("<secure simple pairing>");
872 if (*f & HCI_LMP_ENCAPSULATED_PDU) tag("<encapsulated PDU>");
873 if (*f & HCI_LMP_ERRDATA_REPORTING) tag("<errdata reporting>");
874 if (*f & HCI_LMP_NOFLUSH_PB_FLAG) tag("<no flush PB flag>");
875 f++;
877 /* ------------------- byte 7 --------------------*/
878 if (*f & HCI_LMP_LINK_SUPERVISION_TO)tag("<link supervision timeout changed>");
879 if (*f & HCI_LMP_INQ_RSP_TX_POWER) tag("<inquiry rsp TX power level>");
880 if (*f & HCI_LMP_ENHANCED_POWER_CONTROL)tag("<enhanced power control>");
881 if (*f & HCI_LMP_EXTENDED_FEATURES) tag("<extended features>");
884 void
885 print_features1(uint8_t *f)
888 /* ------------------- byte 0 --------------------*/
889 if (*f & HCI_LMP_SSP) tag("<secure simple pairing>");
892 void
893 print_class(const char *str, uint8_t *uclass)
896 class = (uclass[2] << 16) | (uclass[1] << 8) | uclass[0];
897 width = printf("%s [0x%06x]", str, class);
899 switch(__SHIFTOUT(class, __BITS(0, 1))) {
900 case 0: print_class0(); break;
901 default: break;
904 tag(NULL);
907 void
908 print_class0(void)
911 switch (__SHIFTOUT(class, __BITS(8, 12))) {
912 case 1: /* Computer */
913 switch (__SHIFTOUT(class, __BITS(2, 7))) {
914 case 1: tag("Desktop workstation"); break;
915 case 2: tag("Server-class computer"); break;
916 case 3: tag("Laptop"); break;
917 case 4: tag("Handheld PC/PDA"); break;
918 case 5: tag("Palm Sized PC/PDA"); break;
919 case 6: tag("Wearable computer"); break;
920 default: tag("Computer"); break;
922 break;
924 case 2: /* Phone */
925 switch (__SHIFTOUT(class, __BITS(2, 7))) {
926 case 1: tag("Cellular Phone"); break;
927 case 2: tag("Cordless Phone"); break;
928 case 3: tag("Smart Phone"); break;
929 case 4: tag("Wired Modem/Phone Gateway"); break;
930 case 5: tag("Common ISDN"); break;
931 default:tag("Phone"); break;
933 break;
935 case 3: /* LAN */
936 tag("LAN");
937 switch (__SHIFTOUT(class, __BITS(5, 7))) {
938 case 0: tag("[Fully available]"); break;
939 case 1: tag("[1-17% utilised]"); break;
940 case 2: tag("[17-33% utilised]"); break;
941 case 3: tag("[33-50% utilised]"); break;
942 case 4: tag("[50-67% utilised]"); break;
943 case 5: tag("[67-83% utilised]"); break;
944 case 6: tag("[83-99% utilised]"); break;
945 case 7: tag("[No service available]"); break;
947 break;
949 case 4: /* Audio/Visual */
950 switch (__SHIFTOUT(class, __BITS(2, 7))) {
951 case 1: tag("Wearable Headset"); break;
952 case 2: tag("Hands-free Audio"); break;
953 case 4: tag("Microphone"); break;
954 case 5: tag("Loudspeaker"); break;
955 case 6: tag("Headphones"); break;
956 case 7: tag("Portable Audio"); break;
957 case 8: tag("Car Audio"); break;
958 case 9: tag("Set-top Box"); break;
959 case 10: tag("HiFi Audio"); break;
960 case 11: tag("VCR"); break;
961 case 12: tag("Video Camera"); break;
962 case 13: tag("Camcorder"); break;
963 case 14: tag("Video Monitor"); break;
964 case 15: tag("Video Display and Loudspeaker"); break;
965 case 16: tag("Video Conferencing"); break;
966 case 18: tag("A/V [Gaming/Toy]"); break;
967 default: tag("Audio/Visual"); break;
969 break;
971 case 5: /* Peripheral */
972 switch (__SHIFTOUT(class, __BITS(2, 5))) {
973 case 1: tag("Joystick"); break;
974 case 2: tag("Gamepad"); break;
975 case 3: tag("Remote Control"); break;
976 case 4: tag("Sensing Device"); break;
977 case 5: tag("Digitiser Tablet"); break;
978 case 6: tag("Card Reader"); break;
979 default: tag("Peripheral"); break;
982 if (class & __BIT(6)) tag("Keyboard");
983 if (class & __BIT(7)) tag("Mouse");
984 break;
986 case 6: /* Imaging */
987 if (class & __BIT(4)) tag("Display");
988 if (class & __BIT(5)) tag("Camera");
989 if (class & __BIT(6)) tag("Scanner");
990 if (class & __BIT(7)) tag("Printer");
991 if ((class & __BITS(4, 7)) == 0) tag("Imaging");
992 break;
994 case 7: /* Wearable */
995 switch (__SHIFTOUT(class, __BITS(2, 7))) {
996 case 1: tag("Wrist Watch"); break;
997 case 2: tag("Pager"); break;
998 case 3: tag("Jacket"); break;
999 case 4: tag("Helmet"); break;
1000 case 5: tag("Glasses"); break;
1001 default: tag("Wearable"); break;
1003 break;
1005 case 8: /* Toy */
1006 switch (__SHIFTOUT(class, __BITS(2, 7))) {
1007 case 1: tag("Robot"); break;
1008 case 2: tag("Vehicle"); break;
1009 case 3: tag("Doll / Action Figure"); break;
1010 case 4: tag("Controller"); break;
1011 case 5: tag("Game"); break;
1012 default: tag("Toy"); break;
1014 break;
1016 case 9: /* Health */
1017 switch (__SHIFTOUT(class, __BITS(2, 7))) {
1018 case 1: tag("Blood Pressure Monitor"); break;
1019 case 2: tag("Thermometer"); break;
1020 case 3: tag("Weighing Scale"); break;
1021 case 4: tag("Glucose Meter"); break;
1022 case 5: tag("Pulse Oximeter"); break;
1023 case 6: tag("Heart/Pulse Rate Monitor"); break;
1024 case 7: tag("Health Data Display"); break;
1025 default: tag("Health"); break;
1027 break;
1029 default:
1030 break;
1033 if (class & __BIT(13)) tag("<Limited Discoverable>");
1034 if (class & __BIT(16)) tag("<Positioning>");
1035 if (class & __BIT(17)) tag("<Networking>");
1036 if (class & __BIT(18)) tag("<Rendering>");
1037 if (class & __BIT(19)) tag("<Capturing>");
1038 if (class & __BIT(20)) tag("<Object Transfer>");
1039 if (class & __BIT(21)) tag("<Audio>");
1040 if (class & __BIT(22)) tag("<Telephony>");
1041 if (class & __BIT(23)) tag("<Information>");
1044 void
1045 print_voice(int level)
1047 printf("\tvoice: [0x%4.4x]\n", voice);
1049 if (level == 0)
1050 return;
1052 printf("\t\tInput Coding: ");
1053 switch ((voice & 0x0300) >> 8) {
1054 case 0x00: printf("Linear PCM [%d-bit, pos %d]",
1055 (voice & 0x0020 ? 16 : 8),
1056 (voice & 0x001c) >> 2); break;
1057 case 0x01: printf("u-Law"); break;
1058 case 0x02: printf("A-Law"); break;
1059 case 0x03: printf("unknown"); break;
1062 switch ((voice & 0x00c0) >> 6) {
1063 case 0x00: printf(", 1's complement"); break;
1064 case 0x01: printf(", 2's complement"); break;
1065 case 0x02: printf(", sign magnitude"); break;
1066 case 0x03: printf(", unsigned"); break;
1069 printf("\n\t\tAir Coding: ");
1070 switch (voice & 0x0003) {
1071 case 0x00: printf("CVSD"); break;
1072 case 0x01: printf("u-Law"); break;
1073 case 0x02: printf("A-Law"); break;
1074 case 0x03: printf("Transparent"); break;
1077 printf("\n");
1080 void
1081 print_result(int num, struct bt_devinquiry *r)
1083 hci_remote_name_req_cp ncp;
1084 hci_remote_name_req_compl_ep nep;
1085 struct hostent *hp;
1087 printf("%3d: bdaddr %s", num, bt_ntoa(&r->bdaddr, NULL));
1089 hp = bt_gethostbyaddr((const char *)&r->bdaddr, sizeof(bdaddr_t), AF_BLUETOOTH);
1090 if (hp != NULL)
1091 printf(" (%s)", hp->h_name);
1093 printf("\n");
1095 memset(&ncp, 0, sizeof(ncp));
1096 bdaddr_copy(&ncp.bdaddr, &r->bdaddr);
1097 ncp.page_scan_rep_mode = r->pscan_rep_mode;
1098 ncp.clock_offset = r->clock_offset;
1100 hci_req(HCI_CMD_REMOTE_NAME_REQ,
1101 HCI_EVENT_REMOTE_NAME_REQ_COMPL,
1102 &ncp, sizeof(ncp),
1103 &nep, sizeof(nep));
1105 printf(" : name \"%s\"\n", nep.name);
1106 print_class(" : class", r->dev_class);
1107 printf(" : page scan rep mode 0x%02x\n", r->pscan_rep_mode);
1108 printf(" : clock offset %d\n", le16toh(r->clock_offset));
1109 printf(" : rssi %d\n", r->rssi);
1110 printf("\n");
1113 void
1114 do_inquiry(void)
1116 struct bt_devinquiry *result;
1117 int i, num;
1119 if (opt_inquiry == 0)
1120 return;
1122 printf("Device Discovery from device: %s ...", btr.btr_name);
1123 fflush(stdout);
1125 num = bt_devinquiry(btr.btr_name, INQUIRY_LENGTH,
1126 INQUIRY_MAX_RESPONSES, &result);
1128 if (num == -1) {
1129 printf("failed\n");
1130 err(EXIT_FAILURE, "%s", btr.btr_name);
1133 printf(" %d response%s\n", num, (num == 1 ? "" : "s"));
1135 for (i = 0 ; i < num ; i++)
1136 print_result(i + 1, &result[i]);
1138 free(result);