Skip to content

Commit 8255c7a

Browse files
Przemyslaw Bidarlubos
authored andcommitted
nrf_rpc: Implement OT ned data APIs.
APIs implemented: - otNetDataGet - otNetDataGetNextOnMeshPrefix - otNetDataGetNextService Signed-off-by: Przemyslaw Bida <[email protected]>
1 parent f781fd5 commit 8255c7a

File tree

7 files changed

+667
-11
lines changed

7 files changed

+667
-11
lines changed

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <openthread/link.h>
1313
#include <openthread/thread.h>
1414
#include <openthread/udp.h>
15+
#include <openthread/netdata.h>
1516
#include <openthread/message.h>
1617

1718
#include <string.h>
@@ -534,6 +535,53 @@ static int cmd_test_udp_close(const struct shell *sh, size_t argc, char *argv[])
534535
return 0;
535536
}
536537

538+
static int cmd_test_net_data(const struct shell *sh, size_t argc, char *argv[])
539+
{
540+
const size_t net_data_len = 255;
541+
uint8_t buf[net_data_len];
542+
uint8_t data_len = net_data_len;
543+
544+
if (otNetDataGet(NULL, false, buf, &data_len) == OT_ERROR_NONE) {
545+
shell_print(sh, "Net dataget success.");
546+
} else {
547+
shell_print(sh, "Net dataget failed.");
548+
}
549+
550+
if (otNetDataGet(NULL, true, buf, &data_len) == OT_ERROR_NONE) {
551+
shell_print(sh, "Net dataget success.");
552+
} else {
553+
shell_print(sh, "Net dataget failed.");
554+
}
555+
556+
return 0;
557+
}
558+
559+
static int cmd_test_net_data_mesh_prefix(const struct shell *sh, size_t argc, char *argv[])
560+
{
561+
otNetworkDataIterator iterator = OT_NETWORK_DATA_ITERATOR_INIT;
562+
otBorderRouterConfig config;
563+
otError error;
564+
565+
while ((error = otNetDataGetNextOnMeshPrefix(NULL, &iterator, &config)) == OT_ERROR_NONE) {
566+
shell_print(sh, "iterator %d", iterator);
567+
}
568+
shell_print(sh, "OT error: %d", error);
569+
return 0;
570+
}
571+
572+
static int cmd_test_net_data_next_service(const struct shell *sh, size_t argc, char *argv[])
573+
{
574+
otNetworkDataIterator iterator = OT_NETWORK_DATA_ITERATOR_INIT;
575+
otServiceConfig config;
576+
otError error;
577+
578+
while ((error = otNetDataGetNextService(NULL, &iterator, &config)) == OT_ERROR_NONE) {
579+
shell_print(sh, "iterator %d", iterator);
580+
}
581+
shell_print(sh, "OT error: %d", error);
582+
return 0;
583+
}
584+
537585
static int cmd_ot(const struct shell *sh, size_t argc, char *argv[])
538586
{
539587
return ot_cli_command_send(sh, argc, argv);
@@ -552,6 +600,11 @@ SHELL_STATIC_SUBCMD_SET_CREATE(
552600
SHELL_CMD_ARG(test_udp_init, NULL, "Test udp init API", cmd_test_udp_init, 1, 0),
553601
SHELL_CMD_ARG(test_udp_send, NULL, "Test udp send API", cmd_test_udp_send, 1, 0),
554602
SHELL_CMD_ARG(test_udp_close, NULL, "Test udp close API", cmd_test_udp_close, 1, 0),
603+
SHELL_CMD_ARG(test_net_data, NULL, "Test netdata API", cmd_test_net_data, 1, 0),
604+
SHELL_CMD_ARG(test_net_data_mesh_prefix, NULL, "Test netdata msh prefix API",
605+
cmd_test_net_data_mesh_prefix, 1, 0),
606+
SHELL_CMD_ARG(test_net_data_next_service, NULL, "Test netdata next service API",
607+
cmd_test_net_data_next_service, 1, 0),
555608
SHELL_SUBCMD_SET_END);
556609

557610
SHELL_CMD_ARG_REGISTER(ot, &ot_cmds,

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

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@
44
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
55
*/
66

7+
#include "zcbor_common.h"
8+
#include "zcbor_decode.h"
9+
#include "zcbor_encode.h"
710
#include <ot_rpc_ids.h>
811
#include <ot_rpc_types.h>
912
#include <ot_rpc_common.h>
1013

1114
#include <nrf_rpc_cbor.h>
1215

16+
#include <openthread/netdata.h>
1317
#include <openthread/ip6.h>
1418
#include <openthread/link.h>
1519
#include <openthread/thread.h>
@@ -481,3 +485,164 @@ void otLinkGetFactoryAssignedIeeeEui64(otInstance *aInstance, otExtAddress *aEui
481485
nrf_rpc_cbor_cmd_no_err(&ot_group, OT_RPC_CMD_LINK_GET_FACTORY_ASSIGNED_EUI64, &ctx,
482486
ot_rpc_decode_eui64, aEui64->m8);
483487
}
488+
489+
otError otNetDataGet(otInstance *aInstance, bool aStable, uint8_t *aData, uint8_t *aDataLength)
490+
{
491+
struct zcbor_string zstr;
492+
struct nrf_rpc_cbor_ctx ctx;
493+
otError error = OT_ERROR_NONE;
494+
bool decoded_ok = true;
495+
496+
NRF_RPC_CBOR_ALLOC(&ot_group, ctx, 5);
497+
498+
if (!zcbor_bool_put(ctx.zs, aStable)) {
499+
NRF_RPC_CBOR_DISCARD(&ot_group, ctx);
500+
error = OT_ERROR_INVALID_ARGS;
501+
goto exit;
502+
}
503+
504+
if (!zcbor_uint_encode(ctx.zs, aDataLength, sizeof(*aDataLength))) {
505+
NRF_RPC_CBOR_DISCARD(&ot_group, ctx);
506+
error = OT_ERROR_INVALID_ARGS;
507+
goto exit;
508+
}
509+
510+
nrf_rpc_cbor_cmd_rsp_no_err(&ot_group, OT_RPC_CMD_NETDATA_GET, &ctx);
511+
512+
decoded_ok = zcbor_nil_expect(ctx.zs, NULL);
513+
514+
if (decoded_ok) {
515+
*aDataLength = 0;
516+
goto decode_error;
517+
}
518+
519+
if (ctx.zs->constant_state->error != ZCBOR_ERR_WRONG_TYPE) {
520+
decoded_ok = false;
521+
error = OT_ERROR_FAILED;
522+
goto exit;
523+
}
524+
525+
zcbor_pop_error(ctx.zs);
526+
527+
decoded_ok = zcbor_bstr_decode(ctx.zs, &zstr);
528+
529+
if (!decoded_ok) {
530+
error = OT_ERROR_FAILED;
531+
goto exit;
532+
}
533+
534+
memcpy(aData, zstr.value, (zstr.len < *aDataLength ? zstr.len : *aDataLength));
535+
536+
decode_error:
537+
if (!zcbor_uint_decode(ctx.zs, &error, sizeof(error))) {
538+
error = OT_ERROR_FAILED;
539+
}
540+
541+
nrf_rpc_cbor_decoding_done(&ot_group, &ctx);
542+
543+
exit:
544+
if (!decoded_ok) {
545+
nrf_rpc_cbor_decoding_done(&ot_group, &ctx);
546+
ot_rpc_report_decoding_error(OT_RPC_CMD_NETDATA_GET);
547+
}
548+
549+
return error;
550+
}
551+
552+
otError otNetDataGetNextService(otInstance *aInstance, otNetworkDataIterator *aIterator,
553+
otServiceConfig *aConfig)
554+
{
555+
struct nrf_rpc_cbor_ctx ctx;
556+
otError error;
557+
bool decoded_ok = true;
558+
559+
NRF_RPC_CBOR_ALLOC(&ot_group, ctx, sizeof(*aIterator) + 1);
560+
561+
if (!zcbor_uint_encode(ctx.zs, aIterator, sizeof(*aIterator))) {
562+
NRF_RPC_CBOR_DISCARD(&ot_group, ctx);
563+
error = OT_ERROR_INVALID_ARGS;
564+
goto exit;
565+
}
566+
567+
nrf_rpc_cbor_cmd_rsp_no_err(&ot_group, OT_RPC_CMD_NETDATA_GET_NEXT_SERVICE, &ctx);
568+
569+
decoded_ok = zcbor_uint_decode(ctx.zs, aIterator, sizeof(*aIterator));
570+
571+
if (!decoded_ok) {
572+
error = OT_ERROR_FAILED;
573+
goto exit;
574+
}
575+
576+
decoded_ok = ot_rpc_decode_service_config(&ctx, aConfig);
577+
578+
if (!decoded_ok) {
579+
error = OT_ERROR_FAILED;
580+
goto exit;
581+
}
582+
583+
decoded_ok = zcbor_uint_decode(ctx.zs, &error, sizeof(error));
584+
585+
if (!decoded_ok) {
586+
error = OT_ERROR_FAILED;
587+
goto exit;
588+
}
589+
590+
nrf_rpc_cbor_decoding_done(&ot_group, &ctx);
591+
592+
exit:
593+
if (!decoded_ok) {
594+
nrf_rpc_cbor_decoding_done(&ot_group, &ctx);
595+
ot_rpc_report_decoding_error(OT_RPC_CMD_NETDATA_GET_NEXT_SERVICE);
596+
}
597+
598+
return error;
599+
}
600+
601+
otError otNetDataGetNextOnMeshPrefix(otInstance *aInstance, otNetworkDataIterator *aIterator,
602+
otBorderRouterConfig *aConfig)
603+
{
604+
struct nrf_rpc_cbor_ctx ctx;
605+
otError error = OT_ERROR_NONE;
606+
bool decoded_ok = true;
607+
608+
NRF_RPC_CBOR_ALLOC(&ot_group, ctx, sizeof(*aIterator) + 1);
609+
610+
if (!zcbor_uint_encode(ctx.zs, aIterator, sizeof(*aIterator))) {
611+
NRF_RPC_CBOR_DISCARD(&ot_group, ctx);
612+
error = OT_ERROR_INVALID_ARGS;
613+
goto exit;
614+
}
615+
616+
nrf_rpc_cbor_cmd_rsp_no_err(&ot_group, OT_RPC_CMD_NETDATA_GET_NEXT_ON_MESH_PREFIX, &ctx);
617+
618+
decoded_ok = zcbor_uint_decode(ctx.zs, aIterator, sizeof(*aIterator));
619+
620+
if (!decoded_ok) {
621+
error = OT_ERROR_FAILED;
622+
goto exit;
623+
}
624+
625+
decoded_ok = ot_rpc_decode_border_router_config(&ctx, aConfig);
626+
627+
if (!decoded_ok) {
628+
error = OT_ERROR_FAILED;
629+
goto exit;
630+
}
631+
632+
decoded_ok = zcbor_uint_decode(ctx.zs, &error, sizeof(error));
633+
634+
if (!decoded_ok) {
635+
error = OT_ERROR_FAILED;
636+
goto exit;
637+
}
638+
639+
nrf_rpc_cbor_decoding_done(&ot_group, &ctx);
640+
641+
exit:
642+
if (!decoded_ok) {
643+
ot_rpc_report_decoding_error(OT_RPC_CMD_NETDATA_GET_NEXT_ON_MESH_PREFIX);
644+
nrf_rpc_cbor_decoding_done(&ot_group, &ctx);
645+
}
646+
647+
return error;
648+
}

0 commit comments

Comments
 (0)