-
Notifications
You must be signed in to change notification settings - Fork 251
Expand file tree
/
Copy pathcpp_server_source.template
More file actions
295 lines (264 loc) · 12.2 KB
/
cpp_server_source.template
File metadata and controls
295 lines (264 loc) · 12.2 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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
{% set source = "server" >%}
{% if mlComment != ""%}
{$mlComment}
{% endif %}
{$commonHeader()}
#include "{$serverCppHeaderName}"
#if ERPC_ALLOCATION_POLICY == ERPC_ALLOCATION_POLICY_DYNAMIC
#include <new>
#include "erpc_port.h"
#endif
#include "erpc_manually_constructed.hpp"
{$checkVersion()}
{$>checkCrc()}
using namespace erpc;
using namespace std;
{$usingNamespace() >}
#if ERPC_NESTED_CALLS_DETECTION
extern bool nestingDetection;
#endif
{$generateCrcVariable()}
{$> setSharedMemAddresses()}
{$> constantsDefinitions(consts)}
{$> symbolHeader(group.symbolsMap.symbolsToServer, "deserial", "def")}
{$> symbolSource(group.symbolsMap.symbolsToServer, "deserial", "def")}
{$> symbolHeader(group.symbolsMap.symbolsToClient, "serial", "def")}
{$> symbolSource(group.symbolsMap.symbolsToClient, "serial", "def")}
{$> symbolFreeSpaceHeader(group.symbolsMap.symbolsServerFree, "def")}
{$> symbolFreeSpaceSource(group.symbolsMap.symbolsServerFree, "def")}
{$> symbolHeader(group.symbolsMap.symbolsToServer, "deserial", "noSharedMem")}
{$> symbolSource(group.symbolsMap.symbolsToServer, "deserial", "noSharedMem")}
{$> symbolHeader(group.symbolsMap.symbolsToClient, "serial", "noSharedMem")}
{$> symbolSource(group.symbolsMap.symbolsToClient, "serial", "noSharedMem")}
{$> symbolFreeSpaceHeader(group.symbolsMap.symbolsServerFree, "noSharedMem")}
{$> symbolFreeSpaceSource(group.symbolsMap.symbolsServerFree, "noSharedMem")}
{% def serverShimCode(iface, handler, fn, serverIDName, functionIDName) ------------------------- serverShimCode(fn, serverIDName, functionIDName) %}
{% set serverIndent = "" >%}
{% if (fn.isReturnValue || fn.isSendValue) && generateErrorChecks %}
erpc_status_t err = kErpcStatus_Success;
{% endif -- isReturnValue || isSendValue %}
{% for param in fn.parameters %}
{% if param.isFunction %}{% if param.ifaceScope != ""%}{$param.ifaceScope}{% else %}{$iface.name}{% endif %}_interface::{% endif %}{$param.variable}{% if param.isNullParam %} = NULL{% endif %};
{% if !empty(param.nullVariable) %}
{$param.nullVariable} = NULL;
{% endif %}
{% if !param.shared %}
{% if param.isNullable == false && param.direction != OutDirection %}
{$> addIndent(" ", allocMem(param.mallocServer))}
{% endif -- !param.isNullable && param.direction != OutDirection %}
{% endif -- shared %}
{% endfor -- param %}
{% if fn.needNullVariableOnServer %}
bool isNull;
{% endif -- needNullVariableOnServer %}
{% if fn.needTempVariableServerI32 %}
int32_t _tmp_local_i32;
{% endif %}
{% if fn.needTempVariableServerU16 %}
uint16_t _tmp_local_u16;
{% endif %}
{% if fn.returnValue.type.isNotVoid %}
{$fn.returnValue.resultVariable}{% if fn.returnValue.isNullReturnType %} = NULL{% endif %};
{% endif %}
{% if fn.isReturnValue || fn.isSendValue %}
{% endif %}
// startReadMessage() was already called before this shim was invoked.
{% if fn.isSendValue %}
{% for param in fn.parameters if (param.serializedDirection == "" || param.serializedDirection == OutDirection || param.referencedName != "") %}
{% if param.isNullable %}
{$addIndent(" ", f_paramIsNullableDecode(param))}
{% else -- notNullable %}
{% if param.direction != OutDirection %}
{$addIndent(" ", param.coderCall.decode(param.coderCall))}
{% endif -- param != OutDirection %}
{% endif -- isNullable %}
{% endfor -- parametersToServer %}
{% endif -- isSendValue %}
{% for param in fn.parametersToClient %}
{% if !param.shared %}
{% if param.isNullable == false && param.direction == OutDirection && empty(param.mallocServer) == false %}
{$> addIndent(" ", allocMem(param.mallocServer))}
{% endif -- !param.isNullable && param.direction == OutDirection %}
{% endif -- shared %}
{% endfor -- param %}
{% if (fn.isReturnValue || fn.isSendValue) && generateErrorChecks %}
{% set serverIndent = " " >%}
err = codec->getStatus();
if (err == kErpcStatus_Success)
{
{% endif -- generateErrorChecks %}
{$serverIndent} // Invoke the actual served function.
#if ERPC_NESTED_CALLS_DETECTION
{$serverIndent} nestingDetection = true;
#endif
{% if serverIDName == "serviceID" %}
{% for callbackFunction in fn.functions %}
{$serverIndent} {% if loop.first == false %}else {% endif %}if (({$serverIDName} == {$callbackFunction.serviceId}) && ({$functionIDName} == {$callbackFunction.id}))
{$serverIndent} {
{$serverIndent} m_handler->{$callbackFunction.serverPrototype}
{$serverIndent} }
{% endfor -- callbackFunction in callbackType.functions %}
{% else -- serverIDName == "serviceID" %}
{$serverIndent} {$fn.serverPrototype}
{% endif --serverIDName == "serviceID" %}
#if ERPC_NESTED_CALLS_DETECTION
{$serverIndent} nestingDetection = false;
#endif
{% if fn.isReturnValue %}
{$serverIndent} // preparing MessageBuffer for serializing data
{$serverIndent} {% if generateErrorChecks %}err = {% endif %}messageFactory->prepareServerBufferForSend(codec->getBufferRef(), transport->reserveHeaderSize());
{% if generateErrorChecks %}
}
if (err == kErpcStatus_Success)
{
{% endif -- generateErrorChecks %}
{$serverIndent} // preparing codec for serializing data
{$serverIndent} codec->reset(transport->reserveHeaderSize());
{$serverIndent} // Build response message.
{$serverIndent} codec->startWriteMessage(message_type_t::kReplyMessage, {$serverIDName}, {$functionIDName}, sequence);
{% for param in fn.parametersToClient if (param.serializedDirection == "" || param.serializedDirection == InDirection || param.referencedName != "") %}
{% if param.isNullable %}
{$serverIndent} if ({% if source == "server" && empty(param.nullVariable) == false %}_{% endif %}{$param.name} != NULL)
{$serverIndent} {
{$addIndent(serverIndent & " ", param.coderCall.encode(param.coderCall))}
{$serverIndent} }
{% else -- isNullable %}
{$addIndent(serverIndent & " ", param.coderCall.encode(param.coderCall))}
{% endif -- isNullable %}
{% endfor -- parametersToClient %}
{% if fn.returnValue.type.isNotVoid %}
{% if fn.returnValue.isNullable %}
{$addIndent(serverIndent & " ", f_paramIsNullableEncode(fn.returnValue))}
{% else -- isNullable %}
{$addIndent(serverIndent & " ", fn.returnValue.coderCall.encode(fn.returnValue.coderCall))}
{% endif -- isNullable %}
{% endif -- notVoid %}
{% if generateErrorChecks %}
err = codec->getStatus();
{% endif generateErrorChecks %}
{% endif -- isReturnValue %}
{% if (fn.isReturnValue || fn.isSendValue) && generateErrorChecks %}
}
{% endif -- fn.isReturnValue || fn.isSendValue %}
{% for param in fn.paramsToFree %}
{$> addIndent(" ", param.coderCall.freeingCall(param.coderCall))}
{$> addIndent(" ", param.firstFreeingCall1.firstFreeingCall(param.firstFreeingCall1))}
{% endfor -- parameters %}
{% if fn.returnValue.type.isNotVoid %}
{% set needFreeingCall = fn.returnValue.coderCall.freeingCall(fn.returnValue.coderCall) %}
{% set needFirstFreeingCall = fn.returnValue.firstFreeingCall1.firstFreeingCall(fn.returnValue.firstFreeingCall1) %}
{% if empty(needFreeingCall) == false || empty(needFirstFreeingCall) == false %}
{$> addIndent(" ", needFreeingCall)}
{$> addIndent(" ", needFirstFreeingCall)}
{% endif -- !empty(needFreeingCall) || !empty(needFirstFreeingCall) %}
{% endif -- notVoid %}
{% if (fn.isReturnValue || fn.isSendValue) && generateErrorChecks %}
return err;
{% else %}
return codec->getStatus();
{% endif %}
{% enddef --------------------------------------------------------------------------------- serverShimCode(fn, serverIDName, functionIDName) %}
{% for iface in group.interfaces %}
{% for cb in iface.callbacksInt if (count(cb.callbacks) > 1) %}
// Common function for serializing and deserializing callback functions of same type.
static erpc_status_t {$iface.interfaceClassName}_{$cb.name}_shim({$iface.interfaceClassName} *m_handler, uint32_t serviceID, uint32_t functionID, {$codecClass} * codec, MessageBufferFactory *messageFactory, Transport * transport, uint32_t sequence);
{% endfor %}
{% endfor %}
{% for iface in group.interfaces %}
{% for cb in iface.callbacksInt if (count(cb.callbacks) > 1) %}
// Common function for serializing and deserializing callback functions of same type.
static erpc_status_t {$iface.interfaceClassName}_{$cb.name}_shim({$iface.interfaceClassName} *m_handler, uint32_t serviceID, uint32_t functionID, {$codecClass} * codec, MessageBufferFactory *messageFactory, Transport * transport, uint32_t sequence)
{
{$ serverShimCode(iface, "m_handler", cb.callbacksData, "serviceID", "functionID") >}
}
{% endfor %}
{% endfor %}
{% for iface in group.interfaces -- service subclass method impl %}
{$iface.serviceClassName}::{$iface.serviceClassName}({$iface.interfaceClassName} *_{$iface.interfaceClassName})
: erpc::Service({$iface.interfaceClassName}::m_serviceId)
, m_handler(_{$iface.interfaceClassName})
{
}
{$iface.serviceClassName}::~{$iface.serviceClassName}()
{
}
// Get the pre-computed compile-time hash table
const {$iface.serviceClassName}::FunctionEntry* {$iface.serviceClassName}::getFunctionTable()
{
/*
* Compile-time computed hash table with collision resolution
* Hash table size: {$iface.hashTable.size}
* Function count: {$iface.hashTable.functionCount}
* Load factor: {$iface.hashTable.loadFactor}
* Collisions resolved: {$iface.hashTable.collisions}
* Maximum probe distance: {$iface.hashTable.maxProbe}
* Primary hit rate: {$iface.hashTable.primaryHitRate}%
*/
static const FunctionEntry s_functionTable[HASH_TABLE_SIZE] = {
{% for entry in iface.hashTable.entries %}
{% if entry.isEmpty %}
/* Index {$entry.index} - {$entry.comment} */
{0U, nullptr, nullptr},
{% else %}
/* Index {$entry.index} - {$entry.comment} */
{{$entry.id}U, &{$iface.serviceClassName}::{$entry.name}_shim, "{$entry.name}"},
{% endif %}
{% endfor -- entry %}
};
return s_functionTable;
}
// Find function by ID using compile-time hash table with linear probing
{$iface.serviceClassName}::ShimFunction {$iface.serviceClassName}::findFunction(uint32_t methodId) const
{
const FunctionEntry* table = getFunctionTable();
uint32_t hash_pos = hash_function_id(methodId);
uint32_t original_pos = hash_pos;
// Linear probing search (same as your symbols implementation)
do {
if (table[hash_pos].id == methodId && table[hash_pos].func != nullptr) {
return table[hash_pos].func;
}
if (table[hash_pos].id == 0) { // Empty slot
break;
}
// Move to next slot
hash_pos = (hash_pos + 1) & (HASH_TABLE_SIZE - 1);
} while (hash_pos != original_pos);
return nullptr; // Not found
}
// return service interface handler.
{$iface.interfaceClassName}* {$iface.serviceClassName}::getHandler(void)
{
return m_handler;
}
// Call the correct server shim based on method unique ID.
erpc_status_t {$iface.serviceClassName}::handleInvocation(uint32_t methodId, uint32_t sequence, Codec * codec, MessageBufferFactory *messageFactory, Transport * transport)
{
{% if codecClass != "Codec" %}
{$codecClass} *_codec = static_cast<{$codecClass} *>(codec);
{% endif %}
// O(1) compile-time hash table lookup for function dispatch
ShimFunction shimFunc = findFunction(methodId);
if (shimFunc != nullptr)
{
// Call the found shim function using function pointer
return (this->*shimFunc)({%if codecClass == "Codec" %}codec{% else %}_codec{% endif %}, messageFactory, transport, sequence);
}
else
{
// Method ID not found
return kErpcStatus_InvalidArgument;
}
}
{% for fn in iface.functions %}
// Server shim for {$fn.name} of {$iface.name} interface.
erpc_status_t {$iface.serviceClassName}::{$fn.name}_shim({$codecClass} * codec, MessageBufferFactory *messageFactory, Transport * transport, uint32_t sequence)
{
{% if fn.isCallback %}
return {$iface.interfaceClassName}_{$fn.callbackFNameNoGroup}_shim(m_handler, {$iface.interfaceClassName}::m_serviceId, {$iface.interfaceClassName}::{$getClassFunctionIdName(fn)}, codec, messageFactory, transport, sequence);
{% else -- fn.isCallback >%}
{$ serverShimCode(iface, "m_handler", fn, iface.interfaceClassName & "::m_serviceId", iface.interfaceClassName & "::" & getClassFunctionIdName(fn)) >}
{% endif -- fn.isCallback >%}
}
{% endfor -- fn %}
{% endfor -- iface %}