Home | History | Annotate | Line # | Download | only in bthset
bthset.c revision 1.3
      1  1.3    ragge /*	$NetBSD: bthset.c,v 1.3 2007/09/16 18:44:19 ragge 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.1  gdamore __COPYRIGHT("@(#) Copyright (c) 2006 Itronix, Inc\n"
     36  1.1  gdamore 	    "All rights reserved.\n");
     37  1.3    ragge __RCSID("$NetBSD: bthset.c,v 1.3 2007/09/16 18:44:19 ragge Exp $");
     38  1.1  gdamore 
     39  1.1  gdamore #include <sys/types.h>
     40  1.1  gdamore #include <sys/audioio.h>
     41  1.1  gdamore #include <sys/ioctl.h>
     42  1.1  gdamore #include <sys/time.h>
     43  1.1  gdamore #include <assert.h>
     44  1.1  gdamore #include <bluetooth.h>
     45  1.1  gdamore #include <err.h>
     46  1.1  gdamore #include <event.h>
     47  1.1  gdamore #include <fcntl.h>
     48  1.1  gdamore #include <signal.h>
     49  1.1  gdamore #include <sdp.h>
     50  1.1  gdamore #include <stdarg.h>
     51  1.1  gdamore #include <stdio.h>
     52  1.1  gdamore #include <stdlib.h>
     53  1.1  gdamore #include <string.h>
     54  1.1  gdamore #include <unistd.h>
     55  1.3    ragge #include <errno.h>
     56  1.1  gdamore 
     57  1.1  gdamore #include <dev/bluetooth/btdev.h>
     58  1.2     tron #include <dev/bluetooth/btsco.h>
     59  1.1  gdamore 
     60  1.1  gdamore #include <netbt/rfcomm.h>
     61  1.1  gdamore 
     62  1.1  gdamore #define RING_INTERVAL	5	/* seconds */
     63  1.1  gdamore 
     64  1.1  gdamore int  main(int, char *[]);
     65  1.1  gdamore void usage(void);
     66  1.1  gdamore 
     67  1.1  gdamore void do_signal(int, short, void *);
     68  1.1  gdamore void do_ring(int, short, void *);
     69  1.1  gdamore void do_mixer(int, short, void *);
     70  1.1  gdamore void do_rfcomm(int, short, void *);
     71  1.1  gdamore void do_server(int, short, void *);
     72  1.1  gdamore int send_rfcomm(const char *, ...);
     73  1.1  gdamore 
     74  1.2     tron int init_mixer(struct btsco_info *, const char *);
     75  1.2     tron int init_rfcomm(struct btsco_info *);
     76  1.2     tron int init_server(struct btsco_info *, int);
     77  1.1  gdamore 
     78  1.1  gdamore void remove_pid(void);
     79  1.1  gdamore int write_pid(void);
     80  1.1  gdamore 
     81  1.1  gdamore struct event sigint_ev;		/* bye bye */
     82  1.1  gdamore struct event sigusr1_ev;	/* start ringing */
     83  1.1  gdamore struct event sigusr2_ev;	/* stop ringing */
     84  1.1  gdamore struct event mixer_ev;		/* mixer changed */
     85  1.1  gdamore struct event rfcomm_ev;		/* headset speaks */
     86  1.1  gdamore struct event server_ev;		/* headset connecting */
     87  1.1  gdamore struct event ring_ev;		/* ring timer */
     88  1.1  gdamore 
     89  1.1  gdamore mixer_ctrl_t vgs;	/* speaker control */
     90  1.1  gdamore mixer_ctrl_t vgm;	/* mic control */
     91  1.1  gdamore int ringing;		/* we are ringing */
     92  1.1  gdamore int verbose;		/* copy to stdout */
     93  1.1  gdamore int mx;			/* mixer fd */
     94  1.1  gdamore int rf;			/* rfcomm connection fd */
     95  1.1  gdamore int ag;			/* rfcomm gateway fd */
     96  1.1  gdamore void *ss;		/* sdp handle */
     97  1.1  gdamore 
     98  1.1  gdamore char *command;		/* answer command */
     99  1.1  gdamore char *pidfile;		/* PID file name */
    100  1.1  gdamore 
    101  1.1  gdamore int
    102  1.1  gdamore main(int ac, char *av[])
    103  1.1  gdamore {
    104  1.2     tron 	struct btsco_info	info;
    105  1.1  gdamore 	const char		*mixer;
    106  1.1  gdamore 	int			ch, channel;
    107  1.1  gdamore 
    108  1.1  gdamore 	ag = rf = -1;
    109  1.1  gdamore 	verbose = 0;
    110  1.1  gdamore 	channel = 0;
    111  1.1  gdamore 	pidfile = getenv("BTHSET_PIDFILE");
    112  1.1  gdamore 	command = getenv("BTHSET_COMMAND");
    113  1.1  gdamore 	mixer = getenv("BTHSET_MIXER");
    114  1.1  gdamore 	if (mixer == NULL)
    115  1.1  gdamore 		mixer = "/dev/mixer";
    116  1.1  gdamore 
    117  1.1  gdamore 	while ((ch = getopt(ac, av, "hc:m:p:s:v")) != EOF) {
    118  1.1  gdamore 		switch (ch) {
    119  1.1  gdamore 		case 'c':
    120  1.1  gdamore 			command = optarg;
    121  1.1  gdamore 			break;
    122  1.1  gdamore 
    123  1.1  gdamore 		case 'm':
    124  1.1  gdamore 			mixer = optarg;
    125  1.1  gdamore 			break;
    126  1.1  gdamore 
    127  1.1  gdamore 		case 'p':
    128  1.1  gdamore 			pidfile = optarg;
    129  1.1  gdamore 			break;
    130  1.1  gdamore 
    131  1.1  gdamore 		case 's':
    132  1.1  gdamore 			channel = atoi(optarg);
    133  1.1  gdamore 			break;
    134  1.1  gdamore 
    135  1.1  gdamore 		case 'v':
    136  1.1  gdamore 			verbose = 1;
    137  1.1  gdamore 			break;
    138  1.1  gdamore 
    139  1.1  gdamore 		case 'h':
    140  1.1  gdamore 		default:
    141  1.1  gdamore 			usage();
    142  1.1  gdamore 		}
    143  1.1  gdamore 	}
    144  1.1  gdamore 
    145  1.1  gdamore 	if (mixer == NULL)
    146  1.1  gdamore 		usage();
    147  1.1  gdamore 
    148  1.1  gdamore 	if ((channel < RFCOMM_CHANNEL_MIN || channel > RFCOMM_CHANNEL_MAX)
    149  1.1  gdamore 	    && channel != 0)
    150  1.1  gdamore 		usage();
    151  1.1  gdamore 
    152  1.1  gdamore 	if (write_pid() < 0)
    153  1.1  gdamore 		err(EXIT_FAILURE, "%s", pidfile);
    154  1.1  gdamore 
    155  1.1  gdamore 	event_init();
    156  1.1  gdamore 
    157  1.1  gdamore 	ringing = 0;
    158  1.1  gdamore 	evtimer_set(&ring_ev, do_ring, NULL);
    159  1.1  gdamore 
    160  1.1  gdamore 	signal_set(&sigusr1_ev, SIGUSR1, do_signal, NULL);
    161  1.1  gdamore 	if (signal_add(&sigusr1_ev, NULL) < 0)
    162  1.1  gdamore 		err(EXIT_FAILURE, "SIGUSR1");
    163  1.1  gdamore 
    164  1.1  gdamore 	signal_set(&sigusr2_ev, SIGUSR2, do_signal, NULL);
    165  1.1  gdamore 	if (signal_add(&sigusr2_ev, NULL) < 0)
    166  1.1  gdamore 		err(EXIT_FAILURE, "SIGUSR2");
    167  1.1  gdamore 
    168  1.1  gdamore 	signal_set(&sigint_ev, SIGINT, do_signal, NULL);
    169  1.1  gdamore 	if (signal_add(&sigint_ev, NULL) < 0)
    170  1.1  gdamore 		err(EXIT_FAILURE, "SIGINT");
    171  1.1  gdamore 
    172  1.1  gdamore 	if (init_mixer(&info, mixer) < 0)
    173  1.1  gdamore 		err(EXIT_FAILURE, "%s", mixer);
    174  1.1  gdamore 
    175  1.1  gdamore 	if (channel == 0 && init_rfcomm(&info) < 0)
    176  1.1  gdamore 		err(EXIT_FAILURE, "%s", bt_ntoa(&info.raddr, NULL));
    177  1.1  gdamore 
    178  1.1  gdamore 	if (channel && init_server(&info, channel) < 0)
    179  1.1  gdamore 		err(EXIT_FAILURE, "%d", channel);
    180  1.1  gdamore 
    181  1.1  gdamore 	if (verbose) {
    182  1.1  gdamore 		printf("Headset Info:\n");
    183  1.1  gdamore 		printf("\tmixer: %s\n", mixer);
    184  1.1  gdamore 		printf("\tladdr: %s\n", bt_ntoa(&info.laddr, NULL));
    185  1.1  gdamore 		printf("\traddr: %s\n", bt_ntoa(&info.raddr, NULL));
    186  1.1  gdamore 		printf("\tchannel: %d\n", info.channel);
    187  1.1  gdamore 		printf("\tvgs.dev: %d, vgm.dev: %d\n", vgs.dev, vgm.dev);
    188  1.1  gdamore 		if (channel) printf("\tserver channel: %d\n", channel);
    189  1.1  gdamore 	}
    190  1.1  gdamore 
    191  1.1  gdamore 	event_dispatch();
    192  1.1  gdamore 
    193  1.1  gdamore 	err(EXIT_FAILURE, "event_dispatch");
    194  1.1  gdamore }
    195  1.1  gdamore 
    196  1.1  gdamore void
    197  1.1  gdamore usage(void)
    198  1.1  gdamore {
    199  1.1  gdamore 
    200  1.1  gdamore 	fprintf(stderr,
    201  1.1  gdamore 		"usage: %s [-hv] [-c command] [-m mixer] [-p file] [-s channel]\n"
    202  1.1  gdamore 		"Where:\n"
    203  1.1  gdamore 		"\t-h           display this message\n"
    204  1.1  gdamore 		"\t-v           verbose output\n"
    205  1.1  gdamore 		"\t-c command   command to execute on answer\n"
    206  1.1  gdamore 		"\t-m mixer     mixer path\n"
    207  1.1  gdamore 		"\t-p file      write PID to file\n"
    208  1.1  gdamore 		"\t-s channel   register as audio gateway on channel\n"
    209  1.1  gdamore 		"", getprogname());
    210  1.1  gdamore 
    211  1.1  gdamore 	exit(EXIT_FAILURE);
    212  1.1  gdamore }
    213  1.1  gdamore 
    214  1.1  gdamore void
    215  1.1  gdamore do_signal(int s, short ev, void *arg)
    216  1.1  gdamore {
    217  1.1  gdamore 
    218  1.1  gdamore 	switch (s) {
    219  1.1  gdamore 	case SIGUSR1:
    220  1.1  gdamore 		ringing = 1;	/* start ringing */
    221  1.1  gdamore 		do_ring(0, 0, NULL);
    222  1.1  gdamore 		break;
    223  1.1  gdamore 
    224  1.1  gdamore 	case SIGUSR2:
    225  1.1  gdamore 		ringing = 0;
    226  1.1  gdamore 		break;
    227  1.1  gdamore 
    228  1.1  gdamore 	case SIGINT:
    229  1.1  gdamore 	default:
    230  1.1  gdamore 		exit(EXIT_SUCCESS);
    231  1.1  gdamore 	}
    232  1.1  gdamore }
    233  1.1  gdamore 
    234  1.1  gdamore void
    235  1.1  gdamore do_ring(int s, short ev, void *arg)
    236  1.1  gdamore {
    237  1.1  gdamore 	static struct timeval tv = { RING_INTERVAL, 0 };
    238  1.1  gdamore 
    239  1.1  gdamore 	if (!ringing)
    240  1.1  gdamore 		return;
    241  1.1  gdamore 
    242  1.1  gdamore 	send_rfcomm("RING");
    243  1.1  gdamore 	evtimer_add(&ring_ev, &tv);
    244  1.1  gdamore }
    245  1.1  gdamore 
    246  1.1  gdamore /*
    247  1.1  gdamore  * The mixer device has been twiddled. We check mic and speaker
    248  1.1  gdamore  * settings and send the appropriate commands to the headset,
    249  1.1  gdamore  */
    250  1.1  gdamore void
    251  1.1  gdamore do_mixer(int s, short ev, void *arg)
    252  1.1  gdamore {
    253  1.1  gdamore 	mixer_ctrl_t mc;
    254  1.1  gdamore 	int level;
    255  1.1  gdamore 
    256  1.1  gdamore 	memcpy(&mc, &vgs, sizeof(mc));
    257  1.1  gdamore 	if (ioctl(mx, AUDIO_MIXER_READ, &mc) < 0)
    258  1.1  gdamore 		return;
    259  1.1  gdamore 
    260  1.1  gdamore 	if (memcmp(&vgs, &mc, sizeof(mc))) {
    261  1.1  gdamore 		memcpy(&vgs, &mc, sizeof(mc));
    262  1.2     tron 		level = mc.un.value.level[AUDIO_MIXER_LEVEL_MONO] / BTSCO_DELTA;
    263  1.1  gdamore 
    264  1.1  gdamore 		send_rfcomm("+VGS=%d", level);
    265  1.1  gdamore 	}
    266  1.1  gdamore 
    267  1.1  gdamore 	memcpy(&mc, &vgm, sizeof(mc));
    268  1.1  gdamore 	if (ioctl(mx, AUDIO_MIXER_READ, &mc) < 0)
    269  1.1  gdamore 		return;
    270  1.1  gdamore 
    271  1.1  gdamore 	if (memcmp(&vgm, &mc, sizeof(mc))) {
    272  1.1  gdamore 		memcpy(&vgm, &mc, sizeof(mc));
    273  1.2     tron 		level = mc.un.value.level[AUDIO_MIXER_LEVEL_MONO] / BTSCO_DELTA;
    274  1.1  gdamore 
    275  1.1  gdamore 		send_rfcomm("+VGM=%d", level);
    276  1.1  gdamore 	}
    277  1.1  gdamore }
    278  1.1  gdamore 
    279  1.1  gdamore /*
    280  1.1  gdamore  * RFCOMM socket event.
    281  1.1  gdamore  */
    282  1.1  gdamore void
    283  1.1  gdamore do_rfcomm(int fd, short ev, void *arg)
    284  1.1  gdamore {
    285  1.1  gdamore 	char buf[128];
    286  1.1  gdamore 	int len, level;
    287  1.1  gdamore 
    288  1.1  gdamore 	memset(buf, 0, sizeof(buf));
    289  1.1  gdamore 	len = recv(rf, buf, sizeof(buf), 0);
    290  1.1  gdamore 	if (len <= 0) {
    291  1.1  gdamore 		if (ag < 0)
    292  1.1  gdamore 			errx(EXIT_FAILURE, "Connection Lost");
    293  1.1  gdamore 
    294  1.1  gdamore 		event_del(&rfcomm_ev);
    295  1.1  gdamore 		close(rf);
    296  1.1  gdamore 		rf = -1;
    297  1.1  gdamore 		ringing = 0;
    298  1.1  gdamore 		return;
    299  1.1  gdamore 	}
    300  1.1  gdamore 
    301  1.1  gdamore 	if (verbose)
    302  1.1  gdamore 		printf("> %.*s\n", len, buf);
    303  1.1  gdamore 
    304  1.1  gdamore 	if (len >= 7 && strncmp(buf, "AT+CKPD", 7) == 0) {
    305  1.1  gdamore 		if (ringing && command != NULL) {
    306  1.1  gdamore 			if (verbose)
    307  1.1  gdamore 				printf("%% %s\n", command);
    308  1.1  gdamore 
    309  1.1  gdamore 			system(command);
    310  1.1  gdamore 		}
    311  1.1  gdamore 
    312  1.1  gdamore 		ringing = 0;
    313  1.1  gdamore 		send_rfcomm("OK");
    314  1.1  gdamore 		return;
    315  1.1  gdamore 	}
    316  1.1  gdamore 
    317  1.1  gdamore 	if (len >= 7 && strncmp(buf, "AT+VGS=", 7) == 0) {
    318  1.1  gdamore 		level = atoi(buf + 7);
    319  1.1  gdamore 		if (level < 0 || level > 15)
    320  1.1  gdamore 			return;
    321  1.1  gdamore 
    322  1.2     tron 		vgs.un.value.level[AUDIO_MIXER_LEVEL_MONO] = level * BTSCO_DELTA;
    323  1.1  gdamore 		if (ioctl(mx, AUDIO_MIXER_WRITE, &vgs) < 0)
    324  1.1  gdamore 			return;
    325  1.1  gdamore 
    326  1.1  gdamore 		send_rfcomm("OK");
    327  1.1  gdamore 		return;
    328  1.1  gdamore 	}
    329  1.1  gdamore 
    330  1.1  gdamore 	if (len >= 7 && strncmp(buf, "AT+VGM=", 7) == 0) {
    331  1.1  gdamore 		level = atoi(buf + 7);
    332  1.1  gdamore 		if (level < 0 || level > 15)
    333  1.1  gdamore 			return;
    334  1.1  gdamore 
    335  1.2     tron 		vgm.un.value.level[AUDIO_MIXER_LEVEL_MONO] = level * BTSCO_DELTA;
    336  1.1  gdamore 		if (ioctl(mx, AUDIO_MIXER_WRITE, &vgm) < 0)
    337  1.1  gdamore 			return;
    338  1.1  gdamore 
    339  1.1  gdamore 		send_rfcomm("OK");
    340  1.1  gdamore 		return;
    341  1.1  gdamore 	}
    342  1.1  gdamore 
    343  1.1  gdamore 	send_rfcomm("ERROR");
    344  1.1  gdamore }
    345  1.1  gdamore 
    346  1.1  gdamore /*
    347  1.1  gdamore  * got an incoming connection on the AG socket.
    348  1.1  gdamore  */
    349  1.1  gdamore void
    350  1.1  gdamore do_server(int fd, short ev, void *arg)
    351  1.1  gdamore {
    352  1.1  gdamore 	bdaddr_t *raddr = arg;
    353  1.1  gdamore 	struct sockaddr_bt addr;
    354  1.1  gdamore 	socklen_t len;
    355  1.1  gdamore 	int s;
    356  1.1  gdamore 
    357  1.1  gdamore 	assert(raddr != NULL);
    358  1.1  gdamore 
    359  1.1  gdamore 	len = sizeof(addr);
    360  1.1  gdamore 	s = accept(fd, (struct sockaddr *)&addr, &len);
    361  1.1  gdamore 	if (s < 0)
    362  1.1  gdamore 		return;
    363  1.1  gdamore 
    364  1.1  gdamore 	if (rf >= 0
    365  1.1  gdamore 	    || len != sizeof(addr)
    366  1.1  gdamore 	    || addr.bt_len != sizeof(addr)
    367  1.1  gdamore 	    || addr.bt_family != AF_BLUETOOTH
    368  1.1  gdamore 	    || !bdaddr_same(raddr, &addr.bt_bdaddr)) {
    369  1.1  gdamore 		close(s);
    370  1.1  gdamore 		return;
    371  1.1  gdamore 	}
    372  1.1  gdamore 
    373  1.1  gdamore 	rf = s;
    374  1.1  gdamore 	event_set(&rfcomm_ev, rf, EV_READ | EV_PERSIST, do_rfcomm, NULL);
    375  1.1  gdamore 	if (event_add(&rfcomm_ev, NULL) < 0)
    376  1.1  gdamore 		err(EXIT_FAILURE, "rfcomm_ev");
    377  1.1  gdamore }
    378  1.1  gdamore 
    379  1.1  gdamore /*
    380  1.1  gdamore  * send a message to the RFCOMM socket
    381  1.1  gdamore  */
    382  1.1  gdamore int
    383  1.1  gdamore send_rfcomm(const char *msg, ...)
    384  1.1  gdamore {
    385  1.1  gdamore 	char buf[128], fmt[128];
    386  1.1  gdamore 	va_list ap;
    387  1.1  gdamore 	int len;
    388  1.1  gdamore 
    389  1.1  gdamore 	va_start(ap, msg);
    390  1.1  gdamore 
    391  1.1  gdamore 	if (verbose) {
    392  1.1  gdamore 		snprintf(fmt, sizeof(fmt), "< %s\n", msg);
    393  1.1  gdamore 		vprintf(fmt, ap);
    394  1.1  gdamore 	}
    395  1.1  gdamore 
    396  1.1  gdamore 	snprintf(fmt, sizeof(fmt), "\r\n%s\r\n", msg);
    397  1.1  gdamore 	vsnprintf(buf, sizeof(buf), fmt, ap);
    398  1.1  gdamore 	len = send(rf, buf, strlen(buf), 0);
    399  1.1  gdamore 
    400  1.1  gdamore 	va_end(ap);
    401  1.1  gdamore 	return len;
    402  1.1  gdamore }
    403  1.1  gdamore 
    404  1.1  gdamore /*
    405  1.1  gdamore  * Initialise mixer event
    406  1.1  gdamore  */
    407  1.1  gdamore int
    408  1.2     tron init_mixer(struct btsco_info *info, const char *mixer)
    409  1.1  gdamore {
    410  1.1  gdamore 
    411  1.1  gdamore 	mx = open(mixer, O_WRONLY, 0);
    412  1.1  gdamore 	if (mx < 0)
    413  1.1  gdamore 		return -1;
    414  1.1  gdamore 
    415  1.2     tron 	if (ioctl(mx, BTSCO_GETINFO, info) < 0)
    416  1.1  gdamore 		return -1;
    417  1.1  gdamore 
    418  1.1  gdamore 	/* get initial vol settings */
    419  1.1  gdamore 	memset(&vgs, 0, sizeof(vgs));
    420  1.1  gdamore 	vgs.dev = info->vgs;
    421  1.1  gdamore 	if (ioctl(mx, AUDIO_MIXER_READ, &vgs) < 0)
    422  1.1  gdamore 		return -1;
    423  1.1  gdamore 
    424  1.1  gdamore 	memset(&vgm, 0, sizeof(vgm));
    425  1.1  gdamore 	vgm.dev = info->vgm;
    426  1.1  gdamore 	if (ioctl(mx, AUDIO_MIXER_READ, &vgm) < 0)
    427  1.1  gdamore 		return -1;
    428  1.1  gdamore 
    429  1.1  gdamore 	/* set up mixer changed event */
    430  1.1  gdamore 	if (fcntl(mx, F_SETFL, O_ASYNC) < 0)
    431  1.1  gdamore 		return -1;
    432  1.1  gdamore 
    433  1.1  gdamore 	signal_set(&mixer_ev, SIGIO, do_mixer, NULL);
    434  1.1  gdamore 	if (signal_add(&mixer_ev, NULL) < 0)
    435  1.1  gdamore 		return -1;
    436  1.1  gdamore 
    437  1.1  gdamore 	return 0;
    438  1.1  gdamore }
    439  1.1  gdamore 
    440  1.1  gdamore /*
    441  1.1  gdamore  * Initialise RFCOMM socket
    442  1.1  gdamore  */
    443  1.1  gdamore int
    444  1.2     tron init_rfcomm(struct btsco_info *info)
    445  1.1  gdamore {
    446  1.1  gdamore 	struct sockaddr_bt addr;
    447  1.1  gdamore 
    448  1.1  gdamore 	rf = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
    449  1.1  gdamore 	if (rf < 0)
    450  1.1  gdamore 		return -1;
    451  1.1  gdamore 
    452  1.1  gdamore 	memset(&addr, 0, sizeof(addr));
    453  1.1  gdamore 	addr.bt_len = sizeof(addr);
    454  1.1  gdamore 	addr.bt_family = AF_BLUETOOTH;
    455  1.1  gdamore 	bdaddr_copy(&addr.bt_bdaddr, &info->laddr);
    456  1.1  gdamore 
    457  1.1  gdamore 	if (bind(rf, (struct sockaddr *)&addr, sizeof(addr)) < 0)
    458  1.1  gdamore 		return -1;
    459  1.1  gdamore 
    460  1.1  gdamore 	bdaddr_copy(&addr.bt_bdaddr, &info->raddr);
    461  1.1  gdamore 	addr.bt_channel = info->channel;
    462  1.1  gdamore 
    463  1.1  gdamore 	if (connect(rf, (struct sockaddr *)&addr, sizeof(addr)) < 0)
    464  1.1  gdamore 		return -1;
    465  1.1  gdamore 
    466  1.1  gdamore 	event_set(&rfcomm_ev, rf, EV_READ | EV_PERSIST, do_rfcomm, NULL);
    467  1.1  gdamore 	if (event_add(&rfcomm_ev, NULL) < 0)
    468  1.1  gdamore 		return -1;
    469  1.1  gdamore 
    470  1.1  gdamore 	return 0;
    471  1.1  gdamore }
    472  1.1  gdamore 
    473  1.1  gdamore /*
    474  1.1  gdamore  * Initialise server socket
    475  1.1  gdamore  */
    476  1.1  gdamore int
    477  1.2     tron init_server(struct btsco_info *info, int channel)
    478  1.1  gdamore {
    479  1.1  gdamore 	sdp_hset_profile_t hset;
    480  1.1  gdamore 	struct sockaddr_bt addr;
    481  1.1  gdamore 
    482  1.1  gdamore 	ag = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
    483  1.1  gdamore 	if (ag < 0)
    484  1.1  gdamore 		return -1;
    485  1.1  gdamore 
    486  1.1  gdamore 	memset(&addr, 0, sizeof(addr));
    487  1.1  gdamore 	addr.bt_len = sizeof(addr);
    488  1.1  gdamore 	addr.bt_family = AF_BLUETOOTH;
    489  1.1  gdamore 	addr.bt_channel = channel;
    490  1.1  gdamore 	bdaddr_copy(&addr.bt_bdaddr, &info->laddr);
    491  1.1  gdamore 
    492  1.1  gdamore 	if (bind(ag, (struct sockaddr *)&addr, sizeof(addr)) < 0)
    493  1.1  gdamore 		return -1;
    494  1.1  gdamore 
    495  1.1  gdamore 	if (listen(ag, 1) < 0)
    496  1.1  gdamore 		return -1;
    497  1.1  gdamore 
    498  1.1  gdamore 	event_set(&server_ev, ag, EV_READ | EV_PERSIST, do_server, &info->raddr);
    499  1.1  gdamore 	if (event_add(&server_ev, NULL) < 0)
    500  1.1  gdamore 		return -1;
    501  1.1  gdamore 
    502  1.1  gdamore 	memset(&hset, 0, sizeof(hset));
    503  1.1  gdamore 	hset.server_channel = channel;
    504  1.1  gdamore 
    505  1.1  gdamore 	ss = sdp_open_local(NULL);
    506  1.1  gdamore 	if (ss == NULL || (errno = sdp_error(ss)))
    507  1.1  gdamore 		return -1;
    508  1.1  gdamore 
    509  1.1  gdamore 	if (sdp_register_service(ss,
    510  1.1  gdamore 			SDP_SERVICE_CLASS_HEADSET_AUDIO_GATEWAY,
    511  1.1  gdamore 			&info->laddr, (uint8_t *)&hset, sizeof(hset), NULL) != 0) {
    512  1.1  gdamore 		errno = sdp_error(ss);
    513  1.1  gdamore 		sdp_close(ss);
    514  1.1  gdamore 		return -1;
    515  1.1  gdamore 	}
    516  1.1  gdamore 
    517  1.1  gdamore 	return 0;
    518  1.1  gdamore }
    519  1.1  gdamore 
    520  1.1  gdamore void
    521  1.1  gdamore remove_pid(void)
    522  1.1  gdamore {
    523  1.1  gdamore 
    524  1.1  gdamore 	if (pidfile == NULL)
    525  1.1  gdamore 		return;
    526  1.1  gdamore 
    527  1.1  gdamore 	unlink(pidfile);
    528  1.1  gdamore }
    529  1.1  gdamore 
    530  1.1  gdamore int
    531  1.1  gdamore write_pid(void)
    532  1.1  gdamore {
    533  1.1  gdamore 	char *buf;
    534  1.1  gdamore 	int fd, len;
    535  1.1  gdamore 
    536  1.1  gdamore 	if (pidfile == NULL)
    537  1.1  gdamore 		return 0;
    538  1.1  gdamore 
    539  1.1  gdamore 	fd = open(pidfile, O_WRONLY | O_CREAT | O_TRUNC, 0644);
    540  1.1  gdamore 	if (fd < 0)
    541  1.1  gdamore 		return -1;
    542  1.1  gdamore 
    543  1.1  gdamore 	len = asprintf(&buf, "%d\n", getpid());
    544  1.1  gdamore 	if (len > 0)
    545  1.1  gdamore 		write(fd, buf, len);
    546  1.1  gdamore 
    547  1.1  gdamore 	if (len >= 0 && buf != NULL)
    548  1.1  gdamore 		free(buf);
    549  1.1  gdamore 
    550  1.1  gdamore 	close (fd);
    551  1.1  gdamore 
    552  1.1  gdamore 	return atexit(remove_pid);
    553  1.1  gdamore }
    554