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>

Co-authored-by: Dominik Kilian <Dominik.Kilian@nordicsemi.no>
  • Loading branch information
dchat-nordic and doki-nordic committed Aug 26, 2021
1 parent ed59241 commit 636f1b5
Show file tree
Hide file tree
Showing 13 changed files with 126 additions and 16 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 SM_IPT 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
21 changes: 20 additions & 1 deletion 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 Down Expand Up @@ -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,20 @@ struct nrf_rpc_err_report {
* a newly allocated packet buffer.
* @param[in] _len Requested length of the packet.
*/
/* If nrf_rpc_tr_alloc_tx_buf is a macro, put it directly,
* because it may allocate memory on stack.
*/
#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
7 changes: 6 additions & 1 deletion 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 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
6 changes: 5 additions & 1 deletion 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 @@ -11,6 +11,10 @@

#include <nrf_rpc_rpmsg.h>

#elif defined(CONFIG_NRF_RPC_TR_SHMEM)

#include <sm_ipt_backend.h>

#elif defined(CONFIG_NRF_RPC_TR_CUSTOM)

#include CONFIG_NRF_RPC_TR_CUSTOM_INCLUDE
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
24 changes: 19 additions & 5 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 All @@ -10,9 +10,9 @@
#include <stdbool.h>
#include <string.h>

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

/* A pointer value to pass information that response */
#define RESPONSE_HANDLED_PTR ((uint8_t *)1)
Expand Down Expand Up @@ -366,7 +366,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 @@ -801,3 +801,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
5 changes: 4 additions & 1 deletion nrf_rpc/nrf_rpc_cbor.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 @@ -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);
}
9 changes: 9 additions & 0 deletions nrf_rpc/sm_ipt_backend.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* Copyright (c) 2021 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/

#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
3 changes: 2 additions & 1 deletion 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,6 +43,7 @@ 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.
*/
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 636f1b5

Please sign in to comment.