xref: /netbsd-src/usr.sbin/btconfig/btconfig.c (revision 42f8a9c2e129cc3536b724bdf6d76fa659c0b73d)
1*42f8a9c2Splunky /* $NetBSD: btconfig.c,v 1.28 2017/12/21 09:31:22 plunky Exp $ */
2a5c89047Sgdamore 
3a5c89047Sgdamore /*-
4a5c89047Sgdamore  * Copyright (c) 2006 Itronix Inc.
5a5c89047Sgdamore  * All rights reserved.
6a5c89047Sgdamore  *
7a5c89047Sgdamore  * Written by Iain Hibbert for Itronix Inc.
8a5c89047Sgdamore  *
9a5c89047Sgdamore  * Redistribution and use in source and binary forms, with or without
10a5c89047Sgdamore  * modification, are permitted provided that the following conditions
11a5c89047Sgdamore  * are met:
12a5c89047Sgdamore  * 1. Redistributions of source code must retain the above copyright
13a5c89047Sgdamore  *    notice, this list of conditions and the following disclaimer.
14a5c89047Sgdamore  * 2. Redistributions in binary form must reproduce the above copyright
15a5c89047Sgdamore  *    notice, this list of conditions and the following disclaimer in the
16a5c89047Sgdamore  *    documentation and/or other materials provided with the distribution.
17a5c89047Sgdamore  * 3. The name of Itronix Inc. may not be used to endorse
18a5c89047Sgdamore  *    or promote products derived from this software without specific
19a5c89047Sgdamore  *    prior written permission.
20a5c89047Sgdamore  *
21a5c89047Sgdamore  * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
22a5c89047Sgdamore  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23a5c89047Sgdamore  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24a5c89047Sgdamore  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
25a5c89047Sgdamore  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26a5c89047Sgdamore  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27a5c89047Sgdamore  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28a5c89047Sgdamore  * ON ANY THEORY OF LIABILITY, WHETHER IN
29a5c89047Sgdamore  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30a5c89047Sgdamore  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31a5c89047Sgdamore  * POSSIBILITY OF SUCH DAMAGE.
32a5c89047Sgdamore  */
33a5c89047Sgdamore 
34a5c89047Sgdamore #include <sys/cdefs.h>
359c194566Slukem __COPYRIGHT("@(#) Copyright (c) 2006 Itronix, Inc.  All rights reserved.");
36*42f8a9c2Splunky __RCSID("$NetBSD: btconfig.c,v 1.28 2017/12/21 09:31:22 plunky Exp $");
37a5c89047Sgdamore 
3826a4d1cfSplunky #include <sys/ioctl.h>
39a5c89047Sgdamore #include <sys/param.h>
40a5c89047Sgdamore #include <sys/socket.h>
41a5c89047Sgdamore 
42a5c89047Sgdamore #include <bluetooth.h>
4326a4d1cfSplunky #include <err.h>
4426a4d1cfSplunky #include <errno.h>
4526a4d1cfSplunky #include <stdio.h>
4626a4d1cfSplunky #include <stdlib.h>
4726a4d1cfSplunky #include <string.h>
485cf7cee7Splunky #include <time.h>
4926a4d1cfSplunky #include <unistd.h>
5026a4d1cfSplunky #include <util.h>
51a5c89047Sgdamore 
528756b978Sjoerg __dead static void badarg(const char *);
538756b978Sjoerg __dead static void badparam(const char *);
548756b978Sjoerg __dead static void badval(const char *, const char *);
558756b978Sjoerg __dead static void usage(void);
568756b978Sjoerg static int set_unit(unsigned long);
578756b978Sjoerg static void config_unit(void);
588756b978Sjoerg static void print_val(const char *, const char **, int);
598756b978Sjoerg static void print_info(int);
608756b978Sjoerg static void print_stats(void);
618756b978Sjoerg static void print_class(const char *, uint8_t *);
628756b978Sjoerg static void print_class0(void);
638756b978Sjoerg static void print_voice(int);
648756b978Sjoerg static void tag(const char *);
658756b978Sjoerg static void print_features0(uint8_t *);
668756b978Sjoerg static void print_features1(uint8_t *);
6781aeedabSplunky static void print_features2(uint8_t *);
688756b978Sjoerg static void print_result(int, struct bt_devinquiry *);
698756b978Sjoerg static void do_inquiry(void);
70a5c89047Sgdamore 
718756b978Sjoerg static void hci_req(uint16_t, uint8_t , void *, size_t, void *, size_t);
728756b978Sjoerg static void save_value(uint16_t, void *, size_t);
738756b978Sjoerg static void load_value(uint16_t, void *, size_t);
74a5c89047Sgdamore 
75a5c89047Sgdamore #define MAX_STR_SIZE	0xff
76a5c89047Sgdamore 
77a5c89047Sgdamore /* print width */
788756b978Sjoerg static int width = 0;
79a5c89047Sgdamore #define MAX_WIDTH	70
80a5c89047Sgdamore 
81a5c89047Sgdamore /* global variables */
828756b978Sjoerg static int hci;
838756b978Sjoerg static struct btreq btr;
84a5c89047Sgdamore 
85a5c89047Sgdamore /* command line flags */
868756b978Sjoerg static int verbose = 0;	/* more info */
878756b978Sjoerg static int lflag = 0;		/* list devices */
888756b978Sjoerg static int sflag = 0;		/* get/zero stats */
89a5c89047Sgdamore 
90a5c89047Sgdamore /* device up/down (flag) */
918756b978Sjoerg static int opt_enable = 0;
928756b978Sjoerg static int opt_reset = 0;
93a5c89047Sgdamore #define FLAGS_FMT	"\20"			\
94a5c89047Sgdamore 			"\001UP"		\
95a5c89047Sgdamore 			"\002RUNNING"		\
96a5c89047Sgdamore 			"\003XMIT_CMD"		\
97a5c89047Sgdamore 			"\004XMIT_ACL"		\
98a5c89047Sgdamore 			"\005XMIT_SCO"		\
99a5c89047Sgdamore 			"\006INIT_BDADDR"	\
100a5c89047Sgdamore 			"\007INIT_BUFFER_SIZE"	\
101736a9db0Splunky 			"\010INIT_FEATURES"	\
102736a9db0Splunky 			"\011POWER_UP_NOOP"	\
103aeab3db8Splunky 			"\012INIT_COMMANDS"	\
104b7dec17fSplunky 			"\013MASTER"		\
105736a9db0Splunky 			""
106a5c89047Sgdamore 
107a5c89047Sgdamore /* authorisation (flag) */
1088756b978Sjoerg static int opt_auth = 0;
109a5c89047Sgdamore 
110a5c89047Sgdamore /* encryption (flag) */
1118756b978Sjoerg static int opt_encrypt = 0;
112a5c89047Sgdamore 
113a5c89047Sgdamore /* scan enable options (flags) */
1148756b978Sjoerg static int opt_pscan = 0;
1158756b978Sjoerg static int opt_iscan = 0;
116a5c89047Sgdamore 
117b7dec17fSplunky /* master role option */
1188756b978Sjoerg static int opt_master = 0;
119b7dec17fSplunky 
120a5c89047Sgdamore /* link policy options (flags) */
1218756b978Sjoerg static int opt_switch = 0;
1228756b978Sjoerg static int opt_hold = 0;
1238756b978Sjoerg static int opt_sniff = 0;
1248756b978Sjoerg static int opt_park = 0;
125a5c89047Sgdamore 
126a5c89047Sgdamore /* class of device (hex value) */
1278756b978Sjoerg static int opt_class = 0;
1288756b978Sjoerg static uint32_t class;
129a5c89047Sgdamore 
130a5c89047Sgdamore /* packet type mask (hex value) */
1318756b978Sjoerg static int opt_ptype = 0;
1328756b978Sjoerg static uint32_t ptype;
133a5c89047Sgdamore 
134a5c89047Sgdamore /* unit name (string) */
1358756b978Sjoerg static int opt_name = 0;
1368756b978Sjoerg static char name[MAX_STR_SIZE];
137a5c89047Sgdamore 
138a5c89047Sgdamore /* pin type */
1398756b978Sjoerg static int opt_pin = 0;
140a5c89047Sgdamore 
141a5c89047Sgdamore /* Inquiry */
1428756b978Sjoerg static int opt_rssi = 0;			/* inquiry_with_rssi (obsolete flag) */
1438756b978Sjoerg static int opt_imode = 0;			/* inquiry mode */
1448756b978Sjoerg static int opt_inquiry = 0;
145d8166becSplunky #define INQUIRY_LENGTH		10	/* seconds */
146a5c89047Sgdamore #define INQUIRY_MAX_RESPONSES	10
1478756b978Sjoerg static const char *imodes[] = { "std", "rssi", "ext", NULL };
148a5c89047Sgdamore 
149a5c89047Sgdamore /* Voice Settings */
1508756b978Sjoerg static int opt_voice = 0;
1518756b978Sjoerg static uint32_t voice;
152a5c89047Sgdamore 
153eea08126Stron /* Page Timeout */
1548756b978Sjoerg static int opt_pto = 0;
1558756b978Sjoerg static uint32_t pto;
156eea08126Stron 
157c980b109Splunky /* set SCO mtu */
1588756b978Sjoerg static int opt_scomtu;
1598756b978Sjoerg static uint32_t scomtu;
160c980b109Splunky 
1618756b978Sjoerg static struct parameter {
162a5c89047Sgdamore 	const char	*name;
16326f09dd3Splunky 	enum { P_SET, P_CLR, P_STR, P_HEX, P_NUM, P_VAL } type;
164a5c89047Sgdamore 	int		*opt;
165a5c89047Sgdamore 	void		*val;
166a5c89047Sgdamore } parameters[] = {
167a5c89047Sgdamore 	{ "up",		P_SET,	&opt_enable,	NULL	},
168a5c89047Sgdamore 	{ "enable",	P_SET,	&opt_enable,	NULL	},
169a5c89047Sgdamore 	{ "down",	P_CLR,	&opt_enable,	NULL	},
170a5c89047Sgdamore 	{ "disable",	P_CLR,	&opt_enable,	NULL	},
171a5c89047Sgdamore 	{ "name",	P_STR,	&opt_name,	name	},
172a5c89047Sgdamore 	{ "pscan",	P_SET,	&opt_pscan,	NULL	},
173a5c89047Sgdamore 	{ "-pscan",	P_CLR,	&opt_pscan,	NULL	},
174a5c89047Sgdamore 	{ "iscan",	P_SET,	&opt_iscan,	NULL	},
175a5c89047Sgdamore 	{ "-iscan",	P_CLR,	&opt_iscan,	NULL	},
176b7dec17fSplunky 	{ "master",	P_SET,	&opt_master,	NULL	},
177b7dec17fSplunky 	{ "-master",	P_CLR,	&opt_master,	NULL	},
178a5c89047Sgdamore 	{ "switch",	P_SET,	&opt_switch,	NULL	},
179a5c89047Sgdamore 	{ "-switch",	P_CLR,	&opt_switch,	NULL	},
180a5c89047Sgdamore 	{ "hold",	P_SET,	&opt_hold,	NULL	},
181a5c89047Sgdamore 	{ "-hold",	P_CLR,	&opt_hold,	NULL	},
182a5c89047Sgdamore 	{ "sniff",	P_SET,	&opt_sniff,	NULL	},
183a5c89047Sgdamore 	{ "-sniff",	P_CLR,	&opt_sniff,	NULL	},
184a5c89047Sgdamore 	{ "park",	P_SET,	&opt_park,	NULL	},
185a5c89047Sgdamore 	{ "-park",	P_CLR,	&opt_park,	NULL	},
186a5c89047Sgdamore 	{ "auth",	P_SET,	&opt_auth,	NULL	},
187a5c89047Sgdamore 	{ "-auth",	P_CLR,	&opt_auth,	NULL	},
188a5c89047Sgdamore 	{ "encrypt",	P_SET,	&opt_encrypt,	NULL	},
189a5c89047Sgdamore 	{ "-encrypt",	P_CLR,	&opt_encrypt,	NULL	},
190a5c89047Sgdamore 	{ "ptype",	P_HEX,	&opt_ptype,	&ptype	},
191a5c89047Sgdamore 	{ "class",	P_HEX,	&opt_class,	&class	},
192a5c89047Sgdamore 	{ "fixed",	P_SET,	&opt_pin,	NULL	},
193a5c89047Sgdamore 	{ "variable",	P_CLR,	&opt_pin,	NULL	},
194a5c89047Sgdamore 	{ "inq",	P_SET,	&opt_inquiry,	NULL	},
195a5c89047Sgdamore 	{ "inquiry",	P_SET,	&opt_inquiry,	NULL	},
19626f09dd3Splunky 	{ "imode",	P_VAL,	&opt_imode,	imodes	},
1970d2b4f91Splunky 	{ "rssi",	P_SET,	&opt_rssi,	NULL	},
1980d2b4f91Splunky 	{ "-rssi",	P_CLR,	&opt_rssi,	NULL	},
199a5c89047Sgdamore 	{ "reset",	P_SET,	&opt_reset,	NULL	},
200a5c89047Sgdamore 	{ "voice",	P_HEX,	&opt_voice,	&voice	},
201eea08126Stron 	{ "pto",	P_NUM,	&opt_pto,	&pto	},
202c980b109Splunky 	{ "scomtu",	P_NUM,	&opt_scomtu,	&scomtu	},
203a746bed1Splunky 	{ NULL,		0,	NULL,		NULL	}
204a5c89047Sgdamore };
205a5c89047Sgdamore 
206a5c89047Sgdamore int
main(int ac,char * av[])207a5c89047Sgdamore main(int ac, char *av[])
208a5c89047Sgdamore {
209a5c89047Sgdamore 	int ch;
210a5c89047Sgdamore 	struct parameter *p;
211a5c89047Sgdamore 
212a5c89047Sgdamore 	while ((ch = getopt(ac, av, "hlsvz")) != -1) {
213a5c89047Sgdamore 		switch(ch) {
214a5c89047Sgdamore 		case 'l':
215a5c89047Sgdamore 			lflag = 1;
216a5c89047Sgdamore 			break;
217a5c89047Sgdamore 
218a5c89047Sgdamore 		case 's':
219a5c89047Sgdamore 			sflag = 1;
220a5c89047Sgdamore 			break;
221a5c89047Sgdamore 
222a5c89047Sgdamore 		case 'v':
223a5c89047Sgdamore 			verbose++;
224a5c89047Sgdamore 			break;
225a5c89047Sgdamore 
226a5c89047Sgdamore 		case 'z':
227a5c89047Sgdamore 			sflag = 2;
228a5c89047Sgdamore 			break;
229a5c89047Sgdamore 
230a5c89047Sgdamore 		case 'h':
231a5c89047Sgdamore 		default:
232a5c89047Sgdamore 			usage();
233a5c89047Sgdamore 		}
234a5c89047Sgdamore 	}
235a5c89047Sgdamore 	av += optind;
236a5c89047Sgdamore 	ac -= optind;
237a5c89047Sgdamore 
238a5c89047Sgdamore 	if (lflag && sflag)
239a5c89047Sgdamore 		usage();
240a5c89047Sgdamore 
241a5c89047Sgdamore 	hci = socket(PF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
242a5c89047Sgdamore 	if (hci == -1)
243a5c89047Sgdamore 		err(EXIT_FAILURE, "socket");
244a5c89047Sgdamore 
245a5c89047Sgdamore 	if (ac == 0) {
246a5c89047Sgdamore 		verbose++;
247a5c89047Sgdamore 
248a5c89047Sgdamore 		memset(&btr, 0, sizeof(btr));
249a5c89047Sgdamore 		while (set_unit(SIOCNBTINFO) != -1) {
250a5c89047Sgdamore 			print_info(verbose);
251a5c89047Sgdamore 			print_stats();
252a5c89047Sgdamore 		}
253a5c89047Sgdamore 
254a5c89047Sgdamore 		tag(NULL);
255a5c89047Sgdamore 	} else {
256a5c89047Sgdamore 		strlcpy(btr.btr_name, *av, HCI_DEVNAME_SIZE);
257a5c89047Sgdamore 		av++, ac--;
258a5c89047Sgdamore 
259a5c89047Sgdamore 		if (set_unit(SIOCGBTINFO) < 0)
260a5c89047Sgdamore 			err(EXIT_FAILURE, "%s", btr.btr_name);
261a5c89047Sgdamore 
262a5c89047Sgdamore 		if (ac == 0)
263a5c89047Sgdamore 			verbose += 2;
264a5c89047Sgdamore 
265a5c89047Sgdamore 		while (ac > 0) {
266a5c89047Sgdamore 			for (p = parameters ; ; p++) {
267a5c89047Sgdamore 				if (p->name == NULL)
268a5c89047Sgdamore 					badparam(*av);
269a5c89047Sgdamore 
270a5c89047Sgdamore 				if (strcmp(*av, p->name) == 0)
271a5c89047Sgdamore 					break;
272a5c89047Sgdamore 			}
273a5c89047Sgdamore 
274a5c89047Sgdamore 			switch(p->type) {
275a5c89047Sgdamore 			case P_SET:
276a5c89047Sgdamore 				*(p->opt) = 1;
277a5c89047Sgdamore 				break;
278a5c89047Sgdamore 
279a5c89047Sgdamore 			case P_CLR:
280a5c89047Sgdamore 				*(p->opt) = -1;
281a5c89047Sgdamore 				break;
282a5c89047Sgdamore 
283a5c89047Sgdamore 			case P_STR:
284a5c89047Sgdamore 				if (--ac < 1) badarg(p->name);
285a5c89047Sgdamore 				strlcpy((char *)(p->val), *++av, MAX_STR_SIZE);
286a5c89047Sgdamore 				*(p->opt) = 1;
287a5c89047Sgdamore 				break;
288a5c89047Sgdamore 
289a5c89047Sgdamore 			case P_HEX:
290a5c89047Sgdamore 				if (--ac < 1) badarg(p->name);
291a746bed1Splunky 				*(uint32_t *)(p->val) = strtoul(*++av, NULL, 16);
292a5c89047Sgdamore 				*(p->opt) = 1;
293a5c89047Sgdamore 				break;
294eea08126Stron 
295eea08126Stron 			case P_NUM:
296eea08126Stron 				if (--ac < 1) badarg(p->name);
297a746bed1Splunky 				*(uint32_t *)(p->val) = strtoul(*++av, NULL, 10);
298eea08126Stron 				*(p->opt) = 1;
299eea08126Stron 				break;
30026f09dd3Splunky 
30126f09dd3Splunky 			case P_VAL:
30226f09dd3Splunky 				if (--ac < 1) badarg(p->name);
30326f09dd3Splunky 				++av;
30426f09dd3Splunky 				ch = 0;
30526f09dd3Splunky 				do {
30626f09dd3Splunky 					if (((char **)(p->val))[ch] == NULL)
30726f09dd3Splunky 						badval(p->name, *av);
30826f09dd3Splunky 				} while (strcmp(((char **)(p->val))[ch++], *av));
30926f09dd3Splunky 				*(p->opt) = ch;
31026f09dd3Splunky 				break;
311a5c89047Sgdamore 			}
312a5c89047Sgdamore 
313a5c89047Sgdamore 			av++, ac--;
314a5c89047Sgdamore 		}
315a5c89047Sgdamore 
316a5c89047Sgdamore 		config_unit();
317a5c89047Sgdamore 		print_info(verbose);
318a5c89047Sgdamore 		print_stats();
319a5c89047Sgdamore 		do_inquiry();
320a5c89047Sgdamore 	}
321a5c89047Sgdamore 
322a5c89047Sgdamore 	close(hci);
323a5c89047Sgdamore 	return EXIT_SUCCESS;
324a5c89047Sgdamore }
325a5c89047Sgdamore 
3268756b978Sjoerg static void
badparam(const char * param)327a5c89047Sgdamore badparam(const char *param)
328a5c89047Sgdamore {
329a5c89047Sgdamore 
330a5c89047Sgdamore 	fprintf(stderr, "unknown parameter '%s'\n", param);
331a5c89047Sgdamore 	exit(EXIT_FAILURE);
332a5c89047Sgdamore }
333a5c89047Sgdamore 
3348756b978Sjoerg static void
badarg(const char * param)335a5c89047Sgdamore badarg(const char *param)
336a5c89047Sgdamore {
337a5c89047Sgdamore 
338a5c89047Sgdamore 	fprintf(stderr, "parameter '%s' needs argument\n", param);
339a5c89047Sgdamore 	exit(EXIT_FAILURE);
340a5c89047Sgdamore }
341a5c89047Sgdamore 
3428756b978Sjoerg static void
badval(const char * param,const char * value)34326f09dd3Splunky badval(const char *param, const char *value)
34426f09dd3Splunky {
34526f09dd3Splunky 
34626f09dd3Splunky 	fprintf(stderr, "bad value '%s' for parameter '%s'\n", value, param);
34726f09dd3Splunky 	exit(EXIT_FAILURE);
34826f09dd3Splunky }
34926f09dd3Splunky 
3508756b978Sjoerg static void
usage(void)351a5c89047Sgdamore usage(void)
352a5c89047Sgdamore {
353a5c89047Sgdamore 
354a5c89047Sgdamore 	fprintf(stderr, "usage:	%s [-svz] [device [parameters]]\n", getprogname());
355a5c89047Sgdamore 	fprintf(stderr, "	%s -l\n", getprogname());
356a5c89047Sgdamore 	exit(EXIT_FAILURE);
357a5c89047Sgdamore }
358a5c89047Sgdamore 
359a5c89047Sgdamore /*
360a5c89047Sgdamore  * pretty printing feature
361a5c89047Sgdamore  */
3628756b978Sjoerg static void
tag(const char * f)363a5c89047Sgdamore tag(const char *f)
364a5c89047Sgdamore {
365a5c89047Sgdamore 
366a5c89047Sgdamore 	if (f == NULL) {
367a5c89047Sgdamore 		if (width > 0)
368a5c89047Sgdamore 			printf("\n");
369a5c89047Sgdamore 
370a5c89047Sgdamore 		width = 0;
371a5c89047Sgdamore 	} else {
372a5c89047Sgdamore 		width += printf("%*s%s",
373a5c89047Sgdamore 				(width == 0 ? (lflag ? 0 : 8) : 1),
374a5c89047Sgdamore 				"", f);
375a5c89047Sgdamore 
376a5c89047Sgdamore 		if (width > MAX_WIDTH) {
377a5c89047Sgdamore 			printf("\n");
378a5c89047Sgdamore 			width = 0;
379a5c89047Sgdamore 		}
380a5c89047Sgdamore 	}
381a5c89047Sgdamore }
382a5c89047Sgdamore 
383a5c89047Sgdamore /*
384d8166becSplunky  * basic HCI request wrapper with error check
385a5c89047Sgdamore  */
3868756b978Sjoerg static void
hci_req(uint16_t opcode,uint8_t event,void * cbuf,size_t clen,void * rbuf,size_t rlen)387d8166becSplunky hci_req(uint16_t opcode, uint8_t event, void *cbuf, size_t clen,
388d8166becSplunky     void *rbuf, size_t rlen)
389a5c89047Sgdamore {
390d8166becSplunky 	struct bt_devreq req;
391a5c89047Sgdamore 
392d8166becSplunky 	req.opcode = opcode;
393d8166becSplunky 	req.event = event;
394d8166becSplunky 	req.cparam = cbuf;
395d8166becSplunky 	req.clen = clen;
396d8166becSplunky 	req.rparam = rbuf;
397d8166becSplunky 	req.rlen = rlen;
398a5c89047Sgdamore 
399d8166becSplunky 	if (bt_devreq(hci, &req, 10) == -1)
400d8166becSplunky 		err(EXIT_FAILURE, "cmd (%02x|%03x)",
401d8166becSplunky 		    HCI_OGF(opcode), HCI_OCF(opcode));
402a5c89047Sgdamore 
403d8166becSplunky 	if (event == 0 && rlen > 0 && ((uint8_t *)rbuf)[0] != 0)
404d8166becSplunky 		errx(EXIT_FAILURE, "cmd (%02x|%03x): status 0x%02x",
405d8166becSplunky 		    HCI_OGF(opcode), HCI_OCF(opcode), ((uint8_t *)rbuf)[0]);
406a5c89047Sgdamore }
407a5c89047Sgdamore 
408d8166becSplunky /*
409d8166becSplunky  * write value to device with opcode.
410d8166becSplunky  * provide a small response buffer so that the status can be checked
411d8166becSplunky  */
4128756b978Sjoerg static void
save_value(uint16_t opcode,void * cbuf,size_t clen)413d8166becSplunky save_value(uint16_t opcode, void *cbuf, size_t clen)
414d8166becSplunky {
415d8166becSplunky 	uint8_t buf[1];
416a5c89047Sgdamore 
417d8166becSplunky 	hci_req(opcode, 0, cbuf, clen, buf, sizeof(buf));
418a5c89047Sgdamore }
419a5c89047Sgdamore 
420d8166becSplunky /*
421d8166becSplunky  * read value from device with opcode.
422d8166becSplunky  * use our own buffer and only return the value from the response packet
423d8166becSplunky  */
4248756b978Sjoerg static void
load_value(uint16_t opcode,void * rbuf,size_t rlen)425d8166becSplunky load_value(uint16_t opcode, void *rbuf, size_t rlen)
426d8166becSplunky {
427d8166becSplunky 	uint8_t buf[UINT8_MAX];
428a5c89047Sgdamore 
429d8166becSplunky 	hci_req(opcode, 0, NULL, 0, buf, sizeof(buf));
430d8166becSplunky 	memcpy(rbuf, buf + 1, rlen);
431a5c89047Sgdamore }
432a5c89047Sgdamore 
4338756b978Sjoerg static int
set_unit(unsigned long cmd)434a5c89047Sgdamore set_unit(unsigned long cmd)
435a5c89047Sgdamore {
436a5c89047Sgdamore 
437a5c89047Sgdamore 	if (ioctl(hci, cmd, &btr) == -1)
438a5c89047Sgdamore 		return -1;
439a5c89047Sgdamore 
440a5c89047Sgdamore 	if (btr.btr_flags & BTF_UP) {
441a5c89047Sgdamore 		struct sockaddr_bt sa;
442a5c89047Sgdamore 
443a5c89047Sgdamore 		sa.bt_len = sizeof(sa);
444a5c89047Sgdamore 		sa.bt_family = AF_BLUETOOTH;
445a5c89047Sgdamore 		bdaddr_copy(&sa.bt_bdaddr, &btr.btr_bdaddr);
446a5c89047Sgdamore 
447a5c89047Sgdamore 		if (bind(hci, (struct sockaddr *)&sa, sizeof(sa)) < 0)
448a5c89047Sgdamore 			err(EXIT_FAILURE, "bind");
449a5c89047Sgdamore 
450a5c89047Sgdamore 		if (connect(hci, (struct sockaddr *)&sa, sizeof(sa)) < 0)
451a5c89047Sgdamore 			err(EXIT_FAILURE, "connect");
452a5c89047Sgdamore 	}
453a5c89047Sgdamore 
454a5c89047Sgdamore 	return 0;
455a5c89047Sgdamore }
456a5c89047Sgdamore 
457a5c89047Sgdamore /*
458a5c89047Sgdamore  * apply configuration parameters to unit
459a5c89047Sgdamore  */
4608756b978Sjoerg static void
config_unit(void)461a5c89047Sgdamore config_unit(void)
462a5c89047Sgdamore {
463a5c89047Sgdamore 
4643729e633Splunky 	if (opt_enable) {
4653729e633Splunky 		if (opt_enable > 0)
4663729e633Splunky 			btr.btr_flags |= BTF_UP;
4673729e633Splunky 		else
468a5c89047Sgdamore 			btr.btr_flags &= ~BTF_UP;
469a5c89047Sgdamore 
470a5c89047Sgdamore 		if (ioctl(hci, SIOCSBTFLAGS, &btr) < 0)
471a5c89047Sgdamore 			err(EXIT_FAILURE, "SIOCSBTFLAGS");
472a5c89047Sgdamore 
473a5c89047Sgdamore 		if (set_unit(SIOCGBTINFO) < 0)
474a5c89047Sgdamore 			err(EXIT_FAILURE, "%s", btr.btr_name);
475a5c89047Sgdamore 	}
476a5c89047Sgdamore 
4773729e633Splunky 	if (opt_reset) {
4785cf7cee7Splunky 		static const struct timespec ts = { 0, 100000000 }; /* 100ms */
479a5c89047Sgdamore 
4803729e633Splunky 		btr.btr_flags |= BTF_INIT;
481a5c89047Sgdamore 		if (ioctl(hci, SIOCSBTFLAGS, &btr) < 0)
482a5c89047Sgdamore 			err(EXIT_FAILURE, "SIOCSBTFLAGS");
483a5c89047Sgdamore 
4845cf7cee7Splunky 		hci_req(HCI_CMD_RESET, 0, NULL, 0, NULL, 0);
4853729e633Splunky 
4865cf7cee7Splunky 		do {
4875cf7cee7Splunky 			nanosleep(&ts, NULL);
4885cf7cee7Splunky 			if (ioctl(hci, SIOCGBTINFO, &btr) < 0)
489a5c89047Sgdamore 				err(EXIT_FAILURE, "%s", btr.btr_name);
4905cf7cee7Splunky 		} while ((btr.btr_flags & BTF_INIT) != 0);
491a5c89047Sgdamore 	}
492a5c89047Sgdamore 
493b7dec17fSplunky 	if (opt_master) {
494b7dec17fSplunky 		if (opt_master > 0)
495b7dec17fSplunky 			btr.btr_flags |= BTF_MASTER;
496b7dec17fSplunky 		else
497b7dec17fSplunky 			btr.btr_flags &= ~BTF_MASTER;
498b7dec17fSplunky 
499b7dec17fSplunky 		if (ioctl(hci, SIOCSBTFLAGS, &btr) < 0)
500b7dec17fSplunky 			err(EXIT_FAILURE, "SIOCSBTFLAGS");
501b7dec17fSplunky 	}
502b7dec17fSplunky 
503a5c89047Sgdamore 	if (opt_switch || opt_hold || opt_sniff || opt_park) {
504a5c89047Sgdamore 		uint16_t val = btr.btr_link_policy;
505a5c89047Sgdamore 
506a5c89047Sgdamore 		if (opt_switch > 0) val |= HCI_LINK_POLICY_ENABLE_ROLE_SWITCH;
507a5c89047Sgdamore 		if (opt_switch < 0) val &= ~HCI_LINK_POLICY_ENABLE_ROLE_SWITCH;
508a5c89047Sgdamore 		if (opt_hold > 0)   val |= HCI_LINK_POLICY_ENABLE_HOLD_MODE;
509a5c89047Sgdamore 		if (opt_hold < 0)   val &= ~HCI_LINK_POLICY_ENABLE_HOLD_MODE;
510a5c89047Sgdamore 		if (opt_sniff > 0)  val |= HCI_LINK_POLICY_ENABLE_SNIFF_MODE;
511a5c89047Sgdamore 		if (opt_sniff < 0)  val &= ~HCI_LINK_POLICY_ENABLE_SNIFF_MODE;
512a5c89047Sgdamore 		if (opt_park > 0)   val |= HCI_LINK_POLICY_ENABLE_PARK_MODE;
513a5c89047Sgdamore 		if (opt_park < 0)   val &= ~HCI_LINK_POLICY_ENABLE_PARK_MODE;
514a5c89047Sgdamore 
515a5c89047Sgdamore 		btr.btr_link_policy = val;
516a5c89047Sgdamore 		if (ioctl(hci, SIOCSBTPOLICY, &btr) < 0)
517a5c89047Sgdamore 			err(EXIT_FAILURE, "SIOCSBTPOLICY");
518a5c89047Sgdamore 	}
519a5c89047Sgdamore 
520a5c89047Sgdamore 	if (opt_ptype) {
521a5c89047Sgdamore 		btr.btr_packet_type = ptype;
522a5c89047Sgdamore 		if (ioctl(hci, SIOCSBTPTYPE, &btr) < 0)
523a5c89047Sgdamore 			err(EXIT_FAILURE, "SIOCSBTPTYPE");
524a5c89047Sgdamore 	}
525a5c89047Sgdamore 
526a5c89047Sgdamore 	if (opt_pscan || opt_iscan) {
527a5c89047Sgdamore 		uint8_t val;
528a5c89047Sgdamore 
529a5c89047Sgdamore 		load_value(HCI_CMD_READ_SCAN_ENABLE, &val, sizeof(val));
530a5c89047Sgdamore 		if (opt_pscan > 0) val |= HCI_PAGE_SCAN_ENABLE;
531a5c89047Sgdamore 		if (opt_pscan < 0) val &= ~HCI_PAGE_SCAN_ENABLE;
532a5c89047Sgdamore 		if (opt_iscan > 0) val |= HCI_INQUIRY_SCAN_ENABLE;
533a5c89047Sgdamore 		if (opt_iscan < 0) val &= ~HCI_INQUIRY_SCAN_ENABLE;
534a5c89047Sgdamore 		save_value(HCI_CMD_WRITE_SCAN_ENABLE, &val, sizeof(val));
535a5c89047Sgdamore 	}
536a5c89047Sgdamore 
537a5c89047Sgdamore 	if (opt_auth) {
538a5c89047Sgdamore 		uint8_t val = (opt_auth > 0 ? 1 : 0);
539a5c89047Sgdamore 
540a5c89047Sgdamore 		save_value(HCI_CMD_WRITE_AUTH_ENABLE, &val, sizeof(val));
541a5c89047Sgdamore 	}
542a5c89047Sgdamore 
543a5c89047Sgdamore 	if (opt_encrypt) {
544a5c89047Sgdamore 		uint8_t val = (opt_encrypt > 0 ? 1 : 0);
545a5c89047Sgdamore 
546a5c89047Sgdamore 		save_value(HCI_CMD_WRITE_ENCRYPTION_MODE, &val, sizeof(val));
547a5c89047Sgdamore 	}
548a5c89047Sgdamore 
549a5c89047Sgdamore 	if (opt_name)
550a5c89047Sgdamore 		save_value(HCI_CMD_WRITE_LOCAL_NAME, name, HCI_UNIT_NAME_SIZE);
551a5c89047Sgdamore 
552a5c89047Sgdamore 	if (opt_class) {
553a5c89047Sgdamore 		uint8_t val[HCI_CLASS_SIZE];
554a5c89047Sgdamore 
555a5c89047Sgdamore 		val[0] = (class >> 0) & 0xff;
556a5c89047Sgdamore 		val[1] = (class >> 8) & 0xff;
557a5c89047Sgdamore 		val[2] = (class >> 16) & 0xff;
558a5c89047Sgdamore 
559a5c89047Sgdamore 		save_value(HCI_CMD_WRITE_UNIT_CLASS, val, HCI_CLASS_SIZE);
560a5c89047Sgdamore 	}
561a5c89047Sgdamore 
562a5c89047Sgdamore 	if (opt_pin) {
563a5c89047Sgdamore 		uint8_t val;
564a5c89047Sgdamore 
565a5c89047Sgdamore 		if (opt_pin > 0)	val = 1;
566a5c89047Sgdamore 		else			val = 0;
567a5c89047Sgdamore 
568a5c89047Sgdamore 		save_value(HCI_CMD_WRITE_PIN_TYPE, &val, sizeof(val));
569a5c89047Sgdamore 	}
570a5c89047Sgdamore 
571a5c89047Sgdamore 	if (opt_voice) {
572a5c89047Sgdamore 		uint16_t val;
573a5c89047Sgdamore 
574a5c89047Sgdamore 		val = htole16(voice & 0x03ff);
575a5c89047Sgdamore 		save_value(HCI_CMD_WRITE_VOICE_SETTING, &val, sizeof(val));
576a5c89047Sgdamore 	}
577eea08126Stron 
578eea08126Stron 	if (opt_pto) {
579eea08126Stron 		uint16_t val;
580eea08126Stron 
581eea08126Stron 		val = htole16(pto * 8 / 5);
582eea08126Stron 		save_value(HCI_CMD_WRITE_PAGE_TIMEOUT, &val, sizeof(val));
583eea08126Stron 	}
584c980b109Splunky 
585c980b109Splunky 	if (opt_scomtu) {
586a746bed1Splunky 		if (scomtu > 0xff) {
587c980b109Splunky 			warnx("Invalid SCO mtu %d", scomtu);
588c980b109Splunky 		} else {
589c980b109Splunky 			btr.btr_sco_mtu = scomtu;
590c980b109Splunky 
591c980b109Splunky 			if (ioctl(hci, SIOCSBTSCOMTU, &btr) < 0)
592c980b109Splunky 				warn("SIOCSBTSCOMTU");
593c980b109Splunky 		}
594c980b109Splunky 	}
5950d2b4f91Splunky 
59626f09dd3Splunky 	if (opt_imode | opt_rssi) {
5970d2b4f91Splunky 		uint8_t val = (opt_rssi > 0 ? 1 : 0);
5980d2b4f91Splunky 
59926f09dd3Splunky 		if (opt_imode)
60026f09dd3Splunky 			val = opt_imode - 1;
60126f09dd3Splunky 
6020d2b4f91Splunky 		save_value(HCI_CMD_WRITE_INQUIRY_MODE, &val, sizeof(val));
6030d2b4f91Splunky 	}
604a5c89047Sgdamore }
605a5c89047Sgdamore 
606a5c89047Sgdamore /*
60726f09dd3Splunky  * print value from NULL terminated array given index
60826f09dd3Splunky  */
6098756b978Sjoerg static void
print_val(const char * hdr,const char ** argv,int idx)61026f09dd3Splunky print_val(const char *hdr, const char **argv, int idx)
61126f09dd3Splunky {
61226f09dd3Splunky 	int i = 0;
61326f09dd3Splunky 
61426f09dd3Splunky 	while (i < idx && *argv != NULL)
61526f09dd3Splunky 		i++, argv++;
61626f09dd3Splunky 
61726f09dd3Splunky 	printf("\t%s: %s\n", hdr, *argv == NULL ? "unknown" : *argv);
61826f09dd3Splunky }
61926f09dd3Splunky 
62026f09dd3Splunky /*
621a5c89047Sgdamore  * Print info for Bluetooth Device with varying verbosity levels
622a5c89047Sgdamore  */
6238756b978Sjoerg static void
print_info(int level)624a5c89047Sgdamore print_info(int level)
625a5c89047Sgdamore {
626a2576166Splunky 	uint8_t version, val, buf[MAX_STR_SIZE];
627a5c89047Sgdamore 
628a5c89047Sgdamore 	if (lflag) {
629a5c89047Sgdamore 		tag(btr.btr_name);
630a5c89047Sgdamore 		return;
631a5c89047Sgdamore 	}
632a5c89047Sgdamore 
633a5c89047Sgdamore 	if (level-- < 1)
634a5c89047Sgdamore 		return;
635a5c89047Sgdamore 
636a5c89047Sgdamore 	snprintb((char *)buf, MAX_STR_SIZE, FLAGS_FMT, btr.btr_flags);
637a5c89047Sgdamore 
638a5c89047Sgdamore 	printf("%s: bdaddr %s flags %s\n", btr.btr_name,
639a5c89047Sgdamore 		bt_ntoa(&btr.btr_bdaddr, NULL), buf);
640a5c89047Sgdamore 
641a5c89047Sgdamore 	if (level-- < 1)
642a5c89047Sgdamore 		return;
643a5c89047Sgdamore 
644a5c89047Sgdamore 	printf("\tnum_cmd = %d\n"
64552ea7090Splunky 	       "\tnum_acl = %d (max %d), acl_mtu = %d\n"
64652ea7090Splunky 	       "\tnum_sco = %d (max %d), sco_mtu = %d\n",
647a5c89047Sgdamore 	       btr.btr_num_cmd,
64852ea7090Splunky 	       btr.btr_num_acl, btr.btr_max_acl, btr.btr_acl_mtu,
64952ea7090Splunky 	       btr.btr_num_sco, btr.btr_max_sco, btr.btr_sco_mtu);
650a5c89047Sgdamore 
651a5c89047Sgdamore 	if (level-- < 1 || (btr.btr_flags & BTF_UP) == 0)
652a5c89047Sgdamore 		return;
653a5c89047Sgdamore 
654*42f8a9c2Splunky 	load_value(HCI_CMD_READ_LOCAL_VER, buf, 3);
655a2576166Splunky 	printf("\tHCI version: ");
656*42f8a9c2Splunky 	switch((version = buf[0])) {
657*42f8a9c2Splunky 	case HCI_SPEC_V10:	printf("1.0b");		break;
658*42f8a9c2Splunky 	case HCI_SPEC_V11:	printf("1.1");		break;
659*42f8a9c2Splunky 	case HCI_SPEC_V12:	printf("1.2");		break;
660*42f8a9c2Splunky 	case HCI_SPEC_V20:	printf("2.0 + EDR");	break;
661*42f8a9c2Splunky 	case HCI_SPEC_V21:	printf("2.1 + EDR");	break;
662*42f8a9c2Splunky 	case HCI_SPEC_V30:	printf("3.0 + HS");	break;
663*42f8a9c2Splunky 	case HCI_SPEC_V40:	printf("4.0");		break;
664*42f8a9c2Splunky 	case HCI_SPEC_V41:	printf("4.1");		break;
665*42f8a9c2Splunky 	case HCI_SPEC_V42:	printf("4.2");		break;
666*42f8a9c2Splunky 	case HCI_SPEC_V50:	printf("5.0");		break;
667*42f8a9c2Splunky 	default:		printf("[%d]", version);break;
668a2576166Splunky 	}
669*42f8a9c2Splunky 	printf(" rev 0x%04x\n", le16dec(&buf[1]));
670a2576166Splunky 
671a5c89047Sgdamore 	load_value(HCI_CMD_READ_UNIT_CLASS, buf, HCI_CLASS_SIZE);
6727efcafc9Splunky 	print_class("\tclass:", buf);
673a5c89047Sgdamore 
674a5c89047Sgdamore 	load_value(HCI_CMD_READ_LOCAL_NAME, buf, HCI_UNIT_NAME_SIZE);
675a5c89047Sgdamore 	printf("\tname: \"%s\"\n", buf);
676a5c89047Sgdamore 
677a5c89047Sgdamore 	load_value(HCI_CMD_READ_VOICE_SETTING, buf, sizeof(uint16_t));
678a5c89047Sgdamore 	voice = (buf[1] << 8) | buf[0];
679a5c89047Sgdamore 	print_voice(level);
680a5c89047Sgdamore 
681a5c89047Sgdamore 	load_value(HCI_CMD_READ_PIN_TYPE, &val, sizeof(val));
682a5c89047Sgdamore 	printf("\tpin: %s\n", val ? "fixed" : "variable");
683a5c89047Sgdamore 
68426f09dd3Splunky 	val = 0;
68526f09dd3Splunky 	if (version >= HCI_SPEC_V12)
68626f09dd3Splunky 		load_value(HCI_CMD_READ_INQUIRY_MODE, &val, sizeof(val));
68726f09dd3Splunky 
68826f09dd3Splunky 	print_val("inquiry mode", imodes, val);
68926f09dd3Splunky 
690a5c89047Sgdamore 	width = printf("\toptions:");
691a5c89047Sgdamore 
692a5c89047Sgdamore 	load_value(HCI_CMD_READ_SCAN_ENABLE, &val, sizeof(val));
693a5c89047Sgdamore 	if (val & HCI_INQUIRY_SCAN_ENABLE)	tag("iscan");
694a5c89047Sgdamore 	else if (level > 0)			tag("-iscan");
695a5c89047Sgdamore 
696a5c89047Sgdamore 	if (val & HCI_PAGE_SCAN_ENABLE)		tag("pscan");
697a5c89047Sgdamore 	else if (level > 0)			tag("-pscan");
698a5c89047Sgdamore 
699a5c89047Sgdamore 	load_value(HCI_CMD_READ_AUTH_ENABLE, &val, sizeof(val));
700a5c89047Sgdamore 	if (val)				tag("auth");
701a5c89047Sgdamore 	else if (level > 0)			tag("-auth");
702a5c89047Sgdamore 
703a5c89047Sgdamore 	load_value(HCI_CMD_READ_ENCRYPTION_MODE, &val, sizeof(val));
704a5c89047Sgdamore 	if (val)				tag("encrypt");
705a5c89047Sgdamore 	else if (level > 0)			tag("-encrypt");
706a5c89047Sgdamore 
707a5c89047Sgdamore 	val = btr.btr_link_policy;
708a5c89047Sgdamore 	if (val & HCI_LINK_POLICY_ENABLE_ROLE_SWITCH)	tag("switch");
709a5c89047Sgdamore 	else if (level > 0)				tag("-switch");
710a5c89047Sgdamore 	if (val & HCI_LINK_POLICY_ENABLE_HOLD_MODE)	tag("hold");
711a5c89047Sgdamore 	else if (level > 0)				tag("-hold");
712a5c89047Sgdamore 	if (val & HCI_LINK_POLICY_ENABLE_SNIFF_MODE)	tag("sniff");
713a5c89047Sgdamore 	else if (level > 0)				tag("-sniff");
714a5c89047Sgdamore 	if (val & HCI_LINK_POLICY_ENABLE_PARK_MODE)	tag("park");
715a5c89047Sgdamore 	else if (level > 0)				tag("-park");
716a5c89047Sgdamore 
717a5c89047Sgdamore 	tag(NULL);
718a5c89047Sgdamore 
719a5c89047Sgdamore 	if (level-- < 1)
720a5c89047Sgdamore 		return;
721a5c89047Sgdamore 
722eea08126Stron 	ptype = btr.btr_packet_type;
723eea08126Stron 	width = printf("\tptype: [0x%04x]", ptype);
724eea08126Stron 	if (ptype & HCI_PKT_DM1)		tag("DM1");
725eea08126Stron 	if (ptype & HCI_PKT_DH1)		tag("DH1");
726eea08126Stron 	if (ptype & HCI_PKT_DM3)		tag("DM3");
727eea08126Stron 	if (ptype & HCI_PKT_DH3)		tag("DH3");
728eea08126Stron 	if (ptype & HCI_PKT_DM5)		tag("DM5");
729eea08126Stron 	if (ptype & HCI_PKT_DH5)		tag("DH5");
730eea08126Stron 	if ((ptype & HCI_PKT_2MBPS_DH1) == 0)	tag("2-DH1");
731eea08126Stron 	if ((ptype & HCI_PKT_3MBPS_DH1) == 0)	tag("3-DH1");
732eea08126Stron 	if ((ptype & HCI_PKT_2MBPS_DH3) == 0)	tag("2-DH3");
733eea08126Stron 	if ((ptype & HCI_PKT_3MBPS_DH3) == 0)	tag("3-DH3");
734eea08126Stron 	if ((ptype & HCI_PKT_2MBPS_DH5) == 0)	tag("2-DH5");
735eea08126Stron 	if ((ptype & HCI_PKT_3MBPS_DH5) == 0)	tag("3-DH5");
736eea08126Stron 	tag(NULL);
737eea08126Stron 
73875833930Splunky 	load_value(HCI_CMD_READ_PAGE_TIMEOUT, buf, sizeof(uint16_t));
73975833930Splunky 	printf("\tpage timeout: %d ms\n", le16dec(buf) * 5 / 8);
740a5c89047Sgdamore 
741a5c89047Sgdamore 	if (level-- < 1)
742a5c89047Sgdamore 		return;
743a5c89047Sgdamore 
74452ea7090Splunky 	if (ioctl(hci, SIOCGBTFEAT, &btr) < 0)
74552ea7090Splunky 		err(EXIT_FAILURE, "SIOCGBTFEAT");
746d8166becSplunky 
74752ea7090Splunky 	width = printf("\tfeatures:");
74852ea7090Splunky 	print_features0(btr.btr_features0);
74952ea7090Splunky 	print_features1(btr.btr_features1);
75081aeedabSplunky 	print_features2(btr.btr_features2);
75152ea7090Splunky 	tag(NULL);
752a5c89047Sgdamore }
753a5c89047Sgdamore 
7548756b978Sjoerg static void
print_stats(void)755a5c89047Sgdamore print_stats(void)
756a5c89047Sgdamore {
757a5c89047Sgdamore 
758a5c89047Sgdamore 	if (sflag == 0)
759a5c89047Sgdamore 		return;
760a5c89047Sgdamore 
761a5c89047Sgdamore 	if (sflag == 1) {
762a5c89047Sgdamore 		if (ioctl(hci, SIOCGBTSTATS, &btr) < 0)
763a5c89047Sgdamore 			err(EXIT_FAILURE, "SIOCGBTSTATS");
764a5c89047Sgdamore 	} else  {
765a5c89047Sgdamore 		if (ioctl(hci, SIOCZBTSTATS, &btr) < 0)
766a5c89047Sgdamore 			err(EXIT_FAILURE, "SIOCZBTSTATS");
767a5c89047Sgdamore 	}
768a5c89047Sgdamore 
769c008138eSmbalmer 	printf( "\tTotal bytes sent %d, received %d\n"
770a5c89047Sgdamore 		"\tCommands sent %d, Events received %d\n"
771a5c89047Sgdamore 		"\tACL data packets sent %d, received %d\n"
772a5c89047Sgdamore 		"\tSCO data packets sent %d, received %d\n"
773a5c89047Sgdamore 		"\tInput errors %d, Output errors %d\n",
774a5c89047Sgdamore 		btr.btr_stats.byte_tx, btr.btr_stats.byte_rx,
775a5c89047Sgdamore 		btr.btr_stats.cmd_tx, btr.btr_stats.evt_rx,
776a5c89047Sgdamore 		btr.btr_stats.acl_tx, btr.btr_stats.acl_rx,
777a5c89047Sgdamore 		btr.btr_stats.sco_tx, btr.btr_stats.sco_rx,
778a5c89047Sgdamore 		btr.btr_stats.err_rx, btr.btr_stats.err_tx);
779a5c89047Sgdamore }
780a5c89047Sgdamore 
7818756b978Sjoerg static void
print_features0(uint8_t * f)7829501383aSplunky print_features0(uint8_t *f)
7839501383aSplunky {
784a5c89047Sgdamore 
785a5c89047Sgdamore 	/* ------------------- byte 0 --------------------*/
786a5c89047Sgdamore 	if (*f & HCI_LMP_3SLOT)		    tag("<3 slot>");
787a5c89047Sgdamore 	if (*f & HCI_LMP_5SLOT)		    tag("<5 slot>");
788a5c89047Sgdamore 	if (*f & HCI_LMP_ENCRYPTION)	    tag("<encryption>");
789a5c89047Sgdamore 	if (*f & HCI_LMP_SLOT_OFFSET)	    tag("<slot offset>");
790a5c89047Sgdamore 	if (*f & HCI_LMP_TIMIACCURACY)	    tag("<timing accuracy>");
791a5c89047Sgdamore 	if (*f & HCI_LMP_ROLE_SWITCH)	    tag("<role switch>");
792a5c89047Sgdamore 	if (*f & HCI_LMP_HOLD_MODE)	    tag("<hold mode>");
793a5c89047Sgdamore 	if (*f & HCI_LMP_SNIFF_MODE)	    tag("<sniff mode>");
794a5c89047Sgdamore 	f++;
795a5c89047Sgdamore 
796a5c89047Sgdamore 	/* ------------------- byte 1 --------------------*/
797a5c89047Sgdamore 	if (*f & HCI_LMP_PARK_MODE)	    tag("<park mode>");
798a5c89047Sgdamore 	if (*f & HCI_LMP_RSSI)		    tag("<RSSI>");
799a5c89047Sgdamore 	if (*f & HCI_LMP_CHANNEL_QUALITY)   tag("<channel quality>");
800a5c89047Sgdamore 	if (*f & HCI_LMP_SCO_LINK)	    tag("<SCO link>");
801a5c89047Sgdamore 	if (*f & HCI_LMP_HV2_PKT)	    tag("<HV2>");
802a5c89047Sgdamore 	if (*f & HCI_LMP_HV3_PKT)	    tag("<HV3>");
803a5c89047Sgdamore 	if (*f & HCI_LMP_ULAW_LOG)	    tag("<u-Law log>");
804a5c89047Sgdamore 	if (*f & HCI_LMP_ALAW_LOG)	    tag("<A-Law log>");
805a5c89047Sgdamore 	f++;
806a5c89047Sgdamore 
807a5c89047Sgdamore 	/* ------------------- byte 1 --------------------*/
808a5c89047Sgdamore 	if (*f & HCI_LMP_CVSD)		    tag("<CVSD data>");
809a5c89047Sgdamore 	if (*f & HCI_LMP_PAGISCHEME)	    tag("<paging parameter>");
810a5c89047Sgdamore 	if (*f & HCI_LMP_POWER_CONTROL)	    tag("<power control>");
811a5c89047Sgdamore 	if (*f & HCI_LMP_TRANSPARENT_SCO)   tag("<transparent SCO>");
81281aeedabSplunky 	if (*f & HCI_LMP_FLOW_CONTROL_LAG0) tag("<flow control lag lsb>");
81381aeedabSplunky 	if (*f & HCI_LMP_FLOW_CONTROL_LAG1) tag("<flow control lag mb>");
81481aeedabSplunky 	if (*f & HCI_LMP_FLOW_CONTROL_LAG2) tag("<flow control lag msb>");
815a5c89047Sgdamore 	if (*f & HCI_LMP_BC_ENCRYPTION)	    tag("<broadcast encryption>");
816a5c89047Sgdamore 	f++;
817a5c89047Sgdamore 
818a5c89047Sgdamore 	/* ------------------- byte 3 --------------------*/
819a5c89047Sgdamore 	if (*f & HCI_LMP_EDR_ACL_2MBPS)	    tag("<EDR ACL 2Mbps>");
820a5c89047Sgdamore 	if (*f & HCI_LMP_EDR_ACL_3MBPS)	    tag("<EDR ACL 3Mbps>");
821a5c89047Sgdamore 	if (*f & HCI_LMP_ENHANCED_ISCAN)    tag("<enhanced inquiry scan>");
822a5c89047Sgdamore 	if (*f & HCI_LMP_INTERLACED_ISCAN)  tag("<interlaced inquiry scan>");
823a5c89047Sgdamore 	if (*f & HCI_LMP_INTERLACED_PSCAN)  tag("<interlaced page scan>");
824a5c89047Sgdamore 	if (*f & HCI_LMP_RSSI_INQUIRY)	    tag("<RSSI with inquiry result>");
825a5c89047Sgdamore 	if (*f & HCI_LMP_EV3_PKT)	    tag("<EV3 packets>");
826a5c89047Sgdamore 	f++;
827a5c89047Sgdamore 
828a5c89047Sgdamore 	/* ------------------- byte 4 --------------------*/
829a5c89047Sgdamore 	if (*f & HCI_LMP_EV4_PKT)	    tag("<EV4 packets>");
830a5c89047Sgdamore 	if (*f & HCI_LMP_EV5_PKT)	    tag("<EV5 packets>");
831a5c89047Sgdamore 	if (*f & HCI_LMP_AFH_CAPABLE_SLAVE) tag("<AFH capable slave>");
832a5c89047Sgdamore 	if (*f & HCI_LMP_AFH_CLASS_SLAVE)   tag("<AFH class slave>");
83381aeedabSplunky 	if (*f & HCI_LMP_BR_EDR_UNSUPPORTED)tag("<BR/EDR not supported>");
83481aeedabSplunky 	if (*f & HCI_LMP_LE_CONTROLLER)     tag("<LE (controller)>");
835a5c89047Sgdamore 	if (*f & HCI_LMP_3SLOT_EDR_ACL)	    tag("<3 slot EDR ACL>");
836a5c89047Sgdamore 	f++;
837a5c89047Sgdamore 
838a5c89047Sgdamore 	/* ------------------- byte 5 --------------------*/
839a5c89047Sgdamore 	if (*f & HCI_LMP_5SLOT_EDR_ACL)	    tag("<5 slot EDR ACL>");
8400bda0fe1Splunky 	if (*f & HCI_LMP_SNIFF_SUBRATING)   tag("<sniff subrating>");
8410bda0fe1Splunky 	if (*f & HCI_LMP_PAUSE_ENCRYPTION)  tag("<pause encryption>");
842a5c89047Sgdamore 	if (*f & HCI_LMP_AFH_CAPABLE_MASTER)tag("<AFH capable master>");
843a5c89047Sgdamore 	if (*f & HCI_LMP_AFH_CLASS_MASTER)  tag("<AFH class master>");
844a5c89047Sgdamore 	if (*f & HCI_LMP_EDR_eSCO_2MBPS)    tag("<EDR eSCO 2Mbps>");
845a5c89047Sgdamore 	if (*f & HCI_LMP_EDR_eSCO_3MBPS)    tag("<EDR eSCO 3Mbps>");
846a5c89047Sgdamore 	if (*f & HCI_LMP_3SLOT_EDR_eSCO)    tag("<3 slot EDR eSCO>");
847a5c89047Sgdamore 	f++;
848a5c89047Sgdamore 
849a5c89047Sgdamore 	/* ------------------- byte 6 --------------------*/
8500bda0fe1Splunky 	if (*f & HCI_LMP_EXTENDED_INQUIRY)  tag("<extended inquiry>");
85181aeedabSplunky 	if (*f & HCI_LMP_LE_BR_EDR_CONTROLLER)tag("<simultaneous LE & BR/EDR (controller)>");
8520bda0fe1Splunky 	if (*f & HCI_LMP_SIMPLE_PAIRING)    tag("<secure simple pairing>");
8530bda0fe1Splunky 	if (*f & HCI_LMP_ENCAPSULATED_PDU)  tag("<encapsulated PDU>");
8540bda0fe1Splunky 	if (*f & HCI_LMP_ERRDATA_REPORTING) tag("<errdata reporting>");
8550bda0fe1Splunky 	if (*f & HCI_LMP_NOFLUSH_PB_FLAG)   tag("<no flush PB flag>");
856a5c89047Sgdamore 	f++;
857a5c89047Sgdamore 
858a5c89047Sgdamore 	/* ------------------- byte 7 --------------------*/
8590bda0fe1Splunky 	if (*f & HCI_LMP_LINK_SUPERVISION_TO)tag("<link supervision timeout changed>");
8600bda0fe1Splunky 	if (*f & HCI_LMP_INQ_RSP_TX_POWER)  tag("<inquiry rsp TX power level>");
8619501383aSplunky 	if (*f & HCI_LMP_ENHANCED_POWER_CONTROL)tag("<enhanced power control>");
862a5c89047Sgdamore 	if (*f & HCI_LMP_EXTENDED_FEATURES) tag("<extended features>");
8639501383aSplunky }
864a5c89047Sgdamore 
8658756b978Sjoerg static void
print_features1(uint8_t * f)8669501383aSplunky print_features1(uint8_t *f)
8679501383aSplunky {
8689501383aSplunky 
8699501383aSplunky 	/* ------------------- byte 0 --------------------*/
87081aeedabSplunky 	if (*f & HCI_LMP_SSP)		    tag("<secure simple pairing (host)>");
87181aeedabSplunky 	if (*f & HCI_LMP_LE_HOST)	    tag("<LE (host)>");
87281aeedabSplunky 	if (*f & HCI_LMP_LE_BR_EDR_HOST)    tag("<simultaneous LE & BR/EDR (host)>");
87381aeedabSplunky 	if (*f & HCI_LMP_SECURE_CONN_HOST)  tag("<secure connections (host)>");
87481aeedabSplunky }
87581aeedabSplunky 
87681aeedabSplunky static void
print_features2(uint8_t * f)87781aeedabSplunky print_features2(uint8_t *f)
87881aeedabSplunky {
87981aeedabSplunky 	/* ------------------- byte 0 --------------------*/
88081aeedabSplunky 	if (*f & HCI_LMP_CONNLESS_MASTER)   tag("<connectionless master>");
88181aeedabSplunky 	if (*f & HCI_LMP_CONNLESS_SLAVE)    tag("<connectionless slave>");
88281aeedabSplunky 	if (*f & HCI_LMP_SYNC_TRAIN)	    tag("<synchronization train>");
88381aeedabSplunky 	if (*f & HCI_LMP_SYNC_SCAN)	    tag("<synchronization scan>");
88481aeedabSplunky 	if (*f & HCI_LMP_INQ_RSP_NOTIFY)    tag("<inquiry response notification>");
88581aeedabSplunky 	if (*f & HCI_LMP_INTERLACE_SCAN)    tag("<generalized interlace scan>");
88681aeedabSplunky 	if (*f & HCI_LMP_COARSE_CLOCK)	    tag("<coarse clock adjustment>");
88781aeedabSplunky 
88881aeedabSplunky 	/* ------------------- byte 1 --------------------*/
88981aeedabSplunky 	if (*f & HCI_LMP_SECURE_CONN_CONTROLLER)tag("<secure connections (controller)>");
89081aeedabSplunky 	if (*f & HCI_LMP_PING)		    tag("<ping>");
89181aeedabSplunky 	if (*f & HCI_LMP_TRAIN_NUDGING)	    tag("<train nudging>");
892a5c89047Sgdamore }
893a5c89047Sgdamore 
8948756b978Sjoerg static void
print_class(const char * str,uint8_t * uclass)8957efcafc9Splunky print_class(const char *str, uint8_t *uclass)
896a5c89047Sgdamore {
897a5c89047Sgdamore 
8987efcafc9Splunky 	class = (uclass[2] << 16) | (uclass[1] << 8) | uclass[0];
8997efcafc9Splunky 	width = printf("%s [0x%06x]", str, class);
900a5c89047Sgdamore 
9017efcafc9Splunky 	switch(__SHIFTOUT(class, __BITS(0, 1))) {
9027efcafc9Splunky 	case 0:	print_class0();		break;
9037efcafc9Splunky 	default:			break;
904ba4cd6cdSplunky 	}
9057efcafc9Splunky 
9067efcafc9Splunky 	tag(NULL);
9077efcafc9Splunky }
9087efcafc9Splunky 
9098756b978Sjoerg static void
print_class0(void)9107efcafc9Splunky print_class0(void)
9117efcafc9Splunky {
9127efcafc9Splunky 
9137efcafc9Splunky 	switch (__SHIFTOUT(class, __BITS(8, 12))) {
9147efcafc9Splunky 	case 1:	/* Computer */
9157efcafc9Splunky 		switch (__SHIFTOUT(class, __BITS(2, 7))) {
9167efcafc9Splunky 		case 1: tag("Desktop workstation");		break;
9177efcafc9Splunky 		case 2: tag("Server-class computer");		break;
9187efcafc9Splunky 		case 3: tag("Laptop");				break;
9197efcafc9Splunky 		case 4: tag("Handheld PC/PDA");			break;
9207efcafc9Splunky 		case 5: tag("Palm Sized PC/PDA");		break;
9217efcafc9Splunky 		case 6: tag("Wearable computer");		break;
9227efcafc9Splunky 		default: tag("Computer");			break;
9237efcafc9Splunky 		}
924a5c89047Sgdamore 		break;
925a5c89047Sgdamore 
926a5c89047Sgdamore 	case 2:	/* Phone */
9277efcafc9Splunky 		switch (__SHIFTOUT(class, __BITS(2, 7))) {
928a5c89047Sgdamore 		case 1: tag("Cellular Phone");			break;
929a5c89047Sgdamore 		case 2: tag("Cordless Phone");			break;
930a5c89047Sgdamore 		case 3: tag("Smart Phone");			break;
931a5c89047Sgdamore 		case 4: tag("Wired Modem/Phone Gateway");	break;
932a5c89047Sgdamore 		case 5: tag("Common ISDN");			break;
933a5c89047Sgdamore 		default:tag("Phone");				break;
934a5c89047Sgdamore 		}
935a5c89047Sgdamore 		break;
936a5c89047Sgdamore 
937a5c89047Sgdamore 	case 3:	/* LAN */
938a5c89047Sgdamore 		tag("LAN");
9397efcafc9Splunky 		switch (__SHIFTOUT(class, __BITS(5, 7))) {
940a5c89047Sgdamore 		case 0: tag("[Fully available]");		break;
941a5c89047Sgdamore 		case 1: tag("[1-17% utilised]");		break;
942a5c89047Sgdamore 		case 2: tag("[17-33% utilised]");		break;
943a5c89047Sgdamore 		case 3: tag("[33-50% utilised]");		break;
944a5c89047Sgdamore 		case 4: tag("[50-67% utilised]");		break;
945a5c89047Sgdamore 		case 5: tag("[67-83% utilised]");		break;
946a5c89047Sgdamore 		case 6: tag("[83-99% utilised]");		break;
947a5c89047Sgdamore 		case 7: tag("[No service available]");		break;
948a5c89047Sgdamore 		}
949a5c89047Sgdamore 		break;
950a5c89047Sgdamore 
951a5c89047Sgdamore 	case 4:	/* Audio/Visual */
9527efcafc9Splunky 		switch (__SHIFTOUT(class, __BITS(2, 7))) {
953a5c89047Sgdamore 		case 1: tag("Wearable Headset");		break;
954a5c89047Sgdamore 		case 2: tag("Hands-free Audio");		break;
955a5c89047Sgdamore 		case 4: tag("Microphone");			break;
956a5c89047Sgdamore 		case 5: tag("Loudspeaker");			break;
957a5c89047Sgdamore 		case 6: tag("Headphones");			break;
958a5c89047Sgdamore 		case 7: tag("Portable Audio");			break;
959a5c89047Sgdamore 		case 8: tag("Car Audio");			break;
960a5c89047Sgdamore 		case 9: tag("Set-top Box");			break;
961a5c89047Sgdamore 		case 10: tag("HiFi Audio");			break;
962a5c89047Sgdamore 		case 11: tag("VCR");				break;
963a5c89047Sgdamore 		case 12: tag("Video Camera");			break;
964a5c89047Sgdamore 		case 13: tag("Camcorder");			break;
965a5c89047Sgdamore 		case 14: tag("Video Monitor");			break;
966a5c89047Sgdamore 		case 15: tag("Video Display and Loudspeaker");	break;
967a5c89047Sgdamore 		case 16: tag("Video Conferencing");		break;
968a5c89047Sgdamore 		case 18: tag("A/V [Gaming/Toy]");		break;
969a5c89047Sgdamore 		default: tag("Audio/Visual");			break;
970a5c89047Sgdamore 		}
971a5c89047Sgdamore 		break;
972a5c89047Sgdamore 
973a5c89047Sgdamore 	case 5:	/* Peripheral */
9747efcafc9Splunky 		switch (__SHIFTOUT(class, __BITS(2, 5))) {
975a5c89047Sgdamore 		case 1: tag("Joystick");			break;
976a5c89047Sgdamore 		case 2: tag("Gamepad");				break;
977a5c89047Sgdamore 		case 3: tag("Remote Control");			break;
978a5c89047Sgdamore 		case 4: tag("Sensing Device");			break;
979a5c89047Sgdamore 		case 5: tag("Digitiser Tablet");		break;
980a5c89047Sgdamore 		case 6: tag("Card Reader");			break;
981a5c89047Sgdamore 		default: tag("Peripheral");			break;
982a5c89047Sgdamore 		}
983a5c89047Sgdamore 
9847efcafc9Splunky 		if (class & __BIT(6)) tag("Keyboard");
9857efcafc9Splunky 		if (class & __BIT(7)) tag("Mouse");
986a5c89047Sgdamore 		break;
987a5c89047Sgdamore 
988a5c89047Sgdamore 	case 6:	/* Imaging */
9897efcafc9Splunky 		if (class & __BIT(4)) tag("Display");
9907efcafc9Splunky 		if (class & __BIT(5)) tag("Camera");
9917efcafc9Splunky 		if (class & __BIT(6)) tag("Scanner");
9927efcafc9Splunky 		if (class & __BIT(7)) tag("Printer");
9937efcafc9Splunky 		if ((class & __BITS(4, 7)) == 0) tag("Imaging");
994a5c89047Sgdamore 		break;
995a5c89047Sgdamore 
996a5c89047Sgdamore 	case 7:	/* Wearable */
9977efcafc9Splunky 		switch (__SHIFTOUT(class, __BITS(2, 7))) {
998a5c89047Sgdamore 		case 1: tag("Wrist Watch");			break;
999a5c89047Sgdamore 		case 2: tag("Pager");				break;
1000a5c89047Sgdamore 		case 3: tag("Jacket");				break;
1001a5c89047Sgdamore 		case 4: tag("Helmet");				break;
1002a5c89047Sgdamore 		case 5: tag("Glasses");				break;
1003a5c89047Sgdamore 		default: tag("Wearable");			break;
1004a5c89047Sgdamore 		}
1005a5c89047Sgdamore 		break;
1006a5c89047Sgdamore 
1007a5c89047Sgdamore 	case 8:	/* Toy */
10087efcafc9Splunky 		switch (__SHIFTOUT(class, __BITS(2, 7))) {
1009a5c89047Sgdamore 		case 1: tag("Robot");				break;
1010a5c89047Sgdamore 		case 2: tag("Vehicle");				break;
1011a5c89047Sgdamore 		case 3: tag("Doll / Action Figure");		break;
1012a5c89047Sgdamore 		case 4: tag("Controller");			break;
1013a5c89047Sgdamore 		case 5: tag("Game");				break;
1014a5c89047Sgdamore 		default: tag("Toy");				break;
1015a5c89047Sgdamore 		}
1016a5c89047Sgdamore 		break;
1017a5c89047Sgdamore 
10187efcafc9Splunky 	case 9: /* Health */
10197efcafc9Splunky 		switch (__SHIFTOUT(class, __BITS(2, 7))) {
10207efcafc9Splunky 		case 1:	tag("Blood Pressure Monitor");		break;
10217efcafc9Splunky 		case 2:	tag("Thermometer");			break;
10227efcafc9Splunky 		case 3:	tag("Weighing Scale");			break;
10237efcafc9Splunky 		case 4:	tag("Glucose Meter");			break;
10247efcafc9Splunky 		case 5:	tag("Pulse Oximeter");			break;
10257efcafc9Splunky 		case 6:	tag("Heart/Pulse Rate Monitor");	break;
10267efcafc9Splunky 		case 7:	tag("Health Data Display");		break;
10277efcafc9Splunky 		default: tag("Health");				break;
10287efcafc9Splunky 		}
10297efcafc9Splunky 		break;
10307efcafc9Splunky 
1031a5c89047Sgdamore 	default:
1032a5c89047Sgdamore 		break;
1033a5c89047Sgdamore 	}
1034a5c89047Sgdamore 
10357efcafc9Splunky 	if (class & __BIT(13))	tag("<Limited Discoverable>");
10367efcafc9Splunky 	if (class & __BIT(16))	tag("<Positioning>");
10377efcafc9Splunky 	if (class & __BIT(17))	tag("<Networking>");
10387efcafc9Splunky 	if (class & __BIT(18))	tag("<Rendering>");
10397efcafc9Splunky 	if (class & __BIT(19))	tag("<Capturing>");
10407efcafc9Splunky 	if (class & __BIT(20))	tag("<Object Transfer>");
10417efcafc9Splunky 	if (class & __BIT(21))	tag("<Audio>");
10427efcafc9Splunky 	if (class & __BIT(22))	tag("<Telephony>");
10437efcafc9Splunky 	if (class & __BIT(23))	tag("<Information>");
1044a5c89047Sgdamore }
1045a5c89047Sgdamore 
10468756b978Sjoerg static void
print_voice(int level)1047a5c89047Sgdamore print_voice(int level)
1048a5c89047Sgdamore {
10493729e633Splunky 	printf("\tvoice: [0x%4.4x]\n", voice);
1050a5c89047Sgdamore 
1051a5c89047Sgdamore 	if (level == 0)
1052a5c89047Sgdamore 		return;
1053a5c89047Sgdamore 
1054a5c89047Sgdamore 	printf("\t\tInput Coding: ");
1055a5c89047Sgdamore 	switch ((voice & 0x0300) >> 8) {
1056a5c89047Sgdamore 	case 0x00:	printf("Linear PCM [%d-bit, pos %d]",
1057a5c89047Sgdamore 			(voice & 0x0020 ? 16 : 8),
1058a5c89047Sgdamore 			(voice & 0x001c) >> 2);		break;
1059a5c89047Sgdamore 	case 0x01:	printf("u-Law");		break;
1060a5c89047Sgdamore 	case 0x02:	printf("A-Law");		break;
1061a5c89047Sgdamore 	case 0x03:	printf("unknown");		break;
1062a5c89047Sgdamore 	}
1063a5c89047Sgdamore 
1064a5c89047Sgdamore 	switch ((voice & 0x00c0) >> 6) {
1065a5c89047Sgdamore 	case 0x00:	printf(", 1's complement");	break;
1066a5c89047Sgdamore 	case 0x01:	printf(", 2's complement");	break;
1067a5c89047Sgdamore 	case 0x02:	printf(", sign magnitude");	break;
1068a5c89047Sgdamore 	case 0x03:	printf(", unsigned");		break;
1069a5c89047Sgdamore 	}
1070a5c89047Sgdamore 
1071a5c89047Sgdamore 	printf("\n\t\tAir Coding: ");
1072a5c89047Sgdamore 	switch (voice & 0x0003) {
1073a5c89047Sgdamore 	case 0x00:	printf("CVSD");			break;
1074a5c89047Sgdamore 	case 0x01:	printf("u-Law");		break;
1075a5c89047Sgdamore 	case 0x02:	printf("A-Law");		break;
1076a5c89047Sgdamore 	case 0x03:	printf("Transparent");		break;
1077a5c89047Sgdamore 	}
1078a5c89047Sgdamore 
1079a5c89047Sgdamore 	printf("\n");
1080a5c89047Sgdamore }
1081a5c89047Sgdamore 
10828756b978Sjoerg static void
print_result(int num,struct bt_devinquiry * r)1083d8166becSplunky print_result(int num, struct bt_devinquiry *r)
1084a5c89047Sgdamore {
1085a5c89047Sgdamore 	hci_remote_name_req_cp ncp;
1086a5c89047Sgdamore 	hci_remote_name_req_compl_ep nep;
1087a5c89047Sgdamore 	struct hostent *hp;
1088a5c89047Sgdamore 
1089d8166becSplunky 	printf("%3d: bdaddr %s", num, bt_ntoa(&r->bdaddr, NULL));
1090a5c89047Sgdamore 
1091a5c89047Sgdamore 	hp = bt_gethostbyaddr((const char *)&r->bdaddr, sizeof(bdaddr_t), AF_BLUETOOTH);
1092a5c89047Sgdamore 	if (hp != NULL)
1093a5c89047Sgdamore 		printf(" (%s)", hp->h_name);
1094a5c89047Sgdamore 
1095a5c89047Sgdamore 	printf("\n");
1096a5c89047Sgdamore 
10970d2b4f91Splunky 	memset(&ncp, 0, sizeof(ncp));
1098a5c89047Sgdamore 	bdaddr_copy(&ncp.bdaddr, &r->bdaddr);
1099d8166becSplunky 	ncp.page_scan_rep_mode = r->pscan_rep_mode;
1100a5c89047Sgdamore 	ncp.clock_offset = r->clock_offset;
1101a5c89047Sgdamore 
1102a5c89047Sgdamore 	hci_req(HCI_CMD_REMOTE_NAME_REQ,
1103a5c89047Sgdamore 		HCI_EVENT_REMOTE_NAME_REQ_COMPL,
1104a5c89047Sgdamore 		&ncp, sizeof(ncp),
1105a5c89047Sgdamore 		&nep, sizeof(nep));
1106a5c89047Sgdamore 
1107a5c89047Sgdamore 	printf("   : name \"%s\"\n", nep.name);
1108d8166becSplunky 	print_class("   : class", r->dev_class);
1109d8166becSplunky 	printf("   : page scan rep mode 0x%02x\n", r->pscan_rep_mode);
1110a5c89047Sgdamore 	printf("   : clock offset %d\n", le16toh(r->clock_offset));
11110d2b4f91Splunky 	printf("   : rssi %d\n", r->rssi);
1112a5c89047Sgdamore 	printf("\n");
1113a5c89047Sgdamore }
1114a5c89047Sgdamore 
11158756b978Sjoerg static void
do_inquiry(void)1116a5c89047Sgdamore do_inquiry(void)
1117a5c89047Sgdamore {
1118d8166becSplunky 	struct bt_devinquiry *result;
1119d8166becSplunky 	int i, num;
1120a5c89047Sgdamore 
1121a5c89047Sgdamore 	if (opt_inquiry == 0)
1122a5c89047Sgdamore 		return;
1123a5c89047Sgdamore 
1124a5c89047Sgdamore 	printf("Device Discovery from device: %s ...", btr.btr_name);
1125a5c89047Sgdamore 	fflush(stdout);
1126a5c89047Sgdamore 
1127d8166becSplunky 	num = bt_devinquiry(btr.btr_name, INQUIRY_LENGTH,
1128d8166becSplunky 	    INQUIRY_MAX_RESPONSES, &result);
1129a5c89047Sgdamore 
1130d8166becSplunky 	if (num == -1) {
1131d8166becSplunky 		printf("failed\n");
1132d8166becSplunky 		err(EXIT_FAILURE, "%s", btr.btr_name);
1133a5c89047Sgdamore 	}
1134a5c89047Sgdamore 
1135a5c89047Sgdamore 	printf(" %d response%s\n", num, (num == 1 ? "" : "s"));
1136a5c89047Sgdamore 
1137a5c89047Sgdamore 	for (i = 0 ; i < num ; i++)
1138d8166becSplunky 		print_result(i + 1, &result[i]);
1139d8166becSplunky 
1140d8166becSplunky 	free(result);
1141a5c89047Sgdamore }
1142