tests: Bluetooth: Add per_adv sync with privacy

Add test where privacy is enabled and where
the two devices are bonded and have exchanged IRK
before doing the periodic advertising sync.

Signed-off-by: Emil Gydesen <emil.gydesen@nordicsemi.no>
This commit is contained in:
Emil Gydesen 2023-01-11 17:22:44 +01:00 committed by Carles Cufí
parent 11945688fb
commit 64f3bccdb7
4 changed files with 166 additions and 0 deletions

View file

@ -2,6 +2,8 @@ CONFIG_BT=y
CONFIG_BT_DEVICE_NAME="test_per_adv"
CONFIG_BT_PERIPHERAL=y
CONFIG_BT_CENTRAL=y
CONFIG_BT_SMP=y
CONFIG_BT_PRIVACY=y
CONFIG_BT_EXT_ADV=y
CONFIG_BT_EXT_ADV_MAX_ADV_SET=2

View file

@ -22,6 +22,7 @@ extern enum bst_result_t bst_result;
static struct bt_conn *g_conn;
CREATE_FLAG(flag_connected);
CREATE_FLAG(flag_bonded);
static void connected(struct bt_conn *conn, uint8_t err)
{
@ -56,6 +57,17 @@ static struct bt_conn_cb conn_cbs = {
.disconnected = disconnected,
};
static void pairing_complete_cb(struct bt_conn *conn, bool bonded)
{
if (conn == g_conn && bonded) {
SET_FLAG(flag_bonded);
}
}
static struct bt_conn_auth_info_cb auto_info_cbs = {
.pairing_complete = pairing_complete_cb,
};
static void common_init(void)
{
int err;
@ -69,6 +81,7 @@ static void common_init(void)
printk("Bluetooth initialized\n");
bt_conn_cb_register(&conn_cbs);
bt_conn_auth_info_cb_register(&auto_info_cbs);
}
static void create_per_adv_set(struct bt_le_ext_adv **adv)
@ -227,6 +240,42 @@ static void main_per_adv_conn_advertiser(void)
PASS("Periodic advertiser passed\n");
}
static void main_per_adv_conn_privacy_advertiser(void)
{
struct bt_le_ext_adv *conn_adv;
struct bt_le_ext_adv *per_adv;
common_init();
create_conn_adv_set(&conn_adv);
start_ext_adv_set(conn_adv);
WAIT_FOR_FLAG(flag_connected);
WAIT_FOR_FLAG(flag_bonded);
/* Start periodic advertising after bonding so that the scanner gets
* the resolved address
*/
create_per_adv_set(&per_adv);
start_per_adv_set(per_adv);
start_ext_adv_set(per_adv);
/* Advertise for a bit */
k_sleep(K_SECONDS(10));
stop_per_adv_set(per_adv);
stop_ext_adv_set(per_adv);
stop_ext_adv_set(conn_adv);
delete_adv_set(per_adv);
per_adv = NULL;
delete_adv_set(conn_adv);
conn_adv = NULL;
PASS("Periodic advertiser passed\n");
}
static const struct bst_test_instance per_adv_advertiser[] = {
{
.test_id = "per_adv_advertiser",
@ -244,6 +293,14 @@ static const struct bst_test_instance per_adv_advertiser[] = {
.test_tick_f = test_tick,
.test_main_f = main_per_adv_conn_advertiser
},
{
.test_id = "per_adv_conn_privacy_advertiser",
.test_descr = "Periodic advertising test with concurrent ACL "
"with bonding and PA sync.",
.test_post_init_f = test_init,
.test_tick_f = test_tick,
.test_main_f = main_per_adv_conn_privacy_advertiser
},
BSTEST_END_MARKER
};

View file

@ -24,6 +24,7 @@ static bt_addr_le_t per_addr;
static uint8_t per_sid;
CREATE_FLAG(flag_connected);
CREATE_FLAG(flag_bonded);
CREATE_FLAG(flag_per_adv);
CREATE_FLAG(flag_per_adv_sync);
CREATE_FLAG(flag_per_adv_sync_lost);
@ -61,6 +62,17 @@ static struct bt_conn_cb conn_cbs = {
.disconnected = disconnected,
};
static void pairing_complete_cb(struct bt_conn *conn, bool bonded)
{
if (conn == g_conn && bonded) {
SET_FLAG(flag_bonded);
}
}
static struct bt_conn_auth_info_cb auto_info_cbs = {
.pairing_complete = pairing_complete_cb,
};
static void scan_recv(const struct bt_le_scan_recv_info *info,
struct net_buf_simple *buf)
{
@ -141,6 +153,7 @@ static void common_init(void)
bt_le_scan_cb_register(&scan_callbacks);
bt_le_per_adv_sync_cb_register(&sync_callbacks);
bt_conn_cb_register(&conn_cbs);
bt_conn_auth_info_cb_register(&auto_info_cbs);
}
static void start_scan(void)
@ -179,6 +192,19 @@ static void create_pa_sync(struct bt_le_per_adv_sync **sync)
printk("Periodic sync established.\n");
}
static void start_bonding(void)
{
int err;
printk("Setting security...");
err = bt_conn_set_security(g_conn, BT_SECURITY_L2);
if (err) {
FAIL("Failed to set security: %d\n", err);
return;
}
printk("done.\n");
}
static void main_per_adv_syncer(void)
{
struct bt_le_per_adv_sync *sync = NULL;
@ -223,6 +249,37 @@ static void main_per_adv_conn_syncer(void)
PASS("Periodic advertising syncer passed\n");
}
static void main_per_adv_conn_privacy_syncer(void)
{
struct bt_le_per_adv_sync *sync = NULL;
common_init();
start_scan();
printk("Waiting for connection...");
WAIT_FOR_FLAG(flag_connected);
printk("done.\n");
start_bonding();
printk("Waiting for bonding...");
WAIT_FOR_FLAG(flag_bonded);
printk("done.\n");
start_scan();
printk("Waiting for periodic advertising...\n");
WAIT_FOR_FLAG(flag_per_adv);
printk("Found periodic advertising.\n");
create_pa_sync(&sync);
printk("Waiting for periodic sync lost...\n");
WAIT_FOR_FLAG(flag_per_adv_sync_lost);
PASS("Periodic advertising syncer passed\n");
}
static const struct bst_test_instance per_adv_syncer[] = {
{
.test_id = "per_adv_syncer",
@ -241,6 +298,15 @@ static const struct bst_test_instance per_adv_syncer[] = {
.test_tick_f = test_tick,
.test_main_f = main_per_adv_conn_syncer
},
{
.test_id = "per_adv_conn_privacy_syncer",
.test_descr = "Periodic advertising sync test, but where "
"advertiser and syncer are bonded and using "
"privacy",
.test_post_init_f = test_init,
.test_tick_f = test_tick,
.test_main_f = main_per_adv_conn_privacy_syncer
},
BSTEST_END_MARKER
};

View file

@ -0,0 +1,41 @@
#!/usr/bin/env bash
# Copyright (c) 2023 Nordic Semiconductor ASA
# SPDX-License-Identifier: Apache-2.0
# Basic periodic advertising sync test: an advertiser advertises with periodic
# advertising, and a scanner scans for and syncs to the periodic advertising.
simulation_id="per_adv_conn_privacy"
verbosity_level=2
process_ids=""; exit_code=0
function Execute(){
if [ ! -f $1 ]; then
echo -e " \e[91m`pwd`/`basename $1` cannot be found (did you forget to\
compile it?)\e[39m"
exit 1
fi
timeout 10 $@ & process_ids="$process_ids $!"
}
: "${BSIM_OUT_PATH:?BSIM_OUT_PATH must be defined}"
#Give a default value to BOARD if it does not have one yet:
BOARD="${BOARD:-nrf52_bsim}"
cd ${BSIM_OUT_PATH}/bin
Execute ./bs_${BOARD}_tests_bluetooth_bsim_bt_bsim_test_per_adv_prj_conf \
-v=${verbosity_level} -s=${simulation_id} -d=0 -RealEncryption=0 \
-testid=per_adv_conn_privacy_advertiser -rs=23
Execute ./bs_${BOARD}_tests_bluetooth_bsim_bt_bsim_test_per_adv_prj_conf \
-v=${verbosity_level} -s=${simulation_id} -d=1 -RealEncryption=0 \
-testid=per_adv_conn_privacy_syncer -rs=6
Execute ./bs_2G4_phy_v1 -v=${verbosity_level} -s=${simulation_id} \
-D=2 -sim_length=20e6 $@
for process_id in $process_ids; do
wait $process_id || let "exit_code=$?"
done
exit $exit_code #the last exit code != 0