6 datestring = datetime.datetime.utcfromtimestamp(int(os.environ.get('SOURCE_DATE_EPOCH', time.time())))
7 input_filename = 'inputfil'
10 * VLIB API definitions {datestring}
11 * Input file: {input_filename}
12 * Automatically generated: please edit the input file NOT this file!
15 #if defined(vl_msg_id)||defined(vl_union_id) \\
16 || defined(vl_printfun) ||defined(vl_endianfun) \\
17 || defined(vl_api_version)||defined(vl_typedefs) \\
18 || defined(vl_msg_name)||defined(vl_msg_name_crc_list) \\
19 || defined(vl_api_version_tuple)
20 /* ok, something was selected */
22 #warning no content included from {input_filename}
25 #define VL_API_PACKED(x) x __attribute__ ((packed))
28 bottom_boilerplate = '''\
29 /****** API CRC (whole file) *****/
32 vl_api_version({input_filename}, {file_crc:#08x})
41 /****** Message ID / handler enum ******/
47 output += "vl_msg_id(VL_API_%s, vl_api_%s_t_handler)\n" % \
48 (t.name.upper(), t.name)
57 /****** Message names ******/
63 dont_trace = 0 if t.dont_trace else 1
64 output += "vl_msg_name(vl_api_%s_t, %d)\n" % (t.name, dont_trace)
70 def msg_name_crc_list(s, suffix):
73 /****** Message name, crc list ******/
75 #ifdef vl_msg_name_crc_list
77 output += "#define foreach_vl_msg_name_crc_%s " % suffix
80 output += "\\\n_(VL_API_%s, %s, %08x) " % \
81 (t.name.upper(), t.name, t.crc)
87 def duplicate_wrapper_head(name):
88 s = "#ifndef _vl_api_defined_%s\n" % name
89 s += "#define _vl_api_defined_%s\n" % name
93 def duplicate_wrapper_tail():
97 def typedefs(objs, aliases, filename):
98 name = filename.replace('.', '_')
102 /****** Typedefs ******/
105 #ifndef included_{module}
106 #define included_{module}
108 output = output.format(module=name)
110 for k, v in aliases.items():
111 output += duplicate_wrapper_head(k)
113 output += 'typedef %s vl_api_%s_t[%s];\n' % (v['type'], k, v['length'])
115 output += 'typedef %s vl_api_%s_t;\n' % (v['type'], k)
116 output += duplicate_wrapper_tail()
119 tname = o.__class__.__name__
120 output += duplicate_wrapper_head(o.name)
122 output += "typedef enum {\n"
124 output += " %s = %s,\n" % (b[0], b[1])
125 output += '} vl_api_%s_t;\n' % o.name
128 output += "typedef VL_API_PACKED(union _vl_api_%s {\n" % o.name
130 output += "typedef VL_API_PACKED(struct _vl_api_%s {\n" % o.name
132 if b.type == 'Field':
133 output += " %s %s;\n" % (b.fieldtype, b.fieldname)
134 elif b.type == 'Array':
136 output += " %s %s[0];\n" % (b.fieldtype, b.fieldname)
138 output += " %s %s[%s];\n" % (b.fieldtype, b.fieldname,
141 raise ValueError("Error in processing array type %s" % b)
143 output += '}) vl_api_%s_t;\n' % o.name
144 output += duplicate_wrapper_tail()
147 output += "\n#endif\n\n"
152 format_strings = {'u8': '%u',
165 /****** Print functions *****/
169 #define _uword_fmt \"%lld\"
170 #define _uword_cast (long long)
172 #define _uword_fmt \"%ld\"
173 #define _uword_cast long
178 if t.__class__.__name__ == 'Enum':
181 output += "/***** manual: vl_api_%s_t_print *****/\n\n" % t.name
183 output += duplicate_wrapper_head(t.name + '_t_print')
184 output += "static inline void *vl_api_%s_t_print (vl_api_%s_t *a," % \
186 output += "void *handle)\n{\n"
187 output += " vl_print(handle, \"vl_api_%s_t:\\n\");\n" % t.name
190 if o.type != 'Field':
192 if o.fieldtype in format_strings:
193 output += " vl_print(handle, \"%s: %s\\n\", a->%s);\n" % \
194 (o.fieldname, format_strings[o.fieldtype],
197 output += ' return handle;\n'
199 output += duplicate_wrapper_tail()
201 output += "\n#endif /* vl_printfun */\n"
207 'u16': 'clib_net_to_host_u16',
208 'u32': 'clib_net_to_host_u32',
209 'u64': 'clib_net_to_host_u64',
210 'i16': 'clib_net_to_host_u16',
211 'i32': 'clib_net_to_host_u32',
212 'i64': 'clib_net_to_host_u64',
219 /****** Endian swap functions *****/\n\
222 #undef clib_net_to_host_uword
224 #define clib_net_to_host_uword clib_net_to_host_u64
226 #define clib_net_to_host_uword clib_net_to_host_u32
232 if t.__class__.__name__ == 'Enum':
235 output += "/***** manual: vl_api_%s_t_endian *****/\n\n" % t.name
237 output += duplicate_wrapper_head(t.name + '_t_endian')
238 output += "static inline void vl_api_%s_t_endian (vl_api_%s_t *a)" % \
243 if o.type != 'Field':
245 if o.fieldtype in endian_strings:
246 output += " a->%s = %s(a->%s);\n" % \
247 (o.fieldname, endian_strings[o.fieldtype], o.fieldname)
249 output += " /* a->%s = a->%s (no-op) */\n" % \
250 (o.fieldname, o.fieldname)
253 output += duplicate_wrapper_tail()
254 output += "\n#endif /* vl_endianfun */\n\n"
259 def version_tuple(s, module):
261 /****** Version tuple *****/
263 #ifdef vl_api_version_tuple
266 if 'version' in s['Option']:
267 v = s['Option']['version']
268 (major, minor, patch) = v.split('.')
269 output += "vl_api_version_tuple(%s, %s, %s, %s)\n" % \
270 (module, major, minor, patch)
272 output += "\n#endif /* vl_api_version_tuple */\n\n"
280 def run(input_filename, s, file_crc):
281 basename = os.path.basename(input_filename)
282 filename, file_extension = os.path.splitext(basename)
283 output = top_boilerplate.format(datestring=datestring,
284 input_filename=basename)
286 output += msg_names(s)
287 output += msg_name_crc_list(s, filename)
288 output += typedefs(s['types'] + s['Define'], s['Alias'], filename + file_extension)
289 output += printfun(s['types'] + s['Define'])
290 output += endianfun(s['types'] + s['Define'])
291 output += version_tuple(s, basename)
292 output += bottom_boilerplate.format(input_filename=basename,