5 datestring = datetime.datetime.now()
6 input_filename = 'inputfil'
9 * VLIB API definitions {datestring}
10 * Input file: {input_filename}
11 * Automatically generated: please edit the input file NOT this file!
14 #if defined(vl_msg_id)||defined(vl_union_id) \\
15 || defined(vl_printfun) ||defined(vl_endianfun) \\
16 || defined(vl_api_version)||defined(vl_typedefs) \\
17 || defined(vl_msg_name)||defined(vl_msg_name_crc_list) \\
18 || defined(vl_api_version_tuple)
19 /* ok, something was selected */
21 #warning no content included from {input_filename}
24 #define VL_API_PACKED(x) x __attribute__ ((packed))
27 bottom_boilerplate = '''\
28 /****** API CRC (whole file) *****/
31 vl_api_version({input_filename}, {file_crc:#08x})
40 /****** Message ID / handler enum ******/
46 output += "vl_msg_id(VL_API_%s, vl_api_%s_t_handler)\n" % \
47 (t.name.upper(), t.name)
56 /****** Message names ******/
62 dont_trace = 0 if t.dont_trace else 1
63 output += "vl_msg_name(vl_api_%s_t, %d)\n" % (t.name, dont_trace)
69 def msg_name_crc_list(s, suffix):
72 /****** Message name, crc list ******/
74 #ifdef vl_msg_name_crc_list
76 output += "#define foreach_vl_msg_name_crc_%s " % suffix
79 output += "\\\n_(VL_API_%s, %s, %08x) " % \
80 (t.name.upper(), t.name, t.crc)
86 def duplicate_wrapper_head(name):
87 s = "#ifndef defined_%s\n" % name
88 s += "#define defined_%s\n" % name
92 def duplicate_wrapper_tail():
96 def typedefs(objs, filename):
97 name = filename.replace('.', '_')
101 /****** Typedefs ******/
104 #ifndef included_{module}
105 #define included_{module}
107 output = output.format(module=name)
109 tname = o.__class__.__name__
110 output += duplicate_wrapper_head(o.name)
112 output += "typedef enum {\n"
114 output += " %s = %s,\n" % (b[0], b[1])
115 output += '} vl_api_%s_t;\n' % o.name
118 output += "typedef VL_API_PACKED(union _vl_api_%s {\n" % o.name
120 output += "typedef VL_API_PACKED(struct _vl_api_%s {\n" % o.name
122 if b.type == 'Field':
123 output += " %s %s;\n" % (b.fieldtype, b.fieldname)
124 elif b.type == 'Array':
126 output += " %s %s[0];\n" % (b.fieldtype, b.fieldname)
128 output += " %s %s[%s];\n" % (b.fieldtype, b.fieldname,
131 raise ValueError("Error in processing array type %s" % b)
133 output += '}) vl_api_%s_t;\n' % o.name
134 output += duplicate_wrapper_tail()
137 output += "\n#endif\n\n"
142 format_strings = {'u8': '%u',
155 /****** Print functions *****/
159 #define _uword_fmt \"%lld\"
160 #define _uword_cast (long long)
162 #define _uword_fmt \"%ld\"
163 #define _uword_cast long
168 if t.__class__.__name__ == 'Enum':
171 output += "/***** manual: vl_api_%s_t_print *****/\n\n" % t.name
173 output += duplicate_wrapper_head(t.name + '_t_print')
174 output += "static inline void *vl_api_%s_t_print (vl_api_%s_t *a," % \
176 output += "void *handle)\n{\n"
177 output += " vl_print(handle, \"vl_api_%s_t:\\n\");\n" % t.name
180 if o.type != 'Field':
182 if o.fieldtype in format_strings:
183 output += " vl_print(handle, \"%s: %s\\n\", a->%s);\n" % \
184 (o.fieldname, format_strings[o.fieldtype],
187 output += ' return handle;\n'
189 output += duplicate_wrapper_tail()
191 output += "\n#endif /* vl_printfun */\n"
197 'u16': 'clib_net_to_host_u16',
198 'u32': 'clib_net_to_host_u32',
199 'u64': 'clib_net_to_host_u64',
200 'i16': 'clib_net_to_host_u16',
201 'i32': 'clib_net_to_host_u32',
202 'i64': 'clib_net_to_host_u64',
209 /****** Endian swap functions *****/\n\
212 #undef clib_net_to_host_uword
214 #define clib_net_to_host_uword clib_net_to_host_u64
216 #define clib_net_to_host_uword clib_net_to_host_u32
222 if t.__class__.__name__ == 'Enum':
225 output += "/***** manual: vl_api_%s_t_endian *****/\n\n" % t.name
227 output += duplicate_wrapper_head(t.name + '_t_endian')
228 output += "static inline void vl_api_%s_t_endian (vl_api_%s_t *a)" % \
233 if o.type != 'Field':
235 if o.fieldtype in endian_strings:
236 output += " a->%s = %s(a->%s);\n" % \
237 (o.fieldname, endian_strings[o.fieldtype], o.fieldname)
239 output += " /* a->%s = a->%s (no-op) */\n" % \
240 (o.fieldname, o.fieldname)
243 output += duplicate_wrapper_tail()
244 output += "\n#endif /* vl_endianfun */\n\n"
249 def version_tuple(s, module):
251 /****** Version tuple *****/
253 #ifdef vl_api_version_tuple
256 if 'version' in s['Option']:
257 v = s['Option']['version']
258 (major, minor, patch) = v.split('.')
259 output += "vl_api_version_tuple(%s, %s, %s, %s)\n" % \
260 (module, major, minor, patch)
262 output += "\n#endif /* vl_api_version_tuple */\n\n"
270 def run(input_filename, s, file_crc):
271 basename = os.path.basename(input_filename)
272 filename, file_extension = os.path.splitext(basename)
273 output = top_boilerplate.format(datestring=datestring,
274 input_filename=basename)
276 output += msg_names(s)
277 output += msg_name_crc_list(s, filename)
278 output += typedefs(s['types'] + s['Define'], filename + file_extension)
279 output += printfun(s['types'] + s['Define'])
280 output += endianfun(s['types'] + s['Define'])
281 output += version_tuple(s, basename)
282 output += bottom_boilerplate.format(input_filename=basename,