Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ if(CONFIG_NCS_BM)
add_subdirectory(drivers)
add_subdirectory(lib)
add_subdirectory(subsys)
add_subdirectory(tests)
endif()
1 change: 1 addition & 0 deletions include/bm/bluetooth/ble_qwr.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ extern "C" {
#include <stdbool.h>
#include <ble.h>
#include <ble_gatts.h>
#include <bm/softdevice_handler/nrf_sdh_ble.h>

/**
* @brief Macro for defining a ble_qwr instance.
Expand Down
2 changes: 0 additions & 2 deletions include/bm/softdevice_handler/nrf_sdh.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ struct nrf_sdh_state_evt_observer {
*/
#define NRF_SDH_STATE_EVT_OBSERVER(_observer, _handler, _ctx, _prio) \
PRIO_LEVEL_IS_VALID(_prio); \
static int _handler(enum nrf_sdh_state_evt, void *); \
static TYPE_SECTION_ITERABLE(struct nrf_sdh_state_evt_observer, _observer, \
nrf_sdh_state_evt_observers, PRIO_LEVEL_ORD(_prio)) = { \
.handler = _handler, \
Expand Down Expand Up @@ -190,7 +189,6 @@ struct nrf_sdh_stack_evt_observer {
*/
#define NRF_SDH_STACK_EVT_OBSERVER(_observer, _handler, _ctx, _prio) \
PRIO_LEVEL_IS_VALID(_prio); \
static void _handler(void *); \
static const TYPE_SECTION_ITERABLE(struct nrf_sdh_stack_evt_observer, _observer, \
nrf_sdh_stack_evt_observers, PRIO_LEVEL_ORD(_prio)) = { \
.handler = _handler, \
Expand Down
10 changes: 10 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#
# Copyright (c) 2025 Nordic Semiconductor ASA
#
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
#
if(CONFIG_UNITY)
zephyr_linker_sources(SECTIONS observers.ld)
zephyr_include_directories(.)
zephyr_sources(observers.c)
endif()
2 changes: 0 additions & 2 deletions tests/lib/bluetooth/ble_conn_params/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ cmock_handle(${SOFTDEVICE_INCLUDE_DIR}/ble_gattc.h)
cmock_handle(${SOFTDEVICE_INCLUDE_DIR}/ble_gap.h)
cmock_handle(${ZEPHYR_NRF_BM_MODULE_DIR}/include/bm/softdevice_handler/nrf_sdh_ble.h)

zephyr_linker_sources(SECTIONS evt_obs.ld)

# Generate and add test file
test_runner_generate(src/unity_test.c)
target_sources(app PRIVATE src/unity_test.c)
Expand Down
20 changes: 2 additions & 18 deletions tests/lib/bluetooth/ble_conn_params/src/unity_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
#include <string.h>
#include <stddef.h>
#include <bm/bluetooth/ble_conn_params.h>
#include <bm/softdevice_handler/nrf_sdh.h>
#include <zephyr/sys/iterable_sections.h>

#include <observers.h>

#include "cmock_ble_gap.h"
#include "cmock_ble_gattc.h"
Expand All @@ -26,22 +26,6 @@

struct ble_conn_params_evt app_evt;

/* Invoke the BLE event handlers in ble_conn_params, passing BLE event 'evt'. */
void ble_evt_send(const ble_evt_t *evt)
{
TYPE_SECTION_FOREACH(struct nrf_sdh_ble_evt_observer, nrf_sdh_ble_evt_observers, obs) {
obs->handler(evt, obs->context);
}
}

/* Invoke the state event handlers in ble_conn_params, passing state event. */
void state_evt_send(enum nrf_sdh_state_evt state)
{
TYPE_SECTION_FOREACH(struct nrf_sdh_state_evt_observer, nrf_sdh_state_evt_observers, obs) {
obs->handler(state, obs->context);
}
}

/* Event handler for conn params */
void conn_params_evt_handler(const struct ble_conn_params_evt *evt)
{
Expand Down
2 changes: 2 additions & 0 deletions tests/lib/bluetooth/ble_conn_state/src/unity_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include <bm/bluetooth/ble_conn_state.h>
#include <zephyr/sys/util.h>

#include <observers.h>

static uint16_t conn_handle1;
static uint16_t conn_handle2 = 1;
static uint16_t conn_handle3 = 2;
Expand Down
10 changes: 2 additions & 8 deletions tests/lib/bluetooth/ble_db_discovery/src/unity_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include <bm/bluetooth/ble_db_discovery.h>
#include <bm/bluetooth/services/uuid.h>

#include <observers.h>

#include "cmock_ble.h"
#include "cmock_ble_gap.h"
#include "cmock_ble_gattc.h"
Expand All @@ -20,14 +22,6 @@ BLE_DB_DISCOVERY_DEF(db_discovery)
static struct ble_db_discovery_evt db_evt;
static struct ble_db_discovery_evt db_evt_prev;

void ble_evt_send(const ble_evt_t *evt)
{
TYPE_SECTION_FOREACH(struct nrf_sdh_ble_evt_observer, nrf_sdh_ble_evt_observers, obs)
{
obs->handler(evt, obs->context);
}
}

static void db_discovery_evt_handler(struct ble_db_discovery *db_discovery,
struct ble_db_discovery_evt *evt)
{
Expand Down
25 changes: 11 additions & 14 deletions tests/lib/bluetooth/ble_gq/src/unity_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <bm/bluetooth/ble_gq.h>
#include <zephyr/sys/util.h>

#include <observers.h>

#include "cmock_ble_gattc.h"
#include "cmock_ble_gatts.h"

Expand Down Expand Up @@ -287,7 +289,7 @@ void test_ble_gq_item_add_req_gatt_read_busy_busy_success(void)
.evt.gatts_evt.conn_handle = CONN_HANDLE_1,
};

ble_gq_on_ble_evt(&ble_evt, (void *)&ble_gq);
ble_evt_send(&ble_evt);

TEST_ASSERT_EQUAL(BLE_CONN_HANDLE_INVALID, glob_conn_handle);
TEST_ASSERT_EQUAL(NRF_SUCCESS, glob_error);
Expand Down Expand Up @@ -374,7 +376,7 @@ void test_ble_gq_item_add_req_gatt_write_busy_busy_success(void)
.evt.gatts_evt.conn_handle = CONN_HANDLE_2,
};

ble_gq_on_ble_evt(&ble_evt, (void *)&ble_gq);
ble_evt_send(&ble_evt);

TEST_ASSERT_EQUAL(BLE_CONN_HANDLE_INVALID, glob_conn_handle);
TEST_ASSERT_EQUAL(NRF_SUCCESS, glob_error);
Expand Down Expand Up @@ -417,7 +419,7 @@ void test_ble_gq_item_add_req_srv_discovery_busy_busy_success(void)
.evt.gatts_evt.conn_handle = CONN_HANDLE_1,
};

ble_gq_on_ble_evt(&ble_evt, (void *)&ble_gq);
ble_evt_send(&ble_evt);

TEST_ASSERT_EQUAL(BLE_CONN_HANDLE_INVALID, glob_conn_handle);
TEST_ASSERT_EQUAL(NRF_SUCCESS, glob_error);
Expand Down Expand Up @@ -458,7 +460,7 @@ void test_ble_gq_item_add_req_char_discovery_busy_busy_success(void)
.evt.gatts_evt.conn_handle = CONN_HANDLE_1,
};

ble_gq_on_ble_evt(&ble_evt, (void *)&ble_gq);
ble_evt_send(&ble_evt);

TEST_ASSERT_EQUAL(BLE_CONN_HANDLE_INVALID, glob_conn_handle);
TEST_ASSERT_EQUAL(NRF_SUCCESS, glob_error);
Expand Down Expand Up @@ -499,7 +501,7 @@ void test_ble_gq_item_add_req_desc_discovery_busy_busy_success(void)
.evt.gatts_evt.conn_handle = CONN_HANDLE_1,
};

ble_gq_on_ble_evt(&ble_evt, (void *)&ble_gq);
ble_evt_send(&ble_evt);

TEST_ASSERT_EQUAL(BLE_CONN_HANDLE_INVALID, glob_conn_handle);
TEST_ASSERT_EQUAL(NRF_SUCCESS, glob_error);
Expand Down Expand Up @@ -535,7 +537,7 @@ void test_ble_gq_item_add_req_gatts_hvx_busy_busy_success(void)
.evt.gatts_evt.conn_handle = CONN_HANDLE_1,
};

ble_gq_on_ble_evt(&ble_evt, (void *)&ble_gq);
ble_evt_send(&ble_evt);

TEST_ASSERT_EQUAL(BLE_CONN_HANDLE_INVALID, glob_conn_handle);
TEST_ASSERT_EQUAL(NRF_SUCCESS, glob_error);
Expand Down Expand Up @@ -596,11 +598,6 @@ void test_ble_gq_item_add_req_gatts_hvx_error_invalid_param(void)
TEST_ASSERT_EQUAL(NRF_ERROR_INVALID_PARAM, glob_error);
}

void test_ble_gq_on_ble_evt_null(void)
{
ble_gq_on_ble_evt(NULL, NULL);
}

void test_ble_gq_on_ble_evt_disconnected_event_item_purge(void)
{
uint32_t nrf_err;
Expand Down Expand Up @@ -631,7 +628,7 @@ void test_ble_gq_on_ble_evt_disconnected_event_item_purge(void)
.evt.gap_evt.conn_handle = CONN_HANDLE_2,
};

ble_gq_on_ble_evt(&ble_evt, (void *)&ble_gq);
ble_evt_send(&ble_evt);

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

ble_gq_on_ble_evt(&ble_evt, (void *)&ble_gq);
ble_evt_send(&ble_evt);

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

glob_conn_handle = BLE_CONN_HANDLE_INVALID;
Expand Down
23 changes: 5 additions & 18 deletions tests/lib/bluetooth/ble_qwr/src/unity_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@
#include <strings.h>
#include <bm/bluetooth/ble_qwr.h>

#include <observers.h>

#include "cmock_ble.h"
#include "cmock_ble_gatts.h"

BLE_QWR_DEF(qwr);

static uint16_t ble_qwr_evt_handler(struct ble_qwr *qwr, const struct ble_qwr_evt *evt)
{
return 0;
Expand All @@ -23,7 +27,6 @@ static uint16_t ble_qwr_evt_handler(struct ble_qwr *qwr, const struct ble_qwr_ev
void test_ble_qwr_init_error_null(void)
{
uint32_t nrf_err;
struct ble_qwr qwr;
struct ble_qwr_config qwr_config = {0};

nrf_err = ble_qwr_init(&qwr, NULL);
Expand All @@ -36,7 +39,6 @@ void test_ble_qwr_init_error_null(void)
void test_ble_qwr_init_error_invalid_state(void)
{
uint32_t nrf_err;
struct ble_qwr qwr;
struct ble_qwr_config qwr_config = {0};

nrf_err = ble_qwr_init(&qwr, &qwr_config);
Expand All @@ -51,7 +53,6 @@ void test_ble_qwr_init(void)
{
uint32_t nrf_err;
uint8_t mem[10];
struct ble_qwr qwr;
struct ble_qwr_config qwr_config = {
.mem_buffer = {
.p_mem = mem,
Expand Down Expand Up @@ -95,7 +96,6 @@ void test_ble_qwr_attr_register_error_invalid_param(void)
{
uint32_t nrf_err;
uint8_t mem[10];
struct ble_qwr qwr;
struct ble_qwr_config qwr_config = {
.mem_buffer = {
.p_mem = mem,
Expand All @@ -115,7 +115,6 @@ void test_ble_qwr_attr_register_error_no_mem(void)
{
uint32_t nrf_err;
uint8_t mem[10];
struct ble_qwr qwr;
struct ble_qwr_config qwr_config = {
.mem_buffer = {
.p_mem = mem,
Expand Down Expand Up @@ -168,7 +167,6 @@ void test_ble_qwr_attr_register(void)
{
uint32_t nrf_err;
uint8_t mem[10];
struct ble_qwr qwr;
struct ble_qwr_config qwr_config = {
.mem_buffer = {
.p_mem = mem,
Expand All @@ -194,7 +192,6 @@ void test_ble_qwr_attr_register(void)
void test_ble_qwr_value_get_error_null(void)
{
uint32_t nrf_err;
struct ble_qwr qwr;
uint8_t mem[1];
uint16_t len = sizeof(mem);

Expand Down Expand Up @@ -313,17 +310,6 @@ void test_ble_qwr_conn_handle_assign(void)
TEST_ASSERT_EQUAL(0xC044, qwr.conn_handle);
}

void test_ble_qwr_on_ble_evt_do_nothing(void)
{
ble_evt_t const ble_evt = {0};
struct ble_qwr qwr = {0};

/* We expect these to return immediately */
ble_qwr_on_ble_evt(&ble_evt, NULL);
ble_qwr_on_ble_evt(NULL, &qwr);
ble_qwr_on_ble_evt(&ble_evt, &qwr);
}

void test_ble_qwr_on_ble_evt_mem_req_sd_busy(void)
{
uint32_t nrf_err;
Expand Down Expand Up @@ -433,6 +419,7 @@ void test_ble_qwr_on_ble_evt_mem_req(void)

void setUp(void)
{
memset(&qwr, 0x00, sizeof(qwr));
}

void tearDown(void)
Expand Down
12 changes: 2 additions & 10 deletions tests/lib/bluetooth/ble_scan/src/unity_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
#include <stdint.h>
#include <stddef.h>
#include <bm/bluetooth/ble_scan.h>
#include <bm/softdevice_handler/nrf_sdh.h>
#include <zephyr/sys/iterable_sections.h>

#include <observers.h>

#include "cmock_ble_gap.h"
#include "cmock_ble_gatts.h"
Expand All @@ -28,14 +28,6 @@ BLE_SCAN_DEF(ble_scan);
static struct ble_scan_evt scan_event;
static struct ble_scan_evt scan_event_prev;

/* Invoke the BLE event handlers in ble_conn_params, passing BLE event 'evt'. */
void ble_evt_send(const ble_evt_t *evt)
{
TYPE_SECTION_FOREACH(struct nrf_sdh_ble_evt_observer, nrf_sdh_ble_evt_observers, obs) {
obs->handler(evt, obs->context);
}
}

void scan_event_handler_func(struct ble_scan_evt const *scan_evt)
{
scan_event_prev = scan_event;
Expand Down
30 changes: 30 additions & 0 deletions tests/observers.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (c) 2025 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/
#include <observers.h>
#include <bm/softdevice_handler/nrf_sdh.h>
#include <bm/softdevice_handler/nrf_sdh_ble.h>
#include <bm/softdevice_handler/nrf_sdh_soc.h>

void ble_evt_send(const ble_evt_t *evt)
{
TYPE_SECTION_FOREACH(struct nrf_sdh_ble_evt_observer, nrf_sdh_ble_evt_observers, obs) {
obs->handler(evt, obs->context);
}
}

void soc_evt_send(uint32_t evt)
{
TYPE_SECTION_FOREACH(struct nrf_sdh_soc_evt_observer, nrf_sdh_soc_evt_observers, obs) {
obs->handler(evt, obs->context);
}
}

void state_evt_send(enum nrf_sdh_state_evt state)
{
TYPE_SECTION_FOREACH(struct nrf_sdh_state_evt_observer, nrf_sdh_state_evt_observers, obs) {
obs->handler(state, obs->context);
}
}
17 changes: 17 additions & 0 deletions tests/observers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright (c) 2025 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/
#include <stdint.h>
#include <ble.h>
#include <bm/softdevice_handler/nrf_sdh.h>

/* Invoke the BLE event handler of each observer, passing BLE event 'evt'. */
void ble_evt_send(const ble_evt_t *evt);

/* Invoke the SoC event handler of each observer, passing SoC event 'evt'. */
void soc_evt_send(uint32_t evt);

/* Invoke the state event handler of each observer, passing state event. */
void state_evt_send(enum nrf_sdh_state_evt state);
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
ITERABLE_SECTION_ROM(nrf_sdh_ble_evt_observers, 4)
ITERABLE_SECTION_ROM(nrf_sdh_soc_evt_observers, 4)
ITERABLE_SECTION_RAM(nrf_sdh_state_evt_observers, 4)
Loading
Loading