forked from edrosten/bluez
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavdtp.c
3484 lines (2796 loc) · 80.2 KB
/
avdtp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
*
* BlueZ - Bluetooth protocol stack for Linux
*
* Copyright (C) 2006-2010 Nokia Corporation
* Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <errno.h>
#include <unistd.h>
#include <assert.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <glib.h>
#include "lib/bluetooth.h"
#include "src/log.h"
#include "src/shared/util.h"
#include "src/shared/queue.h"
#include "avdtp.h"
#include "../profiles/audio/a2dp-codecs.h"
#define MAX_SEID 0x3E
static unsigned int seids;
#ifndef MAX
# define MAX(x, y) ((x) > (y) ? (x) : (y))
#endif
#define AVDTP_DISCOVER 0x01
#define AVDTP_GET_CAPABILITIES 0x02
#define AVDTP_SET_CONFIGURATION 0x03
#define AVDTP_GET_CONFIGURATION 0x04
#define AVDTP_RECONFIGURE 0x05
#define AVDTP_OPEN 0x06
#define AVDTP_START 0x07
#define AVDTP_CLOSE 0x08
#define AVDTP_SUSPEND 0x09
#define AVDTP_ABORT 0x0A
#define AVDTP_SECURITY_CONTROL 0x0B
#define AVDTP_GET_ALL_CAPABILITIES 0x0C
#define AVDTP_DELAY_REPORT 0x0D
#define AVDTP_PKT_TYPE_SINGLE 0x00
#define AVDTP_PKT_TYPE_START 0x01
#define AVDTP_PKT_TYPE_CONTINUE 0x02
#define AVDTP_PKT_TYPE_END 0x03
#define AVDTP_MSG_TYPE_COMMAND 0x00
#define AVDTP_MSG_TYPE_GEN_REJECT 0x01
#define AVDTP_MSG_TYPE_ACCEPT 0x02
#define AVDTP_MSG_TYPE_REJECT 0x03
#define REQ_TIMEOUT 6
#define ABORT_TIMEOUT 2
#define DISCONNECT_TIMEOUT 1
#define START_TIMEOUT 1
#if __BYTE_ORDER == __LITTLE_ENDIAN
struct avdtp_common_header {
uint8_t message_type:2;
uint8_t packet_type:2;
uint8_t transaction:4;
} __attribute__ ((packed));
struct avdtp_single_header {
uint8_t message_type:2;
uint8_t packet_type:2;
uint8_t transaction:4;
uint8_t signal_id:6;
uint8_t rfa0:2;
} __attribute__ ((packed));
struct avdtp_start_header {
uint8_t message_type:2;
uint8_t packet_type:2;
uint8_t transaction:4;
uint8_t no_of_packets;
uint8_t signal_id:6;
uint8_t rfa0:2;
} __attribute__ ((packed));
struct avdtp_continue_header {
uint8_t message_type:2;
uint8_t packet_type:2;
uint8_t transaction:4;
} __attribute__ ((packed));
struct seid_info {
uint8_t rfa0:1;
uint8_t inuse:1;
uint8_t seid:6;
uint8_t rfa2:3;
uint8_t type:1;
uint8_t media_type:4;
} __attribute__ ((packed));
struct seid {
uint8_t rfa0:2;
uint8_t seid:6;
} __attribute__ ((packed));
#elif __BYTE_ORDER == __BIG_ENDIAN
struct avdtp_common_header {
uint8_t transaction:4;
uint8_t packet_type:2;
uint8_t message_type:2;
} __attribute__ ((packed));
struct avdtp_single_header {
uint8_t transaction:4;
uint8_t packet_type:2;
uint8_t message_type:2;
uint8_t rfa0:2;
uint8_t signal_id:6;
} __attribute__ ((packed));
struct avdtp_start_header {
uint8_t transaction:4;
uint8_t packet_type:2;
uint8_t message_type:2;
uint8_t no_of_packets;
uint8_t rfa0:2;
uint8_t signal_id:6;
} __attribute__ ((packed));
struct avdtp_continue_header {
uint8_t transaction:4;
uint8_t packet_type:2;
uint8_t message_type:2;
} __attribute__ ((packed));
struct seid_info {
uint8_t seid:6;
uint8_t inuse:1;
uint8_t rfa0:1;
uint8_t media_type:4;
uint8_t type:1;
uint8_t rfa2:3;
} __attribute__ ((packed));
struct seid {
uint8_t seid:6;
uint8_t rfa0:2;
} __attribute__ ((packed));
#else
#error "Unknown byte order"
#endif
/* packets */
struct discover_resp {
struct seid_info seps[0];
} __attribute__ ((packed));
struct getcap_resp {
uint8_t caps[0];
} __attribute__ ((packed));
struct start_req {
struct seid first_seid;
struct seid other_seids[0];
} __attribute__ ((packed));
struct suspend_req {
struct seid first_seid;
struct seid other_seids[0];
} __attribute__ ((packed));
struct seid_rej {
uint8_t error;
} __attribute__ ((packed));
struct conf_rej {
uint8_t category;
uint8_t error;
} __attribute__ ((packed));
#if __BYTE_ORDER == __LITTLE_ENDIAN
struct seid_req {
uint8_t rfa0:2;
uint8_t acp_seid:6;
} __attribute__ ((packed));
struct setconf_req {
uint8_t rfa0:2;
uint8_t acp_seid:6;
uint8_t rfa1:2;
uint8_t int_seid:6;
uint8_t caps[0];
} __attribute__ ((packed));
struct stream_rej {
uint8_t rfa0:2;
uint8_t acp_seid:6;
uint8_t error;
} __attribute__ ((packed));
struct reconf_req {
uint8_t rfa0:2;
uint8_t acp_seid:6;
uint8_t serv_cap;
uint8_t serv_cap_len;
uint8_t caps[0];
} __attribute__ ((packed));
struct delay_req {
uint8_t rfa0:2;
uint8_t acp_seid:6;
uint16_t delay;
} __attribute__ ((packed));
#elif __BYTE_ORDER == __BIG_ENDIAN
struct seid_req {
uint8_t acp_seid:6;
uint8_t rfa0:2;
} __attribute__ ((packed));
struct setconf_req {
uint8_t acp_seid:6;
uint8_t rfa0:2;
uint8_t int_seid:6;
uint8_t rfa1:2;
uint8_t caps[0];
} __attribute__ ((packed));
struct stream_rej {
uint8_t acp_seid:6;
uint8_t rfa0:2;
uint8_t error;
} __attribute__ ((packed));
struct reconf_req {
uint8_t acp_seid:6;
uint8_t rfa0:2;
uint8_t serv_cap;
uint8_t serv_cap_len;
uint8_t caps[0];
} __attribute__ ((packed));
struct delay_req {
uint8_t acp_seid:6;
uint8_t rfa0:2;
uint16_t delay;
} __attribute__ ((packed));
#else
#error "Unknown byte order"
#endif
struct in_buf {
gboolean active;
int no_of_packets;
uint8_t transaction;
uint8_t message_type;
uint8_t signal_id;
uint8_t buf[1024];
uint8_t data_size;
};
struct pending_req {
uint8_t transaction;
uint8_t signal_id;
void *data;
size_t data_size;
struct avdtp_stream *stream; /* Set if the request targeted a stream */
guint timeout;
gboolean collided;
};
struct avdtp_remote_sep {
uint8_t seid;
uint8_t type;
uint8_t media_type;
struct avdtp_service_capability *codec;
gboolean delay_reporting;
GSList *caps; /* of type struct avdtp_service_capability */
struct avdtp_stream *stream;
};
struct avdtp_local_sep {
avdtp_state_t state;
struct avdtp_stream *stream;
struct seid_info info;
uint8_t codec;
uint32_t vndcodec_vendor;
uint16_t vndcodec_codec;
gboolean delay_reporting;
GSList *caps;
struct avdtp_sep_ind *ind;
struct avdtp_sep_cfm *cfm;
void *user_data;
};
struct stream_callback {
avdtp_stream_state_cb cb;
void *user_data;
unsigned int id;
};
struct discover_callback {
unsigned int id;
avdtp_discover_cb_t cb;
void *user_data;
};
struct disconnect_callback {
unsigned int id;
avdtp_disconnect_cb_t cb;
void *user_data;
};
struct avdtp_stream {
GIOChannel *io;
uint16_t imtu;
uint16_t omtu;
struct avdtp *session;
struct avdtp_local_sep *lsep;
uint8_t rseid;
GSList *caps;
GSList *callbacks;
struct avdtp_service_capability *codec;
guint io_id; /* Transport GSource ID */
guint timer; /* Waiting for other side to close or open
* the transport channel */
gboolean open_acp; /* If we are in ACT role for Open */
gboolean close_int; /* If we are in INT role for Close */
gboolean abort_int; /* If we are in INT role for Abort */
guint start_timer; /* Wait START command timer */
gboolean delay_reporting;
uint16_t delay; /* AVDTP 1.3 Delay Reporting feature */
gboolean starting; /* only valid while sep state == OPEN */
};
/* Structure describing an AVDTP connection between two devices */
struct avdtp {
unsigned int ref;
uint16_t version;
guint auth_id;
GIOChannel *io;
guint io_id;
GSList *seps; /* Elements of type struct avdtp_remote_sep * */
struct queue *lseps; /* Elements of type struct avdtp_local_sep * */
GSList *streams; /* Elements of type struct avdtp_stream * */
GSList *req_queue; /* Elements of type struct pending_req * */
GSList *prio_queue; /* Same as req_queue but is processed before it */
struct avdtp_stream *pending_open;
uint16_t imtu;
uint16_t omtu;
struct in_buf in;
char *buf;
struct discover_callback *discover;
struct pending_req *req;
GSList *disconnect;
bool shutdown;
};
static int send_request(struct avdtp *session, gboolean priority,
struct avdtp_stream *stream, uint8_t signal_id,
void *buffer, size_t size);
static gboolean avdtp_parse_resp(struct avdtp *session,
struct avdtp_stream *stream,
uint8_t transaction, uint8_t signal_id,
void *buf, int size);
static gboolean avdtp_parse_rej(struct avdtp *session,
struct avdtp_stream *stream,
uint8_t transaction, uint8_t signal_id,
void *buf, int size);
static int process_queue(struct avdtp *session);
static void avdtp_sep_set_state(struct avdtp *session,
struct avdtp_local_sep *sep,
avdtp_state_t state);
static const char *avdtp_statestr(avdtp_state_t state)
{
switch (state) {
case AVDTP_STATE_IDLE:
return "IDLE";
case AVDTP_STATE_CONFIGURED:
return "CONFIGURED";
case AVDTP_STATE_OPEN:
return "OPEN";
case AVDTP_STATE_STREAMING:
return "STREAMING";
case AVDTP_STATE_CLOSING:
return "CLOSING";
case AVDTP_STATE_ABORTING:
return "ABORTING";
default:
return "<unknown state>";
}
}
static gboolean try_send(int sk, void *data, size_t len)
{
int err;
do {
err = send(sk, data, len, 0);
} while (err < 0 && errno == EINTR);
if (err < 0) {
error("send: %s (%d)", strerror(errno), errno);
return FALSE;
} else if ((size_t) err != len) {
error("try_send: complete buffer not sent (%d/%zu bytes)",
err, len);
return FALSE;
}
return TRUE;
}
static gboolean avdtp_send(struct avdtp *session, uint8_t transaction,
uint8_t message_type, uint8_t signal_id,
void *data, size_t len)
{
unsigned int cont_fragments, sent;
struct avdtp_start_header start;
struct avdtp_continue_header cont;
int sock;
if (session->io == NULL) {
error("avdtp_send: session is closed");
return FALSE;
}
sock = g_io_channel_unix_get_fd(session->io);
/* Single packet - no fragmentation */
if (sizeof(struct avdtp_single_header) + len <= session->omtu) {
struct avdtp_single_header single;
memset(&single, 0, sizeof(single));
single.transaction = transaction;
single.packet_type = AVDTP_PKT_TYPE_SINGLE;
single.message_type = message_type;
single.signal_id = signal_id;
memcpy(session->buf, &single, sizeof(single));
memcpy(session->buf + sizeof(single), data, len);
return try_send(sock, session->buf, sizeof(single) + len);
}
/* Check if there is enough space to start packet */
if (session->omtu < sizeof(start)) {
error("No enough space to fragment packet");
return FALSE;
}
/* Count the number of needed fragments */
cont_fragments = (len - (session->omtu - sizeof(start))) /
(session->omtu - sizeof(cont)) + 1;
DBG("%zu bytes split into %d fragments", len, cont_fragments + 1);
/* Send the start packet */
memset(&start, 0, sizeof(start));
start.transaction = transaction;
start.packet_type = AVDTP_PKT_TYPE_START;
start.message_type = message_type;
start.no_of_packets = cont_fragments + 1;
start.signal_id = signal_id;
memcpy(session->buf, &start, sizeof(start));
memcpy(session->buf + sizeof(start), data,
session->omtu - sizeof(start));
if (!try_send(sock, session->buf, session->omtu))
return FALSE;
DBG("first packet with %zu bytes sent", session->omtu - sizeof(start));
sent = session->omtu - sizeof(start);
/* Send the continue fragments and the end packet */
while (sent < len) {
int left, to_copy;
left = len - sent;
if (left + sizeof(cont) > session->omtu) {
cont.packet_type = AVDTP_PKT_TYPE_CONTINUE;
to_copy = session->omtu - sizeof(cont);
DBG("sending continue with %d bytes", to_copy);
} else {
cont.packet_type = AVDTP_PKT_TYPE_END;
to_copy = left;
DBG("sending end with %d bytes", to_copy);
}
cont.transaction = transaction;
cont.message_type = message_type;
memcpy(session->buf, &cont, sizeof(cont));
memcpy(session->buf + sizeof(cont), data + sent, to_copy);
if (!try_send(sock, session->buf, to_copy + sizeof(cont)))
return FALSE;
sent += to_copy;
}
return TRUE;
}
static void pending_req_free(void *data)
{
struct pending_req *req = data;
if (req->timeout)
g_source_remove(req->timeout);
g_free(req->data);
g_free(req);
}
static void close_stream(struct avdtp_stream *stream)
{
int sock;
if (stream->io == NULL)
return;
sock = g_io_channel_unix_get_fd(stream->io);
shutdown(sock, SHUT_RDWR);
g_io_channel_shutdown(stream->io, FALSE, NULL);
g_io_channel_unref(stream->io);
stream->io = NULL;
}
static gboolean stream_close_timeout(gpointer user_data)
{
struct avdtp_stream *stream = user_data;
DBG("Timed out waiting for peer to close the transport channel");
stream->timer = 0;
close_stream(stream);
return FALSE;
}
static gboolean stream_open_timeout(gpointer user_data)
{
struct avdtp_stream *stream = user_data;
DBG("Timed out waiting for peer to open the transport channel");
stream->timer = 0;
stream->session->pending_open = NULL;
avdtp_abort(stream->session, stream);
return FALSE;
}
void avdtp_error_init(struct avdtp_error *err, uint8_t category, int id)
{
err->category = category;
if (category == AVDTP_ERRNO)
err->err.posix_errno = id;
else
err->err.error_code = id;
}
uint8_t avdtp_error_category(struct avdtp_error *err)
{
return err->category;
}
int avdtp_error_error_code(struct avdtp_error *err)
{
assert(err->category != AVDTP_ERRNO);
return err->err.error_code;
}
int avdtp_error_posix_errno(struct avdtp_error *err)
{
assert(err->category == AVDTP_ERRNO);
return err->err.posix_errno;
}
static struct avdtp_stream *find_stream_by_rseid(struct avdtp *session,
uint8_t rseid)
{
GSList *l;
for (l = session->streams; l != NULL; l = g_slist_next(l)) {
struct avdtp_stream *stream = l->data;
if (stream->rseid == rseid)
return stream;
}
return NULL;
}
static struct avdtp_remote_sep *find_remote_sep(GSList *seps, uint8_t seid)
{
GSList *l;
for (l = seps; l != NULL; l = g_slist_next(l)) {
struct avdtp_remote_sep *sep = l->data;
if (sep->seid == seid)
return sep;
}
return NULL;
}
static void stream_free(void *data)
{
struct avdtp_stream *stream = data;
struct avdtp_remote_sep *rsep;
stream->lsep->info.inuse = 0;
stream->lsep->stream = NULL;
rsep = find_remote_sep(stream->session->seps, stream->rseid);
if (rsep)
rsep->stream = NULL;
if (stream->timer)
g_source_remove(stream->timer);
if (stream->start_timer > 0)
g_source_remove(stream->start_timer);
if (stream->io)
close_stream(stream);
if (stream->io_id)
g_source_remove(stream->io_id);
g_slist_free_full(stream->callbacks, g_free);
g_slist_free_full(stream->caps, g_free);
g_free(stream);
}
static gboolean transport_cb(GIOChannel *chan, GIOCondition cond,
gpointer data)
{
struct avdtp_stream *stream = data;
struct avdtp_local_sep *sep = stream->lsep;
if (stream->close_int && sep->cfm && sep->cfm->close)
sep->cfm->close(stream->session, sep, stream, NULL,
sep->user_data);
if (!(cond & G_IO_NVAL))
close_stream(stream);
stream->io_id = 0;
if (!stream->abort_int)
avdtp_sep_set_state(stream->session, sep, AVDTP_STATE_IDLE);
return FALSE;
}
static void handle_transport_connect(struct avdtp *session, GIOChannel *io,
uint16_t imtu, uint16_t omtu)
{
struct avdtp_stream *stream = session->pending_open;
struct avdtp_local_sep *sep = stream->lsep;
session->pending_open = NULL;
if (stream->timer) {
g_source_remove(stream->timer);
stream->timer = 0;
}
if (io == NULL)
return;
if (stream->io == NULL)
stream->io = g_io_channel_ref(io);
stream->omtu = omtu;
stream->imtu = imtu;
avdtp_sep_set_state(session, sep, AVDTP_STATE_OPEN);
stream->io_id = g_io_add_watch(io, G_IO_ERR | G_IO_HUP | G_IO_NVAL,
(GIOFunc) transport_cb, stream);
}
static int pending_req_cmp(gconstpointer a, gconstpointer b)
{
const struct pending_req *req = a;
const struct avdtp_stream *stream = b;
if (req->stream == stream)
return 0;
return -1;
}
static void cleanup_queue(struct avdtp *session, struct avdtp_stream *stream)
{
GSList *l;
struct pending_req *req;
while ((l = g_slist_find_custom(session->prio_queue, stream,
pending_req_cmp))) {
req = l->data;
pending_req_free(req);
session->prio_queue = g_slist_remove(session->prio_queue, req);
}
while ((l = g_slist_find_custom(session->req_queue, stream,
pending_req_cmp))) {
req = l->data;
pending_req_free(req);
session->req_queue = g_slist_remove(session->req_queue, req);
}
}
static void handle_unanswered_req(struct avdtp *session,
struct avdtp_stream *stream)
{
struct pending_req *req;
struct avdtp_local_sep *lsep;
struct avdtp_error err;
if (!session->req->timeout)
/* Request is in process */
return;
if (session->req->signal_id == AVDTP_ABORT) {
/* Avoid freeing the Abort request here */
DBG("handle_unanswered_req: Abort req, returning");
session->req->stream = NULL;
return;
}
req = session->req;
session->req = NULL;
avdtp_error_init(&err, AVDTP_ERRNO, EIO);
lsep = stream->lsep;
switch (req->signal_id) {
case AVDTP_RECONFIGURE:
error("No reply to Reconfigure request");
if (lsep && lsep->cfm && lsep->cfm->reconfigure)
lsep->cfm->reconfigure(session, lsep, stream, &err,
lsep->user_data);
break;
case AVDTP_OPEN:
error("No reply to Open request");
if (lsep && lsep->cfm && lsep->cfm->open)
lsep->cfm->open(session, lsep, stream, &err,
lsep->user_data);
break;
case AVDTP_START:
error("No reply to Start request");
if (lsep && lsep->cfm && lsep->cfm->start)
lsep->cfm->start(session, lsep, stream, &err,
lsep->user_data);
break;
case AVDTP_SUSPEND:
error("No reply to Suspend request");
if (lsep && lsep->cfm && lsep->cfm->suspend)
lsep->cfm->suspend(session, lsep, stream, &err,
lsep->user_data);
break;
case AVDTP_CLOSE:
error("No reply to Close request");
if (lsep && lsep->cfm && lsep->cfm->close)
lsep->cfm->close(session, lsep, stream, &err,
lsep->user_data);
break;
case AVDTP_SET_CONFIGURATION:
error("No reply to SetConfiguration request");
if (lsep && lsep->cfm && lsep->cfm->set_configuration)
lsep->cfm->set_configuration(session, lsep, stream,
&err, lsep->user_data);
}
pending_req_free(req);
}
static void avdtp_sep_set_state(struct avdtp *session,
struct avdtp_local_sep *sep,
avdtp_state_t state)
{
struct avdtp_stream *stream = sep->stream;
avdtp_state_t old_state;
struct avdtp_error err, *err_ptr = NULL;
GSList *l;
if (!stream) {
error("Error changing sep state: stream not available");
return;
}
if (sep->state == state) {
avdtp_error_init(&err, AVDTP_ERRNO, EIO);
DBG("stream state change failed: %s", avdtp_strerror(&err));
err_ptr = &err;
} else {
err_ptr = NULL;
DBG("stream state changed: %s -> %s",
avdtp_statestr(sep->state),
avdtp_statestr(state));
}
old_state = sep->state;
sep->state = state;
switch (state) {
case AVDTP_STATE_CONFIGURED:
if (sep->info.type == AVDTP_SEP_TYPE_SINK)
avdtp_delay_report(session, stream, stream->delay);
break;
case AVDTP_STATE_OPEN:
stream->starting = FALSE;
break;
case AVDTP_STATE_STREAMING:
if (stream->start_timer) {
g_source_remove(stream->start_timer);
stream->start_timer = 0;
}
stream->open_acp = FALSE;
break;
case AVDTP_STATE_CLOSING:
case AVDTP_STATE_ABORTING:
if (stream->start_timer) {
g_source_remove(stream->start_timer);
stream->start_timer = 0;
}
break;
case AVDTP_STATE_IDLE:
if (stream->start_timer) {
g_source_remove(stream->start_timer);
stream->start_timer = 0;
}
if (session->pending_open == stream)
handle_transport_connect(session, NULL, 0, 0);
if (session->req && session->req->stream == stream)
handle_unanswered_req(session, stream);
/* Remove pending commands for this stream from the queue */
cleanup_queue(session, stream);
break;
default:
break;
}
l = stream->callbacks;
while (l != NULL) {
struct stream_callback *cb = l->data;
l = g_slist_next(l);
cb->cb(stream, old_state, state, err_ptr, cb->user_data);
}
if (state == AVDTP_STATE_IDLE &&
g_slist_find(session->streams, stream)) {
session->streams = g_slist_remove(session->streams, stream);
stream_free(stream);
}
if (session->io && session->shutdown && session->streams == NULL) {
int sock = g_io_channel_unix_get_fd(session->io);
shutdown(sock, SHUT_RDWR);
}
}
static void finalize_discovery(struct avdtp *session, int err)
{
struct discover_callback *discover = session->discover;
struct avdtp_error avdtp_err;
if (!discover)
return;
session->discover = NULL;
avdtp_error_init(&avdtp_err, AVDTP_ERRNO, err);
if (discover->id > 0)
g_source_remove(discover->id);
if (discover->cb)
discover->cb(session, session->seps, err ? &avdtp_err : NULL,
discover->user_data);
g_free(discover);
}
static void release_stream(struct avdtp_stream *stream, struct avdtp *session)
{
struct avdtp_local_sep *sep = stream->lsep;
if (sep->cfm && sep->cfm->abort &&
(sep->state != AVDTP_STATE_ABORTING ||
stream->abort_int))
sep->cfm->abort(session, sep, stream, NULL, sep->user_data);
avdtp_sep_set_state(session, sep, AVDTP_STATE_IDLE);
}
static void sep_free(gpointer data)
{
struct avdtp_remote_sep *sep = data;
g_slist_free_full(sep->caps, g_free);
g_free(sep);
}
static void avdtp_free(void *data)
{
struct avdtp *session = data;
DBG("%p", session);
g_slist_free_full(session->streams, stream_free);
if (session->io) {
g_io_channel_shutdown(session->io, FALSE, NULL);
g_io_channel_unref(session->io);
}
if (session->io_id) {
g_source_remove(session->io_id);
session->io_id = 0;
}
if (session->req)
pending_req_free(session->req);
g_slist_free_full(session->req_queue, pending_req_free);
g_slist_free_full(session->prio_queue, pending_req_free);
g_slist_free_full(session->seps, sep_free);
g_slist_free_full(session->disconnect, g_free);
/* Free copy of the SEP list */
session->lseps = NULL;