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