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