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 defined_%s\n" % name
89 s += "#define defined_%s\n" % name
93 def duplicate_wrapper_tail():
97 def typedefs(objs, filename):
98 name = filename.replace('.', '_')
102 /****** Typedefs ******/
105 #ifndef included_{module}
106 #define included_{module}
108 output = output.format(module=name)
110 tname = o.__class__.__name__
111 output += duplicate_wrapper_head(o.name)
113 output += "typedef enum {\n"
115 output += " %s = %s,\n" % (b[0], b[1])
116 output += '} vl_api_%s_t;\n' % o.name
119 output += "typedef VL_API_PACKED(union _vl_api_%s {\n" % o.name
121 output += "typedef VL_API_PACKED(struct _vl_api_%s {\n" % o.name
123 if b.type == 'Field':
124 output += " %s %s;\n" % (b.fieldtype, b.fieldname)
125 elif b.type == 'Array':
127 output += " %s %s[0];\n" % (b.fieldtype, b.fieldname)
129 output += " %s %s[%s];\n" % (b.fieldtype, b.fieldname,
132 raise ValueError("Error in processing array type %s" % b)
134 output += '}) vl_api_%s_t;\n' % o.name
135 output += duplicate_wrapper_tail()
138 output += "\n#endif\n\n"
143 format_strings = {'u8': '%u',
156 /****** Print functions *****/
160 #define _uword_fmt \"%lld\"
161 #define _uword_cast (long long)
163 #define _uword_fmt \"%ld\"
164 #define _uword_cast long
169 if t.__class__.__name__ == 'Enum':
172 output += "/***** manual: vl_api_%s_t_print *****/\n\n" % t.name
174 output += duplicate_wrapper_head(t.name + '_t_print')
175 output += "static inline void *vl_api_%s_t_print (vl_api_%s_t *a," % \
177 output += "void *handle)\n{\n"
178 output += " vl_print(handle, \"vl_api_%s_t:\\n\");\n" % t.name
181 if o.type != 'Field':
183 if o.fieldtype in format_strings:
184 output += " vl_print(handle, \"%s: %s\\n\", a->%s);\n" % \
185 (o.fieldname, format_strings[o.fieldtype],
188 output += ' return handle;\n'
190 output += duplicate_wrapper_tail()
192 output += "\n#endif /* vl_printfun */\n"
198 'u16': 'clib_net_to_host_u16',
199 'u32': 'clib_net_to_host_u32',
200 'u64': 'clib_net_to_host_u64',
201 'i16': 'clib_net_to_host_u16',
202 'i32': 'clib_net_to_host_u32',
203 'i64': 'clib_net_to_host_u64',
210 /****** Endian swap functions *****/\n\
213 #undef clib_net_to_host_uword
215 #define clib_net_to_host_uword clib_net_to_host_u64
217 #define clib_net_to_host_uword clib_net_to_host_u32
223 if t.__class__.__name__ == 'Enum':
226 output += "/***** manual: vl_api_%s_t_endian *****/\n\n" % t.name
228 output += duplicate_wrapper_head(t.name + '_t_endian')
229 output += "static inline void vl_api_%s_t_endian (vl_api_%s_t *a)" % \
234 if o.type != 'Field':
236 if o.fieldtype in endian_strings:
237 output += " a->%s = %s(a->%s);\n" % \
238 (o.fieldname, endian_strings[o.fieldtype], o.fieldname)
240 output += " /* a->%s = a->%s (no-op) */\n" % \
241 (o.fieldname, o.fieldname)
244 output += duplicate_wrapper_tail()
245 output += "\n#endif /* vl_endianfun */\n\n"
250 def version_tuple(s, module):
252 /****** Version tuple *****/
254 #ifdef vl_api_version_tuple
257 if 'version' in s['Option']:
258 v = s['Option']['version']
259 (major, minor, patch) = v.split('.')
260 output += "vl_api_version_tuple(%s, %s, %s, %s)\n" % \
261 (module, major, minor, patch)
263 output += "\n#endif /* vl_api_version_tuple */\n\n"
271 def run(input_filename, s, file_crc):
272 basename = os.path.basename(input_filename)
273 filename, file_extension = os.path.splitext(basename)
274 output = top_boilerplate.format(datestring=datestring,
275 input_filename=basename)
277 output += msg_names(s)
278 output += msg_name_crc_list(s, filename)
279 output += typedefs(s['types'] + s['Define'], filename + file_extension)
280 output += printfun(s['types'] + s['Define'])
281 output += endianfun(s['types'] + s['Define'])
282 output += version_tuple(s, basename)
283 output += bottom_boilerplate.format(input_filename=basename,