Skip to content

Commit b635a47

Browse files
Thalleynashif
authored andcommitted
tests: Bluetooth: CAP: Add bsim test for unicast_audio_cancel
Add test of the unicast audio cancel that can cancel any pending procedures. This is done by adding a new blocking behavior in the cap acceptor. Signed-off-by: Emil Gydesen <[email protected]>
1 parent 7712f81 commit b635a47

File tree

3 files changed

+123
-14
lines changed

3 files changed

+123
-14
lines changed

tests/bsim/bluetooth/audio/src/cap_acceptor_test.c

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ static struct bt_bap_lc3_preset broadcast_preset_16_2_1 =
3939
static const struct bt_codec_qos_pref unicast_qos_pref =
4040
BT_CODEC_QOS_PREF(true, BT_GAP_LE_PHY_2M, 0u, 60u, 20000u, 40000u, 20000u, 40000u);
4141

42+
static bool auto_start_sink_streams;
4243

4344
static K_SEM_DEFINE(sem_broadcast_started, 0U, ARRAY_SIZE(broadcast_sink_streams));
4445
static K_SEM_DEFINE(sem_broadcast_stopped, 0U, ARRAY_SIZE(broadcast_sink_streams));
@@ -210,15 +211,16 @@ static void unicast_stream_enabled_cb(struct bt_bap_stream *stream)
210211
struct bt_bap_ep_info ep_info;
211212
int err;
212213

213-
printk("Enabled: stream %p\n", stream);
214+
printk("Enabled: stream %p (auto_start_sink_streams %d)\n", stream,
215+
auto_start_sink_streams);
214216

215217
err = bt_bap_ep_get_info(stream->ep, &ep_info);
216218
if (err != 0) {
217219
FAIL("Failed to get ep info: %d\n", err);
218220
return;
219221
}
220222

221-
if (ep_info.dir == BT_AUDIO_DIR_SINK) {
223+
if (auto_start_sink_streams && ep_info.dir == BT_AUDIO_DIR_SINK) {
222224
/* Automatically do the receiver start ready operation */
223225
err = bt_bap_stream_start(stream);
224226

@@ -589,7 +591,22 @@ static void test_cap_acceptor_unicast(void)
589591
{
590592
init();
591593

592-
/* TODO: When babblesim supports ISO, wait for audio stream to pass */
594+
auto_start_sink_streams = true;
595+
596+
/* TODO: wait for audio stream to pass */
597+
598+
WAIT_FOR_FLAG(flag_connected);
599+
600+
PASS("CAP acceptor unicast passed\n");
601+
}
602+
603+
static void test_cap_acceptor_unicast_timeout(void)
604+
{
605+
init();
606+
607+
auto_start_sink_streams = false; /* Cause unicast_audio_start timeout */
608+
609+
/* TODO: wait for audio stream to pass */
593610

594611
WAIT_FOR_FLAG(flag_connected);
595612

@@ -660,15 +677,21 @@ static const struct bst_test_instance test_cap_acceptor[] = {
660677
.test_id = "cap_acceptor_unicast",
661678
.test_post_init_f = test_init,
662679
.test_tick_f = test_tick,
663-
.test_main_f = test_cap_acceptor_unicast
680+
.test_main_f = test_cap_acceptor_unicast,
681+
},
682+
{
683+
.test_id = "cap_acceptor_unicast_timeout",
684+
.test_post_init_f = test_init,
685+
.test_tick_f = test_tick,
686+
.test_main_f = test_cap_acceptor_unicast_timeout,
664687
},
665688
{
666689
.test_id = "cap_acceptor_broadcast",
667690
.test_post_init_f = test_init,
668691
.test_tick_f = test_tick,
669-
.test_main_f = test_cap_acceptor_broadcast
692+
.test_main_f = test_cap_acceptor_broadcast,
670693
},
671-
BSTEST_END_MARKER
694+
BSTEST_END_MARKER,
672695
};
673696

674697
struct bst_test_list *test_cap_acceptor_install(struct bst_test_list *tests)

tests/bsim/bluetooth/audio/src/cap_initiator_unicast_test.c

Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ CREATE_FLAG(flag_discovered);
2626
CREATE_FLAG(flag_codec_found);
2727
CREATE_FLAG(flag_endpoint_found);
2828
CREATE_FLAG(flag_started);
29+
CREATE_FLAG(flag_start_timeout);
2930
CREATE_FLAG(flag_updated);
3031
CREATE_FLAG(flag_stopped);
3132
CREATE_FLAG(flag_mtu_exchanged);
@@ -114,13 +115,13 @@ static void cap_discovery_complete_cb(struct bt_conn *conn, int err,
114115
static void unicast_start_complete_cb(struct bt_bap_unicast_group *unicast_group, int err,
115116
struct bt_conn *conn)
116117
{
117-
if (err != 0) {
118+
if (err == -ECANCELED) {
119+
SET_FLAG(flag_start_timeout);
120+
} else if (err != 0) {
118121
FAIL("Failed to start (failing conn %p): %d", conn, err);
119-
120-
return;
122+
} else {
123+
SET_FLAG(flag_started);
121124
}
122-
123-
SET_FLAG(flag_started);
124125
}
125126

126127
static void unicast_update_complete_cb(int err, struct bt_conn *conn)
@@ -469,7 +470,7 @@ static void unicast_audio_start_inval(struct bt_bap_unicast_group *unicast_group
469470
}
470471
}
471472

472-
static void unicast_audio_start(struct bt_bap_unicast_group *unicast_group)
473+
static void unicast_audio_start(struct bt_bap_unicast_group *unicast_group, bool wait)
473474
{
474475
struct bt_cap_unicast_audio_start_stream_param stream_param[1];
475476
struct bt_cap_unicast_audio_start_param param;
@@ -492,7 +493,9 @@ static void unicast_audio_start(struct bt_bap_unicast_group *unicast_group)
492493
return;
493494
}
494495

495-
WAIT_FOR_FLAG(flag_started);
496+
if (wait) {
497+
WAIT_FOR_FLAG(flag_started);
498+
}
496499
}
497500

498501
static void unicast_audio_update_inval(void)
@@ -584,6 +587,17 @@ static void unicast_audio_stop(struct bt_bap_unicast_group *unicast_group)
584587
}
585588
}
586589

590+
static void unicast_audio_cancel(void)
591+
{
592+
int err;
593+
594+
err = bt_cap_initiator_unicast_audio_cancel();
595+
if (err != 0) {
596+
FAIL("Failed to cancel unicast audio: %d\n", err);
597+
return;
598+
}
599+
}
600+
587601
static void unicast_group_delete_inval(void)
588602
{
589603
int err;
@@ -635,7 +649,7 @@ static void test_main_cap_initiator_unicast(void)
635649

636650
for (size_t j = 0U; j < iterations; j++) {
637651
unicast_audio_start_inval(unicast_group);
638-
unicast_audio_start(unicast_group);
652+
unicast_audio_start(unicast_group, true);
639653

640654
unicast_audio_update_inval();
641655
unicast_audio_update();
@@ -652,13 +666,58 @@ static void test_main_cap_initiator_unicast(void)
652666
PASS("CAP initiator unicast passed\n");
653667
}
654668

669+
static void test_cap_initiator_unicast_timeout(void)
670+
{
671+
struct bt_bap_unicast_group *unicast_group;
672+
const k_timeout_t timeout = K_SECONDS(1);
673+
const size_t iterations = 2;
674+
675+
init();
676+
677+
scan_and_connect();
678+
679+
WAIT_FOR_FLAG(flag_mtu_exchanged);
680+
681+
discover_cas();
682+
683+
discover_sink();
684+
685+
unicast_group_create(&unicast_group);
686+
687+
for (size_t j = 0U; j < iterations; j++) {
688+
unicast_audio_start(unicast_group, false);
689+
690+
k_sleep(timeout);
691+
if ((bool)atomic_get(&flag_started)) {
692+
FAIL("Unexpected start complete\n");
693+
} else {
694+
unicast_audio_cancel();
695+
}
696+
697+
WAIT_FOR_FLAG(flag_start_timeout);
698+
699+
unicast_audio_stop(unicast_group);
700+
}
701+
702+
unicast_group_delete(unicast_group);
703+
unicast_group = NULL;
704+
705+
PASS("CAP initiator unicast passed\n");
706+
}
707+
655708
static const struct bst_test_instance test_cap_initiator_unicast[] = {
656709
{
657710
.test_id = "cap_initiator_unicast",
658711
.test_post_init_f = test_init,
659712
.test_tick_f = test_tick,
660713
.test_main_f = test_main_cap_initiator_unicast,
661714
},
715+
{
716+
.test_id = "cap_initiator_unicast_timeout",
717+
.test_post_init_f = test_init,
718+
.test_tick_f = test_tick,
719+
.test_main_f = test_cap_initiator_unicast_timeout,
720+
},
662721
BSTEST_END_MARKER,
663722
};
664723

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Copyright (c) 2023 Nordic Semiconductor ASA
4+
#
5+
# SPDX-License-Identifier: Apache-2.0
6+
7+
SIMULATION_ID="cap_unicast_timeout"
8+
VERBOSITY_LEVEL=2
9+
EXECUTE_TIMEOUT=20
10+
11+
source ${ZEPHYR_BASE}/tests/bsim/sh_common.source
12+
13+
cd ${BSIM_OUT_PATH}/bin
14+
15+
printf "\n\n======== Running CAP unicast timeout test =========\n\n"
16+
17+
Execute ./bs_${BOARD}_tests_bsim_bluetooth_audio_prj_conf \
18+
-v=${VERBOSITY_LEVEL} -s=${SIMULATION_ID} -d=0 -testid=cap_acceptor_unicast_timeout -rs=23
19+
20+
Execute ./bs_${BOARD}_tests_bsim_bluetooth_audio_prj_conf \
21+
-v=${VERBOSITY_LEVEL} -s=${SIMULATION_ID} -d=1 -testid=cap_initiator_unicast_timeout -rs=46
22+
23+
# Simulation time should be larger than the WAIT_TIME in common.h
24+
Execute ./bs_2G4_phy_v1 -v=${VERBOSITY_LEVEL} -s=${SIMULATION_ID} \
25+
-D=2 -sim_length=60e6 $@
26+
27+
wait_for_background_jobs

0 commit comments

Comments
 (0)