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 #ifdef __TIZEN_PATCH__
76 gboolean remote_suspended;
78 gboolean delay_reporting;
83 GDestroyNotify destroy;
86 struct a2dp_setup_cb {
87 struct a2dp_setup *setup;
88 a2dp_discover_cb_t discover_cb;
89 a2dp_select_cb_t select_cb;
90 a2dp_config_cb_t config_cb;
91 a2dp_stream_cb_t resume_cb;
92 a2dp_stream_cb_t suspend_cb;
99 struct avdtp *session;
100 struct a2dp_sep *sep;
101 struct avdtp_remote_sep *rsep;
102 struct avdtp_stream *stream;
103 struct avdtp_error *err;
104 avdtp_set_configuration_cb setconf_cb;
107 gboolean reconfigure;
114 struct btd_adapter *adapter;
117 uint32_t source_record_id;
118 uint32_t sink_record_id;
119 gboolean sink_enabled;
120 gboolean source_enabled;
123 struct queue *channels;
126 struct a2dp_channel {
127 struct a2dp_server *server;
128 struct btd_device *device;
131 unsigned int state_id;
132 unsigned int auth_id;
133 struct avdtp *session;
136 static GSList *servers = NULL;
137 static GSList *setups = NULL;
138 static unsigned int cb_id = 0;
140 static struct a2dp_setup *setup_ref(struct a2dp_setup *setup)
144 DBG("%p: ref=%d", setup, setup->ref);
149 static struct a2dp_setup *setup_new(struct avdtp *session)
151 struct a2dp_setup *setup;
153 setup = g_new0(struct a2dp_setup, 1);
154 setup->session = avdtp_ref(session);
155 setups = g_slist_append(setups, setup);
160 static void setup_free(struct a2dp_setup *s)
164 setups = g_slist_remove(setups, s);
166 avdtp_unref(s->session);
167 g_slist_free_full(s->cb, g_free);
168 g_slist_free_full(s->caps, g_free);
172 static void setup_unref(struct a2dp_setup *setup)
176 DBG("%p: ref=%d", setup, setup->ref);
184 static struct a2dp_setup_cb *setup_cb_new(struct a2dp_setup *setup)
186 struct a2dp_setup_cb *cb;
188 cb = g_new0(struct a2dp_setup_cb, 1);
192 setup->cb = g_slist_append(setup->cb, cb);
196 static void setup_cb_free(struct a2dp_setup_cb *cb)
198 struct a2dp_setup *setup = cb->setup;
201 g_source_remove(cb->source_id);
203 setup->cb = g_slist_remove(setup->cb, cb);
204 setup_unref(cb->setup);
208 static void finalize_setup_errno(struct a2dp_setup *s, int err,
209 GSourceFunc cb1, ...)
211 GSourceFunc finalize;
213 struct avdtp_error avdtp_err;
216 avdtp_error_init(&avdtp_err, AVDTP_ERRNO, -err);
223 while (finalize != NULL) {
225 finalize = va_arg(args, GSourceFunc);
231 static gboolean finalize_config(gpointer data)
233 struct a2dp_setup *s = data;
235 struct avdtp_stream *stream = s->err ? NULL : s->stream;
237 for (l = s->cb; l != NULL; ) {
238 struct a2dp_setup_cb *cb = l->data;
245 cb->config_cb(s->session, s->sep, stream, s->err,
253 static gboolean finalize_resume(gpointer data)
255 struct a2dp_setup *s = data;
258 for (l = s->cb; l != NULL; ) {
259 struct a2dp_setup_cb *cb = l->data;
266 cb->resume_cb(s->session, s->err, cb->user_data);
273 static gboolean finalize_suspend(gpointer data)
275 struct a2dp_setup *s = data;
278 for (l = s->cb; l != NULL; ) {
279 struct a2dp_setup_cb *cb = l->data;
286 cb->suspend_cb(s->session, s->err, cb->user_data);
293 static void finalize_select(struct a2dp_setup *s)
297 for (l = s->cb; l != NULL; ) {
298 struct a2dp_setup_cb *cb = l->data;
305 cb->select_cb(s->session, s->sep, s->caps, cb->user_data);
310 static void finalize_discover(struct a2dp_setup *s)
314 for (l = s->cb; l != NULL; ) {
315 struct a2dp_setup_cb *cb = l->data;
319 if (!cb->discover_cb)
322 cb->discover_cb(s->session, s->seps, s->err, cb->user_data);
327 static struct a2dp_setup *find_setup_by_session(struct avdtp *session)
331 for (l = setups; l != NULL; l = l->next) {
332 struct a2dp_setup *setup = l->data;
334 if (setup->session == session)
341 static struct a2dp_setup *a2dp_setup_get(struct avdtp *session)
343 struct a2dp_setup *setup;
345 setup = find_setup_by_session(session);
347 setup = setup_new(session);
352 return setup_ref(setup);
355 static struct a2dp_setup *find_setup_by_stream(struct avdtp_stream *stream)
359 for (l = setups; l != NULL; l = l->next) {
360 struct a2dp_setup *setup = l->data;
362 if (setup->stream == stream)
369 static void stream_state_changed(struct avdtp_stream *stream,
370 avdtp_state_t old_state,
371 avdtp_state_t new_state,
372 struct avdtp_error *err,
375 struct a2dp_sep *sep = user_data;
377 if (new_state == AVDTP_STATE_OPEN) {
378 struct a2dp_setup *setup;
381 setup = find_setup_by_stream(stream);
382 if (!setup || !setup->start)
385 setup->start = FALSE;
387 err = avdtp_start(setup->session, stream);
388 if (err < 0 && err != -EINPROGRESS) {
389 error("avdtp_start: %s (%d)", strerror(-err), -err);
390 finalize_setup_errno(setup, err, finalize_resume,
395 sep->starting = TRUE;
400 #ifdef __TIZEN_PATCH__
401 if (new_state == AVDTP_STATE_STREAMING && sep->suspend_timer) {
402 g_source_remove(sep->suspend_timer);
403 sep->suspend_timer = 0;
407 if (new_state != AVDTP_STATE_IDLE)
410 if (sep->suspend_timer) {
411 g_source_remove(sep->suspend_timer);
412 sep->suspend_timer = 0;
416 avdtp_unref(sep->session);
422 if (sep->endpoint && sep->endpoint->clear_configuration)
423 sep->endpoint->clear_configuration(sep, sep->user_data);
426 static gboolean auto_config(gpointer data)
428 struct a2dp_setup *setup = data;
429 struct btd_device *dev = NULL;
430 struct btd_service *service;
432 /* Check if configuration was aborted */
433 if (setup->sep->stream == NULL)
436 if (setup->err != NULL)
439 dev = avdtp_get_device(setup->session);
441 avdtp_stream_add_cb(setup->session, setup->stream,
442 stream_state_changed, setup->sep);
444 if (setup->sep->type == AVDTP_SEP_TYPE_SOURCE) {
445 service = btd_device_get_service(dev, A2DP_SINK_UUID);
446 sink_new_stream(service, setup->session, setup->stream);
448 service = btd_device_get_service(dev, A2DP_SOURCE_UUID);
449 source_new_stream(service, setup->session, setup->stream);
453 if (setup->setconf_cb)
454 setup->setconf_cb(setup->session, setup->stream, setup->err);
456 finalize_config(setup);
468 static void endpoint_setconf_cb(struct a2dp_setup *setup, gboolean ret)
471 setup->err = g_new(struct avdtp_error, 1);
472 avdtp_error_init(setup->err, AVDTP_MEDIA_CODEC,
473 AVDTP_UNSUPPORTED_CONFIGURATION);
479 static gboolean endpoint_match_codec_ind(struct avdtp *session,
480 struct avdtp_media_codec_capability *codec,
483 struct a2dp_sep *sep = user_data;
484 a2dp_vendor_codec_t *remote_codec;
485 a2dp_vendor_codec_t *local_codec;
486 uint8_t *capabilities;
489 if (codec->media_codec_type != A2DP_CODEC_VENDOR)
492 if (sep->endpoint == NULL)
495 length = sep->endpoint->get_capabilities(sep, &capabilities,
497 if (length < sizeof(a2dp_vendor_codec_t))
500 local_codec = (a2dp_vendor_codec_t *) capabilities;
501 remote_codec = (a2dp_vendor_codec_t *) codec->data;
503 if (remote_codec->vendor_id != local_codec->vendor_id)
506 if (remote_codec->codec_id != local_codec->codec_id)
509 DBG("vendor 0x%08x codec 0x%04x", btohl(remote_codec->vendor_id),
510 btohs(remote_codec->codec_id));
514 static gboolean endpoint_setconf_ind(struct avdtp *session,
515 struct avdtp_local_sep *sep,
516 struct avdtp_stream *stream,
518 avdtp_set_configuration_cb cb,
521 struct a2dp_sep *a2dp_sep = user_data;
522 struct a2dp_setup *setup;
524 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
525 DBG("Sink %p: Set_Configuration_Ind", sep);
527 DBG("Source %p: Set_Configuration_Ind", sep);
529 setup = a2dp_setup_get(session);
533 a2dp_sep->stream = stream;
534 setup->sep = a2dp_sep;
535 setup->stream = stream;
536 setup->setconf_cb = cb;
538 for (; caps != NULL; caps = g_slist_next(caps)) {
539 struct avdtp_service_capability *cap = caps->data;
540 struct avdtp_media_codec_capability *codec;
543 if (cap->category == AVDTP_DELAY_REPORTING &&
544 !a2dp_sep->delay_reporting) {
545 setup->err = g_new(struct avdtp_error, 1);
546 avdtp_error_init(setup->err, AVDTP_DELAY_REPORTING,
547 AVDTP_UNSUPPORTED_CONFIGURATION);
551 if (cap->category != AVDTP_MEDIA_CODEC)
554 codec = (struct avdtp_media_codec_capability *) cap->data;
556 if (codec->media_codec_type != a2dp_sep->codec) {
557 setup->err = g_new(struct avdtp_error, 1);
558 avdtp_error_init(setup->err, AVDTP_MEDIA_CODEC,
559 AVDTP_UNSUPPORTED_CONFIGURATION);
563 ret = a2dp_sep->endpoint->set_configuration(a2dp_sep,
565 cap->length - sizeof(*codec),
568 a2dp_sep->user_data);
572 setup->err = g_new(struct avdtp_error, 1);
573 avdtp_error_init(setup->err, AVDTP_MEDIA_CODEC,
574 AVDTP_UNSUPPORTED_CONFIGURATION);
579 g_idle_add(auto_config, setup);
583 static gboolean endpoint_getcap_ind(struct avdtp *session,
584 struct avdtp_local_sep *sep,
585 gboolean get_all, GSList **caps,
586 uint8_t *err, void *user_data)
588 struct a2dp_sep *a2dp_sep = user_data;
589 struct avdtp_service_capability *media_transport, *media_codec;
590 struct avdtp_media_codec_capability *codec_caps;
591 uint8_t *capabilities;
594 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
595 DBG("Sink %p: Get_Capability_Ind", sep);
597 DBG("Source %p: Get_Capability_Ind", sep);
601 media_transport = avdtp_service_cap_new(AVDTP_MEDIA_TRANSPORT,
604 *caps = g_slist_append(*caps, media_transport);
606 length = a2dp_sep->endpoint->get_capabilities(a2dp_sep, &capabilities,
607 a2dp_sep->user_data);
609 codec_caps = g_malloc0(sizeof(*codec_caps) + length);
610 codec_caps->media_type = AVDTP_MEDIA_TYPE_AUDIO;
611 codec_caps->media_codec_type = a2dp_sep->codec;
612 memcpy(codec_caps->data, capabilities, length);
614 media_codec = avdtp_service_cap_new(AVDTP_MEDIA_CODEC, codec_caps,
615 sizeof(*codec_caps) + length);
617 *caps = g_slist_append(*caps, media_codec);
621 struct avdtp_service_capability *delay_reporting;
622 delay_reporting = avdtp_service_cap_new(AVDTP_DELAY_REPORTING,
624 *caps = g_slist_append(*caps, delay_reporting);
630 static void endpoint_open_cb(struct a2dp_setup *setup, gboolean ret)
635 setup->stream = NULL;
636 finalize_setup_errno(setup, -EPERM, finalize_config, NULL);
640 err = avdtp_open(setup->session, setup->stream);
644 error("Error on avdtp_open %s (%d)", strerror(-err), -err);
645 setup->stream = NULL;
646 finalize_setup_errno(setup, err, finalize_config, NULL);
649 static void setconf_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
650 struct avdtp_stream *stream,
651 struct avdtp_error *err, void *user_data)
653 struct a2dp_sep *a2dp_sep = user_data;
654 struct a2dp_setup *setup;
655 struct btd_device *dev;
656 struct btd_service *service;
659 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
660 DBG("Sink %p: Set_Configuration_Cfm", sep);
662 DBG("Source %p: Set_Configuration_Cfm", sep);
664 setup = find_setup_by_session(session);
670 finalize_config(setup);
677 avdtp_stream_add_cb(session, stream, stream_state_changed, a2dp_sep);
678 a2dp_sep->stream = stream;
683 dev = avdtp_get_device(session);
685 /* Notify D-Bus interface of the new stream */
686 if (a2dp_sep->type == AVDTP_SEP_TYPE_SOURCE) {
687 service = btd_device_get_service(dev, A2DP_SINK_UUID);
688 sink_new_stream(service, session, setup->stream);
690 service = btd_device_get_service(dev, A2DP_SOURCE_UUID);
691 source_new_stream(service, session, setup->stream);
694 /* Notify Endpoint */
695 if (a2dp_sep->endpoint) {
696 struct avdtp_service_capability *service;
697 struct avdtp_media_codec_capability *codec;
700 service = avdtp_stream_get_codec(stream);
701 codec = (struct avdtp_media_codec_capability *) service->data;
703 err = a2dp_sep->endpoint->set_configuration(a2dp_sep,
704 codec->data, service->length -
708 a2dp_sep->user_data);
712 setup->stream = NULL;
713 finalize_setup_errno(setup, -EPERM, finalize_config, NULL);
717 ret = avdtp_open(session, stream);
719 error("Error on avdtp_open %s (%d)", strerror(-ret), -ret);
720 setup->stream = NULL;
721 finalize_setup_errno(setup, ret, finalize_config, NULL);
725 static gboolean getconf_ind(struct avdtp *session, struct avdtp_local_sep *sep,
726 uint8_t *err, void *user_data)
728 struct a2dp_sep *a2dp_sep = user_data;
730 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
731 DBG("Sink %p: Get_Configuration_Ind", sep);
733 DBG("Source %p: Get_Configuration_Ind", sep);
737 static void getconf_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
738 struct avdtp_stream *stream, struct avdtp_error *err,
741 struct a2dp_sep *a2dp_sep = user_data;
743 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
744 DBG("Sink %p: Set_Configuration_Cfm", sep);
746 DBG("Source %p: Set_Configuration_Cfm", sep);
749 static gboolean open_ind(struct avdtp *session, struct avdtp_local_sep *sep,
750 struct avdtp_stream *stream, uint8_t *err,
753 struct a2dp_sep *a2dp_sep = user_data;
754 struct a2dp_setup *setup;
756 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
757 DBG("Sink %p: Open_Ind", sep);
759 DBG("Source %p: Open_Ind", sep);
761 setup = a2dp_setup_get(session);
765 setup->stream = stream;
767 if (setup->reconfigure)
768 setup->reconfigure = FALSE;
770 finalize_config(setup);
775 static void open_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
776 struct avdtp_stream *stream, struct avdtp_error *err,
779 struct a2dp_sep *a2dp_sep = user_data;
780 struct a2dp_setup *setup;
782 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
783 DBG("Sink %p: Open_Cfm", sep);
785 DBG("Source %p: Open_Cfm", sep);
787 setup = find_setup_by_session(session);
791 if (setup->reconfigure)
792 setup->reconfigure = FALSE;
795 setup->stream = NULL;
798 finalize_resume(setup);
801 finalize_config(setup);
806 static gboolean suspend_timeout(struct a2dp_sep *sep)
808 if (avdtp_suspend(sep->session, sep->stream) == 0)
809 sep->suspending = TRUE;
811 sep->suspend_timer = 0;
813 avdtp_unref(sep->session);
819 static gboolean start_ind(struct avdtp *session, struct avdtp_local_sep *sep,
820 struct avdtp_stream *stream, uint8_t *err,
823 struct a2dp_sep *a2dp_sep = user_data;
824 struct a2dp_setup *setup;
826 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
827 DBG("Sink %p: Start_Ind", sep);
829 DBG("Source %p: Start_Ind", sep);
831 #ifdef __TIZEN_PATCH__
832 if (!a2dp_sep->locked) {
833 a2dp_sep->session = avdtp_ref(session);
834 if(a2dp_sep->remote_suspended == FALSE)
835 a2dp_sep->suspend_timer = g_timeout_add_seconds(SUSPEND_TIMEOUT,
836 (GSourceFunc) suspend_timeout,
839 a2dp_sep->remote_suspended = FALSE;
843 if (!a2dp_sep->locked) {
844 a2dp_sep->session = avdtp_ref(session);
845 a2dp_sep->suspend_timer = g_timeout_add_seconds(SUSPEND_TIMEOUT,
846 (GSourceFunc) suspend_timeout,
851 if (!a2dp_sep->starting)
854 a2dp_sep->starting = FALSE;
856 setup = find_setup_by_session(session);
858 finalize_resume(setup);
863 static void start_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
864 struct avdtp_stream *stream, struct avdtp_error *err,
867 struct a2dp_sep *a2dp_sep = user_data;
868 struct a2dp_setup *setup;
870 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
871 DBG("Sink %p: Start_Cfm", sep);
873 DBG("Source %p: Start_Cfm", sep);
875 a2dp_sep->starting = FALSE;
877 setup = find_setup_by_session(session);
882 setup->stream = NULL;
886 finalize_resume(setup);
889 static gboolean suspend_ind(struct avdtp *session, struct avdtp_local_sep *sep,
890 struct avdtp_stream *stream, uint8_t *err,
893 struct a2dp_sep *a2dp_sep = user_data;
894 struct a2dp_setup *setup;
898 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
899 DBG("Sink %p: Suspend_Ind", sep);
901 DBG("Source %p: Suspend_Ind", sep);
903 #ifdef __TIZEN_PATCH__
904 a2dp_sep->remote_suspended = TRUE;
907 if (a2dp_sep->suspend_timer) {
908 g_source_remove(a2dp_sep->suspend_timer);
909 a2dp_sep->suspend_timer = 0;
910 avdtp_unref(a2dp_sep->session);
911 a2dp_sep->session = NULL;
914 if (!a2dp_sep->suspending)
917 a2dp_sep->suspending = FALSE;
919 setup = find_setup_by_session(session);
923 start = setup->start;
924 setup->start = FALSE;
926 finalize_suspend(setup);
931 start_err = avdtp_start(session, a2dp_sep->stream);
932 if (start_err < 0 && start_err != -EINPROGRESS) {
933 error("avdtp_start: %s (%d)", strerror(-start_err),
935 finalize_setup_errno(setup, start_err, finalize_resume, NULL);
941 static void suspend_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
942 struct avdtp_stream *stream, struct avdtp_error *err,
945 struct a2dp_sep *a2dp_sep = user_data;
946 struct a2dp_setup *setup;
950 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
951 DBG("Sink %p: Suspend_Cfm", sep);
953 DBG("Source %p: Suspend_Cfm", sep);
955 a2dp_sep->suspending = FALSE;
957 setup = find_setup_by_session(session);
961 start = setup->start;
962 setup->start = FALSE;
965 setup->stream = NULL;
969 finalize_suspend(setup);
975 finalize_resume(setup);
979 start_err = avdtp_start(session, a2dp_sep->stream);
980 if (start_err < 0 && start_err != -EINPROGRESS) {
981 error("avdtp_start: %s (%d)", strerror(-start_err),
983 finalize_setup_errno(setup, start_err, finalize_suspend, NULL);
987 static gboolean close_ind(struct avdtp *session, struct avdtp_local_sep *sep,
988 struct avdtp_stream *stream, uint8_t *err,
991 struct a2dp_sep *a2dp_sep = user_data;
992 struct a2dp_setup *setup;
994 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
995 DBG("Sink %p: Close_Ind", sep);
997 DBG("Source %p: Close_Ind", sep);
999 setup = find_setup_by_session(session);
1003 finalize_setup_errno(setup, -ECONNRESET, finalize_suspend,
1004 finalize_resume, NULL);
1009 static gboolean a2dp_reconfigure(gpointer data)
1011 struct a2dp_setup *setup = data;
1012 struct a2dp_sep *sep = setup->sep;
1014 struct avdtp_media_codec_capability *rsep_codec;
1015 struct avdtp_service_capability *cap;
1018 cap = avdtp_get_codec(setup->rsep);
1019 rsep_codec = (struct avdtp_media_codec_capability *) cap->data;
1022 if (!setup->rsep || sep->codec != rsep_codec->media_codec_type)
1023 setup->rsep = avdtp_find_remote_sep(setup->session, sep->lsep);
1025 posix_err = avdtp_set_configuration(setup->session, setup->rsep,
1029 if (posix_err < 0) {
1030 error("avdtp_set_configuration: %s", strerror(-posix_err));
1037 finalize_setup_errno(setup, posix_err, finalize_config, NULL);
1041 static void close_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
1042 struct avdtp_stream *stream, struct avdtp_error *err,
1045 struct a2dp_sep *a2dp_sep = user_data;
1046 struct a2dp_setup *setup;
1048 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
1049 DBG("Sink %p: Close_Cfm", sep);
1051 DBG("Source %p: Close_Cfm", sep);
1053 setup = find_setup_by_session(session);
1058 setup->stream = NULL;
1060 finalize_config(setup);
1065 setup->rsep = avdtp_stream_get_remote_sep(stream);
1067 if (setup->reconfigure)
1068 g_timeout_add(RECONFIGURE_TIMEOUT, a2dp_reconfigure, setup);
1071 static void abort_ind(struct avdtp *session, struct avdtp_local_sep *sep,
1072 struct avdtp_stream *stream, uint8_t *err,
1075 struct a2dp_sep *a2dp_sep = user_data;
1076 struct a2dp_setup *setup;
1078 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
1079 DBG("Sink %p: Abort_Ind", sep);
1081 DBG("Source %p: Abort_Ind", sep);
1083 a2dp_sep->stream = NULL;
1085 setup = find_setup_by_session(session);
1089 finalize_setup_errno(setup, -ECONNRESET, finalize_suspend,
1091 finalize_config, NULL);
1096 static void abort_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
1097 struct avdtp_stream *stream, struct avdtp_error *err,
1100 struct a2dp_sep *a2dp_sep = user_data;
1101 struct a2dp_setup *setup;
1103 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
1104 DBG("Sink %p: Abort_Cfm", sep);
1106 DBG("Source %p: Abort_Cfm", sep);
1108 setup = find_setup_by_session(session);
1115 static gboolean reconf_ind(struct avdtp *session, struct avdtp_local_sep *sep,
1116 uint8_t *err, void *user_data)
1118 struct a2dp_sep *a2dp_sep = user_data;
1120 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
1121 DBG("Sink %p: ReConfigure_Ind", sep);
1123 DBG("Source %p: ReConfigure_Ind", sep);
1128 static gboolean endpoint_delayreport_ind(struct avdtp *session,
1129 struct avdtp_local_sep *sep,
1130 uint8_t rseid, uint16_t delay,
1131 uint8_t *err, void *user_data)
1133 struct a2dp_sep *a2dp_sep = user_data;
1135 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
1136 DBG("Sink %p: DelayReport_Ind", sep);
1138 DBG("Source %p: DelayReport_Ind", sep);
1140 if (a2dp_sep->endpoint == NULL ||
1141 a2dp_sep->endpoint->set_delay == NULL)
1144 a2dp_sep->endpoint->set_delay(a2dp_sep, delay, a2dp_sep->user_data);
1149 static void reconf_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
1150 struct avdtp_stream *stream, struct avdtp_error *err,
1153 struct a2dp_sep *a2dp_sep = user_data;
1154 struct a2dp_setup *setup;
1156 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
1157 DBG("Sink %p: ReConfigure_Cfm", sep);
1159 DBG("Source %p: ReConfigure_Cfm", sep);
1161 setup = find_setup_by_session(session);
1166 setup->stream = NULL;
1170 finalize_config(setup);
1173 static void delay_report_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
1174 struct avdtp_stream *stream,
1175 struct avdtp_error *err, void *user_data)
1177 struct a2dp_sep *a2dp_sep = user_data;
1179 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
1180 DBG("Sink %p: DelayReport_Cfm", sep);
1182 DBG("Source %p: DelayReport_Cfm", sep);
1185 static struct avdtp_sep_cfm cfm = {
1186 .set_configuration = setconf_cfm,
1187 .get_configuration = getconf_cfm,
1190 .suspend = suspend_cfm,
1193 .reconfigure = reconf_cfm,
1194 .delay_report = delay_report_cfm,
1197 static struct avdtp_sep_ind endpoint_ind = {
1198 .match_codec = endpoint_match_codec_ind,
1199 .get_capability = endpoint_getcap_ind,
1200 .set_configuration = endpoint_setconf_ind,
1201 .get_configuration = getconf_ind,
1204 .suspend = suspend_ind,
1207 .reconfigure = reconf_ind,
1208 .delayreport = endpoint_delayreport_ind,
1211 static sdp_record_t *a2dp_record(uint8_t type)
1213 sdp_list_t *svclass_id, *pfseq, *apseq, *root;
1214 uuid_t root_uuid, l2cap_uuid, avdtp_uuid, a2dp_uuid;
1215 sdp_profile_desc_t profile[1];
1216 sdp_list_t *aproto, *proto[2];
1217 sdp_record_t *record;
1218 sdp_data_t *psm, *version, *features;
1219 uint16_t lp = AVDTP_UUID;
1220 #ifdef __TIZEN_PATCH__
1221 #ifdef SUPPORT_LOCAL_DEVICE_A2DP_SINK
1222 uint16_t a2dp_ver = 0x0102, avdtp_ver = 0x0103, feat = 0x0002;
1224 uint16_t a2dp_ver = 0x0102, avdtp_ver = 0x0103, feat = 0x0001;
1227 uint16_t a2dp_ver = 0x0103, avdtp_ver = 0x0103, feat = 0x000f;
1230 record = sdp_record_alloc();
1234 sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
1235 root = sdp_list_append(0, &root_uuid);
1236 sdp_set_browse_groups(record, root);
1238 if (type == AVDTP_SEP_TYPE_SOURCE)
1239 sdp_uuid16_create(&a2dp_uuid, AUDIO_SOURCE_SVCLASS_ID);
1241 sdp_uuid16_create(&a2dp_uuid, AUDIO_SINK_SVCLASS_ID);
1242 svclass_id = sdp_list_append(0, &a2dp_uuid);
1243 sdp_set_service_classes(record, svclass_id);
1245 sdp_uuid16_create(&profile[0].uuid, ADVANCED_AUDIO_PROFILE_ID);
1246 profile[0].version = a2dp_ver;
1247 pfseq = sdp_list_append(0, &profile[0]);
1248 sdp_set_profile_descs(record, pfseq);
1250 sdp_uuid16_create(&l2cap_uuid, L2CAP_UUID);
1251 proto[0] = sdp_list_append(0, &l2cap_uuid);
1252 psm = sdp_data_alloc(SDP_UINT16, &lp);
1253 proto[0] = sdp_list_append(proto[0], psm);
1254 apseq = sdp_list_append(0, proto[0]);
1256 sdp_uuid16_create(&avdtp_uuid, AVDTP_UUID);
1257 proto[1] = sdp_list_append(0, &avdtp_uuid);
1258 version = sdp_data_alloc(SDP_UINT16, &avdtp_ver);
1259 proto[1] = sdp_list_append(proto[1], version);
1260 apseq = sdp_list_append(apseq, proto[1]);
1262 aproto = sdp_list_append(0, apseq);
1263 sdp_set_access_protos(record, aproto);
1265 features = sdp_data_alloc(SDP_UINT16, &feat);
1266 sdp_attr_add(record, SDP_ATTR_SUPPORTED_FEATURES, features);
1268 if (type == AVDTP_SEP_TYPE_SOURCE)
1269 sdp_set_info_attr(record, "Audio Source", 0, 0);
1271 sdp_set_info_attr(record, "Audio Sink", 0, 0);
1275 sdp_list_free(proto[0], 0);
1276 sdp_list_free(proto[1], 0);
1277 sdp_list_free(apseq, 0);
1278 sdp_list_free(pfseq, 0);
1279 sdp_list_free(aproto, 0);
1280 sdp_list_free(root, 0);
1281 sdp_list_free(svclass_id, 0);
1286 static struct a2dp_server *find_server(GSList *list, struct btd_adapter *a)
1289 for (; list; list = list->next) {
1290 struct a2dp_server *server = list->data;
1292 if (server->adapter == a)
1299 static void channel_free(void *data)
1301 struct a2dp_channel *chan = data;
1303 if (chan->auth_id > 0)
1304 btd_cancel_authorization(chan->auth_id);
1306 if (chan->io_id > 0)
1307 g_source_remove(chan->io_id);
1310 g_io_channel_shutdown(chan->io, TRUE, NULL);
1311 g_io_channel_unref(chan->io);
1314 avdtp_remove_state_cb(chan->state_id);
1319 static void channel_remove(struct a2dp_channel *chan)
1321 struct a2dp_server *server = chan->server;
1323 DBG("chan %p", chan);
1325 queue_remove(server->channels, chan);
1330 static gboolean disconnect_cb(GIOChannel *io, GIOCondition cond, gpointer data)
1332 struct a2dp_channel *chan = data;
1334 DBG("chan %p", chan);
1338 channel_remove(chan);
1343 static void avdtp_state_cb(struct btd_device *dev, struct avdtp *session,
1344 avdtp_session_state_t old_state,
1345 avdtp_session_state_t new_state,
1348 struct a2dp_channel *chan = user_data;
1350 switch (new_state) {
1351 case AVDTP_SESSION_STATE_DISCONNECTED:
1352 if (chan->session == session)
1353 channel_remove(chan);
1355 case AVDTP_SESSION_STATE_CONNECTING:
1357 case AVDTP_SESSION_STATE_CONNECTED:
1362 static struct a2dp_channel *channel_new(struct a2dp_server *server,
1363 struct btd_device *device,
1366 struct a2dp_channel *chan;
1368 chan = g_new0(struct a2dp_channel, 1);
1369 chan->server = server;
1370 chan->device = device;
1371 chan->state_id = avdtp_add_state_cb(device, avdtp_state_cb, chan);
1373 if (!queue_push_tail(server->channels, chan)) {
1381 chan->io = g_io_channel_ref(io);
1382 chan->io_id = g_io_add_watch(io, G_IO_ERR | G_IO_HUP | G_IO_NVAL,
1383 (GIOFunc) disconnect_cb, chan);
1388 static bool match_by_device(const void *data, const void *user_data)
1390 const struct a2dp_channel *chan = data;
1391 const struct btd_device *device = user_data;
1393 return chan->device == device;
1396 struct avdtp *a2dp_avdtp_get(struct btd_device *device)
1398 struct a2dp_server *server;
1399 struct a2dp_channel *chan;
1401 server = find_server(servers, device_get_adapter(device));
1405 chan = queue_find(server->channels, match_by_device, device);
1407 chan = channel_new(server, device, NULL);
1412 #ifdef __TIZEN_PATCH__
1413 if (chan->auth_id) {
1414 DBG("auth is already going...");
1420 return avdtp_ref(chan->session);
1422 chan->session = avdtp_new(NULL, device, server->seps);
1423 if (!chan->session) {
1424 channel_remove(chan);
1428 return avdtp_ref(chan->session);
1431 static void connect_cb(GIOChannel *io, GError *err, gpointer user_data)
1433 struct a2dp_channel *chan = user_data;
1436 error("%s", err->message);
1440 chan->session = avdtp_new(chan->io, chan->device, chan->server->seps);
1441 if (!chan->session) {
1442 error("Unable to create AVDTP session");
1446 g_io_channel_unref(chan->io);
1449 g_source_remove(chan->io_id);
1455 channel_remove(chan);
1458 static void auth_cb(DBusError *derr, void *user_data)
1460 struct a2dp_channel *chan = user_data;
1465 if (derr && dbus_error_is_set(derr)) {
1466 error("Access denied: %s", derr->message);
1470 if (!bt_io_accept(chan->io, connect_cb, chan, NULL, &err)) {
1471 error("bt_io_accept: %s", err->message);
1479 channel_remove(chan);
1482 static void transport_cb(GIOChannel *io, GError *err, gpointer user_data)
1484 struct a2dp_setup *setup = user_data;
1485 uint16_t omtu, imtu;
1488 error("%s", err->message);
1492 bt_io_get(io, &err, BT_IO_OPT_OMTU, &omtu, BT_IO_OPT_IMTU, &imtu,
1495 error("%s", err->message);
1500 if (!avdtp_stream_set_transport(setup->stream,
1501 g_io_channel_unix_get_fd(io),
1505 g_io_channel_set_close_on_unref(io, FALSE);
1513 g_io_channel_shutdown(io, TRUE, NULL);
1516 static void confirm_cb(GIOChannel *io, gpointer data)
1518 struct a2dp_server *server = data;
1519 struct a2dp_channel *chan;
1523 struct btd_device *device;
1526 BT_IO_OPT_SOURCE_BDADDR, &src,
1527 BT_IO_OPT_DEST_BDADDR, &dst,
1528 BT_IO_OPT_DEST, address,
1531 error("%s", err->message);
1536 DBG("AVDTP: incoming connect from %s", address);
1538 device = btd_adapter_find_device(adapter_find(&src), &dst,
1543 chan = queue_find(server->channels, match_by_device, device);
1545 struct a2dp_setup *setup;
1547 setup = find_setup_by_session(chan->session);
1548 if (!setup || !setup->stream)
1551 if (!bt_io_accept(io, transport_cb, setup, NULL, &err)) {
1552 error("bt_io_accept: %s", err->message);
1560 chan = channel_new(server, device, io);
1564 chan->auth_id = btd_request_authorization(&src, &dst,
1565 ADVANCED_AUDIO_UUID,
1567 if (chan->auth_id == 0 && !chan->session)
1568 channel_remove(chan);
1573 g_io_channel_shutdown(io, TRUE, NULL);
1576 static bool a2dp_server_listen(struct a2dp_server *server)
1583 server->io = bt_io_listen(NULL, confirm_cb, server, NULL, &err,
1584 BT_IO_OPT_SOURCE_BDADDR,
1585 btd_adapter_get_address(server->adapter),
1586 BT_IO_OPT_PSM, AVDTP_PSM,
1587 BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_MEDIUM,
1588 #if defined(__TIZEN_PATCH__) && defined(SUPPORT_LOCAL_DEVICE_A2DP_SINK)
1589 BT_IO_OPT_IMTU, 895,
1591 BT_IO_OPT_MASTER, true,
1596 error("%s", err->message);
1602 static struct a2dp_server *a2dp_server_register(struct btd_adapter *adapter)
1604 struct a2dp_server *server;
1606 server = g_new0(struct a2dp_server, 1);
1608 server->adapter = btd_adapter_ref(adapter);
1609 server->seps = queue_new();
1610 server->channels = queue_new();
1612 servers = g_slist_append(servers, server);
1617 static void a2dp_unregister_sep(struct a2dp_sep *sep)
1619 struct a2dp_server *server = sep->server;
1622 sep->destroy(sep->user_data);
1623 sep->endpoint = NULL;
1626 avdtp_unregister_sep(server->seps, sep->lsep);
1630 if (!queue_isempty(server->seps))
1634 g_io_channel_shutdown(server->io, TRUE, NULL);
1635 g_io_channel_unref(server->io);
1640 static void a2dp_server_unregister(struct a2dp_server *server)
1642 servers = g_slist_remove(servers, server);
1643 queue_destroy(server->channels, channel_free);
1644 queue_destroy(server->seps, NULL);
1647 g_io_channel_shutdown(server->io, TRUE, NULL);
1648 g_io_channel_unref(server->io);
1651 btd_adapter_unref(server->adapter);
1655 struct a2dp_sep *a2dp_add_sep(struct btd_adapter *adapter, uint8_t type,
1656 uint8_t codec, gboolean delay_reporting,
1657 struct a2dp_endpoint *endpoint,
1658 void *user_data, GDestroyNotify destroy,
1661 struct a2dp_server *server;
1662 struct a2dp_sep *sep;
1664 uint32_t *record_id;
1665 sdp_record_t *record;
1667 server = find_server(servers, adapter);
1668 if (server == NULL) {
1670 *err = -EPROTONOSUPPORT;
1674 if (type == AVDTP_SEP_TYPE_SINK && !server->sink_enabled) {
1676 *err = -EPROTONOSUPPORT;
1680 if (type == AVDTP_SEP_TYPE_SOURCE && !server->source_enabled) {
1682 *err = -EPROTONOSUPPORT;
1686 sep = g_new0(struct a2dp_sep, 1);
1688 sep->lsep = avdtp_register_sep(server->seps, type,
1689 AVDTP_MEDIA_TYPE_AUDIO, codec,
1690 delay_reporting, &endpoint_ind,
1693 if (sep->lsep == NULL) {
1700 sep->server = server;
1701 sep->endpoint = endpoint;
1704 sep->delay_reporting = delay_reporting;
1705 sep->user_data = user_data;
1706 sep->destroy = destroy;
1708 if (type == AVDTP_SEP_TYPE_SOURCE) {
1709 l = &server->sources;
1710 record_id = &server->source_record_id;
1713 record_id = &server->sink_record_id;
1716 if (*record_id != 0)
1719 record = a2dp_record(type);
1721 error("Unable to allocate new service record");
1722 a2dp_unregister_sep(sep);
1728 if (adapter_service_add(server->adapter, record) < 0) {
1729 error("Unable to register A2DP service record");
1730 sdp_record_free(record);
1731 a2dp_unregister_sep(sep);
1737 if (!a2dp_server_listen(server)) {
1738 sdp_record_free(record);
1739 a2dp_unregister_sep(sep);
1745 *record_id = record->handle;
1748 *l = g_slist_append(*l, sep);
1755 void a2dp_remove_sep(struct a2dp_sep *sep)
1757 struct a2dp_server *server = sep->server;
1759 if (sep->type == AVDTP_SEP_TYPE_SOURCE) {
1760 if (g_slist_find(server->sources, sep) == NULL)
1762 server->sources = g_slist_remove(server->sources, sep);
1763 if (server->sources == NULL && server->source_record_id) {
1764 adapter_service_remove(server->adapter,
1765 server->source_record_id);
1766 server->source_record_id = 0;
1769 if (g_slist_find(server->sinks, sep) == NULL)
1771 server->sinks = g_slist_remove(server->sinks, sep);
1772 if (server->sinks == NULL && server->sink_record_id) {
1773 adapter_service_remove(server->adapter,
1774 server->sink_record_id);
1775 server->sink_record_id = 0;
1782 a2dp_unregister_sep(sep);
1785 static void select_cb(struct a2dp_setup *setup, void *ret, int size)
1787 struct avdtp_service_capability *media_transport, *media_codec;
1788 struct avdtp_media_codec_capability *cap;
1791 DBG("Endpoint replied an invalid configuration");
1795 media_transport = avdtp_service_cap_new(AVDTP_MEDIA_TRANSPORT,
1798 setup->caps = g_slist_append(setup->caps, media_transport);
1800 cap = g_malloc0(sizeof(*cap) + size);
1801 cap->media_type = AVDTP_MEDIA_TYPE_AUDIO;
1802 cap->media_codec_type = setup->sep->codec;
1803 memcpy(cap->data, ret, size);
1805 media_codec = avdtp_service_cap_new(AVDTP_MEDIA_CODEC, cap,
1806 sizeof(*cap) + size);
1808 setup->caps = g_slist_append(setup->caps, media_codec);
1812 finalize_select(setup);
1815 static struct a2dp_sep *a2dp_find_sep(struct avdtp *session, GSList *list,
1818 #ifdef __TIZEN_PATCH__
1819 struct a2dp_sep *selected_sep = NULL;
1822 for (; list; list = list->next) {
1823 struct a2dp_sep *sep = list->data;
1824 #ifdef __TIZEN_PATCH__
1825 struct avdtp_remote_sep *rsep;
1826 struct avdtp_media_codec_capability *cap;
1827 struct avdtp_service_capability *service;
1830 /* Use sender's endpoint if available */
1834 if (sep->endpoint == NULL)
1837 name = sep->endpoint->get_name(sep, sep->user_data);
1838 if (g_strcmp0(sender, name) != 0)
1842 #ifdef __TIZEN_PATCH__
1843 rsep = avdtp_find_remote_sep(session, sep->lsep);
1847 service = avdtp_get_codec(rsep);
1848 cap = (struct avdtp_media_codec_capability *) service->data;
1850 if (cap->media_codec_type != A2DP_CODEC_VENDOR) {
1855 if (avdtp_find_remote_sep(session, sep->lsep) == NULL)
1863 #ifdef __TIZEN_PATCH__
1865 return selected_sep;
1873 static struct a2dp_sep *a2dp_select_sep(struct avdtp *session, uint8_t type,
1876 struct a2dp_server *server;
1877 struct a2dp_sep *sep;
1880 server = find_server(servers, avdtp_get_adapter(session));
1884 l = type == AVDTP_SEP_TYPE_SINK ? server->sources : server->sinks;
1886 /* Check sender's seps first */
1887 sep = a2dp_find_sep(session, l, sender);
1891 return a2dp_find_sep(session, l, NULL);
1894 static void discover_cb(struct avdtp *session, GSList *seps,
1895 struct avdtp_error *err, void *user_data)
1897 struct a2dp_setup *setup = user_data;
1904 finalize_discover(setup);
1907 unsigned int a2dp_discover(struct avdtp *session, a2dp_discover_cb_t cb,
1910 struct a2dp_setup *setup;
1911 struct a2dp_setup_cb *cb_data;
1913 setup = a2dp_setup_get(session);
1917 cb_data = setup_cb_new(setup);
1918 cb_data->discover_cb = cb;
1919 cb_data->user_data = user_data;
1921 if (avdtp_discover(session, discover_cb, setup) == 0)
1924 setup_cb_free(cb_data);
1928 unsigned int a2dp_select_capabilities(struct avdtp *session,
1929 uint8_t type, const char *sender,
1930 a2dp_select_cb_t cb,
1933 struct a2dp_setup *setup;
1934 struct a2dp_setup_cb *cb_data;
1935 struct a2dp_sep *sep;
1936 struct avdtp_service_capability *service;
1937 struct avdtp_media_codec_capability *codec;
1940 sep = a2dp_select_sep(session, type, sender);
1942 error("Unable to select SEP");
1946 setup = a2dp_setup_get(session);
1950 cb_data = setup_cb_new(setup);
1951 cb_data->select_cb = cb;
1952 cb_data->user_data = user_data;
1955 setup->rsep = avdtp_find_remote_sep(session, sep->lsep);
1957 if (setup->rsep == NULL) {
1958 error("Could not find remote sep");
1962 service = avdtp_get_codec(setup->rsep);
1963 codec = (struct avdtp_media_codec_capability *) service->data;
1965 err = sep->endpoint->select_configuration(sep, codec->data,
1966 service->length - sizeof(*codec),
1968 select_cb, sep->user_data);
1973 setup_cb_free(cb_data);
1978 unsigned int a2dp_config(struct avdtp *session, struct a2dp_sep *sep,
1979 a2dp_config_cb_t cb, GSList *caps,
1982 struct a2dp_setup_cb *cb_data;
1984 struct a2dp_server *server;
1985 struct a2dp_setup *setup;
1986 struct a2dp_sep *tmp;
1987 struct avdtp_service_capability *cap;
1988 struct avdtp_media_codec_capability *codec_cap = NULL;
1991 server = find_server(servers, avdtp_get_adapter(session));
1995 for (l = caps; l != NULL; l = l->next) {
1998 if (cap->category != AVDTP_MEDIA_CODEC)
2001 codec_cap = (void *) cap->data;
2008 if (sep->codec != codec_cap->media_codec_type)
2011 DBG("a2dp_config: selected SEP %p", sep->lsep);
2013 setup = a2dp_setup_get(session);
2017 cb_data = setup_cb_new(setup);
2018 cb_data->config_cb = cb;
2019 cb_data->user_data = user_data;
2022 setup->stream = sep->stream;
2024 /* Copy given caps if they are different than current caps */
2025 if (setup->caps != caps) {
2026 g_slist_free_full(setup->caps, g_free);
2027 setup->caps = g_slist_copy(caps);
2030 switch (avdtp_sep_get_state(sep->lsep)) {
2031 case AVDTP_STATE_IDLE:
2032 if (sep->type == AVDTP_SEP_TYPE_SOURCE)
2033 l = server->sources;
2037 for (; l != NULL; l = l->next) {
2039 if (avdtp_has_stream(session, tmp->stream))
2046 setup->reconfigure = TRUE;
2047 if (avdtp_close(session, tmp->stream, FALSE) < 0) {
2048 error("avdtp_close failed");
2054 setup->rsep = avdtp_find_remote_sep(session, sep->lsep);
2055 if (setup->rsep == NULL) {
2056 error("No matching ACP and INT SEPs found");
2060 posix_err = avdtp_set_configuration(session, setup->rsep,
2063 if (posix_err < 0) {
2064 error("avdtp_set_configuration: %s",
2065 strerror(-posix_err));
2069 case AVDTP_STATE_OPEN:
2070 case AVDTP_STATE_STREAMING:
2071 if (avdtp_stream_has_capabilities(setup->stream, caps)) {
2072 DBG("Configuration match: resuming");
2073 cb_data->source_id = g_idle_add(finalize_config,
2075 } else if (!setup->reconfigure) {
2076 setup->reconfigure = TRUE;
2077 if (avdtp_close(session, sep->stream, FALSE) < 0) {
2078 error("avdtp_close failed");
2083 case AVDTP_STATE_CONFIGURED:
2084 case AVDTP_STATE_CLOSING:
2085 case AVDTP_STATE_ABORTING:
2087 error("SEP in bad state for requesting a new stream");
2094 setup_cb_free(cb_data);
2098 unsigned int a2dp_resume(struct avdtp *session, struct a2dp_sep *sep,
2099 a2dp_stream_cb_t cb, void *user_data)
2101 struct a2dp_setup_cb *cb_data;
2102 struct a2dp_setup *setup;
2104 setup = a2dp_setup_get(session);
2108 cb_data = setup_cb_new(setup);
2109 cb_data->resume_cb = cb;
2110 cb_data->user_data = user_data;
2113 setup->stream = sep->stream;
2115 switch (avdtp_sep_get_state(sep->lsep)) {
2116 case AVDTP_STATE_IDLE:
2119 case AVDTP_STATE_CONFIGURED:
2120 setup->start = TRUE;
2122 case AVDTP_STATE_OPEN:
2123 if (avdtp_start(session, sep->stream) < 0) {
2124 error("avdtp_start failed");
2127 sep->starting = TRUE;
2129 case AVDTP_STATE_STREAMING:
2130 if (!sep->suspending && sep->suspend_timer) {
2131 g_source_remove(sep->suspend_timer);
2132 sep->suspend_timer = 0;
2133 avdtp_unref(sep->session);
2134 sep->session = NULL;
2136 if (sep->suspending)
2137 setup->start = TRUE;
2139 cb_data->source_id = g_idle_add(finalize_resume,
2142 case AVDTP_STATE_CLOSING:
2143 case AVDTP_STATE_ABORTING:
2145 error("SEP in bad state for resume");
2152 setup_cb_free(cb_data);
2156 unsigned int a2dp_suspend(struct avdtp *session, struct a2dp_sep *sep,
2157 a2dp_stream_cb_t cb, void *user_data)
2159 struct a2dp_setup_cb *cb_data;
2160 struct a2dp_setup *setup;
2162 setup = a2dp_setup_get(session);
2166 cb_data = setup_cb_new(setup);
2167 cb_data->suspend_cb = cb;
2168 cb_data->user_data = user_data;
2171 setup->stream = sep->stream;
2173 switch (avdtp_sep_get_state(sep->lsep)) {
2174 case AVDTP_STATE_IDLE:
2175 error("a2dp_suspend: no stream to suspend");
2178 case AVDTP_STATE_OPEN:
2179 cb_data->source_id = g_idle_add(finalize_suspend, setup);
2181 case AVDTP_STATE_STREAMING:
2182 if (avdtp_suspend(session, sep->stream) < 0) {
2183 error("avdtp_suspend failed");
2186 sep->suspending = TRUE;
2188 case AVDTP_STATE_CONFIGURED:
2189 case AVDTP_STATE_CLOSING:
2190 case AVDTP_STATE_ABORTING:
2192 error("SEP in bad state for suspend");
2199 setup_cb_free(cb_data);
2203 gboolean a2dp_cancel(unsigned int id)
2207 for (ls = setups; ls != NULL; ls = g_slist_next(ls)) {
2208 struct a2dp_setup *setup = ls->data;
2210 for (l = setup->cb; l != NULL; l = g_slist_next(l)) {
2211 struct a2dp_setup_cb *cb = l->data;
2220 DBG("aborting setup %p", setup);
2221 if (!avdtp_abort(setup->session, setup->stream))
2233 gboolean a2dp_sep_lock(struct a2dp_sep *sep, struct avdtp *session)
2238 DBG("SEP %p locked", sep->lsep);
2244 gboolean a2dp_sep_unlock(struct a2dp_sep *sep, struct avdtp *session)
2246 struct a2dp_server *server = sep->server;
2247 avdtp_state_t state;
2250 state = avdtp_sep_get_state(sep->lsep);
2252 sep->locked = FALSE;
2254 DBG("SEP %p unlocked", sep->lsep);
2256 if (sep->type == AVDTP_SEP_TYPE_SOURCE)
2257 l = server->sources;
2261 /* Unregister sep if it was removed */
2262 if (g_slist_find(l, sep) == NULL) {
2263 a2dp_unregister_sep(sep);
2267 if (!sep->stream || state == AVDTP_STATE_IDLE)
2271 case AVDTP_STATE_OPEN:
2272 /* Set timer here */
2274 case AVDTP_STATE_STREAMING:
2275 if (avdtp_suspend(session, sep->stream) == 0)
2276 sep->suspending = TRUE;
2278 case AVDTP_STATE_IDLE:
2279 case AVDTP_STATE_CONFIGURED:
2280 case AVDTP_STATE_CLOSING:
2281 case AVDTP_STATE_ABORTING:
2289 struct avdtp_stream *a2dp_sep_get_stream(struct a2dp_sep *sep)
2294 struct btd_device *a2dp_setup_get_device(struct a2dp_setup *setup)
2296 if (setup->session == NULL)
2299 return avdtp_get_device(setup->session);
2302 static int a2dp_source_probe(struct btd_service *service)
2304 struct btd_device *dev = btd_service_get_device(service);
2306 DBG("path %s", device_get_path(dev));
2308 source_init(service);
2313 static void a2dp_source_remove(struct btd_service *service)
2315 source_unregister(service);
2318 static int a2dp_sink_probe(struct btd_service *service)
2320 struct btd_device *dev = btd_service_get_device(service);
2322 DBG("path %s", device_get_path(dev));
2324 return sink_init(service);
2327 static void a2dp_sink_remove(struct btd_service *service)
2329 sink_unregister(service);
2332 static int a2dp_source_connect(struct btd_service *service)
2334 struct btd_device *dev = btd_service_get_device(service);
2335 struct btd_adapter *adapter = device_get_adapter(dev);
2336 struct a2dp_server *server;
2337 const char *path = device_get_path(dev);
2339 DBG("path %s", path);
2341 server = find_server(servers, adapter);
2342 if (!server || !server->sink_enabled) {
2343 DBG("Unexpected error: cannot find server");
2344 return -EPROTONOSUPPORT;
2347 /* Return protocol not available if no record/endpoint exists */
2348 if (server->sink_record_id == 0)
2349 return -ENOPROTOOPT;
2351 return source_connect(service);
2354 static int a2dp_source_disconnect(struct btd_service *service)
2356 struct btd_device *dev = btd_service_get_device(service);
2357 const char *path = device_get_path(dev);
2359 DBG("path %s", path);
2361 return source_disconnect(service);
2364 static int a2dp_sink_connect(struct btd_service *service)
2366 struct btd_device *dev = btd_service_get_device(service);
2367 struct btd_adapter *adapter = device_get_adapter(dev);
2368 struct a2dp_server *server;
2369 const char *path = device_get_path(dev);
2371 DBG("path %s", path);
2373 server = find_server(servers, adapter);
2374 if (!server || !server->source_enabled) {
2375 DBG("Unexpected error: cannot find server");
2376 return -EPROTONOSUPPORT;
2379 /* Return protocol not available if no record/endpoint exists */
2380 if (server->source_record_id == 0)
2381 return -ENOPROTOOPT;
2383 return sink_connect(service);
2386 static int a2dp_sink_disconnect(struct btd_service *service)
2388 struct btd_device *dev = btd_service_get_device(service);
2389 const char *path = device_get_path(dev);
2391 DBG("path %s", path);
2393 return sink_disconnect(service);
2396 static int a2dp_source_server_probe(struct btd_profile *p,
2397 struct btd_adapter *adapter)
2399 struct a2dp_server *server;
2401 DBG("path %s", adapter_get_path(adapter));
2403 server = find_server(servers, adapter);
2407 server = a2dp_server_register(adapter);
2409 return -EPROTONOSUPPORT;
2412 server->source_enabled = TRUE;
2417 static void a2dp_source_server_remove(struct btd_profile *p,
2418 struct btd_adapter *adapter)
2420 struct a2dp_server *server;
2422 DBG("path %s", adapter_get_path(adapter));
2424 server = find_server(servers, adapter);
2428 g_slist_free_full(server->sources,
2429 (GDestroyNotify) a2dp_unregister_sep);
2431 if (server->source_record_id) {
2432 adapter_service_remove(server->adapter,
2433 server->source_record_id);
2434 server->source_record_id = 0;
2437 if (server->sink_record_id)
2440 a2dp_server_unregister(server);
2443 static int a2dp_sink_server_probe(struct btd_profile *p,
2444 struct btd_adapter *adapter)
2446 struct a2dp_server *server;
2448 DBG("path %s", adapter_get_path(adapter));
2450 server = find_server(servers, adapter);
2454 server = a2dp_server_register(adapter);
2456 return -EPROTONOSUPPORT;
2459 server->sink_enabled = TRUE;
2464 static void a2dp_sink_server_remove(struct btd_profile *p,
2465 struct btd_adapter *adapter)
2467 struct a2dp_server *server;
2469 DBG("path %s", adapter_get_path(adapter));
2471 server = find_server(servers, adapter);
2475 g_slist_free_full(server->sinks, (GDestroyNotify) a2dp_unregister_sep);
2477 if (server->sink_record_id) {
2478 adapter_service_remove(server->adapter, server->sink_record_id);
2479 server->sink_record_id = 0;
2482 if (server->source_record_id)
2485 a2dp_server_unregister(server);
2488 static int media_server_probe(struct btd_adapter *adapter)
2490 DBG("path %s", adapter_get_path(adapter));
2492 return media_register(adapter);
2495 static void media_server_remove(struct btd_adapter *adapter)
2497 DBG("path %s", adapter_get_path(adapter));
2499 media_unregister(adapter);
2502 static struct btd_profile a2dp_source_profile = {
2503 .name = "a2dp-source",
2504 .priority = BTD_PROFILE_PRIORITY_MEDIUM,
2506 .remote_uuid = A2DP_SOURCE_UUID,
2507 .device_probe = a2dp_source_probe,
2508 .device_remove = a2dp_source_remove,
2510 .auto_connect = true,
2511 .connect = a2dp_source_connect,
2512 .disconnect = a2dp_source_disconnect,
2514 .adapter_probe = a2dp_sink_server_probe,
2515 .adapter_remove = a2dp_sink_server_remove,
2518 static struct btd_profile a2dp_sink_profile = {
2519 .name = "a2dp-sink",
2520 .priority = BTD_PROFILE_PRIORITY_MEDIUM,
2522 .remote_uuid = A2DP_SINK_UUID,
2523 .device_probe = a2dp_sink_probe,
2524 .device_remove = a2dp_sink_remove,
2526 .auto_connect = true,
2527 .connect = a2dp_sink_connect,
2528 .disconnect = a2dp_sink_disconnect,
2530 .adapter_probe = a2dp_source_server_probe,
2531 .adapter_remove = a2dp_source_server_remove,
2534 static struct btd_adapter_driver media_driver = {
2536 .probe = media_server_probe,
2537 .remove = media_server_remove,
2540 static int a2dp_init(void)
2542 btd_register_adapter_driver(&media_driver);
2543 #if defined(__TIZEN_PATCH__) && defined(SUPPORT_LOCAL_DEVICE_A2DP_SINK)
2544 btd_profile_register(&a2dp_source_profile);
2546 btd_profile_register(&a2dp_sink_profile);
2551 static void a2dp_exit(void)
2553 btd_unregister_adapter_driver(&media_driver);
2554 btd_profile_unregister(&a2dp_source_profile);
2555 btd_profile_unregister(&a2dp_sink_profile);
2558 BLUETOOTH_PLUGIN_DEFINE(a2dp, VERSION, BLUETOOTH_PLUGIN_PRIORITY_DEFAULT,
2559 a2dp_init, a2dp_exit)