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