Skip to content

Commit eb29aa2

Browse files
committed
tests: add sdh_evt_dispatch helper file
Add a small helper file to dispatch SoftDevice events to registered observers. This avoids boilerplate in tests, and clutter in the implementations under test to keep the observer's handlers global when compiling unit test. Signed-off-by: Emanuele Di Santo <emdi@nordicsemi.no>
1 parent e6f799b commit eb29aa2

29 files changed

Lines changed: 291 additions & 238 deletions

File tree

CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
# Tests
106106
/tests/subsys/fs/bm_zms/ @nrfconnect/ncs-bm @rghaddab
107107
/tests/subsys/kmu/ @nrfconnect/ncs-eris @nrfconnect/ncs-eris-test
108+
/tests/unit/common/ @nrfconnect/ncs-bm
108109
/tests/unit/lib/ @nrfconnect/ncs-bm
109110
/tests/unit/lib/bluetooth/ble_adv/ @nrfconnect/ncs-bm-test
110111
/tests/unit/lib/bluetooth/ble_conn_params/ @nrfconnect/ncs-bm

Kconfig.nrf_bm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ rsource "lib/Kconfig"
1616
rsource "modules/Kconfig"
1717
rsource "samples/Kconfig"
1818
rsource "subsys/Kconfig"
19+
rsource "tests/Kconfig"
1920
# zephyr-keep-sorted-stop
2021

2122
endif # NCS_BM

lib/bluetooth/ble_conn_state/ble_conn_state.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -372,11 +372,7 @@ uint32_t ble_conn_state_for_each_set_user_flag(uint16_t flag_index,
372372
return for_each_set_flag(bcs.flags.user_flags[flag_index], user_function, ctx);
373373
}
374374

375-
#ifdef CONFIG_UNITY
376-
void ble_evt_handler(const ble_evt_t *ble_evt, void *ctx)
377-
#else
378375
static void ble_evt_handler(const ble_evt_t *ble_evt, void *ctx)
379-
#endif
380376
{
381377
int idx = nrf_sdh_ble_idx_get(ble_evt->evt.gap_evt.conn_handle);
382378

subsys/storage/bm_storage/sd/bm_storage_sd.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,7 @@ static const struct bm_storage_info bm_storage_info = {
9797
.is_erase_before_write = false,
9898
};
9999

100-
#ifndef CONFIG_UNITY
101-
static
102-
#endif
103-
void bm_storage_sd_on_soc_evt(uint32_t evt, void *ctx);
100+
static void bm_storage_sd_on_soc_evt(uint32_t evt, void *ctx);
104101

105102
static bool on_operation_success(struct bm_storage_sd_op *op);
106103

@@ -498,10 +495,7 @@ int bm_storage_sd_on_state_evt(enum nrf_sdh_state_evt evt, void *ctx)
498495
}
499496
NRF_SDH_STATE_EVT_OBSERVER(sdh_state_evt, bm_storage_sd_on_state_evt, NULL, HIGH);
500497

501-
#ifndef CONFIG_UNITY
502-
static
503-
#endif
504-
void bm_storage_sd_on_soc_evt(uint32_t evt, void *ctx)
498+
static void bm_storage_sd_on_soc_evt(uint32_t evt, void *ctx)
505499
{
506500
bool operation_finished;
507501

tests/Kconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#
2+
# Copyright (c) 2026 Nordic Semiconductor ASA
3+
#
4+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
#
6+
7+
config TEST_SDH_EVT_DISPATCH
8+
bool "SoftDevice event dispatch test utility"
9+
depends on UNITY

tests/unit/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@
55
#
66

77
include(${CMAKE_CURRENT_LIST_DIR}/cmake/softdevice_unity_setup.cmake)
8+
9+
add_subdirectory(common)

tests/unit/common/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#
2+
# Copyright (c) 2026 Nordic Semiconductor ASA
3+
#
4+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
#
6+
7+
add_subdirectory_ifdef(CONFIG_TEST_SDH_EVT_DISPATCH sdh_evt_dispatch)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#
2+
# Copyright (c) 2026 Nordic Semiconductor ASA
3+
#
4+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
#
6+
7+
zephyr_library_named(sdh_evt_dispatch)
8+
9+
zephyr_include_directories(include)
10+
zephyr_library_sources(sdh_evt_dispatch.c)
11+
12+
zephyr_linker_sources(SECTIONS sdh_evt_dispatch.ld)
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright (c) 2026 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
*/
6+
7+
/**
8+
* @file
9+
* @brief Test utility for dispatching SoftDevice events to registered observers.
10+
*
11+
* Allows unit tests to send BLE, SoC, and state events to all observers
12+
* registered via NRF_SDH_BLE_OBSERVER, NRF_SDH_SOC_OBSERVER, and
13+
* NRF_SDH_STATE_EVT_OBSERVER without needing to make handler functions
14+
* non-static or extern.
15+
*/
16+
17+
#ifndef SDH_EVT_DISPATCH_H__
18+
#define SDH_EVT_DISPATCH_H__
19+
20+
#include <ble.h>
21+
#include <bm/softdevice_handler/nrf_sdh.h>
22+
23+
#ifdef __cplusplus
24+
extern "C" {
25+
#endif
26+
27+
/**
28+
* @brief Dispatch a BLE event to all registered BLE observers.
29+
*
30+
* Iterates the nrf_sdh_ble_evt_observers iterable section and invokes
31+
* each observer's handler in priority order.
32+
*
33+
* @param evt BLE event to dispatch.
34+
*/
35+
void sdh_evt_dispatch_ble(const ble_evt_t *evt);
36+
37+
/**
38+
* @brief Dispatch a SoC event to all registered SoC observers.
39+
*
40+
* Iterates the nrf_sdh_soc_evt_observers iterable section and invokes
41+
* each observer's handler in priority order.
42+
*
43+
* @param evt_id SoC event ID to dispatch.
44+
*/
45+
void sdh_evt_dispatch_soc(uint32_t evt_id);
46+
47+
/**
48+
* @brief Dispatch a state event to all registered state observers.
49+
*
50+
* Iterates the nrf_sdh_state_evt_observers iterable section and invokes
51+
* each observer's handler in priority order.
52+
*
53+
* @param state State event to dispatch.
54+
*/
55+
void sdh_evt_dispatch_state(enum nrf_sdh_state_evt state);
56+
57+
#ifdef __cplusplus
58+
}
59+
#endif
60+
61+
#endif /* SDH_EVT_DISPATCH_H__ */
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright (c) 2026 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
*/
6+
7+
#include <sdh_evt_dispatch.h>
8+
#include <bm/softdevice_handler/nrf_sdh_ble.h>
9+
#include <bm/softdevice_handler/nrf_sdh_soc.h>
10+
#include <zephyr/sys/iterable_sections.h>
11+
12+
void sdh_evt_dispatch_ble(const ble_evt_t *evt)
13+
{
14+
TYPE_SECTION_FOREACH(struct nrf_sdh_ble_evt_observer,
15+
nrf_sdh_ble_evt_observers, obs) {
16+
obs->handler(evt, obs->context);
17+
}
18+
}
19+
20+
void sdh_evt_dispatch_soc(uint32_t evt_id)
21+
{
22+
TYPE_SECTION_FOREACH(struct nrf_sdh_soc_evt_observer,
23+
nrf_sdh_soc_evt_observers, obs) {
24+
obs->handler(evt_id, obs->context);
25+
}
26+
}
27+
28+
void sdh_evt_dispatch_state(enum nrf_sdh_state_evt state)
29+
{
30+
TYPE_SECTION_FOREACH(struct nrf_sdh_state_evt_observer,
31+
nrf_sdh_state_evt_observers, obs) {
32+
obs->handler(state, obs->context);
33+
}
34+
}

0 commit comments

Comments
 (0)