Skip to content

Commit

Permalink
Tests: Bluetooth: Host: Add ext-adv test to iterate reconnections
Browse files Browse the repository at this point in the history
Where advertisement, connection and disconnection is iterated over 5
times, to verify the reestablishment of extended advertisements when
iterating over multiple times. Additionally, the connection
unreferencing has been decoupled from the disconnect event on both
sides, to demonstrate the dependency of the recycle event.

Signed-off-by: Luis Ubieda <[email protected]>
  • Loading branch information
ubieda authored and henrikbrixandersen committed Feb 7, 2024
1 parent 1f0346c commit f59eb2f
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 4 deletions.
52 changes: 50 additions & 2 deletions tests/bsim/bluetooth/host/adv/extended/src/ext_adv_advertiser.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,16 @@ static void connected(struct bt_conn *conn, uint8_t err)
SET_FLAG(flag_connected);
}

static void free_conn_object_work_fn(struct k_work *work)
{
ARG_UNUSED(work);

bt_conn_unref(g_conn);
g_conn = NULL;
}

static K_WORK_DELAYABLE_DEFINE(free_conn_object_work, free_conn_object_work_fn);

static void disconnected(struct bt_conn *conn, uint8_t reason)
{
char addr[BT_ADDR_LE_STR_LEN];
Expand All @@ -136,8 +146,11 @@ static void disconnected(struct bt_conn *conn, uint8_t reason)

printk("Disconnected: %s (reason %u)\n", addr, reason);

bt_conn_unref(g_conn);
g_conn = NULL;
/* Schedule to cause de-sync between disconnected and recycled events,
* in order to prove the test is relying properly on it.
*/
k_work_schedule(&free_conn_object_work, K_MSEC(100));

UNSET_FLAG(flag_connected);
}

Expand Down Expand Up @@ -188,6 +201,8 @@ static void adv_connect_and_disconnect_cycle(void)
printk("Waiting for Connection object to be recycled...\n");
WAIT_FOR_FLAG(flag_conn_recycled);

/* Iteration Cleanup */
UNSET_FLAG(flag_conn_recycled);
stop_ext_adv_set(ext_adv);
delete_adv_set(ext_adv);
}
Expand Down Expand Up @@ -216,6 +231,30 @@ static void main_ext_conn_adv_advertiser(void)
PASS("Extended advertiser passed\n");
}

static void main_ext_conn_adv_advertiser_x5(void)
{
struct bt_le_ext_adv *ext_adv;

common_init();

bt_conn_cb_register(&conn_cbs);

for (size_t i = 0 ; i < 5 ; i++) {
printk("Iteration %d...\n", i);
adv_connect_and_disconnect_cycle();
}

/* Advertise for a bit */
create_ext_adv_set(&ext_adv, false);
start_ext_adv_set(ext_adv);
k_sleep(K_SECONDS(5));
stop_ext_adv_set(ext_adv);
delete_adv_set(ext_adv);
ext_adv = NULL;

PASS("Extended advertiser passed\n");
}

static const struct bst_test_instance ext_adv_advertiser[] = {
{
.test_id = "ext_adv_advertiser",
Expand All @@ -233,6 +272,15 @@ static const struct bst_test_instance ext_adv_advertiser[] = {
.test_tick_f = test_tick,
.test_main_f = main_ext_conn_adv_advertiser
},
{
.test_id = "ext_adv_conn_advertiser_x5",
.test_descr = "Basic connectable extended advertising test. "
"Starts extended advertising, and restarts it after disconnecting, "
"repeated over 5 times",
.test_post_init_f = test_init,
.test_tick_f = test_tick,
.test_main_f = main_ext_conn_adv_advertiser_x5
},
BSTEST_END_MARKER
};

Expand Down
44 changes: 42 additions & 2 deletions tests/bsim/bluetooth/host/adv/extended/src/ext_adv_scanner.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ static void connected(struct bt_conn *conn, uint8_t err)
SET_FLAG(flag_connected);
}

static void free_conn_object_work_fn(struct k_work *work)
{
ARG_UNUSED(work);

bt_conn_unref(g_conn);
g_conn = NULL;
}

static K_WORK_DELAYABLE_DEFINE(free_conn_object_work, free_conn_object_work_fn);

static void disconnected(struct bt_conn *conn, uint8_t reason)
{
char addr[BT_ADDR_LE_STR_LEN];
Expand All @@ -51,8 +61,10 @@ static void disconnected(struct bt_conn *conn, uint8_t reason)

printk("Disconnected: %s (reason %u)\n", addr, reason);

bt_conn_unref(g_conn);
g_conn = NULL;
/* Schedule to cause de-sync between disconnected and recycled events,
* in order to prove the test is relying properly on it.
*/
k_work_schedule(&free_conn_object_work, K_MSEC(500));

UNSET_FLAG(flag_connected);
}
Expand Down Expand Up @@ -163,8 +175,10 @@ static void scan_connect_and_disconnect_cycle(void)
printk("Waiting for Connection object to be recycled...\n");
WAIT_FOR_FLAG(flag_conn_recycled);

/* Iteration cleanup */
printk("Clearing flag for seen extended advertisements...\n");
UNSET_FLAG(flag_ext_adv_seen);
UNSET_FLAG(flag_conn_recycled);
}

static void main_ext_adv_conn_scanner(void)
Expand All @@ -180,6 +194,22 @@ static void main_ext_adv_conn_scanner(void)
PASS("Extended adv scanner passed\n");
}

static void main_ext_adv_conn_scanner_x5(void)
{
common_init();

for (size_t i = 0 ; i < 5 ; i++) {
printk("Iteration %d...\n", i);
scan_connect_and_disconnect_cycle();
}

start_scan();
printk("Waiting to extended advertisements (again)...\n");
WAIT_FOR_FLAG(flag_ext_adv_seen);

PASS("Extended adv scanner x5 passed\n");
}

static const struct bst_test_instance ext_adv_scanner[] = {
{
.test_id = "ext_adv_scanner",
Expand All @@ -198,6 +228,16 @@ static const struct bst_test_instance ext_adv_scanner[] = {
.test_tick_f = test_tick,
.test_main_f = main_ext_adv_conn_scanner
},
{
.test_id = "ext_adv_conn_scanner_x5",
.test_descr = "Basic extended advertising scanning test. "
"Will scan an extended advertiser, connect "
"and verify it's detected after disconnection,"
"repeated over 5 times",
.test_post_init_f = test_init,
.test_tick_f = test_tick,
.test_main_f = main_ext_adv_conn_scanner_x5
},
BSTEST_END_MARKER
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env bash
# Copyright (c) 2024 Croxel, Inc.
# SPDX-License-Identifier: Apache-2.0

# Extended advertising test:
#
# - Connectable X5: In addition to broadcasting advertisements, it is connectable
# and restarts advertisements once disconnected. The scanner/central scans
# for the packets and establishes the connection, to then disconnect
# shortly-after. This is repeated over 5 times.

source ${ZEPHYR_BASE}/tests/bsim/sh_common.source

simulation_id="ext_adv_conn_x5"
verbosity_level=2
EXECUTE_TIMEOUT=10

cd ${BSIM_OUT_PATH}/bin

Execute ./bs_${BOARD}_tests_bsim_bluetooth_host_adv_extended_prj_advertiser_conf \
-v=${verbosity_level} -s=${simulation_id} -d=0 -RealEncryption=0 \
-testid=ext_adv_conn_advertiser_x5 -rs=23

Execute ./bs_${BOARD}_tests_bsim_bluetooth_host_adv_extended_prj_scanner_conf \
-v=${verbosity_level} -s=${simulation_id} -d=1 -RealEncryption=0 \
-testid=ext_adv_conn_scanner_x5 -rs=6

Execute ./bs_2G4_phy_v1 -v=${verbosity_level} -s=${simulation_id} \
-D=2 -sim_length=10e6 $@

wait_for_background_jobs

0 comments on commit f59eb2f

Please sign in to comment.