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