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: Dominik Chat <dominik.chat@nordicsemi.no>
  • Loading branch information
dchat-nordic committed Aug 9, 2021
1 parent 0fb3b02 commit 3cef3ac
Show file tree
Hide file tree
Showing 13 changed files with 129 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)
9 changes: 8 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
24 changes: 20 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,14 @@ extern "C" {
/* Internal definition used in the macros. */
#define _NRF_RPC_HEADER_SIZE 4

#ifndef NRF_RPC_TR_MAX_HEADER_SIZE
#define NRF_RPC_TR_MAX_HEADER_SIZE 0
#endif

#ifndef NRF_RPC_TR_AUTO_FREE_RX_BUF
#define NRF_RPC_TR_AUTO_FREE_RX_BUF 0
#endif

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

Expand Down Expand Up @@ -230,9 +238,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
2 changes: 1 addition & 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
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
20 changes: 18 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 @@ -767,6 +768,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 +803,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;
2 changes: 1 addition & 1 deletion nrf_rpc/template/nrf_rpc_log_tmpl.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
5 changes: 3 additions & 2 deletions nrf_rpc/template/nrf_rpc_os_tmpl.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 @@ -43,10 +43,11 @@ typedef void (*nrf_rpc_os_work_t)(const uint8_t *data, size_t len);
*
* @param callback Work callback that will be called when something was send
* to a thread pool.
* @param user_data Data pointer passed to ipm device callbacks
*
* @return 0 on success or negative error code.
*/
int nrf_rpc_os_init(nrf_rpc_os_work_t callback);
int nrf_rpc_os_init(nrf_rpc_os_work_t callback, void *user_data);

/** @brief Send work to a thread pool.
*
Expand Down
2 changes: 1 addition & 1 deletion nrf_rpc/template/nrf_rpc_tr_tmpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,4 @@ int nrf_rpc_tr_send(uint8_t *buf, size_t len);
* @}
*/

#endif /* TRANS_RPMSG_H_ */
#endif /* NRF_RPC_TR_TMPL_H_ */

0 comments on commit 3cef3ac

Please sign in to comment.