-
Notifications
You must be signed in to change notification settings - Fork 245
/
bap.c
6455 lines (5073 loc) · 142 KB
/
bap.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
// SPDX-License-Identifier: LGPL-2.1-or-later
/*
*
* BlueZ - Bluetooth protocol stack for Linux
*
* Copyright (C) 2022 Intel Corporation. All rights reserved.
* Copyright 2023-2024 NXP
*
*/
#define _GNU_SOURCE
#include <inttypes.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <errno.h>
#include "lib/bluetooth.h"
#include "lib/uuid.h"
#include "src/shared/io.h"
#include "src/shared/queue.h"
#include "src/shared/util.h"
#include "src/shared/timeout.h"
#include "src/shared/att.h"
#include "src/shared/gatt-db.h"
#include "src/shared/gatt-server.h"
#include "src/shared/gatt-client.h"
#include "src/shared/bap.h"
#include "src/shared/ascs.h"
#include "src/shared/bap-debug.h"
/* Maximum number of ASE(s) */
#define NUM_SINKS 2
#define NUM_SOURCE 2
#define NUM_ASES (NUM_SINKS + NUM_SOURCE)
#define ASE_UUID(_id) (_id < NUM_SINKS ? ASE_SINK_UUID : ASE_SOURCE_UUID)
#define DBG(_bap, fmt, arg...) \
bap_debug(_bap, "%s:%s() " fmt, __FILE__, __func__, ## arg)
#define LTV(_type, _bytes...) \
{ \
.len = 1 + sizeof((uint8_t []) { _bytes }), \
.type = _type, \
.data = { _bytes }, \
}
#define BAP_PROCESS_TIMEOUT 10
#define BAP_FREQ_LTV_TYPE 1
#define BAP_DURATION_LTV_TYPE 2
#define BAP_CHANNEL_ALLOCATION_LTV_TYPE 3
#define BAP_FRAME_LEN_LTV_TYPE 4
#define CODEC_SPECIFIC_CONFIGURATION_MASK (\
(1<<BAP_FREQ_LTV_TYPE)|\
(1<<BAP_DURATION_LTV_TYPE)|\
(1<<BAP_FRAME_LEN_LTV_TYPE))
struct bt_bap_pac_changed {
unsigned int id;
bt_bap_pac_func_t added;
bt_bap_pac_func_t removed;
bt_bap_destroy_func_t destroy;
void *data;
};
struct bt_bap_ready {
unsigned int id;
bt_bap_ready_func_t func;
bt_bap_destroy_func_t destroy;
void *data;
};
struct bt_bap_state {
unsigned int id;
bt_bap_state_func_t func;
bt_bap_connecting_func_t connecting;
bt_bap_destroy_func_t destroy;
void *data;
};
struct bt_bap_cb {
unsigned int id;
bt_bap_func_t attached;
bt_bap_func_t detached;
void *user_data;
};
struct bt_pacs {
struct bt_bap_db *bdb;
struct gatt_db_attribute *service;
struct gatt_db_attribute *sink;
struct gatt_db_attribute *sink_ccc;
struct gatt_db_attribute *sink_loc;
struct gatt_db_attribute *sink_loc_ccc;
struct gatt_db_attribute *source;
struct gatt_db_attribute *source_ccc;
struct gatt_db_attribute *source_loc;
struct gatt_db_attribute *source_loc_ccc;
struct gatt_db_attribute *context;
struct gatt_db_attribute *context_ccc;
struct gatt_db_attribute *supported_context;
struct gatt_db_attribute *supported_context_ccc;
uint32_t source_loc_value;
uint32_t sink_loc_value;
uint16_t source_context_value;
uint16_t sink_context_value;
uint16_t supported_source_context_value;
uint16_t supported_sink_context_value;
};
struct bt_ase {
struct bt_ascs *ascs;
uint8_t id;
struct gatt_db_attribute *attr;
struct gatt_db_attribute *ccc;
};
struct bt_ascs {
struct bt_bap_db *bdb;
struct gatt_db_attribute *service;
struct bt_ase *ase[NUM_ASES];
struct gatt_db_attribute *ase_cp;
struct gatt_db_attribute *ase_cp_ccc;
};
struct bt_bap_db {
struct gatt_db *db;
struct bt_pacs *pacs;
struct bt_ascs *ascs;
struct queue *sinks;
struct queue *sources;
struct queue *broadcast_sources;
struct queue *broadcast_sinks;
};
struct bt_bap_req {
unsigned int id;
struct bt_bap_stream *stream;
uint8_t op;
struct queue *group;
struct iovec *iov;
size_t len;
bt_bap_stream_func_t func;
void *user_data;
};
typedef void (*bap_notify_t)(struct bt_bap *bap, uint16_t value_handle,
const uint8_t *value, uint16_t length,
void *user_data);
struct bt_bap_notify {
unsigned int id;
struct bt_bap *bap;
bap_notify_t func;
void *user_data;
};
struct bt_bap {
int ref_count;
struct bt_bap_db *ldb;
struct bt_bap_db *rdb;
struct bt_gatt_client *client;
struct bt_att *att;
struct bt_bap_req *req;
unsigned int cp_id;
unsigned int process_id;
unsigned int disconn_id;
unsigned int idle_id;
struct queue *reqs;
struct queue *notify;
struct queue *streams;
struct queue *local_eps;
struct queue *remote_eps;
struct queue *pac_cbs;
struct queue *ready_cbs;
struct queue *state_cbs;
bt_bap_debug_func_t debug_func;
bt_bap_destroy_func_t debug_destroy;
void *debug_data;
void *user_data;
};
struct bt_bap_chan {
uint8_t count;
uint32_t location;
};
struct bt_bap_pac {
struct bt_bap_db *bdb;
char *name;
uint8_t type;
struct bt_bap_codec codec;
struct bt_bap_pac_qos qos;
struct iovec *data;
struct iovec *metadata;
struct queue *channels;
struct bt_bap_pac_ops *ops;
void *user_data;
};
struct bt_bap_endpoint {
struct bt_bap *bap;
struct bt_bap_db *bdb;
struct bt_bap_stream *stream;
struct gatt_db_attribute *attr;
uint8_t id;
uint8_t dir;
uint8_t old_state;
uint8_t state;
unsigned int state_id;
};
struct bt_bap_stream_io {
struct bt_bap *bap;
int ref_count;
struct io *io;
bool connecting;
};
struct bt_bap_stream_ops {
uint8_t type;
void (*set_state)(struct bt_bap_stream *stream, uint8_t state);
unsigned int (*config)(struct bt_bap_stream *stream,
struct bt_bap_qos *qos, struct iovec *data,
bt_bap_stream_func_t func, void *user_data);
unsigned int (*qos)(struct bt_bap_stream *stream,
struct bt_bap_qos *qos,
bt_bap_stream_func_t func, void *user_data);
unsigned int (*enable)(struct bt_bap_stream *stream, bool enable_links,
struct iovec *metadata,
bt_bap_stream_func_t func, void *user_data);
unsigned int (*start)(struct bt_bap_stream *stream,
bt_bap_stream_func_t func, void *user_data);
unsigned int (*disable)(struct bt_bap_stream *stream,
bool disable_links, bt_bap_stream_func_t func,
void *user_data);
unsigned int (*stop)(struct bt_bap_stream *stream,
bt_bap_stream_func_t func, void *user_data);
unsigned int (*metadata)(struct bt_bap_stream *stream,
struct iovec *data, bt_bap_stream_func_t func,
void *user_data);
unsigned int (*release)(struct bt_bap_stream *stream,
bt_bap_stream_func_t func, void *user_data);
};
struct bt_bap_stream {
struct bt_bap *bap;
struct bt_bap_endpoint *ep;
struct bt_bap_pac *lpac;
struct bt_bap_pac *rpac;
struct iovec *cc;
struct iovec *meta;
struct bt_bap_qos qos;
struct bt_bap_stream *link;
struct bt_bap_stream_io *io;
const struct bt_bap_stream_ops *ops;
uint8_t old_state;
uint8_t state;
bool client;
void *user_data;
};
/* TODO: Figure out the capabilities types */
#define BT_CODEC_CAP_PARAMS 0x01
#define BT_CODEC_CAP_DRM 0x0a
#define BT_CODEC_CAP_DRM_VALUE 0x0b
struct bt_pac_metadata {
uint8_t len;
uint8_t data[0];
} __packed;
struct bt_pac {
struct bt_bap_codec codec; /* Codec ID */
uint8_t cc_len; /* Codec Capabilities Length */
struct bt_ltv cc[0]; /* Codec Specific Capabilities */
struct bt_pac_metadata meta[0]; /* Metadata */
} __packed;
struct bt_pacs_read_rsp {
uint8_t num_pac;
struct bt_pac pac[0];
} __packed;
struct bt_pacs_context {
uint16_t snk;
uint16_t src;
} __packed;
struct bt_base {
uint8_t big_id;
uint32_t pres_delay;
uint8_t next_bis_index;
struct queue *subgroups;
};
struct bt_subgroup {
uint8_t index;
struct bt_bap_codec codec;
struct iovec *caps;
struct iovec *meta;
struct queue *bises;
};
struct bt_bis {
uint8_t index;
struct iovec *caps;
};
/* Contains local bt_bap_db */
static struct queue *bap_db;
static struct queue *bap_cbs;
static struct queue *sessions;
static bool bap_db_match(const void *data, const void *match_data)
{
const struct bt_bap_db *bdb = data;
const struct gatt_db *db = match_data;
return (bdb->db == db);
}
unsigned int bt_bap_pac_register(struct bt_bap *bap, bt_bap_pac_func_t added,
bt_bap_pac_func_t removed, void *user_data,
bt_bap_destroy_func_t destroy)
{
struct bt_bap_pac_changed *changed;
static unsigned int id;
if (!bap)
return 0;
changed = new0(struct bt_bap_pac_changed, 1);
changed->id = ++id ? id : ++id;
changed->added = added;
changed->removed = removed;
changed->destroy = destroy;
changed->data = user_data;
queue_push_tail(bap->pac_cbs, changed);
return changed->id;
}
static void pac_changed_free(void *data)
{
struct bt_bap_pac_changed *changed = data;
if (changed->destroy)
changed->destroy(changed->data);
free(changed);
}
static bool match_pac_changed_id(const void *data, const void *match_data)
{
const struct bt_bap_pac_changed *changed = data;
unsigned int id = PTR_TO_UINT(match_data);
return (changed->id == id);
}
bool bt_bap_pac_unregister(struct bt_bap *bap, unsigned int id)
{
struct bt_bap_pac_changed *changed;
if (!bap)
return false;
changed = queue_remove_if(bap->pac_cbs, match_pac_changed_id,
UINT_TO_PTR(id));
if (!changed)
return false;
pac_changed_free(changed);
return true;
}
static void pac_foreach(void *data, void *user_data)
{
struct bt_bap_pac *pac = data;
struct iovec *iov = user_data;
struct bt_pacs_read_rsp *rsp;
struct bt_pac *p;
struct bt_pac_metadata *meta;
if (!iov->iov_len) {
rsp = util_iov_push(iov, sizeof(*rsp));
rsp->num_pac = 0;
} else
rsp = iov->iov_base;
rsp->num_pac++;
p = util_iov_push(iov, sizeof(*p));
p->codec.id = pac->codec.id;
p->codec.cid = cpu_to_le16(pac->codec.cid);
p->codec.vid = cpu_to_le16(pac->codec.vid);
if (pac->data) {
p->cc_len = pac->data->iov_len;
util_iov_push_mem(iov, p->cc_len, pac->data->iov_base);
} else
p->cc_len = 0;
meta = util_iov_push(iov, sizeof(*meta));
if (pac->metadata) {
meta->len = pac->metadata->iov_len;
util_iov_push_mem(iov, meta->len, pac->metadata->iov_base);
} else
meta->len = 0;
}
static void pacs_sink_read(struct gatt_db_attribute *attrib,
unsigned int id, uint16_t offset,
uint8_t opcode, struct bt_att *att,
void *user_data)
{
struct bt_pacs *pacs = user_data;
struct bt_bap_db *bdb = pacs->bdb;
struct iovec iov;
uint8_t value[512];
memset(value, 0, sizeof(value));
iov.iov_base = value;
iov.iov_len = 0;
queue_foreach(bdb->sinks, pac_foreach, &iov);
gatt_db_attribute_read_result(attrib, id, 0, iov.iov_base,
iov.iov_len);
}
static void pacs_sink_loc_read(struct gatt_db_attribute *attrib,
unsigned int id, uint16_t offset,
uint8_t opcode, struct bt_att *att,
void *user_data)
{
struct bt_pacs *pacs = user_data;
uint32_t value = cpu_to_le32(pacs->sink_loc_value);
gatt_db_attribute_read_result(attrib, id, 0, (void *) &value,
sizeof(value));
}
static void pacs_source_read(struct gatt_db_attribute *attrib,
unsigned int id, uint16_t offset,
uint8_t opcode, struct bt_att *att,
void *user_data)
{
struct bt_pacs *pacs = user_data;
struct bt_bap_db *bdb = pacs->bdb;
struct iovec iov;
uint8_t value[512];
memset(value, 0, sizeof(value));
iov.iov_base = value;
iov.iov_len = 0;
queue_foreach(bdb->sources, pac_foreach, &iov);
gatt_db_attribute_read_result(attrib, id, 0, iov.iov_base,
iov.iov_len);
}
static void pacs_source_loc_read(struct gatt_db_attribute *attrib,
unsigned int id, uint16_t offset,
uint8_t opcode, struct bt_att *att,
void *user_data)
{
struct bt_pacs *pacs = user_data;
uint32_t value = cpu_to_le32(pacs->source_loc_value);
gatt_db_attribute_read_result(attrib, id, 0, (void *) &value,
sizeof(value));
}
static void pacs_context_read(struct gatt_db_attribute *attrib,
unsigned int id, uint16_t offset,
uint8_t opcode, struct bt_att *att,
void *user_data)
{
struct bt_pacs *pacs = user_data;
struct bt_pacs_context ctx = {
.snk = cpu_to_le16(pacs->sink_context_value),
.src = cpu_to_le16(pacs->source_context_value)
};
gatt_db_attribute_read_result(attrib, id, 0, (void *) &ctx,
sizeof(ctx));
}
static void pacs_supported_context_read(struct gatt_db_attribute *attrib,
unsigned int id, uint16_t offset,
uint8_t opcode, struct bt_att *att,
void *user_data)
{
struct bt_pacs *pacs = user_data;
struct bt_pacs_context ctx = {
.snk = cpu_to_le16(pacs->supported_sink_context_value),
.src = cpu_to_le16(pacs->supported_source_context_value)
};
gatt_db_attribute_read_result(attrib, id, 0, (void *) &ctx,
sizeof(ctx));
}
static struct bt_pacs *pacs_new(struct gatt_db *db)
{
struct bt_pacs *pacs;
bt_uuid_t uuid;
if (!db)
return NULL;
pacs = new0(struct bt_pacs, 1);
/* Populate DB with PACS attributes */
bt_uuid16_create(&uuid, PACS_UUID);
pacs->service = gatt_db_add_service(db, &uuid, true, 19);
bt_uuid16_create(&uuid, PAC_SINK_CHRC_UUID);
pacs->sink = gatt_db_service_add_characteristic(pacs->service, &uuid,
BT_ATT_PERM_READ,
BT_GATT_CHRC_PROP_READ |
BT_GATT_CHRC_PROP_NOTIFY,
pacs_sink_read, NULL,
pacs);
pacs->sink_ccc = gatt_db_service_add_ccc(pacs->service,
BT_ATT_PERM_READ | BT_ATT_PERM_WRITE);
bt_uuid16_create(&uuid, PAC_SINK_LOC_CHRC_UUID);
pacs->sink_loc = gatt_db_service_add_characteristic(pacs->service,
&uuid, BT_ATT_PERM_READ,
BT_GATT_CHRC_PROP_READ |
BT_GATT_CHRC_PROP_NOTIFY,
pacs_sink_loc_read, NULL,
pacs);
pacs->sink_loc_ccc = gatt_db_service_add_ccc(pacs->service,
BT_ATT_PERM_READ | BT_ATT_PERM_WRITE);
bt_uuid16_create(&uuid, PAC_SOURCE_CHRC_UUID);
pacs->sink = gatt_db_service_add_characteristic(pacs->service, &uuid,
BT_ATT_PERM_READ,
BT_GATT_CHRC_PROP_READ |
BT_GATT_CHRC_PROP_NOTIFY,
pacs_source_read, NULL,
pacs);
pacs->sink_ccc = gatt_db_service_add_ccc(pacs->service,
BT_ATT_PERM_READ | BT_ATT_PERM_WRITE);
bt_uuid16_create(&uuid, PAC_SOURCE_LOC_CHRC_UUID);
pacs->source_loc = gatt_db_service_add_characteristic(pacs->service,
&uuid, BT_ATT_PERM_READ,
BT_GATT_CHRC_PROP_READ |
BT_GATT_CHRC_PROP_NOTIFY,
pacs_source_loc_read, NULL,
pacs);
pacs->source_loc_ccc = gatt_db_service_add_ccc(pacs->service,
BT_ATT_PERM_READ | BT_ATT_PERM_WRITE);
bt_uuid16_create(&uuid, PAC_CONTEXT);
pacs->context = gatt_db_service_add_characteristic(pacs->service,
&uuid, BT_ATT_PERM_READ,
BT_GATT_CHRC_PROP_READ |
BT_GATT_CHRC_PROP_NOTIFY,
pacs_context_read, NULL, pacs);
pacs->context_ccc = gatt_db_service_add_ccc(pacs->service,
BT_ATT_PERM_READ | BT_ATT_PERM_WRITE);
bt_uuid16_create(&uuid, PAC_SUPPORTED_CONTEXT);
pacs->supported_context =
gatt_db_service_add_characteristic(pacs->service, &uuid,
BT_ATT_PERM_READ,
BT_GATT_CHRC_PROP_READ |
BT_GATT_CHRC_PROP_NOTIFY,
pacs_supported_context_read, NULL,
pacs);
pacs->supported_context_ccc = gatt_db_service_add_ccc(pacs->service,
BT_ATT_PERM_READ | BT_ATT_PERM_WRITE);
gatt_db_service_set_active(pacs->service, true);
return pacs;
}
static void bap_debug(struct bt_bap *bap, const char *format, ...)
{
va_list ap;
if (!bap || !format || !bap->debug_func)
return;
va_start(ap, format);
util_debug_va(bap->debug_func, bap->debug_data, format, ap);
va_end(ap);
}
static void bap_disconnected(int err, void *user_data)
{
struct bt_bap *bap = user_data;
bap->disconn_id = 0;
DBG(bap, "bap %p disconnected err %d", bap, err);
bt_bap_detach(bap);
}
static struct bt_bap *bap_get_session(struct bt_att *att, struct gatt_db *db)
{
const struct queue_entry *entry;
struct bt_bap *bap;
for (entry = queue_get_entries(sessions); entry; entry = entry->next) {
struct bt_bap *bap = entry->data;
if (att == bt_bap_get_att(bap))
return bap;
}
bap = bt_bap_new(db, NULL);
bap->att = att;
bt_bap_attach(bap, NULL);
return bap;
}
static bool bap_endpoint_match(const void *data, const void *match_data)
{
const struct bt_bap_endpoint *ep = data;
const struct gatt_db_attribute *attr = match_data;
return (ep->attr == attr);
}
static struct bt_bap_endpoint *bap_endpoint_new(struct bt_bap_db *bdb,
struct gatt_db_attribute *attr)
{
struct bt_bap_endpoint *ep;
bt_uuid_t uuid, source, sink;
if (!gatt_db_attribute_get_char_data(attr, NULL, NULL, NULL, NULL,
&uuid))
return NULL;
ep = new0(struct bt_bap_endpoint, 1);
ep->bdb = bdb;
ep->attr = attr;
bt_uuid16_create(&source, ASE_SOURCE_UUID);
bt_uuid16_create(&sink, ASE_SINK_UUID);
if (!bt_uuid_cmp(&source, &uuid))
ep->dir = BT_BAP_SOURCE;
else if (!bt_uuid_cmp(&sink, &uuid))
ep->dir = BT_BAP_SINK;
return ep;
}
static struct bt_bap_endpoint *bap_endpoint_new_broadcast(struct bt_bap_db *bdb,
uint8_t type)
{
struct bt_bap_endpoint *ep;
ep = new0(struct bt_bap_endpoint, 1);
ep->bdb = bdb;
ep->attr = NULL;
if (type == BT_BAP_BCAST_SINK)
ep->dir = BT_BAP_BCAST_SOURCE;
else
ep->dir = BT_BAP_BCAST_SINK;
return ep;
}
static struct bt_bap_endpoint *bap_get_endpoint(struct queue *endpoints,
struct bt_bap_db *db,
struct gatt_db_attribute *attr)
{
struct bt_bap_endpoint *ep;
if (!db || !attr)
return NULL;
ep = queue_find(endpoints, bap_endpoint_match, attr);
if (ep)
return ep;
ep = bap_endpoint_new(db, attr);
if (!ep)
return NULL;
queue_push_tail(endpoints, ep);
return ep;
}
static bool match_ep_type(const void *data, const void *match_data)
{
const struct bt_bap_endpoint *ep = data;
const uint8_t type = PTR_TO_INT(match_data);
return (ep->dir == type);
}
static struct bt_bap_endpoint *bap_get_endpoint_bcast(struct queue *endpoints,
struct bt_bap_db *db, uint8_t type)
{
struct bt_bap_endpoint *ep;
if (!db)
return NULL;
ep = queue_find(endpoints, match_ep_type, INT_TO_PTR(type));
if (ep)
return ep;
ep = bap_endpoint_new_broadcast(db, type);
if (!ep)
return NULL;
queue_push_tail(endpoints, ep);
return ep;
}
static bool bap_endpoint_match_id(const void *data, const void *match_data)
{
const struct bt_bap_endpoint *ep = data;
uint8_t id = PTR_TO_UINT(match_data);
return (ep->id == id);
}
static struct bt_bap_endpoint *bap_get_local_endpoint_id(struct bt_bap *bap,
uint8_t id)
{
struct bt_bap_endpoint *ep;
struct gatt_db_attribute *attr = NULL;
size_t i;
if (!bap)
return NULL;
ep = queue_find(bap->local_eps, bap_endpoint_match_id, UINT_TO_PTR(id));
if (ep)
return ep;
for (i = 0; i < ARRAY_SIZE(bap->ldb->ascs->ase); i++) {
struct bt_ase *ase = bap->ldb->ascs->ase[i];
if (id) {
if (ase->id != id)
continue;
attr = ase->attr;
break;
}
ep = queue_find(bap->local_eps, bap_endpoint_match, ase->attr);
if (!ep) {
attr = ase->attr;
break;
}
}
if (!attr)
return NULL;
ep = bap_endpoint_new(bap->ldb, attr);
if (!ep)
return NULL;
ep->id = id;
queue_push_tail(bap->local_eps, ep);
return ep;
}
static void ascs_ase_read(struct gatt_db_attribute *attrib,
unsigned int id, uint16_t offset,
uint8_t opcode, struct bt_att *att,
void *user_data)
{
struct bt_ase *ase = user_data;
struct bt_bap *bap = bap_get_session(att, ase->ascs->bdb->db);
struct bt_bap_endpoint *ep = bap_get_endpoint(bap->local_eps,
bap->ldb, attrib);
struct bt_ascs_ase_status rsp;
if (!ase || !bap || !ep) {
gatt_db_attribute_read_result(attrib, id, BT_ATT_ERROR_UNLIKELY,
NULL, 0);
return;
}
memset(&rsp, 0, sizeof(rsp));
/* Initialize Endpoint ID with ASE ID */
if (ase->id != ep->id)
ep->id = ase->id;
rsp.id = ep->id;
rsp.state = ep->state;
gatt_db_attribute_read_result(attrib, id, 0, (void *) &rsp,
sizeof(rsp));
}
static void ase_new(struct bt_ascs *ascs, int i)
{
struct bt_ase *ase;
bt_uuid_t uuid;
if (!ascs)
return;
ase = new0(struct bt_ase, 1);
ase->ascs = ascs;
ase->id = i + 1;
bt_uuid16_create(&uuid, ASE_UUID(i));
ase->attr = gatt_db_service_add_characteristic(ascs->service, &uuid,
BT_ATT_PERM_READ,
BT_GATT_CHRC_PROP_READ |
BT_GATT_CHRC_PROP_NOTIFY,
ascs_ase_read, NULL,
ase);
ase->ccc = gatt_db_service_add_ccc(ascs->service,
BT_ATT_PERM_READ | BT_ATT_PERM_WRITE);
ascs->ase[i] = ase;
}
static bool bap_codec_equal(const struct bt_bap_codec *c1,
const struct bt_bap_codec *c2)
{
/* Compare CID and VID if id is 0xff */
if (c1->id == 0xff)
return !memcmp(c1, c2, sizeof(*c1));
return c1->id == c2->id;
}
static void ascs_ase_rsp_add(struct iovec *iov, uint8_t id,
uint8_t code, uint8_t reason)
{
struct bt_ascs_cp_rsp *cp;
struct bt_ascs_ase_rsp *rsp;
if (!iov)
return;
cp = iov->iov_base;
if (cp->num_ase == 0xff)
return;
switch (code) {
/* If the Response_Code value is 0x01 or 0x02, Number_of_ASEs shall be
* set to 0xFF.
*/
case BT_ASCS_RSP_NOT_SUPPORTED:
case BT_ASCS_RSP_TRUNCATED:
cp->num_ase = 0xff;
break;
default:
cp->num_ase++;
break;
}
iov->iov_len += sizeof(*rsp);
iov->iov_base = realloc(iov->iov_base, iov->iov_len);
rsp = iov->iov_base + (iov->iov_len - sizeof(*rsp));
rsp->ase = id;
rsp->code = code;
rsp->reason = reason;
}
static void ascs_ase_rsp_success(struct iovec *iov, uint8_t id)
{
return ascs_ase_rsp_add(iov, id, BT_ASCS_RSP_SUCCESS,
BT_ASCS_REASON_NONE);
}
static void stream_notify_config(struct bt_bap_stream *stream)
{
struct bt_bap_endpoint *ep = stream->ep;
struct bt_bap_pac *lpac = stream->lpac;
struct bt_ascs_ase_status *status;
struct bt_ascs_ase_status_config *config;
size_t len;
DBG(stream->bap, "stream %p", stream);
len = sizeof(*status) + sizeof(*config) + stream->cc->iov_len;
status = malloc(len);
memset(status, 0, len);
status->id = ep->id;
status->state = ep->state;
/* Initialize preffered settings if not set */
if (!lpac->qos.phy)
lpac->qos.phy = 0x02;
if (!lpac->qos.rtn)
lpac->qos.rtn = 0x05;
if (!lpac->qos.latency)
lpac->qos.latency = 10;
if (!lpac->qos.pd_min)
lpac->qos.pd_min = 20000;
if (!lpac->qos.pd_max)
lpac->qos.pd_max = 40000;
if (!lpac->qos.ppd_min)
lpac->qos.ppd_min = lpac->qos.pd_min;
if (!lpac->qos.ppd_max)
lpac->qos.ppd_max = lpac->qos.pd_max;
/* TODO:Add support for setting preffered settings on bt_bap_pac */
config = (void *)status->params;
config->framing = lpac->qos.framing;
config->phy = lpac->qos.phy;
config->rtn = lpac->qos.rtn;
config->latency = cpu_to_le16(lpac->qos.latency);
put_le24(lpac->qos.pd_min, config->pd_min);
put_le24(lpac->qos.pd_max, config->pd_max);
put_le24(lpac->qos.ppd_min, config->ppd_min);
put_le24(lpac->qos.ppd_max, config->ppd_max);
config->codec = lpac->codec;
if (config->codec.id == 0x0ff) {
config->codec.vid = cpu_to_le16(config->codec.vid);
config->codec.cid = cpu_to_le16(config->codec.cid);
}
config->cc_len = stream->cc->iov_len;
memcpy(config->cc, stream->cc->iov_base, stream->cc->iov_len);
gatt_db_attribute_notify(ep->attr, (void *) status, len,
bt_bap_get_att(stream->bap));
free(status);
}
static void stream_notify_qos(struct bt_bap_stream *stream)
{
struct bt_bap_endpoint *ep = stream->ep;
struct bt_ascs_ase_status *status;
struct bt_ascs_ase_status_qos *qos;
size_t len;
DBG(stream->bap, "stream %p", stream);
len = sizeof(*status) + sizeof(*qos);
status = malloc(len);
memset(status, 0, len);
status->id = ep->id;
status->state = ep->state;
qos = (void *)status->params;
qos->cis_id = stream->qos.ucast.cis_id;
qos->cig_id = stream->qos.ucast.cig_id;
put_le24(stream->qos.ucast.io_qos.interval, qos->interval);
qos->framing = stream->qos.ucast.framing;
qos->phy = stream->qos.ucast.io_qos.phy;
qos->sdu = cpu_to_le16(stream->qos.ucast.io_qos.sdu);
qos->rtn = stream->qos.ucast.io_qos.rtn;
qos->latency = cpu_to_le16(stream->qos.ucast.io_qos.latency);
put_le24(stream->qos.ucast.delay, qos->pd);
gatt_db_attribute_notify(ep->attr, (void *) status, len,
bt_bap_get_att(stream->bap));