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 (at) 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 "unix.h" 46 #include "headset.h" 47 #include "manager.h" 48 #include "gateway.h" 49 50 static GIOChannel *sco_server = NULL; 51 52 static GKeyFile *load_config_file(const char *file) 53 { 54 GError *err = NULL; 55 GKeyFile *keyfile; 56 57 keyfile = g_key_file_new(); 58 59 g_key_file_set_list_separator(keyfile, ','); 60 61 if (!g_key_file_load_from_file(keyfile, file, 0, &err)) { 62 error("Parsing %s failed: %s", file, err->message); 63 g_error_free(err); 64 g_key_file_free(keyfile); 65 return NULL; 66 } 67 68 return keyfile; 69 } 70 71 static void sco_server_cb(GIOChannel *chan, GError *err, gpointer data) 72 { 73 int sk; 74 struct audio_device *device; 75 char addr[18]; 76 bdaddr_t src, dst; 77 78 if (err) { 79 error("sco_server_cb: %s", err->message); 80 return; 81 } 82 83 bt_io_get(chan, BT_IO_SCO, &err, 84 BT_IO_OPT_SOURCE_BDADDR, &src, 85 BT_IO_OPT_DEST_BDADDR, &dst, 86 BT_IO_OPT_DEST, addr, 87 BT_IO_OPT_INVALID); 88 if (err) { 89 error("bt_io_get: %s", err->message); 90 goto drop; 91 } 92 93 device = manager_find_device(NULL, &src, &dst, AUDIO_HEADSET_INTERFACE, 94 FALSE); 95 if (!device) 96 device = manager_find_device(NULL, &src, &dst, 97 AUDIO_GATEWAY_INTERFACE, 98 FALSE); 99 100 if (!device) 101 goto drop; 102 103 if (device->headset) { 104 if (headset_get_state(device) < HEADSET_STATE_CONNECTED) { 105 DBG("Refusing SCO from non-connected headset"); 106 goto drop; 107 } 108 109 if (!get_hfp_active(device)) { 110 error("Refusing non-HFP SCO connect attempt from %s", 111 addr); 112 goto drop; 113 } 114 115 if (headset_connect_sco(device, chan) < 0) 116 goto drop; 117 118 headset_set_state(device, HEADSET_STATE_PLAYING); 119 } else if (device->gateway) { 120 if (!gateway_is_connected(device)) { 121 DBG("Refusing SCO from non-connected AG"); 122 goto drop; 123 } 124 125 if (gateway_connect_sco(device, chan) < 0) 126 goto drop; 127 } else 128 goto drop; 129 130 sk = g_io_channel_unix_get_fd(chan); 131 fcntl(sk, F_SETFL, 0); 132 133 DBG("Accepted SCO connection from %s", addr); 134 135 return; 136 137 drop: 138 g_io_channel_shutdown(chan, TRUE, NULL); 139 } 140 141 static DBusConnection *connection; 142 143 static int audio_init(void) 144 { 145 GKeyFile *config; 146 gboolean enable_sco; 147 148 connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL); 149 if (connection == NULL) 150 return -EIO; 151 152 config = load_config_file(CONFIGDIR "/audio.conf"); 153 154 if (unix_init() < 0) { 155 error("Unable to setup unix socket"); 156 goto failed; 157 } 158 159 if (audio_manager_init(connection, config, &enable_sco) < 0) 160 goto failed; 161 162 if (!enable_sco) 163 return 0; 164 165 sco_server = bt_io_listen(BT_IO_SCO, sco_server_cb, NULL, NULL, 166 NULL, NULL, 167 BT_IO_OPT_INVALID); 168 if (!sco_server) { 169 error("Unable to start SCO server socket"); 170 goto failed; 171 } 172 173 return 0; 174 175 failed: 176 audio_manager_exit(); 177 unix_exit(); 178 179 if (connection) { 180 dbus_connection_unref(connection); 181 connection = NULL; 182 } 183 184 return -EIO; 185 } 186 187 static void audio_exit(void) 188 { 189 if (sco_server) { 190 g_io_channel_shutdown(sco_server, TRUE, NULL); 191 g_io_channel_unref(sco_server); 192 sco_server = NULL; 193 } 194 195 audio_manager_exit(); 196 197 unix_exit(); 198 199 dbus_connection_unref(connection); 200 } 201 202 BLUETOOTH_PLUGIN_DEFINE(audio, VERSION, 203 BLUETOOTH_PLUGIN_PRIORITY_DEFAULT, audio_init, audio_exit) 204