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