2 # Copyright (c) 2020 Cisco and/or its affiliates.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
17 # Provide two classes FromJSON and TOJSON that converts between JSON and VPP's
22 This module creates C code for core VPP, VPP plugins and client side VAT and
30 from io import StringIO
33 process_imports = False
36 ###############################################################################
38 '''Class to generate functions converting from VPP binary API to JSON.'''
40 noprint_fields = {'_vl_msg_id': None,
43 is_number = {'u8': None,
54 def __init__(self, module, types, defines, imported_types, stream):
57 self.defines = defines
59 self.types_hash = {'vl_api_'+d.name+'_t':
60 d for d in types + imported_types}
61 self.defines_hash = {d.name: d for d in defines}
64 '''Output the top boilerplate.'''
65 write = self.stream.write
66 write('#ifndef included_{}_api_tojson_h\n'.format(self.module))
67 write('#define included_{}_api_tojson_h\n'.format(self.module))
68 write('#include <vppinfra/cJSON.h>\n\n')
69 write('#include <vat2/jsonconvert.h>\n\n')
72 '''Output the bottom boilerplate.'''
73 write = self.stream.write
76 def get_json_func(self, t):
77 '''Given the type, returns the function to use to create a
81 vt = self.types_hash[t]
82 if vt.type == 'Using' and 'length' not in vt.alias:
83 vt_type = vt.alias['type']
87 if t in self.is_number or vt_type in self.is_number:
88 return 'cJSON_AddNumberToObject', '', False
90 return 'cJSON_AddBoolToObject', '', False
92 # Lookup type name check if it's enum
93 if vt.type == 'Enum' or vt.type == 'EnumFlag':
94 return '{t}_tojson'.format(t=t), '', True
95 return '{t}_tojson'.format(t=t), '&', True
97 def get_json_array_func(self, t):
98 '''Given a type returns the function to create a cJSON object
100 if t in self.is_number:
101 return 'cJSON_CreateNumber', ''
103 return 'cJSON_CreateBool', ''
104 return '{t}_tojson'.format(t=t), '&'
106 def print_string(self, o):
107 '''Create cJSON object from vl_api_string_t'''
108 write = self.stream.write
110 write(' vl_api_string_cJSON_AddToObject(o, "{n}", &a->{n});\n'
111 .format(n=o.fieldname))
114 write(' cJSON_AddStringToObject(o, "{n}", (char *)a->{n});\n'
115 .format(n=o.fieldname))
117 def print_field(self, o):
118 '''Called for every field in a typedef or define.'''
119 write = self.stream.write
120 if o.fieldname in self.noprint_fields:
123 f, p, newobj = self.get_json_func(o.fieldtype)
126 write(' cJSON_AddItemToObject(o, "{n}", {f}({p}a->{n}));\n'
127 .format(f=f, p=p, n=o.fieldname))
129 write(' {f}(o, "{n}", {p}a->{n});\n'
130 .format(f=f, p=p, n=o.fieldname))
132 _dispatch['Field'] = print_field
134 def print_array(self, o):
135 '''Converts a VPP API array to cJSON array.'''
136 write = self.stream.write
141 cJSON *array = cJSON_AddArrayToObject(o, "{n}");
142 for (i = 0; i < {lfield}; i++) {{
143 cJSON_AddItemToArray(array, {f}({p}a->{n}[i]));
148 if o.fieldtype == 'string':
152 lfield = 'a->' + o.lengthfield if o.lengthfield else o.length
153 if o.fieldtype == 'u8':
155 # What is length field doing here?
156 write(' u8 *s = format(0, "0x%U", format_hex_bytes, '
157 '&a->{n}, {lfield});\n'
158 .format(n=o.fieldname, lfield=lfield))
159 write(' cJSON_AddStringToObject(o, "{n}", (char *)s);\n'
160 .format(n=o.fieldname))
161 write(' vec_free(s);\n')
165 f, p = self.get_json_array_func(o.fieldtype)
166 write(forloop.format(lfield=lfield,
172 _dispatch['Array'] = print_array
174 def print_enum(self, o):
175 '''Create cJSON object (string) for VPP API enum'''
176 write = self.stream.write
177 write('static inline cJSON *vl_api_{name}_t_tojson '
178 '(vl_api_{name}_t a) {{\n'.format(name=o.name))
180 write(" switch(a) {\n")
182 write(" case %s:\n" % b[1])
183 write(' return cJSON_CreateString("{}");\n'.format(b[0]))
184 write(' default: return cJSON_CreateString("Invalid ENUM");\n')
186 write(' return 0;\n')
189 _dispatch['Enum'] = print_enum
190 _dispatch['EnumFlag'] = print_enum
192 def print_typedef(self, o):
193 '''Create cJSON (dictionary) object from VPP API typedef'''
194 write = self.stream.write
195 write('static inline cJSON *vl_api_{name}_t_tojson '
196 '(vl_api_{name}_t *a) {{\n'.format(name=o.name))
197 write(' cJSON *o = cJSON_CreateObject();\n')
200 self._dispatch[t.type](self, t)
202 write(' return o;\n')
205 def print_define(self, o):
206 '''Create cJSON (dictionary) object from VPP API define'''
207 write = self.stream.write
208 write('static inline cJSON *vl_api_{name}_t_tojson '
209 '(vl_api_{name}_t *a) {{\n'.format(name=o.name))
210 write(' cJSON *o = cJSON_CreateObject();\n')
211 write(' cJSON_AddStringToObject(o, "_msgname", "{}");\n'
215 self._dispatch[t.type](self, t)
217 write(' return o;\n')
220 def print_using(self, o):
221 '''Create cJSON (dictionary) object from VPP API aliased type'''
225 write = self.stream.write
226 write('static inline cJSON *vl_api_{name}_t_tojson '
227 '(vl_api_{name}_t *a) {{\n'.format(name=o.name))
229 write(' u8 *s = format(0, "%U", format_vl_api_{}_t, a);\n'
231 write(' cJSON *o = cJSON_CreateString((char *)s);\n')
232 write(' vec_free(s);\n')
233 write(' return o;\n')
236 _dispatch['Typedef'] = print_typedef
237 _dispatch['Define'] = print_define
238 _dispatch['Using'] = print_using
239 _dispatch['Union'] = print_typedef
241 def generate_function(self, t):
242 '''Main entry point'''
243 write = self.stream.write
245 write('/* Manual print {} */\n'.format(t.name))
247 self._dispatch[t.type](self, t)
249 def generate_types(self):
250 '''Main entry point'''
252 self.generate_function(t)
254 def generate_defines(self):
255 '''Main entry point'''
256 for t in self.defines:
257 self.generate_function(t)
262 Parse JSON objects into VPP API binary message structures.
265 noprint_fields = {'_vl_msg_id': None,
266 'client_index': None,
268 is_number = {'u8': None,
279 def __init__(self, module, types, defines, imported_types, stream):
282 self.defines = defines
284 self.types_hash = {'vl_api_'+d.name+'_t':
285 d for d in types + imported_types}
286 self.defines_hash = {d.name: d for d in defines}
289 '''Output the top boilerplate.'''
290 write = self.stream.write
291 write('#ifndef included_{}_api_fromjson_h\n'.format(self.module))
292 write('#define included_{}_api_fromjson_h\n'.format(self.module))
293 write('#include <vppinfra/cJSON.h>\n\n')
294 write('#include <vat2/jsonconvert.h>\n\n')
296 def is_base_type(self, t):
297 '''Check if a type is one of the VPP API base types'''
298 if t in self.is_number:
305 '''Output the bottom boilerplate.'''
306 write = self.stream.write
309 def print_string(self, o, toplevel=False):
310 '''Convert JSON string to vl_api_string_t'''
311 write = self.stream.write
313 msgvar = "a" if toplevel else "mp"
314 msgsize = "l" if toplevel else "*len"
317 write(' char *p = cJSON_GetStringValue(item);\n')
318 write(' size_t plen = strlen(p);\n')
319 write(' {msgvar} = realloc({msgvar}, {msgsize} + plen);\n'
320 .format(msgvar=msgvar, msgsize=msgsize))
321 write(' vl_api_c_string_to_api_string(p, (void *){msgvar} + '
322 '{msgsize} - sizeof(vl_api_string_t));\n'
323 .format(msgvar=msgvar, msgsize=msgsize))
324 write(' {msgsize} += plen;\n'.format(msgsize=msgsize))
326 write(' strncpy_s((char *)a->{n}, sizeof(a->{n}), '
327 'cJSON_GetStringValue(item), sizeof(a->{n}) - 1);\n'
328 .format(n=o.fieldname))
330 def print_field(self, o, toplevel=False):
331 '''Called for every field in a typedef or define.'''
332 write = self.stream.write
333 write(' // start field {}\n'.format(o.fieldname))
334 if o.fieldname in self.noprint_fields:
336 is_bt = self.is_base_type(o.fieldtype)
337 t = 'vl_api_{}'.format(o.fieldtype) if is_bt else o.fieldtype
339 msgvar = "a" if toplevel else "mp"
340 msgsize = "&l" if toplevel else "len"
343 write(' vl_api_{t}_fromjson(item, &a->{n});\n'
344 .format(t=o.fieldtype, n=o.fieldname))
346 write(' {msgvar} = {t}_fromjson({msgvar}, '
347 '{msgsize}, item, &a->{n});\n'
348 .format(t=t, n=o.fieldname, msgvar=msgvar, msgsize=msgsize))
349 write(' if (!{msgvar}) return 0;\n'.format(msgvar=msgvar))
351 write(' // end field {}\n'.format(o.fieldname))
353 _dispatch['Field'] = print_field
355 def print_array(self, o, toplevel=False):
356 '''Convert JSON array to VPP API array'''
357 write = self.stream.write
362 cJSON *array = cJSON_GetObjectItem(o, "{n}");
363 int size = cJSON_GetArraySize(array);
364 if (size != {lfield}) return 0;
365 for (i = 0; i < size; i++) {{
366 cJSON *e = cJSON_GetArrayItem(array, i);
374 cJSON *array = cJSON_GetObjectItem(o, "{n}");
375 int size = cJSON_GetArraySize(array);
377 {msgvar} = realloc({msgvar}, {msgsize} + sizeof({t}) * size);
378 {t} *d = (void *){msgvar} + {msgsize};
379 {msgsize} += sizeof({t}) * size;
380 for (i = 0; i < size; i++) {{
381 cJSON *e = cJSON_GetArrayItem(array, i);
387 if o.fieldtype == 'string':
388 self.print_string(o, toplevel)
391 lfield = 'a->' + o.lengthfield if o.lengthfield else o.length
392 msgvar = "a" if toplevel else "mp"
393 msgsize = "l" if toplevel else "*len"
395 if o.fieldtype == 'u8':
397 write(' s = u8string_fromjson(o, "{}");\n'
398 .format(o.fieldname))
399 write(' if (!s) return 0;\n')
400 write(' {} = vec_len(s);\n'.format(lfield))
402 write(' {msgvar} = realloc({msgvar}, {msgsize} + '
403 'vec_len(s));\n'.format(msgvar=msgvar, msgsize=msgsize))
404 write(' memcpy((void *){msgvar} + {msgsize}, s, '
405 'vec_len(s));\n'.format(msgvar=msgvar, msgsize=msgsize))
406 write(' {msgsize} += vec_len(s);\n'.format(msgsize=msgsize))
408 write(' vec_free(s);\n')
410 write(' u8string_fromjson2(o, "{n}", a->{n});\n'
411 .format(n=o.fieldname))
414 is_bt = self.is_base_type(o.fieldtype)
418 call = ('vl_api_{t}_fromjson(e, &d[i]);'
419 .format(t=o.fieldtype))
421 call = ('{t}_fromjson({msgvar}, len, e, &d[i]); '
422 .format(t=o.fieldtype, msgvar=msgvar))
423 write(forloop_vla.format(lfield=lfield,
431 call = ('vl_api_{t}_fromjson(e, &a->{n}[i]);'
432 .format(t=t, n=o.fieldname))
434 call = ('a = {}_fromjson({}, len, e, &a->{}[i]);'
435 .format(t, msgvar, o.fieldname))
436 write(forloop.format(lfield=lfield,
443 _dispatch['Array'] = print_array
445 def print_enum(self, o):
446 '''Convert to JSON enum(string) to VPP API enum (int)'''
447 write = self.stream.write
448 write('static inline void *vl_api_{n}_t_fromjson '
449 '(void *mp, int *len, cJSON *o, vl_api_{n}_t *a) {{\n'
451 write(' char *p = cJSON_GetStringValue(o);\n')
453 write(' if (strcmp(p, "{}") == 0) {{*a = {}; return mp;}}\n'
455 write(' return 0;\n')
458 _dispatch['Enum'] = print_enum
459 _dispatch['EnumFlag'] = print_enum
461 def print_typedef(self, o):
462 '''Convert from JSON object to VPP API binary representation'''
463 write = self.stream.write
465 write('static inline void *vl_api_{name}_t_fromjson (void *mp, '
466 'int *len, cJSON *o, vl_api_{name}_t *a) {{\n'
467 .format(name=o.name))
468 write(' cJSON *item __attribute__ ((unused));\n')
469 write(' u8 *s __attribute__ ((unused));\n')
471 if t.type == 'Field' and t.is_lengthfield:
473 write(' item = cJSON_GetObjectItem(o, "{}");\n'
474 .format(t.fieldname))
475 write(' if (!item) return 0;\n')
477 self._dispatch[t.type](self, t)
479 write(' return mp;\n')
482 def print_union(self, o):
483 '''Convert JSON object to VPP API binary union'''
484 write = self.stream.write
486 write('static inline void *vl_api_{name}_t_fromjson (void *mp, '
487 'int *len, cJSON *o, vl_api_{name}_t *a) {{\n'
488 .format(name=o.name))
489 write(' cJSON *item __attribute__ ((unused));\n')
490 write(' u8 *s __attribute__ ((unused));\n')
492 if t.type == 'Field' and t.is_lengthfield:
494 write(' item = cJSON_GetObjectItem(o, "{}");\n'
495 .format(t.fieldname))
496 write(' if (item) {\n')
497 self._dispatch[t.type](self, t)
499 write(' return mp;\n')
502 def print_define(self, o):
503 '''Convert JSON object to VPP API message'''
504 write = self.stream.write
505 write('static inline vl_api_{name}_t *vl_api_{name}_t_fromjson '
506 '(cJSON *o, int *len) {{\n'.format(name=o.name))
507 write(' cJSON *item __attribute__ ((unused));\n')
508 write(' u8 *s __attribute__ ((unused));\n')
509 write(' int l = sizeof(vl_api_{}_t);\n'.format(o.name))
510 write(' vl_api_{}_t *a = malloc(l);\n'.format(o.name))
513 if t.fieldname in self.noprint_fields:
515 if t.type == 'Field' and t.is_lengthfield:
517 write(' // processing {}: {} {}\n'
518 .format(o.name, t.fieldtype, t.fieldname))
520 write(' item = cJSON_GetObjectItem(o, "{}");\n'
521 .format(t.fieldname))
522 write(' if (!item) return 0;\n')
523 self._dispatch[t.type](self, t, toplevel=True)
527 write(' *len = l;\n')
528 write(' return a;\n')
531 def print_using(self, o):
532 '''Convert JSON field to VPP type alias'''
533 write = self.stream.write
539 write('static inline void *vl_api_{name}_t_fromjson (void *mp, '
540 'int *len, cJSON *o, vl_api_{name}_t *a) {{\n'
541 .format(name=o.name))
542 if 'length' in o.alias:
543 if t.fieldtype != 'u8':
544 raise ValueError("Error in processing type {} for {}"
545 .format(t.fieldtype, o.name))
546 write(' vl_api_u8_string_fromjson(o, (u8 *)a, {});\n'
547 .format(o.alias['length']))
549 write(' vl_api_{t}_fromjson(o, ({t} *)a);\n'
550 .format(t=t.fieldtype))
552 write(' return mp;\n')
555 _dispatch['Typedef'] = print_typedef
556 _dispatch['Define'] = print_define
557 _dispatch['Using'] = print_using
558 _dispatch['Union'] = print_union
560 def generate_function(self, t):
561 '''Main entry point'''
562 write = self.stream.write
564 write('/* Manual print {} */\n'.format(t.name))
566 self._dispatch[t.type](self, t)
568 def generate_types(self):
569 '''Main entry point'''
571 self.generate_function(t)
573 def generate_defines(self):
574 '''Main entry point'''
575 for t in self.defines:
576 self.generate_function(t)
579 def generate_tojson(s, modulename, stream):
580 '''Generate all functions to convert from API to JSON'''
583 write('/* Imported API files */\n')
584 for i in s['Import']:
585 f = i.filename.replace('plugins/', '')
586 write('#include <{}_tojson.h>\n'.format(f))
588 pp = ToJSON(modulename, s['types'], s['Define'], s['imported']['types'],
592 pp.generate_defines()
597 def generate_fromjson(s, modulename, stream):
598 '''Generate all functions to convert from JSON to API'''
600 write('/* Imported API files */\n')
601 for i in s['Import']:
602 f = i.filename.replace('plugins/', '')
603 write('#include <{}_fromjson.h>\n'.format(f))
605 pp = FromJSON(modulename, s['types'], s['Define'], s['imported']['types'],
609 pp.generate_defines()
614 ###############################################################################
617 DATESTRING = datetime.datetime.utcfromtimestamp(
618 int(os.environ.get('SOURCE_DATE_EPOCH', time.time())))
619 TOP_BOILERPLATE = '''\
621 * VLIB API definitions {datestring}
622 * Input file: {input_filename}
623 * Automatically generated: please edit the input file NOT this file!
627 #if defined(vl_msg_id)||defined(vl_union_id) \\
628 || defined(vl_printfun) ||defined(vl_endianfun) \\
629 || defined(vl_api_version)||defined(vl_typedefs) \\
630 || defined(vl_msg_name)||defined(vl_msg_name_crc_list) \\
631 || defined(vl_api_version_tuple)
632 /* ok, something was selected */
634 #warning no content included from {input_filename}
637 #define VL_API_PACKED(x) x __attribute__ ((packed))
640 BOTTOM_BOILERPLATE = '''\
641 /****** API CRC (whole file) *****/
643 #ifdef vl_api_version
644 vl_api_version({input_filename}, {file_crc:#08x})
651 '''Generate macro to map API message id to handler'''
654 /****** Message ID / handler enum ******/
659 for t in s['Define']:
660 output += "vl_msg_id(VL_API_%s, vl_api_%s_t_handler)\n" % \
661 (t.name.upper(), t.name)
668 '''Generate calls to name mapping macro'''
671 /****** Message names ******/
676 for t in s['Define']:
677 dont_trace = 0 if t.dont_trace else 1
678 output += "vl_msg_name(vl_api_%s_t, %d)\n" % (t.name, dont_trace)
684 def msg_name_crc_list(s, suffix):
685 '''Generate list of names to CRC mappings'''
688 /****** Message name, crc list ******/
690 #ifdef vl_msg_name_crc_list
692 output += "#define foreach_vl_msg_name_crc_%s " % suffix
694 for t in s['Define']:
695 output += "\\\n_(VL_API_%s, %s, %08x) " % \
696 (t.name.upper(), t.name, t.crc)
702 def api2c(fieldtype):
703 '''Map between API type names and internal VPP type names'''
704 mappingtable = {'string': 'vl_api_string_t', }
705 if fieldtype in mappingtable:
706 return mappingtable[fieldtype]
710 def typedefs(filename):
711 '''Include in the main files to the types file'''
714 /****** Typedefs ******/
717 #include "{include}.api_types.h"
719 '''.format(include=filename)
723 FORMAT_STRINGS = {'u8': '%u',
736 '''Functions for pretty printing VPP API messages'''
738 noprint_fields = {'_vl_msg_id': None,
739 'client_index': None,
742 def __init__(self, stream):
746 def print_string(o, stream):
747 '''Pretty print a vl_api_string_t'''
750 write(' if (vl_api_string_len(&a->{f}) > 0) {{\n'
751 .format(f=o.fieldname))
752 write(' s = format(s, "\\n%U{f}: %U", '
753 'format_white_space, indent, '
754 'vl_api_format_string, (&a->{f}));\n'.format(f=o.fieldname))
756 write(' s = format(s, "\\n%U{f}:", '
757 'format_white_space, indent);\n'.format(f=o.fieldname))
760 write(' s = format(s, "\\n%U{f}: %s", '
761 'format_white_space, indent, a->{f});\n'
762 .format(f=o.fieldname))
764 def print_field(self, o, stream):
765 '''Pretty print API field'''
767 if o.fieldname in self.noprint_fields:
769 if o.fieldtype in FORMAT_STRINGS:
770 f = FORMAT_STRINGS[o.fieldtype]
771 write(' s = format(s, "\\n%U{n}: {f}", '
772 'format_white_space, indent, a->{n});\n'
773 .format(n=o.fieldname, f=f))
775 write(' s = format(s, "\\n%U{n}: %U", '
776 'format_white_space, indent, '
777 'format_{t}, &a->{n}, indent);\n'
778 .format(n=o.fieldname, t=o.fieldtype))
780 _dispatch['Field'] = print_field
782 def print_array(self, o, stream):
783 '''Pretty print API array'''
787 for (i = 0; i < {lfield}; i++) {{
788 s = format(s, "\\n%U{n}: %U",
789 format_white_space, indent, format_{t}, &a->{n}[i], indent);
793 forloop_format = '''\
794 for (i = 0; i < {lfield}; i++) {{
795 s = format(s, "\\n%U{n}: {t}",
796 format_white_space, indent, a->{n}[i]);
800 if o.fieldtype == 'string':
801 self.print_string(o, stream)
804 if o.fieldtype == 'u8':
806 write(' s = format(s, "\\n%U{n}: %U", format_white_space, '
807 'indent, format_hex_bytes, a->{n}, a->{lfield});\n'
808 .format(n=o.fieldname, lfield=o.lengthfield))
810 write(' s = format(s, "\\n%U{n}: %U", format_white_space, '
811 'indent, format_hex_bytes, a, {lfield});\n'
812 .format(n=o.fieldname, lfield=o.length))
815 lfield = 'a->' + o.lengthfield if o.lengthfield else o.length
816 if o.fieldtype in FORMAT_STRINGS:
817 write(forloop_format.format(lfield=lfield,
818 t=FORMAT_STRINGS[o.fieldtype],
821 write(forloop.format(lfield=lfield, t=o.fieldtype, n=o.fieldname))
823 _dispatch['Array'] = print_array
826 def print_alias(k, v, stream):
827 '''Pretty print type alias'''
829 if ('length' in v.alias and v.alias['length'] and
830 v.alias['type'] == 'u8'):
831 write(' return format(s, "%U", format_hex_bytes, a, {});\n'
832 .format(v.alias['length']))
833 elif v.alias['type'] in FORMAT_STRINGS:
834 write(' return format(s, "{}", *a);\n'
835 .format(FORMAT_STRINGS[v.alias['type']]))
837 write(' return format(s, "{} (print not implemented)");\n'
841 def print_enum(o, stream):
842 '''Pretty print API enum'''
844 write(" switch(*a) {\n")
846 write(" case %s:\n" % b[1])
847 write(' return format(s, "{}");\n'.format(b[0]))
850 _dispatch['Enum'] = print_enum
851 _dispatch['EnumFlag'] = print_enum
853 def print_obj(self, o, stream):
857 if o.type in self._dispatch:
858 self._dispatch[o.type](self, o, stream)
860 write(' s = format(s, "\\n{} {} {} (print not implemented");\n'
861 .format(o.type, o.fieldtype, o.fieldname))
864 def printfun(objs, stream, modulename):
865 '''Main entry point for pretty print function generation'''
869 /****** Print functions *****/
871 #ifndef included_{module}_printfun
872 #define included_{module}_printfun
875 #define _uword_fmt \"%lld\"
876 #define _uword_cast (long long)
878 #define _uword_fmt \"%ld\"
879 #define _uword_cast long
885 static inline void *vl_api_{name}_t_print (vl_api_{name}_t *a, void *handle)
888 u32 indent __attribute__((unused)) = 2;
889 int i __attribute__((unused));
892 h = h.format(module=modulename)
895 pp = Printfun(stream)
898 write("/***** manual: vl_api_%s_t_print *****/\n\n" % t.name)
900 write(signature.format(name=t.name))
901 write(' /* Message definition: vl_api_{}_t: */\n'.format(t.name))
902 write(" s = format(s, \"vl_api_%s_t:\");\n" % t.name)
904 pp.print_obj(o, stream)
905 write(' vec_add1(s, 0);\n')
906 write(' vl_print (handle, (char *)s);\n')
907 write(' vec_free (s);\n')
908 write(' return handle;\n')
912 write("\n#endif /* vl_printfun */\n")
917 def printfun_types(objs, stream, modulename):
918 '''Pretty print API types'''
920 pp = Printfun(stream)
923 /****** Print functions *****/
925 #ifndef included_{module}_printfun_types
926 #define included_{module}_printfun_types
929 h = h.format(module=modulename)
933 static inline u8 *format_vl_api_{name}_t (u8 *s, va_list * args)
935 vl_api_{name}_t *a = va_arg (*args, vl_api_{name}_t *);
936 u32 indent __attribute__((unused)) = va_arg (*args, u32);
937 int i __attribute__((unused));
942 if t.__class__.__name__ == 'Enum' or t.__class__.__name__ == 'EnumFlag':
943 write(signature.format(name=t.name))
944 pp.print_enum(t.block, stream)
945 write(' return s;\n')
950 write("/***** manual: vl_api_%s_t_print *****/\n\n" % t.name)
953 if t.__class__.__name__ == 'Using':
954 write(signature.format(name=t.name))
955 pp.print_alias(t.name, t, stream)
959 write(signature.format(name=t.name))
961 pp.print_obj(o, stream)
963 write(' return s;\n')
967 write("\n#endif /* vl_printfun_types */\n")
970 def generate_imports(imports):
971 '''Add #include matching the API import statements'''
972 output = '/* Imported API files */\n'
973 output += '#ifndef vl_api_version\n'
976 s = i.filename.replace('plugins/', '')
977 output += '#include <{}.h>\n'.format(s)
983 'u16': 'clib_net_to_host_u16',
984 'u32': 'clib_net_to_host_u32',
985 'u64': 'clib_net_to_host_u64',
986 'i16': 'clib_net_to_host_i16',
987 'i32': 'clib_net_to_host_i32',
988 'i64': 'clib_net_to_host_i64',
989 'f64': 'clib_net_to_host_f64',
993 def endianfun_array(o):
994 '''Generate endian functions for arrays'''
996 for (i = 0; i < {length}; i++) {{
997 a->{name}[i] = {format}(a->{name}[i]);
1001 forloop_format = '''\
1002 for (i = 0; i < {length}; i++) {{
1003 {type}_endian(&a->{name}[i]);
1008 if o.fieldtype == 'u8' or o.fieldtype == 'string':
1009 output += ' /* a->{n} = a->{n} (no-op) */\n'.format(n=o.fieldname)
1011 lfield = 'a->' + o.lengthfield if o.lengthfield else o.length
1012 if o.fieldtype in ENDIAN_STRINGS:
1014 .format(length=lfield,
1015 format=ENDIAN_STRINGS[o.fieldtype],
1018 output += (forloop_format
1019 .format(length=lfield, type=o.fieldtype,
1024 NO_ENDIAN_CONVERSION = {'client_index': None}
1027 def endianfun_obj(o):
1028 '''Generate endian conversion function for type'''
1030 if o.type == 'Array':
1031 return endianfun_array(o)
1032 if o.type != 'Field':
1033 output += (' s = format(s, "\\n{} {} {} (print not implemented");\n'
1034 .format(o.type, o.fieldtype, o.fieldname))
1036 if o.fieldname in NO_ENDIAN_CONVERSION:
1037 output += ' /* a->{n} = a->{n} (no-op) */\n'.format(n=o.fieldname)
1039 if o.fieldtype in ENDIAN_STRINGS:
1040 output += (' a->{name} = {format}(a->{name});\n'
1041 .format(name=o.fieldname,
1042 format=ENDIAN_STRINGS[o.fieldtype]))
1043 elif o.fieldtype.startswith('vl_api_'):
1044 output += (' {type}_endian(&a->{name});\n'
1045 .format(type=o.fieldtype, name=o.fieldname))
1047 output += ' /* a->{n} = a->{n} (no-op) */\n'.format(n=o.fieldname)
1052 def endianfun(objs, modulename):
1053 '''Main entry point for endian function generation'''
1056 /****** Endian swap functions *****/\n\
1058 #ifndef included_{module}_endianfun
1059 #define included_{module}_endianfun
1061 #undef clib_net_to_host_uword
1063 #define clib_net_to_host_uword clib_net_to_host_u64
1065 #define clib_net_to_host_uword clib_net_to_host_u32
1069 output = output.format(module=modulename)
1072 static inline void vl_api_{name}_t_endian (vl_api_{name}_t *a)
1074 int i __attribute__((unused));
1078 if t.__class__.__name__ == 'Enum' or t.__class__.__name__ == 'EnumFlag' :
1079 output += signature.format(name=t.name)
1080 if t.enumtype in ENDIAN_STRINGS:
1081 output += (' *a = {}(*a);\n'
1082 .format(ENDIAN_STRINGS[t.enumtype]))
1084 output += (' /* a->{name} = a->{name} (no-op) */\n'
1085 .format(name=t.name))
1091 output += "/***** manual: vl_api_%s_t_endian *****/\n\n" % t.name
1094 if t.__class__.__name__ == 'Using':
1095 output += signature.format(name=t.name)
1096 if ('length' in t.alias and t.alias['length'] and
1097 t.alias['type'] == 'u8'):
1098 output += (' /* a->{name} = a->{name} (no-op) */\n'
1099 .format(name=t.name))
1100 elif t.alias['type'] in FORMAT_STRINGS:
1101 output += (' *a = {}(*a);\n'
1102 .format(ENDIAN_STRINGS[t.alias['type']]))
1104 output += ' /* Not Implemented yet {} */'.format(t.name)
1108 output += signature.format(name=t.name)
1111 output += endianfun_obj(o)
1114 output += "\n#endif"
1115 output += "\n#endif /* vl_endianfun */\n\n"
1120 def version_tuple(s, module):
1121 '''Generate semantic version string'''
1123 /****** Version tuple *****/
1125 #ifdef vl_api_version_tuple
1128 if 'version' in s['Option']:
1129 v = s['Option']['version']
1130 (major, minor, patch) = v.split('.')
1131 output += "vl_api_version_tuple(%s, %s, %s, %s)\n" % \
1132 (module, major, minor, patch)
1134 output += "\n#endif /* vl_api_version_tuple */\n\n"
1139 def generate_include_enum(s, module, stream):
1140 '''Generate <name>.api_enum.h'''
1141 write = stream.write
1144 write('typedef enum {\n')
1145 for t in s['Define']:
1146 write(' VL_API_{},\n'.format(t.name.upper()))
1147 write(' VL_MSG_{}_LAST\n'.format(module.upper()))
1148 write('}} vl_api_{}_enum_t;\n'.format(module))
1151 def generate_include_counters(s, stream):
1152 '''Include file for the counter data model types.'''
1153 write = stream.write
1156 csetname = counters.name
1157 write('typedef enum {\n')
1158 for c in counters.block:
1159 write(' {}_ERROR_{},\n'
1160 .format(csetname.upper(), c['name'].upper()))
1161 write(' {}_N_ERROR\n'.format(csetname.upper()))
1162 write('}} vl_counter_{}_enum_t;\n'.format(csetname))
1164 write('extern vl_counter_t {}_error_counters[];\n'.format(csetname))
1167 def generate_include_types(s, module, stream):
1168 '''Generate separate API _types file.'''
1169 write = stream.write
1171 write('#ifndef included_{module}_api_types_h\n'.format(module=module))
1172 write('#define included_{module}_api_types_h\n'.format(module=module))
1174 if 'version' in s['Option']:
1175 v = s['Option']['version']
1176 (major, minor, patch) = v.split('.')
1177 write('#define VL_API_{m}_API_VERSION_MAJOR {v}\n'
1178 .format(m=module.upper(), v=major))
1179 write('#define VL_API_{m}_API_VERSION_MINOR {v}\n'
1180 .format(m=module.upper(), v=minor))
1181 write('#define VL_API_{m}_API_VERSION_PATCH {v}\n'
1182 .format(m=module.upper(), v=patch))
1185 write('/* Imported API files */\n')
1186 for i in s['Import']:
1187 filename = i.filename.replace('plugins/', '')
1188 write('#include <{}_types.h>\n'.format(filename))
1190 for o in s['types'] + s['Define']:
1191 tname = o.__class__.__name__
1192 if tname == 'Using':
1193 if 'length' in o.alias:
1194 write('typedef %s vl_api_%s_t[%s];\n' %
1195 (o.alias['type'], o.name, o.alias['length']))
1197 write('typedef %s vl_api_%s_t;\n' % (o.alias['type'], o.name))
1198 elif tname == 'Enum' or tname == 'EnumFlag':
1199 if o.enumtype == 'u32':
1200 write("typedef enum {\n")
1202 write("typedef enum __attribute__((packed)) {\n")
1205 write(" %s = %s,\n" % (b[0], b[1]))
1206 write('} vl_api_%s_t;\n' % o.name)
1207 if o.enumtype != 'u32':
1208 size1 = 'sizeof(vl_api_%s_t)' % o.name
1209 size2 = 'sizeof(%s)' % o.enumtype
1210 err_str = 'size of API enum %s is wrong' % o.name
1211 write('STATIC_ASSERT(%s == %s, "%s");\n'
1212 % (size1, size2, err_str))
1214 if tname == 'Union':
1215 write("typedef union __attribute__ ((packed)) _vl_api_%s {\n"
1218 write(("typedef struct __attribute__ ((packed)) _vl_api_%s {\n")
1221 if b.type == 'Option':
1223 if b.type == 'Field':
1224 write(" %s %s;\n" % (api2c(b.fieldtype),
1226 elif b.type == 'Array':
1228 write(" %s %s[0];\n" % (api2c(b.fieldtype),
1231 # Fixed length strings decay to nul terminated u8
1232 if b.fieldtype == 'string':
1235 .format(api2c(b.fieldtype),
1238 write(' u8 {}[{}];\n'
1239 .format(b.fieldname, b.length))
1241 write(" %s %s[%s];\n" %
1242 (api2c(b.fieldtype), b.fieldname,
1245 raise ValueError("Error in processing type {} for {}"
1248 write('} vl_api_%s_t;\n' % o.name)
1250 for t in s['Define']:
1251 write('#define VL_API_{ID}_CRC "{n}_{crc:08x}"\n'
1252 .format(n=t.name, ID=t.name.upper(), crc=t.crc))
1257 def generate_c_boilerplate(services, defines, counters, file_crc,
1259 '''VPP side plugin.'''
1260 write = stream.write
1261 define_hash = {d.name: d for d in defines}
1264 #define vl_endianfun /* define message structures */
1265 #include "{module}.api.h"
1268 /* instantiate all the print functions we know about */
1269 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
1271 #include "{module}.api.h"
1276 write(hdr.format(module=module))
1277 write('static u16\n')
1278 write('setup_message_id_table (void) {\n')
1279 write(' api_main_t *am = my_api_main;\n')
1280 write(' vl_msg_api_msg_config_t c;\n')
1281 write(' u16 msg_id_base = vl_msg_api_get_msg_ids ("{}_{crc:08x}", '
1282 'VL_MSG_{m}_LAST);\n'
1283 .format(module, crc=file_crc, m=module.upper()))
1286 write(' vl_msg_api_add_msg_name_crc (am, "{n}_{crc:08x}",\n'
1287 ' VL_API_{ID} + msg_id_base);\n'
1288 .format(n=d.name, ID=d.name.upper(), crc=d.crc))
1290 d = define_hash[s.caller]
1291 write(' c = (vl_msg_api_msg_config_t) '
1292 ' {{.id = VL_API_{ID} + msg_id_base,\n'
1294 ' .handler = vl_api_{n}_t_handler,\n'
1295 ' .cleanup = vl_noop_handler,\n'
1296 ' .endian = vl_api_{n}_t_endian,\n'
1297 ' .print = vl_api_{n}_t_print,\n'
1298 ' .is_autoendian = 0}};\n'
1299 .format(n=s.caller, ID=s.caller.upper()))
1300 write(' vl_msg_api_config (&c);\n')
1302 d = define_hash[s.reply]
1303 write(' c = (vl_msg_api_msg_config_t) '
1304 '{{.id = VL_API_{ID} + msg_id_base,\n'
1307 ' .cleanup = vl_noop_handler,\n'
1308 ' .endian = vl_api_{n}_t_endian,\n'
1309 ' .print = vl_api_{n}_t_print,\n'
1310 ' .is_autoendian = 0}};\n'
1311 .format(n=s.reply, ID=s.reply.upper()))
1312 write(' vl_msg_api_config (&c);\n')
1316 write(' return msg_id_base;\n')
1319 severity = {'error': 'VL_COUNTER_SEVERITY_ERROR',
1320 'info': 'VL_COUNTER_SEVERITY_INFO',
1321 'warn': 'VL_COUNTER_SEVERITY_WARN'}
1323 for cnt in counters:
1325 write('vl_counter_t {}_error_counters[] = {{\n'.format(csetname))
1328 write(' .name = "{}",\n'.format(c['name']))
1329 write(' .desc = "{}",\n'.format(c['description']))
1330 write(' .severity = {},\n'.format(severity[c['severity']]))
1335 def generate_c_test_boilerplate(services, defines, file_crc, module, plugin,
1337 '''Generate code for legacy style VAT. To be deleted.'''
1338 write = stream.write
1340 define_hash = {d.name: d for d in defines}
1343 #define vl_endianfun /* define message structures */
1344 #include "{module}.api.h"
1347 /* instantiate all the print functions we know about */
1348 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
1350 #include "{module}.api.h"
1355 write(hdr.format(module=module))
1358 d = define_hash[s.reply]
1363 ' * Manual definition requested for: \n'
1364 ' * vl_api_{n}_t_handler()\n'
1368 if not define_hash[s.caller].autoreply:
1369 write('/* Generation not supported (vl_api_{n}_t_handler()) */\n'
1372 write('#ifndef VL_API_{n}_T_HANDLER\n'.format(n=s.reply.upper()))
1373 write('static void\n')
1374 write('vl_api_{n}_t_handler (vl_api_{n}_t * mp) {{\n'
1376 write(' vat_main_t * vam = {}_test_main.vat_main;\n'.format(module))
1377 write(' i32 retval = ntohl(mp->retval);\n')
1378 write(' if (vam->async_mode) {\n')
1379 write(' vam->async_errors += (retval < 0);\n')
1380 write(' } else {\n')
1381 write(' vam->retval = retval;\n')
1382 write(' vam->result_ready = 1;\n')
1388 if define_hash[e].manual_print:
1390 write('static void\n')
1391 write('vl_api_{n}_t_handler (vl_api_{n}_t * mp) {{\n'.format(n=e))
1392 write(' vl_print(0, "{n} event called:");\n'.format(n=e))
1393 write(' vl_api_{n}_t_print(mp, 0);\n'.format(n=e))
1396 write('static void\n')
1397 write('setup_message_id_table (vat_main_t * vam, u16 msg_id_base) {\n')
1399 write(' vl_msg_api_set_handlers(VL_API_{ID} + msg_id_base, '
1401 ' vl_api_{n}_t_handler, '
1402 ' vl_noop_handler,\n'
1403 ' vl_api_{n}_t_endian, '
1404 ' vl_api_{n}_t_print,\n'
1405 ' sizeof(vl_api_{n}_t), 1);\n'
1406 .format(n=s.reply, ID=s.reply.upper()))
1407 write(' hash_set_mem (vam->function_by_name, "{n}", api_{n});\n'
1408 .format(n=s.caller))
1410 write(' hash_set_mem (vam->help_by_name, "{n}", "{help}");\n'
1412 help=define_hash[s.caller].options['vat_help']))
1418 write(' vl_msg_api_set_handlers(VL_API_{ID} + msg_id_base, '
1420 ' vl_api_{n}_t_handler, '
1421 ' vl_noop_handler,\n'
1422 ' vl_api_{n}_t_endian, '
1423 ' vl_api_{n}_t_print,\n'
1424 ' sizeof(vl_api_{n}_t), 1);\n'
1425 .format(n=e, ID=e.upper()))
1429 write('clib_error_t * vat_plugin_register (vat_main_t *vam)\n')
1431 write('clib_error_t * vat_{}_plugin_register (vat_main_t *vam)\n'
1434 write(' {n}_test_main_t * mainp = &{n}_test_main;\n'.format(n=module))
1435 write(' mainp->vat_main = vam;\n')
1436 write(' mainp->msg_id_base = vl_client_get_first_plugin_msg_id '
1437 ' ("{n}_{crc:08x}");\n'
1438 .format(n=module, crc=file_crc))
1439 write(' if (mainp->msg_id_base == (u16) ~0)\n')
1440 write(' return clib_error_return (0, "{} plugin not loaded...");\n'
1442 write(' setup_message_id_table (vam, mainp->msg_id_base);\n')
1443 write('#ifdef VL_API_LOCAL_SETUP_MESSAGE_ID_TABLE\n')
1444 write(' VL_API_LOCAL_SETUP_MESSAGE_ID_TABLE(vam);\n')
1446 write(' return 0;\n')
1451 '''Check if a method is generated already.'''
1452 def _f(module, d, processed, *args):
1453 if d.name in processed:
1455 processed[d.name] = True
1456 return func(module, d, *args)
1460 def c_test_api_service(s, dump, stream):
1461 '''Generate JSON code for a service.'''
1462 write = stream.write
1464 req_reply_template = '''\
1471 mp = vl_api_{n}_t_fromjson(o, &len);
1473 fprintf(stderr, "Failed converting JSON to API\\n");
1477 mp->_vl_msg_id = vac_get_msg_index(VL_API_{N}_CRC);
1478 vl_api_{n}_t_endian(mp);
1479 vac_write((char *)mp, len);
1485 vac_read(&p, &l, 5); // XXX: Fix timeout
1486 // XXX Will fail in case of event received. Do loop
1487 if (ntohs(*((u16 *)p)) != vac_get_msg_index(VL_API_{R}_CRC)) {{
1488 fprintf(stderr, "Mismatched reply\\n");
1491 vl_api_{r}_t *rmp = (vl_api_{r}_t *)p;
1492 vl_api_{r}_t_endian(rmp);
1493 return vl_api_{r}_t_tojson(rmp);
1497 dump_details_template = '''\
1501 u16 msg_id = vac_get_msg_index(VL_API_{N}_CRC);
1504 vl_api_{n}_t *mp = vl_api_{n}_t_fromjson(o, &len);
1506 fprintf(stderr, "Failed converting JSON to API\\n");
1509 mp->_vl_msg_id = msg_id;
1510 vl_api_{n}_t_endian(mp);
1511 vac_write((char *)mp, len);
1514 vat2_control_ping(123); // FIX CONTEXT
1515 cJSON *reply = cJSON_CreateArray();
1517 u16 ping_reply_msg_id = vac_get_msg_index(VL_API_CONTROL_PING_REPLY_CRC);
1518 u16 details_msg_id = vac_get_msg_index(VL_API_{R}_CRC);
1524 vac_read(&p, &l, 5); // XXX: Fix timeout
1526 /* Message can be one of [_details, control_ping_reply
1527 * or unrelated event]
1529 u16 reply_msg_id = ntohs(*((u16 *)p));
1530 if (reply_msg_id == ping_reply_msg_id) {{
1534 if (reply_msg_id == details_msg_id) {{
1535 vl_api_{r}_t *rmp = (vl_api_{r}_t *)p;
1536 vl_api_{r}_t_endian(rmp);
1537 cJSON_AddItemToArray(reply, vl_api_{r}_t_tojson(rmp));
1544 gets_details_reply_template = '''\
1548 u16 msg_id = vac_get_msg_index(VL_API_{N}_CRC);
1551 vl_api_{n}_t *mp = vl_api_{n}_t_fromjson(o, &len);
1553 fprintf(stderr, "Failed converting JSON to API\\n");
1556 mp->_vl_msg_id = msg_id;
1558 vl_api_{n}_t_endian(mp);
1559 vac_write((char *)mp, len);
1562 cJSON *reply = cJSON_CreateArray();
1564 u16 reply_msg_id = vac_get_msg_index(VL_API_{R}_CRC);
1565 u16 details_msg_id = vac_get_msg_index(VL_API_{D}_CRC);
1571 vac_read(&p, &l, 5); // XXX: Fix timeout
1573 /* Message can be one of [_details, control_ping_reply
1574 * or unrelated event]
1576 u16 msg_id = ntohs(*((u16 *)p));
1577 if (msg_id == reply_msg_id) {{
1578 vl_api_{r}_t *rmp = (vl_api_{r}_t *)p;
1579 vl_api_{r}_t_endian(rmp);
1580 cJSON_AddItemToArray(reply, vl_api_{r}_t_tojson(rmp));
1584 if (msg_id == details_msg_id) {{
1585 vl_api_{d}_t *rmp = (vl_api_{d}_t *)p;
1586 vl_api_{d}_t_endian(rmp);
1587 cJSON_AddItemToArray(reply, vl_api_{d}_t_tojson(rmp));
1596 if s.stream_message:
1597 write(gets_details_reply_template
1598 .format(n=s.caller, r=s.reply, N=s.caller.upper(),
1599 R=s.reply.upper(), d=s.stream_message,
1600 D=s.stream_message.upper()))
1602 write(dump_details_template.format(n=s.caller, r=s.reply,
1606 write(req_reply_template.format(n=s.caller, r=s.reply,
1611 def generate_c_test2_boilerplate(services, defines, module, stream):
1612 '''Generate code for VAT2 plugin.'''
1613 write = stream.write
1615 define_hash = {d.name: d for d in defines}
1619 #include <vlibapi/api.h>
1620 #include <vlibmemory/api.h>
1621 #include <vppinfra/error.h>
1622 #include <vnet/ip/ip_format_fns.h>
1623 #include <vnet/ethernet/ethernet_format_fns.h>
1625 #define vl_typedefs /* define message structures */
1626 #include <vpp/api/vpe_all_api_h.h>
1629 #include "{module}.api_enum.h"
1630 #include "{module}.api_types.h"
1632 #define vl_endianfun /* define message structures */
1633 #include "{module}.api.h"
1636 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
1638 #include "{module}.api.h"
1641 #include "{module}.api_tojson.h"
1642 #include "{module}.api_fromjson.h"
1643 #include <vpp-api/client/vppapiclient.h>
1645 #include <vat2/vat2_helpers.h>
1649 write(hdr.format(module=module))
1652 if s.reply not in define_hash:
1654 c_test_api_service(s, s.stream, stream)
1656 write('void vat2_register_function(char *, cJSON * (*)(cJSON *));\n')
1657 # write('__attribute__((constructor))')
1658 write('clib_error_t *\n')
1659 write('vat2_register_plugin (void) {\n')
1661 write(' vat2_register_function("{n}", api_{n});\n'
1662 .format(n=s.caller))
1663 write(' return 0;\n')
1668 # Plugin entry point
1670 def run(args, apifilename, s):
1671 '''Main plugin entry point.'''
1674 if not args.outputdir:
1675 sys.stderr.write('Missing --outputdir argument')
1678 basename = os.path.basename(apifilename)
1679 filename, _ = os.path.splitext(basename)
1680 modulename = filename.replace('.', '_')
1681 filename_enum = os.path.join(args.outputdir + '/' + basename + '_enum.h')
1682 filename_types = os.path.join(args.outputdir + '/' + basename + '_types.h')
1683 filename_c = os.path.join(args.outputdir + '/' + basename + '.c')
1684 filename_c_test = os.path.join(args.outputdir + '/' + basename + '_test.c')
1685 filename_c_test2 = (os.path.join(args.outputdir + '/' + basename +
1687 filename_c_tojson = (os.path.join(args.outputdir +
1688 '/' + basename + '_tojson.h'))
1689 filename_c_fromjson = (os.path.join(args.outputdir + '/' +
1690 basename + '_fromjson.h'))
1692 # Generate separate types file
1694 generate_include_types(s, modulename, st)
1695 with open(filename_types, 'w') as fd:
1697 shutil.copyfileobj(st, fd)
1700 # Generate separate enum file
1702 st.write('#ifndef included_{}_api_enum_h\n'.format(modulename))
1703 st.write('#define included_{}_api_enum_h\n'.format(modulename))
1704 generate_include_enum(s, modulename, st)
1705 generate_include_counters(s['Counters'], st)
1706 st.write('#endif\n')
1707 with open(filename_enum, 'w') as fd:
1709 shutil.copyfileobj(st, fd)
1712 # Generate separate C file
1714 generate_c_boilerplate(s['Service'], s['Define'], s['Counters'],
1715 s['file_crc'], modulename, st)
1716 with open(filename_c, 'w') as fd:
1718 shutil.copyfileobj(st, fd)
1721 # Generate separate C test file
1723 plugin = bool('plugin' in apifilename)
1724 generate_c_test_boilerplate(s['Service'], s['Define'],
1726 modulename, plugin, st)
1727 with open(filename_c_test, 'w') as fd:
1729 shutil.copyfileobj(st, fd)
1732 # Fully autogenerated VATv2 C test file
1734 generate_c_test2_boilerplate(s['Service'], s['Define'],
1736 with open(filename_c_test2, 'w') as fd:
1738 shutil.copyfileobj(st, fd)
1741 # Generate separate JSON file
1743 generate_tojson(s, modulename, st)
1744 with open(filename_c_tojson, 'w') as fd:
1746 shutil.copyfileobj(st, fd)
1749 generate_fromjson(s, modulename, st)
1750 with open(filename_c_fromjson, 'w') as fd:
1752 shutil.copyfileobj(st, fd)
1755 output = TOP_BOILERPLATE.format(datestring=DATESTRING,
1756 input_filename=basename)
1757 output += generate_imports(s['Import'])
1758 output += msg_ids(s)
1759 output += msg_names(s)
1760 output += msg_name_crc_list(s, filename)
1761 output += typedefs(modulename)
1762 printfun_types(s['types'], stream, modulename)
1763 printfun(s['Define'], stream, modulename)
1764 output += stream.getvalue()
1766 output += endianfun(s['types'] + s['Define'], modulename)
1767 output += version_tuple(s, basename)
1768 output += BOTTOM_BOILERPLATE.format(input_filename=basename,
1769 file_crc=s['file_crc'])