Home | History | Annotate | Line # | Download | only in btconfig
btconfig.c revision 1.17
      1 /* $NetBSD: btconfig.c,v 1.17 2009/09/11 19:22:15 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.17 2009/09/11 19:22:15 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 *);
     71 void print_voice(int);
     72 void tag(const char *);
     73 void print_features(const char *, uint8_t, uint8_t *);
     74 void print_features0(uint8_t *);
     75 void print_features1(uint8_t *);
     76 void do_inquiry(void);
     77 void print_result(int, struct result *, int);
     78 
     79 void hci_req(uint16_t, uint8_t , void *, size_t, void *, size_t);
     80 #define save_value(opcode, cbuf, clen)	hci_req(opcode, 0, cbuf, clen, NULL, 0)
     81 #define load_value(opcode, rbuf, rlen)	hci_req(opcode, 0, NULL, 0, rbuf, rlen)
     82 #define hci_cmd(opcode, cbuf, clen)	hci_req(opcode, 0, cbuf, clen, NULL, 0)
     83 
     84 #define MAX_STR_SIZE	0xff
     85 
     86 /* print width */
     87 int width = 0;
     88 #define MAX_WIDTH	70
     89 
     90 /* global variables */
     91 int hci;
     92 struct btreq btr;
     93 
     94 /* command line flags */
     95 int verbose = 0;	/* more info */
     96 int lflag = 0;		/* list devices */
     97 int sflag = 0;		/* get/zero stats */
     98 
     99 /* device up/down (flag) */
    100 int opt_enable = 0;
    101 int opt_reset = 0;
    102 #define FLAGS_FMT	"\20"			\
    103 			"\001UP"		\
    104 			"\002RUNNING"		\
    105 			"\003XMIT_CMD"		\
    106 			"\004XMIT_ACL"		\
    107 			"\005XMIT_SCO"		\
    108 			"\006INIT_BDADDR"	\
    109 			"\007INIT_BUFFER_SIZE"	\
    110 			"\010INIT_FEATURES"	\
    111 			"\011POWER_UP_NOOP"	\
    112 			"\012INIT_COMMANDS"	\
    113 			"\013MASTER"		\
    114 			""
    115 
    116 /* authorisation (flag) */
    117 int opt_auth = 0;
    118 
    119 /* encryption (flag) */
    120 int opt_encrypt = 0;
    121 
    122 /* scan enable options (flags) */
    123 int opt_pscan = 0;
    124 int opt_iscan = 0;
    125 
    126 /* master role option */
    127 int opt_master = 0;
    128 
    129 /* link policy options (flags) */
    130 int opt_switch = 0;
    131 int opt_hold = 0;
    132 int opt_sniff = 0;
    133 int opt_park = 0;
    134 
    135 /* class of device (hex value) */
    136 int opt_class = 0;
    137 uint32_t class;
    138 
    139 /* packet type mask (hex value) */
    140 int opt_ptype = 0;
    141 uint32_t ptype;
    142 
    143 /* unit name (string) */
    144 int opt_name = 0;
    145 char name[MAX_STR_SIZE];
    146 
    147 /* pin type */
    148 int opt_pin = 0;
    149 
    150 /* Inquiry */
    151 int opt_rssi = 0;			/* inquiry_with_rssi (obsolete flag) */
    152 int opt_imode = 0;			/* inquiry mode */
    153 int opt_inquiry = 0;
    154 #define INQUIRY_LENGTH		10	/* about 12 seconds */
    155 #define INQUIRY_MAX_RESPONSES	10
    156 const char *imodes[] = { "std", "rssi", "ext", NULL };
    157 
    158 /* Voice Settings */
    159 int opt_voice = 0;
    160 uint32_t voice;
    161 
    162 /* Page Timeout */
    163 int opt_pto = 0;
    164 uint32_t pto;
    165 
    166 /* set SCO mtu */
    167 int opt_scomtu;
    168 uint32_t scomtu;
    169 
    170 struct parameter {
    171 	const char	*name;
    172 	enum { P_SET, P_CLR, P_STR, P_HEX, P_NUM, P_VAL } type;
    173 	int		*opt;
    174 	void		*val;
    175 } parameters[] = {
    176 	{ "up",		P_SET,	&opt_enable,	NULL	},
    177 	{ "enable",	P_SET,	&opt_enable,	NULL	},
    178 	{ "down",	P_CLR,	&opt_enable,	NULL	},
    179 	{ "disable",	P_CLR,	&opt_enable,	NULL	},
    180 	{ "name",	P_STR,	&opt_name,	name	},
    181 	{ "pscan",	P_SET,	&opt_pscan,	NULL	},
    182 	{ "-pscan",	P_CLR,	&opt_pscan,	NULL	},
    183 	{ "iscan",	P_SET,	&opt_iscan,	NULL	},
    184 	{ "-iscan",	P_CLR,	&opt_iscan,	NULL	},
    185 	{ "master",	P_SET,	&opt_master,	NULL	},
    186 	{ "-master",	P_CLR,	&opt_master,	NULL	},
    187 	{ "switch",	P_SET,	&opt_switch,	NULL	},
    188 	{ "-switch",	P_CLR,	&opt_switch,	NULL	},
    189 	{ "hold",	P_SET,	&opt_hold,	NULL	},
    190 	{ "-hold",	P_CLR,	&opt_hold,	NULL	},
    191 	{ "sniff",	P_SET,	&opt_sniff,	NULL	},
    192 	{ "-sniff",	P_CLR,	&opt_sniff,	NULL	},
    193 	{ "park",	P_SET,	&opt_park,	NULL	},
    194 	{ "-park",	P_CLR,	&opt_park,	NULL	},
    195 	{ "auth",	P_SET,	&opt_auth,	NULL	},
    196 	{ "-auth",	P_CLR,	&opt_auth,	NULL	},
    197 	{ "encrypt",	P_SET,	&opt_encrypt,	NULL	},
    198 	{ "-encrypt",	P_CLR,	&opt_encrypt,	NULL	},
    199 	{ "ptype",	P_HEX,	&opt_ptype,	&ptype	},
    200 	{ "class",	P_HEX,	&opt_class,	&class	},
    201 	{ "fixed",	P_SET,	&opt_pin,	NULL	},
    202 	{ "variable",	P_CLR,	&opt_pin,	NULL	},
    203 	{ "inq",	P_SET,	&opt_inquiry,	NULL	},
    204 	{ "inquiry",	P_SET,	&opt_inquiry,	NULL	},
    205 	{ "imode",	P_VAL,	&opt_imode,	imodes	},
    206 	{ "rssi",	P_SET,	&opt_rssi,	NULL	},
    207 	{ "-rssi",	P_CLR,	&opt_rssi,	NULL	},
    208 	{ "reset",	P_SET,	&opt_reset,	NULL	},
    209 	{ "voice",	P_HEX,	&opt_voice,	&voice	},
    210 	{ "pto",	P_NUM,	&opt_pto,	&pto	},
    211 	{ "scomtu",	P_NUM,	&opt_scomtu,	&scomtu	},
    212 	{ NULL,		0,	NULL,		NULL	}
    213 };
    214 
    215 int
    216 main(int ac, char *av[])
    217 {
    218 	int ch;
    219 	struct parameter *p;
    220 
    221 	while ((ch = getopt(ac, av, "hlsvz")) != -1) {
    222 		switch(ch) {
    223 		case 'l':
    224 			lflag = 1;
    225 			break;
    226 
    227 		case 's':
    228 			sflag = 1;
    229 			break;
    230 
    231 		case 'v':
    232 			verbose++;
    233 			break;
    234 
    235 		case 'z':
    236 			sflag = 2;
    237 			break;
    238 
    239 		case 'h':
    240 		default:
    241 			usage();
    242 		}
    243 	}
    244 	av += optind;
    245 	ac -= optind;
    246 
    247 	if (lflag && sflag)
    248 		usage();
    249 
    250 	hci = socket(PF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
    251 	if (hci == -1)
    252 		err(EXIT_FAILURE, "socket");
    253 
    254 	if (ac == 0) {
    255 		verbose++;
    256 
    257 		memset(&btr, 0, sizeof(btr));
    258 		while (set_unit(SIOCNBTINFO) != -1) {
    259 			print_info(verbose);
    260 			print_stats();
    261 		}
    262 
    263 		tag(NULL);
    264 	} else {
    265 		strlcpy(btr.btr_name, *av, HCI_DEVNAME_SIZE);
    266 		av++, ac--;
    267 
    268 		if (set_unit(SIOCGBTINFO) < 0)
    269 			err(EXIT_FAILURE, "%s", btr.btr_name);
    270 
    271 		if (ac == 0)
    272 			verbose += 2;
    273 
    274 		while (ac > 0) {
    275 			for (p = parameters ; ; p++) {
    276 				if (p->name == NULL)
    277 					badparam(*av);
    278 
    279 				if (strcmp(*av, p->name) == 0)
    280 					break;
    281 			}
    282 
    283 			switch(p->type) {
    284 			case P_SET:
    285 				*(p->opt) = 1;
    286 				break;
    287 
    288 			case P_CLR:
    289 				*(p->opt) = -1;
    290 				break;
    291 
    292 			case P_STR:
    293 				if (--ac < 1) badarg(p->name);
    294 				strlcpy((char *)(p->val), *++av, MAX_STR_SIZE);
    295 				*(p->opt) = 1;
    296 				break;
    297 
    298 			case P_HEX:
    299 				if (--ac < 1) badarg(p->name);
    300 				*(uint32_t *)(p->val) = strtoul(*++av, NULL, 16);
    301 				*(p->opt) = 1;
    302 				break;
    303 
    304 			case P_NUM:
    305 				if (--ac < 1) badarg(p->name);
    306 				*(uint32_t *)(p->val) = strtoul(*++av, NULL, 10);
    307 				*(p->opt) = 1;
    308 				break;
    309 
    310 			case P_VAL:
    311 				if (--ac < 1) badarg(p->name);
    312 				++av;
    313 				ch = 0;
    314 				do {
    315 					if (((char **)(p->val))[ch] == NULL)
    316 						badval(p->name, *av);
    317 				} while (strcmp(((char **)(p->val))[ch++], *av));
    318 				*(p->opt) = ch;
    319 				break;
    320 			}
    321 
    322 			av++, ac--;
    323 		}
    324 
    325 		config_unit();
    326 		print_info(verbose);
    327 		print_stats();
    328 		do_inquiry();
    329 	}
    330 
    331 	close(hci);
    332 	return EXIT_SUCCESS;
    333 }
    334 
    335 void
    336 badparam(const char *param)
    337 {
    338 
    339 	fprintf(stderr, "unknown parameter '%s'\n", param);
    340 	exit(EXIT_FAILURE);
    341 }
    342 
    343 void
    344 badarg(const char *param)
    345 {
    346 
    347 	fprintf(stderr, "parameter '%s' needs argument\n", param);
    348 	exit(EXIT_FAILURE);
    349 }
    350 
    351 void
    352 badval(const char *param, const char *value)
    353 {
    354 
    355 	fprintf(stderr, "bad value '%s' for parameter '%s'\n", value, param);
    356 	exit(EXIT_FAILURE);
    357 }
    358 
    359 void
    360 usage(void)
    361 {
    362 
    363 	fprintf(stderr, "usage:	%s [-svz] [device [parameters]]\n", getprogname());
    364 	fprintf(stderr, "	%s -l\n", getprogname());
    365 	exit(EXIT_FAILURE);
    366 }
    367 
    368 /*
    369  * pretty printing feature
    370  */
    371 void
    372 tag(const char *f)
    373 {
    374 
    375 	if (f == NULL) {
    376 		if (width > 0)
    377 			printf("\n");
    378 
    379 		width = 0;
    380 	} else {
    381 		width += printf("%*s%s",
    382 				(width == 0 ? (lflag ? 0 : 8) : 1),
    383 				"", f);
    384 
    385 		if (width > MAX_WIDTH) {
    386 			printf("\n");
    387 			width = 0;
    388 		}
    389 	}
    390 }
    391 
    392 /*
    393  * basic HCI cmd request function with argument return.
    394  *
    395  * Normally, this will return on COMMAND_STATUS or COMMAND_COMPLETE for the given
    396  * opcode, but if event is given then it will ignore COMMAND_STATUS (unless error)
    397  * and wait for the specified event.
    398  *
    399  * if rbuf/rlen is given, results will be copied into the result buffer for
    400  * COMMAND_COMPLETE/event responses.
    401  */
    402 void
    403 hci_req(uint16_t opcode, uint8_t event, void *cbuf, size_t clen, void *rbuf, size_t rlen)
    404 {
    405 	uint8_t msg[sizeof(hci_cmd_hdr_t) + HCI_CMD_PKT_SIZE];
    406 	hci_event_hdr_t *ep;
    407 	hci_cmd_hdr_t *cp;
    408 
    409 	cp = (hci_cmd_hdr_t *)msg;
    410 	cp->type = HCI_CMD_PKT;
    411 	cp->opcode = opcode = htole16(opcode);
    412 	cp->length = clen = MIN(clen, sizeof(msg) - sizeof(hci_cmd_hdr_t));
    413 
    414 	if (clen) memcpy((cp + 1), cbuf, clen);
    415 
    416 	if (send(hci, msg, sizeof(hci_cmd_hdr_t) + clen, 0) < 0)
    417 		err(EXIT_FAILURE, "HCI Send");
    418 
    419 	ep = (hci_event_hdr_t *)msg;
    420 	for(;;) {
    421 		if (recv(hci, msg, sizeof(msg), 0) < 0) {
    422 			if (errno == EAGAIN || errno == EINTR)
    423 				continue;
    424 
    425 			err(EXIT_FAILURE, "HCI Recv");
    426 		}
    427 
    428 		if (ep->event == HCI_EVENT_COMMAND_STATUS) {
    429 			hci_command_status_ep *cs;
    430 
    431 			cs = (hci_command_status_ep *)(ep + 1);
    432 			if (cs->opcode != opcode)
    433 				continue;
    434 
    435 			if (cs->status)
    436 				errx(EXIT_FAILURE,
    437 				    "HCI cmd (%4.4x) failed (status %d)",
    438 				    opcode, cs->status);
    439 
    440 			if (event == 0)
    441 				break;
    442 
    443 			continue;
    444 		}
    445 
    446 		if (ep->event == HCI_EVENT_COMMAND_COMPL) {
    447 			hci_command_compl_ep *cc;
    448 			uint8_t *ptr;
    449 
    450 			cc = (hci_command_compl_ep *)(ep + 1);
    451 			if (cc->opcode != opcode)
    452 				continue;
    453 
    454 			if (rbuf == NULL)
    455 				break;
    456 
    457 			ptr = (uint8_t *)(cc + 1);
    458 			if (*ptr)
    459 				errx(EXIT_FAILURE,
    460 				    "HCI cmd (%4.4x) failed (status %d)",
    461 				    opcode, *ptr);
    462 
    463 			memcpy(rbuf, ++ptr, rlen);
    464 			break;
    465 		}
    466 
    467 		if (ep->event == event) {
    468 			if (rbuf == NULL)
    469 				break;
    470 
    471 			memcpy(rbuf, (ep + 1), rlen);
    472 			break;
    473 		}
    474 	}
    475 }
    476 
    477 int
    478 set_unit(unsigned long cmd)
    479 {
    480 
    481 	if (ioctl(hci, cmd, &btr) == -1)
    482 		return -1;
    483 
    484 	if (btr.btr_flags & BTF_UP) {
    485 		struct sockaddr_bt sa;
    486 
    487 		sa.bt_len = sizeof(sa);
    488 		sa.bt_family = AF_BLUETOOTH;
    489 		bdaddr_copy(&sa.bt_bdaddr, &btr.btr_bdaddr);
    490 
    491 		if (bind(hci, (struct sockaddr *)&sa, sizeof(sa)) < 0)
    492 			err(EXIT_FAILURE, "bind");
    493 
    494 		if (connect(hci, (struct sockaddr *)&sa, sizeof(sa)) < 0)
    495 			err(EXIT_FAILURE, "connect");
    496 	}
    497 
    498 	return 0;
    499 }
    500 
    501 /*
    502  * apply configuration parameters to unit
    503  */
    504 void
    505 config_unit(void)
    506 {
    507 
    508 	if (opt_enable) {
    509 		if (opt_enable > 0)
    510 			btr.btr_flags |= BTF_UP;
    511 		else
    512 			btr.btr_flags &= ~BTF_UP;
    513 
    514 		if (ioctl(hci, SIOCSBTFLAGS, &btr) < 0)
    515 			err(EXIT_FAILURE, "SIOCSBTFLAGS");
    516 
    517 		if (set_unit(SIOCGBTINFO) < 0)
    518 			err(EXIT_FAILURE, "%s", btr.btr_name);
    519 	}
    520 
    521 	if (opt_reset) {
    522 		hci_cmd(HCI_CMD_RESET, NULL, 0);
    523 
    524 		btr.btr_flags |= BTF_INIT;
    525 		if (ioctl(hci, SIOCSBTFLAGS, &btr) < 0)
    526 			err(EXIT_FAILURE, "SIOCSBTFLAGS");
    527 
    528 		/*
    529 		 * although the reset command will automatically
    530 		 * carry out these commands, we do them manually
    531 		 * just so we can wait for completion.
    532 		 */
    533 		hci_cmd(HCI_CMD_READ_BDADDR, NULL, 0);
    534 		hci_cmd(HCI_CMD_READ_BUFFER_SIZE, NULL, 0);
    535 		hci_cmd(HCI_CMD_READ_LOCAL_FEATURES, NULL, 0);
    536 
    537 		if (set_unit(SIOCGBTINFO) < 0)
    538 			err(EXIT_FAILURE, "%s", btr.btr_name);
    539 	}
    540 
    541 	if (opt_master) {
    542 		if (opt_master > 0)
    543 			btr.btr_flags |= BTF_MASTER;
    544 		else
    545 			btr.btr_flags &= ~BTF_MASTER;
    546 
    547 		if (ioctl(hci, SIOCSBTFLAGS, &btr) < 0)
    548 			err(EXIT_FAILURE, "SIOCSBTFLAGS");
    549 	}
    550 
    551 	if (opt_switch || opt_hold || opt_sniff || opt_park) {
    552 		uint16_t val = btr.btr_link_policy;
    553 
    554 		if (opt_switch > 0) val |= HCI_LINK_POLICY_ENABLE_ROLE_SWITCH;
    555 		if (opt_switch < 0) val &= ~HCI_LINK_POLICY_ENABLE_ROLE_SWITCH;
    556 		if (opt_hold > 0)   val |= HCI_LINK_POLICY_ENABLE_HOLD_MODE;
    557 		if (opt_hold < 0)   val &= ~HCI_LINK_POLICY_ENABLE_HOLD_MODE;
    558 		if (opt_sniff > 0)  val |= HCI_LINK_POLICY_ENABLE_SNIFF_MODE;
    559 		if (opt_sniff < 0)  val &= ~HCI_LINK_POLICY_ENABLE_SNIFF_MODE;
    560 		if (opt_park > 0)   val |= HCI_LINK_POLICY_ENABLE_PARK_MODE;
    561 		if (opt_park < 0)   val &= ~HCI_LINK_POLICY_ENABLE_PARK_MODE;
    562 
    563 		btr.btr_link_policy = val;
    564 		if (ioctl(hci, SIOCSBTPOLICY, &btr) < 0)
    565 			err(EXIT_FAILURE, "SIOCSBTPOLICY");
    566 	}
    567 
    568 	if (opt_ptype) {
    569 		btr.btr_packet_type = ptype;
    570 		if (ioctl(hci, SIOCSBTPTYPE, &btr) < 0)
    571 			err(EXIT_FAILURE, "SIOCSBTPTYPE");
    572 	}
    573 
    574 	if (opt_pscan || opt_iscan) {
    575 		uint8_t val;
    576 
    577 		load_value(HCI_CMD_READ_SCAN_ENABLE, &val, sizeof(val));
    578 		if (opt_pscan > 0) val |= HCI_PAGE_SCAN_ENABLE;
    579 		if (opt_pscan < 0) val &= ~HCI_PAGE_SCAN_ENABLE;
    580 		if (opt_iscan > 0) val |= HCI_INQUIRY_SCAN_ENABLE;
    581 		if (opt_iscan < 0) val &= ~HCI_INQUIRY_SCAN_ENABLE;
    582 		save_value(HCI_CMD_WRITE_SCAN_ENABLE, &val, sizeof(val));
    583 	}
    584 
    585 	if (opt_auth) {
    586 		uint8_t val = (opt_auth > 0 ? 1 : 0);
    587 
    588 		save_value(HCI_CMD_WRITE_AUTH_ENABLE, &val, sizeof(val));
    589 	}
    590 
    591 	if (opt_encrypt) {
    592 		uint8_t val = (opt_encrypt > 0 ? 1 : 0);
    593 
    594 		save_value(HCI_CMD_WRITE_ENCRYPTION_MODE, &val, sizeof(val));
    595 	}
    596 
    597 	if (opt_name)
    598 		save_value(HCI_CMD_WRITE_LOCAL_NAME, name, HCI_UNIT_NAME_SIZE);
    599 
    600 	if (opt_class) {
    601 		uint8_t val[HCI_CLASS_SIZE];
    602 
    603 		val[0] = (class >> 0) & 0xff;
    604 		val[1] = (class >> 8) & 0xff;
    605 		val[2] = (class >> 16) & 0xff;
    606 
    607 		save_value(HCI_CMD_WRITE_UNIT_CLASS, val, HCI_CLASS_SIZE);
    608 	}
    609 
    610 	if (opt_pin) {
    611 		uint8_t val;
    612 
    613 		if (opt_pin > 0)	val = 1;
    614 		else			val = 0;
    615 
    616 		save_value(HCI_CMD_WRITE_PIN_TYPE, &val, sizeof(val));
    617 	}
    618 
    619 	if (opt_voice) {
    620 		uint16_t val;
    621 
    622 		val = htole16(voice & 0x03ff);
    623 		save_value(HCI_CMD_WRITE_VOICE_SETTING, &val, sizeof(val));
    624 	}
    625 
    626 	if (opt_pto) {
    627 		uint16_t val;
    628 
    629 		val = htole16(pto * 8 / 5);
    630 		save_value(HCI_CMD_WRITE_PAGE_TIMEOUT, &val, sizeof(val));
    631 	}
    632 
    633 	if (opt_scomtu) {
    634 		if (scomtu > 0xff) {
    635 			warnx("Invalid SCO mtu %d", scomtu);
    636 		} else {
    637 			btr.btr_sco_mtu = scomtu;
    638 
    639 			if (ioctl(hci, SIOCSBTSCOMTU, &btr) < 0)
    640 				warn("SIOCSBTSCOMTU");
    641 		}
    642 	}
    643 
    644 	if (opt_imode | opt_rssi) {
    645 		uint8_t val = (opt_rssi > 0 ? 1 : 0);
    646 
    647 		if (opt_imode)
    648 			val = opt_imode - 1;
    649 
    650 		save_value(HCI_CMD_WRITE_INQUIRY_MODE, &val, sizeof(val));
    651 	}
    652 }
    653 
    654 /*
    655  * print value from NULL terminated array given index
    656  */
    657 void
    658 print_val(const char *hdr, const char **argv, int idx)
    659 {
    660 	int i = 0;
    661 
    662 	while (i < idx && *argv != NULL)
    663 		i++, argv++;
    664 
    665 	printf("\t%s: %s\n", hdr, *argv == NULL ? "unknown" : *argv);
    666 }
    667 
    668 /*
    669  * Print info for Bluetooth Device with varying verbosity levels
    670  */
    671 void
    672 print_info(int level)
    673 {
    674 	uint8_t version, val, buf[MAX_STR_SIZE];
    675 	uint16_t val16;
    676 
    677 	if (lflag) {
    678 		tag(btr.btr_name);
    679 		return;
    680 	}
    681 
    682 	if (level-- < 1)
    683 		return;
    684 
    685 	snprintb((char *)buf, MAX_STR_SIZE, FLAGS_FMT, btr.btr_flags);
    686 
    687 	printf("%s: bdaddr %s flags %s\n", btr.btr_name,
    688 		bt_ntoa(&btr.btr_bdaddr, NULL), buf);
    689 
    690 	if (level-- < 1)
    691 		return;
    692 
    693 	printf("\tnum_cmd = %d\n"
    694 	       "\tnum_acl = %d, acl_mtu = %d\n"
    695 	       "\tnum_sco = %d, sco_mtu = %d\n",
    696 	       btr.btr_num_cmd,
    697 	       btr.btr_num_acl, btr.btr_acl_mtu,
    698 	       btr.btr_num_sco, btr.btr_sco_mtu);
    699 
    700 	if (level-- < 1 || (btr.btr_flags & BTF_UP) == 0)
    701 		return;
    702 
    703 	load_value(HCI_CMD_READ_LOCAL_VER, &version, sizeof(version));
    704 	printf("\tHCI version: ");
    705 	switch(version) {
    706 	case HCI_SPEC_V10:	printf("1.0b\n");	break;
    707 	case HCI_SPEC_V11:	printf("1.1\n");	break;
    708 	case HCI_SPEC_V12:	printf("1.2\n");	break;
    709 	case HCI_SPEC_V20:	printf("2.0 + EDR\n");	break;
    710 	case HCI_SPEC_V21:	printf("2.1 + EDR\n");	break;
    711 	case HCI_SPEC_V30:	printf("3.0 + HS\n");	break;
    712 	default:		printf("unknown\n");	break;
    713 	}
    714 
    715 	load_value(HCI_CMD_READ_UNIT_CLASS, buf, HCI_CLASS_SIZE);
    716 	class = (buf[2] << 16) | (buf[1] << 8) | (buf[0]);
    717 	print_class("\t");
    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)
    938 {
    939 	int major, minor;
    940 
    941 	major = (class & 0x1f00) >> 8;
    942 	minor = (class & 0x00fc) >> 2;
    943 
    944 	width = printf("%sclass: [0x%6.6x]", str, class);
    945 
    946 	switch (major) {
    947 	case 1:	/* Computer */
    948 		switch (minor) {
    949 		case 1: tag("Desktop");				break;
    950 		case 2: tag("Server");				break;
    951 		case 3: tag("Laptop");				break;
    952 		case 4: tag("Handheld");			break;
    953 		case 5: tag("Palm Sized");			break;
    954 		case 6: tag("Wearable");			break;
    955 		}
    956 		tag("Computer");
    957 		break;
    958 
    959 	case 2:	/* Phone */
    960 		switch (minor) {
    961 		case 1: tag("Cellular Phone");			break;
    962 		case 2: tag("Cordless Phone");			break;
    963 		case 3: tag("Smart Phone");			break;
    964 		case 4: tag("Wired Modem/Phone Gateway");	break;
    965 		case 5: tag("Common ISDN");			break;
    966 		default:tag("Phone");				break;
    967 		}
    968 		break;
    969 
    970 	case 3:	/* LAN */
    971 		tag("LAN");
    972 		switch ((minor & 0x38) >> 3) {
    973 		case 0: tag("[Fully available]");		break;
    974 		case 1: tag("[1-17% utilised]");		break;
    975 		case 2: tag("[17-33% utilised]");		break;
    976 		case 3: tag("[33-50% utilised]");		break;
    977 		case 4: tag("[50-67% utilised]");		break;
    978 		case 5: tag("[67-83% utilised]");		break;
    979 		case 6: tag("[83-99% utilised]");		break;
    980 		case 7: tag("[No service available]");		break;
    981 		}
    982 		break;
    983 
    984 	case 4:	/* Audio/Visual */
    985 		switch (minor) {
    986 		case 1: tag("Wearable Headset");		break;
    987 		case 2: tag("Hands-free Audio");		break;
    988 		case 4: tag("Microphone");			break;
    989 		case 5: tag("Loudspeaker");			break;
    990 		case 6: tag("Headphones");			break;
    991 		case 7: tag("Portable Audio");			break;
    992 		case 8: tag("Car Audio");			break;
    993 		case 9: tag("Set-top Box");			break;
    994 		case 10: tag("HiFi Audio");			break;
    995 		case 11: tag("VCR");				break;
    996 		case 12: tag("Video Camera");			break;
    997 		case 13: tag("Camcorder");			break;
    998 		case 14: tag("Video Monitor");			break;
    999 		case 15: tag("Video Display and Loudspeaker");	break;
   1000 		case 16: tag("Video Conferencing");		break;
   1001 		case 18: tag("A/V [Gaming/Toy]");		break;
   1002 		default: tag("Audio/Visual");			break;
   1003 		}
   1004 		break;
   1005 
   1006 	case 5:	/* Peripheral */
   1007 		switch (minor & 0x0f) {
   1008 		case 1: tag("Joystick");			break;
   1009 		case 2: tag("Gamepad");				break;
   1010 		case 3: tag("Remote Control");			break;
   1011 		case 4: tag("Sensing Device");			break;
   1012 		case 5: tag("Digitiser Tablet");		break;
   1013 		case 6: tag("Card Reader");			break;
   1014 		default: tag("Peripheral");			break;
   1015 		}
   1016 
   1017 		if (minor & 0x10) tag("Keyboard");
   1018 		if (minor & 0x20) tag("Mouse");
   1019 		break;
   1020 
   1021 	case 6:	/* Imaging */
   1022 		if (minor & 0x20) tag("Printer");
   1023 		if (minor & 0x10) tag("Scanner");
   1024 		if (minor & 0x08) tag("Camera");
   1025 		if (minor & 0x04) tag("Display");
   1026 		if ((minor & 0x3c) == 0) tag("Imaging");
   1027 		break;
   1028 
   1029 	case 7:	/* Wearable */
   1030 		switch (minor) {
   1031 		case 1: tag("Wrist Watch");			break;
   1032 		case 2: tag("Pager");				break;
   1033 		case 3: tag("Jacket");				break;
   1034 		case 4: tag("Helmet");				break;
   1035 		case 5: tag("Glasses");				break;
   1036 		default: tag("Wearable");			break;
   1037 		}
   1038 		break;
   1039 
   1040 	case 8:	/* Toy */
   1041 		switch (minor) {
   1042 		case 1: tag("Robot");				break;
   1043 		case 2: tag("Vehicle");				break;
   1044 		case 3: tag("Doll / Action Figure");		break;
   1045 		case 4: tag("Controller");			break;
   1046 		case 5: tag("Game");				break;
   1047 		default: tag("Toy");				break;
   1048 		}
   1049 		break;
   1050 
   1051 	default:
   1052 		break;
   1053 	}
   1054 
   1055 	if (class & 0x002000)	tag("<Limited Discoverable>");
   1056 	if (class & 0x010000)	tag("<Positioning>");
   1057 	if (class & 0x020000)	tag("<Networking>");
   1058 	if (class & 0x040000)	tag("<Rendering>");
   1059 	if (class & 0x080000)	tag("<Capturing>");
   1060 	if (class & 0x100000)	tag("<Object Transfer>");
   1061 	if (class & 0x200000)	tag("<Audio>");
   1062 	if (class & 0x400000)	tag("<Telephony>");
   1063 	if (class & 0x800000)	tag("<Information>");
   1064 	tag(NULL);
   1065 }
   1066 
   1067 void
   1068 print_voice(int level)
   1069 {
   1070 	printf("\tvoice: [0x%4.4x]\n", voice);
   1071 
   1072 	if (level == 0)
   1073 		return;
   1074 
   1075 	printf("\t\tInput Coding: ");
   1076 	switch ((voice & 0x0300) >> 8) {
   1077 	case 0x00:	printf("Linear PCM [%d-bit, pos %d]",
   1078 			(voice & 0x0020 ? 16 : 8),
   1079 			(voice & 0x001c) >> 2);		break;
   1080 	case 0x01:	printf("u-Law");		break;
   1081 	case 0x02:	printf("A-Law");		break;
   1082 	case 0x03:	printf("unknown");		break;
   1083 	}
   1084 
   1085 	switch ((voice & 0x00c0) >> 6) {
   1086 	case 0x00:	printf(", 1's complement");	break;
   1087 	case 0x01:	printf(", 2's complement");	break;
   1088 	case 0x02:	printf(", sign magnitude");	break;
   1089 	case 0x03:	printf(", unsigned");		break;
   1090 	}
   1091 
   1092 	printf("\n\t\tAir Coding: ");
   1093 	switch (voice & 0x0003) {
   1094 	case 0x00:	printf("CVSD");			break;
   1095 	case 0x01:	printf("u-Law");		break;
   1096 	case 0x02:	printf("A-Law");		break;
   1097 	case 0x03:	printf("Transparent");		break;
   1098 	}
   1099 
   1100 	printf("\n");
   1101 }
   1102 
   1103 void
   1104 print_result(int num, struct result *r, int rssi)
   1105 {
   1106 	hci_remote_name_req_cp ncp;
   1107 	hci_remote_name_req_compl_ep nep;
   1108 	struct hostent *hp;
   1109 
   1110 	printf("%3d: bdaddr %s",
   1111 			num,
   1112 			bt_ntoa(&r->bdaddr, NULL));
   1113 
   1114 	hp = bt_gethostbyaddr((const char *)&r->bdaddr, sizeof(bdaddr_t), AF_BLUETOOTH);
   1115 	if (hp != NULL)
   1116 		printf(" (%s)", hp->h_name);
   1117 
   1118 	printf("\n");
   1119 
   1120 	memset(&ncp, 0, sizeof(ncp));
   1121 	bdaddr_copy(&ncp.bdaddr, &r->bdaddr);
   1122 	ncp.page_scan_rep_mode = r->page_scan_rep_mode;
   1123 	ncp.clock_offset = r->clock_offset;
   1124 
   1125 	hci_req(HCI_CMD_REMOTE_NAME_REQ,
   1126 		HCI_EVENT_REMOTE_NAME_REQ_COMPL,
   1127 		&ncp, sizeof(ncp),
   1128 		&nep, sizeof(nep));
   1129 
   1130 	printf("   : name \"%s\"\n", nep.name);
   1131 
   1132 	class = (r->uclass[2] << 16) | (r->uclass[1] << 8) | (r->uclass[0]);
   1133 	print_class("   : ");
   1134 
   1135 	printf("   : page scan rep mode 0x%02x\n", r->page_scan_rep_mode);
   1136 	printf("   : clock offset %d\n", le16toh(r->clock_offset));
   1137 
   1138 	if (rssi)
   1139 		printf("   : rssi %d\n", r->rssi);
   1140 
   1141 	printf("\n");
   1142 }
   1143 
   1144 void
   1145 do_inquiry(void)
   1146 {
   1147 	uint8_t buf[HCI_EVENT_PKT_SIZE];
   1148 	struct result result[INQUIRY_MAX_RESPONSES];
   1149 	hci_inquiry_cp inq;
   1150 	struct hci_filter f;
   1151 	hci_event_hdr_t *hh;
   1152 	int i, j, num, rssi;
   1153 
   1154 	if (opt_inquiry == 0)
   1155 		return;
   1156 
   1157 	printf("Device Discovery from device: %s ...", btr.btr_name);
   1158 	fflush(stdout);
   1159 
   1160 	memset(&f, 0, sizeof(f));
   1161 	hci_filter_set(HCI_EVENT_COMMAND_STATUS, &f);
   1162 	hci_filter_set(HCI_EVENT_COMMAND_COMPL, &f);
   1163 	hci_filter_set(HCI_EVENT_INQUIRY_RESULT, &f);
   1164 	hci_filter_set(HCI_EVENT_RSSI_RESULT, &f);
   1165 	hci_filter_set(HCI_EVENT_INQUIRY_COMPL, &f);
   1166 	hci_filter_set(HCI_EVENT_REMOTE_NAME_REQ_COMPL, &f);
   1167 	hci_filter_set(HCI_EVENT_READ_REMOTE_FEATURES_COMPL, &f);
   1168 	if (setsockopt(hci, BTPROTO_HCI, SO_HCI_EVT_FILTER, &f, sizeof(f)) < 0)
   1169 		err(EXIT_FAILURE, "Can't set event filter");
   1170 
   1171 	/* General Inquiry LAP is 0x9e8b33 */
   1172 	inq.lap[0] = 0x33;
   1173 	inq.lap[1] = 0x8b;
   1174 	inq.lap[2] = 0x9e;
   1175 	inq.inquiry_length = INQUIRY_LENGTH;
   1176 	inq.num_responses = INQUIRY_MAX_RESPONSES;
   1177 
   1178 	hci_cmd(HCI_CMD_INQUIRY, &inq, sizeof(inq));
   1179 
   1180 	num = 0;
   1181 	rssi = 0;
   1182 	hh = (hci_event_hdr_t *)buf;
   1183 
   1184 	for (;;) {
   1185 		if (recv(hci, buf, sizeof(buf), 0) <= 0)
   1186 			err(EXIT_FAILURE, "recv");
   1187 
   1188 		if (hh->event == HCI_EVENT_INQUIRY_COMPL)
   1189 			break;
   1190 
   1191 		if (hh->event == HCI_EVENT_INQUIRY_RESULT) {
   1192 			hci_inquiry_result_ep *ep = (hci_inquiry_result_ep *)(hh + 1);
   1193 			hci_inquiry_response *ir = (hci_inquiry_response *)(ep + 1);
   1194 
   1195 			for (i = 0 ; i < ep->num_responses ; i++) {
   1196 				if (num == INQUIRY_MAX_RESPONSES)
   1197 					break;
   1198 
   1199 				/* some devices keep responding, ignore dupes */
   1200 				for (j = 0 ; j < num ; j++)
   1201 					if (bdaddr_same(&result[j].bdaddr, &ir[i].bdaddr))
   1202 						break;
   1203 
   1204 				if (j < num)
   1205 					continue;
   1206 
   1207 				bdaddr_copy(&result[num].bdaddr, &ir[i].bdaddr);
   1208 				memcpy(&result[num].uclass, &ir[i].uclass, HCI_CLASS_SIZE);
   1209 				result[num].page_scan_rep_mode = ir[i].page_scan_rep_mode;
   1210 				result[num].clock_offset = ir[i].clock_offset;
   1211 				result[num].rssi = 0;
   1212 				num++;
   1213 				printf(".");
   1214 				fflush(stdout);
   1215 			}
   1216 			continue;
   1217 		}
   1218 
   1219 		if (hh->event == HCI_EVENT_RSSI_RESULT) {
   1220 			hci_rssi_result_ep *ep = (hci_rssi_result_ep *)(hh + 1);
   1221 			hci_rssi_response *rr = (hci_rssi_response *)(ep + 1);
   1222 
   1223 			for (i = 0 ; i < ep->num_responses ; i++) {
   1224 				if (num == INQUIRY_MAX_RESPONSES)
   1225 					break;
   1226 
   1227 				/* some devices keep responding, ignore dupes */
   1228 				for (j = 0 ; j < num ; j++)
   1229 					if (bdaddr_same(&result[j].bdaddr, &rr[i].bdaddr))
   1230 						break;
   1231 
   1232 				if (j < num)
   1233 					continue;
   1234 
   1235 				bdaddr_copy(&result[num].bdaddr, &rr[i].bdaddr);
   1236 				memcpy(&result[num].uclass, &rr[i].uclass, HCI_CLASS_SIZE);
   1237 				result[num].page_scan_rep_mode = rr[i].page_scan_rep_mode;
   1238 				result[num].clock_offset = rr[i].clock_offset;
   1239 				result[num].rssi = rr[i].rssi;
   1240 				rssi = 1;
   1241 				num++;
   1242 				printf(".");
   1243 				fflush(stdout);
   1244 			}
   1245 			continue;
   1246 		}
   1247 	}
   1248 
   1249 	printf(" %d response%s\n", num, (num == 1 ? "" : "s"));
   1250 
   1251 	for (i = 0 ; i < num ; i++)
   1252 		print_result(i + 1, &result[i], rssi);
   1253 }
   1254