3 * BlueZ - Bluetooth protocol stack for Linux
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.
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.
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.
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
33 #include <dbus/dbus.h>
36 #include "lib/bluetooth.h"
38 #include "lib/sdp_lib.h"
41 #include "src/plugin.h"
42 #include "src/adapter.h"
43 #include "src/device.h"
44 #include "src/profile.h"
45 #include "src/service.h"
48 #include "src/shared/queue.h"
50 #include "btio/btio.h"
56 #include "a2dp-codecs.h"
59 /* The duration that streams without users are allowed to stay in
61 #define SUSPEND_TIMEOUT 5
62 #define RECONFIGURE_TIMEOUT 500
67 struct a2dp_server *server;
68 struct a2dp_endpoint *endpoint;
71 struct avdtp_local_sep *lsep;
72 struct avdtp *session;
73 struct avdtp_stream *stream;
75 gboolean delay_reporting;
80 GDestroyNotify destroy;
83 struct a2dp_setup_cb {
84 struct a2dp_setup *setup;
85 a2dp_select_cb_t select_cb;
86 a2dp_config_cb_t config_cb;
87 a2dp_stream_cb_t resume_cb;
88 a2dp_stream_cb_t suspend_cb;
95 struct avdtp *session;
97 struct avdtp_remote_sep *rsep;
98 struct avdtp_stream *stream;
99 struct avdtp_error *err;
100 avdtp_set_configuration_cb setconf_cb;
102 gboolean reconfigure;
109 struct btd_adapter *adapter;
112 uint32_t source_record_id;
113 uint32_t sink_record_id;
114 gboolean sink_enabled;
115 gboolean source_enabled;
118 struct queue *channels;
121 struct a2dp_channel {
122 struct a2dp_server *server;
123 struct btd_device *device;
126 unsigned int state_id;
127 unsigned int auth_id;
128 struct avdtp *session;
131 static GSList *servers = NULL;
132 static GSList *setups = NULL;
133 static unsigned int cb_id = 0;
135 static struct a2dp_setup *setup_ref(struct a2dp_setup *setup)
139 DBG("%p: ref=%d", setup, setup->ref);
144 static struct a2dp_setup *setup_new(struct avdtp *session)
146 struct a2dp_setup *setup;
148 setup = g_new0(struct a2dp_setup, 1);
149 setup->session = avdtp_ref(session);
150 setups = g_slist_append(setups, setup);
155 static void setup_free(struct a2dp_setup *s)
159 setups = g_slist_remove(setups, s);
161 avdtp_unref(s->session);
162 g_slist_free_full(s->cb, g_free);
163 g_slist_free_full(s->caps, g_free);
167 static void setup_unref(struct a2dp_setup *setup)
171 DBG("%p: ref=%d", setup, setup->ref);
179 static struct a2dp_setup_cb *setup_cb_new(struct a2dp_setup *setup)
181 struct a2dp_setup_cb *cb;
183 cb = g_new0(struct a2dp_setup_cb, 1);
187 setup->cb = g_slist_append(setup->cb, cb);
191 static void setup_cb_free(struct a2dp_setup_cb *cb)
193 struct a2dp_setup *setup = cb->setup;
196 g_source_remove(cb->source_id);
198 setup->cb = g_slist_remove(setup->cb, cb);
199 setup_unref(cb->setup);
203 static void finalize_setup_errno(struct a2dp_setup *s, int err,
204 GSourceFunc cb1, ...)
206 GSourceFunc finalize;
208 struct avdtp_error avdtp_err;
211 avdtp_error_init(&avdtp_err, AVDTP_ERRNO, -err);
218 while (finalize != NULL) {
220 finalize = va_arg(args, GSourceFunc);
226 static gboolean finalize_config(gpointer data)
228 struct a2dp_setup *s = data;
230 struct avdtp_stream *stream = s->err ? NULL : s->stream;
232 for (l = s->cb; l != NULL; ) {
233 struct a2dp_setup_cb *cb = l->data;
240 cb->config_cb(s->session, s->sep, stream, s->err,
248 static gboolean finalize_resume(gpointer data)
250 struct a2dp_setup *s = data;
253 for (l = s->cb; l != NULL; ) {
254 struct a2dp_setup_cb *cb = l->data;
261 cb->resume_cb(s->session, s->err, cb->user_data);
268 static gboolean finalize_suspend(gpointer data)
270 struct a2dp_setup *s = data;
273 for (l = s->cb; l != NULL; ) {
274 struct a2dp_setup_cb *cb = l->data;
281 cb->suspend_cb(s->session, s->err, cb->user_data);
288 static void finalize_select(struct a2dp_setup *s)
292 for (l = s->cb; l != NULL; ) {
293 struct a2dp_setup_cb *cb = l->data;
300 cb->select_cb(s->session, s->sep, s->caps, cb->user_data);
305 static struct a2dp_setup *find_setup_by_session(struct avdtp *session)
309 for (l = setups; l != NULL; l = l->next) {
310 struct a2dp_setup *setup = l->data;
312 if (setup->session == session)
319 static struct a2dp_setup *a2dp_setup_get(struct avdtp *session)
321 struct a2dp_setup *setup;
323 setup = find_setup_by_session(session);
325 setup = setup_new(session);
330 return setup_ref(setup);
333 static struct a2dp_setup *find_setup_by_stream(struct avdtp_stream *stream)
337 for (l = setups; l != NULL; l = l->next) {
338 struct a2dp_setup *setup = l->data;
340 if (setup->stream == stream)
347 static void stream_state_changed(struct avdtp_stream *stream,
348 avdtp_state_t old_state,
349 avdtp_state_t new_state,
350 struct avdtp_error *err,
353 struct a2dp_sep *sep = user_data;
355 if (new_state == AVDTP_STATE_OPEN) {
356 struct a2dp_setup *setup;
359 setup = find_setup_by_stream(stream);
360 if (!setup || !setup->start)
363 setup->start = FALSE;
365 err = avdtp_start(setup->session, stream);
366 if (err < 0 && err != -EINPROGRESS) {
367 error("avdtp_start: %s (%d)", strerror(-err), -err);
368 finalize_setup_errno(setup, err, finalize_resume,
373 sep->starting = TRUE;
378 #ifdef __TIZEN_PATCH__
379 if (new_state == AVDTP_STATE_STREAMING && sep->suspend_timer) {
380 g_source_remove(sep->suspend_timer);
381 sep->suspend_timer = 0;
385 if (new_state != AVDTP_STATE_IDLE)
388 if (sep->suspend_timer) {
389 g_source_remove(sep->suspend_timer);
390 sep->suspend_timer = 0;
394 avdtp_unref(sep->session);
400 if (sep->endpoint && sep->endpoint->clear_configuration)
401 sep->endpoint->clear_configuration(sep, sep->user_data);
404 static gboolean auto_config(gpointer data)
406 struct a2dp_setup *setup = data;
407 struct btd_device *dev = NULL;
409 struct btd_service *service;
411 /* Check if configuration was aborted */
412 if (setup->sep->stream == NULL)
415 if (setup->err != NULL)
418 dev = avdtp_get_device(setup->session);
420 avdtp_stream_add_cb(setup->session, setup->stream,
421 stream_state_changed, setup->sep);
423 if (setup->sep->type == AVDTP_SEP_TYPE_SOURCE) {
424 service = btd_device_get_service(dev, A2DP_SINK_UUID);
425 sink_new_stream(service, setup->session, setup->stream);
427 service = btd_device_get_service(dev, A2DP_SOURCE_UUID);
428 source_new_stream(service, setup->session, setup->stream);
432 if (setup->setconf_cb)
433 setup->setconf_cb(setup->session, setup->stream, setup->err);
435 finalize_config(setup);
447 static void endpoint_setconf_cb(struct a2dp_setup *setup, gboolean ret)
450 setup->err = g_new(struct avdtp_error, 1);
451 avdtp_error_init(setup->err, AVDTP_MEDIA_CODEC,
452 AVDTP_UNSUPPORTED_CONFIGURATION);
458 static gboolean endpoint_setconf_ind(struct avdtp *session,
459 struct avdtp_local_sep *sep,
460 struct avdtp_stream *stream,
462 avdtp_set_configuration_cb cb,
465 struct a2dp_sep *a2dp_sep = user_data;
466 struct a2dp_setup *setup;
468 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
469 DBG("Sink %p: Set_Configuration_Ind", sep);
471 DBG("Source %p: Set_Configuration_Ind", sep);
473 setup = a2dp_setup_get(session);
477 a2dp_sep->stream = stream;
478 setup->sep = a2dp_sep;
479 setup->stream = stream;
480 setup->setconf_cb = cb;
482 for (; caps != NULL; caps = g_slist_next(caps)) {
483 struct avdtp_service_capability *cap = caps->data;
484 struct avdtp_media_codec_capability *codec;
487 if (cap->category == AVDTP_DELAY_REPORTING &&
488 !a2dp_sep->delay_reporting) {
489 setup->err = g_new(struct avdtp_error, 1);
490 avdtp_error_init(setup->err, AVDTP_DELAY_REPORTING,
491 AVDTP_UNSUPPORTED_CONFIGURATION);
495 if (cap->category != AVDTP_MEDIA_CODEC)
498 codec = (struct avdtp_media_codec_capability *) cap->data;
500 if (codec->media_codec_type != a2dp_sep->codec) {
501 setup->err = g_new(struct avdtp_error, 1);
502 avdtp_error_init(setup->err, AVDTP_MEDIA_CODEC,
503 AVDTP_UNSUPPORTED_CONFIGURATION);
507 ret = a2dp_sep->endpoint->set_configuration(a2dp_sep,
509 cap->length - sizeof(*codec),
512 a2dp_sep->user_data);
516 setup->err = g_new(struct avdtp_error, 1);
517 avdtp_error_init(setup->err, AVDTP_MEDIA_CODEC,
518 AVDTP_UNSUPPORTED_CONFIGURATION);
523 g_idle_add(auto_config, setup);
527 static gboolean endpoint_getcap_ind(struct avdtp *session,
528 struct avdtp_local_sep *sep,
529 gboolean get_all, GSList **caps,
530 uint8_t *err, void *user_data)
532 struct a2dp_sep *a2dp_sep = user_data;
533 struct avdtp_service_capability *media_transport, *media_codec;
534 struct avdtp_media_codec_capability *codec_caps;
535 uint8_t *capabilities;
538 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
539 DBG("Sink %p: Get_Capability_Ind", sep);
541 DBG("Source %p: Get_Capability_Ind", sep);
545 media_transport = avdtp_service_cap_new(AVDTP_MEDIA_TRANSPORT,
548 *caps = g_slist_append(*caps, media_transport);
550 length = a2dp_sep->endpoint->get_capabilities(a2dp_sep, &capabilities,
551 a2dp_sep->user_data);
553 codec_caps = g_malloc0(sizeof(*codec_caps) + length);
554 codec_caps->media_type = AVDTP_MEDIA_TYPE_AUDIO;
555 codec_caps->media_codec_type = a2dp_sep->codec;
556 memcpy(codec_caps->data, capabilities, length);
558 media_codec = avdtp_service_cap_new(AVDTP_MEDIA_CODEC, codec_caps,
559 sizeof(*codec_caps) + length);
561 *caps = g_slist_append(*caps, media_codec);
565 struct avdtp_service_capability *delay_reporting;
566 delay_reporting = avdtp_service_cap_new(AVDTP_DELAY_REPORTING,
568 *caps = g_slist_append(*caps, delay_reporting);
574 static void endpoint_open_cb(struct a2dp_setup *setup, gboolean ret)
579 setup->stream = NULL;
580 finalize_setup_errno(setup, -EPERM, finalize_config, NULL);
584 err = avdtp_open(setup->session, setup->stream);
588 error("Error on avdtp_open %s (%d)", strerror(-err), -err);
589 setup->stream = NULL;
590 finalize_setup_errno(setup, err, finalize_config, NULL);
593 static void setconf_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
594 struct avdtp_stream *stream,
595 struct avdtp_error *err, void *user_data)
597 struct a2dp_sep *a2dp_sep = user_data;
598 struct a2dp_setup *setup;
599 struct btd_device *dev;
600 struct btd_service *service;
603 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
604 DBG("Sink %p: Set_Configuration_Cfm", sep);
606 DBG("Source %p: Set_Configuration_Cfm", sep);
608 setup = find_setup_by_session(session);
614 finalize_config(setup);
621 avdtp_stream_add_cb(session, stream, stream_state_changed, a2dp_sep);
622 a2dp_sep->stream = stream;
627 dev = avdtp_get_device(session);
629 /* Notify D-Bus interface of the new stream */
630 if (a2dp_sep->type == AVDTP_SEP_TYPE_SOURCE) {
631 service = btd_device_get_service(dev, A2DP_SINK_UUID);
632 sink_new_stream(service, session, setup->stream);
634 service = btd_device_get_service(dev, A2DP_SOURCE_UUID);
635 source_new_stream(service, session, setup->stream);
638 /* Notify Endpoint */
639 if (a2dp_sep->endpoint) {
640 struct avdtp_service_capability *service;
641 struct avdtp_media_codec_capability *codec;
644 service = avdtp_stream_get_codec(stream);
645 codec = (struct avdtp_media_codec_capability *) service->data;
647 err = a2dp_sep->endpoint->set_configuration(a2dp_sep,
648 codec->data, service->length -
652 a2dp_sep->user_data);
656 setup->stream = NULL;
657 finalize_setup_errno(setup, -EPERM, finalize_config, NULL);
661 ret = avdtp_open(session, stream);
663 error("Error on avdtp_open %s (%d)", strerror(-ret), -ret);
664 setup->stream = NULL;
665 finalize_setup_errno(setup, ret, finalize_config, NULL);
669 static gboolean getconf_ind(struct avdtp *session, struct avdtp_local_sep *sep,
670 uint8_t *err, void *user_data)
672 struct a2dp_sep *a2dp_sep = user_data;
674 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
675 DBG("Sink %p: Get_Configuration_Ind", sep);
677 DBG("Source %p: Get_Configuration_Ind", sep);
681 static void getconf_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
682 struct avdtp_stream *stream, struct avdtp_error *err,
685 struct a2dp_sep *a2dp_sep = user_data;
687 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
688 DBG("Sink %p: Set_Configuration_Cfm", sep);
690 DBG("Source %p: Set_Configuration_Cfm", sep);
693 static gboolean open_ind(struct avdtp *session, struct avdtp_local_sep *sep,
694 struct avdtp_stream *stream, uint8_t *err,
697 struct a2dp_sep *a2dp_sep = user_data;
698 struct a2dp_setup *setup;
700 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
701 DBG("Sink %p: Open_Ind", sep);
703 DBG("Source %p: Open_Ind", sep);
705 setup = a2dp_setup_get(session);
709 setup->stream = stream;
711 if (setup->reconfigure)
712 setup->reconfigure = FALSE;
714 finalize_config(setup);
719 static void open_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
720 struct avdtp_stream *stream, struct avdtp_error *err,
723 struct a2dp_sep *a2dp_sep = user_data;
724 struct a2dp_setup *setup;
726 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
727 DBG("Sink %p: Open_Cfm", sep);
729 DBG("Source %p: Open_Cfm", sep);
731 setup = find_setup_by_session(session);
735 if (setup->reconfigure)
736 setup->reconfigure = FALSE;
739 setup->stream = NULL;
742 finalize_resume(setup);
745 finalize_config(setup);
750 static gboolean suspend_timeout(struct a2dp_sep *sep)
752 if (avdtp_suspend(sep->session, sep->stream) == 0)
753 sep->suspending = TRUE;
755 sep->suspend_timer = 0;
757 avdtp_unref(sep->session);
763 static gboolean start_ind(struct avdtp *session, struct avdtp_local_sep *sep,
764 struct avdtp_stream *stream, uint8_t *err,
767 struct a2dp_sep *a2dp_sep = user_data;
768 struct a2dp_setup *setup;
770 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
771 DBG("Sink %p: Start_Ind", sep);
773 DBG("Source %p: Start_Ind", sep);
775 if (!a2dp_sep->locked) {
776 a2dp_sep->session = avdtp_ref(session);
777 a2dp_sep->suspend_timer = g_timeout_add_seconds(SUSPEND_TIMEOUT,
778 (GSourceFunc) suspend_timeout,
782 if (!a2dp_sep->starting)
785 a2dp_sep->starting = FALSE;
787 setup = find_setup_by_session(session);
789 finalize_resume(setup);
794 static void start_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
795 struct avdtp_stream *stream, struct avdtp_error *err,
798 struct a2dp_sep *a2dp_sep = user_data;
799 struct a2dp_setup *setup;
801 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
802 DBG("Sink %p: Start_Cfm", sep);
804 DBG("Source %p: Start_Cfm", sep);
806 a2dp_sep->starting = FALSE;
808 setup = find_setup_by_session(session);
813 setup->stream = NULL;
817 finalize_resume(setup);
820 static gboolean suspend_ind(struct avdtp *session, struct avdtp_local_sep *sep,
821 struct avdtp_stream *stream, uint8_t *err,
824 struct a2dp_sep *a2dp_sep = user_data;
825 struct a2dp_setup *setup;
829 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
830 DBG("Sink %p: Suspend_Ind", sep);
832 DBG("Source %p: Suspend_Ind", sep);
834 if (a2dp_sep->suspend_timer) {
835 g_source_remove(a2dp_sep->suspend_timer);
836 a2dp_sep->suspend_timer = 0;
837 avdtp_unref(a2dp_sep->session);
838 a2dp_sep->session = NULL;
841 if (!a2dp_sep->suspending)
844 a2dp_sep->suspending = FALSE;
846 setup = find_setup_by_session(session);
850 start = setup->start;
851 setup->start = FALSE;
853 finalize_suspend(setup);
858 start_err = avdtp_start(session, a2dp_sep->stream);
859 if (start_err < 0 && start_err != -EINPROGRESS) {
860 error("avdtp_start: %s (%d)", strerror(-start_err),
862 finalize_setup_errno(setup, start_err, finalize_resume, NULL);
868 static void suspend_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
869 struct avdtp_stream *stream, struct avdtp_error *err,
872 struct a2dp_sep *a2dp_sep = user_data;
873 struct a2dp_setup *setup;
877 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
878 DBG("Sink %p: Suspend_Cfm", sep);
880 DBG("Source %p: Suspend_Cfm", sep);
882 a2dp_sep->suspending = FALSE;
884 setup = find_setup_by_session(session);
888 start = setup->start;
889 setup->start = FALSE;
892 setup->stream = NULL;
896 finalize_suspend(setup);
902 finalize_resume(setup);
906 start_err = avdtp_start(session, a2dp_sep->stream);
907 if (start_err < 0 && start_err != -EINPROGRESS) {
908 error("avdtp_start: %s (%d)", strerror(-start_err),
910 finalize_setup_errno(setup, start_err, finalize_suspend, NULL);
914 static gboolean close_ind(struct avdtp *session, struct avdtp_local_sep *sep,
915 struct avdtp_stream *stream, uint8_t *err,
918 struct a2dp_sep *a2dp_sep = user_data;
919 struct a2dp_setup *setup;
921 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
922 DBG("Sink %p: Close_Ind", sep);
924 DBG("Source %p: Close_Ind", sep);
926 setup = find_setup_by_session(session);
930 finalize_setup_errno(setup, -ECONNRESET, finalize_suspend,
931 finalize_resume, NULL);
936 static gboolean a2dp_reconfigure(gpointer data)
938 struct a2dp_setup *setup = data;
939 struct a2dp_sep *sep = setup->sep;
941 struct avdtp_media_codec_capability *rsep_codec;
942 struct avdtp_service_capability *cap;
945 cap = avdtp_get_codec(setup->rsep);
946 rsep_codec = (struct avdtp_media_codec_capability *) cap->data;
949 if (!setup->rsep || sep->codec != rsep_codec->media_codec_type)
950 setup->rsep = avdtp_find_remote_sep(setup->session, sep->lsep);
952 posix_err = avdtp_set_configuration(setup->session, setup->rsep,
957 error("avdtp_set_configuration: %s", strerror(-posix_err));
964 finalize_setup_errno(setup, posix_err, finalize_config, NULL);
968 static void close_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
969 struct avdtp_stream *stream, struct avdtp_error *err,
972 struct a2dp_sep *a2dp_sep = user_data;
973 struct a2dp_setup *setup;
975 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
976 DBG("Sink %p: Close_Cfm", sep);
978 DBG("Source %p: Close_Cfm", sep);
980 setup = find_setup_by_session(session);
985 setup->stream = NULL;
987 finalize_config(setup);
992 setup->rsep = avdtp_stream_get_remote_sep(stream);
994 if (setup->reconfigure)
995 g_timeout_add(RECONFIGURE_TIMEOUT, a2dp_reconfigure, setup);
998 static void abort_ind(struct avdtp *session, struct avdtp_local_sep *sep,
999 struct avdtp_stream *stream, uint8_t *err,
1002 struct a2dp_sep *a2dp_sep = user_data;
1003 struct a2dp_setup *setup;
1005 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
1006 DBG("Sink %p: Abort_Ind", sep);
1008 DBG("Source %p: Abort_Ind", sep);
1010 a2dp_sep->stream = NULL;
1012 setup = find_setup_by_session(session);
1016 finalize_setup_errno(setup, -ECONNRESET, finalize_suspend,
1018 finalize_config, NULL);
1023 static void abort_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
1024 struct avdtp_stream *stream, struct avdtp_error *err,
1027 struct a2dp_sep *a2dp_sep = user_data;
1028 struct a2dp_setup *setup;
1030 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
1031 DBG("Sink %p: Abort_Cfm", sep);
1033 DBG("Source %p: Abort_Cfm", sep);
1035 setup = find_setup_by_session(session);
1042 static gboolean reconf_ind(struct avdtp *session, struct avdtp_local_sep *sep,
1043 uint8_t *err, void *user_data)
1045 struct a2dp_sep *a2dp_sep = user_data;
1047 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
1048 DBG("Sink %p: ReConfigure_Ind", sep);
1050 DBG("Source %p: ReConfigure_Ind", sep);
1055 static gboolean endpoint_delayreport_ind(struct avdtp *session,
1056 struct avdtp_local_sep *sep,
1057 uint8_t rseid, uint16_t delay,
1058 uint8_t *err, void *user_data)
1060 struct a2dp_sep *a2dp_sep = user_data;
1062 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
1063 DBG("Sink %p: DelayReport_Ind", sep);
1065 DBG("Source %p: DelayReport_Ind", sep);
1067 if (a2dp_sep->endpoint == NULL ||
1068 a2dp_sep->endpoint->set_delay == NULL)
1071 a2dp_sep->endpoint->set_delay(a2dp_sep, delay, a2dp_sep->user_data);
1076 static void reconf_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
1077 struct avdtp_stream *stream, struct avdtp_error *err,
1080 struct a2dp_sep *a2dp_sep = user_data;
1081 struct a2dp_setup *setup;
1083 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
1084 DBG("Sink %p: ReConfigure_Cfm", sep);
1086 DBG("Source %p: ReConfigure_Cfm", sep);
1088 setup = find_setup_by_session(session);
1093 setup->stream = NULL;
1097 finalize_config(setup);
1100 static void delay_report_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
1101 struct avdtp_stream *stream,
1102 struct avdtp_error *err, void *user_data)
1104 struct a2dp_sep *a2dp_sep = user_data;
1106 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
1107 DBG("Sink %p: DelayReport_Cfm", sep);
1109 DBG("Source %p: DelayReport_Cfm", sep);
1112 static struct avdtp_sep_cfm cfm = {
1113 .set_configuration = setconf_cfm,
1114 .get_configuration = getconf_cfm,
1117 .suspend = suspend_cfm,
1120 .reconfigure = reconf_cfm,
1121 .delay_report = delay_report_cfm,
1124 static struct avdtp_sep_ind endpoint_ind = {
1125 .get_capability = endpoint_getcap_ind,
1126 .set_configuration = endpoint_setconf_ind,
1127 .get_configuration = getconf_ind,
1130 .suspend = suspend_ind,
1133 .reconfigure = reconf_ind,
1134 .delayreport = endpoint_delayreport_ind,
1137 static sdp_record_t *a2dp_record(uint8_t type)
1139 sdp_list_t *svclass_id, *pfseq, *apseq, *root;
1140 uuid_t root_uuid, l2cap_uuid, avdtp_uuid, a2dp_uuid;
1141 sdp_profile_desc_t profile[1];
1142 sdp_list_t *aproto, *proto[2];
1143 sdp_record_t *record;
1144 sdp_data_t *psm, *version, *features;
1145 uint16_t lp = AVDTP_UUID;
1146 #ifdef __TIZEN_PATCH__
1147 uint16_t a2dp_ver = 0x0102, avdtp_ver = 0x0103, feat = 0x0001;
1149 uint16_t a2dp_ver = 0x0103, avdtp_ver = 0x0103, feat = 0x000f;
1152 record = sdp_record_alloc();
1156 sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
1157 root = sdp_list_append(0, &root_uuid);
1158 sdp_set_browse_groups(record, root);
1160 if (type == AVDTP_SEP_TYPE_SOURCE)
1161 sdp_uuid16_create(&a2dp_uuid, AUDIO_SOURCE_SVCLASS_ID);
1163 sdp_uuid16_create(&a2dp_uuid, AUDIO_SINK_SVCLASS_ID);
1164 svclass_id = sdp_list_append(0, &a2dp_uuid);
1165 sdp_set_service_classes(record, svclass_id);
1167 sdp_uuid16_create(&profile[0].uuid, ADVANCED_AUDIO_PROFILE_ID);
1168 profile[0].version = a2dp_ver;
1169 pfseq = sdp_list_append(0, &profile[0]);
1170 sdp_set_profile_descs(record, pfseq);
1172 sdp_uuid16_create(&l2cap_uuid, L2CAP_UUID);
1173 proto[0] = sdp_list_append(0, &l2cap_uuid);
1174 psm = sdp_data_alloc(SDP_UINT16, &lp);
1175 proto[0] = sdp_list_append(proto[0], psm);
1176 apseq = sdp_list_append(0, proto[0]);
1178 sdp_uuid16_create(&avdtp_uuid, AVDTP_UUID);
1179 proto[1] = sdp_list_append(0, &avdtp_uuid);
1180 version = sdp_data_alloc(SDP_UINT16, &avdtp_ver);
1181 proto[1] = sdp_list_append(proto[1], version);
1182 apseq = sdp_list_append(apseq, proto[1]);
1184 aproto = sdp_list_append(0, apseq);
1185 sdp_set_access_protos(record, aproto);
1187 features = sdp_data_alloc(SDP_UINT16, &feat);
1188 sdp_attr_add(record, SDP_ATTR_SUPPORTED_FEATURES, features);
1190 if (type == AVDTP_SEP_TYPE_SOURCE)
1191 sdp_set_info_attr(record, "Audio Source", 0, 0);
1193 sdp_set_info_attr(record, "Audio Sink", 0, 0);
1197 sdp_list_free(proto[0], 0);
1198 sdp_list_free(proto[1], 0);
1199 sdp_list_free(apseq, 0);
1200 sdp_list_free(pfseq, 0);
1201 sdp_list_free(aproto, 0);
1202 sdp_list_free(root, 0);
1203 sdp_list_free(svclass_id, 0);
1208 static struct a2dp_server *find_server(GSList *list, struct btd_adapter *a)
1211 for (; list; list = list->next) {
1212 struct a2dp_server *server = list->data;
1214 if (server->adapter == a)
1221 static void channel_free(void *data)
1223 struct a2dp_channel *chan = data;
1225 if (chan->auth_id > 0)
1226 btd_cancel_authorization(chan->auth_id);
1228 if (chan->io_id > 0)
1229 g_source_remove(chan->io_id);
1232 g_io_channel_shutdown(chan->io, TRUE, NULL);
1233 g_io_channel_unref(chan->io);
1236 avdtp_remove_state_cb(chan->state_id);
1241 static void channel_remove(struct a2dp_channel *chan)
1243 struct a2dp_server *server = chan->server;
1245 DBG("chan %p", chan);
1247 queue_remove(server->channels, chan);
1252 static gboolean disconnect_cb(GIOChannel *io, GIOCondition cond, gpointer data)
1254 struct a2dp_channel *chan = data;
1256 DBG("chan %p", chan);
1260 channel_remove(chan);
1265 static void avdtp_state_cb(struct btd_device *dev, struct avdtp *session,
1266 avdtp_session_state_t old_state,
1267 avdtp_session_state_t new_state,
1270 struct a2dp_channel *chan = user_data;
1272 switch (new_state) {
1273 case AVDTP_SESSION_STATE_DISCONNECTED:
1274 if (chan->session == session)
1275 channel_remove(chan);
1277 case AVDTP_SESSION_STATE_CONNECTING:
1279 case AVDTP_SESSION_STATE_CONNECTED:
1284 static struct a2dp_channel *channel_new(struct a2dp_server *server,
1285 struct btd_device *device,
1288 struct a2dp_channel *chan;
1290 chan = g_new0(struct a2dp_channel, 1);
1291 chan->server = server;
1292 chan->device = device;
1293 chan->state_id = avdtp_add_state_cb(device, avdtp_state_cb, chan);
1295 if (!queue_push_tail(server->channels, chan)) {
1303 chan->io = g_io_channel_ref(io);
1304 chan->io_id = g_io_add_watch(io, G_IO_ERR | G_IO_HUP | G_IO_NVAL,
1305 (GIOFunc) disconnect_cb, chan);
1310 static bool match_by_device(const void *data, const void *user_data)
1312 const struct a2dp_channel *chan = data;
1313 const struct btd_device *device = user_data;
1315 return chan->device == device;
1318 struct avdtp *a2dp_avdtp_get(struct btd_device *device)
1320 struct a2dp_server *server;
1321 struct a2dp_channel *chan;
1323 server = find_server(servers, device_get_adapter(device));
1327 chan = queue_find(server->channels, match_by_device, device);
1329 chan = channel_new(server, device, NULL);
1335 return avdtp_ref(chan->session);
1337 chan->session = avdtp_new(NULL, device, server->seps);
1338 if (!chan->session) {
1339 channel_remove(chan);
1343 return avdtp_ref(chan->session);
1346 static void connect_cb(GIOChannel *io, GError *err, gpointer user_data)
1348 struct a2dp_channel *chan = user_data;
1351 error("%s", err->message);
1355 chan->session = avdtp_new(chan->io, chan->device, chan->server->seps);
1356 if (!chan->session) {
1357 error("Unable to create AVDTP session");
1361 g_io_channel_unref(chan->io);
1364 g_source_remove(chan->io_id);
1370 channel_remove(chan);
1373 static void auth_cb(DBusError *derr, void *user_data)
1375 struct a2dp_channel *chan = user_data;
1380 if (derr && dbus_error_is_set(derr)) {
1381 error("Access denied: %s", derr->message);
1385 if (!bt_io_accept(chan->io, connect_cb, chan, NULL, &err)) {
1386 error("bt_io_accept: %s", err->message);
1394 channel_remove(chan);
1397 static void transport_cb(GIOChannel *io, GError *err, gpointer user_data)
1399 struct a2dp_setup *setup = user_data;
1400 uint16_t omtu, imtu;
1403 error("%s", err->message);
1407 bt_io_get(io, &err, BT_IO_OPT_OMTU, &omtu, BT_IO_OPT_IMTU, &imtu,
1410 error("%s", err->message);
1415 if (!avdtp_stream_set_transport(setup->stream,
1416 g_io_channel_unix_get_fd(io),
1420 g_io_channel_set_close_on_unref(io, FALSE);
1428 g_io_channel_shutdown(io, TRUE, NULL);
1431 static void confirm_cb(GIOChannel *io, gpointer data)
1433 struct a2dp_server *server = data;
1434 struct a2dp_channel *chan;
1438 struct btd_device *device;
1441 BT_IO_OPT_SOURCE_BDADDR, &src,
1442 BT_IO_OPT_DEST_BDADDR, &dst,
1443 BT_IO_OPT_DEST, address,
1446 error("%s", err->message);
1451 DBG("AVDTP: incoming connect from %s", address);
1453 device = btd_adapter_find_device(adapter_find(&src), &dst,
1458 chan = queue_find(server->channels, match_by_device, device);
1460 struct a2dp_setup *setup;
1462 setup = find_setup_by_session(chan->session);
1463 if (!setup || !setup->stream)
1466 if (!bt_io_accept(io, transport_cb, setup, NULL, &err)) {
1467 error("bt_io_accept: %s", err->message);
1475 chan = channel_new(server, device, io);
1479 chan->auth_id = btd_request_authorization(&src, &dst,
1480 ADVANCED_AUDIO_UUID,
1482 if (chan->auth_id == 0 && !chan->session)
1483 channel_remove(chan);
1488 g_io_channel_shutdown(io, TRUE, NULL);
1491 static bool a2dp_server_listen(struct a2dp_server *server)
1498 server->io = bt_io_listen(NULL, confirm_cb, server, NULL, &err,
1499 BT_IO_OPT_SOURCE_BDADDR,
1500 btd_adapter_get_address(server->adapter),
1501 BT_IO_OPT_PSM, AVDTP_PSM,
1502 BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_MEDIUM,
1503 BT_IO_OPT_MASTER, true,
1508 error("%s", err->message);
1514 static struct a2dp_server *a2dp_server_register(struct btd_adapter *adapter)
1516 struct a2dp_server *server;
1518 server = g_new0(struct a2dp_server, 1);
1520 server->adapter = btd_adapter_ref(adapter);
1521 server->seps = queue_new();
1522 server->channels = queue_new();
1524 servers = g_slist_append(servers, server);
1529 static void a2dp_unregister_sep(struct a2dp_sep *sep)
1531 struct a2dp_server *server = sep->server;
1534 sep->destroy(sep->user_data);
1535 sep->endpoint = NULL;
1538 avdtp_unregister_sep(server->seps, sep->lsep);
1542 if (!queue_isempty(server->seps))
1546 g_io_channel_shutdown(server->io, TRUE, NULL);
1547 g_io_channel_unref(server->io);
1552 static void a2dp_server_unregister(struct a2dp_server *server)
1554 servers = g_slist_remove(servers, server);
1555 queue_destroy(server->channels, channel_free);
1556 queue_destroy(server->seps, NULL);
1559 g_io_channel_shutdown(server->io, TRUE, NULL);
1560 g_io_channel_unref(server->io);
1563 btd_adapter_unref(server->adapter);
1567 struct a2dp_sep *a2dp_add_sep(struct btd_adapter *adapter, uint8_t type,
1568 uint8_t codec, gboolean delay_reporting,
1569 struct a2dp_endpoint *endpoint,
1570 void *user_data, GDestroyNotify destroy,
1573 struct a2dp_server *server;
1574 struct a2dp_sep *sep;
1576 uint32_t *record_id;
1577 sdp_record_t *record;
1579 server = find_server(servers, adapter);
1580 if (server == NULL) {
1582 *err = -EPROTONOSUPPORT;
1586 if (type == AVDTP_SEP_TYPE_SINK && !server->sink_enabled) {
1588 *err = -EPROTONOSUPPORT;
1592 if (type == AVDTP_SEP_TYPE_SOURCE && !server->source_enabled) {
1594 *err = -EPROTONOSUPPORT;
1598 sep = g_new0(struct a2dp_sep, 1);
1600 sep->lsep = avdtp_register_sep(server->seps, type,
1601 AVDTP_MEDIA_TYPE_AUDIO, codec,
1602 delay_reporting, &endpoint_ind,
1605 if (sep->lsep == NULL) {
1612 sep->server = server;
1613 sep->endpoint = endpoint;
1616 sep->delay_reporting = delay_reporting;
1617 sep->user_data = user_data;
1618 sep->destroy = destroy;
1620 if (type == AVDTP_SEP_TYPE_SOURCE) {
1621 l = &server->sources;
1622 record_id = &server->source_record_id;
1625 record_id = &server->sink_record_id;
1628 if (*record_id != 0)
1631 record = a2dp_record(type);
1633 error("Unable to allocate new service record");
1634 a2dp_unregister_sep(sep);
1640 if (adapter_service_add(server->adapter, record) < 0) {
1641 error("Unable to register A2DP service record");
1642 sdp_record_free(record);
1643 a2dp_unregister_sep(sep);
1649 if (!a2dp_server_listen(server)) {
1650 sdp_record_free(record);
1651 a2dp_unregister_sep(sep);
1657 *record_id = record->handle;
1660 *l = g_slist_append(*l, sep);
1667 void a2dp_remove_sep(struct a2dp_sep *sep)
1669 struct a2dp_server *server = sep->server;
1671 if (sep->type == AVDTP_SEP_TYPE_SOURCE) {
1672 if (g_slist_find(server->sources, sep) == NULL)
1674 server->sources = g_slist_remove(server->sources, sep);
1675 if (server->sources == NULL && server->source_record_id) {
1676 adapter_service_remove(server->adapter,
1677 server->source_record_id);
1678 server->source_record_id = 0;
1681 if (g_slist_find(server->sinks, sep) == NULL)
1683 server->sinks = g_slist_remove(server->sinks, sep);
1684 if (server->sinks == NULL && server->sink_record_id) {
1685 adapter_service_remove(server->adapter,
1686 server->sink_record_id);
1687 server->sink_record_id = 0;
1694 a2dp_unregister_sep(sep);
1697 static void select_cb(struct a2dp_setup *setup, void *ret, int size)
1699 struct avdtp_service_capability *media_transport, *media_codec;
1700 struct avdtp_media_codec_capability *cap;
1703 DBG("Endpoint replied an invalid configuration");
1707 media_transport = avdtp_service_cap_new(AVDTP_MEDIA_TRANSPORT,
1710 setup->caps = g_slist_append(setup->caps, media_transport);
1712 cap = g_malloc0(sizeof(*cap) + size);
1713 cap->media_type = AVDTP_MEDIA_TYPE_AUDIO;
1714 cap->media_codec_type = setup->sep->codec;
1715 memcpy(cap->data, ret, size);
1717 media_codec = avdtp_service_cap_new(AVDTP_MEDIA_CODEC, cap,
1718 sizeof(*cap) + size);
1720 setup->caps = g_slist_append(setup->caps, media_codec);
1724 finalize_select(setup);
1727 static gboolean check_vendor_codec(struct a2dp_sep *sep, uint8_t *cap,
1730 uint8_t *capabilities;
1732 a2dp_vendor_codec_t *local_codec;
1733 a2dp_vendor_codec_t *remote_codec;
1735 if (len < sizeof(a2dp_vendor_codec_t))
1738 remote_codec = (a2dp_vendor_codec_t *) cap;
1740 if (sep->endpoint == NULL)
1743 length = sep->endpoint->get_capabilities(sep,
1744 &capabilities, sep->user_data);
1746 if (length < sizeof(a2dp_vendor_codec_t))
1749 local_codec = (a2dp_vendor_codec_t *) capabilities;
1751 if (btohl(remote_codec->vendor_id) != btohl(local_codec->vendor_id))
1754 if (btohs(remote_codec->codec_id) != btohs(local_codec->codec_id))
1757 DBG("vendor 0x%08x codec 0x%04x", btohl(remote_codec->vendor_id),
1758 btohs(remote_codec->codec_id));
1763 static struct a2dp_sep *a2dp_find_sep(struct avdtp *session, GSList *list,
1766 #ifdef __TIZEN_PATCH__
1767 struct a2dp_sep *selected_sep = NULL;
1770 for (; list; list = list->next) {
1771 struct a2dp_sep *sep = list->data;
1772 struct avdtp_remote_sep *rsep;
1773 struct avdtp_media_codec_capability *cap;
1774 struct avdtp_service_capability *service;
1776 /* Use sender's endpoint if available */
1780 if (sep->endpoint == NULL)
1783 name = sep->endpoint->get_name(sep, sep->user_data);
1784 if (g_strcmp0(sender, name) != 0)
1788 rsep = avdtp_find_remote_sep(session, sep->lsep);
1792 service = avdtp_get_codec(rsep);
1793 cap = (struct avdtp_media_codec_capability *) service->data;
1795 #ifdef __TIZEN_PATCH__
1796 if (cap->media_codec_type != A2DP_CODEC_VENDOR) {
1801 if (cap->media_codec_type != A2DP_CODEC_VENDOR)
1805 if (check_vendor_codec(sep, cap->data,
1806 service->length - sizeof(*cap)))
1810 #ifdef __TIZEN_PATCH__
1812 return selected_sep;
1820 static struct a2dp_sep *a2dp_select_sep(struct avdtp *session, uint8_t type,
1823 struct a2dp_server *server;
1824 struct a2dp_sep *sep;
1827 server = find_server(servers, avdtp_get_adapter(session));
1831 l = type == AVDTP_SEP_TYPE_SINK ? server->sources : server->sinks;
1833 /* Check sender's seps first */
1834 sep = a2dp_find_sep(session, l, sender);
1838 return a2dp_find_sep(session, l, NULL);
1841 unsigned int a2dp_select_capabilities(struct avdtp *session,
1842 uint8_t type, const char *sender,
1843 a2dp_select_cb_t cb,
1846 struct a2dp_setup *setup;
1847 struct a2dp_setup_cb *cb_data;
1848 struct a2dp_sep *sep;
1849 struct avdtp_service_capability *service;
1850 struct avdtp_media_codec_capability *codec;
1853 sep = a2dp_select_sep(session, type, sender);
1855 error("Unable to select SEP");
1859 setup = a2dp_setup_get(session);
1863 cb_data = setup_cb_new(setup);
1864 cb_data->select_cb = cb;
1865 cb_data->user_data = user_data;
1868 setup->rsep = avdtp_find_remote_sep(session, sep->lsep);
1870 if (setup->rsep == NULL) {
1871 error("Could not find remote sep");
1875 service = avdtp_get_codec(setup->rsep);
1876 codec = (struct avdtp_media_codec_capability *) service->data;
1878 err = sep->endpoint->select_configuration(sep, codec->data,
1879 service->length - sizeof(*codec),
1881 select_cb, sep->user_data);
1886 setup_cb_free(cb_data);
1891 unsigned int a2dp_config(struct avdtp *session, struct a2dp_sep *sep,
1892 a2dp_config_cb_t cb, GSList *caps,
1895 struct a2dp_setup_cb *cb_data;
1897 struct a2dp_server *server;
1898 struct a2dp_setup *setup;
1899 struct a2dp_sep *tmp;
1900 struct avdtp_service_capability *cap;
1901 struct avdtp_media_codec_capability *codec_cap = NULL;
1904 server = find_server(servers, avdtp_get_adapter(session));
1908 for (l = caps; l != NULL; l = l->next) {
1911 if (cap->category != AVDTP_MEDIA_CODEC)
1914 codec_cap = (void *) cap->data;
1921 if (sep->codec != codec_cap->media_codec_type)
1924 DBG("a2dp_config: selected SEP %p", sep->lsep);
1926 setup = a2dp_setup_get(session);
1930 cb_data = setup_cb_new(setup);
1931 cb_data->config_cb = cb;
1932 cb_data->user_data = user_data;
1935 setup->stream = sep->stream;
1937 /* Copy given caps if they are different than current caps */
1938 if (setup->caps != caps) {
1939 g_slist_free_full(setup->caps, g_free);
1940 setup->caps = g_slist_copy(caps);
1943 switch (avdtp_sep_get_state(sep->lsep)) {
1944 case AVDTP_STATE_IDLE:
1945 if (sep->type == AVDTP_SEP_TYPE_SOURCE)
1946 l = server->sources;
1950 for (; l != NULL; l = l->next) {
1952 if (avdtp_has_stream(session, tmp->stream))
1959 setup->reconfigure = TRUE;
1960 if (avdtp_close(session, tmp->stream, FALSE) < 0) {
1961 error("avdtp_close failed");
1967 setup->rsep = avdtp_find_remote_sep(session, sep->lsep);
1968 if (setup->rsep == NULL) {
1969 error("No matching ACP and INT SEPs found");
1973 posix_err = avdtp_set_configuration(session, setup->rsep,
1976 if (posix_err < 0) {
1977 error("avdtp_set_configuration: %s",
1978 strerror(-posix_err));
1982 case AVDTP_STATE_OPEN:
1983 case AVDTP_STATE_STREAMING:
1984 if (avdtp_stream_has_capabilities(setup->stream, caps)) {
1985 DBG("Configuration match: resuming");
1986 cb_data->source_id = g_idle_add(finalize_config,
1988 } else if (!setup->reconfigure) {
1989 setup->reconfigure = TRUE;
1990 if (avdtp_close(session, sep->stream, FALSE) < 0) {
1991 error("avdtp_close failed");
1996 case AVDTP_STATE_CONFIGURED:
1997 case AVDTP_STATE_CLOSING:
1998 case AVDTP_STATE_ABORTING:
2000 error("SEP in bad state for requesting a new stream");
2007 setup_cb_free(cb_data);
2011 unsigned int a2dp_resume(struct avdtp *session, struct a2dp_sep *sep,
2012 a2dp_stream_cb_t cb, void *user_data)
2014 struct a2dp_setup_cb *cb_data;
2015 struct a2dp_setup *setup;
2017 setup = a2dp_setup_get(session);
2021 cb_data = setup_cb_new(setup);
2022 cb_data->resume_cb = cb;
2023 cb_data->user_data = user_data;
2026 setup->stream = sep->stream;
2028 switch (avdtp_sep_get_state(sep->lsep)) {
2029 case AVDTP_STATE_IDLE:
2032 case AVDTP_STATE_CONFIGURED:
2033 setup->start = TRUE;
2035 case AVDTP_STATE_OPEN:
2036 if (avdtp_start(session, sep->stream) < 0) {
2037 error("avdtp_start failed");
2040 sep->starting = TRUE;
2042 case AVDTP_STATE_STREAMING:
2043 if (!sep->suspending && sep->suspend_timer) {
2044 g_source_remove(sep->suspend_timer);
2045 sep->suspend_timer = 0;
2046 avdtp_unref(sep->session);
2047 sep->session = NULL;
2049 if (sep->suspending)
2050 setup->start = TRUE;
2052 cb_data->source_id = g_idle_add(finalize_resume,
2055 case AVDTP_STATE_CLOSING:
2056 case AVDTP_STATE_ABORTING:
2058 error("SEP in bad state for resume");
2065 setup_cb_free(cb_data);
2069 unsigned int a2dp_suspend(struct avdtp *session, struct a2dp_sep *sep,
2070 a2dp_stream_cb_t cb, void *user_data)
2072 struct a2dp_setup_cb *cb_data;
2073 struct a2dp_setup *setup;
2075 setup = a2dp_setup_get(session);
2079 cb_data = setup_cb_new(setup);
2080 cb_data->suspend_cb = cb;
2081 cb_data->user_data = user_data;
2084 setup->stream = sep->stream;
2086 switch (avdtp_sep_get_state(sep->lsep)) {
2087 case AVDTP_STATE_IDLE:
2088 error("a2dp_suspend: no stream to suspend");
2091 case AVDTP_STATE_OPEN:
2092 cb_data->source_id = g_idle_add(finalize_suspend, setup);
2094 case AVDTP_STATE_STREAMING:
2095 if (avdtp_suspend(session, sep->stream) < 0) {
2096 error("avdtp_suspend failed");
2099 sep->suspending = TRUE;
2101 case AVDTP_STATE_CONFIGURED:
2102 case AVDTP_STATE_CLOSING:
2103 case AVDTP_STATE_ABORTING:
2105 error("SEP in bad state for suspend");
2112 setup_cb_free(cb_data);
2116 gboolean a2dp_cancel(unsigned int id)
2120 for (ls = setups; ls != NULL; ls = g_slist_next(ls)) {
2121 struct a2dp_setup *setup = ls->data;
2123 for (l = setup->cb; l != NULL; l = g_slist_next(l)) {
2124 struct a2dp_setup_cb *cb = l->data;
2133 DBG("aborting setup %p", setup);
2134 avdtp_abort(setup->session, setup->stream);
2146 gboolean a2dp_sep_lock(struct a2dp_sep *sep, struct avdtp *session)
2151 DBG("SEP %p locked", sep->lsep);
2157 gboolean a2dp_sep_unlock(struct a2dp_sep *sep, struct avdtp *session)
2159 struct a2dp_server *server = sep->server;
2160 avdtp_state_t state;
2163 state = avdtp_sep_get_state(sep->lsep);
2165 sep->locked = FALSE;
2167 DBG("SEP %p unlocked", sep->lsep);
2169 if (sep->type == AVDTP_SEP_TYPE_SOURCE)
2170 l = server->sources;
2174 /* Unregister sep if it was removed */
2175 if (g_slist_find(l, sep) == NULL) {
2176 a2dp_unregister_sep(sep);
2180 if (!sep->stream || state == AVDTP_STATE_IDLE)
2184 case AVDTP_STATE_OPEN:
2185 /* Set timer here */
2187 case AVDTP_STATE_STREAMING:
2188 if (avdtp_suspend(session, sep->stream) == 0)
2189 sep->suspending = TRUE;
2191 case AVDTP_STATE_IDLE:
2192 case AVDTP_STATE_CONFIGURED:
2193 case AVDTP_STATE_CLOSING:
2194 case AVDTP_STATE_ABORTING:
2202 struct avdtp_stream *a2dp_sep_get_stream(struct a2dp_sep *sep)
2207 struct btd_device *a2dp_setup_get_device(struct a2dp_setup *setup)
2209 if (setup->session == NULL)
2212 return avdtp_get_device(setup->session);
2215 static int a2dp_source_probe(struct btd_service *service)
2217 struct btd_device *dev = btd_service_get_device(service);
2219 DBG("path %s", device_get_path(dev));
2221 source_init(service);
2226 static void a2dp_source_remove(struct btd_service *service)
2228 source_unregister(service);
2231 static int a2dp_sink_probe(struct btd_service *service)
2233 struct btd_device *dev = btd_service_get_device(service);
2235 DBG("path %s", device_get_path(dev));
2237 return sink_init(service);
2240 static void a2dp_sink_remove(struct btd_service *service)
2242 sink_unregister(service);
2245 static int a2dp_source_connect(struct btd_service *service)
2247 struct btd_device *dev = btd_service_get_device(service);
2248 struct btd_adapter *adapter = device_get_adapter(dev);
2249 struct a2dp_server *server;
2250 const char *path = device_get_path(dev);
2252 DBG("path %s", path);
2254 server = find_server(servers, adapter);
2255 if (!server || !server->sink_enabled) {
2256 DBG("Unexpected error: cannot find server");
2257 return -EPROTONOSUPPORT;
2260 /* Return protocol not available if no record/endpoint exists */
2261 if (server->sink_record_id == 0)
2262 return -ENOPROTOOPT;
2264 return source_connect(service);
2267 static int a2dp_source_disconnect(struct btd_service *service)
2269 struct btd_device *dev = btd_service_get_device(service);
2270 const char *path = device_get_path(dev);
2272 DBG("path %s", path);
2274 return source_disconnect(service);
2277 static int a2dp_sink_connect(struct btd_service *service)
2279 struct btd_device *dev = btd_service_get_device(service);
2280 struct btd_adapter *adapter = device_get_adapter(dev);
2281 struct a2dp_server *server;
2282 const char *path = device_get_path(dev);
2284 DBG("path %s", path);
2286 server = find_server(servers, adapter);
2287 if (!server || !server->source_enabled) {
2288 DBG("Unexpected error: cannot find server");
2289 return -EPROTONOSUPPORT;
2292 /* Return protocol not available if no record/endpoint exists */
2293 if (server->source_record_id == 0)
2294 return -ENOPROTOOPT;
2296 return sink_connect(service);
2299 static int a2dp_sink_disconnect(struct btd_service *service)
2301 struct btd_device *dev = btd_service_get_device(service);
2302 const char *path = device_get_path(dev);
2304 DBG("path %s", path);
2306 return sink_disconnect(service);
2309 static int a2dp_source_server_probe(struct btd_profile *p,
2310 struct btd_adapter *adapter)
2312 struct a2dp_server *server;
2314 DBG("path %s", adapter_get_path(adapter));
2316 server = find_server(servers, adapter);
2320 server = a2dp_server_register(adapter);
2322 return -EPROTONOSUPPORT;
2325 server->source_enabled = TRUE;
2330 static void a2dp_source_server_remove(struct btd_profile *p,
2331 struct btd_adapter *adapter)
2333 struct a2dp_server *server;
2335 DBG("path %s", adapter_get_path(adapter));
2337 server = find_server(servers, adapter);
2341 g_slist_free_full(server->sources,
2342 (GDestroyNotify) a2dp_unregister_sep);
2344 if (server->source_record_id) {
2345 adapter_service_remove(server->adapter,
2346 server->source_record_id);
2347 server->source_record_id = 0;
2350 if (server->sink_record_id)
2353 a2dp_server_unregister(server);
2356 static int a2dp_sink_server_probe(struct btd_profile *p,
2357 struct btd_adapter *adapter)
2359 struct a2dp_server *server;
2361 DBG("path %s", adapter_get_path(adapter));
2363 server = find_server(servers, adapter);
2367 server = a2dp_server_register(adapter);
2369 return -EPROTONOSUPPORT;
2372 server->sink_enabled = TRUE;
2377 static void a2dp_sink_server_remove(struct btd_profile *p,
2378 struct btd_adapter *adapter)
2380 struct a2dp_server *server;
2382 DBG("path %s", adapter_get_path(adapter));
2384 server = find_server(servers, adapter);
2388 g_slist_free_full(server->sinks, (GDestroyNotify) a2dp_unregister_sep);
2390 if (server->sink_record_id) {
2391 adapter_service_remove(server->adapter, server->sink_record_id);
2392 server->sink_record_id = 0;
2395 if (server->source_record_id)
2398 a2dp_server_unregister(server);
2401 static int media_server_probe(struct btd_adapter *adapter)
2403 DBG("path %s", adapter_get_path(adapter));
2405 return media_register(adapter);
2408 static void media_server_remove(struct btd_adapter *adapter)
2410 DBG("path %s", adapter_get_path(adapter));
2412 media_unregister(adapter);
2415 static struct btd_profile a2dp_source_profile = {
2416 .name = "a2dp-source",
2417 .priority = BTD_PROFILE_PRIORITY_MEDIUM,
2419 .remote_uuid = A2DP_SOURCE_UUID,
2420 .device_probe = a2dp_source_probe,
2421 .device_remove = a2dp_source_remove,
2423 .auto_connect = true,
2424 .connect = a2dp_source_connect,
2425 .disconnect = a2dp_source_disconnect,
2427 .adapter_probe = a2dp_sink_server_probe,
2428 .adapter_remove = a2dp_sink_server_remove,
2431 static struct btd_profile a2dp_sink_profile = {
2432 .name = "a2dp-sink",
2433 .priority = BTD_PROFILE_PRIORITY_MEDIUM,
2435 .remote_uuid = A2DP_SINK_UUID,
2436 .device_probe = a2dp_sink_probe,
2437 .device_remove = a2dp_sink_remove,
2439 .auto_connect = true,
2440 .connect = a2dp_sink_connect,
2441 .disconnect = a2dp_sink_disconnect,
2443 .adapter_probe = a2dp_source_server_probe,
2444 .adapter_remove = a2dp_source_server_remove,
2447 static struct btd_adapter_driver media_driver = {
2449 .probe = media_server_probe,
2450 .remove = media_server_remove,
2453 static int a2dp_init(void)
2455 btd_register_adapter_driver(&media_driver);
2456 btd_profile_register(&a2dp_source_profile);
2457 btd_profile_register(&a2dp_sink_profile);
2462 static void a2dp_exit(void)
2464 btd_unregister_adapter_driver(&media_driver);
2465 btd_profile_unregister(&a2dp_source_profile);
2466 btd_profile_unregister(&a2dp_sink_profile);
2469 BLUETOOTH_PLUGIN_DEFINE(a2dp, VERSION, BLUETOOTH_PLUGIN_PRIORITY_DEFAULT,
2470 a2dp_init, a2dp_exit)