Skip to content

Commit

Permalink
tests: Bluetooth: Add tx/rx and data verification for BAP unicast
Browse files Browse the repository at this point in the history
The BAP unicast babblesim tests now does RX and TX
and verifies that the data is correctly received on both
the client and server.

Signed-off-by: Emil Gydesen <[email protected]>
  • Loading branch information
Thalley authored and carlescufi committed Oct 20, 2023
1 parent ab60f5e commit f67f840
Show file tree
Hide file tree
Showing 8 changed files with 400 additions and 64 deletions.
2 changes: 1 addition & 1 deletion subsys/bluetooth/audio/bap_stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,7 @@ int bt_bap_stream_release(struct bt_bap_stream *stream)
LOG_DBG("stream %p", stream);

CHECKIF(stream == NULL || stream->ep == NULL || stream->conn == NULL) {
LOG_DBG("Invalid stream");
LOG_DBG("Invalid stream (ep %p, conn %p)", stream->ep, (void *)stream->conn);
return -EINVAL;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/bsim/bluetooth/audio/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ CONFIG_BT_CTLR_ISOAL_SOURCES=2
CONFIG_BT_CTLR_ISOAL_SINKS=2
CONFIG_BT_CTLR_CONN_ISO_LOW_LATENCY_POLICY=y

CONFIG_BT_CTLR_ISO_TX_BUFFERS=3
CONFIG_BT_CTLR_ISO_TX_BUFFERS=4

# Controller advanced options
CONFIG_BT_CTLR_ADVANCED_FEATURES=y
Expand Down
23 changes: 10 additions & 13 deletions tests/bsim/bluetooth/audio/src/bap_broadcast_sink_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,7 @@ static struct bt_le_scan_recv_info broadcaster_info;
static bt_addr_le_t broadcaster_addr;
static struct bt_le_per_adv_sync *pa_sync;
static uint32_t broadcaster_broadcast_id;
static struct broadcast_sink_stream {
struct bt_bap_stream stream;
struct bt_iso_recv_info last_info;
size_t rx_cnt;
} broadcast_sink_streams[CONFIG_BT_BAP_BROADCAST_SNK_STREAM_COUNT];
static struct bap_test_stream broadcast_sink_streams[CONFIG_BT_BAP_BROADCAST_SNK_STREAM_COUNT];
static struct bt_bap_stream *streams[ARRAY_SIZE(broadcast_sink_streams)];

static const struct bt_audio_codec_cap codec_cap = BT_AUDIO_CODEC_CAP_LC3(
Expand Down Expand Up @@ -205,16 +201,17 @@ static void recv_cb(struct bt_bap_stream *stream,
const struct bt_iso_recv_info *info,
struct net_buf *buf)
{
struct broadcast_sink_stream *sink_stream =
CONTAINER_OF(stream, struct broadcast_sink_stream, stream);
struct bap_test_stream *test_stream = CONTAINER_OF(stream, struct bap_test_stream, stream);

if (sink_stream->rx_cnt > 0U && info->ts == sink_stream->last_info.ts) {
FAIL("Duplicated timestamp received: %u\n", sink_stream->last_info.ts);
printk("Incoming audio on stream %p len %u and ts %u\n", stream, buf->len, info->ts);

if (test_stream->rx_cnt > 0U && info->ts == test_stream->last_info.ts) {
FAIL("Duplicated timestamp received: %u\n", test_stream->last_info.ts);
return;
}

if (sink_stream->rx_cnt > 0U && info->seq_num == sink_stream->last_info.seq_num) {
FAIL("Duplicated PSN received: %u\n", sink_stream->last_info.seq_num);
if (test_stream->rx_cnt > 0U && info->seq_num == test_stream->last_info.seq_num) {
FAIL("Duplicated PSN received: %u\n", test_stream->last_info.seq_num);
return;
}

Expand All @@ -229,9 +226,9 @@ static void recv_cb(struct bt_bap_stream *stream,
}

if (memcmp(buf->data, mock_iso_data, buf->len) == 0) {
sink_stream->rx_cnt++;
test_stream->rx_cnt++;

if (sink_stream->rx_cnt >= MIN_SEND_COUNT) {
if (test_stream->rx_cnt >= MIN_SEND_COUNT) {
/* We set the flag is just one stream has received the expected */
SET_FLAG(flag_received);
}
Expand Down
75 changes: 51 additions & 24 deletions tests/bsim/bluetooth/audio/src/bap_broadcast_source_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,11 @@ NET_BUF_POOL_FIXED_DEFINE(tx_pool,
CONFIG_BT_CONN_TX_USER_DATA_SIZE, NULL);

extern enum bst_result_t bst_result;
static volatile size_t sent_count;
static struct bt_bap_stream broadcast_source_streams[CONFIG_BT_BAP_BROADCAST_SRC_STREAM_COUNT];
static struct bap_test_stream broadcast_source_streams[CONFIG_BT_BAP_BROADCAST_SRC_STREAM_COUNT];
static struct bt_bap_lc3_preset preset_16_2_1 = BT_BAP_LC3_BROADCAST_PRESET_16_2_1(
BT_AUDIO_LOCATION_FRONT_LEFT, BT_AUDIO_CONTEXT_TYPE_UNSPECIFIED);
static struct bt_bap_lc3_preset preset_16_2_2 = BT_BAP_LC3_BROADCAST_PRESET_16_2_2(
static struct bt_bap_lc3_preset preset_16_1_1 = BT_BAP_LC3_BROADCAST_PRESET_16_1_1(
BT_AUDIO_LOCATION_FRONT_LEFT, BT_AUDIO_CONTEXT_TYPE_UNSPECIFIED);
CREATE_FLAG(flag_stopping);

static K_SEM_DEFINE(sem_started, 0U, ARRAY_SIZE(broadcast_source_streams));
static K_SEM_DEFINE(sem_stopped, 0U, ARRAY_SIZE(broadcast_source_streams));
Expand All @@ -51,16 +49,18 @@ static void stopped_cb(struct bt_bap_stream *stream, uint8_t reason)
k_sem_give(&sem_stopped);
}

static void sent_cb(struct bt_bap_stream *stream)
static void stream_sent_cb(struct bt_bap_stream *stream)
{
static uint16_t seq_num;
struct bap_test_stream *test_stream = CONTAINER_OF(stream, struct bap_test_stream, stream);
struct net_buf *buf;
int ret;

if (TEST_FLAG(flag_stopping)) {
if (!test_stream->tx_active) {
return;
}

printk("Sent with seq_num %u\n", test_stream->seq_num);

buf = net_buf_alloc(&tx_pool, K_FOREVER);
if (buf == NULL) {
printk("Could not allocate buffer when sending on %p\n",
Expand All @@ -69,23 +69,27 @@ static void sent_cb(struct bt_bap_stream *stream)
}

net_buf_reserve(buf, BT_ISO_CHAN_SEND_RESERVE);
/* Use preset_16_2_1 as that is the config we end up using */
net_buf_add_mem(buf, mock_iso_data, preset_16_2_1.qos.sdu);
ret = bt_bap_stream_send(stream, buf, seq_num++, BT_ISO_TIMESTAMP_NONE);
net_buf_add_mem(buf, mock_iso_data, test_stream->tx_sdu_size);
ret = bt_bap_stream_send(stream, buf, test_stream->seq_num++, BT_ISO_TIMESTAMP_NONE);
if (ret < 0) {
/* This will end broadcasting on this stream. */
net_buf_unref(buf);
FAIL("Unable to broadcast data on %p: %d\n", stream, ret);

/* Only fail if tx is active (may fail if we are disabling the stream) */
if (test_stream->tx_active) {
FAIL("Unable to broadcast data on %p: %d\n", stream, ret);
}

return;
}

sent_count++;
test_stream->tx_cnt++;
}

static struct bt_bap_stream_ops stream_ops = {
.started = started_cb,
.stopped = stopped_cb,
.sent = sent_cb
.sent = stream_sent_cb,
};

static int setup_broadcast_source(struct bt_bap_broadcast_source **source)
Expand All @@ -105,7 +109,7 @@ static int setup_broadcast_source(struct bt_bap_broadcast_source **source)
sizeof(broadcast_source_streams));

for (size_t i = 0; i < ARRAY_SIZE(stream_params); i++) {
stream_params[i].stream = &broadcast_source_streams[i];
stream_params[i].stream = &broadcast_source_streams[i].stream;
bt_bap_stream_cb_register(stream_params[i].stream,
&stream_ops);
#if CONFIG_BT_AUDIO_CODEC_CFG_MAX_DATA_SIZE > 0
Expand All @@ -117,7 +121,7 @@ static int setup_broadcast_source(struct bt_bap_broadcast_source **source)
for (size_t i = 0U; i < ARRAY_SIZE(subgroup_params); i++) {
subgroup_params[i].params_count = 1U;
subgroup_params[i].params = &stream_params[i];
subgroup_params[i].codec_cfg = &preset_16_2_1.codec_cfg;
subgroup_params[i].codec_cfg = &preset_16_1_1.codec_cfg;
}

create_param.params_count = ARRAY_SIZE(subgroup_params);
Expand All @@ -134,6 +138,12 @@ static int setup_broadcast_source(struct bt_bap_broadcast_source **source)
return err;
}

for (size_t i = 0U; i < ARRAY_SIZE(broadcast_source_streams); i++) {
struct bap_test_stream *test_stream = &broadcast_source_streams[i];

test_stream->tx_sdu_size = preset_16_1_1.qos.sdu;
}

return 0;
}

Expand Down Expand Up @@ -303,20 +313,20 @@ static void test_broadcast_source_reconfig(struct bt_bap_broadcast_source *sourc
int err;

for (size_t i = 0; i < ARRAY_SIZE(stream_params); i++) {
stream_params[i].stream = &broadcast_source_streams[i];
stream_params[i].stream = &broadcast_source_streams[i].stream;
stream_params[i].data_len = ARRAY_SIZE(bis_codec_data);
stream_params[i].data = bis_codec_data;
}

for (size_t i = 0U; i < ARRAY_SIZE(subgroup_params); i++) {
subgroup_params[i].params_count = 1U;
subgroup_params[i].params = &stream_params[i];
subgroup_params[i].codec_cfg = &preset_16_2_2.codec_cfg;
subgroup_params[i].codec_cfg = &preset_16_1_1.codec_cfg;
}

reconfig_param.params_count = ARRAY_SIZE(subgroup_params);
reconfig_param.params = subgroup_params;
reconfig_param.qos = &preset_16_2_2.qos;
reconfig_param.qos = &preset_16_1_1.qos;
reconfig_param.packing = BT_ISO_PACKING_SEQUENTIAL;
reconfig_param.encryption = false;

Expand All @@ -326,6 +336,12 @@ static void test_broadcast_source_reconfig(struct bt_bap_broadcast_source *sourc
FAIL("Unable to reconfigure broadcast source: %d\n", err);
return;
}

for (size_t i = 0U; i < ARRAY_SIZE(broadcast_source_streams); i++) {
struct bap_test_stream *test_stream = &broadcast_source_streams[i];

test_stream->tx_sdu_size = preset_16_1_1.qos.sdu;
}
}

static void test_broadcast_source_start(struct bt_bap_broadcast_source *source,
Expand All @@ -351,9 +367,12 @@ static void test_broadcast_source_stop(struct bt_bap_broadcast_source *source)
{
int err;

SET_FLAG(flag_stopping);
printk("Stopping broadcast source\n");

for (size_t i = 0U; i < ARRAY_SIZE(broadcast_source_streams); i++) {
broadcast_source_streams[i].tx_active = false;
}

err = bt_bap_broadcast_source_stop(source);
if (err != 0) {
FAIL("Unable to stop broadcast source: %d\n", err);
Expand All @@ -371,7 +390,6 @@ static void test_broadcast_source_delete(struct bt_bap_broadcast_source *source)
{
int err;

SET_FLAG(flag_stopping);
printk("Deleting broadcast source\n");

err = bt_bap_broadcast_source_delete(source);
Expand Down Expand Up @@ -441,13 +459,22 @@ static void test_main(void)
printk("Sending data\n");
for (size_t i = 0U; i < ARRAY_SIZE(broadcast_source_streams); i++) {
for (unsigned int j = 0U; j < BROADCAST_ENQUEUE_COUNT; j++) {
sent_cb(&broadcast_source_streams[i]);
struct bap_test_stream *test_stream =
CONTAINER_OF(&broadcast_source_streams[i].stream,
struct bap_test_stream, stream);

test_stream->tx_active = true;
stream_sent_cb(&broadcast_source_streams[i].stream);
}
}

/* Keep sending until we reach the minimum expected */
while (sent_count < (MIN_SEND_COUNT * ARRAY_SIZE(broadcast_source_streams))) {
k_sleep(K_MSEC(100));
for (size_t i = 0U; i < ARRAY_SIZE(broadcast_source_streams); i++) {
/* Keep sending until we reach 5 times the minimum expected to be pretty sure
* that the receiver receives enough
*/
while (broadcast_source_streams[i].tx_cnt < MIN_SEND_COUNT) {
k_sleep(K_MSEC(100));
}
}

/* Update metadata while streaming */
Expand Down
Loading

0 comments on commit f67f840

Please sign in to comment.