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