Skip to content

Commit 59a9c3d

Browse files
committed
nrf_rpc: remove sending from rx thread
Commit removes any data sending over serial transport from rx thread and adds sending over allocated pool of threads for incoming data execution. The reason of this: if sending requires some acknowledgements then deadlock of rx thread happens. Signed-off-by: Aleksandr Khromykh <[email protected]>
1 parent 380330c commit 59a9c3d

File tree

1 file changed

+50
-9
lines changed

1 file changed

+50
-9
lines changed

nrf_rpc/nrf_rpc.c

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,19 @@ struct init_packet_data {
8585
const char *strid; /* Remote group unique identifier. */
8686
};
8787

88+
enum internal_pool_data_type {
89+
NRF_RPC_INITIALIZATION,
90+
NRF_RPC_ERROR
91+
};
92+
93+
struct internal_pool_data {
94+
enum internal_pool_data_type type;
95+
const struct nrf_rpc_group *group;
96+
int err;
97+
uint8_t hdr_id;
98+
uint8_t hdr_type;
99+
};
100+
88101
/* Pool of statically allocated command contexts. */
89102
static struct nrf_rpc_cmd_ctx cmd_ctx_pool[CONFIG_NRF_RPC_CMD_CTX_POOL_SIZE];
90103

@@ -102,6 +115,8 @@ static bool is_initialized;
102115
/* Error handler provided to the init function. */
103116
static nrf_rpc_err_handler_t global_err_handler;
104117

118+
static struct internal_pool_data internal_data;
119+
105120
/* Array with all defiend groups */
106121
NRF_RPC_AUTO_ARR(nrf_rpc_groups_array, "grp");
107122

@@ -343,6 +358,24 @@ static inline bool packet_validate(const uint8_t *packet)
343358
(addr < (uintptr_t)0 - (uintptr_t)NRF_RPC_HEADER_SIZE);
344359
}
345360

361+
static void internal_tx_handler(void)
362+
{
363+
struct internal_pool_data copy = internal_data;
364+
365+
nrf_rpc_os_event_set(&copy.group->data->decode_done_event);
366+
367+
if (copy.type == NRF_RPC_INITIALIZATION) {
368+
if (group_init_send(copy.group)) {
369+
NRF_RPC_ERR("Failed to send group init packet for group id: %d strid: %s",
370+
copy.group->data->src_group_id, copy.group->strid);
371+
}
372+
}
373+
374+
if (copy.type == NRF_RPC_ERROR) {
375+
nrf_rpc_err(copy.err, NRF_RPC_ERR_SRC_RECV, copy.group, copy.hdr_id, copy.hdr_type);
376+
}
377+
}
378+
346379
static int transport_init(nrf_rpc_tr_receive_handler_t receive_cb)
347380
{
348381
int err = 0;
@@ -513,7 +546,11 @@ static uint8_t parse_incoming_packet(struct nrf_rpc_cmd_ctx *cmd_ctx,
513546
/* Thread pool callback */
514547
static void execute_packet(const uint8_t *packet, size_t len)
515548
{
516-
parse_incoming_packet(NULL, packet, len);
549+
if (packet == (const uint8_t *)&internal_data) {
550+
internal_tx_handler();
551+
} else {
552+
parse_incoming_packet(NULL, packet, len);
553+
}
517554
}
518555

519556
static bool protocol_version_check(const struct init_packet_data *init_data)
@@ -636,14 +673,13 @@ static int init_packet_handle(struct header *hdr, const struct nrf_rpc_group **g
636673
* If remote processor does not know our group id, send an init packet back,
637674
* since it might have missed our original init packet.
638675
*/
639-
err = group_init_send(*group);
640-
if (err) {
641-
NRF_RPC_ERR("Failed to send group init packet for group id: %d strid: %s",
642-
group_data->src_group_id, (**group).strid);
643-
}
676+
internal_data.type = NRF_RPC_INITIALIZATION;
677+
internal_data.group = *group;
678+
nrf_rpc_os_thread_pool_send((const uint8_t *)&internal_data, sizeof(internal_data));
679+
nrf_rpc_os_event_wait(&(*group)->data->decode_done_event, NRF_RPC_OS_WAIT_FOREVER);
644680
}
645681

646-
return err;
682+
return 0;
647683
}
648684

649685
/* Callback from transport layer that handles incoming. */
@@ -765,8 +801,13 @@ static void receive_handler(const struct nrf_rpc_tr *transport, const uint8_t *p
765801
}
766802

767803
if (err < 0) {
768-
nrf_rpc_err(err, NRF_RPC_ERR_SRC_RECV, group, hdr.id,
769-
hdr.type);
804+
internal_data.type = NRF_RPC_ERROR;
805+
internal_data.group = group;
806+
internal_data.err = err;
807+
internal_data.hdr_id = hdr.id;
808+
internal_data.hdr_type = hdr.type;
809+
nrf_rpc_os_thread_pool_send((const uint8_t *)&internal_data, sizeof(internal_data));
810+
nrf_rpc_os_event_wait(&group->data->decode_done_event, NRF_RPC_OS_WAIT_FOREVER);
770811
}
771812
}
772813

0 commit comments

Comments
 (0)