1 /* $NetBSD: bthset.c,v 1.4 2008/07/21 14:19:21 lukem Exp $ */
4 * Copyright (c) 2006 Itronix Inc.
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
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>
44 #include <bluetooth.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 *[]);
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);
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
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,
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
;
156 pidfile
= getenv("BTHSET_PIDFILE");
157 command
= getenv("BTHSET_COMMAND");
158 mixer
= getenv("BTHSET_MIXER");
160 mixer
= "/dev/mixer";
162 while ((ch
= getopt(ac
, av
, "hc:m:p:s:v")) != EOF
) {
177 channel
= atoi(optarg
);
193 if ((channel
< RFCOMM_CHANNEL_MIN
|| channel
> RFCOMM_CHANNEL_MAX
)
198 err(EXIT_FAILURE
, "%s", pidfile
);
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
);
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
);
238 err(EXIT_FAILURE
, "event_dispatch");
246 "usage: %s [-hv] [-c command] [-m mixer] [-p file] [-s channel]\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"
260 do_signal(int s
, short ev
, void *arg
)
265 ringing
= 1; /* start ringing */
280 do_ring(int s
, short ev
, void *arg
)
282 static struct timeval tv
= { RING_INTERVAL
, 0 };
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,
296 do_mixer(int s
, short ev
, void *arg
)
301 memcpy(&mc
, &vgs
, sizeof(mc
));
302 if (ioctl(mx
, AUDIO_MIXER_READ
, &mc
) < 0)
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)
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.
328 do_rfcomm(int fd
, short ev
, void *arg
)
333 memset(buf
, 0, sizeof(buf
));
334 len
= recv(rf
, buf
, sizeof(buf
), 0);
337 errx(EXIT_FAILURE
, "Connection Lost");
339 event_del(&rfcomm_ev
);
347 printf("> %.*s\n", len
, buf
);
349 if (len
>= 7 && strncmp(buf
, "AT+CKPD", 7) == 0) {
350 if (ringing
&& command
!= NULL
) {
352 printf("%% %s\n", command
);
362 if (len
>= 7 && strncmp(buf
, "AT+VGS=", 7) == 0) {
363 level
= atoi(buf
+ 7);
364 if (level
< 0 || level
> 15)
367 vgs
.un
.value
.level
[AUDIO_MIXER_LEVEL_MONO
] = level
* BTSCO_DELTA
;
368 if (ioctl(mx
, AUDIO_MIXER_WRITE
, &vgs
) < 0)
375 if (len
>= 7 && strncmp(buf
, "AT+VGM=", 7) == 0) {
376 level
= atoi(buf
+ 7);
377 if (level
< 0 || level
> 15)
380 vgm
.un
.value
.level
[AUDIO_MIXER_LEVEL_MONO
] = level
* BTSCO_DELTA
;
381 if (ioctl(mx
, AUDIO_MIXER_WRITE
, &vgm
) < 0)
388 send_rfcomm("ERROR");
392 * got an incoming connection on the AG socket.
395 do_server(int fd
, short ev
, void *arg
)
397 bdaddr_t
*raddr
= arg
;
398 struct sockaddr_bt addr
;
402 assert(raddr
!= NULL
);
405 s
= accept(fd
, (struct sockaddr
*)&addr
, &len
);
410 || len
!= sizeof(addr
)
411 || addr
.bt_len
!= sizeof(addr
)
412 || addr
.bt_family
!= AF_BLUETOOTH
413 || !bdaddr_same(raddr
, &addr
.bt_bdaddr
)) {
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];
437 snprintf(fmt
, sizeof(fmt
), "< %s\n", msg
);
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);
450 * Initialise mixer event
453 init_mixer(struct btsco_info
*info
, const char *mixer
)
456 mx
= open(mixer
, O_WRONLY
, 0);
460 if (ioctl(mx
, BTSCO_GETINFO
, info
) < 0)
463 /* get initial vol settings */
464 memset(&vgs
, 0, sizeof(vgs
));
466 if (ioctl(mx
, AUDIO_MIXER_READ
, &vgs
) < 0)
469 memset(&vgm
, 0, sizeof(vgm
));
471 if (ioctl(mx
, AUDIO_MIXER_READ
, &vgm
) < 0)
474 /* set up mixer changed event */
475 if (fcntl(mx
, F_SETFL
, O_ASYNC
) < 0)
478 signal_set(&mixer_ev
, SIGIO
, do_mixer
, NULL
);
479 if (signal_add(&mixer_ev
, NULL
) < 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
);
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)
505 bdaddr_copy(&addr
.bt_bdaddr
, &info
->raddr
);
506 addr
.bt_channel
= info
->channel
;
508 if (connect(rf
, (struct sockaddr
*)&addr
, sizeof(addr
)) < 0)
511 event_set(&rfcomm_ev
, rf
, EV_READ
| EV_PERSIST
, do_rfcomm
, NULL
);
512 if (event_add(&rfcomm_ev
, NULL
) < 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
);
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)
539 if (listen(ag
, 1) < 0)
542 event_set(&server_ev
, ag
, EV_READ
| EV_PERSIST
, do_server
, &info
->raddr
);
543 if (event_add(&server_ev
, NULL
) < 0)
546 sdp_set_uint(&hset_channel
, channel
);
548 ss
= sdp_open_local(NULL
);
552 if (!sdp_record_insert(ss
, &info
->laddr
, NULL
, &hset_record
)) {
579 fd
= open(pidfile
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0644);
583 len
= asprintf(&buf
, "%d\n", getpid());
587 if (len
>= 0 && buf
!= NULL
)
592 return atexit(remove_pid
);