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