Skip to content

Commit

Permalink
tests: Bluetooth: Audio: Use ext adv for all connectable adv
Browse files Browse the repository at this point in the history
Most specs require the use of extended advertising, and most
tests used legacy advertising.

Implement a common function to create and start an extended
advertising set to reduce code duplication.

Signed-off-by: Emil Gydesen <[email protected]>
  • Loading branch information
Thalley committed Jan 9, 2025
1 parent 48ac31c commit 1a97fe6
Show file tree
Hide file tree
Showing 20 changed files with 210 additions and 349 deletions.
40 changes: 4 additions & 36 deletions tests/bsim/bluetooth/audio/src/bap_broadcast_sink_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ static uint32_t broadcaster_broadcast_id;
static struct audio_test_stream broadcast_sink_streams[CONFIG_BT_BAP_BROADCAST_SNK_STREAM_COUNT];
static struct bt_bap_stream *streams[ARRAY_SIZE(broadcast_sink_streams)];
static uint32_t requested_bis_sync;
static struct bt_le_ext_adv *ext_adv;
static const struct bt_bap_scan_delegator_recv_state *req_recv_state;
static uint8_t recv_state_broadcast_code[BT_ISO_BROADCAST_CODE_SIZE];

Expand Down Expand Up @@ -897,39 +896,6 @@ static void test_broadcast_delete_inval(void)
}
}

static void test_start_adv(void)
{
const struct bt_data ad[] = {
BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
BT_DATA_BYTES(BT_DATA_UUID16_ALL, BT_UUID_16_ENCODE(BT_UUID_BASS_VAL),
BT_UUID_16_ENCODE(BT_UUID_PACS_VAL)),
BT_DATA_BYTES(BT_DATA_SVC_DATA16, BT_UUID_16_ENCODE(BT_UUID_BASS_VAL)),
};
int err;

/* Create a connectable advertising set */
err = bt_le_ext_adv_create(BT_LE_EXT_ADV_CONN, NULL, &ext_adv);
if (err != 0) {
FAIL("Failed to create advertising set (err %d)\n", err);

return;
}

err = bt_le_ext_adv_set_data(ext_adv, ad, ARRAY_SIZE(ad), NULL, 0);
if (err != 0) {
FAIL("Failed to set advertising data (err %d)\n", err);

return;
}

err = bt_le_ext_adv_start(ext_adv, BT_LE_EXT_ADV_START_DEFAULT);
if (err != 0) {
FAIL("Failed to start advertising set (err %d)\n", err);

return;
}
}

static void test_common(void)
{
int err;
Expand Down Expand Up @@ -1144,6 +1110,7 @@ static void test_sink_encrypted_incorrect_code(void)

static void broadcast_sink_with_assistant(void)
{
struct bt_le_ext_adv *ext_adv;
int err;

err = init();
Expand All @@ -1152,7 +1119,7 @@ static void broadcast_sink_with_assistant(void)
return;
}

test_start_adv();
setup_connectable_adv(&ext_adv);
WAIT_FOR_FLAG(flag_connected);

printk("Waiting for PA sync request\n");
Expand Down Expand Up @@ -1200,6 +1167,7 @@ static void broadcast_sink_with_assistant(void)

static void broadcast_sink_with_assistant_incorrect_code(void)
{
struct bt_le_ext_adv *ext_adv;
int err;

err = init();
Expand All @@ -1208,7 +1176,7 @@ static void broadcast_sink_with_assistant_incorrect_code(void)
return;
}

test_start_adv();
setup_connectable_adv(&ext_adv);
WAIT_FOR_FLAG(flag_connected);

printk("Waiting for PA sync request\n");
Expand Down
9 changes: 2 additions & 7 deletions tests/bsim/bluetooth/audio/src/bap_scan_delegator_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,7 @@ static void sync_all_broadcasts(void)

static int common_init(void)
{
struct bt_le_ext_adv *ext_adv;
int err;

err = bt_enable(NULL);
Expand All @@ -709,13 +710,7 @@ static int common_init(void)

bt_le_per_adv_sync_cb_register(&pa_sync_cb);

err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_1, ad, AD_SIZE, NULL, 0);
if (err) {
FAIL("Advertising failed to start (err %d)\n", err);
return err;
}

printk("Advertising successfully started\n");
setup_connectable_adv(&ext_adv);

WAIT_FOR_FLAG(flag_connected);

Expand Down
51 changes: 2 additions & 49 deletions tests/bsim/bluetooth/audio/src/bap_unicast_server_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,6 @@ static struct audio_test_stream
static const struct bt_bap_qos_cfg_pref qos_pref =
BT_BAP_QOS_CFG_PREF(true, BT_GAP_LE_PHY_2M, 0x02, 10, 40000, 40000, 40000, 40000);

static uint8_t unicast_server_addata[] = {
BT_UUID_16_ENCODE(BT_UUID_ASCS_VAL), /* ASCS UUID */
BT_AUDIO_UNICAST_ANNOUNCEMENT_TARGETED, /* Target Announcement */
BT_BYTES_LIST_LE16(PREF_CONTEXT),
BT_BYTES_LIST_LE16(PREF_CONTEXT),
0x00, /* Metadata length */
};

static const struct bt_data unicast_server_ad[] = {
BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
BT_DATA_BYTES(BT_DATA_UUID16_ALL, BT_UUID_16_ENCODE(BT_UUID_ASCS_VAL)),
BT_DATA(BT_DATA_SVC_DATA16, unicast_server_addata, ARRAY_SIZE(unicast_server_addata)),
};
static struct bt_le_ext_adv *ext_adv;

CREATE_FLAG(flag_stream_configured);
Expand Down Expand Up @@ -460,26 +447,7 @@ static void init(void)
&stream_ops);
}

/* Create a connectable advertising set */
err = bt_le_ext_adv_create(BT_LE_EXT_ADV_CONN, NULL, &ext_adv);
if (err != 0) {
FAIL("Failed to create advertising set (err %d)\n", err);
return;
}

err = bt_le_ext_adv_set_data(ext_adv, unicast_server_ad, ARRAY_SIZE(unicast_server_ad),
NULL, 0);
if (err != 0) {
FAIL("Failed to set advertising data (err %d)\n", err);
return;
}

err = bt_le_ext_adv_start(ext_adv, BT_LE_EXT_ADV_START_DEFAULT);
if (err != 0) {
FAIL("Failed to start advertising set (err %d)\n", err);
return;
}
printk("Advertising started\n");
setup_connectable_adv(&ext_adv);
}

static void test_main(void)
Expand Down Expand Up @@ -540,22 +508,7 @@ static void test_main_acl_disconnect(void)
* bt_conn object is properly unref'ed by the stack
*/
for (size_t i = 0U; i < ARRAY_SIZE(dummy_ext_adv); i++) {
const struct bt_le_adv_param param = BT_LE_ADV_PARAM_INIT(
(BT_LE_ADV_OPT_EXT_ADV | BT_LE_ADV_OPT_CONN), BT_GAP_ADV_SLOW_INT_MAX,
BT_GAP_ADV_SLOW_INT_MAX, NULL);
int err;

err = bt_le_ext_adv_create(&param, NULL, &dummy_ext_adv[i]);
if (err != 0) {
FAIL("Failed to create advertising set[%zu] (err %d)\n", i, err);
return;
}

err = bt_le_ext_adv_start(dummy_ext_adv[i], BT_LE_EXT_ADV_START_DEFAULT);
if (err != 0) {
FAIL("Failed to start advertising set[%zu] (err %d)\n", i, err);
return;
}
setup_connectable_adv(&dummy_ext_adv[i]);
}

bt_conn_cb_register(&conn_callbacks);
Expand Down
51 changes: 1 addition & 50 deletions tests/bsim/bluetooth/audio/src/cap_acceptor_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,6 @@

extern enum bst_result_t bst_result;

#define SINK_CONTEXT \
(BT_AUDIO_CONTEXT_TYPE_UNSPECIFIED | BT_AUDIO_CONTEXT_TYPE_MEDIA | \
BT_AUDIO_CONTEXT_TYPE_CONVERSATIONAL)
#define SOURCE_CONTEXT (BT_AUDIO_CONTEXT_TYPE_UNSPECIFIED | BT_AUDIO_CONTEXT_TYPE_NOTIFICATIONS)

CREATE_FLAG(flag_broadcaster_found);
CREATE_FLAG(flag_broadcast_code);
CREATE_FLAG(flag_base_received);
Expand Down Expand Up @@ -415,27 +410,6 @@ static struct bt_bap_scan_delegator_cb scan_delegator_cbs = {
.broadcast_code = broadcast_code_cb,
};

/* TODO: Expand with CAP service data */
static const struct bt_data cap_acceptor_ad[] = {
BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
BT_DATA(BT_DATA_NAME_COMPLETE, CONFIG_BT_DEVICE_NAME, sizeof(CONFIG_BT_DEVICE_NAME) - 1),
BT_DATA_BYTES(BT_DATA_UUID16_SOME, BT_UUID_16_ENCODE(BT_UUID_ASCS_VAL),
BT_UUID_16_ENCODE(BT_UUID_CAS_VAL)),
BT_DATA_BYTES(BT_DATA_SVC_DATA16, BT_UUID_16_ENCODE(BT_UUID_CAS_VAL),
BT_AUDIO_UNICAST_ANNOUNCEMENT_TARGETED),
IF_ENABLED(CONFIG_BT_BAP_UNICAST_SERVER,
(BT_DATA_BYTES(BT_DATA_SVC_DATA16,
BT_UUID_16_ENCODE(BT_UUID_ASCS_VAL),
BT_AUDIO_UNICAST_ANNOUNCEMENT_TARGETED,
BT_BYTES_LIST_LE16(SINK_CONTEXT),
BT_BYTES_LIST_LE16(SOURCE_CONTEXT),
0x00, /* Metadata length */),
))
IF_ENABLED(CONFIG_BT_BAP_SCAN_DELEGATOR,
(BT_DATA_BYTES(BT_DATA_SVC_DATA16, BT_UUID_16_ENCODE(BT_UUID_BASS_VAL)),
))
};

static struct bt_csip_set_member_svc_inst *csip_set_member;

static struct bt_bap_stream *unicast_stream_alloc(void)
Expand Down Expand Up @@ -636,32 +610,9 @@ static int set_supported_contexts(void)

void test_start_adv(void)
{
int err;
struct bt_le_ext_adv *ext_adv;

/* Create a connectable non-scannable advertising set */
err = bt_le_ext_adv_create(BT_LE_EXT_ADV_CONN_CUSTOM, NULL, &ext_adv);
if (err != 0) {
FAIL("Failed to create advertising set (err %d)\n", err);

return;
}

/* Add cap acceptor advertising data */
err = bt_le_ext_adv_set_data(ext_adv, cap_acceptor_ad, ARRAY_SIZE(cap_acceptor_ad), NULL,
0);
if (err != 0) {
FAIL("Failed to set advertising data (err %d)\n", err);

return;
}

err = bt_le_ext_adv_start(ext_adv, BT_LE_EXT_ADV_START_DEFAULT);
if (err != 0) {
FAIL("Failed to start advertising set (err %d)\n", err);

return;
}
setup_connectable_adv(&ext_adv);
}

static void set_available_contexts(void)
Expand Down
90 changes: 84 additions & 6 deletions tests/bsim/bluetooth/audio/src/common.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (c) 2019 Bose Corporation
* Copyright (c) 2020-2021 Nordic Semiconductor ASA
* Copyright (c) 2020-2025 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand All @@ -9,17 +9,24 @@
#include <stddef.h>
#include <stdint.h>

#include <zephyr/autoconf.h>
#include <zephyr/bluetooth/addr.h>
#include <zephyr/bluetooth/audio/audio.h>
#include <zephyr/bluetooth/audio/bap.h>
#include <zephyr/bluetooth/audio/csip.h>
#include <zephyr/bluetooth/audio/tmap.h>
#include <zephyr/bluetooth/bluetooth.h>
#include <zephyr/bluetooth/byteorder.h>
#include <zephyr/bluetooth/conn.h>
#include <zephyr/bluetooth/gap.h>
#include <zephyr/bluetooth/uuid.h>
#include <zephyr/kernel.h>
#include <zephyr/net_buf.h>
#include <zephyr/sys/__assert.h>
#include <zephyr/sys/atomic_types.h>
#include <zephyr/sys/printk.h>
#include <zephyr/sys/util.h>
#include <zephyr/sys/util_macro.h>

#include "bs_cmd_line.h"
#include "bs_dynargs.h"
Expand All @@ -37,9 +44,47 @@ atomic_t flag_disconnected;
atomic_t flag_conn_updated;
atomic_t flag_audio_received;
volatile bt_security_t security_level;

const struct bt_data ad[AD_SIZE] = {
BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR))
#if defined(CONFIG_BT_CSIP_SET_MEMBER)
uint8_t csip_rsi[BT_CSIP_RSI_SIZE];
#endif /* CONFIG_BT_CSIP_SET_MEMBER */

#if defined(CONFIG_BT_TMAP)
const enum bt_tmap_role tmap_role =
(IS_ENABLED(CONFIG_BT_TMAP_CG_SUPPORTED) ? BT_TMAP_ROLE_CG : 0U) |
(IS_ENABLED(CONFIG_BT_TMAP_CT_SUPPORTED) ? BT_TMAP_ROLE_CT : 0U) |
(IS_ENABLED(CONFIG_BT_TMAP_UMS_SUPPORTED) ? BT_TMAP_ROLE_UMS : 0U) |
(IS_ENABLED(CONFIG_BT_TMAP_UMR_SUPPORTED) ? BT_TMAP_ROLE_UMR : 0U) |
(IS_ENABLED(CONFIG_BT_TMAP_BMS_SUPPORTED) ? BT_TMAP_ROLE_BMS : 0U) |
(IS_ENABLED(CONFIG_BT_TMAP_BMR_SUPPORTED) ? BT_TMAP_ROLE_BMR : 0U);
#endif /* CONFIG_BT_TMAP */

static const struct bt_data connectable_ad[] = {
BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
BT_DATA(BT_DATA_NAME_COMPLETE, CONFIG_BT_DEVICE_NAME, sizeof(CONFIG_BT_DEVICE_NAME) - 1),
BT_DATA_BYTES(BT_DATA_UUID16_SOME, BT_UUID_16_ENCODE(BT_UUID_ASCS_VAL),
BT_UUID_16_ENCODE(BT_UUID_CAS_VAL)),
BT_DATA_BYTES(BT_DATA_UUID16_ALL, BT_UUID_16_ENCODE(BT_UUID_BASS_VAL),
BT_UUID_16_ENCODE(BT_UUID_PACS_VAL)),
#if defined(CONFIG_BT_CAP_ACCEPTOR)
BT_DATA_BYTES(BT_DATA_SVC_DATA16, BT_UUID_16_ENCODE(BT_UUID_CAS_VAL),
BT_AUDIO_UNICAST_ANNOUNCEMENT_TARGETED),
#endif /* CONFIG_BT_CAP_ACCEPTOR */
#if defined(CONFIG_BT_BAP_UNICAST_SERVER)
BT_DATA_BYTES(BT_DATA_SVC_DATA16, BT_UUID_16_ENCODE(BT_UUID_ASCS_VAL),
BT_AUDIO_UNICAST_ANNOUNCEMENT_TARGETED, BT_BYTES_LIST_LE16(SINK_CONTEXT),
BT_BYTES_LIST_LE16(SOURCE_CONTEXT), 0x00,
/* Metadata length */),
#endif /* CONFIG_BT_BAP_UNICAST_SERVER */
#if defined(CONFIG_BT_BAP_SCAN_DELEGATOR)
BT_DATA_BYTES(BT_DATA_SVC_DATA16, BT_UUID_16_ENCODE(BT_UUID_BASS_VAL)),
#endif /* CONFIG_BT_BAP_SCAN_DELEGATOR */
#if defined(CONFIG_BT_CSIP_SET_MEMBER)
BT_DATA(BT_DATA_CSIS_RSI, csip_rsi, BT_CSIP_RSI_SIZE),
#endif /* CONFIG_BT_CSIP_SET_MEMBER */
#if defined(CONFIG_BT_TMAP)
BT_DATA_BYTES(BT_DATA_SVC_DATA16, BT_UUID_16_ENCODE(BT_UUID_TMAS_VAL),
BT_UUID_16_ENCODE(tmap_role)),
#endif /* CONFIG_BT_TMAP */
};

static void device_found(const struct bt_le_scan_recv_info *info, struct net_buf_simple *ad_buf)
Expand All @@ -51,8 +96,9 @@ static void device_found(const struct bt_le_scan_recv_info *info, struct net_buf
return;
}

/* We're only interested in connectable events */
if ((info->adv_props & BT_GAP_ADV_PROP_CONNECTABLE) == 0) {
/* We're only interested in extended advertising connectable events */
if (((info->adv_props & BT_GAP_ADV_PROP_EXT_ADV) == 0 ||
(info->adv_props & BT_GAP_ADV_PROP_CONNECTABLE) == 0)) {
return;
}

Expand Down Expand Up @@ -150,6 +196,38 @@ BT_CONN_CB_DEFINE(conn_callbacks) = {
.security_changed = security_changed_cb,
};

void setup_connectable_adv(struct bt_le_ext_adv **ext_adv)
{
int err;

/* Create a non-connectable advertising set */
err = bt_le_ext_adv_create(BT_LE_EXT_ADV_CONN, NULL, ext_adv);
if (err != 0) {
FAIL("Unable to create extended advertising set: %d\n", err);
return;
}

err = bt_le_ext_adv_set_data(*ext_adv, connectable_ad, ARRAY_SIZE(connectable_ad), NULL, 0);
if (err != 0) {
FAIL("Unable to set extended advertising data: %d\n", err);

bt_le_ext_adv_delete(*ext_adv);

return;
}

err = bt_le_ext_adv_start(*ext_adv, BT_LE_EXT_ADV_START_DEFAULT);
if (err != 0) {
FAIL("Failed to start advertising set (err %d)\n", err);

bt_le_ext_adv_delete(*ext_adv);

return;
}

printk("Advertising started\n");
}

void test_tick(bs_time_t HW_device_time)
{
if (bst_result != Passed) {
Expand Down
Loading

0 comments on commit 1a97fe6

Please sign in to comment.