Skip to content

Commit 5de9a06

Browse files
Przemyslaw Bidarlubos
authored andcommitted
net: openthread: rpc: diagnostic functions implementation.
Implementation of RCP diagnostic api set. Signed-off-by: Przemyslaw Bida <[email protected]>
1 parent 1fcec48 commit 5de9a06

File tree

7 files changed

+390
-37
lines changed

7 files changed

+390
-37
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ zephyr_library_sources(
1111
ot_rpc_cli_client.c
1212
ot_rpc_ctrl_client.c
1313
ot_rpc_commissioning_client.c
14+
ot_rpc_diag_client.c
1415
)
1516

1617
zephyr_library_include_directories(
Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
/*
2+
* Copyright (c) 2024 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
*/
6+
7+
#include <ot_rpc_ids.h>
8+
#include <ot_rpc_common.h>
9+
10+
#include <nrf_rpc_cbor.h>
11+
12+
#include <openthread/error.h>
13+
#include <openthread/dataset.h>
14+
#include <openthread/instance.h>
15+
#include <openthread/link.h>
16+
#include <openthread/thread.h>
17+
#include <zephyr/logging/log.h>
18+
19+
NRF_RPC_GROUP_DECLARE(ot_group);
20+
LOG_MODULE_DECLARE(ot_rpc, LOG_LEVEL_DBG);
21+
22+
static char version[256];
23+
static otExtAddress mac = {.m8 = {0xF4, 0xCE, 0x36, 0x24, 0x95, 0x6A, 0xD1, 0xD0}};
24+
static otExtendedPanId ext_pan_id;
25+
static char thread_network_name[OT_NETWORK_NAME_MAX_SIZE + 1];
26+
static otMeshLocalPrefix mesh_prefix;
27+
28+
static void get_uint_t(int id, void *result, size_t result_size)
29+
{
30+
const size_t cbor_buffer_size = 0;
31+
struct nrf_rpc_cbor_ctx ctx;
32+
33+
NRF_RPC_CBOR_ALLOC(&ot_group, ctx, cbor_buffer_size);
34+
35+
nrf_rpc_cbor_cmd_rsp_no_err(&ot_group, id, &ctx);
36+
37+
if (!zcbor_uint_decode(ctx.zs, result, result_size)) {
38+
nrf_rpc_cbor_decoding_done(&ot_group, &ctx);
39+
ot_rpc_report_decoding_error(id);
40+
return;
41+
}
42+
43+
nrf_rpc_cbor_decoding_done(&ot_group, &ctx);
44+
}
45+
46+
static int get_string(int id, char *buffer, size_t buffer_size)
47+
{
48+
const size_t cbor_buffer_size = 0;
49+
size_t bytes_copied = 0;
50+
struct zcbor_string zst;
51+
struct nrf_rpc_cbor_ctx ctx;
52+
53+
NRF_RPC_CBOR_ALLOC(&ot_group, ctx, cbor_buffer_size);
54+
55+
nrf_rpc_cbor_cmd_rsp_no_err(&ot_group, id, &ctx);
56+
57+
if (!zcbor_bstr_decode(ctx.zs, &zst)) {
58+
nrf_rpc_cbor_decoding_done(&ot_group, &ctx);
59+
ot_rpc_report_decoding_error(id);
60+
goto exit;
61+
}
62+
63+
if (buffer_size >= zst.len) {
64+
bytes_copied = zst.len;
65+
} else {
66+
bytes_copied = 0;
67+
nrf_rpc_cbor_decoding_done(&ot_group, &ctx);
68+
ot_rpc_report_decoding_error(id);
69+
goto exit;
70+
}
71+
72+
memcpy(buffer, zst.value, bytes_copied);
73+
74+
nrf_rpc_cbor_decoding_done(&ot_group, &ctx);
75+
76+
exit:
77+
return bytes_copied;
78+
}
79+
80+
const char *otGetVersionString(void)
81+
{
82+
get_string(OT_RPC_CMD_GET_VERSION_STRING, version, sizeof(version));
83+
84+
return version;
85+
}
86+
87+
otError otDatasetGetActive(otInstance *aInstance, otOperationalDataset *aDataset)
88+
{
89+
return 0;
90+
}
91+
92+
uint8_t otLinkGetChannel(otInstance *aInstance)
93+
{
94+
uint8_t ret;
95+
96+
get_uint_t(OT_RPC_CMD_LINK_GET_CHANNEL, &ret, sizeof(ret));
97+
98+
return ret;
99+
}
100+
101+
const otMacCounters *otLinkGetCounters(otInstance *aInstance)
102+
{
103+
return 0;
104+
}
105+
106+
const otExtAddress *otLinkGetExtendedAddress(otInstance *aInstance)
107+
{
108+
int ret_size = get_string(OT_RPC_CMD_LINK_GET_EXTENDED_ADDRESS, mac.m8, sizeof(mac.m8));
109+
110+
if (ret_size != sizeof(mac.m8)) {
111+
LOG_ERR("Received mac addr size is too short");
112+
ot_rpc_report_decoding_error(OT_RPC_CMD_LINK_GET_EXTENDED_ADDRESS);
113+
}
114+
115+
return &mac;
116+
}
117+
118+
otPanId otLinkGetPanId(otInstance *aInstance)
119+
{
120+
otPanId ret;
121+
122+
get_uint_t(OT_RPC_CMD_LINK_GET_PAN_ID, &ret, sizeof(ret));
123+
124+
return ret;
125+
}
126+
127+
uint8_t otNetDataGetStableVersion(otInstance *aInstance)
128+
{
129+
uint8_t ret;
130+
131+
get_uint_t(OT_RPC_CMD_NET_DATA_GET_STABLE_VERSION, &ret, sizeof(ret));
132+
133+
return ret;
134+
}
135+
136+
uint8_t otNetDataGetVersion(otInstance *aInstance)
137+
{
138+
uint8_t ret;
139+
140+
get_uint_t(OT_RPC_CMD_NET_DATA_GET_VERSION, &ret, sizeof(ret));
141+
142+
return ret;
143+
}
144+
145+
const otExtendedPanId *otThreadGetExtendedPanId(otInstance *aInstance)
146+
{
147+
get_string(OT_RPC_CMD_THREAD_GET_EXTENDED_PANID, ext_pan_id.m8, sizeof(ext_pan_id.m8));
148+
149+
return &ext_pan_id;
150+
}
151+
152+
uint8_t otThreadGetLeaderRouterId(otInstance *aInstance)
153+
{
154+
uint8_t ret;
155+
156+
get_uint_t(OT_RPC_CMD_THREAD_GET_LEADER_ROUTER_ID, &ret, sizeof(ret));
157+
158+
return ret;
159+
}
160+
161+
uint8_t otThreadGetLeaderWeight(otInstance *aInstance)
162+
{
163+
uint8_t ret;
164+
165+
get_uint_t(OT_RPC_CMD_THREAD_GET_LEADER_WEIGHT, &ret, sizeof(ret));
166+
167+
return ret;
168+
}
169+
170+
const otMeshLocalPrefix *otThreadGetMeshLocalPrefix(otInstance *aInstance)
171+
{
172+
get_string(OT_RPC_CMD_THREAD_GET_MESH_LOCAL_PREFIX, mesh_prefix.m8, sizeof(mesh_prefix));
173+
174+
return &mesh_prefix;
175+
}
176+
177+
const otMleCounters *otThreadGetMleCounters(otInstance *aInstance)
178+
{
179+
return 0;
180+
}
181+
182+
const char *otThreadGetNetworkName(otInstance *aInstance)
183+
{
184+
get_string(OT_RPC_CMD_THREAD_GET_NETWORK_NAME, thread_network_name,
185+
sizeof(thread_network_name));
186+
187+
return thread_network_name;
188+
}
189+
190+
otError otThreadGetParentInfo(otInstance *aInstance, otRouterInfo *aParentInfo)
191+
{
192+
return 0;
193+
}
194+
195+
uint32_t otThreadGetPartitionId(otInstance *aInstance)
196+
{
197+
uint32_t ret;
198+
199+
get_uint_t(OT_RPC_CMD_THREAD_GET_PARTITION_ID, &ret, sizeof(ret));
200+
201+
return ret;
202+
}

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

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <nrf_rpc_cbor.h>
1111

1212
#include <openthread/ip6.h>
13+
#include <openthread/link.h>
1314

1415
#include <zephyr/net/net_l2.h>
1516
#include <zephyr/net/net_if.h>
@@ -21,8 +22,6 @@ LOG_MODULE_DECLARE(ot_rpc, LOG_LEVEL_DBG);
2122
struct ot_rpc_l2_data {
2223
};
2324

24-
static uint8_t mac[8];
25-
2625
static enum net_verdict ot_rpc_l2_recv(struct net_if *iface, struct net_pkt *pkt)
2726
{
2827
ARG_UNUSED(iface);
@@ -160,26 +159,15 @@ static int ot_rpc_l2_enable(struct net_if *iface, bool state)
160159
{
161160
const size_t cbor_buffer_size = 1;
162161
struct nrf_rpc_cbor_ctx ctx;
163-
struct zcbor_string zst;
164162
int error = 0;
165163

164+
const otExtAddress *mac = otLinkGetExtendedAddress(0);
165+
166166
if (!state) {
167167
otRemoveStateChangeCallback(NULL, ot_state_changed_handler, iface);
168168
}
169169

170-
net_if_set_link_addr(iface, mac, 8, NET_LINK_IEEE802154);
171-
172-
NRF_RPC_CBOR_ALLOC(&ot_group, ctx, 0);
173-
nrf_rpc_cbor_cmd_rsp(&ot_group, OT_RPC_CMD_IF_EXTADDR, &ctx);
174-
175-
if (!zcbor_bstr_decode(ctx.zs, &zst)) {
176-
error = -EINVAL;
177-
nrf_rpc_cbor_decoding_done(&ot_group, &ctx);
178-
goto exit;
179-
}
180-
181-
memcpy(mac, zst.value, sizeof(mac) < zst.len ? sizeof(mac) : zst.len);
182-
nrf_rpc_cbor_decoding_done(&ot_group, &ctx);
170+
net_if_set_link_addr(iface, (uint8_t *)mac->m8, 8, NET_LINK_IEEE802154);
183171

184172
NRF_RPC_CBOR_ALLOC(&ot_group, ctx, cbor_buffer_size);
185173
zcbor_bool_put(ctx.zs, state);
@@ -190,7 +178,6 @@ static int ot_rpc_l2_enable(struct net_if *iface, bool state)
190178
otSetStateChangedCallback(NULL, ot_state_changed_handler, iface);
191179
}
192180

193-
exit:
194181
return error;
195182
}
196183

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,21 @@ enum ot_rpc_cmd_server {
3939
OT_RPC_CMD_DATASET_IS_COMMISSIONED,
4040
OT_RPC_CMD_DATA_SET_ACTIVE_TLVS,
4141
OT_RPC_CMD_DATA_GET_ACTIVE_TLVS,
42+
OT_RPC_CMD_DATASET_ACTIVE_GET,
43+
OT_RPC_CMD_GET_VERSION_STRING,
44+
OT_RPC_CMD_LINK_GET_CHANNEL,
45+
OT_RPC_CMD_LINK_GET_COUNTERS,
46+
OT_RPC_CMD_LINK_GET_EXTENDED_ADDRESS,
47+
OT_RPC_CMD_LINK_GET_PAN_ID,
48+
OT_RPC_CMD_NET_DATA_GET_STABLE_VERSION,
49+
OT_RPC_CMD_NET_DATA_GET_VERSION,
50+
OT_RPC_CMD_THREAD_GET_EXTENDED_PANID,
51+
OT_RPC_CMD_THREAD_GET_LEADER_ROUTER_ID,
52+
OT_RPC_CMD_THREAD_GET_LEADER_WEIGHT,
53+
OT_RPC_CMD_THREAD_GET_MESH_LOCAL_PREFIX,
54+
OT_RPC_CMD_THREAD_GET_MLE_COUNTERS,
55+
OT_RPC_CMD_THREAD_GET_NETWORK_NAME,
56+
OT_RPC_CMD_THREAD_GET_NEXT_NEIGHBOR_INFO,
57+
OT_RPC_CMD_THREAD_GET_PARENT_INFO,
58+
OT_RPC_CMD_THREAD_GET_PARTITION_ID,
4259
};

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ zephyr_library_sources(
1414
zephyr_library_sources_ifdef(CONFIG_NET_L2_OPENTHREAD
1515
ot_rpc_if_server.c
1616
ot_rpc_commissioning_server.c
17+
ot_rpc_diag_server.c
1718
)
1819

1920
zephyr_library_include_directories(

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

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -224,24 +224,6 @@ static void ot_rpc_cmd_remove_state_changed_callback(const struct nrf_rpc_group
224224
nrf_rpc_cbor_rsp_no_err(group, &rsp_ctx);
225225
}
226226

227-
static void ot_get_extaddr(const struct nrf_rpc_group *group, struct nrf_rpc_cbor_ctx *ctx,
228-
void *handler_data)
229-
{
230-
const otExtAddress *data;
231-
size_t length = sizeof(data->m8);
232-
struct nrf_rpc_cbor_ctx rsp_ctx;
233-
234-
nrf_rpc_cbor_decoding_done(group, ctx);
235-
236-
openthread_api_mutex_lock(openthread_get_default_context());
237-
data = otLinkGetExtendedAddress(openthread_get_default_context()->instance);
238-
openthread_api_mutex_unlock(openthread_get_default_context());
239-
240-
NRF_RPC_CBOR_ALLOC(group, rsp_ctx, length + 1);
241-
zcbor_bstr_encode_ptr(rsp_ctx.zs, data->m8, length);
242-
nrf_rpc_cbor_rsp_no_err(group, &rsp_ctx);
243-
}
244-
245227
static void ot_rpc_cmd_ip6_set_enabled(const struct nrf_rpc_group *group,
246228
struct nrf_rpc_cbor_ctx *ctx, void *handler_data)
247229
{
@@ -492,8 +474,6 @@ static void ot_rpc_cmd_get_poll_period(const struct nrf_rpc_group *group,
492474
nrf_rpc_cbor_rsp_no_err(group, &rsp_ctx);
493475
}
494476

495-
NRF_RPC_CBOR_CMD_DECODER(ot_group, ot_get_extaddr, OT_RPC_CMD_IF_EXTADDR, ot_get_extaddr, NULL);
496-
497477
NRF_RPC_CBOR_CMD_DECODER(ot_group, ot_rpc_cmd_ip6_get_unicast_addrs,
498478
OT_RPC_CMD_IP6_GET_UNICAST_ADDRESSES, ot_rpc_cmd_ip6_get_unicast_addrs,
499479
NULL);

0 commit comments

Comments
 (0)