Home | History | Annotate | Download | only in audio
      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 <stdio.h>
     30 #include <stdlib.h>
     31 #include <stdarg.h>
     32 #include <errno.h>
     33 #include <unistd.h>
     34 #include <fcntl.h>
     35 #include <stdint.h>
     36 #include <sys/stat.h>
     37 #include <dirent.h>
     38 #include <ctype.h>
     39 #include <signal.h>
     40 
     41 #include <bluetooth/bluetooth.h>
     42 #include <bluetooth/sdp.h>
     43 #include <bluetooth/sdp_lib.h>
     44 
     45 #include <glib.h>
     46 #include <dbus/dbus.h>
     47 #include <gdbus.h>
     48 
     49 #include "glib-helper.h"
     50 #include "btio.h"
     51 #include "../src/adapter.h"
     52 #include "../src/manager.h"
     53 #include "../src/device.h"
     54 
     55 #include "log.h"
     56 #include "textfile.h"
     57 #include "ipc.h"
     58 #include "device.h"
     59 #include "error.h"
     60 #include "avdtp.h"
     61 #include "media.h"
     62 #include "a2dp.h"
     63 #include "headset.h"
     64 #include "gateway.h"
     65 #include "sink.h"
     66 #include "source.h"
     67 #include "control.h"
     68 #include "manager.h"
     69 #include "sdpd.h"
     70 #include "telephony.h"
     71 #include "unix.h"
     72 
     73 #ifndef DBUS_TYPE_UNIX_FD
     74 #define DBUS_TYPE_UNIX_FD -1
     75 #endif
     76 
     77 typedef enum {
     78 	HEADSET	= 1 << 0,
     79 	GATEWAY	= 1 << 1,
     80 	SINK	= 1 << 2,
     81 	SOURCE	= 1 << 3,
     82 	CONTROL	= 1 << 4,
     83 	TARGET	= 1 << 5,
     84 	INVALID	= 1 << 6
     85 } audio_service_type;
     86 
     87 typedef enum {
     88 		GENERIC_AUDIO = 0,
     89 		ADVANCED_AUDIO,
     90 		AV_REMOTE,
     91 		GET_RECORDS
     92 } audio_sdp_state_t;
     93 
     94 struct audio_adapter {
     95 	struct btd_adapter *btd_adapter;
     96 	gboolean powered;
     97 	uint32_t hsp_ag_record_id;
     98 	uint32_t hfp_ag_record_id;
     99 	uint32_t hfp_hs_record_id;
    100 	GIOChannel *hsp_ag_server;
    101 	GIOChannel *hfp_ag_server;
    102 	GIOChannel *hfp_hs_server;
    103 	gint ref;
    104 };
    105 
    106 static gboolean auto_connect = TRUE;
    107 static int max_connected_headsets = 1;
    108 static DBusConnection *connection = NULL;
    109 static GKeyFile *config = NULL;
    110 static GSList *adapters = NULL;
    111 static GSList *devices = NULL;
    112 
    113 static struct enabled_interfaces enabled = {
    114 	.hfp		= TRUE,
    115 	.headset	= TRUE,
    116 	.gateway	= FALSE,
    117 	.sink		= TRUE,
    118 	.source		= FALSE,
    119 	.control	= TRUE,
    120 	.socket		= TRUE,
    121 	.media		= FALSE
    122 };
    123 
    124 static struct audio_adapter *find_adapter(GSList *list,
    125 					struct btd_adapter *btd_adapter)
    126 {
    127 	for (; list; list = list->next) {
    128 		struct audio_adapter *adapter = list->data;
    129 
    130 		if (adapter->btd_adapter == btd_adapter)
    131 			return adapter;
    132 	}
    133 
    134 	return NULL;
    135 }
    136 
    137 gboolean server_is_enabled(bdaddr_t *src, uint16_t svc)
    138 {
    139 	switch (svc) {
    140 	case HEADSET_SVCLASS_ID:
    141 		return enabled.headset;
    142 	case HEADSET_AGW_SVCLASS_ID:
    143 		return FALSE;
    144 	case HANDSFREE_SVCLASS_ID:
    145 		return enabled.headset && enabled.hfp;
    146 	case HANDSFREE_AGW_SVCLASS_ID:
    147 		return enabled.gateway;
    148 	case AUDIO_SINK_SVCLASS_ID:
    149 		return enabled.sink;
    150 	case AUDIO_SOURCE_SVCLASS_ID:
    151 		return enabled.source;
    152 	case AV_REMOTE_TARGET_SVCLASS_ID:
    153 	case AV_REMOTE_SVCLASS_ID:
    154 		return enabled.control;
    155 	default:
    156 		return FALSE;
    157 	}
    158 }
    159 
    160 static void handle_uuid(const char *uuidstr, struct audio_device *device)
    161 {
    162 	uuid_t uuid;
    163 	uint16_t uuid16;
    164 
    165 	if (bt_string2uuid(&uuid, uuidstr) < 0) {
    166 		error("%s not detected as an UUID-128", uuidstr);
    167 		return;
    168 	}
    169 
    170 	if (!sdp_uuid128_to_uuid(&uuid) && uuid.type != SDP_UUID16) {
    171 		error("Could not convert %s to a UUID-16", uuidstr);
    172 		return;
    173 	}
    174 
    175 	uuid16 = uuid.value.uuid16;
    176 
    177 	if (!server_is_enabled(&device->src, uuid16)) {
    178 		DBG("server not enabled for %s (0x%04x)", uuidstr, uuid16);
    179 		return;
    180 	}
    181 
    182 	switch (uuid16) {
    183 	case HEADSET_SVCLASS_ID:
    184 		DBG("Found Headset record");
    185 		if (device->headset)
    186 			headset_update(device, uuid16, uuidstr);
    187 		else
    188 			device->headset = headset_init(device, uuid16,
    189 							uuidstr);
    190 		break;
    191 	case HEADSET_AGW_SVCLASS_ID:
    192 		DBG("Found Headset AG record");
    193 		break;
    194 	case HANDSFREE_SVCLASS_ID:
    195 		DBG("Found Handsfree record");
    196 		if (device->headset)
    197 			headset_update(device, uuid16, uuidstr);
    198 		else
    199 			device->headset = headset_init(device, uuid16,
    200 								uuidstr);
    201 		break;
    202 	case HANDSFREE_AGW_SVCLASS_ID:
    203 		DBG("Found Handsfree AG record");
    204 		if (enabled.gateway && (device->gateway == NULL))
    205 			device->gateway = gateway_init(device);
    206 		break;
    207 	case AUDIO_SINK_SVCLASS_ID:
    208 		DBG("Found Audio Sink");
    209 		if (device->sink == NULL)
    210 			device->sink = sink_init(device);
    211 		break;
    212 	case AUDIO_SOURCE_SVCLASS_ID:
    213 		DBG("Found Audio Source");
    214 		if (device->source == NULL)
    215 			device->source = source_init(device);
    216 		break;
    217 	case AV_REMOTE_SVCLASS_ID:
    218 	case AV_REMOTE_TARGET_SVCLASS_ID:
    219 		DBG("Found AV %s", uuid16 == AV_REMOTE_SVCLASS_ID ?
    220 							"Remote" : "Target");
    221 		if (device->control)
    222 			control_update(device, uuid16);
    223 		else
    224 			device->control = control_init(device, uuid16);
    225 		if (device->sink && sink_is_active(device))
    226 			avrcp_connect(device);
    227 		break;
    228 	default:
    229 		DBG("Unrecognized UUID: 0x%04X", uuid16);
    230 		break;
    231 	}
    232 }
    233 
    234 static sdp_record_t *hsp_ag_record(uint8_t ch)
    235 {
    236 	sdp_list_t *svclass_id, *pfseq, *apseq, *root;
    237 	uuid_t root_uuid, svclass_uuid, ga_svclass_uuid;
    238 	uuid_t l2cap_uuid, rfcomm_uuid;
    239 	sdp_profile_desc_t profile;
    240 	sdp_record_t *record;
    241 	sdp_list_t *aproto, *proto[2];
    242 	sdp_data_t *channel;
    243 
    244 	record = sdp_record_alloc();
    245 	if (!record)
    246 		return NULL;
    247 
    248 	sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
    249 	root = sdp_list_append(0, &root_uuid);
    250 	sdp_set_browse_groups(record, root);
    251 
    252 	sdp_uuid16_create(&svclass_uuid, HEADSET_AGW_SVCLASS_ID);
    253 	svclass_id = sdp_list_append(0, &svclass_uuid);
    254 	sdp_uuid16_create(&ga_svclass_uuid, GENERIC_AUDIO_SVCLASS_ID);
    255 	svclass_id = sdp_list_append(svclass_id, &ga_svclass_uuid);
    256 	sdp_set_service_classes(record, svclass_id);
    257 
    258 	sdp_uuid16_create(&profile.uuid, HEADSET_PROFILE_ID);
    259 	profile.version = 0x0102;
    260 	pfseq = sdp_list_append(0, &profile);
    261 	sdp_set_profile_descs(record, pfseq);
    262 
    263 	sdp_uuid16_create(&l2cap_uuid, L2CAP_UUID);
    264 	proto[0] = sdp_list_append(0, &l2cap_uuid);
    265 	apseq = sdp_list_append(0, proto[0]);
    266 
    267 	sdp_uuid16_create(&rfcomm_uuid, RFCOMM_UUID);
    268 	proto[1] = sdp_list_append(0, &rfcomm_uuid);
    269 	channel = sdp_data_alloc(SDP_UINT8, &ch);
    270 	proto[1] = sdp_list_append(proto[1], channel);
    271 	apseq = sdp_list_append(apseq, proto[1]);
    272 
    273 	aproto = sdp_list_append(0, apseq);
    274 	sdp_set_access_protos(record, aproto);
    275 
    276 	sdp_set_info_attr(record, "Headset Audio Gateway", 0, 0);
    277 
    278 	sdp_data_free(channel);
    279 	sdp_list_free(proto[0], 0);
    280 	sdp_list_free(proto[1], 0);
    281 	sdp_list_free(apseq, 0);
    282 	sdp_list_free(pfseq, 0);
    283 	sdp_list_free(aproto, 0);
    284 	sdp_list_free(root, 0);
    285 	sdp_list_free(svclass_id, 0);
    286 
    287 	return record;
    288 }
    289 
    290 static sdp_record_t *hfp_hs_record(uint8_t ch)
    291 {
    292 	sdp_list_t *svclass_id, *pfseq, *apseq, *root;
    293 	uuid_t root_uuid, svclass_uuid, ga_svclass_uuid;
    294 	uuid_t l2cap_uuid, rfcomm_uuid;
    295 	sdp_profile_desc_t profile;
    296 	sdp_record_t *record;
    297 	sdp_list_t *aproto, *proto[2];
    298 	sdp_data_t *channel;
    299 
    300 	record = sdp_record_alloc();
    301 	if (!record)
    302 		return NULL;
    303 
    304 	sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
    305 	root = sdp_list_append(0, &root_uuid);
    306 	sdp_set_browse_groups(record, root);
    307 
    308 	sdp_uuid16_create(&svclass_uuid, HANDSFREE_SVCLASS_ID);
    309 	svclass_id = sdp_list_append(0, &svclass_uuid);
    310 	sdp_uuid16_create(&ga_svclass_uuid, GENERIC_AUDIO_SVCLASS_ID);
    311 	svclass_id = sdp_list_append(svclass_id, &ga_svclass_uuid);
    312 	sdp_set_service_classes(record, svclass_id);
    313 
    314 	sdp_uuid16_create(&profile.uuid, HANDSFREE_PROFILE_ID);
    315 	profile.version = 0x0105;
    316 	pfseq = sdp_list_append(0, &profile);
    317 	sdp_set_profile_descs(record, pfseq);
    318 
    319 	sdp_uuid16_create(&l2cap_uuid, L2CAP_UUID);
    320 	proto[0] = sdp_list_append(0, &l2cap_uuid);
    321 	apseq = sdp_list_append(0, proto[0]);
    322 
    323 	sdp_uuid16_create(&rfcomm_uuid, RFCOMM_UUID);
    324 	proto[1] = sdp_list_append(0, &rfcomm_uuid);
    325 	channel = sdp_data_alloc(SDP_UINT8, &ch);
    326 	proto[1] = sdp_list_append(proto[1], channel);
    327 	apseq = sdp_list_append(apseq, proto[1]);
    328 
    329 	aproto = sdp_list_append(0, apseq);
    330 	sdp_set_access_protos(record, aproto);
    331 
    332 	sdp_set_info_attr(record, "Hands-Free", 0, 0);
    333 
    334 	sdp_data_free(channel);
    335 	sdp_list_free(proto[0], 0);
    336 	sdp_list_free(proto[1], 0);
    337 	sdp_list_free(apseq, 0);
    338 	sdp_list_free(pfseq, 0);
    339 	sdp_list_free(aproto, 0);
    340 	sdp_list_free(root, 0);
    341 	sdp_list_free(svclass_id, 0);
    342 
    343 	return record;
    344 }
    345 
    346 static sdp_record_t *hfp_ag_record(uint8_t ch, uint32_t feat)
    347 {
    348 	sdp_list_t *svclass_id, *pfseq, *apseq, *root;
    349 	uuid_t root_uuid, svclass_uuid, ga_svclass_uuid;
    350 	uuid_t l2cap_uuid, rfcomm_uuid;
    351 	sdp_profile_desc_t profile;
    352 	sdp_list_t *aproto, *proto[2];
    353 	sdp_record_t *record;
    354 	sdp_data_t *channel, *features;
    355 	uint8_t netid = 0x01;
    356 	uint16_t sdpfeat;
    357 	sdp_data_t *network;
    358 
    359 	record = sdp_record_alloc();
    360 	if (!record)
    361 		return NULL;
    362 
    363 	network = sdp_data_alloc(SDP_UINT8, &netid);
    364 	if (!network) {
    365 		sdp_record_free(record);
    366 		return NULL;
    367 	}
    368 
    369 	sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
    370 	root = sdp_list_append(0, &root_uuid);
    371 	sdp_set_browse_groups(record, root);
    372 
    373 	sdp_uuid16_create(&svclass_uuid, HANDSFREE_AGW_SVCLASS_ID);
    374 	svclass_id = sdp_list_append(0, &svclass_uuid);
    375 	sdp_uuid16_create(&ga_svclass_uuid, GENERIC_AUDIO_SVCLASS_ID);
    376 	svclass_id = sdp_list_append(svclass_id, &ga_svclass_uuid);
    377 	sdp_set_service_classes(record, svclass_id);
    378 
    379 	sdp_uuid16_create(&profile.uuid, HANDSFREE_PROFILE_ID);
    380 	profile.version = 0x0105;
    381 	pfseq = sdp_list_append(0, &profile);
    382 	sdp_set_profile_descs(record, pfseq);
    383 
    384 	sdp_uuid16_create(&l2cap_uuid, L2CAP_UUID);
    385 	proto[0] = sdp_list_append(0, &l2cap_uuid);
    386 	apseq = sdp_list_append(0, proto[0]);
    387 
    388 	sdp_uuid16_create(&rfcomm_uuid, RFCOMM_UUID);
    389 	proto[1] = sdp_list_append(0, &rfcomm_uuid);
    390 	channel = sdp_data_alloc(SDP_UINT8, &ch);
    391 	proto[1] = sdp_list_append(proto[1], channel);
    392 	apseq = sdp_list_append(apseq, proto[1]);
    393 
    394 	sdpfeat = (uint16_t) feat & 0xF;
    395 	features = sdp_data_alloc(SDP_UINT16, &sdpfeat);
    396 	sdp_attr_add(record, SDP_ATTR_SUPPORTED_FEATURES, features);
    397 
    398 	aproto = sdp_list_append(0, apseq);
    399 	sdp_set_access_protos(record, aproto);
    400 
    401 	sdp_set_info_attr(record, "Hands-Free Audio Gateway", 0, 0);
    402 
    403 	sdp_attr_add(record, SDP_ATTR_EXTERNAL_NETWORK, network);
    404 
    405 	sdp_data_free(channel);
    406 	sdp_list_free(proto[0], 0);
    407 	sdp_list_free(proto[1], 0);
    408 	sdp_list_free(apseq, 0);
    409 	sdp_list_free(pfseq, 0);
    410 	sdp_list_free(aproto, 0);
    411 	sdp_list_free(root, 0);
    412 	sdp_list_free(svclass_id, 0);
    413 
    414 	return record;
    415 }
    416 
    417 static void headset_auth_cb(DBusError *derr, void *user_data)
    418 {
    419 	struct audio_device *device = user_data;
    420 	GError *err = NULL;
    421 	GIOChannel *io;
    422 
    423 	if (device->hs_preauth_id) {
    424 		g_source_remove(device->hs_preauth_id);
    425 		device->hs_preauth_id = 0;
    426 	}
    427 
    428 	if (derr && dbus_error_is_set(derr)) {
    429 		error("Access denied: %s", derr->message);
    430 		headset_set_state(device, HEADSET_STATE_DISCONNECTED);
    431 		return;
    432 	}
    433 
    434 	io = headset_get_rfcomm(device);
    435 
    436 	if (!bt_io_accept(io, headset_connect_cb, device, NULL, &err)) {
    437 		error("bt_io_accept: %s", err->message);
    438 		g_error_free(err);
    439 		headset_set_state(device, HEADSET_STATE_DISCONNECTED);
    440 		return;
    441 	}
    442 }
    443 
    444 static gboolean hs_preauth_cb(GIOChannel *chan, GIOCondition cond,
    445 							gpointer user_data)
    446 {
    447 	struct audio_device *device = user_data;
    448 
    449 	DBG("Headset disconnected during authorization");
    450 
    451 	audio_device_cancel_authorization(device, headset_auth_cb, device);
    452 
    453 	headset_set_state(device, HEADSET_STATE_DISCONNECTED);
    454 
    455 	device->hs_preauth_id = 0;
    456 
    457 	return FALSE;
    458 }
    459 
    460 static void ag_confirm(GIOChannel *chan, gpointer data)
    461 {
    462 	const char *server_uuid, *remote_uuid;
    463 	struct audio_device *device;
    464 	gboolean hfp_active;
    465 	bdaddr_t src, dst;
    466 	int perr;
    467 	GError *err = NULL;
    468 	uint8_t ch;
    469 
    470 	bt_io_get(chan, BT_IO_RFCOMM, &err,
    471 			BT_IO_OPT_SOURCE_BDADDR, &src,
    472 			BT_IO_OPT_DEST_BDADDR, &dst,
    473 			BT_IO_OPT_CHANNEL, &ch,
    474 			BT_IO_OPT_INVALID);
    475 	if (err) {
    476 		error("%s", err->message);
    477 		g_error_free(err);
    478 		goto drop;
    479 	}
    480 
    481 	if (ch == DEFAULT_HS_AG_CHANNEL) {
    482 		hfp_active = FALSE;
    483 		server_uuid = HSP_AG_UUID;
    484 		remote_uuid = HSP_HS_UUID;
    485 	} else {
    486 		hfp_active = TRUE;
    487 		server_uuid = HFP_AG_UUID;
    488 		remote_uuid = HFP_HS_UUID;
    489 	}
    490 
    491 	device = manager_get_device(&src, &dst, TRUE);
    492 	if (!device)
    493 		goto drop;
    494 
    495 	if (!manager_allow_headset_connection(device)) {
    496 		DBG("Refusing headset: too many existing connections");
    497 		goto drop;
    498 	}
    499 
    500 	if (!device->headset) {
    501 		btd_device_add_uuid(device->btd_dev, remote_uuid);
    502 		if (!device->headset)
    503 			goto drop;
    504 	}
    505 
    506 	if (headset_get_state(device) > HEADSET_STATE_DISCONNECTED) {
    507 		DBG("Refusing new connection since one already exists");
    508 		goto drop;
    509 	}
    510 
    511 	set_hfp_active(device, hfp_active);
    512 
    513 	if (headset_connect_rfcomm(device, chan) < 0) {
    514 		error("headset_connect_rfcomm failed");
    515 		goto drop;
    516 	}
    517 
    518 	headset_set_state(device, HEADSET_STATE_CONNECTING);
    519 
    520 	perr = audio_device_request_authorization(device, server_uuid,
    521 						headset_auth_cb, device);
    522 	if (perr < 0) {
    523 		DBG("Authorization denied: %s", strerror(-perr));
    524 		headset_set_state(device, HEADSET_STATE_DISCONNECTED);
    525 		return;
    526 	}
    527 
    528 	device->hs_preauth_id = g_io_add_watch(chan,
    529 					G_IO_NVAL | G_IO_HUP | G_IO_ERR,
    530 					hs_preauth_cb, device);
    531 
    532 	device->auto_connect = auto_connect;
    533 
    534 	return;
    535 
    536 drop:
    537 	g_io_channel_shutdown(chan, TRUE, NULL);
    538 }
    539 
    540 static void gateway_auth_cb(DBusError *derr, void *user_data)
    541 {
    542 	struct audio_device *device = user_data;
    543 
    544 	if (derr && dbus_error_is_set(derr))
    545 		error("Access denied: %s", derr->message);
    546 	else {
    547 		char ag_address[18];
    548 
    549 		ba2str(&device->dst, ag_address);
    550 		DBG("Accepted AG connection from %s for %s",
    551 			ag_address, device->path);
    552 
    553 		gateway_start_service(device);
    554 	}
    555 }
    556 
    557 static void hf_io_cb(GIOChannel *chan, gpointer data)
    558 {
    559 	bdaddr_t src, dst;
    560 	GError *err = NULL;
    561 	uint8_t ch;
    562 	const char *server_uuid, *remote_uuid;
    563 	struct audio_device *device;
    564 	int perr;
    565 
    566 	bt_io_get(chan, BT_IO_RFCOMM, &err,
    567 			BT_IO_OPT_SOURCE_BDADDR, &src,
    568 			BT_IO_OPT_DEST_BDADDR, &dst,
    569 			BT_IO_OPT_CHANNEL, &ch,
    570 			BT_IO_OPT_INVALID);
    571 
    572 	if (err) {
    573 		error("%s", err->message);
    574 		g_error_free(err);
    575 		return;
    576 	}
    577 
    578 	server_uuid = HFP_AG_UUID;
    579 	remote_uuid = HFP_HS_UUID;
    580 
    581 	device = manager_get_device(&src, &dst, TRUE);
    582 	if (!device)
    583 		goto drop;
    584 
    585 	if (!device->gateway) {
    586 		btd_device_add_uuid(device->btd_dev, remote_uuid);
    587 		if (!device->gateway)
    588 			goto drop;
    589 	}
    590 
    591 	if (gateway_is_connected(device)) {
    592 		DBG("Refusing new connection since one already exists");
    593 		goto drop;
    594 	}
    595 
    596 	if (gateway_connect_rfcomm(device, chan) < 0) {
    597 		error("Allocating new GIOChannel failed!");
    598 		goto drop;
    599 	}
    600 
    601 	perr = audio_device_request_authorization(device, server_uuid,
    602 						gateway_auth_cb, device);
    603 	if (perr < 0) {
    604 		DBG("Authorization denied!");
    605 		goto drop;
    606 	}
    607 
    608 	return;
    609 
    610 drop:
    611 	g_io_channel_shutdown(chan, TRUE, NULL);
    612 }
    613 
    614 static int headset_server_init(struct audio_adapter *adapter)
    615 {
    616 	uint8_t chan = DEFAULT_HS_AG_CHANNEL;
    617 	sdp_record_t *record;
    618 	gboolean master = TRUE;
    619 	GError *err = NULL;
    620 	uint32_t features;
    621 	GIOChannel *io;
    622 	bdaddr_t src;
    623 
    624 	if (config) {
    625 		gboolean tmp;
    626 
    627 		tmp = g_key_file_get_boolean(config, "General", "Master",
    628 						&err);
    629 		if (err) {
    630 			DBG("audio.conf: %s", err->message);
    631 			g_clear_error(&err);
    632 		} else
    633 			master = tmp;
    634 	}
    635 
    636 	adapter_get_address(adapter->btd_adapter, &src);
    637 
    638 	io =  bt_io_listen(BT_IO_RFCOMM, NULL, ag_confirm, adapter, NULL, &err,
    639 				BT_IO_OPT_SOURCE_BDADDR, &src,
    640 				BT_IO_OPT_CHANNEL, chan,
    641 				BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_MEDIUM,
    642 				BT_IO_OPT_MASTER, master,
    643 				BT_IO_OPT_INVALID);
    644 	if (!io)
    645 		goto failed;
    646 
    647 	adapter->hsp_ag_server = io;
    648 
    649 	record = hsp_ag_record(chan);
    650 	if (!record) {
    651 		error("Unable to allocate new service record");
    652 		goto failed;
    653 	}
    654 
    655 	if (add_record_to_server(&src, record) < 0) {
    656 		error("Unable to register HS AG service record");
    657 		sdp_record_free(record);
    658 		goto failed;
    659 	}
    660 	adapter->hsp_ag_record_id = record->handle;
    661 
    662 	features = headset_config_init(config);
    663 
    664 	if (!enabled.hfp)
    665 		return 0;
    666 
    667 	chan = DEFAULT_HF_AG_CHANNEL;
    668 
    669 	io = bt_io_listen(BT_IO_RFCOMM, NULL, ag_confirm, adapter, NULL, &err,
    670 				BT_IO_OPT_SOURCE_BDADDR, &src,
    671 				BT_IO_OPT_CHANNEL, chan,
    672 				BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_MEDIUM,
    673 				BT_IO_OPT_MASTER, master,
    674 				BT_IO_OPT_INVALID);
    675 	if (!io)
    676 		goto failed;
    677 
    678 	adapter->hfp_ag_server = io;
    679 
    680 	record = hfp_ag_record(chan, features);
    681 	if (!record) {
    682 		error("Unable to allocate new service record");
    683 		goto failed;
    684 	}
    685 
    686 	if (add_record_to_server(&src, record) < 0) {
    687 		error("Unable to register HF AG service record");
    688 		sdp_record_free(record);
    689 		goto failed;
    690 	}
    691 	adapter->hfp_ag_record_id = record->handle;
    692 
    693 	return 0;
    694 
    695 failed:
    696 	if (err) {
    697 		error("%s", err->message);
    698 		g_error_free(err);
    699 	}
    700 
    701 	if (adapter->hsp_ag_server) {
    702 		g_io_channel_shutdown(adapter->hsp_ag_server, TRUE, NULL);
    703 		g_io_channel_unref(adapter->hsp_ag_server);
    704 		adapter->hsp_ag_server = NULL;
    705 	}
    706 
    707 	if (adapter->hfp_ag_server) {
    708 		g_io_channel_shutdown(adapter->hfp_ag_server, TRUE, NULL);
    709 		g_io_channel_unref(adapter->hfp_ag_server);
    710 		adapter->hfp_ag_server = NULL;
    711 	}
    712 
    713 	return -1;
    714 }
    715 
    716 static int gateway_server_init(struct audio_adapter *adapter)
    717 {
    718 	uint8_t chan = DEFAULT_HFP_HS_CHANNEL;
    719 	sdp_record_t *record;
    720 	gboolean master = TRUE;
    721 	GError *err = NULL;
    722 	GIOChannel *io;
    723 	bdaddr_t src;
    724 
    725 	if (config) {
    726 		gboolean tmp;
    727 
    728 		tmp = g_key_file_get_boolean(config, "General", "Master",
    729 						&err);
    730 		if (err) {
    731 			DBG("audio.conf: %s", err->message);
    732 			g_clear_error(&err);
    733 		} else
    734 			master = tmp;
    735 	}
    736 
    737 	adapter_get_address(adapter->btd_adapter, &src);
    738 
    739 	io = bt_io_listen(BT_IO_RFCOMM, NULL, hf_io_cb, adapter, NULL, &err,
    740 				BT_IO_OPT_SOURCE_BDADDR, &src,
    741 				BT_IO_OPT_CHANNEL, chan,
    742 				BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_MEDIUM,
    743 				BT_IO_OPT_MASTER, master,
    744 				BT_IO_OPT_INVALID);
    745 	if (!io) {
    746 		error("%s", err->message);
    747 		g_error_free(err);
    748 		return -1;
    749 	}
    750 
    751 	adapter->hfp_hs_server = io;
    752 	record = hfp_hs_record(chan);
    753 	if (!record) {
    754 		error("Unable to allocate new service record");
    755 		return -1;
    756 	}
    757 
    758 	if (add_record_to_server(&src, record) < 0) {
    759 		error("Unable to register HFP HS service record");
    760 		sdp_record_free(record);
    761 		g_io_channel_unref(adapter->hfp_hs_server);
    762 		adapter->hfp_hs_server = NULL;
    763 		return -1;
    764 	}
    765 
    766 	adapter->hfp_hs_record_id = record->handle;
    767 
    768 	return 0;
    769 }
    770 
    771 static int audio_probe(struct btd_device *device, GSList *uuids)
    772 {
    773 	struct btd_adapter *adapter = device_get_adapter(device);
    774 	bdaddr_t src, dst;
    775 	struct audio_device *audio_dev;
    776 
    777 	adapter_get_address(adapter, &src);
    778 	device_get_address(device, &dst);
    779 
    780 	audio_dev = manager_get_device(&src, &dst, TRUE);
    781 	if (!audio_dev) {
    782 		DBG("unable to get a device object");
    783 		return -1;
    784 	}
    785 
    786 	g_slist_foreach(uuids, (GFunc) handle_uuid, audio_dev);
    787 
    788 	return 0;
    789 }
    790 
    791 static void audio_remove(struct btd_device *device)
    792 {
    793 	struct audio_device *dev;
    794 	const char *path;
    795 
    796 	path = device_get_path(device);
    797 
    798 	dev = manager_find_device(path, NULL, NULL, NULL, FALSE);
    799 	if (!dev)
    800 		return;
    801 
    802 	devices = g_slist_remove(devices, dev);
    803 
    804 	audio_device_unregister(dev);
    805 
    806 }
    807 
    808 static struct audio_adapter *audio_adapter_ref(struct audio_adapter *adp)
    809 {
    810 	adp->ref++;
    811 
    812 	DBG("%p: ref=%d", adp, adp->ref);
    813 
    814 	return adp;
    815 }
    816 
    817 static void audio_adapter_unref(struct audio_adapter *adp)
    818 {
    819 	adp->ref--;
    820 
    821 	DBG("%p: ref=%d", adp, adp->ref);
    822 
    823 	if (adp->ref > 0)
    824 		return;
    825 
    826 	adapters = g_slist_remove(adapters, adp);
    827 	btd_adapter_unref(adp->btd_adapter);
    828 	g_free(adp);
    829 }
    830 
    831 static struct audio_adapter *audio_adapter_create(struct btd_adapter *adapter)
    832 {
    833 	struct audio_adapter *adp;
    834 
    835 	adp = g_new0(struct audio_adapter, 1);
    836 	adp->btd_adapter = btd_adapter_ref(adapter);
    837 
    838 	return audio_adapter_ref(adp);
    839 }
    840 
    841 static struct audio_adapter *audio_adapter_get(struct btd_adapter *adapter)
    842 {
    843 	struct audio_adapter *adp;
    844 
    845 	adp = find_adapter(adapters, adapter);
    846 	if (!adp) {
    847 		adp = audio_adapter_create(adapter);
    848 		if (!adp)
    849 			return NULL;
    850 		adapters = g_slist_append(adapters, adp);
    851 	} else
    852 		audio_adapter_ref(adp);
    853 
    854 	return adp;
    855 }
    856 
    857 static void state_changed(struct btd_adapter *adapter, gboolean powered)
    858 {
    859 	struct audio_adapter *adp;
    860 	static gboolean telephony = FALSE;
    861 	GSList *l;
    862 
    863 	DBG("%s powered %s", adapter_get_path(adapter),
    864 						powered ? "on" : "off");
    865 
    866 	/* ignore powered change, adapter is powering down */
    867 	if (powered && adapter_powering_down(adapter))
    868 		return;
    869 
    870 	adp = find_adapter(adapters, adapter);
    871 	if (!adp)
    872 		return;
    873 
    874 	adp->powered = powered;
    875 
    876 	if (powered) {
    877 		/* telephony driver already initialized*/
    878 		if (telephony == TRUE)
    879 			return;
    880 		telephony_init();
    881 		telephony = TRUE;
    882 		return;
    883 	}
    884 
    885 	/* telephony not initialized just ignore power down */
    886 	if (telephony == FALSE)
    887 		return;
    888 
    889 	for (l = adapters; l; l = l->next) {
    890 		adp = l->data;
    891 
    892 		if (adp->powered == TRUE)
    893 			return;
    894 	}
    895 
    896 	telephony_exit();
    897 	telephony = FALSE;
    898 }
    899 
    900 static int headset_server_probe(struct btd_adapter *adapter)
    901 {
    902 	struct audio_adapter *adp;
    903 	const gchar *path = adapter_get_path(adapter);
    904 	int err;
    905 
    906 	DBG("path %s", path);
    907 
    908 	adp = audio_adapter_get(adapter);
    909 	if (!adp)
    910 		return -EINVAL;
    911 
    912 	err = headset_server_init(adp);
    913 	if (err < 0) {
    914 		audio_adapter_unref(adp);
    915 		return err;
    916 	}
    917 
    918 	btd_adapter_register_powered_callback(adapter, state_changed);
    919 	state_changed(adapter, TRUE);
    920 
    921 	return 0;
    922 }
    923 
    924 static void headset_server_remove(struct btd_adapter *adapter)
    925 {
    926 	struct audio_adapter *adp;
    927 	const gchar *path = adapter_get_path(adapter);
    928 
    929 	DBG("path %s", path);
    930 
    931 	adp = find_adapter(adapters, adapter);
    932 	if (!adp)
    933 		return;
    934 
    935 	if (adp->hsp_ag_record_id) {
    936 		remove_record_from_server(adp->hsp_ag_record_id);
    937 		adp->hsp_ag_record_id = 0;
    938 	}
    939 
    940 	if (adp->hsp_ag_server) {
    941 		g_io_channel_shutdown(adp->hsp_ag_server, TRUE, NULL);
    942 		g_io_channel_unref(adp->hsp_ag_server);
    943 		adp->hsp_ag_server = NULL;
    944 	}
    945 
    946 	if (adp->hfp_ag_record_id) {
    947 		remove_record_from_server(adp->hfp_ag_record_id);
    948 		adp->hfp_ag_record_id = 0;
    949 	}
    950 
    951 	if (adp->hfp_ag_server) {
    952 		g_io_channel_shutdown(adp->hfp_ag_server, TRUE, NULL);
    953 		g_io_channel_unref(adp->hfp_ag_server);
    954 		adp->hfp_ag_server = NULL;
    955 	}
    956 
    957 	btd_adapter_unregister_powered_callback(adapter, state_changed);
    958 
    959 	audio_adapter_unref(adp);
    960 }
    961 
    962 static int gateway_server_probe(struct btd_adapter *adapter)
    963 {
    964 	struct audio_adapter *adp;
    965 
    966 	adp = audio_adapter_get(adapter);
    967 	if (!adp)
    968 		return -EINVAL;
    969 
    970 	return gateway_server_init(adp);
    971 }
    972 
    973 static void gateway_server_remove(struct btd_adapter *adapter)
    974 {
    975 	struct audio_adapter *adp;
    976 	const gchar *path = adapter_get_path(adapter);
    977 
    978 	DBG("path %s", path);
    979 
    980 	adp = find_adapter(adapters, adapter);
    981 	if (!adp)
    982 		return;
    983 
    984 	if (adp->hfp_hs_record_id) {
    985 		remove_record_from_server(adp->hfp_hs_record_id);
    986 		adp->hfp_hs_record_id = 0;
    987 	}
    988 
    989 	if (adp->hfp_hs_server) {
    990 		g_io_channel_unref(adp->hfp_hs_server);
    991 		adp->hfp_hs_server = NULL;
    992 	}
    993 
    994 	audio_adapter_unref(adp);
    995 }
    996 
    997 static int a2dp_server_probe(struct btd_adapter *adapter)
    998 {
    999 	struct audio_adapter *adp;
   1000 	const gchar *path = adapter_get_path(adapter);
   1001 	bdaddr_t src;
   1002 	int err;
   1003 
   1004 	DBG("path %s", path);
   1005 
   1006 	adp = audio_adapter_get(adapter);
   1007 	if (!adp)
   1008 		return -EINVAL;
   1009 
   1010 	adapter_get_address(adapter, &src);
   1011 
   1012 	err = a2dp_register(connection, &src, config);
   1013 	if (err < 0)
   1014 		audio_adapter_unref(adp);
   1015 
   1016 	return err;
   1017 }
   1018 
   1019 static void a2dp_server_remove(struct btd_adapter *adapter)
   1020 {
   1021 	struct audio_adapter *adp;
   1022 	const gchar *path = adapter_get_path(adapter);
   1023 	bdaddr_t src;
   1024 
   1025 	DBG("path %s", path);
   1026 
   1027 	adp = find_adapter(adapters, adapter);
   1028 	if (!adp)
   1029 		return;
   1030 
   1031 	adapter_get_address(adapter, &src);
   1032 	a2dp_unregister(&src);
   1033 	audio_adapter_unref(adp);
   1034 }
   1035 
   1036 static int avrcp_server_probe(struct btd_adapter *adapter)
   1037 {
   1038 	struct audio_adapter *adp;
   1039 	const gchar *path = adapter_get_path(adapter);
   1040 	bdaddr_t src;
   1041 
   1042 	DBG("path %s", path);
   1043 
   1044 	adp = audio_adapter_get(adapter);
   1045 	if (!adp)
   1046 		return -EINVAL;
   1047 
   1048 	adapter_get_address(adapter, &src);
   1049 
   1050 	return avrcp_register(connection, &src, config);
   1051 }
   1052 
   1053 static void avrcp_server_remove(struct btd_adapter *adapter)
   1054 {
   1055 	struct audio_adapter *adp;
   1056 	const gchar *path = adapter_get_path(adapter);
   1057 	bdaddr_t src;
   1058 
   1059 	DBG("path %s", path);
   1060 
   1061 	adp = find_adapter(adapters, adapter);
   1062 	if (!adp)
   1063 		return;
   1064 
   1065 	adapter_get_address(adapter, &src);
   1066 	avrcp_unregister(&src);
   1067 	audio_adapter_unref(adp);
   1068 }
   1069 
   1070 static int media_server_probe(struct btd_adapter *adapter)
   1071 {
   1072 	struct audio_adapter *adp;
   1073 	const gchar *path = adapter_get_path(adapter);
   1074 	bdaddr_t src;
   1075 
   1076 	DBG("path %s", path);
   1077 
   1078 	adp = audio_adapter_get(adapter);
   1079 	if (!adp)
   1080 		return -EINVAL;
   1081 
   1082 	adapter_get_address(adapter, &src);
   1083 
   1084 	return media_register(connection, path, &src);
   1085 }
   1086 
   1087 static void media_server_remove(struct btd_adapter *adapter)
   1088 {
   1089 	struct audio_adapter *adp;
   1090 	const gchar *path = adapter_get_path(adapter);
   1091 
   1092 	DBG("path %s", path);
   1093 
   1094 	adp = find_adapter(adapters, adapter);
   1095 	if (!adp)
   1096 		return;
   1097 
   1098 	media_unregister(path);
   1099 	audio_adapter_unref(adp);
   1100 }
   1101 
   1102 static struct btd_device_driver audio_driver = {
   1103 	.name	= "audio",
   1104 	.uuids	= BTD_UUIDS(HSP_HS_UUID, HFP_HS_UUID, HSP_AG_UUID, HFP_AG_UUID,
   1105 			ADVANCED_AUDIO_UUID, A2DP_SOURCE_UUID, A2DP_SINK_UUID,
   1106 			AVRCP_TARGET_UUID, AVRCP_REMOTE_UUID),
   1107 	.probe	= audio_probe,
   1108 	.remove	= audio_remove,
   1109 };
   1110 
   1111 static struct btd_adapter_driver headset_server_driver = {
   1112 	.name	= "audio-headset",
   1113 	.probe	= headset_server_probe,
   1114 	.remove	= headset_server_remove,
   1115 };
   1116 
   1117 static struct btd_adapter_driver gateway_server_driver = {
   1118 	.name	= "audio-gateway",
   1119 	.probe	= gateway_server_probe,
   1120 	.remove	= gateway_server_remove,
   1121 };
   1122 
   1123 static struct btd_adapter_driver a2dp_server_driver = {
   1124 	.name	= "audio-a2dp",
   1125 	.probe	= a2dp_server_probe,
   1126 	.remove	= a2dp_server_remove,
   1127 };
   1128 
   1129 static struct btd_adapter_driver avrcp_server_driver = {
   1130 	.name	= "audio-control",
   1131 	.probe	= avrcp_server_probe,
   1132 	.remove	= avrcp_server_remove,
   1133 };
   1134 
   1135 static struct btd_adapter_driver media_server_driver = {
   1136 	.name	= "media",
   1137 	.probe	= media_server_probe,
   1138 	.remove	= media_server_remove,
   1139 };
   1140 
   1141 int audio_manager_init(DBusConnection *conn, GKeyFile *conf,
   1142 							gboolean *enable_sco)
   1143 {
   1144 	char **list;
   1145 	int i;
   1146 	gboolean b;
   1147 	GError *err = NULL;
   1148 
   1149 	connection = dbus_connection_ref(conn);
   1150 
   1151 	if (!conf)
   1152 		goto proceed;
   1153 
   1154 	config = conf;
   1155 
   1156 	list = g_key_file_get_string_list(config, "General", "Enable",
   1157 						NULL, NULL);
   1158 	for (i = 0; list && list[i] != NULL; i++) {
   1159 		if (g_str_equal(list[i], "Headset"))
   1160 			enabled.headset = TRUE;
   1161 		else if (g_str_equal(list[i], "Gateway"))
   1162 			enabled.gateway = TRUE;
   1163 		else if (g_str_equal(list[i], "Sink"))
   1164 			enabled.sink = TRUE;
   1165 		else if (g_str_equal(list[i], "Source"))
   1166 			enabled.source = TRUE;
   1167 		else if (g_str_equal(list[i], "Control"))
   1168 			enabled.control = TRUE;
   1169 		else if (g_str_equal(list[i], "Socket"))
   1170 			enabled.socket = TRUE;
   1171 		else if (g_str_equal(list[i], "Media"))
   1172 			enabled.media = TRUE;
   1173 	}
   1174 	g_strfreev(list);
   1175 
   1176 	list = g_key_file_get_string_list(config, "General", "Disable",
   1177 						NULL, NULL);
   1178 	for (i = 0; list && list[i] != NULL; i++) {
   1179 		if (g_str_equal(list[i], "Headset"))
   1180 			enabled.headset = FALSE;
   1181 		else if (g_str_equal(list[i], "Gateway"))
   1182 			enabled.gateway = FALSE;
   1183 		else if (g_str_equal(list[i], "Sink"))
   1184 			enabled.sink = FALSE;
   1185 		else if (g_str_equal(list[i], "Source"))
   1186 			enabled.source = FALSE;
   1187 		else if (g_str_equal(list[i], "Control"))
   1188 			enabled.control = FALSE;
   1189 		else if (g_str_equal(list[i], "Socket"))
   1190 			enabled.socket = FALSE;
   1191 		else if (g_str_equal(list[i], "Media"))
   1192 			enabled.media = FALSE;
   1193 	}
   1194 	g_strfreev(list);
   1195 
   1196 	b = g_key_file_get_boolean(config, "General", "AutoConnect", &err);
   1197 	if (err) {
   1198 		DBG("audio.conf: %s", err->message);
   1199 		g_clear_error(&err);
   1200 	} else
   1201 		auto_connect = b;
   1202 
   1203 	b = g_key_file_get_boolean(config, "Headset", "HFP",
   1204 					&err);
   1205 	if (err)
   1206 		g_clear_error(&err);
   1207 	else
   1208 		enabled.hfp = b;
   1209 
   1210 	err = NULL;
   1211 	i = g_key_file_get_integer(config, "Headset", "MaxConnected",
   1212 					&err);
   1213 	if (err) {
   1214 		DBG("audio.conf: %s", err->message);
   1215 		g_clear_error(&err);
   1216 	} else
   1217 		max_connected_headsets = i;
   1218 
   1219 proceed:
   1220 	if (enabled.socket)
   1221 		unix_init();
   1222 
   1223 	if (enabled.media)
   1224 		btd_register_adapter_driver(&media_server_driver);
   1225 
   1226 	if (enabled.headset)
   1227 		btd_register_adapter_driver(&headset_server_driver);
   1228 
   1229 	if (enabled.gateway)
   1230 		btd_register_adapter_driver(&gateway_server_driver);
   1231 
   1232 	if (enabled.source || enabled.sink)
   1233 		btd_register_adapter_driver(&a2dp_server_driver);
   1234 
   1235 	if (enabled.control)
   1236 		btd_register_adapter_driver(&avrcp_server_driver);
   1237 
   1238 	btd_register_device_driver(&audio_driver);
   1239 
   1240 	*enable_sco = (enabled.gateway || enabled.headset);
   1241 
   1242 	return 0;
   1243 }
   1244 
   1245 void audio_manager_exit(void)
   1246 {
   1247 	/* Bail out early if we haven't been initialized */
   1248 	if (connection == NULL)
   1249 		return;
   1250 
   1251 	dbus_connection_unref(connection);
   1252 	connection = NULL;
   1253 
   1254 	if (config) {
   1255 		g_key_file_free(config);
   1256 		config = NULL;
   1257 	}
   1258 
   1259 	if (enabled.socket)
   1260 		unix_exit();
   1261 
   1262 	if (enabled.media)
   1263 		btd_unregister_adapter_driver(&media_server_driver);
   1264 
   1265 	if (enabled.headset)
   1266 		btd_unregister_adapter_driver(&headset_server_driver);
   1267 
   1268 	if (enabled.gateway)
   1269 		btd_unregister_adapter_driver(&gateway_server_driver);
   1270 
   1271 	if (enabled.source || enabled.sink)
   1272 		btd_unregister_adapter_driver(&a2dp_server_driver);
   1273 
   1274 	if (enabled.control)
   1275 		btd_unregister_adapter_driver(&avrcp_server_driver);
   1276 
   1277 	btd_unregister_device_driver(&audio_driver);
   1278 }
   1279 
   1280 struct audio_device *manager_find_device(const char *path,
   1281 					const bdaddr_t *src,
   1282 					const bdaddr_t *dst,
   1283 					const char *interface,
   1284 					gboolean connected)
   1285 {
   1286 	GSList *l;
   1287 
   1288 	for (l = devices; l != NULL; l = l->next) {
   1289 		struct audio_device *dev = l->data;
   1290 
   1291 		if ((path && (strcmp(path, "")) && strcmp(dev->path, path)))
   1292 			continue;
   1293 
   1294 		if ((src && bacmp(src, BDADDR_ANY)) && bacmp(&dev->src, src))
   1295 			continue;
   1296 
   1297 		if ((dst && bacmp(dst, BDADDR_ANY)) && bacmp(&dev->dst, dst))
   1298 			continue;
   1299 
   1300 		if (interface && !strcmp(AUDIO_HEADSET_INTERFACE, interface)
   1301 				&& !dev->headset)
   1302 			continue;
   1303 
   1304 		if (interface && !strcmp(AUDIO_GATEWAY_INTERFACE, interface)
   1305 				&& !dev->gateway)
   1306 			continue;
   1307 
   1308 		if (interface && !strcmp(AUDIO_SINK_INTERFACE, interface)
   1309 				&& !dev->sink)
   1310 			continue;
   1311 
   1312 		if (interface && !strcmp(AUDIO_SOURCE_INTERFACE, interface)
   1313 				&& !dev->source)
   1314 			continue;
   1315 
   1316 		if (interface && !strcmp(AUDIO_CONTROL_INTERFACE, interface)
   1317 				&& !dev->control)
   1318 			continue;
   1319 
   1320 		if (connected && !audio_device_is_active(dev, interface))
   1321 			continue;
   1322 
   1323 		return dev;
   1324 	}
   1325 
   1326 	return NULL;
   1327 }
   1328 
   1329 struct audio_device *manager_get_device(const bdaddr_t *src,
   1330 					const bdaddr_t *dst,
   1331 					gboolean create)
   1332 {
   1333 	struct audio_device *dev;
   1334 	struct btd_adapter *adapter;
   1335 	struct btd_device *device;
   1336 	char addr[18];
   1337 	const char *path;
   1338 
   1339 	dev = manager_find_device(NULL, src, dst, NULL, FALSE);
   1340 	if (dev)
   1341 		return dev;
   1342 
   1343 	if (!create)
   1344 		return NULL;
   1345 
   1346 	ba2str(src, addr);
   1347 
   1348 	adapter = manager_find_adapter(src);
   1349 	if (!adapter) {
   1350 		error("Unable to get a btd_adapter object for %s",
   1351 				addr);
   1352 		return NULL;
   1353 	}
   1354 
   1355 	ba2str(dst, addr);
   1356 
   1357 	device = adapter_get_device(connection, adapter, addr);
   1358 	if (!device) {
   1359 		error("Unable to get btd_device object for %s", addr);
   1360 		return NULL;
   1361 	}
   1362 
   1363 	path = device_get_path(device);
   1364 
   1365 	dev = audio_device_register(connection, device, path, src, dst);
   1366 	if (!dev)
   1367 		return NULL;
   1368 
   1369 	devices = g_slist_append(devices, dev);
   1370 
   1371 	return dev;
   1372 }
   1373 
   1374 gboolean manager_allow_headset_connection(struct audio_device *device)
   1375 {
   1376 	GSList *l;
   1377 	int connected = 0;
   1378 
   1379 	for (l = devices; l != NULL; l = l->next) {
   1380 		struct audio_device *dev = l->data;
   1381 		struct headset *hs = dev->headset;
   1382 
   1383 		if (dev == device)
   1384 			continue;
   1385 
   1386 		if (bacmp(&dev->src, &device->src))
   1387 			continue;
   1388 
   1389 		if (!hs)
   1390 			continue;
   1391 
   1392 		if (headset_get_state(dev) > HEADSET_STATE_DISCONNECTED)
   1393 			connected++;
   1394 
   1395 		if (connected >= max_connected_headsets)
   1396 			return FALSE;
   1397 	}
   1398 
   1399 	return TRUE;
   1400 }
   1401 
   1402 void manager_set_fast_connectable(gboolean enable)
   1403 {
   1404 	GSList *l;
   1405 
   1406 	for (l = adapters; l != NULL; l = l->next) {
   1407 		struct audio_adapter *adapter = l->data;
   1408 
   1409 		if (btd_adapter_set_fast_connectable(adapter->btd_adapter,
   1410 								enable))
   1411 			error("Changing fast connectable for hci%d failed",
   1412 				adapter_get_dev_id(adapter->btd_adapter));
   1413 	}
   1414 }
   1415