6 datestring = datetime.datetime.utcfromtimestamp(
7 int(os.environ.get('SOURCE_DATE_EPOCH', time.time())))
8 input_filename = 'inputfil'
11 * VLIB API definitions {datestring}
12 * Input file: {input_filename}
13 * Automatically generated: please edit the input file NOT this file!
16 #if defined(vl_msg_id)||defined(vl_union_id) \\
17 || defined(vl_printfun) ||defined(vl_endianfun) \\
18 || defined(vl_api_version)||defined(vl_typedefs) \\
19 || defined(vl_msg_name)||defined(vl_msg_name_crc_list) \\
20 || defined(vl_api_version_tuple)
21 /* ok, something was selected */
23 #warning no content included from {input_filename}
26 #define VL_API_PACKED(x) x __attribute__ ((packed))
29 bottom_boilerplate = '''\
30 /****** API CRC (whole file) *****/
33 vl_api_version({input_filename}, {file_crc:#08x})
42 /****** Message ID / handler enum ******/
48 output += "vl_msg_id(VL_API_%s, vl_api_%s_t_handler)\n" % \
49 (t.name.upper(), t.name)
58 /****** Message names ******/
64 dont_trace = 0 if t.dont_trace else 1
65 output += "vl_msg_name(vl_api_%s_t, %d)\n" % (t.name, dont_trace)
71 def msg_name_crc_list(s, suffix):
74 /****** Message name, crc list ******/
76 #ifdef vl_msg_name_crc_list
78 output += "#define foreach_vl_msg_name_crc_%s " % suffix
81 output += "\\\n_(VL_API_%s, %s, %08x) " % \
82 (t.name.upper(), t.name, t.crc)
88 def duplicate_wrapper_head(name):
89 s = "#ifndef _vl_api_defined_%s\n" % name
90 s += "#define _vl_api_defined_%s\n" % name
94 def duplicate_wrapper_tail():
99 mappingtable = {'string': 'vl_api_string_t', }
100 if fieldtype in mappingtable:
101 return mappingtable[fieldtype]
105 def typedefs(objs, aliases, filename):
106 name = filename.replace('.', '_')
110 /****** Typedefs ******/
113 #ifndef included_{module}
114 #define included_{module}
116 output = output.format(module=name)
118 for k, v in aliases.items():
119 output += duplicate_wrapper_head(k)
121 output += 'typedef %s vl_api_%s_t[%s];\n' % (v['type'], k, v['length'])
123 output += 'typedef %s vl_api_%s_t;\n' % (v['type'], k)
124 output += duplicate_wrapper_tail()
127 tname = o.__class__.__name__
128 output += duplicate_wrapper_head(o.name)
130 output += "typedef enum {\n"
132 output += " %s = %s,\n" % (b[0], b[1])
133 output += '} vl_api_%s_t;\n' % o.name
136 output += "typedef VL_API_PACKED(union _vl_api_%s {\n" % o.name
138 output += "typedef VL_API_PACKED(struct _vl_api_%s {\n" % o.name
140 if b.type == 'Field':
141 output += " %s %s;\n" % (api2c(b.fieldtype), b.fieldname)
142 elif b.type == 'Array':
144 output += " %s %s[0];\n" % (api2c(b.fieldtype), b.fieldname)
146 output += " %s %s[%s];\n" % (api2c(b.fieldtype), b.fieldname,
149 raise ValueError("Error in processing array type %s" % b)
151 output += '}) vl_api_%s_t;\n' % o.name
152 output += duplicate_wrapper_tail()
155 output += "\n#endif\n\n"
160 format_strings = {'u8': '%u',
173 /****** Print functions *****/
177 #define _uword_fmt \"%lld\"
178 #define _uword_cast (long long)
180 #define _uword_fmt \"%ld\"
181 #define _uword_cast long
186 if t.__class__.__name__ == 'Enum':
189 output += "/***** manual: vl_api_%s_t_print *****/\n\n" % t.name
191 output += duplicate_wrapper_head(t.name + '_t_print')
192 output += "static inline void *vl_api_%s_t_print (vl_api_%s_t *a," % \
194 output += "void *handle)\n{\n"
195 output += " vl_print(handle, \"vl_api_%s_t:\\n\");\n" % t.name
198 if o.type != 'Field':
200 if o.fieldtype in format_strings:
201 output += " vl_print(handle, \"%s: %s\\n\", a->%s);\n" % \
202 (o.fieldname, format_strings[o.fieldtype],
205 output += ' return handle;\n'
207 output += duplicate_wrapper_tail()
209 output += "\n#endif /* vl_printfun */\n"
215 'u16': 'clib_net_to_host_u16',
216 'u32': 'clib_net_to_host_u32',
217 'u64': 'clib_net_to_host_u64',
218 'i16': 'clib_net_to_host_u16',
219 'i32': 'clib_net_to_host_u32',
220 'i64': 'clib_net_to_host_u64',
227 /****** Endian swap functions *****/\n\
230 #undef clib_net_to_host_uword
232 #define clib_net_to_host_uword clib_net_to_host_u64
234 #define clib_net_to_host_uword clib_net_to_host_u32
240 if t.__class__.__name__ == 'Enum':
243 output += "/***** manual: vl_api_%s_t_endian *****/\n\n" % t.name
245 output += duplicate_wrapper_head(t.name + '_t_endian')
246 output += "static inline void vl_api_%s_t_endian (vl_api_%s_t *a)" % \
251 if o.type != 'Field':
253 if o.fieldtype in endian_strings:
254 output += " a->%s = %s(a->%s);\n" % \
255 (o.fieldname, endian_strings[o.fieldtype], o.fieldname)
257 output += " /* a->%s = a->%s (no-op) */\n" % \
258 (o.fieldname, o.fieldname)
261 output += duplicate_wrapper_tail()
262 output += "\n#endif /* vl_endianfun */\n\n"
267 def version_tuple(s, module):
269 /****** Version tuple *****/
271 #ifdef vl_api_version_tuple
274 if 'version' in s['Option']:
275 v = s['Option']['version']
276 (major, minor, patch) = v.split('.')
277 output += "vl_api_version_tuple(%s, %s, %s, %s)\n" % \
278 (module, major, minor, patch)
280 output += "\n#endif /* vl_api_version_tuple */\n\n"
288 def run(input_filename, s, file_crc):
289 basename = os.path.basename(input_filename)
290 filename, file_extension = os.path.splitext(basename)
291 output = top_boilerplate.format(datestring=datestring,
292 input_filename=basename)
294 output += msg_names(s)
295 output += msg_name_crc_list(s, filename)
296 output += typedefs(s['types'] + s['Define'], s['Alias'], filename + file_extension)
297 output += printfun(s['types'] + s['Define'])
298 output += endianfun(s['types'] + s['Define'])
299 output += version_tuple(s, basename)
300 output += bottom_boilerplate.format(input_filename=basename,