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;
1577 avdtp_remote_sep_set_destroy(sep->sep, NULL, NULL);
1583 static void remove_remote_sep(void *data)
1585 struct a2dp_remote_sep *sep = data;
1588 remote_sep_free(sep);
1592 g_dbus_unregister_interface(btd_get_dbus_connection(), sep->path,
1593 MEDIA_ENDPOINT_INTERFACE);
1596 static void channel_free(void *data)
1598 struct a2dp_channel *chan = data;
1600 if (chan->auth_id > 0)
1601 btd_cancel_authorization(chan->auth_id);
1603 if (chan->io_id > 0)
1604 g_source_remove(chan->io_id);
1607 g_io_channel_shutdown(chan->io, TRUE, NULL);
1608 g_io_channel_unref(chan->io);
1611 avdtp_remove_state_cb(chan->state_id);
1613 queue_destroy(chan->seps, remove_remote_sep);
1614 free(chan->last_used);
1618 static void channel_remove(struct a2dp_channel *chan)
1620 struct a2dp_server *server = chan->server;
1622 DBG("chan %p", chan);
1624 queue_remove(server->channels, chan);
1629 static gboolean disconnect_cb(GIOChannel *io, GIOCondition cond, gpointer data)
1631 struct a2dp_channel *chan = data;
1633 DBG("chan %p", chan);
1637 channel_remove(chan);
1642 static void caps_add_codec(GSList **l, uint8_t codec, uint8_t *caps,
1645 struct avdtp_service_capability *media_transport, *media_codec;
1646 struct avdtp_media_codec_capability *cap;
1648 media_transport = avdtp_service_cap_new(AVDTP_MEDIA_TRANSPORT,
1651 *l = g_slist_append(*l, media_transport);
1653 cap = g_malloc0(sizeof(*cap) + size);
1654 cap->media_type = AVDTP_MEDIA_TYPE_AUDIO;
1655 cap->media_codec_type = codec;
1656 memcpy(cap->data, caps, size);
1658 media_codec = avdtp_service_cap_new(AVDTP_MEDIA_CODEC, cap,
1659 sizeof(*cap) + size);
1661 *l = g_slist_append(*l, media_codec);
1670 static int match_client(const void *data, const void *user_data)
1672 struct a2dp_sep *sep = (void *) data;
1673 const struct a2dp_endpoint *endpoint = sep->endpoint;
1674 const struct client *client = user_data;
1676 if (strcmp(client->sender, endpoint->get_name(sep, sep->user_data)))
1679 return strcmp(client->path, endpoint->get_path(sep, sep->user_data));
1682 static struct a2dp_sep *find_sep(struct a2dp_server *server, uint8_t type,
1683 const char *sender, const char *path)
1686 struct client client = { sender, path };
1688 l = type == AVDTP_SEP_TYPE_SINK ? server->sources : server->sinks;
1690 l = g_slist_find_custom(l, &client, match_client);
1697 static int parse_properties(DBusMessageIter *props, uint8_t **caps, int *size)
1699 while (dbus_message_iter_get_arg_type(props) == DBUS_TYPE_DICT_ENTRY) {
1701 DBusMessageIter value, entry;
1704 dbus_message_iter_recurse(props, &entry);
1705 dbus_message_iter_get_basic(&entry, &key);
1707 dbus_message_iter_next(&entry);
1708 dbus_message_iter_recurse(&entry, &value);
1710 var = dbus_message_iter_get_arg_type(&value);
1711 if (strcasecmp(key, "Capabilities") == 0) {
1712 DBusMessageIter array;
1714 if (var != DBUS_TYPE_ARRAY)
1717 dbus_message_iter_recurse(&value, &array);
1718 dbus_message_iter_get_fixed_array(&array, caps, size);
1722 dbus_message_iter_next(props);
1728 static void reconfig_cb(struct avdtp *session, struct a2dp_sep *sep,
1729 struct avdtp_stream *stream, int err, void *user_data)
1731 DBusMessage *msg = user_data;
1734 g_dbus_send_message(btd_get_dbus_connection(),
1735 btd_error_failed(msg, strerror(-err)));
1737 g_dbus_send_reply(btd_get_dbus_connection(), msg,
1740 dbus_message_unref(msg);
1743 static int a2dp_reconfig(struct a2dp_channel *chan, const char *sender,
1744 struct a2dp_sep *lsep, struct a2dp_remote_sep *rsep,
1745 uint8_t *caps, int size, void *user_data)
1747 struct a2dp_setup *setup;
1748 struct a2dp_setup_cb *cb_data;
1752 setup = a2dp_setup_get(chan->session);
1756 cb_data = setup_cb_new(setup);
1757 cb_data->config_cb = reconfig_cb;
1758 cb_data->user_data = user_data;
1763 g_slist_free_full(setup->caps, g_free);
1766 caps_add_codec(&setup->caps, setup->sep->codec, caps, size);
1768 l = avdtp_get_type(rsep->sep) == AVDTP_SEP_TYPE_SINK ?
1769 chan->server->sources :
1770 chan->server->sinks;
1772 /* Check for existing stream and close it */
1773 for (; l; l = g_slist_next(l)) {
1774 struct a2dp_sep *tmp = l->data;
1776 /* Attempt to reconfigure if a stream already exists */
1778 /* Only allow switching sep from the same sender */
1779 if (strcmp(sender, tmp->endpoint->get_name(tmp,
1783 /* Check if stream is for the channel */
1784 if (!avdtp_has_stream(chan->session, tmp->stream))
1787 err = avdtp_close(chan->session, tmp->stream, FALSE);
1789 err = avdtp_abort(chan->session, tmp->stream);
1791 error("avdtp_abort: %s",
1797 setup->reconfigure = TRUE;
1803 err = avdtp_set_configuration(setup->session, setup->rsep->sep,
1808 error("avdtp_set_configuration: %s", strerror(-err));
1815 setup_cb_free(cb_data);
1819 static DBusMessage *set_configuration(DBusConnection *conn, DBusMessage *msg,
1822 struct a2dp_remote_sep *rsep = data;
1823 struct a2dp_channel *chan = rsep->chan;
1824 struct a2dp_sep *lsep = NULL;
1825 struct avdtp_service_capability *service;
1826 struct avdtp_media_codec_capability *codec;
1827 DBusMessageIter args, props;
1828 const char *sender, *path;
1832 sender = dbus_message_get_sender(msg);
1834 dbus_message_iter_init(msg, &args);
1836 dbus_message_iter_get_basic(&args, &path);
1837 dbus_message_iter_next(&args);
1839 lsep = find_sep(chan->server, avdtp_get_type(rsep->sep), sender, path);
1841 return btd_error_invalid_args(msg);
1843 service = avdtp_get_codec(rsep->sep);
1844 codec = (struct avdtp_media_codec_capability *) service->data;
1846 /* Check if codec really matches */
1847 if (!endpoint_match_codec_ind(chan->session, codec, lsep))
1848 return btd_error_invalid_args(msg);
1850 dbus_message_iter_recurse(&args, &props);
1851 if (dbus_message_iter_get_arg_type(&props) != DBUS_TYPE_DICT_ENTRY)
1852 return btd_error_invalid_args(msg);
1854 if (parse_properties(&props, &caps, &size) < 0)
1855 return btd_error_invalid_args(msg);
1857 err = a2dp_reconfig(chan, sender, lsep, rsep, caps, size,
1858 dbus_message_ref(msg));
1860 dbus_message_unref(msg);
1861 return btd_error_failed(msg, strerror(-err));
1867 static const GDBusMethodTable sep_methods[] = {
1868 { GDBUS_ASYNC_METHOD("SetConfiguration",
1869 GDBUS_ARGS({ "endpoint", "o" },
1870 { "properties", "a{sv}" } ),
1871 NULL, set_configuration) },
1875 static gboolean get_uuid(const GDBusPropertyTable *property,
1876 DBusMessageIter *iter, void *data)
1878 struct a2dp_remote_sep *sep = data;
1881 switch (avdtp_get_type(sep->sep)) {
1882 case AVDTP_SEP_TYPE_SOURCE:
1883 uuid = A2DP_SOURCE_UUID;
1885 case AVDTP_SEP_TYPE_SINK:
1886 uuid = A2DP_SINK_UUID;
1892 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &uuid);
1897 static gboolean get_codec(const GDBusPropertyTable *property,
1898 DBusMessageIter *iter, void *data)
1900 struct a2dp_remote_sep *sep = data;
1901 struct avdtp_service_capability *cap = avdtp_get_codec(sep->sep);
1902 struct avdtp_media_codec_capability *codec = (void *) cap->data;
1904 dbus_message_iter_append_basic(iter, DBUS_TYPE_BYTE,
1905 &codec->media_codec_type);
1910 static gboolean get_capabilities(const GDBusPropertyTable *property,
1911 DBusMessageIter *iter, void *data)
1913 struct a2dp_remote_sep *sep = data;
1914 struct avdtp_service_capability *service = avdtp_get_codec(sep->sep);
1915 struct avdtp_media_codec_capability *codec = (void *) service->data;
1916 uint8_t *caps = codec->data;
1917 DBusMessageIter array;
1919 dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
1920 DBUS_TYPE_BYTE_AS_STRING, &array);
1922 dbus_message_iter_append_fixed_array(&array, DBUS_TYPE_BYTE, &caps,
1923 service->length - sizeof(*codec));
1925 dbus_message_iter_close_container(iter, &array);
1930 static gboolean get_device(const GDBusPropertyTable *property,
1931 DBusMessageIter *iter, void *data)
1933 struct a2dp_remote_sep *sep = data;
1936 path = device_get_path(sep->chan->device);
1938 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &path);
1943 static gboolean get_delay_reporting(const GDBusPropertyTable *property,
1944 DBusMessageIter *iter, void *data)
1946 struct a2dp_remote_sep *sep = data;
1947 dbus_bool_t delay_report;
1949 delay_report = avdtp_get_delay_reporting(sep->sep);
1951 dbus_message_iter_append_basic(iter, DBUS_TYPE_BOOLEAN, &delay_report);
1956 static const GDBusPropertyTable sep_properties[] = {
1957 { "UUID", "s", get_uuid, NULL, NULL },
1958 { "Codec", "y", get_codec, NULL, NULL },
1959 { "Capabilities", "ay", get_capabilities, NULL, NULL },
1960 { "Device", "o", get_device, NULL, NULL },
1961 { "DelayReporting", "b", get_delay_reporting, NULL, NULL },
1965 static void remote_sep_destroy(void *user_data)
1967 struct a2dp_remote_sep *sep = user_data;
1969 if (queue_remove(sep->chan->seps, sep))
1970 remove_remote_sep(sep);
1973 static void register_remote_sep(void *data, void *user_data)
1975 struct avdtp_remote_sep *rsep = data;
1976 struct a2dp_channel *chan = user_data;
1977 struct a2dp_remote_sep *sep;
1979 sep = queue_find(chan->seps, match_remote_sep, rsep);
1983 sep = new0(struct a2dp_remote_sep, 1);
1987 if (asprintf(&sep->path, "%s/sep%d",
1988 device_get_path(chan->device),
1989 avdtp_get_seid(rsep)) < 0) {
1990 error("Could not allocate path for remote sep %s/sep%d",
1991 device_get_path(chan->device),
1992 avdtp_get_seid(rsep));
1997 if (g_dbus_register_interface(btd_get_dbus_connection(),
1998 sep->path, MEDIA_ENDPOINT_INTERFACE,
1999 sep_methods, NULL, sep_properties,
2000 sep, remote_sep_free) == FALSE) {
2001 error("Could not register remote sep %s", sep->path);
2007 DBG("Found remote SEP: %s", sep->path);
2009 avdtp_remote_sep_set_destroy(rsep, sep, remote_sep_destroy);
2012 queue_push_tail(chan->seps, sep);
2015 static bool match_seid(const void *data, const void *user_data)
2017 const struct a2dp_remote_sep *sep = data;
2018 const uint8_t *seid = user_data;
2020 return avdtp_get_seid(sep->sep) == *seid;
2023 static int match_sep(const void *data, const void *user_data)
2025 struct a2dp_sep *sep = (void *) data;
2026 const uint8_t *seid = user_data;
2028 return *seid - avdtp_sep_get_seid(sep->lsep);
2031 static struct a2dp_sep *find_sep_by_seid(struct a2dp_server *server,
2036 l = g_slist_find_custom(server->sources, &seid, match_sep);
2040 l = g_slist_find_custom(server->sinks, &seid, match_sep);
2047 static void load_remote_sep(struct a2dp_channel *chan, GKeyFile *key_file,
2050 struct a2dp_sep *lsep;
2051 struct a2dp_remote_sep *sep;
2052 struct avdtp_remote_sep *rsep;
2053 uint8_t lseid, rseid;
2059 for (; *seids; seids++) {
2062 uint8_t delay_reporting;
2068 if (sscanf(*seids, "%02hhx", &rseid) != 1)
2071 value = g_key_file_get_string(key_file, "Endpoints", *seids,
2076 /* Try loading with delay_reporting first */
2077 if (sscanf(value, "%02hhx:%02hhx:%02hhx:%s", &type, &codec,
2078 &delay_reporting, caps) != 4) {
2079 /* Try old format */
2080 if (sscanf(value, "%02hhx:%02hhx:%s", &type, &codec,
2082 warn("Unable to load Endpoint: seid %u", rseid);
2086 delay_reporting = false;
2089 for (i = 0, size = strlen(caps); i < size; i += 2) {
2090 uint8_t *tmp = data + i / 2;
2092 if (sscanf(caps + i, "%02hhx", tmp) != 1) {
2093 warn("Unable to load Endpoint: seid %u", rseid);
2103 caps_add_codec(&l, codec, data, size / 2);
2105 rsep = avdtp_register_remote_sep(chan->session, rseid, type, l,
2108 warn("Unable to register Endpoint: seid %u", rseid);
2112 register_remote_sep(rsep, chan);
2115 value = g_key_file_get_string(key_file, "Endpoints", "LastUsed", NULL);
2119 if (sscanf(value, "%02hhx:%02hhx", &lseid, &rseid) != 2) {
2120 warn("Unable to load LastUsed");
2127 lsep = find_sep_by_seid(chan->server, lseid);
2129 warn("Unable to load LastUsed: lseid %u not found", lseid);
2133 sep = queue_find(chan->seps, match_seid, &rseid);
2135 warn("Unable to load LastUsed: rseid %u not found", rseid);
2139 add_last_used(chan, lsep, sep);
2142 static void load_remote_seps(struct a2dp_channel *chan)
2144 struct btd_device *device = chan->device;
2145 char filename[PATH_MAX];
2150 ba2str(device_get_address(device), dst_addr);
2152 snprintf(filename, PATH_MAX, STORAGEDIR "/%s/cache/%s",
2153 btd_adapter_get_storage_dir(device_get_adapter(device)),
2155 key_file = g_key_file_new();
2156 g_key_file_load_from_file(key_file, filename, 0, NULL);
2157 keys = g_key_file_get_keys(key_file, "Endpoints", NULL, NULL);
2159 load_remote_sep(chan, key_file, keys);
2162 g_key_file_free(key_file);
2165 static void avdtp_state_cb(struct btd_device *dev, struct avdtp *session,
2166 avdtp_session_state_t old_state,
2167 avdtp_session_state_t new_state,
2170 struct a2dp_channel *chan = user_data;
2172 switch (new_state) {
2173 case AVDTP_SESSION_STATE_DISCONNECTED:
2174 if (chan->session == session)
2175 channel_remove(chan);
2177 case AVDTP_SESSION_STATE_CONNECTING:
2179 case AVDTP_SESSION_STATE_CONNECTED:
2181 chan->session = session;
2182 load_remote_seps(chan);
2187 static struct a2dp_channel *channel_new(struct a2dp_server *server,
2188 struct btd_device *device,
2191 struct a2dp_channel *chan;
2193 chan = g_new0(struct a2dp_channel, 1);
2194 chan->server = server;
2195 chan->device = device;
2196 chan->seps = queue_new();
2197 chan->state_id = avdtp_add_state_cb(device, avdtp_state_cb, chan);
2199 if (!queue_push_tail(server->channels, chan)) {
2207 chan->io = g_io_channel_ref(io);
2208 chan->io_id = g_io_add_watch(io, G_IO_ERR | G_IO_HUP | G_IO_NVAL,
2209 (GIOFunc) disconnect_cb, chan);
2214 static bool match_by_device(const void *data, const void *user_data)
2216 const struct a2dp_channel *chan = data;
2217 const struct btd_device *device = user_data;
2219 return chan->device == device;
2222 struct avdtp *a2dp_avdtp_get(struct btd_device *device)
2224 struct a2dp_server *server;
2225 struct a2dp_channel *chan;
2226 const struct queue_entry *entry;
2228 server = find_server(servers, device_get_adapter(device));
2232 chan = queue_find(server->channels, match_by_device, device);
2234 chan = channel_new(server, device, NULL);
2239 #ifdef TIZEN_FEATURE_BLUEZ_MODIFY
2240 if (chan->auth_id) {
2241 DBG("auth is already going...");
2247 return avdtp_ref(chan->session);
2249 /* Check if there is any SEP available */
2250 for (entry = queue_get_entries(server->seps); entry;
2251 entry = entry->next) {
2252 struct avdtp_local_sep *sep = entry->data;
2254 if (avdtp_sep_get_state(sep) == AVDTP_STATE_IDLE)
2258 DBG("Unable to find any available SEP");
2263 chan->session = avdtp_new(chan->io, device, server->seps);
2264 if (!chan->session) {
2265 channel_remove(chan);
2269 return avdtp_ref(chan->session);
2272 static void connect_cb(GIOChannel *io, GError *err, gpointer user_data)
2274 struct a2dp_channel *chan = user_data;
2277 error("%s", err->message);
2281 if (!chan->session) {
2282 chan->session = avdtp_new(chan->io, chan->device,
2283 chan->server->seps);
2284 if (!chan->session) {
2285 error("Unable to create AVDTP session");
2290 g_io_channel_unref(chan->io);
2293 g_source_remove(chan->io_id);
2299 channel_remove(chan);
2302 static void auth_cb(DBusError *derr, void *user_data)
2304 struct a2dp_channel *chan = user_data;
2309 if (derr && dbus_error_is_set(derr)) {
2310 error("Access denied: %s", derr->message);
2314 if (!bt_io_accept(chan->io, connect_cb, chan, NULL, &err)) {
2315 error("bt_io_accept: %s", err->message);
2323 channel_remove(chan);
2326 static void transport_cb(GIOChannel *io, GError *err, gpointer user_data)
2328 struct a2dp_setup *setup = user_data;
2329 uint16_t omtu, imtu;
2331 if (!g_slist_find(setups, setup)) {
2332 warn("bt_io_accept: setup %p no longer valid", setup);
2333 g_io_channel_shutdown(io, TRUE, NULL);
2338 error("%s", err->message);
2342 bt_io_get(io, &err, BT_IO_OPT_OMTU, &omtu, BT_IO_OPT_IMTU, &imtu,
2345 error("%s", err->message);
2350 if (!avdtp_stream_set_transport(setup->stream,
2351 g_io_channel_unix_get_fd(io),
2355 g_io_channel_set_close_on_unref(io, FALSE);
2357 g_io_channel_unref(setup->io);
2366 g_io_channel_shutdown(io, TRUE, NULL);
2369 static void confirm_cb(GIOChannel *io, gpointer data)
2371 struct a2dp_server *server = data;
2372 struct a2dp_channel *chan;
2376 struct btd_device *device;
2379 BT_IO_OPT_SOURCE_BDADDR, &src,
2380 BT_IO_OPT_DEST_BDADDR, &dst,
2381 BT_IO_OPT_DEST, address,
2384 error("%s", err->message);
2389 DBG("AVDTP: incoming connect from %s", address);
2391 device = btd_adapter_find_device(adapter_find(&src), &dst,
2396 #ifdef TIZEN_FEATURE_BLUEZ_MODIFY
2397 if (device_is_profile_blocked(device, A2DP_SINK_UUID)) {
2398 DBG("A2DP is restricted");
2403 chan = queue_find(server->channels, match_by_device, device);
2405 struct a2dp_setup *setup;
2407 setup = find_setup_by_session(chan->session);
2408 if (!setup || !setup->stream)
2412 error("transport channel already exists");
2416 if (!bt_io_accept(io, transport_cb, setup, NULL, &err)) {
2417 error("bt_io_accept: %s", err->message);
2423 * Reference the channel so it can be shutdown properly
2424 * stopping bt_io_accept from calling the callback with invalid
2427 setup->io = g_io_channel_ref(io);
2432 chan = channel_new(server, device, io);
2436 chan->auth_id = btd_request_authorization(&src, &dst,
2437 ADVANCED_AUDIO_UUID,
2439 if (chan->auth_id == 0 && !chan->session)
2440 channel_remove(chan);
2445 g_io_channel_shutdown(io, TRUE, NULL);
2448 static bool a2dp_server_listen(struct a2dp_server *server)
2456 if (main_opts.mps == MPS_OFF)
2457 mode = BT_IO_MODE_BASIC;
2459 mode = BT_IO_MODE_STREAMING;
2461 #if defined(TIZEN_FEATURE_BLUEZ_MODIFY)
2462 if (btd_adapter_get_a2dp_role(server->adapter) == BLUETOOTH_A2DP_SINK_ROLE) {
2463 server->io = bt_io_listen(NULL, confirm_cb, server, NULL, &err,
2464 BT_IO_OPT_SOURCE_BDADDR,
2465 btd_adapter_get_address(server->adapter),
2466 BT_IO_OPT_PSM, AVDTP_PSM,
2467 BT_IO_OPT_MODE, mode,
2468 BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_MEDIUM,
2469 BT_IO_OPT_IMTU, 895,
2470 BT_IO_OPT_MASTER, true,
2473 server->io = bt_io_listen(NULL, confirm_cb, server, NULL, &err,
2474 BT_IO_OPT_SOURCE_BDADDR,
2475 btd_adapter_get_address(server->adapter),
2476 BT_IO_OPT_PSM, AVDTP_PSM,
2477 BT_IO_OPT_MODE, mode,
2478 BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_MEDIUM,
2479 BT_IO_OPT_MASTER, true,
2483 server->io = bt_io_listen(NULL, confirm_cb, server, NULL, &err,
2484 BT_IO_OPT_SOURCE_BDADDR,
2485 btd_adapter_get_address(server->adapter),
2486 BT_IO_OPT_PSM, AVDTP_PSM,
2487 BT_IO_OPT_MODE, mode,
2488 BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_MEDIUM,
2489 BT_IO_OPT_MASTER, true,
2495 error("%s", err->message);
2501 static struct a2dp_server *a2dp_server_register(struct btd_adapter *adapter)
2503 struct a2dp_server *server;
2505 server = g_new0(struct a2dp_server, 1);
2507 server->adapter = btd_adapter_ref(adapter);
2508 server->seps = queue_new();
2509 server->channels = queue_new();
2511 servers = g_slist_append(servers, server);
2516 static void a2dp_unregister_sep(struct a2dp_sep *sep)
2518 struct a2dp_server *server = sep->server;
2521 sep->destroy(sep->user_data);
2522 sep->endpoint = NULL;
2525 avdtp_unregister_sep(server->seps, sep->lsep);
2529 if (!queue_isempty(server->seps))
2533 g_io_channel_shutdown(server->io, TRUE, NULL);
2534 g_io_channel_unref(server->io);
2539 static void a2dp_server_unregister(struct a2dp_server *server)
2541 servers = g_slist_remove(servers, server);
2542 queue_destroy(server->channels, channel_free);
2543 queue_destroy(server->seps, NULL);
2546 g_io_channel_shutdown(server->io, TRUE, NULL);
2547 g_io_channel_unref(server->io);
2550 btd_adapter_unref(server->adapter);
2554 struct a2dp_sep *a2dp_add_sep(struct btd_adapter *adapter, uint8_t type,
2555 uint8_t codec, gboolean delay_reporting,
2556 struct a2dp_endpoint *endpoint,
2557 void *user_data, GDestroyNotify destroy,
2560 struct a2dp_server *server;
2561 struct a2dp_sep *sep;
2563 uint32_t *record_id;
2564 sdp_record_t *record;
2566 server = find_server(servers, adapter);
2567 if (server == NULL) {
2569 *err = -EPROTONOSUPPORT;
2573 if (type == AVDTP_SEP_TYPE_SINK && !server->sink_enabled) {
2575 *err = -EPROTONOSUPPORT;
2579 if (type == AVDTP_SEP_TYPE_SOURCE && !server->source_enabled) {
2581 *err = -EPROTONOSUPPORT;
2585 sep = g_new0(struct a2dp_sep, 1);
2587 sep->lsep = avdtp_register_sep(server->seps, type,
2588 AVDTP_MEDIA_TYPE_AUDIO, codec,
2589 delay_reporting, &endpoint_ind,
2592 if (sep->lsep == NULL) {
2599 sep->server = server;
2600 sep->endpoint = endpoint;
2603 sep->delay_reporting = delay_reporting;
2604 sep->user_data = user_data;
2605 sep->destroy = destroy;
2607 if (type == AVDTP_SEP_TYPE_SOURCE) {
2608 l = &server->sources;
2609 record_id = &server->source_record_id;
2612 record_id = &server->sink_record_id;
2615 if (*record_id != 0)
2618 #ifdef TIZEN_FEATURE_BLUEZ_MODIFY
2619 if (btd_adapter_get_a2dp_role(adapter) == BLUETOOTH_A2DP_SINK_ROLE)
2620 record = a2dp_record(type, true);
2622 record = a2dp_record(type, false);
2624 record = a2dp_record(type);
2627 error("Unable to allocate new service record");
2628 a2dp_unregister_sep(sep);
2634 if (adapter_service_add(server->adapter, record) < 0) {
2635 error("Unable to register A2DP service record");
2636 sdp_record_free(record);
2637 a2dp_unregister_sep(sep);
2643 if (!a2dp_server_listen(server)) {
2644 sdp_record_free(record);
2645 a2dp_unregister_sep(sep);
2651 *record_id = record->handle;
2654 *l = g_slist_append(*l, sep);
2661 void a2dp_remove_sep(struct a2dp_sep *sep)
2663 struct a2dp_server *server = sep->server;
2665 if (sep->type == AVDTP_SEP_TYPE_SOURCE) {
2666 if (g_slist_find(server->sources, sep) == NULL)
2668 server->sources = g_slist_remove(server->sources, sep);
2669 if (server->sources == NULL && server->source_record_id) {
2670 adapter_service_remove(server->adapter,
2671 server->source_record_id);
2672 server->source_record_id = 0;
2675 if (g_slist_find(server->sinks, sep) == NULL)
2677 server->sinks = g_slist_remove(server->sinks, sep);
2678 if (server->sinks == NULL && server->sink_record_id) {
2679 adapter_service_remove(server->adapter,
2680 server->sink_record_id);
2681 server->sink_record_id = 0;
2688 a2dp_unregister_sep(sep);
2691 static void select_cb(struct a2dp_setup *setup, void *ret, int size)
2693 struct avdtp_service_capability *service;
2694 struct avdtp_media_codec_capability *codec;
2698 caps_add_codec(&setup->caps, setup->sep->codec, ret, size);
2702 setup->sep = queue_pop_head(setup->eps);
2704 error("Unable to select a valid configuration");
2708 setup->rsep = find_remote_sep(setup->chan, setup->sep);
2709 service = avdtp_get_codec(setup->rsep->sep);
2710 codec = (struct avdtp_media_codec_capability *) service->data;
2712 err = setup->sep->endpoint->select_configuration(setup->sep,
2714 service->length - sizeof(*codec),
2716 select_cb, setup->sep->user_data);
2721 finalize_select(setup);
2725 static struct queue *a2dp_find_eps(struct avdtp *session, GSList *list,
2728 #ifdef TIZEN_FEATURE_BLUEZ_MODIFY
2729 struct queue *selected_sep = NULL;
2732 struct a2dp_channel *chan = find_channel(session);
2733 struct queue *seps = NULL;
2735 for (; list; list = list->next) {
2736 struct a2dp_sep *sep = list->data;
2737 struct avdtp_remote_sep *rsep;
2738 #ifdef TIZEN_FEATURE_BLUEZ_MODIFY
2739 struct avdtp_media_codec_capability *cap;
2740 struct avdtp_service_capability *service;
2743 /* Use sender's endpoint if available */
2747 if (sep->endpoint == NULL)
2750 name = sep->endpoint->get_name(sep, sep->user_data);
2751 if (g_strcmp0(sender, name) != 0)
2755 #ifdef TIZEN_FEATURE_BLUEZ_MODIFY
2756 rsep = avdtp_find_remote_sep(session, sep->lsep);
2760 service = avdtp_get_codec(rsep);
2761 cap = (struct avdtp_media_codec_capability *) service->data;
2763 if (cap->media_codec_type != A2DP_CODEC_VENDOR) {
2765 selected_sep = queue_new();
2766 queue_push_head(selected_sep, sep);
2768 #ifdef TIZEN_FEATURE_SSC_ENCODER
2770 /* This is vendor codec */
2771 queue_push_head(selected_sep, sep);
2772 if (endpoint_match_codec_preference(session, cap, sep))
2773 return selected_sep;
2778 rsep = avdtp_find_remote_sep(session, sep->lsep);
2785 /* Prepend last used so it is preferred over others */
2786 if (chan->last_used && (chan->last_used->lsep == sep &&
2787 chan->last_used->rsep->sep == rsep))
2788 queue_push_head(seps, sep);
2790 queue_push_tail(seps, sep);
2793 #ifdef TIZEN_FEATURE_BLUEZ_MODIFY
2795 return selected_sep;
2803 static struct queue *a2dp_select_eps(struct avdtp *session, uint8_t type,
2806 struct a2dp_server *server;
2810 server = find_server(servers, avdtp_get_adapter(session));
2814 l = type == AVDTP_SEP_TYPE_SINK ? server->sources : server->sinks;
2816 /* Check sender's seps first */
2817 seps = a2dp_find_eps(session, l, sender);
2821 return a2dp_find_eps(session, l, NULL);
2824 static void store_remote_sep(void *data, void *user_data)
2826 struct a2dp_remote_sep *sep = data;
2827 GKeyFile *key_file = user_data;
2828 char seid[4], value[256];
2829 struct avdtp_service_capability *service = avdtp_get_codec(sep->sep);
2830 struct avdtp_media_codec_capability *codec = (void *) service->data;
2834 sprintf(seid, "%02hhx", avdtp_get_seid(sep->sep));
2836 offset = sprintf(value, "%02hhx:%02hhx:%02hhx:",
2837 avdtp_get_type(sep->sep), codec->media_codec_type,
2838 avdtp_get_delay_reporting(sep->sep));
2840 for (i = 0; i < service->length - sizeof(*codec); i++)
2841 offset += sprintf(value + offset, "%02hhx", codec->data[i]);
2843 g_key_file_set_string(key_file, "Endpoints", seid, value);
2846 static void store_remote_seps(struct a2dp_channel *chan)
2848 struct btd_device *device = chan->device;
2849 char filename[PATH_MAX];
2855 if (queue_isempty(chan->seps))
2858 ba2str(device_get_address(device), dst_addr);
2860 snprintf(filename, PATH_MAX, STORAGEDIR "/%s/cache/%s",
2861 btd_adapter_get_storage_dir(device_get_adapter(device)),
2863 key_file = g_key_file_new();
2864 g_key_file_load_from_file(key_file, filename, 0, NULL);
2866 data = g_key_file_get_string(key_file, "Endpoints", "LastUsed",
2869 /* Remove current endpoints since it might have changed */
2870 g_key_file_remove_group(key_file, "Endpoints", NULL);
2872 queue_foreach(chan->seps, store_remote_sep, key_file);
2875 g_key_file_set_string(key_file, "Endpoints", "LastUsed",
2880 data = g_key_file_to_data(key_file, &length, NULL);
2881 g_file_set_contents(filename, data, length, NULL);
2884 g_key_file_free(key_file);
2887 static void discover_cb(struct avdtp *session, GSList *seps,
2888 struct avdtp_error *err, void *user_data)
2890 struct a2dp_setup *setup = user_data;
2891 uint16_t version = avdtp_get_version(session);
2893 DBG("version 0x%04x err %p", version, err);
2895 #ifdef TIZEN_FEATURE_BLUEZ_MODIFY
2896 if (!g_slist_find(setups, setup)) {
2897 warn("setup %p no longer valid", setup);
2906 g_slist_foreach(seps, register_remote_sep, setup->chan);
2908 /* Only store version has been initialized as features like
2909 * Delay Reporting may not be queried if the version in
2913 store_remote_seps(setup->chan);
2916 finalize_discover(setup);
2919 unsigned int a2dp_discover(struct avdtp *session, a2dp_discover_cb_t cb,
2922 struct a2dp_setup *setup;
2923 struct a2dp_setup_cb *cb_data;
2925 setup = a2dp_setup_get(session);
2929 cb_data = setup_cb_new(setup);
2930 cb_data->discover_cb = cb;
2931 cb_data->user_data = user_data;
2933 if (avdtp_discover(session, discover_cb, setup) == 0)
2936 setup_cb_free(cb_data);
2940 unsigned int a2dp_select_capabilities(struct avdtp *session,
2941 uint8_t type, const char *sender,
2942 a2dp_select_cb_t cb,
2945 struct a2dp_setup *setup;
2946 struct a2dp_setup_cb *cb_data;
2948 struct avdtp_service_capability *service;
2949 struct avdtp_media_codec_capability *codec;
2952 eps = a2dp_select_eps(session, type, sender);
2954 error("Unable to select SEP");
2958 setup = a2dp_setup_get(session);
2962 cb_data = setup_cb_new(setup);
2963 cb_data->select_cb = cb;
2964 cb_data->user_data = user_data;
2967 setup->sep = queue_pop_head(eps);
2968 setup->rsep = find_remote_sep(setup->chan, setup->sep);
2970 if (setup->rsep == NULL) {
2971 error("Could not find remote sep");
2975 service = avdtp_get_codec(setup->rsep->sep);
2976 codec = (struct avdtp_media_codec_capability *) service->data;
2978 err = setup->sep->endpoint->select_configuration(setup->sep,
2984 setup->sep->user_data);
2991 setup_cb_free(cb_data);
2996 unsigned int a2dp_config(struct avdtp *session, struct a2dp_sep *sep,
2997 a2dp_config_cb_t cb, GSList *caps,
3000 struct a2dp_setup_cb *cb_data;
3002 struct a2dp_server *server;
3003 struct a2dp_setup *setup;
3004 struct a2dp_sep *tmp;
3005 struct avdtp_service_capability *cap;
3006 struct avdtp_media_codec_capability *codec_cap = NULL;
3009 server = find_server(servers, avdtp_get_adapter(session));
3013 for (l = caps; l != NULL; l = l->next) {
3016 if (cap->category != AVDTP_MEDIA_CODEC)
3019 codec_cap = (void *) cap->data;
3026 if (sep->codec != codec_cap->media_codec_type)
3029 DBG("a2dp_config: selected SEP %p", sep->lsep);
3031 setup = a2dp_setup_get(session);
3035 cb_data = setup_cb_new(setup);
3036 cb_data->config_cb = cb;
3037 cb_data->user_data = user_data;
3040 setup->stream = sep->stream;
3042 /* Copy given caps if they are different than current caps */
3043 if (setup->caps != caps) {
3044 g_slist_free_full(setup->caps, g_free);
3045 setup->caps = g_slist_copy(caps);
3048 switch (avdtp_sep_get_state(sep->lsep)) {
3049 case AVDTP_STATE_IDLE:
3050 if (sep->type == AVDTP_SEP_TYPE_SOURCE)
3051 l = server->sources;
3055 for (; l != NULL; l = l->next) {
3057 if (avdtp_has_stream(session, tmp->stream))
3064 setup->reconfigure = TRUE;
3065 if (avdtp_close(session, tmp->stream, FALSE) < 0) {
3066 error("avdtp_close failed");
3072 setup->rsep = find_remote_sep(setup->chan, sep);
3073 if (setup->rsep == NULL) {
3074 error("No matching ACP and INT SEPs found");
3078 posix_err = avdtp_set_configuration(session, setup->rsep->sep,
3081 if (posix_err < 0) {
3082 error("avdtp_set_configuration: %s",
3083 strerror(-posix_err));
3087 case AVDTP_STATE_OPEN:
3088 case AVDTP_STATE_STREAMING:
3089 if (avdtp_stream_has_capabilities(setup->stream, caps)) {
3090 DBG("Configuration match: resuming");
3091 cb_data->source_id = g_idle_add(finalize_config,
3093 } else if (!setup->reconfigure) {
3094 setup->reconfigure = TRUE;
3095 if (avdtp_close(session, sep->stream, FALSE) < 0) {
3096 error("avdtp_close failed");
3101 case AVDTP_STATE_CONFIGURED:
3102 case AVDTP_STATE_CLOSING:
3103 case AVDTP_STATE_ABORTING:
3105 error("SEP in bad state for requesting a new stream");
3112 setup_cb_free(cb_data);
3116 unsigned int a2dp_resume(struct avdtp *session, struct a2dp_sep *sep,
3117 a2dp_stream_cb_t cb, void *user_data)
3119 struct a2dp_setup_cb *cb_data;
3120 struct a2dp_setup *setup;
3122 setup = a2dp_setup_get(session);
3126 cb_data = setup_cb_new(setup);
3127 cb_data->resume_cb = cb;
3128 cb_data->user_data = user_data;
3131 setup->stream = sep->stream;
3133 switch (avdtp_sep_get_state(sep->lsep)) {
3134 case AVDTP_STATE_IDLE:
3137 case AVDTP_STATE_CONFIGURED:
3138 setup->start = TRUE;
3140 case AVDTP_STATE_OPEN:
3141 if (avdtp_start(session, sep->stream) < 0) {
3142 error("avdtp_start failed");
3145 sep->starting = TRUE;
3147 case AVDTP_STATE_STREAMING:
3148 if (!sep->suspending && sep->suspend_timer) {
3149 g_source_remove(sep->suspend_timer);
3150 sep->suspend_timer = 0;
3151 avdtp_unref(sep->session);
3152 sep->session = NULL;
3154 if (sep->suspending)
3155 setup->start = TRUE;
3157 cb_data->source_id = g_idle_add(finalize_resume,
3160 case AVDTP_STATE_CLOSING:
3161 case AVDTP_STATE_ABORTING:
3163 error("SEP in bad state for resume");
3170 setup_cb_free(cb_data);
3174 unsigned int a2dp_suspend(struct avdtp *session, struct a2dp_sep *sep,
3175 a2dp_stream_cb_t cb, void *user_data)
3177 struct a2dp_setup_cb *cb_data;
3178 struct a2dp_setup *setup;
3180 setup = a2dp_setup_get(session);
3184 cb_data = setup_cb_new(setup);
3185 cb_data->suspend_cb = cb;
3186 cb_data->user_data = user_data;
3189 setup->stream = sep->stream;
3191 switch (avdtp_sep_get_state(sep->lsep)) {
3192 case AVDTP_STATE_IDLE:
3193 error("a2dp_suspend: no stream to suspend");
3196 case AVDTP_STATE_OPEN:
3197 cb_data->source_id = g_idle_add(finalize_suspend, setup);
3199 case AVDTP_STATE_STREAMING:
3200 if (avdtp_suspend(session, sep->stream) < 0) {
3201 error("avdtp_suspend failed");
3204 sep->suspending = TRUE;
3206 case AVDTP_STATE_CONFIGURED:
3207 case AVDTP_STATE_CLOSING:
3208 case AVDTP_STATE_ABORTING:
3210 error("SEP in bad state for suspend");
3217 setup_cb_free(cb_data);
3221 gboolean a2dp_cancel(unsigned int id)
3225 for (ls = setups; ls != NULL; ls = g_slist_next(ls)) {
3226 struct a2dp_setup *setup = ls->data;
3228 for (l = setup->cb; l != NULL; l = g_slist_next(l)) {
3229 struct a2dp_setup_cb *cb = l->data;
3238 DBG("aborting setup %p", setup);
3239 if (!avdtp_abort(setup->session, setup->stream))
3251 gboolean a2dp_sep_lock(struct a2dp_sep *sep, struct avdtp *session)
3256 DBG("SEP %p locked", sep->lsep);
3262 gboolean a2dp_sep_unlock(struct a2dp_sep *sep, struct avdtp *session)
3264 struct a2dp_server *server = sep->server;
3265 avdtp_state_t state;
3268 state = avdtp_sep_get_state(sep->lsep);
3270 sep->locked = FALSE;
3272 DBG("SEP %p unlocked", sep->lsep);
3274 if (sep->type == AVDTP_SEP_TYPE_SOURCE)
3275 l = server->sources;
3279 /* Unregister sep if it was removed */
3280 if (g_slist_find(l, sep) == NULL) {
3281 a2dp_unregister_sep(sep);
3285 if (!sep->stream || state == AVDTP_STATE_IDLE)
3289 case AVDTP_STATE_OPEN:
3290 /* Set timer here */
3292 case AVDTP_STATE_STREAMING:
3293 if (avdtp_suspend(session, sep->stream) == 0)
3294 sep->suspending = TRUE;
3296 case AVDTP_STATE_IDLE:
3297 case AVDTP_STATE_CONFIGURED:
3298 case AVDTP_STATE_CLOSING:
3299 case AVDTP_STATE_ABORTING:
3307 struct avdtp_stream *a2dp_sep_get_stream(struct a2dp_sep *sep)
3312 struct btd_device *a2dp_setup_get_device(struct a2dp_setup *setup)
3314 if (setup->session == NULL)
3317 return avdtp_get_device(setup->session);
3320 const char *a2dp_setup_remote_path(struct a2dp_setup *setup)
3323 return setup->rsep->path;
3329 static int a2dp_source_probe(struct btd_service *service)
3331 struct btd_device *dev = btd_service_get_device(service);
3333 DBG("path %s", device_get_path(dev));
3335 source_init(service);
3340 static void a2dp_source_remove(struct btd_service *service)
3342 source_unregister(service);
3345 static int a2dp_sink_probe(struct btd_service *service)
3347 struct btd_device *dev = btd_service_get_device(service);
3349 DBG("path %s", device_get_path(dev));
3351 return sink_init(service);
3354 static void a2dp_sink_remove(struct btd_service *service)
3356 sink_unregister(service);
3359 static int a2dp_source_connect(struct btd_service *service)
3361 struct btd_device *dev = btd_service_get_device(service);
3362 struct btd_adapter *adapter = device_get_adapter(dev);
3363 struct a2dp_server *server;
3364 const char *path = device_get_path(dev);
3366 DBG("path %s", path);
3368 server = find_server(servers, adapter);
3369 if (!server || !server->sink_enabled) {
3370 DBG("Unexpected error: cannot find server");
3371 return -EPROTONOSUPPORT;
3374 /* Return protocol not available if no record/endpoint exists */
3375 if (server->sink_record_id == 0)
3376 return -ENOPROTOOPT;
3378 return source_connect(service);
3381 static int a2dp_source_disconnect(struct btd_service *service)
3383 struct btd_device *dev = btd_service_get_device(service);
3384 const char *path = device_get_path(dev);
3386 DBG("path %s", path);
3388 return source_disconnect(service);
3391 static int a2dp_sink_connect(struct btd_service *service)
3393 struct btd_device *dev = btd_service_get_device(service);
3394 struct btd_adapter *adapter = device_get_adapter(dev);
3395 struct a2dp_server *server;
3396 const char *path = device_get_path(dev);
3398 DBG("path %s", path);
3400 server = find_server(servers, adapter);
3401 if (!server || !server->source_enabled) {
3402 DBG("Unexpected error: cannot find server");
3403 return -EPROTONOSUPPORT;
3406 /* Return protocol not available if no record/endpoint exists */
3407 if (server->source_record_id == 0)
3408 return -ENOPROTOOPT;
3410 return sink_connect(service);
3413 static int a2dp_sink_disconnect(struct btd_service *service)
3415 struct btd_device *dev = btd_service_get_device(service);
3416 const char *path = device_get_path(dev);
3418 DBG("path %s", path);
3420 return sink_disconnect(service);
3423 static int a2dp_source_server_probe(struct btd_profile *p,
3424 struct btd_adapter *adapter)
3426 struct a2dp_server *server;
3428 DBG("path %s", adapter_get_path(adapter));
3430 server = find_server(servers, adapter);
3434 server = a2dp_server_register(adapter);
3436 return -EPROTONOSUPPORT;
3439 server->source_enabled = TRUE;
3444 static void a2dp_source_server_remove(struct btd_profile *p,
3445 struct btd_adapter *adapter)
3447 struct a2dp_server *server;
3449 DBG("path %s", adapter_get_path(adapter));
3451 server = find_server(servers, adapter);
3455 g_slist_free_full(server->sources,
3456 (GDestroyNotify) a2dp_unregister_sep);
3458 if (server->source_record_id) {
3459 adapter_service_remove(server->adapter,
3460 server->source_record_id);
3461 server->source_record_id = 0;
3464 if (server->sink_record_id)
3467 a2dp_server_unregister(server);
3470 static int a2dp_sink_server_probe(struct btd_profile *p,
3471 struct btd_adapter *adapter)
3473 struct a2dp_server *server;
3475 DBG("path %s", adapter_get_path(adapter));
3477 server = find_server(servers, adapter);
3481 server = a2dp_server_register(adapter);
3483 return -EPROTONOSUPPORT;
3486 server->sink_enabled = TRUE;
3491 static void a2dp_sink_server_remove(struct btd_profile *p,
3492 struct btd_adapter *adapter)
3494 struct a2dp_server *server;
3496 DBG("path %s", adapter_get_path(adapter));
3498 server = find_server(servers, adapter);
3502 g_slist_free_full(server->sinks, (GDestroyNotify) a2dp_unregister_sep);
3504 if (server->sink_record_id) {
3505 adapter_service_remove(server->adapter, server->sink_record_id);
3506 server->sink_record_id = 0;
3509 if (server->source_record_id)
3512 a2dp_server_unregister(server);
3515 static int media_server_probe(struct btd_adapter *adapter)
3517 DBG("path %s", adapter_get_path(adapter));
3519 return media_register(adapter);
3522 static void media_server_remove(struct btd_adapter *adapter)
3524 DBG("path %s", adapter_get_path(adapter));
3526 media_unregister(adapter);
3529 static struct btd_profile a2dp_source_profile = {
3530 .name = "a2dp-source",
3531 .priority = BTD_PROFILE_PRIORITY_MEDIUM,
3533 .remote_uuid = A2DP_SOURCE_UUID,
3534 .device_probe = a2dp_source_probe,
3535 .device_remove = a2dp_source_remove,
3537 .auto_connect = true,
3538 .connect = a2dp_source_connect,
3539 .disconnect = a2dp_source_disconnect,
3541 .adapter_probe = a2dp_sink_server_probe,
3542 .adapter_remove = a2dp_sink_server_remove,
3545 static struct btd_profile a2dp_sink_profile = {
3546 .name = "a2dp-sink",
3547 .priority = BTD_PROFILE_PRIORITY_MEDIUM,
3549 .remote_uuid = A2DP_SINK_UUID,
3550 .device_probe = a2dp_sink_probe,
3551 .device_remove = a2dp_sink_remove,
3553 .auto_connect = true,
3554 .connect = a2dp_sink_connect,
3555 .disconnect = a2dp_sink_disconnect,
3557 .adapter_probe = a2dp_source_server_probe,
3558 .adapter_remove = a2dp_source_server_remove,
3561 static struct btd_adapter_driver media_driver = {
3563 .probe = media_server_probe,
3564 .remove = media_server_remove,
3567 static int a2dp_init(void)
3569 btd_register_adapter_driver(&media_driver);
3570 btd_profile_register(&a2dp_source_profile);
3571 btd_profile_register(&a2dp_sink_profile);
3576 static void a2dp_exit(void)
3578 btd_unregister_adapter_driver(&media_driver);
3579 btd_profile_unregister(&a2dp_source_profile);
3580 btd_profile_unregister(&a2dp_sink_profile);
3583 BLUETOOTH_PLUGIN_DEFINE(a2dp, VERSION, BLUETOOTH_PLUGIN_PRIORITY_DEFAULT,
3584 a2dp_init, a2dp_exit)