Skip to content

Commit f6a246c

Browse files
committed
test: observers: add common functions to dispatch events to observers
Add common functions to dispatch events to observers. Signed-off-by: Emanuele Di Santo <[email protected]>
1 parent 51a4c72 commit f6a246c

File tree

17 files changed

+158
-137
lines changed

17 files changed

+158
-137
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ if(CONFIG_NCS_BM)
1212
add_subdirectory(drivers)
1313
add_subdirectory(lib)
1414
add_subdirectory(subsys)
15+
add_subdirectory(tests)
1516
endif()

tests/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#
2+
# Copyright (c) 2025 Nordic Semiconductor ASA
3+
#
4+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
#
6+
if(CONFIG_UNITY)
7+
zephyr_linker_sources(SECTIONS observers.ld)
8+
zephyr_include_directories(.)
9+
zephyr_sources(observers.c)
10+
endif()

tests/lib/bluetooth/ble_conn_params/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ cmock_handle(${SOFTDEVICE_INCLUDE_DIR}/ble_gattc.h)
2525
cmock_handle(${SOFTDEVICE_INCLUDE_DIR}/ble_gap.h)
2626
cmock_handle(${ZEPHYR_NRF_BM_MODULE_DIR}/include/bm/softdevice_handler/nrf_sdh_ble.h)
2727

28-
zephyr_linker_sources(SECTIONS evt_obs.ld)
29-
3028
# Generate and add test file
3129
test_runner_generate(src/unity_test.c)
3230
target_sources(app PRIVATE src/unity_test.c)

tests/lib/bluetooth/ble_conn_params/src/unity_test.c

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
#include <string.h>
1111
#include <stddef.h>
1212
#include <bm/bluetooth/ble_conn_params.h>
13-
#include <bm/softdevice_handler/nrf_sdh.h>
14-
#include <zephyr/sys/iterable_sections.h>
13+
14+
#include <observers.h>
1515

1616
#include "cmock_ble_gap.h"
1717
#include "cmock_ble_gattc.h"
@@ -26,22 +26,6 @@
2626

2727
struct ble_conn_params_evt app_evt;
2828

29-
/* Invoke the BLE event handlers in ble_conn_params, passing BLE event 'evt'. */
30-
void ble_evt_send(const ble_evt_t *evt)
31-
{
32-
TYPE_SECTION_FOREACH(struct nrf_sdh_ble_evt_observer, nrf_sdh_ble_evt_observers, obs) {
33-
obs->handler(evt, obs->context);
34-
}
35-
}
36-
37-
/* Invoke the state event handlers in ble_conn_params, passing state event. */
38-
void state_evt_send(enum nrf_sdh_state_evt state)
39-
{
40-
TYPE_SECTION_FOREACH(struct nrf_sdh_state_evt_observer, nrf_sdh_state_evt_observers, obs) {
41-
obs->handler(state, obs->context);
42-
}
43-
}
44-
4529
/* Event handler for conn params */
4630
void conn_params_evt_handler(const struct ble_conn_params_evt *evt)
4731
{

tests/lib/bluetooth/ble_conn_state/src/unity_test.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include <bm/bluetooth/ble_conn_state.h>
1313
#include <zephyr/sys/util.h>
1414

15+
#include <observers.h>
16+
1517
static uint16_t conn_handle1;
1618
static uint16_t conn_handle2 = 1;
1719
static uint16_t conn_handle3 = 2;

tests/lib/bluetooth/ble_db_discovery/src/unity_test.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include <bm/bluetooth/ble_db_discovery.h>
1010
#include <bm/bluetooth/services/uuid.h>
1111

12+
#include <observers.h>
13+
1214
#include "cmock_ble.h"
1315
#include "cmock_ble_gap.h"
1416
#include "cmock_ble_gattc.h"
@@ -20,14 +22,6 @@ BLE_DB_DISCOVERY_DEF(db_discovery)
2022
static struct ble_db_discovery_evt db_evt;
2123
static struct ble_db_discovery_evt db_evt_prev;
2224

23-
void ble_evt_send(const ble_evt_t *evt)
24-
{
25-
TYPE_SECTION_FOREACH(struct nrf_sdh_ble_evt_observer, nrf_sdh_ble_evt_observers, obs)
26-
{
27-
obs->handler(evt, obs->context);
28-
}
29-
}
30-
3125
static void db_discovery_evt_handler(struct ble_db_discovery *db_discovery,
3226
struct ble_db_discovery_evt *evt)
3327
{

tests/lib/bluetooth/ble_gq/src/unity_test.c

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include <bm/bluetooth/ble_gq.h>
1111
#include <zephyr/sys/util.h>
1212

13+
#include <observers.h>
14+
1315
#include "cmock_ble_gattc.h"
1416
#include "cmock_ble_gatts.h"
1517

@@ -287,7 +289,7 @@ void test_ble_gq_item_add_req_gatt_read_busy_busy_success(void)
287289
.evt.gatts_evt.conn_handle = CONN_HANDLE_1,
288290
};
289291

290-
ble_gq_on_ble_evt(&ble_evt, (void *)&ble_gq);
292+
ble_evt_send(&ble_evt);
291293

292294
TEST_ASSERT_EQUAL(BLE_CONN_HANDLE_INVALID, glob_conn_handle);
293295
TEST_ASSERT_EQUAL(NRF_SUCCESS, glob_error);
@@ -374,7 +376,7 @@ void test_ble_gq_item_add_req_gatt_write_busy_busy_success(void)
374376
.evt.gatts_evt.conn_handle = CONN_HANDLE_2,
375377
};
376378

377-
ble_gq_on_ble_evt(&ble_evt, (void *)&ble_gq);
379+
ble_evt_send(&ble_evt);
378380

379381
TEST_ASSERT_EQUAL(BLE_CONN_HANDLE_INVALID, glob_conn_handle);
380382
TEST_ASSERT_EQUAL(NRF_SUCCESS, glob_error);
@@ -417,7 +419,7 @@ void test_ble_gq_item_add_req_srv_discovery_busy_busy_success(void)
417419
.evt.gatts_evt.conn_handle = CONN_HANDLE_1,
418420
};
419421

420-
ble_gq_on_ble_evt(&ble_evt, (void *)&ble_gq);
422+
ble_evt_send(&ble_evt);
421423

422424
TEST_ASSERT_EQUAL(BLE_CONN_HANDLE_INVALID, glob_conn_handle);
423425
TEST_ASSERT_EQUAL(NRF_SUCCESS, glob_error);
@@ -458,7 +460,7 @@ void test_ble_gq_item_add_req_char_discovery_busy_busy_success(void)
458460
.evt.gatts_evt.conn_handle = CONN_HANDLE_1,
459461
};
460462

461-
ble_gq_on_ble_evt(&ble_evt, (void *)&ble_gq);
463+
ble_evt_send(&ble_evt);
462464

463465
TEST_ASSERT_EQUAL(BLE_CONN_HANDLE_INVALID, glob_conn_handle);
464466
TEST_ASSERT_EQUAL(NRF_SUCCESS, glob_error);
@@ -499,7 +501,7 @@ void test_ble_gq_item_add_req_desc_discovery_busy_busy_success(void)
499501
.evt.gatts_evt.conn_handle = CONN_HANDLE_1,
500502
};
501503

502-
ble_gq_on_ble_evt(&ble_evt, (void *)&ble_gq);
504+
ble_evt_send(&ble_evt);
503505

504506
TEST_ASSERT_EQUAL(BLE_CONN_HANDLE_INVALID, glob_conn_handle);
505507
TEST_ASSERT_EQUAL(NRF_SUCCESS, glob_error);
@@ -535,7 +537,7 @@ void test_ble_gq_item_add_req_gatts_hvx_busy_busy_success(void)
535537
.evt.gatts_evt.conn_handle = CONN_HANDLE_1,
536538
};
537539

538-
ble_gq_on_ble_evt(&ble_evt, (void *)&ble_gq);
540+
ble_evt_send(&ble_evt);
539541

540542
TEST_ASSERT_EQUAL(BLE_CONN_HANDLE_INVALID, glob_conn_handle);
541543
TEST_ASSERT_EQUAL(NRF_SUCCESS, glob_error);
@@ -596,11 +598,6 @@ void test_ble_gq_item_add_req_gatts_hvx_error_invalid_param(void)
596598
TEST_ASSERT_EQUAL(NRF_ERROR_INVALID_PARAM, glob_error);
597599
}
598600

599-
void test_ble_gq_on_ble_evt_null(void)
600-
{
601-
ble_gq_on_ble_evt(NULL, NULL);
602-
}
603-
604601
void test_ble_gq_on_ble_evt_disconnected_event_item_purge(void)
605602
{
606603
uint32_t nrf_err;
@@ -631,7 +628,7 @@ void test_ble_gq_on_ble_evt_disconnected_event_item_purge(void)
631628
.evt.gap_evt.conn_handle = CONN_HANDLE_2,
632629
};
633630

634-
ble_gq_on_ble_evt(&ble_evt, (void *)&ble_gq);
631+
ble_evt_send(&ble_evt);
635632

636633
/* Purge in progress. Receive an (arbitrary) GATT event to trigger queue processing.
637634
* The item in the queue should be purged, so expect no call to the SoftDevice.
@@ -641,7 +638,7 @@ void test_ble_gq_on_ble_evt_disconnected_event_item_purge(void)
641638
.evt.gap_evt.conn_handle = CONN_HANDLE_2,
642639
};
643640

644-
ble_gq_on_ble_evt(&ble_evt, (void *)&ble_gq);
641+
ble_evt_send(&ble_evt);
645642

646643
TEST_ASSERT_EQUAL(BLE_CONN_HANDLE_INVALID, glob_conn_handle);
647644
TEST_ASSERT_EQUAL(NRF_SUCCESS, glob_error);
@@ -656,7 +653,7 @@ void setUp(void)
656653
/* Deregister all the registered connection handles by sending disconnect events. */
657654
for (uint32_t i = 0; i < ARRAY_SIZE(conn_handles); ++i) {
658655
ble_evt.evt.gap_evt.conn_handle = conn_handles[i];
659-
ble_gq_on_ble_evt(&ble_evt, (void *)&ble_gq);
656+
ble_evt_send(&ble_evt);
660657
}
661658

662659
glob_conn_handle = BLE_CONN_HANDLE_INVALID;

tests/lib/bluetooth/ble_qwr/src/unity_test.c

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@
1212
#include <strings.h>
1313
#include <bm/bluetooth/ble_qwr.h>
1414

15+
#include <observers.h>
16+
1517
#include "cmock_ble.h"
1618
#include "cmock_ble_gatts.h"
1719

20+
BLE_QWR_DEF(qwr);
21+
1822
static uint16_t ble_qwr_evt_handler(struct ble_qwr *qwr, const struct ble_qwr_evt *evt)
1923
{
2024
return 0;
@@ -23,7 +27,6 @@ static uint16_t ble_qwr_evt_handler(struct ble_qwr *qwr, const struct ble_qwr_ev
2327
void test_ble_qwr_init_error_null(void)
2428
{
2529
uint32_t nrf_err;
26-
struct ble_qwr qwr;
2730
struct ble_qwr_config qwr_config = {0};
2831

2932
nrf_err = ble_qwr_init(&qwr, NULL);
@@ -36,7 +39,6 @@ void test_ble_qwr_init_error_null(void)
3639
void test_ble_qwr_init_error_invalid_state(void)
3740
{
3841
uint32_t nrf_err;
39-
struct ble_qwr qwr;
4042
struct ble_qwr_config qwr_config = {0};
4143

4244
nrf_err = ble_qwr_init(&qwr, &qwr_config);
@@ -51,7 +53,6 @@ void test_ble_qwr_init(void)
5153
{
5254
uint32_t nrf_err;
5355
uint8_t mem[10];
54-
struct ble_qwr qwr;
5556
struct ble_qwr_config qwr_config = {
5657
.mem_buffer = {
5758
.p_mem = mem,
@@ -95,7 +96,6 @@ void test_ble_qwr_attr_register_error_invalid_param(void)
9596
{
9697
uint32_t nrf_err;
9798
uint8_t mem[10];
98-
struct ble_qwr qwr;
9999
struct ble_qwr_config qwr_config = {
100100
.mem_buffer = {
101101
.p_mem = mem,
@@ -115,7 +115,6 @@ void test_ble_qwr_attr_register_error_no_mem(void)
115115
{
116116
uint32_t nrf_err;
117117
uint8_t mem[10];
118-
struct ble_qwr qwr;
119118
struct ble_qwr_config qwr_config = {
120119
.mem_buffer = {
121120
.p_mem = mem,
@@ -168,7 +167,6 @@ void test_ble_qwr_attr_register(void)
168167
{
169168
uint32_t nrf_err;
170169
uint8_t mem[10];
171-
struct ble_qwr qwr;
172170
struct ble_qwr_config qwr_config = {
173171
.mem_buffer = {
174172
.p_mem = mem,
@@ -194,7 +192,6 @@ void test_ble_qwr_attr_register(void)
194192
void test_ble_qwr_value_get_error_null(void)
195193
{
196194
uint32_t nrf_err;
197-
struct ble_qwr qwr;
198195
uint8_t mem[1];
199196
uint16_t len = sizeof(mem);
200197

@@ -313,17 +310,6 @@ void test_ble_qwr_conn_handle_assign(void)
313310
TEST_ASSERT_EQUAL(0xC044, qwr.conn_handle);
314311
}
315312

316-
void test_ble_qwr_on_ble_evt_do_nothing(void)
317-
{
318-
ble_evt_t const ble_evt = {0};
319-
struct ble_qwr qwr = {0};
320-
321-
/* We expect these to return immediately */
322-
ble_qwr_on_ble_evt(&ble_evt, NULL);
323-
ble_qwr_on_ble_evt(NULL, &qwr);
324-
ble_qwr_on_ble_evt(&ble_evt, &qwr);
325-
}
326-
327313
void test_ble_qwr_on_ble_evt_mem_req_sd_busy(void)
328314
{
329315
uint32_t nrf_err;
@@ -433,6 +419,7 @@ void test_ble_qwr_on_ble_evt_mem_req(void)
433419

434420
void setUp(void)
435421
{
422+
memset(&qwr, 0x00, sizeof(qwr));
436423
}
437424

438425
void tearDown(void)

tests/lib/bluetooth/ble_scan/src/unity_test.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
#include <stdint.h>
1010
#include <stddef.h>
1111
#include <bm/bluetooth/ble_scan.h>
12-
#include <bm/softdevice_handler/nrf_sdh.h>
13-
#include <zephyr/sys/iterable_sections.h>
12+
13+
#include <observers.h>
1414

1515
#include "cmock_ble_gap.h"
1616
#include "cmock_ble_gatts.h"
@@ -28,14 +28,6 @@ BLE_SCAN_DEF(ble_scan);
2828
static struct ble_scan_evt scan_event;
2929
static struct ble_scan_evt scan_event_prev;
3030

31-
/* Invoke the BLE event handlers in ble_conn_params, passing BLE event 'evt'. */
32-
void ble_evt_send(const ble_evt_t *evt)
33-
{
34-
TYPE_SECTION_FOREACH(struct nrf_sdh_ble_evt_observer, nrf_sdh_ble_evt_observers, obs) {
35-
obs->handler(evt, obs->context);
36-
}
37-
}
38-
3931
void scan_event_handler_func(struct ble_scan_evt const *scan_evt)
4032
{
4133
scan_event_prev = scan_event;

tests/observers.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) 2025 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
*/
6+
#include <observers.h>
7+
#include <bm/softdevice_handler/nrf_sdh.h>
8+
#include <bm/softdevice_handler/nrf_sdh_ble.h>
9+
#include <bm/softdevice_handler/nrf_sdh_soc.h>
10+
11+
void ble_evt_send(const ble_evt_t *evt)
12+
{
13+
TYPE_SECTION_FOREACH(struct nrf_sdh_ble_evt_observer, nrf_sdh_ble_evt_observers, obs) {
14+
obs->handler(evt, obs->context);
15+
}
16+
}
17+
18+
void soc_evt_send(uint32_t evt)
19+
{
20+
TYPE_SECTION_FOREACH(struct nrf_sdh_soc_evt_observer, nrf_sdh_soc_evt_observers, obs) {
21+
obs->handler(evt, obs->context);
22+
}
23+
}
24+
25+
void state_evt_send(enum nrf_sdh_state_evt state)
26+
{
27+
TYPE_SECTION_FOREACH(struct nrf_sdh_state_evt_observer, nrf_sdh_state_evt_observers, obs) {
28+
obs->handler(state, obs->context);
29+
}
30+
}

0 commit comments

Comments
 (0)