-
Notifications
You must be signed in to change notification settings - Fork 252
Expand file tree
/
Copy pathc_common_functions.template
More file actions
494 lines (457 loc) · 18.1 KB
/
c_common_functions.template
File metadata and controls
494 lines (457 loc) · 18.1 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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
{#--------------------commonHeader----------------------#}
{% def commonHeader() %}
/*
* Generated by erpcgen {$erpcVersion} on {$todaysDate}.
*
* AUTOGENERATED - DO NOT EDIT
*/
{% enddef --commonHeader %}
{% def checkVersion() %}
#if {$erpcVersionNumber} != ERPC_VERSION_NUMBER
#error "The generated shim code version is different to the rest of eRPC code."
#endif
{% enddef --checkVersion %}
{% def checkCrc() %}
{% if empty(crc16) == false %}
#if !defined(ERPC_GENERATED_CRC) || ERPC_GENERATED_CRC != {$crc16}
#error "Macro 'ERPC_GENERATED_CRC' should be defined with value {$crc16}."
#endif
{% endif -- empty(crc16) == false %}
{% enddef --checkCrc %}
{% def generateCrcVariable() %}
{% if empty(crc16) == false %}
// for mdk/keil do not forgett add "--muldefweak" for linker
extern const uint32_t erpc_generated_crc;
#pragma weak erpc_generated_crc
extern const uint32_t erpc_generated_crc = {$crc16};
{% endif -- empty(crc16) == false %}
{% enddef --checkCrc %}
{# ---------------- constantsDefinitions ---------------- #}
{% def constantsDefinitions(consts) %}
{% if not empty(consts) %}
// Constant variable definitions
{% for c in consts %}
#pragma weak {$c.name}
extern const {$c.typeAndName} = {$c.value};
{% endfor -- consts %}
{% endif %}
{% enddef -- constantsDefinitions %}
{# ---------------- symbolHeader ---------------- #}
{% def symbolHeader(symbols, way, shared) %}
{% if symbols %}
{% for s in symbols if (shared == "def") || (shared == "noSharedMem" && s.noSharedMem) %}
{% if s.type == "struct" %}
{% if way == "serial" %}
{$ structSerialHeader(s, shared)}
{% else %}
{$ structDeserialHeader(s, shared)}
{% endif -- way == "serial" %}
{% else %}
{% if way == "serial" %}
{$ unionSerialHeader(s, shared)}
{% else %}
{$ unionDeserialHeader(s, shared)}
{% endif -- way == "serial" %}
{% endif -- s.type == "union/struct" %}
{% endfor -- symbols %}
{% endif -- symbols%}
{% enddef -- serialHeader %}
{# ---------------- symbolSource ---------------- #}
{% def symbolSource(symbols, way, shared) %}
{% if symbols %}
{% for s in symbols if (shared == "def") || (shared == "noSharedMem" && s.noSharedMem) %}
{% if s.type == "struct" %}
{% if way == "serial" %}
{$ structSerialSource(s, shared)}
{% else %}
{$ structDeserialSource(s, shared)}
{% endif -- way == "serial" %}
{% else -- s.type == "union/struct" %}
{% if way == "serial" %}
{$ unionSerialSource(s, shared)}
{% else %}
{$ unionDeserialSource(s, shared)}
{% endif -- way == "serial" %}
{% endif -- s.type == "union/struct" %}
{% endfor -- symbols %}
{% endif -- symbols%}
{% enddef -- serialHeader %}
{# ---------------- symbolFreeSpaceHeader ---------------- #}
{% def symbolFreeSpaceHeader(symbols, shared) %}
{% if symbols %}
{% for s in symbols if (shared == "def") || (shared == "noSharedMem" && s.noSharedMem) %}
{% if s.type == "struct" %}
{$ structFreeSpaceHeader(s, shared)}
{% else %}
{$ unionFreeSpaceHeader(s, shared)}
{% endif %}
{% endfor -- symbols %}
{% endif -- symbols%}
{% enddef -- serialHeader %}
{# ---------------- symbolFreeSpaceSource ---------------- #}
{% def symbolFreeSpaceSource(symbols, shared) %}
{% if symbols %}
{% for s in symbols if (shared == "def") || (shared == "noSharedMem" && s.noSharedMem) %}
{% if s.type == "struct" %}
{$ structFreeSpaceSource(s, shared)}
{% else %}
{$ unionFreeSpaceSource(s, shared)}
{% endif %}
{% endfor -- symbols %}
{% endif -- symbols%}
{% enddef -- serialHeader %}
{# ---------------- structDeserialHeader ---------------- #}
{% def structDeserialHeader(struct, shared) %}
{% if struct.noSharedMem && shared == "noSharedMem" %}
//! @brief Function to read no shared member of shared struct {$struct.name}
static void read_{$struct.name}_struct_shared(erpc::{$codecClass} * codec, {$struct.name} * data);
{% else %}
//! @brief Function to read struct {$struct.name}
static void read_{$struct.name}_struct(erpc::{$codecClass} * codec, {$struct.name} * data);
{% endif %}
{% enddef -- structDeserialHeader %}
{# ---------------- structSerialHeader ---------------- #}
{% def structSerialHeader(struct, shared) %}
{% if struct.noSharedMem && shared == "noSharedMem" %}
//! @brief Function to write no shared member of shared struct {$struct.name}
static void write_{$struct.name}_struct_shared(erpc::{$codecClass} * codec, const {$struct.name} * data);
{% else %}
//! @brief Function to write struct {$struct.name}
static void write_{$struct.name}_struct(erpc::{$codecClass} * codec, const {$struct.name} * data);
{% endif %}
{% enddef -- structSerialHeader %}
{# ---------------- structDeserialSource ---------------- #}
{% def structDeserialSource(struct, shared) %}
{% if struct.noSharedMem && shared == "noSharedMem" %}
//! @brief Read no shared member of shared struct {$struct.name} function implementation
static void read_{$struct.name}_struct_shared(erpc::{$codecClass} * codec, {$struct.name} * data)
{% else %}
// Read struct {$struct.name} function implementation
static void read_{$struct.name}_struct(erpc::{$codecClass} * codec, {$struct.name} * data)
{% endif %}
{
{% if struct.hasNullableMember %}
bool isNull;
{% endif -- hasNullableMember %}
{% if struct.needTempVariable %}
int32_t _tmp_local;
{% endif %}
{% for mem in struct.members if (shared == "def" && not mem.serializedViaMember) || (mem.noSharedMem && shared == "noSharedMem") %}
{% if mem.isNullable %}
codec->readNullFlag(&isNull);
if (!isNull)
{
{% if source == "server" %}
{$> addIndent(" ", allocMem(mem.coderCall.memberAllocation))}
{% endif %}
{$ addIndent(" ", mem.coderCall.decode(mem.coderCall))}
}
else
{
{$mem.coderCall.name}{$mem.structElements} = NULL;
{$mem.coderCall.name}{$mem.structElementsCount} = 0;
}{$loop.addNewLineIfNotLast}
{% else -- notNullable %}
{% if source == "server" %}
{$> addIndent(" ", allocMem(mem.coderCall.memberAllocation))}
{% endif %}
{$addIndent(" ", mem.coderCall.decode(mem.coderCall))}{$loop.addNewLineIfNotLast}
{% endif -- notNullable %}
{% endfor -- struct.members %}
}
{% enddef -- structDeserialSource %}
{# ---------------- structSerialSource ---------------- #}
{% def structSerialSource(struct, shared) %}
{% if struct.noSharedMem && shared == "noSharedMem" %}
// Write no shared member of shared struct {$struct.name} function implementation
static void write_{$struct.name}_struct_shared(erpc::{$codecClass} * codec, const {$struct.name} * data)
{% else %}
// Write struct {$struct.name} function implementation
static void write_{$struct.name}_struct(erpc::{$codecClass} * codec, const {$struct.name} * data)
{% endif %}
{
{% for mem in struct.members if (shared == "def" && not mem.serializedViaMember) || (mem.noSharedMem && shared == "noSharedMem") %}
{% if mem.isNullable %}
if ({$mem.coderCall.name}{$mem.structElements} == NULL)
{
codec->writeNullFlag(true);
}
else
{
codec->writeNullFlag(false);
{$addIndent(" ", mem.coderCall.encode(mem.coderCall))}
}{$loop.addNewLineIfNotLast}
{% else -- notNullable %}
{$addIndent(" ", mem.coderCall.encode(mem.coderCall))}{$loop.addNewLineIfNotLast}
{% endif -- notNullable %}
{% endfor -- struct.members %}
}
{% enddef -- structSerialSource %}
{# ---------------- structFreeSpaceHeader ---------------- #}
{% def structFreeSpaceHeader(struct, shared) %}
{% if struct.noSharedMem && shared == "noSharedMem" %}
//! @brief Function to free space allocated inside no shared member of shared struct {$struct.name}
static void free_{$struct.name}_struct_shared({$struct.name} * data);
{% else %}
//! @brief Function to free space allocated inside struct {$struct.name}
static void free_{$struct.name}_struct({$struct.name} * data);
{% endif %}
{% enddef -- structFreeSpaceHeader %}
{# ---------------- structFreeSpaceSource ---------------- #}
{% def structFreeSpaceSource(struct, shared) %}
{% if struct.noSharedMem && shared == "noSharedMem" %}
// Free space allocated inside no shared member of shared struct {$struct.name} function implementation
static void free_{$struct.name}_struct_shared({$struct.name} * data)
{% else %}
// Free space allocated inside struct {$struct.name} function implementation
static void free_{$struct.name}_struct({$struct.name} * data)
{% endif %}
{
{% set needFreeNewline = "false" %}
{% for mem in struct.membersToFree if (shared == "def" && not mem.serializedViaMember) || (mem.noSharedMem && shared == "noSharedMem") %}
{% if needFreeNewline == "true" %}
{% else %}
{% set needFreeNewline = "true" %}
{% endif -- needFreeNewline == "true" %}
{$addIndent(" ", mem.coderCall.freeingCall(mem.coderCall))}
{% if empty(mem.coderCall.memberAllocation) == false %}
{$addIndent(" ", mem.coderCall.freeingCall2(mem.coderCall))}
{% endif %}
{% endfor -- struct.members %}
}
{% enddef -- structFreeSpaceSource %}
{# ---------------- unionDeserialHeader ---------------- #}
{% def unionDeserialHeader(union, shared) %}
{% if union.noSharedMem && shared == "noSharedMem" %}
//! @brief Function to read no shared member of shared union {$union.name}
static void read_{$union.name}_union_shared(erpc::{$codecClass} * codec, int32_t * discriminator, {$union.name} * data);
{% else %}
//! @brief Function to read union {$union.name}
static void read_{$union.name}_union(erpc::{$codecClass} * codec, int32_t * discriminator, {$union.name} * data);
{% endif %}
{% enddef -- unionDeserialHeader %}
{# ---------------- unionSerialHeader ---------------- #}
{% def unionSerialHeader(union, shared) %}
{% if union.noSharedMem && shared == "noSharedMem" %}
//! @brief Function to write no shared member of shared union {$union.name}
static void write_{$union.name}_union_shared(erpc::{$codecClass} * codec, int32_t discriminator, const {$union.name} * data);
{% else %}
//! @brief Function to write union {$union.name}
static void write_{$union.name}_union(erpc::{$codecClass} * codec, int32_t discriminator, const {$union.name} * data);
{% endif %}
{% enddef -- unionSerialHeader %}
{# ---------------- unionDeserialSource ---------------- #}
{% def unionDeserialSource(union, shared) %}
{% if union.noSharedMem && shared == "noSharedMem" %}
// Read no shared member of shared union {$union.name} function implementation
static void read_{$union.name}_union_shared(erpc::{$codecClass} * codec, int32_t * discriminator, {$union.name} * data)
{% else %}
// Read union {$union.name} function implementation
static void read_{$union.name}_union(erpc::{$codecClass} * codec, int32_t * discriminator, {$union.name} * data)
{% endif %}
{
{% if union.needTempVariable %}
int32_t _tmp_local;
{% endif %}
{$addIndent(" ", decodeUnionType(union.coderCall))}
}
{% enddef -- unionDeserialSource %}
{# ---------------- unionSerialSource ---------------- #}
{% def unionSerialSource(union, shared) %}
{% if union.noSharedMem && shared == "noSharedMem" %}
// Write no shared member of shared union {$union.name} function implementation
static void write_{$union.name}_union_shared(erpc::{$codecClass} * codec, int32_t discriminator, const {$union.name} * data)
{% else %}
// Write union {$union.name} function implementation
static void write_{$union.name}_union(erpc::{$codecClass} * codec, int32_t discriminator, const {$union.name} * data)
{% endif %}
{
{$addIndent(" ", encodeUnionType(union.coderCall))}
}
{% enddef -- unionSerialSource %}
{# ---------------- unionFreeSpaceHeader ---------------- #}
{% def unionFreeSpaceHeader(union, shared) %}
{% if union.noSharedMem && shared == "noSharedMem" %}
//! @brief Function to free space allocated inside no shared member of shared union {$union.name}
static void free_{$union.name}_union_shared(int32_t discriminator, {$union.name} * data);
{% else %}
//! @brief Function to free space allocated inside union {$union.name}
static void free_{$union.name}_union(int32_t discriminator, {$union.name} * data);
{% endif %}
{% enddef -- unionFreeSpaceHeader %}
{# ---------------- unionFreeSpaceSource ---------------- #}
{% def unionFreeSpaceSource(union, shared) %}
{% if union.noSharedMem && shared == "noSharedMem" %}
// Free space allocated inside no shared member of shared union {$union.name} function implementation
static void free_{$union.name}_union_shared(int32_t discriminator, {$union.name} * data)
{% else %}
// Free space allocated inside union {$union.name} function implementation
static void free_{$union.name}_union(int32_t discriminator, {$union.name} * data)
{% endif %}
{
{$addIndent(" ", freeUnion(union.coderCall))}
}
{% enddef -- unionFreeSpaceSource %}
{# ---------------- callbacksTable ---------------- #}
{% def callbackTable(functionsTypes) %}
{% if functionsTypes %}
{% for f in functionsTypes %}
{% if count(f.callbacks) > 1 %}
static const {$f.name} _{$f.name}[] = { {% for c in f.callbacks %}{$c.name}{% if !loop.last %}, {% endif -- loop.last %}{% endfor -- f.callbacks %} };
{% endif -- f.callbacks.size() %}
{% endfor -- functionsTypes %}
{% endif -- functionsTypes %}
{% enddef ------------------------------- callbackTable %}
{# ---------------- allocMem ---------------- #}
{% def allocMem(info) %}
{% if empty(info) == false %}
{$info.name} = ({$info.typePointerValue}) erpc_malloc({$info.size}sizeof({$info.typeValue}));
{% if generateAllocErrorChecks == true %}
if ({$info.name} == NULL)
{
codec->updateStatus(kErpcStatus_MemoryError);
}
{% if info.isOutChar == true %}
else
{
{$info.name}[{$info.chSize}] = '\0';
}
{% endif --info.isOutChar == true %}
{% else -- generateAllocErrorChecks == true %}
{% if info.isOutChar == true %}
{$info.name}[{$info.chSize}] = '\0';
{% endif --info.isOutChar == true %}
{% endif -- generateAllocErrorChecks == true %}
{% endif -- empty(info) == false %}
{% enddef ------------------------------- allocMem %}
{# ---------------- freeData ---------------- #}
{% def freeData(info) %}
{% if !info.zeroCopy %}
if ({$info.freeName})
{
erpc_free({$info.freeName});
}
{% endif %}
{% enddef ------------------------------- freeData %}
{# ---------------- freeStruct ---------------- #}
{% def freeStruct(info) %}
{% if info.inDataContainer %}
free_{$info.typeName}_struct(&{$info.name});
{% else -- not inDataContainer %}
if ({$info.name})
{
free_{$info.typeName}_struct({$info.name});
}
{% endif -- inDataContainer %}
{% enddef ------------------------------- freeStruct %}
{# ---------------- freeList ---------------- #}
{% def freeList(info) %}
{% if info.needFreeingCall == true %}
{$freeArray(info)}
{% endif %}
if ({$info.name})
{
erpc_free({$info.name});
}
{% enddef ------------------------------- freeList %}
{# ---------------- freeArray ---------------- #}
{% def freeArray(info) %}
for (uint32_t {$info.forLoopCount} = 0; {$info.forLoopCount} < {$info.size}; ++{$info.forLoopCount})
{
{$addIndent(" ", info.protoNext.freeingCall(info.protoNext))}
}
{% enddef ------------------------------- freeArray %}
{# ---------------- freeUnion ---------------- #}
{% def freeUnion(info) %}
switch ({$info.dataLiteral}{$info.discriminatorName})
{
{% for case in info.cases %}
{% if case.needCaseFreeingCall == true %}
{% if case.name == "default" %}
default:
{% else %}
case {% if case.name != "" %}{$case.name}{% else %}{$case.value}{% endif %}:
{% endif -- default or case %}
{
{% for member in case.members %}
{% if member.isNeedFreeingCall %}
{$addIndent(" ", member.coderCall.freeingCall(member.coderCall))}
{% endif %}
{% if empty(member.coderCall.memberAllocation) == false %}
{$addIndent(" ", member.coderCall.freeingCall2(member.coderCall))}
{% endif%}
{% endfor -- members %}
break;
}
{% endif %}
{% endfor -- cases %}
}
{% enddef ------------------------------- freeUnion %}
{% def freeUnionType(info) %}
{% if info.inDataContainer %}
free_{$info.typeName}_union({% if info.castDiscriminator %}static_cast<int32_t>({% endif %}{$info.dataLiteral}{$info.discriminatorName}{% if info.castDiscriminator %}){% endif %}, &{$info.name});
{% else -- not inDataContainer %}
if ({$info.name})
{
free_{$info.typeName}_union({% if info.castDiscriminator %}static_cast<int32_t>({% endif %}{$info.discriminatorName}{% if info.castDiscriminator %}){% endif %}, {$info.name});
}
{% endif -- inDataContainer %}
{% enddef ------------------------------- freeUnionType %}
{% def setSharedMemAddresses() %}
{% if sharedMemBeginAddr != "" %}
#define ERPC_SHARED_MEMORY_BEGIN {$sharedMemBeginAddr}
#define ERPC_SHARED_MEMORY_END {$sharedMemEndAddr}
{% endif %}
{% enddef ------------------------------- setSharedMemAddresses %}
{% def unionMembersDeclaration(info) %}
{% for unionCase in info.unionCases %}
{% if count(unionCase) > 1 %}
struct
{
{% set indent = " " %}
{% else %}
{% set indent = "" %}
{% endif %}
{% for unionMember in unionCase %}
{$indent}{$unionMember.typenameName};
{% endfor %}
{% if count(unionCase) > 1 %}
};
{% endif %}
{% endfor%}
{% enddef ------------------------------- unionMembersDeclaration %}
{% def f_paramIsNullableEncode(param) %}
if ({$param.nullableName} == NULL{% if ((source == "client") && (param.direction != ReturnDirection) && (empty(param.lengthName) == false)) %} || {$param.lengthName} == NULL{% endif %})
{
codec->writeNullFlag(true);
}
else
{
codec->writeNullFlag(false);
{# Out will send only null information from client to server that it is not null #}
{% if param.direction != OutDirection %}
{$addIndent(" ", param.coderCall.encode(param.coderCall))}
{% endif -- param.direction != OutDirection %}
}
{% enddef ------------------------------- f_paramIsNullableEncode %}
{% def f_paramIsNullableDecode(param) %}
codec->readNullFlag(&isNull);
if (!isNull)
{
{% if source == "server" %}
{$> addIndent(" ", allocMem(param.mallocServer))}
{% endif %}
{$> addIndent(" ", allocMem(param.firstAlloc))}
{# Out will receive only null information from client to server that it is not null #}
{% if param.direction != OutDirection %}
{$addIndent(" ", param.coderCall.decode(param.coderCall))}
{% endif -- param.direction != OutDirection %}
{% if source == "server" && empty(param.nullVariable) == false %}
_{$param.nullableName} = &{$param.nullableName};
{% endif %}
}
else
{
{% if source == "server" && !empty(param.nullVariable) %}_{% endif %}{$param.nullableName} = NULL;
}
{% enddef ------------------------------- f_paramIsNullableDecode %}