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