3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2002-2010 Marcel Holtmann <marcel@holtmann.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
37 #include <sys/ioctl.h>
38 #include <sys/socket.h>
40 #include <bluetooth/bluetooth.h>
41 #include <bluetooth/hci.h>
42 #include <bluetooth/hci_lib.h>
43 #include <bluetooth/sco.h>
44 #include <bluetooth/rfcomm.h>
46 static volatile int terminate = 0;
48 static void sig_term(int sig) {
52 static int rfcomm_connect(bdaddr_t *src, bdaddr_t *dst, uint8_t channel)
54 struct sockaddr_rc addr;
57 if ((s = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM)) < 0) {
61 memset(&addr, 0, sizeof(addr));
62 addr.rc_family = AF_BLUETOOTH;
63 bacpy(&addr.rc_bdaddr, src);
65 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
70 memset(&addr, 0, sizeof(addr));
71 addr.rc_family = AF_BLUETOOTH;
72 bacpy(&addr.rc_bdaddr, dst);
73 addr.rc_channel = channel;
74 if (connect(s, (struct sockaddr *)&addr, sizeof(addr)) < 0 ){
82 static int sco_connect(bdaddr_t *src, bdaddr_t *dst, uint16_t *handle, uint16_t *mtu)
84 struct sockaddr_sco addr;
85 struct sco_conninfo conn;
86 struct sco_options opts;
90 if ((s = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_SCO)) < 0) {
94 memset(&addr, 0, sizeof(addr));
95 addr.sco_family = AF_BLUETOOTH;
96 bacpy(&addr.sco_bdaddr, src);
98 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
103 memset(&addr, 0, sizeof(addr));
104 addr.sco_family = AF_BLUETOOTH;
105 bacpy(&addr.sco_bdaddr, dst);
107 if (connect(s, (struct sockaddr *)&addr, sizeof(addr)) < 0 ){
112 memset(&conn, 0, sizeof(conn));
115 if (getsockopt(s, SOL_SCO, SCO_CONNINFO, &conn, &size) < 0) {
120 memset(&opts, 0, sizeof(opts));
123 if (getsockopt(s, SOL_SCO, SCO_OPTIONS, &opts, &size) < 0) {
129 *handle = conn.hci_handle;
137 static void usage(void)
140 "\thstest play <file> <bdaddr> [channel]\n"
141 "\thstest record <file> <bdaddr> [channel]\n");
147 int main(int argc, char *argv[])
152 struct timeval timeout;
153 unsigned char buf[2048], *p;
154 int maxfd, sel, rlen, wlen;
164 uint16_t sco_handle, sco_mtu, vs;
168 str2ba(argv[3], &bdaddr);
172 str2ba(argv[3], &bdaddr);
173 channel = atoi(argv[4]);
180 if (strncmp(argv[1], "play", 4) == 0) {
183 } else if (strncmp(argv[1], "rec", 3) == 0) {
185 filemode = O_WRONLY | O_CREAT | O_TRUNC;
193 hci_devba(0, &local);
194 dd = hci_open_dev(0);
195 hci_read_voice_setting(dd, &vs, 1000);
197 fprintf(stderr, "Voice setting: 0x%04x\n", vs);
200 fprintf(stderr, "The voice setting must be 0x0060\n");
204 if (strcmp(filename, "-") == 0) {
216 if ((fd = open(filename, filemode)) < 0) {
217 perror("Can't open input/output file");
222 memset(&sa, 0, sizeof(sa));
223 sa.sa_flags = SA_NOCLDSTOP;
224 sa.sa_handler = sig_term;
225 sigaction(SIGTERM, &sa, NULL);
226 sigaction(SIGINT, &sa, NULL);
228 sa.sa_handler = SIG_IGN;
229 sigaction(SIGCHLD, &sa, NULL);
230 sigaction(SIGPIPE, &sa, NULL);
232 if ((rd = rfcomm_connect(&local, &bdaddr, channel)) < 0) {
233 perror("Can't connect RFCOMM channel");
237 fprintf(stderr, "RFCOMM channel connected\n");
239 if ((sd = sco_connect(&local, &bdaddr, &sco_handle, &sco_mtu)) < 0) {
240 perror("Can't connect SCO audio channel");
245 fprintf(stderr, "SCO audio channel connected (handle %d, mtu %d)\n", sco_handle, sco_mtu);
247 if (mode == RECORD) {
248 if (write(rd, "RING\r\n", 6) < 0)
252 maxfd = (rd > sd) ? rd : sd;
261 timeout.tv_usec = 10000;
263 if ((sel = select(maxfd + 1, &rfds, NULL, NULL, &timeout)) > 0) {
265 if (FD_ISSET(rd, &rfds)) {
266 memset(buf, 0, sizeof(buf));
267 rlen = read(rd, buf, sizeof(buf));
269 fprintf(stderr, "%s\n", buf);
270 wlen = write(rd, "OK\r\n", 4);
274 if (FD_ISSET(sd, &rfds)) {
275 memset(buf, 0, sizeof(buf));
276 rlen = read(sd, buf, sizeof(buf));
280 rlen = read(fd, buf, rlen);
284 while (rlen > sco_mtu) {
285 wlen += write(sd, p, sco_mtu);
289 wlen += write(sd, p, rlen);
292 wlen = write(fd, buf, rlen);