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!
17 #if defined(vl_msg_id)||defined(vl_union_id) \\
18 || defined(vl_printfun) ||defined(vl_endianfun) \\
19 || defined(vl_api_version)||defined(vl_typedefs) \\
20 || defined(vl_msg_name)||defined(vl_msg_name_crc_list) \\
21 || defined(vl_api_version_tuple)
22 /* ok, something was selected */
24 #warning no content included from {input_filename}
27 #define VL_API_PACKED(x) x __attribute__ ((packed))
30 bottom_boilerplate = '''\
31 /****** API CRC (whole file) *****/
34 vl_api_version({input_filename}, {file_crc:#08x})
43 /****** Message ID / handler enum ******/
49 output += "vl_msg_id(VL_API_%s, vl_api_%s_t_handler)\n" % \
50 (t.name.upper(), t.name)
59 /****** Message names ******/
65 dont_trace = 0 if t.dont_trace else 1
66 output += "vl_msg_name(vl_api_%s_t, %d)\n" % (t.name, dont_trace)
72 def msg_name_crc_list(s, suffix):
75 /****** Message name, crc list ******/
77 #ifdef vl_msg_name_crc_list
79 output += "#define foreach_vl_msg_name_crc_%s " % suffix
82 output += "\\\n_(VL_API_%s, %s, %08x) " % \
83 (t.name.upper(), t.name, t.crc)
89 def duplicate_wrapper_head(name):
90 s = "#ifndef _vl_api_defined_%s\n" % name
91 s += "#define _vl_api_defined_%s\n" % name
95 def duplicate_wrapper_tail():
100 mappingtable = {'string': 'vl_api_string_t', }
101 if fieldtype in mappingtable:
102 return mappingtable[fieldtype]
106 def typedefs(objs, aliases, filename):
107 name = filename.replace('.', '_')
111 /****** Typedefs ******/
114 #ifndef included_{module}
115 #define included_{module}
117 output = output.format(module=name)
119 for k, v in aliases.items():
120 output += duplicate_wrapper_head(k)
122 output += 'typedef %s vl_api_%s_t[%s];\n' % (v['type'], k, v['length'])
124 output += 'typedef %s vl_api_%s_t;\n' % (v['type'], k)
125 output += duplicate_wrapper_tail()
128 tname = o.__class__.__name__
129 output += duplicate_wrapper_head(o.name)
131 if o.enumtype == 'u32':
132 output += "typedef enum {\n"
134 output += "typedef enum __attribute__((__packed__)) {\n"
137 output += " %s = %s,\n" % (b[0], b[1])
138 output += '} vl_api_%s_t;\n' % o.name
139 if o.enumtype != 'u32':
140 size1 = 'sizeof(vl_api_%s_t)' % o.name
141 size2 = 'sizeof(%s)' % o.enumtype
142 err_str = 'size of API enum %s is wrong' % o.name
143 output += 'STATIC_ASSERT(%s == %s, "%s");\n' % (size1, size2, err_str)
146 output += "typedef VL_API_PACKED(union _vl_api_%s {\n" % o.name
148 output += "typedef VL_API_PACKED(struct _vl_api_%s {\n" % o.name
150 if b.type == 'Field':
151 output += " %s %s;\n" % (api2c(b.fieldtype), b.fieldname)
152 elif b.type == 'Array':
154 output += " %s %s[0];\n" % (api2c(b.fieldtype), b.fieldname)
156 # Fixed length strings decay to nul terminated u8
157 if b.fieldtype == 'string':
159 output += ' {} {};\n'.format(api2c(b.fieldtype), b.fieldname)
161 output += ' u8 {}[{}];\n'.format(b.fieldname, b.length)
163 output += " %s %s[%s];\n" % (api2c(b.fieldtype), b.fieldname,
166 raise ValueError("Error in processing array type %s" % b)
168 output += '}) vl_api_%s_t;\n' % o.name
169 output += duplicate_wrapper_tail()
172 output += "\n#endif\n\n"
177 format_strings = {'u8': '%u',
190 /****** Print functions *****/
194 #define _uword_fmt \"%lld\"
195 #define _uword_cast (long long)
197 #define _uword_fmt \"%ld\"
198 #define _uword_cast long
203 if t.__class__.__name__ == 'Enum':
206 output += "/***** manual: vl_api_%s_t_print *****/\n\n" % t.name
208 output += duplicate_wrapper_head(t.name + '_t_print')
209 output += "static inline void *vl_api_%s_t_print (vl_api_%s_t *a," % \
211 output += "void *handle)\n{\n"
212 output += " vl_print(handle, \"vl_api_%s_t:\\n\");\n" % t.name
215 if o.type != 'Field':
217 if o.fieldtype in format_strings:
218 output += " vl_print(handle, \"%s: %s\\n\", a->%s);\n" % \
219 (o.fieldname, format_strings[o.fieldtype],
222 output += ' return handle;\n'
224 output += duplicate_wrapper_tail()
226 output += "\n#endif /* vl_printfun */\n"
232 'u16': 'clib_net_to_host_u16',
233 'u32': 'clib_net_to_host_u32',
234 'u64': 'clib_net_to_host_u64',
235 'i16': 'clib_net_to_host_u16',
236 'i32': 'clib_net_to_host_u32',
237 'i64': 'clib_net_to_host_u64',
238 'f64': 'clib_net_to_host_u64',
245 /****** Endian swap functions *****/\n\
248 #undef clib_net_to_host_uword
250 #define clib_net_to_host_uword clib_net_to_host_u64
252 #define clib_net_to_host_uword clib_net_to_host_u32
258 if t.__class__.__name__ == 'Enum':
261 output += "/***** manual: vl_api_%s_t_endian *****/\n\n" % t.name
263 output += duplicate_wrapper_head(t.name + '_t_endian')
264 output += "static inline void vl_api_%s_t_endian (vl_api_%s_t *a)" % \
269 if o.type != 'Field':
271 if o.fieldtype in endian_strings:
272 output += " a->%s = %s(a->%s);\n" % \
273 (o.fieldname, endian_strings[o.fieldtype], o.fieldname)
275 output += " /* a->%s = a->%s (no-op) */\n" % \
276 (o.fieldname, o.fieldname)
279 output += duplicate_wrapper_tail()
280 output += "\n#endif /* vl_endianfun */\n\n"
285 def version_tuple(s, module):
287 /****** Version tuple *****/
289 #ifdef vl_api_version_tuple
292 if 'version' in s['Option']:
293 v = s['Option']['version']
294 (major, minor, patch) = v.split('.')
295 output += "vl_api_version_tuple(%s, %s, %s, %s)\n" % \
296 (module, major, minor, patch)
298 output += "\n#endif /* vl_api_version_tuple */\n\n"
306 def run(input_filename, s):
307 basename = os.path.basename(input_filename)
308 filename, file_extension = os.path.splitext(basename)
309 output = top_boilerplate.format(datestring=datestring,
310 input_filename=basename)
312 output += msg_names(s)
313 output += msg_name_crc_list(s, filename)
314 output += typedefs(s['types'] + s['Define'], s['Alias'], filename + file_extension)
315 output += printfun(s['types'] + s['Define'])
316 output += endianfun(s['types'] + s['Define'])
317 output += version_tuple(s, basename)
318 output += bottom_boilerplate.format(input_filename=basename,
319 file_crc=s['file_crc'])