Home | History | Annotate | Download | only in audio
      1 /*
      2  *
      3  *  BlueZ - Bluetooth protocol stack for Linux
      4  *
      5  *  Copyright (C) 2006-2007  Nokia Corporation
      6  *  Copyright (C) 2004-2009  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 <sys/socket.h>
     31 #include <sys/un.h>
     32 #include <stdlib.h>
     33 #include <errno.h>
     34 #include <unistd.h>
     35 #include <stdint.h>
     36 
     37 #include <bluetooth/bluetooth.h>
     38 #include <bluetooth/sdp.h>
     39 #include <dbus/dbus.h>
     40 #include <glib.h>
     41 
     42 #include "logging.h"
     43 #include "ipc.h"
     44 #include "device.h"
     45 #include "manager.h"
     46 #include "avdtp.h"
     47 #include "a2dp.h"
     48 #include "headset.h"
     49 #include "sink.h"
     50 #include "gateway.h"
     51 #include "unix.h"
     52 #include "glib-helper.h"
     53 
     54 #define check_nul(str) (str[sizeof(str) - 1] == '\0')
     55 
     56 typedef enum {
     57 	TYPE_NONE,
     58 	TYPE_HEADSET,
     59 	TYPE_GATEWAY,
     60 	TYPE_SINK,
     61 	TYPE_SOURCE
     62 } service_type_t;
     63 
     64 typedef void (*notify_cb_t) (struct audio_device *dev, void *data);
     65 
     66 struct a2dp_data {
     67 	struct avdtp *session;
     68 	struct avdtp_stream *stream;
     69 	struct a2dp_sep *sep;
     70 };
     71 
     72 struct headset_data {
     73 	gboolean locked;
     74 };
     75 
     76 struct unix_client {
     77 	struct audio_device *dev;
     78 	GSList *caps;
     79 	service_type_t type;
     80 	char *interface;
     81 	uint8_t seid;
     82 	union {
     83 		struct a2dp_data a2dp;
     84 		struct headset_data hs;
     85 	} d;
     86 	int sock;
     87 	int lock;
     88 	int data_fd; /* To be deleted once two phase configuration is fully implemented */
     89 	unsigned int req_id;
     90 	unsigned int cb_id;
     91 	gboolean (*cancel) (struct audio_device *dev, unsigned int id);
     92 };
     93 
     94 static GSList *clients = NULL;
     95 
     96 static int unix_sock = -1;
     97 
     98 static void client_free(struct unix_client *client)
     99 {
    100 	debug("client_free(%p)", client);
    101 
    102 	if (client->cancel && client->dev && client->req_id > 0)
    103 		client->cancel(client->dev, client->req_id);
    104 
    105 	if (client->sock >= 0)
    106 		close(client->sock);
    107 
    108 	if (client->caps) {
    109 		g_slist_foreach(client->caps, (GFunc) g_free, NULL);
    110 		g_slist_free(client->caps);
    111 	}
    112 
    113 	g_free(client->interface);
    114 	g_free(client);
    115 }
    116 
    117 /* Pass file descriptor through local domain sockets (AF_LOCAL, formerly
    118  * AF_UNIX) and the sendmsg() system call with the cmsg_type field of a "struct
    119  * cmsghdr" set to SCM_RIGHTS and the data being an integer value equal to the
    120  * handle of the file descriptor to be passed. */
    121 static int unix_sendmsg_fd(int sock, int fd)
    122 {
    123 	char cmsg_b[CMSG_SPACE(sizeof(int))], m = 'm';
    124 	struct cmsghdr *cmsg;
    125 	struct iovec iov = { &m, sizeof(m) };
    126 	struct msghdr msgh;
    127 
    128 	memset(&msgh, 0, sizeof(msgh));
    129 	msgh.msg_iov = &iov;
    130 	msgh.msg_iovlen = 1;
    131 	msgh.msg_control = &cmsg_b;
    132 	msgh.msg_controllen = CMSG_LEN(sizeof(int));
    133 
    134 	cmsg = CMSG_FIRSTHDR(&msgh);
    135 	cmsg->cmsg_level = SOL_SOCKET;
    136 	cmsg->cmsg_type = SCM_RIGHTS;
    137 	cmsg->cmsg_len = CMSG_LEN(sizeof(int));
    138 	/* Initialize the payload */
    139 	memcpy(CMSG_DATA(cmsg), &fd, sizeof(int));
    140 
    141 	return sendmsg(sock, &msgh, MSG_NOSIGNAL);
    142 }
    143 
    144 static void unix_ipc_sendmsg(struct unix_client *client,
    145 					const bt_audio_msg_header_t *msg)
    146 {
    147 	const char *type = bt_audio_strtype(msg->type);
    148 	const char *name = bt_audio_strname(msg->name);
    149 
    150 	debug("Audio API: %s -> %s", type, name);
    151 
    152 	if (send(client->sock, msg, msg->length, 0) < 0)
    153 		error("Error %s(%d)", strerror(errno), errno);
    154 }
    155 
    156 static void unix_ipc_error(struct unix_client *client, uint8_t name, int err)
    157 {
    158 	char buf[BT_SUGGESTED_BUFFER_SIZE];
    159 	bt_audio_error_t *rsp = (void *) buf;
    160 
    161 	if (!g_slist_find(clients, client))
    162 		return;
    163 
    164 	memset(buf, 0, sizeof(buf));
    165 	rsp->h.type = BT_ERROR;
    166 	rsp->h.name = name;
    167 	rsp->h.length = sizeof(*rsp);
    168 
    169 	rsp->posix_errno = err;
    170 
    171 	unix_ipc_sendmsg(client, &rsp->h);
    172 }
    173 
    174 static service_type_t select_service(struct audio_device *dev, const char *interface)
    175 {
    176 	if (!interface) {
    177 		if (dev->sink && avdtp_is_connected(&dev->src, &dev->dst))
    178 			return TYPE_SINK;
    179 		else if (dev->headset && headset_is_active(dev))
    180 			return TYPE_HEADSET;
    181 		else if (dev->sink)
    182 			return TYPE_SINK;
    183 		else if (dev->headset)
    184 			return TYPE_HEADSET;
    185 	} else if (!strcmp(interface, AUDIO_SINK_INTERFACE) && dev->sink)
    186 		return TYPE_SINK;
    187 	else if (!strcmp(interface, AUDIO_HEADSET_INTERFACE) && dev->headset)
    188 		return TYPE_HEADSET;
    189 	else if (!strcmp(interface, AUDIO_GATEWAY_INTERFACE) && dev->gateway)
    190 		return TYPE_GATEWAY;
    191 
    192 	return TYPE_NONE;
    193 }
    194 
    195 static void stream_state_changed(struct avdtp_stream *stream,
    196 					avdtp_state_t old_state,
    197 					avdtp_state_t new_state,
    198 					struct avdtp_error *err,
    199 					void *user_data)
    200 {
    201 	struct unix_client *client = user_data;
    202 	struct a2dp_data *a2dp = &client->d.a2dp;
    203 
    204 	switch (new_state) {
    205 	case AVDTP_STATE_IDLE:
    206 		if (a2dp->sep) {
    207 			a2dp_sep_unlock(a2dp->sep, a2dp->session);
    208 			a2dp->sep = NULL;
    209 		}
    210 		if (a2dp->session) {
    211 			avdtp_unref(a2dp->session);
    212 			a2dp->session = NULL;
    213 		}
    214 		a2dp->stream = NULL;
    215 		client->cb_id = 0;
    216 		break;
    217 	default:
    218 		break;
    219 	}
    220 }
    221 
    222 static uint8_t headset_generate_capability(struct audio_device *dev,
    223 						codec_capabilities_t *codec)
    224 {
    225 	pcm_capabilities_t *pcm;
    226 
    227 	codec->seid = BT_A2DP_SEID_RANGE + 1;
    228 	codec->transport = BT_CAPABILITIES_TRANSPORT_SCO;
    229 	codec->type = BT_HFP_CODEC_PCM;
    230 	codec->length = sizeof(*pcm);
    231 
    232 	pcm = (void *) codec;
    233 	pcm->sampling_rate = 8000;
    234 	if (dev->headset) {
    235 		if (headset_get_nrec(dev))
    236 			pcm->flags |= BT_PCM_FLAG_NREC;
    237 		if (!headset_get_sco_hci(dev))
    238 			pcm->flags |= BT_PCM_FLAG_PCM_ROUTING;
    239 		codec->configured = headset_is_active(dev);
    240 		codec->lock = headset_get_lock(dev);
    241 	} else {
    242 		pcm->flags |= BT_PCM_FLAG_NREC;
    243 		codec->configured = TRUE;
    244 		codec->lock = 0;
    245 	}
    246 
    247 	return codec->length;
    248 }
    249 
    250 static void headset_discovery_complete(struct audio_device *dev, void *user_data)
    251 {
    252 	struct unix_client *client = user_data;
    253 	char buf[BT_SUGGESTED_BUFFER_SIZE];
    254 	struct bt_get_capabilities_rsp *rsp = (void *) buf;
    255 	uint8_t length;
    256 
    257 	client->req_id = 0;
    258 
    259 	if (!dev)
    260 		goto failed;
    261 
    262 	memset(buf, 0, sizeof(buf));
    263 
    264 	length = headset_generate_capability(dev, (void *) rsp->data);
    265 
    266 	rsp->h.type = BT_RESPONSE;
    267 	rsp->h.name = BT_GET_CAPABILITIES;
    268 	rsp->h.length = sizeof(*rsp) + length;
    269 
    270 	ba2str(&dev->src, rsp->source);
    271 	ba2str(&dev->dst, rsp->destination);
    272 	strncpy(rsp->object, dev->path, sizeof(rsp->object));
    273 
    274 	unix_ipc_sendmsg(client, &rsp->h);
    275 
    276 	return;
    277 
    278 failed:
    279 	error("discovery failed");
    280 	unix_ipc_error(client, BT_SET_CONFIGURATION, EIO);
    281 }
    282 
    283 static void headset_setup_complete(struct audio_device *dev, void *user_data)
    284 {
    285 	struct unix_client *client = user_data;
    286 	char buf[BT_SUGGESTED_BUFFER_SIZE];
    287 	struct bt_set_configuration_rsp *rsp = (void *) buf;
    288 
    289 	client->req_id = 0;
    290 
    291 	if (!dev)
    292 		goto failed;
    293 
    294 	memset(buf, 0, sizeof(buf));
    295 
    296 	rsp->h.type = BT_RESPONSE;
    297 	rsp->h.name = BT_SET_CONFIGURATION;
    298 	rsp->h.length = sizeof(*rsp);
    299 
    300 	rsp->link_mtu = 48;
    301 
    302 	client->data_fd = headset_get_sco_fd(dev);
    303 
    304 	unix_ipc_sendmsg(client, &rsp->h);
    305 
    306 	return;
    307 
    308 failed:
    309 	error("config failed");
    310 	unix_ipc_error(client, BT_SET_CONFIGURATION, EIO);
    311 }
    312 
    313 static void gateway_setup_complete(struct audio_device *dev, void *user_data)
    314 {
    315 	struct unix_client *client = user_data;
    316 	char buf[BT_SUGGESTED_BUFFER_SIZE];
    317 	struct bt_set_configuration_rsp *rsp = (void *) buf;
    318 
    319 	if (!dev) {
    320 		unix_ipc_error(client, BT_SET_CONFIGURATION, EIO);
    321 		return;
    322 	}
    323 
    324 	client->req_id = 0;
    325 
    326 	memset(buf, 0, sizeof(buf));
    327 
    328 	rsp->h.type = BT_RESPONSE;
    329 	rsp->h.name = BT_SET_CONFIGURATION;
    330 	rsp->h.length = sizeof(*rsp);
    331 
    332 	rsp->link_mtu = 48;
    333 
    334 	client->data_fd = gateway_get_sco_fd(dev);
    335 
    336 	unix_ipc_sendmsg(client, &rsp->h);
    337 }
    338 
    339 static void headset_resume_complete(struct audio_device *dev, void *user_data)
    340 {
    341 	struct unix_client *client = user_data;
    342 	char buf[BT_SUGGESTED_BUFFER_SIZE];
    343 	struct bt_start_stream_rsp *rsp = (void *) buf;
    344 	struct bt_new_stream_ind *ind = (void *) buf;
    345 
    346 	client->req_id = 0;
    347 
    348 	if (!dev)
    349 		goto failed;
    350 
    351 	client->data_fd = headset_get_sco_fd(dev);
    352 	if (client->data_fd < 0) {
    353 		error("Unable to get a SCO fd");
    354 		goto failed;
    355 	}
    356 
    357 	memset(buf, 0, sizeof(buf));
    358 	rsp->h.type = BT_RESPONSE;
    359 	rsp->h.name = BT_START_STREAM;
    360 	rsp->h.length = sizeof(*rsp);
    361 
    362 	unix_ipc_sendmsg(client, &rsp->h);
    363 
    364 	memset(buf, 0, sizeof(buf));
    365 	ind->h.type = BT_INDICATION;
    366 	ind->h.name = BT_NEW_STREAM;
    367 	ind->h.length = sizeof(*ind);
    368 
    369 	unix_ipc_sendmsg(client, &ind->h);
    370 
    371 	if (unix_sendmsg_fd(client->sock, client->data_fd) < 0) {
    372 		error("unix_sendmsg_fd: %s(%d)", strerror(errno), errno);
    373 		goto failed;
    374 	}
    375 
    376 	return;
    377 
    378 failed:
    379 	error("headset_resume_complete: resume failed");
    380 	unix_ipc_error(client, BT_START_STREAM, EIO);
    381 }
    382 
    383 static void gateway_resume_complete(struct audio_device *dev, void *user_data)
    384 {
    385 	struct unix_client *client = user_data;
    386 	char buf[BT_SUGGESTED_BUFFER_SIZE];
    387 	struct bt_start_stream_rsp *rsp = (void *) buf;
    388 	struct bt_new_stream_ind *ind = (void *) buf;
    389 
    390 	memset(buf, 0, sizeof(buf));
    391 	rsp->h.type = BT_RESPONSE;
    392 	rsp->h.name = BT_START_STREAM;
    393 	rsp->h.length = sizeof(*rsp);
    394 
    395 	unix_ipc_sendmsg(client, &rsp->h);
    396 
    397 	memset(buf, 0, sizeof(buf));
    398 	ind->h.type = BT_INDICATION;
    399 	ind->h.name = BT_NEW_STREAM;
    400 	ind->h.length = sizeof(*ind);
    401 
    402 	unix_ipc_sendmsg(client, &ind->h);
    403 
    404 	client->data_fd = gateway_get_sco_fd(dev);
    405 	if (unix_sendmsg_fd(client->sock, client->data_fd) < 0) {
    406 		error("unix_sendmsg_fd: %s(%d)", strerror(errno), errno);
    407 		unix_ipc_error(client, BT_START_STREAM, EIO);
    408 	}
    409 
    410 	client->req_id = 0;
    411 }
    412 
    413 static void headset_suspend_complete(struct audio_device *dev, void *user_data)
    414 {
    415 	struct unix_client *client = user_data;
    416 	char buf[BT_SUGGESTED_BUFFER_SIZE];
    417 	struct bt_stop_stream_rsp *rsp = (void *) buf;
    418 
    419 	if (!dev)
    420 		goto failed;
    421 
    422 	memset(buf, 0, sizeof(buf));
    423 	rsp->h.type = BT_RESPONSE;
    424 	rsp->h.name = BT_STOP_STREAM;
    425 	rsp->h.length = sizeof(*rsp);
    426 
    427 	unix_ipc_sendmsg(client, &rsp->h);
    428 
    429 	return;
    430 
    431 failed:
    432 	error("suspend failed");
    433 	unix_ipc_error(client, BT_STOP_STREAM, EIO);
    434 }
    435 
    436 static void print_mpeg12(struct mpeg_codec_cap *mpeg)
    437 {
    438 	debug("Media Codec: MPEG12"
    439 		" Channel Modes: %s%s%s%s"
    440 		" Frequencies: %s%s%s%s%s%s"
    441 		" Layers: %s%s%s"
    442 		" CRC: %s",
    443 		mpeg->channel_mode & MPEG_CHANNEL_MODE_MONO ? "Mono " : "",
    444 		mpeg->channel_mode & MPEG_CHANNEL_MODE_DUAL_CHANNEL ?
    445 		"DualChannel " : "",
    446 		mpeg->channel_mode & MPEG_CHANNEL_MODE_STEREO ? "Stereo " : "",
    447 		mpeg->channel_mode & MPEG_CHANNEL_MODE_JOINT_STEREO ?
    448 		"JointStereo " : "",
    449 		mpeg->frequency & MPEG_SAMPLING_FREQ_16000 ? "16Khz " : "",
    450 		mpeg->frequency & MPEG_SAMPLING_FREQ_22050 ? "22.05Khz " : "",
    451 		mpeg->frequency & MPEG_SAMPLING_FREQ_24000 ? "24Khz " : "",
    452 		mpeg->frequency & MPEG_SAMPLING_FREQ_32000 ? "32Khz " : "",
    453 		mpeg->frequency & MPEG_SAMPLING_FREQ_44100 ? "44.1Khz " : "",
    454 		mpeg->frequency & MPEG_SAMPLING_FREQ_48000 ? "48Khz " : "",
    455 		mpeg->layer & MPEG_LAYER_MP1 ? "1 " : "",
    456 		mpeg->layer & MPEG_LAYER_MP2 ? "2 " : "",
    457 		mpeg->layer & MPEG_LAYER_MP3 ? "3 " : "",
    458 		mpeg->crc ? "Yes" : "No");
    459 }
    460 
    461 static void print_sbc(struct sbc_codec_cap *sbc)
    462 {
    463 	debug("Media Codec: SBC"
    464 		" Channel Modes: %s%s%s%s"
    465 		" Frequencies: %s%s%s%s"
    466 		" Subbands: %s%s"
    467 		" Blocks: %s%s%s%s"
    468 		" Bitpool: %d-%d",
    469 		sbc->channel_mode & SBC_CHANNEL_MODE_MONO ? "Mono " : "",
    470 		sbc->channel_mode & SBC_CHANNEL_MODE_DUAL_CHANNEL ?
    471 		"DualChannel " : "",
    472 		sbc->channel_mode & SBC_CHANNEL_MODE_STEREO ? "Stereo " : "",
    473 		sbc->channel_mode & SBC_CHANNEL_MODE_JOINT_STEREO ? "JointStereo" : "",
    474 		sbc->frequency & SBC_SAMPLING_FREQ_16000 ? "16Khz " : "",
    475 		sbc->frequency & SBC_SAMPLING_FREQ_32000 ? "32Khz " : "",
    476 		sbc->frequency & SBC_SAMPLING_FREQ_44100 ? "44.1Khz " : "",
    477 		sbc->frequency & SBC_SAMPLING_FREQ_48000 ? "48Khz " : "",
    478 		sbc->subbands & SBC_SUBBANDS_4 ? "4 " : "",
    479 		sbc->subbands & SBC_SUBBANDS_8 ? "8 " : "",
    480 		sbc->block_length & SBC_BLOCK_LENGTH_4 ? "4 " : "",
    481 		sbc->block_length & SBC_BLOCK_LENGTH_8 ? "8 " : "",
    482 		sbc->block_length & SBC_BLOCK_LENGTH_12 ? "12 " : "",
    483 		sbc->block_length & SBC_BLOCK_LENGTH_16 ? "16 " : "",
    484 		sbc->min_bitpool, sbc->max_bitpool);
    485 }
    486 
    487 static int a2dp_append_codec(struct bt_get_capabilities_rsp *rsp,
    488 				struct avdtp_service_capability *cap,
    489 				uint8_t seid,
    490 				uint8_t configured,
    491 				uint8_t lock)
    492 {
    493 	struct avdtp_media_codec_capability *codec_cap = (void *) cap->data;
    494 	codec_capabilities_t *codec = (void *) rsp + rsp->h.length;
    495 	size_t space_left;
    496 
    497 	if (rsp->h.length > BT_SUGGESTED_BUFFER_SIZE)
    498 		return -ENOMEM;
    499 
    500 	space_left = BT_SUGGESTED_BUFFER_SIZE - rsp->h.length;
    501 
    502 	/* endianess prevent direct cast */
    503 	if (codec_cap->media_codec_type == A2DP_CODEC_SBC) {
    504 		struct sbc_codec_cap *sbc_cap = (void *) codec_cap;
    505 		sbc_capabilities_t *sbc = (void *) codec;
    506 
    507 		if (space_left < sizeof(sbc_capabilities_t))
    508 			return -ENOMEM;
    509 
    510 		codec->length = sizeof(sbc_capabilities_t);
    511 
    512 		sbc->channel_mode = sbc_cap->channel_mode;
    513 		sbc->frequency = sbc_cap->frequency;
    514 		sbc->allocation_method = sbc_cap->allocation_method;
    515 		sbc->subbands = sbc_cap->subbands;
    516 		sbc->block_length = sbc_cap->block_length;
    517 		sbc->min_bitpool = sbc_cap->min_bitpool;
    518 		sbc->max_bitpool = sbc_cap->max_bitpool;
    519 
    520 		print_sbc(sbc_cap);
    521 		codec->type = BT_A2DP_SBC_SINK;
    522 	} else if (codec_cap->media_codec_type == A2DP_CODEC_MPEG12) {
    523 		struct mpeg_codec_cap *mpeg_cap = (void *) codec_cap;
    524 		mpeg_capabilities_t *mpeg = (void *) codec;
    525 
    526 		if (space_left < sizeof(mpeg_capabilities_t))
    527 			return -ENOMEM;
    528 
    529 		codec->length = sizeof(mpeg_capabilities_t);
    530 
    531 		mpeg->channel_mode = mpeg_cap->channel_mode;
    532 		mpeg->crc = mpeg_cap->crc;
    533 		mpeg->layer = mpeg_cap->layer;
    534 		mpeg->frequency = mpeg_cap->frequency;
    535 		mpeg->mpf = mpeg_cap->mpf;
    536 		mpeg->bitrate = mpeg_cap->bitrate;
    537 
    538 		print_mpeg12(mpeg_cap);
    539 		codec->type = BT_A2DP_MPEG12_SINK;
    540 	} else {
    541 		size_t codec_length, type_length, total_length;
    542 
    543 		codec_length = cap->length - (sizeof(struct avdtp_service_capability)
    544 				+ sizeof(struct avdtp_media_codec_capability));
    545 		type_length = sizeof(codec_cap->media_codec_type);
    546 		total_length = type_length + codec_length +
    547 				sizeof(codec_capabilities_t);
    548 
    549 		if (space_left < total_length)
    550 			return -ENOMEM;
    551 
    552 		codec->length = total_length;
    553 		memcpy(codec->data, &codec_cap->media_codec_type, type_length);
    554 		memcpy(codec->data + type_length, codec_cap->data,
    555 			codec_length);
    556 		codec->type = BT_A2DP_UNKNOWN_SINK;
    557 	}
    558 
    559 	codec->seid = seid;
    560 	codec->configured = configured;
    561 	codec->lock = lock;
    562 	rsp->h.length += codec->length;
    563 
    564 	debug("Append %s seid %d - length %d - total %d",
    565 		configured ? "configured" : "", seid, codec->length,
    566 		rsp->h.length);
    567 
    568 	return 0;
    569 }
    570 
    571 static void a2dp_discovery_complete(struct avdtp *session, GSList *seps,
    572 					struct avdtp_error *err,
    573 					void *user_data)
    574 {
    575 	struct unix_client *client = user_data;
    576 	char buf[BT_SUGGESTED_BUFFER_SIZE];
    577 	struct bt_get_capabilities_rsp *rsp = (void *) buf;
    578 	struct a2dp_data *a2dp = &client->d.a2dp;
    579 	GSList *l;
    580 
    581 	if (!g_slist_find(clients, client)) {
    582 		debug("Client disconnected during discovery");
    583 		return;
    584 	}
    585 
    586 	if (err)
    587 		goto failed;
    588 
    589 	memset(buf, 0, sizeof(buf));
    590 	client->req_id = 0;
    591 
    592 	rsp->h.type = BT_RESPONSE;
    593 	rsp->h.name = BT_GET_CAPABILITIES;
    594 	rsp->h.length = sizeof(*rsp);
    595 	ba2str(&client->dev->src, rsp->source);
    596 	ba2str(&client->dev->dst, rsp->destination);
    597 	strncpy(rsp->object, client->dev->path, sizeof(rsp->object));
    598 
    599 	for (l = seps; l; l = g_slist_next(l)) {
    600 		struct avdtp_remote_sep *rsep = l->data;
    601 		struct a2dp_sep *sep;
    602 		struct avdtp_service_capability *cap;
    603 		struct avdtp_stream *stream;
    604 		uint8_t type, seid, configured = 0, lock = 0;
    605 		GSList *cl;
    606 
    607 		type = avdtp_get_type(rsep);
    608 
    609 		if (type != AVDTP_SEP_TYPE_SINK)
    610 			continue;
    611 
    612 		cap = avdtp_get_codec(rsep);
    613 
    614 		if (cap->category != AVDTP_MEDIA_CODEC)
    615 			continue;
    616 
    617 		seid = avdtp_get_seid(rsep);
    618 
    619 		if (client->seid != 0 && client->seid != seid)
    620 			continue;
    621 
    622 		stream = avdtp_get_stream(rsep);
    623 		if (stream) {
    624 			configured = 1;
    625 			if (client->seid == seid)
    626 				cap = avdtp_stream_get_codec(stream);
    627 		}
    628 
    629 		for (cl = clients; cl; cl = cl->next) {
    630 			struct unix_client *c = cl->data;
    631 			struct a2dp_data *ca2dp = &c->d.a2dp;
    632 
    633 			if (ca2dp && ca2dp->session == session &&
    634 					c->seid == seid) {
    635 				lock = c->lock;
    636 				break;
    637 			}
    638 		}
    639 
    640 		sep = a2dp_get_sep(session, stream);
    641 		if (sep && a2dp_sep_get_lock(sep))
    642 			lock = BT_WRITE_LOCK;
    643 
    644 		a2dp_append_codec(rsp, cap, seid, configured, lock);
    645 	}
    646 
    647 	unix_ipc_sendmsg(client, &rsp->h);
    648 
    649 	return;
    650 
    651 failed:
    652 	error("discovery failed");
    653 	unix_ipc_error(client, BT_GET_CAPABILITIES, EIO);
    654 
    655 	if (a2dp->sep) {
    656 		a2dp_sep_unlock(a2dp->sep, a2dp->session);
    657 		a2dp->sep = NULL;
    658 	}
    659 
    660 	avdtp_unref(a2dp->session);
    661 	a2dp->session = NULL;
    662 	a2dp->stream = NULL;
    663 }
    664 
    665 static void a2dp_config_complete(struct avdtp *session, struct a2dp_sep *sep,
    666 					struct avdtp_stream *stream,
    667 					struct avdtp_error *err,
    668 					void *user_data)
    669 {
    670 	struct unix_client *client = user_data;
    671 	char buf[BT_SUGGESTED_BUFFER_SIZE];
    672 	struct bt_set_configuration_rsp *rsp = (void *) buf;
    673 	struct a2dp_data *a2dp = &client->d.a2dp;
    674 	uint16_t imtu, omtu;
    675 	GSList *caps;
    676 
    677 	client->req_id = 0;
    678 
    679 	if (err)
    680 		goto failed;
    681 
    682 	memset(buf, 0, sizeof(buf));
    683 
    684 	if (!stream)
    685 		goto failed;
    686 
    687 	if (client->cb_id > 0)
    688 		avdtp_stream_remove_cb(a2dp->session, a2dp->stream,
    689 								client->cb_id);
    690 
    691 	a2dp->sep = sep;
    692 	a2dp->stream = stream;
    693 
    694 	if (!avdtp_stream_get_transport(stream, &client->data_fd, &imtu, &omtu,
    695 					&caps)) {
    696 		error("Unable to get stream transport");
    697 		goto failed;
    698 	}
    699 
    700 	rsp->h.type = BT_RESPONSE;
    701 	rsp->h.name = BT_SET_CONFIGURATION;
    702 	rsp->h.length = sizeof(*rsp);
    703 
    704 	/* FIXME: Use imtu when fd_opt is CFG_FD_OPT_READ */
    705 	rsp->link_mtu = omtu;
    706 
    707 	unix_ipc_sendmsg(client, &rsp->h);
    708 
    709 	client->cb_id = avdtp_stream_add_cb(session, stream,
    710 						stream_state_changed, client);
    711 
    712 	return;
    713 
    714 failed:
    715 	error("config failed");
    716 
    717 	unix_ipc_error(client, BT_SET_CONFIGURATION, EIO);
    718 
    719 	avdtp_unref(a2dp->session);
    720 
    721 	a2dp->session = NULL;
    722 	a2dp->stream = NULL;
    723 	a2dp->sep = NULL;
    724 }
    725 
    726 static void a2dp_resume_complete(struct avdtp *session,
    727 				struct avdtp_error *err, void *user_data)
    728 {
    729 	struct unix_client *client = user_data;
    730 	char buf[BT_SUGGESTED_BUFFER_SIZE];
    731 	struct bt_start_stream_rsp *rsp = (void *) buf;
    732 	struct bt_new_stream_ind *ind = (void *) buf;
    733 	struct a2dp_data *a2dp = &client->d.a2dp;
    734 
    735 	if (err)
    736 		goto failed;
    737 
    738 	memset(buf, 0, sizeof(buf));
    739 	rsp->h.type = BT_RESPONSE;
    740 	rsp->h.name = BT_START_STREAM;
    741 	rsp->h.length = sizeof(*rsp);
    742 
    743 	unix_ipc_sendmsg(client, &rsp->h);
    744 
    745 	memset(buf, 0, sizeof(buf));
    746 	ind->h.type = BT_RESPONSE;
    747 	ind->h.name = BT_NEW_STREAM;
    748 	rsp->h.length = sizeof(*ind);
    749 
    750 	unix_ipc_sendmsg(client, &ind->h);
    751 
    752 	if (unix_sendmsg_fd(client->sock, client->data_fd) < 0) {
    753 		error("unix_sendmsg_fd: %s(%d)", strerror(errno), errno);
    754 		goto failed;
    755 	}
    756 
    757 	return;
    758 
    759 failed:
    760 	error("resume failed");
    761 
    762 	unix_ipc_error(client, BT_START_STREAM, EIO);
    763 
    764 	if (client->cb_id > 0) {
    765 		avdtp_stream_remove_cb(a2dp->session, a2dp->stream,
    766 					client->cb_id);
    767 		client->cb_id = 0;
    768 	}
    769 
    770 	if (a2dp->sep) {
    771 		a2dp_sep_unlock(a2dp->sep, a2dp->session);
    772 		a2dp->sep = NULL;
    773 	}
    774 
    775 	avdtp_unref(a2dp->session);
    776 	a2dp->session = NULL;
    777 	a2dp->stream = NULL;
    778 }
    779 
    780 static void a2dp_suspend_complete(struct avdtp *session,
    781 				struct avdtp_error *err, void *user_data)
    782 {
    783 	struct unix_client *client = user_data;
    784 	char buf[BT_SUGGESTED_BUFFER_SIZE];
    785 	struct bt_stop_stream_rsp *rsp = (void *) buf;
    786 	struct a2dp_data *a2dp = &client->d.a2dp;
    787 
    788 	if (err)
    789 		goto failed;
    790 
    791 	memset(buf, 0, sizeof(buf));
    792 	rsp->h.type = BT_RESPONSE;
    793 	rsp->h.name = BT_STOP_STREAM;
    794 	rsp->h.length = sizeof(*rsp);
    795 
    796 	unix_ipc_sendmsg(client, &rsp->h);
    797 
    798 	return;
    799 
    800 failed:
    801 	error("suspend failed");
    802 
    803 	unix_ipc_error(client, BT_STOP_STREAM, EIO);
    804 
    805 	if (a2dp->sep) {
    806 		a2dp_sep_unlock(a2dp->sep, a2dp->session);
    807 		a2dp->sep = NULL;
    808 	}
    809 
    810 	avdtp_unref(a2dp->session);
    811 	a2dp->session = NULL;
    812 	a2dp->stream = NULL;
    813 }
    814 
    815 static void start_discovery(struct audio_device *dev, struct unix_client *client)
    816 {
    817 	struct a2dp_data *a2dp;
    818 	int err = 0;
    819 
    820 	switch (client->type) {
    821 	case TYPE_SINK:
    822 		a2dp = &client->d.a2dp;
    823 
    824 		if (!a2dp->session)
    825 			a2dp->session = avdtp_get(&dev->src, &dev->dst);
    826 
    827 		if (!a2dp->session) {
    828 			error("Unable to get a session");
    829 			goto failed;
    830 		}
    831 
    832 		err = avdtp_discover(a2dp->session, a2dp_discovery_complete,
    833 					client);
    834 		if (err) {
    835 			if (a2dp->session) {
    836 				avdtp_unref(a2dp->session);
    837 				a2dp->session = NULL;
    838 			}
    839 			goto failed;
    840 		}
    841 		break;
    842 
    843 	case TYPE_HEADSET:
    844 	case TYPE_GATEWAY:
    845 		headset_discovery_complete(dev, client);
    846 		break;
    847 
    848 	default:
    849 		error("No known services for device");
    850 		goto failed;
    851 	}
    852 
    853 	client->dev = dev;
    854 
    855 	return;
    856 
    857 failed:
    858 	unix_ipc_error(client, BT_GET_CAPABILITIES, err ? : EIO);
    859 }
    860 
    861 static void open_complete(struct audio_device *dev, void *user_data)
    862 {
    863 	struct unix_client *client = user_data;
    864 	char buf[BT_SUGGESTED_BUFFER_SIZE];
    865 	struct bt_open_rsp *rsp = (void *) buf;
    866 
    867 	memset(buf, 0, sizeof(buf));
    868 
    869 	rsp->h.type = BT_RESPONSE;
    870 	rsp->h.name = BT_OPEN;
    871 	rsp->h.length = sizeof(*rsp);
    872 
    873 	ba2str(&dev->src, rsp->source);
    874 	ba2str(&dev->dst, rsp->destination);
    875 	strncpy(rsp->object, dev->path, sizeof(rsp->object));
    876 
    877 	unix_ipc_sendmsg(client, &rsp->h);
    878 
    879 	return;
    880 }
    881 
    882 static void start_open(struct audio_device *dev, struct unix_client *client)
    883 {
    884 	struct a2dp_data *a2dp;
    885 	struct headset_data *hs;
    886 	struct avdtp_remote_sep *rsep;
    887 	gboolean unref_avdtp_on_fail = FALSE;
    888 
    889 	switch (client->type) {
    890 	case TYPE_SINK:
    891 	case TYPE_SOURCE:
    892 		a2dp = &client->d.a2dp;
    893 
    894 		if (!a2dp->session) {
    895 			a2dp->session = avdtp_get(&dev->src, &dev->dst);
    896 			unref_avdtp_on_fail = TRUE;
    897 		}
    898 
    899 		if (!a2dp->session) {
    900 			error("Unable to get a session");
    901 			goto failed;
    902 		}
    903 
    904 		if (a2dp->sep) {
    905 			error("Client already has an opened session");
    906 			goto failed;
    907 		}
    908 
    909 		rsep = avdtp_get_remote_sep(a2dp->session, client->seid);
    910 		if (!rsep) {
    911 			error("Invalid seid %d", client->seid);
    912 			goto failed;
    913 		}
    914 
    915 		a2dp->sep = a2dp_get(a2dp->session, rsep);
    916 		if (!a2dp->sep) {
    917 			error("seid %d not available or locked", client->seid);
    918 			goto failed;
    919 		}
    920 
    921 		if (!a2dp_sep_lock(a2dp->sep, a2dp->session)) {
    922 			error("Unable to open seid %d", client->seid);
    923 			a2dp->sep = NULL;
    924 			goto failed;
    925 		}
    926 
    927 		break;
    928 
    929 	case TYPE_HEADSET:
    930 		hs = &client->d.hs;
    931 
    932 		if (hs->locked) {
    933 			error("Client already has an opened session");
    934 			goto failed;
    935 		}
    936 
    937 		hs->locked = headset_lock(dev, client->lock);
    938 		if (!hs->locked) {
    939 			error("Unable to open seid %d", client->seid);
    940 			goto failed;
    941 		}
    942 		break;
    943 
    944         case TYPE_GATEWAY:
    945                 break;
    946 	default:
    947 		error("No known services for device");
    948 		goto failed;
    949 	}
    950 
    951 	client->dev = dev;
    952 
    953 	open_complete(dev, client);
    954 
    955 	return;
    956 
    957 failed:
    958 	if (unref_avdtp_on_fail && a2dp->session) {
    959 		avdtp_unref(a2dp->session);
    960 		a2dp->session = NULL;
    961 	}
    962 	unix_ipc_error(client, BT_OPEN, EINVAL);
    963 }
    964 
    965 static void start_config(struct audio_device *dev, struct unix_client *client)
    966 {
    967 	struct a2dp_data *a2dp;
    968 	struct headset_data *hs;
    969 	unsigned int id;
    970 
    971 	switch (client->type) {
    972 	case TYPE_SINK:
    973 	case TYPE_SOURCE:
    974 		a2dp = &client->d.a2dp;
    975 
    976 		if (!a2dp->session)
    977 			a2dp->session = avdtp_get(&dev->src, &dev->dst);
    978 
    979 		if (!a2dp->session) {
    980 			error("Unable to get a session");
    981 			goto failed;
    982 		}
    983 
    984 		if (!a2dp->sep) {
    985 			error("seid %d not opened", client->seid);
    986 			goto failed;
    987 		}
    988 
    989 		id = a2dp_config(a2dp->session, a2dp->sep, a2dp_config_complete,
    990 					client->caps, client);
    991 		client->cancel = a2dp_cancel;
    992 		break;
    993 
    994 	case TYPE_HEADSET:
    995 		hs = &client->d.hs;
    996 
    997 		if (!hs->locked) {
    998 			error("seid %d not opened", client->seid);
    999 			goto failed;
   1000 		}
   1001 
   1002 		id = headset_config_stream(dev, TRUE, headset_setup_complete,
   1003 						client);
   1004 		client->cancel = headset_cancel_stream;
   1005 		break;
   1006 	case TYPE_GATEWAY:
   1007 		if (gateway_config_stream(dev, gateway_setup_complete, client) >= 0) {
   1008 			client->cancel = gateway_cancel_stream;
   1009 			id = 1;
   1010 		} else
   1011 			id = 0;
   1012 		break;
   1013 
   1014 	default:
   1015 		error("No known services for device");
   1016 		goto failed;
   1017 	}
   1018 
   1019 	if (id == 0) {
   1020 		error("config failed");
   1021 		goto failed;
   1022 	}
   1023 
   1024 	client->req_id = id;
   1025 
   1026 	return;
   1027 
   1028 failed:
   1029 	unix_ipc_error(client, BT_SET_CONFIGURATION, EIO);
   1030 }
   1031 
   1032 static void start_resume(struct audio_device *dev, struct unix_client *client)
   1033 {
   1034 	struct a2dp_data *a2dp;
   1035 	struct headset_data *hs;
   1036 	unsigned int id;
   1037 	gboolean unref_avdtp_on_fail = FALSE;
   1038 
   1039 	switch (client->type) {
   1040 	case TYPE_SINK:
   1041 	case TYPE_SOURCE:
   1042 		a2dp = &client->d.a2dp;
   1043 
   1044 		if (!a2dp->session) {
   1045 			a2dp->session = avdtp_get(&dev->src, &dev->dst);
   1046 			unref_avdtp_on_fail = TRUE;
   1047 		}
   1048 
   1049 		if (!a2dp->session) {
   1050 			error("Unable to get a session");
   1051 			goto failed;
   1052 		}
   1053 
   1054 		if (!a2dp->sep) {
   1055 			error("seid not opened");
   1056 			goto failed;
   1057 		}
   1058 
   1059 		id = a2dp_resume(a2dp->session, a2dp->sep, a2dp_resume_complete,
   1060 					client);
   1061 		client->cancel = a2dp_cancel;
   1062 
   1063 		break;
   1064 
   1065 	case TYPE_HEADSET:
   1066 		hs = &client->d.hs;
   1067 
   1068 		if (!hs->locked) {
   1069 			error("seid not opened");
   1070 			goto failed;
   1071 		}
   1072 
   1073 		id = headset_request_stream(dev, headset_resume_complete,
   1074 						client);
   1075 		client->cancel = headset_cancel_stream;
   1076 		break;
   1077 
   1078 	case TYPE_GATEWAY:
   1079 		if (gateway_request_stream(dev, gateway_resume_complete, client))
   1080 			id = 1;
   1081 		else
   1082 			id = 0;
   1083 		client->cancel = gateway_cancel_stream;
   1084 		break;
   1085 
   1086 	default:
   1087 		error("No known services for device");
   1088 		goto failed;
   1089 	}
   1090 
   1091 	if (id == 0) {
   1092 		error("start_resume: resume failed");
   1093 		goto failed;
   1094 	}
   1095 
   1096 	client->req_id = id;
   1097 
   1098 	return;
   1099 
   1100 failed:
   1101 	if (unref_avdtp_on_fail && a2dp->session) {
   1102 		avdtp_unref(a2dp->session);
   1103 		a2dp->session = NULL;
   1104 	}
   1105 	unix_ipc_error(client, BT_START_STREAM, EIO);
   1106 }
   1107 
   1108 static void start_suspend(struct audio_device *dev, struct unix_client *client)
   1109 {
   1110 	struct a2dp_data *a2dp;
   1111 	struct headset_data *hs;
   1112 	unsigned int id;
   1113 	gboolean unref_avdtp_on_fail = FALSE;
   1114 
   1115 	switch (client->type) {
   1116 	case TYPE_SINK:
   1117 	case TYPE_SOURCE:
   1118 		a2dp = &client->d.a2dp;
   1119 
   1120 		if (!a2dp->session) {
   1121 			a2dp->session = avdtp_get(&dev->src, &dev->dst);
   1122 			unref_avdtp_on_fail = TRUE;
   1123 		}
   1124 
   1125 		if (!a2dp->session) {
   1126 			error("Unable to get a session");
   1127 			goto failed;
   1128 		}
   1129 
   1130 		if (!a2dp->sep) {
   1131 			error("Unable to get a sep");
   1132 			goto failed;
   1133 		}
   1134 
   1135 		id = a2dp_suspend(a2dp->session, a2dp->sep,
   1136 					a2dp_suspend_complete, client);
   1137 		client->cancel = a2dp_cancel;
   1138 		break;
   1139 
   1140 	case TYPE_HEADSET:
   1141 		hs = &client->d.hs;
   1142 
   1143 		if (!hs->locked) {
   1144 			error("seid not opened");
   1145 			goto failed;
   1146 		}
   1147 
   1148 		id = headset_suspend_stream(dev, headset_suspend_complete,
   1149 						client);
   1150 		client->cancel = headset_cancel_stream;
   1151 		break;
   1152 
   1153 	case TYPE_GATEWAY:
   1154 		gateway_suspend_stream(dev);
   1155 		client->cancel = gateway_cancel_stream;
   1156 		headset_suspend_complete(dev, client);
   1157 		id = 1;
   1158 		break;
   1159 
   1160 	default:
   1161 		error("No known services for device");
   1162 		goto failed;
   1163 	}
   1164 
   1165 	if (id == 0) {
   1166 		error("suspend failed");
   1167 		goto failed;
   1168 	}
   1169 
   1170 	return;
   1171 
   1172 failed:
   1173 	if (unref_avdtp_on_fail && a2dp->session) {
   1174 		avdtp_unref(a2dp->session);
   1175 		a2dp->session = NULL;
   1176 	}
   1177 	unix_ipc_error(client, BT_STOP_STREAM, EIO);
   1178 }
   1179 
   1180 static void close_complete(struct audio_device *dev, void *user_data)
   1181 {
   1182 	struct unix_client *client = user_data;
   1183 	char buf[BT_SUGGESTED_BUFFER_SIZE];
   1184 	struct bt_close_rsp *rsp = (void *) buf;
   1185 
   1186 	memset(buf, 0, sizeof(buf));
   1187 
   1188 	rsp->h.type = BT_RESPONSE;
   1189 	rsp->h.name = BT_CLOSE;
   1190 	rsp->h.length = sizeof(*rsp);
   1191 
   1192 	unix_ipc_sendmsg(client, &rsp->h);
   1193 
   1194 	return;
   1195 }
   1196 
   1197 static void start_close(struct audio_device *dev, struct unix_client *client,
   1198 			gboolean reply)
   1199 {
   1200 	struct a2dp_data *a2dp;
   1201 	struct headset_data *hs;
   1202 
   1203 	if (!client->dev)
   1204 		goto failed;
   1205 
   1206 	switch (client->type) {
   1207 	case TYPE_HEADSET:
   1208 		hs = &client->d.hs;
   1209 
   1210 		if (client->dev && hs->locked) {
   1211 			headset_unlock(client->dev, client->lock);
   1212 			hs->locked = FALSE;
   1213 		}
   1214 		break;
   1215         case TYPE_GATEWAY:
   1216                 break;
   1217 	case TYPE_SOURCE:
   1218 	case TYPE_SINK:
   1219 		a2dp = &client->d.a2dp;
   1220 
   1221 		if (client->cb_id > 0)
   1222 			avdtp_stream_remove_cb(a2dp->session, a2dp->stream,
   1223 								client->cb_id);
   1224 		if (a2dp->sep) {
   1225 			a2dp_sep_unlock(a2dp->sep, a2dp->session);
   1226 			a2dp->sep = NULL;
   1227 		}
   1228 		if (a2dp->session) {
   1229 			avdtp_unref(a2dp->session);
   1230 			a2dp->session = NULL;
   1231 		}
   1232 		break;
   1233 	default:
   1234 		error("No known services for device");
   1235 		goto failed;
   1236 	}
   1237 
   1238 	if (!reply)
   1239 		return;
   1240 
   1241 	close_complete(dev, client);
   1242 	client->dev = NULL;
   1243 
   1244 	return;
   1245 
   1246 failed:
   1247 	if (reply)
   1248 		unix_ipc_error(client, BT_STOP_STREAM, EINVAL);
   1249 }
   1250 
   1251 static void handle_getcapabilities_req(struct unix_client *client,
   1252 					struct bt_get_capabilities_req *req)
   1253 {
   1254 	struct audio_device *dev;
   1255 	bdaddr_t src, dst;
   1256 	int err = EIO;
   1257 
   1258 	if (!check_nul(req->source) || !check_nul(req->destination) ||
   1259 			!check_nul(req->object)) {
   1260 		err = EINVAL;
   1261 		goto failed;
   1262 	}
   1263 
   1264 	str2ba(req->source, &src);
   1265 	str2ba(req->destination, &dst);
   1266 
   1267 	if (req->transport == BT_CAPABILITIES_TRANSPORT_SCO)
   1268 		client->interface = g_strdup(AUDIO_HEADSET_INTERFACE);
   1269 	else if (req->transport == BT_CAPABILITIES_TRANSPORT_A2DP)
   1270 		client->interface = g_strdup(AUDIO_SINK_INTERFACE);
   1271 
   1272 	if (!manager_find_device(req->object, &src, &dst, NULL, FALSE))
   1273 		goto failed;
   1274 
   1275 	dev = manager_find_device(req->object, &src, &dst, client->interface,
   1276 				TRUE);
   1277 	if (!dev && (req->flags & BT_FLAG_AUTOCONNECT))
   1278 		dev = manager_find_device(req->object, &src, &dst,
   1279 					client->interface, FALSE);
   1280 
   1281 	if (!dev && req->transport == BT_CAPABILITIES_TRANSPORT_SCO) {
   1282 		g_free(client->interface);
   1283 		client->interface = g_strdup(AUDIO_GATEWAY_INTERFACE);
   1284 
   1285 		dev = manager_find_device(req->object, &src, &dst,
   1286 				client->interface, TRUE);
   1287 		if (!dev && (req->flags & BT_FLAG_AUTOCONNECT))
   1288 			dev = manager_find_device(req->object, &src, &dst,
   1289 					client->interface, FALSE);
   1290 	}
   1291 
   1292 	if (!dev) {
   1293 		error("Unable to find a matching device");
   1294 		goto failed;
   1295 	}
   1296 
   1297 	client->type = select_service(dev, client->interface);
   1298 	if (client->type == TYPE_NONE) {
   1299 		error("No matching service found");
   1300 		goto failed;
   1301 	}
   1302 
   1303 	client->seid = req->seid;
   1304 
   1305 	start_discovery(dev, client);
   1306 
   1307 	return;
   1308 
   1309 failed:
   1310 	unix_ipc_error(client, BT_GET_CAPABILITIES, err);
   1311 }
   1312 
   1313 static int handle_sco_open(struct unix_client *client, struct bt_open_req *req)
   1314 {
   1315 	if (!client->interface)
   1316 		client->interface = g_strdup(AUDIO_HEADSET_INTERFACE);
   1317 	else if (!g_str_equal(client->interface, AUDIO_HEADSET_INTERFACE) &&
   1318 		!g_str_equal(client->interface, AUDIO_GATEWAY_INTERFACE))
   1319 		return -EIO;
   1320 
   1321 	debug("open sco - object=%s source=%s destination=%s lock=%s%s",
   1322 			strcmp(req->object, "") ? req->object : "ANY",
   1323 			strcmp(req->source, "") ? req->source : "ANY",
   1324 			strcmp(req->destination, "") ? req->destination : "ANY",
   1325 			req->lock & BT_READ_LOCK ? "read" : "",
   1326 			req->lock & BT_WRITE_LOCK ? "write" : "");
   1327 
   1328 	return 0;
   1329 }
   1330 
   1331 static int handle_a2dp_open(struct unix_client *client, struct bt_open_req *req)
   1332 {
   1333 	if (!client->interface)
   1334 		client->interface = g_strdup(AUDIO_SINK_INTERFACE);
   1335 	else if (!g_str_equal(client->interface, AUDIO_SINK_INTERFACE))
   1336 		return -EIO;
   1337 
   1338 	debug("open a2dp - object=%s source=%s destination=%s lock=%s%s",
   1339 			strcmp(req->object, "") ? req->object : "ANY",
   1340 			strcmp(req->source, "") ? req->source : "ANY",
   1341 			strcmp(req->destination, "") ? req->destination : "ANY",
   1342 			req->lock & BT_READ_LOCK ? "read" : "",
   1343 			req->lock & BT_WRITE_LOCK ? "write" : "");
   1344 
   1345 	return 0;
   1346 }
   1347 
   1348 static void handle_open_req(struct unix_client *client, struct bt_open_req *req)
   1349 {
   1350 	struct audio_device *dev;
   1351 	bdaddr_t src, dst;
   1352 	int err = 0;
   1353 
   1354 	if (!check_nul(req->source) || !check_nul(req->destination) ||
   1355 			!check_nul(req->object)) {
   1356 		err = EINVAL;
   1357 		goto failed;
   1358 	}
   1359 
   1360 	str2ba(req->source, &src);
   1361 	str2ba(req->destination, &dst);
   1362 
   1363 	if (req->seid > BT_A2DP_SEID_RANGE) {
   1364 		err = handle_sco_open(client, req);
   1365 		if (err < 0) {
   1366 			err = -err;
   1367 			goto failed;
   1368 		}
   1369 	} else {
   1370 		err = handle_a2dp_open(client, req);
   1371 		if (err < 0) {
   1372 			err = -err;
   1373 			goto failed;
   1374 		}
   1375 	}
   1376 
   1377 	if (!manager_find_device(req->object, &src, &dst, NULL, FALSE))
   1378 		goto failed;
   1379 
   1380 	dev = manager_find_device(req->object, &src, &dst, client->interface,
   1381 				TRUE);
   1382 	if (!dev)
   1383 		dev = manager_find_device(req->object, &src, &dst,
   1384 					client->interface, FALSE);
   1385 
   1386 	if (!dev)
   1387 		goto failed;
   1388 
   1389 	client->seid = req->seid;
   1390 	client->lock = req->lock;
   1391 
   1392 	start_open(dev, client);
   1393 
   1394 	return;
   1395 
   1396 failed:
   1397 	unix_ipc_error(client, BT_OPEN, err ? : EIO);
   1398 }
   1399 
   1400 static int handle_sco_transport(struct unix_client *client,
   1401 				struct bt_set_configuration_req *req)
   1402 {
   1403 	struct audio_device *dev = client->dev;
   1404 
   1405 	if (!client->interface) {
   1406 		if (dev->headset)
   1407 			client->interface = g_strdup(AUDIO_HEADSET_INTERFACE);
   1408 		else if (dev->gateway)
   1409 			client->interface = g_strdup(AUDIO_GATEWAY_INTERFACE);
   1410 		else
   1411 			return -EIO;
   1412 	} else if (!g_str_equal(client->interface, AUDIO_HEADSET_INTERFACE) &&
   1413 			!g_str_equal(client->interface, AUDIO_GATEWAY_INTERFACE))
   1414 		return -EIO;
   1415 
   1416 	return 0;
   1417 }
   1418 
   1419 static int handle_a2dp_transport(struct unix_client *client,
   1420 				struct bt_set_configuration_req *req)
   1421 {
   1422 	struct avdtp_service_capability *media_transport, *media_codec;
   1423 	struct sbc_codec_cap sbc_cap;
   1424 	struct mpeg_codec_cap mpeg_cap;
   1425 
   1426 	if (!client->interface)
   1427 		client->interface = g_strdup(AUDIO_SINK_INTERFACE);
   1428 	else if (!g_str_equal(client->interface, AUDIO_SINK_INTERFACE))
   1429 		return -EIO;
   1430 
   1431 	if (client->caps) {
   1432 		g_slist_foreach(client->caps, (GFunc) g_free, NULL);
   1433 		g_slist_free(client->caps);
   1434 		client->caps = NULL;
   1435 	}
   1436 
   1437 	media_transport = avdtp_service_cap_new(AVDTP_MEDIA_TRANSPORT,
   1438 						NULL, 0);
   1439 
   1440 	client->caps = g_slist_append(client->caps, media_transport);
   1441 
   1442 	if (req->codec.type == BT_A2DP_MPEG12_SINK) {
   1443 		mpeg_capabilities_t *mpeg = (void *) &req->codec;
   1444 
   1445 		memset(&mpeg_cap, 0, sizeof(mpeg_cap));
   1446 
   1447 		mpeg_cap.cap.media_type = AVDTP_MEDIA_TYPE_AUDIO;
   1448 		mpeg_cap.cap.media_codec_type = A2DP_CODEC_MPEG12;
   1449 		mpeg_cap.channel_mode = mpeg->channel_mode;
   1450 		mpeg_cap.crc = mpeg->crc;
   1451 		mpeg_cap.layer = mpeg->layer;
   1452 		mpeg_cap.frequency = mpeg->frequency;
   1453 		mpeg_cap.mpf = mpeg->mpf;
   1454 		mpeg_cap.bitrate = mpeg->bitrate;
   1455 
   1456 		media_codec = avdtp_service_cap_new(AVDTP_MEDIA_CODEC, &mpeg_cap,
   1457 							sizeof(mpeg_cap));
   1458 
   1459 		print_mpeg12(&mpeg_cap);
   1460 	} else if (req->codec.type == BT_A2DP_SBC_SINK) {
   1461 		sbc_capabilities_t *sbc = (void *) &req->codec;
   1462 
   1463 		memset(&sbc_cap, 0, sizeof(sbc_cap));
   1464 
   1465 		sbc_cap.cap.media_type = AVDTP_MEDIA_TYPE_AUDIO;
   1466 		sbc_cap.cap.media_codec_type = A2DP_CODEC_SBC;
   1467 		sbc_cap.channel_mode = sbc->channel_mode;
   1468 		sbc_cap.frequency = sbc->frequency;
   1469 		sbc_cap.allocation_method = sbc->allocation_method;
   1470 		sbc_cap.subbands = sbc->subbands;
   1471 		sbc_cap.block_length = sbc->block_length;
   1472 		sbc_cap.min_bitpool = sbc->min_bitpool;
   1473 		sbc_cap.max_bitpool = sbc->max_bitpool;
   1474 
   1475 		media_codec = avdtp_service_cap_new(AVDTP_MEDIA_CODEC, &sbc_cap,
   1476 							sizeof(sbc_cap));
   1477 
   1478 		print_sbc(&sbc_cap);
   1479 	} else
   1480 		return -EINVAL;
   1481 
   1482 	client->caps = g_slist_append(client->caps, media_codec);
   1483 
   1484 	return 0;
   1485 }
   1486 
   1487 static void handle_setconfiguration_req(struct unix_client *client,
   1488 					struct bt_set_configuration_req *req)
   1489 {
   1490 	int err = 0;
   1491 
   1492 	if (req->codec.seid != client->seid) {
   1493 		error("Unable to set configuration: seid %d not opened",
   1494 				client->seid);
   1495 		goto failed;
   1496 	}
   1497 
   1498 	if (!client->dev)
   1499 		goto failed;
   1500 
   1501 	if (req->codec.transport == BT_CAPABILITIES_TRANSPORT_SCO) {
   1502 		err = handle_sco_transport(client, req);
   1503 		if (err < 0) {
   1504 			err = -err;
   1505 			goto failed;
   1506 		}
   1507 	} else if (req->codec.transport == BT_CAPABILITIES_TRANSPORT_A2DP) {
   1508 		err = handle_a2dp_transport(client, req);
   1509 		if (err < 0) {
   1510 			err = -err;
   1511 			goto failed;
   1512 		}
   1513 	}
   1514 
   1515 	start_config(client->dev, client);
   1516 
   1517 	return;
   1518 
   1519 failed:
   1520 	unix_ipc_error(client, BT_SET_CONFIGURATION, err ? : EIO);
   1521 }
   1522 
   1523 static void handle_streamstart_req(struct unix_client *client,
   1524 					struct bt_start_stream_req *req)
   1525 {
   1526 	if (!client->dev)
   1527 		goto failed;
   1528 
   1529 	start_resume(client->dev, client);
   1530 
   1531 	return;
   1532 
   1533 failed:
   1534 	unix_ipc_error(client, BT_START_STREAM, EIO);
   1535 }
   1536 
   1537 static void handle_streamstop_req(struct unix_client *client,
   1538 					struct bt_stop_stream_req *req)
   1539 {
   1540 	if (!client->dev)
   1541 		goto failed;
   1542 
   1543 	start_suspend(client->dev, client);
   1544 
   1545 	return;
   1546 
   1547 failed:
   1548 	unix_ipc_error(client, BT_STOP_STREAM, EIO);
   1549 }
   1550 
   1551 static void handle_close_req(struct unix_client *client,
   1552 				struct bt_close_req *req)
   1553 {
   1554 	if (!client->dev)
   1555 		goto failed;
   1556 
   1557 	start_close(client->dev, client, TRUE);
   1558 
   1559 	return;
   1560 
   1561 failed:
   1562 	unix_ipc_error(client, BT_CLOSE, EIO);
   1563 }
   1564 
   1565 static void handle_control_req(struct unix_client *client,
   1566 					struct bt_control_req *req)
   1567 {
   1568 	/* FIXME: really implement that */
   1569 	char buf[BT_SUGGESTED_BUFFER_SIZE];
   1570 	struct bt_set_configuration_rsp *rsp = (void *) buf;
   1571 
   1572 	memset(buf, 0, sizeof(buf));
   1573 	rsp->h.type = BT_RESPONSE;
   1574 	rsp->h.name = BT_CONTROL;
   1575 	rsp->h.length = sizeof(*rsp);
   1576 
   1577 	unix_ipc_sendmsg(client, &rsp->h);
   1578 }
   1579 
   1580 static gboolean client_cb(GIOChannel *chan, GIOCondition cond, gpointer data)
   1581 {
   1582 	char buf[BT_SUGGESTED_BUFFER_SIZE];
   1583 	bt_audio_msg_header_t *msghdr = (void *) buf;
   1584 	struct unix_client *client = data;
   1585 	int len;
   1586 	const char *type, *name;
   1587 
   1588 	if (cond & G_IO_NVAL)
   1589 		return FALSE;
   1590 
   1591 	if (cond & (G_IO_HUP | G_IO_ERR)) {
   1592 		debug("Unix client disconnected (fd=%d)", client->sock);
   1593 
   1594 		goto failed;
   1595 	}
   1596 
   1597 	memset(buf, 0, sizeof(buf));
   1598 
   1599 	len = recv(client->sock, buf, sizeof(buf), 0);
   1600 	if (len < 0) {
   1601 		error("recv: %s (%d)", strerror(errno), errno);
   1602 		goto failed;
   1603 	}
   1604 
   1605 	type = bt_audio_strtype(msghdr->type);
   1606 	name = bt_audio_strname(msghdr->name);
   1607 
   1608 	debug("Audio API: %s <- %s", type, name);
   1609 
   1610 	if (msghdr->length != len) {
   1611 		error("Invalid message: length mismatch");
   1612 		goto failed;
   1613 	}
   1614 
   1615 	switch (msghdr->name) {
   1616 	case BT_GET_CAPABILITIES:
   1617 		handle_getcapabilities_req(client,
   1618 				(struct bt_get_capabilities_req *) msghdr);
   1619 		break;
   1620 	case BT_OPEN:
   1621 		handle_open_req(client,
   1622 				(struct bt_open_req *) msghdr);
   1623 		break;
   1624 	case BT_SET_CONFIGURATION:
   1625 		handle_setconfiguration_req(client,
   1626 				(struct bt_set_configuration_req *) msghdr);
   1627 		break;
   1628 	case BT_START_STREAM:
   1629 		handle_streamstart_req(client,
   1630 				(struct bt_start_stream_req *) msghdr);
   1631 		break;
   1632 	case BT_STOP_STREAM:
   1633 		handle_streamstop_req(client,
   1634 				(struct bt_stop_stream_req *) msghdr);
   1635 		break;
   1636 	case BT_CLOSE:
   1637 		handle_close_req(client,
   1638 				(struct bt_close_req *) msghdr);
   1639 		break;
   1640 	case BT_CONTROL:
   1641 		handle_control_req(client,
   1642 				(struct bt_control_req *) msghdr);
   1643 		break;
   1644 	default:
   1645 		error("Audio API: received unexpected message name %d",
   1646 				msghdr->name);
   1647 	}
   1648 
   1649 	return TRUE;
   1650 
   1651 failed:
   1652 	clients = g_slist_remove(clients, client);
   1653 	start_close(client->dev, client, FALSE);
   1654 	client_free(client);
   1655 	return FALSE;
   1656 }
   1657 
   1658 static gboolean server_cb(GIOChannel *chan, GIOCondition cond, gpointer data)
   1659 {
   1660 	struct sockaddr_un addr;
   1661 	socklen_t addrlen;
   1662 	int sk, cli_sk;
   1663 	struct unix_client *client;
   1664 	GIOChannel *io;
   1665 
   1666 	if (cond & G_IO_NVAL)
   1667 		return FALSE;
   1668 
   1669 	if (cond & (G_IO_HUP | G_IO_ERR)) {
   1670 		g_io_channel_close(chan);
   1671 		return FALSE;
   1672 	}
   1673 
   1674 	sk = g_io_channel_unix_get_fd(chan);
   1675 
   1676 	memset(&addr, 0, sizeof(addr));
   1677 	addrlen = sizeof(addr);
   1678 
   1679 	cli_sk = accept(sk, (struct sockaddr *) &addr, &addrlen);
   1680 	if (cli_sk < 0) {
   1681 		error("accept: %s (%d)", strerror(errno), errno);
   1682 		return TRUE;
   1683 	}
   1684 
   1685 	debug("Accepted new client connection on unix socket (fd=%d)", cli_sk);
   1686 	set_nonblocking(cli_sk);
   1687 
   1688 	client = g_new0(struct unix_client, 1);
   1689 	client->sock = cli_sk;
   1690 	clients = g_slist_append(clients, client);
   1691 
   1692 	io = g_io_channel_unix_new(cli_sk);
   1693 	g_io_add_watch(io, G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
   1694 							client_cb, client);
   1695 	g_io_channel_unref(io);
   1696 
   1697 	return TRUE;
   1698 }
   1699 
   1700 void unix_device_removed(struct audio_device *dev)
   1701 {
   1702 	GSList *l;
   1703 
   1704 	debug("unix_device_removed(%p)", dev);
   1705 
   1706 	l = clients;
   1707 	while (l) {
   1708 		struct unix_client *client = l->data;
   1709 
   1710 		l = l->next;
   1711 
   1712 		if (client->dev == dev) {
   1713 			clients = g_slist_remove(clients, client);
   1714 			start_close(client->dev, client, FALSE);
   1715 			client_free(client);
   1716 		}
   1717 	}
   1718 }
   1719 
   1720 int unix_init(void)
   1721 {
   1722 	GIOChannel *io;
   1723 	struct sockaddr_un addr = {
   1724 		AF_UNIX, BT_IPC_SOCKET_NAME
   1725 	};
   1726 
   1727 	int sk, err;
   1728 
   1729 	sk = socket(PF_LOCAL, SOCK_STREAM, 0);
   1730 	if (sk < 0) {
   1731 		err = errno;
   1732 		error("Can't create unix socket: %s (%d)", strerror(err), err);
   1733 		return -err;
   1734 	}
   1735 
   1736 	if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
   1737 		error("Can't bind unix socket: %s (%d)", strerror(errno),
   1738 				errno);
   1739 		close(sk);
   1740 		return -1;
   1741 	}
   1742 
   1743 	set_nonblocking(sk);
   1744 
   1745 	if (listen(sk, 1) < 0) {
   1746 		error("Can't listen on unix socket: %s (%d)",
   1747 						strerror(errno), errno);
   1748 		close(sk);
   1749 		return -1;
   1750 	}
   1751 
   1752 	unix_sock = sk;
   1753 
   1754 	io = g_io_channel_unix_new(sk);
   1755 	g_io_add_watch(io, G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
   1756 							server_cb, NULL);
   1757 	g_io_channel_unref(io);
   1758 
   1759 	debug("Unix socket created: %d", sk);
   1760 
   1761 	return 0;
   1762 }
   1763 
   1764 void unix_exit(void)
   1765 {
   1766 	g_slist_foreach(clients, (GFunc) client_free, NULL);
   1767 	g_slist_free(clients);
   1768 	if (unix_sock >= 0) {
   1769 		close(unix_sock);
   1770 		unix_sock = -1;
   1771 	}
   1772 }
   1773