• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  *  BlueZ - Bluetooth protocol stack for Linux
4  *
5  *  Copyright (C) 2006-2010  Nokia Corporation
6  *  Copyright (C) 2004-2010  Marcel Holtmann <marcel@holtmann.org>
7  *
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or
12  *  (at your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the Free Software
21  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22  *
23  */
24 
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28 
29 #include <errno.h>
30 #include <sys/socket.h>
31 #include <unistd.h>
32 #include <fcntl.h>
33 #include <bluetooth/bluetooth.h>
34 #include <bluetooth/sdp.h>
35 #include <bluetooth/sdp_lib.h>
36 
37 #include <glib.h>
38 #include <dbus/dbus.h>
39 
40 #include "glib-helper.h"
41 #include "btio.h"
42 #include "plugin.h"
43 #include "log.h"
44 #include "device.h"
45 #include "headset.h"
46 #include "manager.h"
47 #include "gateway.h"
48 
49 static GIOChannel *sco_server = NULL;
50 
load_config_file(const char * file)51 static GKeyFile *load_config_file(const char *file)
52 {
53 	GError *err = NULL;
54 	GKeyFile *keyfile;
55 
56 	keyfile = g_key_file_new();
57 
58 	g_key_file_set_list_separator(keyfile, ',');
59 
60 	if (!g_key_file_load_from_file(keyfile, file, 0, &err)) {
61 		error("Parsing %s failed: %s", file, err->message);
62 		g_error_free(err);
63 		g_key_file_free(keyfile);
64 		return NULL;
65 	}
66 
67 	return keyfile;
68 }
69 
sco_server_cb(GIOChannel * chan,GError * err,gpointer data)70 static void sco_server_cb(GIOChannel *chan, GError *err, gpointer data)
71 {
72 	int sk;
73 	struct audio_device *device;
74 	char addr[18];
75 	bdaddr_t src, dst;
76 
77 	if (err) {
78 		error("sco_server_cb: %s", err->message);
79 		return;
80 	}
81 
82 	bt_io_get(chan, BT_IO_SCO, &err,
83 			BT_IO_OPT_SOURCE_BDADDR, &src,
84 			BT_IO_OPT_DEST_BDADDR, &dst,
85 			BT_IO_OPT_DEST, addr,
86 			BT_IO_OPT_INVALID);
87 	if (err) {
88 		error("bt_io_get: %s", err->message);
89 		goto drop;
90 	}
91 
92 	device = manager_find_device(NULL, &src, &dst, AUDIO_HEADSET_INTERFACE,
93 					FALSE);
94 	if (!device)
95 		device = manager_find_device(NULL, &src, &dst,
96 						AUDIO_GATEWAY_INTERFACE,
97 						FALSE);
98 
99 	if (!device)
100 		goto drop;
101 
102 	if (device->headset) {
103 		if (headset_get_state(device) < HEADSET_STATE_CONNECTED) {
104 			DBG("Refusing SCO from non-connected headset");
105 			goto drop;
106 		}
107 
108 		if (!get_hfp_active(device)) {
109 			error("Refusing non-HFP SCO connect attempt from %s",
110 									addr);
111 			goto drop;
112 		}
113 
114 		if (headset_connect_sco(device, chan) < 0)
115 			goto drop;
116 
117 		headset_set_state(device, HEADSET_STATE_PLAYING);
118 	} else if (device->gateway) {
119 		if (!gateway_is_connected(device)) {
120 			DBG("Refusing SCO from non-connected AG");
121 			goto drop;
122 		}
123 
124 		if (gateway_connect_sco(device, chan) < 0)
125 			goto drop;
126 	} else
127 		goto drop;
128 
129 	sk = g_io_channel_unix_get_fd(chan);
130 	fcntl(sk, F_SETFL, 0);
131 
132 	DBG("Accepted SCO connection from %s", addr);
133 
134 	return;
135 
136 drop:
137 	g_io_channel_shutdown(chan, TRUE, NULL);
138 }
139 
140 static DBusConnection *connection;
141 
audio_init(void)142 static int audio_init(void)
143 {
144 	GKeyFile *config;
145 	gboolean enable_sco;
146 
147 	connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
148 	if (connection == NULL)
149 		return -EIO;
150 
151 	config = load_config_file(CONFIGDIR "/audio.conf");
152 
153 	if (audio_manager_init(connection, config, &enable_sco) < 0)
154 		goto failed;
155 
156 	if (!enable_sco)
157 		return 0;
158 
159 	sco_server = bt_io_listen(BT_IO_SCO, sco_server_cb, NULL, NULL,
160 					NULL, NULL,
161 					BT_IO_OPT_INVALID);
162 	if (!sco_server) {
163 		error("Unable to start SCO server socket");
164 		goto failed;
165 	}
166 
167 	return 0;
168 
169 failed:
170 	audio_manager_exit();
171 
172 	if (connection) {
173 		dbus_connection_unref(connection);
174 		connection = NULL;
175 	}
176 
177 	return -EIO;
178 }
179 
audio_exit(void)180 static void audio_exit(void)
181 {
182 	if (sco_server) {
183 		g_io_channel_shutdown(sco_server, TRUE, NULL);
184 		g_io_channel_unref(sco_server);
185 		sco_server = NULL;
186 	}
187 
188 	audio_manager_exit();
189 
190 	dbus_connection_unref(connection);
191 }
192 
193 BLUETOOTH_PLUGIN_DEFINE(audio, VERSION,
194 			BLUETOOTH_PLUGIN_PRIORITY_DEFAULT, audio_init, audio_exit)
195