From 1cf2330a882dca5849351a0e576714e567a48117 Mon Sep 17 00:00:00 2001 From: Przemyslaw Bida Date: Thu, 21 Nov 2024 13:11:36 +0100 Subject: [PATCH] nrf_rpc: Add command to obtain version of remote. Commit adds command to pbtain the remote ncs commit sha version. Signed-off-by: Przemyslaw Bida --- CODEOWNERS | 1 + .../libraries/nrf_rpc/nrf_rpc_dev_info.rst | 24 +++++++++ .../releases/release-notes-changelog.rst | 2 +- include/nrf_rpc/nrf_rpc_dev_info.h | 28 +++++++++++ .../client/CMakeLists.txt | 1 + .../protocols_serialization/client/prj.conf | 2 + .../client/src/dev_info_shell.c | 24 +++++++++ .../protocols_serialization/server/prj.conf | 3 ++ subsys/nrf_rpc/CMakeLists.txt | 2 + subsys/nrf_rpc/Kconfig | 2 + subsys/nrf_rpc/dev_info/CMakeLists.txt | 10 ++++ subsys/nrf_rpc/dev_info/Kconfig | 40 +++++++++++++++ subsys/nrf_rpc/dev_info/client/CMakeLists.txt | 15 ++++++ .../nrf_rpc/dev_info/client/dev_info_client.c | 38 ++++++++++++++ subsys/nrf_rpc/dev_info/common/CMakeLists.txt | 11 +++++ subsys/nrf_rpc/dev_info/common/dev_info_rpc.c | 49 +++++++++++++++++++ .../dev_info/common/dev_info_rpc_ids.h | 16 ++++++ subsys/nrf_rpc/dev_info/server/CMakeLists.txt | 15 ++++++ .../nrf_rpc/dev_info/server/dev_info_server.c | 36 ++++++++++++++ subsys/nrf_rpc/include/nrf_rpc_os.h | 2 +- .../nrf_rpc/dev_info/client/CMakeLists.txt | 27 ++++++++++ tests/subsys/nrf_rpc/dev_info/client/prj.conf | 19 +++++++ .../dev_info/client/src/dev_info_suite.c | 43 ++++++++++++++++ .../nrf_rpc/dev_info/client/testcase.yaml | 7 +++ .../dev_info/common/nrf_rpc_single_thread.c | 36 ++++++++++++++ .../nrf_rpc/dev_info/common/test_rpc_env.h | 40 +++++++++++++++ .../nrf_rpc/dev_info/server/CMakeLists.txt | 27 ++++++++++ tests/subsys/nrf_rpc/dev_info/server/prj.conf | 20 ++++++++ .../dev_info/server/src/dev_info_suite.c | 37 ++++++++++++++ .../nrf_rpc/dev_info/server/testcase.yaml | 7 +++ 30 files changed, 582 insertions(+), 2 deletions(-) create mode 100644 doc/nrf/libraries/nrf_rpc/nrf_rpc_dev_info.rst create mode 100644 include/nrf_rpc/nrf_rpc_dev_info.h create mode 100644 samples/nrf_rpc/protocols_serialization/client/src/dev_info_shell.c create mode 100644 subsys/nrf_rpc/dev_info/CMakeLists.txt create mode 100644 subsys/nrf_rpc/dev_info/Kconfig create mode 100644 subsys/nrf_rpc/dev_info/client/CMakeLists.txt create mode 100644 subsys/nrf_rpc/dev_info/client/dev_info_client.c create mode 100644 subsys/nrf_rpc/dev_info/common/CMakeLists.txt create mode 100644 subsys/nrf_rpc/dev_info/common/dev_info_rpc.c create mode 100644 subsys/nrf_rpc/dev_info/common/dev_info_rpc_ids.h create mode 100644 subsys/nrf_rpc/dev_info/server/CMakeLists.txt create mode 100644 subsys/nrf_rpc/dev_info/server/dev_info_server.c create mode 100644 tests/subsys/nrf_rpc/dev_info/client/CMakeLists.txt create mode 100644 tests/subsys/nrf_rpc/dev_info/client/prj.conf create mode 100644 tests/subsys/nrf_rpc/dev_info/client/src/dev_info_suite.c create mode 100644 tests/subsys/nrf_rpc/dev_info/client/testcase.yaml create mode 100644 tests/subsys/nrf_rpc/dev_info/common/nrf_rpc_single_thread.c create mode 100644 tests/subsys/nrf_rpc/dev_info/common/test_rpc_env.h create mode 100644 tests/subsys/nrf_rpc/dev_info/server/CMakeLists.txt create mode 100644 tests/subsys/nrf_rpc/dev_info/server/prj.conf create mode 100644 tests/subsys/nrf_rpc/dev_info/server/src/dev_info_suite.c create mode 100644 tests/subsys/nrf_rpc/dev_info/server/testcase.yaml diff --git a/CODEOWNERS b/CODEOWNERS index d43e28a48016..e7f69c0ac0da 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -793,6 +793,7 @@ /tests/subsys/nfc/rpc/ @nrfconnect/ncs-protocols-serialization /tests/subsys/nrf_compress/ @nordicjm /tests/subsys/nrf_profiler/ @nrfconnect/ncs-si-bluebagel +/tests/subsys/nrf_rpc/ @nrfconnect/ncs-protocols-serialization /tests/subsys/partition_manager/region/ @nordicjm @tejlmand /tests/subsys/partition_manager/static_pm_file/ @nordicjm @tejlmand /tests/subsys/pcd/ @nrfconnect/ncs-pluto diff --git a/doc/nrf/libraries/nrf_rpc/nrf_rpc_dev_info.rst b/doc/nrf/libraries/nrf_rpc/nrf_rpc_dev_info.rst new file mode 100644 index 000000000000..4be628df0ce1 --- /dev/null +++ b/doc/nrf/libraries/nrf_rpc/nrf_rpc_dev_info.rst @@ -0,0 +1,24 @@ +.. _nrf_rpc_dev_info: + +nRF RPC device information +########################## + +.. contents:: + :local: + :depth: 2 + +This library allows you to obtain the device information functions of the :ref:`nrfxlib:nrf_rpc` on a remote server. + +Configuration +************* + +Use the :kconfig:option:`CONFIG_NRF_RPC_DEV_INFO` Kconfig option to enable this library. +To enable it for the client, use the :kconfig:option:`CONFIG_NRF_RPC_DEV_INFO_CLIENT` Kconfig option and respectively, +:kconfig:option:`CONFIG_NRF_RPC_DEV_INFO_SERVER` for the server. + +API documentation +***************** + +| Header file: :file:`include/nrf_rpc/nrf_rpc_dev_info.h` + +.. doxygengroup:: nrf_rpc_dev_info diff --git a/doc/nrf/releases_and_maturity/releases/release-notes-changelog.rst b/doc/nrf/releases_and_maturity/releases/release-notes-changelog.rst index 2aee0cfbdf08..0875df125107 100644 --- a/doc/nrf/releases_and_maturity/releases/release-notes-changelog.rst +++ b/doc/nrf/releases_and_maturity/releases/release-notes-changelog.rst @@ -580,7 +580,7 @@ Libraries for NFC nRF RPC libraries ----------------- -|no_changes_yet_note| +* Added the :ref:`nrf_rpc_dev_info` library for obtaining information about a device connected through the :ref:`nrfxlib:nrf_rpc`. Other libraries --------------- diff --git a/include/nrf_rpc/nrf_rpc_dev_info.h b/include/nrf_rpc/nrf_rpc_dev_info.h new file mode 100644 index 000000000000..45c85336a4dd --- /dev/null +++ b/include/nrf_rpc/nrf_rpc_dev_info.h @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2024 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause + */ + +#ifndef UTILS_NRF_RPC_UTILS_H_ +#define UTILS_NRF_RPC_UTILS_H_ + +/** + * @defgroup nrf_rpc_dev_info nRF RPC device information. + * @{ + * @brief nRF RPC device information functions. + * + */ + +/** @brief Get version of remote server the RPC client is connected to. + * + * @retval version of the remote on success. + * @retval NULL on failure. + */ +const char *nrf_rpc_get_ncs_commit_sha(void); + +/** + * @} + */ + +#endif /* UTILS_NRF_RPC_UTILS_H_ */ diff --git a/samples/nrf_rpc/protocols_serialization/client/CMakeLists.txt b/samples/nrf_rpc/protocols_serialization/client/CMakeLists.txt index a59f459d6c88..42eae5ab4082 100644 --- a/samples/nrf_rpc/protocols_serialization/client/CMakeLists.txt +++ b/samples/nrf_rpc/protocols_serialization/client/CMakeLists.txt @@ -18,4 +18,5 @@ zephyr_library_sources_ifdef(CONFIG_OPENTHREAD_RPC src/ot_shell.c) zephyr_library_sources_ifdef(CONFIG_MPSL_CX_SOFTWARE src/coex_shell.c) zephyr_library_sources_ifdef(CONFIG_LOG_FORWARDER_RPC src/log_rpc_shell.c) zephyr_library_sources_ifdef(CONFIG_NFC_RPC src/nfc_shell.c) +zephyr_library_sources_ifdef(CONFIG_NRF_RPC_DEV_INFO src/dev_info_shell.c) # NORDIC SDK APP START diff --git a/samples/nrf_rpc/protocols_serialization/client/prj.conf b/samples/nrf_rpc/protocols_serialization/client/prj.conf index c11a86b36456..6d8b40d05d22 100644 --- a/samples/nrf_rpc/protocols_serialization/client/prj.conf +++ b/samples/nrf_rpc/protocols_serialization/client/prj.conf @@ -23,3 +23,5 @@ CONFIG_LOG_BUFFER_SIZE=4096 CONFIG_LOG_FUNC_NAME_PREFIX_DBG=n CONFIG_LOG_BACKEND_RTT=n CONFIG_SHELL_LOG_BACKEND=y + +CONFIG_NRF_RPC_DEV_INFO=y diff --git a/samples/nrf_rpc/protocols_serialization/client/src/dev_info_shell.c b/samples/nrf_rpc/protocols_serialization/client/src/dev_info_shell.c new file mode 100644 index 000000000000..c2ae0b2b1f61 --- /dev/null +++ b/samples/nrf_rpc/protocols_serialization/client/src/dev_info_shell.c @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2024 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause + */ + +#include +#include + +static int remote_version_cmd(const struct shell *sh, size_t argc, char *argv[]) +{ + const char *version = nrf_rpc_get_ncs_commit_sha(); + + shell_print(sh, "Remote version: %s", version); + + return 0; +} + +SHELL_STATIC_SUBCMD_SET_CREATE(util_cmds, + SHELL_CMD_ARG(remote_version, NULL, "Get server version", + remote_version_cmd, 1, 0), + SHELL_SUBCMD_SET_END); + +SHELL_CMD_ARG_REGISTER(rpc, &util_cmds, "nRF RPC utility commands", NULL, 1, 0); diff --git a/samples/nrf_rpc/protocols_serialization/server/prj.conf b/samples/nrf_rpc/protocols_serialization/server/prj.conf index 3c531875cbc8..9e703b03aef7 100644 --- a/samples/nrf_rpc/protocols_serialization/server/prj.conf +++ b/samples/nrf_rpc/protocols_serialization/server/prj.conf @@ -15,3 +15,6 @@ CONFIG_NRF_RPC_UART_RELIABLE=y CONFIG_UART_LINE_CTRL=y CONFIG_UART_INTERRUPT_DRIVEN=y + +CONFIG_NRF_RPC_DEV_INFO=y +CONFIG_NRF_RPC_DEV_INFO_SERVER=y diff --git a/subsys/nrf_rpc/CMakeLists.txt b/subsys/nrf_rpc/CMakeLists.txt index 8185edd48f9c..35c07495dca4 100644 --- a/subsys/nrf_rpc/CMakeLists.txt +++ b/subsys/nrf_rpc/CMakeLists.txt @@ -17,3 +17,5 @@ zephyr_library_sources_ifdef(CONFIG_NRF_RPC_SERIALIZE_API nrf_rpc_serialize.c) zephyr_library_sources_ifdef(CONFIG_NRF_RPC_CALLBACK_PROXY nrf_rpc_cbkproxy.c) zephyr_library_sources_ifdef(CONFIG_NRF_RPC_UART_TRANSPORT nrf_rpc_uart.c) + +add_subdirectory_ifdef(CONFIG_NRF_RPC_DEV_INFO dev_info) diff --git a/subsys/nrf_rpc/Kconfig b/subsys/nrf_rpc/Kconfig index 95df5beb182a..897b2b09b037 100644 --- a/subsys/nrf_rpc/Kconfig +++ b/subsys/nrf_rpc/Kconfig @@ -187,6 +187,8 @@ source "${ZEPHYR_BASE}/subsys/logging/Kconfig.template.log_config" endif # NRF_RPC_CBOR +rsource "dev_info/Kconfig" + endif # NRF_RPC endmenu diff --git a/subsys/nrf_rpc/dev_info/CMakeLists.txt b/subsys/nrf_rpc/dev_info/CMakeLists.txt new file mode 100644 index 000000000000..5ebf215bcadf --- /dev/null +++ b/subsys/nrf_rpc/dev_info/CMakeLists.txt @@ -0,0 +1,10 @@ +# +# Copyright (c) 2024 Nordic Semiconductor +# +# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause +# + +add_subdirectory(common) + +add_subdirectory_ifdef(CONFIG_NRF_RPC_DEV_INFO_CLIENT client) +add_subdirectory_ifdef(CONFIG_NRF_RPC_DEV_INFO_SERVER server) diff --git a/subsys/nrf_rpc/dev_info/Kconfig b/subsys/nrf_rpc/dev_info/Kconfig new file mode 100644 index 000000000000..b5efb9b7f12b --- /dev/null +++ b/subsys/nrf_rpc/dev_info/Kconfig @@ -0,0 +1,40 @@ +# +# Copyright (c) 2024 Nordic Semiconductor +# +# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause +# + +menuconfig NRF_RPC_DEV_INFO + bool "nRF RPC device information functionalities" + depends on NRF_RPC + depends on NRF_RPC_CBOR + help + nRF RPC device information functionalities + +if NRF_RPC_DEV_INFO + +choice NRF_RPC_DEV_INFO_ROLE_CHOICE + prompt "nRF RPC device information role selection" + default NRF_RPC_DEV_INFO_CLIENT + help + nRF RPC device information role selection + +config NRF_RPC_DEV_INFO_SERVER + bool "nRF RPC device information server role" + help + Enable nRF RPC device information server role. + +config NRF_RPC_DEV_INFO_CLIENT + bool "nRF RPC device information client role" + help + Enable nRF RPC device information client role. + +endchoice + +config DEV_INFO_RPC_INITIALIZE_NRF_RPC + bool "Automatically initialize nRF RPC library" + help + Initialize nRF RPC library during the system startup. Disabling this + option allow user to initialize it in a different way. + +endif # NRF_RPC_DEV_INFO diff --git a/subsys/nrf_rpc/dev_info/client/CMakeLists.txt b/subsys/nrf_rpc/dev_info/client/CMakeLists.txt new file mode 100644 index 000000000000..5e9a9d74ddc0 --- /dev/null +++ b/subsys/nrf_rpc/dev_info/client/CMakeLists.txt @@ -0,0 +1,15 @@ +# +# Copyright (c) 2024 Nordic Semiconductor +# +# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause +# + +zephyr_library() + +zephyr_library_sources( + dev_info_client.c +) + +zephyr_library_include_directories( + ${CMAKE_CURRENT_SOURCE_DIR}/../common +) diff --git a/subsys/nrf_rpc/dev_info/client/dev_info_client.c b/subsys/nrf_rpc/dev_info/client/dev_info_client.c new file mode 100644 index 000000000000..ac4ca744b5c7 --- /dev/null +++ b/subsys/nrf_rpc/dev_info/client/dev_info_client.c @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2024 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause + */ + +#include "zephyr/sys/util.h" +#include +#include + +#include +#include + +NRF_RPC_GROUP_DECLARE(dev_info_group); + +static char version[16]; + +const char *nrf_rpc_get_ncs_commit_sha(void) +{ + struct nrf_rpc_cbor_ctx ctx; + size_t size = ARRAY_SIZE(version); + + memset(version, 0, ARRAY_SIZE(version)); + + NRF_RPC_CBOR_ALLOC(&dev_info_group, ctx, 0); + + nrf_rpc_cbor_cmd_rsp_no_err(&dev_info_group, DEV_INFO_RPC_GET_VERSION, &ctx); + + nrf_rpc_decode_str(&ctx, version, size); + + if (!nrf_rpc_decoding_done_and_check(&dev_info_group, &ctx)) { + nrf_rpc_err(-EBADMSG, NRF_RPC_ERR_SRC_RECV, &dev_info_group, + DEV_INFO_RPC_GET_VERSION, NRF_RPC_PACKET_TYPE_RSP); + return NULL; + } + + return version; +} diff --git a/subsys/nrf_rpc/dev_info/common/CMakeLists.txt b/subsys/nrf_rpc/dev_info/common/CMakeLists.txt new file mode 100644 index 000000000000..b7e3aaa09937 --- /dev/null +++ b/subsys/nrf_rpc/dev_info/common/CMakeLists.txt @@ -0,0 +1,11 @@ +# +# Copyright (c) 2024 Nordic Semiconductor +# +# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause +# + +zephyr_library() + +zephyr_library_sources( + dev_info_rpc.c +) diff --git a/subsys/nrf_rpc/dev_info/common/dev_info_rpc.c b/subsys/nrf_rpc/dev_info/common/dev_info_rpc.c new file mode 100644 index 000000000000..595698b80456 --- /dev/null +++ b/subsys/nrf_rpc/dev_info/common/dev_info_rpc.c @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2024 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause + */ + +#if CONFIG_NRF_RPC_IPC_SERVICE +#include +#elif CONFIG_NRF_RPC_UART_TRANSPORT +#include +#elif CONFIG_MOCK_NRF_RPC_TRANSPORT +#include +#endif +#include + +#include +#include + +#ifdef CONFIG_NRF_RPC_IPC_SERVICE +NRF_RPC_IPC_TRANSPORT(dev_info_rpc_tr, DEVICE_DT_GET(DT_NODELABEL(ipc0)), "dev_info_rpc_ept"); +#elif defined(CONFIG_NRF_RPC_UART_TRANSPORT) +#define dev_info_rpc_tr NRF_RPC_UART_TRANSPORT(DT_CHOSEN(nordic_rpc_uart)) +#elif defined(CONFIG_MOCK_NRF_RPC_TRANSPORT) +#define dev_info_rpc_tr mock_nrf_rpc_tr +#endif +NRF_RPC_GROUP_DEFINE(dev_info_group, "dev_info", &dev_info_rpc_tr, NULL, NULL, NULL); +LOG_MODULE_REGISTER(dev_info_rpc, LOG_LEVEL_DBG); + +#ifdef CONFIG_DEV_INFO_RPC_INITIALIZE_NRF_RPC +static void err_handler(const struct nrf_rpc_err_report *report) +{ + LOG_ERR("nRF RPC error %d ocurred. See nRF RPC logs for more details", report->code); + k_oops(); +} + +static int serialization_init(void) +{ + int err; + + err = nrf_rpc_init(err_handler); + if (err) { + return -EINVAL; + } + + return 0; +} + +SYS_INIT(serialization_init, POST_KERNEL, CONFIG_APPLICATION_INIT_PRIORITY); +#endif /* CONFIG_DEV_INFO_RPC_INITIALIZE_NRF_RPC */ diff --git a/subsys/nrf_rpc/dev_info/common/dev_info_rpc_ids.h b/subsys/nrf_rpc/dev_info/common/dev_info_rpc_ids.h new file mode 100644 index 000000000000..7f7bdf3867d6 --- /dev/null +++ b/subsys/nrf_rpc/dev_info/common/dev_info_rpc_ids.h @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2024 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause + */ + +#ifndef DEV_INFO_RPC_IDS_H_ +#define DEV_INFO_RPC_IDS_H_ + +/** @brief Command IDs accepted by the device information over RPC server. + */ +enum ot_rpc_cmd_server { + DEV_INFO_RPC_GET_VERSION = 0, +}; + +#endif /* DEV_INFO_RPC_IDS_H_ */ diff --git a/subsys/nrf_rpc/dev_info/server/CMakeLists.txt b/subsys/nrf_rpc/dev_info/server/CMakeLists.txt new file mode 100644 index 000000000000..110304819d2a --- /dev/null +++ b/subsys/nrf_rpc/dev_info/server/CMakeLists.txt @@ -0,0 +1,15 @@ +# +# Copyright (c) 2024 Nordic Semiconductor +# +# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause +# + +zephyr_library() + +zephyr_library_sources( + dev_info_server.c +) + +zephyr_library_include_directories( + ${CMAKE_CURRENT_SOURCE_DIR}/../common +) diff --git a/subsys/nrf_rpc/dev_info/server/dev_info_server.c b/subsys/nrf_rpc/dev_info/server/dev_info_server.c new file mode 100644 index 000000000000..916eadb213f0 --- /dev/null +++ b/subsys/nrf_rpc/dev_info/server/dev_info_server.c @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2024 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause + */ + +#include +#include + +#include +#include + +NRF_RPC_GROUP_DECLARE(dev_info_group); + +static void get_server_version(const struct nrf_rpc_group *group, struct nrf_rpc_cbor_ctx *ctx, + void *handler_data) +{ + struct nrf_rpc_cbor_ctx rsp_ctx; + size_t cbor_buffer_size = 0; + + if (!nrf_rpc_decoding_done_and_check(group, ctx)) { + nrf_rpc_err(-EBADMSG, NRF_RPC_ERR_SRC_RECV, group, DEV_INFO_RPC_GET_VERSION, + NRF_RPC_PACKET_TYPE_CMD); + return; + } + + cbor_buffer_size += 2 + strlen(NCS_COMMIT_STRING); + + NRF_RPC_CBOR_ALLOC(group, rsp_ctx, cbor_buffer_size); + + nrf_rpc_encode_str(&rsp_ctx, NCS_COMMIT_STRING, strlen(NCS_COMMIT_STRING)); + nrf_rpc_cbor_rsp_no_err(group, &rsp_ctx); +} + +NRF_RPC_CBOR_CMD_DECODER(dev_info_group, get_server_version, DEV_INFO_RPC_GET_VERSION, + get_server_version, NULL); diff --git a/subsys/nrf_rpc/include/nrf_rpc_os.h b/subsys/nrf_rpc/include/nrf_rpc_os.h index c657f5a7bd9b..def87a991987 100644 --- a/subsys/nrf_rpc/include/nrf_rpc_os.h +++ b/subsys/nrf_rpc/include/nrf_rpc_os.h @@ -11,7 +11,7 @@ #include /** - * @defgroup nrf_rpc_os_zephyr nRF PRC OS abstraction for Zephyr. + * @defgroup nrf_rpc_os_zephyr nRF RPC OS abstraction for Zephyr. * @{ * @brief nRF PRC OS abstraction for Zephyr. * diff --git a/tests/subsys/nrf_rpc/dev_info/client/CMakeLists.txt b/tests/subsys/nrf_rpc/dev_info/client/CMakeLists.txt new file mode 100644 index 000000000000..40b89233f1e3 --- /dev/null +++ b/tests/subsys/nrf_rpc/dev_info/client/CMakeLists.txt @@ -0,0 +1,27 @@ +# +# Copyright (c) 2024 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(nrf_rpc_dev_info_client_test) + +FILE(GLOB app_sources src/*.c) + +target_include_directories(app PRIVATE + ${ZEPHYR_NRF_MODULE_DIR}/subsys/nrf_rpc/dev_info/common + ${ZEPHYR_NRF_MODULE_DIR}/subsys/nrf_rpc/dev_info/client + ../common +) + +target_sources(app PRIVATE + ${app_sources} + ../common/nrf_rpc_single_thread.c +) + +# Enforce single-threaded nRF RPC command processing. +target_link_options(app PUBLIC + -Wl,--wrap=nrf_rpc_os_init,--wrap=nrf_rpc_os_thread_pool_send +) diff --git a/tests/subsys/nrf_rpc/dev_info/client/prj.conf b/tests/subsys/nrf_rpc/dev_info/client/prj.conf new file mode 100644 index 000000000000..8bcc765c2c40 --- /dev/null +++ b/tests/subsys/nrf_rpc/dev_info/client/prj.conf @@ -0,0 +1,19 @@ +# +# Copyright (c) 2024 Nordic Semiconductor ASA +# +# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause +# + +# Ztest configuration +CONFIG_ZTEST=y +CONFIG_TEST_RANDOM_GENERATOR=y + +CONFIG_NRF_RPC_CALLBACK_PROXY=n + +CONFIG_NRF_RPC=y +CONFIG_MOCK_NRF_RPC=y +CONFIG_MOCK_NRF_RPC_TRANSPORT=y + +CONFIG_KERNEL_MEM_POOL=y +CONFIG_HEAP_MEM_POOL_SIZE=4096 +CONFIG_NRF_RPC_DEV_INFO=y diff --git a/tests/subsys/nrf_rpc/dev_info/client/src/dev_info_suite.c b/tests/subsys/nrf_rpc/dev_info/client/src/dev_info_suite.c new file mode 100644 index 000000000000..c0d542ff8019 --- /dev/null +++ b/tests/subsys/nrf_rpc/dev_info/client/src/dev_info_suite.c @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2024 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause + */ + +#include "zephyr/ztest_assert.h" +#include +#include +#include + +#include +#include + +#include +#include + +static void nrf_rpc_err_handler(const struct nrf_rpc_err_report *report) +{ + zassert_ok(report->code); +} + +static void tc_setup(void *f) +{ + mock_nrf_rpc_tr_expect_add(RPC_INIT_REQ, RPC_INIT_RSP); + zassert_ok(nrf_rpc_init(nrf_rpc_err_handler)); + mock_nrf_rpc_tr_expect_reset(); +} + +ZTEST(dev_info_rpc_client, test_get_version) +{ + const char *commit = NCS_COMMIT_STRING; + const char *version = 0; + + mock_nrf_rpc_tr_expect_add(RPC_CMD(DEV_INFO_RPC_GET_VERSION), + RPC_RSP(0x6C, STRING_TO_TUPLE(NCS_COMMIT_STRING))); + version = nrf_rpc_get_ncs_commit_sha(); + mock_nrf_rpc_tr_expect_done(); + + zexpect_str_equal(commit, version); +} + +ZTEST_SUITE(dev_info_rpc_client, NULL, NULL, tc_setup, NULL, NULL); diff --git a/tests/subsys/nrf_rpc/dev_info/client/testcase.yaml b/tests/subsys/nrf_rpc/dev_info/client/testcase.yaml new file mode 100644 index 000000000000..c990714fef39 --- /dev/null +++ b/tests/subsys/nrf_rpc/dev_info/client/testcase.yaml @@ -0,0 +1,7 @@ +tests: + nrf_rpc.dev_info.client: + sysbuild: true + platform_allow: native_sim + tags: ci_build sysbuild ci_tests_subsys_nrf_rpc + integration_platforms: + - native_sim diff --git a/tests/subsys/nrf_rpc/dev_info/common/nrf_rpc_single_thread.c b/tests/subsys/nrf_rpc/dev_info/common/nrf_rpc_single_thread.c new file mode 100644 index 000000000000..04d79c7033bf --- /dev/null +++ b/tests/subsys/nrf_rpc/dev_info/common/nrf_rpc_single_thread.c @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2024 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause + */ + +/* + * Replacement implementation of selected nRF RPC OS functions, which enables single-threaded + * processing of a received nRF RPC command. + * + * Typically, an nRF RPC command that initiates a conversation is dispatched by the nRF RPC core + * using a dedicated thread pool. In unit tests, however, it is preferable to dispatch the command + * synchronously so that no operation timeouts are needed to detect a test case failure. + */ + +#include + +#include + +static nrf_rpc_os_work_t receive_callback; + +int __real_nrf_rpc_os_init(nrf_rpc_os_work_t callback); + +int __wrap_nrf_rpc_os_init(nrf_rpc_os_work_t callback) +{ + receive_callback = callback; + + return __real_nrf_rpc_os_init(callback); +} + +void __wrap_nrf_rpc_os_thread_pool_send(const uint8_t *data, size_t len) +{ + zassert_not_null(receive_callback); + + receive_callback(data, len); +} diff --git a/tests/subsys/nrf_rpc/dev_info/common/test_rpc_env.h b/tests/subsys/nrf_rpc/dev_info/common/test_rpc_env.h new file mode 100644 index 000000000000..21758c5fe24b --- /dev/null +++ b/tests/subsys/nrf_rpc/dev_info/common/test_rpc_env.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2024 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause + */ + +#include +#include + +/* Macros for constructing CBOR data items. */ + +#define CBOR_FALSE 0xf4 +#define CBOR_TRUE 0xf5 +#define CBOR_NULL 0xf6 + +#define CBOR_UINT64(value) 0x1B, BT_BYTES_LIST_LE64(BSWAP_64(value)) +#define CBOR_UINT32(value) 0x1A, BT_BYTES_LIST_LE32(BSWAP_32(value)) +#define CBOR_UINT16(value) 0x19, BT_BYTES_LIST_LE16(BSWAP_16(value)) +/* for value bigger than 0x17 */ +#define CBOR_UINT8(value) 0x18, (value) + +/* Macros for constructing nRF RPC packets for the OpenThread command group. */ + +#define RPC_PKT(bytes...) \ + (mock_nrf_rpc_pkt_t) \ + { \ + .data = (uint8_t[]){bytes}, .len = sizeof((uint8_t[]){bytes}), \ + } + +#define RPC_INIT_REQ \ + RPC_PKT(0x04, 0x00, 0xff, 0x00, 0xff, 0x00, 'd', 'e', 'v', '_', 'i', 'n', 'f', 'o') +#define RPC_INIT_RSP \ + RPC_PKT(0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 'd', 'e', 'v', '_', 'i', 'n', 'f', 'o') +#define RPC_CMD(cmd, ...) RPC_PKT(0x80, cmd, 0xff, 0x00, 0x00 __VA_OPT__(,) __VA_ARGS__, 0xf6) +#define RPC_RSP(...) RPC_PKT(0x01, 0xff, 0x00, 0x00, 0x00 __VA_OPT__(,) __VA_ARGS__, 0xf6) +#define NO_RSP RPC_PKT() + +#define STRING_TO_TUPLE(str) \ + str[0], str[1], str[2], str[3], str[4], str[5], str[6], str[7], str[8], str[9], str[10], \ + str[11] diff --git a/tests/subsys/nrf_rpc/dev_info/server/CMakeLists.txt b/tests/subsys/nrf_rpc/dev_info/server/CMakeLists.txt new file mode 100644 index 000000000000..f144cbc250a7 --- /dev/null +++ b/tests/subsys/nrf_rpc/dev_info/server/CMakeLists.txt @@ -0,0 +1,27 @@ +# +# Copyright (c) 2024 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(nrf_rpc_dev_info_server_test) + +FILE(GLOB app_sources src/*.c) + +target_include_directories(app PRIVATE + ${ZEPHYR_NRF_MODULE_DIR}/subsys/nrf_rpc/dev_info/common + ${ZEPHYR_NRF_MODULE_DIR}/subsys/nrf_rpc/dev_info/server + ../common +) + +target_sources(app PRIVATE + ${app_sources} + ../common/nrf_rpc_single_thread.c +) + +# Enforce single-threaded nRF RPC command processing. +target_link_options(app PUBLIC + -Wl,--wrap=nrf_rpc_os_init,--wrap=nrf_rpc_os_thread_pool_send +) diff --git a/tests/subsys/nrf_rpc/dev_info/server/prj.conf b/tests/subsys/nrf_rpc/dev_info/server/prj.conf new file mode 100644 index 000000000000..4a2a3d249ab8 --- /dev/null +++ b/tests/subsys/nrf_rpc/dev_info/server/prj.conf @@ -0,0 +1,20 @@ +# +# Copyright (c) 2024 Nordic Semiconductor ASA +# +# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause +# + +# Ztest configuration +CONFIG_ZTEST=y +CONFIG_TEST_RANDOM_GENERATOR=y + +CONFIG_NRF_RPC_CALLBACK_PROXY=n + +CONFIG_NRF_RPC=y +CONFIG_MOCK_NRF_RPC=y +CONFIG_MOCK_NRF_RPC_TRANSPORT=y + +CONFIG_KERNEL_MEM_POOL=y +CONFIG_HEAP_MEM_POOL_SIZE=4096 +CONFIG_NRF_RPC_DEV_INFO=y +CONFIG_NRF_RPC_DEV_INFO_SERVER=y diff --git a/tests/subsys/nrf_rpc/dev_info/server/src/dev_info_suite.c b/tests/subsys/nrf_rpc/dev_info/server/src/dev_info_suite.c new file mode 100644 index 000000000000..1ec7061f1348 --- /dev/null +++ b/tests/subsys/nrf_rpc/dev_info/server/src/dev_info_suite.c @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2024 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause + */ + +#include +#include +#include + +#include + +#include +#include + +static void nrf_rpc_err_handler(const struct nrf_rpc_err_report *report) +{ + zassert_ok(report->code); +} + +static void tc_setup(void *f) +{ + mock_nrf_rpc_tr_expect_add(RPC_INIT_REQ, RPC_INIT_RSP); + zassert_ok(nrf_rpc_init(nrf_rpc_err_handler)); + mock_nrf_rpc_tr_expect_reset(); +} + +ZTEST(dev_info_rpc_server, test_get_server_version) +{ + const char *commit = NCS_COMMIT_STRING; + + mock_nrf_rpc_tr_expect_add(RPC_RSP(0x6C, STRING_TO_TUPLE(commit)), NO_RSP); + mock_nrf_rpc_tr_receive(RPC_CMD(DEV_INFO_RPC_GET_VERSION)); + mock_nrf_rpc_tr_expect_done(); +} + +ZTEST_SUITE(dev_info_rpc_server, NULL, NULL, tc_setup, NULL, NULL); diff --git a/tests/subsys/nrf_rpc/dev_info/server/testcase.yaml b/tests/subsys/nrf_rpc/dev_info/server/testcase.yaml new file mode 100644 index 000000000000..38d3d4941cdc --- /dev/null +++ b/tests/subsys/nrf_rpc/dev_info/server/testcase.yaml @@ -0,0 +1,7 @@ +tests: + nrf_rpc.dev_info.server: + sysbuild: true + platform_allow: native_sim + tags: ci_build sysbuild ci_tests_subsys_nrf_rpc + integration_platforms: + - native_sim