forked from nrfconnect/sdk-zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbap_scan_delegator.c
More file actions
1919 lines (1496 loc) · 56 KB
/
bap_scan_delegator.c
File metadata and controls
1919 lines (1496 loc) · 56 KB
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
/* Bluetooth Scan Delegator */
/*
* Copyright (c) 2019 Bose Corporation
* Copyright (c) 2021-2023 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <errno.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <sys/types.h>
#include <zephyr/autoconf.h>
#include <zephyr/bluetooth/addr.h>
#include <zephyr/bluetooth/att.h>
#include <zephyr/bluetooth/audio/audio.h>
#include <zephyr/bluetooth/audio/bap.h>
#include <zephyr/bluetooth/bluetooth.h>
#include <zephyr/bluetooth/conn.h>
#include <zephyr/bluetooth/gap.h>
#include <zephyr/bluetooth/gatt.h>
#include <zephyr/bluetooth/hci_types.h>
#include <zephyr/bluetooth/iso.h>
#include <zephyr/bluetooth/l2cap.h>
#include <zephyr/bluetooth/buf.h>
#include <zephyr/bluetooth/uuid.h>
#include <zephyr/init.h>
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
#include <zephyr/net_buf.h>
#include <zephyr/sys/__assert.h>
#include <zephyr/sys/atomic.h>
#include <zephyr/sys/byteorder.h>
#include <zephyr/sys/util.h>
#include <zephyr/sys/util_macro.h>
#include <zephyr/toolchain.h>
LOG_MODULE_REGISTER(bt_bap_scan_delegator, CONFIG_BT_BAP_SCAN_DELEGATOR_LOG_LEVEL);
#if !defined(CONFIG_ARCH_POSIX) && defined(CONFIG_BT_BAP_SCAN_DELEGATOR_LOG_LEVEL_DBG) && \
defined(CONFIG_LOG) && !defined(CONFIG_LOG_MODE_DEFERRED)
#warning Logging when non-deferred log mode is selected is not fully supported. \
Use the CONFIG_LOG_MODE_DEFERRED Kconfig option when this feature is enabled.
#endif
#include "common/bt_str.h"
#include "audio_internal.h"
#include "bap_internal.h"
#include "../host/conn_internal.h"
#include "../host/hci_core.h"
#define PAST_TIMEOUT K_SECONDS(10)
#define SCAN_DELEGATOR_BUF_SEM_TIMEOUT K_MSEC(CONFIG_BT_BAP_SCAN_DELEGATOR_BUF_TIMEOUT)
NET_BUF_SIMPLE_DEFINE_STATIC(read_buf, BT_ATT_MAX_ATTRIBUTE_LEN);
struct bass_recv_state_flags {
bool updated: 1;
};
/* TODO: Merge bass_recv_state_internal_t and bt_bap_scan_delegator_recv_state */
struct bass_recv_state_internal {
const struct bt_gatt_attr *attr;
bool active;
uint8_t index;
/* Determines whether the remote has requested a PA sync request and app has accepted */
bool pa_sync_requested;
struct bt_bap_scan_delegator_recv_state state;
uint8_t broadcast_code[BT_ISO_BROADCAST_CODE_SIZE];
/** Requested BIS sync bitfield for each subgroup */
uint32_t requested_bis_sync[CONFIG_BT_BAP_BASS_MAX_SUBGROUPS];
/* Mutex (reentrant Locking) ensure multiple threads to safely access receive state data */
struct k_mutex mutex;
struct bass_recv_state_flags flags[CONFIG_BT_MAX_CONN];
struct k_work_delayable notify_work;
};
struct bt_bap_scan_delegator_inst {
uint8_t next_src_id;
struct bass_recv_state_internal recv_states
[CONFIG_BT_BAP_SCAN_DELEGATOR_RECV_STATE_COUNT];
};
enum scan_delegator_flag {
SCAN_DELEGATOR_FLAG_REGISTERED_CONN_CB,
SCAN_DELEGATOR_FLAG_REGISTERED_SCAN_DELIGATOR,
SCAN_DELEGATOR_FLAG_NUM,
};
static ATOMIC_DEFINE(scan_delegator_flags, SCAN_DELEGATOR_FLAG_NUM);
static struct bt_bap_scan_delegator_inst scan_delegator;
static struct bt_bap_scan_delegator_cb *scan_delegator_cbs;
static void set_receive_state_changed_cb(struct bt_conn *conn, void *data)
{
struct bass_recv_state_internal *internal_state = data;
struct bass_recv_state_flags *flags = &internal_state->flags[bt_conn_index(conn)];
struct bt_conn_info conn_info;
int err;
err = bt_conn_get_info(conn, &conn_info);
__ASSERT_NO_MSG(err == 0);
if (conn_info.state != BT_CONN_STATE_CONNECTED ||
!bt_gatt_is_subscribed(conn, internal_state->attr, BT_GATT_CCC_NOTIFY)) {
return;
}
flags->updated = true;
/* We may schedule the same work multiple times, but that is OK as scheduling the same work
* multiple times is a no-op
*/
err = k_work_schedule(&internal_state->notify_work, K_NO_WAIT);
__ASSERT(err >= 0, "Failed to schedule work: %d", err);
}
static void set_receive_state_changed(struct bass_recv_state_internal *internal_state)
{
bt_conn_foreach(BT_CONN_TYPE_LE, set_receive_state_changed_cb, (void *)internal_state);
}
/**
* @brief Returns whether a value's bits is a subset of another value's bits
*
* @param a The subset to check
* @param b The bits to check against
*
* @return True if @p a is a bitwise subset of @p b
*/
static bool bits_subset_of(uint32_t a, uint32_t b)
{
return (((a) & (~(b))) == 0);
}
static bool bis_syncs_unique_or_no_pref(uint32_t requested_bis_syncs,
uint32_t aggregated_bis_syncs)
{
if (requested_bis_syncs == 0U || aggregated_bis_syncs == 0U) {
return true;
}
if (requested_bis_syncs == BT_BAP_BIS_SYNC_NO_PREF &&
aggregated_bis_syncs == BT_BAP_BIS_SYNC_NO_PREF) {
return true;
}
return (requested_bis_syncs & aggregated_bis_syncs) == 0U;
}
static bool valid_bis_sync_request(uint32_t requested_bis_syncs, uint32_t aggregated_bis_syncs)
{
/* Verify that the request BIS sync indexes are unique or no preference */
if (!bis_syncs_unique_or_no_pref(requested_bis_syncs, aggregated_bis_syncs)) {
LOG_DBG("Duplicate BIS index 0x%08x (aggregated %x)", requested_bis_syncs,
aggregated_bis_syncs);
return false;
}
if (requested_bis_syncs != BT_BAP_BIS_SYNC_NO_PREF &&
aggregated_bis_syncs == BT_BAP_BIS_SYNC_NO_PREF) {
LOG_DBG("Invalid BIS index 0x%08X mixing BT_BAP_BIS_SYNC_NO_PREF and specific BIS",
requested_bis_syncs);
return false;
}
if (!valid_bis_syncs(requested_bis_syncs)) {
LOG_DBG("Invalid BIS sync: 0x%08X", requested_bis_syncs);
return false;
}
return true;
}
static void bt_debug_dump_recv_state(struct bass_recv_state_internal *internal_state)
{
__maybe_unused int err;
err = k_mutex_lock(&internal_state->mutex, SCAN_DELEGATOR_BUF_SEM_TIMEOUT);
__ASSERT(err == 0, "Failed to lock mutex: %d", err);
if (internal_state->active) {
const struct bt_bap_scan_delegator_recv_state *state = &internal_state->state;
const bool is_bad_code = state->encrypt_state == BT_BAP_BIG_ENC_STATE_BAD_CODE;
LOG_DBG("Receive State[%d]: src ID %u, addr %s, adv_sid %u, broadcast_id 0x%06X, "
"pa_sync_state %u, encrypt state %u%s%s, num_subgroups %u",
internal_state->index, state->src_id, bt_addr_le_str(&state->addr),
state->adv_sid, state->broadcast_id, state->pa_sync_state,
state->encrypt_state, is_bad_code ? ", bad code" : "",
is_bad_code ? bt_hex(state->bad_code, sizeof(state->bad_code)) : "",
state->num_subgroups);
for (uint8_t i = 0U; i < state->num_subgroups; i++) {
const struct bt_bap_bass_subgroup *subgroup = &state->subgroups[i];
LOG_DBG("\tSubgroup[%u]: BIS sync %u (requested %u), metadata_len %zu, "
"metadata: %s",
i, subgroup->bis_sync, internal_state->requested_bis_sync[i],
subgroup->metadata_len,
bt_hex(subgroup->metadata, subgroup->metadata_len));
}
} else {
LOG_DBG("Inactive receive state");
}
err = k_mutex_unlock(&internal_state->mutex);
__ASSERT(err == 0, "Failed to unlock mutex: %d", err);
}
static void receive_state_notify_cb(struct bt_conn *conn, void *data)
{
struct bass_recv_state_internal *internal_state = data;
struct bass_recv_state_flags *flags = &internal_state->flags[bt_conn_index(conn)];
struct bt_conn_info conn_info;
int err;
err = bt_conn_get_info(conn, &conn_info);
__ASSERT_NO_MSG(err == 0);
if (conn_info.state != BT_CONN_STATE_CONNECTED ||
!bt_gatt_is_subscribed(conn, internal_state->attr, BT_GATT_CCC_NOTIFY)) {
return;
}
if (flags->updated) {
uint16_t max_ntf_size;
uint16_t ntf_size;
max_ntf_size = bt_audio_get_max_ntf_size(conn);
ntf_size = MIN(max_ntf_size, read_buf.len);
if (ntf_size < read_buf.len) {
LOG_DBG("Sending truncated notification (%u/%u)", ntf_size, read_buf.len);
}
LOG_DBG("Sending bytes %u for %p", ntf_size, (void *)conn);
err = bt_gatt_notify_uuid(conn, BT_UUID_BASS_RECV_STATE, internal_state->attr,
read_buf.data, ntf_size);
if (err == 0) {
flags->updated = false;
return;
}
LOG_DBG("Could not notify receive state: %d", err);
err = k_work_reschedule(&internal_state->notify_work,
K_USEC(conn_info.le.interval_us));
__ASSERT(err >= 0, "Failed to reschedule work: %d", err);
}
}
static void net_buf_put_recv_state(const struct bass_recv_state_internal *internal_state)
{
const struct bt_bap_scan_delegator_recv_state *state = &internal_state->state;
net_buf_simple_reset(&read_buf);
__ASSERT(internal_state, "NULL receive state");
if (!internal_state->active) {
/* Notify empty */
return;
}
(void)net_buf_simple_add_u8(&read_buf, state->src_id);
(void)net_buf_simple_add_u8(&read_buf, state->addr.type);
(void)net_buf_simple_add_mem(&read_buf, &state->addr.a,
sizeof(state->addr.a));
(void)net_buf_simple_add_u8(&read_buf, state->adv_sid);
(void)net_buf_simple_add_le24(&read_buf, state->broadcast_id);
(void)net_buf_simple_add_u8(&read_buf, state->pa_sync_state);
(void)net_buf_simple_add_u8(&read_buf, state->encrypt_state);
if (state->encrypt_state == BT_BAP_BIG_ENC_STATE_BAD_CODE) {
(void)net_buf_simple_add_mem(&read_buf, &state->bad_code,
sizeof(state->bad_code));
}
(void)net_buf_simple_add_u8(&read_buf, state->num_subgroups);
for (int i = 0; i < state->num_subgroups; i++) {
const struct bt_bap_bass_subgroup *subgroup = &state->subgroups[i];
(void)net_buf_simple_add_le32(&read_buf, subgroup->bis_sync);
(void)net_buf_simple_add_u8(&read_buf, subgroup->metadata_len);
(void)net_buf_simple_add_mem(&read_buf, subgroup->metadata,
subgroup->metadata_len);
}
}
static void receive_state_updated(struct bt_conn *conn,
struct bass_recv_state_internal *internal_state)
{
if (IS_ENABLED(CONFIG_BT_BAP_SCAN_DELEGATOR_LOG_LEVEL_DBG)) {
bt_debug_dump_recv_state(internal_state);
}
if (scan_delegator_cbs != NULL && scan_delegator_cbs->recv_state_updated != NULL) {
scan_delegator_cbs->recv_state_updated(conn, &internal_state->state);
}
}
static void notify_work_handler(struct k_work *work)
{
struct bass_recv_state_internal *internal_state = CONTAINER_OF(
k_work_delayable_from_work(work), struct bass_recv_state_internal, notify_work);
__maybe_unused int err;
err = k_mutex_lock(&internal_state->mutex, SCAN_DELEGATOR_BUF_SEM_TIMEOUT);
__ASSERT(err == 0, "Failed to lock mutex: %d", err);
net_buf_put_recv_state(internal_state);
bt_conn_foreach(BT_CONN_TYPE_LE, receive_state_notify_cb, internal_state);
err = k_mutex_unlock(&internal_state->mutex);
__ASSERT(err == 0, "Failed to unlock mutex: %d", err);
}
static void bis_sync_request_updated(struct bt_conn *conn,
const struct bass_recv_state_internal *internal_state)
{
if (scan_delegator_cbs != NULL &&
scan_delegator_cbs->bis_sync_req != NULL) {
scan_delegator_cbs->bis_sync_req(conn, &internal_state->state,
internal_state->requested_bis_sync);
} else {
LOG_WRN("bis_sync_req callback is missing");
}
}
static void scan_delegator_security_changed(struct bt_conn *conn,
bt_security_t level,
enum bt_security_err err)
{
if (err != 0 || level < BT_SECURITY_L2 || !bt_le_bond_exists(conn->id, &conn->le.dst)) {
return;
}
/* Notify all receive states after a bonded device reconnects */
for (size_t i = 0; i < ARRAY_SIZE(scan_delegator.recv_states); i++) {
const struct bass_recv_state_internal *internal_state =
&scan_delegator.recv_states[i];
if (!internal_state->active) {
continue;
}
set_receive_state_changed_cb(conn, (void *)internal_state);
}
}
BT_CONN_CB_DEFINE(conn_callbacks) = {
.security_changed = scan_delegator_security_changed,
};
static uint8_t next_src_id(void)
{
uint8_t next_src_id;
bool unique = false;
while (!unique) {
next_src_id = scan_delegator.next_src_id++;
unique = true;
for (size_t i = 0; i < ARRAY_SIZE(scan_delegator.recv_states); i++) {
if (scan_delegator.recv_states[i].active &&
scan_delegator.recv_states[i].state.src_id == next_src_id) {
unique = false;
break;
}
}
}
return next_src_id;
}
static struct bass_recv_state_internal *bass_lookup_src_id(uint8_t src_id)
{
for (size_t i = 0; i < ARRAY_SIZE(scan_delegator.recv_states); i++) {
if (scan_delegator.recv_states[i].active &&
scan_delegator.recv_states[i].state.src_id == src_id) {
return &scan_delegator.recv_states[i];
}
}
return NULL;
}
/* BAP 6.5.4 states that the combined Source_Address_Type, Source_Adv_SID, and Broadcast_ID fields
* are what makes a receive state unique.
*/
static struct bass_recv_state_internal *bass_lookup_state(uint8_t addr_type, uint8_t adv_sid,
uint32_t broadcast_id)
{
struct bass_recv_state_internal *res = NULL;
ARRAY_FOR_EACH_PTR(scan_delegator.recv_states, recv_state_internal) {
__maybe_unused int err;
if (!recv_state_internal->active) {
continue;
}
err = k_mutex_lock(&recv_state_internal->mutex, SCAN_DELEGATOR_BUF_SEM_TIMEOUT);
__ASSERT(err == 0, "Failed to lock mutex: %d", err);
if (recv_state_internal->state.addr.type == addr_type &&
recv_state_internal->state.adv_sid == adv_sid &&
recv_state_internal->state.broadcast_id == broadcast_id) {
res = recv_state_internal;
}
err = k_mutex_unlock(&recv_state_internal->mutex);
__ASSERT(err == 0, "Failed to unlock mutex: %d", err);
if (res != NULL) {
break;
}
}
return res;
}
static struct bass_recv_state_internal *get_free_recv_state(void)
{
for (size_t i = 0U; i < ARRAY_SIZE(scan_delegator.recv_states); i++) {
struct bass_recv_state_internal *free_internal_state =
&scan_delegator.recv_states[i];
if (!free_internal_state->active) {
return free_internal_state;
}
}
return NULL;
}
static void free_recv_state(struct bass_recv_state_internal *state)
{
state->active = false;
state->pa_sync_requested = false;
(void)memset(&state->state, 0, sizeof(state->state));
(void)memset(state->broadcast_code, 0, sizeof(state->broadcast_code));
(void)memset(state->requested_bis_sync, 0, sizeof(state->requested_bis_sync));
}
static bool supports_past(struct bt_conn *conn, uint8_t pa_sync_val)
{
if (IS_ENABLED(CONFIG_BT_PER_ADV_SYNC_TRANSFER_RECEIVER)) {
struct bt_le_local_features local_features;
struct bt_conn_remote_info remote_info;
int err;
err = bt_le_get_local_features(&local_features);
if (err != 0) {
LOG_DBG("Failed to get local features: %d", err);
return false;
}
err = bt_conn_get_remote_info(conn, &remote_info);
if (err != 0) {
LOG_DBG("Failed to get remote info: %d", err);
return false;
}
LOG_DBG("%p remote %s PAST, local %s PAST (req %u)", (void *)conn,
BT_FEAT_LE_PAST_SEND(remote_info.le.features) ? "supports"
: "does not support",
BT_FEAT_LE_PAST_RECV(local_features.features) ? "supports"
: "does not support",
pa_sync_val);
return pa_sync_val == BT_BAP_BASS_PA_REQ_SYNC_PAST &&
BT_FEAT_LE_PAST_SEND(remote_info.le.features) &&
BT_FEAT_LE_PAST_RECV(local_features.features);
} else {
return false;
}
}
static int pa_sync_request(struct bt_conn *conn,
const struct bt_bap_scan_delegator_recv_state *state,
uint8_t pa_sync_val, uint16_t pa_interval)
{
int err = -EACCES;
if (scan_delegator_cbs != NULL &&
scan_delegator_cbs->pa_sync_req != NULL) {
const bool past_supported = supports_past(conn, pa_sync_val);
err = scan_delegator_cbs->pa_sync_req(conn, state,
past_supported,
pa_interval);
} else {
LOG_WRN("pa_sync_req callback is missing, rejecting PA sync request");
}
return err;
}
static int pa_sync_term_request(struct bt_conn *conn,
const struct bt_bap_scan_delegator_recv_state *state)
{
int err = -EACCES;
if (scan_delegator_cbs != NULL &&
scan_delegator_cbs->pa_sync_req != NULL) {
err = scan_delegator_cbs->pa_sync_term_req(conn, state);
} else {
LOG_WRN("pa_sync_term_req callback is missing, rejecting PA sync term request");
}
return err;
}
static int scan_delegator_add_src(struct bt_conn *conn,
struct net_buf_simple *buf)
{
struct bass_recv_state_internal *internal_state = NULL;
struct bt_bap_scan_delegator_recv_state *state;
bt_addr_le_t *addr;
uint8_t pa_sync;
uint16_t pa_interval;
uint32_t aggregated_bis_syncs = 0;
uint32_t broadcast_id;
bool bis_sync_requested;
uint16_t total_len;
struct bt_bap_bass_cp_add_src *add_src;
uint8_t adv_sid;
int err;
/* subtract 1 as the opcode has already been pulled */
if (buf->len < sizeof(struct bt_bap_bass_cp_add_src) - 1) {
LOG_DBG("Invalid length %u", buf->size);
return BT_GATT_ERR(BT_ATT_ERR_WRITE_REQ_REJECTED);
}
add_src = (void *)(buf->data - 1);
total_len = sizeof(struct bt_bap_bass_cp_add_src) - 1;
for (int i = 0; i < add_src->num_subgroups; i++) {
struct bt_bap_bass_cp_subgroup *subgroup;
uint16_t index = total_len;
total_len += sizeof(struct bt_bap_bass_cp_subgroup);
if (total_len > buf->len) {
LOG_DBG("Invalid length %u", buf->len);
return BT_GATT_ERR(BT_ATT_ERR_WRITE_REQ_REJECTED);
}
subgroup = (void *)&buf->data[index];
total_len += subgroup->metadata_len;
if (total_len > buf->len) {
LOG_DBG("Invalid length %u", buf->len);
return BT_GATT_ERR(BT_ATT_ERR_WRITE_REQ_REJECTED);
}
}
if (total_len != buf->len) {
LOG_DBG("Invalid length %u", buf->len);
return BT_GATT_ERR(BT_ATT_ERR_WRITE_REQ_REJECTED);
}
addr = net_buf_simple_pull_mem(buf, sizeof(*addr));
if (addr->type > BT_ADDR_LE_RANDOM) {
LOG_DBG("Invalid address type %u", addr->type);
return BT_GATT_ERR(BT_ATT_ERR_VALUE_NOT_ALLOWED);
}
adv_sid = net_buf_simple_pull_u8(buf);
if (adv_sid > BT_GAP_SID_MAX) {
LOG_DBG("Invalid adv SID %u", adv_sid);
return BT_GATT_ERR(BT_ATT_ERR_VALUE_NOT_ALLOWED);
}
broadcast_id = net_buf_simple_pull_le24(buf);
internal_state = bass_lookup_state(addr->type, adv_sid, broadcast_id);
if (internal_state != NULL) {
LOG_DBG("Adding addr type=0x%02X adv_sid=0x%02X and broadcast_id=0x%06X would "
"result in duplication",
addr->type, adv_sid, broadcast_id);
return BT_GATT_ERR(BT_ATT_ERR_WRITE_REQ_REJECTED);
}
internal_state = get_free_recv_state();
if (internal_state == NULL) {
LOG_DBG("Could not get free receive state");
return BT_GATT_ERR(BT_ATT_ERR_INSUFFICIENT_RESOURCES);
}
err = k_mutex_lock(&internal_state->mutex, SCAN_DELEGATOR_BUF_SEM_TIMEOUT);
__ASSERT(err == 0, "Failed to lock mutex: %d", err);
state = &internal_state->state;
state->src_id = next_src_id();
bt_addr_le_copy(&state->addr, addr);
state->adv_sid = adv_sid;
state->broadcast_id = broadcast_id;
pa_sync = net_buf_simple_pull_u8(buf);
if (pa_sync > BT_BAP_BASS_PA_REQ_SYNC) {
err = k_mutex_unlock(&internal_state->mutex);
__ASSERT(err == 0, "Failed to unlock mutex: %d", err);
LOG_DBG("Invalid PA sync value %u", pa_sync);
return BT_GATT_ERR(BT_ATT_ERR_VALUE_NOT_ALLOWED);
}
pa_interval = net_buf_simple_pull_le16(buf);
state->num_subgroups = net_buf_simple_pull_u8(buf);
if (state->num_subgroups > CONFIG_BT_BAP_BASS_MAX_SUBGROUPS) {
err = k_mutex_unlock(&internal_state->mutex);
__ASSERT(err == 0, "Failed to unlock mutex: %d", err);
LOG_WRN("Too many subgroups %u/%u", state->num_subgroups,
CONFIG_BT_BAP_BASS_MAX_SUBGROUPS);
return BT_GATT_ERR(BT_ATT_ERR_INSUFFICIENT_RESOURCES);
}
bis_sync_requested = false;
for (int i = 0; i < state->num_subgroups; i++) {
struct bt_bap_bass_subgroup *subgroup = &state->subgroups[i];
uint8_t *metadata;
internal_state->requested_bis_sync[i] = net_buf_simple_pull_le32(buf);
if (internal_state->requested_bis_sync[i] &&
pa_sync == BT_BAP_BASS_PA_REQ_NO_SYNC) {
err = k_mutex_unlock(&internal_state->mutex);
__ASSERT(err == 0, "Failed to unlock mutex: %d", err);
LOG_DBG("Cannot sync to BIS without PA");
return BT_GATT_ERR(BT_ATT_ERR_VALUE_NOT_ALLOWED);
}
if (internal_state->requested_bis_sync[i] != 0U) {
bis_sync_requested = true;
}
if (!valid_bis_sync_request(internal_state->requested_bis_sync[i],
aggregated_bis_syncs)) {
err = k_mutex_unlock(&internal_state->mutex);
__ASSERT(err == 0, "Failed to unlock mutex: %d", err);
LOG_DBG("Invalid BIS Sync request[%d]", i);
return BT_GATT_ERR(BT_ATT_ERR_VALUE_NOT_ALLOWED);
}
aggregated_bis_syncs |= internal_state->requested_bis_sync[i];
subgroup->metadata_len = net_buf_simple_pull_u8(buf);
if (subgroup->metadata_len > CONFIG_BT_AUDIO_CODEC_CFG_MAX_METADATA_SIZE) {
err = k_mutex_unlock(&internal_state->mutex);
__ASSERT(err == 0, "Failed to unlock mutex: %d", err);
LOG_WRN("Metadata too long %u/%u", subgroup->metadata_len,
CONFIG_BT_AUDIO_CODEC_CFG_MAX_METADATA_SIZE);
return BT_GATT_ERR(BT_ATT_ERR_INSUFFICIENT_RESOURCES);
}
metadata = net_buf_simple_pull_mem(buf, subgroup->metadata_len);
(void)memcpy(subgroup->metadata, metadata,
subgroup->metadata_len);
}
if (scan_delegator_cbs != NULL && scan_delegator_cbs->add_source != NULL) {
/* Unlock mutex to avoid potential deadlock on app callback */
err = k_mutex_unlock(&internal_state->mutex);
__ASSERT(err == 0, "Failed to unlock mutex: %d", err);
err = scan_delegator_cbs->add_source(conn, state);
if (err != 0) {
err = k_mutex_lock(&internal_state->mutex, SCAN_DELEGATOR_BUF_SEM_TIMEOUT);
__ASSERT(err == 0, "Failed to lock mutex: %d", err);
(void)memset(state, 0, sizeof(*state));
internal_state->active = false;
err = k_mutex_unlock(&internal_state->mutex);
__ASSERT(err == 0, "Failed to unlock mutex: %d", err);
LOG_DBG("add_source callback rejected: 0x%02x", err);
return BT_GATT_ERR(BT_ATT_ERR_WRITE_REQ_REJECTED);
}
err = k_mutex_lock(&internal_state->mutex, SCAN_DELEGATOR_BUF_SEM_TIMEOUT);
__ASSERT(err == 0, "Failed to lock mutex: %d", err);
}
/* The active flag shall be set before any application callbacks, so that any calls for the
* receive state can be processed
*/
internal_state->active = true;
if (pa_sync != BT_BAP_BASS_PA_REQ_NO_SYNC) {
/* Unlock mutex to avoid potential deadlock on app callback */
err = k_mutex_unlock(&internal_state->mutex);
__ASSERT(err == 0, "Failed to unlock mutex: %d", err);
err = pa_sync_request(conn, state, pa_sync, pa_interval);
if (err != 0) {
err = k_mutex_lock(&internal_state->mutex, SCAN_DELEGATOR_BUF_SEM_TIMEOUT);
__ASSERT(err == 0, "Failed to lock mutex: %d", err);
free_recv_state(internal_state);
err = k_mutex_unlock(&internal_state->mutex);
__ASSERT(err == 0, "Failed to unlock mutex: %d", err);
LOG_DBG("PA sync %u from %p was rejected with reason %d", pa_sync,
(void *)conn, err);
return BT_GATT_ERR(BT_ATT_ERR_WRITE_REQ_REJECTED);
}
err = k_mutex_lock(&internal_state->mutex, SCAN_DELEGATOR_BUF_SEM_TIMEOUT);
__ASSERT(err == 0, "Failed to lock mutex: %d", err);
internal_state->pa_sync_requested = true;
}
set_receive_state_changed(internal_state);
err = k_mutex_unlock(&internal_state->mutex);
__ASSERT(err == 0, "Failed to unlock mutex: %d", err);
LOG_DBG("New source added");
/* app callback */
receive_state_updated(conn, internal_state);
if (bis_sync_requested) {
bis_sync_request_updated(conn, internal_state);
}
return 0;
}
static int scan_delegator_mod_src(struct bt_conn *conn,
struct net_buf_simple *buf)
{
uint32_t requested_bis_sync[CONFIG_BT_BAP_BASS_MAX_SUBGROUPS] = {};
struct bt_bap_scan_delegator_recv_state backup_state;
bool backup_pa_sync_requested;
struct bass_recv_state_internal *internal_state;
struct bt_bap_scan_delegator_recv_state *state;
uint8_t src_id;
bool state_changed = false;
uint16_t pa_interval;
uint8_t num_subgroups;
struct bt_bap_bass_subgroup
subgroups[CONFIG_BT_BAP_BASS_MAX_SUBGROUPS] = { 0 };
uint8_t pa_sync;
uint32_t aggregated_bis_syncs = 0;
bool bis_sync_change_requested;
uint16_t total_len;
struct bt_bap_bass_cp_mod_src *mod_src;
int err;
/* subtract 1 as the opcode has already been pulled */
if (buf->len < sizeof(struct bt_bap_bass_cp_mod_src) - 1) {
LOG_DBG("Invalid length %u", buf->len);
return BT_GATT_ERR(BT_ATT_ERR_WRITE_REQ_REJECTED);
}
mod_src = (void *)(buf->data - 1);
total_len = sizeof(struct bt_bap_bass_cp_mod_src) - 1;
for (int i = 0; i < mod_src->num_subgroups; i++) {
struct bt_bap_bass_cp_subgroup *subgroup;
uint16_t index = total_len;
total_len += sizeof(struct bt_bap_bass_cp_subgroup);
if (total_len > buf->len) {
LOG_DBG("Invalid length %u", buf->len);
return BT_GATT_ERR(BT_ATT_ERR_WRITE_REQ_REJECTED);
}
subgroup = (void *)&buf->data[index];
total_len += subgroup->metadata_len;
if (total_len > buf->len) {
LOG_DBG("Invalid length %u", buf->len);
return BT_GATT_ERR(BT_ATT_ERR_WRITE_REQ_REJECTED);
}
}
if (total_len != buf->len) {
LOG_DBG("Invalid length %u", buf->len);
return BT_GATT_ERR(BT_ATT_ERR_WRITE_REQ_REJECTED);
}
src_id = net_buf_simple_pull_u8(buf);
internal_state = bass_lookup_src_id(src_id);
LOG_DBG("src_id %u", src_id);
if (internal_state == NULL) {
LOG_DBG("Could not find state by src id %u", src_id);
return BT_GATT_ERR(BT_BAP_BASS_ERR_INVALID_SRC_ID);
}
pa_sync = net_buf_simple_pull_u8(buf);
if (pa_sync > BT_BAP_BASS_PA_REQ_SYNC) {
LOG_DBG("Invalid PA sync value %u", pa_sync);
return BT_GATT_ERR(BT_ATT_ERR_VALUE_NOT_ALLOWED);
}
pa_interval = net_buf_simple_pull_le16(buf);
num_subgroups = net_buf_simple_pull_u8(buf);
if (num_subgroups > CONFIG_BT_BAP_BASS_MAX_SUBGROUPS) {
LOG_WRN("Too many subgroups %u/%u", num_subgroups,
CONFIG_BT_BAP_BASS_MAX_SUBGROUPS);
return BT_GATT_ERR(BT_ATT_ERR_INSUFFICIENT_RESOURCES);
}
err = k_mutex_lock(&internal_state->mutex, SCAN_DELEGATOR_BUF_SEM_TIMEOUT);
__ASSERT(err == 0, "Failed to lock mutex: %d", err);
bis_sync_change_requested = false;
for (int i = 0; i < num_subgroups; i++) {
struct bt_bap_bass_subgroup *subgroup = &subgroups[i];
uint8_t *metadata;
requested_bis_sync[i] = net_buf_simple_pull_le32(buf);
if (requested_bis_sync[i] != 0U && pa_sync == BT_BAP_BASS_PA_REQ_NO_SYNC) {
err = k_mutex_unlock(&internal_state->mutex);
__ASSERT(err == 0, "Failed to unlock mutex: %d", err);
LOG_DBG("Cannot sync to BIS without PA");
return BT_GATT_ERR(BT_ATT_ERR_VALUE_NOT_ALLOWED);
}
/* If the BIS sync request is different than what was previously was requested, or
* different than what we are current synced to, we set bis_sync_change_requested to
* let the application know that the state may need a change
*/
if (internal_state->requested_bis_sync[i] != requested_bis_sync[i] ||
internal_state->state.subgroups[i].bis_sync != requested_bis_sync[i]) {
bis_sync_change_requested = true;
}
if (!valid_bis_sync_request(requested_bis_sync[i], aggregated_bis_syncs)) {
err = k_mutex_unlock(&internal_state->mutex);
__ASSERT(err == 0, "Failed to unlock mutex: %d", err);
LOG_DBG("Invalid BIS Sync request[%d]", i);
return BT_GATT_ERR(BT_ATT_ERR_VALUE_NOT_ALLOWED);
}
aggregated_bis_syncs |= requested_bis_sync[i];
subgroup->metadata_len = net_buf_simple_pull_u8(buf);
if (subgroup->metadata_len > CONFIG_BT_AUDIO_CODEC_CFG_MAX_METADATA_SIZE) {
err = k_mutex_unlock(&internal_state->mutex);
__ASSERT(err == 0, "Failed to unlock mutex: %d", err);
LOG_WRN("Metadata too long %u/%u", subgroup->metadata_len,
CONFIG_BT_AUDIO_CODEC_CFG_MAX_METADATA_SIZE);
return BT_GATT_ERR(BT_ATT_ERR_INSUFFICIENT_RESOURCES);
}
metadata = net_buf_simple_pull_mem(buf, subgroup->metadata_len);
(void)memcpy(subgroup->metadata, metadata,
subgroup->metadata_len);
}
/* All input has been validated; update receive state and check for changes */
state = &internal_state->state;
/* Store backup in case upper layers rejects */
(void)memcpy(&backup_state, state, sizeof(backup_state));
backup_pa_sync_requested = internal_state->pa_sync_requested;
if (state->num_subgroups != num_subgroups) {
state->num_subgroups = num_subgroups;
state_changed = true;
}
for (int i = 0; i < num_subgroups; i++) {
const bool metadata_len_changed =
subgroups[i].metadata_len != state->subgroups[i].metadata_len;
if (metadata_len_changed) {
state->subgroups[i].metadata_len = subgroups[i].metadata_len;
state_changed = true;
}
if (metadata_len_changed ||
memcmp(subgroups[i].metadata, state->subgroups[i].metadata,
sizeof(subgroups[i].metadata)) != 0) {
if (state->subgroups[i].metadata_len == 0U) {
memset(state->subgroups[i].metadata, 0,
state->subgroups[i].metadata_len);
} else {
(void)memcpy(state->subgroups[i].metadata, subgroups[i].metadata,
state->subgroups[i].metadata_len);
}
state_changed = true;
}
}
if (scan_delegator_cbs != NULL && scan_delegator_cbs->modify_source != NULL) {
/* Unlock mutex to avoid potential deadlock on app callback */
err = k_mutex_unlock(&internal_state->mutex);
__ASSERT(err == 0, "Failed to unlock mutex: %d", err);
err = scan_delegator_cbs->modify_source(conn, state);
if (err != 0) {
err = k_mutex_lock(&internal_state->mutex, SCAN_DELEGATOR_BUF_SEM_TIMEOUT);
__ASSERT(err == 0, "Failed to lock mutex: %d", err);
/* Restore backup */
(void)memcpy(state, &backup_state, sizeof(backup_state));
err = k_mutex_unlock(&internal_state->mutex);
__ASSERT(err == 0, "Failed to unlock mutex: %d", err);
LOG_DBG("Modify Source rejected with reason 0x%02x", err);
return BT_GATT_ERR(BT_ATT_ERR_WRITE_REQ_REJECTED);
}
err = k_mutex_lock(&internal_state->mutex, SCAN_DELEGATOR_BUF_SEM_TIMEOUT);
__ASSERT(err == 0, "Failed to lock mutex: %d", err);
}
/* Only send the sync request to upper layers if it is requested, and
* we have not already sent the request to the application and if we are not already synced
*/
if (pa_sync != BT_BAP_BASS_PA_REQ_NO_SYNC && !internal_state->pa_sync_requested &&
state->pa_sync_state != BT_BAP_PA_STATE_SYNCED) {
const uint8_t pa_sync_state = state->pa_sync_state;
/* Unlock mutex to avoid potential deadlock on app callback */
err = k_mutex_unlock(&internal_state->mutex);
__ASSERT(err == 0, "Failed to unlock mutex: %d", err);
err = pa_sync_request(conn, state, pa_sync, pa_interval);
if (err != 0) {
err = k_mutex_lock(&internal_state->mutex, SCAN_DELEGATOR_BUF_SEM_TIMEOUT);
__ASSERT(err == 0, "Failed to lock mutex: %d", err);
/* Restore backup */
(void)memcpy(state, &backup_state, sizeof(backup_state));
internal_state->pa_sync_requested = backup_pa_sync_requested;
err = k_mutex_unlock(&internal_state->mutex);
__ASSERT(err == 0, "Failed to unlock mutex: %d", err);
LOG_DBG("PA sync %u from %p was rejected with reason %d", pa_sync,
(void *)conn, err);
return BT_GATT_ERR(BT_ATT_ERR_WRITE_REQ_REJECTED);
} else if (pa_sync_state != state->pa_sync_state) {
/* Temporary work around if the state is changed when pa_sync_request is
* called. See https://github.com/zephyrproject-rtos/zephyr/issues/79308 for
* more information about this issue.
*/
state_changed = true;
}
err = k_mutex_lock(&internal_state->mutex, SCAN_DELEGATOR_BUF_SEM_TIMEOUT);
__ASSERT(err == 0, "Failed to lock mutex: %d", err);
internal_state->pa_sync_requested = true;
} else if (pa_sync == BT_BAP_BASS_PA_REQ_NO_SYNC &&
(internal_state->pa_sync_requested ||
state->pa_sync_state == BT_BAP_PA_STATE_INFO_REQ ||
state->pa_sync_state == BT_BAP_PA_STATE_SYNCED)) {
/* Only send sync term request if we have received a sync request, or if the state
* reflects a pending PAST or active sync
*/