Skip to content

Commit 77b3230

Browse files
alxelaxrlubos
authored andcommitted
net: openthread: rpc: using nrf_rpc encoders and decoders
Commit adds refactoring openthread clients to use nrf_rpc encoders and decoders instead of zcbor API. Signed-off-by: Aleksandr Khromykh <[email protected]>
1 parent 19c70d7 commit 77b3230

File tree

13 files changed

+274
-791
lines changed

13 files changed

+274
-791
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
#include <ot_rpc_ids.h>
88
#include <ot_rpc_common.h>
9-
9+
#include <nrf_rpc/nrf_rpc_serialize.h>
1010
#include <nrf_rpc_cbor.h>
1111

1212
#include <openthread/cli.h>
@@ -37,7 +37,7 @@ void otCliInputLine(char *aBuf)
3737
struct nrf_rpc_cbor_ctx ctx;
3838

3939
NRF_RPC_CBOR_ALLOC(&ot_group, ctx, cbor_buffer_size);
40-
zcbor_tstr_encode_ptr(ctx.zs, aBuf, buffer_len);
40+
nrf_rpc_encode_str(&ctx, aBuf, buffer_len);
4141

4242
nrf_rpc_cbor_cmd_no_err(&ot_group, OT_RPC_CMD_CLI_INPUT_LINE, &ctx, ot_rpc_decode_void,
4343
NULL);

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

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
#include <ot_rpc_ids.h>
88
#include <ot_rpc_common.h>
9-
9+
#include <nrf_rpc/nrf_rpc_serialize.h>
1010
#include <nrf_rpc_cbor.h>
1111

1212
#include <openthread/error.h>
@@ -27,54 +27,43 @@ static otMeshLocalPrefix mesh_prefix;
2727

2828
static void get_uint_t(int id, void *result, size_t result_size)
2929
{
30-
const size_t cbor_buffer_size = 0;
3130
struct nrf_rpc_cbor_ctx ctx;
31+
uint32_t value;
3232

33-
NRF_RPC_CBOR_ALLOC(&ot_group, ctx, cbor_buffer_size);
33+
NRF_RPC_CBOR_ALLOC(&ot_group, ctx, 0);
3434

3535
nrf_rpc_cbor_cmd_rsp_no_err(&ot_group, id, &ctx);
3636

37-
if (!zcbor_uint_decode(ctx.zs, result, result_size)) {
38-
nrf_rpc_cbor_decoding_done(&ot_group, &ctx);
37+
value = nrf_rpc_decode_uint(&ctx);
38+
if (!nrf_rpc_decoding_done_and_check(&ot_group, &ctx)) {
3939
ot_rpc_report_decoding_error(id);
4040
return;
4141
}
4242

43-
nrf_rpc_cbor_decoding_done(&ot_group, &ctx);
43+
memcpy(result, &value, result_size);
4444
}
4545

4646
static int get_string(int id, char *buffer, size_t buffer_size)
4747
{
48-
const size_t cbor_buffer_size = 0;
49-
size_t bytes_copied = 0;
50-
struct zcbor_string zst;
5148
struct nrf_rpc_cbor_ctx ctx;
49+
size_t size = 0;
50+
const void *buf = NULL;
5251

53-
NRF_RPC_CBOR_ALLOC(&ot_group, ctx, cbor_buffer_size);
52+
NRF_RPC_CBOR_ALLOC(&ot_group, ctx, 0);
5453

5554
nrf_rpc_cbor_cmd_rsp_no_err(&ot_group, id, &ctx);
5655

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;
56+
buf = nrf_rpc_decode_buffer_ptr_and_size(&ctx, &size);
57+
if (buf && size) {
58+
memcpy(buffer, buf, MIN(size, buffer_size));
6159
}
6260

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);
61+
if (!nrf_rpc_decoding_done_and_check(&ot_group, &ctx)) {
6862
ot_rpc_report_decoding_error(id);
69-
goto exit;
63+
return 0;
7064
}
7165

72-
memcpy(buffer, zst.value, bytes_copied);
73-
74-
nrf_rpc_cbor_decoding_done(&ot_group, &ctx);
75-
76-
exit:
77-
return bytes_copied;
66+
return buf && size ? MIN(size, buffer_size) : 0;
7867
}
7968

8069
const char *otGetVersionString(void)

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

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
#include <ot_rpc_ids.h>
88
#include <ot_rpc_common.h>
9-
9+
#include <nrf_rpc/nrf_rpc_serialize.h>
1010
#include <nrf_rpc_cbor.h>
1111

1212
#include <openthread/ip6.h>
@@ -165,7 +165,7 @@ static int ot_rpc_l2_enable(struct net_if *iface, bool state)
165165
}
166166

167167
NRF_RPC_CBOR_ALLOC(&ot_group, ctx, cbor_buffer_size);
168-
zcbor_bool_put(ctx.zs, state);
168+
nrf_rpc_encode_bool(&ctx, state);
169169
nrf_rpc_cbor_cmd_no_err(&ot_group, OT_RPC_CMD_IF_ENABLE, &ctx, ot_rpc_decode_void, NULL);
170170

171171
if (state) {
@@ -206,46 +206,44 @@ NET_DEVICE_INIT(ot_rpc, "ot_rpc", ot_rpc_dev_init, /* pm */ NULL, /* device inst
206206
static void ot_rpc_cmd_if_receive(const struct nrf_rpc_group *group, struct nrf_rpc_cbor_ctx *ctx,
207207
void *handler_data)
208208
{
209-
struct zcbor_string pkt_data;
210-
bool decoded_ok;
209+
const uint8_t *pkt_data;
210+
size_t pkt_data_len = 0;
211211
struct net_if *iface;
212212
struct net_pkt *pkt = NULL;
213213
struct nrf_rpc_cbor_ctx rsp_ctx;
214214

215-
decoded_ok = zcbor_bstr_decode(ctx->zs, &pkt_data);
216-
nrf_rpc_cbor_decoding_done(group, ctx);
217-
218-
if (!decoded_ok) {
219-
NET_ERR("Failed to decode packet data");
220-
goto out;
221-
}
222-
223-
iface = net_if_get_first_by_type(&NET_L2_GET_NAME(OPENTHREAD));
224-
if (!iface) {
225-
NET_ERR("There is no net interface for OpenThread");
226-
goto out;
227-
}
215+
pkt_data = nrf_rpc_decode_buffer_ptr_and_size(ctx, &pkt_data_len);
216+
if (pkt_data && pkt_data_len) {
217+
iface = net_if_get_first_by_type(&NET_L2_GET_NAME(OPENTHREAD));
218+
if (iface) {
219+
pkt = net_pkt_rx_alloc_with_buffer(iface, pkt_data_len, AF_UNSPEC, 0,
220+
K_NO_WAIT);
221+
} else {
222+
NET_ERR("There is no net interface for OpenThread");
223+
}
228224

229-
pkt = net_pkt_rx_alloc_with_buffer(iface, pkt_data.len, AF_UNSPEC, 0, K_NO_WAIT);
230-
if (!pkt) {
231-
NET_ERR("Failed to reserve net pkt");
232-
goto out;
225+
if (pkt) {
226+
net_pkt_write(pkt, pkt_data, pkt_data_len);
227+
} else {
228+
NET_ERR("Failed to reserve net pkt");
229+
}
233230
}
234231

235-
net_pkt_write(pkt, pkt_data.value, pkt_data.len);
236-
237-
NET_DBG("Passing Ip6 packet to Zephyr net stack");
238-
239-
if (net_recv_data(iface, pkt) < 0) {
240-
NET_ERR("net_recv_data failed");
241-
goto out;
232+
if (!nrf_rpc_decoding_done_and_check(group, ctx)) {
233+
NET_ERR("Failed to decode packet data");
234+
ot_rpc_report_decoding_error(OT_RPC_CMD_IF_RECEIVE);
235+
if (pkt) {
236+
net_pkt_unref(pkt);
237+
}
238+
return;
242239
}
243240

244-
pkt = NULL;
245-
246-
out:
247241
if (pkt) {
248-
net_pkt_unref(pkt);
242+
NET_DBG("Passing Ip6 packet to Zephyr net stack");
243+
if (net_recv_data(iface, pkt) < 0) {
244+
NET_ERR("net_recv_data failed");
245+
net_pkt_unref(pkt);
246+
}
249247
}
250248

251249
NRF_RPC_CBOR_ALLOC(group, rsp_ctx, 0);

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

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,6 @@
1212

1313
#include <openthread/instance.h>
1414

15-
static otError decode_ot_error(struct nrf_rpc_cbor_ctx *ctx)
16-
{
17-
otError error;
18-
19-
if (!zcbor_uint_decode(ctx->zs, &error, sizeof(error))) {
20-
error = OT_ERROR_PARSE;
21-
}
22-
23-
nrf_rpc_cbor_decoding_done(&ot_group, ctx);
24-
25-
return error;
26-
}
27-
2815
otInstance *otInstanceInitSingle(void)
2916
{
3017
struct nrf_rpc_cbor_ctx ctx;
@@ -97,17 +84,19 @@ otError otSetStateChangedCallback(otInstance *aInstance, otStateChangedCallback
9784
{
9885
const size_t cbor_buffer_size = 10;
9986
struct nrf_rpc_cbor_ctx ctx;
87+
otError error;
10088

10189
ARG_UNUSED(aInstance);
10290

10391
NRF_RPC_CBOR_ALLOC(&ot_group, ctx, cbor_buffer_size);
10492
/* TODO: implement callback & address pseudonymization. */
105-
zcbor_uint32_put(ctx.zs, (uint32_t)aCallback);
106-
zcbor_uint32_put(ctx.zs, (uint32_t)aContext);
93+
nrf_rpc_encode_uint(&ctx, (uint32_t)aCallback);
94+
nrf_rpc_encode_uint(&ctx, (uint32_t)aContext);
10795

108-
nrf_rpc_cbor_cmd_rsp_no_err(&ot_group, OT_RPC_CMD_SET_STATE_CHANGED_CALLBACK, &ctx);
96+
nrf_rpc_cbor_cmd_no_err(&ot_group, OT_RPC_CMD_SET_STATE_CHANGED_CALLBACK, &ctx,
97+
ot_rpc_decode_error, &error);
10998

110-
return decode_ot_error(&ctx);
99+
return error;
111100
}
112101

113102
void otRemoveStateChangeCallback(otInstance *aInstance, otStateChangedCallback aCallback,
@@ -120,8 +109,8 @@ void otRemoveStateChangeCallback(otInstance *aInstance, otStateChangedCallback a
120109

121110
NRF_RPC_CBOR_ALLOC(&ot_group, ctx, cbor_buffer_size);
122111
/* TODO: implement callback & address pseudonymization. */
123-
zcbor_uint32_put(ctx.zs, (uint32_t)aCallback);
124-
zcbor_uint32_put(ctx.zs, (uint32_t)aContext);
112+
nrf_rpc_encode_uint(&ctx, (uint32_t)aCallback);
113+
nrf_rpc_encode_uint(&ctx, (uint32_t)aContext);
125114

126115
nrf_rpc_cbor_cmd_no_err(&ot_group, OT_RPC_CMD_REMOVE_STATE_CHANGED_CALLBACK, &ctx,
127116
ot_rpc_decode_void, NULL);
@@ -134,20 +123,19 @@ static void ot_rpc_cmd_state_changed(const struct nrf_rpc_group *group,
134123
uint32_t callback;
135124
uint32_t context;
136125
otChangedFlags flags;
137-
bool decoded_ok;
138126

139-
decoded_ok = zcbor_uint32_decode(ctx->zs, &callback) &&
140-
zcbor_uint32_decode(ctx->zs, &context) && zcbor_uint32_decode(ctx->zs, &flags);
141-
nrf_rpc_cbor_decoding_done(group, ctx);
127+
callback = nrf_rpc_decode_uint(ctx);
128+
context = nrf_rpc_decode_uint(ctx);
129+
flags = nrf_rpc_decode_uint(ctx);
142130

143-
if (!decoded_ok) {
144-
goto out;
131+
if (!nrf_rpc_decoding_done_and_check(group, ctx)) {
132+
ot_rpc_report_decoding_error(OT_RPC_CMD_STATE_CHANGED);
133+
return;
145134
}
146135

147136
/* TODO: implement callback & address pseudonymization. */
148137
((otStateChangedCallback)callback)(flags, (void *)context);
149138

150-
out:
151139
NRF_RPC_CBOR_ALLOC(group, rsp_ctx, 0);
152140
nrf_rpc_cbor_rsp_no_err(group, &rsp_ctx);
153141
}

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

Lines changed: 19 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,6 @@
1818
#define OT_RPC_MAX_NUM_UNICAST_ADDRESSES 8
1919
#define OT_RPC_MAX_NUM_MULTICAST_ADDRESSES 8
2020

21-
static otError decode_ot_error(struct nrf_rpc_cbor_ctx *ctx)
22-
{
23-
otError error;
24-
25-
if (!zcbor_uint_decode(ctx->zs, &error, sizeof(error))) {
26-
error = OT_ERROR_PARSE;
27-
}
28-
29-
nrf_rpc_cbor_decoding_done(&ot_group, ctx);
30-
31-
return error;
32-
}
33-
3421
const otNetifAddress *otIp6GetUnicastAddresses(otInstance *aInstance)
3522
{
3623
struct nrf_rpc_cbor_ctx ctx;
@@ -140,39 +127,33 @@ const otNetifMulticastAddress *otIp6GetMulticastAddresses(otInstance *aInstance)
140127
otError otIp6SetEnabled(otInstance *aInstance, bool aEnabled)
141128
{
142129
struct nrf_rpc_cbor_ctx ctx;
130+
otError error;
143131

144132
ARG_UNUSED(aInstance);
145133

146134
NRF_RPC_CBOR_ALLOC(&ot_group, ctx, 1);
135+
nrf_rpc_encode_bool(&ctx, aEnabled);
136+
nrf_rpc_cbor_cmd_no_err(&ot_group, OT_RPC_CMD_IP6_SET_ENABLED, &ctx, ot_rpc_decode_error,
137+
&error);
147138

148-
if (!zcbor_bool_encode(ctx.zs, &aEnabled)) {
149-
NRF_RPC_CBOR_DISCARD(&ot_group, ctx);
150-
return OT_ERROR_INVALID_ARGS;
151-
}
152-
153-
nrf_rpc_cbor_cmd_rsp_no_err(&ot_group, OT_RPC_CMD_IP6_SET_ENABLED, &ctx);
154-
155-
return decode_ot_error(&ctx);
139+
return error;
156140
}
157141

158142
bool otIp6IsEnabled(otInstance *aInstance)
159143
{
160144
struct nrf_rpc_cbor_ctx ctx;
161-
bool enabled = false;
162-
bool decoded_ok;
145+
bool enabled;
163146

164147
ARG_UNUSED(aInstance);
165148

166149
NRF_RPC_CBOR_ALLOC(&ot_group, ctx, 0);
167150

168151
nrf_rpc_cbor_cmd_rsp_no_err(&ot_group, OT_RPC_CMD_IP6_IS_ENABLED, &ctx);
169152

170-
decoded_ok = zcbor_bool_decode(ctx.zs, &enabled);
171-
nrf_rpc_cbor_decoding_done(&ot_group, &ctx);
153+
enabled = nrf_rpc_decode_bool(&ctx);
172154

173-
if (!decoded_ok) {
174-
nrf_rpc_err(-EBADMSG, NRF_RPC_ERR_SRC_RECV, &ot_group, OT_RPC_CMD_IP6_IS_ENABLED,
175-
NRF_RPC_PACKET_TYPE_RSP);
155+
if (!nrf_rpc_decoding_done_and_check(&ot_group, &ctx)) {
156+
ot_rpc_report_decoding_error(OT_RPC_CMD_IP6_IS_ENABLED);
176157
}
177158

178159
return enabled;
@@ -181,35 +162,30 @@ bool otIp6IsEnabled(otInstance *aInstance)
181162
otError otIp6SubscribeMulticastAddress(otInstance *aInstance, const otIp6Address *aAddress)
182163
{
183164
struct nrf_rpc_cbor_ctx ctx;
165+
otError error;
184166

185167
ARG_UNUSED(aInstance);
186168

187169
NRF_RPC_CBOR_ALLOC(&ot_group, ctx, 1 + OT_IP6_ADDRESS_SIZE);
170+
nrf_rpc_encode_buffer(&ctx, (const char *)aAddress, OT_IP6_ADDRESS_SIZE);
171+
nrf_rpc_cbor_cmd_no_err(&ot_group, OT_RPC_CMD_IP6_SUBSCRIBE_MADDR, &ctx,
172+
ot_rpc_decode_error, &error);
188173

189-
if (!zcbor_bstr_encode_ptr(ctx.zs, (const char *)aAddress, OT_IP6_ADDRESS_SIZE)) {
190-
NRF_RPC_CBOR_DISCARD(&ot_group, ctx);
191-
return OT_ERROR_INVALID_ARGS;
192-
}
193-
194-
nrf_rpc_cbor_cmd_rsp_no_err(&ot_group, OT_RPC_CMD_IP6_SUBSCRIBE_MADDR, &ctx);
195-
196-
return decode_ot_error(&ctx);
174+
return error;
197175
}
198176

199177
otError otIp6UnsubscribeMulticastAddress(otInstance *aInstance, const otIp6Address *aAddress)
200178
{
201179
struct nrf_rpc_cbor_ctx ctx;
180+
otError error;
202181

203182
ARG_UNUSED(aInstance);
204183

205184
NRF_RPC_CBOR_ALLOC(&ot_group, ctx, 1 + OT_IP6_ADDRESS_SIZE);
206185

207-
if (!zcbor_bstr_encode_ptr(ctx.zs, (const char *)aAddress, OT_IP6_ADDRESS_SIZE)) {
208-
NRF_RPC_CBOR_DISCARD(&ot_group, ctx);
209-
return OT_ERROR_INVALID_ARGS;
210-
}
211-
212-
nrf_rpc_cbor_cmd_rsp_no_err(&ot_group, OT_RPC_CMD_IP6_UNSUBSCRIBE_MADDR, &ctx);
186+
nrf_rpc_encode_buffer(&ctx, (const char *)aAddress, OT_IP6_ADDRESS_SIZE);
187+
nrf_rpc_cbor_cmd_no_err(&ot_group, OT_RPC_CMD_IP6_UNSUBSCRIBE_MADDR, &ctx,
188+
ot_rpc_decode_error, &error);
213189

214-
return decode_ot_error(&ctx);
190+
return error;
215191
}

0 commit comments

Comments
 (0)