og_B_mapper.py 38.1 KB
Newer Older
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
#
# (C) Semantix Information Technologies.
#
# Semantix Information Technologies is licensing the code of the
# Data Modelling Tools (DMT) in the following dual-license mode:
#
# Commercial Developer License:
#       The DMT Commercial Developer License is the suggested version
# to use for the development of proprietary and/or commercial software.
# This version is for developers/companies who do not want to comply
# with the terms of the GNU Lesser General Public License version 2.1.
#
# GNU LGPL v. 2.1:
#       This version of DMT is the one to use for the development of
# applications, when you are willing to comply with the terms of the
# GNU Lesser General Public License version 2.1.
#
# Note that in both cases, there are no charges (royalties) for the
# generated code.
#
'''
This is the code generator for the ObjectGeode code mappers.
This backend is called by aadl2glueC, when an OG subprogram
is identified in the input concurrency view.

All the ASSERT supported encodings are handled by this code
in the form of generated C macros:

  DECLARE_Typename
  DEFINE_Typename
  ENCODE_UPER_Typename     (uses Semantix's asn1scc)
  ENCODE_ACN_Typename     (uses Semantix's asn1scc)
  ENCODE_NATIVE_Typename   (uses memory dumps)
  DECODE_UPER_Typename     (uses Semantix's asn1scc)
  DECODE_ACN_Typename     (uses Semantix's asn1scc)
  DECODE_NATIVE_Typename   (uses memory dumps)

The macros are called from the wrapping layer, created by
Maxime Perottin's buildSupport.

Implementation notes: OG is by far the most complex case;
It is used to model asynchronous processes (SDL)...

The following are done by the buildSupport prior to this backend:

vm_if.c :

 For each Provided Interface which is provided by the modeled APLC,
 a "bridge" function must be created inside this file. This function
 will be in one of two forms:
 (a) if the PI has no associated PARAMETER with it, then this function
     is simply doing
     (I)   G2S_OUTPUT(...);
     (II)  sdl_loop_FVNAME();

 (b) if the PI has an associated PARAMETER with it, then this function
     must
     (I)   decode the ASN.1 message (incoming arguments: pointer,length)
     (II)  place the decoded values in the appropriate ObjectGeode generated
           global variable (for this PI)
     (III) G2S_OUTPUT(...);
     (IV)  sdl_loop_FVNAME();

hpostdef.h :

  OG-related definitions: for each type used by this APLC, a call to

    DEFINE_ASN1TYPENAME(name_of_variable)

  must be issued, to reserve space for the variable. The macro knows
  (by way of this code generator) what size to reserve to accomodate
  the maximum configuration of this message.

  Also, for each Required Interface used by the modeled APLC, this header
  file must also declare a C MACRO:
         #define riname(param1, param2, ...)
  which will
     (I)   encode all the input data from the input params, via the
           ENCODE_ macros, into the reserved spaces (the DEFINE_ASN1TYPENAME
           ones)
     (II)  call the vm callback to access the RI (vm_fvname_riname)
     (III) decode all the output data from the output params, via the
           DECODE_ macros.
  This MACRO will be called by the ObjectGeode generated code whenever the
  RI is to be called.

hpredef.h :

 Contains the prototypes (extern declarations) of the vm callbacks.
'''

from commonPy.asnAST import isSequenceVariable, sourceSequenceLimit, AsnInt, AsnBool, AsnReal, AsnEnumerated

from .recursiveMapper import RecursiveMapper
from .asynchronousTool import ASynchronousToolGlueGenerator

isAsynchronous = True
ogBackend = None


def Version():
    print("Code generator: " + "$Id: og_B_mapper.py 2390 2012-07-19 12:39:17Z ttsiodras $")


105
# noinspection PyListCreation
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
class FromObjectGeodeToASN1SCC(RecursiveMapper):
    def __init__(self):
        self.uniqueID = 0

    def UniqueID(self):
        self.uniqueID += 1
        return self.uniqueID

    def DecreaseUniqueID(self):
        self.uniqueID -= 1

    def MapInteger(self, srcSDLVariable, destVar, _, __, ___):
        return ["%s = (asn1SccSint) %s;\n" % (destVar, srcSDLVariable)]

    def MapReal(self, srcSDLVariable, destVar, _, __, ___):
        return ["%s = (double)%s;\n" % (destVar, srcSDLVariable)]

    def MapBoolean(self, srcSDLVariable, destVar, _, __, ___):
        return ["%s = (%s==SDL_TRUE)?0xff:0;\n" % (destVar, srcSDLVariable)]

    def MapOctetString(self, srcSDLVariable, destVar, node, __, ___):
        lines = []
        lines.append("{\n")
        lines.append("    int i;\n")
        lines.append("    for(i=0; i<%s.length; i++) {\n" % srcSDLVariable)
        lines.append("        unsigned char value = 0;\n")
        lines.append("        if(%s.cont[i].cont[0]) value |= 128;\n" % srcSDLVariable)
        lines.append("        if(%s.cont[i].cont[1]) value |= 64;\n" % srcSDLVariable)
        lines.append("        if(%s.cont[i].cont[2]) value |= 32;\n" % srcSDLVariable)
        lines.append("        if(%s.cont[i].cont[3]) value |= 16;\n" % srcSDLVariable)
        lines.append("        if(%s.cont[i].cont[4]) value |= 8;\n" % srcSDLVariable)
        lines.append("        if(%s.cont[i].cont[5]) value |= 4;\n" % srcSDLVariable)
        lines.append("        if(%s.cont[i].cont[6]) value |= 2;\n" % srcSDLVariable)
        lines.append("        if(%s.cont[i].cont[7]) value |= 1;\n" % srcSDLVariable)
        lines.append("        %s.arr[i] = value;\n" % destVar)
        lines.append("    }\n")
142
143
        # for i in xrange(0, node._range[-1]):
        #     lines.append("    placeHolder[%d] = %s[%d];\n" % (i, srcSDLVariable, i))
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
        if isSequenceVariable(node):
            lines.append("    %s.nCount = %s.length;\n" % (destVar, srcSDLVariable))
        lines.append("}\n")
        return lines

    def MapEnumerated(self, srcSDLVariable, destVar, _, __, ___):
        return ["%s = %s;\n" % (destVar, srcSDLVariable)]

    def MapSequence(self, srcSDLVariable, destVar, node, leafTypeDict, names):
        lines = []
        for child in node._members:
            lines.extend(
                self.Map(
                    "%s.fd_%s" % (srcSDLVariable, self.CleanName(child[0]).lower()),
                    destVar + "." + self.CleanName(child[0]),
                    child[1],
                    leafTypeDict,
                    names))
        return lines

    def MapSet(self, srcSDLVariable, destVar, node, leafTypeDict, names):
        return self.MapSequence(srcSDLVariable, destVar, node, leafTypeDict, names)  # pragma: nocover

    def MapChoice(self, srcSDLVariable, destVar, node, leafTypeDict, names):
        lines = []
        childNo = 0
        for child in node._members:
            childNo += 1
            lines.append("%sif (%s.present == %d) {\n" % (self.maybeElse(childNo), srcSDLVariable, childNo))
173
            lines.extend(
174
                ["    " + x
175
176
177
178
179
180
                 for x in self.Map(
                     "%s.u.u%d.fd_%s" % (srcSDLVariable, childNo, self.CleanName(child[0]).lower()),
                     destVar + (".u.%s" % self.CleanName(child[0])),
                     child[1],
                     leafTypeDict,
                     names)])
181
182
183
184
185
186
187
188
189
190
            lines.append("    %s.kind = %s;\n" % (destVar, self.CleanName(child[2])))
            lines.append("}\n")
        return lines

    def MapSequenceOf(self, srcSDLVariable, destVar, node, leafTypeDict, names):
        lines = []
        lines.append("{\n")
        uniqueId = self.UniqueID()
        lines.append("    int i%s;\n" % uniqueId)
        lines.append("    for(i%s=0; i%s<%s.length; i%s++) {\n" % (uniqueId, uniqueId, srcSDLVariable, uniqueId))
191
        lines.extend(
192
            ["        " + x
193
194
195
196
197
198
             for x in self.Map(
                 "%s.cont[i%s]" % (srcSDLVariable, uniqueId),
                 "%s.arr[i%s]" % (destVar, uniqueId),
                 node._containedType,
                 leafTypeDict,
                 names)])
199
200
201
202
203
204
205
206
207
208
209
        lines.append("    }\n")
        if isSequenceVariable(node):
            lines.append("    %s.nCount = %s.length;\n" % (destVar, srcSDLVariable))
        lines.append("}\n")
        self.DecreaseUniqueID()
        return lines

    def MapSetOf(self, srcSDLVariable, destVar, node, leafTypeDict, names):
        return self.MapSequenceOf(srcSDLVariable, destVar, node, leafTypeDict, names)  # pragma: nocover


210
# noinspection PyListCreation
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
class FromObjectGeodeToOSS(RecursiveMapper):
    def __init__(self):
        self.uniqueID = 0

    def UniqueID(self):
        self.uniqueID += 1
        return self.uniqueID

    def DecreaseUniqueID(self):
        self.uniqueID -= 1

    def MapInteger(self, srcSDLVariable, destVar, _, __, ___):
        return ["%s = %s;\n" % (destVar, srcSDLVariable)]

    def MapReal(self, srcSDLVariable, destVar, _, __, ___):
        return ["%s = %s;\n" % (destVar, srcSDLVariable)]

    def MapBoolean(self, srcSDLVariable, destVar, _, __, ___):
        return ["%s = (%s==SDL_TRUE)?0xff:0;\n" % (destVar, srcSDLVariable)]

    def MapOctetString(self, srcSDLVariable, destVar, _, __, ___):
        lines = []
        lines.append("{\n")
        lines.append("    int i;\n")
        lines.append("    for(i=0; i<%s.length; i++) {\n" % srcSDLVariable)
        lines.append("        unsigned char value = 0;\n")
        lines.append("        if(%s.cont[i].cont[0]) value |= 128;\n" % srcSDLVariable)
        lines.append("        if(%s.cont[i].cont[1]) value |= 64;\n" % srcSDLVariable)
        lines.append("        if(%s.cont[i].cont[2]) value |= 32;\n" % srcSDLVariable)
        lines.append("        if(%s.cont[i].cont[3]) value |= 16;\n" % srcSDLVariable)
        lines.append("        if(%s.cont[i].cont[4]) value |= 8;\n" % srcSDLVariable)
        lines.append("        if(%s.cont[i].cont[5]) value |= 4;\n" % srcSDLVariable)
        lines.append("        if(%s.cont[i].cont[6]) value |= 2;\n" % srcSDLVariable)
        lines.append("        if(%s.cont[i].cont[7]) value |= 1;\n" % srcSDLVariable)
        lines.append("        %s.value[i] = value;\n" % destVar)
        lines.append("    }\n")
247
248
        # for i in xrange(0, node._range[-1]):
        #     lines.append("    placeHolder[%d] = %s[%d];\n" % (i, srcSDLVariable, i))
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
        lines.append("    %s.length = %s.length;\n" % (destVar, srcSDLVariable))
        lines.append("}\n")
        return lines

    def MapEnumerated(self, srcSDLVariable, destVar, _, __, ___):
        return ["%s = %s;\n" % (destVar, srcSDLVariable)]

    def MapSequence(self, srcSDLVariable, destVar, node, leafTypeDict, names):
        lines = []
        for child in node._members:
            lines.extend(
                self.Map(
                    "%s.fd_%s" % (srcSDLVariable, self.CleanName(child[0]).lower()),
                    destVar + "." + self.CleanName(child[0]),
                    child[1],
                    leafTypeDict,
                    names))
        return lines

    def MapSet(self, srcSDLVariable, destVar, node, leafTypeDict, names):
        return self.MapSequence(srcSDLVariable, destVar, node, leafTypeDict, names)  # pragma: nocover

    def MapChoice(self, srcSDLVariable, destVar, node, leafTypeDict, names):
        lines = []
        childNo = 0
        for child in node._members:
            childNo += 1
            lines.append("%sif (%s.present == %d) {\n" % (self.maybeElse(childNo), srcSDLVariable, childNo))
277
            lines.extend(
278
                ["    " + x
279
280
281
282
283
284
                 for x in self.Map(
                     "%s.u.u%d.fd_%s" % (srcSDLVariable, childNo, self.CleanName(child[0]).lower()),
                     destVar + (".u.%s" % self.CleanName(child[0])),
                     child[1],
                     leafTypeDict,
                     names)])
285
286
287
288
289
290
291
292
293
294
            lines.append("    %s.choice = OSS_%s_chosen;\n" % (destVar, self.CleanName(child[0])))
            lines.append("}\n")
        return lines

    def MapSequenceOf(self, srcSDLVariable, destVar, node, leafTypeDict, names):
        lines = []
        lines.append("{\n")
        uniqueId = self.UniqueID()
        lines.append("    int i%s;\n" % uniqueId)
        lines.append("    for(i%s=0; i%s<%s.length; i%s++) {\n" % (uniqueId, uniqueId, srcSDLVariable, uniqueId))
295
        lines.extend(
296
            ["        " + x
297
298
299
300
301
302
             for x in self.Map(
                 "%s.cont[i%s]" % (srcSDLVariable, uniqueId),
                 "%s.value[i%s]" % (destVar, uniqueId),
                 node._containedType,
                 leafTypeDict,
                 names)])
303
304
305
306
307
308
309
310
311
312
        lines.append("    }\n")
        lines.append("    %s.count = %s.length;\n" % (destVar, srcSDLVariable))
        lines.append("}\n")
        self.DecreaseUniqueID()
        return lines

    def MapSetOf(self, srcSDLVariable, destVar, node, leafTypeDict, names):
        return self.MapSequenceOf(srcSDLVariable, destVar, node, leafTypeDict, names)  # pragma: nocover


313
# noinspection PyListCreation
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
class FromASN1SCCtoObjectGeode(RecursiveMapper):
    def __init__(self):
        self.uniqueID = 0

    def UniqueID(self):
        self.uniqueID += 1
        return self.uniqueID

    def DecreaseUniqueID(self):
        self.uniqueID -= 1

    def MapInteger(self, srcVar, dstSDLVariable, _, __, ___):
        return ["%s = %s;\n" % (dstSDLVariable, srcVar)]

    def MapReal(self, srcVar, dstSDLVariable, _, __, ___):
        return ["%s = %s;\n" % (dstSDLVariable, srcVar)]

    def MapBoolean(self, srcVar, dstSDLVariable, _, __, ___):
        return ["%s = (%s)?SDL_TRUE:SDL_FALSE;\n" % (dstSDLVariable, srcVar)]

    def MapOctetString(self, srcVar, dstSDLVariable, node, _, __):
335
336
        # for i in xrange(0, node._range[-1]):
        #     lines.append("%s[%d] = %s->buf[%d];\n" % (dstSDLVariable, i, srcVar, i))
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
        lines = []
        limit = sourceSequenceLimit(node, srcVar)
        lines.append("{\n")
        lines.append("    int i;\n")
        lines.append("    for(i=0; i<%s; i++) {\n" % limit)
        lines.append("        unsigned char value = %s.arr[i];\n" % srcVar)
        lines.append("        %s.cont[i].cont[0] = (value & 128) >> 7;\n" % (dstSDLVariable))
        lines.append("        %s.cont[i].cont[1] = (value & 64) >> 6;\n" % (dstSDLVariable))
        lines.append("        %s.cont[i].cont[2] = (value & 32) >> 5;\n" % (dstSDLVariable))
        lines.append("        %s.cont[i].cont[3] = (value & 16) >> 4;\n" % (dstSDLVariable))
        lines.append("        %s.cont[i].cont[4] = (value & 8) >> 3;\n" % (dstSDLVariable))
        lines.append("        %s.cont[i].cont[5] = (value & 4) >> 2;\n" % (dstSDLVariable))
        lines.append("        %s.cont[i].cont[6] = (value & 2) >> 1;\n" % (dstSDLVariable))
        lines.append("        %s.cont[i].cont[7] = value & 1;\n" % (dstSDLVariable))
        lines.append("        %s.cont[i].length = 8;\n" % (dstSDLVariable))
        lines.append("    }\n")
        lines.append("    while(i<%d) {\n" % node._range[-1])
        lines.append("        %s.cont[i].cont[0]=0;\n" % dstSDLVariable)
        lines.append("        %s.cont[i].cont[1]=0;\n" % dstSDLVariable)
        lines.append("        %s.cont[i].cont[2]=0;\n" % dstSDLVariable)
        lines.append("        %s.cont[i].cont[3]=0;\n" % dstSDLVariable)
        lines.append("        %s.cont[i].cont[4]=0;\n" % dstSDLVariable)
        lines.append("        %s.cont[i].cont[5]=0;\n" % dstSDLVariable)
        lines.append("        %s.cont[i].cont[6]=0;\n" % dstSDLVariable)
        lines.append("        %s.cont[i].cont[7]=0;\n" % dstSDLVariable)
        lines.append("        %s.cont[i].length=0;\n" % dstSDLVariable)
        lines.append("        i++;\n")
        lines.append("    };\n")
        lines.append("    %s.length = %s;\n" % (dstSDLVariable, limit))
        lines.append("}\n")
        return lines

    def MapEnumerated(self, srcVar, dstSDLVariable, _, __, ___):
        return ["%s = %s;\n" % (dstSDLVariable, srcVar)]

    def MapSequence(self, srcVar, dstSDLVariable, node, leafTypeDict, names):
        lines = []
        for child in node._members:
            lines.extend(
                self.Map(
                    srcVar + "." + self.CleanName(child[0]),
                    "%s.fd_%s" % (dstSDLVariable, self.CleanName(child[0]).lower()),
                    child[1],
                    leafTypeDict,
                    names))
        return lines

    def MapSet(self, srcVar, dstSDLVariable, node, leafTypeDict, names):
        return self.MapSequence(srcVar, dstSDLVariable, node, leafTypeDict, names)  # pragma: nocover

    def MapChoice(self, srcVar, dstSDLVariable, node, leafTypeDict, names):
        lines = []
        childNo = 0
        for child in node._members:
            childNo += 1
            lines.append("%sif (%s.kind == %s) {\n" %
                         (self.maybeElse(childNo), srcVar, self.CleanName(child[2])))
394
            lines.extend(
395
                ['    ' + x
396
397
398
399
400
401
                 for x in self.Map(
                     srcVar + ".u." + self.CleanName(child[0]),
                     "%s.u.u%d.fd_%s" % (dstSDLVariable, childNo, self.CleanName(child[0]).lower()),
                     child[1],
                     leafTypeDict,
                     names)])
402
403
404
405
406
407
408
409
410
411
412
413
            lines.append("    %s.present = %d;\n" % (dstSDLVariable, childNo))
            lines.append("}\n")
        return lines

    def MapSequenceOf(self, srcVar, dstSDLVariable, node, leafTypeDict, names):
        lines = []
        lines.append("{\n")
        uniqueId = self.UniqueID()
        lines.append("    int i%s;\n" % uniqueId)
        limit = sourceSequenceLimit(node, srcVar)
        lines.append("    %s.length = %s;\n" % (dstSDLVariable, limit))
        lines.append("    for(i%s=0; i%s<%s; i%s++) {\n" % (uniqueId, uniqueId, limit, uniqueId))
414
        lines.extend(
415
            ["        " + x
416
417
418
419
420
421
             for x in self.Map(
                 srcVar + ".arr[i%s]" % uniqueId,
                 "%s.cont[i%s]" % (dstSDLVariable, uniqueId),
                 node._containedType,
                 leafTypeDict,
                 names)])
422
423
424
425
426
427
428
429
430
        lines.append("    }\n")
        lines.append("}\n")
        self.DecreaseUniqueID()
        return lines

    def MapSetOf(self, srcVar, dstSDLVariable, node, leafTypeDict, names):
        return self.MapSequenceOf(srcVar, dstSDLVariable, node, leafTypeDict, names)  # pragma: nocover


431
# noinspection PyListCreation
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
class FromOSStoObjectGeode(RecursiveMapper):
    def __init__(self):
        self.uniqueID = 0

    def UniqueID(self):
        self.uniqueID += 1
        return self.uniqueID

    def DecreaseUniqueID(self):
        self.uniqueID -= 1

    def MapInteger(self, srcVar, dstSDLVariable, _, __, ___):
        return ["%s = %s;\n" % (dstSDLVariable, srcVar)]

    def MapReal(self, srcVar, dstSDLVariable, _, __, ___):
        return ["%s = %s;\n" % (dstSDLVariable, srcVar)]

    def MapBoolean(self, srcVar, dstSDLVariable, _, __, ___):
        return ["%s = (%s)?SDL_TRUE:SDL_FALSE;\n" % (dstSDLVariable, srcVar)]

    def MapOctetString(self, srcVar, dstSDLVariable, node, _, __):
        lines = []
454
455
        # for i in xrange(0, node._range[-1]):
        #     lines.append("%s[%d] = %s->buf[%d];\n" % (dstSDLVariable, i, srcVar, i))
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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
        lines.append("{\n")
        lines.append("    int i;\n")
        lines.append("    for(i=0; i<%s.length; i++) {\n" % srcVar)
        lines.append("        unsigned char value = %s.value[i];\n" % srcVar)
        lines.append("        %s.cont[i].cont[0] = value & 128;\n" % (dstSDLVariable))
        lines.append("        %s.cont[i].cont[1] = value & 64;\n" % (dstSDLVariable))
        lines.append("        %s.cont[i].cont[2] = value & 32;\n" % (dstSDLVariable))
        lines.append("        %s.cont[i].cont[3] = value & 16;\n" % (dstSDLVariable))
        lines.append("        %s.cont[i].cont[4] = value & 8;\n" % (dstSDLVariable))
        lines.append("        %s.cont[i].cont[5] = value & 4;\n" % (dstSDLVariable))
        lines.append("        %s.cont[i].cont[6] = value & 2;\n" % (dstSDLVariable))
        lines.append("        %s.cont[i].cont[7] = value & 1;\n" % (dstSDLVariable))
        lines.append("        %s.cont[i].length = 8;\n" % (dstSDLVariable))
        lines.append("    }\n")
        lines.append("    while(i<%d) {\n" % node._range[-1])
        lines.append("        %s.cont[i].cont[0]=0;\n" % dstSDLVariable)
        lines.append("        %s.cont[i].cont[1]=0;\n" % dstSDLVariable)
        lines.append("        %s.cont[i].cont[2]=0;\n" % dstSDLVariable)
        lines.append("        %s.cont[i].cont[3]=0;\n" % dstSDLVariable)
        lines.append("        %s.cont[i].cont[4]=0;\n" % dstSDLVariable)
        lines.append("        %s.cont[i].cont[5]=0;\n" % dstSDLVariable)
        lines.append("        %s.cont[i].cont[6]=0;\n" % dstSDLVariable)
        lines.append("        %s.cont[i].cont[7]=0;\n" % dstSDLVariable)
        lines.append("        %s.cont[i].length=0;\n" % dstSDLVariable)
        lines.append("        i++;\n")
        lines.append("    };\n")
        lines.append("    %s.length = %s.length;\n" % (dstSDLVariable, srcVar))
        lines.append("}\n")
        return lines

    def MapEnumerated(self, srcVar, dstSDLVariable, _, __, ___):
        return ["%s = %s;\n" % (dstSDLVariable, srcVar)]

    def MapSequence(self, srcVar, dstSDLVariable, node, leafTypeDict, names):
        lines = []
        for child in node._members:
            lines.extend(
                self.Map(
                    srcVar + "." + self.CleanName(child[0]),
                    "%s.fd_%s" % (dstSDLVariable, self.CleanName(child[0]).lower()),
                    child[1],
                    leafTypeDict,
                    names))
        return lines

    def MapSet(self, srcVar, dstSDLVariable, node, leafTypeDict, names):
        return self.MapSequence(srcVar, dstSDLVariable, node, leafTypeDict, names)  # pragma: nocover

    def MapChoice(self, srcVar, dstSDLVariable, node, leafTypeDict, names):
        lines = []
        childNo = 0
        for child in node._members:
            childNo += 1
            lines.append("%sif (%s.choice == OSS_%s_chosen) {\n" %
                         (self.maybeElse(childNo), srcVar, self.CleanName(child[0])))
511
            lines.extend(
512
                ['    ' + x
513
514
515
516
517
518
                 for x in self.Map(
                     srcVar + ".u." + self.CleanName(child[0]),
                     "%s.u.u%d.fd_%s" % (dstSDLVariable, childNo, self.CleanName(child[0]).lower()),
                     child[1],
                     leafTypeDict,
                     names)])
519
520
521
522
523
524
525
526
527
528
529
            lines.append("    %s.present = %d;\n" % (dstSDLVariable, childNo))
            lines.append("}\n")
        return lines

    def MapSequenceOf(self, srcVar, dstSDLVariable, node, leafTypeDict, names):
        lines = []
        lines.append("{\n")
        uniqueId = self.UniqueID()
        lines.append("    int i%s;\n" % uniqueId)
        lines.append("    %s.length = %s.count;\n" % (dstSDLVariable, srcVar))
        lines.append("    for(i%s=0; i%s<%s.count; i%s++) {\n" % (uniqueId, uniqueId, srcVar, uniqueId))
530
        lines.extend(
531
            ["        " + x
532
533
534
535
536
537
             for x in self.Map(
                 srcVar + ".value[i%s]" % uniqueId,
                 "%s.cont[i%s]" % (dstSDLVariable, uniqueId),
                 node._containedType,
                 leafTypeDict,
                 names)])
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
        lines.append("    }\n")
        lines.append("}\n")
        self.DecreaseUniqueID()
        return lines

    def MapSetOf(self, srcVar, dstSDLVariable, node, leafTypeDict, names):
        return self.MapSequenceOf(srcVar, dstSDLVariable, node, leafTypeDict, names)  # pragma: nocover


class OG_GlueGenerator(ASynchronousToolGlueGenerator):
    def __init__(self):
        ASynchronousToolGlueGenerator.__init__(self)
        self.FromObjectGeodeToASN1SCC = FromObjectGeodeToASN1SCC()
        self.FromObjectGeodeToOSS = FromObjectGeodeToOSS()
        self.FromASN1SCCtoObjectGeode = FromASN1SCCtoObjectGeode()
        self.FromOSStoObjectGeode = FromOSStoObjectGeode()
        self.declarations = {}

    def Version(self):
        print("Code generator: " + "$Id: og_B_mapper.py 2390 2012-07-19 12:39:17Z ttsiodras $")  # pragma: no cover

    def HeadersOnStartup(self, unused_asnFile, unused_outputDir, unused_maybeFVname):
        self.C_HeaderFile.write("#include <assert.h>\n\n")
        if self.useOSS:
            self.C_HeaderFile.write("#include \"%s.oss.h\" // OSS generated\n\n" % self.asn_name)
            self.C_HeaderFile.write("#include \"%s.h\" // Space certified compiler (only for REQUIRED_BYTES_FOR_ENCODING)\n\n" % self.asn_name)
            self.C_HeaderFile.write("extern OssGlobal *g_world;\n\n")
        else:
            self.C_HeaderFile.write("#include \"%s.h\" // Space certified compiler generated\n\n" % self.asn_name)
        self.C_HeaderFile.write("#define ASSERT_MAX(a, b)  (((a) > (b)) ? (a) : (b))\n\n")
        self.C_HeaderFile.write("#define ASSERT_MAX3(a, b, c) ((ASSERT_MAX(a,b)<c) ? (c) : (ASSERT_MAX(a,b)))\n\n")
        self.C_HeaderFile.write("#ifdef __linux__\n")
        self.C_HeaderFile.write("#define CLEAR_MEM(ptr, size)  (void)memset(ptr, 0, size)\n")
        self.C_HeaderFile.write("#else\n")
        self.C_HeaderFile.write("#define CLEAR_MEM(ptr, size)\n")
        self.C_HeaderFile.write("#endif\n\n")

    def Encoder(self, nodeTypename, node, leafTypeDict, names, encoding):
        fileOutHeader = self.C_HeaderFile

        # This method also generates the DECLARE and DEFINE macros.
        if nodeTypename not in self.declarations:
            self.declarations[nodeTypename] = 1
            fileOutHeader.write("#define DECLARE_%s(varName) \\\n" % self.CleanNameAsToolWants(nodeTypename))
            fileOutHeader.write("    extern char varName[ASSERT_MAX3(asn1Scc%s_REQUIRED_BYTES_FOR_ENCODING, asn1Scc%s_REQUIRED_BYTES_FOR_ACN_ENCODING, sizeof(asn1Scc%s))];\\\n" %
                                (self.CleanNameAsToolWants(nodeTypename),
                                 self.CleanNameAsToolWants(nodeTypename),
                                 self.CleanNameAsToolWants(nodeTypename)))
            fileOutHeader.write("    extern int varName##_len;\\\n\\\n\n")

            fileOutHeader.write("#define DEFINE_%s(varName) \\\n" %
                                self.CleanNameAsToolWants(nodeTypename))
            fileOutHeader.write("    char varName[ASSERT_MAX3(asn1Scc%s_REQUIRED_BYTES_FOR_ENCODING, asn1Scc%s_REQUIRED_BYTES_FOR_ACN_ENCODING, sizeof(asn1Scc%s))];\\\n" %
                                (self.CleanNameAsToolWants(nodeTypename),
                                 self.CleanNameAsToolWants(nodeTypename),
                                 self.CleanNameAsToolWants(nodeTypename)))
            fileOutHeader.write("    int varName##_len;\\\n\\\n\n")

        # Create the macro that does the encoding
        fileOutHeader.write(
            "#define ENCODE_%s_%s(varName, param1) {\\\n" %
            ({"uper": "UPER", "native": "NATIVE", "acn": "ACN"}[encoding.lower()], self.CleanNameAsToolWants(nodeTypename)))

        if self.useOSS and encoding.lower() == "uper":
            fileOutHeader.write("    STATIC OssBuf strm;\\\n")
            fileOutHeader.write("    STATIC OSS_%s var_%s;\\\n" %
                                (self.CleanNameAsToolWants(nodeTypename),
                                 self.CleanNameAsToolWants(nodeTypename)))
            fileOutHeader.write("\\\n    strm.value=NULL;\\\n")
            fileOutHeader.write("    strm.length = 0;\\\n")
        elif encoding.lower() in ["uper", "acn"]:
            fileOutHeader.write("    int errorCode = 314159;\\\n")
            fileOutHeader.write("    STATIC BitStream strm;\\\n")
            fileOutHeader.write(
                "    STATIC asn1Scc%s var_%s;\\\n" %
                (self.CleanNameAsToolWants(nodeTypename), self.CleanNameAsToolWants(nodeTypename)))
            fileOutHeader.write(
                "\\\n    BitStream_Init(&strm, varName, asn1Scc%s_REQUIRED_BYTES_FOR_%sENCODING);\\\n" %
616
                (self.CleanNameAsToolWants(nodeTypename), "ACN_" if encoding.lower() == "acn" else ""))
617
618
619
620
621
622
623
624
625
626
627
        else:
            fileOutHeader.write(
                "    STATIC asn1Scc%s var_%s;\\\n" %
                (self.CleanNameAsToolWants(nodeTypename), self.CleanNameAsToolWants(nodeTypename)))
            fileOutHeader.write(
                "    CLEAR_MEM(&var_%s, sizeof(var_%s));\\\n" %
                (self.CleanNameAsToolWants(nodeTypename), self.CleanNameAsToolWants(nodeTypename)))

        # Write the mapping code for the message
        if self.useOSS and encoding.lower() == "uper":
            lines = self.FromObjectGeodeToOSS.Map(
628
                # (isinstance(node, AsnInt) or isinstance(node, AsnBool)) and "(param1)" or "(*(param1))",
629
630
631
632
633
634
635
                "(param1)",
                "var_" + self.CleanNameAsToolWants(nodeTypename),
                node,
                leafTypeDict,
                names)
        else:
            lines = self.FromObjectGeodeToASN1SCC.Map(
636
                # (isinstance(node, AsnInt) or isinstance(node, AsnBool)) and "(param1)" or "(*(param1))",
637
638
639
640
641
642
                "(param1)",
                "var_" + self.CleanNameAsToolWants(nodeTypename),
                node,
                leafTypeDict,
                names)

643
        lines = ["    " + x.rstrip() + "\\" for x in lines]
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
        fileOutHeader.write("\n".join(lines))

        # Call the encoder
        if self.useOSS and encoding.lower() == "uper":
            fileOutHeader.write(
                "\n    if (0 != ossEncode(g_world, OSS_%s_PDU, &var_%s, &strm)) {\\\n" %
                (self.CleanNameAsToolWants(nodeTypename), self.CleanNameAsToolWants(nodeTypename)))
            fileOutHeader.write(
                '\tfprintf(stderr, "Could not encode %s (at %%s, %%d), error message was %%s\\n", __FILE__, __LINE__, ossGetErrMsg(g_world));\\\n' %
                (nodeTypename))
            fileOutHeader.write("        varName##_len = -1;\\\n")
            fileOutHeader.write("    } else {\\\n")
            fileOutHeader.write("        memcpy(&varName[0], strm.value, strm.length);\\\n")
            fileOutHeader.write("        ossFreeBuf(g_world, strm.value);\\\n")
            fileOutHeader.write("        varName##_len = strm.length;\\\n")
            fileOutHeader.write("    }\\\n")
            fileOutHeader.write("}\n\n")
        elif encoding.lower() in ["uper", "acn"]:
            fileOutHeader.write(
                "\n    if (asn1Scc%s_%sEncode(&var_%s, &strm, &errorCode, TRUE) == FALSE) {\\\n" %
                (self.CleanNameAsToolWants(nodeTypename),
                 "ACN_" if encoding.lower() == "acn" else "",
                 self.CleanNameAsToolWants(nodeTypename)))
            fileOutHeader.write(
                '\tfprintf(stderr, "Could not encode %s (at %%s, %%d), error code was %%d\\n", __FILE__, __LINE__, errorCode);\\\n' %
                (nodeTypename))
            fileOutHeader.write("        varName##_len = -1;\\\n")
            fileOutHeader.write("    } else {\\\n")
            fileOutHeader.write("        varName##_len = BitStream_GetLength(&strm);\\\n")
            fileOutHeader.write("    }\\\n")
            fileOutHeader.write("}\n\n")
        else:
            fileOutHeader.write(
                "\n    memcpy(&varName[0], &var_%s, sizeof(asn1Scc%s));\\\n" %
                (self.CleanNameAsToolWants(nodeTypename), self.CleanNameAsToolWants(nodeTypename)))
            fileOutHeader.write(
                "    varName##_len = sizeof(asn1Scc%s);\\\n" %
                self.CleanNameAsToolWants(nodeTypename))
            fileOutHeader.write("}\n\n")

        coding = {"uper": "UPER", "native": "NATIVE", "acn": "ACN"}[encoding.lower()]
        if isinstance(node, AsnInt) or isinstance(node, AsnBool) or isinstance(node, AsnReal) or isinstance(node, AsnEnumerated):
            fileOutHeader.write(
                "#define ENCODE_SYNC_%s_%s(varName, param1) ENCODE_%s_%s(varName, param1)\n\n" %
                (coding,
                 self.CleanNameAsToolWants(nodeTypename),
                 coding,
                 self.CleanNameAsToolWants(nodeTypename)))
            fileOutHeader.write(
                "#define ENCODE_ASYNC_%s_%s(varName, param1) ENCODE_%s_%s(varName, param1)\n\n" %
                (coding,
                 self.CleanNameAsToolWants(nodeTypename),
                 coding,
                 self.CleanNameAsToolWants(nodeTypename)))
        else:
            fileOutHeader.write(
                "#define ENCODE_SYNC_%s_%s(varName, param1) ENCODE_%s_%s(varName, param1)\n\n" %
                (coding,
                 self.CleanNameAsToolWants(nodeTypename),
                 coding,
                 self.CleanNameAsToolWants(nodeTypename)))
            fileOutHeader.write(
                "#define ENCODE_ASYNC_%s_%s(varName, param1) ENCODE_%s_%s(varName, (*param1))\n\n" %
                (coding,
                 self.CleanNameAsToolWants(nodeTypename),
                 coding,
                 self.CleanNameAsToolWants(nodeTypename)))

    def Decoder(self, nodeTypename, node, leafTypeDict, names, encoding):
        fileOutHeader = self.C_HeaderFile
        fileOutHeader.write(
            "#define DECODE_%s_%s(pBuffer, iBufferSize, pSdlVar) {\\\n" %
            ({"uper": "UPER", "native": "NATIVE", "acn": "ACN"}[encoding.lower()], self.CleanNameAsToolWants(nodeTypename)))

        if self.useOSS and encoding.lower() == "uper":
            fileOutHeader.write("    int pdutype = OSS_%s_PDU;\\\n" % self.CleanNameAsToolWants(nodeTypename))
            fileOutHeader.write("    STATIC OssBuf strm;\\\n")
            fileOutHeader.write(
                "    OSS_%s *pVar_%s = NULL;\\\n" %
                (self.CleanNameAsToolWants(nodeTypename), self.CleanNameAsToolWants(nodeTypename)))
            fileOutHeader.write("    strm.value = pBuffer;\\\n")
            fileOutHeader.write("    strm.length = iBufferSize;\\\n")
            fileOutHeader.write(
                "    if (0 == ossDecode(g_world, &pdutype, &strm, (void**)&pVar_%s)) {\\\n" %
                (self.CleanNameAsToolWants(nodeTypename)))
        elif encoding.lower() in ["uper", "acn"]:
            fileOutHeader.write("    int errorCode = 314159;\\\n")
            fileOutHeader.write("    STATIC BitStream strm;\\\n")
            fileOutHeader.write(
                "    STATIC asn1Scc%s var_%s;\\\n" %
                (self.CleanNameAsToolWants(nodeTypename), self.CleanNameAsToolWants(nodeTypename)))
            fileOutHeader.write("    BitStream_AttachBuffer(&strm, pBuffer, iBufferSize);\\\n")
            fileOutHeader.write(
                "    if(asn1Scc%s_%sDecode(&var_%s, &strm, &errorCode)) {\\\n" %
738
                (self.CleanNameAsToolWants(nodeTypename), "ACN_" if encoding.lower() == "acn" else "", self.CleanNameAsToolWants(nodeTypename)))
739
740
741
742
743
744
745
746
747
748
749
750
        else:
            fileOutHeader.write(
                "    STATIC asn1Scc%s var_%s;\\\n" %
                (self.CleanNameAsToolWants(nodeTypename), self.CleanNameAsToolWants(nodeTypename)))
            fileOutHeader.write(
                "    assert(iBufferSize == sizeof(asn1Scc%s));\\\n" %
                self.CleanNameAsToolWants(nodeTypename))
            fileOutHeader.write(
                "    memcpy(&var_%s, pBuffer, sizeof(asn1Scc%s));\\\n" %
                (self.CleanNameAsToolWants(nodeTypename), self.CleanNameAsToolWants(nodeTypename)))
            fileOutHeader.write("    {\\\n")

751
752
        # dstSDLVariable = (isinstance(node, AsnInt) or isinstance(node, AsnBool)) and "(pSdlVar)" or "(*(pSdlVar))"
        # dstSDLVariable = (isinstance(node, AsnInt)) and "(pSdlVar)" or "(*(pSdlVar))"
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
        dstSDLVariable = "(*(pSdlVar))"

        if self.useOSS and encoding.lower() == "uper":
            lines = self.FromOSStoObjectGeode.Map(
                "(*pVar_" + self.CleanNameAsToolWants(nodeTypename) + ")",
                dstSDLVariable,
                node,
                leafTypeDict,
                names)
        else:
            lines = self.FromASN1SCCtoObjectGeode.Map(
                "var_" + self.CleanNameAsToolWants(nodeTypename),
                dstSDLVariable,
                node,
                leafTypeDict,
                names)

770
        lines = ["        " + x.rstrip() + "\\" for x in lines]
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
        fileOutHeader.write("\n".join(lines))

        if self.useOSS and encoding.lower() == "uper":
            fileOutHeader.write("\n        ossFreeBuf(g_world, pVar_%s);\\\n" % self.CleanNameAsToolWants(nodeTypename))
            fileOutHeader.write("        /*return 0*/;\\\n")
            fileOutHeader.write("    } else {\\\n")
            fileOutHeader.write('        fprintf(stderr, "Could not decode %s\\n");\\\n' % (self.CleanNameAsToolWants(nodeTypename)))
            fileOutHeader.write("        /*return -1*/;\\\n")
            fileOutHeader.write("    }\\\n")
            fileOutHeader.write("}\n\n")
        elif encoding.lower() in ["uper", "acn"]:
            fileOutHeader.write("\n        /*return 0*/;\\\n")
            fileOutHeader.write("    } else {\\\n")
            fileOutHeader.write('\tfprintf(stderr, "Could not decode %s\\n");\\\n' % (self.CleanNameAsToolWants(nodeTypename)))
            fileOutHeader.write("        /*return -1*/;\\\n")
            fileOutHeader.write("    }\\\n")
            fileOutHeader.write("}\n\n")
        else:
            fileOutHeader.write("\n        /*return 0*/;\\\n")
            fileOutHeader.write("    }\\\n")
            fileOutHeader.write("}\n\n")

793
794
795
        # fileOutHeader.write(
        #     "#define DECODE_%s(pBuffer, iBufferSize, pSdlVar) DECODE_UPER_%s(pBuffer, iBufferSize, pSdlVar)\n\n" %
        #     (self.CleanNameAsToolWants(nodeTypename), self.CleanNameAsToolWants(nodeTypename)))
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833


def OnStartup(modelingLanguage, asnFile, outputDir, maybeFVname, useOSS):
    global ogBackend
    ogBackend = OG_GlueGenerator()
    ogBackend.OnStartup(modelingLanguage, asnFile, outputDir, maybeFVname, useOSS)


def OnBasic(nodeTypename, node, leafTypeDict, names):
    ogBackend.OnBasic(nodeTypename, node, leafTypeDict, names)


def OnSequence(nodeTypename, node, leafTypeDict, names):
    ogBackend.OnSequence(nodeTypename, node, leafTypeDict, names)


def OnSet(nodeTypename, node, leafTypeDict, names):
    ogBackend.OnSet(nodeTypename, node, leafTypeDict, names)  # pragma: nocover


def OnEnumerated(nodeTypename, node, leafTypeDict, names):
    ogBackend.OnEnumerated(nodeTypename, node, leafTypeDict, names)


def OnSequenceOf(nodeTypename, node, leafTypeDict, names):
    ogBackend.OnSequenceOf(nodeTypename, node, leafTypeDict, names)


def OnSetOf(nodeTypename, node, leafTypeDict, names):
    ogBackend.OnSetOf(nodeTypename, node, leafTypeDict, names)  # pragma: nocover


def OnChoice(nodeTypename, node, leafTypeDict, names):
    ogBackend.OnChoice(nodeTypename, node, leafTypeDict, names)


def OnShutdown(modelingLanguage, asnFile, maybeFVname):
    ogBackend.OnShutdown(modelingLanguage, asnFile, maybeFVname)