Skip to content

Commit 8227c78

Browse files
alxelaxnordicjm
authored andcommitted
net: openthread: rpc: using nrf_rpc encoders and decoders
Commit adds refactoring openthread servers to use nrf_rpc encoders and decoders instead of zcbor API. Signed-off-by: Aleksandr Khromykh <[email protected]>
1 parent d318939 commit 8227c78

File tree

11 files changed

+241
-464
lines changed

11 files changed

+241
-464
lines changed

subsys/net/openthread/rpc/common/ot_rpc_common.c

Lines changed: 27 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -223,42 +223,21 @@ void ot_rpc_decode_message_info(struct nrf_rpc_cbor_ctx *ctx, otMessageInfo *aMe
223223
aMessageInfo->mMulticastLoop = nrf_rpc_decode_bool(ctx);
224224
}
225225

226-
bool ot_rpc_encode_service_config(struct nrf_rpc_cbor_ctx *ctx, const otServiceConfig *config)
226+
void ot_rpc_encode_service_config(struct nrf_rpc_cbor_ctx *ctx, const otServiceConfig *config)
227227
{
228228

229229
if (config == NULL) {
230-
return zcbor_nil_put(ctx->zs, NULL);
231-
}
232-
233-
if (!zcbor_uint_encode(ctx->zs, &config->mServiceId, sizeof(config->mServiceId))) {
234-
return false;
235-
}
236-
237-
if (!zcbor_uint_encode(ctx->zs, &config->mEnterpriseNumber,
238-
sizeof(config->mEnterpriseNumber))) {
239-
return false;
240-
}
241-
242-
if (!zcbor_bstr_encode_ptr(ctx->zs, (const char *)&config->mServiceData,
243-
config->mServiceDataLength)) {
244-
return false;
245-
}
246-
247-
if (!zcbor_bool_put(ctx->zs, config->mServerConfig.mStable)) {
248-
return false;
249-
}
250-
251-
if (!zcbor_bstr_encode_ptr(ctx->zs, (const char *)&config->mServerConfig.mServerData,
252-
config->mServerConfig.mServerDataLength)) {
253-
return false;
254-
}
255-
256-
if (!zcbor_uint_encode(ctx->zs, &config->mServerConfig.mRloc16,
257-
sizeof(config->mServerConfig.mRloc16))) {
258-
return false;
230+
nrf_rpc_encode_null(ctx);
231+
return;
259232
}
260233

261-
return true;
234+
nrf_rpc_encode_uint(ctx, config->mServiceId);
235+
nrf_rpc_encode_uint(ctx, config->mEnterpriseNumber);
236+
nrf_rpc_encode_buffer(ctx, config->mServiceData, config->mServiceDataLength);
237+
nrf_rpc_encode_bool(ctx, config->mServerConfig.mStable);
238+
nrf_rpc_encode_buffer(ctx, config->mServerConfig.mServerData,
239+
config->mServerConfig.mServerDataLength);
240+
nrf_rpc_encode_uint(ctx, config->mServerConfig.mRloc16);
262241
}
263242

264243
void ot_rpc_decode_service_config(struct nrf_rpc_cbor_ctx *ctx, otServiceConfig *config)
@@ -293,72 +272,28 @@ void ot_rpc_decode_service_config(struct nrf_rpc_cbor_ctx *ctx, otServiceConfig
293272
config->mServerConfig.mRloc16 = nrf_rpc_decode_uint(ctx);
294273
}
295274

296-
bool ot_rpc_encode_border_router_config(struct nrf_rpc_cbor_ctx *ctx,
275+
void ot_rpc_encode_border_router_config(struct nrf_rpc_cbor_ctx *ctx,
297276
const otBorderRouterConfig *config)
298277
{
299-
signed int tmp;
300-
301278
if (config == NULL) {
302-
return zcbor_nil_put(ctx->zs, NULL);
303-
}
304-
305-
if (!zcbor_bstr_encode_ptr(ctx->zs, (const char *)config->mPrefix.mPrefix.mFields.m8,
306-
OT_IP6_ADDRESS_SIZE)) {
307-
return false;
308-
}
309-
310-
if (!zcbor_uint_encode(ctx->zs, &config->mPrefix.mLength,
311-
sizeof(config->mPrefix.mLength))) {
312-
return false;
313-
}
314-
315-
tmp = config->mPreference;
316-
317-
if (!zcbor_int_encode(ctx->zs, &tmp, sizeof(tmp))) {
318-
return false;
319-
}
320-
321-
if (!zcbor_bool_put(ctx->zs, config->mPreferred)) {
322-
return false;
323-
}
324-
325-
if (!zcbor_bool_put(ctx->zs, config->mSlaac)) {
326-
return false;
327-
}
328-
329-
if (!zcbor_bool_put(ctx->zs, config->mDhcp)) {
330-
return false;
331-
}
332-
333-
if (!zcbor_bool_put(ctx->zs, config->mConfigure)) {
334-
return false;
335-
}
336-
337-
if (!zcbor_bool_put(ctx->zs, config->mDefaultRoute)) {
338-
return false;
339-
}
340-
341-
if (!zcbor_bool_put(ctx->zs, config->mOnMesh)) {
342-
return false;
343-
}
344-
345-
if (!zcbor_bool_put(ctx->zs, config->mStable)) {
346-
return false;
347-
}
348-
349-
if (!zcbor_bool_put(ctx->zs, config->mNdDns)) {
350-
return false;
351-
}
352-
353-
if (!zcbor_bool_put(ctx->zs, config->mDp)) {
354-
return false;
355-
}
356-
357-
if (!zcbor_uint_encode(ctx->zs, &config->mRloc16, sizeof(config->mRloc16))) {
358-
return false;
279+
nrf_rpc_encode_null(ctx);
280+
return;
359281
}
360282

361-
return true;
283+
nrf_rpc_encode_buffer(ctx, config->mPrefix.mPrefix.mFields.m8,
284+
OT_IP6_ADDRESS_SIZE);
285+
nrf_rpc_encode_uint(ctx, config->mPrefix.mLength);
286+
nrf_rpc_encode_int(ctx, config->mPreference);
287+
nrf_rpc_encode_bool(ctx, config->mPreferred);
288+
nrf_rpc_encode_bool(ctx, config->mSlaac);
289+
nrf_rpc_encode_bool(ctx, config->mDhcp);
290+
nrf_rpc_encode_bool(ctx, config->mConfigure);
291+
nrf_rpc_encode_bool(ctx, config->mDefaultRoute);
292+
nrf_rpc_encode_bool(ctx, config->mOnMesh);
293+
nrf_rpc_encode_bool(ctx, config->mStable);
294+
nrf_rpc_encode_bool(ctx, config->mNdDns);
295+
nrf_rpc_encode_bool(ctx, config->mDp);
296+
nrf_rpc_encode_uint(ctx, config->mRloc16);
362297
}
363298

364299
void ot_rpc_decode_border_router_config(struct nrf_rpc_cbor_ctx *ctx, otBorderRouterConfig *config)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ void ot_rpc_report_cmd_decoding_error(uint8_t cmd_evt_id);
7979
void ot_rpc_report_rsp_decoding_error(uint8_t cmd_evt_id);
8080
void ot_rpc_decode_message_info(struct nrf_rpc_cbor_ctx *ctx, otMessageInfo *aMessageInfo);
8181
void ot_rpc_encode_message_info(struct nrf_rpc_cbor_ctx *ctx, const otMessageInfo *aMessageInfo);
82-
bool ot_rpc_encode_service_config(struct nrf_rpc_cbor_ctx *ctx,
82+
void ot_rpc_encode_service_config(struct nrf_rpc_cbor_ctx *ctx,
8383
const otServiceConfig *service_config);
8484
void ot_rpc_decode_service_config(struct nrf_rpc_cbor_ctx *ctx, otServiceConfig *service_config);
85-
bool ot_rpc_encode_border_router_config(struct nrf_rpc_cbor_ctx *ctx,
85+
void ot_rpc_encode_border_router_config(struct nrf_rpc_cbor_ctx *ctx,
8686
const otBorderRouterConfig *service_config);
8787
void ot_rpc_decode_border_router_config(struct nrf_rpc_cbor_ctx *ctx,
8888
otBorderRouterConfig *service_config);

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

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

7+
#include <nrf_rpc/nrf_rpc_serialize.h>
78
#include <ot_rpc_ids.h>
89
#include <ot_rpc_common.h>
910

1011
#include <nrf_rpc_cbor.h>
1112

1213
#include <openthread/cli.h>
13-
1414
#include <zephyr/net/openthread.h>
1515

1616
#include <stdio.h>
@@ -33,7 +33,7 @@ static int ot_cli_output_callback(void *aContext, const char *aFormat, va_list a
3333
num_written = MIN((size_t)result, sizeof(output_line_buffer));
3434
cbor_buffer_size += num_written;
3535
NRF_RPC_CBOR_ALLOC(&ot_group, ctx, cbor_buffer_size);
36-
zcbor_tstr_encode_ptr(ctx.zs, output_line_buffer, num_written);
36+
nrf_rpc_encode_str(&ctx, output_line_buffer, num_written);
3737

3838
nrf_rpc_cbor_cmd_no_err(&ot_group, OT_RPC_CMD_CLI_OUTPUT, &ctx, ot_rpc_decode_void, NULL);
3939

@@ -68,27 +68,23 @@ NRF_RPC_CBOR_CMD_DECODER(ot_group, ot_rpc_cmd_cli_init, OT_RPC_CMD_CLI_INIT, ot_
6868
static void ot_rpc_cmd_cli_input_line(const struct nrf_rpc_group *group,
6969
struct nrf_rpc_cbor_ctx *ctx, void *handler_data)
7070
{
71-
struct zcbor_string input_line;
7271
char input_line_buffer[256];
7372
struct nrf_rpc_cbor_ctx rsp_ctx;
73+
char *result;
7474

7575
/* Parse the input */
76+
result = nrf_rpc_decode_str(ctx, input_line_buffer, sizeof(input_line_buffer));
7677

77-
if (!zcbor_tstr_decode(ctx->zs, &input_line)) {
78-
goto error;
78+
if (!nrf_rpc_decoding_done_and_check(group, ctx)) {
79+
ot_rpc_report_cmd_decoding_error(OT_RPC_CMD_CLI_INPUT_LINE);
80+
return;
7981
}
8082

81-
if (input_line.len >= sizeof(input_line_buffer)) {
82-
goto error;
83+
if (result == NULL) {
84+
return;
8385
}
8486

85-
memcpy(input_line_buffer, input_line.value, input_line.len);
86-
input_line_buffer[input_line.len] = '\0';
87-
88-
nrf_rpc_cbor_decoding_done(group, ctx);
89-
9087
/* Execute OT CLI command */
91-
9288
openthread_api_mutex_lock(openthread_get_default_context());
9389
otCliInputLine(input_line_buffer);
9490
openthread_api_mutex_unlock(openthread_get_default_context());
@@ -98,9 +94,6 @@ static void ot_rpc_cmd_cli_input_line(const struct nrf_rpc_group *group,
9894
NRF_RPC_CBOR_ALLOC(group, rsp_ctx, 0);
9995

10096
nrf_rpc_cbor_rsp_no_err(group, &rsp_ctx);
101-
102-
error:
103-
return;
10497
}
10598

10699
NRF_RPC_CBOR_CMD_DECODER(ot_group, ot_rpc_cmd_cli_input_line, OT_RPC_CMD_CLI_INPUT_LINE,

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

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
55
*/
66

7+
#include <nrf_rpc/nrf_rpc_serialize.h>
78
#include <ot_rpc_ids.h>
89
#include <ot_rpc_common.h>
910

@@ -72,17 +73,14 @@ static void ot_rpc_cmd_if_enable(const struct nrf_rpc_group *group, struct nrf_r
7273
void *handler_data)
7374
{
7475
bool enable;
75-
bool decoded_ok;
7676
int ret;
7777
struct net_if *iface;
7878
struct nrf_rpc_cbor_ctx rsp_ctx;
7979

80-
decoded_ok = zcbor_bool_decode(ctx->zs, &enable);
81-
nrf_rpc_cbor_decoding_done(group, ctx);
82-
83-
if (!decoded_ok) {
84-
NET_ERR("Failed to decode RPC argument");
85-
goto out;
80+
enable = nrf_rpc_decode_bool(ctx);
81+
if (!nrf_rpc_decoding_done_and_check(group, ctx)) {
82+
ot_rpc_report_cmd_decoding_error(OT_RPC_CMD_IF_ENABLE);
83+
return;
8684
}
8785

8886
iface = net_if_get_first_by_type(&NET_L2_GET_NAME(OPENTHREAD));
@@ -134,46 +132,45 @@ NRF_RPC_CBOR_CMD_DECODER(ot_group, ot_rpc_cmd_if_enable, OT_RPC_CMD_IF_ENABLE, o
134132
static void ot_rpc_cmd_if_send(const struct nrf_rpc_group *group, struct nrf_rpc_cbor_ctx *ctx,
135133
void *handler_data)
136134
{
137-
struct zcbor_string pkt_data;
138-
bool decoded_ok;
135+
const uint8_t *pkt_data;
136+
size_t pkt_data_len = 0;
139137
struct net_if *iface;
140138
struct net_pkt *pkt = NULL;
141139
struct nrf_rpc_cbor_ctx rsp_ctx;
142140

143-
decoded_ok = zcbor_bstr_decode(ctx->zs, &pkt_data);
144-
nrf_rpc_cbor_decoding_done(group, ctx);
145-
146-
if (!decoded_ok) {
147-
NET_ERR("Failed to decode packet data");
148-
goto out;
149-
}
141+
pkt_data = nrf_rpc_decode_buffer_ptr_and_size(ctx, &pkt_data_len);
150142

151-
iface = net_if_get_first_by_type(&NET_L2_GET_NAME(OPENTHREAD));
152-
if (!iface) {
153-
NET_ERR("There is no net interface for OpenThread");
154-
goto out;
155-
}
143+
if (pkt_data && pkt_data_len) {
144+
iface = net_if_get_first_by_type(&NET_L2_GET_NAME(OPENTHREAD));
145+
if (iface) {
146+
pkt = net_pkt_alloc_with_buffer(iface, pkt_data_len, AF_UNSPEC, 0,
147+
K_NO_WAIT);
148+
} else {
149+
NET_ERR("There is no net interface for OpenThread");
150+
}
156151

157-
pkt = net_pkt_alloc_with_buffer(iface, pkt_data.len, AF_UNSPEC, 0, K_NO_WAIT);
158-
if (!pkt) {
159-
NET_ERR("Failed to reserve net pkt");
160-
goto out;
152+
if (pkt) {
153+
net_pkt_write(pkt, pkt_data, pkt_data_len);
154+
} else {
155+
NET_ERR("Failed to reserve net pkt");
156+
}
161157
}
162158

163-
net_pkt_write(pkt, pkt_data.value, pkt_data.len);
164-
165-
NET_DBG("Sending Ip6 packet to OpenThread");
166-
167-
if (net_send_data(pkt) < 0) {
168-
NET_ERR("net_send_data failed");
169-
goto out;
159+
if (!nrf_rpc_decoding_done_and_check(group, ctx)) {
160+
NET_ERR("Failed to decode packet data");
161+
ot_rpc_report_cmd_decoding_error(OT_RPC_CMD_IF_SEND);
162+
if (pkt) {
163+
net_pkt_unref(pkt);
164+
}
165+
return;
170166
}
171167

172-
pkt = NULL;
173-
174-
out:
175168
if (pkt) {
176-
net_pkt_unref(pkt);
169+
NET_DBG("Sending Ip6 packet to OpenThread");
170+
if (net_send_data(pkt) < 0) {
171+
NET_ERR("net_send_data failed");
172+
net_pkt_unref(pkt);
173+
}
177174
}
178175

179176
NRF_RPC_CBOR_ALLOC(group, rsp_ctx, 0);

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

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,9 @@ static void ot_state_changed_callback(otChangedFlags aFlags, void *aContext)
203203
ot_rpc_callback_t *cb = aContext;
204204

205205
NRF_RPC_CBOR_ALLOC(&ot_group, ctx, 15);
206-
zcbor_uint32_put(ctx.zs, cb->callback);
207-
zcbor_uint32_put(ctx.zs, cb->context);
208-
zcbor_uint32_put(ctx.zs, aFlags);
206+
nrf_rpc_encode_uint(&ctx, cb->callback);
207+
nrf_rpc_encode_uint(&ctx, cb->context);
208+
nrf_rpc_encode_uint(&ctx, aFlags);
209209

210210
openthread_api_mutex_unlock(openthread_get_default_context());
211211
nrf_rpc_cbor_cmd_no_err(&ot_group, OT_RPC_CMD_STATE_CHANGED, &ctx, ot_rpc_decode_void,
@@ -218,18 +218,16 @@ static void ot_rpc_cmd_set_state_changed_callback(const struct nrf_rpc_group *gr
218218
{
219219
uint32_t callback;
220220
uint32_t context;
221-
bool decoded_ok;
222221
ot_rpc_callback_t *cb;
223222
otError error;
224223
struct nrf_rpc_cbor_ctx rsp_ctx;
225224

226-
decoded_ok = zcbor_uint32_decode(ctx->zs, &callback);
227-
decoded_ok = decoded_ok && zcbor_uint32_decode(ctx->zs, &context);
228-
nrf_rpc_cbor_decoding_done(group, ctx);
225+
callback = nrf_rpc_decode_uint(ctx);
226+
context = nrf_rpc_decode_uint(ctx);
229227

230-
if (!decoded_ok) {
231-
error = OT_ERROR_INVALID_ARGS;
232-
goto out;
228+
if (!nrf_rpc_decoding_done_and_check(group, ctx)) {
229+
ot_rpc_report_cmd_decoding_error(OT_RPC_CMD_SET_STATE_CHANGED_CALLBACK);
230+
return;
233231
}
234232

235233
cb = ot_rpc_callback_put(callback, context);
@@ -246,7 +244,7 @@ static void ot_rpc_cmd_set_state_changed_callback(const struct nrf_rpc_group *gr
246244

247245
out:
248246
NRF_RPC_CBOR_ALLOC(group, rsp_ctx, 5);
249-
zcbor_uint32_put(rsp_ctx.zs, (uint32_t)error);
247+
nrf_rpc_encode_uint(&rsp_ctx, error);
250248
nrf_rpc_cbor_rsp_no_err(group, &rsp_ctx);
251249
}
252250

@@ -260,16 +258,15 @@ static void ot_rpc_cmd_remove_state_changed_callback(const struct nrf_rpc_group
260258
{
261259
uint32_t callback;
262260
uint32_t context;
263-
bool decoded_ok;
264261
ot_rpc_callback_t *cb;
265262
struct nrf_rpc_cbor_ctx rsp_ctx;
266263

267-
decoded_ok = zcbor_uint32_decode(ctx->zs, &callback);
268-
decoded_ok = decoded_ok && zcbor_uint32_decode(ctx->zs, &context);
269-
nrf_rpc_cbor_decoding_done(group, ctx);
264+
callback = nrf_rpc_decode_uint(ctx);
265+
context = nrf_rpc_decode_uint(ctx);
270266

271-
if (!decoded_ok) {
272-
goto out;
267+
if (!nrf_rpc_decoding_done_and_check(group, ctx)) {
268+
ot_rpc_report_cmd_decoding_error(OT_RPC_CMD_REMOVE_STATE_CHANGED_CALLBACK);
269+
return;
273270
}
274271

275272
cb = ot_rpc_callback_del(callback, context);

0 commit comments

Comments
 (0)