Skip to content

Commit 67236de

Browse files
jori-nordiccarlescufi
authored andcommitted
Bluetooth: host: add test for some GATT settings bugs
This test is designed to check the soundness of GATT caching across reboots. It will fail if the fixes for zephyrproject-rtos#54173 and zephyrproject-rtos#54172 are not present. Signed-off-by: Jonathan Rico <[email protected]>
1 parent 76f23d6 commit 67236de

File tree

15 files changed

+1664
-0
lines changed

15 files changed

+1664
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
5+
if (NOT DEFINED ENV{BSIM_COMPONENTS_PATH})
6+
message(FATAL_ERROR "This test requires the BabbleSim simulator. Please set\
7+
the environment variable BSIM_COMPONENTS_PATH to point to its components \
8+
folder. More information can be found in\
9+
https://babblesim.github.io/folder_structure_and_env.html")
10+
endif()
11+
12+
find_package(Zephyr HINTS $ENV{ZEPHYR_BASE})
13+
project(bsim_test_gatt_settings)
14+
15+
target_sources(app PRIVATE
16+
src/server.c
17+
src/client.c
18+
src/utils.c
19+
src/gatt_utils.c
20+
src/settings.c
21+
src/main.c
22+
)
23+
24+
zephyr_include_directories(
25+
$ENV{BSIM_COMPONENTS_PATH}/libUtilv1/src/
26+
$ENV{BSIM_COMPONENTS_PATH}/libPhyComv1/src/
27+
)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
CONFIG_BT=y
2+
CONFIG_BT_PERIPHERAL=y
3+
CONFIG_BT_CENTRAL=y
4+
CONFIG_BT_DEVICE_NAME="GATT settings"
5+
6+
CONFIG_LOG=y
7+
CONFIG_ASSERT=y
8+
CONFIG_BT_TESTING=y
9+
10+
CONFIG_BT_AUTO_PHY_UPDATE=n
11+
CONFIG_BT_AUTO_DATA_LEN_UPDATE=n
12+
CONFIG_BT_GAP_AUTO_UPDATE_CONN_PARAMS=n
13+
14+
CONFIG_BT_SMP=y
15+
CONFIG_BT_GATT_CLIENT=y
16+
CONFIG_BT_GATT_DYNAMIC_DB=y
17+
CONFIG_BT_GATT_CACHING=y
18+
19+
CONFIG_SETTINGS=y
20+
CONFIG_SETTINGS_CUSTOM=y
21+
CONFIG_BT_SETTINGS=y
22+
23+
CONFIG_BT_GATT_AUTO_DISCOVER_CCC=y
24+
CONFIG_BT_GATT_AUTO_RESUBSCRIBE=n
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
/*
2+
* Copyright (c) 2023 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include "utils.h"
8+
#include "gatt_utils.h"
9+
10+
#include <zephyr/bluetooth/addr.h>
11+
#include <zephyr/bluetooth/bluetooth.h>
12+
#include <zephyr/bluetooth/conn.h>
13+
#include <zephyr/settings/settings.h>
14+
#include <zephyr/toolchain/gcc.h>
15+
16+
#include <stdint.h>
17+
#include <string.h>
18+
19+
void client_round_0(void)
20+
{
21+
struct bt_conn *conn;
22+
23+
printk("start round 0...........\n");
24+
25+
conn = connect_as_peripheral();
26+
printk("connected: conn %p\n", conn);
27+
wait_bonded();
28+
29+
gatt_discover();
30+
activate_robust_caching();
31+
/* subscribe to the SC indication, so we don't have to ATT read to
32+
* become change-aware.
33+
*/
34+
gatt_subscribe_to_service_changed(true);
35+
36+
read_test_char(true);
37+
38+
disconnect(conn);
39+
}
40+
41+
void client_round_1(void)
42+
{
43+
struct bt_conn *conn;
44+
45+
printk("start round 1...........\n");
46+
47+
conn = connect_as_peripheral();
48+
printk("connected: conn %p\n", conn);
49+
wait_secured();
50+
51+
/* server should remember we are change-aware */
52+
read_test_char(true);
53+
54+
disconnect(conn);
55+
}
56+
57+
void client_round_2(void)
58+
{
59+
struct bt_conn *conn;
60+
61+
printk("start round 2...........\n");
62+
63+
conn = connect_as_peripheral();
64+
printk("connected: conn %p\n", conn);
65+
wait_secured();
66+
67+
/* We are change-unaware. wait until the Service Changed indication is
68+
* received, that should then make us change-aware.
69+
*/
70+
wait_for_sc_indication();
71+
read_test_char(true);
72+
73+
/* We sleep just enough so that the server's `delayed store` work item
74+
* is executed. We still trigger a disconnect, even though the server
75+
* device will be unresponsive for this round.
76+
*/
77+
k_sleep(K_MSEC(CONFIG_BT_SETTINGS_DELAYED_STORE_MS));
78+
79+
disconnect(conn);
80+
}
81+
82+
void client_round_3(void)
83+
{
84+
struct bt_conn *conn;
85+
86+
printk("start round 3...........\n");
87+
88+
conn = connect_as_peripheral();
89+
printk("connected: conn %p\n", conn);
90+
wait_secured();
91+
92+
/* server should remember we are change-aware */
93+
read_test_char(true);
94+
95+
/* Unsubscribe from the SC indication.
96+
*
97+
* In the next round, we will be change-unaware, so the first ATT read
98+
* will fail, but the second one will succeed and we will be marked as
99+
* change-aware again.
100+
*/
101+
gatt_subscribe_to_service_changed(false);
102+
103+
disconnect(conn);
104+
}
105+
106+
void client_round_4(void)
107+
{
108+
struct bt_conn *conn;
109+
110+
printk("start round 4...........\n");
111+
112+
conn = connect_as_peripheral();
113+
printk("connected: conn %p\n", conn);
114+
wait_secured();
115+
116+
/* GATT DB has changed again.
117+
* Before disc: svc1
118+
* After disc: svc1 + svc2
119+
* At boot: svc1
120+
* Expect a failure on the first read of the same GATT handle.
121+
*/
122+
read_test_char(false);
123+
read_test_char(true);
124+
125+
disconnect(conn);
126+
}
127+
128+
void client_round_5(void)
129+
{
130+
printk("start round 5...........\n");
131+
printk("don't need to do anything, central will "
132+
"not connect to us\n");
133+
}
134+
135+
void client_round_6(void)
136+
{
137+
struct bt_conn *conn;
138+
139+
printk("start round 6...........\n");
140+
conn = connect_as_peripheral();
141+
printk("connected: conn %p\n", conn);
142+
wait_secured();
143+
144+
/* GATT DB has changed again.
145+
* Expect a failure on the first read of the same GATT handle.
146+
*/
147+
read_test_char(false);
148+
read_test_char(true);
149+
150+
disconnect(conn);
151+
}
152+
153+
void client_procedure(void)
154+
{
155+
bt_enable(NULL);
156+
settings_load();
157+
158+
client_round_0();
159+
client_round_1();
160+
client_round_2();
161+
client_round_3();
162+
client_round_4();
163+
client_round_5();
164+
client_round_6();
165+
166+
PASS("PASS\n");
167+
}

0 commit comments

Comments
 (0)