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