3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2006-2010 Nokia Corporation
6 * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
37 #include <sys/socket.h>
38 #include <netinet/in.h>
42 #include "lib/bluetooth.h"
44 #include "src/shared/util.h"
45 #include "src/shared/queue.h"
47 #include "../profiles/audio/a2dp-codecs.h"
50 static unsigned int seids;
53 # define MAX(x, y) ((x) > (y) ? (x) : (y))
56 #define AVDTP_DISCOVER 0x01
57 #define AVDTP_GET_CAPABILITIES 0x02
58 #define AVDTP_SET_CONFIGURATION 0x03
59 #define AVDTP_GET_CONFIGURATION 0x04
60 #define AVDTP_RECONFIGURE 0x05
61 #define AVDTP_OPEN 0x06
62 #define AVDTP_START 0x07
63 #define AVDTP_CLOSE 0x08
64 #define AVDTP_SUSPEND 0x09
65 #define AVDTP_ABORT 0x0A
66 #define AVDTP_SECURITY_CONTROL 0x0B
67 #define AVDTP_GET_ALL_CAPABILITIES 0x0C
68 #define AVDTP_DELAY_REPORT 0x0D
70 #define AVDTP_PKT_TYPE_SINGLE 0x00
71 #define AVDTP_PKT_TYPE_START 0x01
72 #define AVDTP_PKT_TYPE_CONTINUE 0x02
73 #define AVDTP_PKT_TYPE_END 0x03
75 #define AVDTP_MSG_TYPE_COMMAND 0x00
76 #define AVDTP_MSG_TYPE_GEN_REJECT 0x01
77 #define AVDTP_MSG_TYPE_ACCEPT 0x02
78 #define AVDTP_MSG_TYPE_REJECT 0x03
81 #define ABORT_TIMEOUT 2
82 #define DISCONNECT_TIMEOUT 1
83 #define START_TIMEOUT 1
85 #if __BYTE_ORDER == __LITTLE_ENDIAN
87 struct avdtp_common_header {
88 uint8_t message_type:2;
89 uint8_t packet_type:2;
90 uint8_t transaction:4;
91 } __attribute__ ((packed));
93 struct avdtp_single_header {
94 uint8_t message_type:2;
95 uint8_t packet_type:2;
96 uint8_t transaction:4;
99 } __attribute__ ((packed));
101 struct avdtp_start_header {
102 uint8_t message_type:2;
103 uint8_t packet_type:2;
104 uint8_t transaction:4;
105 uint8_t no_of_packets;
108 } __attribute__ ((packed));
110 struct avdtp_continue_header {
111 uint8_t message_type:2;
112 uint8_t packet_type:2;
113 uint8_t transaction:4;
114 } __attribute__ ((packed));
122 uint8_t media_type:4;
123 } __attribute__ ((packed));
128 } __attribute__ ((packed));
130 #elif __BYTE_ORDER == __BIG_ENDIAN
132 struct avdtp_common_header {
133 uint8_t transaction:4;
134 uint8_t packet_type:2;
135 uint8_t message_type:2;
136 } __attribute__ ((packed));
138 struct avdtp_single_header {
139 uint8_t transaction:4;
140 uint8_t packet_type:2;
141 uint8_t message_type:2;
144 } __attribute__ ((packed));
146 struct avdtp_start_header {
147 uint8_t transaction:4;
148 uint8_t packet_type:2;
149 uint8_t message_type:2;
150 uint8_t no_of_packets;
153 } __attribute__ ((packed));
155 struct avdtp_continue_header {
156 uint8_t transaction:4;
157 uint8_t packet_type:2;
158 uint8_t message_type:2;
159 } __attribute__ ((packed));
165 uint8_t media_type:4;
168 } __attribute__ ((packed));
173 } __attribute__ ((packed));
176 #error "Unknown byte order"
181 struct discover_resp {
182 struct seid_info seps[0];
183 } __attribute__ ((packed));
187 } __attribute__ ((packed));
190 struct seid first_seid;
191 struct seid other_seids[0];
192 } __attribute__ ((packed));
195 struct seid first_seid;
196 struct seid other_seids[0];
197 } __attribute__ ((packed));
201 } __attribute__ ((packed));
206 } __attribute__ ((packed));
208 #if __BYTE_ORDER == __LITTLE_ENDIAN
213 } __attribute__ ((packed));
222 } __attribute__ ((packed));
228 } __attribute__ ((packed));
235 uint8_t serv_cap_len;
238 } __attribute__ ((packed));
244 } __attribute__ ((packed));
246 #elif __BYTE_ORDER == __BIG_ENDIAN
251 } __attribute__ ((packed));
260 } __attribute__ ((packed));
266 } __attribute__ ((packed));
273 uint8_t serv_cap_len;
276 } __attribute__ ((packed));
282 } __attribute__ ((packed));
285 #error "Unknown byte order"
292 uint8_t message_type;
303 struct avdtp_stream *stream; /* Set if the request targeted a stream */
308 struct avdtp_remote_sep {
312 struct avdtp_service_capability *codec;
313 gboolean delay_reporting;
314 GSList *caps; /* of type struct avdtp_service_capability */
315 struct avdtp_stream *stream;
318 struct avdtp_local_sep {
320 struct avdtp_stream *stream;
321 struct seid_info info;
323 uint32_t vndcodec_vendor;
324 uint16_t vndcodec_codec;
325 gboolean delay_reporting;
327 struct avdtp_sep_ind *ind;
328 struct avdtp_sep_cfm *cfm;
332 struct stream_callback {
333 avdtp_stream_state_cb cb;
338 struct discover_callback {
340 avdtp_discover_cb_t cb;
344 struct disconnect_callback {
346 avdtp_disconnect_cb_t cb;
350 struct avdtp_stream {
354 struct avdtp *session;
355 struct avdtp_local_sep *lsep;
359 struct avdtp_service_capability *codec;
360 guint io_id; /* Transport GSource ID */
361 guint timer; /* Waiting for other side to close or open
362 * the transport channel */
363 gboolean open_acp; /* If we are in ACT role for Open */
364 gboolean close_int; /* If we are in INT role for Close */
365 gboolean abort_int; /* If we are in INT role for Abort */
366 guint start_timer; /* Wait START command timer */
367 gboolean delay_reporting;
368 uint16_t delay; /* AVDTP 1.3 Delay Reporting feature */
369 gboolean starting; /* only valid while sep state == OPEN */
372 /* Structure describing an AVDTP connection between two devices */
384 GSList *seps; /* Elements of type struct avdtp_remote_sep * */
385 struct queue *lseps; /* Elements of type struct avdtp_local_sep * */
387 GSList *streams; /* Elements of type struct avdtp_stream * */
389 GSList *req_queue; /* Elements of type struct pending_req * */
390 GSList *prio_queue; /* Same as req_queue but is processed before it */
392 struct avdtp_stream *pending_open;
401 struct discover_callback *discover;
402 struct pending_req *req;
409 static int send_request(struct avdtp *session, gboolean priority,
410 struct avdtp_stream *stream, uint8_t signal_id,
411 void *buffer, size_t size);
412 static gboolean avdtp_parse_resp(struct avdtp *session,
413 struct avdtp_stream *stream,
414 uint8_t transaction, uint8_t signal_id,
415 void *buf, int size);
416 static gboolean avdtp_parse_rej(struct avdtp *session,
417 struct avdtp_stream *stream,
418 uint8_t transaction, uint8_t signal_id,
419 void *buf, int size);
420 static int process_queue(struct avdtp *session);
421 static void avdtp_sep_set_state(struct avdtp *session,
422 struct avdtp_local_sep *sep,
423 avdtp_state_t state);
425 static const char *avdtp_statestr(avdtp_state_t state)
428 case AVDTP_STATE_IDLE:
430 case AVDTP_STATE_CONFIGURED:
432 case AVDTP_STATE_OPEN:
434 case AVDTP_STATE_STREAMING:
436 case AVDTP_STATE_CLOSING:
438 case AVDTP_STATE_ABORTING:
441 return "<unknown state>";
445 static gboolean try_send(int sk, void *data, size_t len)
450 err = send(sk, data, len, 0);
451 } while (err < 0 && errno == EINTR);
454 error("send: %s (%d)", strerror(errno), errno);
456 } else if ((size_t) err != len) {
457 error("try_send: complete buffer not sent (%d/%zu bytes)",
465 static gboolean avdtp_send(struct avdtp *session, uint8_t transaction,
466 uint8_t message_type, uint8_t signal_id,
467 void *data, size_t len)
469 unsigned int cont_fragments, sent;
470 struct avdtp_start_header start;
471 struct avdtp_continue_header cont;
474 if (session->io == NULL) {
475 error("avdtp_send: session is closed");
479 sock = g_io_channel_unix_get_fd(session->io);
481 /* Single packet - no fragmentation */
482 if (sizeof(struct avdtp_single_header) + len <= session->omtu) {
483 struct avdtp_single_header single;
485 memset(&single, 0, sizeof(single));
487 single.transaction = transaction;
488 single.packet_type = AVDTP_PKT_TYPE_SINGLE;
489 single.message_type = message_type;
490 single.signal_id = signal_id;
492 memcpy(session->buf, &single, sizeof(single));
493 memcpy(session->buf + sizeof(single), data, len);
495 return try_send(sock, session->buf, sizeof(single) + len);
498 /* Check if there is enough space to start packet */
499 if (session->omtu < sizeof(start)) {
500 error("No enough space to fragment packet");
504 /* Count the number of needed fragments */
505 cont_fragments = (len - (session->omtu - sizeof(start))) /
506 (session->omtu - sizeof(cont)) + 1;
508 DBG("%zu bytes split into %d fragments", len, cont_fragments + 1);
510 /* Send the start packet */
511 memset(&start, 0, sizeof(start));
512 start.transaction = transaction;
513 start.packet_type = AVDTP_PKT_TYPE_START;
514 start.message_type = message_type;
515 start.no_of_packets = cont_fragments + 1;
516 start.signal_id = signal_id;
518 memcpy(session->buf, &start, sizeof(start));
519 memcpy(session->buf + sizeof(start), data,
520 session->omtu - sizeof(start));
522 if (!try_send(sock, session->buf, session->omtu))
525 DBG("first packet with %zu bytes sent", session->omtu - sizeof(start));
527 sent = session->omtu - sizeof(start);
529 /* Send the continue fragments and the end packet */
534 if (left + sizeof(cont) > session->omtu) {
535 cont.packet_type = AVDTP_PKT_TYPE_CONTINUE;
536 to_copy = session->omtu - sizeof(cont);
537 DBG("sending continue with %d bytes", to_copy);
539 cont.packet_type = AVDTP_PKT_TYPE_END;
541 DBG("sending end with %d bytes", to_copy);
544 cont.transaction = transaction;
545 cont.message_type = message_type;
547 memcpy(session->buf, &cont, sizeof(cont));
548 memcpy(session->buf + sizeof(cont), data + sent, to_copy);
550 if (!try_send(sock, session->buf, to_copy + sizeof(cont)))
559 static void pending_req_free(void *data)
561 struct pending_req *req = data;
564 g_source_remove(req->timeout);
569 static void close_stream(struct avdtp_stream *stream)
573 if (stream->io == NULL)
576 sock = g_io_channel_unix_get_fd(stream->io);
578 shutdown(sock, SHUT_RDWR);
580 g_io_channel_shutdown(stream->io, FALSE, NULL);
582 g_io_channel_unref(stream->io);
586 static gboolean stream_close_timeout(gpointer user_data)
588 struct avdtp_stream *stream = user_data;
590 DBG("Timed out waiting for peer to close the transport channel");
594 close_stream(stream);
599 static gboolean stream_open_timeout(gpointer user_data)
601 struct avdtp_stream *stream = user_data;
603 DBG("Timed out waiting for peer to open the transport channel");
607 stream->session->pending_open = NULL;
609 avdtp_abort(stream->session, stream);
614 void avdtp_error_init(struct avdtp_error *err, uint8_t category, int id)
616 err->category = category;
618 if (category == AVDTP_ERRNO)
619 err->err.posix_errno = id;
621 err->err.error_code = id;
624 uint8_t avdtp_error_category(struct avdtp_error *err)
626 return err->category;
629 int avdtp_error_error_code(struct avdtp_error *err)
631 assert(err->category != AVDTP_ERRNO);
632 return err->err.error_code;
635 int avdtp_error_posix_errno(struct avdtp_error *err)
637 assert(err->category == AVDTP_ERRNO);
638 return err->err.posix_errno;
641 static struct avdtp_stream *find_stream_by_rseid(struct avdtp *session,
646 for (l = session->streams; l != NULL; l = g_slist_next(l)) {
647 struct avdtp_stream *stream = l->data;
649 if (stream->rseid == rseid)
656 static struct avdtp_remote_sep *find_remote_sep(GSList *seps, uint8_t seid)
660 for (l = seps; l != NULL; l = g_slist_next(l)) {
661 struct avdtp_remote_sep *sep = l->data;
663 if (sep->seid == seid)
670 static void stream_free(void *data)
672 struct avdtp_stream *stream = data;
673 struct avdtp_remote_sep *rsep;
675 stream->lsep->info.inuse = 0;
676 stream->lsep->stream = NULL;
678 rsep = find_remote_sep(stream->session->seps, stream->rseid);
683 g_source_remove(stream->timer);
685 if (stream->start_timer > 0)
686 g_source_remove(stream->start_timer);
689 close_stream(stream);
692 g_source_remove(stream->io_id);
694 g_slist_free_full(stream->callbacks, g_free);
695 g_slist_free_full(stream->caps, g_free);
700 static gboolean transport_cb(GIOChannel *chan, GIOCondition cond,
703 struct avdtp_stream *stream = data;
704 struct avdtp_local_sep *sep = stream->lsep;
706 if (stream->close_int && sep->cfm && sep->cfm->close)
707 sep->cfm->close(stream->session, sep, stream, NULL,
710 if (!(cond & G_IO_NVAL))
711 close_stream(stream);
715 if (!stream->abort_int)
716 avdtp_sep_set_state(stream->session, sep, AVDTP_STATE_IDLE);
721 static void handle_transport_connect(struct avdtp *session, GIOChannel *io,
722 uint16_t imtu, uint16_t omtu)
724 struct avdtp_stream *stream = session->pending_open;
725 struct avdtp_local_sep *sep = stream->lsep;
727 session->pending_open = NULL;
730 g_source_remove(stream->timer);
737 if (stream->io == NULL)
738 stream->io = g_io_channel_ref(io);
743 avdtp_sep_set_state(session, sep, AVDTP_STATE_OPEN);
745 stream->io_id = g_io_add_watch(io, G_IO_ERR | G_IO_HUP | G_IO_NVAL,
746 (GIOFunc) transport_cb, stream);
749 static int pending_req_cmp(gconstpointer a, gconstpointer b)
751 const struct pending_req *req = a;
752 const struct avdtp_stream *stream = b;
754 if (req->stream == stream)
760 static void cleanup_queue(struct avdtp *session, struct avdtp_stream *stream)
763 struct pending_req *req;
765 while ((l = g_slist_find_custom(session->prio_queue, stream,
768 pending_req_free(req);
769 session->prio_queue = g_slist_remove(session->prio_queue, req);
772 while ((l = g_slist_find_custom(session->req_queue, stream,
775 pending_req_free(req);
776 session->req_queue = g_slist_remove(session->req_queue, req);
780 static void handle_unanswered_req(struct avdtp *session,
781 struct avdtp_stream *stream)
783 struct pending_req *req;
784 struct avdtp_local_sep *lsep;
785 struct avdtp_error err;
787 if (!session->req->timeout)
788 /* Request is in process */
791 if (session->req->signal_id == AVDTP_ABORT) {
792 /* Avoid freeing the Abort request here */
793 DBG("handle_unanswered_req: Abort req, returning");
794 session->req->stream = NULL;
801 avdtp_error_init(&err, AVDTP_ERRNO, EIO);
805 switch (req->signal_id) {
806 case AVDTP_RECONFIGURE:
807 error("No reply to Reconfigure request");
808 if (lsep && lsep->cfm && lsep->cfm->reconfigure)
809 lsep->cfm->reconfigure(session, lsep, stream, &err,
813 error("No reply to Open request");
814 if (lsep && lsep->cfm && lsep->cfm->open)
815 lsep->cfm->open(session, lsep, stream, &err,
819 error("No reply to Start request");
820 if (lsep && lsep->cfm && lsep->cfm->start)
821 lsep->cfm->start(session, lsep, stream, &err,
825 error("No reply to Suspend request");
826 if (lsep && lsep->cfm && lsep->cfm->suspend)
827 lsep->cfm->suspend(session, lsep, stream, &err,
831 error("No reply to Close request");
832 if (lsep && lsep->cfm && lsep->cfm->close)
833 lsep->cfm->close(session, lsep, stream, &err,
836 case AVDTP_SET_CONFIGURATION:
837 error("No reply to SetConfiguration request");
838 if (lsep && lsep->cfm && lsep->cfm->set_configuration)
839 lsep->cfm->set_configuration(session, lsep, stream,
840 &err, lsep->user_data);
843 pending_req_free(req);
846 static void avdtp_sep_set_state(struct avdtp *session,
847 struct avdtp_local_sep *sep,
850 struct avdtp_stream *stream = sep->stream;
851 avdtp_state_t old_state;
852 struct avdtp_error err, *err_ptr = NULL;
856 error("Error changing sep state: stream not available");
860 if (sep->state == state) {
861 avdtp_error_init(&err, AVDTP_ERRNO, EIO);
862 DBG("stream state change failed: %s", avdtp_strerror(&err));
866 DBG("stream state changed: %s -> %s",
867 avdtp_statestr(sep->state),
868 avdtp_statestr(state));
871 old_state = sep->state;
875 case AVDTP_STATE_CONFIGURED:
876 if (sep->info.type == AVDTP_SEP_TYPE_SINK)
877 avdtp_delay_report(session, stream, stream->delay);
879 case AVDTP_STATE_OPEN:
880 stream->starting = FALSE;
882 case AVDTP_STATE_STREAMING:
883 if (stream->start_timer) {
884 g_source_remove(stream->start_timer);
885 stream->start_timer = 0;
887 stream->open_acp = FALSE;
889 case AVDTP_STATE_CLOSING:
890 case AVDTP_STATE_ABORTING:
891 if (stream->start_timer) {
892 g_source_remove(stream->start_timer);
893 stream->start_timer = 0;
896 case AVDTP_STATE_IDLE:
897 if (stream->start_timer) {
898 g_source_remove(stream->start_timer);
899 stream->start_timer = 0;
901 if (session->pending_open == stream)
902 handle_transport_connect(session, NULL, 0, 0);
903 if (session->req && session->req->stream == stream)
904 handle_unanswered_req(session, stream);
905 /* Remove pending commands for this stream from the queue */
906 cleanup_queue(session, stream);
912 l = stream->callbacks;
914 struct stream_callback *cb = l->data;
916 cb->cb(stream, old_state, state, err_ptr, cb->user_data);
919 if (state == AVDTP_STATE_IDLE &&
920 g_slist_find(session->streams, stream)) {
921 session->streams = g_slist_remove(session->streams, stream);
925 if (session->io && session->shutdown && session->streams == NULL) {
926 int sock = g_io_channel_unix_get_fd(session->io);
927 shutdown(sock, SHUT_RDWR);
931 static void finalize_discovery(struct avdtp *session, int err)
933 struct discover_callback *discover = session->discover;
934 struct avdtp_error avdtp_err;
939 session->discover = NULL;
941 avdtp_error_init(&avdtp_err, AVDTP_ERRNO, err);
943 if (discover->id > 0)
944 g_source_remove(discover->id);
947 discover->cb(session, session->seps, err ? &avdtp_err : NULL,
948 discover->user_data);
952 static void release_stream(struct avdtp_stream *stream, struct avdtp *session)
954 struct avdtp_local_sep *sep = stream->lsep;
956 if (sep->cfm && sep->cfm->abort &&
957 (sep->state != AVDTP_STATE_ABORTING ||
959 sep->cfm->abort(session, sep, stream, NULL, sep->user_data);
961 avdtp_sep_set_state(session, sep, AVDTP_STATE_IDLE);
964 static void sep_free(gpointer data)
966 struct avdtp_remote_sep *sep = data;
968 g_slist_free_full(sep->caps, g_free);
972 static void avdtp_free(void *data)
974 struct avdtp *session = data;
978 g_slist_free_full(session->streams, stream_free);
981 g_io_channel_shutdown(session->io, FALSE, NULL);
982 g_io_channel_unref(session->io);
985 if (session->io_id) {
986 g_source_remove(session->io_id);
991 pending_req_free(session->req);
993 g_slist_free_full(session->req_queue, pending_req_free);
994 g_slist_free_full(session->prio_queue, pending_req_free);
995 g_slist_free_full(session->seps, sep_free);
996 g_slist_free_full(session->disconnect, g_free);
998 /* Free copy of the SEP list */
999 session->lseps = NULL;
1001 g_free(session->buf);
1006 static void process_disconnect(void *data)
1008 struct disconnect_callback *callback = data;
1010 callback->cb(callback->user_data);
1015 static void connection_lost(struct avdtp *session, int err)
1017 DBG("Disconnected: %s (%d)", strerror(err), err);
1019 g_slist_foreach(session->streams, (GFunc) release_stream, session);
1020 session->streams = NULL;
1024 finalize_discovery(session, err);
1026 g_slist_free_full(session->disconnect, process_disconnect);
1027 session->disconnect = NULL;
1029 avdtp_unref(session);
1032 void avdtp_unref(struct avdtp *session)
1039 DBG("%p: ref=%d", session, session->ref);
1041 if (session->ref > 0)
1044 finalize_discovery(session, ECONNABORTED);
1046 avdtp_free(session);
1049 struct avdtp *avdtp_ref(struct avdtp *session)
1053 DBG("%p: ref=%d", session, session->ref);
1058 static bool match_by_seid(const void *data, const void *user_data)
1060 const struct avdtp_local_sep *sep = data;
1061 uint8_t seid = PTR_TO_UINT(user_data);
1063 return sep->info.seid == seid;
1066 static struct avdtp_local_sep *find_local_sep_by_seid(struct avdtp *session,
1069 return queue_find(session->lseps, match_by_seid, INT_TO_PTR(seid));
1072 struct avdtp_remote_sep *avdtp_find_remote_sep(struct avdtp *session,
1073 struct avdtp_local_sep *lsep)
1077 if (lsep->info.inuse)
1080 for (l = session->seps; l != NULL; l = g_slist_next(l)) {
1081 struct avdtp_remote_sep *sep = l->data;
1082 struct avdtp_service_capability *cap;
1083 struct avdtp_media_codec_capability *codec_data;
1085 /* Type must be different: source <-> sink */
1086 if (sep->type == lsep->info.type)
1089 if (sep->media_type != lsep->info.media_type)
1096 codec_data = (void *) cap->data;
1098 if (codec_data->media_codec_type != lsep->codec)
1101 /* FIXME: Add Vendor Specific Codec match to SEP callback */
1102 if (lsep->codec == A2DP_CODEC_VENDOR) {
1103 a2dp_vendor_codec_t *vndcodec =
1104 (void *) codec_data->data;
1106 if (btohl(vndcodec->vendor_id) != lsep->vndcodec_vendor)
1109 if (btohs(vndcodec->codec_id) != lsep->vndcodec_codec)
1113 if (sep->stream == NULL)
1120 static GSList *caps_to_list(uint8_t *data, int size,
1121 struct avdtp_service_capability **codec,
1122 gboolean *delay_reporting)
1127 if (delay_reporting)
1128 *delay_reporting = FALSE;
1130 for (processed = 0, caps = NULL; processed + 2 <= size;) {
1131 struct avdtp_service_capability *cap;
1132 uint8_t length, category;
1137 if (processed + 2 + length > size) {
1138 error("Invalid capability data in getcap resp");
1142 cap = g_malloc(sizeof(struct avdtp_service_capability) +
1144 memcpy(cap, data, 2 + length);
1146 processed += 2 + length;
1149 caps = g_slist_append(caps, cap);
1151 if (category == AVDTP_MEDIA_CODEC &&
1153 sizeof(struct avdtp_media_codec_capability))
1155 else if (category == AVDTP_DELAY_REPORTING && delay_reporting)
1156 *delay_reporting = TRUE;
1162 static gboolean avdtp_unknown_cmd(struct avdtp *session, uint8_t transaction,
1165 return avdtp_send(session, transaction, AVDTP_MSG_TYPE_GEN_REJECT,
1166 signal_id, NULL, 0);
1169 static void copy_seps(void *data, void *user_data)
1171 struct avdtp_local_sep *sep = data;
1172 struct seid_info **p = user_data;
1174 memcpy(*p, &sep->info, sizeof(struct seid_info));
1178 static gboolean avdtp_discover_cmd(struct avdtp *session, uint8_t transaction,
1179 void *buf, int size)
1181 unsigned int rsp_size, sep_count;
1182 struct seid_info *seps, *p;
1185 sep_count = queue_length(session->lseps);
1187 if (sep_count == 0) {
1188 uint8_t err = AVDTP_NOT_SUPPORTED_COMMAND;
1189 return avdtp_send(session, transaction, AVDTP_MSG_TYPE_REJECT,
1190 AVDTP_DISCOVER, &err, sizeof(err));
1193 rsp_size = sep_count * sizeof(struct seid_info);
1195 seps = g_new0(struct seid_info, sep_count);
1198 queue_foreach(session->lseps, copy_seps, &p);
1200 ret = avdtp_send(session, transaction, AVDTP_MSG_TYPE_ACCEPT,
1201 AVDTP_DISCOVER, seps, rsp_size);
1207 static gboolean avdtp_getcap_cmd(struct avdtp *session, uint8_t transaction,
1208 struct seid_req *req, unsigned int size,
1212 struct avdtp_local_sep *sep = NULL;
1213 unsigned int rsp_size;
1214 uint8_t err, buf[1024], *ptr = buf;
1217 cmd = get_all ? AVDTP_GET_ALL_CAPABILITIES : AVDTP_GET_CAPABILITIES;
1219 if (size < sizeof(struct seid_req)) {
1220 err = AVDTP_BAD_LENGTH;
1224 sep = find_local_sep_by_seid(session, req->acp_seid);
1226 err = AVDTP_BAD_ACP_SEID;
1230 if (!sep->ind->get_capability(session, sep, &caps, &err,
1234 for (l = caps, rsp_size = 0; l != NULL; l = g_slist_next(l)) {
1235 struct avdtp_service_capability *cap = l->data;
1237 if (rsp_size + cap->length + 2 > sizeof(buf))
1240 memcpy(ptr, cap, cap->length + 2);
1241 rsp_size += cap->length + 2;
1242 ptr += cap->length + 2;
1247 if (get_all && sep->delay_reporting) {
1248 ptr[0] = AVDTP_DELAY_REPORTING;
1255 return avdtp_send(session, transaction, AVDTP_MSG_TYPE_ACCEPT, cmd,
1259 return avdtp_send(session, transaction, AVDTP_MSG_TYPE_REJECT, cmd,
1263 static void setconf_cb(struct avdtp *session, struct avdtp_stream *stream,
1264 struct avdtp_error *err)
1266 struct conf_rej rej;
1267 struct avdtp_local_sep *sep;
1270 rej.error = AVDTP_UNSUPPORTED_CONFIGURATION;
1271 rej.category = err->err.error_code;
1272 avdtp_send(session, session->in.transaction,
1273 AVDTP_MSG_TYPE_REJECT, AVDTP_SET_CONFIGURATION,
1278 if (!avdtp_send(session, session->in.transaction, AVDTP_MSG_TYPE_ACCEPT,
1279 AVDTP_SET_CONFIGURATION, NULL, 0)) {
1280 stream_free(stream);
1285 sep->stream = stream;
1286 sep->info.inuse = 1;
1287 session->streams = g_slist_append(session->streams, stream);
1289 avdtp_sep_set_state(session, sep, AVDTP_STATE_CONFIGURED);
1292 static gboolean avdtp_setconf_cmd(struct avdtp *session, uint8_t transaction,
1293 struct setconf_req *req, unsigned int size)
1295 struct conf_rej rej;
1296 struct avdtp_local_sep *sep;
1297 struct avdtp_stream *stream;
1298 uint8_t err, category = 0x00;
1301 if (size < sizeof(struct setconf_req)) {
1302 error("Too short getcap request");
1306 sep = find_local_sep_by_seid(session, req->acp_seid);
1308 err = AVDTP_BAD_ACP_SEID;
1313 err = AVDTP_SEP_IN_USE;
1317 stream = g_new0(struct avdtp_stream, 1);
1318 stream->session = session;
1320 stream->rseid = req->int_seid;
1321 stream->caps = caps_to_list(req->caps,
1322 size - sizeof(struct setconf_req),
1324 &stream->delay_reporting);
1327 * Verify that the Media Transport capability's length = 0.
1330 for (l = stream->caps; l != NULL; l = g_slist_next(l)) {
1331 struct avdtp_service_capability *cap = l->data;
1333 if (cap->category == AVDTP_MEDIA_TRANSPORT &&
1335 err = AVDTP_BAD_MEDIA_TRANSPORT_FORMAT;
1340 if (stream->delay_reporting && session->version < 0x0103)
1341 session->version = 0x0103;
1343 if (sep->ind && sep->ind->set_configuration) {
1344 if (!sep->ind->set_configuration(session, sep, stream,
1348 err = AVDTP_UNSUPPORTED_CONFIGURATION;
1353 if (!avdtp_send(session, transaction, AVDTP_MSG_TYPE_ACCEPT,
1354 AVDTP_SET_CONFIGURATION, NULL, 0)) {
1355 stream_free(stream);
1359 sep->stream = stream;
1360 sep->info.inuse = 1;
1361 session->streams = g_slist_append(session->streams, stream);
1363 avdtp_sep_set_state(session, sep, AVDTP_STATE_CONFIGURED);
1369 stream_free(stream);
1372 rej.category = category;
1373 return avdtp_send(session, transaction, AVDTP_MSG_TYPE_REJECT,
1374 AVDTP_SET_CONFIGURATION, &rej, sizeof(rej));
1377 static gboolean avdtp_getconf_cmd(struct avdtp *session, uint8_t transaction,
1378 struct seid_req *req, int size)
1381 struct avdtp_local_sep *sep = NULL;
1387 if (size < (int) sizeof(struct seid_req)) {
1388 error("Too short getconf request");
1392 memset(buf, 0, sizeof(buf));
1394 sep = find_local_sep_by_seid(session, req->acp_seid);
1396 err = AVDTP_BAD_ACP_SEID;
1399 if (!sep->stream || !sep->stream->caps) {
1400 err = AVDTP_UNSUPPORTED_CONFIGURATION;
1404 for (l = sep->stream->caps, rsp_size = 0; l; l = g_slist_next(l)) {
1405 struct avdtp_service_capability *cap = l->data;
1407 if (rsp_size + cap->length + 2 > (int) sizeof(buf))
1410 memcpy(ptr, cap, cap->length + 2);
1411 rsp_size += cap->length + 2;
1412 ptr += cap->length + 2;
1415 return avdtp_send(session, transaction, AVDTP_MSG_TYPE_ACCEPT,
1416 AVDTP_GET_CONFIGURATION, buf, rsp_size);
1419 return avdtp_send(session, transaction, AVDTP_MSG_TYPE_REJECT,
1420 AVDTP_GET_CONFIGURATION, &err, sizeof(err));
1423 static gboolean avdtp_reconf_cmd(struct avdtp *session, uint8_t transaction,
1424 struct seid_req *req, int size)
1426 struct conf_rej rej;
1428 rej.error = AVDTP_NOT_SUPPORTED_COMMAND;
1429 rej.category = 0x00;
1431 return avdtp_send(session, transaction, AVDTP_MSG_TYPE_REJECT,
1432 AVDTP_RECONFIGURE, &rej, sizeof(rej));
1435 static void check_seid_collision(struct pending_req *req, uint8_t id)
1437 struct seid_req *seid = req->data;
1439 if (seid->acp_seid == id)
1440 req->collided = TRUE;
1443 static void check_start_collision(struct pending_req *req, uint8_t id)
1445 struct start_req *start = req->data;
1446 struct seid *seid = &start->first_seid;
1447 int count = 1 + req->data_size - sizeof(struct start_req);
1450 for (i = 0; i < count; i++, seid++) {
1451 if (seid->seid == id) {
1452 req->collided = TRUE;
1458 static void check_suspend_collision(struct pending_req *req, uint8_t id)
1460 struct suspend_req *suspend = req->data;
1461 struct seid *seid = &suspend->first_seid;
1462 int count = 1 + req->data_size - sizeof(struct suspend_req);
1465 for (i = 0; i < count; i++, seid++) {
1466 if (seid->seid == id) {
1467 req->collided = TRUE;
1473 static void avdtp_check_collision(struct avdtp *session, uint8_t cmd,
1474 struct avdtp_stream *stream)
1476 struct pending_req *req = session->req;
1478 if (req == NULL || (req->signal_id != cmd && cmd != AVDTP_ABORT))
1481 if (cmd == AVDTP_ABORT)
1482 cmd = req->signal_id;
1487 check_seid_collision(req, stream->rseid);
1490 check_start_collision(req, stream->rseid);
1493 check_suspend_collision(req, stream->rseid);
1498 static gboolean avdtp_open_cmd(struct avdtp *session, uint8_t transaction,
1499 struct seid_req *req, unsigned int size)
1501 struct avdtp_local_sep *sep;
1502 struct avdtp_stream *stream;
1505 if (size < sizeof(struct seid_req)) {
1506 error("Too short abort request");
1510 sep = find_local_sep_by_seid(session, req->acp_seid);
1512 err = AVDTP_BAD_ACP_SEID;
1516 if (sep->state != AVDTP_STATE_CONFIGURED) {
1517 err = AVDTP_BAD_STATE;
1521 stream = sep->stream;
1523 if (sep->ind && sep->ind->open) {
1524 if (!sep->ind->open(session, sep, stream, &err,
1529 avdtp_check_collision(session, AVDTP_OPEN, stream);
1531 if (!avdtp_send(session, transaction, AVDTP_MSG_TYPE_ACCEPT,
1532 AVDTP_OPEN, NULL, 0))
1535 stream->open_acp = TRUE;
1536 session->pending_open = stream;
1537 stream->timer = g_timeout_add_seconds(REQ_TIMEOUT,
1538 stream_open_timeout,
1544 return avdtp_send(session, transaction, AVDTP_MSG_TYPE_REJECT,
1545 AVDTP_OPEN, &err, sizeof(err));
1548 static gboolean avdtp_start_cmd(struct avdtp *session, uint8_t transaction,
1549 struct start_req *req, unsigned int size)
1551 struct avdtp_local_sep *sep;
1552 struct avdtp_stream *stream;
1553 struct stream_rej rej;
1555 uint8_t err, failed_seid;
1558 if (size < sizeof(struct start_req)) {
1559 error("Too short start request");
1563 seid_count = 1 + size - sizeof(struct start_req);
1565 seid = &req->first_seid;
1567 for (i = 0; i < seid_count; i++, seid++) {
1568 failed_seid = seid->seid;
1570 sep = find_local_sep_by_seid(session, req->first_seid.seid);
1571 if (!sep || !sep->stream) {
1572 err = AVDTP_BAD_ACP_SEID;
1576 stream = sep->stream;
1578 /* Also reject start cmd if state is not open */
1579 if (sep->state != AVDTP_STATE_OPEN) {
1580 err = AVDTP_BAD_STATE;
1583 stream->starting = TRUE;
1585 if (sep->ind && sep->ind->start) {
1586 if (!sep->ind->start(session, sep, stream, &err,
1591 avdtp_check_collision(session, AVDTP_START, stream);
1593 avdtp_sep_set_state(session, sep, AVDTP_STATE_STREAMING);
1596 return avdtp_send(session, transaction, AVDTP_MSG_TYPE_ACCEPT,
1597 AVDTP_START, NULL, 0);
1600 DBG("Rejecting (%d)", err);
1601 memset(&rej, 0, sizeof(rej));
1602 rej.acp_seid = failed_seid;
1604 return avdtp_send(session, transaction, AVDTP_MSG_TYPE_REJECT,
1605 AVDTP_START, &rej, sizeof(rej));
1608 static gboolean avdtp_close_cmd(struct avdtp *session, uint8_t transaction,
1609 struct seid_req *req, unsigned int size)
1611 struct avdtp_local_sep *sep;
1612 struct avdtp_stream *stream;
1615 if (size < sizeof(struct seid_req)) {
1616 error("Too short close request");
1620 sep = find_local_sep_by_seid(session, req->acp_seid);
1621 if (!sep || !sep->stream) {
1622 err = AVDTP_BAD_ACP_SEID;
1626 if (sep->state != AVDTP_STATE_OPEN &&
1627 sep->state != AVDTP_STATE_STREAMING) {
1628 err = AVDTP_BAD_STATE;
1632 stream = sep->stream;
1634 if (sep->ind && sep->ind->close) {
1635 if (!sep->ind->close(session, sep, stream, &err,
1640 avdtp_check_collision(session, AVDTP_CLOSE, stream);
1642 avdtp_sep_set_state(session, sep, AVDTP_STATE_CLOSING);
1644 if (!avdtp_send(session, transaction, AVDTP_MSG_TYPE_ACCEPT,
1645 AVDTP_CLOSE, NULL, 0))
1648 stream->timer = g_timeout_add_seconds(REQ_TIMEOUT,
1649 stream_close_timeout,
1655 return avdtp_send(session, transaction, AVDTP_MSG_TYPE_REJECT,
1656 AVDTP_CLOSE, &err, sizeof(err));
1659 static gboolean avdtp_suspend_cmd(struct avdtp *session, uint8_t transaction,
1660 struct suspend_req *req, unsigned int size)
1662 struct avdtp_local_sep *sep;
1663 struct avdtp_stream *stream;
1664 struct stream_rej rej;
1666 uint8_t err, failed_seid;
1669 if (size < sizeof(struct suspend_req)) {
1670 error("Too short suspend request");
1674 seid_count = 1 + size - sizeof(struct suspend_req);
1676 seid = &req->first_seid;
1678 for (i = 0; i < seid_count; i++, seid++) {
1679 failed_seid = seid->seid;
1681 sep = find_local_sep_by_seid(session, req->first_seid.seid);
1682 if (!sep || !sep->stream) {
1683 err = AVDTP_BAD_ACP_SEID;
1687 stream = sep->stream;
1689 if (sep->state != AVDTP_STATE_STREAMING) {
1690 err = AVDTP_BAD_STATE;
1694 if (sep->ind && sep->ind->suspend) {
1695 if (!sep->ind->suspend(session, sep, stream, &err,
1700 avdtp_check_collision(session, AVDTP_SUSPEND, stream);
1702 avdtp_sep_set_state(session, sep, AVDTP_STATE_OPEN);
1705 return avdtp_send(session, transaction, AVDTP_MSG_TYPE_ACCEPT,
1706 AVDTP_SUSPEND, NULL, 0);
1709 memset(&rej, 0, sizeof(rej));
1710 rej.acp_seid = failed_seid;
1712 return avdtp_send(session, transaction, AVDTP_MSG_TYPE_REJECT,
1713 AVDTP_SUSPEND, &rej, sizeof(rej));
1716 static gboolean avdtp_abort_cmd(struct avdtp *session, uint8_t transaction,
1717 struct seid_req *req, unsigned int size)
1719 struct avdtp_local_sep *sep;
1723 if (size < sizeof(struct seid_req)) {
1724 error("Too short abort request");
1728 sep = find_local_sep_by_seid(session, req->acp_seid);
1729 if (!sep || !sep->stream)
1732 if (sep->ind && sep->ind->abort)
1733 sep->ind->abort(session, sep, sep->stream, &err,
1736 avdtp_check_collision(session, AVDTP_ABORT, sep->stream);
1738 ret = avdtp_send(session, transaction, AVDTP_MSG_TYPE_ACCEPT,
1739 AVDTP_ABORT, NULL, 0);
1741 avdtp_sep_set_state(session, sep, AVDTP_STATE_ABORTING);
1746 static gboolean avdtp_secctl_cmd(struct avdtp *session, uint8_t transaction,
1747 struct seid_req *req, int size)
1749 return avdtp_unknown_cmd(session, transaction, AVDTP_SECURITY_CONTROL);
1752 static gboolean avdtp_delayreport_cmd(struct avdtp *session,
1753 uint8_t transaction,
1754 struct delay_req *req,
1757 struct avdtp_local_sep *sep;
1758 struct avdtp_stream *stream;
1761 if (size < sizeof(struct delay_req)) {
1762 error("Too short delay report request");
1766 sep = find_local_sep_by_seid(session, req->acp_seid);
1767 if (!sep || !sep->stream) {
1768 err = AVDTP_BAD_ACP_SEID;
1772 stream = sep->stream;
1774 switch (sep->state) {
1775 case AVDTP_STATE_IDLE:
1776 case AVDTP_STATE_ABORTING:
1777 case AVDTP_STATE_CLOSING:
1778 err = AVDTP_BAD_STATE;
1780 case AVDTP_STATE_CONFIGURED:
1781 case AVDTP_STATE_OPEN:
1782 case AVDTP_STATE_STREAMING:
1787 stream->delay = ntohs(req->delay);
1789 if (sep->ind && sep->ind->delayreport) {
1790 if (!sep->ind->delayreport(session, sep, stream->rseid,
1791 stream->delay, &err,
1796 return avdtp_send(session, transaction, AVDTP_MSG_TYPE_ACCEPT,
1797 AVDTP_DELAY_REPORT, NULL, 0);
1800 return avdtp_send(session, transaction, AVDTP_MSG_TYPE_REJECT,
1801 AVDTP_DELAY_REPORT, &err, sizeof(err));
1804 static gboolean avdtp_parse_cmd(struct avdtp *session, uint8_t transaction,
1805 uint8_t signal_id, void *buf, int size)
1807 switch (signal_id) {
1808 case AVDTP_DISCOVER:
1809 DBG("Received DISCOVER_CMD");
1810 return avdtp_discover_cmd(session, transaction, buf, size);
1811 case AVDTP_GET_CAPABILITIES:
1812 DBG("Received GET_CAPABILITIES_CMD");
1813 return avdtp_getcap_cmd(session, transaction, buf, size,
1815 case AVDTP_GET_ALL_CAPABILITIES:
1816 DBG("Received GET_ALL_CAPABILITIES_CMD");
1817 return avdtp_getcap_cmd(session, transaction, buf, size, TRUE);
1818 case AVDTP_SET_CONFIGURATION:
1819 DBG("Received SET_CONFIGURATION_CMD");
1820 return avdtp_setconf_cmd(session, transaction, buf, size);
1821 case AVDTP_GET_CONFIGURATION:
1822 DBG("Received GET_CONFIGURATION_CMD");
1823 return avdtp_getconf_cmd(session, transaction, buf, size);
1824 case AVDTP_RECONFIGURE:
1825 DBG("Received RECONFIGURE_CMD");
1826 return avdtp_reconf_cmd(session, transaction, buf, size);
1828 DBG("Received OPEN_CMD");
1829 return avdtp_open_cmd(session, transaction, buf, size);
1831 DBG("Received START_CMD");
1832 return avdtp_start_cmd(session, transaction, buf, size);
1834 DBG("Received CLOSE_CMD");
1835 return avdtp_close_cmd(session, transaction, buf, size);
1837 DBG("Received SUSPEND_CMD");
1838 return avdtp_suspend_cmd(session, transaction, buf, size);
1840 DBG("Received ABORT_CMD");
1841 return avdtp_abort_cmd(session, transaction, buf, size);
1842 case AVDTP_SECURITY_CONTROL:
1843 DBG("Received SECURITY_CONTROL_CMD");
1844 return avdtp_secctl_cmd(session, transaction, buf, size);
1845 case AVDTP_DELAY_REPORT:
1846 DBG("Received DELAY_REPORT_CMD");
1847 return avdtp_delayreport_cmd(session, transaction, buf, size);
1849 DBG("Received unknown request id %u", signal_id);
1850 return avdtp_unknown_cmd(session, transaction, signal_id);
1854 enum avdtp_parse_result { PARSE_ERROR, PARSE_FRAGMENT, PARSE_SUCCESS };
1856 static enum avdtp_parse_result avdtp_parse_data(struct avdtp *session,
1857 void *buf, size_t size)
1859 struct avdtp_common_header *header = buf;
1860 struct avdtp_single_header *single = (void *) session->buf;
1861 struct avdtp_start_header *start = (void *) session->buf;
1865 switch (header->packet_type) {
1866 case AVDTP_PKT_TYPE_SINGLE:
1867 if (size < sizeof(*single)) {
1868 error("Received too small single packet (%zu bytes)",
1872 if (session->in.active) {
1873 error("SINGLE: Invalid AVDTP packet fragmentation");
1877 payload = session->buf + sizeof(*single);
1878 payload_size = size - sizeof(*single);
1880 session->in.active = TRUE;
1881 session->in.data_size = 0;
1882 session->in.no_of_packets = 1;
1883 session->in.transaction = header->transaction;
1884 session->in.message_type = header->message_type;
1885 session->in.signal_id = single->signal_id;
1888 case AVDTP_PKT_TYPE_START:
1889 if (size < sizeof(*start)) {
1890 error("Received too small start packet (%zu bytes)",
1894 if (session->in.active) {
1895 error("START: Invalid AVDTP packet fragmentation");
1899 session->in.active = TRUE;
1900 session->in.data_size = 0;
1901 session->in.transaction = header->transaction;
1902 session->in.message_type = header->message_type;
1903 session->in.no_of_packets = start->no_of_packets;
1904 session->in.signal_id = start->signal_id;
1906 payload = session->buf + sizeof(*start);
1907 payload_size = size - sizeof(*start);
1910 case AVDTP_PKT_TYPE_CONTINUE:
1911 if (size < sizeof(struct avdtp_continue_header)) {
1912 error("Received too small continue packet (%zu bytes)",
1916 if (!session->in.active) {
1917 error("CONTINUE: Invalid AVDTP packet fragmentation");
1920 if (session->in.transaction != header->transaction) {
1921 error("Continue transaction id doesn't match");
1924 if (session->in.no_of_packets <= 1) {
1925 error("Too few continue packets");
1929 payload = session->buf + sizeof(struct avdtp_continue_header);
1930 payload_size = size - sizeof(struct avdtp_continue_header);
1933 case AVDTP_PKT_TYPE_END:
1934 if (size < sizeof(struct avdtp_continue_header)) {
1935 error("Received too small end packet (%zu bytes)",
1939 if (!session->in.active) {
1940 error("END: Invalid AVDTP packet fragmentation");
1943 if (session->in.transaction != header->transaction) {
1944 error("End transaction id doesn't match");
1947 if (session->in.no_of_packets > 1) {
1948 error("Got an end packet too early");
1952 payload = session->buf + sizeof(struct avdtp_continue_header);
1953 payload_size = size - sizeof(struct avdtp_continue_header);
1957 error("Invalid AVDTP packet type 0x%02X", header->packet_type);
1961 if (session->in.data_size + payload_size >
1962 sizeof(session->in.buf)) {
1963 error("Not enough incoming buffer space!");
1967 memcpy(session->in.buf + session->in.data_size, payload, payload_size);
1968 session->in.data_size += payload_size;
1970 if (session->in.no_of_packets > 1) {
1971 session->in.no_of_packets--;
1972 DBG("Received AVDTP fragment. %d to go",
1973 session->in.no_of_packets);
1974 return PARSE_FRAGMENT;
1977 session->in.active = FALSE;
1979 return PARSE_SUCCESS;
1982 static gboolean session_cb(GIOChannel *chan, GIOCondition cond,
1985 struct avdtp *session = data;
1986 struct avdtp_common_header *header;
1992 if (cond & G_IO_NVAL) {
1998 header = (void *) session->buf;
2000 if (cond & (G_IO_HUP | G_IO_ERR))
2003 fd = g_io_channel_unix_get_fd(chan);
2004 size = read(fd, session->buf, session->imtu);
2006 error("IO Channel read error");
2010 if ((size_t) size < sizeof(struct avdtp_common_header)) {
2011 error("Received too small packet (%zu bytes)", size);
2015 switch (avdtp_parse_data(session, session->buf, size)) {
2018 case PARSE_FRAGMENT:
2024 /* Take a reference to protect against callback destroying session */
2027 if (session->in.message_type == AVDTP_MSG_TYPE_COMMAND) {
2028 if (!avdtp_parse_cmd(session, session->in.transaction,
2029 session->in.signal_id,
2031 session->in.data_size)) {
2032 error("Unable to handle command. Disconnecting");
2036 if (session->req && session->req->collided) {
2037 DBG("Collision detected");
2041 avdtp_unref(session);
2045 if (session->req == NULL) {
2046 error("No pending request, ignoring message");
2047 avdtp_unref(session);
2051 if (header->transaction != session->req->transaction) {
2052 error("Transaction label doesn't match");
2053 avdtp_unref(session);
2057 if (session->in.signal_id != session->req->signal_id) {
2058 error("Response signal doesn't match");
2059 avdtp_unref(session);
2063 g_source_remove(session->req->timeout);
2064 session->req->timeout = 0;
2066 switch (header->message_type) {
2067 case AVDTP_MSG_TYPE_ACCEPT:
2068 if (!avdtp_parse_resp(session, session->req->stream,
2069 session->in.transaction,
2070 session->in.signal_id,
2072 session->in.data_size)) {
2073 error("Unable to parse accept response");
2077 case AVDTP_MSG_TYPE_REJECT:
2078 if (!avdtp_parse_rej(session, session->req->stream,
2079 session->in.transaction,
2080 session->in.signal_id,
2082 session->in.data_size)) {
2083 error("Unable to parse reject response");
2087 case AVDTP_MSG_TYPE_GEN_REJECT:
2088 error("Received a General Reject message");
2091 error("Unknown message type 0x%02X", header->message_type);
2096 pending_req_free(session->req);
2097 session->req = NULL;
2099 if (session->ref > 1)
2100 process_queue(session);
2102 avdtp_unref(session);
2108 connection_lost(session, EIO);
2113 static int set_priority(int fd, int priority)
2117 err = setsockopt(fd, SOL_SOCKET, SO_PRIORITY, &priority,
2119 if (err == 0 || errno == ENOTSOCK)
2123 error("setsockopt(SO_PRIORITY): %s (%d)", strerror(-err), -err);
2128 struct avdtp *avdtp_new(int fd, size_t imtu, size_t omtu, uint16_t version,
2129 struct queue *lseps)
2131 struct avdtp *session;
2132 GIOCondition cond = G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL;
2140 error("dup(): %s (%d)", strerror(errno), errno);
2144 if (set_priority(new_fd, 6) < 0)
2147 session = g_new0(struct avdtp, 1);
2148 session->io = g_io_channel_unix_new(new_fd);
2149 session->version = version;
2150 session->imtu = imtu;
2151 session->omtu = omtu;
2152 session->buf = g_malloc0(MAX(session->imtu, session->omtu));
2154 /* This watch should be low priority since otherwise the
2155 * connect callback might be dispatched before the session
2156 * callback if the kernel wakes us up at the same time for
2157 * them. This could happen if a headset is very quick in
2158 * sending the Start command after connecting the stream
2159 * transport channel.
2161 session->io_id = g_io_add_watch_full(session->io, G_PRIORITY_LOW, cond,
2162 (GIOFunc) session_cb, session,
2165 session->lseps = lseps;
2167 return avdtp_ref(session);
2170 unsigned int avdtp_add_disconnect_cb(struct avdtp *session,
2171 avdtp_disconnect_cb_t cb,
2174 struct disconnect_callback *callback;
2175 static unsigned int id = 0;
2177 callback = g_new0(struct disconnect_callback, 1);
2178 callback->id = ++id;
2180 callback->user_data = user_data;
2181 session->disconnect = g_slist_append(session->disconnect, callback);
2186 gboolean avdtp_remove_disconnect_cb(struct avdtp *session, unsigned int id)
2190 for (l = session->disconnect; l; l = g_slist_next(l)) {
2191 struct disconnect_callback *callback = l->data;
2193 if (callback->id != id)
2196 session->disconnect = g_slist_remove(session->disconnect,
2205 void avdtp_shutdown(struct avdtp *session)
2208 bool aborting = false;
2213 for (l = session->streams; l; l = g_slist_next(l)) {
2214 struct avdtp_stream *stream = l->data;
2216 if (stream->abort_int ||
2217 avdtp_close(session, stream, TRUE) == 0)
2222 /* defer shutdown until all streams are aborted properly */
2223 session->shutdown = true;
2225 int sock = g_io_channel_unix_get_fd(session->io);
2227 shutdown(sock, SHUT_RDWR);
2231 static void queue_request(struct avdtp *session, struct pending_req *req,
2235 session->prio_queue = g_slist_append(session->prio_queue, req);
2237 session->req_queue = g_slist_append(session->req_queue, req);
2240 static uint8_t req_get_seid(struct pending_req *req)
2242 if (req->signal_id == AVDTP_DISCOVER)
2245 return ((struct seid_req *) (req->data))->acp_seid;
2248 static int cancel_request(struct avdtp *session, int err)
2250 struct pending_req *req;
2251 struct seid_req sreq;
2252 struct avdtp_local_sep *lsep;
2253 struct avdtp_stream *stream;
2255 struct avdtp_error averr;
2258 session->req = NULL;
2260 avdtp_error_init(&averr, AVDTP_ERRNO, err);
2262 seid = req_get_seid(req);
2264 stream = find_stream_by_rseid(session, seid);
2269 stream->abort_int = TRUE;
2270 lsep = stream->lsep;
2274 switch (req->signal_id) {
2275 case AVDTP_RECONFIGURE:
2276 error("Reconfigure: %s (%d)", strerror(err), err);
2277 if (lsep && lsep->cfm && lsep->cfm->reconfigure)
2278 lsep->cfm->reconfigure(session, lsep, stream, &averr,
2282 error("Open: %s (%d)", strerror(err), err);
2283 if (lsep && lsep->cfm && lsep->cfm->open)
2284 lsep->cfm->open(session, lsep, stream, &averr,
2288 error("Start: %s (%d)", strerror(err), err);
2289 if (lsep && lsep->cfm && lsep->cfm->start) {
2290 lsep->cfm->start(session, lsep, stream, &averr,
2293 stream->starting = FALSE;
2297 error("Suspend: %s (%d)", strerror(err), err);
2298 if (lsep && lsep->cfm && lsep->cfm->suspend)
2299 lsep->cfm->suspend(session, lsep, stream, &averr,
2303 error("Close: %s (%d)", strerror(err), err);
2304 if (lsep && lsep->cfm && lsep->cfm->close) {
2305 lsep->cfm->close(session, lsep, stream, &averr,
2308 stream->close_int = FALSE;
2311 case AVDTP_SET_CONFIGURATION:
2312 error("SetConfiguration: %s (%d)", strerror(err), err);
2313 if (lsep && lsep->cfm && lsep->cfm->set_configuration)
2314 lsep->cfm->set_configuration(session, lsep, stream,
2315 &averr, lsep->user_data);
2317 case AVDTP_DISCOVER:
2318 error("Discover: %s (%d)", strerror(err), err);
2320 case AVDTP_GET_CAPABILITIES:
2321 error("GetCapabilities: %s (%d)", strerror(err), err);
2324 error("Abort: %s (%d)", strerror(err), err);
2331 memset(&sreq, 0, sizeof(sreq));
2332 sreq.acp_seid = seid;
2334 err = send_request(session, TRUE, stream, AVDTP_ABORT, &sreq,
2337 error("Unable to send abort request");
2344 connection_lost(session, err);
2346 pending_req_free(req);
2350 static gboolean request_timeout(gpointer user_data)
2352 struct avdtp *session = user_data;
2354 cancel_request(session, ETIMEDOUT);
2359 static int send_req(struct avdtp *session, gboolean priority,
2360 struct pending_req *req)
2362 static int transaction = 0;
2365 if (session->req != NULL) {
2366 queue_request(session, req, priority);
2370 req->transaction = transaction++;
2373 /* FIXME: Should we retry to send if the buffer
2374 was not totally sent or in case of EINTR? */
2375 if (!avdtp_send(session, req->transaction, AVDTP_MSG_TYPE_COMMAND,
2376 req->signal_id, req->data, req->data_size)) {
2383 req->timeout = g_timeout_add_seconds(req->signal_id == AVDTP_ABORT ?
2384 ABORT_TIMEOUT : REQ_TIMEOUT,
2395 static int send_request(struct avdtp *session, gboolean priority,
2396 struct avdtp_stream *stream, uint8_t signal_id,
2397 void *buffer, size_t size)
2399 struct pending_req *req;
2401 if (size > 0 && !buffer) {
2402 DBG("Invalid buffer %p", buffer);
2406 if (stream && stream->abort_int && signal_id != AVDTP_ABORT) {
2407 DBG("Unable to send requests while aborting");
2411 req = g_new0(struct pending_req, 1);
2412 req->signal_id = signal_id;
2413 req->data_size = size;
2414 req->stream = stream;
2417 req->data = g_malloc(size);
2418 memcpy(req->data, buffer, size);
2421 return send_req(session, priority, req);
2424 static gboolean avdtp_discover_resp(struct avdtp *session,
2425 struct discover_resp *resp, int size)
2430 gboolean getcap_pending = FALSE;
2432 if (session->version >= 0x0103)
2433 getcap_cmd = AVDTP_GET_ALL_CAPABILITIES;
2435 getcap_cmd = AVDTP_GET_CAPABILITIES;
2437 sep_count = size / sizeof(struct seid_info);
2439 for (i = 0; i < sep_count; i++) {
2440 struct avdtp_remote_sep *sep;
2441 struct avdtp_stream *stream;
2442 struct seid_req req;
2444 DBG("seid %d type %d media %d in use %d",
2445 resp->seps[i].seid, resp->seps[i].type,
2446 resp->seps[i].media_type, resp->seps[i].inuse);
2448 stream = find_stream_by_rseid(session, resp->seps[i].seid);
2450 sep = find_remote_sep(session->seps, resp->seps[i].seid);
2452 if (resp->seps[i].inuse && !stream)
2454 sep = g_new0(struct avdtp_remote_sep, 1);
2455 session->seps = g_slist_append(session->seps, sep);
2458 sep->stream = stream;
2459 sep->seid = resp->seps[i].seid;
2460 sep->type = resp->seps[i].type;
2461 sep->media_type = resp->seps[i].media_type;
2463 memset(&req, 0, sizeof(req));
2464 req.acp_seid = sep->seid;
2466 ret = send_request(session, TRUE, NULL, getcap_cmd,
2470 getcap_pending = TRUE;
2473 if (!getcap_pending)
2474 finalize_discovery(session, -ret);
2479 static gboolean avdtp_get_capabilities_resp(struct avdtp *session,
2480 struct getcap_resp *resp,
2483 struct avdtp_remote_sep *sep;
2486 /* Check for minimum required packet size includes:
2487 * 1. getcap resp header
2488 * 2. media transport capability (2 bytes)
2489 * 3. media codec capability type + length (2 bytes)
2490 * 4. the actual media codec elements
2492 if (size < (sizeof(struct getcap_resp) + 4 +
2493 sizeof(struct avdtp_media_codec_capability))) {
2494 error("Too short getcap resp packet");
2498 seid = ((struct seid_req *) session->req->data)->acp_seid;
2500 sep = find_remote_sep(session->seps, seid);
2502 DBG("seid %d type %d media %d", sep->seid,
2503 sep->type, sep->media_type);
2506 g_slist_free_full(sep->caps, g_free);
2509 sep->delay_reporting = FALSE;
2512 sep->caps = caps_to_list(resp->caps, size - sizeof(struct getcap_resp),
2513 &sep->codec, &sep->delay_reporting);
2518 static gboolean avdtp_set_configuration_resp(struct avdtp *session,
2519 struct avdtp_stream *stream,
2520 struct avdtp_single_header *resp,
2523 struct avdtp_local_sep *sep = stream->lsep;
2525 avdtp_sep_set_state(session, sep, AVDTP_STATE_CONFIGURED);
2527 if (sep->cfm && sep->cfm->set_configuration)
2528 sep->cfm->set_configuration(session, sep, stream, NULL,
2534 static gboolean avdtp_reconfigure_resp(struct avdtp *session,
2535 struct avdtp_stream *stream,
2536 struct avdtp_single_header *resp,
2542 static gboolean avdtp_open_resp(struct avdtp *session,
2543 struct avdtp_stream *stream,
2544 struct seid_rej *resp, int size)
2546 struct avdtp_local_sep *sep = stream->lsep;
2548 session->pending_open = stream;
2550 if (!stream->open_acp && sep->cfm && sep->cfm->open)
2551 sep->cfm->open(session, sep, stream, NULL, sep->user_data);
2556 static gboolean avdtp_start_resp(struct avdtp *session,
2557 struct avdtp_stream *stream,
2558 struct seid_rej *resp, int size)
2560 struct avdtp_local_sep *sep = stream->lsep;
2562 /* We might be in STREAMING already if both sides send START_CMD at the
2563 * same time and the one in SNK role doesn't reject it as it should */
2564 if (sep->state != AVDTP_STATE_STREAMING)
2565 avdtp_sep_set_state(session, sep, AVDTP_STATE_STREAMING);
2567 if (sep->cfm && sep->cfm->start)
2568 sep->cfm->start(session, sep, stream, NULL, sep->user_data);
2573 static gboolean avdtp_close_resp(struct avdtp *session,
2574 struct avdtp_stream *stream,
2575 struct seid_rej *resp, int size)
2577 struct avdtp_local_sep *sep = stream->lsep;
2579 avdtp_sep_set_state(session, sep, AVDTP_STATE_CLOSING);
2581 close_stream(stream);
2586 static gboolean avdtp_suspend_resp(struct avdtp *session,
2587 struct avdtp_stream *stream,
2588 void *data, int size)
2590 struct avdtp_local_sep *sep = stream->lsep;
2592 avdtp_sep_set_state(session, sep, AVDTP_STATE_OPEN);
2594 if (sep->cfm && sep->cfm->suspend)
2595 sep->cfm->suspend(session, sep, stream, NULL, sep->user_data);
2600 static gboolean avdtp_abort_resp(struct avdtp *session,
2601 struct avdtp_stream *stream,
2602 struct seid_rej *resp, int size)
2604 struct avdtp_local_sep *sep = stream->lsep;
2606 avdtp_sep_set_state(session, sep, AVDTP_STATE_ABORTING);
2608 if (sep->cfm && sep->cfm->abort)
2609 sep->cfm->abort(session, sep, stream, NULL, sep->user_data);
2611 avdtp_sep_set_state(session, sep, AVDTP_STATE_IDLE);
2616 static gboolean avdtp_delay_report_resp(struct avdtp *session,
2617 struct avdtp_stream *stream,
2618 void *data, int size)
2620 struct avdtp_local_sep *sep = stream->lsep;
2622 if (sep->cfm && sep->cfm->delay_report)
2623 sep->cfm->delay_report(session, sep, stream, NULL,
2629 static gboolean avdtp_parse_resp(struct avdtp *session,
2630 struct avdtp_stream *stream,
2631 uint8_t transaction, uint8_t signal_id,
2632 void *buf, int size)
2634 struct pending_req *next;
2635 const char *get_all = "";
2637 if (session->prio_queue)
2638 next = session->prio_queue->data;
2639 else if (session->req_queue)
2640 next = session->req_queue->data;
2644 switch (signal_id) {
2645 case AVDTP_DISCOVER:
2646 DBG("DISCOVER request succeeded");
2647 return avdtp_discover_resp(session, buf, size);
2648 case AVDTP_GET_ALL_CAPABILITIES:
2650 case AVDTP_GET_CAPABILITIES:
2651 DBG("GET_%sCAPABILITIES request succeeded", get_all);
2652 if (!avdtp_get_capabilities_resp(session, buf, size))
2654 if (!(next && (next->signal_id == AVDTP_GET_CAPABILITIES ||
2655 next->signal_id == AVDTP_GET_ALL_CAPABILITIES)))
2656 finalize_discovery(session, 0);
2660 /* The remaining commands require an existing stream so bail out
2661 * here if the stream got unexpectedly disconnected */
2663 DBG("AVDTP: stream was closed while waiting for reply");
2667 switch (signal_id) {
2668 case AVDTP_SET_CONFIGURATION:
2669 DBG("SET_CONFIGURATION request succeeded");
2670 return avdtp_set_configuration_resp(session, stream,
2672 case AVDTP_RECONFIGURE:
2673 DBG("RECONFIGURE request succeeded");
2674 return avdtp_reconfigure_resp(session, stream, buf, size);
2676 DBG("OPEN request succeeded");
2677 return avdtp_open_resp(session, stream, buf, size);
2679 DBG("SUSPEND request succeeded");
2680 return avdtp_suspend_resp(session, stream, buf, size);
2682 DBG("START request succeeded");
2683 return avdtp_start_resp(session, stream, buf, size);
2685 DBG("CLOSE request succeeded");
2686 return avdtp_close_resp(session, stream, buf, size);
2688 DBG("ABORT request succeeded");
2689 return avdtp_abort_resp(session, stream, buf, size);
2690 case AVDTP_DELAY_REPORT:
2691 DBG("DELAY_REPORT request succeeded");
2692 return avdtp_delay_report_resp(session, stream, buf, size);
2695 error("Unknown signal id in accept response: %u", signal_id);
2699 static gboolean seid_rej_to_err(struct seid_rej *rej, unsigned int size,
2700 struct avdtp_error *err)
2702 if (size < sizeof(struct seid_rej)) {
2703 error("Too small packet for seid_rej");
2707 avdtp_error_init(err, 0x00, rej->error);
2712 static gboolean conf_rej_to_err(struct conf_rej *rej, unsigned int size,
2713 struct avdtp_error *err)
2715 if (size < sizeof(struct conf_rej)) {
2716 error("Too small packet for conf_rej");
2720 avdtp_error_init(err, rej->category, rej->error);
2725 static gboolean stream_rej_to_err(struct stream_rej *rej, unsigned int size,
2726 struct avdtp_error *err,
2729 if (size < sizeof(struct stream_rej)) {
2730 error("Too small packet for stream_rej");
2734 avdtp_error_init(err, 0x00, rej->error);
2737 *acp_seid = rej->acp_seid;
2742 static gboolean avdtp_parse_rej(struct avdtp *session,
2743 struct avdtp_stream *stream,
2744 uint8_t transaction, uint8_t signal_id,
2745 void *buf, int size)
2747 struct avdtp_error err;
2749 struct avdtp_local_sep *sep = stream ? stream->lsep : NULL;
2751 switch (signal_id) {
2752 case AVDTP_DISCOVER:
2753 case AVDTP_GET_CAPABILITIES:
2754 case AVDTP_GET_ALL_CAPABILITIES:
2755 if (!seid_rej_to_err(buf, size, &err))
2757 error("%s request rejected: %s (%d)",
2758 signal_id == AVDTP_DISCOVER ? "DISCOVER" :
2759 signal_id == AVDTP_GET_CAPABILITIES ?
2760 "GET_CAPABILITIES" : "GET_ALL_CAPABILITIES",
2761 avdtp_strerror(&err), err.err.error_code);
2762 if (session->discover) {
2763 session->discover->cb(session, session->seps, &err,
2764 session->discover->user_data);
2765 g_free(session->discover);
2766 session->discover = NULL;
2770 if (!seid_rej_to_err(buf, size, &err))
2772 error("OPEN request rejected: %s (%d)",
2773 avdtp_strerror(&err), err.err.error_code);
2774 if (sep && sep->cfm && sep->cfm->open)
2775 sep->cfm->open(session, sep, stream, &err,
2778 case AVDTP_SET_CONFIGURATION:
2779 if (!conf_rej_to_err(buf, size, &err))
2781 error("SET_CONFIGURATION request rejected: %s (%d)",
2782 avdtp_strerror(&err), err.err.error_code);
2783 if (sep && sep->cfm && sep->cfm->set_configuration)
2784 sep->cfm->set_configuration(session, sep, stream,
2785 &err, sep->user_data);
2787 case AVDTP_GET_CONFIGURATION:
2788 if (!seid_rej_to_err(buf, size, &err))
2790 error("GET_CONFIGURATION request rejected: %s (%d)",
2791 avdtp_strerror(&err), err.err.error_code);
2792 if (sep && sep->cfm && sep->cfm->get_configuration)
2793 sep->cfm->get_configuration(session, sep, stream, &err,
2796 case AVDTP_RECONFIGURE:
2797 if (!conf_rej_to_err(buf, size, &err))
2799 error("RECONFIGURE request rejected: %s (%d)",
2800 avdtp_strerror(&err), err.err.error_code);
2801 if (sep && sep->cfm && sep->cfm->reconfigure)
2802 sep->cfm->reconfigure(session, sep, stream, &err,
2806 if (!stream_rej_to_err(buf, size, &err, &acp_seid))
2808 error("START request rejected: %s (%d)",
2809 avdtp_strerror(&err), err.err.error_code);
2810 if (sep && sep->cfm && sep->cfm->start) {
2811 stream->starting = FALSE;
2812 sep->cfm->start(session, sep, stream, &err,
2817 if (!stream_rej_to_err(buf, size, &err, &acp_seid))
2819 error("SUSPEND request rejected: %s (%d)",
2820 avdtp_strerror(&err), err.err.error_code);
2821 if (sep && sep->cfm && sep->cfm->suspend)
2822 sep->cfm->suspend(session, sep, stream, &err,
2826 if (!stream_rej_to_err(buf, size, &err, &acp_seid))
2828 error("CLOSE request rejected: %s (%d)",
2829 avdtp_strerror(&err), err.err.error_code);
2830 if (sep && sep->cfm && sep->cfm->close) {
2831 sep->cfm->close(session, sep, stream, &err,
2833 stream->close_int = FALSE;
2837 if (!stream_rej_to_err(buf, size, &err, &acp_seid))
2839 error("ABORT request rejected: %s (%d)",
2840 avdtp_strerror(&err), err.err.error_code);
2841 if (sep && sep->cfm && sep->cfm->abort)
2842 sep->cfm->abort(session, sep, stream, &err,
2845 case AVDTP_DELAY_REPORT:
2846 if (!stream_rej_to_err(buf, size, &err, &acp_seid))
2848 error("DELAY_REPORT request rejected: %s (%d)",
2849 avdtp_strerror(&err), err.err.error_code);
2850 if (sep && sep->cfm && sep->cfm->delay_report)
2851 sep->cfm->delay_report(session, sep, stream, &err,
2855 error("Unknown reject response signal id: %u", signal_id);
2860 struct avdtp_service_capability *avdtp_stream_get_codec(
2861 struct avdtp_stream *stream)
2865 for (l = stream->caps; l; l = l->next) {
2866 struct avdtp_service_capability *cap = l->data;
2868 if (cap->category == AVDTP_MEDIA_CODEC)
2875 static gboolean avdtp_stream_has_capability(struct avdtp_stream *stream,
2876 struct avdtp_service_capability *cap)
2879 struct avdtp_service_capability *stream_cap;
2881 for (l = stream->caps; l; l = g_slist_next(l)) {
2882 stream_cap = l->data;
2884 if (stream_cap->category != cap->category ||
2885 stream_cap->length != cap->length)
2888 if (memcmp(stream_cap->data, cap->data, cap->length) == 0)
2895 gboolean avdtp_stream_has_capabilities(struct avdtp_stream *stream,
2898 for (; caps; caps = g_slist_next(caps)) {
2899 struct avdtp_service_capability *cap = caps->data;
2901 if (!avdtp_stream_has_capability(stream, cap))
2908 struct avdtp_remote_sep *avdtp_stream_get_remote_sep(
2909 struct avdtp_stream *stream)
2913 for (l = stream->session->seps; l; l = l->next) {
2914 struct avdtp_remote_sep *sep = l->data;
2916 if (sep->seid == stream->rseid)
2923 gboolean avdtp_stream_set_transport(struct avdtp_stream *stream, int fd,
2924 size_t imtu, size_t omtu)
2928 if (stream != stream->session->pending_open)
2931 if (set_priority(fd, 5) < 0)
2934 io = g_io_channel_unix_new(fd);
2936 handle_transport_connect(stream->session, io, imtu, omtu);
2938 g_io_channel_unref(io);
2943 gboolean avdtp_stream_get_transport(struct avdtp_stream *stream, int *sock,
2944 uint16_t *imtu, uint16_t *omtu,
2947 if (stream->io == NULL)
2951 *sock = g_io_channel_unix_get_fd(stream->io);
2954 *omtu = stream->omtu;
2957 *imtu = stream->imtu;
2960 *caps = stream->caps;
2965 static int process_queue(struct avdtp *session)
2968 struct pending_req *req;
2973 if (session->prio_queue)
2974 queue = &session->prio_queue;
2976 queue = &session->req_queue;
2984 *queue = g_slist_remove(*queue, req);
2986 return send_req(session, FALSE, req);
2989 struct avdtp_service_capability *avdtp_get_codec(struct avdtp_remote_sep *sep)
2994 struct avdtp_service_capability *avdtp_service_cap_new(uint8_t category,
2998 struct avdtp_service_capability *cap;
3000 if (category < AVDTP_MEDIA_TRANSPORT ||
3001 category > AVDTP_DELAY_REPORTING)
3004 if (length > 0 && !data)
3007 cap = g_malloc(sizeof(struct avdtp_service_capability) + length);
3008 cap->category = category;
3009 cap->length = length;
3012 memcpy(cap->data, data, length);
3017 static gboolean process_discover(gpointer data)
3019 struct avdtp *session = data;
3021 session->discover->id = 0;
3023 finalize_discovery(session, 0);
3028 int avdtp_discover(struct avdtp *session, avdtp_discover_cb_t cb,
3033 if (session->discover)
3036 session->discover = g_new0(struct discover_callback, 1);
3038 if (session->seps) {
3039 session->discover->cb = cb;
3040 session->discover->user_data = user_data;
3041 session->discover->id = g_idle_add(process_discover, session);
3045 err = send_request(session, FALSE, NULL, AVDTP_DISCOVER, NULL, 0);
3047 session->discover->cb = cb;
3048 session->discover->user_data = user_data;
3054 gboolean avdtp_stream_remove_cb(struct avdtp *session,
3055 struct avdtp_stream *stream,
3059 struct stream_callback *cb;
3064 for (cb = NULL, l = stream->callbacks; l != NULL; l = l->next) {
3065 struct stream_callback *tmp = l->data;
3066 if (tmp && tmp->id == id) {
3075 stream->callbacks = g_slist_remove(stream->callbacks, cb);
3081 unsigned int avdtp_stream_add_cb(struct avdtp *session,
3082 struct avdtp_stream *stream,
3083 avdtp_stream_state_cb cb, void *data)
3085 struct stream_callback *stream_cb;
3086 static unsigned int id = 0;
3088 stream_cb = g_new(struct stream_callback, 1);
3090 stream_cb->user_data = data;
3091 stream_cb->id = ++id;
3093 stream->callbacks = g_slist_append(stream->callbacks, stream_cb);
3095 return stream_cb->id;
3098 int avdtp_get_configuration(struct avdtp *session, struct avdtp_stream *stream)
3100 struct seid_req req;
3102 memset(&req, 0, sizeof(req));
3103 req.acp_seid = stream->rseid;
3105 return send_request(session, FALSE, stream, AVDTP_GET_CONFIGURATION,
3109 static void copy_capabilities(gpointer data, gpointer user_data)
3111 struct avdtp_service_capability *src_cap = data;
3112 struct avdtp_service_capability *dst_cap;
3113 GSList **l = user_data;
3115 dst_cap = avdtp_service_cap_new(src_cap->category, src_cap->data,
3118 *l = g_slist_append(*l, dst_cap);
3121 int avdtp_set_configuration(struct avdtp *session,
3122 struct avdtp_remote_sep *rsep,
3123 struct avdtp_local_sep *lsep,
3125 struct avdtp_stream **stream)
3127 struct setconf_req *req;
3128 struct avdtp_stream *new_stream;
3131 struct avdtp_service_capability *cap;
3134 if (!(lsep && rsep))
3137 DBG("%p: int_seid=%u, acp_seid=%u", session,
3138 lsep->info.seid, rsep->seid);
3140 new_stream = g_new0(struct avdtp_stream, 1);
3141 new_stream->session = session;
3142 new_stream->lsep = lsep;
3143 new_stream->rseid = rsep->seid;
3145 if (rsep->delay_reporting && lsep->delay_reporting) {
3146 struct avdtp_service_capability *delay_reporting;
3148 delay_reporting = avdtp_service_cap_new(AVDTP_DELAY_REPORTING,
3150 caps = g_slist_append(caps, delay_reporting);
3151 new_stream->delay_reporting = TRUE;
3154 g_slist_foreach(caps, copy_capabilities, &new_stream->caps);
3156 /* Calculate total size of request */
3157 for (l = caps, caps_len = 0; l != NULL; l = g_slist_next(l)) {
3159 caps_len += cap->length + 2;
3162 req = g_malloc0(sizeof(struct setconf_req) + caps_len);
3164 req->int_seid = lsep->info.seid;
3165 req->acp_seid = rsep->seid;
3167 /* Copy the capabilities into the request */
3168 for (l = caps, ptr = req->caps; l != NULL; l = g_slist_next(l)) {
3170 memcpy(ptr, cap, cap->length + 2);
3171 ptr += cap->length + 2;
3174 err = send_request(session, FALSE, new_stream,
3175 AVDTP_SET_CONFIGURATION, req,
3176 sizeof(struct setconf_req) + caps_len);
3178 stream_free(new_stream);
3180 lsep->info.inuse = 1;
3181 lsep->stream = new_stream;
3182 rsep->stream = new_stream;
3183 session->streams = g_slist_append(session->streams, new_stream);
3185 *stream = new_stream;
3193 int avdtp_open(struct avdtp *session, struct avdtp_stream *stream)
3195 struct seid_req req;
3197 if (!g_slist_find(session->streams, stream))
3200 if (stream->lsep->state > AVDTP_STATE_CONFIGURED)
3203 memset(&req, 0, sizeof(req));
3204 req.acp_seid = stream->rseid;
3206 return send_request(session, FALSE, stream, AVDTP_OPEN,
3210 static gboolean start_timeout(gpointer user_data)
3212 struct avdtp_stream *stream = user_data;
3213 struct avdtp *session = stream->session;
3215 stream->open_acp = FALSE;
3217 if (avdtp_start(session, stream) < 0)
3218 error("wait_timeout: avdtp_start failed");
3220 stream->start_timer = 0;
3225 int avdtp_start(struct avdtp *session, struct avdtp_stream *stream)
3227 struct start_req req;
3230 if (!g_slist_find(session->streams, stream))
3233 if (stream->lsep->state != AVDTP_STATE_OPEN)
3236 /* Recommendation 12:
3237 * If the RD has configured and opened a stream it is also responsible
3238 * to start the streaming via GAVDP_START.
3240 if (stream->open_acp) {
3241 /* If timer already active wait it */
3242 if (stream->start_timer)
3245 stream->start_timer = g_timeout_add_seconds(START_TIMEOUT,
3251 if (stream->close_int == TRUE) {
3252 error("avdtp_start: rejecting start since close is initiated");
3256 if (stream->starting == TRUE) {
3257 DBG("stream already started");
3258 return -EINPROGRESS;
3261 memset(&req, 0, sizeof(req));
3262 req.first_seid.seid = stream->rseid;
3264 ret = send_request(session, FALSE, stream, AVDTP_START,
3267 stream->starting = TRUE;
3272 int avdtp_close(struct avdtp *session, struct avdtp_stream *stream,
3275 struct seid_req req;
3278 if (!g_slist_find(session->streams, stream))
3281 if (stream->close_int == TRUE) {
3282 error("avdtp_close: rejecting since close is already initiated");
3286 /* If stream is not yet in the OPEN state, let's use ABORT_CMD */
3287 if (stream->lsep->state < AVDTP_STATE_OPEN)
3288 return avdtp_abort(session, stream);
3290 if (immediate && session->req && stream == session->req->stream)
3291 return avdtp_abort(session, stream);
3293 memset(&req, 0, sizeof(req));
3294 req.acp_seid = stream->rseid;
3296 ret = send_request(session, FALSE, stream, AVDTP_CLOSE,
3299 stream->close_int = TRUE;
3304 int avdtp_suspend(struct avdtp *session, struct avdtp_stream *stream)
3306 struct seid_req req;
3308 if (!g_slist_find(session->streams, stream))
3311 if (stream->lsep->state <= AVDTP_STATE_OPEN || stream->close_int)
3314 memset(&req, 0, sizeof(req));
3315 req.acp_seid = stream->rseid;
3317 return send_request(session, FALSE, stream, AVDTP_SUSPEND,
3321 int avdtp_abort(struct avdtp *session, struct avdtp_stream *stream)
3323 struct seid_req req;
3326 if (!g_slist_find(session->streams, stream))
3329 if (stream->lsep->state == AVDTP_STATE_ABORTING)
3332 if (session->req && session->req->timeout > 0 &&
3333 stream == session->req->stream)
3334 return cancel_request(session, ECANCELED);
3336 memset(&req, 0, sizeof(req));
3337 req.acp_seid = stream->rseid;
3339 ret = send_request(session, TRUE, stream, AVDTP_ABORT,
3342 stream->abort_int = TRUE;
3347 int avdtp_delay_report(struct avdtp *session, struct avdtp_stream *stream,
3350 struct delay_req req;
3352 if (!g_slist_find(session->streams, stream))
3355 if (stream->lsep->state != AVDTP_STATE_CONFIGURED &&
3356 stream->lsep->state != AVDTP_STATE_STREAMING)
3359 if (!stream->delay_reporting || session->version < 0x0103)
3362 stream->delay = delay;
3364 memset(&req, 0, sizeof(req));
3365 req.acp_seid = stream->rseid;
3366 req.delay = htons(delay);
3368 return send_request(session, TRUE, stream, AVDTP_DELAY_REPORT,
3372 struct avdtp_local_sep *avdtp_register_sep(struct queue *lseps, uint8_t type,
3375 gboolean delay_reporting,
3376 struct avdtp_sep_ind *ind,
3377 struct avdtp_sep_cfm *cfm,
3380 struct avdtp_local_sep *sep;
3381 uint8_t seid = util_get_uid(&seids, MAX_SEID);
3386 sep = g_new0(struct avdtp_local_sep, 1);
3388 sep->state = AVDTP_STATE_IDLE;
3389 sep->info.seid = seid;
3390 sep->info.type = type;
3391 sep->info.media_type = media_type;
3392 sep->codec = codec_type;
3395 sep->user_data = user_data;
3396 sep->delay_reporting = delay_reporting;
3398 DBG("SEP %p registered: type:%d codec:%d seid:%d", sep,
3399 sep->info.type, sep->codec, sep->info.seid);
3401 if (!queue_push_tail(lseps, sep)) {
3409 void avdtp_sep_set_vendor_codec(struct avdtp_local_sep *sep, uint32_t vendor_id,
3412 sep->vndcodec_vendor = vendor_id;
3413 sep->vndcodec_codec = codec_id;
3416 int avdtp_unregister_sep(struct queue *lseps, struct avdtp_local_sep *sep)
3422 release_stream(sep->stream, sep->stream->session);
3424 DBG("SEP %p unregistered: type:%d codec:%d seid:%d", sep,
3425 sep->info.type, sep->codec, sep->info.seid);
3427 util_clear_uid(&seids, sep->info.seid);
3428 queue_remove(lseps, sep);
3434 const char *avdtp_strerror(struct avdtp_error *err)
3436 if (err->category == AVDTP_ERRNO)
3437 return strerror(err->err.posix_errno);
3439 switch (err->err.error_code) {
3440 case AVDTP_BAD_HEADER_FORMAT:
3441 return "Bad Header Format";
3442 case AVDTP_BAD_LENGTH:
3443 return "Bad Packet Length";
3444 case AVDTP_BAD_ACP_SEID:
3445 return "Bad Acceptor SEID";
3446 case AVDTP_SEP_IN_USE:
3447 return "Stream End Point in Use";
3448 case AVDTP_SEP_NOT_IN_USE:
3449 return "Stream End Point Not in Use";
3450 case AVDTP_BAD_SERV_CATEGORY:
3451 return "Bad Service Category";
3452 case AVDTP_BAD_PAYLOAD_FORMAT:
3453 return "Bad Payload format";
3454 case AVDTP_NOT_SUPPORTED_COMMAND:
3455 return "Command Not Supported";
3456 case AVDTP_INVALID_CAPABILITIES:
3457 return "Invalid Capabilities";
3458 case AVDTP_BAD_RECOVERY_TYPE:
3459 return "Bad Recovery Type";
3460 case AVDTP_BAD_MEDIA_TRANSPORT_FORMAT:
3461 return "Bad Media Transport Format";
3462 case AVDTP_BAD_RECOVERY_FORMAT:
3463 return "Bad Recovery Format";
3464 case AVDTP_BAD_ROHC_FORMAT:
3465 return "Bad Header Compression Format";
3466 case AVDTP_BAD_CP_FORMAT:
3467 return "Bad Content Protection Format";
3468 case AVDTP_BAD_MULTIPLEXING_FORMAT:
3469 return "Bad Multiplexing Format";
3470 case AVDTP_UNSUPPORTED_CONFIGURATION:
3471 return "Configuration not supported";
3472 case AVDTP_BAD_STATE:
3475 return "Unknown error";
3479 avdtp_state_t avdtp_sep_get_state(struct avdtp_local_sep *sep)
3484 gboolean avdtp_has_stream(struct avdtp *session, struct avdtp_stream *stream)
3486 return g_slist_find(session->streams, stream) ? TRUE : FALSE;