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