Skip to content

Commit 060af68

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

File tree

5 files changed

+366
-0
lines changed

5 files changed

+366
-0
lines changed

CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
# Tests
9696
/tests/lib/bluetooth/ble_adv/ @nrfconnect/ncs-bm-test
9797
/tests/lib/bluetooth/ble_conn_state/ @nrfconnect/ncs-bm
98+
/tests/lib/bluetooth/ble_db_discovery/ @nrfconnect/ncs-bm
9899
/tests/lib/bluetooth/ble_qwr/ @nrfconnect/ncs-bm
99100
/tests/lib/bluetooth/ble_racp/ @nrfconnect/ncs-bm
100101
/tests/lib/bluetooth/ble_radio_notif/ @nrfconnect/ncs-bm
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
cmock_handle(${ZEPHYR_NRF_BM_MODULE_DIR}/include/bm/bluetooth/ble_gq.h)
25+
26+
add_compile_definitions(
27+
SVCALL_AS_NORMAL_FUNCTION=1
28+
NRF54L15_XXAA)
29+
30+
target_compile_definitions( app PRIVATE
31+
CONFIG_NRF_SDH_BLE_TOTAL_LINK_COUNT=1
32+
CONFIG_BLE_DB_DISCOVERY_MAX_SRV=6
33+
CONFIG_BLE_GQ_QUEUE_SIZE=8
34+
CONFIG_BLE_GQ_HEAP_SIZE=256
35+
CONFIG_BLE_GQ_MAX_CONNECTIONS=1
36+
CONFIG_SRV_DISC_START_HANDLE=1
37+
CONFIG_BLE_GATT_DB_MAX_CHARS=6
38+
)
39+
40+
# Generate and add test file
41+
test_runner_generate(src/unity_test.c)
42+
target_sources(app PRIVATE src/unity_test.c)
43+
44+
target_include_directories(app PRIVATE ${ZEPHYR_NRF_BM_MODULE_DIR}/include)
45+
target_include_directories(app PRIVATE ${SOFTDEVICE_INCLUDE_DIR})
46+
target_include_directories(app PRIVATE ${ZEPHYR_HAL_NORDIC_MODULE_DIR}/nrfx/mdk)
47+
target_include_directories(app PRIVATE ${ZEPHYR_CMSIS_MODULE_DIR}/CMSIS/Core/Include)
48+
49+
target_sources(app
50+
PRIVATE
51+
${ZEPHYR_NRF_BM_MODULE_DIR}/lib/bluetooth/ble_db_discovery/ble_db_discovery.c
52+
)
53+
54+
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: 301 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,301 @@
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 <bm/bluetooth/ble_db_discovery.h>
10+
#include <bm/bluetooth/services/uuid.h>
11+
12+
#include "cmock_ble.h"
13+
#include "cmock_ble_gap.h"
14+
#include "cmock_ble_gattc.h"
15+
#include "cmock_ble_gq.h"
16+
17+
BLE_GQ_DEF(ble_gatt_queue);
18+
19+
struct ble_db_discovery_evt db_evt;
20+
static void db_discovery_evt_handler(struct ble_db_discovery *db_discovery,
21+
struct ble_db_discovery_evt *evt)
22+
{
23+
printk("event received %d\n", db_evt.evt_type);
24+
db_evt = *evt;
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_ble_gq_conn_handle_register_ExpectAnyArgsAndReturn(NRF_SUCCESS);
177+
__cmock_ble_gq_item_add_ExpectAnyArgsAndReturn(NRF_SUCCESS);
178+
TEST_ASSERT_EQUAL(NRF_SUCCESS, ble_db_discovery_start(&db_discovery, 0));
179+
180+
TEST_ASSERT_EQUAL(NRF_ERROR_BUSY, ble_db_discovery_start(&db_discovery, 0));
181+
}
182+
183+
void test_ble_db_discovery_start(void)
184+
{
185+
struct ble_db_discovery db_discovery = {0};
186+
struct ble_db_discovery_config config = {
187+
.gatt_queue = &ble_gatt_queue,
188+
.evt_handler = db_discovery_evt_handler,
189+
};
190+
191+
TEST_ASSERT_EQUAL(NRF_SUCCESS, ble_db_discovery_init(&db_discovery, &config));
192+
193+
ble_uuid_t hrs_uuid = {.type = BLE_UUID_TYPE_BLE, .uuid = BLE_UUID_HEART_RATE_SERVICE};
194+
195+
TEST_ASSERT_EQUAL(NRF_SUCCESS, ble_db_discovery_evt_register(&db_discovery, &hrs_uuid));
196+
197+
__cmock_ble_gq_conn_handle_register_ExpectAndReturn(&ble_gatt_queue, 8, NRF_SUCCESS);
198+
__cmock_ble_gq_item_add_ExpectAnyArgsAndReturn(NRF_SUCCESS);
199+
TEST_ASSERT_EQUAL(NRF_SUCCESS, ble_db_discovery_start(&db_discovery, 8));
200+
}
201+
202+
void test_ble_db_discovery_on_ble_evt(void)
203+
{
204+
struct ble_db_discovery db_discovery = {0};
205+
struct ble_db_discovery_config config = {
206+
.gatt_queue = &ble_gatt_queue,
207+
.evt_handler = db_discovery_evt_handler,
208+
};
209+
ble_evt_t evt;
210+
211+
__cmock_ble_gq_conn_handle_register_ExpectAndReturn(&ble_gatt_queue, 8, NRF_SUCCESS);
212+
__cmock_ble_gq_item_add_ExpectAnyArgsAndReturn(NRF_SUCCESS);
213+
214+
TEST_ASSERT_EQUAL(NRF_SUCCESS, ble_db_discovery_init(&db_discovery, &config));
215+
216+
ble_uuid_t hrs_uuid = {.type = BLE_UUID_TYPE_BLE, .uuid = BLE_UUID_HEART_RATE_SERVICE};
217+
TEST_ASSERT_EQUAL(NRF_SUCCESS, ble_db_discovery_evt_register(&db_discovery, &hrs_uuid));
218+
219+
TEST_ASSERT_EQUAL(NRF_SUCCESS, ble_db_discovery_start(&db_discovery, 8));
220+
221+
evt.header.evt_id = BLE_GATTC_EVT_PRIM_SRVC_DISC_RSP;
222+
evt.evt.gattc_evt.conn_handle = 0;
223+
evt.evt.gattc_evt.params.prim_srvc_disc_rsp.services[0].uuid.uuid =
224+
BLE_UUID_HEART_RATE_SERVICE;
225+
evt.evt.gattc_evt.params.prim_srvc_disc_rsp.services[0].uuid.type = BLE_UUID_TYPE_BLE;
226+
evt.evt.gattc_evt.params.prim_srvc_disc_rsp.count = 1;
227+
ble_db_discovery_on_ble_evt(&evt, &db_discovery);
228+
TEST_ASSERT_NOT_EQUAL(BLE_DB_DISCOVERY_ERROR, db_evt.evt_type);
229+
230+
evt.header.evt_id = BLE_GATTC_EVT_CHAR_DISC_RSP;
231+
evt.evt.gattc_evt.conn_handle = 0;
232+
evt.evt.gattc_evt.gatt_status = BLE_GATT_STATUS_SUCCESS;
233+
evt.evt.gattc_evt.params.char_disc_rsp.count = 1;
234+
evt.evt.gattc_evt.params.char_disc_rsp.chars[0].uuid.uuid = BLE_UUID_BATTERY_LEVEL_CHAR;
235+
evt.evt.gattc_evt.params.char_disc_rsp.chars[0].uuid.type = BLE_UUID_TYPE_BLE;
236+
ble_db_discovery_on_ble_evt(&evt, &db_discovery);
237+
TEST_ASSERT_NOT_EQUAL(BLE_DB_DISCOVERY_ERROR, db_evt.evt_type);
238+
239+
evt.header.evt_id = BLE_GATTC_EVT_DESC_DISC_RSP;
240+
evt.evt.gattc_evt.conn_handle = 0;
241+
evt.evt.gattc_evt.gatt_status = BLE_GATT_STATUS_SUCCESS;
242+
evt.evt.gattc_evt.params.desc_disc_rsp.count = 1;
243+
evt.evt.gattc_evt.params.desc_disc_rsp.descs[0].handle = 0;
244+
evt.evt.gattc_evt.params.desc_disc_rsp.descs[0].uuid.uuid =
245+
BLE_UUID_DESCRIPTOR_CHAR_USER_DESC;
246+
evt.evt.gattc_evt.params.desc_disc_rsp.descs[0].uuid.type = BLE_UUID_TYPE_BLE;
247+
ble_db_discovery_on_ble_evt(&evt, &db_discovery);
248+
TEST_ASSERT_NOT_EQUAL(BLE_DB_DISCOVERY_ERROR, db_evt.evt_type);
249+
250+
evt.header.evt_id = BLE_GAP_EVT_DISCONNECTED;
251+
evt.evt.gap_evt.params.disconnected.reason = BLE_HCI_CONNECTION_TIMEOUT;
252+
ble_db_discovery_on_ble_evt(&evt, &db_discovery);
253+
TEST_ASSERT_NOT_EQUAL(BLE_DB_DISCOVERY_ERROR, db_evt.evt_type);
254+
}
255+
256+
void test_ble_db_discovery_on_ble_evt_fail(void)
257+
{
258+
struct ble_db_discovery db_discovery = {0};
259+
struct ble_db_discovery_config config = {
260+
.gatt_queue = &ble_gatt_queue,
261+
.evt_handler = db_discovery_evt_handler,
262+
};
263+
ble_evt_t evt;
264+
265+
__cmock_ble_gq_conn_handle_register_ExpectAndReturn(&ble_gatt_queue, 4, NRF_SUCCESS);
266+
__cmock_ble_gq_item_add_ExpectAnyArgsAndReturn(NRF_SUCCESS);
267+
268+
TEST_ASSERT_EQUAL(NRF_SUCCESS, ble_db_discovery_init(&db_discovery, &config));
269+
270+
ble_uuid_t hrs_uuid = {.type = BLE_UUID_TYPE_BLE, .uuid = BLE_UUID_HEART_RATE_SERVICE};
271+
TEST_ASSERT_EQUAL(NRF_SUCCESS, ble_db_discovery_evt_register(&db_discovery, &hrs_uuid));
272+
273+
TEST_ASSERT_EQUAL(NRF_SUCCESS, ble_db_discovery_start(&db_discovery, 4));
274+
275+
__cmock_ble_gq_item_add_ExpectAnyArgsAndReturn(NRF_ERROR_NO_MEM);
276+
__cmock_ble_gq_item_add_ExpectAnyArgsAndReturn(NRF_ERROR_NO_MEM);
277+
278+
evt.header.evt_id = BLE_GATTC_EVT_PRIM_SRVC_DISC_RSP;
279+
evt.evt.gattc_evt.conn_handle = 4;
280+
evt.evt.gattc_evt.gatt_status = BLE_GATT_STATUS_SUCCESS;
281+
evt.evt.gattc_evt.params.prim_srvc_disc_rsp.services[0].uuid.uuid =
282+
BLE_UUID_HEART_RATE_SERVICE;
283+
evt.evt.gattc_evt.params.prim_srvc_disc_rsp.services[0].uuid.type = BLE_UUID_TYPE_BLE;
284+
evt.evt.gattc_evt.params.prim_srvc_disc_rsp.count = 1;
285+
ble_db_discovery_on_ble_evt(&evt, &db_discovery);
286+
TEST_ASSERT_EQUAL(BLE_DB_DISCOVERY_ERROR, db_evt.evt_type);
287+
}
288+
289+
void setUp(void)
290+
{
291+
}
292+
void tearDown(void)
293+
{
294+
}
295+
296+
extern int unity_main(void);
297+
298+
int main(void)
299+
{
300+
return unity_main();
301+
}
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)