Skip to content

Commit

Permalink
nrf_rpc: Added sm_ipt backend support
Browse files Browse the repository at this point in the history
Added support for sm_ipt backend
Updated templates, updated Kconfig
to support sm_ipt.

Signed-off-by: dchat-nordic <dominik.chat@nordicsemi.no>
  • Loading branch information
dchat-nordic committed Jul 27, 2021
1 parent 9984ce2 commit 3ca3257
Show file tree
Hide file tree
Showing 13 changed files with 209 additions and 26 deletions.
5 changes: 3 additions & 2 deletions nrf_rpc/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#
# Copyright (c) 2020 Nordic Semiconductor
# Copyright (c) 2020-2021 Nordic Semiconductor
#
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
#

zephyr_include_directories(include)

zephyr_library()
zephyr_library_sources(nrf_rpc.c)

zephyr_library_sources_ifdef(CONFIG_NRF_RPC_CBOR nrf_rpc_cbor.c)
zephyr_library_sources_ifdef(CONFIG_NRF_RPC_TR_SHMEM sm_ipt_backend.c)
zephyr_library_sources_ifdef(CONFIG_NRF_RPC nrf_rpc.c)

zephyr_linker_sources(SECTIONS nrf_rpc.ld)
31 changes: 30 additions & 1 deletion nrf_rpc/Kconfig
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (c) 2020 Nordic Semiconductor
# Copyright (c) 2020-2021 Nordic Semiconductor
#
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
#
Expand All @@ -26,6 +26,13 @@ config NRF_RPC_TR_RPMSG
help
If enabled selects RPMsg as a transport layer for nRF PRC.

config NRF_RPC_TR_SHMEM
bool "nRF RPC over shared memory"
select SM_IPT
help
If enabled selects shared memory as a transport layer for nRF PRC.
It requires additional API implemented in nrf_rpc_os.h file.

config NRF_RPC_TR_CUSTOM
bool "User provided transport layer"
help
Expand Down Expand Up @@ -63,4 +70,26 @@ config NRF_RPC_THREAD_POOL_SIZE
the remote side. If there is no available threads then remote side
will wait.

choice
prompt "Automatically registered arrays implementation"
default NRF_RPC_AUTO_ARR_SECTIONS

config NRF_RPC_AUTO_ARR_SECTIONS
bool "Place in sections"
help
Automatically registered array items will be placed in dedicated
sections. It will work only on platforms where linker script can
be altered, because the linker script is responsible for putting
them into right place and sort them.

config NRF_RPC_AUTO_ARR_CONSTRUCTOR
bool "Use constructor attribute"
help
Place automatically registered array items into arrays using
constructor function: __attribute__((constructor)). Not all
platforms correctly supports constructor attribute, so make
sure that it is supported on your platform.

endchoice

endif # NRF_RPC
19 changes: 15 additions & 4 deletions nrf_rpc/include/nrf_rpc.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Nordic Semiconductor ASA
* Copyright (c) 2020-2021 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/
Expand All @@ -11,9 +11,9 @@
#include <stdbool.h>
#include <stddef.h>

#include <nrf_rpc_errno.h>
#include <nrf_rpc_common.h>
#include <nrf_rpc_tr.h>
#include "nrf_rpc_errno.h"
#include "nrf_rpc_common.h"
#include "nrf_rpc_tr.h"

/**
* @defgroup nrf_rpc nRF RPC (Remote Procedure Calls) module.
Expand All @@ -28,6 +28,9 @@ extern "C" {
/* Internal definition used in the macros. */
#define _NRF_RPC_HEADER_SIZE 4

#define NRF_RPC_TR_MAX_HEADER_SIZE 0
#define NRF_RPC_TR_AUTO_FREE_RX_BUF 0

/** @brief Special value to indicate that ID is unknown or irrelevant. */
#define NRF_RPC_ID_UNKNOWN 0xFF

Expand Down Expand Up @@ -230,9 +233,17 @@ struct nrf_rpc_err_report {
* a newly allocated packet buffer.
* @param[in] _len Requested length of the packet.
*/
#ifdef nrf_rpc_tr_alloc_tx_buf
#define NRF_RPC_ALLOC(_packet, _len) \
nrf_rpc_tr_alloc_tx_buf(&(_packet), _NRF_RPC_HEADER_SIZE + (_len)); \
*(uint8_t **)&(_packet) += _NRF_RPC_HEADER_SIZE
#else
#define NRF_RPC_ALLOC(_packet, _len) \
do { \
extern uint8_t *_nrf_rpc_alloc(size_t len); \
(_packet) = _nrf_rpc_alloc(_len); \
} while (0)
#endif

/** @brief Deallocate memory for a packet.
*
Expand Down
15 changes: 10 additions & 5 deletions nrf_rpc/include/nrf_rpc_cbor.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Nordic Semiconductor ASA
* Copyright (c) 2020-2021 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/
Expand All @@ -11,11 +11,11 @@
#include <stdbool.h>
#include <stddef.h>

#include <tinycbor/cbor.h>
#include <tinycbor/cbor_buf_writer.h>
#include <tinycbor/cbor_buf_reader.h>
#include "tinycbor/cbor.h"
#include "tinycbor/cbor_buf_writer.h"
#include "tinycbor/cbor_buf_reader.h"

#include <nrf_rpc.h>
#include "nrf_rpc.h"

/**
* @defgroup nrf_rpc_cbor TinyCBOR serialization layer for nRF RPC.
Expand Down Expand Up @@ -131,9 +131,14 @@ struct nrf_rpc_cbor_rsp_ctx {
* resources to encode and send a packet.
* @param[in] _len Requested length of the packet.
*/
#ifdef nrf_rpc_tr_alloc_tx_buf
#define NRF_RPC_CBOR_ALLOC(_ctx, _len) \
NRF_RPC_ALLOC((_ctx).out_packet, (_len) + 1); \
_nrf_rpc_cbor_prepare((struct nrf_rpc_cbor_ctx *)(&(_ctx)), (_len) + 1)
#else
#define NRF_RPC_CBOR_ALLOC(_ctx, _len) \
_nrf_rpc_cbor_prepare((struct nrf_rpc_cbor_ctx *)(&(_ctx)), (_len) + 1)
#endif

/** @brief Deallocate memory for a packet.
*
Expand Down
60 changes: 59 additions & 1 deletion nrf_rpc/include/nrf_rpc_common.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Nordic Semiconductor ASA
* Copyright (c) 2020-2021 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/
Expand Down Expand Up @@ -101,6 +101,20 @@ extern "C" {
#define NRF_RPC_ASSERT(_expr) \
__NRF_RPC_ASSERT(_expr)

#if defined(CONFIG_NRF_RPC_AUTO_ARR_SECTIONS)

/** @brief Initialize automatically registered arrays.
*
* This function must be called before use of the
* @a NRF_RPC_AUTO_ARR_FOR and @a NRF_RPC_AUTO_ARR_GET.
*
* @return 0 on success or negative error code.
*/
static inline int nrf_rpc_auto_arr_init(void)
{
return 0;
}

/** @brief Creates new automatically registered array.
*
* @param _name Name of the array. Can be used later for
Expand Down Expand Up @@ -150,6 +164,50 @@ extern "C" {
#define NRF_RPC_AUTO_ARR_GET(_array_ptr, _index, _type) \
(((_type *)((const uint8_t *const *)(_array_ptr) + 1))[_index])

#elif defined(CONFIG_NRF_RPC_AUTO_ARR_CONSTRUCTOR)

struct _nrf_rpc_auto_arr_item {
const char *key;
const void *data;
struct _nrf_rpc_auto_arr_item *next;
bool is_array;
};

//int nrf_rpc_auto_arr_init(void);
//void _nrf_rpc_auto_arr_item_init(struct _nrf_rpc_auto_arr_item *item, const void *data, const char *key, bool is_array);

#define NRF_RPC_AUTO_ARR(_name, _array_key) \
void **_name; \
__attribute__((constructor)) \
static void NRF_RPC_CONCAT(_name, _auto_arr)(void) { \
static struct _nrf_rpc_auto_arr_item item; \
_nrf_rpc_auto_arr_item_init(&item, &_name, _array_key ".a", true); \
} \

#define NRF_RPC_AUTO_ARR_ITEM(_type, _name, _array_key, _item_key) \
__attribute__((constructor)) \
static void NRF_RPC_CONCAT(_name, _auto_arr_init)(void) { \
extern _type _name; \
static struct _nrf_rpc_auto_arr_item item; \
_nrf_rpc_auto_arr_item_init(&item, (void *)&_name, _array_key ".i." _item_key, false); \
} \
_type _name


#define NRF_RPC_AUTO_ARR_FOR(_it, _var, _array_ptr, _type) \
(_it) = *(void ***)(_array_ptr), (_var) = *(_type **)(_it); \
(_var) != NULL; \
(*(_type ***)&(_it))++, (_var) = *(_type **)(_it)

#define NRF_RPC_AUTO_ARR_GET(_array_ptr, _index, _type) \
(*((*(_type ***)_array_ptr)[_index]))

#else

#error No nRF RPC auto arrays implementation selected!

#endif

/**
* @}
*/
Expand Down
8 changes: 6 additions & 2 deletions nrf_rpc/include/nrf_rpc_tr.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Nordic Semiconductor ASA
* Copyright (c) 2020-2021 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/
Expand All @@ -9,7 +9,11 @@

#if defined(CONFIG_NRF_RPC_TR_RPMSG)

#include <nrf_rpc_rpmsg.h>
#include "nrf_rpc_rpmsg.h"

#elif defined(CONFIG_NRF_RPC_TR_SHMEM)

#include "sm_ipt_backend.h"

#elif defined(CONFIG_NRF_RPC_TR_CUSTOM)

Expand Down
47 changes: 47 additions & 0 deletions nrf_rpc/include/sm_ipt_backend.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2021 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/

#ifndef SM_IPT_BACKEND_H
#define SM_IPT_BACKEND_H

#include "sm_ipt.h"

#ifdef __cplusplus
extern "C" {
#endif

extern struct sm_ipt_ctx sm_ipt_context;

static inline int nrf_rpc_tr_init(sm_ipt_receive_handler_t callback)
{
return sm_ipt_init(&sm_ipt_context, callback);
}

static inline void nrf_rpc_tr_free_rx_buf(const uint8_t *packet)
{
sm_ipt_free_rx_buf(&sm_ipt_context, packet);
}

static inline void nrf_rpc_tr_alloc_tx_buf(uint8_t **buf, size_t len)
{
sm_ipt_alloc_tx_buf(&sm_ipt_context, buf, len);
}

static inline void nrf_rpc_tr_free_tx_buf(uint8_t *buf)
{
sm_ipt_free_tx_buf(&sm_ipt_context, buf);
}

static inline int nrf_rpc_tr_send(uint8_t *buf, size_t len)
{
return sm_ipt_send(&sm_ipt_context, buf, len);
}

#ifdef __cplusplus
}
#endif

#endif
25 changes: 23 additions & 2 deletions nrf_rpc/nrf_rpc.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Nordic Semiconductor ASA
* Copyright (c) 2020-2021 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/
Expand Down Expand Up @@ -95,6 +95,7 @@ static struct nrf_rpc_cmd_ctx *cmd_ctx_alloc(void)

static void cmd_ctx_free(struct nrf_rpc_cmd_ctx *ctx)
{
NRF_RPC_DBG("Command context %d free", ctx->id);
nrf_rpc_os_tls_set(NULL);
nrf_rpc_os_ctx_pool_release(ctx->id);
}
Expand Down Expand Up @@ -366,7 +367,7 @@ static void receive_handler(const uint8_t *packet, size_t len)
}

NRF_RPC_DBG("Received %d bytes packet from %d to %d, type 0x%02X, "
"cmd/evt/cnt 0x%02X, grp %d (%s)", len, hdr.src, hdr.dst,
"cmd/evt/cnt 0x%02X, grp %d (%s)", (int)len, hdr.src, hdr.dst,
hdr.type, hdr.id, hdr.group_id,
(group != NULL) ? group->strid : "unknown");

Expand Down Expand Up @@ -709,6 +710,11 @@ int nrf_rpc_init(nrf_rpc_err_handler_t err_handler)

NRF_RPC_DBG("Initializing nRF RPC module");

err = nrf_rpc_auto_arr_init();
if (err < 0) {
return err;
}

groups_check_sum = 0;

global_err_handler = err_handler;
Expand Down Expand Up @@ -767,6 +773,7 @@ int nrf_rpc_init(nrf_rpc_err_handler_t err_handler)
return err;
}


/** Report an error that cannot be reported as a function return value */
void nrf_rpc_err(int code, enum nrf_rpc_err_src src,
const struct nrf_rpc_group *group, uint8_t id,
Expand Down Expand Up @@ -801,3 +808,17 @@ void nrf_rpc_err(int code, enum nrf_rpc_err_src src,
global_err_handler(&report);
}
}

#ifndef nrf_rpc_tr_alloc_tx_buf

uint8_t *_nrf_rpc_alloc(size_t len)
{
uint8_t *packet;

NRF_RPC_ASSERT(len < 0x70000000);

nrf_rpc_tr_alloc_tx_buf(&packet, _NRF_RPC_HEADER_SIZE + len);
return &packet[_NRF_RPC_HEADER_SIZE];
}

#endif
13 changes: 8 additions & 5 deletions nrf_rpc/nrf_rpc_cbor.c
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/*
* Copyright (c) 2020 Nordic Semiconductor ASA
* Copyright (c) 2020-2021 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/
#define NRF_RPC_LOG_MODULE NRF_RPC_CBOR
#include <nrf_rpc_log.h>
#include "nrf_rpc_log.h"

#include <stdint.h>
#include <stddef.h>

#include <nrf_rpc.h>
#include <nrf_rpc_cbor.h>
#include <nrf_rpc_common.h>
#include "nrf_rpc.h"
#include "nrf_rpc_cbor.h"
#include "nrf_rpc_common.h"

struct handler_proxy_ctx {
CborValue value;
Expand Down Expand Up @@ -227,6 +227,9 @@ void _nrf_rpc_cbor_proxy_handler(const uint8_t *packet, size_t len,

void _nrf_rpc_cbor_prepare(struct nrf_rpc_cbor_ctx *ctx, size_t len)
{
#ifndef nrf_rpc_tr_alloc_tx_buf
NRF_RPC_ALLOC(ctx->out_packet, len + 1);
#endif
cbor_buf_writer_init(&ctx->writer, ctx->out_packet, len);
cbor_encoder_init(&ctx->encoder, &ctx->writer.enc, 0);
}
3 changes: 3 additions & 0 deletions nrf_rpc/sm_ipt_backend.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "sm_ipt_backend.h"

struct sm_ipt_ctx sm_ipt_context;
Loading

0 comments on commit 3ca3257

Please sign in to comment.