Home | History | Annotate | Line # | Download | only in btattach
btattach.c revision 1.1
      1  1.1  plunky /*	$NetBSD: btattach.c,v 1.1 2008/04/15 11:17:48 plunky Exp $	*/
      2  1.1  plunky 
      3  1.1  plunky /*-
      4  1.1  plunky  * Copyright (c) 2008 Iain Hibbert
      5  1.1  plunky  * All rights reserved.
      6  1.1  plunky  *
      7  1.1  plunky  * Redistribution and use in source and binary forms, with or without
      8  1.1  plunky  * modification, are permitted provided that the following conditions
      9  1.1  plunky  * are met:
     10  1.1  plunky  * 1. Redistributions of source code must retain the above copyright
     11  1.1  plunky  *    notice, this list of conditions and the following disclaimer.
     12  1.1  plunky  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  plunky  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  plunky  *    documentation and/or other materials provided with the distribution.
     15  1.1  plunky  *
     16  1.1  plunky  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.1  plunky  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  1.1  plunky  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.1  plunky  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  1.1  plunky  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  1.1  plunky  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  1.1  plunky  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  1.1  plunky  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  1.1  plunky  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  1.1  plunky  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  1.1  plunky  */
     27  1.1  plunky 
     28  1.1  plunky #include <sys/cdefs.h>
     29  1.1  plunky __COPYRIGHT("@(#) Copyright (c) 2008 Iain Hibbert\n"
     30  1.1  plunky 	    "All rights reserved.\n");
     31  1.1  plunky __RCSID("$NetBSD: btattach.c,v 1.1 2008/04/15 11:17:48 plunky Exp $");
     32  1.1  plunky 
     33  1.1  plunky #include <sys/ioctl.h>
     34  1.1  plunky #include <sys/param.h>
     35  1.1  plunky #include <sys/uio.h>
     36  1.1  plunky 
     37  1.1  plunky #include <bluetooth.h>
     38  1.1  plunky #include <err.h>
     39  1.1  plunky #include <errno.h>
     40  1.1  plunky #include <fcntl.h>
     41  1.1  plunky #include <stdio.h>
     42  1.1  plunky #include <stdlib.h>
     43  1.1  plunky #include <string.h>
     44  1.1  plunky #include <termios.h>
     45  1.1  plunky #include <unistd.h>
     46  1.1  plunky #include <util.h>
     47  1.1  plunky 
     48  1.1  plunky #include "btattach.h"
     49  1.1  plunky 
     50  1.1  plunky static void sighandler(int);
     51  1.1  plunky static void usage(void);
     52  1.1  plunky 
     53  1.1  plunky static int sigcount = 0;	/* signals received */
     54  1.1  plunky static int opt_debug = 0;	/* global? */
     55  1.1  plunky 
     56  1.1  plunky const struct devtype types[] = {
     57  1.1  plunky     {
     58  1.1  plunky 	.name = "bcsp",
     59  1.1  plunky 	.line = "bcsp",
     60  1.1  plunky 	.descr = "Generic BlueCore Serial Protocol",
     61  1.1  plunky 	.cflag = CRTSCTS,
     62  1.1  plunky 	.speed = B57600,
     63  1.1  plunky     },
     64  1.1  plunky     {
     65  1.1  plunky 	.name = "bcm2035",
     66  1.1  plunky 	.line = "btuart",
     67  1.1  plunky 	.descr = "Broadcom BCM2035",
     68  1.1  plunky 	.init = &init_bcm2035,
     69  1.1  plunky 	.speed = B115200,
     70  1.1  plunky     },
     71  1.1  plunky     {
     72  1.1  plunky 	.name = "bgb2xx",
     73  1.1  plunky 	.line = "btuart",
     74  1.1  plunky 	.descr = "Philips BGB2xx module",
     75  1.1  plunky 	.init = &init_bgb2xx,
     76  1.1  plunky 	.cflag = CRTSCTS,
     77  1.1  plunky 	.speed = B115200,
     78  1.1  plunky     },
     79  1.1  plunky     {
     80  1.1  plunky 	.name = "btuart",
     81  1.1  plunky 	.line = "btuart",
     82  1.1  plunky 	.descr = "Generic UART (the default)",
     83  1.1  plunky     },
     84  1.1  plunky     {
     85  1.1  plunky 	.name = "csr",
     86  1.1  plunky 	.line = "btuart",
     87  1.1  plunky 	.descr = "CSR Casira serial adaptor",
     88  1.1  plunky 	.init = &init_csr,
     89  1.1  plunky 	.cflag = CRTSCTS,
     90  1.1  plunky 	.speed = B57600,
     91  1.1  plunky     },
     92  1.1  plunky     {
     93  1.1  plunky 	.name = "digi",
     94  1.1  plunky 	.line = "btuart",
     95  1.1  plunky 	.descr = "Digianswer based cards",
     96  1.1  plunky 	.init = &init_digi,
     97  1.1  plunky 	.cflag = CRTSCTS,
     98  1.1  plunky 	.speed = B9600,
     99  1.1  plunky     },
    100  1.1  plunky     {
    101  1.1  plunky 	.name = "ericsson",
    102  1.1  plunky 	.line = "btuart",
    103  1.1  plunky 	.descr = "Ericsson based modules",
    104  1.1  plunky 	.init = &init_ericsson,
    105  1.1  plunky 	.cflag = CRTSCTS,
    106  1.1  plunky 	.speed = B57600,
    107  1.1  plunky     },
    108  1.1  plunky     {
    109  1.1  plunky 	.name = "st",
    110  1.1  plunky 	.line = "btuart",
    111  1.1  plunky 	.descr = "ST Microelectronics minikits based on STLC2410/STLC2415",
    112  1.1  plunky 	.init = &init_st,
    113  1.1  plunky 	.cflag = CRTSCTS,
    114  1.1  plunky 	.speed = B57600,
    115  1.1  plunky     },
    116  1.1  plunky     {
    117  1.1  plunky 	.name = "stlc2500",
    118  1.1  plunky 	.descr = "ST Microelectronics minikits based on STLC2500",
    119  1.1  plunky 	.init = &init_stlc2500,
    120  1.1  plunky 	.cflag = CRTSCTS,
    121  1.1  plunky 	.speed = B115200,
    122  1.1  plunky     },
    123  1.1  plunky     {
    124  1.1  plunky 	.name = "swave",
    125  1.1  plunky 	.line = "btuart",
    126  1.1  plunky 	.descr = "Silicon Wave kits",
    127  1.1  plunky 	.init = &init_swave,
    128  1.1  plunky 	.cflag = CRTSCTS,
    129  1.1  plunky 	.speed = B57600,
    130  1.1  plunky     },
    131  1.1  plunky     {
    132  1.1  plunky 	.name = "texas",
    133  1.1  plunky 	.line = "btuart",
    134  1.1  plunky 	.descr = "Texas Instruments",
    135  1.1  plunky 	.cflag = CRTSCTS,
    136  1.1  plunky 	.speed = B115200,
    137  1.1  plunky     },
    138  1.1  plunky };
    139  1.1  plunky 
    140  1.1  plunky int
    141  1.1  plunky main(int argc, char *argv[])
    142  1.1  plunky {
    143  1.1  plunky 	const struct devtype *type;
    144  1.1  plunky 	struct termios tio;
    145  1.1  plunky 	unsigned int init_speed, speed;
    146  1.1  plunky 	tcflag_t cflag;
    147  1.1  plunky 	int fd, ch, i;
    148  1.1  plunky 	const char *name;
    149  1.1  plunky 	char *ptr;
    150  1.1  plunky 
    151  1.1  plunky 	init_speed = 0;
    152  1.1  plunky 	cflag = CLOCAL;
    153  1.1  plunky 	name = "btuart";
    154  1.1  plunky 
    155  1.1  plunky 	while ((ch = getopt(argc, argv, "dfi:op")) != -1) {
    156  1.1  plunky 		switch (ch) {
    157  1.1  plunky 		case 'd':
    158  1.1  plunky 			opt_debug++;
    159  1.1  plunky 			break;
    160  1.1  plunky 
    161  1.1  plunky 		case 'f':
    162  1.1  plunky 			cflag |= CRTSCTS;
    163  1.1  plunky 			break;
    164  1.1  plunky 
    165  1.1  plunky 		case 'i':
    166  1.1  plunky 			init_speed = strtoul(optarg, &ptr, 10);
    167  1.1  plunky 			if (ptr[0] != '\0')
    168  1.1  plunky 				errx(EXIT_FAILURE, "invalid speed: %s", optarg);
    169  1.1  plunky 
    170  1.1  plunky 			break;
    171  1.1  plunky 
    172  1.1  plunky 		case 'o':
    173  1.1  plunky 			cflag |= (PARENB | PARODD);
    174  1.1  plunky 			break;
    175  1.1  plunky 
    176  1.1  plunky 		case 'p':
    177  1.1  plunky 			cflag |= PARENB;
    178  1.1  plunky 			break;
    179  1.1  plunky 
    180  1.1  plunky 		case '?':
    181  1.1  plunky 		default:
    182  1.1  plunky 			usage();
    183  1.1  plunky 		}
    184  1.1  plunky 	}
    185  1.1  plunky 	argc -= optind;
    186  1.1  plunky 	argv += optind;
    187  1.1  plunky 
    188  1.1  plunky 	if (argc == 3) {
    189  1.1  plunky 		name = argv[0];
    190  1.1  plunky 		argv++;
    191  1.1  plunky 		argc--;
    192  1.1  plunky 	}
    193  1.1  plunky 
    194  1.1  plunky 	for (i = 0; ; i++) {
    195  1.1  plunky 		if (i == __arraycount(types))
    196  1.1  plunky 			errx(EXIT_FAILURE, "unknown type: %s", name);
    197  1.1  plunky 
    198  1.1  plunky 		type = &types[i];
    199  1.1  plunky 		if (strcasecmp(type->name, name) == 0)
    200  1.1  plunky 			break;
    201  1.1  plunky 	}
    202  1.1  plunky 
    203  1.1  plunky 	if (argc != 2)
    204  1.1  plunky 		usage();
    205  1.1  plunky 
    206  1.1  plunky 	/* parse tty speed */
    207  1.1  plunky 	speed = strtoul(argv[1], &ptr, 10);
    208  1.1  plunky 	if (ptr[0] != '\0')
    209  1.1  plunky 		errx(EXIT_FAILURE, "invalid speed: %s", argv[1]);
    210  1.1  plunky 
    211  1.1  plunky 	if (init_speed == 0)
    212  1.1  plunky 		init_speed = (type->speed ? type->speed : speed);
    213  1.1  plunky 
    214  1.1  plunky 	/* open tty */
    215  1.1  plunky 	if ((fd = open(argv[0], O_RDWR | O_NDELAY | O_EXLOCK, 0)) < 0)
    216  1.1  plunky 		err(EXIT_FAILURE, "%s", argv[0]);
    217  1.1  plunky 
    218  1.1  plunky 	/* setup tty */
    219  1.1  plunky 	if (tcgetattr(fd, &tio) < 0)
    220  1.1  plunky 		err(EXIT_FAILURE, "tcgetattr");
    221  1.1  plunky 
    222  1.1  plunky 	cfmakeraw(&tio);
    223  1.1  plunky 	tio.c_cflag |= (cflag | type->cflag);
    224  1.1  plunky 
    225  1.1  plunky 	if (cfsetspeed(&tio, init_speed) < 0
    226  1.1  plunky 	    || tcsetattr(fd, TCSANOW, &tio) < 0
    227  1.1  plunky 	    || tcflush(fd, TCIOFLUSH) < 0)
    228  1.1  plunky 		err(EXIT_FAILURE, "tty setup failed");
    229  1.1  plunky 
    230  1.1  plunky 	/* initialize device */
    231  1.1  plunky 	if (type->init != NULL)
    232  1.1  plunky 		(*type->init)(fd, speed);
    233  1.1  plunky 
    234  1.1  plunky 	if (cfsetspeed(&tio, speed) < 0
    235  1.1  plunky 	    || tcsetattr(fd, TCSADRAIN, &tio) < 0)
    236  1.1  plunky 		err(EXIT_FAILURE, "tty setup failed");
    237  1.1  plunky 
    238  1.1  plunky 	/* start line discipline */
    239  1.1  plunky 	if (ioctl(fd, TIOCSLINED, type->line) < 0)
    240  1.1  plunky 		err(EXIT_FAILURE, "%s", type->line);
    241  1.1  plunky 
    242  1.1  plunky 	if (opt_debug == 0 && daemon(0, 0) < 0)
    243  1.1  plunky 		warn("detach failed!");
    244  1.1  plunky 
    245  1.1  plunky 	/* store PID in "/var/run/btattach-{tty}.pid" */
    246  1.1  plunky 	ptr = strrchr(argv[0], '/');
    247  1.1  plunky 	asprintf(&ptr, "%s-%s", getprogname(), (ptr ? ptr + 1 : argv[0]));
    248  1.1  plunky 	if (ptr == NULL || pidfile(ptr) < 0)
    249  1.1  plunky 		warn("no pidfile");
    250  1.1  plunky 
    251  1.1  plunky 	free(ptr);
    252  1.1  plunky 
    253  1.1  plunky 	(void)signal(SIGHUP, sighandler);
    254  1.1  plunky 	(void)signal(SIGINT, sighandler);
    255  1.1  plunky 	(void)signal(SIGTERM, sighandler);
    256  1.1  plunky 	(void)signal(SIGTSTP, sighandler);
    257  1.1  plunky 	(void)signal(SIGUSR1, sighandler);
    258  1.1  plunky 	(void)signal(SIGUSR2, sighandler);
    259  1.1  plunky 
    260  1.1  plunky 	while (sigcount == 0)
    261  1.1  plunky 		select(0, NULL, NULL, NULL, NULL);
    262  1.1  plunky 
    263  1.1  plunky 	return EXIT_SUCCESS;
    264  1.1  plunky }
    265  1.1  plunky 
    266  1.1  plunky static void
    267  1.1  plunky usage(void)
    268  1.1  plunky {
    269  1.1  plunky 	int i;
    270  1.1  plunky 
    271  1.1  plunky 	fprintf(stderr,
    272  1.1  plunky 		"Usage: %s [-dfop] [-i speed] [type] tty speed\n"
    273  1.1  plunky 		"\n"
    274  1.1  plunky 		"Where:\n"
    275  1.1  plunky 		"\t-d          debug mode (no detach, dump io)\n"
    276  1.1  plunky 		"\t-f          enable flow control\n"
    277  1.1  plunky 		"\t-i speed    init speed\n"
    278  1.1  plunky 		"\t-o          odd parity\n"
    279  1.1  plunky 		"\t-p          even parity\n"
    280  1.1  plunky 		"\n"
    281  1.1  plunky 		"Known types:\n"
    282  1.1  plunky 		"", getprogname());
    283  1.1  plunky 
    284  1.1  plunky 	for (i = 0; i < __arraycount(types); i++)
    285  1.1  plunky 		fprintf(stderr, "\t%-12s%s\n", types[i].name, types[i].descr);
    286  1.1  plunky 
    287  1.1  plunky 	exit(EXIT_FAILURE);
    288  1.1  plunky }
    289  1.1  plunky 
    290  1.1  plunky static void
    291  1.1  plunky sighandler(int s)
    292  1.1  plunky {
    293  1.1  plunky 
    294  1.1  plunky 	sigcount++;
    295  1.1  plunky }
    296  1.1  plunky 
    297  1.1  plunky static void
    298  1.1  plunky hexdump(uint8_t *ptr, size_t len)
    299  1.1  plunky {
    300  1.1  plunky 
    301  1.1  plunky 	while (len--)
    302  1.1  plunky 		printf(" %2.2x", *ptr++);
    303  1.1  plunky }
    304  1.1  plunky 
    305  1.1  plunky /*
    306  1.1  plunky  * send HCI comamnd
    307  1.1  plunky  */
    308  1.1  plunky void
    309  1.1  plunky uart_send_cmd(int fd, uint16_t opcode, void *buf, size_t len)
    310  1.1  plunky {
    311  1.1  plunky 	struct iovec iov[2];
    312  1.1  plunky 	hci_cmd_hdr_t hdr;
    313  1.1  plunky 
    314  1.1  plunky 	hdr.type = HCI_CMD_PKT;
    315  1.1  plunky 	hdr.opcode = htole16(opcode);
    316  1.1  plunky 	hdr.length = len;
    317  1.1  plunky 
    318  1.1  plunky 	iov[0].iov_base = &hdr;
    319  1.1  plunky 	iov[0].iov_len = sizeof(hdr);
    320  1.1  plunky 	iov[1].iov_base = buf;
    321  1.1  plunky 	iov[1].iov_len = len;
    322  1.1  plunky 
    323  1.1  plunky 	if (opt_debug) {
    324  1.1  plunky 		printf("<<");
    325  1.1  plunky 		hexdump(iov[0].iov_base, iov[0].iov_len);
    326  1.1  plunky 		hexdump(iov[1].iov_base, iov[1].iov_len);
    327  1.1  plunky 		printf("\n");
    328  1.1  plunky 		fflush(stdout);
    329  1.1  plunky 	}
    330  1.1  plunky 
    331  1.1  plunky 	if (writev(fd, iov, __arraycount(iov)) < 0)
    332  1.1  plunky 		err(EXIT_FAILURE, "writev");
    333  1.1  plunky 
    334  1.1  plunky 	tcdrain(fd);
    335  1.1  plunky }
    336  1.1  plunky 
    337  1.1  plunky /*
    338  1.1  plunky  * get next character
    339  1.1  plunky  * store in iovec and inc counter if it fits
    340  1.1  plunky  */
    341  1.1  plunky static uint8_t
    342  1.1  plunky uart_getc(int fd, struct iovec *iov, int ioc, size_t *count)
    343  1.1  plunky {
    344  1.1  plunky 	uint8_t ch, *b;
    345  1.1  plunky 	ssize_t n;
    346  1.1  plunky 	size_t off;
    347  1.1  plunky 
    348  1.1  plunky 	n = read(fd, &ch, sizeof(ch));
    349  1.1  plunky 	if (n < 0)
    350  1.1  plunky 		err(EXIT_FAILURE, "read");
    351  1.1  plunky 
    352  1.1  plunky 	if (n == 0)
    353  1.1  plunky 		errx(EXIT_FAILURE, "eof");
    354  1.1  plunky 
    355  1.1  plunky 	if (opt_debug)
    356  1.1  plunky 		printf(" %2.2x", ch);
    357  1.1  plunky 
    358  1.1  plunky 	off = *count;
    359  1.1  plunky 	while (ioc > 0) {
    360  1.1  plunky 		if (iov->iov_len > off) {
    361  1.1  plunky 			b = iov->iov_base;
    362  1.1  plunky 			b[off] = ch;
    363  1.1  plunky 			*count += 1;
    364  1.1  plunky 			break;
    365  1.1  plunky 		}
    366  1.1  plunky 
    367  1.1  plunky 		off -= iov->iov_len;
    368  1.1  plunky 		iov++;
    369  1.1  plunky 		ioc--;
    370  1.1  plunky 	}
    371  1.1  plunky 
    372  1.1  plunky 	return ch;
    373  1.1  plunky }
    374  1.1  plunky 
    375  1.1  plunky /*
    376  1.1  plunky  * read next packet, storing into iovec
    377  1.1  plunky  */
    378  1.1  plunky static size_t
    379  1.1  plunky uart_recv_pkt(int fd, struct iovec *iov, int ioc)
    380  1.1  plunky {
    381  1.1  plunky 	size_t count, want;
    382  1.1  plunky 	uint8_t type;
    383  1.1  plunky 
    384  1.1  plunky 	if (opt_debug)
    385  1.1  plunky 		printf(">>");
    386  1.1  plunky 
    387  1.1  plunky 	count = 0;
    388  1.1  plunky 	type = uart_getc(fd, iov, ioc, &count);
    389  1.1  plunky 	switch(type) {
    390  1.1  plunky 	case HCI_EVENT_PKT:
    391  1.1  plunky 		(void)uart_getc(fd, iov, ioc, &count);	/* event */
    392  1.1  plunky 		want = sizeof(hci_event_hdr_t);
    393  1.1  plunky 		want += uart_getc(fd, iov, ioc, &count);
    394  1.1  plunky 		break;
    395  1.1  plunky 
    396  1.1  plunky 	case HCI_ACL_DATA_PKT:
    397  1.1  plunky 		(void)uart_getc(fd, iov, ioc, &count);	/* handle LSB */
    398  1.1  plunky 		(void)uart_getc(fd, iov, ioc, &count);	/* handle MSB */
    399  1.1  plunky 		want = sizeof(hci_acldata_hdr_t);
    400  1.1  plunky 		want += uart_getc(fd, iov, ioc, &count);	/* LSB */
    401  1.1  plunky 		want += uart_getc(fd, iov, ioc, &count) << 8;	/* MSB */
    402  1.1  plunky 		break;
    403  1.1  plunky 
    404  1.1  plunky 	case HCI_SCO_DATA_PKT:
    405  1.1  plunky 		(void)uart_getc(fd, iov, ioc, &count);	/* handle LSB */
    406  1.1  plunky 		(void)uart_getc(fd, iov, ioc, &count);	/* handle MSB */
    407  1.1  plunky 		want = sizeof(hci_scodata_hdr_t);
    408  1.1  plunky 		want += uart_getc(fd, iov, ioc, &count);
    409  1.1  plunky 		break;
    410  1.1  plunky 
    411  1.1  plunky 	default: /* out of sync? */
    412  1.1  plunky 		err(EXIT_FAILURE, "unknown packet type 0x%2.2x", type);
    413  1.1  plunky 	}
    414  1.1  plunky 
    415  1.1  plunky 	while (want-- > 0)
    416  1.1  plunky 		(void)uart_getc(fd, iov, ioc, &count);
    417  1.1  plunky 
    418  1.1  plunky 	if (opt_debug)
    419  1.1  plunky 		printf("\n");
    420  1.1  plunky 
    421  1.1  plunky 	return count;
    422  1.1  plunky }
    423  1.1  plunky 
    424  1.1  plunky /*
    425  1.1  plunky  * read next matching event packet to buffer
    426  1.1  plunky  */
    427  1.1  plunky size_t
    428  1.1  plunky uart_recv_ev(int fd, uint8_t event, void *buf, size_t len)
    429  1.1  plunky {
    430  1.1  plunky 	struct iovec iov[2];
    431  1.1  plunky 	hci_event_hdr_t hdr;
    432  1.1  plunky 	size_t n;
    433  1.1  plunky 
    434  1.1  plunky 	iov[0].iov_base = &hdr;
    435  1.1  plunky 	iov[0].iov_len = sizeof(hdr);
    436  1.1  plunky 	iov[1].iov_base = buf;
    437  1.1  plunky 	iov[1].iov_len = len;
    438  1.1  plunky 
    439  1.1  plunky 	for (;;) {
    440  1.1  plunky 		n = uart_recv_pkt(fd, iov, __arraycount(iov));
    441  1.1  plunky 		if (n < sizeof(hdr)
    442  1.1  plunky 		    || hdr.type != HCI_EVENT_PKT
    443  1.1  plunky 		    || hdr.event != event)
    444  1.1  plunky 			continue;
    445  1.1  plunky 
    446  1.1  plunky 		n -= sizeof(hdr);
    447  1.1  plunky 		break;
    448  1.1  plunky 	}
    449  1.1  plunky 
    450  1.1  plunky 	return n;
    451  1.1  plunky }
    452  1.1  plunky 
    453  1.1  plunky /*
    454  1.1  plunky  * read next matching command_complete event to buffer
    455  1.1  plunky  */
    456  1.1  plunky size_t
    457  1.1  plunky uart_recv_cc(int fd, uint16_t opcode, void *buf, size_t len)
    458  1.1  plunky {
    459  1.1  plunky 	struct iovec iov[3];
    460  1.1  plunky 	hci_event_hdr_t hdr;
    461  1.1  plunky 	hci_command_compl_ep cc;
    462  1.1  plunky 	size_t n;
    463  1.1  plunky 
    464  1.1  plunky 	iov[0].iov_base = &hdr;
    465  1.1  plunky 	iov[0].iov_len = sizeof(hdr);
    466  1.1  plunky 	iov[1].iov_base = &cc;
    467  1.1  plunky 	iov[1].iov_len = sizeof(cc);
    468  1.1  plunky 	iov[2].iov_base = buf;
    469  1.1  plunky 	iov[2].iov_len = len;
    470  1.1  plunky 
    471  1.1  plunky 	for (;;) {
    472  1.1  plunky 		n = uart_recv_pkt(fd, iov, __arraycount(iov));
    473  1.1  plunky 		if (n < sizeof(hdr)
    474  1.1  plunky 		    || hdr.type != HCI_EVENT_PKT
    475  1.1  plunky 		    || hdr.event != HCI_EVENT_COMMAND_COMPL)
    476  1.1  plunky 			continue;
    477  1.1  plunky 
    478  1.1  plunky 		n -= sizeof(hdr);
    479  1.1  plunky 		if (n < sizeof(cc)
    480  1.1  plunky 		    || cc.opcode != htole16(opcode))
    481  1.1  plunky 			continue;
    482  1.1  plunky 
    483  1.1  plunky 		n -= sizeof(cc);
    484  1.1  plunky 		break;
    485  1.1  plunky 	}
    486  1.1  plunky 
    487  1.1  plunky 	return n;
    488  1.1  plunky }
    489