Skip to content

Commit 81962e6

Browse files
bt_rpc: Added unit test to check for gatt service registration cycle
check if gatt services are correctly clean after remove Signed-off-by: Robert Gałat <robert.galat@nordicsemi.no>
1 parent 722e2dc commit 81962e6

File tree

9 files changed

+538
-3
lines changed

9 files changed

+538
-3
lines changed

subsys/bluetooth/rpc/common/bt_rpc_gatt_common.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,14 @@ const struct bt_gatt_attr *bt_rpc_gatt_index_to_attr(uint32_t index)
8181
{
8282
uint32_t service_index = index >> ATTR_INDEX_POS;
8383
uint32_t attr_index = index & ATTR_INDEX_MASK;
84-
const struct bt_gatt_service *service = svc_cache.services[service_index];
84+
const struct bt_gatt_service *service;
85+
86+
if (service_index >= svc_cache.count) {
87+
return NULL;
88+
}
8589

86-
if ((service_index >= svc_cache.count) || (attr_index >= service->attr_count)) {
90+
service = svc_cache.services[service_index];
91+
if (!service || (attr_index >= service->attr_count)) {
8792
return NULL;
8893
}
8994

@@ -203,7 +208,7 @@ void bt_rpc_gatt_foreach_attr_type(uint16_t start_handle, uint16_t end_handle,
203208
}
204209

205210
for (size_t j = 0; j < svc->attr_count; j++) {
206-
struct bt_gatt_attr *attr = &svc->attrs[j];
211+
const struct bt_gatt_attr *attr = &svc->attrs[j];
207212

208213
handle = bt_gatt_attr_get_handle(attr);
209214

subsys/bluetooth/rpc/host/bt_rpc_gatt_host.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,7 @@ static void bt_rpc_gatt_service_unregister_rpc_handler(const struct nrf_rpc_grou
729729

730730
svc = bt_rpc_gatt_get_service_by_index(svc_index);
731731
if (!svc) {
732+
LOG_ERR("Service unregister: Invalid service index %u", svc_index);
732733
result = -EINVAL;
733734
}
734735

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#
2+
# Copyright (c) 2025 Nordic Semiconductor ASA
3+
#
4+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
#
6+
7+
cmake_minimum_required(VERSION 3.20.0)
8+
9+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
10+
project(bluetooth_rpc_gatt_service_test)
11+
12+
FILE(GLOB app_sources src/*.c)
13+
14+
# Remove log_stub.c from glob since we include it explicitly
15+
list(FILTER app_sources EXCLUDE REGEX ".*log_stub\\.c$")
16+
17+
# Add src directory for test_config.h
18+
# Note: We're using the real zephyr/bluetooth/gatt.h now, so we don't need
19+
# to override it with a stub
20+
target_include_directories(app PRIVATE
21+
${CMAKE_CURRENT_SOURCE_DIR}/src
22+
${ZEPHYR_NRF_MODULE_DIR}/subsys/bluetooth/rpc/common
23+
)
24+
25+
# Include the source file directly for unit testing
26+
target_sources(app PRIVATE
27+
${app_sources}
28+
${CMAKE_CURRENT_SOURCE_DIR}/src/log_stub.c
29+
${ZEPHYR_NRF_MODULE_DIR}/subsys/bluetooth/rpc/common/bt_rpc_gatt_common.c
30+
)
31+
32+
# Include test config header before compilation to define config values
33+
# that aren't available via Kconfig without CONFIG_BT_RPC
34+
target_compile_options(app PRIVATE
35+
-include ${CMAKE_CURRENT_SOURCE_DIR}/src/test_config.h
36+
)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
CONFIG_ZTEST=y
2+
3+
# Minimal BT configuration - just enough to use the real gatt.h header
4+
# We don't need full BT stack functionality, just the type definitions
5+
CONFIG_BT=y
6+
7+
# Minimal configuration for testing service cache only
8+
# Note: CONFIG_BT_RPC_GATT_SRV_MAX and CONFIG_BT_RPC_LOG_LEVEL are defined
9+
# via compile definitions in CMakeLists.txt since they require CONFIG_BT_RPC
10+
11+
# Logging (minimal)
12+
CONFIG_LOG=y
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* Copyright (c) 2025 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
*
6+
* Stub implementation for logging module to avoid linker errors
7+
* when testing without full BT RPC stack
8+
*/
9+
10+
#include <zephyr/logging/log.h>
11+
12+
/* Stub the log constant structure that's normally generated */
13+
LOG_MODULE_REGISTER(BT_RPC, 4);

0 commit comments

Comments
 (0)