Skip to content

Commit 18c2ea2

Browse files
theob-prokartben
authored andcommitted
Tests: Bluetooth: Check GATT settings cleanup
Check that the GATT settings added when bonding to a peer and subscribing to a CCC are correctly deleted when calling `bt_unpair`. Testing hooks have been added in the Bluetooth settings abstraction layer to know which key are being added/deleted. Signed-off-by: Théo Battrel <theo.battrel@nordicsemi.no>
1 parent 65334e0 commit 18c2ea2

File tree

12 files changed

+693
-0
lines changed

12 files changed

+693
-0
lines changed

subsys/bluetooth/host/settings.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,18 @@ int bt_settings_init(void)
298298
return 0;
299299
}
300300

301+
__weak void bt_testing_settings_store_hook(const char *key, const void *value, size_t val_len)
302+
{
303+
ARG_UNUSED(key);
304+
ARG_UNUSED(value);
305+
ARG_UNUSED(val_len);
306+
}
307+
308+
__weak void bt_testing_settings_delete_hook(const char *key)
309+
{
310+
ARG_UNUSED(key);
311+
}
312+
301313
int bt_settings_store(const char *key, uint8_t id, const bt_addr_le_t *addr, const void *value,
302314
size_t val_len)
303315
{
@@ -318,6 +330,10 @@ int bt_settings_store(const char *key, uint8_t id, const bt_addr_le_t *addr, con
318330
}
319331
}
320332

333+
if (IS_ENABLED(CONFIG_BT_TESTING)) {
334+
bt_testing_settings_store_hook(key_str, value, val_len);
335+
}
336+
321337
return settings_save_one(key_str, value, val_len);
322338
}
323339

@@ -340,6 +356,10 @@ int bt_settings_delete(const char *key, uint8_t id, const bt_addr_le_t *addr)
340356
}
341357
}
342358

359+
if (IS_ENABLED(CONFIG_BT_TESTING)) {
360+
bt_testing_settings_delete_hook(key_str);
361+
}
362+
343363
return settings_delete(key_str);
344364
}
345365

subsys/bluetooth/host/settings.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7+
#include <stddef.h>
8+
#include <stdint.h>
9+
10+
#include <zephyr/bluetooth/addr.h>
11+
712
#include <zephyr/settings/settings.h>
813

914
/* Max settings key length (with all components) */
@@ -21,6 +26,9 @@ int bt_settings_store(const char *key, uint8_t id, const bt_addr_le_t *addr, con
2126
size_t val_len);
2227
int bt_settings_delete(const char *key, uint8_t id, const bt_addr_le_t *addr);
2328

29+
void bt_testing_settings_store_hook(const char *key, const void *value, size_t val_len);
30+
void bt_testing_settings_delete_hook(const char *key);
31+
2432
/* Helpers for keys containing a bdaddr */
2533
void bt_settings_encode_key(char *path, size_t path_size, const char *subsys,
2634
const bt_addr_le_t *addr, const char *key);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
7+
project(settings_clear)
8+
9+
add_subdirectory(${ZEPHYR_BASE}/tests/bluetooth/common/testlib testlib)
10+
target_link_libraries(app PRIVATE testlib)
11+
12+
add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit)
13+
target_link_libraries(app PRIVATE babblekit)
14+
15+
zephyr_include_directories(
16+
${BSIM_COMPONENTS_PATH}/libUtilv1/src/
17+
${BSIM_COMPONENTS_PATH}/libPhyComv1/src/
18+
)
19+
20+
target_sources(app PRIVATE
21+
src/main.c
22+
src/bt_settings_hook.c
23+
src/server.c
24+
src/client.c
25+
)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
CONFIG_BT_TESTING=y
2+
3+
CONFIG_BT=y
4+
CONFIG_BT_CENTRAL=y
5+
CONFIG_BT_PERIPHERAL=y
6+
CONFIG_BT_DEVICE_NAME="GATT settings clear"
7+
8+
# Dependency of testlib/adv and testlib/scan.
9+
CONFIG_BT_EXT_ADV=y
10+
11+
CONFIG_BT_AUTO_PHY_UPDATE=n
12+
CONFIG_BT_AUTO_DATA_LEN_UPDATE=n
13+
CONFIG_BT_GAP_AUTO_UPDATE_CONN_PARAMS=n
14+
15+
CONFIG_BT_GATT_AUTO_DISCOVER_CCC=y
16+
CONFIG_BT_GATT_AUTO_RESUBSCRIBE=n
17+
18+
CONFIG_BT_PRIVACY=n
19+
20+
CONFIG_BT_SMP=y
21+
CONFIG_BT_GATT_CLIENT=y
22+
CONFIG_BT_GATT_DYNAMIC_DB=y
23+
CONFIG_BT_GATT_CACHING=y
24+
25+
CONFIG_SETTINGS=y
26+
CONFIG_BT_SETTINGS=y
27+
CONFIG_FLASH=y
28+
CONFIG_NVS=y
29+
CONFIG_FLASH_MAP=y
30+
CONFIG_SETTINGS_NVS=y
31+
32+
CONFIG_LOG=y
33+
CONFIG_ASSERT=y
34+
35+
CONFIG_THREAD_NAME=y
36+
CONFIG_LOG_THREAD_ID_PREFIX=y
37+
38+
CONFIG_ARCH_POSIX_TRAP_ON_FATAL=y
39+
40+
CONFIG_HEAP_MEM_POOL_SIZE=2048
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/*
2+
* Copyright (c) 2024 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <stddef.h>
8+
#include <string.h>
9+
10+
#include <zephyr/kernel.h>
11+
12+
#include <zephyr/logging/log.h>
13+
14+
#include "babblekit/testcase.h"
15+
16+
LOG_MODULE_REGISTER(bt_settings_hook, LOG_LEVEL_DBG);
17+
18+
struct settings_list_node {
19+
sys_snode_t node;
20+
char *key;
21+
};
22+
23+
static K_MUTEX_DEFINE(settings_list_mutex);
24+
static atomic_t settings_record_enabled = ATOMIC_INIT(0);
25+
26+
static sys_slist_t settings_list = SYS_SLIST_STATIC_INIT(settings_list);
27+
28+
static void add_key(const char *key)
29+
{
30+
struct settings_list_node *new_node;
31+
size_t key_size = strlen(key);
32+
bool key_already_in = false;
33+
34+
struct settings_list_node *loop_node;
35+
36+
if (settings_record_enabled == 1) {
37+
k_mutex_lock(&settings_list_mutex, K_FOREVER);
38+
39+
SYS_SLIST_FOR_EACH_CONTAINER(&settings_list, loop_node, node) {
40+
if (strcmp(loop_node->key, key) == 0) {
41+
key_already_in = true;
42+
}
43+
}
44+
45+
if (!key_already_in) {
46+
new_node = k_malloc(sizeof(struct settings_list_node));
47+
TEST_ASSERT(new_node != NULL, "Failed to malloc new_node");
48+
49+
new_node->key = k_malloc(sizeof(char) * key_size);
50+
TEST_ASSERT(new_node->key != NULL, "Failed to malloc new_node->key");
51+
52+
memcpy(new_node->key, key, sizeof(char) * key_size);
53+
54+
sys_slist_append(&settings_list, &new_node->node);
55+
}
56+
57+
k_mutex_unlock(&settings_list_mutex);
58+
}
59+
}
60+
61+
static void remove_key(const char *key)
62+
{
63+
struct settings_list_node *loop_node, *prev_loop_node;
64+
65+
prev_loop_node = NULL;
66+
67+
if (settings_record_enabled == 1) {
68+
k_mutex_lock(&settings_list_mutex, K_FOREVER);
69+
70+
SYS_SLIST_FOR_EACH_CONTAINER(&settings_list, loop_node, node) {
71+
if (strcmp(loop_node->key, key) == 0) {
72+
break;
73+
}
74+
75+
prev_loop_node = loop_node;
76+
}
77+
78+
if (loop_node != NULL) {
79+
sys_slist_remove(&settings_list, &prev_loop_node->node, &loop_node->node);
80+
81+
k_free(loop_node->key);
82+
k_free(loop_node);
83+
}
84+
85+
k_mutex_unlock(&settings_list_mutex);
86+
}
87+
}
88+
89+
void start_settings_record(void)
90+
{
91+
atomic_set(&settings_record_enabled, 1);
92+
}
93+
94+
void stop_settings_record(void)
95+
{
96+
atomic_set(&settings_record_enabled, 0);
97+
}
98+
99+
void settings_list_cleanup(void)
100+
{
101+
struct settings_list_node *loop_node, *next_loop_node, *prev_loop_node;
102+
103+
prev_loop_node = NULL;
104+
105+
k_mutex_lock(&settings_list_mutex, K_FOREVER);
106+
107+
SYS_SLIST_FOR_EACH_CONTAINER_SAFE(&settings_list, loop_node, next_loop_node, node) {
108+
sys_slist_remove(&settings_list, &prev_loop_node->node, &loop_node->node);
109+
110+
k_free(loop_node->key);
111+
k_free(loop_node);
112+
}
113+
114+
k_mutex_unlock(&settings_list_mutex);
115+
}
116+
117+
int get_settings_list_size(void)
118+
{
119+
struct settings_list_node *loop_node;
120+
int number_of_settings = 0;
121+
122+
SYS_SLIST_FOR_EACH_CONTAINER(&settings_list, loop_node, node) {
123+
LOG_ERR("Setting registered: %s", loop_node->key);
124+
number_of_settings += 1;
125+
}
126+
127+
return number_of_settings;
128+
}
129+
130+
void bt_testing_settings_store_hook(const char *key, const void *value, size_t val_len)
131+
{
132+
LOG_DBG("Store: %s", key);
133+
LOG_HEXDUMP_DBG(value, val_len, "Data:");
134+
135+
add_key(key);
136+
}
137+
138+
void bt_testing_settings_delete_hook(const char *key)
139+
{
140+
LOG_DBG("Delete: %s", key);
141+
142+
remove_key(key);
143+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* Copyright (c) 2024 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef ZEPHYR_TESTS_BSIM_BLUETOOTH_HOST_GATT_SETTINGS_CLEAR_SRC_BT_SETTINGS_HOOK_H_
8+
#define ZEPHYR_TESTS_BSIM_BLUETOOTH_HOST_GATT_SETTINGS_CLEAR_SRC_BT_SETTINGS_HOOK_H_
9+
10+
void start_settings_record(void);
11+
void stop_settings_record(void);
12+
void settings_list_cleanup(void);
13+
int get_settings_list_size(void);
14+
15+
#endif /* ZEPHYR_TESTS_BSIM_BLUETOOTH_HOST_GATT_SETTINGS_CLEAR_SRC_BT_SETTINGS_HOOK_H_ */

0 commit comments

Comments
 (0)