forked from EmbeddedRPC/erpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
erpc_rpmsg_lite_transport.cpp
228 lines (197 loc) · 6.04 KB
/
erpc_rpmsg_lite_transport.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
* Copyright (c) 2015-2021, Freescale Semiconductor, Inc.
* Copyright 2016-2022 NXP
* Copyright 2021 ACRIOS Systems s.r.o.
* All rights reserved.
*
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include "erpc_rpmsg_lite_transport.hpp"
#include "erpc_config_internal.h"
extern "C" {
#include "rpmsg_ns.h"
}
using namespace erpc;
////////////////////////////////////////////////////////////////////////////////
// Variables
////////////////////////////////////////////////////////////////////////////////
uint8_t RPMsgBase::s_initialized = 0U;
struct rpmsg_lite_instance *RPMsgBase::s_rpmsg = NULL;
////////////////////////////////////////////////////////////////////////////////
// Code
////////////////////////////////////////////////////////////////////////////////
int32_t RPMsgTransport::rpmsg_read_cb(void *payload, uint32_t payload_len, uint32_t src, void *priv)
{
RPMsgTransport *transport = reinterpret_cast<RPMsgTransport *>(priv);
if (payload_len <= ERPC_DEFAULT_BUFFER_SIZE)
{
MessageBuffer message(reinterpret_cast<uint8_t *>(payload), payload_len);
message.setUsed((uint16_t)payload_len);
(void)transport->m_messageQueue.add(message);
}
return RL_HOLD;
}
RPMsgTransport::RPMsgTransport(void) : Transport(), RPMsgBase(), m_dst_addr(0), m_rpmsg_ept(NULL), m_crcImpl(NULL) {}
RPMsgTransport::~RPMsgTransport(void)
{
bool skip = false;
if (s_rpmsg != RL_NULL)
{
if (m_rpmsg_ept != RL_NULL)
{
if (RL_SUCCESS != rpmsg_lite_destroy_ept(s_rpmsg, m_rpmsg_ept))
{
skip = true;
}
}
if (!skip && (RL_SUCCESS != rpmsg_lite_deinit(s_rpmsg)))
{
skip = true;
}
if (!skip)
{
s_initialized = 0U;
}
}
}
erpc_status_t RPMsgTransport::init(uint32_t src_addr, uint32_t dst_addr, void *base_address, uint32_t length,
uint32_t rpmsg_link_id)
{
erpc_status_t status = kErpcStatus_Success;
if (0U == s_initialized)
{
#if RL_USE_STATIC_API
s_rpmsg = rpmsg_lite_master_init(base_address, length, rpmsg_link_id, RL_NO_FLAGS, &m_static_context);
#else
s_rpmsg = rpmsg_lite_master_init(base_address, length, rpmsg_link_id, RL_NO_FLAGS);
#endif
if (s_rpmsg == RL_NULL)
{
status = kErpcStatus_InitFailed;
}
if (status == kErpcStatus_Success)
{
#if RL_USE_STATIC_API
m_rpmsg_ept = rpmsg_lite_create_ept(s_rpmsg, src_addr, rpmsg_read_cb, this, &m_ept_context);
#else
m_rpmsg_ept = rpmsg_lite_create_ept(s_rpmsg, src_addr, rpmsg_read_cb, this);
#endif
if (m_rpmsg_ept == RL_NULL)
{
status = kErpcStatus_InitFailed;
}
}
if (status == kErpcStatus_Success)
{
s_initialized = 1U;
m_dst_addr = dst_addr;
}
else
{
if (s_rpmsg != RL_NULL)
{
if (RL_SUCCESS == rpmsg_lite_deinit(s_rpmsg))
{
s_rpmsg = NULL;
}
}
}
}
return status;
}
erpc_status_t RPMsgTransport::init(uint32_t src_addr, uint32_t dst_addr, void *base_address, uint32_t rpmsg_link_id,
void (*ready_cb)(void), char *nameservice_name)
{
erpc_status_t status = kErpcStatus_Success;
if (0U == s_initialized)
{
#if RL_USE_STATIC_API
s_rpmsg = rpmsg_lite_remote_init(base_address, rpmsg_link_id, RL_NO_FLAGS, &m_static_context);
#else
s_rpmsg = rpmsg_lite_remote_init(base_address, rpmsg_link_id, RL_NO_FLAGS);
#endif
if (s_rpmsg == RL_NULL)
{
status = kErpcStatus_InitFailed;
}
if (status == kErpcStatus_Success)
{
/* Signal the other core we are ready */
if (ready_cb != NULL)
{
ready_cb();
}
(void)rpmsg_lite_wait_for_link_up(s_rpmsg, RL_BLOCK);
#if RL_USE_STATIC_API
m_rpmsg_ept = rpmsg_lite_create_ept(s_rpmsg, src_addr, rpmsg_read_cb, this, &m_ept_context);
#else
m_rpmsg_ept = rpmsg_lite_create_ept(s_rpmsg, src_addr, rpmsg_read_cb, this);
#endif
if (m_rpmsg_ept == RL_NULL)
{
status = kErpcStatus_InitFailed;
}
}
if (status == kErpcStatus_Success)
{
if (NULL != nameservice_name)
{
if (RL_SUCCESS != rpmsg_ns_announce(s_rpmsg, m_rpmsg_ept, nameservice_name, (uint32_t)RL_NS_CREATE))
{
status = kErpcStatus_InitFailed;
}
}
}
if (status == kErpcStatus_Success)
{
m_dst_addr = dst_addr;
s_initialized = 1U;
}
else
{
if (m_rpmsg_ept != RL_NULL)
{
if (RL_SUCCESS != rpmsg_lite_destroy_ept(s_rpmsg, m_rpmsg_ept))
{
m_rpmsg_ept = NULL;
}
}
if (s_rpmsg != RL_NULL)
{
if (RL_SUCCESS == rpmsg_lite_deinit(s_rpmsg))
{
s_rpmsg = NULL;
}
}
}
}
return status;
}
erpc_status_t RPMsgTransport::receive(MessageBuffer *message)
{
while (!m_messageQueue.get(message))
{
}
return kErpcStatus_Success;
}
erpc_status_t RPMsgTransport::send(MessageBuffer *message)
{
int32_t ret_val = rpmsg_lite_send_nocopy(s_rpmsg, m_rpmsg_ept, m_dst_addr, reinterpret_cast<char *>(message->get()),
message->getUsed());
message->set(NULL, 0);
return (ret_val != RL_SUCCESS) ? kErpcStatus_SendFailed : kErpcStatus_Success;
}
bool RPMsgTransport::hasMessage(void)
{
return ((0UL < m_messageQueue.size()) ? true : false);
}
void RPMsgTransport::setCrc16(Crc16 *crcImpl)
{
erpc_assert(crcImpl != NULL);
m_crcImpl = crcImpl;
}
Crc16 *RPMsgTransport::getCrc16(void)
{
return m_crcImpl;
}