Skip to content

Commit 59ad79d

Browse files
Damian-Nordicrlubos
authored andcommitted
tests: net: openthread: rpc: initial server unit tests
1. Extend mock nRF RPC transport to support simulated reception of an nRF RPC packet. 2. Add initial unit tests for the OpenThread RPC server. This includes mocking Zephyr OpenThread L2 functions and certain nRF RPC APIs to support single-threaded mode of operation. Signed-off-by: Damian Krolik <[email protected]>
1 parent 921503e commit 59ad79d

File tree

11 files changed

+651
-24
lines changed

11 files changed

+651
-24
lines changed

subsys/net/openthread/rpc/Kconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ config OPENTHREAD_RPC_CLIENT
3131

3232
config OPENTHREAD_RPC_SERVER
3333
bool "OpenThread over RPC server"
34-
depends on NET_L2_OPENTHREAD
34+
depends on NET_L2_OPENTHREAD || ZTEST
3535
help
3636
Enables OpenThread over RPC server role that runs the full OpenThread
3737
stack and exposes OpenThread functions using nRF RPC library to a client

subsys/net/openthread/rpc/client/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,4 @@ zephyr_library_include_directories(
1717
${CMAKE_CURRENT_SOURCE_DIR}/../common
1818
)
1919

20-
# TODO: add dependency on NET_L2_OPENTHREAD and remove the code below
2120
zephyr_include_directories(${ZEPHYR_OPENTHREAD_MODULE_DIR}/include)

subsys/net/openthread/rpc/server/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ zephyr_library()
99
zephyr_library_sources(
1010
ot_rpc_cli_server.c
1111
ot_rpc_ctrl_server.c
12+
)
13+
14+
zephyr_library_sources_ifdef(CONFIG_NET_L2_OPENTHREAD
1215
ot_rpc_if_server.c
1316
ot_rpc_commissioning_server.c
1417
)
1518

1619
zephyr_library_include_directories(
1720
${CMAKE_CURRENT_SOURCE_DIR}/../common
1821
)
19-
20-
# TODO: add dependency on NET_L2_OPENTHREAD and remove the code below
21-
zephyr_include_directories(${ZEPHYR_OPENTHREAD_MODULE_DIR}/include)

tests/include/mock_nrf_rpc_transport.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ void mock_nrf_rpc_tr_expect_done(void);
5757
* @brief Clears the list of expected nRF RPC packets.
5858
*/
5959
void mock_nrf_rpc_tr_expect_reset(void);
60+
61+
/**
62+
* @brief Simulates an nRF RPC packet reception from the remote node.
63+
*/
64+
void mock_nrf_rpc_tr_receive(mock_nrf_rpc_pkt_t packet);
65+
6066
/**
6167
* @}
6268
*/

tests/mocks/nrf_rpc/mock_nrf_rpc_transport.c

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,33 @@ typedef struct mock_nrf_rpc_tr_ctx {
2828
struct k_work response_work;
2929
} mock_nrf_rpc_tr_ctx_t;
3030

31+
static void log_payload(const char *caption, const uint8_t *payload, size_t length)
32+
{
33+
char payload_str[32];
34+
35+
if (!bin2hex(payload, length, payload_str, sizeof(payload_str))) {
36+
/*
37+
* If bin2hex() returns 0, this means that the payload converted to HEX is too
38+
* large to fit within 'payload_str'. In such a case, log as long prefix of the
39+
* payload as possible, followed by "...". The code below calculates the prefix
40+
* length.
41+
*/
42+
length = (sizeof(payload_str) - sizeof("...")) / 2;
43+
bin2hex(payload, length, payload_str, sizeof(payload_str));
44+
strcat(payload_str, "...");
45+
}
46+
47+
printk("%s: %s of length %zu\n", caption, payload_str, length);
48+
}
49+
3150
/* Asynchronous task to simulate reception of a response packet. */
3251
static void response_send(struct k_work *work)
3352
{
3453
mock_nrf_rpc_tr_ctx_t *ctx = CONTAINER_OF(work, mock_nrf_rpc_tr_ctx_t, response_work);
3554
mock_nrf_rpc_pkt_t *response = ctx->cur_response;
3655

56+
log_payload("Responding with nRF RPC packet", response->data, response->len);
57+
3758
ctx->receive_cb(ctx->transport, response->data, response->len, ctx->receive_ctx);
3859
}
3960

@@ -51,25 +72,6 @@ static int init(const struct nrf_rpc_tr *transport, nrf_rpc_tr_receive_handler_t
5172
return 0;
5273
}
5374

54-
static void log_payload(const char *caption, const uint8_t *payload, size_t length)
55-
{
56-
char payload_str[32];
57-
58-
if (!bin2hex(payload, length, payload_str, sizeof(payload_str))) {
59-
/*
60-
* If bin2hex() returns 0, this means that the payload converted to HEX is too
61-
* large to fit within 'payload_str'. In such a case, log as long prefix of the
62-
* payload as possible, followed by "...". The code below calculates the prefix
63-
* length.
64-
*/
65-
length = (sizeof(payload_str) - sizeof("...")) / 2;
66-
bin2hex(payload, length, payload_str, sizeof(payload_str));
67-
strcat(payload_str, "...");
68-
}
69-
70-
printk("%s: %s of length %zu\n", caption, payload_str, length);
71-
}
72-
7375
static int send(const struct nrf_rpc_tr *transport, const uint8_t *data, size_t length)
7476
{
7577
mock_nrf_rpc_tr_ctx_t *ctx = transport->ctx;
@@ -158,3 +160,14 @@ void mock_nrf_rpc_tr_expect_reset(void)
158160
ctx->num_expected = 0;
159161
ctx->cur_expected = 0;
160162
}
163+
164+
void mock_nrf_rpc_tr_receive(mock_nrf_rpc_pkt_t packet)
165+
{
166+
mock_nrf_rpc_tr_ctx_t *ctx = mock_nrf_rpc_tr.ctx;
167+
168+
zassert_not_null(ctx->receive_cb);
169+
170+
log_payload("Received nRF RPC packet", packet.data, packet.len);
171+
172+
ctx->receive_cb(ctx->transport, packet.data, packet.len, ctx->receive_ctx);
173+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#
2+
# Copyright (c) 2024 Nordic Semiconductor ASA
3+
#
4+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
#
6+
cmake_minimum_required(VERSION 3.20.0)
7+
8+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
9+
project(ot_rpc_server_test)
10+
11+
FILE(GLOB app_sources src/*.c)
12+
13+
target_include_directories(app PRIVATE
14+
# Needed to access OpenThread RPC command IDs.
15+
${ZEPHYR_NRF_MODULE_DIR}/subsys/net/openthread/rpc/common
16+
)
17+
18+
target_sources(app PRIVATE
19+
${app_sources}
20+
)
21+
22+
# Fill the gaps due to not setting NET_L2_OPENTHREAD.
23+
zephyr_include_directories(
24+
${ZEPHYR_OPENTHREAD_MODULE_DIR}/include
25+
)
26+
27+
zephyr_compile_definitions(
28+
CONFIG_OPENTHREAD_PKT_LIST_SIZE=1
29+
)
30+
31+
# Enforce single-threaded nRF RPC command processing.
32+
target_link_options(app PUBLIC
33+
-Wl,--wrap=nrf_rpc_os_init,--wrap=nrf_rpc_os_thread_pool_send
34+
)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#
2+
# Copyright (c) 2024 Nordic Semiconductor ASA
3+
#
4+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
#
6+
7+
# Ztest configuration
8+
CONFIG_ZTEST=y
9+
CONFIG_TEST_RANDOM_GENERATOR=y
10+
11+
CONFIG_OPENTHREAD_RPC=y
12+
CONFIG_OPENTHREAD_RPC_SERVER=y
13+
CONFIG_NRF_RPC_CBKPROXY_OUT_SLOTS=0
14+
15+
CONFIG_MOCK_NRF_RPC=y
16+
CONFIG_MOCK_NRF_RPC_TRANSPORT=y
17+
18+
CONFIG_KERNEL_MEM_POOL=y
19+
CONFIG_HEAP_MEM_POOL_SIZE=4096

0 commit comments

Comments
 (0)