Skip to content

Commit 8e5673c

Browse files
mkapala-nordicjhedberg
authored andcommitted
bluetooth: tests: Add bsim test for setting bondable flag per-conn
Added test for the bt_conn_set_bondable API function. Check if we can pair without setting the bonding flag on the per-connection basis if the device was already bonded on the other identity. Signed-off-by: Mateusz Kapala <[email protected]>
1 parent 5b44ebe commit 8e5673c

File tree

11 files changed

+556
-0
lines changed

11 files changed

+556
-0
lines changed

tests/bsim/bluetooth/host/compile.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ app=tests/bsim/bluetooth/host/privacy/legacy compile
6565

6666
app=tests/bsim/bluetooth/host/security/bond_overwrite_allowed compile
6767
app=tests/bsim/bluetooth/host/security/bond_overwrite_denied compile
68+
app=tests/bsim/bluetooth/host/security/bond_per_connection compile
6869
app=tests/bsim/bluetooth/host/security/ccc_update compile
6970
app=tests/bsim/bluetooth/host/security/ccc_update conf_file=prj_2.conf compile
7071

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
5+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
6+
project(bsim_test_multi_id_bond_per_connection)
7+
8+
target_sources(app PRIVATE
9+
src/bs_bt_utils.c
10+
src/central.c
11+
src/main.c
12+
src/peripheral.c
13+
)
14+
15+
zephyr_include_directories(
16+
${BSIM_COMPONENTS_PATH}/libUtilv1/src/
17+
${BSIM_COMPONENTS_PATH}/libPhyComv1/src/
18+
)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
CONFIG_BT=y
2+
CONFIG_BT_PERIPHERAL=y
3+
CONFIG_BT_CENTRAL=y
4+
5+
CONFIG_BT_SMP=y
6+
7+
CONFIG_ASSERT=y
8+
CONFIG_BT_TESTING=y
9+
CONFIG_LOG=y
10+
11+
CONFIG_BT_ID_MAX=3
12+
CONFIG_BT_MAX_PAIRED=2
13+
CONFIG_BT_MAX_CONN=1
14+
15+
CONFIG_BT_PRIVACY=y
16+
17+
CONFIG_BT_BONDABLE_PER_CONNECTION=y
Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
/*
2+
* Copyright (c) 2023 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include "bs_bt_utils.h"
8+
9+
BUILD_ASSERT(CONFIG_BT_MAX_PAIRED >= 2, "CONFIG_BT_MAX_PAIRED is too small.");
10+
BUILD_ASSERT(CONFIG_BT_ID_MAX >= 3, "CONFIG_BT_ID_MAX is too small.");
11+
12+
#define BS_SECONDS(dur_sec) ((bs_time_t)dur_sec * 1000000)
13+
#define TEST_TIMEOUT_SIMULATED BS_SECONDS(60)
14+
15+
void test_tick(bs_time_t HW_device_time)
16+
{
17+
bs_trace_debug_time(0, "Simulation ends now.\n");
18+
if (bst_result != Passed) {
19+
bst_result = Failed;
20+
bs_trace_error("Test did not pass before simulation ended.\n");
21+
}
22+
}
23+
24+
void test_init(void)
25+
{
26+
bst_ticker_set_next_tick_absolute(TEST_TIMEOUT_SIMULATED);
27+
bst_result = In_progress;
28+
}
29+
30+
DEFINE_FLAG(flag_is_connected);
31+
struct bt_conn *g_conn;
32+
DEFINE_FLAG(bondable);
33+
DEFINE_FLAG(call_bt_conn_set_bondable);
34+
35+
void wait_connected(void)
36+
{
37+
WAIT_FOR_FLAG(flag_is_connected);
38+
}
39+
40+
void wait_disconnected(void)
41+
{
42+
WAIT_FOR_FLAG_UNSET(flag_is_connected);
43+
}
44+
45+
static void disconnected(struct bt_conn *conn, uint8_t reason)
46+
{
47+
UNSET_FLAG(flag_is_connected);
48+
}
49+
50+
BUILD_ASSERT(CONFIG_BT_MAX_CONN == 1, "This test assumes a single link.");
51+
static void connected(struct bt_conn *conn, uint8_t err)
52+
{
53+
ASSERT((!g_conn || (conn == g_conn)), "Unexpected new connection.");
54+
55+
if (!g_conn) {
56+
g_conn = bt_conn_ref(conn);
57+
}
58+
59+
if (err != 0) {
60+
clear_g_conn();
61+
return;
62+
}
63+
64+
SET_FLAG(flag_is_connected);
65+
66+
if (GET_FLAG(call_bt_conn_set_bondable) && bt_conn_set_bondable(conn, GET_FLAG(bondable))) {
67+
ASSERT(0, "Fail during setting bondable flag for given connection.");
68+
}
69+
}
70+
71+
BT_CONN_CB_DEFINE(conn_callbacks) = {
72+
.connected = connected,
73+
.disconnected = disconnected,
74+
};
75+
76+
void clear_g_conn(void)
77+
{
78+
struct bt_conn *conn;
79+
80+
conn = g_conn;
81+
g_conn = NULL;
82+
ASSERT(conn, "Test error: No g_conn!\n");
83+
bt_conn_unref(conn);
84+
}
85+
86+
/* The following flags are raised by events and lowered by test code. */
87+
DEFINE_FLAG(flag_pairing_complete);
88+
DEFINE_FLAG(flag_bonded);
89+
DEFINE_FLAG(flag_not_bonded);
90+
91+
static void pairing_complete(struct bt_conn *conn, bool bonded)
92+
{
93+
SET_FLAG(flag_pairing_complete);
94+
95+
if (bonded) {
96+
SET_FLAG(flag_bonded);
97+
} else {
98+
SET_FLAG(flag_not_bonded);
99+
}
100+
}
101+
102+
static struct bt_conn_auth_info_cb bt_conn_auth_info_cb = {
103+
.pairing_complete = pairing_complete,
104+
};
105+
106+
void bs_bt_utils_setup(void)
107+
{
108+
int err;
109+
110+
err = bt_enable(NULL);
111+
ASSERT(!err, "bt_enable failed.\n");
112+
err = bt_conn_auth_info_cb_register(&bt_conn_auth_info_cb);
113+
ASSERT(!err, "bt_conn_auth_info_cb_register failed.\n");
114+
}
115+
116+
static void scan_connect_to_first_result__device_found(const bt_addr_le_t *addr, int8_t rssi,
117+
uint8_t type, struct net_buf_simple *ad)
118+
{
119+
char addr_str[BT_ADDR_LE_STR_LEN];
120+
int err;
121+
122+
if (g_conn != NULL) {
123+
return;
124+
}
125+
126+
/* We're only interested in connectable events */
127+
if (type != BT_HCI_ADV_IND && type != BT_HCI_ADV_DIRECT_IND) {
128+
FAIL("Unexpected advertisement type.");
129+
}
130+
131+
bt_addr_le_to_str(addr, addr_str, sizeof(addr_str));
132+
printk("Got scan result, connecting.. dst %s, RSSI %d\n", addr_str, rssi);
133+
134+
err = bt_le_scan_stop();
135+
ASSERT(!err, "Err bt_le_scan_stop %d", err);
136+
137+
err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN, BT_LE_CONN_PARAM_DEFAULT, &g_conn);
138+
ASSERT(!err, "Err bt_conn_le_create %d", err);
139+
}
140+
141+
void scan_connect_to_first_result(void)
142+
{
143+
int err;
144+
145+
err = bt_le_scan_start(BT_LE_SCAN_PASSIVE, scan_connect_to_first_result__device_found);
146+
ASSERT(!err, "Err bt_le_scan_start %d", err);
147+
}
148+
149+
void disconnect(void)
150+
{
151+
int err;
152+
153+
err = bt_conn_disconnect(g_conn, BT_HCI_ERR_REMOTE_USER_TERM_CONN);
154+
ASSERT(!err, "Err bt_conn_disconnect %d", err);
155+
}
156+
157+
void unpair(int id)
158+
{
159+
int err;
160+
161+
err = bt_unpair(id, BT_ADDR_LE_ANY);
162+
ASSERT(!err, "Err bt_unpair %d", err);
163+
}
164+
165+
void set_security(bt_security_t sec)
166+
{
167+
int err;
168+
169+
err = bt_conn_set_security(g_conn, sec);
170+
ASSERT(!err, "Err bt_conn_set_security %d", err);
171+
}
172+
173+
void advertise_connectable(int id, bt_addr_le_t *directed_dst)
174+
{
175+
int err;
176+
struct bt_le_adv_param param = {};
177+
178+
param.id = id;
179+
param.interval_min = 0x0020;
180+
param.interval_max = 0x4000;
181+
param.options |= BT_LE_ADV_OPT_ONE_TIME;
182+
param.options |= BT_LE_ADV_OPT_CONNECTABLE;
183+
184+
if (directed_dst) {
185+
param.options |= BT_LE_ADV_OPT_DIR_ADDR_RPA;
186+
param.peer = directed_dst;
187+
}
188+
189+
err = bt_le_adv_start(&param, NULL, 0, NULL, 0);
190+
ASSERT(err == 0, "Advertising failed to start (err %d)\n", err);
191+
}
192+
193+
void set_bondable(bool enable)
194+
{
195+
if (enable) {
196+
SET_FLAG(bondable);
197+
} else {
198+
UNSET_FLAG(bondable);
199+
}
200+
}
201+
202+
void enable_bt_conn_set_bondable(bool enable)
203+
{
204+
if (enable) {
205+
SET_FLAG(call_bt_conn_set_bondable);
206+
} else {
207+
UNSET_FLAG(call_bt_conn_set_bondable);
208+
}
209+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**
2+
* Common functions and helpers for BSIM GATT tests
3+
*
4+
* Copyright (c) 2023 Nordic Semiconductor ASA
5+
*
6+
* SPDX-License-Identifier: Apache-2.0
7+
*/
8+
9+
#include "bs_tracing.h"
10+
#include "bs_types.h"
11+
#include "bstests.h"
12+
#include "time_machine.h"
13+
#include "zephyr/sys/__assert.h"
14+
15+
#include <errno.h>
16+
#include <stddef.h>
17+
#include <stdint.h>
18+
19+
#include <zephyr/bluetooth/bluetooth.h>
20+
#include <zephyr/bluetooth/conn.h>
21+
#include <zephyr/bluetooth/gatt.h>
22+
#include <zephyr/bluetooth/hci.h>
23+
#include <zephyr/bluetooth/uuid.h>
24+
#include <zephyr/kernel.h>
25+
#include <zephyr/types.h>
26+
27+
extern enum bst_result_t bst_result;
28+
29+
#define DECLARE_FLAG(flag) extern atomic_t flag
30+
#define DEFINE_FLAG(flag) atomic_t flag = (atomic_t) false
31+
#define SET_FLAG(flag) (void)atomic_set(&flag, (atomic_t) true)
32+
#define UNSET_FLAG(flag) (void)atomic_set(&flag, (atomic_t) false)
33+
#define WAIT_FOR_FLAG(flag) \
34+
while (!(bool)atomic_get(&flag)) { \
35+
(void)k_sleep(K_MSEC(1)); \
36+
}
37+
#define WAIT_FOR_FLAG_UNSET(flag) \
38+
while ((bool)atomic_get(&flag)) { \
39+
(void)k_sleep(K_MSEC(1)); \
40+
}
41+
#define TAKE_FLAG(flag) \
42+
while (!(bool)atomic_cas(&flag, true, false)) { \
43+
(void)k_sleep(K_MSEC(1)); \
44+
}
45+
#define GET_FLAG(flag) \
46+
(bool)atomic_get(&flag)
47+
48+
#define ASSERT(expr, ...) \
49+
do { \
50+
if (!(expr)) { \
51+
FAIL(__VA_ARGS__); \
52+
} \
53+
} while (0)
54+
55+
#define FAIL(...) \
56+
do { \
57+
bst_result = Failed; \
58+
bs_trace_error_time_line(__VA_ARGS__); \
59+
} while (0)
60+
61+
#define PASS(...) \
62+
do { \
63+
bst_result = Passed; \
64+
bs_trace_info_time(1, __VA_ARGS__); \
65+
} while (0)
66+
67+
void test_tick(bs_time_t HW_device_time);
68+
void test_init(void);
69+
70+
DECLARE_FLAG(flag_pairing_complete);
71+
DECLARE_FLAG(flag_bonded);
72+
DECLARE_FLAG(flag_not_bonded);
73+
74+
extern struct bt_conn *g_conn;
75+
void wait_connected(void);
76+
void wait_disconnected(void);
77+
void clear_g_conn(void);
78+
void bs_bt_utils_setup(void);
79+
void scan_connect_to_first_result(void);
80+
void disconnect(void);
81+
void unpair(int id);
82+
void set_security(bt_security_t sec);
83+
void advertise_connectable(int id, bt_addr_le_t *directed_dst);
84+
void set_bondable(bool enable);
85+
void enable_bt_conn_set_bondable(bool enable);
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (c) 2023 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include "bs_bt_utils.h"
8+
#include "zephyr/bluetooth/addr.h"
9+
#include "zephyr/bluetooth/conn.h"
10+
11+
#include <stdint.h>
12+
13+
#include <zephyr/bluetooth/bluetooth.h>
14+
15+
void central(void)
16+
{
17+
bs_bt_utils_setup();
18+
19+
printk("== Bonding id a - global bondable mode ==\n");
20+
BUILD_ASSERT(CONFIG_BT_BONDABLE, "CONFIG_BT_BONDABLE must be enabled by default.");
21+
enable_bt_conn_set_bondable(false);
22+
scan_connect_to_first_result();
23+
wait_connected();
24+
set_security(BT_SECURITY_L2);
25+
TAKE_FLAG(flag_pairing_complete);
26+
TAKE_FLAG(flag_bonded);
27+
disconnect();
28+
wait_disconnected();
29+
unpair(BT_ID_DEFAULT);
30+
clear_g_conn();
31+
32+
printk("== Bonding id a - bond per-connection ==\n");
33+
enable_bt_conn_set_bondable(true);
34+
set_bondable(true);
35+
scan_connect_to_first_result();
36+
wait_connected();
37+
set_security(BT_SECURITY_L2);
38+
TAKE_FLAG(flag_pairing_complete);
39+
TAKE_FLAG(flag_bonded);
40+
disconnect();
41+
wait_disconnected();
42+
clear_g_conn();
43+
44+
printk("== Bonding id b - bond per-connection ==\n");
45+
scan_connect_to_first_result();
46+
wait_connected();
47+
set_security(BT_SECURITY_L2);
48+
TAKE_FLAG(flag_pairing_complete);
49+
TAKE_FLAG(flag_not_bonded);
50+
disconnect();
51+
wait_disconnected();
52+
clear_g_conn();
53+
54+
PASS("PASS\n");
55+
}

0 commit comments

Comments
 (0)