1 /*
2 * Copyright (c) 2022-2023 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6 #include <errno.h>
7 #include <stdbool.h>
8 #include <stddef.h>
9 #include <stdio.h>
10 #include <stdint.h>
11 #include <string.h>
12
13 #include <zephyr/autoconf.h>
14 #include <zephyr/bluetooth/addr.h>
15 #include <zephyr/bluetooth/audio/aics.h>
16 #include <zephyr/bluetooth/audio/audio.h>
17 #include <zephyr/bluetooth/audio/bap.h>
18 #include <zephyr/bluetooth/audio/bap_lc3_preset.h>
19 #include <zephyr/bluetooth/audio/cap.h>
20 #include <zephyr/bluetooth/audio/csip.h>
21 #include <zephyr/bluetooth/audio/lc3.h>
22 #include <zephyr/bluetooth/audio/pacs.h>
23 #include <zephyr/bluetooth/audio/micp.h>
24 #include <zephyr/bluetooth/audio/vcp.h>
25 #include <zephyr/bluetooth/bluetooth.h>
26 #include <zephyr/bluetooth/byteorder.h>
27 #include <zephyr/bluetooth/gap.h>
28 #include <zephyr/bluetooth/iso.h>
29 #include <zephyr/bluetooth/uuid.h>
30 #include <zephyr/kernel.h>
31 #include <zephyr/net_buf.h>
32 #include <zephyr/sys/byteorder.h>
33 #include <zephyr/sys/printk.h>
34 #include <zephyr/sys/util.h>
35 #include <zephyr/sys/util_macro.h>
36
37 #include "bap_stream_rx.h"
38 #include "bstests.h"
39 #include "common.h"
40 #include "bap_common.h"
41
42 #if defined(CONFIG_BT_CAP_ACCEPTOR)
43 /* Zephyr Controller works best while Extended Advertising interval to be a multiple
44 * of the ISO Interval minus 10 ms (max. advertising random delay). This is
45 * required to place the AUX_ADV_IND PDUs in a non-overlapping interval with the
46 * Broadcast ISO radio events.
47 */
48 #define BT_LE_EXT_ADV_CONN_CUSTOM \
49 BT_LE_ADV_PARAM(BT_LE_ADV_OPT_EXT_ADV | BT_LE_ADV_OPT_CONN, \
50 BT_GAP_MS_TO_ADV_INTERVAL(140), \
51 BT_GAP_MS_TO_ADV_INTERVAL(140), \
52 NULL)
53
54 extern enum bst_result_t bst_result;
55
56 #define SINK_CONTEXT \
57 (BT_AUDIO_CONTEXT_TYPE_UNSPECIFIED | BT_AUDIO_CONTEXT_TYPE_MEDIA | \
58 BT_AUDIO_CONTEXT_TYPE_CONVERSATIONAL)
59 #define SOURCE_CONTEXT (BT_AUDIO_CONTEXT_TYPE_UNSPECIFIED | BT_AUDIO_CONTEXT_TYPE_NOTIFICATIONS)
60
61 CREATE_FLAG(flag_broadcaster_found);
62 CREATE_FLAG(flag_broadcast_code);
63 CREATE_FLAG(flag_base_received);
64 CREATE_FLAG(flag_pa_synced);
65 CREATE_FLAG(flag_syncable);
66 CREATE_FLAG(flag_pa_sync_lost);
67 CREATE_FLAG(flag_pa_request);
68 CREATE_FLAG(flag_bis_sync_requested);
69 CREATE_FLAG(flag_base_metadata_updated);
70 CREATE_FLAG(flag_unicast_stream_configured);
71
72 static struct bt_bap_broadcast_sink *g_broadcast_sink;
73 static struct bt_le_scan_recv_info broadcaster_info;
74 static bt_addr_le_t broadcaster_addr;
75 static struct bt_le_per_adv_sync *pa_sync;
76 static uint32_t broadcaster_broadcast_id;
77 static struct audio_test_stream broadcast_sink_streams[CONFIG_BT_BAP_BROADCAST_SNK_STREAM_COUNT];
78
79 static const struct bt_bap_qos_cfg_pref unicast_qos_pref =
80 BT_BAP_QOS_CFG_PREF(true, BT_GAP_LE_PHY_2M, 0u, 60u, 20000u, 40000u, 20000u, 40000u);
81
82 static bool auto_start_sink_streams;
83
84 static K_SEM_DEFINE(sem_broadcast_started, 0U, ARRAY_SIZE(broadcast_sink_streams));
85 static K_SEM_DEFINE(sem_broadcast_stopped, 0U, ARRAY_SIZE(broadcast_sink_streams));
86
87 static uint32_t bis_index_bitfield;
88
89 #define UNICAST_CHANNEL_COUNT_1 BIT(0)
90
91 static struct bt_cap_stream unicast_streams[CONFIG_BT_ASCS_MAX_ASE_SNK_COUNT +
92 CONFIG_BT_ASCS_MAX_ASE_SRC_COUNT];
93
subgroup_data_func_cb(struct bt_data * data,void * user_data)94 static bool subgroup_data_func_cb(struct bt_data *data, void *user_data)
95 {
96 bool *stream_context_found = (bool *)user_data;
97
98 printk("type %u len %u\n", data->type, data->data_len);
99
100 if (!valid_metadata_type(data->type, data->data_len)) {
101 return false;
102 }
103
104 if (data->type == BT_AUDIO_METADATA_TYPE_STREAM_CONTEXT) {
105 if (data->data_len != 2) { /* Stream context size */
106 return false;
107 }
108
109 *stream_context_found = true;
110 return false;
111 }
112
113 return true;
114 }
115
valid_subgroup_metadata_cb(const struct bt_bap_base_subgroup * subgroup,void * user_data)116 static bool valid_subgroup_metadata_cb(const struct bt_bap_base_subgroup *subgroup, void *user_data)
117 {
118 static uint8_t metadata[CONFIG_BT_AUDIO_CODEC_CFG_MAX_METADATA_SIZE];
119 static size_t metadata_size;
120 bool stream_context_found = false;
121 uint8_t *meta;
122 int ret;
123
124 ret = bt_bap_base_get_subgroup_codec_meta(subgroup, &meta);
125 if (ret < 0) {
126 FAIL("Could not get subgroup meta: %d\n", ret);
127 return false;
128 }
129
130 if (TEST_FLAG(flag_base_received) &&
131 ((size_t)ret != metadata_size || memcmp(meta, metadata, metadata_size) != 0)) {
132 printk("Metadata updated\n");
133 SET_FLAG(flag_base_metadata_updated);
134 }
135
136 metadata_size = (size_t)ret;
137
138 ret = bt_audio_data_parse(meta, (size_t)ret, subgroup_data_func_cb, &stream_context_found);
139 if (ret != 0 && ret != -ECANCELED) {
140 return false;
141 }
142
143 if (!stream_context_found) {
144 printk("Subgroup did not have streaming context\n");
145 }
146
147 /* if this is false, the iterator will return early with an error */
148 return stream_context_found;
149 }
150
base_recv_cb(struct bt_bap_broadcast_sink * sink,const struct bt_bap_base * base,size_t base_size)151 static void base_recv_cb(struct bt_bap_broadcast_sink *sink, const struct bt_bap_base *base,
152 size_t base_size)
153 {
154 /* Create a mask for the maximum BIS we can sync to using the number of
155 * broadcast_sink_streams we have. We add an additional 1 since the bis indexes
156 * start from 1 and not 0.
157 */
158 const uint32_t bis_index_mask = BIT_MASK(ARRAY_SIZE(broadcast_sink_streams) + 1U);
159 uint32_t base_bis_index_bitfield = 0U;
160 int ret;
161
162 ret = bt_bap_base_get_subgroup_count(base);
163 if (ret < 0) {
164 FAIL("Failed to get subgroup count: %d\n", ret);
165 return;
166 }
167
168 printk("Received BASE with %d subgroups from broadcast sink %p\n", ret, sink);
169
170 if (ret == 0) {
171 FAIL("subgroup_count was 0");
172 return;
173 }
174
175 ret = bt_bap_base_foreach_subgroup(base, valid_subgroup_metadata_cb, NULL);
176 if (ret != 0) {
177 FAIL("Failed to parse subgroups: %d\n", ret);
178 return;
179 }
180
181 ret = bt_bap_base_get_bis_indexes(base, &base_bis_index_bitfield);
182 if (ret != 0) {
183 FAIL("Failed to BIS indexes: %d\n", ret);
184 return;
185 }
186
187 bis_index_bitfield = base_bis_index_bitfield & bis_index_mask;
188
189 SET_FLAG(flag_base_received);
190 }
191
syncable_cb(struct bt_bap_broadcast_sink * sink,const struct bt_iso_biginfo * biginfo)192 static void syncable_cb(struct bt_bap_broadcast_sink *sink, const struct bt_iso_biginfo *biginfo)
193 {
194 printk("Broadcast sink %p syncable with%s encryption\n",
195 sink, biginfo->encryption ? "" : "out");
196 SET_FLAG(flag_syncable);
197 }
198
199 static struct bt_bap_broadcast_sink_cb broadcast_sink_cbs = {
200 .base_recv = base_recv_cb,
201 .syncable = syncable_cb,
202 };
203
scan_check_and_sync_broadcast(struct bt_data * data,void * user_data)204 static bool scan_check_and_sync_broadcast(struct bt_data *data, void *user_data)
205 {
206 const struct bt_le_scan_recv_info *info = user_data;
207 char le_addr[BT_ADDR_LE_STR_LEN];
208 struct bt_uuid_16 adv_uuid;
209 uint32_t broadcast_id;
210
211 if (TEST_FLAG(flag_broadcaster_found)) {
212 /* no-op*/
213 return false;
214 }
215
216 if (data->type != BT_DATA_SVC_DATA16) {
217 return true;
218 }
219
220 if (data->data_len < BT_UUID_SIZE_16 + BT_AUDIO_BROADCAST_ID_SIZE) {
221 return true;
222 }
223
224 if (!bt_uuid_create(&adv_uuid.uuid, data->data, BT_UUID_SIZE_16)) {
225 return true;
226 }
227
228 if (bt_uuid_cmp(&adv_uuid.uuid, BT_UUID_BROADCAST_AUDIO)) {
229 return true;
230 }
231
232 broadcast_id = sys_get_le24(data->data + BT_UUID_SIZE_16);
233
234 bt_addr_le_to_str(info->addr, le_addr, sizeof(le_addr));
235
236 printk("Found broadcaster with ID 0x%06X and addr %s and sid 0x%02X\n", broadcast_id,
237 le_addr, info->sid);
238
239 SET_FLAG(flag_broadcaster_found);
240
241 /* Store info for PA sync parameters */
242 memcpy(&broadcaster_info, info, sizeof(broadcaster_info));
243 bt_addr_le_copy(&broadcaster_addr, info->addr);
244 broadcaster_broadcast_id = broadcast_id;
245
246 /* Stop parsing */
247 return false;
248 }
249
broadcast_scan_recv(const struct bt_le_scan_recv_info * info,struct net_buf_simple * ad)250 static void broadcast_scan_recv(const struct bt_le_scan_recv_info *info, struct net_buf_simple *ad)
251 {
252 if (info->interval != 0U) {
253 bt_data_parse(ad, scan_check_and_sync_broadcast, (void *)info);
254 }
255 }
256
257 static struct bt_le_scan_cb bap_scan_cb = {
258 .recv = broadcast_scan_recv,
259 };
260
bap_pa_sync_synced_cb(struct bt_le_per_adv_sync * sync,struct bt_le_per_adv_sync_synced_info * info)261 static void bap_pa_sync_synced_cb(struct bt_le_per_adv_sync *sync,
262 struct bt_le_per_adv_sync_synced_info *info)
263 {
264 if (sync == pa_sync) {
265 printk("PA sync %p synced for broadcast sink with broadcast ID 0x%06X\n", sync,
266 broadcaster_broadcast_id);
267
268 SET_FLAG(flag_pa_synced);
269 }
270 }
271
bap_pa_sync_terminated_cb(struct bt_le_per_adv_sync * sync,const struct bt_le_per_adv_sync_term_info * info)272 static void bap_pa_sync_terminated_cb(struct bt_le_per_adv_sync *sync,
273 const struct bt_le_per_adv_sync_term_info *info)
274 {
275 if (sync == pa_sync) {
276 printk("PA sync %p lost with reason %u\n", sync, info->reason);
277 pa_sync = NULL;
278
279 SET_FLAG(flag_pa_sync_lost);
280 }
281 }
282
283 static struct bt_le_per_adv_sync_cb bap_pa_sync_cb = {
284 .synced = bap_pa_sync_synced_cb,
285 .term = bap_pa_sync_terminated_cb,
286 };
287
started_cb(struct bt_bap_stream * stream)288 static void started_cb(struct bt_bap_stream *stream)
289 {
290 struct audio_test_stream *test_stream = audio_test_stream_from_bap_stream(stream);
291
292 memset(&test_stream->last_info, 0, sizeof(test_stream->last_info));
293 test_stream->rx_cnt = 0U;
294 test_stream->seq_num = 0U;
295 test_stream->tx_cnt = 0U;
296
297 printk("Stream %p started\n", stream);
298 k_sem_give(&sem_broadcast_started);
299 }
300
stopped_cb(struct bt_bap_stream * stream,uint8_t reason)301 static void stopped_cb(struct bt_bap_stream *stream, uint8_t reason)
302 {
303 printk("Stream %p stopped with reason 0x%02X\n", stream, reason);
304 k_sem_give(&sem_broadcast_stopped);
305 }
306
307 static struct bt_bap_stream_ops broadcast_stream_ops = {
308 .started = started_cb,
309 .stopped = stopped_cb,
310 .recv = bap_stream_rx_recv_cb,
311 };
312
unicast_stream_enabled_cb(struct bt_bap_stream * stream)313 static void unicast_stream_enabled_cb(struct bt_bap_stream *stream)
314 {
315 struct bt_bap_ep_info ep_info;
316 int err;
317
318 printk("Enabled: stream %p (auto_start_sink_streams %d)\n", stream,
319 auto_start_sink_streams);
320
321 err = bt_bap_ep_get_info(stream->ep, &ep_info);
322 if (err != 0) {
323 FAIL("Failed to get ep info: %d\n", err);
324 return;
325 }
326
327 if (auto_start_sink_streams && ep_info.dir == BT_AUDIO_DIR_SINK) {
328 /* Automatically do the receiver start ready operation */
329 err = bt_bap_stream_start(stream);
330
331 if (err != 0) {
332 FAIL("Failed to start stream: %d\n", err);
333 return;
334 }
335 }
336 }
337
338 static struct bt_bap_stream_ops unicast_stream_ops = {
339 .enabled = unicast_stream_enabled_cb,
340 };
341
pa_sync_req_cb(struct bt_conn * conn,const struct bt_bap_scan_delegator_recv_state * recv_state,bool past_avail,uint16_t pa_interval)342 static int pa_sync_req_cb(struct bt_conn *conn,
343 const struct bt_bap_scan_delegator_recv_state *recv_state,
344 bool past_avail, uint16_t pa_interval)
345 {
346 if (recv_state->pa_sync_state == BT_BAP_PA_STATE_SYNCED ||
347 recv_state->pa_sync_state == BT_BAP_PA_STATE_INFO_REQ) {
348 /* Already syncing */
349 /* TODO: Terminate existing sync and then sync to new?*/
350 return -EALREADY;
351 }
352
353 printk("Sync request\n");
354
355 bt_addr_le_copy(&broadcaster_addr, &recv_state->addr);
356 broadcaster_info.sid = recv_state->adv_sid;
357 broadcaster_info.interval = pa_interval;
358
359 SET_FLAG(flag_pa_request);
360
361 return 0;
362 }
363
pa_sync_term_req_cb(struct bt_conn * conn,const struct bt_bap_scan_delegator_recv_state * recv_state)364 static int pa_sync_term_req_cb(struct bt_conn *conn,
365 const struct bt_bap_scan_delegator_recv_state *recv_state)
366 {
367 if (pa_sync == NULL || recv_state->pa_sync_state == BT_BAP_PA_STATE_NOT_SYNCED) {
368 return -EALREADY;
369 }
370
371 UNSET_FLAG(flag_pa_request);
372
373 return 0;
374 }
375
bis_sync_req_cb(struct bt_conn * conn,const struct bt_bap_scan_delegator_recv_state * recv_state,const uint32_t bis_sync_req[CONFIG_BT_BAP_BASS_MAX_SUBGROUPS])376 static int bis_sync_req_cb(struct bt_conn *conn,
377 const struct bt_bap_scan_delegator_recv_state *recv_state,
378 const uint32_t bis_sync_req[CONFIG_BT_BAP_BASS_MAX_SUBGROUPS])
379 {
380 uint32_t total_bis = 0U;
381
382 broadcaster_broadcast_id = recv_state->broadcast_id;
383
384 for (int i = 0; i < recv_state->num_subgroups; i++) {
385 total_bis |= bis_sync_req[i];
386 }
387
388 if (total_bis != 0U) {
389 SET_FLAG(flag_bis_sync_requested);
390 } else {
391 UNSET_FLAG(flag_bis_sync_requested);
392 }
393
394 return 0;
395 }
396
broadcast_code_cb(struct bt_conn * conn,const struct bt_bap_scan_delegator_recv_state * recv_state,const uint8_t broadcast_code[BT_ISO_BROADCAST_CODE_SIZE])397 static void broadcast_code_cb(struct bt_conn *conn,
398 const struct bt_bap_scan_delegator_recv_state *recv_state,
399 const uint8_t broadcast_code[BT_ISO_BROADCAST_CODE_SIZE])
400 {
401 printk("Broadcast code received for %p\n", recv_state);
402
403 if (memcmp(broadcast_code, BROADCAST_CODE, sizeof(BROADCAST_CODE)) != 0) {
404 FAIL("Failed to receive correct broadcast code\n");
405 return;
406 }
407
408 SET_FLAG(flag_broadcast_code);
409 }
410
411 static struct bt_bap_scan_delegator_cb scan_delegator_cbs = {
412 .pa_sync_req = pa_sync_req_cb,
413 .pa_sync_term_req = pa_sync_term_req_cb,
414 .bis_sync_req = bis_sync_req_cb,
415 .broadcast_code = broadcast_code_cb,
416 };
417
418 /* TODO: Expand with CAP service data */
419 static const struct bt_data cap_acceptor_ad[] = {
420 BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
421 BT_DATA(BT_DATA_NAME_COMPLETE, CONFIG_BT_DEVICE_NAME, sizeof(CONFIG_BT_DEVICE_NAME) - 1),
422 BT_DATA_BYTES(BT_DATA_UUID16_SOME, BT_UUID_16_ENCODE(BT_UUID_ASCS_VAL),
423 BT_UUID_16_ENCODE(BT_UUID_CAS_VAL)),
424 BT_DATA_BYTES(BT_DATA_SVC_DATA16, BT_UUID_16_ENCODE(BT_UUID_CAS_VAL),
425 BT_AUDIO_UNICAST_ANNOUNCEMENT_TARGETED),
426 IF_ENABLED(CONFIG_BT_BAP_UNICAST_SERVER,
427 (BT_DATA_BYTES(BT_DATA_SVC_DATA16,
428 BT_UUID_16_ENCODE(BT_UUID_ASCS_VAL),
429 BT_AUDIO_UNICAST_ANNOUNCEMENT_TARGETED,
430 BT_BYTES_LIST_LE16(SINK_CONTEXT),
431 BT_BYTES_LIST_LE16(SOURCE_CONTEXT),
432 0x00, /* Metadata length */),
433 ))
434 IF_ENABLED(CONFIG_BT_BAP_SCAN_DELEGATOR,
435 (BT_DATA_BYTES(BT_DATA_SVC_DATA16, BT_UUID_16_ENCODE(BT_UUID_BASS_VAL)),
436 ))
437 };
438
439 static struct bt_csip_set_member_svc_inst *csip_set_member;
440
unicast_stream_alloc(void)441 static struct bt_bap_stream *unicast_stream_alloc(void)
442 {
443 for (size_t i = 0; i < ARRAY_SIZE(unicast_streams); i++) {
444 struct bt_bap_stream *stream = &unicast_streams[i].bap_stream;
445
446 if (!stream->conn) {
447 return stream;
448 }
449 }
450
451 return NULL;
452 }
453
unicast_server_config(struct bt_conn * conn,const struct bt_bap_ep * ep,enum bt_audio_dir dir,const struct bt_audio_codec_cfg * codec_cfg,struct bt_bap_stream ** stream,struct bt_bap_qos_cfg_pref * const pref,struct bt_bap_ascs_rsp * rsp)454 static int unicast_server_config(struct bt_conn *conn, const struct bt_bap_ep *ep,
455 enum bt_audio_dir dir, const struct bt_audio_codec_cfg *codec_cfg,
456 struct bt_bap_stream **stream,
457 struct bt_bap_qos_cfg_pref *const pref,
458 struct bt_bap_ascs_rsp *rsp)
459 {
460 printk("ASE Codec Config: conn %p ep %p dir %u\n", conn, ep, dir);
461
462 print_codec_cfg(codec_cfg);
463
464 *stream = unicast_stream_alloc();
465 if (*stream == NULL) {
466 printk("No streams available\n");
467 *rsp = BT_BAP_ASCS_RSP(BT_BAP_ASCS_RSP_CODE_NO_MEM, BT_BAP_ASCS_REASON_NONE);
468
469 return -ENOMEM;
470 }
471
472 printk("ASE Codec Config stream %p\n", *stream);
473
474 SET_FLAG(flag_unicast_stream_configured);
475
476 *pref = unicast_qos_pref;
477
478 return 0;
479 }
480
unicast_server_reconfig(struct bt_bap_stream * stream,enum bt_audio_dir dir,const struct bt_audio_codec_cfg * codec_cfg,struct bt_bap_qos_cfg_pref * const pref,struct bt_bap_ascs_rsp * rsp)481 static int unicast_server_reconfig(struct bt_bap_stream *stream, enum bt_audio_dir dir,
482 const struct bt_audio_codec_cfg *codec_cfg,
483 struct bt_bap_qos_cfg_pref *const pref,
484 struct bt_bap_ascs_rsp *rsp)
485 {
486 printk("ASE Codec Reconfig: stream %p\n", stream);
487
488 print_codec_cfg(codec_cfg);
489
490 *pref = unicast_qos_pref;
491
492 *rsp = BT_BAP_ASCS_RSP(BT_BAP_ASCS_RSP_CODE_CONF_UNSUPPORTED, BT_BAP_ASCS_REASON_NONE);
493
494 /* We only support one QoS at the moment, reject changes */
495 return -ENOEXEC;
496 }
497
unicast_server_qos(struct bt_bap_stream * stream,const struct bt_bap_qos_cfg * qos,struct bt_bap_ascs_rsp * rsp)498 static int unicast_server_qos(struct bt_bap_stream *stream, const struct bt_bap_qos_cfg *qos,
499 struct bt_bap_ascs_rsp *rsp)
500 {
501 printk("QoS: stream %p qos %p\n", stream, qos);
502
503 print_qos(qos);
504
505 return 0;
506 }
507
ascs_data_func_cb(struct bt_data * data,void * user_data)508 static bool ascs_data_func_cb(struct bt_data *data, void *user_data)
509 {
510 struct bt_bap_ascs_rsp *rsp = (struct bt_bap_ascs_rsp *)user_data;
511
512 if (!BT_AUDIO_METADATA_TYPE_IS_KNOWN(data->type)) {
513 printk("Invalid metadata type %u or length %u\n", data->type, data->data_len);
514 *rsp = BT_BAP_ASCS_RSP(BT_BAP_ASCS_RSP_CODE_METADATA_REJECTED, data->type);
515 return false;
516 }
517
518 return true;
519 }
520
unicast_server_enable(struct bt_bap_stream * stream,const uint8_t meta[],size_t meta_len,struct bt_bap_ascs_rsp * rsp)521 static int unicast_server_enable(struct bt_bap_stream *stream, const uint8_t meta[],
522 size_t meta_len, struct bt_bap_ascs_rsp *rsp)
523 {
524 printk("Enable: stream %p meta_len %zu\n", stream, meta_len);
525
526 return bt_audio_data_parse(meta, meta_len, ascs_data_func_cb, rsp);
527 }
528
unicast_server_start(struct bt_bap_stream * stream,struct bt_bap_ascs_rsp * rsp)529 static int unicast_server_start(struct bt_bap_stream *stream, struct bt_bap_ascs_rsp *rsp)
530 {
531 printk("Start: stream %p\n", stream);
532
533 return 0;
534 }
535
unicast_server_metadata(struct bt_bap_stream * stream,const uint8_t meta[],size_t meta_len,struct bt_bap_ascs_rsp * rsp)536 static int unicast_server_metadata(struct bt_bap_stream *stream, const uint8_t meta[],
537 size_t meta_len, struct bt_bap_ascs_rsp *rsp)
538 {
539 printk("Metadata: stream %p meta_len %zu\n", stream, meta_len);
540
541 return bt_audio_data_parse(meta, meta_len, ascs_data_func_cb, rsp);
542 }
543
unicast_server_disable(struct bt_bap_stream * stream,struct bt_bap_ascs_rsp * rsp)544 static int unicast_server_disable(struct bt_bap_stream *stream, struct bt_bap_ascs_rsp *rsp)
545 {
546 printk("Disable: stream %p\n", stream);
547
548 return 0;
549 }
550
unicast_server_stop(struct bt_bap_stream * stream,struct bt_bap_ascs_rsp * rsp)551 static int unicast_server_stop(struct bt_bap_stream *stream, struct bt_bap_ascs_rsp *rsp)
552 {
553 printk("Stop: stream %p\n", stream);
554
555 return 0;
556 }
557
unicast_server_release(struct bt_bap_stream * stream,struct bt_bap_ascs_rsp * rsp)558 static int unicast_server_release(struct bt_bap_stream *stream, struct bt_bap_ascs_rsp *rsp)
559 {
560 printk("Release: stream %p\n", stream);
561
562 return 0;
563 }
564
565 static struct bt_bap_unicast_server_register_param param = {
566 CONFIG_BT_ASCS_MAX_ASE_SNK_COUNT,
567 CONFIG_BT_ASCS_MAX_ASE_SRC_COUNT
568 };
569
570 static struct bt_bap_unicast_server_cb unicast_server_cbs = {
571 .config = unicast_server_config,
572 .reconfig = unicast_server_reconfig,
573 .qos = unicast_server_qos,
574 .enable = unicast_server_enable,
575 .start = unicast_server_start,
576 .metadata = unicast_server_metadata,
577 .disable = unicast_server_disable,
578 .stop = unicast_server_stop,
579 .release = unicast_server_release,
580 };
581
set_location(void)582 static void set_location(void)
583 {
584 int err;
585
586 if (IS_ENABLED(CONFIG_BT_PAC_SNK_LOC)) {
587 err = bt_pacs_set_location(BT_AUDIO_DIR_SINK,
588 BT_AUDIO_LOCATION_FRONT_CENTER);
589 if (err != 0) {
590 FAIL("Failed to set sink location (err %d)\n", err);
591 return;
592 }
593 }
594
595 if (IS_ENABLED(CONFIG_BT_PAC_SRC_LOC)) {
596 err = bt_pacs_set_location(BT_AUDIO_DIR_SOURCE,
597 BT_AUDIO_LOCATION_FRONT_LEFT |
598 BT_AUDIO_LOCATION_FRONT_RIGHT);
599 if (err != 0) {
600 FAIL("Failed to set source location (err %d)\n", err);
601 return;
602 }
603 }
604
605 printk("Location successfully set\n");
606 }
607
set_supported_contexts(void)608 static int set_supported_contexts(void)
609 {
610 int err;
611
612 if (IS_ENABLED(CONFIG_BT_PAC_SNK)) {
613 err = bt_pacs_set_supported_contexts(BT_AUDIO_DIR_SINK, SINK_CONTEXT);
614 if (err != 0) {
615 printk("Failed to set sink supported contexts (err %d)\n",
616 err);
617
618 return err;
619 }
620 }
621
622 if (IS_ENABLED(CONFIG_BT_PAC_SRC)) {
623 err = bt_pacs_set_supported_contexts(BT_AUDIO_DIR_SOURCE, SOURCE_CONTEXT);
624 if (err != 0) {
625 printk("Failed to set source supported contexts (err %d)\n",
626 err);
627
628 return err;
629 }
630 }
631
632 printk("Supported contexts successfully set\n");
633
634 return 0;
635 }
636
test_start_adv(void)637 void test_start_adv(void)
638 {
639 int err;
640 struct bt_le_ext_adv *ext_adv;
641
642 /* Create a connectable non-scannable advertising set */
643 err = bt_le_ext_adv_create(BT_LE_EXT_ADV_CONN_CUSTOM, NULL, &ext_adv);
644 if (err != 0) {
645 FAIL("Failed to create advertising set (err %d)\n", err);
646
647 return;
648 }
649
650 /* Add cap acceptor advertising data */
651 err = bt_le_ext_adv_set_data(ext_adv, cap_acceptor_ad, ARRAY_SIZE(cap_acceptor_ad), NULL,
652 0);
653 if (err != 0) {
654 FAIL("Failed to set advertising data (err %d)\n", err);
655
656 return;
657 }
658
659 err = bt_le_ext_adv_start(ext_adv, BT_LE_EXT_ADV_START_DEFAULT);
660 if (err != 0) {
661 FAIL("Failed to start advertising set (err %d)\n", err);
662
663 return;
664 }
665 }
666
set_available_contexts(void)667 static void set_available_contexts(void)
668 {
669 int err;
670
671 err = bt_pacs_set_available_contexts(BT_AUDIO_DIR_SINK, SINK_CONTEXT);
672 if (IS_ENABLED(CONFIG_BT_PAC_SNK) && err != 0) {
673 FAIL("Failed to set sink available contexts (err %d)\n", err);
674 return;
675 }
676
677 err = bt_pacs_set_available_contexts(BT_AUDIO_DIR_SOURCE, SOURCE_CONTEXT);
678 if (IS_ENABLED(CONFIG_BT_PAC_SRC) && err != 0) {
679 FAIL("Failed to set source available contexts (err %d)\n", err);
680 return;
681 }
682
683 printk("Available contexts successfully set\n");
684 }
685
init(void)686 static void init(void)
687 {
688 const struct bt_csip_set_member_register_param csip_set_member_param = {
689 .set_size = 3,
690 .rank = 1,
691 .lockable = true,
692 /* Using the CSIP_SET_MEMBER test sample SIRK */
693 .sirk = { 0xcd, 0xcc, 0x72, 0xdd, 0x86, 0x8c, 0xcd, 0xce,
694 0x22, 0xfd, 0xa1, 0x21, 0x09, 0x7d, 0x7d, 0x45 },
695 };
696 static const struct bt_audio_codec_cap codec_cap = BT_AUDIO_CODEC_CAP_LC3(
697 BT_AUDIO_CODEC_CAP_FREQ_ANY, BT_AUDIO_CODEC_CAP_DURATION_ANY,
698 BT_AUDIO_CODEC_CAP_CHAN_COUNT_SUPPORT(1, 2), 30, 240, 2,
699 (BT_AUDIO_CONTEXT_TYPE_CONVERSATIONAL | BT_AUDIO_CONTEXT_TYPE_MEDIA));
700 int err;
701
702 err = bt_enable(NULL);
703 if (err != 0) {
704 FAIL("Bluetooth enable failed (err %d)\n", err);
705 return;
706 }
707
708 printk("Bluetooth initialized\n");
709
710 if (IS_ENABLED(CONFIG_BT_CAP_ACCEPTOR_SET_MEMBER)) {
711 err = bt_cap_acceptor_register(&csip_set_member_param, &csip_set_member);
712 if (err != 0) {
713 FAIL("CAP acceptor failed to register (err %d)\n", err);
714 return;
715 }
716 }
717
718 if (IS_ENABLED(CONFIG_BT_BAP_UNICAST_SERVER)) {
719 static struct bt_pacs_cap unicast_cap = {
720 .codec_cap = &codec_cap,
721 };
722
723 err = bt_pacs_cap_register(BT_AUDIO_DIR_SINK, &unicast_cap);
724 if (err != 0) {
725 FAIL("Broadcast capability register failed (err %d)\n",
726 err);
727
728 return;
729 }
730
731 err = bt_pacs_cap_register(BT_AUDIO_DIR_SOURCE, &unicast_cap);
732 if (err != 0) {
733 FAIL("Broadcast capability register failed (err %d)\n", err);
734
735 return;
736 }
737
738 err = bt_bap_unicast_server_register(¶m);
739 if (err != 0) {
740 FAIL("Failed to register unicast server (err %d)\n", err);
741
742 return;
743 }
744
745 err = bt_bap_unicast_server_register_cb(&unicast_server_cbs);
746 if (err != 0) {
747 FAIL("Failed to register unicast server callbacks (err %d)\n",
748 err);
749
750 return;
751 }
752
753 for (size_t i = 0U; i < ARRAY_SIZE(unicast_streams); i++) {
754 bt_cap_stream_ops_register(&unicast_streams[i], &unicast_stream_ops);
755 }
756 }
757
758 if (IS_ENABLED(CONFIG_BT_BAP_BROADCAST_SINK)) {
759 static struct bt_pacs_cap broadcast_cap = {
760 .codec_cap = &codec_cap,
761 };
762
763 err = bt_pacs_cap_register(BT_AUDIO_DIR_SINK, &broadcast_cap);
764 if (err != 0) {
765 FAIL("Broadcast capability register failed (err %d)\n",
766 err);
767
768 return;
769 }
770
771 err = bt_bap_scan_delegator_register(&scan_delegator_cbs);
772 if (err != 0) {
773 FAIL("Scan deligator register failed (err %d)\n", err);
774
775 return;
776 }
777
778 bt_bap_broadcast_sink_register_cb(&broadcast_sink_cbs);
779 bt_le_per_adv_sync_cb_register(&bap_pa_sync_cb);
780 bt_le_scan_cb_register(&bap_scan_cb);
781
782 UNSET_FLAG(flag_broadcaster_found);
783 UNSET_FLAG(flag_broadcast_code);
784 UNSET_FLAG(flag_base_received);
785 UNSET_FLAG(flag_pa_synced);
786 UNSET_FLAG(flag_pa_request);
787 UNSET_FLAG(flag_audio_received);
788 UNSET_FLAG(flag_base_metadata_updated);
789 UNSET_FLAG(flag_bis_sync_requested);
790
791 for (size_t i = 0U; i < ARRAY_SIZE(broadcast_sink_streams); i++) {
792 bt_cap_stream_ops_register(
793 cap_stream_from_audio_test_stream(&broadcast_sink_streams[i]),
794 &broadcast_stream_ops);
795 }
796 }
797
798 if (IS_ENABLED(CONFIG_BT_PACS)) {
799 set_supported_contexts();
800 set_available_contexts();
801 set_location();
802 }
803
804 if (IS_ENABLED(CONFIG_BT_VCP_VOL_REND)) {
805 char output_desc[CONFIG_BT_VCP_VOL_REND_VOCS_INSTANCE_COUNT][16];
806 char input_desc[CONFIG_BT_VCP_VOL_REND_AICS_INSTANCE_COUNT][16];
807 struct bt_vcp_vol_rend_register_param vcp_param = {0};
808
809 for (size_t i = 0U; i < ARRAY_SIZE(vcp_param.vocs_param); i++) {
810 vcp_param.vocs_param[i].location_writable = true;
811 vcp_param.vocs_param[i].desc_writable = true;
812 snprintf(output_desc[i], sizeof(output_desc[i]), "Output %d", i + 1);
813 vcp_param.vocs_param[i].output_desc = output_desc[i];
814 vcp_param.vocs_param[i].cb = NULL;
815 }
816
817 for (size_t i = 0U; i < ARRAY_SIZE(vcp_param.aics_param); i++) {
818 vcp_param.aics_param[i].desc_writable = true;
819 snprintf(input_desc[i], sizeof(input_desc[i]), "VCP Input %d", i + 1);
820 vcp_param.aics_param[i].description = input_desc[i];
821 vcp_param.aics_param[i].type = BT_AICS_INPUT_TYPE_DIGITAL;
822 vcp_param.aics_param[i].status = true;
823 vcp_param.aics_param[i].gain_mode = BT_AICS_MODE_MANUAL;
824 vcp_param.aics_param[i].units = 1;
825 vcp_param.aics_param[i].min_gain = 0;
826 vcp_param.aics_param[i].max_gain = 100;
827 vcp_param.aics_param[i].cb = NULL;
828 }
829
830 vcp_param.step = 1;
831 vcp_param.mute = BT_VCP_STATE_UNMUTED;
832 vcp_param.volume = 100;
833 vcp_param.cb = NULL;
834 err = bt_vcp_vol_rend_register(&vcp_param);
835 if (err != 0) {
836 FAIL("Failed to register VCS (err %d)\n", err);
837
838 return;
839 }
840 }
841
842 if (IS_ENABLED(CONFIG_BT_MICP_MIC_DEV)) {
843 struct bt_micp_mic_dev_register_param micp_param = {0};
844
845 #if defined(CONFIG_BT_MICP_MIC_DEV_AICS)
846 char input_desc[CONFIG_BT_MICP_MIC_DEV_AICS_INSTANCE_COUNT][16];
847
848 for (int i = 0; i < ARRAY_SIZE(micp_param.aics_param); i++) {
849 micp_param.aics_param[i].desc_writable = true;
850 snprintf(input_desc[i], sizeof(input_desc[i]), "MICP Input %d", i + 1);
851 micp_param.aics_param[i].description = input_desc[i];
852 micp_param.aics_param[i].type = BT_AICS_INPUT_TYPE_DIGITAL;
853 micp_param.aics_param[i].status = true;
854 micp_param.aics_param[i].gain_mode = BT_AICS_MODE_MANUAL;
855 micp_param.aics_param[i].units = 1;
856 micp_param.aics_param[i].min_gain = 0;
857 micp_param.aics_param[i].max_gain = 100;
858 micp_param.aics_param[i].cb = NULL;
859 }
860 #endif /* CONFIG_BT_MICP_MIC_DEV_AICS */
861
862 err = bt_micp_mic_dev_register(&micp_param);
863 if (err != 0) {
864 FAIL("Failed to register MICS (err %d)\n", err);
865 return;
866 }
867 }
868 }
869
test_cap_acceptor_unicast(void)870 static void test_cap_acceptor_unicast(void)
871 {
872 init();
873
874 test_start_adv();
875
876 auto_start_sink_streams = true;
877
878 /* TODO: wait for audio stream to pass */
879
880 WAIT_FOR_FLAG(flag_connected);
881
882 PASS("CAP acceptor unicast passed\n");
883 }
884
test_cap_acceptor_unicast_timeout(void)885 static void test_cap_acceptor_unicast_timeout(void)
886 {
887 init();
888
889 test_start_adv();
890
891 auto_start_sink_streams = false; /* Cause unicast_audio_start timeout */
892
893 /* TODO: wait for audio stream to pass */
894
895 WAIT_FOR_FLAG(flag_connected);
896
897 PASS("CAP acceptor unicast passed\n");
898 }
899
pa_sync_create(void)900 static void pa_sync_create(void)
901 {
902 struct bt_le_per_adv_sync_param create_params = {0};
903 int err;
904
905 bt_addr_le_copy(&create_params.addr, &broadcaster_addr);
906 create_params.options = BT_LE_PER_ADV_SYNC_OPT_FILTER_DUPLICATE;
907 create_params.sid = broadcaster_info.sid;
908 create_params.skip = PA_SYNC_SKIP;
909 create_params.timeout = interval_to_sync_timeout(broadcaster_info.interval);
910
911 err = bt_le_per_adv_sync_create(&create_params, &pa_sync);
912 if (err != 0) {
913 FAIL("Could not create Broadcast PA sync: %d\n", err);
914 return;
915 }
916
917 printk("Broadcast source found, waiting for PA sync\n");
918 WAIT_FOR_FLAG(flag_pa_synced);
919 }
920
pa_sync_to_broadcaster(void)921 static void pa_sync_to_broadcaster(void)
922 {
923 int err;
924
925 printk("Scanning for broadcast sources\n");
926 err = bt_le_scan_start(BT_LE_SCAN_ACTIVE, NULL);
927 if (err != 0) {
928 FAIL("Unable to start scan for broadcast sources: %d", err);
929 return;
930 }
931
932 WAIT_FOR_FLAG(flag_broadcaster_found);
933
934 printk("Broadcast source found, stopping scan\n");
935 err = bt_le_scan_stop();
936 if (err != 0) {
937 FAIL("bt_le_scan_stop failed with %d\n", err);
938 return;
939 }
940
941 printk("Scan stopped, attempting to PA sync to the broadcaster with id 0x%06X\n",
942 broadcaster_broadcast_id);
943
944 pa_sync_create();
945 }
946
create_and_sync_sink(struct bt_bap_stream * bap_streams[],size_t * stream_count)947 static void create_and_sync_sink(struct bt_bap_stream *bap_streams[], size_t *stream_count)
948 {
949 int err;
950
951 printk("Creating the broadcast sink\n");
952 err = bt_bap_broadcast_sink_create(pa_sync, broadcaster_broadcast_id, &g_broadcast_sink);
953 if (err != 0) {
954 FAIL("Unable to create the sink: %d\n", err);
955 return;
956 }
957
958 printk("Broadcast source PA synced, waiting for BASE\n");
959 WAIT_FOR_FLAG(flag_base_received);
960 printk("BASE received\n");
961
962 printk("Waiting for BIG syncable\n");
963 WAIT_FOR_FLAG(flag_syncable);
964
965 for (size_t i = 0U; i < ARRAY_SIZE(broadcast_sink_streams); i++) {
966 bap_streams[i] = bap_stream_from_audio_test_stream(&broadcast_sink_streams[i]);
967 }
968
969 printk("Syncing the sink\n");
970 *stream_count = 0;
971 for (int i = 1; i < BT_ISO_MAX_GROUP_ISO_COUNT; i++) {
972 if ((bis_index_bitfield & BIT(i)) != 0) {
973 *stream_count += 1;
974 }
975 }
976
977 err = bt_bap_broadcast_sink_sync(g_broadcast_sink, bis_index_bitfield, bap_streams, NULL);
978 if (err != 0) {
979 FAIL("Unable to sync the sink: %d\n", err);
980 return;
981 }
982
983 /* Wait for all to be started */
984 printk("Waiting for %zu streams to be started\n", *stream_count);
985 for (size_t i = 0U; i < *stream_count; i++) {
986 k_sem_take(&sem_broadcast_started, K_FOREVER);
987 }
988 }
989
sink_wait_for_data(void)990 static void sink_wait_for_data(void)
991 {
992 printk("Waiting for data\n");
993 WAIT_FOR_FLAG(flag_audio_received);
994 backchannel_sync_send_all(); /* let other devices know we have received what we wanted */
995 }
996
base_wait_for_metadata_update(void)997 static void base_wait_for_metadata_update(void)
998 {
999 printk("Waiting for meta update\n");
1000 WAIT_FOR_FLAG(flag_base_metadata_updated);
1001 backchannel_sync_send_all(); /* let others know we have received a metadata update */
1002 }
1003
wait_for_broadcast_code(void)1004 static void wait_for_broadcast_code(void)
1005 {
1006 printk("Waiting for broadcast code\n");
1007 WAIT_FOR_FLAG(flag_broadcast_code);
1008 }
1009
wait_for_streams_stop(int stream_count)1010 static void wait_for_streams_stop(int stream_count)
1011 {
1012 /* The order of PA sync lost and BIG Sync lost is irrelevant
1013 * and depend on timeout parameters. We just wait for PA first, but
1014 * either way will work.
1015 */
1016 printk("Waiting for PA disconnected\n");
1017 WAIT_FOR_FLAG(flag_pa_sync_lost);
1018
1019 printk("Waiting for %zu streams to be stopped\n", stream_count);
1020 for (size_t i = 0U; i < stream_count; i++) {
1021 k_sem_take(&sem_broadcast_stopped, K_FOREVER);
1022 }
1023 }
1024
test_cap_acceptor_broadcast(void)1025 static void test_cap_acceptor_broadcast(void)
1026 {
1027 static struct bt_bap_stream *bap_streams[ARRAY_SIZE(broadcast_sink_streams)];
1028 size_t stream_count;
1029
1030 init();
1031
1032 pa_sync_to_broadcaster();
1033
1034 create_and_sync_sink(bap_streams, &stream_count);
1035
1036 sink_wait_for_data();
1037
1038 wait_for_streams_stop(stream_count);
1039
1040 PASS("CAP acceptor broadcast passed\n");
1041 }
1042
test_cap_acceptor_broadcast_reception(void)1043 static void test_cap_acceptor_broadcast_reception(void)
1044 {
1045 static struct bt_bap_stream *bap_streams[ARRAY_SIZE(broadcast_sink_streams)];
1046 size_t stream_count;
1047 int err;
1048
1049 init();
1050
1051 test_start_adv();
1052
1053 WAIT_FOR_FLAG(flag_pa_request);
1054 WAIT_FOR_FLAG(flag_bis_sync_requested);
1055
1056 pa_sync_create();
1057
1058 create_and_sync_sink(bap_streams, &stream_count);
1059
1060 wait_for_broadcast_code();
1061 sink_wait_for_data();
1062
1063 /* Since we are re-using the BAP broadcast source test
1064 * we get a metadata update
1065 */
1066 base_wait_for_metadata_update();
1067
1068 /* when flag_bis_sync_requested is unset the bis_sync for all subgroups were set to 0 */
1069 WAIT_FOR_UNSET_FLAG(flag_bis_sync_requested);
1070
1071 UNSET_FLAG(flag_syncable);
1072 err = bt_bap_broadcast_sink_stop(g_broadcast_sink);
1073 if (err != 0) {
1074 FAIL("Unable to stop the sink: %d\n", err);
1075 return;
1076 }
1077
1078 WAIT_FOR_FLAG(flag_syncable);
1079 /* Although in theory we can now sync, in practice we can not since all BIS-syncs are 0 */
1080 backchannel_sync_send_all(); /* signal that we have stopped listening */
1081
1082 wait_for_streams_stop(stream_count);
1083
1084 PASS("CAP acceptor broadcast reception passed\n");
1085 }
1086
test_cap_acceptor_capture_and_render(void)1087 static void test_cap_acceptor_capture_and_render(void)
1088 {
1089 init();
1090
1091 test_start_adv();
1092
1093 WAIT_FOR_FLAG(flag_connected);
1094
1095 PASS("CAP acceptor unicast passed\n");
1096 }
1097
1098 static const struct bst_test_instance test_cap_acceptor[] = {
1099 {
1100 .test_id = "cap_acceptor_unicast",
1101 .test_pre_init_f = test_init,
1102 .test_tick_f = test_tick,
1103 .test_main_f = test_cap_acceptor_unicast,
1104 },
1105 {
1106 .test_id = "cap_acceptor_unicast_timeout",
1107 .test_pre_init_f = test_init,
1108 .test_tick_f = test_tick,
1109 .test_main_f = test_cap_acceptor_unicast_timeout,
1110 },
1111 {
1112 .test_id = "cap_acceptor_broadcast",
1113 .test_pre_init_f = test_init,
1114 .test_tick_f = test_tick,
1115 .test_main_f = test_cap_acceptor_broadcast,
1116 },
1117 {
1118 .test_id = "cap_acceptor_broadcast_reception",
1119 .test_pre_init_f = test_init,
1120 .test_tick_f = test_tick,
1121 .test_main_f = test_cap_acceptor_broadcast_reception,
1122 },
1123 {
1124 .test_id = "cap_acceptor_capture_and_render",
1125 .test_pre_init_f = test_init,
1126 .test_tick_f = test_tick,
1127 .test_main_f = test_cap_acceptor_capture_and_render,
1128 },
1129 BSTEST_END_MARKER,
1130 };
1131
test_cap_acceptor_install(struct bst_test_list * tests)1132 struct bst_test_list *test_cap_acceptor_install(struct bst_test_list *tests)
1133 {
1134 return bst_add_tests(tests, test_cap_acceptor);
1135 }
1136
1137 #else /* !(CONFIG_BT_CAP_ACCEPTOR) */
1138
test_cap_acceptor_install(struct bst_test_list * tests)1139 struct bst_test_list *test_cap_acceptor_install(struct bst_test_list *tests)
1140 {
1141 return tests;
1142 }
1143
1144 #endif /* CONFIG_BT_CAP_ACCEPTOR */
1145