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