diff --git a/include/nrf_rpc/nrf_rpc_serialize.h b/include/nrf_rpc/nrf_rpc_serialize.h index 569b41bccd6d..f0d976dc52eb 100644 --- a/include/nrf_rpc/nrf_rpc_serialize.h +++ b/include/nrf_rpc/nrf_rpc_serialize.h @@ -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. diff --git a/subsys/net/openthread/rpc/server/ot_rpc_cli.c b/subsys/net/openthread/rpc/server/ot_rpc_cli.c index 6c04d50ea2ac..ba5fb5a69f11 100644 --- a/subsys/net/openthread/rpc/server/ot_rpc_cli.c +++ b/subsys/net/openthread/rpc/server/ot_rpc_cli.c @@ -68,25 +68,34 @@ 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 */ @@ -94,6 +103,8 @@ static void ot_rpc_cmd_cli_input_line(const struct nrf_rpc_group *group, 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, diff --git a/subsys/nrf_rpc/nrf_rpc_serialize.c b/subsys/nrf_rpc/nrf_rpc_serialize.c index beeac98d0b0a..27792875f48e 100644 --- a/subsys/nrf_rpc/nrf_rpc_serialize.c +++ b/subsys/nrf_rpc/nrf_rpc_serialize.c @@ -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;