Skip to content

Commit cc06a9b

Browse files
theob-prom-alperen-sener
authored andcommitted
[nrf fromtree] Tests: Bluetooth: Add 'device_name' GATT test
The goal of this test is to ensure that setting the device name by writing to the GAP 'Device Name' characteristic work as expected. Signed-off-by: Théo Battrel <[email protected]> (cherry picked from commit 1612220)
1 parent fb79cbc commit cc06a9b

File tree

8 files changed

+459
-0
lines changed

8 files changed

+459
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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(device_name)
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/server.c
23+
src/client.c
24+
)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
CONFIG_BT_TESTING=y
2+
3+
CONFIG_BT=y
4+
CONFIG_BT_CENTRAL=y
5+
CONFIG_BT_PERIPHERAL=y
6+
7+
CONFIG_BT_DEVICE_NAME_DYNAMIC=y
8+
CONFIG_BT_DEVICE_NAME_GATT_WRITABLE=y
9+
10+
# Dependency of testlib/adv and testlib/scan.
11+
CONFIG_BT_EXT_ADV=y
12+
13+
CONFIG_BT_AUTO_PHY_UPDATE=n
14+
CONFIG_BT_GATT_AUTO_UPDATE_MTU=n
15+
CONFIG_BT_AUTO_DATA_LEN_UPDATE=n
16+
CONFIG_BT_GAP_AUTO_UPDATE_CONN_PARAMS=n
17+
18+
CONFIG_BT_GATT_AUTO_DISCOVER_CCC=y
19+
CONFIG_BT_GATT_AUTO_RESUBSCRIBE=n
20+
21+
CONFIG_BT_SMP=y
22+
CONFIG_BT_GATT_CLIENT=y
23+
CONFIG_BT_GATT_DYNAMIC_DB=y
24+
CONFIG_BT_GATT_CACHING=y
25+
26+
CONFIG_LOG=y
27+
CONFIG_ASSERT=y
28+
29+
CONFIG_THREAD_NAME=y
30+
CONFIG_LOG_THREAD_ID_PREFIX=y
31+
32+
CONFIG_ARCH_POSIX_TRAP_ON_FATAL=y
33+
34+
CONFIG_BT_L2CAP_TX_MTU=512
35+
CONFIG_BT_BUF_ACL_RX_SIZE=518
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*
2+
* Copyright (c) 2025 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/kernel.h>
8+
9+
#include <zephyr/bluetooth/bluetooth.h>
10+
#include <zephyr/bluetooth/conn.h>
11+
#include <zephyr/bluetooth/att.h>
12+
#include <zephyr/bluetooth/gatt.h>
13+
14+
#include <zephyr/settings/settings.h>
15+
16+
#include <zephyr/logging/log.h>
17+
18+
#include "testlib/adv.h"
19+
#include "testlib/att.h"
20+
#include "testlib/att_read.h"
21+
#include "testlib/att_write.h"
22+
#include "testlib/conn.h"
23+
24+
#include "babblekit/flags.h"
25+
#include "babblekit/sync.h"
26+
#include "babblekit/testcase.h"
27+
28+
#include "common.h"
29+
30+
LOG_MODULE_REGISTER(client, LOG_LEVEL_DBG);
31+
32+
static DEFINE_FLAG(client_security_changed_flag);
33+
34+
static struct bt_conn_cb client_conn_cb;
35+
36+
static void security_changed(struct bt_conn *conn, bt_security_t level, enum bt_security_err err)
37+
{
38+
char addr_str[BT_ADDR_LE_STR_LEN];
39+
40+
bt_addr_le_to_str(bt_conn_get_dst(conn), addr_str, sizeof(addr_str));
41+
42+
TEST_ASSERT(err == 0, "Security update failed: %s level %u err %d", addr_str, level, err);
43+
44+
LOG_DBG("Security changed: %s level %u", addr_str, level);
45+
SET_FLAG(client_security_changed_flag);
46+
}
47+
48+
static void init_client_conn_callbacks(void)
49+
{
50+
int err;
51+
52+
client_conn_cb.connected = NULL;
53+
client_conn_cb.disconnected = NULL;
54+
client_conn_cb.security_changed = security_changed;
55+
56+
err = bt_conn_cb_register(&client_conn_cb);
57+
TEST_ASSERT(err == 0, "Failed to set client conn callbacks (err %d)", err);
58+
}
59+
60+
void find_characteristic(struct bt_conn *conn, const struct bt_uuid *svc,
61+
const struct bt_uuid *chrc, uint16_t *chrc_value_handle)
62+
{
63+
int err;
64+
uint16_t svc_handle;
65+
uint16_t svc_end_handle;
66+
uint16_t chrc_end_handle;
67+
68+
err = bt_testlib_gatt_discover_primary(&svc_handle, &svc_end_handle, conn, svc,
69+
BT_ATT_FIRST_ATTRIBUTE_HANDLE,
70+
BT_ATT_LAST_ATTRIBUTE_HANDLE);
71+
TEST_ASSERT(err == 0, "Failed to discover service: %d", err);
72+
73+
LOG_DBG("svc_handle: %u, svc_end_handle: %u", svc_handle, svc_end_handle);
74+
75+
err = bt_testlib_gatt_discover_characteristic(chrc_value_handle, &chrc_end_handle, NULL,
76+
conn, chrc, (svc_handle + 1), svc_end_handle);
77+
TEST_ASSERT(err == 0, "Failed to get value handle: %d", err);
78+
79+
LOG_DBG("chrc_value_handle: %u, chrc_end_handle: %u", *chrc_value_handle, chrc_end_handle);
80+
}
81+
82+
void client_procedure(void)
83+
{
84+
int err;
85+
struct bt_conn *conn;
86+
uint16_t handle;
87+
88+
char server_new_name[CONFIG_BT_DEVICE_NAME_MAX];
89+
90+
NET_BUF_SIMPLE_DEFINE(attr_value_buf, BT_ATT_MAX_ATTRIBUTE_LEN);
91+
92+
generate_name(server_new_name, CONFIG_BT_DEVICE_NAME_MAX);
93+
94+
TEST_START("client");
95+
96+
bk_sync_init();
97+
98+
err = bt_enable(NULL);
99+
TEST_ASSERT(err == 0, "Cannot enable Bluetooth (err %d)", err);
100+
101+
LOG_DBG("Bluetooth initialized");
102+
103+
init_client_conn_callbacks();
104+
105+
err = bt_testlib_adv_conn(&conn, BT_ID_DEFAULT, ADVERTISER_NAME);
106+
TEST_ASSERT(err == 0, "Failed to start connectable advertising (err %d)", err);
107+
108+
err = bt_testlib_att_exchange_mtu(conn);
109+
TEST_ASSERT(err == 0, "Failed to update MTU (err %d)", err);
110+
111+
find_characteristic(conn, BT_UUID_GAP, BT_UUID_GAP_DEVICE_NAME, &handle);
112+
113+
err = bt_testlib_att_read_by_handle_sync(&attr_value_buf, NULL, NULL, conn,
114+
BT_ATT_CHAN_OPT_UNENHANCED_ONLY, handle, 0);
115+
TEST_ASSERT(err == 0, "Failed to read characteristic (err %d)", err);
116+
117+
LOG_DBG("Device Name of the server: %.*s", attr_value_buf.len, attr_value_buf.data);
118+
119+
err = bt_testlib_att_write(conn, BT_ATT_CHAN_OPT_UNENHANCED_ONLY, handle, server_new_name,
120+
sizeof(server_new_name));
121+
TEST_ASSERT(err == BT_ATT_ERR_SUCCESS, "Got ATT error: %d", err);
122+
123+
bk_sync_send();
124+
125+
TEST_PASS("client");
126+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright (c) 2025 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef ZEPHYR_TESTS_BSIM_BLUETOOTH_HOST_GATT_DEVICE_NAME_SRC_COMMON_H_
8+
#define ZEPHYR_TESTS_BSIM_BLUETOOTH_HOST_GATT_DEVICE_NAME_SRC_COMMON_H_
9+
10+
#include <stddef.h>
11+
#include <stdint.h>
12+
13+
#include <zephyr/bluetooth/bluetooth.h>
14+
15+
#define ADVERTISER_NAME "Advertiser Pro II"
16+
17+
static void generate_name(uint8_t *name, size_t length)
18+
{
19+
for (size_t i = 0; i < length; i++) {
20+
name[i] = (i % 26) + 97;
21+
}
22+
}
23+
24+
#endif /* ZEPHYR_TESTS_BSIM_BLUETOOTH_HOST_DEVICE_NAME_CLEAR_SRC_COMMON_H_ */
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright (c) 2025 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/kernel.h>
8+
9+
#include "bs_tracing.h"
10+
#include "bstests.h"
11+
#include "babblekit/testcase.h"
12+
#include "testlib/log_utils.h"
13+
14+
extern void server_procedure(void);
15+
extern void client_procedure(void);
16+
extern enum bst_result_t bst_result;
17+
18+
static void test_end_cb(void)
19+
{
20+
if (bst_result != Passed) {
21+
TEST_PRINT("Test has not passed.");
22+
}
23+
}
24+
25+
static const struct bst_test_instance entrypoints[] = {
26+
{
27+
.test_id = "server",
28+
.test_delete_f = test_end_cb,
29+
.test_main_f = server_procedure,
30+
},
31+
{
32+
.test_id = "client",
33+
.test_delete_f = test_end_cb,
34+
.test_main_f = client_procedure,
35+
},
36+
BSTEST_END_MARKER,
37+
};
38+
39+
static struct bst_test_list *install(struct bst_test_list *tests)
40+
{
41+
return bst_add_tests(tests, entrypoints);
42+
};
43+
44+
bst_test_install_t test_installers[] = {install, NULL};
45+
46+
int main(void)
47+
{
48+
bst_main();
49+
50+
return 0;
51+
}

0 commit comments

Comments
 (0)