Skip to content

Commit

Permalink
net: openthread: rpc: dynamically allocate memory for CLI line
Browse files Browse the repository at this point in the history
CLI commands may require large buffer for command strings. This commit
introduces usage of dynamic allocation to avoid usage of huge static
buffer.

Signed-off-by: Konrad Derda <[email protected]>
  • Loading branch information
konradderda committed Dec 3, 2024
1 parent 2c9bdcf commit e76b1ef
Showing 1 changed file with 16 additions and 5 deletions.
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

0 comments on commit e76b1ef

Please sign in to comment.