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
34 #include <dbus/dbus.h>
37 #include "lib/bluetooth.h"
39 #include "lib/sdp_lib.h"
42 #include "gdbus/gdbus.h"
45 #include "src/plugin.h"
46 #include "src/adapter.h"
47 #include "src/device.h"
48 #include "src/dbus-common.h"
49 #include "src/error.h"
50 #include "src/profile.h"
51 #include "src/service.h"
54 #include "src/shared/queue.h"
55 #include "src/shared/util.h"
57 #include "btio/btio.h"
63 #include "a2dp-codecs.h"
66 /* The duration that streams without users are allowed to stay in
68 #define SUSPEND_TIMEOUT 5
69 #define RECONFIGURE_TIMEOUT 500
72 #define MEDIA_ENDPOINT_INTERFACE "org.bluez.MediaEndpoint1"
74 #ifdef TIZEN_FEATURE_BLUEZ_MODIFY
75 #ifdef TIZEN_FEATURE_SSC_ENCODER
76 /* Vendor and Codec ID for Samsung Scalable codec */
77 #define PREFRED_VENDOR_ID 0x00000075
78 #define PREFRED_CODEC_ID 0x0103
83 struct a2dp_server *server;
84 struct a2dp_endpoint *endpoint;
87 struct avdtp_local_sep *lsep;
88 struct avdtp *session;
89 struct avdtp_stream *stream;
91 #ifdef TIZEN_FEATURE_BLUEZ_MODIFY
92 gboolean remote_suspended;
94 gboolean delay_reporting;
99 GDestroyNotify destroy;
102 struct a2dp_setup_cb {
103 struct a2dp_setup *setup;
104 a2dp_discover_cb_t discover_cb;
105 a2dp_select_cb_t select_cb;
106 a2dp_config_cb_t config_cb;
107 a2dp_stream_cb_t resume_cb;
108 a2dp_stream_cb_t suspend_cb;
115 struct a2dp_channel *chan;
116 struct avdtp *session;
118 struct a2dp_sep *sep;
119 struct a2dp_remote_sep *rsep;
120 struct avdtp_stream *stream;
121 struct avdtp_error *err;
122 avdtp_set_configuration_cb setconf_cb;
125 gboolean reconfigure;
133 struct btd_adapter *adapter;
136 uint32_t source_record_id;
137 uint32_t sink_record_id;
138 gboolean sink_enabled;
139 gboolean source_enabled;
142 struct queue *channels;
145 struct a2dp_remote_sep {
146 struct a2dp_channel *chan;
148 struct avdtp_remote_sep *sep;
151 struct a2dp_last_used {
152 struct a2dp_sep *lsep;
153 struct a2dp_remote_sep *rsep;
156 struct a2dp_channel {
157 struct a2dp_server *server;
158 struct btd_device *device;
161 unsigned int state_id;
162 unsigned int auth_id;
163 struct avdtp *session;
165 struct a2dp_last_used *last_used;
168 static GSList *servers = NULL;
169 static GSList *setups = NULL;
170 static unsigned int cb_id = 0;
172 static struct a2dp_setup *setup_ref(struct a2dp_setup *setup)
176 DBG("%p: ref=%d", setup, setup->ref);
181 static bool match_by_session(const void *data, const void *user_data)
183 const struct a2dp_channel *chan = data;
184 const struct avdtp *session = user_data;
186 return chan->session == session;
189 static struct a2dp_channel *find_channel(struct avdtp *session)
193 for (l = servers; l; l = g_slist_next(l)) {
194 struct a2dp_server *server = l->data;
195 struct a2dp_channel *chan;
197 chan = queue_find(server->channels, match_by_session, session);
205 static struct a2dp_setup *setup_new(struct avdtp *session)
207 struct a2dp_setup *setup;
208 struct a2dp_channel *chan;
210 chan = find_channel(session);
214 setup = g_new0(struct a2dp_setup, 1);
215 setup->session = avdtp_ref(session);
216 setup->chan = find_channel(session);
217 setups = g_slist_append(setups, setup);
222 static void setup_free(struct a2dp_setup *s)
227 g_io_channel_shutdown(s->io, TRUE, NULL);
228 g_io_channel_unref(s->io);
231 queue_destroy(s->eps, NULL);
233 setups = g_slist_remove(setups, s);
235 avdtp_unref(s->session);
236 g_slist_free_full(s->cb, g_free);
237 g_slist_free_full(s->caps, g_free);
241 static void setup_unref(struct a2dp_setup *setup)
245 DBG("%p: ref=%d", setup, setup->ref);
253 static struct a2dp_setup_cb *setup_cb_new(struct a2dp_setup *setup)
255 struct a2dp_setup_cb *cb;
257 cb = g_new0(struct a2dp_setup_cb, 1);
261 setup->cb = g_slist_append(setup->cb, cb);
265 static void setup_cb_free(struct a2dp_setup_cb *cb)
267 struct a2dp_setup *setup = cb->setup;
270 g_source_remove(cb->source_id);
272 setup->cb = g_slist_remove(setup->cb, cb);
273 setup_unref(cb->setup);
277 static void finalize_setup_errno(struct a2dp_setup *s, int err,
278 GSourceFunc cb1, ...)
280 GSourceFunc finalize;
282 #ifdef TIZEN_FEATURE_BLUEZ_MODIFY
283 struct avdtp_error *avdtp_err;
285 struct avdtp_error avdtp_err;
289 #ifdef TIZEN_FEATURE_BLUEZ_MODIFY
290 avdtp_err = g_new(struct avdtp_error, 1);
291 avdtp_error_init(avdtp_err, AVDTP_ERRNO, -err);
294 avdtp_error_init(&avdtp_err, AVDTP_ERRNO, -err);
302 while (finalize != NULL) {
304 finalize = va_arg(args, GSourceFunc);
310 static int error_to_errno(struct avdtp_error *err)
317 if (avdtp_error_category(err) != AVDTP_ERRNO)
320 perr = avdtp_error_posix_errno(err);
327 * An unexpect error has occurred setup may be attempted again.
333 static gboolean finalize_config(gpointer data)
335 struct a2dp_setup *s = data;
337 struct avdtp_stream *stream = s->err ? NULL : s->stream;
339 for (l = s->cb; l != NULL; ) {
340 struct a2dp_setup_cb *cb = l->data;
347 cb->config_cb(s->session, s->sep, stream,
348 error_to_errno(s->err), cb->user_data);
355 static gboolean finalize_resume(gpointer data)
357 struct a2dp_setup *s = data;
360 for (l = s->cb; l != NULL; ) {
361 struct a2dp_setup_cb *cb = l->data;
368 cb->resume_cb(s->session, error_to_errno(s->err),
376 static gboolean finalize_suspend(gpointer data)
378 struct a2dp_setup *s = data;
381 for (l = s->cb; l != NULL; ) {
382 struct a2dp_setup_cb *cb = l->data;
389 cb->suspend_cb(s->session, error_to_errno(s->err),
397 static void finalize_select(struct a2dp_setup *s)
401 for (l = s->cb; l != NULL; ) {
402 struct a2dp_setup_cb *cb = l->data;
409 cb->select_cb(s->session, s->sep, s->caps, cb->user_data);
414 static void finalize_discover(struct a2dp_setup *s)
418 for (l = s->cb; l != NULL; ) {
419 struct a2dp_setup_cb *cb = l->data;
423 if (!cb->discover_cb)
426 cb->discover_cb(s->session, s->seps, error_to_errno(s->err),
432 static struct a2dp_setup *find_setup_by_session(struct avdtp *session)
436 for (l = setups; l != NULL; l = l->next) {
437 struct a2dp_setup *setup = l->data;
439 if (setup->session == session)
446 static struct a2dp_setup *a2dp_setup_get(struct avdtp *session)
448 struct a2dp_setup *setup;
450 setup = find_setup_by_session(session);
452 setup = setup_new(session);
457 return setup_ref(setup);
460 static struct a2dp_setup *find_setup_by_stream(struct avdtp_stream *stream)
464 for (l = setups; l != NULL; l = l->next) {
465 struct a2dp_setup *setup = l->data;
467 if (setup->stream == stream)
474 static void stream_state_changed(struct avdtp_stream *stream,
475 avdtp_state_t old_state,
476 avdtp_state_t new_state,
477 struct avdtp_error *err,
480 struct a2dp_sep *sep = user_data;
482 if (new_state == AVDTP_STATE_OPEN) {
483 struct a2dp_setup *setup;
486 setup = find_setup_by_stream(stream);
487 if (!setup || !setup->start)
490 setup->start = FALSE;
492 err = avdtp_start(setup->session, stream);
493 if (err < 0 && err != -EINPROGRESS) {
494 error("avdtp_start: %s (%d)", strerror(-err), -err);
495 finalize_setup_errno(setup, err, finalize_resume,
500 sep->starting = TRUE;
505 #ifdef TIZEN_FEATURE_BLUEZ_MODIFY
506 if (new_state == AVDTP_STATE_STREAMING && sep->suspend_timer) {
507 g_source_remove(sep->suspend_timer);
508 sep->suspend_timer = 0;
512 if (new_state != AVDTP_STATE_IDLE)
515 if (sep->suspend_timer) {
516 g_source_remove(sep->suspend_timer);
517 sep->suspend_timer = 0;
521 avdtp_unref(sep->session);
527 if (sep->endpoint && sep->endpoint->clear_configuration)
528 sep->endpoint->clear_configuration(sep, sep->user_data);
531 static gboolean auto_config(gpointer data)
533 struct a2dp_setup *setup = data;
534 struct btd_device *dev = NULL;
535 struct btd_service *service;
537 /* Check if configuration was aborted */
538 if (setup->sep->stream == NULL)
541 if (setup->err != NULL)
544 dev = avdtp_get_device(setup->session);
546 avdtp_stream_add_cb(setup->session, setup->stream,
547 stream_state_changed, setup->sep);
549 if (setup->sep->type == AVDTP_SEP_TYPE_SOURCE) {
550 service = btd_device_get_service(dev, A2DP_SINK_UUID);
551 sink_new_stream(service, setup->session, setup->stream);
553 service = btd_device_get_service(dev, A2DP_SOURCE_UUID);
554 source_new_stream(service, setup->session, setup->stream);
558 if (setup->setconf_cb)
559 setup->setconf_cb(setup->session, setup->stream, setup->err);
561 finalize_config(setup);
573 static void endpoint_setconf_cb(struct a2dp_setup *setup, gboolean ret)
576 setup->err = g_new(struct avdtp_error, 1);
577 avdtp_error_init(setup->err, AVDTP_MEDIA_CODEC,
578 AVDTP_UNSUPPORTED_CONFIGURATION);
585 #ifdef TIZEN_FEATURE_BLUEZ_MODIFY
586 #ifdef TIZEN_FEATURE_SSC_ENCODER
587 static gboolean endpoint_match_codec_preference(struct avdtp *session,
588 struct avdtp_media_codec_capability *codec,
591 struct a2dp_sep *sep = user_data;
592 uint8_t *capabilities;
593 a2dp_vendor_codec_t *local_codec;
596 length = sep->endpoint->get_capabilities(sep, &capabilities,
598 if (length < sizeof(a2dp_vendor_codec_t))
601 local_codec = (a2dp_vendor_codec_t *) capabilities;
602 DBG("vendor 0x%08x codec 0x%04x", btohl(local_codec->vendor_id),
603 btohs(local_codec->codec_id));
604 if (btohl(local_codec->vendor_id) == PREFRED_VENDOR_ID &&
605 btohs(local_codec->codec_id) == PREFRED_CODEC_ID) {
606 DBG("Samsung Scalable Codec Selected");
614 static gboolean endpoint_match_codec_ind(struct avdtp *session,
615 struct avdtp_media_codec_capability *codec,
618 struct a2dp_sep *sep = user_data;
619 a2dp_vendor_codec_t *remote_codec;
620 a2dp_vendor_codec_t *local_codec;
621 uint8_t *capabilities;
624 if (codec->media_codec_type != A2DP_CODEC_VENDOR)
627 if (sep->endpoint == NULL)
630 length = sep->endpoint->get_capabilities(sep, &capabilities,
632 if (length < sizeof(a2dp_vendor_codec_t))
635 local_codec = (a2dp_vendor_codec_t *) capabilities;
636 remote_codec = (a2dp_vendor_codec_t *) codec->data;
638 if (A2DP_GET_VENDOR_ID(*remote_codec) !=
639 A2DP_GET_VENDOR_ID(*local_codec))
642 if (A2DP_GET_CODEC_ID(*remote_codec) != A2DP_GET_CODEC_ID(*local_codec))
645 DBG("vendor 0x%08x codec 0x%04x", A2DP_GET_VENDOR_ID(*remote_codec),
646 A2DP_GET_CODEC_ID(*remote_codec));
650 static void reverse_discover(struct avdtp *session, GSList *seps, int err,
656 static gboolean endpoint_setconf_ind(struct avdtp *session,
657 struct avdtp_local_sep *sep,
658 struct avdtp_stream *stream,
660 avdtp_set_configuration_cb cb,
663 struct a2dp_sep *a2dp_sep = user_data;
664 struct a2dp_setup *setup;
666 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
667 DBG("Sink %p: Set_Configuration_Ind", sep);
669 DBG("Source %p: Set_Configuration_Ind", sep);
671 setup = a2dp_setup_get(session);
675 a2dp_sep->stream = stream;
676 setup->sep = a2dp_sep;
677 setup->stream = stream;
678 setup->setconf_cb = cb;
680 for (; caps != NULL; caps = g_slist_next(caps)) {
681 struct avdtp_service_capability *cap = caps->data;
682 struct avdtp_media_codec_capability *codec;
685 if (cap->category == AVDTP_DELAY_REPORTING &&
686 !a2dp_sep->delay_reporting) {
687 setup->err = g_new(struct avdtp_error, 1);
688 avdtp_error_init(setup->err, AVDTP_DELAY_REPORTING,
689 AVDTP_UNSUPPORTED_CONFIGURATION);
693 if (cap->category != AVDTP_MEDIA_CODEC)
696 codec = (struct avdtp_media_codec_capability *) cap->data;
698 if (codec->media_codec_type != a2dp_sep->codec) {
699 setup->err = g_new(struct avdtp_error, 1);
700 avdtp_error_init(setup->err, AVDTP_MEDIA_CODEC,
701 AVDTP_UNSUPPORTED_CONFIGURATION);
705 ret = a2dp_sep->endpoint->set_configuration(a2dp_sep,
707 cap->length - sizeof(*codec),
710 a2dp_sep->user_data);
712 /* Attempt to reverse discover if there are no remote
715 if (queue_isempty(setup->chan->seps))
716 a2dp_discover(session, reverse_discover, NULL);
721 setup->err = g_new(struct avdtp_error, 1);
722 avdtp_error_init(setup->err, AVDTP_MEDIA_CODEC,
723 AVDTP_UNSUPPORTED_CONFIGURATION);
728 g_idle_add(auto_config, setup);
732 static gboolean endpoint_getcap_ind(struct avdtp *session,
733 struct avdtp_local_sep *sep,
734 gboolean get_all, GSList **caps,
735 uint8_t *err, void *user_data)
737 struct a2dp_sep *a2dp_sep = user_data;
738 struct avdtp_service_capability *media_transport, *media_codec;
739 struct avdtp_media_codec_capability *codec_caps;
740 uint8_t *capabilities;
743 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
744 DBG("Sink %p: Get_Capability_Ind", sep);
746 DBG("Source %p: Get_Capability_Ind", sep);
750 media_transport = avdtp_service_cap_new(AVDTP_MEDIA_TRANSPORT,
753 *caps = g_slist_append(*caps, media_transport);
755 length = a2dp_sep->endpoint->get_capabilities(a2dp_sep, &capabilities,
756 a2dp_sep->user_data);
758 codec_caps = g_malloc0(sizeof(*codec_caps) + length);
759 codec_caps->media_type = AVDTP_MEDIA_TYPE_AUDIO;
760 codec_caps->media_codec_type = a2dp_sep->codec;
761 memcpy(codec_caps->data, capabilities, length);
763 media_codec = avdtp_service_cap_new(AVDTP_MEDIA_CODEC, codec_caps,
764 sizeof(*codec_caps) + length);
766 *caps = g_slist_append(*caps, media_codec);
770 struct avdtp_service_capability *delay_reporting;
771 delay_reporting = avdtp_service_cap_new(AVDTP_DELAY_REPORTING,
773 *caps = g_slist_append(*caps, delay_reporting);
779 static void endpoint_open_cb(struct a2dp_setup *setup, gboolean ret)
784 setup->stream = NULL;
785 finalize_setup_errno(setup, -EPERM, finalize_config, NULL);
789 err = avdtp_open(setup->session, setup->stream);
793 error("avdtp_open %s (%d)", strerror(-err), -err);
794 setup->stream = NULL;
795 finalize_setup_errno(setup, err, finalize_config, NULL);
800 static void setconf_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
801 struct avdtp_stream *stream,
802 struct avdtp_error *err, void *user_data)
804 struct a2dp_sep *a2dp_sep = user_data;
805 struct a2dp_setup *setup;
806 struct btd_device *dev;
807 struct btd_service *service;
810 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
811 DBG("Sink %p: Set_Configuration_Cfm", sep);
813 DBG("Source %p: Set_Configuration_Cfm", sep);
815 setup = find_setup_by_session(session);
821 finalize_config(setup);
828 avdtp_stream_add_cb(session, stream, stream_state_changed, a2dp_sep);
829 a2dp_sep->stream = stream;
834 dev = avdtp_get_device(session);
836 /* Notify D-Bus interface of the new stream */
837 if (a2dp_sep->type == AVDTP_SEP_TYPE_SOURCE) {
838 service = btd_device_get_service(dev, A2DP_SINK_UUID);
839 sink_new_stream(service, session, setup->stream);
841 service = btd_device_get_service(dev, A2DP_SOURCE_UUID);
842 source_new_stream(service, session, setup->stream);
845 /* Notify Endpoint */
846 if (a2dp_sep->endpoint) {
847 struct avdtp_service_capability *service;
848 struct avdtp_media_codec_capability *codec;
851 service = avdtp_stream_get_codec(stream);
852 codec = (struct avdtp_media_codec_capability *) service->data;
854 err = a2dp_sep->endpoint->set_configuration(a2dp_sep,
855 codec->data, service->length -
859 a2dp_sep->user_data);
863 setup->stream = NULL;
864 finalize_setup_errno(setup, -EPERM, finalize_config, NULL);
869 ret = avdtp_open(session, stream);
871 error("avdtp_open %s (%d)", strerror(-ret), -ret);
872 setup->stream = NULL;
873 finalize_setup_errno(setup, ret, finalize_config, NULL);
877 static gboolean getconf_ind(struct avdtp *session, struct avdtp_local_sep *sep,
878 uint8_t *err, void *user_data)
880 struct a2dp_sep *a2dp_sep = user_data;
882 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
883 DBG("Sink %p: Get_Configuration_Ind", sep);
885 DBG("Source %p: Get_Configuration_Ind", sep);
889 static void getconf_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
890 struct avdtp_stream *stream, struct avdtp_error *err,
893 struct a2dp_sep *a2dp_sep = user_data;
895 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
896 DBG("Sink %p: Set_Configuration_Cfm", sep);
898 DBG("Source %p: Set_Configuration_Cfm", sep);
901 static bool match_remote_sep(const void *data, const void *user_data)
903 const struct a2dp_remote_sep *sep = data;
904 const struct avdtp_remote_sep *rsep = user_data;
906 return sep->sep == rsep;
909 static void store_last_used(struct a2dp_channel *chan, uint8_t lseid,
913 char filename[PATH_MAX];
919 ba2str(device_get_address(chan->device), dst_addr);
921 snprintf(filename, PATH_MAX, STORAGEDIR "/%s/cache/%s",
922 btd_adapter_get_storage_dir(device_get_adapter(chan->device)),
924 key_file = g_key_file_new();
925 g_key_file_load_from_file(key_file, filename, 0, NULL);
927 sprintf(value, "%02hhx:%02hhx", lseid, rseid);
929 g_key_file_set_string(key_file, "Endpoints", "LastUsed", value);
931 data = g_key_file_to_data(key_file, &len, NULL);
932 g_file_set_contents(filename, data, len, NULL);
935 g_key_file_free(key_file);
938 static void add_last_used(struct a2dp_channel *chan, struct a2dp_sep *lsep,
939 struct a2dp_remote_sep *rsep)
941 if (!chan->last_used)
942 chan->last_used = new0(struct a2dp_last_used, 1);
944 chan->last_used->lsep = lsep;
945 chan->last_used->rsep = rsep;
948 static void update_last_used(struct a2dp_channel *chan, struct a2dp_sep *lsep,
949 struct avdtp_stream *stream)
951 struct avdtp_remote_sep *rsep;
952 struct a2dp_remote_sep *sep;
954 rsep = avdtp_stream_get_remote_sep(stream);
955 sep = queue_find(chan->seps, match_remote_sep, rsep);
957 error("Unable to find remote SEP");
961 /* Check if already stored then skip */
962 if (chan->last_used && (chan->last_used->lsep == lsep &&
963 chan->last_used->rsep == sep))
966 add_last_used(chan, lsep, sep);
968 store_last_used(chan, avdtp_sep_get_seid(lsep->lsep),
969 avdtp_get_seid(rsep));
972 static gboolean open_ind(struct avdtp *session, struct avdtp_local_sep *sep,
973 struct avdtp_stream *stream, uint8_t *err,
976 struct a2dp_sep *a2dp_sep = user_data;
977 struct a2dp_setup *setup;
979 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
980 DBG("Sink %p: Open_Ind", sep);
982 DBG("Source %p: Open_Ind", sep);
984 setup = a2dp_setup_get(session);
988 setup->stream = stream;
990 if (!err && setup->chan)
991 update_last_used(setup->chan, a2dp_sep, stream);
993 if (setup->reconfigure)
994 setup->reconfigure = FALSE;
996 finalize_config(setup);
1001 static void open_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
1002 struct avdtp_stream *stream, struct avdtp_error *err,
1005 struct a2dp_sep *a2dp_sep = user_data;
1006 struct a2dp_setup *setup;
1008 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
1009 DBG("Sink %p: Open_Cfm", sep);
1011 DBG("Source %p: Open_Cfm", sep);
1013 setup = find_setup_by_session(session);
1017 if (setup->reconfigure)
1018 setup->reconfigure = FALSE;
1021 setup->stream = NULL;
1024 finalize_resume(setup);
1025 } else if (setup->chan)
1026 update_last_used(setup->chan, a2dp_sep, stream);
1028 finalize_config(setup);
1033 static gboolean suspend_timeout(struct a2dp_sep *sep)
1035 if (avdtp_suspend(sep->session, sep->stream) == 0)
1036 sep->suspending = TRUE;
1038 sep->suspend_timer = 0;
1040 avdtp_unref(sep->session);
1041 sep->session = NULL;
1046 static gboolean start_ind(struct avdtp *session, struct avdtp_local_sep *sep,
1047 struct avdtp_stream *stream, uint8_t *err,
1050 struct a2dp_sep *a2dp_sep = user_data;
1051 struct a2dp_setup *setup;
1053 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
1054 DBG("Sink %p: Start_Ind", sep);
1056 DBG("Source %p: Start_Ind", sep);
1058 #ifdef TIZEN_FEATURE_BLUEZ_MODIFY
1059 if (!a2dp_sep->locked) {
1060 if (a2dp_sep->remote_suspended == FALSE) {
1061 a2dp_sep->session = avdtp_ref(session);
1062 a2dp_sep->suspend_timer = g_timeout_add_seconds(SUSPEND_TIMEOUT,
1063 (GSourceFunc) suspend_timeout,
1066 a2dp_sep->remote_suspended = FALSE;
1070 if (!a2dp_sep->locked) {
1071 a2dp_sep->session = avdtp_ref(session);
1072 a2dp_sep->suspend_timer = g_timeout_add_seconds(SUSPEND_TIMEOUT,
1073 (GSourceFunc) suspend_timeout,
1078 if (!a2dp_sep->starting)
1081 a2dp_sep->starting = FALSE;
1083 setup = find_setup_by_session(session);
1085 finalize_resume(setup);
1090 static void start_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
1091 struct avdtp_stream *stream, struct avdtp_error *err,
1094 struct a2dp_sep *a2dp_sep = user_data;
1095 struct a2dp_setup *setup;
1097 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
1098 DBG("Sink %p: Start_Cfm", sep);
1100 DBG("Source %p: Start_Cfm", sep);
1102 a2dp_sep->starting = FALSE;
1104 setup = find_setup_by_session(session);
1109 setup->stream = NULL;
1113 finalize_resume(setup);
1116 static gboolean suspend_ind(struct avdtp *session, struct avdtp_local_sep *sep,
1117 struct avdtp_stream *stream, uint8_t *err,
1120 struct a2dp_sep *a2dp_sep = user_data;
1121 struct a2dp_setup *setup;
1125 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
1126 DBG("Sink %p: Suspend_Ind", sep);
1128 DBG("Source %p: Suspend_Ind", sep);
1130 #ifdef TIZEN_FEATURE_BLUEZ_MODIFY
1131 a2dp_sep->remote_suspended = TRUE;
1134 if (a2dp_sep->suspend_timer) {
1135 g_source_remove(a2dp_sep->suspend_timer);
1136 a2dp_sep->suspend_timer = 0;
1137 avdtp_unref(a2dp_sep->session);
1138 a2dp_sep->session = NULL;
1141 if (!a2dp_sep->suspending)
1144 a2dp_sep->suspending = FALSE;
1146 setup = find_setup_by_session(session);
1150 start = setup->start;
1151 setup->start = FALSE;
1153 finalize_suspend(setup);
1158 start_err = avdtp_start(session, a2dp_sep->stream);
1159 if (start_err < 0 && start_err != -EINPROGRESS) {
1160 error("avdtp_start: %s (%d)", strerror(-start_err),
1162 finalize_setup_errno(setup, start_err, finalize_resume, NULL);
1168 static void suspend_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
1169 struct avdtp_stream *stream, struct avdtp_error *err,
1172 struct a2dp_sep *a2dp_sep = user_data;
1173 struct a2dp_setup *setup;
1177 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
1178 DBG("Sink %p: Suspend_Cfm", sep);
1180 DBG("Source %p: Suspend_Cfm", sep);
1182 a2dp_sep->suspending = FALSE;
1184 setup = find_setup_by_session(session);
1188 start = setup->start;
1189 setup->start = FALSE;
1192 setup->stream = NULL;
1196 finalize_suspend(setup);
1202 finalize_resume(setup);
1206 start_err = avdtp_start(session, a2dp_sep->stream);
1207 if (start_err < 0 && start_err != -EINPROGRESS) {
1208 error("avdtp_start: %s (%d)", strerror(-start_err), -start_err);
1209 finalize_setup_errno(setup, start_err, finalize_suspend, NULL);
1213 static gboolean close_ind(struct avdtp *session, struct avdtp_local_sep *sep,
1214 struct avdtp_stream *stream, uint8_t *err,
1217 struct a2dp_sep *a2dp_sep = user_data;
1218 struct a2dp_setup *setup;
1220 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
1221 DBG("Sink %p: Close_Ind", sep);
1223 DBG("Source %p: Close_Ind", sep);
1225 setup = find_setup_by_session(session);
1229 finalize_setup_errno(setup, -ECONNRESET, finalize_suspend,
1230 finalize_resume, NULL);
1235 static struct a2dp_remote_sep *find_remote_sep(struct a2dp_channel *chan,
1236 struct a2dp_sep *sep)
1238 struct avdtp_remote_sep *rsep;
1240 rsep = avdtp_find_remote_sep(chan->session, sep->lsep);
1242 return queue_find(chan->seps, match_remote_sep, rsep);
1245 static gboolean a2dp_reconfigure(gpointer data)
1247 struct a2dp_setup *setup = data;
1248 struct a2dp_sep *sep = setup->sep;
1250 struct avdtp_media_codec_capability *rsep_codec;
1251 struct avdtp_service_capability *cap;
1254 error("no valid local SEP");
1255 posix_err = -EINVAL;
1260 cap = avdtp_get_codec(setup->rsep->sep);
1261 rsep_codec = (struct avdtp_media_codec_capability *) cap->data;
1264 if (!setup->rsep || sep->codec != rsep_codec->media_codec_type)
1265 setup->rsep = find_remote_sep(setup->chan, sep);
1268 error("unable to find remote SEP");
1269 posix_err = -EINVAL;
1273 posix_err = avdtp_set_configuration(setup->session, setup->rsep->sep,
1277 if (posix_err < 0) {
1278 error("avdtp_set_configuration: %s", strerror(-posix_err));
1285 finalize_setup_errno(setup, posix_err, finalize_config, NULL);
1289 static struct a2dp_remote_sep *get_remote_sep(struct a2dp_channel *chan,
1290 struct avdtp_stream *stream)
1292 struct avdtp_remote_sep *rsep;
1294 rsep = avdtp_stream_get_remote_sep(stream);
1296 return queue_find(chan->seps, match_remote_sep, rsep);
1299 static void close_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
1300 struct avdtp_stream *stream, struct avdtp_error *err,
1303 struct a2dp_sep *a2dp_sep = user_data;
1304 struct a2dp_setup *setup;
1306 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
1307 DBG("Sink %p: Close_Cfm", sep);
1309 DBG("Source %p: Close_Cfm", sep);
1311 setup = find_setup_by_session(session);
1316 setup->stream = NULL;
1318 finalize_config(setup);
1323 setup->rsep = get_remote_sep(setup->chan, stream);
1325 if (setup->reconfigure)
1326 g_timeout_add(RECONFIGURE_TIMEOUT, a2dp_reconfigure, setup);
1329 static void abort_ind(struct avdtp *session, struct avdtp_local_sep *sep,
1330 struct avdtp_stream *stream, uint8_t *err,
1333 struct a2dp_sep *a2dp_sep = user_data;
1334 struct a2dp_setup *setup;
1336 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
1337 DBG("Sink %p: Abort_Ind", sep);
1339 DBG("Source %p: Abort_Ind", sep);
1341 a2dp_sep->stream = NULL;
1343 setup = find_setup_by_session(session);
1347 finalize_setup_errno(setup, -ECONNRESET, finalize_suspend,
1349 finalize_config, NULL);
1354 static void abort_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
1355 struct avdtp_stream *stream, struct avdtp_error *err,
1358 struct a2dp_sep *a2dp_sep = user_data;
1359 struct a2dp_setup *setup;
1361 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
1362 DBG("Sink %p: Abort_Cfm", sep);
1364 DBG("Source %p: Abort_Cfm", sep);
1366 setup = find_setup_by_session(session);
1370 if (setup->reconfigure) {
1371 g_timeout_add(RECONFIGURE_TIMEOUT, a2dp_reconfigure, setup);
1378 static gboolean reconf_ind(struct avdtp *session, struct avdtp_local_sep *sep,
1379 uint8_t *err, void *user_data)
1381 struct a2dp_sep *a2dp_sep = user_data;
1383 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
1384 DBG("Sink %p: ReConfigure_Ind", sep);
1386 DBG("Source %p: ReConfigure_Ind", sep);
1391 static gboolean endpoint_delayreport_ind(struct avdtp *session,
1392 struct avdtp_local_sep *sep,
1393 uint8_t rseid, uint16_t delay,
1394 uint8_t *err, void *user_data)
1396 struct a2dp_sep *a2dp_sep = user_data;
1398 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
1399 DBG("Sink %p: DelayReport_Ind", sep);
1401 DBG("Source %p: DelayReport_Ind", sep);
1403 if (a2dp_sep->endpoint == NULL ||
1404 a2dp_sep->endpoint->set_delay == NULL)
1407 a2dp_sep->endpoint->set_delay(a2dp_sep, delay, a2dp_sep->user_data);
1412 static void reconf_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
1413 struct avdtp_stream *stream, struct avdtp_error *err,
1416 struct a2dp_sep *a2dp_sep = user_data;
1417 struct a2dp_setup *setup;
1419 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
1420 DBG("Sink %p: ReConfigure_Cfm", sep);
1422 DBG("Source %p: ReConfigure_Cfm", sep);
1424 setup = find_setup_by_session(session);
1429 setup->stream = NULL;
1433 finalize_config(setup);
1436 static void delay_report_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
1437 struct avdtp_stream *stream,
1438 struct avdtp_error *err, void *user_data)
1440 struct a2dp_sep *a2dp_sep = user_data;
1442 if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
1443 DBG("Sink %p: DelayReport_Cfm", sep);
1445 DBG("Source %p: DelayReport_Cfm", sep);
1448 static struct avdtp_sep_cfm cfm = {
1449 .set_configuration = setconf_cfm,
1450 .get_configuration = getconf_cfm,
1453 .suspend = suspend_cfm,
1456 .reconfigure = reconf_cfm,
1457 .delay_report = delay_report_cfm,
1460 static struct avdtp_sep_ind endpoint_ind = {
1461 .match_codec = endpoint_match_codec_ind,
1462 .get_capability = endpoint_getcap_ind,
1463 .set_configuration = endpoint_setconf_ind,
1464 .get_configuration = getconf_ind,
1467 .suspend = suspend_ind,
1470 .reconfigure = reconf_ind,
1471 .delayreport = endpoint_delayreport_ind,
1474 #ifdef TIZEN_FEATURE_BLUEZ_MODIFY
1475 static sdp_record_t *a2dp_record(uint8_t type, gboolean sink_enabled)
1477 static sdp_record_t *a2dp_record(uint8_t type)
1480 sdp_list_t *svclass_id, *pfseq, *apseq, *root;
1481 uuid_t root_uuid, l2cap_uuid, avdtp_uuid, a2dp_uuid;
1482 sdp_profile_desc_t profile[1];
1483 sdp_list_t *aproto, *proto[2];
1484 sdp_record_t *record;
1485 sdp_data_t *psm, *version, *features;
1486 uint16_t lp = AVDTP_UUID;
1487 #ifdef TIZEN_FEATURE_BLUEZ_MODIFY
1488 uint16_t a2dp_ver, avdtp_ver, feat;
1490 DBG("A2DP record for Sink role");
1495 DBG("A2DP record for Source role");
1501 uint16_t a2dp_ver = 0x0103, avdtp_ver = 0x0103, feat = 0x000f;
1504 record = sdp_record_alloc();
1508 sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
1509 root = sdp_list_append(0, &root_uuid);
1510 sdp_set_browse_groups(record, root);
1512 if (type == AVDTP_SEP_TYPE_SOURCE)
1513 sdp_uuid16_create(&a2dp_uuid, AUDIO_SOURCE_SVCLASS_ID);
1515 sdp_uuid16_create(&a2dp_uuid, AUDIO_SINK_SVCLASS_ID);
1516 svclass_id = sdp_list_append(0, &a2dp_uuid);
1517 sdp_set_service_classes(record, svclass_id);
1519 sdp_uuid16_create(&profile[0].uuid, ADVANCED_AUDIO_PROFILE_ID);
1520 profile[0].version = a2dp_ver;
1521 pfseq = sdp_list_append(0, &profile[0]);
1522 sdp_set_profile_descs(record, pfseq);
1524 sdp_uuid16_create(&l2cap_uuid, L2CAP_UUID);
1525 proto[0] = sdp_list_append(0, &l2cap_uuid);
1526 psm = sdp_data_alloc(SDP_UINT16, &lp);
1527 proto[0] = sdp_list_append(proto[0], psm);
1528 apseq = sdp_list_append(0, proto[0]);
1530 sdp_uuid16_create(&avdtp_uuid, AVDTP_UUID);
1531 proto[1] = sdp_list_append(0, &avdtp_uuid);
1532 version = sdp_data_alloc(SDP_UINT16, &avdtp_ver);
1533 proto[1] = sdp_list_append(proto[1], version);
1534 apseq = sdp_list_append(apseq, proto[1]);
1536 aproto = sdp_list_append(0, apseq);
1537 sdp_set_access_protos(record, aproto);
1539 features = sdp_data_alloc(SDP_UINT16, &feat);
1540 sdp_attr_add(record, SDP_ATTR_SUPPORTED_FEATURES, features);
1542 if (type == AVDTP_SEP_TYPE_SOURCE)
1543 sdp_set_info_attr(record, "Audio Source", 0, 0);
1545 sdp_set_info_attr(record, "Audio Sink", 0, 0);
1549 sdp_list_free(proto[0], 0);
1550 sdp_list_free(proto[1], 0);
1551 sdp_list_free(apseq, 0);
1552 sdp_list_free(pfseq, 0);
1553 sdp_list_free(aproto, 0);
1554 sdp_list_free(root, 0);
1555 sdp_list_free(svclass_id, 0);
1560 static struct a2dp_server *find_server(GSList *list, struct btd_adapter *a)
1563 for (; list; list = list->next) {
1564 struct a2dp_server *server = list->data;
1566 if (server->adapter == a)
1573 static void remote_sep_free(void *data)
1575 struct a2dp_remote_sep *sep = data;
1581 static void remove_remote_sep(void *data)
1583 struct a2dp_remote_sep *sep = data;
1586 remote_sep_free(sep);
1590 g_dbus_unregister_interface(btd_get_dbus_connection(), sep->path,
1591 MEDIA_ENDPOINT_INTERFACE);
1594 static void channel_free(void *data)
1596 struct a2dp_channel *chan = data;
1598 if (chan->auth_id > 0)
1599 btd_cancel_authorization(chan->auth_id);
1601 if (chan->io_id > 0)
1602 g_source_remove(chan->io_id);
1605 g_io_channel_shutdown(chan->io, TRUE, NULL);
1606 g_io_channel_unref(chan->io);
1609 avdtp_remove_state_cb(chan->state_id);
1611 queue_destroy(chan->seps, remove_remote_sep);
1612 free(chan->last_used);
1616 static void channel_remove(struct a2dp_channel *chan)
1618 struct a2dp_server *server = chan->server;
1620 DBG("chan %p", chan);
1622 queue_remove(server->channels, chan);
1627 static gboolean disconnect_cb(GIOChannel *io, GIOCondition cond, gpointer data)
1629 struct a2dp_channel *chan = data;
1631 DBG("chan %p", chan);
1635 channel_remove(chan);
1640 static void caps_add_codec(GSList **l, uint8_t codec, uint8_t *caps,
1643 struct avdtp_service_capability *media_transport, *media_codec;
1644 struct avdtp_media_codec_capability *cap;
1646 media_transport = avdtp_service_cap_new(AVDTP_MEDIA_TRANSPORT,
1649 *l = g_slist_append(*l, media_transport);
1651 cap = g_malloc0(sizeof(*cap) + size);
1652 cap->media_type = AVDTP_MEDIA_TYPE_AUDIO;
1653 cap->media_codec_type = codec;
1654 memcpy(cap->data, caps, size);
1656 media_codec = avdtp_service_cap_new(AVDTP_MEDIA_CODEC, cap,
1657 sizeof(*cap) + size);
1659 *l = g_slist_append(*l, media_codec);
1668 static int match_client(const void *data, const void *user_data)
1670 struct a2dp_sep *sep = (void *) data;
1671 const struct a2dp_endpoint *endpoint = sep->endpoint;
1672 const struct client *client = user_data;
1674 if (strcmp(client->sender, endpoint->get_name(sep, sep->user_data)))
1677 return strcmp(client->path, endpoint->get_path(sep, sep->user_data));
1680 static struct a2dp_sep *find_sep(struct a2dp_server *server, uint8_t type,
1681 const char *sender, const char *path)
1684 struct client client = { sender, path };
1686 l = type == AVDTP_SEP_TYPE_SINK ? server->sources : server->sinks;
1688 l = g_slist_find_custom(l, &client, match_client);
1695 static int parse_properties(DBusMessageIter *props, uint8_t **caps, int *size)
1697 while (dbus_message_iter_get_arg_type(props) == DBUS_TYPE_DICT_ENTRY) {
1699 DBusMessageIter value, entry;
1702 dbus_message_iter_recurse(props, &entry);
1703 dbus_message_iter_get_basic(&entry, &key);
1705 dbus_message_iter_next(&entry);
1706 dbus_message_iter_recurse(&entry, &value);
1708 var = dbus_message_iter_get_arg_type(&value);
1709 if (strcasecmp(key, "Capabilities") == 0) {
1710 DBusMessageIter array;
1712 if (var != DBUS_TYPE_ARRAY)
1715 dbus_message_iter_recurse(&value, &array);
1716 dbus_message_iter_get_fixed_array(&array, caps, size);
1720 dbus_message_iter_next(props);
1726 static void reconfig_cb(struct avdtp *session, struct a2dp_sep *sep,
1727 struct avdtp_stream *stream, int err, void *user_data)
1729 DBusMessage *msg = user_data;
1732 g_dbus_send_message(btd_get_dbus_connection(),
1733 btd_error_failed(msg, strerror(-err)));
1735 g_dbus_send_reply(btd_get_dbus_connection(), msg,
1738 dbus_message_unref(msg);
1741 static int a2dp_reconfig(struct a2dp_channel *chan, const char *sender,
1742 struct a2dp_sep *lsep, struct a2dp_remote_sep *rsep,
1743 uint8_t *caps, int size, void *user_data)
1745 struct a2dp_setup *setup;
1746 struct a2dp_setup_cb *cb_data;
1750 setup = a2dp_setup_get(chan->session);
1754 cb_data = setup_cb_new(setup);
1755 cb_data->config_cb = reconfig_cb;
1756 cb_data->user_data = user_data;
1761 g_slist_free_full(setup->caps, g_free);
1764 caps_add_codec(&setup->caps, setup->sep->codec, caps, size);
1766 l = avdtp_get_type(rsep->sep) == AVDTP_SEP_TYPE_SINK ?
1767 chan->server->sources :
1768 chan->server->sinks;
1770 /* Check for existing stream and close it */
1771 for (; l; l = g_slist_next(l)) {
1772 struct a2dp_sep *tmp = l->data;
1774 /* Attempt to reconfigure if a stream already exists */
1776 /* Only allow switching sep from the same sender */
1777 if (strcmp(sender, tmp->endpoint->get_name(tmp,
1781 /* Check if stream is for the channel */
1782 if (!avdtp_has_stream(chan->session, tmp->stream))
1785 err = avdtp_close(chan->session, tmp->stream, FALSE);
1787 err = avdtp_abort(chan->session, tmp->stream);
1789 error("avdtp_abort: %s",
1795 setup->reconfigure = TRUE;
1801 err = avdtp_set_configuration(setup->session, setup->rsep->sep,
1806 error("avdtp_set_configuration: %s", strerror(-err));
1813 setup_cb_free(cb_data);
1817 static DBusMessage *set_configuration(DBusConnection *conn, DBusMessage *msg,
1820 struct a2dp_remote_sep *rsep = data;
1821 struct a2dp_channel *chan = rsep->chan;
1822 struct a2dp_sep *lsep = NULL;
1823 struct avdtp_service_capability *service;
1824 struct avdtp_media_codec_capability *codec;
1825 DBusMessageIter args, props;
1826 const char *sender, *path;
1830 sender = dbus_message_get_sender(msg);
1832 dbus_message_iter_init(msg, &args);
1834 dbus_message_iter_get_basic(&args, &path);
1835 dbus_message_iter_next(&args);
1837 lsep = find_sep(chan->server, avdtp_get_type(rsep->sep), sender, path);
1839 return btd_error_invalid_args(msg);
1841 service = avdtp_get_codec(rsep->sep);
1842 codec = (struct avdtp_media_codec_capability *) service->data;
1844 /* Check if codec really matches */
1845 if (!endpoint_match_codec_ind(chan->session, codec, lsep))
1846 return btd_error_invalid_args(msg);
1848 dbus_message_iter_recurse(&args, &props);
1849 if (dbus_message_iter_get_arg_type(&props) != DBUS_TYPE_DICT_ENTRY)
1850 return btd_error_invalid_args(msg);
1852 if (parse_properties(&props, &caps, &size) < 0)
1853 return btd_error_invalid_args(msg);
1855 err = a2dp_reconfig(chan, sender, lsep, rsep, caps, size,
1856 dbus_message_ref(msg));
1858 dbus_message_unref(msg);
1859 return btd_error_failed(msg, strerror(-err));
1865 static const GDBusMethodTable sep_methods[] = {
1866 { GDBUS_ASYNC_METHOD("SetConfiguration",
1867 GDBUS_ARGS({ "endpoint", "o" },
1868 { "properties", "a{sv}" } ),
1869 NULL, set_configuration) },
1873 static gboolean get_uuid(const GDBusPropertyTable *property,
1874 DBusMessageIter *iter, void *data)
1876 struct a2dp_remote_sep *sep = data;
1879 switch (avdtp_get_type(sep->sep)) {
1880 case AVDTP_SEP_TYPE_SOURCE:
1881 uuid = A2DP_SOURCE_UUID;
1883 case AVDTP_SEP_TYPE_SINK:
1884 uuid = A2DP_SINK_UUID;
1890 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &uuid);
1895 static gboolean get_codec(const GDBusPropertyTable *property,
1896 DBusMessageIter *iter, void *data)
1898 struct a2dp_remote_sep *sep = data;
1899 struct avdtp_service_capability *cap = avdtp_get_codec(sep->sep);
1900 struct avdtp_media_codec_capability *codec = (void *) cap->data;
1902 dbus_message_iter_append_basic(iter, DBUS_TYPE_BYTE,
1903 &codec->media_codec_type);
1908 static gboolean get_capabilities(const GDBusPropertyTable *property,
1909 DBusMessageIter *iter, void *data)
1911 struct a2dp_remote_sep *sep = data;
1912 struct avdtp_service_capability *service = avdtp_get_codec(sep->sep);
1913 struct avdtp_media_codec_capability *codec = (void *) service->data;
1914 uint8_t *caps = codec->data;
1915 DBusMessageIter array;
1917 dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
1918 DBUS_TYPE_BYTE_AS_STRING, &array);
1920 dbus_message_iter_append_fixed_array(&array, DBUS_TYPE_BYTE, &caps,
1921 service->length - sizeof(*codec));
1923 dbus_message_iter_close_container(iter, &array);
1928 static gboolean get_device(const GDBusPropertyTable *property,
1929 DBusMessageIter *iter, void *data)
1931 struct a2dp_remote_sep *sep = data;
1934 path = device_get_path(sep->chan->device);
1936 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &path);
1941 static gboolean get_delay_reporting(const GDBusPropertyTable *property,
1942 DBusMessageIter *iter, void *data)
1944 struct a2dp_remote_sep *sep = data;
1945 dbus_bool_t delay_report;
1947 delay_report = avdtp_get_delay_reporting(sep->sep);
1949 dbus_message_iter_append_basic(iter, DBUS_TYPE_BOOLEAN, &delay_report);
1954 static const GDBusPropertyTable sep_properties[] = {
1955 { "UUID", "s", get_uuid, NULL, NULL },
1956 { "Codec", "y", get_codec, NULL, NULL },
1957 { "Capabilities", "ay", get_capabilities, NULL, NULL },
1958 { "Device", "o", get_device, NULL, NULL },
1959 { "DelayReporting", "b", get_delay_reporting, NULL, NULL },
1963 static void register_remote_sep(void *data, void *user_data)
1965 struct avdtp_remote_sep *rsep = data;
1966 struct a2dp_channel *chan = user_data;
1967 struct a2dp_remote_sep *sep;
1969 sep = queue_find(chan->seps, match_remote_sep, rsep);
1973 sep = new0(struct a2dp_remote_sep, 1);
1977 if (asprintf(&sep->path, "%s/sep%d",
1978 device_get_path(chan->device),
1979 avdtp_get_seid(rsep)) < 0) {
1980 error("Could not allocate path for remote sep %s/sep%d",
1981 device_get_path(chan->device),
1982 avdtp_get_seid(rsep));
1987 if (g_dbus_register_interface(btd_get_dbus_connection(),
1988 sep->path, MEDIA_ENDPOINT_INTERFACE,
1989 sep_methods, NULL, sep_properties,
1990 sep, remote_sep_free) == FALSE) {
1991 error("Could not register remote sep %s", sep->path);
1997 DBG("Found remote SEP: %s", sep->path);
2000 queue_push_tail(chan->seps, sep);
2003 static bool match_seid(const void *data, const void *user_data)
2005 const struct a2dp_remote_sep *sep = data;
2006 const uint8_t *seid = user_data;
2008 return avdtp_get_seid(sep->sep) == *seid;
2011 static int match_sep(const void *data, const void *user_data)
2013 struct a2dp_sep *sep = (void *) data;
2014 const uint8_t *seid = user_data;
2016 return *seid - avdtp_sep_get_seid(sep->lsep);
2019 static struct a2dp_sep *find_sep_by_seid(struct a2dp_server *server,
2024 l = g_slist_find_custom(server->sources, &seid, match_sep);
2028 l = g_slist_find_custom(server->sinks, &seid, match_sep);
2035 static void load_remote_sep(struct a2dp_channel *chan, GKeyFile *key_file,
2038 struct a2dp_sep *lsep;
2039 struct a2dp_remote_sep *sep;
2040 struct avdtp_remote_sep *rsep;
2041 uint8_t lseid, rseid;
2047 for (; *seids; seids++) {
2050 uint8_t delay_reporting;
2056 if (sscanf(*seids, "%02hhx", &rseid) != 1)
2059 value = g_key_file_get_string(key_file, "Endpoints", *seids,
2064 /* Try loading with delay_reporting first */
2065 if (sscanf(value, "%02hhx:%02hhx:%02hhx:%s", &type, &codec,
2066 &delay_reporting, caps) != 4) {
2067 /* Try old format */
2068 if (sscanf(value, "%02hhx:%02hhx:%s", &type, &codec,
2070 warn("Unable to load Endpoint: seid %u", rseid);
2074 delay_reporting = false;
2077 for (i = 0, size = strlen(caps); i < size; i += 2) {
2078 uint8_t *tmp = data + i / 2;
2080 if (sscanf(caps + i, "%02hhx", tmp) != 1) {
2081 warn("Unable to load Endpoint: seid %u", rseid);
2091 caps_add_codec(&l, codec, data, size / 2);
2093 rsep = avdtp_register_remote_sep(chan->session, rseid, type, l,
2096 warn("Unable to register Endpoint: seid %u", rseid);
2100 register_remote_sep(rsep, chan);
2103 value = g_key_file_get_string(key_file, "Endpoints", "LastUsed", NULL);
2107 if (sscanf(value, "%02hhx:%02hhx", &lseid, &rseid) != 2) {
2108 warn("Unable to load LastUsed");
2115 lsep = find_sep_by_seid(chan->server, lseid);
2117 warn("Unable to load LastUsed: lseid %u not found", lseid);
2121 sep = queue_find(chan->seps, match_seid, &rseid);
2123 warn("Unable to load LastUsed: rseid %u not found", rseid);
2127 add_last_used(chan, lsep, sep);
2130 static void load_remote_seps(struct a2dp_channel *chan)
2132 struct btd_device *device = chan->device;
2133 char filename[PATH_MAX];
2138 ba2str(device_get_address(device), dst_addr);
2140 snprintf(filename, PATH_MAX, STORAGEDIR "/%s/cache/%s",
2141 btd_adapter_get_storage_dir(device_get_adapter(device)),
2143 key_file = g_key_file_new();
2144 g_key_file_load_from_file(key_file, filename, 0, NULL);
2145 keys = g_key_file_get_keys(key_file, "Endpoints", NULL, NULL);
2147 load_remote_sep(chan, key_file, keys);
2150 g_key_file_free(key_file);
2153 static void avdtp_state_cb(struct btd_device *dev, struct avdtp *session,
2154 avdtp_session_state_t old_state,
2155 avdtp_session_state_t new_state,
2158 struct a2dp_channel *chan = user_data;
2160 switch (new_state) {
2161 case AVDTP_SESSION_STATE_DISCONNECTED:
2162 if (chan->session == session)
2163 channel_remove(chan);
2165 case AVDTP_SESSION_STATE_CONNECTING:
2167 case AVDTP_SESSION_STATE_CONNECTED:
2169 chan->session = session;
2170 load_remote_seps(chan);
2175 static struct a2dp_channel *channel_new(struct a2dp_server *server,
2176 struct btd_device *device,
2179 struct a2dp_channel *chan;
2181 chan = g_new0(struct a2dp_channel, 1);
2182 chan->server = server;
2183 chan->device = device;
2184 chan->seps = queue_new();
2185 chan->state_id = avdtp_add_state_cb(device, avdtp_state_cb, chan);
2187 if (!queue_push_tail(server->channels, chan)) {
2195 chan->io = g_io_channel_ref(io);
2196 chan->io_id = g_io_add_watch(io, G_IO_ERR | G_IO_HUP | G_IO_NVAL,
2197 (GIOFunc) disconnect_cb, chan);
2202 static bool match_by_device(const void *data, const void *user_data)
2204 const struct a2dp_channel *chan = data;
2205 const struct btd_device *device = user_data;
2207 return chan->device == device;
2210 struct avdtp *a2dp_avdtp_get(struct btd_device *device)
2212 struct a2dp_server *server;
2213 struct a2dp_channel *chan;
2214 const struct queue_entry *entry;
2216 server = find_server(servers, device_get_adapter(device));
2220 chan = queue_find(server->channels, match_by_device, device);
2222 chan = channel_new(server, device, NULL);
2227 #ifdef TIZEN_FEATURE_BLUEZ_MODIFY
2228 if (chan->auth_id) {
2229 DBG("auth is already going...");
2235 return avdtp_ref(chan->session);
2237 /* Check if there is any SEP available */
2238 for (entry = queue_get_entries(server->seps); entry;
2239 entry = entry->next) {
2240 struct avdtp_local_sep *sep = entry->data;
2242 if (avdtp_sep_get_state(sep) == AVDTP_STATE_IDLE)
2246 DBG("Unable to find any available SEP");
2251 chan->session = avdtp_new(chan->io, device, server->seps);
2252 if (!chan->session) {
2253 channel_remove(chan);
2257 return avdtp_ref(chan->session);
2260 static void connect_cb(GIOChannel *io, GError *err, gpointer user_data)
2262 struct a2dp_channel *chan = user_data;
2265 error("%s", err->message);
2269 if (!chan->session) {
2270 chan->session = avdtp_new(chan->io, chan->device,
2271 chan->server->seps);
2272 if (!chan->session) {
2273 error("Unable to create AVDTP session");
2278 g_io_channel_unref(chan->io);
2281 g_source_remove(chan->io_id);
2287 channel_remove(chan);
2290 static void auth_cb(DBusError *derr, void *user_data)
2292 struct a2dp_channel *chan = user_data;
2297 if (derr && dbus_error_is_set(derr)) {
2298 error("Access denied: %s", derr->message);
2302 if (!bt_io_accept(chan->io, connect_cb, chan, NULL, &err)) {
2303 error("bt_io_accept: %s", err->message);
2311 channel_remove(chan);
2314 static void transport_cb(GIOChannel *io, GError *err, gpointer user_data)
2316 struct a2dp_setup *setup = user_data;
2317 uint16_t omtu, imtu;
2319 if (!g_slist_find(setups, setup)) {
2320 warn("bt_io_accept: setup %p no longer valid", setup);
2321 g_io_channel_shutdown(io, TRUE, NULL);
2326 error("%s", err->message);
2330 bt_io_get(io, &err, BT_IO_OPT_OMTU, &omtu, BT_IO_OPT_IMTU, &imtu,
2333 error("%s", err->message);
2338 if (!avdtp_stream_set_transport(setup->stream,
2339 g_io_channel_unix_get_fd(io),
2343 g_io_channel_set_close_on_unref(io, FALSE);
2345 g_io_channel_unref(setup->io);
2354 g_io_channel_shutdown(io, TRUE, NULL);
2357 static void confirm_cb(GIOChannel *io, gpointer data)
2359 struct a2dp_server *server = data;
2360 struct a2dp_channel *chan;
2364 struct btd_device *device;
2367 BT_IO_OPT_SOURCE_BDADDR, &src,
2368 BT_IO_OPT_DEST_BDADDR, &dst,
2369 BT_IO_OPT_DEST, address,
2372 error("%s", err->message);
2377 DBG("AVDTP: incoming connect from %s", address);
2379 device = btd_adapter_find_device(adapter_find(&src), &dst,
2384 #ifdef TIZEN_FEATURE_BLUEZ_MODIFY
2385 if (device_is_profile_blocked(device, A2DP_SINK_UUID)) {
2386 DBG("A2DP is restricted");
2391 chan = queue_find(server->channels, match_by_device, device);
2393 struct a2dp_setup *setup;
2395 setup = find_setup_by_session(chan->session);
2396 if (!setup || !setup->stream)
2400 error("transport channel already exists");
2404 if (!bt_io_accept(io, transport_cb, setup, NULL, &err)) {
2405 error("bt_io_accept: %s", err->message);
2411 * Reference the channel so it can be shutdown properly
2412 * stopping bt_io_accept from calling the callback with invalid
2415 setup->io = g_io_channel_ref(io);
2420 chan = channel_new(server, device, io);
2424 chan->auth_id = btd_request_authorization(&src, &dst,
2425 ADVANCED_AUDIO_UUID,
2427 if (chan->auth_id == 0 && !chan->session)
2428 channel_remove(chan);
2433 g_io_channel_shutdown(io, TRUE, NULL);
2436 static bool a2dp_server_listen(struct a2dp_server *server)
2444 if (main_opts.mps == MPS_OFF)
2445 mode = BT_IO_MODE_BASIC;
2447 mode = BT_IO_MODE_STREAMING;
2449 #if defined(TIZEN_FEATURE_BLUEZ_MODIFY)
2450 if (btd_adapter_get_a2dp_role(server->adapter) == BLUETOOTH_A2DP_SINK_ROLE) {
2451 server->io = bt_io_listen(NULL, confirm_cb, server, NULL, &err,
2452 BT_IO_OPT_SOURCE_BDADDR,
2453 btd_adapter_get_address(server->adapter),
2454 BT_IO_OPT_PSM, AVDTP_PSM,
2455 BT_IO_OPT_MODE, mode,
2456 BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_MEDIUM,
2457 BT_IO_OPT_IMTU, 895,
2458 BT_IO_OPT_MASTER, true,
2461 server->io = bt_io_listen(NULL, confirm_cb, server, NULL, &err,
2462 BT_IO_OPT_SOURCE_BDADDR,
2463 btd_adapter_get_address(server->adapter),
2464 BT_IO_OPT_PSM, AVDTP_PSM,
2465 BT_IO_OPT_MODE, mode,
2466 BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_MEDIUM,
2467 BT_IO_OPT_MASTER, true,
2471 server->io = bt_io_listen(NULL, confirm_cb, server, NULL, &err,
2472 BT_IO_OPT_SOURCE_BDADDR,
2473 btd_adapter_get_address(server->adapter),
2474 BT_IO_OPT_PSM, AVDTP_PSM,
2475 BT_IO_OPT_MODE, mode,
2476 BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_MEDIUM,
2477 BT_IO_OPT_MASTER, true,
2483 error("%s", err->message);
2489 static struct a2dp_server *a2dp_server_register(struct btd_adapter *adapter)
2491 struct a2dp_server *server;
2493 server = g_new0(struct a2dp_server, 1);
2495 server->adapter = btd_adapter_ref(adapter);
2496 server->seps = queue_new();
2497 server->channels = queue_new();
2499 servers = g_slist_append(servers, server);
2504 static void a2dp_unregister_sep(struct a2dp_sep *sep)
2506 struct a2dp_server *server = sep->server;
2509 sep->destroy(sep->user_data);
2510 sep->endpoint = NULL;
2513 avdtp_unregister_sep(server->seps, sep->lsep);
2517 if (!queue_isempty(server->seps))
2521 g_io_channel_shutdown(server->io, TRUE, NULL);
2522 g_io_channel_unref(server->io);
2527 static void a2dp_server_unregister(struct a2dp_server *server)
2529 servers = g_slist_remove(servers, server);
2530 queue_destroy(server->channels, channel_free);
2531 queue_destroy(server->seps, NULL);
2534 g_io_channel_shutdown(server->io, TRUE, NULL);
2535 g_io_channel_unref(server->io);
2538 btd_adapter_unref(server->adapter);
2542 struct a2dp_sep *a2dp_add_sep(struct btd_adapter *adapter, uint8_t type,
2543 uint8_t codec, gboolean delay_reporting,
2544 struct a2dp_endpoint *endpoint,
2545 void *user_data, GDestroyNotify destroy,
2548 struct a2dp_server *server;
2549 struct a2dp_sep *sep;
2551 uint32_t *record_id;
2552 sdp_record_t *record;
2554 server = find_server(servers, adapter);
2555 if (server == NULL) {
2557 *err = -EPROTONOSUPPORT;
2561 if (type == AVDTP_SEP_TYPE_SINK && !server->sink_enabled) {
2563 *err = -EPROTONOSUPPORT;
2567 if (type == AVDTP_SEP_TYPE_SOURCE && !server->source_enabled) {
2569 *err = -EPROTONOSUPPORT;
2573 sep = g_new0(struct a2dp_sep, 1);
2575 sep->lsep = avdtp_register_sep(server->seps, type,
2576 AVDTP_MEDIA_TYPE_AUDIO, codec,
2577 delay_reporting, &endpoint_ind,
2580 if (sep->lsep == NULL) {
2587 sep->server = server;
2588 sep->endpoint = endpoint;
2591 sep->delay_reporting = delay_reporting;
2592 sep->user_data = user_data;
2593 sep->destroy = destroy;
2595 if (type == AVDTP_SEP_TYPE_SOURCE) {
2596 l = &server->sources;
2597 record_id = &server->source_record_id;
2600 record_id = &server->sink_record_id;
2603 if (*record_id != 0)
2606 #ifdef TIZEN_FEATURE_BLUEZ_MODIFY
2607 if (btd_adapter_get_a2dp_role(adapter) == BLUETOOTH_A2DP_SINK_ROLE)
2608 record = a2dp_record(type, true);
2610 record = a2dp_record(type, false);
2612 record = a2dp_record(type);
2615 error("Unable to allocate new service record");
2616 a2dp_unregister_sep(sep);
2622 if (adapter_service_add(server->adapter, record) < 0) {
2623 error("Unable to register A2DP service record");
2624 sdp_record_free(record);
2625 a2dp_unregister_sep(sep);
2631 if (!a2dp_server_listen(server)) {
2632 sdp_record_free(record);
2633 a2dp_unregister_sep(sep);
2639 *record_id = record->handle;
2642 *l = g_slist_append(*l, sep);
2649 void a2dp_remove_sep(struct a2dp_sep *sep)
2651 struct a2dp_server *server = sep->server;
2653 if (sep->type == AVDTP_SEP_TYPE_SOURCE) {
2654 if (g_slist_find(server->sources, sep) == NULL)
2656 server->sources = g_slist_remove(server->sources, sep);
2657 if (server->sources == NULL && server->source_record_id) {
2658 adapter_service_remove(server->adapter,
2659 server->source_record_id);
2660 server->source_record_id = 0;
2663 if (g_slist_find(server->sinks, sep) == NULL)
2665 server->sinks = g_slist_remove(server->sinks, sep);
2666 if (server->sinks == NULL && server->sink_record_id) {
2667 adapter_service_remove(server->adapter,
2668 server->sink_record_id);
2669 server->sink_record_id = 0;
2676 a2dp_unregister_sep(sep);
2679 static void select_cb(struct a2dp_setup *setup, void *ret, int size)
2681 struct avdtp_service_capability *service;
2682 struct avdtp_media_codec_capability *codec;
2686 caps_add_codec(&setup->caps, setup->sep->codec, ret, size);
2690 setup->sep = queue_pop_head(setup->eps);
2692 error("Unable to select a valid configuration");
2696 setup->rsep = find_remote_sep(setup->chan, setup->sep);
2697 service = avdtp_get_codec(setup->rsep->sep);
2698 codec = (struct avdtp_media_codec_capability *) service->data;
2700 err = setup->sep->endpoint->select_configuration(setup->sep,
2702 service->length - sizeof(*codec),
2704 select_cb, setup->sep->user_data);
2709 finalize_select(setup);
2713 static struct queue *a2dp_find_eps(struct avdtp *session, GSList *list,
2716 #ifdef TIZEN_FEATURE_BLUEZ_MODIFY
2717 struct queue *selected_sep = NULL;
2720 struct a2dp_channel *chan = find_channel(session);
2721 struct queue *seps = NULL;
2723 for (; list; list = list->next) {
2724 struct a2dp_sep *sep = list->data;
2725 struct avdtp_remote_sep *rsep;
2726 #ifdef TIZEN_FEATURE_BLUEZ_MODIFY
2727 struct avdtp_media_codec_capability *cap;
2728 struct avdtp_service_capability *service;
2731 /* Use sender's endpoint if available */
2735 if (sep->endpoint == NULL)
2738 name = sep->endpoint->get_name(sep, sep->user_data);
2739 if (g_strcmp0(sender, name) != 0)
2743 #ifdef TIZEN_FEATURE_BLUEZ_MODIFY
2744 rsep = avdtp_find_remote_sep(session, sep->lsep);
2748 service = avdtp_get_codec(rsep);
2749 cap = (struct avdtp_media_codec_capability *) service->data;
2751 if (cap->media_codec_type != A2DP_CODEC_VENDOR) {
2753 selected_sep = queue_new();
2754 queue_push_head(selected_sep, sep);
2756 #ifdef TIZEN_FEATURE_SSC_ENCODER
2758 /* This is vendor codec */
2759 queue_push_head(selected_sep, sep);
2760 if (endpoint_match_codec_preference(session, cap, sep))
2761 return selected_sep;
2766 rsep = avdtp_find_remote_sep(session, sep->lsep);
2773 /* Prepend last used so it is preferred over others */
2774 if (chan->last_used && (chan->last_used->lsep == sep &&
2775 chan->last_used->rsep->sep == rsep))
2776 queue_push_head(seps, sep);
2778 queue_push_tail(seps, sep);
2781 #ifdef TIZEN_FEATURE_BLUEZ_MODIFY
2783 return selected_sep;
2791 static struct queue *a2dp_select_eps(struct avdtp *session, uint8_t type,
2794 struct a2dp_server *server;
2798 server = find_server(servers, avdtp_get_adapter(session));
2802 l = type == AVDTP_SEP_TYPE_SINK ? server->sources : server->sinks;
2804 /* Check sender's seps first */
2805 seps = a2dp_find_eps(session, l, sender);
2809 return a2dp_find_eps(session, l, NULL);
2812 static void store_remote_sep(void *data, void *user_data)
2814 struct a2dp_remote_sep *sep = data;
2815 GKeyFile *key_file = user_data;
2816 char seid[4], value[256];
2817 struct avdtp_service_capability *service = avdtp_get_codec(sep->sep);
2818 struct avdtp_media_codec_capability *codec = (void *) service->data;
2822 sprintf(seid, "%02hhx", avdtp_get_seid(sep->sep));
2824 offset = sprintf(value, "%02hhx:%02hhx:%02hhx:",
2825 avdtp_get_type(sep->sep), codec->media_codec_type,
2826 avdtp_get_delay_reporting(sep->sep));
2828 for (i = 0; i < service->length - sizeof(*codec); i++)
2829 offset += sprintf(value + offset, "%02hhx", codec->data[i]);
2831 g_key_file_set_string(key_file, "Endpoints", seid, value);
2834 static void store_remote_seps(struct a2dp_channel *chan)
2836 struct btd_device *device = chan->device;
2837 char filename[PATH_MAX];
2843 if (queue_isempty(chan->seps))
2846 ba2str(device_get_address(device), dst_addr);
2848 snprintf(filename, PATH_MAX, STORAGEDIR "/%s/cache/%s",
2849 btd_adapter_get_storage_dir(device_get_adapter(device)),
2851 key_file = g_key_file_new();
2852 g_key_file_load_from_file(key_file, filename, 0, NULL);
2854 data = g_key_file_get_string(key_file, "Endpoints", "LastUsed",
2857 /* Remove current endpoints since it might have changed */
2858 g_key_file_remove_group(key_file, "Endpoints", NULL);
2860 queue_foreach(chan->seps, store_remote_sep, key_file);
2863 g_key_file_set_string(key_file, "Endpoints", "LastUsed",
2868 data = g_key_file_to_data(key_file, &length, NULL);
2869 g_file_set_contents(filename, data, length, NULL);
2872 g_key_file_free(key_file);
2875 static void discover_cb(struct avdtp *session, GSList *seps,
2876 struct avdtp_error *err, void *user_data)
2878 struct a2dp_setup *setup = user_data;
2879 uint16_t version = avdtp_get_version(session);
2881 DBG("version 0x%04x err %p", version, err);
2883 #ifdef TIZEN_FEATURE_BLUEZ_MODIFY
2884 if (!g_slist_find(setups, setup)) {
2885 warn("setup %p no longer valid", setup);
2894 g_slist_foreach(seps, register_remote_sep, setup->chan);
2896 /* Only store version has been initialized as features like
2897 * Delay Reporting may not be queried if the version in
2901 store_remote_seps(setup->chan);
2904 finalize_discover(setup);
2907 unsigned int a2dp_discover(struct avdtp *session, a2dp_discover_cb_t cb,
2910 struct a2dp_setup *setup;
2911 struct a2dp_setup_cb *cb_data;
2913 setup = a2dp_setup_get(session);
2917 cb_data = setup_cb_new(setup);
2918 cb_data->discover_cb = cb;
2919 cb_data->user_data = user_data;
2921 if (avdtp_discover(session, discover_cb, setup) == 0)
2924 setup_cb_free(cb_data);
2928 unsigned int a2dp_select_capabilities(struct avdtp *session,
2929 uint8_t type, const char *sender,
2930 a2dp_select_cb_t cb,
2933 struct a2dp_setup *setup;
2934 struct a2dp_setup_cb *cb_data;
2936 struct avdtp_service_capability *service;
2937 struct avdtp_media_codec_capability *codec;
2940 eps = a2dp_select_eps(session, type, sender);
2942 error("Unable to select SEP");
2946 setup = a2dp_setup_get(session);
2950 cb_data = setup_cb_new(setup);
2951 cb_data->select_cb = cb;
2952 cb_data->user_data = user_data;
2955 setup->sep = queue_pop_head(eps);
2956 setup->rsep = find_remote_sep(setup->chan, setup->sep);
2958 if (setup->rsep == NULL) {
2959 error("Could not find remote sep");
2963 service = avdtp_get_codec(setup->rsep->sep);
2964 codec = (struct avdtp_media_codec_capability *) service->data;
2966 err = setup->sep->endpoint->select_configuration(setup->sep,
2972 setup->sep->user_data);
2979 setup_cb_free(cb_data);
2984 unsigned int a2dp_config(struct avdtp *session, struct a2dp_sep *sep,
2985 a2dp_config_cb_t cb, GSList *caps,
2988 struct a2dp_setup_cb *cb_data;
2990 struct a2dp_server *server;
2991 struct a2dp_setup *setup;
2992 struct a2dp_sep *tmp;
2993 struct avdtp_service_capability *cap;
2994 struct avdtp_media_codec_capability *codec_cap = NULL;
2997 server = find_server(servers, avdtp_get_adapter(session));
3001 for (l = caps; l != NULL; l = l->next) {
3004 if (cap->category != AVDTP_MEDIA_CODEC)
3007 codec_cap = (void *) cap->data;
3014 if (sep->codec != codec_cap->media_codec_type)
3017 DBG("a2dp_config: selected SEP %p", sep->lsep);
3019 setup = a2dp_setup_get(session);
3023 cb_data = setup_cb_new(setup);
3024 cb_data->config_cb = cb;
3025 cb_data->user_data = user_data;
3028 setup->stream = sep->stream;
3030 /* Copy given caps if they are different than current caps */
3031 if (setup->caps != caps) {
3032 g_slist_free_full(setup->caps, g_free);
3033 setup->caps = g_slist_copy(caps);
3036 switch (avdtp_sep_get_state(sep->lsep)) {
3037 case AVDTP_STATE_IDLE:
3038 if (sep->type == AVDTP_SEP_TYPE_SOURCE)
3039 l = server->sources;
3043 for (; l != NULL; l = l->next) {
3045 if (avdtp_has_stream(session, tmp->stream))
3052 setup->reconfigure = TRUE;
3053 if (avdtp_close(session, tmp->stream, FALSE) < 0) {
3054 error("avdtp_close failed");
3060 setup->rsep = find_remote_sep(setup->chan, sep);
3061 if (setup->rsep == NULL) {
3062 error("No matching ACP and INT SEPs found");
3066 posix_err = avdtp_set_configuration(session, setup->rsep->sep,
3069 if (posix_err < 0) {
3070 error("avdtp_set_configuration: %s",
3071 strerror(-posix_err));
3075 case AVDTP_STATE_OPEN:
3076 case AVDTP_STATE_STREAMING:
3077 if (avdtp_stream_has_capabilities(setup->stream, caps)) {
3078 DBG("Configuration match: resuming");
3079 cb_data->source_id = g_idle_add(finalize_config,
3081 } else if (!setup->reconfigure) {
3082 setup->reconfigure = TRUE;
3083 if (avdtp_close(session, sep->stream, FALSE) < 0) {
3084 error("avdtp_close failed");
3089 case AVDTP_STATE_CONFIGURED:
3090 case AVDTP_STATE_CLOSING:
3091 case AVDTP_STATE_ABORTING:
3093 error("SEP in bad state for requesting a new stream");
3100 setup_cb_free(cb_data);
3104 unsigned int a2dp_resume(struct avdtp *session, struct a2dp_sep *sep,
3105 a2dp_stream_cb_t cb, void *user_data)
3107 struct a2dp_setup_cb *cb_data;
3108 struct a2dp_setup *setup;
3110 setup = a2dp_setup_get(session);
3114 cb_data = setup_cb_new(setup);
3115 cb_data->resume_cb = cb;
3116 cb_data->user_data = user_data;
3119 setup->stream = sep->stream;
3121 switch (avdtp_sep_get_state(sep->lsep)) {
3122 case AVDTP_STATE_IDLE:
3125 case AVDTP_STATE_CONFIGURED:
3126 setup->start = TRUE;
3128 case AVDTP_STATE_OPEN:
3129 if (avdtp_start(session, sep->stream) < 0) {
3130 error("avdtp_start failed");
3133 sep->starting = TRUE;
3135 case AVDTP_STATE_STREAMING:
3136 if (!sep->suspending && sep->suspend_timer) {
3137 g_source_remove(sep->suspend_timer);
3138 sep->suspend_timer = 0;
3139 avdtp_unref(sep->session);
3140 sep->session = NULL;
3142 if (sep->suspending)
3143 setup->start = TRUE;
3145 cb_data->source_id = g_idle_add(finalize_resume,
3148 case AVDTP_STATE_CLOSING:
3149 case AVDTP_STATE_ABORTING:
3151 error("SEP in bad state for resume");
3158 setup_cb_free(cb_data);
3162 unsigned int a2dp_suspend(struct avdtp *session, struct a2dp_sep *sep,
3163 a2dp_stream_cb_t cb, void *user_data)
3165 struct a2dp_setup_cb *cb_data;
3166 struct a2dp_setup *setup;
3168 setup = a2dp_setup_get(session);
3172 cb_data = setup_cb_new(setup);
3173 cb_data->suspend_cb = cb;
3174 cb_data->user_data = user_data;
3177 setup->stream = sep->stream;
3179 switch (avdtp_sep_get_state(sep->lsep)) {
3180 case AVDTP_STATE_IDLE:
3181 error("a2dp_suspend: no stream to suspend");
3184 case AVDTP_STATE_OPEN:
3185 cb_data->source_id = g_idle_add(finalize_suspend, setup);
3187 case AVDTP_STATE_STREAMING:
3188 if (avdtp_suspend(session, sep->stream) < 0) {
3189 error("avdtp_suspend failed");
3192 sep->suspending = TRUE;
3194 case AVDTP_STATE_CONFIGURED:
3195 case AVDTP_STATE_CLOSING:
3196 case AVDTP_STATE_ABORTING:
3198 error("SEP in bad state for suspend");
3205 setup_cb_free(cb_data);
3209 gboolean a2dp_cancel(unsigned int id)
3213 for (ls = setups; ls != NULL; ls = g_slist_next(ls)) {
3214 struct a2dp_setup *setup = ls->data;
3216 for (l = setup->cb; l != NULL; l = g_slist_next(l)) {
3217 struct a2dp_setup_cb *cb = l->data;
3226 DBG("aborting setup %p", setup);
3227 if (!avdtp_abort(setup->session, setup->stream))
3239 gboolean a2dp_sep_lock(struct a2dp_sep *sep, struct avdtp *session)
3244 DBG("SEP %p locked", sep->lsep);
3250 gboolean a2dp_sep_unlock(struct a2dp_sep *sep, struct avdtp *session)
3252 struct a2dp_server *server = sep->server;
3253 avdtp_state_t state;
3256 state = avdtp_sep_get_state(sep->lsep);
3258 sep->locked = FALSE;
3260 DBG("SEP %p unlocked", sep->lsep);
3262 if (sep->type == AVDTP_SEP_TYPE_SOURCE)
3263 l = server->sources;
3267 /* Unregister sep if it was removed */
3268 if (g_slist_find(l, sep) == NULL) {
3269 a2dp_unregister_sep(sep);
3273 if (!sep->stream || state == AVDTP_STATE_IDLE)
3277 case AVDTP_STATE_OPEN:
3278 /* Set timer here */
3280 case AVDTP_STATE_STREAMING:
3281 if (avdtp_suspend(session, sep->stream) == 0)
3282 sep->suspending = TRUE;
3284 case AVDTP_STATE_IDLE:
3285 case AVDTP_STATE_CONFIGURED:
3286 case AVDTP_STATE_CLOSING:
3287 case AVDTP_STATE_ABORTING:
3295 struct avdtp_stream *a2dp_sep_get_stream(struct a2dp_sep *sep)
3300 struct btd_device *a2dp_setup_get_device(struct a2dp_setup *setup)
3302 if (setup->session == NULL)
3305 return avdtp_get_device(setup->session);
3308 const char *a2dp_setup_remote_path(struct a2dp_setup *setup)
3311 return setup->rsep->path;
3317 static int a2dp_source_probe(struct btd_service *service)
3319 struct btd_device *dev = btd_service_get_device(service);
3321 DBG("path %s", device_get_path(dev));
3323 source_init(service);
3328 static void a2dp_source_remove(struct btd_service *service)
3330 source_unregister(service);
3333 static int a2dp_sink_probe(struct btd_service *service)
3335 struct btd_device *dev = btd_service_get_device(service);
3337 DBG("path %s", device_get_path(dev));
3339 return sink_init(service);
3342 static void a2dp_sink_remove(struct btd_service *service)
3344 sink_unregister(service);
3347 static int a2dp_source_connect(struct btd_service *service)
3349 struct btd_device *dev = btd_service_get_device(service);
3350 struct btd_adapter *adapter = device_get_adapter(dev);
3351 struct a2dp_server *server;
3352 const char *path = device_get_path(dev);
3354 DBG("path %s", path);
3356 server = find_server(servers, adapter);
3357 if (!server || !server->sink_enabled) {
3358 DBG("Unexpected error: cannot find server");
3359 return -EPROTONOSUPPORT;
3362 /* Return protocol not available if no record/endpoint exists */
3363 if (server->sink_record_id == 0)
3364 return -ENOPROTOOPT;
3366 return source_connect(service);
3369 static int a2dp_source_disconnect(struct btd_service *service)
3371 struct btd_device *dev = btd_service_get_device(service);
3372 const char *path = device_get_path(dev);
3374 DBG("path %s", path);
3376 return source_disconnect(service);
3379 static int a2dp_sink_connect(struct btd_service *service)
3381 struct btd_device *dev = btd_service_get_device(service);
3382 struct btd_adapter *adapter = device_get_adapter(dev);
3383 struct a2dp_server *server;
3384 const char *path = device_get_path(dev);
3386 DBG("path %s", path);
3388 server = find_server(servers, adapter);
3389 if (!server || !server->source_enabled) {
3390 DBG("Unexpected error: cannot find server");
3391 return -EPROTONOSUPPORT;
3394 /* Return protocol not available if no record/endpoint exists */
3395 if (server->source_record_id == 0)
3396 return -ENOPROTOOPT;
3398 return sink_connect(service);
3401 static int a2dp_sink_disconnect(struct btd_service *service)
3403 struct btd_device *dev = btd_service_get_device(service);
3404 const char *path = device_get_path(dev);
3406 DBG("path %s", path);
3408 return sink_disconnect(service);
3411 static int a2dp_source_server_probe(struct btd_profile *p,
3412 struct btd_adapter *adapter)
3414 struct a2dp_server *server;
3416 DBG("path %s", adapter_get_path(adapter));
3418 server = find_server(servers, adapter);
3422 server = a2dp_server_register(adapter);
3424 return -EPROTONOSUPPORT;
3427 server->source_enabled = TRUE;
3432 static void a2dp_source_server_remove(struct btd_profile *p,
3433 struct btd_adapter *adapter)
3435 struct a2dp_server *server;
3437 DBG("path %s", adapter_get_path(adapter));
3439 server = find_server(servers, adapter);
3443 g_slist_free_full(server->sources,
3444 (GDestroyNotify) a2dp_unregister_sep);
3446 if (server->source_record_id) {
3447 adapter_service_remove(server->adapter,
3448 server->source_record_id);
3449 server->source_record_id = 0;
3452 if (server->sink_record_id)
3455 a2dp_server_unregister(server);
3458 static int a2dp_sink_server_probe(struct btd_profile *p,
3459 struct btd_adapter *adapter)
3461 struct a2dp_server *server;
3463 DBG("path %s", adapter_get_path(adapter));
3465 server = find_server(servers, adapter);
3469 server = a2dp_server_register(adapter);
3471 return -EPROTONOSUPPORT;
3474 server->sink_enabled = TRUE;
3479 static void a2dp_sink_server_remove(struct btd_profile *p,
3480 struct btd_adapter *adapter)
3482 struct a2dp_server *server;
3484 DBG("path %s", adapter_get_path(adapter));
3486 server = find_server(servers, adapter);
3490 g_slist_free_full(server->sinks, (GDestroyNotify) a2dp_unregister_sep);
3492 if (server->sink_record_id) {
3493 adapter_service_remove(server->adapter, server->sink_record_id);
3494 server->sink_record_id = 0;
3497 if (server->source_record_id)
3500 a2dp_server_unregister(server);
3503 static int media_server_probe(struct btd_adapter *adapter)
3505 DBG("path %s", adapter_get_path(adapter));
3507 return media_register(adapter);
3510 static void media_server_remove(struct btd_adapter *adapter)
3512 DBG("path %s", adapter_get_path(adapter));
3514 media_unregister(adapter);
3517 static struct btd_profile a2dp_source_profile = {
3518 .name = "a2dp-source",
3519 .priority = BTD_PROFILE_PRIORITY_MEDIUM,
3521 .remote_uuid = A2DP_SOURCE_UUID,
3522 .device_probe = a2dp_source_probe,
3523 .device_remove = a2dp_source_remove,
3525 .auto_connect = true,
3526 .connect = a2dp_source_connect,
3527 .disconnect = a2dp_source_disconnect,
3529 .adapter_probe = a2dp_sink_server_probe,
3530 .adapter_remove = a2dp_sink_server_remove,
3533 static struct btd_profile a2dp_sink_profile = {
3534 .name = "a2dp-sink",
3535 .priority = BTD_PROFILE_PRIORITY_MEDIUM,
3537 .remote_uuid = A2DP_SINK_UUID,
3538 .device_probe = a2dp_sink_probe,
3539 .device_remove = a2dp_sink_remove,
3541 .auto_connect = true,
3542 .connect = a2dp_sink_connect,
3543 .disconnect = a2dp_sink_disconnect,
3545 .adapter_probe = a2dp_source_server_probe,
3546 .adapter_remove = a2dp_source_server_remove,
3549 static struct btd_adapter_driver media_driver = {
3551 .probe = media_server_probe,
3552 .remove = media_server_remove,
3555 static int a2dp_init(void)
3557 btd_register_adapter_driver(&media_driver);
3558 btd_profile_register(&a2dp_source_profile);
3559 btd_profile_register(&a2dp_sink_profile);
3564 static void a2dp_exit(void)
3566 btd_unregister_adapter_driver(&media_driver);
3567 btd_profile_unregister(&a2dp_source_profile);
3568 btd_profile_unregister(&a2dp_sink_profile);
3571 BLUETOOTH_PLUGIN_DEFINE(a2dp, VERSION, BLUETOOTH_PLUGIN_PRIORITY_DEFAULT,
3572 a2dp_init, a2dp_exit)