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