nrfxlib API 3.3.99
Loading...
Searching...
No Matches
nrf_rpc.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5 */
6
7#ifndef _NRF_RPC_H_
8#define _NRF_RPC_H_
9
10#include <stdint.h>
11#include <stdbool.h>
12#include <stddef.h>
13
14#include <nrf_rpc_errno.h>
15#include <nrf_rpc_common.h>
16#include <nrf_rpc_tr.h>
17#include <nrf_rpc_os.h>
18
25#ifdef __cplusplus
26extern "C" {
27#endif
28
30#define NRF_RPC_PROTOCOL_VERSION 0
31
33#define NRF_RPC_ID_UNKNOWN 0xFF
34
36#define NRF_RPC_FLAGS_WAIT_ON_INIT 0x01
37
39#define NRF_RPC_FLAGS_INITIATOR 0x02
40
42#define NRF_RPC_FLAG_COND(_cond, _flag) ((_cond) ? (_flag) : 0UL)
43
44/* Forward declaration. */
46struct nrf_rpc_group;
47
60
70
71/* Helper nRF group structure declaration needed for callback definition. */
72struct nrf_rpc_group;
73
81typedef void (*nrf_rpc_handler_t)(const struct nrf_rpc_group *group, const uint8_t *packet,
82 size_t len, void *handler_data);
83
91typedef void (*nrf_rpc_ack_handler_t)(uint8_t id, void *handler_data);
92
107typedef void (*nrf_rpc_err_handler_t)(const struct nrf_rpc_err_report *report);
108
115typedef void (*nrf_rpc_group_bound_handler_t)(const struct nrf_rpc_group *group);
116
117/* Structure used internally to define registered command or event decoder. */
118struct _nrf_rpc_decoder {
119 uint8_t id;
120 nrf_rpc_handler_t handler;
121 void *handler_data;
122};
123
128 struct nrf_rpc_os_event decode_done_event;
130};
131
151
155
157 int code;
158
160 const struct nrf_rpc_group *group;
161
164
166 uint8_t id;
167
172
174 const char *file;
175
177 int line;
178
180 const char *func;
181};
182
186{
188 void (*handler)(void *context);
189
191 void *context;
192
195};
196
220#define NRF_RPC_GROUP_DEFINE_INTERNAL__(_name, _strid, _transport, _ack_handler, \
221 _ack_data, _err_handler, _bound_handler, \
222 _wait_on_init, _initiator) \
223 NRF_RPC_AUTO_ARR(NRF_RPC_CONCAT(_name, _cmd_array), \
224 "cmd_" NRF_RPC_STRINGIFY(_name)); \
225 NRF_RPC_AUTO_ARR(NRF_RPC_CONCAT(_name, _evt_array), \
226 "evt_" NRF_RPC_STRINGIFY(_name)); \
227 \
228 static struct nrf_rpc_group_data NRF_RPC_CONCAT(_name, _group_data) = { \
229 .src_group_id = NRF_RPC_ID_UNKNOWN, \
230 .dst_group_id = NRF_RPC_ID_UNKNOWN, \
231 .transport_initialized = false, \
232 }; \
233 \
234 NRF_RPC_AUTO_ARR_ITEM(const struct nrf_rpc_group, _name, "grp", \
235 _strid) = { \
236 .cmd_array = &NRF_RPC_CONCAT(_name, _cmd_array), \
237 .evt_array = &NRF_RPC_CONCAT(_name, _evt_array), \
238 .data = &NRF_RPC_CONCAT(_name, _group_data), \
239 .ack_handler = _ack_handler, \
240 .ack_handler_data = _ack_data, \
241 .strid = _strid, \
242 .transport = _transport, \
243 .err_handler = _err_handler, \
244 .bound_handler = _bound_handler, \
245 .flags = NRF_RPC_FLAG_COND(_wait_on_init, NRF_RPC_FLAGS_WAIT_ON_INIT) \
246 | NRF_RPC_FLAG_COND(_initiator, NRF_RPC_FLAGS_INITIATOR), \
247 }
248
264#define NRF_RPC_GROUP_DEFINE(_name, _strid, _transport, _ack_handler, _ack_data, _err_handler) \
265 NRF_RPC_GROUP_DEFINE_INTERNAL__(_name, _strid, _transport, _ack_handler, _ack_data, \
266 _err_handler, NULL, \
267 IS_ENABLED(CONFIG_NRF_RPC_GROUP_DEFAULT_WAIT_ON_INIT), \
268 IS_ENABLED(CONFIG_NRF_RPC_GROUP_DEFAULT_INITIATOR))
269
299#define NRF_RPC_GROUP_DEFINE_NOWAIT(_name, _strid, _transport, _ack_handler, \
300 _ack_data, _err_handler, _bound_handler, \
301 _initiator) \
302 NRF_RPC_GROUP_DEFINE_INTERNAL__(_name, _strid, _transport, _ack_handler, \
303 _ack_data, _err_handler, _bound_handler, \
304 false, _initiator) \
305
312#define NRF_RPC_GROUP_DECLARE(_name) \
313 extern const struct nrf_rpc_group _name
314
324#define NRF_RPC_CMD_DECODER(_group, _name, _cmd, _handler, _data) \
325 NRF_RPC_STATIC_ASSERT(_cmd <= 0xFE, "Command out of range"); \
326 NRF_RPC_AUTO_ARR_ITEM(const struct _nrf_rpc_decoder, \
327 NRF_RPC_CONCAT(_name, _cmd_dec), \
328 "cmd_" NRF_RPC_STRINGIFY(_group), \
329 NRF_RPC_STRINGIFY(_name)) = { \
330 .id = _cmd, \
331 .handler = _handler, \
332 .handler_data = _data, \
333 }
334
344#define NRF_RPC_EVT_DECODER(_group, _name, _evt, _handler, _data) \
345 NRF_RPC_STATIC_ASSERT(_evt <= 0xFE, "Event out of range"); \
346 NRF_RPC_AUTO_ARR_ITEM(const struct _nrf_rpc_decoder, \
347 NRF_RPC_CONCAT(_name, _evt_dec), \
348 "evt_" NRF_RPC_STRINGIFY(_group), \
349 NRF_RPC_STRINGIFY(_name)) = { \
350 .id = _evt, \
351 .handler = _handler, \
352 .handler_data = _data, \
353 }
354
361#define NRF_RPC_GROUP_STATUS(_group) \
362 (_group.data->transport_initialized && (_group.data->dst_group_id != NRF_RPC_ID_UNKNOWN))
363
373
384
402
421int nrf_rpc_bind(void);
422
430void nrf_rpc_unbind(void);
431
443
444
456void nrf_rpc_stop(bool cleanup);
457
463void nrf_rpc_resume(void);
464
465
481int nrf_rpc_cmd(const struct nrf_rpc_group *group, uint8_t cmd, uint8_t *packet,
482 size_t len, nrf_rpc_handler_t handler, void *handler_data);
483
502int nrf_rpc_cmd_rsp(const struct nrf_rpc_group *group, uint8_t cmd, uint8_t *packet,
503 size_t len, const uint8_t **rsp_packet, size_t *rsp_len);
504
522void nrf_rpc_cmd_no_err(const struct nrf_rpc_group *group, uint8_t cmd, uint8_t *packet,
523 size_t len, nrf_rpc_handler_t handler, void *handler_data);
524
539void nrf_rpc_cmd_rsp_no_err(const struct nrf_rpc_group *group, uint8_t cmd,
540 uint8_t *packet, size_t len, const uint8_t **rsp_packet,
541 size_t *rsp_len);
542
554int nrf_rpc_evt(const struct nrf_rpc_group *group, uint8_t evt, uint8_t *packet,
555 size_t len);
556
569void nrf_rpc_evt_no_err(const struct nrf_rpc_group *group, uint8_t evt,
570 uint8_t *packet, size_t len);
571
582int nrf_rpc_rsp(const struct nrf_rpc_group *group, uint8_t *packet, size_t len);
583
595void nrf_rpc_rsp_no_err(const struct nrf_rpc_group *group, uint8_t *packet, size_t len);
596
608void nrf_rpc_decoding_done(const struct nrf_rpc_group *group, const uint8_t *packet);
609
622void nrf_rpc_err(int code, enum nrf_rpc_err_src src,
623 const struct nrf_rpc_group *group, uint8_t id,
624 uint8_t packet_type);
625
640void nrf_rpc_err_impl(int code, enum nrf_rpc_err_src src,
641 const struct nrf_rpc_group *group, uint8_t id,
642 uint8_t packet_type, const char *file, int line, const char *func);
643
644#ifdef CONFIG_NRF_RPC_DETAILED_ERROR_REPORTING
645#define nrf_rpc_err(_code, _src, _group, _id, _packet_type) \
646 nrf_rpc_err_impl(_code, _src, _group, _id, _packet_type, __FILE__, __LINE__, __func__)
647#endif
648
658void nrf_rpc_alloc_tx_buf(const struct nrf_rpc_group *group, uint8_t **buf, size_t len);
659
668void nrf_rpc_free_tx_buf(const struct nrf_rpc_group *group, uint8_t *buf);
669
674#ifdef __cplusplus
675}
676#endif
677
678#endif /* _NRF_RPC_H_ */
void nrf_rpc_cmd_rsp_no_err(const struct nrf_rpc_group *group, uint8_t cmd, uint8_t *packet, size_t len, const uint8_t **rsp_packet, size_t *rsp_len)
Send a command, get response as an output parameter and pass any error to an error handler.
void nrf_rpc_cmd_no_err(const struct nrf_rpc_group *group, uint8_t cmd, uint8_t *packet, size_t len, nrf_rpc_handler_t handler, void *handler_data)
Send a command, provide callback to handle response and pass any error to an error handler.
int nrf_rpc_cmd_rsp(const struct nrf_rpc_group *group, uint8_t cmd, uint8_t *packet, size_t len, const uint8_t **rsp_packet, size_t *rsp_len)
Send a command and get response as an output parameter.
int nrf_rpc_rsp(const struct nrf_rpc_group *group, uint8_t *packet, size_t len)
Send a response.
void(* nrf_rpc_err_handler_t)(const struct nrf_rpc_err_report *report)
Callback to report error that cannot be returned by the API call.
Definition nrf_rpc.h:107
int nrf_rpc_bind(void)
Binds the nRF RPC groups.
void nrf_rpc_unbind(void)
Unbinds the nRF RPC groups.
void nrf_rpc_set_bound_handler(nrf_rpc_group_bound_handler_t bound_handler)
Register a global bound handler.
void nrf_rpc_rsp_no_err(const struct nrf_rpc_group *group, uint8_t *packet, size_t len)
Send a response and pass any error to an error handler.
void nrf_rpc_stop(bool cleanup)
Temporarily suspend all RPC communication.
void nrf_rpc_free_tx_buf(const struct nrf_rpc_group *group, uint8_t *buf)
Deallocates Tx buffer.
nrf_rpc_err_src
Error source.
Definition nrf_rpc.h:65
@ NRF_RPC_ERR_SRC_REMOTE
Error reported be the remote.
Definition nrf_rpc.h:68
@ NRF_RPC_ERR_SRC_RECV
Error during receiving.
Definition nrf_rpc.h:66
@ NRF_RPC_ERR_SRC_SEND
Error during sending.
Definition nrf_rpc.h:67
int nrf_rpc_cmd(const struct nrf_rpc_group *group, uint8_t cmd, uint8_t *packet, size_t len, nrf_rpc_handler_t handler, void *handler_data)
Send a command and provide callback to handle response.
int nrf_rpc_setup(nrf_rpc_err_handler_t err_handler, nrf_rpc_group_bound_handler_t bound_handler)
Initialize the nRF RPC internal state.
void(* nrf_rpc_ack_handler_t)(uint8_t id, void *handler_data)
Callback called when ACK was received.
Definition nrf_rpc.h:91
void nrf_rpc_resume(void)
resumes RPC communication
void nrf_rpc_err_impl(int code, enum nrf_rpc_err_src src, const struct nrf_rpc_group *group, uint8_t id, uint8_t packet_type, const char *file, int line, const char *func)
Report an error to nRF RPC error handler.
int nrf_rpc_init(nrf_rpc_err_handler_t err_handler)
Initialize the nRF RPC.
void nrf_rpc_register_cleanup_handler(struct nrf_rpc_cleanup_handler *handler)
Registers the cleanup handler.
void nrf_rpc_evt_no_err(const struct nrf_rpc_group *group, uint8_t evt, uint8_t *packet, size_t len)
Send an event and pass any error to an error handler.
void(* nrf_rpc_handler_t)(const struct nrf_rpc_group *group, const uint8_t *packet, size_t len, void *handler_data)
Callback that handles decoding of commands, events and responses.
Definition nrf_rpc.h:81
nrf_rpc_packet_type
Type of packet.
Definition nrf_rpc.h:52
@ NRF_RPC_PACKET_TYPE_ERR
Error report from remote.
Definition nrf_rpc.h:56
@ NRF_RPC_PACKET_TYPE_ACK
Event acknowledge.
Definition nrf_rpc.h:55
@ NRF_RPC_PACKET_TYPE_CMD
Command.
Definition nrf_rpc.h:58
@ NRF_RPC_PACKET_TYPE_INIT
Initialization packet.
Definition nrf_rpc.h:57
@ NRF_RPC_PACKET_TYPE_RSP
Response.
Definition nrf_rpc.h:54
@ NRF_RPC_PACKET_TYPE_EVT
Event.
Definition nrf_rpc.h:53
void(* nrf_rpc_group_bound_handler_t)(const struct nrf_rpc_group *group)
Callback called when the command group is bound.
Definition nrf_rpc.h:115
int nrf_rpc_evt(const struct nrf_rpc_group *group, uint8_t evt, uint8_t *packet, size_t len)
Send an event.
void nrf_rpc_decoding_done(const struct nrf_rpc_group *group, const uint8_t *packet)
Indicate that decoding of the input packet is done.
void nrf_rpc_err(int code, enum nrf_rpc_err_src src, const struct nrf_rpc_group *group, uint8_t id, uint8_t packet_type)
Report an error to nRF RPC error handler.
void nrf_rpc_alloc_tx_buf(const struct nrf_rpc_group *group, uint8_t **buf, size_t len)
Allocates buffer for a packet.
void(* handler)(void *context)
Handler to the cleanup function invoked on the call to nrf_rpc_stop.
Definition nrf_rpc.h:188
struct nrf_rpc_cleanup_handler * next
Pointer to the next element in the list. Managed automatically by the nrf_rpc.
Definition nrf_rpc.h:194
void * context
Custom context passed as the function parameter.
Definition nrf_rpc.h:191
Cleanup procedure list element.
Definition nrf_rpc.h:186
int code
Error code.
Definition nrf_rpc.h:157
const char * file
File where the error was reported.
Definition nrf_rpc.h:174
const char * func
Function where the error was reported.
Definition nrf_rpc.h:180
int line
Line number where the error was reported.
Definition nrf_rpc.h:177
enum nrf_rpc_packet_type packet_type
Type of packet. Value may be outside defined enum values if packet is malformed.
Definition nrf_rpc.h:171
enum nrf_rpc_err_src src
Source of the error.
Definition nrf_rpc.h:163
const struct nrf_rpc_group * group
Group where the error occurred or NULL if it is unknown.
Definition nrf_rpc.h:160
uint8_t id
Command or event id or NRF_RPC_ID_UNKNOWN.
Definition nrf_rpc.h:166
Error report.
Definition nrf_rpc.h:154
uint8_t dst_group_id
Definition nrf_rpc.h:127
bool transport_initialized
Definition nrf_rpc.h:129
struct nrf_rpc_os_event decode_done_event
Definition nrf_rpc.h:128
uint8_t src_group_id
Definition nrf_rpc.h:126
Group data structure. It contains no constant group data.
Definition nrf_rpc.h:125
nrf_rpc_err_handler_t err_handler
Definition nrf_rpc.h:147
nrf_rpc_ack_handler_t ack_handler
Definition nrf_rpc.h:144
nrf_rpc_group_bound_handler_t bound_handler
Definition nrf_rpc.h:148
struct nrf_rpc_group_data * data
Definition nrf_rpc.h:142
void * ack_handler_data
Definition nrf_rpc.h:145
const uint32_t flags
Definition nrf_rpc.h:149
const void * cmd_array
Definition nrf_rpc.h:140
const struct nrf_rpc_tr * transport
Definition nrf_rpc.h:143
const char * strid
Definition nrf_rpc.h:146
const void * evt_array
Definition nrf_rpc.h:141
Defines a group of commands and events.
Definition nrf_rpc.h:139
nRF RPC transport structure.
Definition nrf_rpc_tr.h:92