unix.c revision 839a47467fb33fdb8df098e86028e6fa9ce7f101
1/*
2 *
3 *  BlueZ - Bluetooth protocol stack for Linux
4 *
5 *  Copyright (C) 2006-2007  Nokia Corporation
6 *  Copyright (C) 2004-2008  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 "logging.h"
43#include "ipc.h"
44#include "device.h"
45#include "manager.h"
46#include "avdtp.h"
47#include "a2dp.h"
48#include "headset.h"
49#include "sink.h"
50#include "unix.h"
51#include "glib-helper.h"
52
53typedef enum {
54	TYPE_NONE,
55	TYPE_HEADSET,
56	TYPE_SINK,
57	TYPE_SOURCE
58} service_type_t;
59
60typedef void (*notify_cb_t) (struct audio_device *dev, void *data);
61
62struct a2dp_data {
63	struct avdtp *session;
64	struct avdtp_stream *stream;
65	struct a2dp_sep *sep;
66};
67
68struct headset_data {
69	headset_lock_t lock;
70};
71
72struct unix_client {
73	struct audio_device *dev;
74	GSList *caps;
75	service_type_t type;
76	char *interface;
77	union {
78		struct a2dp_data a2dp;
79		struct headset_data hs;
80	} d;
81	int sock;
82	int access_mode;
83	int data_fd; /* To be deleted once two phase configuration is fully implemented */
84	unsigned int req_id;
85	unsigned int cb_id;
86	gboolean (*cancel) (struct audio_device *dev, unsigned int id);
87};
88
89static GSList *clients = NULL;
90
91static int unix_sock = -1;
92
93static void client_free(struct unix_client *client)
94{
95	struct a2dp_data *a2dp;
96
97	switch (client->type) {
98	case TYPE_SINK:
99	case TYPE_SOURCE:
100		a2dp = &client->d.a2dp;
101		if (client->cb_id > 0)
102			avdtp_stream_remove_cb(a2dp->session, a2dp->stream,
103								client->cb_id);
104		if (a2dp->sep)
105			a2dp_sep_unlock(a2dp->sep, a2dp->session);
106		if (a2dp->session)
107			avdtp_unref(a2dp->session);
108		break;
109	default:
110		break;
111	}
112
113	if (client->sock >= 0)
114		close(client->sock);
115
116	if (client->caps) {
117		g_slist_foreach(client->caps, (GFunc) g_free, NULL);
118		g_slist_free(client->caps);
119	}
120
121	g_free(client->interface);
122	g_free(client);
123}
124
125/* Pass file descriptor through local domain sockets (AF_LOCAL, formerly
126 * AF_UNIX) and the sendmsg() system call with the cmsg_type field of a "struct
127 * cmsghdr" set to SCM_RIGHTS and the data being an integer value equal to the
128 * handle of the file descriptor to be passed. */
129static int unix_sendmsg_fd(int sock, int fd)
130{
131	char cmsg_b[CMSG_SPACE(sizeof(int))], m = 'm';
132	struct cmsghdr *cmsg;
133	struct iovec iov = { &m, sizeof(m) };
134	struct msghdr msgh;
135
136	memset(&msgh, 0, sizeof(msgh));
137	msgh.msg_iov = &iov;
138	msgh.msg_iovlen = 1;
139	msgh.msg_control = &cmsg_b;
140	msgh.msg_controllen = CMSG_LEN(sizeof(int));
141
142	cmsg = CMSG_FIRSTHDR(&msgh);
143	cmsg->cmsg_level = SOL_SOCKET;
144	cmsg->cmsg_type = SCM_RIGHTS;
145	cmsg->cmsg_len = CMSG_LEN(sizeof(int));
146	/* Initialize the payload */
147	(*(int *) CMSG_DATA(cmsg)) = fd;
148
149	return sendmsg(sock, &msgh, MSG_NOSIGNAL);
150}
151
152static void unix_ipc_sendmsg(struct unix_client *client,
153					const bt_audio_msg_header_t *msg)
154{
155	debug("Audio API: sending %s", bt_audio_strmsg(msg->msg_type));
156
157	if (send(client->sock, msg, BT_AUDIO_IPC_PACKET_SIZE, 0) < 0)
158		error("Error %s(%d)", strerror(errno), errno);
159}
160
161static void unix_ipc_error(struct unix_client *client, int type, int err)
162{
163	char buf[BT_AUDIO_IPC_PACKET_SIZE];
164	bt_audio_rsp_msg_header_t *rsp_hdr = (void *) buf;
165
166	if (!g_slist_find(clients, client))
167		return;
168
169	memset(buf, 0, sizeof(buf));
170	rsp_hdr->msg_h.msg_type = type;
171	rsp_hdr->posix_errno = err;
172
173	unix_ipc_sendmsg(client, &rsp_hdr->msg_h);
174}
175
176static service_type_t select_service(struct audio_device *dev, const char *interface)
177{
178	if (!interface) {
179		if (dev->sink && avdtp_is_connected(&dev->src, &dev->dst))
180			return TYPE_SINK;
181		else if (dev->headset && headset_is_active(dev))
182			return TYPE_HEADSET;
183		else if (dev->sink)
184			return TYPE_SINK;
185		else if (dev->headset)
186			return TYPE_HEADSET;
187	} else if (!strcmp(interface, AUDIO_SINK_INTERFACE) && dev->sink)
188		return TYPE_SINK;
189	else if (!strcmp(interface, AUDIO_HEADSET_INTERFACE) && dev->headset)
190		return TYPE_HEADSET;
191
192	return TYPE_NONE;
193}
194
195static void stream_state_changed(struct avdtp_stream *stream,
196					avdtp_state_t old_state,
197					avdtp_state_t new_state,
198					struct avdtp_error *err,
199					void *user_data)
200{
201	struct unix_client *client = user_data;
202	struct a2dp_data *a2dp = &client->d.a2dp;
203
204	switch (new_state) {
205	case AVDTP_STATE_IDLE:
206		if (a2dp->sep) {
207			a2dp_sep_unlock(a2dp->sep, a2dp->session);
208			a2dp->sep = NULL;
209		}
210		client->dev = NULL;
211		if (a2dp->session) {
212			avdtp_unref(a2dp->session);
213			a2dp->session = NULL;
214		}
215		a2dp->stream = NULL;
216		client->cb_id = 0;
217		break;
218	default:
219		break;
220	}
221}
222
223static void headset_discovery_complete(struct audio_device *dev, void *user_data)
224{
225	struct unix_client *client = user_data;
226	char buf[BT_AUDIO_IPC_PACKET_SIZE];
227	struct bt_getcapabilities_rsp *rsp = (void *) buf;
228
229	client->req_id = 0;
230
231	if (!dev)
232		goto failed;
233
234	memset(buf, 0, sizeof(buf));
235
236	rsp->rsp_h.msg_h.msg_type = BT_GETCAPABILITIES_RSP;
237	rsp->transport  = BT_CAPABILITIES_TRANSPORT_SCO;
238	rsp->sampling_rate = 8000;
239
240	unix_ipc_sendmsg(client, &rsp->rsp_h.msg_h);
241
242	return;
243
244failed:
245	error("discovery failed");
246	unix_ipc_error(client, BT_SETCONFIGURATION_RSP, EIO);
247}
248
249static void headset_setup_complete(struct audio_device *dev, void *user_data)
250{
251	struct unix_client *client = user_data;
252	char buf[BT_AUDIO_IPC_PACKET_SIZE];
253	struct bt_setconfiguration_rsp *rsp = (void *) buf;
254	struct headset_data *hs = &client->d.hs;
255
256	client->req_id = 0;
257
258	if (!dev)
259		goto failed;
260
261	memset(buf, 0, sizeof(buf));
262
263	rsp->rsp_h.msg_h.msg_type = BT_SETCONFIGURATION_RSP;
264	rsp->transport  = BT_CAPABILITIES_TRANSPORT_SCO;
265	rsp->access_mode = client->access_mode;
266	rsp->link_mtu = 48;
267
268	client->data_fd = headset_get_sco_fd(dev);
269
270	unix_ipc_sendmsg(client, &rsp->rsp_h.msg_h);
271
272	return;
273
274failed:
275	error("config failed");
276	unix_ipc_error(client, BT_SETCONFIGURATION_RSP, EIO);
277}
278
279static void headset_resume_complete(struct audio_device *dev, void *user_data)
280{
281	struct unix_client *client = user_data;
282	char buf[BT_AUDIO_IPC_PACKET_SIZE];
283	struct bt_streamstart_rsp *rsp = (void *) buf;
284	struct bt_streamfd_ind *ind = (void *) buf;
285	struct headset_data *hs = &client->d.hs;
286
287	client->req_id = 0;
288
289	if (!dev)
290		goto failed;
291
292	if (!headset_lock(dev, hs->lock)) {
293		error("Unable to lock headset");
294		goto failed;
295	}
296
297	memset(buf, 0, sizeof(buf));
298
299	rsp->rsp_h.msg_h.msg_type = BT_STREAMSTART_RSP;
300
301	unix_ipc_sendmsg(client, &rsp->rsp_h.msg_h);
302
303	memset(buf, 0, sizeof(buf));
304	ind->h.msg_type = BT_STREAMFD_IND;
305	unix_ipc_sendmsg(client, &ind->h);
306
307	client->data_fd = headset_get_sco_fd(dev);
308
309	if (unix_sendmsg_fd(client->sock, client->data_fd) < 0) {
310		error("unix_sendmsg_fd: %s(%d)", strerror(errno), errno);
311		headset_unlock(client->dev, hs->lock);
312		goto failed;
313	}
314
315	return;
316
317failed:
318	error("headset_resume_complete: resume failed");
319	unix_ipc_error(client, BT_STREAMSTART_RSP, EIO);
320}
321
322static void headset_suspend_complete(struct audio_device *dev, void *user_data)
323{
324	struct unix_client *client = user_data;
325	char buf[BT_AUDIO_IPC_PACKET_SIZE];
326	struct bt_streamstart_rsp *rsp = (void *) buf;
327
328	if (!dev)
329		goto failed;
330
331	memset(buf, 0, sizeof(buf));
332	rsp->rsp_h.msg_h.msg_type = BT_STREAMSTOP_RSP;
333	rsp->rsp_h.posix_errno = 0;
334	unix_ipc_sendmsg(client, &rsp->rsp_h.msg_h);
335
336	return;
337
338failed:
339	error("suspend failed");
340	unix_ipc_error(client, BT_STREAMSTOP_RSP, EIO);
341	client->dev = NULL;
342}
343
344static void a2dp_discovery_complete(struct avdtp *session, GSList *seps,
345					struct avdtp_error *err,
346					void *user_data)
347{
348	struct unix_client *client = user_data;
349	char buf[BT_AUDIO_IPC_PACKET_SIZE];
350	struct bt_getcapabilities_rsp *rsp = (void *) buf;
351	struct a2dp_data *a2dp = &client->d.a2dp;
352	struct sbc_codec_cap *sbc_cap = NULL;
353	struct mpeg_codec_cap *mpeg_cap = NULL;
354	GSList *l;
355
356	if (!g_slist_find(clients, client)) {
357		debug("Client disconnected during discovery");
358		return;
359	}
360
361	if (err)
362		goto failed;
363
364	memset(buf, 0, sizeof(buf));
365	client->req_id = 0;
366
367	rsp->rsp_h.msg_h.msg_type = BT_GETCAPABILITIES_RSP;
368	rsp->transport = BT_CAPABILITIES_TRANSPORT_A2DP;
369
370	for (l = seps; l; l = g_slist_next(l)) {
371		struct avdtp_remote_sep *rsep = l->data;
372		struct avdtp_service_capability *cap;
373		struct avdtp_media_codec_capability *codec_cap;
374
375		cap = avdtp_get_codec(rsep);
376
377		if (cap->category != AVDTP_MEDIA_CODEC)
378			continue;
379
380		codec_cap = (void *) cap->data;
381
382		if (codec_cap->media_codec_type == A2DP_CODEC_SBC && !sbc_cap)
383			sbc_cap = (void *) codec_cap;
384
385		if (codec_cap->media_codec_type == A2DP_CODEC_MPEG12 && !mpeg_cap)
386			mpeg_cap = (void *) codec_cap;
387	}
388
389	/* endianess prevent direct cast */
390	if (sbc_cap) {
391		rsp->sbc_capabilities.channel_mode = sbc_cap->channel_mode;
392		rsp->sbc_capabilities.frequency = sbc_cap->frequency;
393		rsp->sbc_capabilities.allocation_method = sbc_cap->allocation_method;
394		rsp->sbc_capabilities.subbands = sbc_cap->subbands;
395		rsp->sbc_capabilities.block_length = sbc_cap->block_length;
396		rsp->sbc_capabilities.min_bitpool = sbc_cap->min_bitpool;
397		rsp->sbc_capabilities.max_bitpool = sbc_cap->max_bitpool;
398	}
399
400	if (mpeg_cap) {
401		rsp->mpeg_capabilities.channel_mode = mpeg_cap->channel_mode;
402		rsp->mpeg_capabilities.crc = mpeg_cap->crc;
403		rsp->mpeg_capabilities.layer = mpeg_cap->layer;
404		rsp->mpeg_capabilities.frequency = mpeg_cap->frequency;
405		rsp->mpeg_capabilities.mpf = mpeg_cap->mpf;
406		rsp->mpeg_capabilities.bitrate = mpeg_cap->bitrate;
407	}
408
409	unix_ipc_sendmsg(client, &rsp->rsp_h.msg_h);
410
411	return;
412
413failed:
414	error("discovery failed");
415	unix_ipc_error(client, BT_GETCAPABILITIES_RSP, EIO);
416
417	avdtp_unref(a2dp->session);
418
419	a2dp->session = NULL;
420	a2dp->stream = NULL;
421}
422
423static void a2dp_config_complete(struct avdtp *session, struct a2dp_sep *sep,
424					struct avdtp_stream *stream,
425					struct avdtp_error *err,
426					void *user_data)
427{
428	struct unix_client *client = user_data;
429	char buf[BT_AUDIO_IPC_PACKET_SIZE];
430	struct bt_setconfiguration_rsp *rsp = (void *) buf;
431	struct a2dp_data *a2dp = &client->d.a2dp;
432	uint16_t imtu, omtu;
433	GSList *caps;
434
435	if (err)
436		goto failed;
437
438	memset(buf, 0, sizeof(buf));
439	client->req_id = 0;
440
441	if (!stream)
442		goto failed;
443
444	if (!a2dp_sep_lock(sep, session)) {
445		error("Unable to lock A2DP source SEP");
446		goto failed;
447	}
448
449	a2dp->sep = sep;
450	a2dp->stream = stream;
451
452	if (!avdtp_stream_get_transport(stream, &client->data_fd, &imtu, &omtu,
453					&caps)) {
454		error("Unable to get stream transport");
455		goto failed;
456	}
457
458	rsp->rsp_h.msg_h.msg_type = BT_SETCONFIGURATION_RSP;
459	rsp->transport = BT_CAPABILITIES_TRANSPORT_A2DP;
460	client->access_mode = BT_CAPABILITIES_ACCESS_MODE_WRITE;
461	rsp->access_mode = client->access_mode;
462	/* FIXME: Use imtu when fd_opt is CFG_FD_OPT_READ */
463	rsp->link_mtu = omtu;
464
465	unix_ipc_sendmsg(client, &rsp->rsp_h.msg_h);
466
467	client->cb_id = avdtp_stream_add_cb(session, stream,
468						stream_state_changed, client);
469
470	return;
471
472failed:
473	error("config failed");
474
475	if (a2dp->sep) {
476		a2dp_sep_unlock(a2dp->sep, a2dp->session);
477		a2dp->sep = NULL;
478	}
479	unix_ipc_error(client, BT_SETCONFIGURATION_RSP, EIO);
480
481	avdtp_unref(a2dp->session);
482
483	a2dp->session = NULL;
484	a2dp->stream = NULL;
485}
486
487static void a2dp_resume_complete(struct avdtp *session,
488				struct avdtp_error *err, void *user_data)
489{
490	struct unix_client *client = user_data;
491	char buf[BT_AUDIO_IPC_PACKET_SIZE];
492	struct bt_streamstart_rsp *rsp = (void *) buf;
493	struct bt_streamfd_ind *ind = (void *) buf;
494	struct a2dp_data *a2dp = &client->d.a2dp;
495
496	if (err)
497		goto failed;
498
499	memset(buf, 0, sizeof(buf));
500	rsp->rsp_h.msg_h.msg_type = BT_STREAMSTART_RSP;
501	rsp->rsp_h.posix_errno = 0;
502	unix_ipc_sendmsg(client, &rsp->rsp_h.msg_h);
503
504	memset(buf, 0, sizeof(buf));
505	ind->h.msg_type = BT_STREAMFD_IND;
506	unix_ipc_sendmsg(client, &ind->h);
507
508	if (unix_sendmsg_fd(client->sock, client->data_fd) < 0) {
509		error("unix_sendmsg_fd: %s(%d)", strerror(errno), errno);
510		goto failed;
511	}
512
513	return;
514
515failed:
516	error("resume failed");
517
518	if (a2dp->sep) {
519		a2dp_sep_unlock(a2dp->sep, a2dp->session);
520		a2dp->sep = NULL;
521	}
522	unix_ipc_error(client, BT_STREAMSTART_RSP, EIO);
523
524	if (client->cb_id > 0) {
525		avdtp_stream_remove_cb(a2dp->session, a2dp->stream,
526					client->cb_id);
527		client->cb_id = 0;
528	}
529
530	avdtp_unref(a2dp->session);
531
532	a2dp->session = NULL;
533	a2dp->stream = NULL;
534}
535
536static void a2dp_suspend_complete(struct avdtp *session,
537				struct avdtp_error *err, void *user_data)
538{
539	struct unix_client *client = user_data;
540	char buf[BT_AUDIO_IPC_PACKET_SIZE];
541	struct bt_streamstart_rsp *rsp = (void *) buf;
542	struct a2dp_data *a2dp = &client->d.a2dp;
543
544	if (err)
545		goto failed;
546
547	memset(buf, 0, sizeof(buf));
548	rsp->rsp_h.msg_h.msg_type = BT_STREAMSTOP_RSP;
549	rsp->rsp_h.posix_errno = 0;
550	unix_ipc_sendmsg(client, &rsp->rsp_h.msg_h);
551
552	return;
553
554failed:
555	error("suspend failed");
556
557	if (a2dp->sep) {
558		a2dp_sep_unlock(a2dp->sep, a2dp->session);
559		a2dp->sep = NULL;
560	}
561	unix_ipc_error(client, BT_STREAMSTOP_RSP, EIO);
562
563	avdtp_unref(a2dp->session);
564
565	a2dp->session = NULL;
566	a2dp->stream = NULL;
567}
568
569static void start_discovery(struct audio_device *dev, struct unix_client *client)
570{
571	struct a2dp_data *a2dp;
572	int err = 0;
573
574	client->type = select_service(dev, client->interface);
575
576	switch (client->type) {
577	case TYPE_SINK:
578		a2dp = &client->d.a2dp;
579
580		if (!a2dp->session)
581			a2dp->session = avdtp_get(&dev->src, &dev->dst);
582
583		if (!a2dp->session) {
584			error("Unable to get a session");
585			goto failed;
586		}
587
588		err = avdtp_discover(a2dp->session, a2dp_discovery_complete,
589					client);
590		if (err)
591			goto failed;
592		break;
593
594	case TYPE_HEADSET:
595		headset_discovery_complete(dev, client);
596		break;
597
598	default:
599		error("No known services for device");
600		goto failed;
601	}
602
603	client->dev = dev;
604
605	return;
606
607failed:
608	unix_ipc_error(client, BT_GETCAPABILITIES_RSP, err ? : EIO);
609}
610
611static void start_config(struct audio_device *dev, struct unix_client *client)
612{
613	struct a2dp_data *a2dp;
614	struct headset_data *hs;
615	unsigned int id;
616
617	client->type = select_service(dev, client->interface);
618
619	switch (client->type) {
620	case TYPE_SINK:
621		a2dp = &client->d.a2dp;
622
623		if (!a2dp->session)
624			a2dp->session = avdtp_get(&dev->src, &dev->dst);
625
626		if (!a2dp->session) {
627			error("Unable to get a session");
628			goto failed;
629		}
630
631		id = a2dp_source_config(a2dp->session, a2dp_config_complete,
632					client->caps, client);
633		client->cancel = a2dp_source_cancel;
634		break;
635
636	case TYPE_HEADSET:
637		hs = &client->d.hs;
638
639		switch (client->access_mode) {
640		case BT_CAPABILITIES_ACCESS_MODE_READ:
641			hs->lock = HEADSET_LOCK_READ;
642			break;
643		case BT_CAPABILITIES_ACCESS_MODE_WRITE:
644			hs->lock = HEADSET_LOCK_WRITE;
645			break;
646		case BT_CAPABILITIES_ACCESS_MODE_READWRITE:
647			hs->lock = HEADSET_LOCK_READ | HEADSET_LOCK_WRITE;
648			break;
649		default:
650			hs->lock = 0;
651			break;
652		}
653
654		id = headset_suspend_stream(dev, headset_setup_complete,
655					hs->lock, client);
656		client->cancel = headset_cancel_stream;
657		break;
658
659	default:
660		error("No known services for device");
661		goto failed;
662	}
663
664	if (id == 0) {
665		error("config failed");
666		goto failed;
667	}
668
669	client->req_id = id;
670	client->dev = dev;
671
672	return;
673
674failed:
675	unix_ipc_error(client, BT_SETCONFIGURATION_RSP, EIO);
676}
677
678static void start_resume(struct audio_device *dev, struct unix_client *client)
679{
680	struct a2dp_data *a2dp;
681	struct headset_data *hs;
682	unsigned int id;
683
684	client->type = select_service(dev, client->interface);
685
686	switch (client->type) {
687	case TYPE_SINK:
688		a2dp = &client->d.a2dp;
689
690		if (!a2dp->session)
691			a2dp->session = avdtp_get(&dev->src, &dev->dst);
692
693		if (!a2dp->session) {
694			error("Unable to get a session");
695			goto failed;
696		}
697
698		if (!a2dp->sep) {
699			error("Unable to get a sep");
700			goto failed;
701		}
702
703		id = a2dp_source_resume(a2dp->session, a2dp->sep,
704					a2dp_resume_complete, client);
705		client->cancel = a2dp_source_cancel;
706
707		break;
708
709	case TYPE_HEADSET:
710		hs = &client->d.hs;
711
712		id = headset_request_stream(dev, headset_resume_complete,
713					hs->lock, client);
714		client->cancel = headset_cancel_stream;
715		break;
716
717	default:
718		error("No known services for device");
719		goto failed;
720	}
721
722	if (id == 0) {
723		error("start_resume: resume failed");
724		goto failed;
725	}
726
727	return;
728
729failed:
730	unix_ipc_error(client, BT_STREAMSTART_RSP, EIO);
731}
732
733static void start_suspend(struct audio_device *dev, struct unix_client *client)
734{
735	struct a2dp_data *a2dp;
736	struct headset_data *hs;
737	unsigned int id;
738
739	client->type = select_service(dev, client->interface);
740
741	switch (client->type) {
742	case TYPE_SINK:
743		a2dp = &client->d.a2dp;
744
745		if (!a2dp->session)
746			a2dp->session = avdtp_get(&dev->src, &dev->dst);
747
748		if (!a2dp->session) {
749			error("Unable to get a session");
750			goto failed;
751		}
752
753		if (!a2dp->sep) {
754			error("Unable to get a sep");
755			goto failed;
756		}
757
758		id = a2dp_source_suspend(a2dp->session, a2dp->sep,
759					a2dp_suspend_complete, client);
760		client->cancel = a2dp_source_cancel;
761		break;
762
763	case TYPE_HEADSET:
764		hs = &client->d.hs;
765
766		id = headset_suspend_stream(dev, headset_suspend_complete,
767					~hs->lock, client);
768		client->cancel = headset_cancel_stream;
769		break;
770
771	default:
772		error("No known services for device");
773		goto failed;
774	}
775
776	if (id == 0) {
777		error("suspend failed");
778		goto failed;
779	}
780
781	return;
782
783failed:
784	unix_ipc_error(client, BT_STREAMSTOP_RSP, EIO);
785}
786
787static void handle_getcapabilities_req(struct unix_client *client,
788					struct bt_getcapabilities_req *req)
789{
790	struct audio_device *dev;
791	bdaddr_t bdaddr;
792
793	str2ba(req->device, &bdaddr);
794
795	if (client->interface) {
796		g_free(client->interface);
797		client->interface = NULL;
798	}
799
800	if (req->transport == BT_CAPABILITIES_TRANSPORT_SCO)
801		client->interface = g_strdup(AUDIO_HEADSET_INTERFACE);
802	else if (req->transport == BT_CAPABILITIES_TRANSPORT_A2DP)
803		client->interface = g_strdup(AUDIO_SINK_INTERFACE);
804
805	if (!manager_find_device(&bdaddr, NULL, FALSE))
806		goto failed;
807
808	dev = manager_find_device(&bdaddr, client->interface, TRUE);
809	if (!dev) {
810		if (req->flags & BT_FLAG_AUTOCONNECT)
811			dev = manager_find_device(&bdaddr, client->interface, FALSE);
812		else
813			goto failed;
814	}
815
816	if (!dev)
817		goto failed;
818
819	start_discovery(dev, client);
820
821	return;
822
823failed:
824	unix_ipc_error(client, BT_GETCAPABILITIES_RSP, EIO);
825}
826
827static int handle_sco_transport(struct unix_client *client,
828				struct bt_setconfiguration_req *req)
829{
830	client->interface = g_strdup(AUDIO_HEADSET_INTERFACE);
831
832	debug("config sco - device = %s access_mode = %u", req->device,
833			req->access_mode);
834
835	return 0;
836}
837
838static int handle_a2dp_transport(struct unix_client *client,
839				struct bt_setconfiguration_req *req)
840{
841	struct avdtp_service_capability *media_transport, *media_codec;
842	struct sbc_codec_cap sbc_cap;
843	struct mpeg_codec_cap mpeg_cap;
844
845	client->interface = g_strdup(AUDIO_SINK_INTERFACE);
846
847	if (client->caps) {
848		g_slist_foreach(client->caps, (GFunc) g_free, NULL);
849		g_slist_free(client->caps);
850	}
851
852	media_transport = avdtp_service_cap_new(AVDTP_MEDIA_TRANSPORT,
853						NULL, 0);
854
855	client->caps = g_slist_append(client->caps, media_transport);
856
857	debug("config a2dp - device = %s access_mode = %u", req->device,
858			req->access_mode);
859
860	if (req->mpeg_capabilities.frequency) {
861
862		memset(&mpeg_cap, 0, sizeof(mpeg_cap));
863
864		mpeg_cap.cap.media_type = AVDTP_MEDIA_TYPE_AUDIO;
865		mpeg_cap.cap.media_codec_type = A2DP_CODEC_MPEG12;
866		mpeg_cap.channel_mode = req->mpeg_capabilities.channel_mode;
867		mpeg_cap.crc = req->mpeg_capabilities.crc;
868		mpeg_cap.layer = req->mpeg_capabilities.layer;
869		mpeg_cap.frequency = req->mpeg_capabilities.frequency;
870		mpeg_cap.mpf = req->mpeg_capabilities.mpf;
871		mpeg_cap.bitrate = req->mpeg_capabilities.bitrate;
872
873		media_codec = avdtp_service_cap_new(AVDTP_MEDIA_CODEC, &mpeg_cap,
874							sizeof(mpeg_cap));
875
876		debug("codec mpeg12 - frequency = %u channel_mode = %u "
877			"layer = %u crc = %u mpf = %u bitrate = %u",
878			mpeg_cap.frequency, mpeg_cap.channel_mode,
879			mpeg_cap.layer, mpeg_cap.crc, mpeg_cap.mpf,
880			mpeg_cap.bitrate);
881	} else if (req->sbc_capabilities.frequency) {
882		memset(&sbc_cap, 0, sizeof(sbc_cap));
883
884		sbc_cap.cap.media_type = AVDTP_MEDIA_TYPE_AUDIO;
885		sbc_cap.cap.media_codec_type = A2DP_CODEC_SBC;
886		sbc_cap.channel_mode = req->sbc_capabilities.channel_mode;
887		sbc_cap.frequency = req->sbc_capabilities.frequency;
888		sbc_cap.allocation_method = req->sbc_capabilities.allocation_method;
889		sbc_cap.subbands = req->sbc_capabilities.subbands;
890		sbc_cap.block_length = req->sbc_capabilities.block_length;
891		sbc_cap.min_bitpool = req->sbc_capabilities.min_bitpool;
892		sbc_cap.max_bitpool = req->sbc_capabilities.max_bitpool;
893
894		media_codec = avdtp_service_cap_new(AVDTP_MEDIA_CODEC, &sbc_cap,
895							sizeof(sbc_cap));
896
897		debug("codec sbc - frequency = %u channel_mode = %u "
898			"allocation = %u subbands = %u blocks = %u "
899			"bitpool = %u", sbc_cap.frequency,
900			sbc_cap.channel_mode, sbc_cap.allocation_method,
901			sbc_cap.subbands, sbc_cap.block_length,
902			sbc_cap.max_bitpool);
903	} else
904		return -EINVAL;
905
906	client->caps = g_slist_append(client->caps, media_codec);
907
908	return 0;
909}
910
911static void handle_setconfiguration_req(struct unix_client *client,
912					struct bt_setconfiguration_req *req)
913{
914	struct audio_device *dev;
915	bdaddr_t bdaddr;
916	int err = 0;
917
918	if (!req->access_mode) {
919		err = EINVAL;
920		goto failed;
921	}
922
923	str2ba(req->device, &bdaddr);
924
925	if (client->interface) {
926		g_free(client->interface);
927		client->interface = NULL;
928	}
929
930	if (req->transport == BT_CAPABILITIES_TRANSPORT_SCO) {
931		err = handle_sco_transport(client, req);
932		if (err < 0) {
933			err = -err;
934			goto failed;
935		}
936	} else if (req->transport == BT_CAPABILITIES_TRANSPORT_A2DP) {
937		err = handle_a2dp_transport(client, req);
938		if (err < 0) {
939			err = -err;
940			goto failed;
941		}
942	}
943
944	if (!manager_find_device(&bdaddr, NULL, FALSE))
945		goto failed;
946
947	dev = manager_find_device(&bdaddr, client->interface, TRUE);
948	if (!dev)
949		dev = manager_find_device(&bdaddr, client->interface, FALSE);
950
951	if (!dev)
952		goto failed;
953
954	client->access_mode = req->access_mode;
955
956	start_config(dev, client);
957
958	return;
959
960failed:
961	unix_ipc_error(client, BT_SETCONFIGURATION_RSP, err ? : EIO);
962}
963
964static void handle_streamstart_req(struct unix_client *client,
965					struct bt_streamstart_req *req)
966{
967	if (!client->dev)
968		goto failed;
969
970	start_resume(client->dev, client);
971
972	return;
973
974failed:
975	unix_ipc_error(client, BT_STREAMSTART_REQ, EIO);
976}
977
978static void handle_streamstop_req(struct unix_client *client,
979					struct bt_streamstop_req *req)
980{
981	if (!client->dev)
982		goto failed;
983
984	start_suspend(client->dev, client);
985
986	return;
987
988failed:
989	unix_ipc_error(client, BT_STREAMSTOP_REQ, EIO);
990}
991
992static void handle_control_req(struct unix_client *client,
993					struct bt_control_req *req)
994{
995	/* FIXME: really implement that */
996	char buf[BT_AUDIO_IPC_PACKET_SIZE];
997	struct bt_setconfiguration_rsp *rsp = (void *) buf;
998
999	memset(buf, 0, sizeof(buf));
1000	rsp->rsp_h.msg_h.msg_type = BT_CONTROL_RSP;
1001	rsp->rsp_h.posix_errno = 0;
1002
1003	unix_ipc_sendmsg(client, &rsp->rsp_h.msg_h);
1004}
1005
1006static gboolean client_cb(GIOChannel *chan, GIOCondition cond, gpointer data)
1007{
1008	char buf[BT_AUDIO_IPC_PACKET_SIZE];
1009	bt_audio_msg_header_t *msghdr = (void *) buf;
1010	struct unix_client *client = data;
1011	int len;
1012	struct a2dp_data *a2dp = &client->d.a2dp;
1013	struct headset_data *hs = &client->d.hs;
1014	const char *type;
1015
1016	if (cond & G_IO_NVAL)
1017		return FALSE;
1018
1019	if (cond & (G_IO_HUP | G_IO_ERR)) {
1020		debug("Unix client disconnected (fd=%d)", client->sock);
1021		switch (client->type) {
1022		case TYPE_HEADSET:
1023			if (client->dev)
1024				headset_unlock(client->dev, hs->lock);
1025			break;
1026		case TYPE_SOURCE:
1027		case TYPE_SINK:
1028			if (a2dp->sep) {
1029				a2dp_sep_unlock(a2dp->sep, a2dp->session);
1030				a2dp->sep = NULL;
1031			}
1032			break;
1033		default:
1034			break;
1035		}
1036
1037		if (client->cancel && client->req_id > 0)
1038			client->cancel(client->dev, client->req_id);
1039		goto failed;
1040	}
1041
1042	memset(buf, 0, sizeof(buf));
1043
1044	len = recv(client->sock, buf, sizeof(buf), MSG_WAITALL);
1045	if (len < 0) {
1046		error("recv: %s (%d)", strerror(errno), errno);
1047		goto failed;
1048	}
1049
1050	if ((type = bt_audio_strmsg(msghdr->msg_type)))
1051		debug("Audio API: received %s", type);
1052
1053	switch (msghdr->msg_type) {
1054	case BT_GETCAPABILITIES_REQ:
1055		handle_getcapabilities_req(client,
1056				(struct bt_getcapabilities_req *) msghdr);
1057		break;
1058	case BT_SETCONFIGURATION_REQ:
1059		handle_setconfiguration_req(client,
1060				(struct bt_setconfiguration_req *) msghdr);
1061		break;
1062	case BT_STREAMSTART_REQ:
1063		handle_streamstart_req(client,
1064				(struct bt_streamstart_req *) msghdr);
1065		break;
1066	case BT_STREAMSTOP_REQ:
1067		handle_streamstop_req(client,
1068				(struct bt_streamstop_req *) msghdr);
1069		break;
1070	case BT_CONTROL_REQ:
1071		handle_control_req(client,
1072				(struct bt_control_req *) msghdr);
1073		break;
1074	default:
1075		error("Audio API: received unexpected packet type %d",
1076				msghdr->msg_type);
1077	}
1078
1079	return TRUE;
1080
1081failed:
1082	clients = g_slist_remove(clients, client);
1083	client_free(client);
1084	return FALSE;
1085}
1086
1087static gboolean server_cb(GIOChannel *chan, GIOCondition cond, gpointer data)
1088{
1089	struct sockaddr_un addr;
1090	socklen_t addrlen;
1091	int sk, cli_sk;
1092	struct unix_client *client;
1093	GIOChannel *io;
1094
1095	if (cond & G_IO_NVAL)
1096		return FALSE;
1097
1098	if (cond & (G_IO_HUP | G_IO_ERR)) {
1099		g_io_channel_close(chan);
1100		return FALSE;
1101	}
1102
1103	sk = g_io_channel_unix_get_fd(chan);
1104
1105	memset(&addr, 0, sizeof(addr));
1106	addrlen = sizeof(addr);
1107
1108	cli_sk = accept(sk, (struct sockaddr *) &addr, &addrlen);
1109	if (cli_sk < 0) {
1110		error("accept: %s (%d)", strerror(errno), errno);
1111		return TRUE;
1112	}
1113
1114	debug("Accepted new client connection on unix socket (fd=%d)", cli_sk);
1115
1116	client = g_new0(struct unix_client, 1);
1117	client->sock = cli_sk;
1118	clients = g_slist_append(clients, client);
1119
1120	io = g_io_channel_unix_new(cli_sk);
1121	g_io_add_watch(io, G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
1122							client_cb, client);
1123	g_io_channel_unref(io);
1124
1125	return TRUE;
1126}
1127
1128int unix_init(void)
1129{
1130	GIOChannel *io;
1131	struct sockaddr_un addr = {
1132		AF_UNIX, BT_IPC_SOCKET_NAME
1133	};
1134
1135	int sk, err;
1136
1137	sk = socket(PF_LOCAL, SOCK_STREAM, 0);
1138	if (sk < 0) {
1139		err = errno;
1140		error("Can't create unix socket: %s (%d)", strerror(err), err);
1141		return -err;
1142	}
1143
1144	if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1145		error("Can't bind unix socket: %s (%d)", strerror(errno),
1146				errno);
1147		close(sk);
1148		return -1;
1149	}
1150
1151	set_nonblocking(sk);
1152
1153	unix_sock = sk;
1154
1155	listen(sk, 1);
1156
1157	io = g_io_channel_unix_new(sk);
1158	g_io_add_watch(io, G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
1159							server_cb, NULL);
1160	g_io_channel_unref(io);
1161
1162	debug("Unix socket created: %d", sk);
1163
1164	return 0;
1165}
1166
1167void unix_exit(void)
1168{
1169	g_slist_foreach(clients, (GFunc) client_free, NULL);
1170	g_slist_free(clients);
1171	close(unix_sock);
1172	unix_sock = -1;
1173}
1174