Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net: rpc: openthread: dynamic allocation for CLI commands #19228

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions include/nrf_rpc/nrf_rpc_serialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,15 @@ int64_t nrf_rpc_decode_int64(struct nrf_rpc_cbor_ctx *ctx);
*/
char *nrf_rpc_decode_str(struct nrf_rpc_cbor_ctx *ctx, char *buffer, size_t buffer_size);

/** @brief Decode a string pointer and length. Moves CBOR pointer past string on success.
*
* @param[in,out] ctx CBOR decoding context.
* @param[out] size String length.
*
* @retval Pointer to a string within CBOR stream or NULL on error.
*/
const void *nrf_rpc_decode_str_ptr_and_len(struct nrf_rpc_cbor_ctx *ctx, size_t *len);

/** Decode a string value into a scratchpad.
*
* @param[in] scratchpad Pointer to the scratchpad.
Expand Down
21 changes: 16 additions & 5 deletions subsys/net/openthread/rpc/server/ot_rpc_cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,32 +68,43 @@ NRF_RPC_CBOR_CMD_DECODER(ot_group, ot_rpc_cmd_cli_init, OT_RPC_CMD_CLI_INIT, ot_
static void ot_rpc_cmd_cli_input_line(const struct nrf_rpc_group *group,
struct nrf_rpc_cbor_ctx *ctx, void *handler_data)
{
char input_line_buffer[256];
struct nrf_rpc_cbor_ctx rsp_ctx;
char *result;
char *buffer;
const void *ptr;
size_t len;

/* Parse the input */
result = nrf_rpc_decode_str(ctx, input_line_buffer, sizeof(input_line_buffer));
ptr = nrf_rpc_decode_str_ptr_and_len(ctx, &len);

if (ptr) {
buffer = malloc(len + 1);
if (buffer) {
memcpy(buffer, ptr, len);
buffer[len] = '\0';
}
}

if (!nrf_rpc_decoding_done_and_check(group, ctx)) {
ot_rpc_report_cmd_decoding_error(OT_RPC_CMD_CLI_INPUT_LINE);
return;
}

if (result == NULL) {
if (!ptr || !buffer) {
return;
}

/* Execute OT CLI command */
openthread_api_mutex_lock(openthread_get_default_context());
otCliInputLine(input_line_buffer);
otCliInputLine(buffer);
openthread_api_mutex_unlock(openthread_get_default_context());

/* Encode and send the response */

NRF_RPC_CBOR_ALLOC(group, rsp_ctx, 0);

nrf_rpc_cbor_rsp_no_err(group, &rsp_ctx);

free(buffer);
}

NRF_RPC_CBOR_CMD_DECODER(ot_group, ot_rpc_cmd_cli_input_line, OT_RPC_CMD_CLI_INPUT_LINE,
Expand Down
26 changes: 26 additions & 0 deletions subsys/nrf_rpc/nrf_rpc_serialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,32 @@ char *nrf_rpc_decode_str(struct nrf_rpc_cbor_ctx *ctx, char *buffer, size_t buff
return buffer;
}

const void *nrf_rpc_decode_str_ptr_and_len(struct nrf_rpc_cbor_ctx *ctx, size_t *len)
{
struct zcbor_string zst = {0};

if (is_decoder_invalid(ctx)) {
return NULL;
}

if (zcbor_nil_expect(ctx->zs, NULL)) {
return NULL;
}

if (ctx->zs->constant_state->error != ZCBOR_ERR_WRONG_TYPE) {
return NULL;
}

zcbor_pop_error(ctx->zs);

if (!zcbor_tstr_decode(ctx->zs, &zst)) {
return NULL;
}

*len = zst.len;
return zst.value;
}

char *nrf_rpc_decode_str_into_scratchpad(struct nrf_rpc_scratchpad *scratchpad, size_t *len)
{
struct nrf_rpc_cbor_ctx *ctx = scratchpad->ctx;
Expand Down
Loading