-
Notifications
You must be signed in to change notification settings - Fork 255
Expand file tree
/
Copy patherpc_transport_arbitrator.cpp
More file actions
266 lines (223 loc) · 6.68 KB
/
Copy patherpc_transport_arbitrator.cpp
File metadata and controls
266 lines (223 loc) · 6.68 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
* Copyright (c) 2016, Freescale Semiconductor, Inc.
* Copyright 2016-2020 NXP
* Copyright 2021 ACRIOS Systems s.r.o.
* All rights reserved.
*
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include "erpc_transport_arbitrator.h"
#include "erpc_manually_constructed.h"
#include <cassert>
#include <cstdio>
#include <string>
#if ERPC_THREADS_IS(NONE)
#error "Arbitrator code does not work in no-threading configuration."
#endif
using namespace erpc;
////////////////////////////////////////////////////////////////////////////////
// Code
////////////////////////////////////////////////////////////////////////////////
ERPC_MANUALLY_CONSTRUCTED_ARRAY_STATIC(TransportArbitrator::PendingClientInfo, s_pendingClientInfoArray,
ERPC_CLIENTS_THREADS_AMOUNT);
TransportArbitrator::TransportArbitrator(void)
: Transport()
, m_sharedTransport(NULL)
, m_codec(NULL)
, m_clientList(NULL)
, m_clientFreeList(NULL)
, m_clientListMutex()
{
}
TransportArbitrator::~TransportArbitrator(void)
{
// Dispose of client info objects.
freeClientList(m_clientList);
freeClientList(m_clientFreeList);
}
void TransportArbitrator::setCrc16(Crc16 *crcImpl)
{
assert(crcImpl);
assert(m_sharedTransport);
m_sharedTransport->setCrc16(crcImpl);
}
bool TransportArbitrator::hasMessage(void)
{
assert(m_sharedTransport && "shared transport is not set");
return m_sharedTransport->hasMessage();
}
erpc_status_t TransportArbitrator::receive(MessageBuffer *message)
{
assert(m_sharedTransport && "shared transport is not set");
erpc_status_t err;
message_type_t msgType;
uint32_t service;
Md5Hash requestNumber;
uint32_t sequence;
PendingClientInfo *client;
while (true)
{
// Receive a message.
err = m_sharedTransport->receive(message);
if (err != kErpcStatus_Success)
{
// if we timeout, we must unblock all pending client(s)
if (err == kErpcStatus_Timeout)
{
client = m_clientList;
for (; client; client = client->m_next)
{
if (client->m_isValid)
{
client->m_sem.put();
}
}
}
break;
}
m_codec->setBuffer(*message);
// Parse the message header.
m_codec->startReadMessage(&msgType, &service, requestNumber, &sequence);
err = m_codec->getStatus();
if (err != kErpcStatus_Success)
{
continue;
}
// If this message is an invocation, return it to the calling server.
if ((msgType == kInvocationMessage) || (msgType == kOnewayMessage))
{
break;
}
// Just ignore messages we don't know what to do with.
if (msgType != kReplyMessage)
{
continue;
}
// Check if there is a client waiting for this message.
client = m_clientList;
for (; client; client = client->m_next)
{
if (client->m_isValid && (sequence == client->m_request->getSequence()))
{
// Swap the received message buffer with the client's message buffer.
client->m_request->getCodec()->getBuffer()->swap(message);
// Wake up the client receive thread.
client->m_sem.put();
break;
}
}
#if ERPC_NESTED_CALLS
// If received answer is not for postponed client, it can be for nested server call.
if (client == NULL)
{
break;
}
#endif
}
return err;
}
erpc_status_t TransportArbitrator::send(MessageBuffer *message)
{
assert(m_sharedTransport && "shared transport is not set");
return m_sharedTransport->send(message);
}
TransportArbitrator::client_token_t TransportArbitrator::prepareClientReceive(RequestContext &request)
{
PendingClientInfo *info = addPendingClient();
if (NULL != info)
{
info->m_request = &request;
info->m_isValid = true;
}
return reinterpret_cast<client_token_t>(info);
}
erpc_status_t TransportArbitrator::clientReceive(client_token_t token)
{
assert((token != 0) && "invalid client token");
// Convert token to pointer to info struct for this client receive request.
PendingClientInfo *info = reinterpret_cast<PendingClientInfo *>(token);
// Wait on the semaphore until we're signaled.
info->m_sem.get(Semaphore::kWaitForever);
removePendingClient(info);
return kErpcStatus_Success;
}
TransportArbitrator::PendingClientInfo *TransportArbitrator::createPendingClient(void){ ERPC_CREATE_NEW_OBJECT(
TransportArbitrator::PendingClientInfo, s_pendingClientInfoArray, ERPC_CLIENTS_THREADS_AMOUNT) }
TransportArbitrator::PendingClientInfo *TransportArbitrator::addPendingClient(void)
{
Mutex::Guard lock(m_clientListMutex);
// Get a free client info node, or allocate one.
PendingClientInfo *info = NULL;
if (!m_clientFreeList)
{
info = createPendingClient();
}
else
{
info = m_clientFreeList;
m_clientFreeList = m_clientFreeList->m_next;
}
// Add to active list.
if (!m_clientList)
{
m_clientList = info;
}
else
{
info->m_next = m_clientList;
m_clientList = info;
}
return info;
}
void TransportArbitrator::removePendingClient(PendingClientInfo *info)
{
Mutex::Guard lock(m_clientListMutex);
PendingClientInfo *node;
// Clear fields.
info->m_request = NULL;
info->m_isValid = false;
// Remove from active list.
if (m_clientList == info)
{
m_clientList = info->m_next;
}
else
{
node = m_clientList;
while (node != NULL)
{
if (node->m_next == info)
{
node->m_next = info->m_next;
break;
}
node = node->m_next;
}
}
// Add to free list.
info->m_next = m_clientFreeList;
m_clientFreeList = info;
}
void TransportArbitrator::freeClientList(PendingClientInfo *list)
{
PendingClientInfo *info = list;
PendingClientInfo *temp;
while (info != NULL)
{
temp = info;
info = info->m_next;
ERPC_DESTROY_OBJECT(temp, s_pendingClientInfoArray, ERPC_CLIENTS_THREADS_AMOUNT)
}
}
TransportArbitrator::PendingClientInfo::PendingClientInfo(void)
: m_request(NULL)
, m_sem(0)
, m_isValid(false)
, m_next(NULL)
{
}
TransportArbitrator::PendingClientInfo::~PendingClientInfo(void) {}
////////////////////////////////////////////////////////////////////////////////
// EOF
////////////////////////////////////////////////////////////////////////////////