Skip to content

Commit

Permalink
Bluetooth: Controller: add Unit tests for hci_internal wrapper
Browse files Browse the repository at this point in the history
This commit extracts the wrapper function in its own file, to avoid
bloating the hci_internal file, as well as tests its funcionality in a
unit test.
  • Loading branch information
KyraLengfeld committed Jan 21, 2025
1 parent c771edd commit eba935e
Show file tree
Hide file tree
Showing 10 changed files with 167 additions and 3 deletions.
5 changes: 5 additions & 0 deletions scripts/ci/tags.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,11 @@ ci_tests_subsys_audio_module:
- nrf/subsys/audio/
- nrf/tests/subsys/audio/

ci_tests_subsys_bluetooth_controller:
files:
- nrf/subsys/bluetooth/controller
- nrf/tests/subsys/bluetooth/controller

ci_tests_subsys_mpsl:
files:
- nrf/subsys/mpsl/
Expand Down
5 changes: 4 additions & 1 deletion subsys/bluetooth/controller/hci_internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
#include <sdc_hci_vs.h>

#include "hci_internal.h"
#if defined(CONFIG_BT_HCI_ACL_FLOW_CONTROL)
#include "hci_internal_wrappers.h"
#endif
#include "ecdh.h"

#define CMD_COMPLETE_MIN_SIZE (BT_HCI_EVT_HDR_SIZE \
Expand Down Expand Up @@ -1947,7 +1950,7 @@ int hci_internal_msg_get(uint8_t *msg_out, sdc_hci_msg_type_t *msg_type_out)
return 0;
}

const int retval = sdc_hci_get(msg_out, msg_type_out);
const int retval = sdc_hci_get(msg_out, (uint8_t *)msg_type_out);

#if defined(CONFIG_BT_CTLR_SDC_PAWR_SYNC)
if (retval == 0 && *msg_type_out == SDC_HCI_MSG_TYPE_EVT
Expand Down
26 changes: 26 additions & 0 deletions subsys/bluetooth/controller/hci_internal_wrappers.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (c) 2025 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/

#include "hci_internal_wrappers.h"
#include <zephyr/sys/util.h>

/* Note: When host and controller are built separately, the RX Buffer Count communicated here from
* the host might be exeeding the controller's TX command buffer count. This may result in the
* controller sending up to host_total_num_acl_data_packets, it cannot receive the acknoledgements
* via its TX command buffers for.
* As we do not propagate the TX command buffer count information to the controller, the simplest
* approach is to align the communicated host RX Buffer Count to it here.
*/
#if defined(CONFIG_BT_HCI_ACL_FLOW_CONTROL)
int sdc_hci_cmd_cb_host_buffer_size_wrapper(const sdc_hci_cmd_cb_host_buffer_size_t *cmd_params)
{
sdc_hci_cmd_cb_host_buffer_size_t ctrl_cmd_params = *cmd_params;
ctrl_cmd_params.host_total_num_acl_data_packets = MIN(
ctrl_cmd_params.host_total_num_acl_data_packets, (CONFIG_BT_BUF_CMD_TX_COUNT - 1));

return sdc_hci_cmd_cb_host_buffer_size(&ctrl_cmd_params);
}
#endif
21 changes: 21 additions & 0 deletions subsys/bluetooth/controller/hci_internal_wrappers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

/*
* Copyright (c) 2020 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/

/** @file
* @brief Internal HCI interface Wrappers
*/

#include <stdint.h>
#include <stdbool.h>
#include <sdc_hci_cmd_controller_baseband.h>

#ifndef HCI_INTERNAL_WRAPPERS_H__
#define HCI_INTERNAL_WRAPPERS_H__

int sdc_hci_cmd_cb_host_buffer_size_wrapper(const sdc_hci_cmd_cb_host_buffer_size_t *cmd_params);

#endif
26 changes: 26 additions & 0 deletions tests/subsys/bluetooth/controller/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#
# Copyright (c) 2025 Nordic Semiconductor ASA
#
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
#

cmake_minimum_required(VERSION 3.20.0)

find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(hci_cmd_cb_host_buffer_size_test)

# Generate runner for the test
test_runner_generate(src/hci_cmd_cb_host_buffer_size_test.c)

# Add Unit Under Test source files
target_sources(app PRIVATE ${ZEPHYR_NRF_MODULE_DIR}/subsys/bluetooth/controller/hci_internal_wrappers.c)

# Add test source file
target_sources(app PRIVATE src/hci_cmd_cb_host_buffer_size_test.c)

# Create mocks
cmock_handle(${ZEPHYR_NRFXLIB_MODULE_DIR}/softdevice_controller/include/sdc_hci_cmd_controller_baseband.h)

# Include paths
include_directories(${ZEPHYR_HAL_NORDIC_MODULE_DIR}/nrfx)
include_directories(${ZEPHYR_NRF_MODULE_DIR}/subsys/bluetooth/controller)
13 changes: 13 additions & 0 deletions tests/subsys/bluetooth/controller/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#
# Copyright (c) 2025 Nordic Semiconductor ASA
#
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
#
CONFIG_BT_CENTRAL=y
CONFIG_BT_HCI=y
CONFIG_BT=y

CONFIG_BT_HCI_ACL_FLOW_CONTROL=y
CONFIG_BT_BUF_CMD_TX_COUNT=10

CONFIG_UNITY=y
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (c) 2025 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/
#include <unity.h>
#include <stdbool.h>
#include <stdio.h>
#include <errno.h>
#include <zephyr/sys/util.h>

#include "hci_internal_wrappers.h"
#include "cmock_sdc_hci_cmd_controller_baseband.h"

#define TEST_HOST_ACL_DATA_PCKT_LNGTH 55
#define TEST_HOST_SYNC_DATA_PCKT_LNGTH 55
#define TEST_HOST_NUM_SYNC_DATA_PCKTS 5555

/* The unity_main is not declared in any header file. It is only defined in the generated test
* runner because of ncs' unity configuration. It is therefore declared here to avoid a compiler
* warning.
*/
extern int unity_main(void);

typedef struct {
uint16_t acl_input_pckts;
uint16_t exp_acl_pckts;
} test_vector_t;

static const test_vector_t test_vectors[] = {
{5, 5}, /* Input within range, no adjustment needed */
{15, (CONFIG_BT_BUF_CMD_TX_COUNT - 1)}, /* Input exceeds TX buffer count - 1 limit */
{0xFF, (CONFIG_BT_BUF_CMD_TX_COUNT - 1)}, /* Maximum input value, corner case */
};

void test_sdc_hci_cmd_cb_host_buffer_size_wrapper(void)
{
sdc_hci_cmd_cb_host_buffer_size_t cmd_params;
cmd_params.host_acl_data_packet_length = TEST_HOST_ACL_DATA_PCKT_LNGTH;
cmd_params.host_sync_data_packet_length = TEST_HOST_SYNC_DATA_PCKT_LNGTH;
cmd_params.host_total_num_sync_data_packets = TEST_HOST_NUM_SYNC_DATA_PCKTS;

for (size_t i = 0; i < ARRAY_SIZE(test_vectors); i++) {
const test_vector_t *v = &test_vectors[i];

sdc_hci_cmd_cb_host_buffer_size_t exp_cmd_params = cmd_params;
exp_cmd_params.host_total_num_acl_data_packets = v->exp_acl_pckts;
__cmock_sdc_hci_cmd_cb_host_buffer_size_ExpectAndReturn(&exp_cmd_params, 0);

cmd_params.host_total_num_acl_data_packets = v->acl_input_pckts;
sdc_hci_cmd_cb_host_buffer_size_wrapper(&cmd_params);
}
}

int main(void)
{
(void)unity_main();

return 0;
}
8 changes: 8 additions & 0 deletions tests/subsys/bluetooth/controller/testcase.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
tests:
bluetooth.controller:
platform_allow: native_sim
integration_platforms:
- native_sim
tags:
- unittest
- ci_tests_subsys_bluetooth_controller
2 changes: 1 addition & 1 deletion tests/subsys/bluetooth/enocean/testcase.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ tests:
- ci_build
integration_platforms:
- native_sim
- qemu_cortex_m3
- qemu_cortex_m3K
4 changes: 3 additions & 1 deletion tests/subsys/mpsl/pm/testcase.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
tests:
mpsl.pm:
platform_allow: native_sim
platform_allow:
- native_sim
- native_posix
integration_platforms:
- native_sim
tags:
Expand Down

0 comments on commit eba935e

Please sign in to comment.