Upgrade bluez5_37 :Merge the code from private
[platform/upstream/bluez.git] / profiles / audio / a2dp.c
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  *  Copyright (C) 2011  BMW Car IT GmbH. All rights reserved.
8  *
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software
22  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
23  *
24  */
25
26 #ifdef HAVE_CONFIG_H
27 #include <config.h>
28 #endif
29
30 #include <stdlib.h>
31 #include <errno.h>
32
33 #include <dbus/dbus.h>
34 #include <glib.h>
35
36 #include "lib/bluetooth.h"
37 #include "lib/sdp.h"
38 #include "lib/sdp_lib.h"
39 #include "lib/uuid.h"
40
41 #include "src/plugin.h"
42 #include "src/adapter.h"
43 #include "src/device.h"
44 #include "src/profile.h"
45 #include "src/service.h"
46 #include "src/log.h"
47 #include "src/sdpd.h"
48 #include "src/shared/queue.h"
49
50 #include "btio/btio.h"
51
52 #include "avdtp.h"
53 #include "sink.h"
54 #include "source.h"
55 #include "a2dp.h"
56 #include "a2dp-codecs.h"
57 #include "media.h"
58
59 /* The duration that streams without users are allowed to stay in
60  * STREAMING state. */
61 #define SUSPEND_TIMEOUT 5
62 #define RECONFIGURE_TIMEOUT 500
63
64 #define AVDTP_PSM 25
65
66 struct a2dp_sep {
67         struct a2dp_server *server;
68         struct a2dp_endpoint *endpoint;
69         uint8_t type;
70         uint8_t codec;
71         struct avdtp_local_sep *lsep;
72         struct avdtp *session;
73         struct avdtp_stream *stream;
74         guint suspend_timer;
75 #ifdef __TIZEN_PATCH__
76         gboolean remote_suspended;
77 #endif
78         gboolean delay_reporting;
79         gboolean locked;
80         gboolean suspending;
81         gboolean starting;
82         void *user_data;
83         GDestroyNotify destroy;
84 };
85
86 struct a2dp_setup_cb {
87         struct a2dp_setup *setup;
88         a2dp_discover_cb_t discover_cb;
89         a2dp_select_cb_t select_cb;
90         a2dp_config_cb_t config_cb;
91         a2dp_stream_cb_t resume_cb;
92         a2dp_stream_cb_t suspend_cb;
93         guint source_id;
94         void *user_data;
95         unsigned int id;
96 };
97
98 struct a2dp_setup {
99         struct avdtp *session;
100         struct a2dp_sep *sep;
101         struct avdtp_remote_sep *rsep;
102         struct avdtp_stream *stream;
103         struct avdtp_error *err;
104         avdtp_set_configuration_cb setconf_cb;
105         GSList *seps;
106         GSList *caps;
107         gboolean reconfigure;
108         gboolean start;
109         GSList *cb;
110         int ref;
111 };
112
113 struct a2dp_server {
114         struct btd_adapter *adapter;
115         GSList *sinks;
116         GSList *sources;
117         uint32_t source_record_id;
118         uint32_t sink_record_id;
119         gboolean sink_enabled;
120         gboolean source_enabled;
121         GIOChannel *io;
122         struct queue *seps;
123         struct queue *channels;
124 };
125
126 struct a2dp_channel {
127         struct a2dp_server *server;
128         struct btd_device *device;
129         GIOChannel *io;
130         guint io_id;
131         unsigned int state_id;
132         unsigned int auth_id;
133         struct avdtp *session;
134 };
135
136 static GSList *servers = NULL;
137 static GSList *setups = NULL;
138 static unsigned int cb_id = 0;
139
140 static struct a2dp_setup *setup_ref(struct a2dp_setup *setup)
141 {
142         setup->ref++;
143
144         DBG("%p: ref=%d", setup, setup->ref);
145
146         return setup;
147 }
148
149 static struct a2dp_setup *setup_new(struct avdtp *session)
150 {
151         struct a2dp_setup *setup;
152
153         setup = g_new0(struct a2dp_setup, 1);
154         setup->session = avdtp_ref(session);
155         setups = g_slist_append(setups, setup);
156
157         return setup;
158 }
159
160 static void setup_free(struct a2dp_setup *s)
161 {
162         DBG("%p", s);
163
164         setups = g_slist_remove(setups, s);
165         if (s->session)
166                 avdtp_unref(s->session);
167         g_slist_free_full(s->cb, g_free);
168         g_slist_free_full(s->caps, g_free);
169         g_free(s);
170 }
171
172 static void setup_unref(struct a2dp_setup *setup)
173 {
174         setup->ref--;
175
176         DBG("%p: ref=%d", setup, setup->ref);
177
178         if (setup->ref > 0)
179                 return;
180
181         setup_free(setup);
182 }
183
184 static struct a2dp_setup_cb *setup_cb_new(struct a2dp_setup *setup)
185 {
186         struct a2dp_setup_cb *cb;
187
188         cb = g_new0(struct a2dp_setup_cb, 1);
189         cb->setup = setup;
190         cb->id = ++cb_id;
191
192         setup->cb = g_slist_append(setup->cb, cb);
193         return cb;
194 }
195
196 static void setup_cb_free(struct a2dp_setup_cb *cb)
197 {
198         struct a2dp_setup *setup = cb->setup;
199
200         if (cb->source_id)
201                 g_source_remove(cb->source_id);
202
203         setup->cb = g_slist_remove(setup->cb, cb);
204         setup_unref(cb->setup);
205         g_free(cb);
206 }
207
208 static void finalize_setup_errno(struct a2dp_setup *s, int err,
209                                         GSourceFunc cb1, ...)
210 {
211         GSourceFunc finalize;
212         va_list args;
213         struct avdtp_error avdtp_err;
214
215         if (err < 0) {
216                 avdtp_error_init(&avdtp_err, AVDTP_ERRNO, -err);
217                 s->err = &avdtp_err;
218         }
219
220         va_start(args, cb1);
221         finalize = cb1;
222         setup_ref(s);
223         while (finalize != NULL) {
224                 finalize(s);
225                 finalize = va_arg(args, GSourceFunc);
226         }
227         setup_unref(s);
228         va_end(args);
229 }
230
231 static gboolean finalize_config(gpointer data)
232 {
233         struct a2dp_setup *s = data;
234         GSList *l;
235         struct avdtp_stream *stream = s->err ? NULL : s->stream;
236
237         for (l = s->cb; l != NULL; ) {
238                 struct a2dp_setup_cb *cb = l->data;
239
240                 l = l->next;
241
242                 if (!cb->config_cb)
243                         continue;
244
245                 cb->config_cb(s->session, s->sep, stream, s->err,
246                                                         cb->user_data);
247                 setup_cb_free(cb);
248         }
249
250         return FALSE;
251 }
252
253 static gboolean finalize_resume(gpointer data)
254 {
255         struct a2dp_setup *s = data;
256         GSList *l;
257
258         for (l = s->cb; l != NULL; ) {
259                 struct a2dp_setup_cb *cb = l->data;
260
261                 l = l->next;
262
263                 if (!cb->resume_cb)
264                         continue;
265
266                 cb->resume_cb(s->session, s->err, cb->user_data);
267                 setup_cb_free(cb);
268         }
269
270         return FALSE;
271 }
272
273 static gboolean finalize_suspend(gpointer data)
274 {
275         struct a2dp_setup *s = data;
276         GSList *l;
277
278         for (l = s->cb; l != NULL; ) {
279                 struct a2dp_setup_cb *cb = l->data;
280
281                 l = l->next;
282
283                 if (!cb->suspend_cb)
284                         continue;
285
286                 cb->suspend_cb(s->session, s->err, cb->user_data);
287                 setup_cb_free(cb);
288         }
289
290         return FALSE;
291 }
292
293 static void finalize_select(struct a2dp_setup *s)
294 {
295         GSList *l;
296
297         for (l = s->cb; l != NULL; ) {
298                 struct a2dp_setup_cb *cb = l->data;
299
300                 l = l->next;
301
302                 if (!cb->select_cb)
303                         continue;
304
305                 cb->select_cb(s->session, s->sep, s->caps, cb->user_data);
306                 setup_cb_free(cb);
307         }
308 }
309
310 static void finalize_discover(struct a2dp_setup *s)
311 {
312         GSList *l;
313
314         for (l = s->cb; l != NULL; ) {
315                 struct a2dp_setup_cb *cb = l->data;
316
317                 l = l->next;
318
319                 if (!cb->discover_cb)
320                         continue;
321
322                 cb->discover_cb(s->session, s->seps, s->err, cb->user_data);
323                 setup_cb_free(cb);
324         }
325 }
326
327 static struct a2dp_setup *find_setup_by_session(struct avdtp *session)
328 {
329         GSList *l;
330
331         for (l = setups; l != NULL; l = l->next) {
332                 struct a2dp_setup *setup = l->data;
333
334                 if (setup->session == session)
335                         return setup;
336         }
337
338         return NULL;
339 }
340
341 static struct a2dp_setup *a2dp_setup_get(struct avdtp *session)
342 {
343         struct a2dp_setup *setup;
344
345         setup = find_setup_by_session(session);
346         if (!setup) {
347                 setup = setup_new(session);
348                 if (!setup)
349                         return NULL;
350         }
351
352         return setup_ref(setup);
353 }
354
355 static struct a2dp_setup *find_setup_by_stream(struct avdtp_stream *stream)
356 {
357         GSList *l;
358
359         for (l = setups; l != NULL; l = l->next) {
360                 struct a2dp_setup *setup = l->data;
361
362                 if (setup->stream == stream)
363                         return setup;
364         }
365
366         return NULL;
367 }
368
369 static void stream_state_changed(struct avdtp_stream *stream,
370                                         avdtp_state_t old_state,
371                                         avdtp_state_t new_state,
372                                         struct avdtp_error *err,
373                                         void *user_data)
374 {
375         struct a2dp_sep *sep = user_data;
376
377         if (new_state == AVDTP_STATE_OPEN) {
378                 struct a2dp_setup *setup;
379                 int err;
380
381                 setup = find_setup_by_stream(stream);
382                 if (!setup || !setup->start)
383                         return;
384
385                 setup->start = FALSE;
386
387                 err = avdtp_start(setup->session, stream);
388                 if (err < 0 && err != -EINPROGRESS) {
389                         error("avdtp_start: %s (%d)", strerror(-err), -err);
390                         finalize_setup_errno(setup, err, finalize_resume,
391                                                                         NULL);
392                         return;
393                 }
394
395                 sep->starting = TRUE;
396
397                 return;
398         }
399
400 #ifdef __TIZEN_PATCH__
401         if (new_state == AVDTP_STATE_STREAMING && sep->suspend_timer) {
402                 g_source_remove(sep->suspend_timer);
403                 sep->suspend_timer = 0;
404         }
405 #endif
406
407         if (new_state != AVDTP_STATE_IDLE)
408                 return;
409
410         if (sep->suspend_timer) {
411                 g_source_remove(sep->suspend_timer);
412                 sep->suspend_timer = 0;
413         }
414
415         if (sep->session) {
416                 avdtp_unref(sep->session);
417                 sep->session = NULL;
418         }
419
420         sep->stream = NULL;
421
422         if (sep->endpoint && sep->endpoint->clear_configuration)
423                 sep->endpoint->clear_configuration(sep, sep->user_data);
424 }
425
426 static gboolean auto_config(gpointer data)
427 {
428         struct a2dp_setup *setup = data;
429         struct btd_device *dev = NULL;
430         struct btd_service *service;
431
432         /* Check if configuration was aborted */
433         if (setup->sep->stream == NULL)
434                 return FALSE;
435
436         if (setup->err != NULL)
437                 goto done;
438
439         dev = avdtp_get_device(setup->session);
440
441         avdtp_stream_add_cb(setup->session, setup->stream,
442                                 stream_state_changed, setup->sep);
443
444         if (setup->sep->type == AVDTP_SEP_TYPE_SOURCE) {
445                 service = btd_device_get_service(dev, A2DP_SINK_UUID);
446                 sink_new_stream(service, setup->session, setup->stream);
447         } else {
448                 service = btd_device_get_service(dev, A2DP_SOURCE_UUID);
449                 source_new_stream(service, setup->session, setup->stream);
450         }
451
452 done:
453         if (setup->setconf_cb)
454                 setup->setconf_cb(setup->session, setup->stream, setup->err);
455
456         finalize_config(setup);
457
458         if (setup->err) {
459                 g_free(setup->err);
460                 setup->err = NULL;
461         }
462
463         setup_unref(setup);
464
465         return FALSE;
466 }
467
468 static void endpoint_setconf_cb(struct a2dp_setup *setup, gboolean ret)
469 {
470         if (ret == FALSE) {
471                 setup->err = g_new(struct avdtp_error, 1);
472                 avdtp_error_init(setup->err, AVDTP_MEDIA_CODEC,
473                                         AVDTP_UNSUPPORTED_CONFIGURATION);
474         }
475
476         auto_config(setup);
477 }
478
479 static gboolean endpoint_match_codec_ind(struct avdtp *session,
480                                 struct avdtp_media_codec_capability *codec,
481                                 void *user_data)
482 {
483         struct a2dp_sep *sep = user_data;
484         a2dp_vendor_codec_t *remote_codec;
485         a2dp_vendor_codec_t *local_codec;
486         uint8_t *capabilities;
487         size_t length;
488
489         if (codec->media_codec_type != A2DP_CODEC_VENDOR)
490                 return TRUE;
491
492         if (sep->endpoint == NULL)
493                 return FALSE;
494
495         length = sep->endpoint->get_capabilities(sep, &capabilities,
496                                                         sep->user_data);
497         if (length < sizeof(a2dp_vendor_codec_t))
498                 return FALSE;
499
500         local_codec = (a2dp_vendor_codec_t *) capabilities;
501         remote_codec = (a2dp_vendor_codec_t *) codec->data;
502
503         if (remote_codec->vendor_id != local_codec->vendor_id)
504                 return FALSE;
505
506         if (remote_codec->codec_id != local_codec->codec_id)
507                 return FALSE;
508
509         DBG("vendor 0x%08x codec 0x%04x", btohl(remote_codec->vendor_id),
510                                                 btohs(remote_codec->codec_id));
511         return TRUE;
512 }
513
514 static gboolean endpoint_setconf_ind(struct avdtp *session,
515                                                 struct avdtp_local_sep *sep,
516                                                 struct avdtp_stream *stream,
517                                                 GSList *caps,
518                                                 avdtp_set_configuration_cb cb,
519                                                 void *user_data)
520 {
521         struct a2dp_sep *a2dp_sep = user_data;
522         struct a2dp_setup *setup;
523
524         if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
525                 DBG("Sink %p: Set_Configuration_Ind", sep);
526         else
527                 DBG("Source %p: Set_Configuration_Ind", sep);
528
529         setup = a2dp_setup_get(session);
530         if (!session)
531                 return FALSE;
532
533         a2dp_sep->stream = stream;
534         setup->sep = a2dp_sep;
535         setup->stream = stream;
536         setup->setconf_cb = cb;
537
538         for (; caps != NULL; caps = g_slist_next(caps)) {
539                 struct avdtp_service_capability *cap = caps->data;
540                 struct avdtp_media_codec_capability *codec;
541                 gboolean ret;
542
543                 if (cap->category == AVDTP_DELAY_REPORTING &&
544                                         !a2dp_sep->delay_reporting) {
545                         setup->err = g_new(struct avdtp_error, 1);
546                         avdtp_error_init(setup->err, AVDTP_DELAY_REPORTING,
547                                         AVDTP_UNSUPPORTED_CONFIGURATION);
548                         goto done;
549                 }
550
551                 if (cap->category != AVDTP_MEDIA_CODEC)
552                         continue;
553
554                 codec = (struct avdtp_media_codec_capability *) cap->data;
555
556                 if (codec->media_codec_type != a2dp_sep->codec) {
557                         setup->err = g_new(struct avdtp_error, 1);
558                         avdtp_error_init(setup->err, AVDTP_MEDIA_CODEC,
559                                         AVDTP_UNSUPPORTED_CONFIGURATION);
560                         goto done;
561                 }
562
563                 ret = a2dp_sep->endpoint->set_configuration(a2dp_sep,
564                                                 codec->data,
565                                                 cap->length - sizeof(*codec),
566                                                 setup,
567                                                 endpoint_setconf_cb,
568                                                 a2dp_sep->user_data);
569                 if (ret == 0)
570                         return TRUE;
571
572                 setup->err = g_new(struct avdtp_error, 1);
573                 avdtp_error_init(setup->err, AVDTP_MEDIA_CODEC,
574                                         AVDTP_UNSUPPORTED_CONFIGURATION);
575                 break;
576         }
577
578 done:
579         g_idle_add(auto_config, setup);
580         return TRUE;
581 }
582
583 static gboolean endpoint_getcap_ind(struct avdtp *session,
584                                         struct avdtp_local_sep *sep,
585                                         gboolean get_all, GSList **caps,
586                                         uint8_t *err, void *user_data)
587 {
588         struct a2dp_sep *a2dp_sep = user_data;
589         struct avdtp_service_capability *media_transport, *media_codec;
590         struct avdtp_media_codec_capability *codec_caps;
591         uint8_t *capabilities;
592         size_t length;
593
594         if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
595                 DBG("Sink %p: Get_Capability_Ind", sep);
596         else
597                 DBG("Source %p: Get_Capability_Ind", sep);
598
599         *caps = NULL;
600
601         media_transport = avdtp_service_cap_new(AVDTP_MEDIA_TRANSPORT,
602                                                 NULL, 0);
603
604         *caps = g_slist_append(*caps, media_transport);
605
606         length = a2dp_sep->endpoint->get_capabilities(a2dp_sep, &capabilities,
607                                                         a2dp_sep->user_data);
608
609         codec_caps = g_malloc0(sizeof(*codec_caps) + length);
610         codec_caps->media_type = AVDTP_MEDIA_TYPE_AUDIO;
611         codec_caps->media_codec_type = a2dp_sep->codec;
612         memcpy(codec_caps->data, capabilities, length);
613
614         media_codec = avdtp_service_cap_new(AVDTP_MEDIA_CODEC, codec_caps,
615                                                 sizeof(*codec_caps) + length);
616
617         *caps = g_slist_append(*caps, media_codec);
618         g_free(codec_caps);
619
620         if (get_all) {
621                 struct avdtp_service_capability *delay_reporting;
622                 delay_reporting = avdtp_service_cap_new(AVDTP_DELAY_REPORTING,
623                                                                 NULL, 0);
624                 *caps = g_slist_append(*caps, delay_reporting);
625         }
626
627         return TRUE;
628 }
629
630 static void endpoint_open_cb(struct a2dp_setup *setup, gboolean ret)
631 {
632         int err;
633
634         if (ret == FALSE) {
635                 setup->stream = NULL;
636                 finalize_setup_errno(setup, -EPERM, finalize_config, NULL);
637                 return;
638         }
639
640         err = avdtp_open(setup->session, setup->stream);
641         if (err == 0)
642                 return;
643
644         error("Error on avdtp_open %s (%d)", strerror(-err), -err);
645         setup->stream = NULL;
646         finalize_setup_errno(setup, err, finalize_config, NULL);
647 }
648
649 static void setconf_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
650                                 struct avdtp_stream *stream,
651                                 struct avdtp_error *err, void *user_data)
652 {
653         struct a2dp_sep *a2dp_sep = user_data;
654         struct a2dp_setup *setup;
655         struct btd_device *dev;
656         struct btd_service *service;
657         int ret;
658
659         if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
660                 DBG("Sink %p: Set_Configuration_Cfm", sep);
661         else
662                 DBG("Source %p: Set_Configuration_Cfm", sep);
663
664         setup = find_setup_by_session(session);
665
666         if (err) {
667                 if (setup) {
668                         setup_ref(setup);
669                         setup->err = err;
670                         finalize_config(setup);
671                         setup->err = NULL;
672                         setup_unref(setup);
673                 }
674                 return;
675         }
676
677         avdtp_stream_add_cb(session, stream, stream_state_changed, a2dp_sep);
678         a2dp_sep->stream = stream;
679
680         if (!setup)
681                 return;
682
683         dev = avdtp_get_device(session);
684
685         /* Notify D-Bus interface of the new stream */
686         if (a2dp_sep->type == AVDTP_SEP_TYPE_SOURCE) {
687                 service = btd_device_get_service(dev, A2DP_SINK_UUID);
688                 sink_new_stream(service, session, setup->stream);
689         } else {
690                 service = btd_device_get_service(dev, A2DP_SOURCE_UUID);
691                 source_new_stream(service, session, setup->stream);
692         }
693
694         /* Notify Endpoint */
695         if (a2dp_sep->endpoint) {
696                 struct avdtp_service_capability *service;
697                 struct avdtp_media_codec_capability *codec;
698                 int err;
699
700                 service = avdtp_stream_get_codec(stream);
701                 codec = (struct avdtp_media_codec_capability *) service->data;
702
703                 err = a2dp_sep->endpoint->set_configuration(a2dp_sep,
704                                                 codec->data, service->length -
705                                                 sizeof(*codec),
706                                                 setup,
707                                                 endpoint_open_cb,
708                                                 a2dp_sep->user_data);
709                 if (err == 0)
710                         return;
711
712                 setup->stream = NULL;
713                 finalize_setup_errno(setup, -EPERM, finalize_config, NULL);
714                 return;
715         }
716
717         ret = avdtp_open(session, stream);
718         if (ret < 0) {
719                 error("Error on avdtp_open %s (%d)", strerror(-ret), -ret);
720                 setup->stream = NULL;
721                 finalize_setup_errno(setup, ret, finalize_config, NULL);
722         }
723 }
724
725 static gboolean getconf_ind(struct avdtp *session, struct avdtp_local_sep *sep,
726                                 uint8_t *err, void *user_data)
727 {
728         struct a2dp_sep *a2dp_sep = user_data;
729
730         if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
731                 DBG("Sink %p: Get_Configuration_Ind", sep);
732         else
733                 DBG("Source %p: Get_Configuration_Ind", sep);
734         return TRUE;
735 }
736
737 static void getconf_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
738                         struct avdtp_stream *stream, struct avdtp_error *err,
739                         void *user_data)
740 {
741         struct a2dp_sep *a2dp_sep = user_data;
742
743         if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
744                 DBG("Sink %p: Set_Configuration_Cfm", sep);
745         else
746                 DBG("Source %p: Set_Configuration_Cfm", sep);
747 }
748
749 static gboolean open_ind(struct avdtp *session, struct avdtp_local_sep *sep,
750                                 struct avdtp_stream *stream, uint8_t *err,
751                                 void *user_data)
752 {
753         struct a2dp_sep *a2dp_sep = user_data;
754         struct a2dp_setup *setup;
755
756         if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
757                 DBG("Sink %p: Open_Ind", sep);
758         else
759                 DBG("Source %p: Open_Ind", sep);
760
761         setup = a2dp_setup_get(session);
762         if (!setup)
763                 return FALSE;
764
765         setup->stream = stream;
766
767         if (setup->reconfigure)
768                 setup->reconfigure = FALSE;
769
770         finalize_config(setup);
771
772         return TRUE;
773 }
774
775 static void open_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
776                         struct avdtp_stream *stream, struct avdtp_error *err,
777                         void *user_data)
778 {
779         struct a2dp_sep *a2dp_sep = user_data;
780         struct a2dp_setup *setup;
781
782         if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
783                 DBG("Sink %p: Open_Cfm", sep);
784         else
785                 DBG("Source %p: Open_Cfm", sep);
786
787         setup = find_setup_by_session(session);
788         if (!setup)
789                 return;
790
791         if (setup->reconfigure)
792                 setup->reconfigure = FALSE;
793
794         if (err) {
795                 setup->stream = NULL;
796                 setup->err = err;
797                 if (setup->start)
798                         finalize_resume(setup);
799         }
800
801         finalize_config(setup);
802
803         return;
804 }
805
806 static gboolean suspend_timeout(struct a2dp_sep *sep)
807 {
808         if (avdtp_suspend(sep->session, sep->stream) == 0)
809                 sep->suspending = TRUE;
810
811         sep->suspend_timer = 0;
812
813         avdtp_unref(sep->session);
814         sep->session = NULL;
815
816         return FALSE;
817 }
818
819 static gboolean start_ind(struct avdtp *session, struct avdtp_local_sep *sep,
820                                 struct avdtp_stream *stream, uint8_t *err,
821                                 void *user_data)
822 {
823         struct a2dp_sep *a2dp_sep = user_data;
824         struct a2dp_setup *setup;
825
826         if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
827                 DBG("Sink %p: Start_Ind", sep);
828         else
829                 DBG("Source %p: Start_Ind", sep);
830
831 #ifdef __TIZEN_PATCH__
832         if (!a2dp_sep->locked) {
833                 a2dp_sep->session = avdtp_ref(session);
834                 if(a2dp_sep->remote_suspended == FALSE)
835                         a2dp_sep->suspend_timer = g_timeout_add_seconds(SUSPEND_TIMEOUT,
836                                                         (GSourceFunc) suspend_timeout,
837                                                         a2dp_sep);
838                 else
839                         a2dp_sep->remote_suspended = FALSE;
840         }
841 #else
842
843         if (!a2dp_sep->locked) {
844                 a2dp_sep->session = avdtp_ref(session);
845                 a2dp_sep->suspend_timer = g_timeout_add_seconds(SUSPEND_TIMEOUT,
846                                                 (GSourceFunc) suspend_timeout,
847                                                 a2dp_sep);
848         }
849 #endif
850
851         if (!a2dp_sep->starting)
852                 return TRUE;
853
854         a2dp_sep->starting = FALSE;
855
856         setup = find_setup_by_session(session);
857         if (setup)
858                 finalize_resume(setup);
859
860         return TRUE;
861 }
862
863 static void start_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
864                         struct avdtp_stream *stream, struct avdtp_error *err,
865                         void *user_data)
866 {
867         struct a2dp_sep *a2dp_sep = user_data;
868         struct a2dp_setup *setup;
869
870         if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
871                 DBG("Sink %p: Start_Cfm", sep);
872         else
873                 DBG("Source %p: Start_Cfm", sep);
874
875         a2dp_sep->starting = FALSE;
876
877         setup = find_setup_by_session(session);
878         if (!setup)
879                 return;
880
881         if (err) {
882                 setup->stream = NULL;
883                 setup->err = err;
884         }
885
886         finalize_resume(setup);
887 }
888
889 static gboolean suspend_ind(struct avdtp *session, struct avdtp_local_sep *sep,
890                                 struct avdtp_stream *stream, uint8_t *err,
891                                 void *user_data)
892 {
893         struct a2dp_sep *a2dp_sep = user_data;
894         struct a2dp_setup *setup;
895         gboolean start;
896         int start_err;
897
898         if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
899                 DBG("Sink %p: Suspend_Ind", sep);
900         else
901                 DBG("Source %p: Suspend_Ind", sep);
902
903 #ifdef __TIZEN_PATCH__
904         a2dp_sep->remote_suspended = TRUE;
905 #endif
906
907         if (a2dp_sep->suspend_timer) {
908                 g_source_remove(a2dp_sep->suspend_timer);
909                 a2dp_sep->suspend_timer = 0;
910                 avdtp_unref(a2dp_sep->session);
911                 a2dp_sep->session = NULL;
912         }
913
914         if (!a2dp_sep->suspending)
915                 return TRUE;
916
917         a2dp_sep->suspending = FALSE;
918
919         setup = find_setup_by_session(session);
920         if (!setup)
921                 return TRUE;
922
923         start = setup->start;
924         setup->start = FALSE;
925
926         finalize_suspend(setup);
927
928         if (!start)
929                 return TRUE;
930
931         start_err = avdtp_start(session, a2dp_sep->stream);
932         if (start_err < 0 && start_err != -EINPROGRESS) {
933                 error("avdtp_start: %s (%d)", strerror(-start_err),
934                                                                 -start_err);
935                 finalize_setup_errno(setup, start_err, finalize_resume, NULL);
936         }
937
938         return TRUE;
939 }
940
941 static void suspend_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
942                         struct avdtp_stream *stream, struct avdtp_error *err,
943                         void *user_data)
944 {
945         struct a2dp_sep *a2dp_sep = user_data;
946         struct a2dp_setup *setup;
947         gboolean start;
948         int start_err;
949
950         if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
951                 DBG("Sink %p: Suspend_Cfm", sep);
952         else
953                 DBG("Source %p: Suspend_Cfm", sep);
954
955         a2dp_sep->suspending = FALSE;
956
957         setup = find_setup_by_session(session);
958         if (!setup)
959                 return;
960
961         start = setup->start;
962         setup->start = FALSE;
963
964         if (err) {
965                 setup->stream = NULL;
966                 setup->err = err;
967         }
968
969         finalize_suspend(setup);
970
971         if (!start)
972                 return;
973
974         if (err) {
975                 finalize_resume(setup);
976                 return;
977         }
978
979         start_err = avdtp_start(session, a2dp_sep->stream);
980         if (start_err < 0 && start_err != -EINPROGRESS) {
981                 error("avdtp_start: %s (%d)", strerror(-start_err),
982                                                                 -start_err);
983                 finalize_setup_errno(setup, start_err, finalize_suspend, NULL);
984         }
985 }
986
987 static gboolean close_ind(struct avdtp *session, struct avdtp_local_sep *sep,
988                                 struct avdtp_stream *stream, uint8_t *err,
989                                 void *user_data)
990 {
991         struct a2dp_sep *a2dp_sep = user_data;
992         struct a2dp_setup *setup;
993
994         if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
995                 DBG("Sink %p: Close_Ind", sep);
996         else
997                 DBG("Source %p: Close_Ind", sep);
998
999         setup = find_setup_by_session(session);
1000         if (!setup)
1001                 return TRUE;
1002
1003         finalize_setup_errno(setup, -ECONNRESET, finalize_suspend,
1004                                                         finalize_resume, NULL);
1005
1006         return TRUE;
1007 }
1008
1009 static gboolean a2dp_reconfigure(gpointer data)
1010 {
1011         struct a2dp_setup *setup = data;
1012         struct a2dp_sep *sep = setup->sep;
1013         int posix_err;
1014         struct avdtp_media_codec_capability *rsep_codec;
1015         struct avdtp_service_capability *cap;
1016
1017         if (setup->rsep) {
1018                 cap = avdtp_get_codec(setup->rsep);
1019                 rsep_codec = (struct avdtp_media_codec_capability *) cap->data;
1020         }
1021
1022         if (!setup->rsep || sep->codec != rsep_codec->media_codec_type)
1023                 setup->rsep = avdtp_find_remote_sep(setup->session, sep->lsep);
1024
1025         posix_err = avdtp_set_configuration(setup->session, setup->rsep,
1026                                                 sep->lsep,
1027                                                 setup->caps,
1028                                                 &setup->stream);
1029         if (posix_err < 0) {
1030                 error("avdtp_set_configuration: %s", strerror(-posix_err));
1031                 goto failed;
1032         }
1033
1034         return FALSE;
1035
1036 failed:
1037         finalize_setup_errno(setup, posix_err, finalize_config, NULL);
1038         return FALSE;
1039 }
1040
1041 static void close_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
1042                         struct avdtp_stream *stream, struct avdtp_error *err,
1043                         void *user_data)
1044 {
1045         struct a2dp_sep *a2dp_sep = user_data;
1046         struct a2dp_setup *setup;
1047
1048         if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
1049                 DBG("Sink %p: Close_Cfm", sep);
1050         else
1051                 DBG("Source %p: Close_Cfm", sep);
1052
1053         setup = find_setup_by_session(session);
1054         if (!setup)
1055                 return;
1056
1057         if (err) {
1058                 setup->stream = NULL;
1059                 setup->err = err;
1060                 finalize_config(setup);
1061                 return;
1062         }
1063
1064         if (!setup->rsep)
1065                 setup->rsep = avdtp_stream_get_remote_sep(stream);
1066
1067         if (setup->reconfigure)
1068                 g_timeout_add(RECONFIGURE_TIMEOUT, a2dp_reconfigure, setup);
1069 }
1070
1071 static void abort_ind(struct avdtp *session, struct avdtp_local_sep *sep,
1072                                 struct avdtp_stream *stream, uint8_t *err,
1073                                 void *user_data)
1074 {
1075         struct a2dp_sep *a2dp_sep = user_data;
1076         struct a2dp_setup *setup;
1077
1078         if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
1079                 DBG("Sink %p: Abort_Ind", sep);
1080         else
1081                 DBG("Source %p: Abort_Ind", sep);
1082
1083         a2dp_sep->stream = NULL;
1084
1085         setup = find_setup_by_session(session);
1086         if (!setup)
1087                 return;
1088
1089         finalize_setup_errno(setup, -ECONNRESET, finalize_suspend,
1090                                                         finalize_resume,
1091                                                         finalize_config, NULL);
1092
1093         return;
1094 }
1095
1096 static void abort_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
1097                         struct avdtp_stream *stream, struct avdtp_error *err,
1098                         void *user_data)
1099 {
1100         struct a2dp_sep *a2dp_sep = user_data;
1101         struct a2dp_setup *setup;
1102
1103         if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
1104                 DBG("Sink %p: Abort_Cfm", sep);
1105         else
1106                 DBG("Source %p: Abort_Cfm", sep);
1107
1108         setup = find_setup_by_session(session);
1109         if (!setup)
1110                 return;
1111
1112         setup_unref(setup);
1113 }
1114
1115 static gboolean reconf_ind(struct avdtp *session, struct avdtp_local_sep *sep,
1116                                 uint8_t *err, void *user_data)
1117 {
1118         struct a2dp_sep *a2dp_sep = user_data;
1119
1120         if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
1121                 DBG("Sink %p: ReConfigure_Ind", sep);
1122         else
1123                 DBG("Source %p: ReConfigure_Ind", sep);
1124
1125         return TRUE;
1126 }
1127
1128 static gboolean endpoint_delayreport_ind(struct avdtp *session,
1129                                                 struct avdtp_local_sep *sep,
1130                                                 uint8_t rseid, uint16_t delay,
1131                                                 uint8_t *err, void *user_data)
1132 {
1133         struct a2dp_sep *a2dp_sep = user_data;
1134
1135         if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
1136                 DBG("Sink %p: DelayReport_Ind", sep);
1137         else
1138                 DBG("Source %p: DelayReport_Ind", sep);
1139
1140         if (a2dp_sep->endpoint == NULL ||
1141                                 a2dp_sep->endpoint->set_delay == NULL)
1142                 return FALSE;
1143
1144         a2dp_sep->endpoint->set_delay(a2dp_sep, delay, a2dp_sep->user_data);
1145
1146         return TRUE;
1147 }
1148
1149 static void reconf_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
1150                         struct avdtp_stream *stream, struct avdtp_error *err,
1151                         void *user_data)
1152 {
1153         struct a2dp_sep *a2dp_sep = user_data;
1154         struct a2dp_setup *setup;
1155
1156         if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
1157                 DBG("Sink %p: ReConfigure_Cfm", sep);
1158         else
1159                 DBG("Source %p: ReConfigure_Cfm", sep);
1160
1161         setup = find_setup_by_session(session);
1162         if (!setup)
1163                 return;
1164
1165         if (err) {
1166                 setup->stream = NULL;
1167                 setup->err = err;
1168         }
1169
1170         finalize_config(setup);
1171 }
1172
1173 static void delay_report_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
1174                                 struct avdtp_stream *stream,
1175                                 struct avdtp_error *err, void *user_data)
1176 {
1177         struct a2dp_sep *a2dp_sep = user_data;
1178
1179         if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
1180                 DBG("Sink %p: DelayReport_Cfm", sep);
1181         else
1182                 DBG("Source %p: DelayReport_Cfm", sep);
1183 }
1184
1185 static struct avdtp_sep_cfm cfm = {
1186         .set_configuration      = setconf_cfm,
1187         .get_configuration      = getconf_cfm,
1188         .open                   = open_cfm,
1189         .start                  = start_cfm,
1190         .suspend                = suspend_cfm,
1191         .close                  = close_cfm,
1192         .abort                  = abort_cfm,
1193         .reconfigure            = reconf_cfm,
1194         .delay_report           = delay_report_cfm,
1195 };
1196
1197 static struct avdtp_sep_ind endpoint_ind = {
1198         .match_codec            = endpoint_match_codec_ind,
1199         .get_capability         = endpoint_getcap_ind,
1200         .set_configuration      = endpoint_setconf_ind,
1201         .get_configuration      = getconf_ind,
1202         .open                   = open_ind,
1203         .start                  = start_ind,
1204         .suspend                = suspend_ind,
1205         .close                  = close_ind,
1206         .abort                  = abort_ind,
1207         .reconfigure            = reconf_ind,
1208         .delayreport            = endpoint_delayreport_ind,
1209 };
1210
1211 static sdp_record_t *a2dp_record(uint8_t type)
1212 {
1213         sdp_list_t *svclass_id, *pfseq, *apseq, *root;
1214         uuid_t root_uuid, l2cap_uuid, avdtp_uuid, a2dp_uuid;
1215         sdp_profile_desc_t profile[1];
1216         sdp_list_t *aproto, *proto[2];
1217         sdp_record_t *record;
1218         sdp_data_t *psm, *version, *features;
1219         uint16_t lp = AVDTP_UUID;
1220 #ifdef __TIZEN_PATCH__
1221 #ifdef SUPPORT_LOCAL_DEVICE_A2DP_SINK
1222         uint16_t a2dp_ver = 0x0102, avdtp_ver = 0x0103, feat = 0x0002;
1223 #else
1224         uint16_t a2dp_ver = 0x0102, avdtp_ver = 0x0103, feat = 0x0001;
1225 #endif
1226 #else
1227         uint16_t a2dp_ver = 0x0103, avdtp_ver = 0x0103, feat = 0x000f;
1228 #endif
1229
1230         record = sdp_record_alloc();
1231         if (!record)
1232                 return NULL;
1233
1234         sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
1235         root = sdp_list_append(0, &root_uuid);
1236         sdp_set_browse_groups(record, root);
1237
1238         if (type == AVDTP_SEP_TYPE_SOURCE)
1239                 sdp_uuid16_create(&a2dp_uuid, AUDIO_SOURCE_SVCLASS_ID);
1240         else
1241                 sdp_uuid16_create(&a2dp_uuid, AUDIO_SINK_SVCLASS_ID);
1242         svclass_id = sdp_list_append(0, &a2dp_uuid);
1243         sdp_set_service_classes(record, svclass_id);
1244
1245         sdp_uuid16_create(&profile[0].uuid, ADVANCED_AUDIO_PROFILE_ID);
1246         profile[0].version = a2dp_ver;
1247         pfseq = sdp_list_append(0, &profile[0]);
1248         sdp_set_profile_descs(record, pfseq);
1249
1250         sdp_uuid16_create(&l2cap_uuid, L2CAP_UUID);
1251         proto[0] = sdp_list_append(0, &l2cap_uuid);
1252         psm = sdp_data_alloc(SDP_UINT16, &lp);
1253         proto[0] = sdp_list_append(proto[0], psm);
1254         apseq = sdp_list_append(0, proto[0]);
1255
1256         sdp_uuid16_create(&avdtp_uuid, AVDTP_UUID);
1257         proto[1] = sdp_list_append(0, &avdtp_uuid);
1258         version = sdp_data_alloc(SDP_UINT16, &avdtp_ver);
1259         proto[1] = sdp_list_append(proto[1], version);
1260         apseq = sdp_list_append(apseq, proto[1]);
1261
1262         aproto = sdp_list_append(0, apseq);
1263         sdp_set_access_protos(record, aproto);
1264
1265         features = sdp_data_alloc(SDP_UINT16, &feat);
1266         sdp_attr_add(record, SDP_ATTR_SUPPORTED_FEATURES, features);
1267
1268         if (type == AVDTP_SEP_TYPE_SOURCE)
1269                 sdp_set_info_attr(record, "Audio Source", 0, 0);
1270         else
1271                 sdp_set_info_attr(record, "Audio Sink", 0, 0);
1272
1273         free(psm);
1274         free(version);
1275         sdp_list_free(proto[0], 0);
1276         sdp_list_free(proto[1], 0);
1277         sdp_list_free(apseq, 0);
1278         sdp_list_free(pfseq, 0);
1279         sdp_list_free(aproto, 0);
1280         sdp_list_free(root, 0);
1281         sdp_list_free(svclass_id, 0);
1282
1283         return record;
1284 }
1285
1286 static struct a2dp_server *find_server(GSList *list, struct btd_adapter *a)
1287 {
1288
1289         for (; list; list = list->next) {
1290                 struct a2dp_server *server = list->data;
1291
1292                 if (server->adapter == a)
1293                         return server;
1294         }
1295
1296         return NULL;
1297 }
1298
1299 static void channel_free(void *data)
1300 {
1301         struct a2dp_channel *chan = data;
1302
1303         if (chan->auth_id > 0)
1304                 btd_cancel_authorization(chan->auth_id);
1305
1306         if (chan->io_id > 0)
1307                 g_source_remove(chan->io_id);
1308
1309         if (chan->io) {
1310                 g_io_channel_shutdown(chan->io, TRUE, NULL);
1311                 g_io_channel_unref(chan->io);
1312         }
1313
1314         avdtp_remove_state_cb(chan->state_id);
1315
1316         g_free(chan);
1317 }
1318
1319 static void channel_remove(struct a2dp_channel *chan)
1320 {
1321         struct a2dp_server *server = chan->server;
1322
1323         DBG("chan %p", chan);
1324
1325         queue_remove(server->channels, chan);
1326
1327         channel_free(chan);
1328 }
1329
1330 static gboolean disconnect_cb(GIOChannel *io, GIOCondition cond, gpointer data)
1331 {
1332         struct a2dp_channel *chan = data;
1333
1334         DBG("chan %p", chan);
1335
1336         chan->io_id = 0;
1337
1338         channel_remove(chan);
1339
1340         return FALSE;
1341 }
1342
1343 static void avdtp_state_cb(struct btd_device *dev, struct avdtp *session,
1344                                         avdtp_session_state_t old_state,
1345                                         avdtp_session_state_t new_state,
1346                                         void *user_data)
1347 {
1348         struct a2dp_channel *chan = user_data;
1349
1350         switch (new_state) {
1351         case AVDTP_SESSION_STATE_DISCONNECTED:
1352                 if (chan->session == session)
1353                         channel_remove(chan);
1354                 break;
1355         case AVDTP_SESSION_STATE_CONNECTING:
1356                 break;
1357         case AVDTP_SESSION_STATE_CONNECTED:
1358                 break;
1359         }
1360 }
1361
1362 static struct a2dp_channel *channel_new(struct a2dp_server *server,
1363                                         struct btd_device *device,
1364                                         GIOChannel *io)
1365 {
1366         struct a2dp_channel *chan;
1367
1368         chan = g_new0(struct a2dp_channel, 1);
1369         chan->server = server;
1370         chan->device = device;
1371         chan->state_id = avdtp_add_state_cb(device, avdtp_state_cb, chan);
1372
1373         if (!queue_push_tail(server->channels, chan)) {
1374                 g_free(chan);
1375                 return NULL;
1376         }
1377
1378         if (!io)
1379                 return chan;
1380
1381         chan->io = g_io_channel_ref(io);
1382         chan->io_id = g_io_add_watch(io, G_IO_ERR | G_IO_HUP | G_IO_NVAL,
1383                                         (GIOFunc) disconnect_cb, chan);
1384
1385         return chan;
1386 }
1387
1388 static bool match_by_device(const void *data, const void *user_data)
1389 {
1390         const struct a2dp_channel *chan = data;
1391         const struct btd_device *device = user_data;
1392
1393         return chan->device == device;
1394 }
1395
1396 struct avdtp *a2dp_avdtp_get(struct btd_device *device)
1397 {
1398         struct a2dp_server *server;
1399         struct a2dp_channel *chan;
1400
1401         server = find_server(servers, device_get_adapter(device));
1402         if (server == NULL)
1403                 return NULL;
1404
1405         chan = queue_find(server->channels, match_by_device, device);
1406         if (!chan) {
1407                 chan = channel_new(server, device, NULL);
1408                 if (!chan)
1409                         return NULL;
1410         }
1411
1412 #ifdef __TIZEN_PATCH__
1413         if (chan->auth_id) {
1414                 DBG("auth is already going...");
1415                 return NULL;
1416         }
1417 #endif
1418
1419         if (chan->session)
1420                 return avdtp_ref(chan->session);
1421
1422         chan->session = avdtp_new(NULL, device, server->seps);
1423         if (!chan->session) {
1424                 channel_remove(chan);
1425                 return NULL;
1426         }
1427
1428         return avdtp_ref(chan->session);
1429 }
1430
1431 static void connect_cb(GIOChannel *io, GError *err, gpointer user_data)
1432 {
1433         struct a2dp_channel *chan = user_data;
1434
1435         if (err) {
1436                 error("%s", err->message);
1437                 goto fail;
1438         }
1439
1440         chan->session = avdtp_new(chan->io, chan->device, chan->server->seps);
1441         if (!chan->session) {
1442                 error("Unable to create AVDTP session");
1443                 goto fail;
1444         }
1445
1446         g_io_channel_unref(chan->io);
1447         chan->io = NULL;
1448
1449         g_source_remove(chan->io_id);
1450         chan->io_id = 0;
1451
1452         return;
1453
1454 fail:
1455         channel_remove(chan);
1456 }
1457
1458 static void auth_cb(DBusError *derr, void *user_data)
1459 {
1460         struct a2dp_channel *chan = user_data;
1461         GError *err = NULL;
1462
1463         chan->auth_id = 0;
1464
1465         if (derr && dbus_error_is_set(derr)) {
1466                 error("Access denied: %s", derr->message);
1467                 goto fail;
1468         }
1469
1470         if (!bt_io_accept(chan->io, connect_cb, chan, NULL, &err)) {
1471                 error("bt_io_accept: %s", err->message);
1472                 g_error_free(err);
1473                 goto fail;
1474         }
1475
1476         return;
1477
1478 fail:
1479         channel_remove(chan);
1480 }
1481
1482 static void transport_cb(GIOChannel *io, GError *err, gpointer user_data)
1483 {
1484         struct a2dp_setup *setup = user_data;
1485         uint16_t omtu, imtu;
1486
1487         if (err) {
1488                 error("%s", err->message);
1489                 goto drop;
1490         }
1491
1492         bt_io_get(io, &err, BT_IO_OPT_OMTU, &omtu, BT_IO_OPT_IMTU, &imtu,
1493                                                         BT_IO_OPT_INVALID);
1494         if (err) {
1495                 error("%s", err->message);
1496                 g_error_free(err);
1497                 goto drop;
1498         }
1499
1500         if (!avdtp_stream_set_transport(setup->stream,
1501                                         g_io_channel_unix_get_fd(io),
1502                                         imtu, omtu))
1503                 goto drop;
1504
1505         g_io_channel_set_close_on_unref(io, FALSE);
1506
1507         setup_unref(setup);
1508
1509         return;
1510
1511 drop:
1512         setup_unref(setup);
1513         g_io_channel_shutdown(io, TRUE, NULL);
1514
1515 }
1516 static void confirm_cb(GIOChannel *io, gpointer data)
1517 {
1518         struct a2dp_server *server = data;
1519         struct a2dp_channel *chan;
1520         char address[18];
1521         bdaddr_t src, dst;
1522         GError *err = NULL;
1523         struct btd_device *device;
1524
1525         bt_io_get(io, &err,
1526                         BT_IO_OPT_SOURCE_BDADDR, &src,
1527                         BT_IO_OPT_DEST_BDADDR, &dst,
1528                         BT_IO_OPT_DEST, address,
1529                         BT_IO_OPT_INVALID);
1530         if (err) {
1531                 error("%s", err->message);
1532                 g_error_free(err);
1533                 goto drop;
1534         }
1535
1536         DBG("AVDTP: incoming connect from %s", address);
1537
1538         device = btd_adapter_find_device(adapter_find(&src), &dst,
1539                                                                 BDADDR_BREDR);
1540         if (!device)
1541                 goto drop;
1542
1543         chan = queue_find(server->channels, match_by_device, device);
1544         if (chan) {
1545                 struct a2dp_setup *setup;
1546
1547                 setup = find_setup_by_session(chan->session);
1548                 if (!setup || !setup->stream)
1549                         goto drop;
1550
1551                 if (!bt_io_accept(io, transport_cb, setup, NULL, &err)) {
1552                         error("bt_io_accept: %s", err->message);
1553                         g_error_free(err);
1554                         goto drop;
1555                 }
1556
1557                 return;
1558         }
1559
1560         chan = channel_new(server, device, io);
1561         if (!chan)
1562                 goto drop;
1563
1564         chan->auth_id = btd_request_authorization(&src, &dst,
1565                                                         ADVANCED_AUDIO_UUID,
1566                                                         auth_cb, chan);
1567         if (chan->auth_id == 0 && !chan->session)
1568                 channel_remove(chan);
1569
1570         return;
1571
1572 drop:
1573         g_io_channel_shutdown(io, TRUE, NULL);
1574 }
1575
1576 static bool a2dp_server_listen(struct a2dp_server *server)
1577 {
1578         GError *err = NULL;
1579
1580         if (server->io)
1581                 return true;
1582
1583         server->io = bt_io_listen(NULL, confirm_cb, server, NULL, &err,
1584                                 BT_IO_OPT_SOURCE_BDADDR,
1585                                 btd_adapter_get_address(server->adapter),
1586                                 BT_IO_OPT_PSM, AVDTP_PSM,
1587                                 BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_MEDIUM,
1588 #if defined(__TIZEN_PATCH__) && defined(SUPPORT_LOCAL_DEVICE_A2DP_SINK)
1589                                 BT_IO_OPT_IMTU, 895,
1590 #endif
1591                                 BT_IO_OPT_MASTER, true,
1592                                 BT_IO_OPT_INVALID);
1593         if (server->io)
1594                 return true;
1595
1596         error("%s", err->message);
1597         g_error_free(err);
1598
1599         return false;
1600 }
1601
1602 static struct a2dp_server *a2dp_server_register(struct btd_adapter *adapter)
1603 {
1604         struct a2dp_server *server;
1605
1606         server = g_new0(struct a2dp_server, 1);
1607
1608         server->adapter = btd_adapter_ref(adapter);
1609         server->seps = queue_new();
1610         server->channels = queue_new();
1611
1612         servers = g_slist_append(servers, server);
1613
1614         return server;
1615 }
1616
1617 static void a2dp_unregister_sep(struct a2dp_sep *sep)
1618 {
1619         struct a2dp_server *server = sep->server;
1620
1621         if (sep->destroy) {
1622                 sep->destroy(sep->user_data);
1623                 sep->endpoint = NULL;
1624         }
1625
1626         avdtp_unregister_sep(server->seps, sep->lsep);
1627
1628         g_free(sep);
1629
1630         if (!queue_isempty(server->seps))
1631                 return;
1632
1633         if (server->io) {
1634                 g_io_channel_shutdown(server->io, TRUE, NULL);
1635                 g_io_channel_unref(server->io);
1636                 server->io = NULL;
1637         }
1638 }
1639
1640 static void a2dp_server_unregister(struct a2dp_server *server)
1641 {
1642         servers = g_slist_remove(servers, server);
1643         queue_destroy(server->channels, channel_free);
1644         queue_destroy(server->seps, NULL);
1645
1646         if (server->io) {
1647                 g_io_channel_shutdown(server->io, TRUE, NULL);
1648                 g_io_channel_unref(server->io);
1649         }
1650
1651         btd_adapter_unref(server->adapter);
1652         g_free(server);
1653 }
1654
1655 struct a2dp_sep *a2dp_add_sep(struct btd_adapter *adapter, uint8_t type,
1656                                 uint8_t codec, gboolean delay_reporting,
1657                                 struct a2dp_endpoint *endpoint,
1658                                 void *user_data, GDestroyNotify destroy,
1659                                 int *err)
1660 {
1661         struct a2dp_server *server;
1662         struct a2dp_sep *sep;
1663         GSList **l;
1664         uint32_t *record_id;
1665         sdp_record_t *record;
1666
1667         server = find_server(servers, adapter);
1668         if (server == NULL) {
1669                 if (err)
1670                         *err = -EPROTONOSUPPORT;
1671                 return NULL;
1672         }
1673
1674         if (type == AVDTP_SEP_TYPE_SINK && !server->sink_enabled) {
1675                 if (err)
1676                         *err = -EPROTONOSUPPORT;
1677                 return NULL;
1678         }
1679
1680         if (type == AVDTP_SEP_TYPE_SOURCE && !server->source_enabled) {
1681                 if (err)
1682                         *err = -EPROTONOSUPPORT;
1683                 return NULL;
1684         }
1685
1686         sep = g_new0(struct a2dp_sep, 1);
1687
1688         sep->lsep = avdtp_register_sep(server->seps, type,
1689                                         AVDTP_MEDIA_TYPE_AUDIO, codec,
1690                                         delay_reporting, &endpoint_ind,
1691                                         &cfm, sep);
1692
1693         if (sep->lsep == NULL) {
1694                 g_free(sep);
1695                 if (err)
1696                         *err = -EINVAL;
1697                 return NULL;
1698         }
1699
1700         sep->server = server;
1701         sep->endpoint = endpoint;
1702         sep->codec = codec;
1703         sep->type = type;
1704         sep->delay_reporting = delay_reporting;
1705         sep->user_data = user_data;
1706         sep->destroy = destroy;
1707
1708         if (type == AVDTP_SEP_TYPE_SOURCE) {
1709                 l = &server->sources;
1710                 record_id = &server->source_record_id;
1711         } else {
1712                 l = &server->sinks;
1713                 record_id = &server->sink_record_id;
1714         }
1715
1716         if (*record_id != 0)
1717                 goto add;
1718
1719         record = a2dp_record(type);
1720         if (!record) {
1721                 error("Unable to allocate new service record");
1722                 a2dp_unregister_sep(sep);
1723                 if (err)
1724                         *err = -EINVAL;
1725                 return NULL;
1726         }
1727
1728         if (adapter_service_add(server->adapter, record) < 0) {
1729                 error("Unable to register A2DP service record");
1730                 sdp_record_free(record);
1731                 a2dp_unregister_sep(sep);
1732                 if (err)
1733                         *err = -EINVAL;
1734                 return NULL;
1735         }
1736
1737         if (!a2dp_server_listen(server)) {
1738                 sdp_record_free(record);
1739                 a2dp_unregister_sep(sep);
1740                 if (err)
1741                         *err = -EINVAL;
1742                 return NULL;
1743         }
1744
1745         *record_id = record->handle;
1746
1747 add:
1748         *l = g_slist_append(*l, sep);
1749
1750         if (err)
1751                 *err = 0;
1752         return sep;
1753 }
1754
1755 void a2dp_remove_sep(struct a2dp_sep *sep)
1756 {
1757         struct a2dp_server *server = sep->server;
1758
1759         if (sep->type == AVDTP_SEP_TYPE_SOURCE) {
1760                 if (g_slist_find(server->sources, sep) == NULL)
1761                         return;
1762                 server->sources = g_slist_remove(server->sources, sep);
1763                 if (server->sources == NULL && server->source_record_id) {
1764                         adapter_service_remove(server->adapter,
1765                                                 server->source_record_id);
1766                         server->source_record_id = 0;
1767                 }
1768         } else {
1769                 if (g_slist_find(server->sinks, sep) == NULL)
1770                         return;
1771                 server->sinks = g_slist_remove(server->sinks, sep);
1772                 if (server->sinks == NULL && server->sink_record_id) {
1773                         adapter_service_remove(server->adapter,
1774                                                 server->sink_record_id);
1775                         server->sink_record_id = 0;
1776                 }
1777         }
1778
1779         if (sep->locked)
1780                 return;
1781
1782         a2dp_unregister_sep(sep);
1783 }
1784
1785 static void select_cb(struct a2dp_setup *setup, void *ret, int size)
1786 {
1787         struct avdtp_service_capability *media_transport, *media_codec;
1788         struct avdtp_media_codec_capability *cap;
1789
1790         if (size < 0) {
1791                 DBG("Endpoint replied an invalid configuration");
1792                 goto done;
1793         }
1794
1795         media_transport = avdtp_service_cap_new(AVDTP_MEDIA_TRANSPORT,
1796                                                 NULL, 0);
1797
1798         setup->caps = g_slist_append(setup->caps, media_transport);
1799
1800         cap = g_malloc0(sizeof(*cap) + size);
1801         cap->media_type = AVDTP_MEDIA_TYPE_AUDIO;
1802         cap->media_codec_type = setup->sep->codec;
1803         memcpy(cap->data, ret, size);
1804
1805         media_codec = avdtp_service_cap_new(AVDTP_MEDIA_CODEC, cap,
1806                                                 sizeof(*cap) + size);
1807
1808         setup->caps = g_slist_append(setup->caps, media_codec);
1809         g_free(cap);
1810
1811 done:
1812         finalize_select(setup);
1813 }
1814
1815 static struct a2dp_sep *a2dp_find_sep(struct avdtp *session, GSList *list,
1816                                         const char *sender)
1817 {
1818 #ifdef __TIZEN_PATCH__
1819         struct a2dp_sep *selected_sep = NULL;
1820 #endif
1821
1822         for (; list; list = list->next) {
1823                 struct a2dp_sep *sep = list->data;
1824 #ifdef __TIZEN_PATCH__
1825                 struct avdtp_remote_sep *rsep;
1826                 struct avdtp_media_codec_capability *cap;
1827                 struct avdtp_service_capability *service;
1828 #endif
1829
1830                 /* Use sender's endpoint if available */
1831                 if (sender) {
1832                         const char *name;
1833
1834                         if (sep->endpoint == NULL)
1835                                 continue;
1836
1837                         name = sep->endpoint->get_name(sep, sep->user_data);
1838                         if (g_strcmp0(sender, name) != 0)
1839                                 continue;
1840                 }
1841
1842 #ifdef __TIZEN_PATCH__
1843                 rsep = avdtp_find_remote_sep(session, sep->lsep);
1844                 if (rsep == NULL)
1845                         continue;
1846
1847                 service = avdtp_get_codec(rsep);
1848                 cap = (struct avdtp_media_codec_capability *) service->data;
1849
1850                 if (cap->media_codec_type != A2DP_CODEC_VENDOR) {
1851                         selected_sep = sep;
1852                         continue;
1853                 }
1854 #else
1855                 if (avdtp_find_remote_sep(session, sep->lsep) == NULL)
1856                         continue;
1857
1858 #endif
1859                 return sep;
1860
1861         }
1862
1863 #ifdef __TIZEN_PATCH__
1864         if (selected_sep)
1865                 return selected_sep;
1866         else
1867                 return NULL;
1868 #else
1869         return NULL;
1870 #endif
1871 }
1872
1873 static struct a2dp_sep *a2dp_select_sep(struct avdtp *session, uint8_t type,
1874                                         const char *sender)
1875 {
1876         struct a2dp_server *server;
1877         struct a2dp_sep *sep;
1878         GSList *l;
1879
1880         server = find_server(servers, avdtp_get_adapter(session));
1881         if (!server)
1882                 return NULL;
1883
1884         l = type == AVDTP_SEP_TYPE_SINK ? server->sources : server->sinks;
1885
1886         /* Check sender's seps first */
1887         sep = a2dp_find_sep(session, l, sender);
1888         if (sep != NULL)
1889                 return sep;
1890
1891         return a2dp_find_sep(session, l, NULL);
1892 }
1893
1894 static void discover_cb(struct avdtp *session, GSList *seps,
1895                                 struct avdtp_error *err, void *user_data)
1896 {
1897         struct a2dp_setup *setup = user_data;
1898
1899         DBG("err %p", err);
1900
1901         setup->seps = seps;
1902         setup->err = err;
1903
1904         finalize_discover(setup);
1905 }
1906
1907 unsigned int a2dp_discover(struct avdtp *session, a2dp_discover_cb_t cb,
1908                                                         void *user_data)
1909 {
1910         struct a2dp_setup *setup;
1911         struct a2dp_setup_cb *cb_data;
1912
1913         setup = a2dp_setup_get(session);
1914         if (!setup)
1915                 return 0;
1916
1917         cb_data = setup_cb_new(setup);
1918         cb_data->discover_cb = cb;
1919         cb_data->user_data = user_data;
1920
1921         if (avdtp_discover(session, discover_cb, setup) == 0)
1922                 return cb_data->id;
1923
1924         setup_cb_free(cb_data);
1925         return 0;
1926 }
1927
1928 unsigned int a2dp_select_capabilities(struct avdtp *session,
1929                                         uint8_t type, const char *sender,
1930                                         a2dp_select_cb_t cb,
1931                                         void *user_data)
1932 {
1933         struct a2dp_setup *setup;
1934         struct a2dp_setup_cb *cb_data;
1935         struct a2dp_sep *sep;
1936         struct avdtp_service_capability *service;
1937         struct avdtp_media_codec_capability *codec;
1938         int err;
1939
1940         sep = a2dp_select_sep(session, type, sender);
1941         if (!sep) {
1942                 error("Unable to select SEP");
1943                 return 0;
1944         }
1945
1946         setup = a2dp_setup_get(session);
1947         if (!setup)
1948                 return 0;
1949
1950         cb_data = setup_cb_new(setup);
1951         cb_data->select_cb = cb;
1952         cb_data->user_data = user_data;
1953
1954         setup->sep = sep;
1955         setup->rsep = avdtp_find_remote_sep(session, sep->lsep);
1956
1957         if (setup->rsep == NULL) {
1958                 error("Could not find remote sep");
1959                 goto fail;
1960         }
1961
1962         service = avdtp_get_codec(setup->rsep);
1963         codec = (struct avdtp_media_codec_capability *) service->data;
1964
1965         err = sep->endpoint->select_configuration(sep, codec->data,
1966                                         service->length - sizeof(*codec),
1967                                         setup,
1968                                         select_cb, sep->user_data);
1969         if (err == 0)
1970                 return cb_data->id;
1971
1972 fail:
1973         setup_cb_free(cb_data);
1974         return 0;
1975
1976 }
1977
1978 unsigned int a2dp_config(struct avdtp *session, struct a2dp_sep *sep,
1979                                 a2dp_config_cb_t cb, GSList *caps,
1980                                 void *user_data)
1981 {
1982         struct a2dp_setup_cb *cb_data;
1983         GSList *l;
1984         struct a2dp_server *server;
1985         struct a2dp_setup *setup;
1986         struct a2dp_sep *tmp;
1987         struct avdtp_service_capability *cap;
1988         struct avdtp_media_codec_capability *codec_cap = NULL;
1989         int posix_err;
1990
1991         server = find_server(servers, avdtp_get_adapter(session));
1992         if (!server)
1993                 return 0;
1994
1995         for (l = caps; l != NULL; l = l->next) {
1996                 cap = l->data;
1997
1998                 if (cap->category != AVDTP_MEDIA_CODEC)
1999                         continue;
2000
2001                 codec_cap = (void *) cap->data;
2002                 break;
2003         }
2004
2005         if (!codec_cap)
2006                 return 0;
2007
2008         if (sep->codec != codec_cap->media_codec_type)
2009                 return 0;
2010
2011         DBG("a2dp_config: selected SEP %p", sep->lsep);
2012
2013         setup = a2dp_setup_get(session);
2014         if (!setup)
2015                 return 0;
2016
2017         cb_data = setup_cb_new(setup);
2018         cb_data->config_cb = cb;
2019         cb_data->user_data = user_data;
2020
2021         setup->sep = sep;
2022         setup->stream = sep->stream;
2023
2024         /* Copy given caps if they are different than current caps */
2025         if (setup->caps != caps) {
2026                 g_slist_free_full(setup->caps, g_free);
2027                 setup->caps = g_slist_copy(caps);
2028         }
2029
2030         switch (avdtp_sep_get_state(sep->lsep)) {
2031         case AVDTP_STATE_IDLE:
2032                 if (sep->type == AVDTP_SEP_TYPE_SOURCE)
2033                         l = server->sources;
2034                 else
2035                         l = server->sinks;
2036
2037                 for (; l != NULL; l = l->next) {
2038                         tmp = l->data;
2039                         if (avdtp_has_stream(session, tmp->stream))
2040                                 break;
2041                 }
2042
2043                 if (l != NULL) {
2044                         if (tmp->locked)
2045                                 goto failed;
2046                         setup->reconfigure = TRUE;
2047                         if (avdtp_close(session, tmp->stream, FALSE) < 0) {
2048                                 error("avdtp_close failed");
2049                                 goto failed;
2050                         }
2051                         break;
2052                 }
2053
2054                 setup->rsep = avdtp_find_remote_sep(session, sep->lsep);
2055                 if (setup->rsep == NULL) {
2056                         error("No matching ACP and INT SEPs found");
2057                         goto failed;
2058                 }
2059
2060                 posix_err = avdtp_set_configuration(session, setup->rsep,
2061                                                         sep->lsep, caps,
2062                                                         &setup->stream);
2063                 if (posix_err < 0) {
2064                         error("avdtp_set_configuration: %s",
2065                                 strerror(-posix_err));
2066                         goto failed;
2067                 }
2068                 break;
2069         case AVDTP_STATE_OPEN:
2070         case AVDTP_STATE_STREAMING:
2071                 if (avdtp_stream_has_capabilities(setup->stream, caps)) {
2072                         DBG("Configuration match: resuming");
2073                         cb_data->source_id = g_idle_add(finalize_config,
2074                                                                 setup);
2075                 } else if (!setup->reconfigure) {
2076                         setup->reconfigure = TRUE;
2077                         if (avdtp_close(session, sep->stream, FALSE) < 0) {
2078                                 error("avdtp_close failed");
2079                                 goto failed;
2080                         }
2081                 }
2082                 break;
2083         case AVDTP_STATE_CONFIGURED:
2084         case AVDTP_STATE_CLOSING:
2085         case AVDTP_STATE_ABORTING:
2086         default:
2087                 error("SEP in bad state for requesting a new stream");
2088                 goto failed;
2089         }
2090
2091         return cb_data->id;
2092
2093 failed:
2094         setup_cb_free(cb_data);
2095         return 0;
2096 }
2097
2098 unsigned int a2dp_resume(struct avdtp *session, struct a2dp_sep *sep,
2099                                 a2dp_stream_cb_t cb, void *user_data)
2100 {
2101         struct a2dp_setup_cb *cb_data;
2102         struct a2dp_setup *setup;
2103
2104         setup = a2dp_setup_get(session);
2105         if (!setup)
2106                 return 0;
2107
2108         cb_data = setup_cb_new(setup);
2109         cb_data->resume_cb = cb;
2110         cb_data->user_data = user_data;
2111
2112         setup->sep = sep;
2113         setup->stream = sep->stream;
2114
2115         switch (avdtp_sep_get_state(sep->lsep)) {
2116         case AVDTP_STATE_IDLE:
2117                 goto failed;
2118                 break;
2119         case AVDTP_STATE_CONFIGURED:
2120                 setup->start = TRUE;
2121                 break;
2122         case AVDTP_STATE_OPEN:
2123                 if (avdtp_start(session, sep->stream) < 0) {
2124                         error("avdtp_start failed");
2125                         goto failed;
2126                 }
2127                 sep->starting = TRUE;
2128                 break;
2129         case AVDTP_STATE_STREAMING:
2130                 if (!sep->suspending && sep->suspend_timer) {
2131                         g_source_remove(sep->suspend_timer);
2132                         sep->suspend_timer = 0;
2133                         avdtp_unref(sep->session);
2134                         sep->session = NULL;
2135                 }
2136                 if (sep->suspending)
2137                         setup->start = TRUE;
2138                 else
2139                         cb_data->source_id = g_idle_add(finalize_resume,
2140                                                                 setup);
2141                 break;
2142         case AVDTP_STATE_CLOSING:
2143         case AVDTP_STATE_ABORTING:
2144         default:
2145                 error("SEP in bad state for resume");
2146                 goto failed;
2147         }
2148
2149         return cb_data->id;
2150
2151 failed:
2152         setup_cb_free(cb_data);
2153         return 0;
2154 }
2155
2156 unsigned int a2dp_suspend(struct avdtp *session, struct a2dp_sep *sep,
2157                                 a2dp_stream_cb_t cb, void *user_data)
2158 {
2159         struct a2dp_setup_cb *cb_data;
2160         struct a2dp_setup *setup;
2161
2162         setup = a2dp_setup_get(session);
2163         if (!setup)
2164                 return 0;
2165
2166         cb_data = setup_cb_new(setup);
2167         cb_data->suspend_cb = cb;
2168         cb_data->user_data = user_data;
2169
2170         setup->sep = sep;
2171         setup->stream = sep->stream;
2172
2173         switch (avdtp_sep_get_state(sep->lsep)) {
2174         case AVDTP_STATE_IDLE:
2175                 error("a2dp_suspend: no stream to suspend");
2176                 goto failed;
2177                 break;
2178         case AVDTP_STATE_OPEN:
2179                 cb_data->source_id = g_idle_add(finalize_suspend, setup);
2180                 break;
2181         case AVDTP_STATE_STREAMING:
2182                 if (avdtp_suspend(session, sep->stream) < 0) {
2183                         error("avdtp_suspend failed");
2184                         goto failed;
2185                 }
2186                 sep->suspending = TRUE;
2187                 break;
2188         case AVDTP_STATE_CONFIGURED:
2189         case AVDTP_STATE_CLOSING:
2190         case AVDTP_STATE_ABORTING:
2191         default:
2192                 error("SEP in bad state for suspend");
2193                 goto failed;
2194         }
2195
2196         return cb_data->id;
2197
2198 failed:
2199         setup_cb_free(cb_data);
2200         return 0;
2201 }
2202
2203 gboolean a2dp_cancel(unsigned int id)
2204 {
2205         GSList *ls;
2206
2207         for (ls = setups; ls != NULL; ls = g_slist_next(ls)) {
2208                 struct a2dp_setup *setup = ls->data;
2209                 GSList *l;
2210                 for (l = setup->cb; l != NULL; l = g_slist_next(l)) {
2211                         struct a2dp_setup_cb *cb = l->data;
2212
2213                         if (cb->id != id)
2214                                 continue;
2215
2216                         setup_ref(setup);
2217                         setup_cb_free(cb);
2218
2219                         if (!setup->cb) {
2220                                 DBG("aborting setup %p", setup);
2221                                 if (!avdtp_abort(setup->session, setup->stream))
2222                                         return TRUE;
2223                         }
2224
2225                         setup_unref(setup);
2226                         return TRUE;
2227                 }
2228         }
2229
2230         return FALSE;
2231 }
2232
2233 gboolean a2dp_sep_lock(struct a2dp_sep *sep, struct avdtp *session)
2234 {
2235         if (sep->locked)
2236                 return FALSE;
2237
2238         DBG("SEP %p locked", sep->lsep);
2239         sep->locked = TRUE;
2240
2241         return TRUE;
2242 }
2243
2244 gboolean a2dp_sep_unlock(struct a2dp_sep *sep, struct avdtp *session)
2245 {
2246         struct a2dp_server *server = sep->server;
2247         avdtp_state_t state;
2248         GSList *l;
2249
2250         state = avdtp_sep_get_state(sep->lsep);
2251
2252         sep->locked = FALSE;
2253
2254         DBG("SEP %p unlocked", sep->lsep);
2255
2256         if (sep->type == AVDTP_SEP_TYPE_SOURCE)
2257                 l = server->sources;
2258         else
2259                 l = server->sinks;
2260
2261         /* Unregister sep if it was removed */
2262         if (g_slist_find(l, sep) == NULL) {
2263                 a2dp_unregister_sep(sep);
2264                 return TRUE;
2265         }
2266
2267         if (!sep->stream || state == AVDTP_STATE_IDLE)
2268                 return TRUE;
2269
2270         switch (state) {
2271         case AVDTP_STATE_OPEN:
2272                 /* Set timer here */
2273                 break;
2274         case AVDTP_STATE_STREAMING:
2275                 if (avdtp_suspend(session, sep->stream) == 0)
2276                         sep->suspending = TRUE;
2277                 break;
2278         case AVDTP_STATE_IDLE:
2279         case AVDTP_STATE_CONFIGURED:
2280         case AVDTP_STATE_CLOSING:
2281         case AVDTP_STATE_ABORTING:
2282         default:
2283                 break;
2284         }
2285
2286         return TRUE;
2287 }
2288
2289 struct avdtp_stream *a2dp_sep_get_stream(struct a2dp_sep *sep)
2290 {
2291         return sep->stream;
2292 }
2293
2294 struct btd_device *a2dp_setup_get_device(struct a2dp_setup *setup)
2295 {
2296         if (setup->session == NULL)
2297                 return NULL;
2298
2299         return avdtp_get_device(setup->session);
2300 }
2301
2302 static int a2dp_source_probe(struct btd_service *service)
2303 {
2304         struct btd_device *dev = btd_service_get_device(service);
2305
2306         DBG("path %s", device_get_path(dev));
2307
2308         source_init(service);
2309
2310         return 0;
2311 }
2312
2313 static void a2dp_source_remove(struct btd_service *service)
2314 {
2315         source_unregister(service);
2316 }
2317
2318 static int a2dp_sink_probe(struct btd_service *service)
2319 {
2320         struct btd_device *dev = btd_service_get_device(service);
2321
2322         DBG("path %s", device_get_path(dev));
2323
2324         return sink_init(service);
2325 }
2326
2327 static void a2dp_sink_remove(struct btd_service *service)
2328 {
2329         sink_unregister(service);
2330 }
2331
2332 static int a2dp_source_connect(struct btd_service *service)
2333 {
2334         struct btd_device *dev = btd_service_get_device(service);
2335         struct btd_adapter *adapter = device_get_adapter(dev);
2336         struct a2dp_server *server;
2337         const char *path = device_get_path(dev);
2338
2339         DBG("path %s", path);
2340
2341         server = find_server(servers, adapter);
2342         if (!server || !server->sink_enabled) {
2343                 DBG("Unexpected error: cannot find server");
2344                 return -EPROTONOSUPPORT;
2345         }
2346
2347         /* Return protocol not available if no record/endpoint exists */
2348         if (server->sink_record_id == 0)
2349                 return -ENOPROTOOPT;
2350
2351         return source_connect(service);
2352 }
2353
2354 static int a2dp_source_disconnect(struct btd_service *service)
2355 {
2356         struct btd_device *dev = btd_service_get_device(service);
2357         const char *path = device_get_path(dev);
2358
2359         DBG("path %s", path);
2360
2361         return source_disconnect(service);
2362 }
2363
2364 static int a2dp_sink_connect(struct btd_service *service)
2365 {
2366         struct btd_device *dev = btd_service_get_device(service);
2367         struct btd_adapter *adapter = device_get_adapter(dev);
2368         struct a2dp_server *server;
2369         const char *path = device_get_path(dev);
2370
2371         DBG("path %s", path);
2372
2373         server = find_server(servers, adapter);
2374         if (!server || !server->source_enabled) {
2375                 DBG("Unexpected error: cannot find server");
2376                 return -EPROTONOSUPPORT;
2377         }
2378
2379         /* Return protocol not available if no record/endpoint exists */
2380         if (server->source_record_id == 0)
2381                 return -ENOPROTOOPT;
2382
2383         return sink_connect(service);
2384 }
2385
2386 static int a2dp_sink_disconnect(struct btd_service *service)
2387 {
2388         struct btd_device *dev = btd_service_get_device(service);
2389         const char *path = device_get_path(dev);
2390
2391         DBG("path %s", path);
2392
2393         return sink_disconnect(service);
2394 }
2395
2396 static int a2dp_source_server_probe(struct btd_profile *p,
2397                                                 struct btd_adapter *adapter)
2398 {
2399         struct a2dp_server *server;
2400
2401         DBG("path %s", adapter_get_path(adapter));
2402
2403         server = find_server(servers, adapter);
2404         if (server != NULL)
2405                 goto done;
2406
2407         server = a2dp_server_register(adapter);
2408         if (server == NULL)
2409                 return -EPROTONOSUPPORT;
2410
2411 done:
2412         server->source_enabled = TRUE;
2413
2414         return 0;
2415 }
2416
2417 static void a2dp_source_server_remove(struct btd_profile *p,
2418                                                 struct btd_adapter *adapter)
2419 {
2420         struct a2dp_server *server;
2421
2422         DBG("path %s", adapter_get_path(adapter));
2423
2424         server = find_server(servers, adapter);
2425         if (!server)
2426                 return;
2427
2428         g_slist_free_full(server->sources,
2429                                         (GDestroyNotify) a2dp_unregister_sep);
2430
2431         if (server->source_record_id) {
2432                 adapter_service_remove(server->adapter,
2433                                         server->source_record_id);
2434                 server->source_record_id = 0;
2435         }
2436
2437         if (server->sink_record_id)
2438                 return;
2439
2440         a2dp_server_unregister(server);
2441 }
2442
2443 static int a2dp_sink_server_probe(struct btd_profile *p,
2444                                                 struct btd_adapter *adapter)
2445 {
2446         struct a2dp_server *server;
2447
2448         DBG("path %s", adapter_get_path(adapter));
2449
2450         server = find_server(servers, adapter);
2451         if (server != NULL)
2452                 goto done;
2453
2454         server = a2dp_server_register(adapter);
2455         if (server == NULL)
2456                 return -EPROTONOSUPPORT;
2457
2458 done:
2459         server->sink_enabled = TRUE;
2460
2461         return 0;
2462 }
2463
2464 static void a2dp_sink_server_remove(struct btd_profile *p,
2465                                                 struct btd_adapter *adapter)
2466 {
2467         struct a2dp_server *server;
2468
2469         DBG("path %s", adapter_get_path(adapter));
2470
2471         server = find_server(servers, adapter);
2472         if (!server)
2473                 return;
2474
2475         g_slist_free_full(server->sinks, (GDestroyNotify) a2dp_unregister_sep);
2476
2477         if (server->sink_record_id) {
2478                 adapter_service_remove(server->adapter, server->sink_record_id);
2479                 server->sink_record_id = 0;
2480         }
2481
2482         if (server->source_record_id)
2483                 return;
2484
2485         a2dp_server_unregister(server);
2486 }
2487
2488 static int media_server_probe(struct btd_adapter *adapter)
2489 {
2490         DBG("path %s", adapter_get_path(adapter));
2491
2492         return media_register(adapter);
2493 }
2494
2495 static void media_server_remove(struct btd_adapter *adapter)
2496 {
2497         DBG("path %s", adapter_get_path(adapter));
2498
2499         media_unregister(adapter);
2500 }
2501
2502 static struct btd_profile a2dp_source_profile = {
2503         .name           = "a2dp-source",
2504         .priority       = BTD_PROFILE_PRIORITY_MEDIUM,
2505
2506         .remote_uuid    = A2DP_SOURCE_UUID,
2507         .device_probe   = a2dp_source_probe,
2508         .device_remove  = a2dp_source_remove,
2509
2510         .auto_connect   = true,
2511         .connect        = a2dp_source_connect,
2512         .disconnect     = a2dp_source_disconnect,
2513
2514         .adapter_probe  = a2dp_sink_server_probe,
2515         .adapter_remove = a2dp_sink_server_remove,
2516 };
2517
2518 static struct btd_profile a2dp_sink_profile = {
2519         .name           = "a2dp-sink",
2520         .priority       = BTD_PROFILE_PRIORITY_MEDIUM,
2521
2522         .remote_uuid    = A2DP_SINK_UUID,
2523         .device_probe   = a2dp_sink_probe,
2524         .device_remove  = a2dp_sink_remove,
2525
2526         .auto_connect   = true,
2527         .connect        = a2dp_sink_connect,
2528         .disconnect     = a2dp_sink_disconnect,
2529
2530         .adapter_probe  = a2dp_source_server_probe,
2531         .adapter_remove = a2dp_source_server_remove,
2532 };
2533
2534 static struct btd_adapter_driver media_driver = {
2535         .name           = "media",
2536         .probe          = media_server_probe,
2537         .remove         = media_server_remove,
2538 };
2539
2540 static int a2dp_init(void)
2541 {
2542         btd_register_adapter_driver(&media_driver);
2543 #if defined(__TIZEN_PATCH__) && defined(SUPPORT_LOCAL_DEVICE_A2DP_SINK)
2544         btd_profile_register(&a2dp_source_profile);
2545 #endif
2546         btd_profile_register(&a2dp_sink_profile);
2547
2548         return 0;
2549 }
2550
2551 static void a2dp_exit(void)
2552 {
2553         btd_unregister_adapter_driver(&media_driver);
2554         btd_profile_unregister(&a2dp_source_profile);
2555         btd_profile_unregister(&a2dp_sink_profile);
2556 }
2557
2558 BLUETOOTH_PLUGIN_DEFINE(a2dp, VERSION, BLUETOOTH_PLUGIN_PRIORITY_DEFAULT,
2559                                                         a2dp_init, a2dp_exit)