3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2006-2010 Nokia Corporation
6 * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
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.
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.
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
30 #include <sys/socket.h>
33 #include <bluetooth/bluetooth.h>
34 #include <bluetooth/sdp.h>
35 #include <bluetooth/sdp_lib.h>
38 #include <dbus/dbus.h>
40 #include "glib-helper.h"
49 static GIOChannel *sco_server = NULL;
51 static GKeyFile *load_config_file(const char *file)
56 keyfile = g_key_file_new();
58 g_key_file_set_list_separator(keyfile, ',');
60 if (!g_key_file_load_from_file(keyfile, file, 0, &err)) {
61 error("Parsing %s failed: %s", file, err->message);
63 g_key_file_free(keyfile);
70 static void sco_server_cb(GIOChannel *chan, GError *err, gpointer data)
73 struct audio_device *device;
78 error("sco_server_cb: %s", err->message);
82 bt_io_get(chan, BT_IO_SCO, &err,
83 BT_IO_OPT_SOURCE_BDADDR, &src,
84 BT_IO_OPT_DEST_BDADDR, &dst,
88 error("bt_io_get: %s", err->message);
92 device = manager_find_device(NULL, &src, &dst, AUDIO_HEADSET_INTERFACE,
95 device = manager_find_device(NULL, &src, &dst,
96 AUDIO_GATEWAY_INTERFACE,
102 if (device->headset) {
103 if (headset_get_state(device) < HEADSET_STATE_CONNECTED) {
104 DBG("Refusing SCO from non-connected headset");
108 if (!headset_get_hfp_active(device)) {
109 error("Refusing non-HFP SCO connect attempt from %s",
114 if (headset_connect_sco(device, chan) < 0)
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");
124 if (gateway_connect_sco(device, chan) < 0)
129 sk = g_io_channel_unix_get_fd(chan);
130 fcntl(sk, F_SETFL, 0);
132 DBG("Accepted SCO connection from %s", addr);
137 g_io_channel_shutdown(chan, TRUE, NULL);
140 static DBusConnection *connection;
142 static int audio_init(void)
147 connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
148 if (connection == NULL)
151 config = load_config_file(CONFIGDIR "/audio.conf");
153 if (audio_manager_init(connection, config, &enable_sco) < 0)
159 sco_server = bt_io_listen(BT_IO_SCO, sco_server_cb, NULL, NULL,
163 error("Unable to start SCO server socket");
170 audio_manager_exit();
173 dbus_connection_unref(connection);
180 static void audio_exit(void)
183 g_io_channel_shutdown(sco_server, TRUE, NULL);
184 g_io_channel_unref(sco_server);
188 audio_manager_exit();
190 dbus_connection_unref(connection);
193 BLUETOOTH_PLUGIN_DEFINE(audio, VERSION,
194 BLUETOOTH_PLUGIN_PRIORITY_DEFAULT, audio_init, audio_exit)