Skip to content

Commit 7f61671

Browse files
Damian-Nordicrlubos
authored andcommitted
net: openthread: rpc: add otUdpIsOpen
Add serialization of otUdpIsOpen() API. Signed-off-by: Damian Krolik <[email protected]>
1 parent abb2790 commit 7f61671

File tree

7 files changed

+147
-4
lines changed

7 files changed

+147
-4
lines changed

samples/nrf_rpc/protocols_serialization/client/src/ot_shell.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,11 @@ static int cmd_test_udp_init(const struct shell *sh, size_t argc, char *argv[])
532532
otSockAddr listen_sock_addr;
533533
otError error;
534534

535+
if (otUdpIsOpen(NULL, &udp_socket)) {
536+
shell_info(sh, "Skipping initialization: socket already open");
537+
return 0;
538+
}
539+
535540
memset(&udp_socket, 0, sizeof(udp_socket));
536541
memset(&listen_sock_addr, 0, sizeof(listen_sock_addr));
537542

subsys/net/openthread/rpc/client/ot_rpc_udp.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ otError otUdpOpen(otInstance *aInstance, otUdpSocket *aSocket, otUdpReceive aCal
135135
aSocket->mContext = aContext;
136136
aSocket->mHandler = aCallback;
137137

138-
NRF_RPC_CBOR_ALLOC(&ot_group, ctx, sizeof(key) + 2);
138+
NRF_RPC_CBOR_ALLOC(&ot_group, ctx, sizeof(key) + 1);
139139
nrf_rpc_encode_uint(&ctx, key);
140140
nrf_rpc_cbor_cmd_no_err(&ot_group, OT_RPC_CMD_UDP_OPEN, &ctx, ot_rpc_decode_error, &error);
141141

@@ -164,3 +164,25 @@ otError otUdpSend(otInstance *aInstance, otUdpSocket *aSocket, otMessage *aMessa
164164

165165
return error;
166166
}
167+
168+
bool otUdpIsOpen(otInstance *aInstance, const otUdpSocket *aSocket)
169+
{
170+
struct nrf_rpc_cbor_ctx ctx;
171+
ot_socket_key key = (ot_socket_key)aSocket;
172+
bool open;
173+
174+
ARG_UNUSED(aInstance);
175+
__ASSERT_NO_MSG(aSocket != NULL);
176+
177+
NRF_RPC_CBOR_ALLOC(&ot_group, ctx, sizeof(key) + 1);
178+
nrf_rpc_encode_uint(&ctx, key);
179+
nrf_rpc_cbor_cmd_rsp_no_err(&ot_group, OT_RPC_CMD_UDP_IS_OPEN, &ctx);
180+
181+
open = nrf_rpc_decode_bool(&ctx);
182+
183+
if (!nrf_rpc_decoding_done_and_check(&ot_group, &ctx)) {
184+
ot_rpc_report_rsp_decoding_error(OT_RPC_CMD_UDP_IS_OPEN);
185+
}
186+
187+
return open;
188+
}

subsys/net/openthread/rpc/common/ot_rpc_ids.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ enum ot_rpc_cmd_server {
106106
OT_RPC_CMD_MESSAGE_READ,
107107
OT_RPC_CMD_MESSAGE_GET_THREAD_LINK_INFO,
108108
OT_RPC_CMD_UDP_OPEN,
109+
OT_RPC_CMD_UDP_IS_OPEN,
109110
OT_RPC_CMD_UDP_SEND,
110111
OT_RPC_CMD_UDP_BIND,
111112
OT_RPC_CMD_UDP_CLOSE,

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ zephyr_library_sources(
2121
ot_rpc_netdiag.c
2222
ot_rpc_srp_client.c
2323
ot_rpc_thread.c
24-
)
25-
26-
zephyr_library_sources_ifdef(CONFIG_NET_L2_OPENTHREAD
2724
ot_rpc_udp.c
2825
)
2926

subsys/net/openthread/rpc/server/ot_rpc_udp.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,13 +279,41 @@ static void ot_rpc_udp_connect(const struct nrf_rpc_group *group, struct nrf_rpc
279279
nrf_rpc_cbor_rsp_no_err(group, &rsp_ctx);
280280
}
281281

282+
static void ot_rpc_udp_is_open(const struct nrf_rpc_group *group, struct nrf_rpc_cbor_ctx *ctx,
283+
void *handler_data)
284+
{
285+
bool open = false;
286+
ot_socket_key soc_key;
287+
nrf_udp_socket *socket;
288+
289+
soc_key = nrf_rpc_decode_uint(ctx);
290+
291+
if (!nrf_rpc_decoding_done_and_check(group, ctx)) {
292+
ot_rpc_report_cmd_decoding_error(OT_RPC_CMD_UDP_IS_OPEN);
293+
return;
294+
}
295+
296+
ot_rpc_mutex_lock();
297+
socket = nrf_udp_find_socket(soc_key);
298+
299+
if (socket) {
300+
open = otUdpIsOpen(openthread_get_default_instance(), &socket->mSocket);
301+
}
302+
303+
ot_rpc_mutex_unlock();
304+
nrf_rpc_rsp_send_bool(group, open);
305+
}
306+
282307
NRF_RPC_CBOR_CMD_DECODER(ot_group, ot_rpc_udp_bind, OT_RPC_CMD_UDP_BIND, ot_rpc_udp_bind, NULL);
283308

284309
NRF_RPC_CBOR_CMD_DECODER(ot_group, ot_rpc_udp_close, OT_RPC_CMD_UDP_CLOSE, ot_rpc_udp_close, NULL);
285310

286311
NRF_RPC_CBOR_CMD_DECODER(ot_group, ot_rpc_udp_connect, OT_RPC_CMD_UDP_CONNECT, ot_rpc_udp_connect,
287312
NULL);
288313

314+
NRF_RPC_CBOR_CMD_DECODER(ot_group, ot_rpc_udp_is_open, OT_RPC_CMD_UDP_IS_OPEN, ot_rpc_udp_is_open,
315+
NULL);
316+
289317
NRF_RPC_CBOR_CMD_DECODER(ot_group, ot_rpc_udp_open, OT_RPC_CMD_UDP_OPEN, ot_rpc_udp_open, NULL);
290318

291319
NRF_RPC_CBOR_CMD_DECODER(ot_group, ot_rpc_udp_send, OT_RPC_CMD_UDP_SEND, ot_rpc_udp_send, NULL);

tests/subsys/net/openthread/rpc/client/src/udp_suite.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,4 +243,32 @@ ZTEST(ot_rpc_udp, test_udp_receive)
243243
zassert_equal(handle_udp_receive_fake.arg1_val, (otMessage *)1);
244244
}
245245

246+
ZTEST(ot_rpc_udp, test_otUdpIsOpen_true)
247+
{
248+
bool open;
249+
otUdpSocket socket;
250+
251+
mock_nrf_rpc_tr_expect_add(
252+
RPC_CMD(OT_RPC_CMD_UDP_IS_OPEN, CBOR_UINT32((ot_socket_key)&socket)),
253+
RPC_RSP(CBOR_TRUE));
254+
open = otUdpIsOpen(NULL, &socket);
255+
mock_nrf_rpc_tr_expect_done();
256+
257+
zassert_true(open);
258+
}
259+
260+
ZTEST(ot_rpc_udp, test_otUdpIsOpen_false)
261+
{
262+
bool open;
263+
otUdpSocket socket;
264+
265+
mock_nrf_rpc_tr_expect_add(
266+
RPC_CMD(OT_RPC_CMD_UDP_IS_OPEN, CBOR_UINT32((ot_socket_key)&socket)),
267+
RPC_RSP(CBOR_FALSE));
268+
open = otUdpIsOpen(NULL, &socket);
269+
mock_nrf_rpc_tr_expect_done();
270+
271+
zassert_false(open);
272+
}
273+
246274
ZTEST_SUITE(ot_rpc_udp, NULL, NULL, tc_setup, tc_clean, NULL);
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright (c) 2024 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
*/
6+
7+
#include <mock_nrf_rpc_transport.h>
8+
#include <ot_rpc_ids.h>
9+
#include <test_rpc_env.h>
10+
11+
#include <zephyr/fff.h>
12+
#include <zephyr/kernel.h>
13+
#include <zephyr/ztest.h>
14+
15+
#include <openthread/udp.h>
16+
17+
#define FOREACH_FAKE(f) \
18+
f(otUdpIsOpen); \
19+
f(otUdpOpen); \
20+
f(otUdpClose); \
21+
f(otUdpBind); \
22+
f(otUdpConnect); \
23+
f(otUdpSend)
24+
25+
FAKE_VALUE_FUNC(bool, otUdpIsOpen, otInstance *, const otUdpSocket *);
26+
FAKE_VALUE_FUNC(otError, otUdpOpen, otInstance *, otUdpSocket *, otUdpReceive, void *);
27+
FAKE_VALUE_FUNC(otError, otUdpClose, otInstance *, otUdpSocket *);
28+
FAKE_VALUE_FUNC(otError, otUdpBind, otInstance *, otUdpSocket *, const otSockAddr *,
29+
otNetifIdentifier);
30+
FAKE_VALUE_FUNC(otError, otUdpConnect, otInstance *, otUdpSocket *, const otSockAddr *);
31+
FAKE_VALUE_FUNC(otError, otUdpSend, otInstance *, otUdpSocket *, otMessage *,
32+
const otMessageInfo *);
33+
34+
static void nrf_rpc_err_handler(const struct nrf_rpc_err_report *report)
35+
{
36+
zassert_ok(report->code);
37+
}
38+
39+
static void tc_setup(void *f)
40+
{
41+
mock_nrf_rpc_tr_expect_add(RPC_INIT_REQ, RPC_INIT_RSP);
42+
zassert_ok(nrf_rpc_init(nrf_rpc_err_handler));
43+
mock_nrf_rpc_tr_expect_reset();
44+
45+
FOREACH_FAKE(RESET_FAKE);
46+
FFF_RESET_HISTORY();
47+
}
48+
49+
/*
50+
* Test reception of otUdpIsOpen() command with non-existent socket key.
51+
* Test serialization of the result: false.
52+
*/
53+
ZTEST(ot_rpc_udp, test_otUdpIsOpen_false)
54+
{
55+
mock_nrf_rpc_tr_expect_add(RPC_RSP(CBOR_FALSE), NO_RSP);
56+
mock_nrf_rpc_tr_receive(RPC_CMD(OT_RPC_CMD_UDP_IS_OPEN, CBOR_UINT32(1)));
57+
mock_nrf_rpc_tr_expect_done();
58+
59+
zassert_equal(otUdpIsOpen_fake.call_count, 0);
60+
}
61+
62+
ZTEST_SUITE(ot_rpc_udp, NULL, NULL, tc_setup, NULL, NULL);

0 commit comments

Comments
 (0)