forked from nrfconnect/sdk-zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbap_broadcast_assistant_test.c
More file actions
975 lines (794 loc) · 25.9 KB
/
bap_broadcast_assistant_test.c
File metadata and controls
975 lines (794 loc) · 25.9 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
/*
* Copyright (c) 2021-2025 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <zephyr/autoconf.h>
#include <zephyr/bluetooth/assigned_numbers.h>
#include <zephyr/bluetooth/att.h>
#include <zephyr/bluetooth/audio/audio.h>
#include <zephyr/bluetooth/audio/bap.h>
#include <zephyr/bluetooth/addr.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.h>
#include <zephyr/bluetooth/iso.h>
#include <zephyr/bluetooth/uuid.h>
#include <zephyr/net_buf.h>
#include <zephyr/sys/printk.h>
#include <zephyr/sys/util.h>
#include <zephyr/sys/util_macro.h>
#include "../../../../../subsys/bluetooth/host/hci_core.h"
#include "common.h"
#include "bap_common.h"
#include "bstests.h"
#include "syscalls/kernel.h"
#ifdef CONFIG_BT_BAP_BROADCAST_ASSISTANT
extern enum bst_result_t bst_result;
/* BASS variables */
static volatile uint32_t g_broadcast_id;
static volatile uint8_t g_recv_state_count;
static struct bt_bap_scan_delegator_recv_state recv_state;
CREATE_FLAG(flag_discovery_complete);
CREATE_FLAG(flag_write_complete);
CREATE_FLAG(flag_cb_called);
CREATE_FLAG(flag_broadcaster_found);
CREATE_FLAG(flag_pa_synced);
CREATE_FLAG(flag_pa_terminated);
CREATE_FLAG(flag_mtu_exchanged);
CREATE_FLAG(flag_recv_state_read);
CREATE_FLAG(flag_recv_state_updated);
CREATE_FLAG(flag_recv_state_updated_with_bis_sync);
CREATE_FLAG(flag_recv_state_removed);
CREATE_FLAG(flag_broadcast_code_requested);
CREATE_FLAG(flag_incorrect_broadcast_code);
CREATE_FLAG(flag_remove_source_cb_called);
CREATE_FLAG(flag_remove_source_rejected);
/* Broadcaster variables */
static bt_addr_le_t g_broadcaster_addr;
static struct bt_le_scan_recv_info g_broadcaster_info;
static struct bt_le_per_adv_sync *g_pa_sync;
static uint8_t metadata[] = {BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_VENDOR, LONG_META)};
static const char *phy2str(uint8_t phy)
{
switch (phy) {
case 0: return "No packets";
case BT_GAP_LE_PHY_1M: return "LE 1M";
case BT_GAP_LE_PHY_2M: return "LE 2M";
case BT_GAP_LE_PHY_CODED: return "LE Coded";
default: return "Unknown";
}
}
static void bap_broadcast_assistant_discover_cb(struct bt_conn *conn, int err,
uint8_t recv_state_count)
{
if (err != 0) {
FAIL("BASS discover failed (%d)\n", err);
return;
}
printk("BASS discover done with %u recv states\n", recv_state_count);
g_recv_state_count = recv_state_count;
SET_FLAG(flag_discovery_complete);
}
static void bap_broadcast_assistant_scan_cb(const struct bt_le_scan_recv_info *info,
uint32_t broadcast_id)
{
char le_addr[BT_ADDR_LE_STR_LEN];
bt_addr_le_to_str(info->addr, le_addr, sizeof(le_addr));
printk("Scan Recv: [DEVICE]: %s, broadcast_id 0x%06X, "
"interval (ms) %u), SID 0x%x, RSSI %i\n",
le_addr, broadcast_id, info->interval * 5 / 4,
info->sid, info->rssi);
(void)memcpy(&g_broadcaster_info, info, sizeof(g_broadcaster_info));
bt_addr_le_copy(&g_broadcaster_addr, info->addr);
g_broadcast_id = broadcast_id;
SET_FLAG(flag_broadcaster_found);
}
static bool metadata_entry(struct bt_data *data, void *user_data)
{
char metadata[CONFIG_BT_AUDIO_CODEC_CFG_MAX_METADATA_SIZE] = {0};
(void)bin2hex(data->data, data->data_len, metadata, sizeof(metadata));
printk("\t\tMetadata length %u, type %u, data: %s\n",
data->data_len, data->type, metadata);
return true;
}
static void bap_broadcast_assistant_recv_state_cb(
struct bt_conn *conn, int err,
const struct bt_bap_scan_delegator_recv_state *state)
{
char le_addr[BT_ADDR_LE_STR_LEN];
char bad_code[BT_ISO_BROADCAST_CODE_SIZE * 2 + 1];
if (err != 0) {
FAIL("BASS recv state read failed (%d)\n", err);
return;
}
SET_FLAG(flag_recv_state_read);
if (state == NULL) {
/* Empty receive state */
return;
}
bt_addr_le_to_str(&state->addr, le_addr, sizeof(le_addr));
(void)bin2hex(state->bad_code, BT_ISO_BROADCAST_CODE_SIZE, bad_code, sizeof(bad_code));
printk("BASS recv state: src_id %u, addr %s, sid %u, sync_state %u, encrypt_state %u%s%s\n",
state->src_id, le_addr, state->adv_sid, state->pa_sync_state, state->encrypt_state,
state->encrypt_state == BT_BAP_BIG_ENC_STATE_BAD_CODE ? ", bad code: " : "",
state->encrypt_state == BT_BAP_BIG_ENC_STATE_BAD_CODE ? bad_code : "");
if (state->encrypt_state == BT_BAP_BIG_ENC_STATE_BCODE_REQ) {
SET_FLAG(flag_broadcast_code_requested);
} else if (state->encrypt_state == BT_BAP_BIG_ENC_STATE_BAD_CODE) {
SET_FLAG(flag_incorrect_broadcast_code);
if (memcmp(state->bad_code, BAD_BROADCAST_CODE, BT_ISO_BROADCAST_CODE_SIZE) !=
0) {
FAIL("Bad code is not what we sent\n");
return;
}
for (uint8_t i = 0; i < state->num_subgroups; i++) {
const struct bt_bap_bass_subgroup *subgroup = &state->subgroups[i];
if (subgroup->bis_sync != 0U) {
FAIL("Invalid BIS sync value 0x%08X for bad broadcast code\n",
subgroup->bis_sync);
return;
}
}
}
for (uint8_t i = 0; i < state->num_subgroups; i++) {
const struct bt_bap_bass_subgroup *subgroup = &state->subgroups[i];
struct net_buf_simple buf;
printk("\t[%d]: BIS sync %u, metadata_len %u\n",
i, subgroup->bis_sync, subgroup->metadata_len);
net_buf_simple_init_with_data(&buf, (void *)subgroup->metadata,
subgroup->metadata_len);
bt_data_parse(&buf, metadata_entry, NULL);
if (subgroup->bis_sync != 0) {
SET_FLAG(flag_recv_state_updated_with_bis_sync);
}
}
#if defined(CONFIG_BT_PER_ADV_SYNC_TRANSFER_SENDER)
if (state->pa_sync_state == BT_BAP_PA_STATE_INFO_REQ) {
printk("Sending PAST %p to %p\n", g_pa_sync, conn);
err = bt_le_per_adv_sync_transfer(g_pa_sync, conn,
BT_UUID_BASS_VAL);
if (err != 0) {
FAIL("Could not transfer periodic adv sync: %d\n", err);
return;
}
}
#endif /* CONFIG_BT_PER_ADV_SYNC_TRANSFER_SENDER */
memcpy(&recv_state, state, sizeof(recv_state));
SET_FLAG(flag_cb_called);
SET_FLAG(flag_recv_state_updated);
}
static void bap_broadcast_assistant_recv_state_removed_cb(struct bt_conn *conn, uint8_t src_id)
{
printk("BASS recv state %u removed\n", src_id);
SET_FLAG(flag_cb_called);
SET_FLAG(flag_recv_state_removed);
}
static void bap_broadcast_assistant_scan_start_cb(struct bt_conn *conn, int err)
{
if (err != 0) {
FAIL("BASS scan start failed (%d)\n", err);
return;
}
printk("BASS scan start successful\n");
SET_FLAG(flag_write_complete);
}
static void bap_broadcast_assistant_scan_stop_cb(struct bt_conn *conn, int err)
{
if (err != 0) {
FAIL("BASS scan stop failed (%d)\n", err);
return;
}
printk("BASS scan stop successful\n");
SET_FLAG(flag_write_complete);
}
static void bap_broadcast_assistant_add_src_cb(struct bt_conn *conn, int err)
{
if (err != 0) {
FAIL("BASS add source failed (%d)\n", err);
return;
}
printk("BASS add source successful\n");
SET_FLAG(flag_write_complete);
}
static void bap_broadcast_assistant_mod_src_cb(struct bt_conn *conn, int err)
{
if (err != 0) {
FAIL("BASS modify source failed (%d)\n", err);
return;
}
printk("BASS modify source successful\n");
SET_FLAG(flag_write_complete);
}
static void bap_broadcast_assistant_broadcast_code_cb(struct bt_conn *conn, int err)
{
if (err != 0) {
FAIL("BASS broadcast code failed (%d)\n", err);
return;
}
printk("BASS broadcast code successful\n");
SET_FLAG(flag_write_complete);
}
static void bap_broadcast_assistant_rem_src_cb(struct bt_conn *conn, int err)
{
SET_FLAG(flag_remove_source_cb_called);
if (err != 0) {
if (err == BT_ATT_ERR_WRITE_REQ_REJECTED) {
SET_FLAG(flag_remove_source_rejected);
printk("Remove source rejected (expected): err=%d\n", err);
return;
}
FAIL("BASS remove source failed (err %d)\n", err);
return;
}
printk("BASS remove source successful\n");
SET_FLAG(flag_write_complete);
}
static struct bt_bap_broadcast_assistant_cb broadcast_assistant_cbs = {
.discover = bap_broadcast_assistant_discover_cb,
.scan = bap_broadcast_assistant_scan_cb,
.recv_state = bap_broadcast_assistant_recv_state_cb,
.recv_state_removed = bap_broadcast_assistant_recv_state_removed_cb,
.scan_start = bap_broadcast_assistant_scan_start_cb,
.scan_stop = bap_broadcast_assistant_scan_stop_cb,
.add_src = bap_broadcast_assistant_add_src_cb,
.mod_src = bap_broadcast_assistant_mod_src_cb,
.broadcast_code = bap_broadcast_assistant_broadcast_code_cb,
.rem_src = bap_broadcast_assistant_rem_src_cb,
};
static void att_mtu_updated(struct bt_conn *conn, uint16_t tx, uint16_t rx)
{
SET_FLAG(flag_mtu_exchanged);
}
static struct bt_gatt_cb gatt_callbacks = {
.att_mtu_updated = att_mtu_updated,
};
static void sync_cb(struct bt_le_per_adv_sync *sync,
struct bt_le_per_adv_sync_synced_info *info)
{
char le_addr[BT_ADDR_LE_STR_LEN];
bt_addr_le_to_str(info->addr, le_addr, sizeof(le_addr));
printk("PER_ADV_SYNC[%u]: [DEVICE]: %s synced, "
"Interval 0x%04x (%u ms), PHY %s\n",
bt_le_per_adv_sync_get_index(sync), le_addr, info->interval,
info->interval * 5 / 4, phy2str(info->phy));
SET_FLAG(flag_pa_synced);
}
static void term_cb(struct bt_le_per_adv_sync *sync,
const struct bt_le_per_adv_sync_term_info *info)
{
char le_addr[BT_ADDR_LE_STR_LEN];
bt_addr_le_to_str(info->addr, le_addr, sizeof(le_addr));
printk("PER_ADV_SYNC[%u]: [DEVICE]: %s sync terminated\n",
bt_le_per_adv_sync_get_index(sync), le_addr);
SET_FLAG(flag_pa_terminated);
}
static struct bt_le_per_adv_sync_cb sync_callbacks = {
.synced = sync_cb,
.term = term_cb,
};
static void test_exchange_mtu(void)
{
WAIT_FOR_FLAG(flag_mtu_exchanged);
printk("MTU exchanged\n");
}
static void update_conn_params(void)
{
/* When we are the broadcast assistant we do not know the PA interval or ISO interval, so
* set the connection parameters to something that is unlike to be a multiple of either to
* avoid issues with e.g. PAST.
* 45 fits nicely as it is not a multiple of neither BT_BAP_ADV_PARAM_BROADCAST_FAST or
* BT_BAP_ADV_PARAM_BROADCAST_SLOW, nor 7.5 ms or 10 ms for ISO
*/
int err;
UNSET_FLAG(flag_conn_updated);
err = bt_conn_le_param_update(default_conn,
BT_LE_CONN_PARAM(BT_GAP_MS_TO_CONN_INTERVAL(35),
BT_GAP_MS_TO_CONN_INTERVAL(35), 0,
BT_GAP_MS_TO_CONN_TIMEOUT(4000)));
if (err != 0) {
FAIL("Failed to update connection parameters %d\n", err);
return;
}
WAIT_FOR_FLAG(flag_conn_updated);
}
static void test_bass_discover(void)
{
int err;
printk("Discovering BASS\n");
UNSET_FLAG(flag_discovery_complete);
err = bt_bap_broadcast_assistant_discover(default_conn);
if (err != 0) {
FAIL("Failed to discover BASS %d\n", err);
return;
}
WAIT_FOR_FLAG(flag_discovery_complete);
/* Verify that we can discover again */
UNSET_FLAG(flag_discovery_complete);
err = bt_bap_broadcast_assistant_discover(default_conn);
if (err != 0) {
FAIL("Failed to discover BASS for the second time: %d\n", err);
return;
}
WAIT_FOR_FLAG(flag_discovery_complete);
printk("Discovery complete\n");
}
static void test_bass_read_receive_states(void)
{
for (uint8_t i = 0U; i < g_recv_state_count; i++) {
int err;
UNSET_FLAG(flag_recv_state_read);
err = bt_bap_broadcast_assistant_read_recv_state(default_conn, i);
if (err != 0) {
FAIL("Failed to read receive state with idx %u: %d\n",
i, err);
return;
}
WAIT_FOR_FLAG(flag_recv_state_read);
}
printk("Receive state read complete\n");
}
static void test_bass_scan_start(void)
{
int err;
printk("Starting scan\n");
UNSET_FLAG(flag_write_complete);
err = bt_bap_broadcast_assistant_scan_start(default_conn, true);
if (err != 0) {
FAIL("Could not write scan start to BASS (err %d)\n", err);
return;
}
WAIT_FOR_FLAG(flag_write_complete);
WAIT_FOR_FLAG(flag_broadcaster_found);
printk("Scan started\n");
}
static void test_bass_scan_stop(void)
{
int err;
printk("Stopping scan\n");
UNSET_FLAG(flag_write_complete);
err = bt_bap_broadcast_assistant_scan_stop(default_conn);
if (err != 0) {
FAIL("Could not write scan stop to BASS (err %d)\n", err);
return;
}
WAIT_FOR_FLAG(flag_write_complete);
printk("Scan stopped\n");
}
static void test_bass_create_pa_sync(void)
{
int err;
struct bt_le_per_adv_sync_param sync_create_param = { 0 };
printk("Creating Periodic Advertising Sync...\n");
bt_addr_le_copy(&sync_create_param.addr, &g_broadcaster_addr);
sync_create_param.sid = g_broadcaster_info.sid;
sync_create_param.timeout = 0xa;
err = bt_le_per_adv_sync_create(&sync_create_param, &g_pa_sync);
if (err != 0) {
FAIL("Could not create PA syncs (err %d)\n", err);
return;
}
WAIT_FOR_FLAG(flag_pa_synced);
printk("PA synced\n");
}
static void test_bass_add_source(void)
{
int err;
struct bt_bap_broadcast_assistant_add_src_param add_src_param = { 0 };
struct bt_bap_bass_subgroup subgroup = { 0 };
printk("Adding source\n");
UNSET_FLAG(flag_write_complete);
UNSET_FLAG(flag_cb_called);
bt_addr_le_copy(&add_src_param.addr, &g_broadcaster_addr);
add_src_param.adv_sid = g_broadcaster_info.sid;
add_src_param.num_subgroups = 1;
add_src_param.pa_interval = g_broadcaster_info.interval;
add_src_param.pa_sync = false;
add_src_param.broadcast_id = g_broadcast_id;
add_src_param.subgroups = &subgroup;
subgroup.bis_sync = 0;
subgroup.metadata_len = 0;
err = bt_bap_broadcast_assistant_add_src(default_conn, &add_src_param);
if (err != 0) {
FAIL("Could not add source (err %d)\n", err);
return;
}
WAIT_FOR_FLAG(flag_recv_state_updated);
if (!bt_addr_le_eq(&recv_state.addr, &add_src_param.addr)) {
char addr[BT_ADDR_LE_STR_LEN];
char expected_addr[BT_ADDR_LE_STR_LEN];
bt_addr_le_to_str(&recv_state.addr, addr, sizeof(addr));
bt_addr_le_to_str(&add_src_param.addr, expected_addr, sizeof(expected_addr));
FAIL("Unexpected addr %s != %s\n", addr, expected_addr);
return;
}
if (recv_state.adv_sid != add_src_param.adv_sid) {
FAIL("Unexpected SID: %u\n", recv_state.adv_sid);
return;
}
if (recv_state.pa_sync_state != BT_BAP_PA_STATE_NOT_SYNCED) {
FAIL("Unexpected PA sync state: %d\n", recv_state.pa_sync_state);
return;
}
if (recv_state.encrypt_state != BT_BAP_BIG_ENC_STATE_NO_ENC) {
FAIL("Unexpected BIG encryption state: %d\n", recv_state.pa_sync_state);
return;
}
if (recv_state.broadcast_id != add_src_param.broadcast_id) {
FAIL("Unexpected broadcast ID: 0x%06X != 0x%06X\n", recv_state.broadcast_id,
add_src_param.broadcast_id);
return;
}
if (recv_state.num_subgroups != add_src_param.num_subgroups) {
FAIL("Unexpected number of subgroups: %u\n", recv_state.num_subgroups);
return;
}
WAIT_FOR_FLAG(flag_cb_called);
WAIT_FOR_FLAG(flag_write_complete);
printk("Source added\n");
}
static void test_bass_mod_source(bool pa_sync, uint32_t bis_sync)
{
int err;
struct bt_bap_broadcast_assistant_mod_src_param mod_src_param = { 0 };
struct bt_bap_bass_subgroup subgroup = {0};
uint32_t remote_bis_sync;
printk("Modify source\n");
UNSET_FLAG(flag_cb_called);
UNSET_FLAG(flag_write_complete);
UNSET_FLAG(flag_recv_state_updated);
mod_src_param.src_id = recv_state.src_id;
mod_src_param.num_subgroups = 1;
mod_src_param.pa_sync = pa_sync;
mod_src_param.subgroups = &subgroup;
mod_src_param.pa_interval = g_broadcaster_info.interval;
subgroup.bis_sync = bis_sync;
/* Leave metadata as is */
subgroup.metadata_len = recv_state.subgroups[0].metadata_len;
memcpy(subgroup.metadata, recv_state.subgroups[0].metadata, sizeof(metadata));
err = bt_bap_broadcast_assistant_mod_src(default_conn, &mod_src_param);
if (err != 0) {
FAIL("Could not modify source (err %d)\n", err);
return;
}
if (recv_state.pa_sync_state == BT_BAP_PA_STATE_NOT_SYNCED) {
printk("Source modified, waiting for server to PA sync\n");
WAIT_FOR_AND_CLEAR_FLAG(flag_recv_state_updated);
}
if (recv_state.pa_sync_state == BT_BAP_PA_STATE_INFO_REQ) {
/* Wait for PAST to finish and then a new receive state */
printk("Waiting for PAST sync\n");
WAIT_FOR_AND_CLEAR_FLAG(flag_recv_state_updated);
}
if (pa_sync) {
if (recv_state.pa_sync_state != BT_BAP_PA_STATE_SYNCED) {
FAIL("Unexpected PA sync state: %d\n", recv_state.pa_sync_state);
return;
}
if (recv_state.encrypt_state != BT_BAP_BIG_ENC_STATE_NO_ENC) {
FAIL("Unexpected BIG encryption state: %d\n", recv_state.pa_sync_state);
return;
}
}
if (recv_state.num_subgroups != mod_src_param.num_subgroups) {
FAIL("Unexpected number of subgroups: %u\n", recv_state.num_subgroups);
return;
}
/* Wait for another notification that updates the metadata of the subgroups */
if (recv_state.subgroups[0].metadata_len == 0U) {
printk("Waiting for another receive state update with metadata\n");
WAIT_FOR_AND_CLEAR_FLAG(flag_recv_state_updated);
}
if (bis_sync != 0) {
remote_bis_sync = recv_state.subgroups[0].bis_sync;
printk("Waiting for BIS sync\n");
if (remote_bis_sync == 0U &&
recv_state.encrypt_state == BT_BAP_BIG_ENC_STATE_NO_ENC) {
/* Wait for another notification, which will either request a
* broadcast code for encrypted broadcasts, or have the BIS sync
* values set
*/
printk("Waiting for another receive state update with BIS sync\n");
WAIT_FOR_AND_CLEAR_FLAG(flag_recv_state_updated);
remote_bis_sync = recv_state.subgroups[0].bis_sync;
}
if (recv_state.encrypt_state == BT_BAP_BIG_ENC_STATE_BCODE_REQ) {
printk("Remote is requesting broadcast code\n");
if (remote_bis_sync != 0U) {
FAIL("Unexpected BIS sync value: %u", remote_bis_sync);
return;
}
} else if (recv_state.encrypt_state == BT_BAP_BIG_ENC_STATE_BAD_CODE) {
printk("Remote responded with bad code\n");
if (remote_bis_sync != 0U) {
FAIL("Unexpected BIS sync value: %u", remote_bis_sync);
return;
}
} else {
WAIT_FOR_FLAG(flag_recv_state_updated_with_bis_sync);
if (remote_bis_sync != subgroup.bis_sync) {
FAIL("Unexpected BIS sync value: %u != %u\n", remote_bis_sync,
subgroup.bis_sync);
return;
}
}
}
WAIT_FOR_FLAG(flag_cb_called);
WAIT_FOR_FLAG(flag_write_complete);
printk("Source modified\n");
}
static void test_bass_mod_source_long_meta(void)
{
int err;
struct bt_bap_broadcast_assistant_mod_src_param mod_src_param = { 0 };
struct bt_bap_bass_subgroup subgroup = { 0 };
uint32_t remote_bis_sync;
printk("Long write\n");
UNSET_FLAG(flag_cb_called);
UNSET_FLAG(flag_write_complete);
UNSET_FLAG(flag_recv_state_updated);
mod_src_param.src_id = recv_state.src_id;
mod_src_param.num_subgroups = 1;
mod_src_param.pa_sync = true;
mod_src_param.subgroups = &subgroup;
mod_src_param.pa_interval = g_broadcaster_info.interval;
subgroup.bis_sync = recv_state.subgroups[0].bis_sync;
subgroup.metadata_len = sizeof(metadata);
memcpy(subgroup.metadata, metadata, sizeof(metadata));
err = bt_bap_broadcast_assistant_mod_src(default_conn, &mod_src_param);
if (err != 0) {
FAIL("Could not modify source (err %d)\n", err);
return;
}
printk("Source modified, waiting for receive state\n");
WAIT_FOR_FLAG(flag_recv_state_updated);
remote_bis_sync = recv_state.subgroups[0].bis_sync;
if (recv_state.pa_sync_state != BT_BAP_PA_STATE_SYNCED) {
FAIL("Unexpected PA sync state: %d\n", recv_state.pa_sync_state);
return;
}
if (recv_state.encrypt_state != BT_BAP_BIG_ENC_STATE_NO_ENC) {
FAIL("Unexpected BIG encryption state: %d\n", recv_state.pa_sync_state);
return;
}
if (recv_state.num_subgroups != mod_src_param.num_subgroups) {
FAIL("Unexpected number of subgroups: %u\n", recv_state.num_subgroups);
return;
}
if (remote_bis_sync != 0U && remote_bis_sync != subgroup.bis_sync) {
FAIL("Unexpected BIS sync value: %u\n", remote_bis_sync);
return;
}
if (memcmp(recv_state.subgroups[0].metadata, subgroup.metadata, subgroup.metadata_len) !=
0) {
FAIL("Unexpected metadata (len %u / %u)\n", recv_state.subgroups[0].metadata_len,
subgroup.metadata_len);
return;
}
WAIT_FOR_FLAG(flag_cb_called);
WAIT_FOR_FLAG(flag_write_complete);
printk("Source modified with long meta\n");
}
static void test_bass_broadcast_code(const uint8_t broadcast_code[BT_ISO_BROADCAST_CODE_SIZE])
{
int err;
printk("Adding broadcast code\n");
UNSET_FLAG(flag_write_complete);
do {
err = bt_bap_broadcast_assistant_set_broadcast_code(default_conn, recv_state.src_id,
broadcast_code);
if (err == -EBUSY) {
k_sleep(BAP_RETRY_WAIT);
} else if (err != 0) {
FAIL("Could not add broadcast code (err %d)\n", err);
return;
}
} while (err == -EBUSY);
WAIT_FOR_FLAG(flag_write_complete);
printk("Broadcast code added\n");
}
static void test_bass_remove_source(void)
{
int err;
printk("Removing source\n");
UNSET_FLAG(flag_cb_called);
UNSET_FLAG(flag_write_complete);
UNSET_FLAG(flag_remove_source_cb_called);
UNSET_FLAG(flag_remove_source_rejected);
err = bt_bap_broadcast_assistant_rem_src(default_conn, recv_state.src_id);
if (err != 0) {
FAIL("Could not remove source (err %d)\n", err);
return;
}
/* Wait for the remove callback to be invoked */
WAIT_FOR_FLAG(flag_remove_source_cb_called);
if (TEST_FLAG(flag_remove_source_rejected)) {
printk("Remove source was rejected as expected\n");
return;
}
WAIT_FOR_FLAG(flag_cb_called);
WAIT_FOR_FLAG(flag_write_complete);
printk("Source removed\n");
}
static int common_init(void)
{
int err;
err = bt_enable(NULL);
if (err != 0) {
FAIL("Bluetooth enable failed (err %d)\n", err);
return err;
}
bt_gatt_cb_register(&gatt_callbacks);
bt_bap_broadcast_assistant_register_cb(&broadcast_assistant_cbs);
bt_le_per_adv_sync_cb_register(&sync_callbacks);
bt_le_scan_cb_register(&common_scan_cb);
printk("Starting scan\n");
err = bt_le_scan_start(BT_LE_SCAN_PASSIVE, NULL);
if (err != 0) {
FAIL("Scanning failed to start (err %d)\n", err);
return err;
}
printk("Scanning successfully started\n");
WAIT_FOR_FLAG(flag_connected);
update_conn_params();
test_exchange_mtu();
test_bass_discover();
test_bass_read_receive_states();
return 0;
}
static int common_deinit(void)
{
int err;
bt_le_scan_cb_unregister(&common_scan_cb);
err = bt_bap_broadcast_assistant_unregister_cb(&broadcast_assistant_cbs);
if (err != 0) {
FAIL("Bluetooth enable failed (err %d)\n", err);
return err;
}
err = bt_gatt_cb_unregister(&gatt_callbacks);
if (err != 0) {
FAIL("Failed to unregister GATT callbacks (err %d)\n", err);
return err;
}
return 0;
}
static void test_main_client_sync(void)
{
int err;
err = common_init();
if (err != 0) {
FAIL("Bluetooth enable failed (err %d)\n", err);
return;
}
test_bass_scan_start();
test_bass_scan_stop();
test_bass_create_pa_sync();
test_bass_add_source();
test_bass_mod_source(true, 0);
test_bass_mod_source_long_meta();
test_bass_mod_source(true, BT_ISO_BIS_INDEX_BIT(1) | BT_ISO_BIS_INDEX_BIT(2));
test_bass_broadcast_code(BROADCAST_CODE);
printk("Waiting for receive state with BIS sync\n");
WAIT_FOR_FLAG(flag_recv_state_updated_with_bis_sync);
test_bass_mod_source(false, 0);
test_bass_remove_source();
err = common_deinit();
if (err != 0) {
FAIL("Failed to deinitialize resources (err %d)\n", err);
return;
}
PASS("BAP Broadcast Assistant Client Sync Passed\n");
}
static void test_main_client_sync_incorrect_code(void)
{
int err;
err = common_init();
if (err != 0) {
FAIL("Bluetooth enable failed (err %d)\n", err);
return;
}
test_bass_scan_start();
test_bass_scan_stop();
test_bass_create_pa_sync();
test_bass_add_source();
test_bass_mod_source(true, BT_ISO_BIS_INDEX_BIT(1));
WAIT_FOR_FLAG(flag_broadcast_code_requested);
test_bass_broadcast_code(INCORRECT_BROADCAST_CODE);
WAIT_FOR_FLAG(flag_incorrect_broadcast_code);
test_bass_mod_source(false, 0);
test_bass_remove_source();
err = common_deinit();
if (err != 0) {
FAIL("Failed to deinitialize resources (err %d)\n", err);
return;
}
PASS("BAP Broadcast Assistant Client Sync Passed\n");
}
static void test_main_server_sync_client_rem(void)
{
int err;
err = common_init();
if (err != 0) {
FAIL("Bluetooth enable failed (err %d)\n", err);
return;
}
WAIT_FOR_FLAG(flag_recv_state_updated);
test_bass_broadcast_code(BROADCAST_CODE);
printk("Waiting for receive state with BIS sync\n");
WAIT_FOR_FLAG(flag_recv_state_updated_with_bis_sync);
printk("Attempting to remove source for the first time\n");
test_bass_mod_source(false, 0);
test_bass_remove_source();
WAIT_FOR_FLAG(flag_remove_source_rejected);
printk("First remove source attempt was rejected as expected\n");
test_bass_remove_source();
err = common_deinit();
if (err != 0) {
FAIL("Failed to deinitialize resources (err %d)\n", err);
return;
}
PASS("BAP Broadcast Assistant Server Sync Passed\n");
}
static void test_main_server_sync_server_rem(void)
{
int err;
err = common_init();
if (err != 0) {
FAIL("Bluetooth enable failed (err %d)\n", err);
return;
}
WAIT_FOR_FLAG(flag_recv_state_updated);
test_bass_broadcast_code(BROADCAST_CODE);
printk("Waiting for receive state with BIS sync\n");
WAIT_FOR_FLAG(flag_recv_state_updated_with_bis_sync);
WAIT_FOR_FLAG(flag_recv_state_removed);
err = common_deinit();
if (err != 0) {
FAIL("Failed to deinitialize resources (err %d)\n", err);
return;
}
PASS("BAP Broadcast Assistant Server Sync Passed\n");
}
static const struct bst_test_instance test_bass[] = {
{
.test_id = "bap_broadcast_assistant_client_sync",
.test_pre_init_f = test_init,
.test_tick_f = test_tick,
.test_main_f = test_main_client_sync,
},
{
.test_id = "bap_broadcast_assistant_client_sync_incorrect_code",
.test_pre_init_f = test_init,
.test_tick_f = test_tick,
.test_main_f = test_main_client_sync_incorrect_code,
},
{
.test_id = "bap_broadcast_assistant_server_sync_client_rem",
.test_pre_init_f = test_init,
.test_tick_f = test_tick,
.test_main_f = test_main_server_sync_client_rem,
},
{
.test_id = "bap_broadcast_assistant_server_sync_server_rem",
.test_pre_init_f = test_init,
.test_tick_f = test_tick,
.test_main_f = test_main_server_sync_server_rem,
},
BSTEST_END_MARKER,
};
struct bst_test_list *test_bap_broadcast_assistant_install(struct bst_test_list *tests)
{
return bst_add_tests(tests, test_bass);
}
#else
struct bst_test_list *test_bap_broadcast_assistant_install(struct bst_test_list *tests)
{
return tests;
}
#endif /* CONFIG_BT_BAP_BROADCAST_ASSISTANT */