Skip to content

Commit dd6929e

Browse files
committed
lib: bluetooth: db_disovery: Add unit tests
Signed-off-by: Asil Zogby <asilz@stud.ntnu.no>
1 parent b79ab14 commit dd6929e

File tree

4 files changed

+279
-0
lines changed

4 files changed

+279
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
11+
project(unit_test_ble_db_discovery)
12+
13+
set(SOFTDEVICE_VARIANT "s145")
14+
set(SOFTDEVICE_INCLUDE_DIR "${ZEPHYR_NRF_BM_MODULE_DIR}/components/softdevice/${SOFTDEVICE_VARIANT}/${SOFTDEVICE_VARIANT}_API/include")
15+
16+
cmock_handle(${SOFTDEVICE_INCLUDE_DIR}/ble.h)
17+
cmock_handle(${SOFTDEVICE_INCLUDE_DIR}/ble_gatts.h
18+
WORD_EXCLUDE
19+
"__STATIC_INLINE")
20+
cmock_handle(${SOFTDEVICE_INCLUDE_DIR}/ble_gattc.h
21+
WORD_EXCLUDE
22+
"__STATIC_INLINE")
23+
cmock_handle(${SOFTDEVICE_INCLUDE_DIR}/ble_gap.h)
24+
25+
add_compile_definitions(
26+
SVCALL_AS_NORMAL_FUNCTION=1
27+
NRF54L15_XXAA)
28+
29+
target_compile_definitions( app PRIVATE
30+
CONFIG_NRF_SDH_BLE_TOTAL_LINK_COUNT=1
31+
CONFIG_BLE_DB_DISCOVERY_MAX_SRV=6
32+
CONFIG_BLE_GQ_QUEUE_SIZE=8
33+
CONFIG_BLE_GQ_HEAP_SIZE=256
34+
CONFIG_BLE_GQ_MAX_CONNECTIONS=1
35+
CONFIG_SRV_DISC_START_HANDLE=1
36+
CONFIG_BLE_GATT_DB_MAX_CHARS=6
37+
)
38+
39+
# Generate and add test file
40+
test_runner_generate(src/unity_test.c)
41+
target_sources(app PRIVATE src/unity_test.c)
42+
43+
target_include_directories(app PRIVATE ${ZEPHYR_NRF_BM_MODULE_DIR}/include)
44+
target_include_directories(app PRIVATE ${SOFTDEVICE_INCLUDE_DIR})
45+
target_include_directories(app PRIVATE ${ZEPHYR_HAL_NORDIC_MODULE_DIR}/nrfx/mdk)
46+
target_include_directories(app PRIVATE ${ZEPHYR_CMSIS_MODULE_DIR}/CMSIS/Core/Include)
47+
48+
target_sources(app
49+
PRIVATE
50+
${ZEPHYR_NRF_BM_MODULE_DIR}/lib/bluetooth/ble_db_discovery/ble_db_discovery.c
51+
)
52+
target_sources(app
53+
PRIVATE
54+
${ZEPHYR_NRF_BM_MODULE_DIR}/lib/bluetooth/ble_gq/gatt_queue.c
55+
)
56+
57+
zephyr_include_directories(${ZEPHYR_NRF_BM_MODULE_DIR}/include)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#
2+
# Copyright (c) 2025 Nordic Semiconductor ASA
3+
#
4+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
#
6+
CONFIG_UNITY=y
Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
/*
2+
* Copyright (c) 2025 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
*/
6+
7+
#include <nrf_error.h>
8+
#include <unity.h>
9+
#include <stdint.h>
10+
#include <string.h>
11+
#include <stddef.h>
12+
#include <bm/bluetooth/ble_db_discovery.h>
13+
#include <bm/bluetooth/services/uuid.h>
14+
#include <bm/bluetooth/ble_gq.h>
15+
16+
#include "cmock_ble.h"
17+
#include "cmock_ble_gap.h"
18+
#include "cmock_ble_gattc.h"
19+
20+
BLE_GQ_DEF(ble_gatt_queue);
21+
22+
static void db_discovery_evt_handler(struct ble_db_discovery *db_discovery,
23+
struct ble_db_discovery_evt *adv)
24+
{
25+
}
26+
27+
void test_ble_adv_init_error_null(void)
28+
{
29+
struct ble_db_discovery db_discovery = {0};
30+
struct ble_db_discovery_config config = {
31+
.gatt_queue = &ble_gatt_queue,
32+
.evt_handler = db_discovery_evt_handler,
33+
};
34+
int ret;
35+
36+
ret = ble_db_discovery_init(NULL, &config);
37+
TEST_ASSERT_EQUAL(NRF_ERROR_NULL, ret);
38+
ret = ble_db_discovery_init(&db_discovery, NULL);
39+
TEST_ASSERT_EQUAL(NRF_ERROR_NULL, ret);
40+
config.evt_handler = NULL;
41+
ret = ble_db_discovery_init(&db_discovery, &config);
42+
TEST_ASSERT_EQUAL(NRF_ERROR_NULL, ret);
43+
}
44+
45+
void test_ble_db_discovery_init(void)
46+
{
47+
int ret;
48+
49+
struct ble_db_discovery db_discovery = {0};
50+
struct ble_db_discovery_config config = {
51+
.gatt_queue = &ble_gatt_queue,
52+
.evt_handler = db_discovery_evt_handler,
53+
};
54+
55+
ret = ble_db_discovery_init(&db_discovery, &config);
56+
TEST_ASSERT_EQUAL(NRF_SUCCESS, ret);
57+
TEST_ASSERT_EQUAL(0, db_discovery.num_of_handlers_reg);
58+
TEST_ASSERT_NOT_EQUAL(NULL, db_discovery.evt_handler);
59+
TEST_ASSERT_NOT_EQUAL(NULL, db_discovery.gatt_queue);
60+
}
61+
62+
void test_ble_db_discovery_evt_register_null(void)
63+
{
64+
uint32_t ret;
65+
66+
struct ble_db_discovery db_discovery = {0};
67+
struct ble_db_discovery_config config = {
68+
.gatt_queue = &ble_gatt_queue,
69+
.evt_handler = db_discovery_evt_handler,
70+
};
71+
72+
ble_uuid_t hrs_uuid = {.type = BLE_UUID_TYPE_BLE, .uuid = BLE_UUID_HEART_RATE_SERVICE};
73+
74+
ret = ble_db_discovery_init(&db_discovery, &config);
75+
TEST_ASSERT_EQUAL(NRF_SUCCESS, ret);
76+
77+
ret = ble_db_discovery_evt_register(&db_discovery, NULL);
78+
TEST_ASSERT_EQUAL(NRF_ERROR_NULL, ret);
79+
ret = ble_db_discovery_evt_register(NULL, &hrs_uuid);
80+
TEST_ASSERT_EQUAL(NRF_ERROR_NULL, ret);
81+
}
82+
83+
void test_ble_db_discovery_evt_register_invalid_state(void)
84+
{
85+
struct ble_db_discovery db_discovery = {0};
86+
87+
ble_uuid_t hrs_uuid = {.type = BLE_UUID_TYPE_BLE, .uuid = BLE_UUID_HEART_RATE_SERVICE};
88+
89+
TEST_ASSERT_EQUAL(NRF_ERROR_INVALID_STATE,
90+
ble_db_discovery_evt_register(&db_discovery, &hrs_uuid));
91+
}
92+
93+
void test_ble_db_discovery_evt_register_no_mem(void)
94+
{
95+
struct ble_db_discovery db_discovery = {0};
96+
struct ble_db_discovery_config config = {
97+
.gatt_queue = &ble_gatt_queue,
98+
.evt_handler = db_discovery_evt_handler,
99+
};
100+
101+
TEST_ASSERT_EQUAL(NRF_SUCCESS, ble_db_discovery_init(&db_discovery, &config));
102+
103+
ble_uuid_t hrs_uuid = {.type = BLE_UUID_TYPE_BLE, .uuid = BLE_UUID_IMMEDIATE_ALERT_SERVICE};
104+
105+
for (size_t i = 0; i < CONFIG_BLE_DB_DISCOVERY_MAX_SRV; ++i) {
106+
TEST_ASSERT_EQUAL(NRF_SUCCESS,
107+
ble_db_discovery_evt_register(&db_discovery, &hrs_uuid));
108+
hrs_uuid.uuid++;
109+
}
110+
TEST_ASSERT_EQUAL(CONFIG_BLE_DB_DISCOVERY_MAX_SRV, db_discovery.num_of_handlers_reg);
111+
TEST_ASSERT_EQUAL(NRF_ERROR_NO_MEM,
112+
ble_db_discovery_evt_register(&db_discovery, &hrs_uuid));
113+
}
114+
115+
void test_ble_db_discovery_evt_register(void)
116+
{
117+
struct ble_db_discovery db_discovery = {0};
118+
struct ble_db_discovery_config config = {
119+
.gatt_queue = &ble_gatt_queue,
120+
.evt_handler = db_discovery_evt_handler,
121+
};
122+
123+
TEST_ASSERT_EQUAL(NRF_SUCCESS, ble_db_discovery_init(&db_discovery, &config));
124+
125+
ble_uuid_t hrs_uuid = {.type = BLE_UUID_TYPE_BLE, .uuid = BLE_UUID_HEART_RATE_SERVICE};
126+
127+
TEST_ASSERT_EQUAL(NRF_SUCCESS, ble_db_discovery_evt_register(&db_discovery, &hrs_uuid));
128+
}
129+
130+
void test_ble_db_discovery_start_null(void)
131+
{
132+
struct ble_db_discovery db_discovery = {0};
133+
struct ble_db_discovery_config config = {
134+
.gatt_queue = &ble_gatt_queue,
135+
.evt_handler = db_discovery_evt_handler,
136+
};
137+
138+
TEST_ASSERT_EQUAL(NRF_SUCCESS, ble_db_discovery_init(&db_discovery, &config));
139+
140+
ble_uuid_t hrs_uuid = {.type = BLE_UUID_TYPE_BLE, .uuid = BLE_UUID_HEART_RATE_SERVICE};
141+
142+
TEST_ASSERT_EQUAL(NRF_SUCCESS, ble_db_discovery_evt_register(&db_discovery, &hrs_uuid));
143+
144+
TEST_ASSERT_EQUAL(NRF_ERROR_NULL, ble_db_discovery_start(NULL, 0));
145+
}
146+
147+
void test_ble_db_discovery_start_invalid_state(void)
148+
{
149+
struct ble_db_discovery db_discovery = {0};
150+
struct ble_db_discovery_config config = {
151+
.gatt_queue = &ble_gatt_queue,
152+
.evt_handler = db_discovery_evt_handler,
153+
};
154+
155+
TEST_ASSERT_EQUAL(NRF_ERROR_INVALID_STATE, ble_db_discovery_start(&db_discovery, 0));
156+
157+
TEST_ASSERT_EQUAL(NRF_SUCCESS, ble_db_discovery_init(&db_discovery, &config));
158+
159+
TEST_ASSERT_EQUAL(NRF_ERROR_INVALID_STATE, ble_db_discovery_start(&db_discovery, 0));
160+
}
161+
162+
void test_ble_db_discovery_start_busy(void)
163+
{
164+
struct ble_db_discovery db_discovery = {0};
165+
struct ble_db_discovery_config config = {
166+
.gatt_queue = &ble_gatt_queue,
167+
.evt_handler = db_discovery_evt_handler,
168+
};
169+
170+
TEST_ASSERT_EQUAL(NRF_SUCCESS, ble_db_discovery_init(&db_discovery, &config));
171+
172+
ble_uuid_t hrs_uuid = {.type = BLE_UUID_TYPE_BLE, .uuid = BLE_UUID_HEART_RATE_SERVICE};
173+
174+
TEST_ASSERT_EQUAL(NRF_SUCCESS, ble_db_discovery_evt_register(&db_discovery, &hrs_uuid));
175+
176+
__cmock_sd_ble_gattc_primary_services_discover_ExpectAnyArgsAndReturn(NRF_SUCCESS);
177+
TEST_ASSERT_EQUAL(NRF_SUCCESS, ble_db_discovery_start(&db_discovery, 0));
178+
179+
TEST_ASSERT_EQUAL(NRF_ERROR_BUSY, ble_db_discovery_start(&db_discovery, 0));
180+
}
181+
182+
void test_ble_db_discovery_start(void)
183+
{
184+
struct ble_db_discovery db_discovery = {0};
185+
struct ble_db_discovery_config config = {
186+
.gatt_queue = &ble_gatt_queue,
187+
.evt_handler = db_discovery_evt_handler,
188+
};
189+
190+
TEST_ASSERT_EQUAL(NRF_SUCCESS, ble_db_discovery_init(&db_discovery, &config));
191+
192+
ble_uuid_t hrs_uuid = {.type = BLE_UUID_TYPE_BLE, .uuid = BLE_UUID_HEART_RATE_SERVICE};
193+
194+
TEST_ASSERT_EQUAL(NRF_SUCCESS, ble_db_discovery_evt_register(&db_discovery, &hrs_uuid));
195+
196+
__cmock_sd_ble_gattc_primary_services_discover_ExpectAnyArgsAndReturn(NRF_SUCCESS);
197+
TEST_ASSERT_EQUAL(NRF_SUCCESS, ble_db_discovery_start(&db_discovery, 0));
198+
}
199+
200+
void setUp(void)
201+
{
202+
}
203+
void tearDown(void)
204+
{
205+
}
206+
207+
extern int unity_main(void);
208+
209+
int main(void)
210+
{
211+
return unity_main();
212+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
tests:
2+
lib.ble_db_discovery:
3+
platform_allow: native_sim
4+
tags: unittest

0 commit comments

Comments
 (0)