tests: Bluetooth: bsim: Added tests for RPA
This adds two tests to ensure the resolvable private address is rotated for the peripheral and central after their respective timeout. Signed-off-by: Alexandre Dauphinais <alexandre.dauphinais@nordicsemi.no>
This commit is contained in:
parent
0f692f5b84
commit
cd95f1cf88
21 changed files with 963 additions and 0 deletions
25
tests/bluetooth/bsim_bt/bsim_test_rpa_central/CMakeLists.txt
Normal file
25
tests/bluetooth/bsim_bt/bsim_test_rpa_central/CMakeLists.txt
Normal file
|
@ -0,0 +1,25 @@
|
|||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
cmake_minimum_required(VERSION 3.20.0)
|
||||
|
||||
if (NOT DEFINED ENV{BSIM_COMPONENTS_PATH})
|
||||
message(FATAL_ERROR "This test requires the BabbleSim simulator. Please set\
|
||||
the environment variable BSIM_COMPONENTS_PATH to point to its components \
|
||||
folder. More information can be found in\
|
||||
https://babblesim.github.io/folder_structure_and_env.html")
|
||||
endif()
|
||||
|
||||
find_package(Zephyr HINTS $ENV{ZEPHYR_BASE})
|
||||
project(bsim_test_rpa_central)
|
||||
|
||||
target_sources(app PRIVATE
|
||||
src/bs_bt_utils.c
|
||||
src/tester.c
|
||||
src/main.c
|
||||
src/dut.c
|
||||
)
|
||||
|
||||
zephyr_include_directories(
|
||||
$ENV{BSIM_COMPONENTS_PATH}/libUtilv1/src/
|
||||
$ENV{BSIM_COMPONENTS_PATH}/libPhyComv1/src/
|
||||
)
|
13
tests/bluetooth/bsim_bt/bsim_test_rpa_central/prj.conf
Normal file
13
tests/bluetooth/bsim_bt/bsim_test_rpa_central/prj.conf
Normal file
|
@ -0,0 +1,13 @@
|
|||
CONFIG_BT=y
|
||||
CONFIG_BT_PERIPHERAL=y
|
||||
CONFIG_BT_CENTRAL=y
|
||||
CONFIG_BT_SMP=y
|
||||
CONFIG_ASSERT=y
|
||||
|
||||
CONFIG_BT_EXT_ADV=y
|
||||
CONFIG_BT_PRIVACY=y
|
||||
CONFIG_BT_RPA_TIMEOUT=10
|
||||
|
||||
CONFIG_BT_CTLR_ADV_SET=2
|
||||
CONFIG_BT_CTLR_SCAN_REQ_NOTIFY=y
|
||||
CONFIG_BT_SCAN_WITH_IDENTITY=y
|
|
@ -0,0 +1,75 @@
|
|||
/*
|
||||
* Copyright (c) 2023 Nordic Semiconductor ASA
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "bs_bt_utils.h"
|
||||
#include "bs_pc_backchannel.h"
|
||||
#include "argparse.h"
|
||||
|
||||
#define BS_SECONDS(dur_sec) ((bs_time_t)dur_sec * 1000000)
|
||||
#define TEST_TIMEOUT_SIMULATED BS_SECONDS(60)
|
||||
#define CHANNEL_ID 0
|
||||
#define MSG_SIZE 1
|
||||
|
||||
void test_tick(bs_time_t HW_device_time)
|
||||
{
|
||||
bs_trace_debug_time(0, "Simulation ends now.\n");
|
||||
if (bst_result != Passed) {
|
||||
bst_result = Failed;
|
||||
bs_trace_error("Test did not pass before simulation ended.\n");
|
||||
}
|
||||
}
|
||||
|
||||
void test_init(void)
|
||||
{
|
||||
bst_ticker_set_next_tick_absolute(TEST_TIMEOUT_SIMULATED);
|
||||
bst_result = In_progress;
|
||||
}
|
||||
|
||||
void backchannel_init(uint peer)
|
||||
{
|
||||
uint device_number = get_device_nbr();
|
||||
uint device_numbers[] = {peer};
|
||||
uint channel_numbers[] = {CHANNEL_ID};
|
||||
uint *ch;
|
||||
|
||||
ch = bs_open_back_channel(device_number, device_numbers, channel_numbers,
|
||||
ARRAY_SIZE(channel_numbers));
|
||||
if (!ch) {
|
||||
FAIL("Unable to open backchannel\n");
|
||||
}
|
||||
}
|
||||
|
||||
void backchannel_sync_send(void)
|
||||
{
|
||||
uint8_t sync_msg[MSG_SIZE] = {get_device_nbr()};
|
||||
|
||||
bs_bc_send_msg(CHANNEL_ID, sync_msg, ARRAY_SIZE(sync_msg));
|
||||
}
|
||||
|
||||
void backchannel_sync_wait(void)
|
||||
{
|
||||
uint8_t sync_msg[MSG_SIZE];
|
||||
|
||||
while (true) {
|
||||
if (bs_bc_is_msg_received(CHANNEL_ID) > 0) {
|
||||
bs_bc_receive_msg(CHANNEL_ID, sync_msg, ARRAY_SIZE(sync_msg));
|
||||
if (sync_msg[0] != get_device_nbr()) {
|
||||
/* Received a message from another device, exit */
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
k_sleep(K_MSEC(1));
|
||||
}
|
||||
}
|
||||
|
||||
void print_address(bt_addr_le_t *addr)
|
||||
{
|
||||
char array[BT_ADDR_LE_STR_LEN];
|
||||
|
||||
bt_addr_le_to_str(addr, array, sizeof(array));
|
||||
printk("Address : %s\n", array);
|
||||
}
|
|
@ -0,0 +1,72 @@
|
|||
/**
|
||||
* Common functions and helpers for BSIM ADV tests
|
||||
*
|
||||
* Copyright (c) 2023 Nordic Semiconductor ASA
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "bs_tracing.h"
|
||||
#include "bs_types.h"
|
||||
#include "bstests.h"
|
||||
#include "time_machine.h"
|
||||
|
||||
#include <errno.h>
|
||||
|
||||
#include <zephyr/sys/__assert.h>
|
||||
#include <zephyr/bluetooth/bluetooth.h>
|
||||
#include <zephyr/bluetooth/conn.h>
|
||||
#include <zephyr/bluetooth/gatt.h>
|
||||
#include <zephyr/bluetooth/hci.h>
|
||||
#include <zephyr/bluetooth/uuid.h>
|
||||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/types.h>
|
||||
|
||||
extern enum bst_result_t bst_result;
|
||||
|
||||
#define DECLARE_FLAG(flag) extern atomic_t flag
|
||||
#define DEFINE_FLAG(flag) atomic_t flag = (atomic_t) false
|
||||
#define SET_FLAG(flag) (void)atomic_set(&flag, (atomic_t) true)
|
||||
#define UNSET_FLAG(flag) (void)atomic_set(&flag, (atomic_t) false)
|
||||
#define WAIT_FOR_FLAG(flag) \
|
||||
while (!(bool)atomic_get(&flag)) { \
|
||||
(void)k_sleep(K_MSEC(1)); \
|
||||
}
|
||||
#define WAIT_FOR_FLAG_UNSET(flag) \
|
||||
while ((bool)atomic_get(&flag)) { \
|
||||
(void)k_sleep(K_MSEC(1)); \
|
||||
}
|
||||
#define TAKE_FLAG(flag) \
|
||||
while (!(bool)atomic_cas(&flag, true, false)) { \
|
||||
(void)k_sleep(K_MSEC(1)); \
|
||||
}
|
||||
|
||||
#define ASSERT(expr, ...) \
|
||||
do { \
|
||||
if (!(expr)) { \
|
||||
FAIL(__VA_ARGS__); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#define FAIL(...) \
|
||||
do { \
|
||||
bst_result = Failed; \
|
||||
bs_trace_error_time_line(__VA_ARGS__); \
|
||||
} while (0)
|
||||
|
||||
#define PASS(...) \
|
||||
do { \
|
||||
bst_result = Passed; \
|
||||
bs_trace_info_time(1, __VA_ARGS__); \
|
||||
} while (0)
|
||||
|
||||
#define DUT_CENTRAL_ID 0
|
||||
#define TESTER_PERIPHERAL_ID 1
|
||||
|
||||
void test_tick(bs_time_t HW_device_time);
|
||||
void test_init(void);
|
||||
void backchannel_init(uint peer);
|
||||
void backchannel_sync_send(void);
|
||||
void backchannel_sync_wait(void);
|
||||
|
||||
void print_address(bt_addr_le_t *addr);
|
48
tests/bluetooth/bsim_bt/bsim_test_rpa_central/src/dut.c
Normal file
48
tests/bluetooth/bsim_bt/bsim_test_rpa_central/src/dut.c
Normal file
|
@ -0,0 +1,48 @@
|
|||
/*
|
||||
* Copyright (c) 2023 Nordic Semiconductor ASA
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "bs_bt_utils.h"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <zephyr/bluetooth/addr.h>
|
||||
#include <zephyr/bluetooth/conn.h>
|
||||
#include <zephyr/bluetooth/bluetooth.h>
|
||||
|
||||
void start_scanning(void)
|
||||
{
|
||||
int err;
|
||||
struct bt_le_scan_param param;
|
||||
|
||||
/* Enable bluetooth */
|
||||
err = bt_enable(NULL);
|
||||
if (err) {
|
||||
FAIL("Failed to enable bluetooth (err %d\n)", err);
|
||||
}
|
||||
|
||||
/* Start active scanning */
|
||||
param.type = BT_LE_SCAN_TYPE_ACTIVE;
|
||||
param.options = BT_LE_SCAN_OPT_FILTER_DUPLICATE;
|
||||
param.interval = BT_GAP_SCAN_FAST_INTERVAL;
|
||||
param.window = BT_GAP_SCAN_FAST_WINDOW;
|
||||
param.timeout = 0;
|
||||
param.interval_coded = 0;
|
||||
param.window_coded = 0;
|
||||
|
||||
err = bt_le_scan_start(¶m, NULL);
|
||||
if (err) {
|
||||
FAIL("Failed to start scanning");
|
||||
}
|
||||
}
|
||||
|
||||
void dut_procedure(void)
|
||||
{
|
||||
start_scanning();
|
||||
|
||||
/* Nothing to do */
|
||||
|
||||
PASS("PASS\n");
|
||||
}
|
39
tests/bluetooth/bsim_bt/bsim_test_rpa_central/src/main.c
Normal file
39
tests/bluetooth/bsim_bt/bsim_test_rpa_central/src/main.c
Normal file
|
@ -0,0 +1,39 @@
|
|||
/*
|
||||
* Copyright (c) 2023 Nordic Semiconductor ASA
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "bs_bt_utils.h"
|
||||
#include "bstests.h"
|
||||
|
||||
void tester_procedure(void);
|
||||
void dut_procedure(void);
|
||||
|
||||
static const struct bst_test_instance test_to_add[] = {
|
||||
{
|
||||
.test_id = "central",
|
||||
.test_post_init_f = test_init,
|
||||
.test_tick_f = test_tick,
|
||||
.test_main_f = dut_procedure,
|
||||
},
|
||||
{
|
||||
.test_id = "peripheral",
|
||||
.test_post_init_f = test_init,
|
||||
.test_tick_f = test_tick,
|
||||
.test_main_f = tester_procedure,
|
||||
},
|
||||
BSTEST_END_MARKER,
|
||||
};
|
||||
|
||||
static struct bst_test_list *install(struct bst_test_list *tests)
|
||||
{
|
||||
return bst_add_tests(tests, test_to_add);
|
||||
};
|
||||
|
||||
bst_test_install_t test_installers[] = {install, NULL};
|
||||
|
||||
void main(void)
|
||||
{
|
||||
bst_main();
|
||||
}
|
127
tests/bluetooth/bsim_bt/bsim_test_rpa_central/src/tester.c
Normal file
127
tests/bluetooth/bsim_bt/bsim_test_rpa_central/src/tester.c
Normal file
|
@ -0,0 +1,127 @@
|
|||
/*
|
||||
* Copyright (c) 2023 Nordic Semiconductor ASA
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "bs_bt_utils.h"
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <zephyr/bluetooth/addr.h>
|
||||
#include <zephyr/bluetooth/bluetooth.h>
|
||||
#include <zephyr/bluetooth/conn.h>
|
||||
#include <zephyr/toolchain/gcc.h>
|
||||
|
||||
DEFINE_FLAG(flag_new_address);
|
||||
|
||||
void scanned_cb(struct bt_le_ext_adv *adv, struct bt_le_ext_adv_scanned_info *info)
|
||||
{
|
||||
static bool init;
|
||||
static int64_t old_time;
|
||||
static bt_addr_le_t old_addr;
|
||||
bt_addr_le_t new_addr;
|
||||
|
||||
if (bst_result == Passed) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!init) {
|
||||
old_addr = *info->addr;
|
||||
old_time = k_uptime_get();
|
||||
init = true;
|
||||
}
|
||||
|
||||
new_addr = *info->addr;
|
||||
|
||||
/* Check if the scan request comes from a new address */
|
||||
if (bt_addr_le_cmp(&old_addr, &new_addr)) {
|
||||
int64_t new_time, diff, time_diff_ms, rpa_timeout_ms;
|
||||
|
||||
printk("Scanned request from new ");
|
||||
print_address(info->addr);
|
||||
|
||||
/* Ensure the RPA rotation occurs within +-10% of CONFIG_BT_RPA_TIMEOUT */
|
||||
new_time = k_uptime_get();
|
||||
time_diff_ms = new_time - old_time;
|
||||
rpa_timeout_ms = CONFIG_BT_RPA_TIMEOUT * MSEC_PER_SEC;
|
||||
|
||||
if (time_diff_ms > rpa_timeout_ms) {
|
||||
diff = time_diff_ms - rpa_timeout_ms;
|
||||
} else {
|
||||
diff = rpa_timeout_ms - time_diff_ms;
|
||||
}
|
||||
|
||||
if (diff > rpa_timeout_ms * 0.10) {
|
||||
FAIL("RPA rotation did not occur within +-10%% of CONFIG_BT_RPA_TIMEOUT");
|
||||
}
|
||||
old_time = new_time;
|
||||
|
||||
SET_FLAG(flag_new_address);
|
||||
}
|
||||
|
||||
old_addr = new_addr;
|
||||
}
|
||||
|
||||
static struct bt_le_ext_adv_cb adv_callbacks = {
|
||||
.scanned = scanned_cb,
|
||||
};
|
||||
|
||||
void start_advertising(void)
|
||||
{
|
||||
int err;
|
||||
uint8_t mfg_data[] = {0xAB, 0xCD, 0xEF};
|
||||
const struct bt_data sd[] = {BT_DATA(BT_DATA_MANUFACTURER_DATA, mfg_data, 3)};
|
||||
struct bt_le_adv_param params;
|
||||
struct bt_le_ext_adv_start_param start_params;
|
||||
struct bt_le_ext_adv *adv;
|
||||
|
||||
/* Enable bluetooth */
|
||||
err = bt_enable(NULL);
|
||||
if (err) {
|
||||
FAIL("Failed to enable bluetooth (err %d\n)", err);
|
||||
}
|
||||
|
||||
/* Create advertising set */
|
||||
params.id = BT_ID_DEFAULT;
|
||||
params.sid = 0;
|
||||
params.secondary_max_skip = 0;
|
||||
params.options = BT_LE_ADV_OPT_EXT_ADV | BT_LE_ADV_OPT_SCANNABLE |
|
||||
BT_LE_ADV_OPT_NOTIFY_SCAN_REQ | BT_LE_ADV_OPT_USE_NAME;
|
||||
params.interval_min = BT_GAP_ADV_FAST_INT_MIN_1;
|
||||
params.interval_max = BT_GAP_ADV_FAST_INT_MAX_1;
|
||||
params.peer = NULL;
|
||||
|
||||
err = bt_le_ext_adv_create(¶ms, &adv_callbacks, &adv);
|
||||
if (err) {
|
||||
FAIL("Failed to create advertising set (err %d)\n", err);
|
||||
}
|
||||
|
||||
/* Set scan data */
|
||||
err = bt_le_ext_adv_set_data(adv, NULL, 0, sd, ARRAY_SIZE(sd));
|
||||
if (err) {
|
||||
FAIL("Failed to set advertising data (err %d)", err);
|
||||
}
|
||||
|
||||
/* Start advertising */
|
||||
start_params.timeout = 0;
|
||||
start_params.num_events = 0;
|
||||
|
||||
err = bt_le_ext_adv_start(adv, &start_params);
|
||||
if (err) {
|
||||
FAIL("Failed to start advertising (err %d)\n", err);
|
||||
}
|
||||
}
|
||||
|
||||
void tester_procedure(void)
|
||||
{
|
||||
start_advertising();
|
||||
|
||||
for (int i = 0; i < 5; i++) {
|
||||
WAIT_FOR_FLAG(flag_new_address);
|
||||
UNSET_FLAG(flag_new_address);
|
||||
}
|
||||
|
||||
PASS("PASS\n");
|
||||
}
|
24
tests/bluetooth/bsim_bt/bsim_test_rpa_central/test_scripts/_compile.sh
Executable file
24
tests/bluetooth/bsim_bt/bsim_test_rpa_central/test_scripts/_compile.sh
Executable file
|
@ -0,0 +1,24 @@
|
|||
#!/usr/bin/env bash
|
||||
# Copyright 2023 Nordic Semiconductor ASA
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
set -eu
|
||||
bash_source_dir="$(realpath "$(dirname "${BASH_SOURCE[0]}")")"
|
||||
|
||||
# Read variable definitions output by _env.sh
|
||||
source <("${bash_source_dir}/_env.sh")
|
||||
|
||||
|
||||
: "${BSIM_OUT_PATH:?BSIM_OUT_PATH must be defined}"
|
||||
: "${BSIM_COMPONENTS_PATH:?BSIM_COMPONENTS_PATH must be defined}"
|
||||
: "${ZEPHYR_BASE:?ZEPHYR_BASE must be defined}"
|
||||
|
||||
|
||||
WORK_DIR="${WORK_DIR:-${ZEPHYR_BASE}/bsim_bt_out}"
|
||||
BOARD="${BOARD:-nrf52_bsim}"
|
||||
BOARD_ROOT="${BOARD_ROOT:-${ZEPHYR_BASE}}"
|
||||
INCR_BUILD=1
|
||||
mkdir -p ${WORK_DIR}
|
||||
|
||||
west build -b nrf52_bsim
|
||||
cp build/zephyr/zephyr.exe $central_exe
|
31
tests/bluetooth/bsim_bt/bsim_test_rpa_central/test_scripts/_env.sh
Executable file
31
tests/bluetooth/bsim_bt/bsim_test_rpa_central/test_scripts/_env.sh
Executable file
|
@ -0,0 +1,31 @@
|
|||
#!/usr/bin/env bash
|
||||
# Copyright 2023 Nordic Semiconductor ASA
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
set -eu
|
||||
bash_source_dir="$(realpath "$(dirname "${BASH_SOURCE[0]}")")"
|
||||
|
||||
: "${BSIM_OUT_PATH:?BSIM_OUT_PATH must be defined}"
|
||||
|
||||
test_name="$(basename "$(realpath "$bash_source_dir/..")")"
|
||||
bsim_bin="${BSIM_OUT_PATH}/bin"
|
||||
verbosity_level=2
|
||||
BOARD="${BOARD:-nrf52_bsim}"
|
||||
simulation_id="$test_name"
|
||||
central_exe="${bsim_bin}/bs_${BOARD}_tests_bluetooth_bsim_bt_${test_name}_prj_conf"
|
||||
peripheral_exe="${bsim_bin}/bs_${BOARD}_tests_bluetooth_bsim_bt_${test_name}_prj_conf"
|
||||
|
||||
function print_var {
|
||||
# Print a shell-sourceable variable definition.
|
||||
local var_name="$1"
|
||||
local var_repr="${!var_name@Q}"
|
||||
echo "$var_name=$var_repr"
|
||||
}
|
||||
|
||||
print_var test_name
|
||||
print_var bsim_bin
|
||||
print_var verbosity_level
|
||||
print_var BOARD
|
||||
print_var simulation_id
|
||||
print_var central_exe
|
||||
print_var peripheral_exe
|
41
tests/bluetooth/bsim_bt/bsim_test_rpa_central/test_scripts/run_test.sh
Executable file
41
tests/bluetooth/bsim_bt/bsim_test_rpa_central/test_scripts/run_test.sh
Executable file
|
@ -0,0 +1,41 @@
|
|||
#!/usr/bin/env bash
|
||||
# Copyright 2023 Nordic Semiconductor ASA
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
set -eu
|
||||
bash_source_dir="$(realpath "$(dirname "${BASH_SOURCE[0]}")")"
|
||||
|
||||
# Read variable definitions output by _env.sh
|
||||
source <("${bash_source_dir}/_env.sh")
|
||||
|
||||
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 30 $@ &
|
||||
process_ids="$process_ids $!"
|
||||
}
|
||||
|
||||
: "${BSIM_OUT_PATH:?BSIM_OUT_PATH must be defined}"
|
||||
|
||||
cd ${BSIM_OUT_PATH}/bin
|
||||
|
||||
Execute "$central_exe" \
|
||||
-v=${verbosity_level} -s=${simulation_id} -d=0 -testid=central -RealEncryption=1
|
||||
|
||||
Execute "$peripheral_exe" \
|
||||
-v=${verbosity_level} -s=${simulation_id} -d=1 -testid=peripheral -RealEncryption=1
|
||||
|
||||
Execute ./bs_2G4_phy_v1 -v=${verbosity_level} -s=${simulation_id} \
|
||||
-D=2 -sim_length=60e6 $@
|
||||
|
||||
for process_id in $process_ids; do
|
||||
wait $process_id || let "exit_code=$?"
|
||||
done
|
||||
|
||||
exit $exit_code #the last exit code != 0
|
|
@ -0,0 +1,25 @@
|
|||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
cmake_minimum_required(VERSION 3.20.0)
|
||||
|
||||
if (NOT DEFINED ENV{BSIM_COMPONENTS_PATH})
|
||||
message(FATAL_ERROR "This test requires the BabbleSim simulator. Please set\
|
||||
the environment variable BSIM_COMPONENTS_PATH to point to its components \
|
||||
folder. More information can be found in\
|
||||
https://babblesim.github.io/folder_structure_and_env.html")
|
||||
endif()
|
||||
|
||||
find_package(Zephyr HINTS $ENV{ZEPHYR_BASE})
|
||||
project(bsim_test_rpa_peripheral)
|
||||
|
||||
target_sources(app PRIVATE
|
||||
src/bs_bt_utils.c
|
||||
src/tester.c
|
||||
src/main.c
|
||||
src/dut.c
|
||||
)
|
||||
|
||||
zephyr_include_directories(
|
||||
$ENV{BSIM_COMPONENTS_PATH}/libUtilv1/src/
|
||||
$ENV{BSIM_COMPONENTS_PATH}/libPhyComv1/src/
|
||||
)
|
|
@ -0,0 +1,9 @@
|
|||
CONFIG_BT=y
|
||||
CONFIG_BT_PERIPHERAL=y
|
||||
CONFIG_BT_CENTRAL=y
|
||||
CONFIG_BT_SMP=y
|
||||
CONFIG_ASSERT=y
|
||||
|
||||
CONFIG_BT_EXT_ADV=y
|
||||
CONFIG_BT_PRIVACY=y
|
||||
CONFIG_BT_RPA_TIMEOUT=10
|
|
@ -0,0 +1,76 @@
|
|||
/*
|
||||
* Copyright (c) 2023 Nordic Semiconductor ASA
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "bs_bt_utils.h"
|
||||
#include "bs_pc_backchannel.h"
|
||||
#include "argparse.h"
|
||||
|
||||
#define BS_SECONDS(dur_sec) ((bs_time_t)dur_sec * 1000000)
|
||||
#define TEST_TIMEOUT_SIMULATED BS_SECONDS(120)
|
||||
#define CHANNEL_ID 0
|
||||
#define MSG_SIZE 1
|
||||
|
||||
void test_tick(bs_time_t HW_device_time)
|
||||
{
|
||||
bs_trace_debug_time(0, "Simulation ends now.\n");
|
||||
if (bst_result != Passed) {
|
||||
bst_result = Failed;
|
||||
|
||||
bs_trace_error("Test did not pass before simulation ended.\n");
|
||||
}
|
||||
}
|
||||
|
||||
void test_init(void)
|
||||
{
|
||||
bst_ticker_set_next_tick_absolute(TEST_TIMEOUT_SIMULATED);
|
||||
bst_result = In_progress;
|
||||
}
|
||||
|
||||
void backchannel_init(uint peer)
|
||||
{
|
||||
uint device_number = get_device_nbr();
|
||||
uint device_numbers[] = {peer};
|
||||
uint channel_numbers[] = {CHANNEL_ID};
|
||||
uint *ch;
|
||||
|
||||
ch = bs_open_back_channel(device_number, device_numbers, channel_numbers,
|
||||
ARRAY_SIZE(channel_numbers));
|
||||
if (!ch) {
|
||||
FAIL("Unable to open backchannel\n");
|
||||
}
|
||||
}
|
||||
|
||||
void backchannel_sync_send(void)
|
||||
{
|
||||
uint8_t sync_msg[MSG_SIZE] = {get_device_nbr()};
|
||||
|
||||
bs_bc_send_msg(CHANNEL_ID, sync_msg, ARRAY_SIZE(sync_msg));
|
||||
}
|
||||
|
||||
void backchannel_sync_wait(void)
|
||||
{
|
||||
uint8_t sync_msg[MSG_SIZE];
|
||||
|
||||
while (true) {
|
||||
if (bs_bc_is_msg_received(CHANNEL_ID) > 0) {
|
||||
bs_bc_receive_msg(CHANNEL_ID, sync_msg, ARRAY_SIZE(sync_msg));
|
||||
if (sync_msg[0] != get_device_nbr()) {
|
||||
/* Received a message from another device, exit */
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
k_sleep(K_MSEC(1));
|
||||
}
|
||||
}
|
||||
|
||||
void print_address(bt_addr_le_t *addr)
|
||||
{
|
||||
char array[BT_ADDR_LE_STR_LEN];
|
||||
|
||||
bt_addr_le_to_str(addr, array, sizeof(array));
|
||||
printk("Address : %s\n", array);
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
/**
|
||||
* Common functions and helpers for BSIM ADV tests
|
||||
*
|
||||
* Copyright (c) 2023 Nordic Semiconductor ASA
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "bs_tracing.h"
|
||||
#include "bs_types.h"
|
||||
#include "bstests.h"
|
||||
#include "time_machine.h"
|
||||
|
||||
#include <errno.h>
|
||||
|
||||
#include <zephyr/sys/__assert.h>
|
||||
#include <zephyr/bluetooth/bluetooth.h>
|
||||
#include <zephyr/bluetooth/conn.h>
|
||||
#include <zephyr/bluetooth/gatt.h>
|
||||
#include <zephyr/bluetooth/hci.h>
|
||||
#include <zephyr/bluetooth/uuid.h>
|
||||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/types.h>
|
||||
|
||||
extern enum bst_result_t bst_result;
|
||||
|
||||
#define ASSERT(expr, ...) \
|
||||
do { \
|
||||
if (!(expr)) { \
|
||||
FAIL(__VA_ARGS__); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#define FAIL(...) \
|
||||
do { \
|
||||
bst_result = Failed; \
|
||||
bs_trace_error_time_line(__VA_ARGS__); \
|
||||
} while (0)
|
||||
|
||||
#define PASS(...) \
|
||||
do { \
|
||||
bst_result = Passed; \
|
||||
bs_trace_info_time(1, __VA_ARGS__); \
|
||||
} while (0)
|
||||
|
||||
#define TESTER_CENTRAL_ID 0
|
||||
#define DUT_PERIPHERAL_ID 1
|
||||
|
||||
void test_tick(bs_time_t HW_device_time);
|
||||
void test_init(void);
|
||||
void backchannel_init(uint peer);
|
||||
void backchannel_sync_send(void);
|
||||
void backchannel_sync_wait(void);
|
||||
|
||||
void print_address(bt_addr_le_t *addr);
|
64
tests/bluetooth/bsim_bt/bsim_test_rpa_peripheral/src/dut.c
Normal file
64
tests/bluetooth/bsim_bt/bsim_test_rpa_peripheral/src/dut.c
Normal file
|
@ -0,0 +1,64 @@
|
|||
/*
|
||||
* Copyright (c) 2023 Nordic Semiconductor ASA
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "bs_bt_utils.h"
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <zephyr/bluetooth/addr.h>
|
||||
#include <zephyr/bluetooth/bluetooth.h>
|
||||
#include <zephyr/bluetooth/conn.h>
|
||||
#include <zephyr/toolchain/gcc.h>
|
||||
|
||||
bool cb_rpa_expired(struct bt_le_ext_adv *adv)
|
||||
{
|
||||
backchannel_sync_send();
|
||||
|
||||
/* Return true to rotate the current RPA */
|
||||
return true;
|
||||
}
|
||||
|
||||
void start_advertising(void)
|
||||
{
|
||||
int err;
|
||||
static struct bt_le_ext_adv_cb cb_adv;
|
||||
struct bt_le_ext_adv_start_param start_params;
|
||||
struct bt_le_ext_adv *adv;
|
||||
|
||||
/* Enable bluetooth */
|
||||
err = bt_enable(NULL);
|
||||
if (err) {
|
||||
FAIL("Failed to enable bluetooth (err %d\n)", err);
|
||||
}
|
||||
|
||||
/* Start non-connectable extended advertising with private address */
|
||||
start_params.timeout = 0;
|
||||
start_params.num_events = 0;
|
||||
cb_adv.rpa_expired = cb_rpa_expired;
|
||||
|
||||
err = bt_le_ext_adv_create(BT_LE_EXT_ADV_CONN_NAME, &cb_adv, &adv);
|
||||
if (err) {
|
||||
FAIL("Failed to create advertising set (err %d)\n", err);
|
||||
}
|
||||
|
||||
err = bt_le_ext_adv_start(adv, &start_params);
|
||||
if (err) {
|
||||
FAIL("Failed to enable periodic advertising (err %d)\n", err);
|
||||
}
|
||||
}
|
||||
|
||||
void dut_procedure(void)
|
||||
{
|
||||
/* Setup synchronization channel */
|
||||
backchannel_init(TESTER_CENTRAL_ID);
|
||||
|
||||
start_advertising();
|
||||
|
||||
/* Nothing to do */
|
||||
|
||||
PASS("PASS\n");
|
||||
}
|
39
tests/bluetooth/bsim_bt/bsim_test_rpa_peripheral/src/main.c
Normal file
39
tests/bluetooth/bsim_bt/bsim_test_rpa_peripheral/src/main.c
Normal file
|
@ -0,0 +1,39 @@
|
|||
/*
|
||||
* Copyright (c) 2023 Nordic Semiconductor ASA
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "bs_bt_utils.h"
|
||||
#include "bstests.h"
|
||||
|
||||
void tester_procedure(void);
|
||||
void dut_procedure(void);
|
||||
|
||||
static const struct bst_test_instance test_to_add[] = {
|
||||
{
|
||||
.test_id = "central",
|
||||
.test_post_init_f = test_init,
|
||||
.test_tick_f = test_tick,
|
||||
.test_main_f = tester_procedure,
|
||||
},
|
||||
{
|
||||
.test_id = "peripheral",
|
||||
.test_post_init_f = test_init,
|
||||
.test_tick_f = test_tick,
|
||||
.test_main_f = dut_procedure,
|
||||
},
|
||||
BSTEST_END_MARKER,
|
||||
};
|
||||
|
||||
static struct bst_test_list *install(struct bst_test_list *tests)
|
||||
{
|
||||
return bst_add_tests(tests, test_to_add);
|
||||
};
|
||||
|
||||
bst_test_install_t test_installers[] = {install, NULL};
|
||||
|
||||
void main(void)
|
||||
{
|
||||
bst_main();
|
||||
}
|
102
tests/bluetooth/bsim_bt/bsim_test_rpa_peripheral/src/tester.c
Normal file
102
tests/bluetooth/bsim_bt/bsim_test_rpa_peripheral/src/tester.c
Normal file
|
@ -0,0 +1,102 @@
|
|||
/*
|
||||
* Copyright (c) 2023 Nordic Semiconductor ASA
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "bs_bt_utils.h"
|
||||
#include "zephyr/bluetooth/addr.h"
|
||||
#include "zephyr/bluetooth/conn.h"
|
||||
#include <stdint.h>
|
||||
#include <zephyr/bluetooth/bluetooth.h>
|
||||
|
||||
static volatile int64_t old_time, new_time;
|
||||
static bt_addr_le_t old_addr;
|
||||
static bt_addr_le_t *new_addr;
|
||||
|
||||
static void cb_device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type,
|
||||
struct net_buf_simple *ad)
|
||||
{
|
||||
static bool init;
|
||||
|
||||
if (!init) {
|
||||
old_addr = *addr;
|
||||
old_time = k_uptime_get();
|
||||
init = true;
|
||||
}
|
||||
|
||||
new_addr = (bt_addr_le_t *)addr;
|
||||
new_time = k_uptime_get();
|
||||
}
|
||||
|
||||
void start_scanning(void)
|
||||
{
|
||||
int err;
|
||||
struct bt_le_scan_param params;
|
||||
|
||||
/* Enable bluetooth */
|
||||
err = bt_enable(NULL);
|
||||
if (err) {
|
||||
FAIL("Failed to enable bluetooth (err %d\n)", err);
|
||||
}
|
||||
|
||||
/* Start passive scanning */
|
||||
params.type = BT_LE_SCAN_TYPE_PASSIVE;
|
||||
params.options = BT_LE_SCAN_OPT_FILTER_DUPLICATE;
|
||||
params.interval = BT_GAP_SCAN_FAST_INTERVAL;
|
||||
params.window = BT_GAP_SCAN_FAST_WINDOW;
|
||||
|
||||
err = bt_le_scan_start(¶ms, cb_device_found);
|
||||
if (err) {
|
||||
FAIL("Failed to start scanning");
|
||||
}
|
||||
}
|
||||
|
||||
void tester_procedure(void)
|
||||
{
|
||||
int err;
|
||||
|
||||
/* Setup synchronization channel */
|
||||
backchannel_init(DUT_PERIPHERAL_ID);
|
||||
|
||||
start_scanning();
|
||||
|
||||
/* Wait for the first address rotation */
|
||||
backchannel_sync_wait();
|
||||
|
||||
for (uint16_t i = 0; i < 5; i++) {
|
||||
int64_t diff, time_diff_ms, rpa_timeout_ms;
|
||||
|
||||
backchannel_sync_wait();
|
||||
|
||||
/* Compare old and new address */
|
||||
err = bt_addr_le_cmp(&old_addr, new_addr);
|
||||
if (err == 0) {
|
||||
FAIL("RPA did not rotate", err);
|
||||
}
|
||||
|
||||
/* Ensure the RPA rotation occurs within +-10% of CONFIG_BT_RPA_TIMEOUT */
|
||||
time_diff_ms = new_time - old_time;
|
||||
rpa_timeout_ms = CONFIG_BT_RPA_TIMEOUT * MSEC_PER_SEC;
|
||||
|
||||
if (time_diff_ms > rpa_timeout_ms) {
|
||||
diff = time_diff_ms - rpa_timeout_ms;
|
||||
} else {
|
||||
diff = rpa_timeout_ms - time_diff_ms;
|
||||
}
|
||||
|
||||
if (diff > (rpa_timeout_ms / 10)) {
|
||||
FAIL("RPA rotation did not occur within +-10%% of CONFIG_BT_RPA_TIMEOUT");
|
||||
}
|
||||
|
||||
printk("Old ");
|
||||
print_address(&old_addr);
|
||||
printk("New ");
|
||||
print_address(new_addr);
|
||||
|
||||
old_addr = *new_addr;
|
||||
old_time = new_time;
|
||||
}
|
||||
|
||||
PASS("PASS\n");
|
||||
}
|
24
tests/bluetooth/bsim_bt/bsim_test_rpa_peripheral/test_scripts/_compile.sh
Executable file
24
tests/bluetooth/bsim_bt/bsim_test_rpa_peripheral/test_scripts/_compile.sh
Executable file
|
@ -0,0 +1,24 @@
|
|||
#!/usr/bin/env bash
|
||||
# Copyright 2023 Nordic Semiconductor ASA
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
set -eu
|
||||
bash_source_dir="$(realpath "$(dirname "${BASH_SOURCE[0]}")")"
|
||||
|
||||
# Read variable definitions output by _env.sh
|
||||
source <("${bash_source_dir}/_env.sh")
|
||||
|
||||
|
||||
: "${BSIM_OUT_PATH:?BSIM_OUT_PATH must be defined}"
|
||||
: "${BSIM_COMPONENTS_PATH:?BSIM_COMPONENTS_PATH must be defined}"
|
||||
: "${ZEPHYR_BASE:?ZEPHYR_BASE must be defined}"
|
||||
|
||||
|
||||
WORK_DIR="${WORK_DIR:-${ZEPHYR_BASE}/bsim_bt_out}"
|
||||
BOARD="${BOARD:-nrf52_bsim}"
|
||||
BOARD_ROOT="${BOARD_ROOT:-${ZEPHYR_BASE}}"
|
||||
INCR_BUILD=1
|
||||
mkdir -p ${WORK_DIR}
|
||||
|
||||
west build -b nrf52_bsim
|
||||
cp build/zephyr/zephyr.exe $central_exe
|
31
tests/bluetooth/bsim_bt/bsim_test_rpa_peripheral/test_scripts/_env.sh
Executable file
31
tests/bluetooth/bsim_bt/bsim_test_rpa_peripheral/test_scripts/_env.sh
Executable file
|
@ -0,0 +1,31 @@
|
|||
#!/usr/bin/env bash
|
||||
# Copyright 2023 Nordic Semiconductor ASA
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
set -eu
|
||||
bash_source_dir="$(realpath "$(dirname "${BASH_SOURCE[0]}")")"
|
||||
|
||||
: "${BSIM_OUT_PATH:?BSIM_OUT_PATH must be defined}"
|
||||
|
||||
test_name="$(basename "$(realpath "$bash_source_dir/..")")"
|
||||
bsim_bin="${BSIM_OUT_PATH}/bin"
|
||||
verbosity_level=2
|
||||
BOARD="${BOARD:-nrf52_bsim}"
|
||||
simulation_id="$test_name"
|
||||
central_exe="${bsim_bin}/bs_${BOARD}_tests_bluetooth_bsim_bt_${test_name}_prj_conf"
|
||||
peripheral_exe="${bsim_bin}/bs_${BOARD}_tests_bluetooth_bsim_bt_${test_name}_prj_conf"
|
||||
|
||||
function print_var {
|
||||
# Print a shell-sourceable variable definition.
|
||||
local var_name="$1"
|
||||
local var_repr="${!var_name@Q}"
|
||||
echo "$var_name=$var_repr"
|
||||
}
|
||||
|
||||
print_var test_name
|
||||
print_var bsim_bin
|
||||
print_var verbosity_level
|
||||
print_var BOARD
|
||||
print_var simulation_id
|
||||
print_var central_exe
|
||||
print_var peripheral_exe
|
41
tests/bluetooth/bsim_bt/bsim_test_rpa_peripheral/test_scripts/run_test.sh
Executable file
41
tests/bluetooth/bsim_bt/bsim_test_rpa_peripheral/test_scripts/run_test.sh
Executable file
|
@ -0,0 +1,41 @@
|
|||
#!/usr/bin/env bash
|
||||
# Copyright 2023 Nordic Semiconductor ASA
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
set -eu
|
||||
bash_source_dir="$(realpath "$(dirname "${BASH_SOURCE[0]}")")"
|
||||
|
||||
# Read variable definitions output by _env.sh
|
||||
source <("${bash_source_dir}/_env.sh")
|
||||
|
||||
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 30 $@ &
|
||||
process_ids="$process_ids $!"
|
||||
}
|
||||
|
||||
: "${BSIM_OUT_PATH:?BSIM_OUT_PATH must be defined}"
|
||||
|
||||
cd ${BSIM_OUT_PATH}/bin
|
||||
|
||||
Execute "$central_exe" \
|
||||
-v=${verbosity_level} -s=${simulation_id} -d=0 -testid=central -RealEncryption=1
|
||||
|
||||
Execute "$peripheral_exe" \
|
||||
-v=${verbosity_level} -s=${simulation_id} -d=1 -testid=peripheral -RealEncryption=1
|
||||
|
||||
Execute ./bs_2G4_phy_v1 -v=${verbosity_level} -s=${simulation_id} \
|
||||
-D=2 -sim_length=120e6 $@
|
||||
|
||||
for process_id in $process_ids; do
|
||||
wait $process_id || let "exit_code=$?"
|
||||
done
|
||||
|
||||
exit $exit_code #the last exit code != 0
|
|
@ -49,6 +49,8 @@ app=tests/bluetooth/bsim_bt/bsim_test_l2cap compile &
|
|||
app=tests/bluetooth/bsim_bt/bsim_test_l2cap_userdata compile &
|
||||
app=tests/bluetooth/bsim_bt/bsim_test_l2cap_stress compile &
|
||||
app=tests/bluetooth/bsim_bt/bsim_test_iso compile &
|
||||
app=tests/bluetooth/bsim_bt/bsim_test_rpa_central compile &
|
||||
app=tests/bluetooth/bsim_bt/bsim_test_rpa_peripheral compile &
|
||||
app=tests/bluetooth/bsim_bt/bsim_test_iso conf_file=prj_vs_dp.conf \
|
||||
compile &
|
||||
app=tests/bluetooth/bsim_bt/bsim_test_audio compile &
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue