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