Skip to content

tests: Bluetooth: Audio: Add common start_broadcast_adv #90434

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 40 additions & 2 deletions include/zephyr/bluetooth/bluetooth.h
Original file line number Diff line number Diff line change
Expand Up @@ -1714,6 +1714,31 @@ int bt_le_ext_adv_delete(struct bt_le_ext_adv *adv);
*/
uint8_t bt_le_ext_adv_get_index(struct bt_le_ext_adv *adv);

enum bt_le_ext_adv_state {
/** No state */
BT_LE_EXT_ADV_STATE_NONE,

/** The advertising set has been created but not started */
BT_LE_EXT_ADV_STATE_CREATED,

/** The advertising set is started */
BT_LE_EXT_ADV_STATE_STARTED,

/** Ther advertising set is temporarily paused */
BT_LE_EXT_ADV_STATE_PAUSED,
};

enum bt_le_per_adv_state {
/** No state */
BT_LE_PER_ADV_STATE_NONE,

/** The advertising set has been configured for periodic advertising */
BT_LE_PER_ADV_STATE_CONFIGURED,

/** Periodic advertising is started */
BT_LE_PER_ADV_STATE_STARTED,
};

/** @brief Advertising set info structure. */
struct bt_le_ext_adv_info {
/** Local identity handle. */
Expand All @@ -1722,8 +1747,19 @@ struct bt_le_ext_adv_info {
/** Currently selected Transmit Power (dBM). */
int8_t tx_power;

/** Current local advertising address used. */
/**
* @brief Current local advertising address used.
*
* If @ref bt_le_ext_adv_info.ext_adv_state is not BT_LE_EXT_ADV_STATE_STARTED the value
* of this address may change when bt_le_ext_adv_start() is called.
*/
const bt_addr_le_t *addr;

/** Extended advertising state */
enum bt_le_ext_adv_state ext_adv_state;

/** Periodic advertising state */
enum bt_le_per_adv_state per_adv_state;
};

/**
Expand All @@ -1732,7 +1768,9 @@ struct bt_le_ext_adv_info {
* @param adv Advertising set object
* @param info Advertising set info object
*
* @return Zero on success or (negative) error code on failure.
* @retval 0 Success
* @retval -EINVAL @p adv or @p info is NULL
* @retval -ENXIO @p adv is not a created advertising set
*/
int bt_le_ext_adv_get_info(const struct bt_le_ext_adv *adv,
struct bt_le_ext_adv_info *info);
Expand Down
35 changes: 35 additions & 0 deletions subsys/bluetooth/host/adv.c
Original file line number Diff line number Diff line change
Expand Up @@ -1592,10 +1592,45 @@ void bt_le_adv_resume(void)
int bt_le_ext_adv_get_info(const struct bt_le_ext_adv *adv,
struct bt_le_ext_adv_info *info)
{
if (adv == NULL) {
LOG_DBG("adv is NULL");
return -EINVAL;
}

if (info == NULL) {
LOG_DBG("info is NULL");
return -EINVAL;
}

if (!atomic_test_bit(adv->flags, BT_ADV_CREATED)) {
LOG_DBG("Advertising set %p is not created", adv);
return -ENXIO;
}

(void)memset(info, 0, sizeof(*info));

info->id = adv->id;
info->tx_power = adv->tx_power;
info->addr = &adv->random_addr;

if (atomic_test_bit(adv->flags, BT_ADV_ENABLED)) {
info->ext_adv_state = BT_LE_EXT_ADV_STATE_STARTED;
} else if (atomic_test_bit(adv->flags, BT_ADV_PAUSED)) {
info->ext_adv_state = BT_LE_EXT_ADV_STATE_PAUSED;
} else {
info->ext_adv_state = BT_LE_EXT_ADV_STATE_CREATED;
}

if (IS_ENABLED(CONFIG_BT_PER_ADV)) {
if (atomic_test_bit(adv->flags, BT_PER_ADV_PARAMS_SET)) {
info->per_adv_state = BT_LE_PER_ADV_STATE_CONFIGURED;
} else if (atomic_test_bit(adv->flags, BT_PER_ADV_ENABLED)) {
info->per_adv_state = BT_LE_PER_ADV_STATE_STARTED;
} else {
info->per_adv_state = BT_LE_PER_ADV_STATE_NONE;
}
}

return 0;
}

Expand Down
6 changes: 5 additions & 1 deletion subsys/bluetooth/host/shell/bt.c
Original file line number Diff line number Diff line change
Expand Up @@ -2566,7 +2566,11 @@ static int cmd_adv_info(const struct shell *sh, size_t argc, char *argv[])

shell_print(sh, "Advertiser[%d] %p", selected_adv, adv);
shell_print(sh, "Id: %d, TX power: %d dBm", info.id, info.tx_power);
print_le_addr("Address", info.addr);
shell_print(sh, "Adv state: %d", info.ext_adv_state);
if (info.ext_adv_state == BT_LE_EXT_ADV_STATE_STARTED) {
print_le_addr("Address", info.addr);
}
shell_print(sh, "Per Adv state: %d", info.per_adv_state);

return 0;
}
Expand Down
27 changes: 4 additions & 23 deletions tests/bsim/bluetooth/audio/src/cap_initiator_broadcast_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -248,25 +248,6 @@ static void setup_extended_adv_data(struct bt_cap_broadcast_source *source,
}
}

static void start_extended_adv(struct bt_le_ext_adv *adv)
{
int err;

/* Start extended advertising */
err = bt_le_ext_adv_start(adv, BT_LE_EXT_ADV_START_DEFAULT);
if (err) {
FAIL("Failed to start extended advertising: %d\n", err);
return;
}

/* Enable Periodic Advertising */
err = bt_le_per_adv_start(adv);
if (err) {
FAIL("Failed to enable periodic advertising: %d\n", err);
return;
}
}

static void stop_and_delete_extended_adv(struct bt_le_ext_adv *adv)
{
int err;
Expand Down Expand Up @@ -662,7 +643,7 @@ static void test_main_cap_initiator_broadcast(void)

setup_extended_adv_data(broadcast_source, adv);

start_extended_adv(adv);
start_broadcast_adv(adv);

/* Wait for all to be started */
printk("Waiting for broadcast_streams to be started\n");
Expand Down Expand Up @@ -708,7 +689,7 @@ static void test_main_cap_initiator_broadcast_inval(void)

setup_extended_adv_data(broadcast_source, adv);

start_extended_adv(adv);
start_broadcast_adv(adv);

/* Wait for all to be started */
printk("Waiting for broadcast_streams to be started\n");
Expand Down Expand Up @@ -754,7 +735,7 @@ static void test_main_cap_initiator_broadcast_update(void)

setup_extended_adv_data(broadcast_source, adv);

start_extended_adv(adv);
start_broadcast_adv(adv);

/* Wait for all to be started */
printk("Waiting for broadcast_streams to be started\n");
Expand Down Expand Up @@ -848,7 +829,7 @@ static int test_cap_initiator_ac(const struct cap_initiator_ac_param *param)

test_broadcast_audio_start(broadcast_source, adv);
setup_extended_adv_data(broadcast_source, adv);
start_extended_adv(adv);
start_broadcast_adv(adv);

/* Wait for all to be started */
printk("Waiting for broadcast_streams to be started\n");
Expand Down
39 changes: 39 additions & 0 deletions tests/bsim/bluetooth/audio/src/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,45 @@ void setup_broadcast_adv(struct bt_le_ext_adv **adv)
}
}

void start_broadcast_adv(struct bt_le_ext_adv *adv)
{
char addr_str[BT_ADDR_LE_STR_LEN];
struct bt_le_ext_adv_info info;
int err;

err = bt_le_ext_adv_get_info(adv, &info);
if (err != 0) {
FAIL("Failed to get adv info: %d\n", err);
return;
}

if (info.per_adv_state == BT_LE_PER_ADV_STATE_NONE) {
FAIL("Cannot start periodic advertising for non-periodic advertising set");
return;
}

if (info.ext_adv_state == BT_LE_EXT_ADV_STATE_CREATED) {
/* Start extended advertising */
err = bt_le_ext_adv_start(adv, BT_LE_EXT_ADV_START_DEFAULT);
if (err != 0) {
FAIL("Failed to start extended advertising: %d\n", err);
return;
}
}

if (info.per_adv_state == BT_LE_PER_ADV_STATE_CONFIGURED) {
/* Enable Periodic Advertising */
err = bt_le_per_adv_start(adv);
if (err != 0) {
FAIL("Failed to enable periodic advertising: %d\n", err);
return;
}
}

bt_addr_le_to_str(info.addr, addr_str, sizeof(addr_str));
printk("Started advertising with addr %s\n", addr_str);
}

void test_tick(bs_time_t HW_device_time)
{
if (bst_result != Passed) {
Expand Down
1 change: 1 addition & 0 deletions tests/bsim/bluetooth/audio/src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ extern uint8_t csip_rsi[BT_CSIP_RSI_SIZE];
void disconnected(struct bt_conn *conn, uint8_t reason);
void setup_connectable_adv(struct bt_le_ext_adv **ext_adv);
void setup_broadcast_adv(struct bt_le_ext_adv **adv);
void start_broadcast_adv(struct bt_le_ext_adv *adv);
void test_tick(bs_time_t HW_device_time);
void test_init(void);
uint16_t get_dev_cnt(void);
Expand Down
21 changes: 1 addition & 20 deletions tests/bsim/bluetooth/audio/src/gmap_ugg_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -1066,25 +1066,6 @@ static void setup_extended_adv_data(struct bt_cap_broadcast_source *source,
}
}

static void start_extended_adv(struct bt_le_ext_adv *adv)
{
int err;

/* Start extended advertising */
err = bt_le_ext_adv_start(adv, BT_LE_EXT_ADV_START_DEFAULT);
if (err) {
FAIL("Failed to start extended advertising: %d\n", err);
return;
}

/* Enable Periodic Advertising */
err = bt_le_per_adv_start(adv);
if (err) {
FAIL("Failed to enable periodic advertising: %d\n", err);
return;
}
}

static void stop_and_delete_extended_adv(struct bt_le_ext_adv *adv)
{
int err;
Expand Down Expand Up @@ -1222,7 +1203,7 @@ static int test_gmap_ugg_broadcast_ac(const struct gmap_broadcast_ac_param *para

broadcast_audio_start(broadcast_source, adv);
setup_extended_adv_data(broadcast_source, adv);
start_extended_adv(adv);
start_broadcast_adv(adv);

/* Wait for all to be started */
printk("Waiting for broadcast_streams to be started\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,29 +175,6 @@ static int setup_extended_adv_data(struct bt_cap_broadcast_source *source,
return 0;
}

static int start_extended_adv(struct bt_le_ext_adv *adv)
{
int err;

/* Start extended advertising */
err = bt_le_ext_adv_start(adv, BT_LE_EXT_ADV_START_DEFAULT);
if (err) {
printk("Failed to start extended advertising: %d\n", err);

return err;
}

/* Enable Periodic Advertising */
err = bt_le_per_adv_start(adv);
if (err) {
printk("Failed to enable periodic advertising: %d\n", err);

return err;
}

return 0;
}

static int stop_extended_adv(struct bt_le_ext_adv *adv)
{
int err;
Expand Down Expand Up @@ -289,11 +266,7 @@ static void test_main(void)
FAIL("Public Broadcast source failed\n");
}

err = start_extended_adv(adv);
if (err != 0) {
printk("Unable to start extended advertiser: %d\n", err);
FAIL("Public Broadcast source failed\n");
}
start_broadcast_adv(adv);

k_sem_take(&sem_started, SEM_TIMEOUT);

Expand Down
Loading