Skip to content

Commit 7777351

Browse files
Damian-Nordicrlubos
authored andcommitted
nrf_rpc: detect reboot of the remote
Add nrf_rpc_set_bound_handler() that allows to register a global handler that is called when the remote binds to a local nRF RPC group (either for the first time, or after a reboot. Signed-off-by: Damian Krolik <[email protected]>
1 parent b3f964a commit 7777351

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

nrf_rpc/include/nrf_rpc.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,16 @@ struct nrf_rpc_err_report {
338338
#define NRF_RPC_GROUP_STATUS(_group) \
339339
(_group.data->transport_initialized && (_group.data->dst_group_id != NRF_RPC_ID_UNKNOWN))
340340

341+
/** @brief Register a global bound handler.
342+
*
343+
* Registers a global handler that is called each time the remote peer binds to a local group.
344+
* This can be used by the application to detect that the peer has restarted, to reset any local
345+
* state associated with the peer.
346+
*
347+
* @param bound_handler Bound handler.
348+
*/
349+
void nrf_rpc_set_bound_handler(nrf_rpc_group_bound_handler_t bound_handler);
350+
341351
/** @brief Initialize the nRF RPC
342352
*
343353
* @param err_handler Error handler that will be called to report error in

nrf_rpc/nrf_rpc.c

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ struct internal_task {
9696
union {
9797
struct {
9898
const struct nrf_rpc_group *group;
99-
bool send_reply;
99+
bool first_init;
100+
bool needs_reply;
100101
bool signal_groups_init_event;
101102
} group_init;
102103

@@ -126,6 +127,9 @@ static bool is_initialized;
126127
/* Error handler provided to the init function. */
127128
static nrf_rpc_err_handler_t global_err_handler;
128129

130+
/* Bound group handler provided to the init function. */
131+
static nrf_rpc_group_bound_handler_t global_bound_handler;
132+
129133
static struct internal_task internal_task;
130134
static struct nrf_rpc_os_event internal_task_consumed;
131135

@@ -379,13 +383,19 @@ static void internal_tx_handler(void)
379383
case NRF_RPC_TASK_GROUP_INIT: {
380384
const struct nrf_rpc_group *group = task.group_init.group;
381385

382-
if (task.group_init.send_reply && group_init_send(group)) {
386+
if (task.group_init.needs_reply && group_init_send(group)) {
383387
NRF_RPC_ERR("Failed to send group init packet for group id: %d strid: %s",
384388
group->data->src_group_id, group->strid);
385389
}
386390

387-
if (group->bound_handler != NULL) {
388-
group->bound_handler(group);
391+
if (task.group_init.first_init || task.group_init.needs_reply) {
392+
if (group->bound_handler != NULL) {
393+
group->bound_handler(group);
394+
}
395+
396+
if (global_bound_handler != NULL) {
397+
global_bound_handler(group);
398+
}
389399
}
390400

391401
if (task.group_init.signal_groups_init_event) {
@@ -648,7 +658,7 @@ static int init_packet_handle(struct header *hdr, const struct nrf_rpc_group **g
648658
struct nrf_rpc_group_data *group_data;
649659
bool first_init;
650660
bool signal_groups_init_event = false;
651-
bool send_reply;
661+
bool needs_reply;
652662

653663
*group = NULL;
654664

@@ -697,18 +707,19 @@ static int init_packet_handle(struct header *hdr, const struct nrf_rpc_group **g
697707
* either we are not an initiator, which is indicated by NRF_RPC_FLAGS_INITIATOR
698708
* flag, or the remote has missed our init packet.
699709
*/
700-
send_reply = (hdr->dst_group_id == NRF_RPC_ID_UNKNOWN);
710+
needs_reply = (hdr->dst_group_id == NRF_RPC_ID_UNKNOWN);
701711

702712
/*
703713
* Spawn the async task only if necessary. The async task is used to avoid sending the init
704714
* reply in the transport receive thread. The application is also notified about the group
705715
* initialization from within the task to ensure that when this happens the init reply has
706716
* already been sent and the remote is ready to receive nRF RPC commands.
707717
*/
708-
if (((*group)->bound_handler != NULL) || send_reply || signal_groups_init_event) {
718+
if (first_init || needs_reply || signal_groups_init_event) {
709719
internal_task.type = NRF_RPC_TASK_GROUP_INIT;
710720
internal_task.group_init.group = *group;
711-
internal_task.group_init.send_reply = send_reply;
721+
internal_task.group_init.first_init = first_init;
722+
internal_task.group_init.needs_reply = needs_reply;
712723
internal_task.group_init.signal_groups_init_event = signal_groups_init_event;
713724
nrf_rpc_os_thread_pool_send((const uint8_t *)&internal_task, sizeof(internal_task));
714725
nrf_rpc_os_event_wait(&internal_task_consumed, NRF_RPC_OS_WAIT_FOREVER);
@@ -1075,6 +1086,11 @@ void nrf_rpc_rsp_no_err(const struct nrf_rpc_group *group, uint8_t *packet, size
10751086

10761087
/* ======================== Common API functions ======================== */
10771088

1089+
void nrf_rpc_set_bound_handler(nrf_rpc_group_bound_handler_t bound_handler)
1090+
{
1091+
global_bound_handler = bound_handler;
1092+
}
1093+
10781094
int nrf_rpc_init(nrf_rpc_err_handler_t err_handler)
10791095
{
10801096
int err;

0 commit comments

Comments
 (0)