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 ******/
45 for t in s['defines']:
46 output += "vl_msg_id(VL_API_%s, vl_api_%s_t_handler)\n" % \
47 (t.name.upper(), t.name)
56 /****** Message names ******/
61 for t in s['defines']:
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
78 for t in s['defines']:
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(s, filename):
97 name = filename.replace('.', '_')
101 /****** Typedefs ******/
104 #ifndef included_{module}
105 #define included_{module}
107 output = output.format(module=name)
109 output += duplicate_wrapper_head(e.name)
110 output += "typedef enum {\n"
112 output += " %s = %s,\n" % (b[0], b[1])
113 output += '} vl_api_%s_t;\n' % e.name
114 output += duplicate_wrapper_tail()
116 for t in s['typedefs'] + s['defines']:
117 output += duplicate_wrapper_head(t.name)
118 output += "typedef VL_API_PACKED(struct _vl_api_%s {\n" % t.name
120 if b.type == 'Field':
121 output += " %s %s;\n" % (b.fieldtype, b.fieldname)
122 elif b.type == 'Array':
124 output += " %s %s[0];\n" % (b.fieldtype, b.fieldname)
126 output += " %s %s[%s];\n" % (b.fieldtype, b.fieldname,
129 raise ValueError("Error in processing array type %s" % b)
131 output += '}) vl_api_%s_t;\n' % t.name
132 output += duplicate_wrapper_tail()
135 output += "\n#endif\n\n"
140 format_strings = {'u8': '%u',
153 /****** Print functions *****/
157 #define _uword_fmt \"%lld\"
158 #define _uword_cast (long long)
160 #define _uword_fmt \"%ld\"
161 #define _uword_cast long
165 for t in s['typedefs'] + s['defines']:
167 output += "/***** manual: vl_api_%s_t_print *****/\n\n" % t.name
169 output += duplicate_wrapper_head(t.name + '_t_print')
170 output += "static inline void *vl_api_%s_t_print (vl_api_%s_t *a," % \
172 output += "void *handle)\n{\n"
173 output += " vl_print(handle, \"vl_api_%s_t:\\n\");\n" % t.name
176 if o.type != 'Field':
178 if o.fieldtype in format_strings:
179 output += " vl_print(handle, \"%s: %s\\n\", a->%s);\n" % \
180 (o.fieldname, format_strings[o.fieldtype],
183 output += ' return handle;\n'
185 output += duplicate_wrapper_tail()
187 output += "\n#endif /* vl_printfun */\n"
193 'u16': 'clib_net_to_host_u16',
194 'u32': 'clib_net_to_host_u32',
195 'u64': 'clib_net_to_host_u64',
196 'i16': 'clib_net_to_host_u16',
197 'i32': 'clib_net_to_host_u32',
198 'i64': 'clib_net_to_host_u64',
205 /****** Endian swap functions *****/\n\
208 #undef clib_net_to_host_uword
210 #define clib_net_to_host_uword clib_net_to_host_u64
212 #define clib_net_to_host_uword clib_net_to_host_u32
217 for t in s['typedefs'] + s['defines']:
219 output += "/***** manual: vl_api_%s_t_endian *****/\n\n" % t.name
221 output += duplicate_wrapper_head(t.name + '_t_endian')
222 output += "static inline void vl_api_%s_t_endian (vl_api_%s_t *a)" % \
227 if o.type != 'Field':
229 if o.fieldtype in endian_strings:
230 output += " a->%s = %s(a->%s);\n" % \
231 (o.fieldname, endian_strings[o.fieldtype], o.fieldname)
233 output += " /* a->%s = a->%s (no-op) */\n" % \
234 (o.fieldname, o.fieldname)
237 output += duplicate_wrapper_tail()
238 output += "\n#endif /* vl_endianfun */\n\n"
243 def version_tuple(s, module):
245 /****** Version tuple *****/
247 #ifdef vl_api_version_tuple
250 if 'version' in s['options']:
251 v = s['options']['version']
252 (major, minor, patch) = v.split('.')
253 output += "vl_api_version_tuple(%s, %s, %s, %s)\n" % \
254 (module, major, minor, patch)
256 output += "\n#endif /* vl_api_version_tuple */\n\n"
264 def run(input_filename, s, file_crc):
265 basename = os.path.basename(input_filename)
266 filename, file_extension = os.path.splitext(basename)
267 output = top_boilerplate.format(datestring=datestring,
268 input_filename=basename)
270 output += msg_names(s)
271 output += msg_name_crc_list(s, filename)
272 output += typedefs(s, filename + file_extension)
273 output += printfun(s)
274 output += endianfun(s)
275 output += version_tuple(s, basename)
276 output += bottom_boilerplate.format(input_filename=basename,