Skip to content

Commit 4a24943

Browse files
jori-nordicaescolar
authored andcommitted
tests: Bluetooth: Refactor host/privacy/peripheral test
The test had a number of problems, rewrote to fix those and not use backchannels. Fixes #56733 Signed-off-by: Jonathan Rico <[email protected]>
1 parent 9c93042 commit 4a24943

File tree

6 files changed

+54
-125
lines changed

6 files changed

+54
-125
lines changed

tests/bsim/bluetooth/host/privacy/peripheral/src/bs_bt_utils.c

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,10 @@
55
*/
66

77
#include "bs_bt_utils.h"
8-
#include "bs_pc_backchannel.h"
98
#include "argparse.h"
109

1110
#define BS_SECONDS(dur_sec) ((bs_time_t)dur_sec * 1000000)
12-
#define TEST_TIMEOUT_SIMULATED BS_SECONDS(120)
13-
#define CHANNEL_ID 0
14-
#define MSG_SIZE 1
11+
#define TEST_TIMEOUT_SIMULATED BS_SECONDS(70)
1512

1613
void test_tick(bs_time_t HW_device_time)
1714
{
@@ -29,44 +26,6 @@ void test_init(void)
2926
bst_result = In_progress;
3027
}
3128

32-
void backchannel_init(uint peer)
33-
{
34-
uint device_number = get_device_nbr();
35-
uint device_numbers[] = {peer};
36-
uint channel_numbers[] = {CHANNEL_ID};
37-
uint *ch;
38-
39-
ch = bs_open_back_channel(device_number, device_numbers, channel_numbers,
40-
ARRAY_SIZE(channel_numbers));
41-
if (!ch) {
42-
FAIL("Unable to open backchannel\n");
43-
}
44-
}
45-
46-
void backchannel_sync_send(void)
47-
{
48-
uint8_t sync_msg[MSG_SIZE] = {get_device_nbr()};
49-
50-
bs_bc_send_msg(CHANNEL_ID, sync_msg, ARRAY_SIZE(sync_msg));
51-
}
52-
53-
void backchannel_sync_wait(void)
54-
{
55-
uint8_t sync_msg[MSG_SIZE];
56-
57-
while (true) {
58-
if (bs_bc_is_msg_received(CHANNEL_ID) > 0) {
59-
bs_bc_receive_msg(CHANNEL_ID, sync_msg, ARRAY_SIZE(sync_msg));
60-
if (sync_msg[0] != get_device_nbr()) {
61-
/* Received a message from another device, exit */
62-
break;
63-
}
64-
}
65-
66-
k_sleep(K_MSEC(1));
67-
}
68-
}
69-
7029
void print_address(bt_addr_le_t *addr)
7130
{
7231
char array[BT_ADDR_LE_STR_LEN];

tests/bsim/bluetooth/host/privacy/peripheral/src/bs_bt_utils.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,5 @@ extern enum bst_result_t bst_result;
4848

4949
void test_tick(bs_time_t HW_device_time);
5050
void test_init(void);
51-
void backchannel_init(uint peer);
52-
void backchannel_sync_send(void);
53-
void backchannel_sync_wait(void);
5451

5552
void print_address(bt_addr_le_t *addr);

tests/bsim/bluetooth/host/privacy/peripheral/src/dut.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
bool cb_rpa_expired(struct bt_le_ext_adv *adv)
1818
{
19-
backchannel_sync_send();
20-
2119
/* Return true to rotate the current RPA */
2220
return true;
2321
}
@@ -53,12 +51,8 @@ void start_advertising(void)
5351

5452
void dut_procedure(void)
5553
{
56-
/* Setup synchronization channel */
57-
backchannel_init(TESTER_CENTRAL_ID);
58-
5954
start_advertising();
6055

6156
/* Nothing to do */
62-
6357
PASS("PASS\n");
6458
}

tests/bsim/bluetooth/host/privacy/peripheral/src/tester.c

Lines changed: 50 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -10,93 +10,84 @@
1010
#include <stdint.h>
1111
#include <zephyr/bluetooth/bluetooth.h>
1212

13-
static volatile int64_t old_time, new_time;
13+
#define EXPECTED_NUM_ROTATIONS 5
14+
1415
static bt_addr_le_t old_addr;
15-
static bt_addr_le_t *new_addr;
16+
static int64_t old_time;
17+
static int rpa_rotations;
1618

17-
static void cb_device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type,
18-
struct net_buf_simple *ad)
19+
static void test_address(bt_addr_le_t *addr)
1920
{
20-
static bool init;
21+
int64_t diff_ms, rpa_timeout_ms;
2122

22-
if (!init) {
23-
old_addr = *addr;
23+
/* Only save the address + time if this is the first scan */
24+
if (bt_addr_le_eq(&old_addr, BT_ADDR_LE_ANY)) {
25+
bt_addr_le_copy(&old_addr, addr);
2426
old_time = k_uptime_get();
25-
init = true;
27+
return;
28+
}
29+
30+
/* Compare old and new address */
31+
if (bt_addr_le_eq(&old_addr, addr)) {
32+
return;
33+
}
34+
35+
printk("Old ");
36+
print_address(&old_addr);
37+
printk("New ");
38+
print_address(addr);
39+
40+
rpa_rotations++;
41+
42+
/* Ensure the RPA rotation occurs within +-10% of CONFIG_BT_RPA_TIMEOUT */
43+
diff_ms = k_uptime_get() - old_time;
44+
rpa_timeout_ms = CONFIG_BT_RPA_TIMEOUT * MSEC_PER_SEC;
45+
46+
if (abs(diff_ms - rpa_timeout_ms) > (rpa_timeout_ms / 10)) {
47+
FAIL("RPA rotation did not occur within +-10% of CONFIG_BT_RPA_TIMEOUT\n");
2648
}
2749

28-
new_addr = (bt_addr_le_t *)addr;
29-
new_time = k_uptime_get();
50+
bt_addr_le_copy(&old_addr, addr);
51+
old_time = k_uptime_get();
52+
53+
if (rpa_rotations > EXPECTED_NUM_ROTATIONS) {
54+
PASS("PASS\n");
55+
}
56+
}
57+
58+
static void cb_device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type,
59+
struct net_buf_simple *ad)
60+
{
61+
test_address((bt_addr_le_t *)addr);
3062
}
3163

3264
void start_scanning(void)
3365
{
34-
int err;
3566
struct bt_le_scan_param params;
3667

37-
/* Enable bluetooth */
38-
err = bt_enable(NULL);
39-
if (err) {
40-
FAIL("Failed to enable bluetooth (err %d\n)", err);
41-
}
42-
4368
/* Start passive scanning */
4469
params.type = BT_LE_SCAN_TYPE_PASSIVE;
4570
params.options = BT_LE_SCAN_OPT_FILTER_DUPLICATE;
4671
params.interval = BT_GAP_SCAN_FAST_INTERVAL;
4772
params.window = BT_GAP_SCAN_FAST_WINDOW;
73+
params.timeout = 0;
4874

49-
err = bt_le_scan_start(&params, cb_device_found);
75+
int err = bt_le_scan_start(&params, cb_device_found);
5076
if (err) {
5177
FAIL("Failed to start scanning");
5278
}
5379
}
5480

5581
void tester_procedure(void)
5682
{
57-
int err;
83+
/* Enable bluetooth */
84+
int err = bt_enable(NULL);
5885

59-
/* Setup synchronization channel */
60-
backchannel_init(DUT_PERIPHERAL_ID);
86+
if (err) {
87+
FAIL("Failed to enable bluetooth (err %d\n)", err);
88+
}
6189

6290
start_scanning();
6391

64-
/* Wait for the first address rotation */
65-
backchannel_sync_wait();
66-
67-
for (uint16_t i = 0; i < 5; i++) {
68-
int64_t diff, time_diff_ms, rpa_timeout_ms;
69-
70-
backchannel_sync_wait();
71-
72-
/* Compare old and new address */
73-
err = bt_addr_le_cmp(&old_addr, new_addr);
74-
if (err == 0) {
75-
FAIL("RPA did not rotate", err);
76-
}
77-
78-
/* Ensure the RPA rotation occurs within +-10% of CONFIG_BT_RPA_TIMEOUT */
79-
time_diff_ms = new_time - old_time;
80-
rpa_timeout_ms = CONFIG_BT_RPA_TIMEOUT * MSEC_PER_SEC;
81-
82-
if (time_diff_ms > rpa_timeout_ms) {
83-
diff = time_diff_ms - rpa_timeout_ms;
84-
} else {
85-
diff = rpa_timeout_ms - time_diff_ms;
86-
}
87-
88-
if (diff > (rpa_timeout_ms / 10)) {
89-
FAIL("RPA rotation did not occur within +-10%% of CONFIG_BT_RPA_TIMEOUT");
90-
}
91-
92-
printk("Old ");
93-
print_address(&old_addr);
94-
printk("New ");
95-
print_address(new_addr);
96-
97-
old_addr = *new_addr;
98-
old_time = new_time;
99-
}
100-
101-
PASS("PASS\n");
92+
/* The rest of the test is driven by the callback */
10293
}

tests/bsim/bluetooth/host/privacy/peripheral/test_scripts/_compile.sh

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,5 @@ bash_source_dir="$(realpath "$(dirname "${BASH_SOURCE[0]}")")"
88
# Read variable definitions output by _env.sh
99
source "${bash_source_dir}/_env.sh"
1010

11-
12-
: "${BSIM_OUT_PATH:?BSIM_OUT_PATH must be defined}"
13-
: "${BSIM_COMPONENTS_PATH:?BSIM_COMPONENTS_PATH must be defined}"
14-
: "${ZEPHYR_BASE:?ZEPHYR_BASE must be defined}"
15-
16-
17-
WORK_DIR="${WORK_DIR:-${ZEPHYR_BASE}/bsim_out}"
18-
BOARD="${BOARD:-nrf52_bsim}"
19-
BOARD_ROOT="${BOARD_ROOT:-${ZEPHYR_BASE}}"
20-
INCR_BUILD=1
21-
mkdir -p ${WORK_DIR}
22-
23-
west build -b nrf52_bsim
24-
cp build/zephyr/zephyr.exe $central_exe
11+
west build -b nrf52_bsim && \
12+
cp build/zephyr/zephyr.exe $central_exe

tests/bsim/bluetooth/host/privacy/peripheral/test_scripts/run_test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ Execute "$peripheral_exe" \
1919
-v=${verbosity_level} -s=${simulation_id} -d=1 -testid=peripheral -RealEncryption=1
2020

2121
Execute ./bs_2G4_phy_v1 -v=${verbosity_level} -s=${simulation_id} \
22-
-D=2 -sim_length=120e6 $@
22+
-D=2 -sim_length=70e6 $@
2323

2424
wait_for_background_jobs

0 commit comments

Comments
 (0)