Expand PMF_FN_* macros.
[netbsd-mini2440.git] / usr.bin / bthset / bthset.c
blobf7fa6659909df34dd290bfaeff6e874d15156e28
1 /* $NetBSD: bthset.c,v 1.4 2008/07/21 14:19:21 lukem Exp $ */
3 /*-
4 * Copyright (c) 2006 Itronix Inc.
5 * All rights reserved.
7 * Written by Iain Hibbert for Itronix Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of Itronix Inc. may not be used to endorse
18 * or promote products derived from this software without specific
19 * prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
25 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28 * ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/cdefs.h>
35 __COPYRIGHT("@(#) Copyright (c) 2006 Itronix, Inc. All rights reserved.");
36 __RCSID("$NetBSD: bthset.c,v 1.4 2008/07/21 14:19:21 lukem Exp $");
38 #include <sys/types.h>
39 #include <sys/audioio.h>
40 #include <sys/ioctl.h>
41 #include <sys/time.h>
43 #include <assert.h>
44 #include <bluetooth.h>
45 #include <err.h>
46 #include <event.h>
47 #include <fcntl.h>
48 #include <sdp.h>
49 #include <signal.h>
50 #include <stdarg.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <unistd.h>
55 #include <errno.h>
57 #include <dev/bluetooth/btdev.h>
58 #include <dev/bluetooth/btsco.h>
60 #include <netbt/rfcomm.h>
62 #define RING_INTERVAL 5 /* seconds */
64 int main(int, char *[]);
65 void usage(void);
67 void do_signal(int, short, void *);
68 void do_ring(int, short, void *);
69 void do_mixer(int, short, void *);
70 void do_rfcomm(int, short, void *);
71 void do_server(int, short, void *);
72 int send_rfcomm(const char *, ...);
74 int init_mixer(struct btsco_info *, const char *);
75 int init_rfcomm(struct btsco_info *);
76 int init_server(struct btsco_info *, int);
78 void remove_pid(void);
79 int write_pid(void);
81 struct event sigint_ev; /* bye bye */
82 struct event sigusr1_ev; /* start ringing */
83 struct event sigusr2_ev; /* stop ringing */
84 struct event mixer_ev; /* mixer changed */
85 struct event rfcomm_ev; /* headset speaks */
86 struct event server_ev; /* headset connecting */
87 struct event ring_ev; /* ring timer */
89 mixer_ctrl_t vgs; /* speaker control */
90 mixer_ctrl_t vgm; /* mic control */
91 int ringing; /* we are ringing */
92 int verbose; /* copy to stdout */
93 int mx; /* mixer fd */
94 int rf; /* rfcomm connection fd */
95 int ag; /* rfcomm gateway fd */
96 sdp_session_t ss; /* SDP server session */
98 char *command; /* answer command */
99 char *pidfile; /* PID file name */
101 /* Headset Audio Gateway service record */
102 uint8_t hset_data[] = {
103 0x09, 0x00, 0x00, // uint16 ServiceRecordHandle
104 0x0a, 0x00, 0x00, 0x00, // uint32 0x00000000
105 0x00,
107 0x09, 0x00, 0x01, // uint16 ServiceClassIDList
108 0x35, 0x06, // seq8(6)
109 0x19, 0x11, 0x12, // uuid16 HeadsetAudioGateway
110 0x19, 0x12, 0x03, // uuid16 GenericAudio
112 0x09, 0x00, 0x04, // uint16 ProtocolDescriptorList
113 0x35, 0x0c, // seq8(12)
114 0x35, 0x03, // seq8(3)
115 0x19, 0x01, 0x00, // uuid16 L2CAP
116 0x35, 0x05, // seq8(5)
117 0x19, 0x00, 0x03, // uuid16 RFCOMM
118 0x08, 0x00, // uint8 %hset_channel%
120 0x09, 0x00, 0x05, // uint16 BrowseGroupList
121 0x35, 0x03, // seq8(3)
122 0x19, 0x10, 0x02, // uuid16 PublicBrowseGroup
124 0x09, 0x00, 0x06, // uint16 LanguageBaseAttributeIDList
125 0x35, 0x09, // seq8(9)
126 0x09, 0x65, 0x6e, // uint16 0x656e ("en")
127 0x09, 0x00, 0x6a, // uint16 106 (UTF-8)
128 0x09, 0x01, 0x00, // uint16 PrimaryLanguageBaseID
130 0x09, 0x00, 0x09, // uint16 BluetoothProfileDescriptorList
131 0x35, 0x08, // seq8(8)
132 0x35, 0x06, // seq8(6)
133 0x19, 0x11, 0x08, // uuid16 Headset
134 0x09, 0x01, 0x00, // uint16 v1.0
136 0x09, 0x01, 0x00, // uint16 PrimaryLanguageBaseID + ServiceNameOffset
137 0x25, 0x0d, 0x56, 0x6f, // str8(13) "Voice Gateway"
138 0x69, 0x63, 0x65, 0x20,
139 0x47, 0x61, 0x74, 0x65,
140 0x77, 0x61, 0x79
143 sdp_data_t hset_record = { hset_data + 0, hset_data + 91 };
144 sdp_data_t hset_channel = { hset_data + 36, hset_data + 37 };
147 main(int ac, char *av[])
149 struct btsco_info info;
150 const char *mixer;
151 int ch, channel;
153 ag = rf = -1;
154 verbose = 0;
155 channel = 0;
156 pidfile = getenv("BTHSET_PIDFILE");
157 command = getenv("BTHSET_COMMAND");
158 mixer = getenv("BTHSET_MIXER");
159 if (mixer == NULL)
160 mixer = "/dev/mixer";
162 while ((ch = getopt(ac, av, "hc:m:p:s:v")) != EOF) {
163 switch (ch) {
164 case 'c':
165 command = optarg;
166 break;
168 case 'm':
169 mixer = optarg;
170 break;
172 case 'p':
173 pidfile = optarg;
174 break;
176 case 's':
177 channel = atoi(optarg);
178 break;
180 case 'v':
181 verbose = 1;
182 break;
184 case 'h':
185 default:
186 usage();
190 if (mixer == NULL)
191 usage();
193 if ((channel < RFCOMM_CHANNEL_MIN || channel > RFCOMM_CHANNEL_MAX)
194 && channel != 0)
195 usage();
197 if (write_pid() < 0)
198 err(EXIT_FAILURE, "%s", pidfile);
200 event_init();
202 ringing = 0;
203 evtimer_set(&ring_ev, do_ring, NULL);
205 signal_set(&sigusr1_ev, SIGUSR1, do_signal, NULL);
206 if (signal_add(&sigusr1_ev, NULL) < 0)
207 err(EXIT_FAILURE, "SIGUSR1");
209 signal_set(&sigusr2_ev, SIGUSR2, do_signal, NULL);
210 if (signal_add(&sigusr2_ev, NULL) < 0)
211 err(EXIT_FAILURE, "SIGUSR2");
213 signal_set(&sigint_ev, SIGINT, do_signal, NULL);
214 if (signal_add(&sigint_ev, NULL) < 0)
215 err(EXIT_FAILURE, "SIGINT");
217 if (init_mixer(&info, mixer) < 0)
218 err(EXIT_FAILURE, "%s", mixer);
220 if (channel == 0 && init_rfcomm(&info) < 0)
221 err(EXIT_FAILURE, "%s", bt_ntoa(&info.raddr, NULL));
223 if (channel && init_server(&info, channel) < 0)
224 err(EXIT_FAILURE, "%d", channel);
226 if (verbose) {
227 printf("Headset Info:\n");
228 printf("\tmixer: %s\n", mixer);
229 printf("\tladdr: %s\n", bt_ntoa(&info.laddr, NULL));
230 printf("\traddr: %s\n", bt_ntoa(&info.raddr, NULL));
231 printf("\tchannel: %d\n", info.channel);
232 printf("\tvgs.dev: %d, vgm.dev: %d\n", vgs.dev, vgm.dev);
233 if (channel) printf("\tserver channel: %d\n", channel);
236 event_dispatch();
238 err(EXIT_FAILURE, "event_dispatch");
241 void
242 usage(void)
245 fprintf(stderr,
246 "usage: %s [-hv] [-c command] [-m mixer] [-p file] [-s channel]\n"
247 "Where:\n"
248 "\t-h display this message\n"
249 "\t-v verbose output\n"
250 "\t-c command command to execute on answer\n"
251 "\t-m mixer mixer path\n"
252 "\t-p file write PID to file\n"
253 "\t-s channel register as audio gateway on channel\n"
254 "", getprogname());
256 exit(EXIT_FAILURE);
259 void
260 do_signal(int s, short ev, void *arg)
263 switch (s) {
264 case SIGUSR1:
265 ringing = 1; /* start ringing */
266 do_ring(0, 0, NULL);
267 break;
269 case SIGUSR2:
270 ringing = 0;
271 break;
273 case SIGINT:
274 default:
275 exit(EXIT_SUCCESS);
279 void
280 do_ring(int s, short ev, void *arg)
282 static struct timeval tv = { RING_INTERVAL, 0 };
284 if (!ringing)
285 return;
287 send_rfcomm("RING");
288 evtimer_add(&ring_ev, &tv);
292 * The mixer device has been twiddled. We check mic and speaker
293 * settings and send the appropriate commands to the headset,
295 void
296 do_mixer(int s, short ev, void *arg)
298 mixer_ctrl_t mc;
299 int level;
301 memcpy(&mc, &vgs, sizeof(mc));
302 if (ioctl(mx, AUDIO_MIXER_READ, &mc) < 0)
303 return;
305 if (memcmp(&vgs, &mc, sizeof(mc))) {
306 memcpy(&vgs, &mc, sizeof(mc));
307 level = mc.un.value.level[AUDIO_MIXER_LEVEL_MONO] / BTSCO_DELTA;
309 send_rfcomm("+VGS=%d", level);
312 memcpy(&mc, &vgm, sizeof(mc));
313 if (ioctl(mx, AUDIO_MIXER_READ, &mc) < 0)
314 return;
316 if (memcmp(&vgm, &mc, sizeof(mc))) {
317 memcpy(&vgm, &mc, sizeof(mc));
318 level = mc.un.value.level[AUDIO_MIXER_LEVEL_MONO] / BTSCO_DELTA;
320 send_rfcomm("+VGM=%d", level);
325 * RFCOMM socket event.
327 void
328 do_rfcomm(int fd, short ev, void *arg)
330 char buf[128];
331 int len, level;
333 memset(buf, 0, sizeof(buf));
334 len = recv(rf, buf, sizeof(buf), 0);
335 if (len <= 0) {
336 if (ag < 0)
337 errx(EXIT_FAILURE, "Connection Lost");
339 event_del(&rfcomm_ev);
340 close(rf);
341 rf = -1;
342 ringing = 0;
343 return;
346 if (verbose)
347 printf("> %.*s\n", len, buf);
349 if (len >= 7 && strncmp(buf, "AT+CKPD", 7) == 0) {
350 if (ringing && command != NULL) {
351 if (verbose)
352 printf("%% %s\n", command);
354 system(command);
357 ringing = 0;
358 send_rfcomm("OK");
359 return;
362 if (len >= 7 && strncmp(buf, "AT+VGS=", 7) == 0) {
363 level = atoi(buf + 7);
364 if (level < 0 || level > 15)
365 return;
367 vgs.un.value.level[AUDIO_MIXER_LEVEL_MONO] = level * BTSCO_DELTA;
368 if (ioctl(mx, AUDIO_MIXER_WRITE, &vgs) < 0)
369 return;
371 send_rfcomm("OK");
372 return;
375 if (len >= 7 && strncmp(buf, "AT+VGM=", 7) == 0) {
376 level = atoi(buf + 7);
377 if (level < 0 || level > 15)
378 return;
380 vgm.un.value.level[AUDIO_MIXER_LEVEL_MONO] = level * BTSCO_DELTA;
381 if (ioctl(mx, AUDIO_MIXER_WRITE, &vgm) < 0)
382 return;
384 send_rfcomm("OK");
385 return;
388 send_rfcomm("ERROR");
392 * got an incoming connection on the AG socket.
394 void
395 do_server(int fd, short ev, void *arg)
397 bdaddr_t *raddr = arg;
398 struct sockaddr_bt addr;
399 socklen_t len;
400 int s;
402 assert(raddr != NULL);
404 len = sizeof(addr);
405 s = accept(fd, (struct sockaddr *)&addr, &len);
406 if (s < 0)
407 return;
409 if (rf >= 0
410 || len != sizeof(addr)
411 || addr.bt_len != sizeof(addr)
412 || addr.bt_family != AF_BLUETOOTH
413 || !bdaddr_same(raddr, &addr.bt_bdaddr)) {
414 close(s);
415 return;
418 rf = s;
419 event_set(&rfcomm_ev, rf, EV_READ | EV_PERSIST, do_rfcomm, NULL);
420 if (event_add(&rfcomm_ev, NULL) < 0)
421 err(EXIT_FAILURE, "rfcomm_ev");
425 * send a message to the RFCOMM socket
428 send_rfcomm(const char *msg, ...)
430 char buf[128], fmt[128];
431 va_list ap;
432 int len;
434 va_start(ap, msg);
436 if (verbose) {
437 snprintf(fmt, sizeof(fmt), "< %s\n", msg);
438 vprintf(fmt, ap);
441 snprintf(fmt, sizeof(fmt), "\r\n%s\r\n", msg);
442 vsnprintf(buf, sizeof(buf), fmt, ap);
443 len = send(rf, buf, strlen(buf), 0);
445 va_end(ap);
446 return len;
450 * Initialise mixer event
453 init_mixer(struct btsco_info *info, const char *mixer)
456 mx = open(mixer, O_WRONLY, 0);
457 if (mx < 0)
458 return -1;
460 if (ioctl(mx, BTSCO_GETINFO, info) < 0)
461 return -1;
463 /* get initial vol settings */
464 memset(&vgs, 0, sizeof(vgs));
465 vgs.dev = info->vgs;
466 if (ioctl(mx, AUDIO_MIXER_READ, &vgs) < 0)
467 return -1;
469 memset(&vgm, 0, sizeof(vgm));
470 vgm.dev = info->vgm;
471 if (ioctl(mx, AUDIO_MIXER_READ, &vgm) < 0)
472 return -1;
474 /* set up mixer changed event */
475 if (fcntl(mx, F_SETFL, O_ASYNC) < 0)
476 return -1;
478 signal_set(&mixer_ev, SIGIO, do_mixer, NULL);
479 if (signal_add(&mixer_ev, NULL) < 0)
480 return -1;
482 return 0;
486 * Initialise RFCOMM socket
489 init_rfcomm(struct btsco_info *info)
491 struct sockaddr_bt addr;
493 rf = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
494 if (rf < 0)
495 return -1;
497 memset(&addr, 0, sizeof(addr));
498 addr.bt_len = sizeof(addr);
499 addr.bt_family = AF_BLUETOOTH;
500 bdaddr_copy(&addr.bt_bdaddr, &info->laddr);
502 if (bind(rf, (struct sockaddr *)&addr, sizeof(addr)) < 0)
503 return -1;
505 bdaddr_copy(&addr.bt_bdaddr, &info->raddr);
506 addr.bt_channel = info->channel;
508 if (connect(rf, (struct sockaddr *)&addr, sizeof(addr)) < 0)
509 return -1;
511 event_set(&rfcomm_ev, rf, EV_READ | EV_PERSIST, do_rfcomm, NULL);
512 if (event_add(&rfcomm_ev, NULL) < 0)
513 return -1;
515 return 0;
519 * Initialise server socket
522 init_server(struct btsco_info *info, int channel)
524 struct sockaddr_bt addr;
526 ag = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
527 if (ag < 0)
528 return -1;
530 memset(&addr, 0, sizeof(addr));
531 addr.bt_len = sizeof(addr);
532 addr.bt_family = AF_BLUETOOTH;
533 addr.bt_channel = channel;
534 bdaddr_copy(&addr.bt_bdaddr, &info->laddr);
536 if (bind(ag, (struct sockaddr *)&addr, sizeof(addr)) < 0)
537 return -1;
539 if (listen(ag, 1) < 0)
540 return -1;
542 event_set(&server_ev, ag, EV_READ | EV_PERSIST, do_server, &info->raddr);
543 if (event_add(&server_ev, NULL) < 0)
544 return -1;
546 sdp_set_uint(&hset_channel, channel);
548 ss = sdp_open_local(NULL);
549 if (ss == NULL)
550 return -1;
552 if (!sdp_record_insert(ss, &info->laddr, NULL, &hset_record)) {
553 sdp_close(ss);
554 return -1;
557 return 0;
560 void
561 remove_pid(void)
564 if (pidfile == NULL)
565 return;
567 unlink(pidfile);
571 write_pid(void)
573 char *buf;
574 int fd, len;
576 if (pidfile == NULL)
577 return 0;
579 fd = open(pidfile, O_WRONLY | O_CREAT | O_TRUNC, 0644);
580 if (fd < 0)
581 return -1;
583 len = asprintf(&buf, "%d\n", getpid());
584 if (len > 0)
585 write(fd, buf, len);
587 if (len >= 0 && buf != NULL)
588 free(buf);
590 close (fd);
592 return atexit(remove_pid);