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