blob: d173b81138d76205ec33ed180314d5cfd9c1a9f8 [file] [log] [blame]
Harald Welte245daf92015-08-30 22:38:40 +02001#!/usr/bin/python
2
3import re, os, sys, string
4import datetime
5import getopt
6import getpass
7
8version = "0.5"
9
10lines = ""
11iesDefs = {}
12ieofielist = {}
13outdir = './'
14
15filenames = []
16verbosity = 0
17prefix = ""
18
19FAIL = '\033[91m'
20WARN = '\033[93m'
21ENDC = '\033[0m'
22
23fileprefix = ""
24
25def printFail(string):
26 sys.stderr.write(FAIL + string + ENDC + "\n")
27
28def printWarning(string):
29 print WARN + string + ENDC
30
31def printDebug(string):
32 if verbosity > 0:
33 print string
34
35def outputHeaderToFile(f, filename):
36 now = datetime.datetime.now()
37 f.write("""/*******************************************************************************
38
39 Eurecom OpenAirInterface
40 Copyright(c) 1999 - 2012 Eurecom
41
42 This program is free software; you can redistribute it and/or modify it
43 under the terms and conditions of the GNU General Public License,
44 version 2, as published by the Free Software Foundation.
45
46 This program is distributed in the hope it will be useful, but WITHOUT
47 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
48 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
49 more details.
50
51 You should have received a copy of the GNU General Public License along with
52 this program; if not, write to the Free Software Foundation, Inc.,
53 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
54
55 The full GNU General Public License is included in this distribution in
56 the file called "COPYING".
57
58 Contact Information
59 Openair Admin: openair_admin@eurecom.fr
60 Openair Tech : openair_tech@eurecom.fr
61 Forums : http://forums.eurecom.fr/openairinterface
62 Address : EURECOM, Campus SophiaTech, 450 Route des Chappes
63 06410 Biot FRANCE
64
65*******************************************************************************/
66
67""")
68 f.write("/*******************************************************************************\n")
69 f.write(" * This file had been created by asn1tostruct.py script v%s\n" % (version))
70 f.write(" * Please do not modify this file but regenerate it via script.\n")
71 f.write(" * Created on: %s by %s\n * from %s\n" % (str(now), getpass.getuser(), filenames))
72 f.write(" ******************************************************************************/\n")
73
74def lowerFirstCamelWord(word):
75 """ puts the first word in a CamelCase Word in lowercase.
76
77 I.e. CustomerID becomes customerID, XMLInfoTest becomes xmlInfoTest
78 """
79 newstr = ''
80 swapped = word.swapcase()
81 idx = 0
82
83 # if it's all-caps, return an all-lowered version
84 lowered = word.lower()
85
86 if swapped == lowered:
87 return lowered
88
89 for c in swapped:
90 if c in string.lowercase:
91 newstr += c
92 idx += 1
93 else:
94 break
95 if idx < 2:
96 newstr += word[idx:]
97 else:
98 newstr = newstr[:-1]+ word[idx-1:]
99
100 return newstr
101
102def usage():
103 print "Python parser for asn1 v%s" % (version)
104 print "Usage: python asn1tostruct.py [options]"
105 print "Available options:"
106 print "-d Enable script debug"
107 print "-f [file] Input file to parse"
108 print "-o [dir] Output files to given directory"
109 print "-h Print this help and return"
110
111try:
112 opts, args = getopt.getopt(sys.argv[1:], "df:ho:", ["debug", "file", "help", "outdir"])
113except getopt.GetoptError as err:
114 # print help information and exit:
115 usage()
116 sys.exit(2)
117
118for o, a in opts:
119 if o in ("-f", "--file"):
120 filenames.append(a)
121 if o in ("-d", "--debug"):
122 verbosity = 1
123 if o in ("-o", "--outdir"):
124 outdir = a
125 if outdir.rfind('/') != len(outdir):
126 outdir += '/'
127 if o in ("-h", "--help"):
128 usage()
129 sys.exit(2)
130
131for filename in filenames:
132 file = open(filename, 'r')
133 for line in file:
134 # Removing any comment
135 if line.find('--') >= 0:
136 line = line[:line.find('--')]
137 # Removing any carriage return
138 lines += re.sub('\r', '', line)
139
140 for m in re.findall(r'([a-zA-Z0-9-]+)\s*::=\s+SEQUENCE\s+\(\s*SIZE\s*\(\s*\d+\s*\.\.\s*[0-9a-zA-Z-]+\s*\)\s*\)\s*OF\s+[a-zA-Z-]+\s*\{\s*\{\s*([0-9a-zA-Z-]+)\s*\}\s*\}', lines, re.MULTILINE):
141 ieofielist[m[0]] = m[1]
142 for m in re.findall(r'([a-zA-Z0-9-]+)\s*::=\s+E-RAB-IE-ContainerList\s*\{\s*\{\s*([a-zA-Z0-9-]+)\s*\}\s*\}', lines, re.MULTILINE):
143 ieofielist[m[0]] = m[1]
144
145 for i in re.findall(r'([a-zA-Z0-9-]+)\s+([A-Z0-9-]+)\s*::=\s*\{\s+([\,\|\{\}\t\n\.{3}\ \-a-zA-Z0-9]+)\s+}\n', lines, re.MULTILINE):
146 ies = []
147 maxLength = 0
148 # TODO: handle extensions
149 if i[1].find('EXTENSION') >= 0:
150 continue
151 if fileprefix == "":
152 fileprefix = i[1][:i[1].find('-')].lower()
153 for j in re.findall(r'\s*\{\s*([a-zA-Z0-9-\ \t]+)\s*\}\s*[\|,]*', i[2], re.MULTILINE):
154 for k in re.findall(r'ID\s*([a-zA-Z0-9\-]+)\s*CRITICALITY\s*([a-zA-Z0-9\-]+)\s+[A-Z]+\s+([a-zA-Z0-9\-]+)\s*PRESENCE\s*([a-zA-Z0-9\-]+)', j, re.MULTILINE):
155 printDebug("Got new ie for message " + i[0] + ": " + str(k))
156 if len(k[2]) > maxLength:
157 maxLength = len(k[2])
158 ies.append(k)
159
160 if len(ies) > 0:
161 iesDefs[i[0]] = { "length": maxLength, "ies": ies}
162 else:
163 printWarning("Didn't find any information element for message: " + i[0])
164
165if len(iesDefs) == 0:
166 printFail("No Information Element parsed, exiting")
167 sys.exit(0)
168
169f = open(outdir + fileprefix + '_ies_defs.h', 'w')
170outputHeaderToFile(f, filename)
171f.write("#include \"%s_common.h\"\n\n" % (fileprefix))
172f.write("#ifndef %s_IES_DEFS_H_\n#define %s_IES_DEFS_H_\n\n" % (fileprefix.upper(), fileprefix.upper()))
173
174for key in iesDefs:
175
176 if key not in ieofielist.values():
177 continue
178
179 for (i, j) in ieofielist.items():
180 if j == key:
181 break
182
183 f.write("typedef struct %sIEs_s {\n" % (re.sub('-', '_', i)))
184 f.write(" A_SEQUENCE_OF(struct %s_s) %s;\n" % (re.sub('IEs', '', re.sub('-', '_', ieofielist[i])), lowerFirstCamelWord(re.sub('IEs', '', re.sub('-', '_', ieofielist[i])))))
185 f.write("} %sIEs_t;\n\n" % (re.sub('-', '_', i)))
186
187for key in iesDefs:
188 keyupperunderscore = re.sub('-', '_', key.upper())
189 keylowerunderscore = re.sub('-', '_', key.lower())
190 shift = 0
191
192 if len(iesDefs[key]["ies"]) == 0:
193 continue
194
195 # Presence mask
196 for ie in iesDefs[key]["ies"]:
197 ieupperunderscore = re.sub('-', '_', ie[2].upper())
198 if ie[3] == "optional" or ie[3] == "conditional":
199 f.write("#define {0:<{pad}} {1}\n".format("%s_%s_PRESENT" % (keyupperunderscore, ieupperunderscore), "(1 << %d)" % shift,
200 pad=iesDefs[key]["length"] + len(keyupperunderscore) + 9))
201 shift += 1
202 if (shift > 0):
203 f.write("\n")
204
205 f.write("typedef struct %s_s {\n" % (re.sub('-', '_', key)))
206 if (shift > 0):
207 f.write(" {0:<{pad}} {1};\n".format("uint16_t", "presenceMask", pad=iesDefs[key]["length"] + 2))
208 for ie in iesDefs[key]["ies"]:
209 ieunderscore = re.sub('-', '_', ie[2])
210 iename = re.sub('id-', '', ie[0])
211 ienameunderscore = lowerFirstCamelWord(re.sub('-', '_', iename))
212 if ie[2] in ieofielist:
213 f.write(" %sIEs_t %s;" % (re.sub('-', '_', ie[2]), ienameunderscore))
214 else:
215 f.write(" {0:<{pad}} {1};".format("%s_t" % ieunderscore, ienameunderscore, pad=iesDefs[key]["length"] + 2))
216 if ie[3] == "optional":
217 f.write(" ///< Optional field")
218 elif ie[3] == "conditional":
219 f.write(" ///< Conditional field")
220 f.write("\n")
221
222 f.write("} %s_t;\n\n" % (re.sub('-', '_', key)))
223
224f.write("typedef struct %s_message_s {\n" % (fileprefix))
225f.write(" uint8_t procedureCode;\n")
226f.write(" uint8_t criticality;\n")
227f.write(" uint8_t direction;\n")
228f.write(" union {\n")
229
230messageList = iesDefs.keys()
231messageList.sort()
232for message in messageList:
233 if message in ieofielist.values():
234 continue
235 if len(iesDefs[message]["ies"]) == 0:
236 continue
237 f.write(" %s_t %s;\n" % (re.sub('-', '_', message), lowerFirstCamelWord(re.sub('-', '_', message))))
238f.write(" } msg;\n")
239f.write("} %s_message;\n\n" % (fileprefix))
240
241for key in iesDefs:
242 if key in ieofielist.values():
243 continue
244 structName = re.sub('ies', '', key)
245 asn1cStruct = re.sub('-', '_', re.sub('IEs', '', re.sub('-IEs', '', key)))
246 asn1cStruct = re.sub('Item', 'List', asn1cStruct)
247 keylowerunderscore = re.sub('-', '_', key.lower())
248 firstlower = re.sub('Item', 'List', re.sub('enb', 'eNB', lowerFirstCamelWord(asn1cStruct)))
249 f.write("/** \\brief Decode function for %s ies.\n" % (key))
250 if len(iesDefs[key]["ies"]) != 0:
251 f.write(" * \\param %s Pointer to ASN1 structure in which data will be stored\n" % (lowerFirstCamelWord(re.sub('-', '_', key))))
252 f.write(" * \\param any_p Pointer to the ANY value to decode.\n")
253 f.write(" **/\n")
254 f.write("int %s_decode_%s(\n" % (fileprefix, keylowerunderscore))
255
256 if len(iesDefs[key]["ies"]) != 0:
257 f.write(" %s_t *%s,\n" % (re.sub('-', '_', key), lowerFirstCamelWord(re.sub('-', '_', key))))
258 f.write(" ANY_t *any_p);\n\n")
259
260 if len(iesDefs[key]["ies"]) == 0:
261 continue
262
263 f.write("/** \\brief Encode function for %s ies.\n" % (key))
264 f.write(" * \\param %s Pointer to the ASN1 structure.\n" % (firstlower))
265 f.write(" * \\param %s Pointer to the IES structure.\n" % (lowerFirstCamelWord(re.sub('-', '_', key))))
266 f.write(" **/\n")
267 f.write("int %s_encode_%s(\n" % (fileprefix, re.sub('-', '_', structName.lower())))
268 f.write(" %s_t *%s,\n" % (asn1cStruct, firstlower))
269 f.write(" %s_t *%s);\n\n" % (re.sub('-', '_', key), lowerFirstCamelWord(re.sub('-', '_', key))))
270
271for key in iesDefs:
272 if key not in ieofielist.values():
273 continue
274 asn1cStruct = re.sub('-', '_', re.sub('IEs', '', key))
275 asn1cStruct = re.sub('Item', 'List', asn1cStruct)
276 firstlower = re.sub('Item', 'List', re.sub('enb', 'eNB', lowerFirstCamelWord(asn1cStruct)))
277 f.write("/** \\brief Encode function for %s ies.\n" % (key))
278 f.write(" * \\param %s Pointer to the ASN1 structure.\n" % (firstlower))
279 f.write(" * \\param %s Pointer to the IES structure.\n" % (lowerFirstCamelWord(re.sub('-', '_', key))))
280 f.write(" **/\n")
281 f.write("int %s_encode_%s(\n" % (fileprefix, firstlower.lower()))
282 f.write(" %s_t *%s,\n" % (asn1cStruct, firstlower))
283 f.write(" %sIEs_t *%sIEs);\n\n" % (asn1cStruct, firstlower))
284 f.write("/** \\brief Decode function for %s ies.\n" % (key))
285 f.write(" * \\param any_p Pointer to the ANY value to decode.\n")
286 f.write(" * \\param callback Callback function called when any_p is successfully decoded.\n")
287 f.write(" **/\n")
288 f.write("int %s_decode_%s(\n" % (fileprefix, firstlower.lower()))
289 f.write(" %sIEs_t *%sIEs,\n" % (asn1cStruct, firstlower))
290 f.write(" %s_t *%s);\n\n" % (asn1cStruct, lowerFirstCamelWord(asn1cStruct)))
291f.write("#endif /* %s_IES_DEFS_H_ */\n\n" % (fileprefix.upper()))
292
293#Generate Decode functions
294f = open(outdir + fileprefix + '_decoder.c', 'w')
295outputHeaderToFile(f, filename)
296f.write("#include \"%s_common.h\"\n#include \"%s_ies_defs.h\"\n\n" % (fileprefix, fileprefix))
297for key in iesDefs:
298 if key in ieofielist.values():
299 continue
300 structName = re.sub('ies', '', key)
301 asn1cStruct = re.sub('-', '_', re.sub('IEs', '', key))
302 asn1cStruct = re.sub('Item', 'List', asn1cStruct)
303 ielistname = re.sub('UE', 'ue', asn1cStruct)
304 ielistnamefirstlower = ielistname[:1].lower() + ielistname[1:]
305 asn1cStructfirstlower = asn1cStruct[:1].lower() + asn1cStruct[1:]
306 keyName = re.sub('-', '_', key)
307 keyupperunderscore = keyName.upper()
308 firstlower = re.sub('Item', 'List', re.sub('enb', 'eNB', lowerFirstCamelWord(asn1cStruct)))
309
310 iesaccess = ""
311 if key not in ieofielist.values():
312 iesaccess = "%s_ies." % (firstlower)
313
314 f.write("int %s_decode_%s(\n" % (fileprefix, re.sub('-', '_', structName.lower())))
315 if len(iesDefs[key]["ies"]) != 0:
316 f.write(" %s_t *%s,\n" % (re.sub('-', '_', key), lowerFirstCamelWord(re.sub('-', '_', key))))
317 f.write(" ANY_t *any_p) {\n\n")
318
319 f.write(" %s_t %s;\n %s_t *%s_p = &%s;\n" % (asn1cStruct, asn1cStructfirstlower, asn1cStruct, asn1cStructfirstlower, asn1cStructfirstlower))
320 f.write(" int i, decoded = 0;\n")
321 if len(iesDefs[key]["ies"]) != 0:
322 f.write(" int tempDecoded = 0;\n")
323
324 f.write(" assert(any_p != NULL);\n")
325 if len(iesDefs[key]["ies"]) != 0:
326 f.write(" assert(%s != NULL);\n\n" % (lowerFirstCamelWord(re.sub('-', '_', key))))
327
328 f.write(" %s_DEBUG(\"Decoding message %s (%%s:%%d)\\n\", __FILE__, __LINE__);\n\n" % (fileprefix.upper(), re.sub('-', '_', keyName)))
329 f.write(" ANY_to_type_aper(any_p, &asn_DEF_%s, (void**)&%s_p);\n\n" % (asn1cStruct, asn1cStructfirstlower))
330 f.write(" for (i = 0; i < %s_p->%slist.count; i++) {\n" % (asn1cStructfirstlower, iesaccess))
331 f.write(" IE_t *ie_p;\n")
332 f.write(" ie_p = %s_p->%slist.array[i];\n" % (asn1cStructfirstlower, iesaccess))
333 f.write(" switch(ie_p->id) {\n")
334 for ie in iesDefs[key]["ies"]:
335 iename = re.sub('id-', '', ie[0])
336 ienameunderscore = lowerFirstCamelWord(re.sub('-', '_', iename))
337 ienameunderscorefirstlower = lowerFirstCamelWord(ienameunderscore)
338 ietypesubst = re.sub('-', '', ie[2])
339 ietypeunderscore = re.sub('-', '_', ie[2])
340 ieupperunderscore = re.sub('-', '_', ie[2]).upper()
341 if ie[3] == "optional":
342 f.write(" /* Optional field */\n")
343 elif ie[3] == "conditional":
344 f.write(" /* Conditional field */\n")
345 f.write(" case ProtocolIE_ID_%s:\n" % (re.sub('-', '_', ie[0])))
346 f.write(" {\n")
347 f.write(" %s_t %s;\n" % (ietypeunderscore, lowerFirstCamelWord(ietypesubst)))
348 f.write(" %s_t *%s_p = &%s;\n" % (ietypeunderscore, lowerFirstCamelWord(ietypesubst), lowerFirstCamelWord(ietypesubst)))
349 if ie[3] != "mandatory":
350 f.write(" %s->presenceMask |= %s_%s_PRESENT;\n" % (lowerFirstCamelWord(re.sub('-', '_', key)), keyupperunderscore, ieupperunderscore))
351 f.write(" tempDecoded = ANY_to_type_aper(&ie_p->value, &asn_DEF_%s, (void**)&%s_p);\n" % (ietypeunderscore, lowerFirstCamelWord(ietypesubst)))
352 f.write(" if (tempDecoded < 0) {\n")
353 f.write(" %s_DEBUG(\"Decoding of IE %s failed\\n\");\n" % (fileprefix.upper(), ienameunderscore))
354 f.write(" return -1;\n")
355 f.write(" }\n")
356 f.write(" decoded += tempDecoded;\n")
357 f.write(" if (asn1_xer_print)\n")
358 f.write(" xer_fprint(stdout, &asn_DEF_%s, %s_p);\n" % (ietypeunderscore, lowerFirstCamelWord(ietypesubst)))
359 if ie[2] in ieofielist.keys():
360 f.write(" if (%s_decode_%s(&%s->%s, %s_p) < 0)\n" % (fileprefix, ietypeunderscore.lower(), lowerFirstCamelWord(re.sub('-', '_', key)), ienameunderscore, lowerFirstCamelWord(ietypesubst)))
361 f.write(" %s_DEBUG(\"Decoding of encapsulated IE %s failed\\n\");\n" % (fileprefix.upper(), lowerFirstCamelWord(ietypesubst)))
362 else:
363 f.write(" memcpy(&%s->%s, %s_p, sizeof(%s_t));\n" % (lowerFirstCamelWord(re.sub('-', '_', key)), ienameunderscore, lowerFirstCamelWord(ietypesubst), ietypeunderscore))
364 f.write(" } break;\n")
365 f.write(" default:\n")
366 f.write(" %s_DEBUG(\"Unknown protocol IE id (%%d) for message %s\\n\", (int)ie_p->id);\n" % (fileprefix.upper(), re.sub('-', '_', structName.lower())))
367 f.write(" return -1;\n")
368 f.write(" }\n")
369 f.write(" }\n")
370 f.write(" return decoded;\n")
371 f.write("}\n\n")
372
373for key in iesDefs:
374 if key not in ieofielist.values():
375 continue
376
377 keyname = re.sub('IEs', '', re.sub('Item', 'List', key))
378
379 f.write("int %s_decode_%s(\n" % (fileprefix, re.sub('-', '_', keyname).lower()))
380 f.write(" %sIEs_t *%sIEs,\n" % (re.sub('-', '_', keyname), lowerFirstCamelWord(re.sub('-', '_', keyname))))
381 f.write(" %s_t *%s) {\n\n" % (re.sub('-', '_', keyname), lowerFirstCamelWord(re.sub('-', '_', keyname))))
382 f.write(" int i, decoded = 0;\n")
383 f.write(" int tempDecoded = 0;\n\n")
384 f.write(" for (i = 0; i < %s->list.count; i++) {\n" % (lowerFirstCamelWord(re.sub('-', '_', keyname))))
385 f.write(" IE_t *ie_p = %s->list.array[i];\n" % (lowerFirstCamelWord(re.sub('-', '_', keyname))))
386 f.write(" switch (ie_p->id) {\n")
387 for ie in iesDefs[key]["ies"]:
388 iename = re.sub('id-', '', ie[0])
389 ienameunderscore = lowerFirstCamelWord(re.sub('-', '_', iename))
390 f.write(" case ProtocolIE_ID_%s:\n" % (re.sub('-', '_', ie[0])))
391 f.write(" {\n")
392 f.write(" %s_t *%s_p;\n" % (re.sub('-', '_', ie[2]), lowerFirstCamelWord(re.sub('-', '', ie[2]))))
393 f.write(" tempDecoded = ANY_to_type_aper(&ie_p->value, &asn_DEF_%s, (void**)&%s_p);\n" % (re.sub('-', '_', ie[2]), lowerFirstCamelWord(re.sub('-', '', ie[2]))))
394 f.write(" if (tempDecoded < 0) {\n")
395 f.write(" %s_DEBUG(\"Decoding of IE %s for message %s failed\\n\");\n" % (fileprefix.upper(), ienameunderscore, re.sub('-', '_', keyname)))
396 f.write(" return -1;\n")
397 f.write(" }\n")
398 f.write(" decoded += tempDecoded;\n")
399 f.write(" if (asn1_xer_print)\n")
400 f.write(" xer_fprint(stdout, &asn_DEF_%s, %s_p);\n" % (re.sub('-', '_', ie[2]), lowerFirstCamelWord(re.sub('-', '', ie[2]))))
401 f.write(" ASN_SEQUENCE_ADD(&%sIEs->%s, %s_p);\n" % (lowerFirstCamelWord(re.sub('-', '_', keyname)),
402 re.sub('IEs', '', lowerFirstCamelWord(re.sub('-', '_', key))), lowerFirstCamelWord(re.sub('-', '', ie[2]))))
403 f.write(" } break;\n")
404 f.write(" default:\n")
405 f.write(" %s_DEBUG(\"Unknown protocol IE id (%%d) for message %s\\n\", (int)ie_p->id);\n" % (fileprefix.upper(), re.sub('-', '_', structName.lower())))
406 f.write(" return -1;\n")
407 f.write(" }\n")
408 f.write(" }\n")
409 f.write(" return decoded;\n")
410 f.write("}\n\n")
411
412
413#Generate IES Encode functions
414f = open(outdir + fileprefix + '_encoder.c', 'w')
415outputHeaderToFile(f,filename)
416f.write("#include \"%s_common.h\"\n" % (fileprefix))
417f.write("#include \"%s_ies_defs.h\"\n\n" % (fileprefix))
418for key in iesDefs:
419 if key in ieofielist.values():
420 continue
421
422 structName = re.sub('ies', '', key)
423 asn1cStruct = re.sub('-', '_', re.sub('IEs', '', key))
424 asn1cStruct = re.sub('Item', 'List', asn1cStruct)
425 asn1cStructfirstlower = asn1cStruct[:1].lower() + asn1cStruct[1:]
426 firstwordlower = re.sub('Item', 'List', re.sub('enb', 'eNB', lowerFirstCamelWord(asn1cStruct)))
427
428 iesaccess = ""
429 if key not in ieofielist.values():
430 iesaccess = "%s_ies." % (firstwordlower)
431
432 keyName = re.sub('-', '_', key)
433 keyupperunderscore = keyName.upper()
434 # No IE to encode...
435 if len(iesDefs[key]["ies"]) == 0:
436 continue
437
438 f.write("int %s_encode_%s(\n" % (fileprefix, re.sub('-', '_', structName.lower())))
439 f.write(" %s_t *%s,\n" % (asn1cStruct, firstwordlower))
440 f.write(" %s_t *%s) {\n\n" % (re.sub('-', '_', key), lowerFirstCamelWord(re.sub('-', '_', key))))
441
442 f.write(" IE_t *ie;\n\n")
443
444 for ie in iesDefs[key]["ies"]:
445 iename = re.sub('-', '_', re.sub('id-', '', ie[0]))
446 ienameunderscore = re.sub('-', '_', iename)
447 ienamefirstwordlower = lowerFirstCamelWord(iename)
448 ieupperunderscore = re.sub('-', '_', ie[2]).upper()
449 ietypeunderscore = re.sub('-', '_', ie[2])
450 if ie[3] != "mandatory":
451 if ie[3] == "optional":
452 f.write(" /* Optional field */\n")
453 elif ie[3] == "conditional":
454 f.write(" /* Conditional field */\n")
455 f.write(" if ((%s->presenceMask & %s_%s_PRESENT)\n" % (lowerFirstCamelWord(re.sub('-', '_', key)), keyupperunderscore, ieupperunderscore))
456 f.write(" == %s_%s_PRESENT) {\n" % (keyupperunderscore, ieupperunderscore))
457 f.write(" if ((ie = %s_new_ie(ProtocolIE_ID_%s,\n" % (fileprefix, re.sub('-', '_', ie[0])))
458 f.write(" Criticality_%s,\n" % (ie[1]))
459 f.write(" &asn_DEF_%s,\n" % (ietypeunderscore))
460 f.write(" &%s->%s)) == NULL) {\n" % (lowerFirstCamelWord(re.sub('-', '_', key)), ienamefirstwordlower))
461 f.write(" return -1;\n")
462 f.write(" }\n")
463 f.write(" ASN_SEQUENCE_ADD(&%s->%slist, ie);\n" % (firstwordlower, iesaccess))
464 f.write(" }\n\n")
465 else:
466 if ie[2] in ieofielist.keys():
467 f.write(" %s_t %s;\n\n" % (ietypeunderscore, ienamefirstwordlower))
468 f.write(" memset(&%s, 0, sizeof(%s_t));\n" % (ienamefirstwordlower, ietypeunderscore))
469 f.write("\n")
470 f.write(" if (%s_encode_%s(&%s, &%s->%s) < 0) return -1;\n" % (fileprefix, ietypeunderscore.lower(), ienamefirstwordlower, lowerFirstCamelWord(re.sub('-', '_', key)), ienamefirstwordlower))
471 f.write(" if ((ie = %s_new_ie(ProtocolIE_ID_%s,\n" % (fileprefix, re.sub('-', '_', ie[0])))
472 f.write(" Criticality_%s,\n" % (ie[1]))
473 f.write(" &asn_DEF_%s,\n" % (ietypeunderscore))
474 if ie[2] in ieofielist.keys():
475 f.write(" &%s)) == NULL) {\n" % (ienamefirstwordlower))
476 else:
477 f.write(" &%s->%s)) == NULL) {\n" % (lowerFirstCamelWord(re.sub('-', '_', key)), ienamefirstwordlower))
478 f.write(" return -1;\n")
479 f.write(" }\n")
480 f.write(" ASN_SEQUENCE_ADD(&%s->%slist, ie);\n\n" % (firstwordlower, iesaccess))
481
482 f.write(" return 0;\n")
483 f.write("}\n\n")
484
485for (key, value) in iesDefs.items():
486 if key not in ieofielist.values():
487 continue
488
489 ie = value["ies"][0]
490 ietypeunderscore = re.sub('-', '_', ie[2])
491 asn1cStruct = re.sub('-', '_', re.sub('IEs', '', re.sub('-IEs', '', key)))
492 asn1cStruct = re.sub('Item', 'List', asn1cStruct)
493 firstwordlower = re.sub('Item', 'List', re.sub('enb', 'eNB', lowerFirstCamelWord(asn1cStruct)))
494
495 for (i, j) in ieofielist.items():
496 if j == key:
497 break
498 f.write("int %s_encode_%s(\n" % (fileprefix, re.sub('-', '_', i).lower()))
499 f.write(" %s_t *%s,\n" % (asn1cStruct, firstwordlower))
500 f.write(" %sIEs_t *%sIEs) {\n\n" % (re.sub('-', '_', i), lowerFirstCamelWord(re.sub('-', '_', i))))
501 f.write(" int i;\n")
502
503 f.write(" IE_t *ie;\n\n")
504
505 f.write(" for (i = 0; i < %sIEs->%s.count; i++) {\n" % (firstwordlower, re.sub('IEs', '', lowerFirstCamelWord(re.sub('-', '_', key)))))
506 f.write(" if ((ie = %s_new_ie(ProtocolIE_ID_%s,\n" % (fileprefix, re.sub('-', '_', ie[0])))
507 f.write(" Criticality_%s,\n" % (ie[1]))
508 f.write(" &asn_DEF_%s,\n" % (ietypeunderscore))
509 f.write(" %sIEs->%s.array[i])) == NULL) {\n" % (firstwordlower, re.sub('IEs', '', lowerFirstCamelWord(re.sub('-', '_', key)))))
510 f.write(" return -1;\n")
511 f.write(" }\n")
512 f.write(" ASN_SEQUENCE_ADD(&%s->list, ie);\n" % (firstwordlower))
513 f.write(" }\n")
514 f.write(" return 0;\n")
515 f.write("}\n\n")