nrfxlib API 2.8.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};
173
197#define NRF_RPC_GROUP_DEFINE_INTERNAL__(_name, _strid, _transport, _ack_handler, \
198 _ack_data, _err_handler, _bound_handler, \
199 _wait_on_init, _initiator) \
200 NRF_RPC_AUTO_ARR(NRF_RPC_CONCAT(_name, _cmd_array), \
201 "cmd_" NRF_RPC_STRINGIFY(_name)); \
202 NRF_RPC_AUTO_ARR(NRF_RPC_CONCAT(_name, _evt_array), \
203 "evt_" NRF_RPC_STRINGIFY(_name)); \
204 \
205 static struct nrf_rpc_group_data NRF_RPC_CONCAT(_name, _group_data) = { \
206 .src_group_id = NRF_RPC_ID_UNKNOWN, \
207 .dst_group_id = NRF_RPC_ID_UNKNOWN, \
208 .transport_initialized = false, \
209 }; \
210 \
211 NRF_RPC_AUTO_ARR_ITEM(const struct nrf_rpc_group, _name, "grp", \
212 _strid) = { \
213 .cmd_array = &NRF_RPC_CONCAT(_name, _cmd_array), \
214 .evt_array = &NRF_RPC_CONCAT(_name, _evt_array), \
215 .data = &NRF_RPC_CONCAT(_name, _group_data), \
216 .ack_handler = _ack_handler, \
217 .ack_handler_data = _ack_data, \
218 .strid = _strid, \
219 .transport = _transport, \
220 .err_handler = _err_handler, \
221 .bound_handler = _bound_handler, \
222 .flags = NRF_RPC_FLAG_COND(_wait_on_init, NRF_RPC_FLAGS_WAIT_ON_INIT) \
223 | NRF_RPC_FLAG_COND(_initiator, NRF_RPC_FLAGS_INITIATOR), \
224 }
225
241#define NRF_RPC_GROUP_DEFINE(_name, _strid, _transport, _ack_handler, _ack_data, \
242 _err_handler) \
243 NRF_RPC_GROUP_DEFINE_INTERNAL__(_name, _strid, _transport, _ack_handler, \
244 _ack_data, _err_handler, NULL, true, \
245 true) \
246
276#define NRF_RPC_GROUP_DEFINE_NOWAIT(_name, _strid, _transport, _ack_handler, \
277 _ack_data, _err_handler, _bound_handler, \
278 _initiator) \
279 NRF_RPC_GROUP_DEFINE_INTERNAL__(_name, _strid, _transport, _ack_handler, \
280 _ack_data, _err_handler, _bound_handler, \
281 false, _initiator) \
282
289#define NRF_RPC_GROUP_DECLARE(_name) \
290 extern const struct nrf_rpc_group _name
291
301#define NRF_RPC_CMD_DECODER(_group, _name, _cmd, _handler, _data) \
302 NRF_RPC_STATIC_ASSERT(_cmd <= 0xFE, "Command out of range"); \
303 NRF_RPC_AUTO_ARR_ITEM(const struct _nrf_rpc_decoder, \
304 NRF_RPC_CONCAT(_name, _cmd_dec), \
305 "cmd_" NRF_RPC_STRINGIFY(_group), \
306 NRF_RPC_STRINGIFY(_name)) = { \
307 .id = _cmd, \
308 .handler = _handler, \
309 .handler_data = _data, \
310 }
311
321#define NRF_RPC_EVT_DECODER(_group, _name, _evt, _handler, _data) \
322 NRF_RPC_STATIC_ASSERT(_evt <= 0xFE, "Event out of range"); \
323 NRF_RPC_AUTO_ARR_ITEM(const struct _nrf_rpc_decoder, \
324 NRF_RPC_CONCAT(_name, _evt_dec), \
325 "evt_" NRF_RPC_STRINGIFY(_group), \
326 NRF_RPC_STRINGIFY(_name)) = { \
327 .id = _evt, \
328 .handler = _handler, \
329 .handler_data = _data, \
330 }
331
338#define NRF_RPC_GROUP_STATUS(_group) \
339 (_group.data->transport_initialized && (_group.data->dst_group_id != NRF_RPC_ID_UNKNOWN))
340
349
365static inline int nrf_rpc_cmd(const struct nrf_rpc_group *group, uint8_t cmd,
366 uint8_t *packet, size_t len,
367 nrf_rpc_handler_t handler, void *handler_data);
368
387static inline int nrf_rpc_cmd_rsp(const struct nrf_rpc_group *group,
388 uint8_t cmd, uint8_t *packet, size_t len,
389 const uint8_t **rsp_packet, size_t *rsp_len);
390
408static inline void nrf_rpc_cmd_no_err(const struct nrf_rpc_group *group,
409 uint8_t cmd, uint8_t *packet, size_t len,
410 nrf_rpc_handler_t handler,
411 void *handler_data);
412
427static inline void nrf_rpc_cmd_rsp_no_err(const struct nrf_rpc_group *group,
428 uint8_t cmd, uint8_t *packet,
429 size_t len,
430 const uint8_t **rsp_packet,
431 size_t *rsp_len);
432
444int nrf_rpc_evt(const struct nrf_rpc_group *group, uint8_t evt, uint8_t *packet,
445 size_t len);
446
459void nrf_rpc_evt_no_err(const struct nrf_rpc_group *group, uint8_t evt,
460 uint8_t *packet, size_t len);
461
472int nrf_rpc_rsp(const struct nrf_rpc_group *group, uint8_t *packet, size_t len);
473
485void nrf_rpc_rsp_no_err(const struct nrf_rpc_group *group, uint8_t *packet, size_t len);
486
498void nrf_rpc_decoding_done(const struct nrf_rpc_group *group, const uint8_t *packet);
499
512void nrf_rpc_err(int code, enum nrf_rpc_err_src src,
513 const struct nrf_rpc_group *group, uint8_t id,
514 uint8_t packet_type);
515
516/* Inline definitions. */
517
518static inline int nrf_rpc_cmd(const struct nrf_rpc_group *group, uint8_t cmd,
519 uint8_t *packet, size_t len,
520 nrf_rpc_handler_t handler, void *handler_data)
521{
522 int nrf_rpc_cmd_common(const struct nrf_rpc_group *group, uint32_t cmd,
523 uint8_t *packet, size_t len, void *ptr1,
524 void *ptr2);
525
526 return nrf_rpc_cmd_common(group, cmd, packet, len, handler,
527 handler_data);
528}
529
530static inline int nrf_rpc_cmd_rsp(const struct nrf_rpc_group *group,
531 uint8_t cmd, uint8_t *packet, size_t len,
532 const uint8_t **rsp_packet, size_t *rsp_len)
533{
534 int nrf_rpc_cmd_common(const struct nrf_rpc_group *group, uint32_t cmd,
535 uint8_t *packet, size_t len, void *ptr1,
536 void *ptr2);
537
538 return nrf_rpc_cmd_common(group, cmd | 0x10000, packet, len, rsp_packet,
539 rsp_len);
540}
541
542static inline void nrf_rpc_cmd_no_err(const struct nrf_rpc_group *group,
543 uint8_t cmd, uint8_t *packet, size_t len,
544 nrf_rpc_handler_t handler,
545 void *handler_data)
546{
547 void nrf_rpc_cmd_common_no_err(const struct nrf_rpc_group *group,
548 uint32_t cmd, uint8_t *packet,
549 size_t len, void *ptr1, void *ptr2);
550
551 nrf_rpc_cmd_common_no_err(group, cmd, packet, len, handler,
552 handler_data);
553}
554
555static inline void nrf_rpc_cmd_rsp_no_err(const struct nrf_rpc_group *group,
556 uint8_t cmd, uint8_t *packet,
557 size_t len,
558 const uint8_t **rsp_packet,
559 size_t *rsp_len)
560{
561 void nrf_rpc_cmd_common_no_err(const struct nrf_rpc_group *group,
562 uint32_t cmd, uint8_t *packet,
563 size_t len, void *ptr1, void *ptr2);
564
565 nrf_rpc_cmd_common_no_err(group, cmd | 0x10000, packet, len, rsp_packet,
566 rsp_len);
567}
568
578void nrf_rpc_alloc_tx_buf(const struct nrf_rpc_group *group, uint8_t **buf, size_t len);
579
588void nrf_rpc_free_tx_buf(const struct nrf_rpc_group *group, uint8_t *buf);
589
594#ifdef __cplusplus
595}
596#endif
597
598#endif /* _NRF_RPC_H_ */
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
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_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
void(* nrf_rpc_ack_handler_t)(uint8_t id, void *handler_data)
Callback called when ACK was received.
Definition nrf_rpc.h:91
static 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.
Definition nrf_rpc.h:542
int nrf_rpc_init(nrf_rpc_err_handler_t err_handler)
Initialize the nRF RPC.
static 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.
Definition nrf_rpc.h:530
static 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.
Definition nrf_rpc.h:518
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.
static 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.
Definition nrf_rpc.h:555
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.
int code
Error code.
Definition nrf_rpc.h:157
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