Skip to content

Commit f777f4f

Browse files
committed
nrf_rpc: add nrf_rpc_setup, nrf_rpc_bind and nrf_rpc_unbind
1. Add nrf_rpc_setup() and nrf_rpc_bind() experimental APIs that behave together like existing nrf_rpc_init(). 2. Add nrf_rpc_unbind() which clears the effects of former calling nrf_rpc_bind(). These are needed to support re-connecting to the peer, for example after noticing that a peer has been reset, and the peer is not a group initiator (thus, it does not send an nRF RPC initialization packet automatically at startup). Signed-off-by: Damian Krolik <[email protected]>
1 parent 07359b5 commit f777f4f

File tree

2 files changed

+124
-52
lines changed

2 files changed

+124
-52
lines changed

nrf_rpc/include/nrf_rpc.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,8 @@ struct nrf_rpc_cleanup_handler
363363
void nrf_rpc_set_bound_handler(nrf_rpc_group_bound_handler_t bound_handler);
364364

365365
/** @brief Initialize the nRF RPC
366+
*
367+
* Calling this function is equivalent to calling both @ref nrf_rpc_setup and @ref nrf_rpc_bind.
366368
*
367369
* @param err_handler Error handler that will be called to report error in
368370
* nRF RPC.
@@ -371,6 +373,52 @@ void nrf_rpc_set_bound_handler(nrf_rpc_group_bound_handler_t bound_handler);
371373
*/
372374
int nrf_rpc_init(nrf_rpc_err_handler_t err_handler);
373375

376+
/** @brief Initialize the nRF RPC internal state.
377+
*
378+
* Sets up all variables related to nRF RPC, including contexts, groups, and transports.
379+
* It does not initiate any communication with the peer.
380+
*
381+
* @note This function can be invoked multiple times; however, only the first call has an effect.
382+
* @note This function is not thread-safe and should not be called concurrently from multiple
383+
* threads.
384+
*
385+
* @warning This function is experimental and subject to change or removal without prior notice.
386+
*
387+
* @param err_handler Error handler that will be called to report an error in the nRF RPC.
388+
* @param bound_handler Bound handler that will be called when a group is bound with the peer.
389+
*
390+
* @return 0 on success or negative error code.
391+
*/
392+
int nrf_rpc_setup(nrf_rpc_err_handler_t err_handler, nrf_rpc_group_bound_handler_t bound_handler);
393+
394+
/** @brief Binds the nRF RPC groups.
395+
*
396+
* Sends an initializaton packet to the peer for each group defined with the @ref
397+
* NRF_RPC_FLAGS_INITIATOR flag. It then waits until all groups defined with the @ref
398+
* NRF_RPC_FLAGS_WAIT_ON_INIT flag have been bound.
399+
*
400+
* A group is considered bound when an initialization packet for that group is received from the
401+
* peer, indicating the numerical identifier assigned to the group by the peer.
402+
*
403+
* The function may time out if some groups are not bound within the duration specified by the
404+
* CONFIG_NRF_RPC_GROUP_INIT_WAIT_TIME Kconfig option.
405+
*
406+
* @note This function is not thread-safe; should not be called concurrently from multiple threads.
407+
*
408+
* @warning This function is experimental and subject to change or removal without prior notice.
409+
*
410+
* @return 0 on success or negative error code.
411+
*/
412+
int nrf_rpc_bind(void);
413+
414+
/** @brief Unbinds the nRF RPC groups.
415+
*
416+
* Resets the effect of @ref nrf_rpc_bind so that no groups are considered bound after returning
417+
* from this function.
418+
*
419+
* @warning This function is experimental and subject to change or removal without prior notice.
420+
*/
421+
void nrf_rpc_unbind(void);
374422

375423
/** @brief Registers the cleanup handler.
376424
*

nrf_rpc/nrf_rpc.c

Lines changed: 76 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -419,48 +419,6 @@ static void internal_tx_handler(void)
419419
}
420420
}
421421

422-
static int transport_init(nrf_rpc_tr_receive_handler_t receive_cb)
423-
{
424-
int err = 0;
425-
void *iter;
426-
const struct nrf_rpc_group *group;
427-
428-
for (NRF_RPC_AUTO_ARR_FOR(iter, group, &nrf_rpc_groups_array,
429-
const struct nrf_rpc_group)) {
430-
const struct nrf_rpc_tr *transport = group->transport;
431-
struct nrf_rpc_group_data *data = group->data;
432-
433-
err = transport->api->init(transport, receive_cb, NULL);
434-
if (err) {
435-
NRF_RPC_ERR("Failed to initialize transport, err: %d", err);
436-
continue;
437-
}
438-
439-
group->data->transport_initialized = true;
440-
441-
if (group->flags & NRF_RPC_FLAGS_INITIATOR) {
442-
err = group_init_send(group);
443-
if (err) {
444-
NRF_RPC_ERR("Failed to send group init packet for group id: %d strid: %s err: %d",
445-
data->src_group_id, group->strid, err);
446-
continue;
447-
}
448-
}
449-
}
450-
451-
/* Group initialization errors are not propagated to the caller. */
452-
err = 0;
453-
454-
if (waiting_group_count > 0) {
455-
err = nrf_rpc_os_event_wait(&groups_init_event, CONFIG_NRF_RPC_GROUP_INIT_WAIT_TIME);
456-
if (err) {
457-
NRF_RPC_ERR("Not all groups are ready to use.");
458-
}
459-
}
460-
461-
return err;
462-
}
463-
464422
/* ======================== Receiving Packets ======================== */
465423

466424
/* Find in array and execute command or event handler */
@@ -1143,10 +1101,9 @@ void nrf_rpc_set_bound_handler(nrf_rpc_group_bound_handler_t bound_handler)
11431101
global_bound_handler = bound_handler;
11441102
}
11451103

1146-
int nrf_rpc_init(nrf_rpc_err_handler_t err_handler)
1104+
int nrf_rpc_setup(nrf_rpc_err_handler_t err_handler, nrf_rpc_group_bound_handler_t bound_handler)
11471105
{
11481106
int err;
1149-
int i;
11501107
void *iter;
11511108
const struct nrf_rpc_group *group;
11521109
uint8_t group_id = 0;
@@ -1161,6 +1118,7 @@ int nrf_rpc_init(nrf_rpc_err_handler_t err_handler)
11611118
nrf_rpc_os_mutex_init(&cleanup_mutex);
11621119

11631120
global_err_handler = err_handler;
1121+
global_bound_handler = bound_handler;
11641122

11651123
for (NRF_RPC_AUTO_ARR_FOR(iter, group, &nrf_rpc_groups_array,
11661124
const struct nrf_rpc_group)) {
@@ -1194,8 +1152,6 @@ int nrf_rpc_init(nrf_rpc_err_handler_t err_handler)
11941152
group_count = group_id;
11951153
waiting_group_count = wait_count;
11961154

1197-
memset(&cmd_ctx_pool, 0, sizeof(cmd_ctx_pool));
1198-
11991155
err = nrf_rpc_os_init(execute_packet);
12001156
if (err < 0) {
12011157
return err;
@@ -1211,7 +1167,7 @@ int nrf_rpc_init(nrf_rpc_err_handler_t err_handler)
12111167
return err;
12121168
}
12131169

1214-
for (i = 0; i < CONFIG_NRF_RPC_CMD_CTX_POOL_SIZE; i++) {
1170+
for (int i = 0; i < CONFIG_NRF_RPC_CMD_CTX_POOL_SIZE; i++) {
12151171
cmd_ctx_pool[i].id = i;
12161172
err = nrf_rpc_os_mutex_init(&cmd_ctx_pool[i].mutex);
12171173
if (err < 0) {
@@ -1223,17 +1179,85 @@ int nrf_rpc_init(nrf_rpc_err_handler_t err_handler)
12231179
}
12241180
}
12251181

1226-
err = transport_init(receive_handler);
1227-
if (err < 0) {
1228-
return err;
1229-
}
1230-
12311182
is_initialized = true;
12321183
NRF_RPC_DBG("Done initializing nRF RPC module");
12331184

12341185
return err;
12351186
}
12361187

1188+
int nrf_rpc_bind(void)
1189+
{
1190+
int err;
1191+
void *iter;
1192+
const struct nrf_rpc_group *group;
1193+
1194+
for (NRF_RPC_AUTO_ARR_FOR(iter, group, &nrf_rpc_groups_array, const struct nrf_rpc_group)) {
1195+
const struct nrf_rpc_tr *transport = group->transport;
1196+
struct nrf_rpc_group_data *data = group->data;
1197+
1198+
if (!group->data->transport_initialized) {
1199+
err = transport->api->init(transport, receive_handler, NULL);
1200+
if (err) {
1201+
NRF_RPC_ERR("Failed to initialize transport, err: %d", err);
1202+
continue;
1203+
}
1204+
1205+
group->data->transport_initialized = true;
1206+
}
1207+
1208+
if (group->flags & NRF_RPC_FLAGS_INITIATOR) {
1209+
err = group_init_send(group);
1210+
if (err) {
1211+
NRF_RPC_ERR("Failed to send group init packet for group id: %d "
1212+
"strid: %s err: %d",
1213+
data->src_group_id, group->strid, err);
1214+
continue;
1215+
}
1216+
}
1217+
}
1218+
1219+
/* Group initialization errors are not propagated to the caller. */
1220+
err = 0;
1221+
1222+
if (waiting_group_count > 0) {
1223+
err = nrf_rpc_os_event_wait(&groups_init_event,
1224+
CONFIG_NRF_RPC_GROUP_INIT_WAIT_TIME);
1225+
if (err) {
1226+
NRF_RPC_ERR("Not all groups are ready to use.");
1227+
}
1228+
}
1229+
1230+
return err;
1231+
}
1232+
1233+
void nrf_rpc_unbind(void)
1234+
{
1235+
void *iter;
1236+
const struct nrf_rpc_group *group;
1237+
1238+
initialized_group_count = 0;
1239+
1240+
for (NRF_RPC_AUTO_ARR_FOR(iter, group, &nrf_rpc_groups_array, const struct nrf_rpc_group)) {
1241+
group->data->dst_group_id = NRF_RPC_ID_UNKNOWN;
1242+
}
1243+
}
1244+
1245+
int nrf_rpc_init(nrf_rpc_err_handler_t err_handler)
1246+
{
1247+
int err;
1248+
1249+
if (is_initialized) {
1250+
return 0;
1251+
}
1252+
1253+
err = nrf_rpc_setup(err_handler, global_bound_handler);
1254+
if (err < 0) {
1255+
return err;
1256+
}
1257+
1258+
return nrf_rpc_bind();
1259+
}
1260+
12371261
void nrf_rpc_register_cleanup_handler(struct nrf_rpc_cleanup_handler *handler)
12381262
{
12391263
nrf_rpc_os_mutex_lock(&cleanup_mutex);

0 commit comments

Comments
 (0)