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_base_type(self, t):
79 vt = self.types_hash[t]
80 if vt.type == 'Using' and 'length' not in vt.alias:
81 vt_type = vt.alias['type']
86 def get_json_func(self, t):
87 '''Given the type, returns the function to use to create a
89 vt, vt_type = self.get_base_type(t)
91 if t in self.is_number or vt_type in self.is_number:
92 return 'cJSON_AddNumberToObject', '', False
94 return 'cJSON_AddBoolToObject', '', False
96 # Lookup type name check if it's enum
97 if vt.type == 'Enum' or vt.type == 'EnumFlag':
98 return '{t}_tojson'.format(t=t), '', True
99 return '{t}_tojson'.format(t=t), '&', True
101 def get_json_array_func(self, t):
102 '''Given a type returns the function to create a cJSON object
104 if t in self.is_number:
105 return 'cJSON_CreateNumber', ''
107 return 'cJSON_CreateBool', ''
108 vt, vt_type = self.get_base_type(t)
109 if vt.type == 'Enum' or vt.type == 'EnumFlag':
110 return '{t}_tojson'.format(t=t), ''
111 return '{t}_tojson'.format(t=t), '&'
113 def print_string(self, o):
114 '''Create cJSON object from vl_api_string_t'''
115 write = self.stream.write
117 write(' vl_api_string_cJSON_AddToObject(o, "{n}", &a->{n});\n'
118 .format(n=o.fieldname))
121 write(' cJSON_AddStringToObject(o, "{n}", (char *)a->{n});\n'
122 .format(n=o.fieldname))
124 def print_field(self, o):
125 '''Called for every field in a typedef or define.'''
126 write = self.stream.write
127 if o.fieldname in self.noprint_fields:
130 f, p, newobj = self.get_json_func(o.fieldtype)
133 write(' cJSON_AddItemToObject(o, "{n}", {f}({p}a->{n}));\n'
134 .format(f=f, p=p, n=o.fieldname))
136 write(' {f}(o, "{n}", {p}a->{n});\n'
137 .format(f=f, p=p, n=o.fieldname))
139 _dispatch['Field'] = print_field
141 def print_array(self, o):
142 '''Converts a VPP API array to cJSON array.'''
143 write = self.stream.write
148 cJSON *array = cJSON_AddArrayToObject(o, "{n}");
149 for (i = 0; i < {lfield}; i++) {{
150 cJSON_AddItemToArray(array, {f}({p}a->{n}[i]));
155 if o.fieldtype == 'string':
159 lfield = 'a->' + o.lengthfield if o.lengthfield else o.length
160 if o.fieldtype == 'u8':
162 # What is length field doing here?
163 write(' u8 *s = format(0, "0x%U", format_hex_bytes, '
164 '&a->{n}, {lfield});\n'
165 .format(n=o.fieldname, lfield=lfield))
166 write(' cJSON_AddStringToObject(o, "{n}", (char *)s);\n'
167 .format(n=o.fieldname))
168 write(' vec_free(s);\n')
172 f, p = self.get_json_array_func(o.fieldtype)
173 write(forloop.format(lfield=lfield,
179 _dispatch['Array'] = print_array
181 def print_enum(self, o):
182 '''Create cJSON object (string) for VPP API enum'''
183 write = self.stream.write
184 write('static inline cJSON *vl_api_{name}_t_tojson '
185 '(vl_api_{name}_t a) {{\n'.format(name=o.name))
187 write(" switch(a) {\n")
189 write(" case %s:\n" % b[1])
190 write(' return cJSON_CreateString("{}");\n'.format(b[0]))
191 write(' default: return cJSON_CreateString("Invalid ENUM");\n')
193 write(' return 0;\n')
196 _dispatch['Enum'] = print_enum
198 def print_enum_flag(self, o):
199 '''Create cJSON object (string) for VPP API enum'''
200 write = self.stream.write
201 write('static inline cJSON *vl_api_{name}_t_tojson '
202 '(vl_api_{name}_t a) {{\n'.format(name=o.name))
203 write(' cJSON *array = cJSON_CreateArray();\n')
206 write(' if (a & {})\n'.format(b[0]))
207 write(' cJSON_AddItemToArray(array, cJSON_CreateString("{}"));\n'.format(b[0]))
208 write(' return array;\n')
211 _dispatch['EnumFlag'] = print_enum_flag
213 def print_typedef(self, o):
214 '''Create cJSON (dictionary) object from VPP API typedef'''
215 write = self.stream.write
216 write('static inline cJSON *vl_api_{name}_t_tojson '
217 '(vl_api_{name}_t *a) {{\n'.format(name=o.name))
218 write(' cJSON *o = cJSON_CreateObject();\n')
221 self._dispatch[t.type](self, t)
223 write(' return o;\n')
226 def print_define(self, o):
227 '''Create cJSON (dictionary) object from VPP API define'''
228 write = self.stream.write
229 write('static inline cJSON *vl_api_{name}_t_tojson '
230 '(vl_api_{name}_t *a) {{\n'.format(name=o.name))
231 write(' cJSON *o = cJSON_CreateObject();\n')
232 write(' cJSON_AddStringToObject(o, "_msgname", "{}");\n'
236 self._dispatch[t.type](self, t)
238 write(' return o;\n')
241 def print_using(self, o):
242 '''Create cJSON (dictionary) object from VPP API aliased type'''
246 write = self.stream.write
247 write('static inline cJSON *vl_api_{name}_t_tojson '
248 '(vl_api_{name}_t *a) {{\n'.format(name=o.name))
250 write(' u8 *s = format(0, "%U", format_vl_api_{}_t, a);\n'
252 write(' cJSON *o = cJSON_CreateString((char *)s);\n')
253 write(' vec_free(s);\n')
254 write(' return o;\n')
257 _dispatch['Typedef'] = print_typedef
258 _dispatch['Define'] = print_define
259 _dispatch['Using'] = print_using
260 _dispatch['Union'] = print_typedef
262 def generate_function(self, t):
263 '''Main entry point'''
264 write = self.stream.write
266 write('/* Manual print {} */\n'.format(t.name))
268 self._dispatch[t.type](self, t)
270 def generate_types(self):
271 '''Main entry point'''
273 self.generate_function(t)
275 def generate_defines(self):
276 '''Main entry point'''
277 for t in self.defines:
278 self.generate_function(t)
283 Parse JSON objects into VPP API binary message structures.
286 noprint_fields = {'_vl_msg_id': None,
287 'client_index': None,
289 is_number = {'u8': None,
300 def __init__(self, module, types, defines, imported_types, stream):
303 self.defines = defines
305 self.types_hash = {'vl_api_'+d.name+'_t':
306 d for d in types + imported_types}
307 self.defines_hash = {d.name: d for d in defines}
310 '''Output the top boilerplate.'''
311 write = self.stream.write
312 write('#ifndef included_{}_api_fromjson_h\n'.format(self.module))
313 write('#define included_{}_api_fromjson_h\n'.format(self.module))
314 write('#include <vppinfra/cJSON.h>\n\n')
315 write('#include <vat2/jsonconvert.h>\n\n')
316 write('#pragma GCC diagnostic ignored "-Wunused-label"\n')
318 def is_base_type(self, t):
319 '''Check if a type is one of the VPP API base types'''
320 if t in self.is_number:
327 '''Output the bottom boilerplate.'''
328 write = self.stream.write
331 def print_string(self, o, toplevel=False):
332 '''Convert JSON string to vl_api_string_t'''
333 write = self.stream.write
335 msgvar = "a" if toplevel else "*mp"
336 msgsize = "l" if toplevel else "*len"
339 write(' char *p = cJSON_GetStringValue(item);\n')
340 write(' size_t plen = strlen(p);\n')
341 write(' {msgvar} = realloc({msgvar}, {msgsize} + plen);\n'
342 .format(msgvar=msgvar, msgsize=msgsize))
343 write(' if ({msgvar} == 0) goto error;\n'.format(msgvar=msgvar))
344 write(' vl_api_c_string_to_api_string(p, (void *){msgvar} + '
345 '{msgsize} - sizeof(vl_api_string_t));\n'
346 .format(msgvar=msgvar, msgsize=msgsize))
347 write(' {msgsize} += plen;\n'.format(msgsize=msgsize))
349 write(' strncpy_s((char *)a->{n}, sizeof(a->{n}), '
350 'cJSON_GetStringValue(item), sizeof(a->{n}) - 1);\n'
351 .format(n=o.fieldname))
353 def print_field(self, o, toplevel=False):
354 '''Called for every field in a typedef or define.'''
355 write = self.stream.write
356 if o.fieldname in self.noprint_fields:
358 is_bt = self.is_base_type(o.fieldtype)
359 t = 'vl_api_{}'.format(o.fieldtype) if is_bt else o.fieldtype
361 msgvar = "(void **)&a" if toplevel else "mp"
362 msgsize = "&l" if toplevel else "len"
365 write(' vl_api_{t}_fromjson(item, &a->{n});\n'
366 .format(t=o.fieldtype, n=o.fieldname))
368 write(' if ({t}_fromjson({msgvar}, '
369 '{msgsize}, item, &a->{n}) < 0) goto error;\n'
370 .format(t=t, n=o.fieldname, msgvar=msgvar, msgsize=msgsize))
372 _dispatch['Field'] = print_field
374 def print_array(self, o, toplevel=False):
375 '''Convert JSON array to VPP API array'''
376 write = self.stream.write
381 cJSON *array = cJSON_GetObjectItem(o, "{n}");
382 int size = cJSON_GetArraySize(array);
383 if (size != {lfield}) return 0;
384 for (i = 0; i < size; i++) {{
385 cJSON *e = cJSON_GetArrayItem(array, i);
393 cJSON *array = cJSON_GetObjectItem(o, "{n}");
394 int size = cJSON_GetArraySize(array);
396 *{msgvar} = realloc({msgvar}, {msgsize} + sizeof({t}) * size);
397 {t} *d = (void *){msgvar} + {msgsize};
398 {msgsize} += sizeof({t}) * size;
399 for (i = 0; i < size; i++) {{
400 cJSON *e = cJSON_GetArrayItem(array, i);
406 if o.fieldtype == 'string':
407 self.print_string(o, toplevel)
410 lfield = 'a->' + o.lengthfield if o.lengthfield else o.length
411 msgvar = "(void **)&a" if toplevel else "mp"
412 msgsize = "l" if toplevel else "*len"
414 if o.fieldtype == 'u8':
416 write(' s = u8string_fromjson(o, "{}");\n'
417 .format(o.fieldname))
418 write(' if (!s) return 0;\n')
419 write(' {} = vec_len(s);\n'.format(lfield))
421 write(' *{msgvar} = realloc({msgvar}, {msgsize} + '
422 'vec_len(s));\n'.format(msgvar=msgvar, msgsize=msgsize))
423 write(' memcpy((void *){msgvar} + {msgsize}, s, '
424 'vec_len(s));\n'.format(msgvar=msgvar, msgsize=msgsize))
425 write(' {msgsize} += vec_len(s);\n'.format(msgsize=msgsize))
427 write(' vec_free(s);\n')
429 write(' u8string_fromjson2(o, "{n}", a->{n});\n'
430 .format(n=o.fieldname))
433 is_bt = self.is_base_type(o.fieldtype)
437 call = ('vl_api_{t}_fromjson(e, &d[i]);'
438 .format(t=o.fieldtype))
440 call = ('{t}_fromjson({msgvar}, len, e, &d[i]); '
441 .format(t=o.fieldtype, msgvar=msgvar))
442 write(forloop_vla.format(lfield=lfield,
450 call = ('vl_api_{t}_fromjson(e, &a->{n}[i]);'
451 .format(t=t, n=o.fieldname))
453 call = ('if ({}_fromjson({}, len, e, &a->{}[i]) < 0) goto error;'
454 .format(t, msgvar, o.fieldname))
455 write(forloop.format(lfield=lfield,
462 _dispatch['Array'] = print_array
464 def print_enum(self, o):
465 '''Convert to JSON enum(string) to VPP API enum (int)'''
466 write = self.stream.write
467 write('static inline int vl_api_{n}_t_fromjson'
468 '(void **mp, int *len, cJSON *o, vl_api_{n}_t *a) {{\n'
470 write(' char *p = cJSON_GetStringValue(o);\n')
472 write(' if (strcmp(p, "{}") == 0) {{*a = {}; return 0;}}\n'
475 write(' return -1;\n')
478 _dispatch['Enum'] = print_enum
480 def print_enum_flag(self, o):
481 '''Convert to JSON enum(string) to VPP API enum (int)'''
482 write = self.stream.write
483 write('static inline void *vl_api_{n}_t_fromjson '
484 '(void *mp, int *len, cJSON *o, vl_api_{n}_t *a) {{\n'
488 write(' for (i = 0; i < cJSON_GetArraySize(o); i++) {\n')
489 write(' cJSON *e = cJSON_GetArrayItem(o, i);\n')
490 write(' char *p = cJSON_GetStringValue(e);\n')
491 write(' if (!p) return 0;\n')
493 write(' if (strcmp(p, "{}") == 0) *a |= {};\n'
496 write(' return mp;\n')
499 _dispatch['EnumFlag'] = print_enum_flag
501 def print_typedef(self, o):
502 '''Convert from JSON object to VPP API binary representation'''
503 write = self.stream.write
505 write('static inline int vl_api_{name}_t_fromjson (void **mp, '
506 'int *len, cJSON *o, vl_api_{name}_t *a) {{\n'
507 .format(name=o.name))
508 write(' cJSON *item __attribute__ ((unused));\n')
509 write(' u8 *s __attribute__ ((unused));\n')
511 if t.type == 'Field' and t.is_lengthfield:
513 write('\n item = cJSON_GetObjectItem(o, "{}");\n'
514 .format(t.fieldname))
515 write(' if (!item) goto error;\n')
517 self._dispatch[t.type](self, t)
519 write('\n return 0;\n')
521 write(' return -1;\n')
524 def print_union(self, o):
525 '''Convert JSON object to VPP API binary union'''
526 write = self.stream.write
528 write('static inline int vl_api_{name}_t_fromjson (void **mp, '
529 'int *len, cJSON *o, vl_api_{name}_t *a) {{\n'
530 .format(name=o.name))
531 write(' cJSON *item __attribute__ ((unused));\n')
532 write(' u8 *s __attribute__ ((unused));\n')
534 if t.type == 'Field' and t.is_lengthfield:
536 write(' item = cJSON_GetObjectItem(o, "{}");\n'
537 .format(t.fieldname))
538 write(' if (item) {\n')
539 self._dispatch[t.type](self, t)
541 write('\n return 0;\n')
543 write(' return -1;\n')
546 def print_define(self, o):
547 '''Convert JSON object to VPP API message'''
548 write = self.stream.write
549 write('static inline vl_api_{name}_t *vl_api_{name}_t_fromjson '
550 '(cJSON *o, int *len) {{\n'.format(name=o.name))
551 write(' cJSON *item __attribute__ ((unused));\n')
552 write(' u8 *s __attribute__ ((unused));\n')
553 write(' int l = sizeof(vl_api_{}_t);\n'.format(o.name))
554 write(' vl_api_{}_t *a = malloc(l);\n'.format(o.name))
558 if t.fieldname in self.noprint_fields:
560 if t.type == 'Field' and t.is_lengthfield:
562 write(' item = cJSON_GetObjectItem(o, "{}");\n'
563 .format(t.fieldname))
564 write(' if (!item) goto error;\n')
565 self._dispatch[t.type](self, t, toplevel=True)
568 write(' *len = l;\n')
569 write(' return a;\n')
572 write(' return 0;\n')
575 def print_using(self, o):
576 '''Convert JSON field to VPP type alias'''
577 write = self.stream.write
583 write('static inline void *vl_api_{name}_t_fromjson (void *mp, '
584 'int *len, cJSON *o, vl_api_{name}_t *a) {{\n'
585 .format(name=o.name))
586 if 'length' in o.alias:
587 if t.fieldtype != 'u8':
588 raise ValueError("Error in processing type {} for {}"
589 .format(t.fieldtype, o.name))
590 write(' vl_api_u8_string_fromjson(o, (u8 *)a, {});\n'
591 .format(o.alias['length']))
593 write(' vl_api_{t}_fromjson(o, ({t} *)a);\n'
594 .format(t=t.fieldtype))
596 write(' return mp;\n')
599 _dispatch['Typedef'] = print_typedef
600 _dispatch['Define'] = print_define
601 _dispatch['Using'] = print_using
602 _dispatch['Union'] = print_union
604 def generate_function(self, t):
605 '''Main entry point'''
606 write = self.stream.write
608 write('/* Manual print {} */\n'.format(t.name))
610 self._dispatch[t.type](self, t)
612 def generate_types(self):
613 '''Main entry point'''
615 self.generate_function(t)
617 def generate_defines(self):
618 '''Main entry point'''
619 for t in self.defines:
620 self.generate_function(t)
623 def generate_tojson(s, modulename, stream):
624 '''Generate all functions to convert from API to JSON'''
627 write('/* Imported API files */\n')
628 for i in s['Import']:
629 f = i.filename.replace('plugins/', '')
630 write('#include <{}_tojson.h>\n'.format(f))
632 pp = ToJSON(modulename, s['types'], s['Define'], s['imported']['types'],
636 pp.generate_defines()
641 def generate_fromjson(s, modulename, stream):
642 '''Generate all functions to convert from JSON to API'''
644 write('/* Imported API files */\n')
645 for i in s['Import']:
646 f = i.filename.replace('plugins/', '')
647 write('#include <{}_fromjson.h>\n'.format(f))
649 pp = FromJSON(modulename, s['types'], s['Define'], s['imported']['types'],
653 pp.generate_defines()
658 ###############################################################################
661 DATESTRING = datetime.datetime.utcfromtimestamp(
662 int(os.environ.get('SOURCE_DATE_EPOCH', time.time())))
663 TOP_BOILERPLATE = '''\
665 * VLIB API definitions {datestring}
666 * Input file: {input_filename}
667 * Automatically generated: please edit the input file NOT this file!
671 #if defined(vl_msg_id)||defined(vl_union_id) \\
672 || defined(vl_printfun) ||defined(vl_endianfun) \\
673 || defined(vl_api_version)||defined(vl_typedefs) \\
674 || defined(vl_msg_name)||defined(vl_msg_name_crc_list) \\
675 || defined(vl_api_version_tuple)
676 /* ok, something was selected */
678 #warning no content included from {input_filename}
681 #define VL_API_PACKED(x) x __attribute__ ((packed))
684 BOTTOM_BOILERPLATE = '''\
685 /****** API CRC (whole file) *****/
687 #ifdef vl_api_version
688 vl_api_version({input_filename}, {file_crc:#08x})
695 '''Generate macro to map API message id to handler'''
698 /****** Message ID / handler enum ******/
703 for t in s['Define']:
704 output += "vl_msg_id(VL_API_%s, vl_api_%s_t_handler)\n" % \
705 (t.name.upper(), t.name)
712 '''Generate calls to name mapping macro'''
715 /****** Message names ******/
720 for t in s['Define']:
721 dont_trace = 0 if t.dont_trace else 1
722 output += "vl_msg_name(vl_api_%s_t, %d)\n" % (t.name, dont_trace)
728 def msg_name_crc_list(s, suffix):
729 '''Generate list of names to CRC mappings'''
732 /****** Message name, crc list ******/
734 #ifdef vl_msg_name_crc_list
736 output += "#define foreach_vl_msg_name_crc_%s " % suffix
738 for t in s['Define']:
739 output += "\\\n_(VL_API_%s, %s, %08x) " % \
740 (t.name.upper(), t.name, t.crc)
746 def api2c(fieldtype):
747 '''Map between API type names and internal VPP type names'''
748 mappingtable = {'string': 'vl_api_string_t', }
749 if fieldtype in mappingtable:
750 return mappingtable[fieldtype]
754 def typedefs(filename):
755 '''Include in the main files to the types file'''
758 /****** Typedefs ******/
761 #include "{include}.api_types.h"
763 '''.format(include=filename)
767 FORMAT_STRINGS = {'u8': '%u',
780 '''Functions for pretty printing VPP API messages'''
782 noprint_fields = {'_vl_msg_id': None,
783 'client_index': None,
786 def __init__(self, stream):
790 def print_string(o, stream):
791 '''Pretty print a vl_api_string_t'''
794 write(' if (vl_api_string_len(&a->{f}) > 0) {{\n'
795 .format(f=o.fieldname))
796 write(' s = format(s, "\\n%U{f}: %U", '
797 'format_white_space, indent, '
798 'vl_api_format_string, (&a->{f}));\n'.format(f=o.fieldname))
800 write(' s = format(s, "\\n%U{f}:", '
801 'format_white_space, indent);\n'.format(f=o.fieldname))
804 write(' s = format(s, "\\n%U{f}: %s", '
805 'format_white_space, indent, a->{f});\n'
806 .format(f=o.fieldname))
808 def print_field(self, o, stream):
809 '''Pretty print API field'''
811 if o.fieldname in self.noprint_fields:
813 if o.fieldtype in FORMAT_STRINGS:
814 f = FORMAT_STRINGS[o.fieldtype]
815 write(' s = format(s, "\\n%U{n}: {f}", '
816 'format_white_space, indent, a->{n});\n'
817 .format(n=o.fieldname, f=f))
819 write(' s = format(s, "\\n%U{n}: %U", '
820 'format_white_space, indent, '
821 'format_{t}, &a->{n}, indent);\n'
822 .format(n=o.fieldname, t=o.fieldtype))
824 _dispatch['Field'] = print_field
826 def print_array(self, o, stream):
827 '''Pretty print API array'''
831 for (i = 0; i < {lfield}; i++) {{
832 s = format(s, "\\n%U{n}: %U",
833 format_white_space, indent, format_{t}, &a->{n}[i], indent);
837 forloop_format = '''\
838 for (i = 0; i < {lfield}; i++) {{
839 s = format(s, "\\n%U{n}: {t}",
840 format_white_space, indent, a->{n}[i]);
844 if o.fieldtype == 'string':
845 self.print_string(o, stream)
848 if o.fieldtype == 'u8':
850 write(' s = format(s, "\\n%U{n}: %U", format_white_space, '
851 'indent, format_hex_bytes, a->{n}, a->{lfield});\n'
852 .format(n=o.fieldname, lfield=o.lengthfield))
854 write(' s = format(s, "\\n%U{n}: %U", format_white_space, '
855 'indent, format_hex_bytes, a, {lfield});\n'
856 .format(n=o.fieldname, lfield=o.length))
859 lfield = 'a->' + o.lengthfield if o.lengthfield else o.length
860 if o.fieldtype in FORMAT_STRINGS:
861 write(forloop_format.format(lfield=lfield,
862 t=FORMAT_STRINGS[o.fieldtype],
865 write(forloop.format(lfield=lfield, t=o.fieldtype, n=o.fieldname))
867 _dispatch['Array'] = print_array
870 def print_alias(k, v, stream):
871 '''Pretty print type alias'''
873 if ('length' in v.alias and v.alias['length'] and
874 v.alias['type'] == 'u8'):
875 write(' return format(s, "%U", format_hex_bytes, a, {});\n'
876 .format(v.alias['length']))
877 elif v.alias['type'] in FORMAT_STRINGS:
878 write(' return format(s, "{}", *a);\n'
879 .format(FORMAT_STRINGS[v.alias['type']]))
881 write(' return format(s, "{} (print not implemented)");\n'
885 def print_enum(o, stream):
886 '''Pretty print API enum'''
888 write(" switch(*a) {\n")
890 write(" case %s:\n" % b[1])
891 write(' return format(s, "{}");\n'.format(b[0]))
894 _dispatch['Enum'] = print_enum
895 _dispatch['EnumFlag'] = print_enum
897 def print_obj(self, o, stream):
901 if o.type in self._dispatch:
902 self._dispatch[o.type](self, o, stream)
904 write(' s = format(s, "\\n{} {} {} (print not implemented");\n'
905 .format(o.type, o.fieldtype, o.fieldname))
908 def printfun(objs, stream, modulename):
909 '''Main entry point for pretty print function generation'''
913 /****** Print functions *****/
915 #ifndef included_{module}_printfun
916 #define included_{module}_printfun
919 #define _uword_fmt \"%lld\"
920 #define _uword_cast (long long)
922 #define _uword_fmt \"%ld\"
923 #define _uword_cast long
929 static inline void *vl_api_{name}_t_print (vl_api_{name}_t *a, void *handle)
932 u32 indent __attribute__((unused)) = 2;
933 int i __attribute__((unused));
936 h = h.format(module=modulename)
939 pp = Printfun(stream)
942 write("/***** manual: vl_api_%s_t_print *****/\n\n" % t.name)
944 write(signature.format(name=t.name))
945 write(' /* Message definition: vl_api_{}_t: */\n'.format(t.name))
946 write(" s = format(s, \"vl_api_%s_t:\");\n" % t.name)
948 pp.print_obj(o, stream)
949 write(' vec_add1(s, 0);\n')
950 write(' vl_print (handle, (char *)s);\n')
951 write(' vec_free (s);\n')
952 write(' return handle;\n')
956 write("\n#endif /* vl_printfun */\n")
961 def printfun_types(objs, stream, modulename):
962 '''Pretty print API types'''
964 pp = Printfun(stream)
967 /****** Print functions *****/
969 #ifndef included_{module}_printfun_types
970 #define included_{module}_printfun_types
973 h = h.format(module=modulename)
977 static inline u8 *format_vl_api_{name}_t (u8 *s, va_list * args)
979 vl_api_{name}_t *a = va_arg (*args, vl_api_{name}_t *);
980 u32 indent __attribute__((unused)) = va_arg (*args, u32);
981 int i __attribute__((unused));
986 if t.__class__.__name__ == 'Enum' or t.__class__.__name__ == 'EnumFlag':
987 write(signature.format(name=t.name))
988 pp.print_enum(t.block, stream)
989 write(' return s;\n')
994 write("/***** manual: vl_api_%s_t_print *****/\n\n" % t.name)
997 if t.__class__.__name__ == 'Using':
998 write(signature.format(name=t.name))
999 pp.print_alias(t.name, t, stream)
1003 write(signature.format(name=t.name))
1005 pp.print_obj(o, stream)
1007 write(' return s;\n')
1011 write("\n#endif /* vl_printfun_types */\n")
1014 def generate_imports(imports):
1015 '''Add #include matching the API import statements'''
1016 output = '/* Imported API files */\n'
1017 output += '#ifndef vl_api_version\n'
1020 s = i.filename.replace('plugins/', '')
1021 output += '#include <{}.h>\n'.format(s)
1022 output += '#endif\n'
1027 'u16': 'clib_net_to_host_u16',
1028 'u32': 'clib_net_to_host_u32',
1029 'u64': 'clib_net_to_host_u64',
1030 'i16': 'clib_net_to_host_i16',
1031 'i32': 'clib_net_to_host_i32',
1032 'i64': 'clib_net_to_host_i64',
1033 'f64': 'clib_net_to_host_f64',
1037 def endianfun_array(o):
1038 '''Generate endian functions for arrays'''
1040 for (i = 0; i < {length}; i++) {{
1041 a->{name}[i] = {format}(a->{name}[i]);
1045 forloop_format = '''\
1046 for (i = 0; i < {length}; i++) {{
1047 {type}_endian(&a->{name}[i]);
1052 if o.fieldtype == 'u8' or o.fieldtype == 'string' or o.fieldtype == 'bool':
1053 output += ' /* a->{n} = a->{n} (no-op) */\n'.format(n=o.fieldname)
1055 lfield = 'a->' + o.lengthfield if o.lengthfield else o.length
1056 if o.fieldtype in ENDIAN_STRINGS:
1058 .format(length=lfield,
1059 format=ENDIAN_STRINGS[o.fieldtype],
1062 output += (forloop_format
1063 .format(length=lfield, type=o.fieldtype,
1068 NO_ENDIAN_CONVERSION = {'client_index': None}
1071 def endianfun_obj(o):
1072 '''Generate endian conversion function for type'''
1074 if o.type == 'Array':
1075 return endianfun_array(o)
1076 if o.type != 'Field':
1077 output += (' s = format(s, "\\n{} {} {} (print not implemented");\n'
1078 .format(o.type, o.fieldtype, o.fieldname))
1080 if o.fieldname in NO_ENDIAN_CONVERSION:
1081 output += ' /* a->{n} = a->{n} (no-op) */\n'.format(n=o.fieldname)
1083 if o.fieldtype in ENDIAN_STRINGS:
1084 output += (' a->{name} = {format}(a->{name});\n'
1085 .format(name=o.fieldname,
1086 format=ENDIAN_STRINGS[o.fieldtype]))
1087 elif o.fieldtype.startswith('vl_api_'):
1088 output += (' {type}_endian(&a->{name});\n'
1089 .format(type=o.fieldtype, name=o.fieldname))
1091 output += ' /* a->{n} = a->{n} (no-op) */\n'.format(n=o.fieldname)
1096 def endianfun(objs, modulename):
1097 '''Main entry point for endian function generation'''
1100 /****** Endian swap functions *****/\n\
1102 #ifndef included_{module}_endianfun
1103 #define included_{module}_endianfun
1105 #undef clib_net_to_host_uword
1107 #define clib_net_to_host_uword clib_net_to_host_u64
1109 #define clib_net_to_host_uword clib_net_to_host_u32
1113 output = output.format(module=modulename)
1116 static inline void vl_api_{name}_t_endian (vl_api_{name}_t *a)
1118 int i __attribute__((unused));
1122 if t.__class__.__name__ == 'Enum' or t.__class__.__name__ == 'EnumFlag' :
1123 output += signature.format(name=t.name)
1124 if t.enumtype in ENDIAN_STRINGS:
1125 output += (' *a = {}(*a);\n'
1126 .format(ENDIAN_STRINGS[t.enumtype]))
1128 output += (' /* a->{name} = a->{name} (no-op) */\n'
1129 .format(name=t.name))
1135 output += "/***** manual: vl_api_%s_t_endian *****/\n\n" % t.name
1138 if t.__class__.__name__ == 'Using':
1139 output += signature.format(name=t.name)
1140 if ('length' in t.alias and t.alias['length'] and
1141 t.alias['type'] == 'u8'):
1142 output += (' /* a->{name} = a->{name} (no-op) */\n'
1143 .format(name=t.name))
1144 elif t.alias['type'] in FORMAT_STRINGS:
1145 output += (' *a = {}(*a);\n'
1146 .format(ENDIAN_STRINGS[t.alias['type']]))
1148 output += ' /* Not Implemented yet {} */'.format(t.name)
1152 output += signature.format(name=t.name)
1155 output += endianfun_obj(o)
1158 output += "\n#endif"
1159 output += "\n#endif /* vl_endianfun */\n\n"
1164 def version_tuple(s, module):
1165 '''Generate semantic version string'''
1167 /****** Version tuple *****/
1169 #ifdef vl_api_version_tuple
1172 if 'version' in s['Option']:
1173 v = s['Option']['version']
1174 (major, minor, patch) = v.split('.')
1175 output += "vl_api_version_tuple(%s, %s, %s, %s)\n" % \
1176 (module, major, minor, patch)
1178 output += "\n#endif /* vl_api_version_tuple */\n\n"
1183 def generate_include_enum(s, module, stream):
1184 '''Generate <name>.api_enum.h'''
1185 write = stream.write
1188 write('typedef enum {\n')
1189 for t in s['Define']:
1190 write(' VL_API_{},\n'.format(t.name.upper()))
1191 write(' VL_MSG_{}_LAST\n'.format(module.upper()))
1192 write('}} vl_api_{}_enum_t;\n'.format(module))
1195 def generate_include_counters(s, stream):
1196 '''Include file for the counter data model types.'''
1197 write = stream.write
1200 csetname = counters.name
1201 write('typedef enum {\n')
1202 for c in counters.block:
1203 write(' {}_ERROR_{},\n'
1204 .format(csetname.upper(), c['name'].upper()))
1205 write(' {}_N_ERROR\n'.format(csetname.upper()))
1206 write('}} vl_counter_{}_enum_t;\n'.format(csetname))
1208 write('extern vl_counter_t {}_error_counters[];\n'.format(csetname))
1211 def generate_include_types(s, module, stream):
1212 '''Generate separate API _types file.'''
1213 write = stream.write
1215 write('#ifndef included_{module}_api_types_h\n'.format(module=module))
1216 write('#define included_{module}_api_types_h\n'.format(module=module))
1218 if 'version' in s['Option']:
1219 v = s['Option']['version']
1220 (major, minor, patch) = v.split('.')
1221 write('#define VL_API_{m}_API_VERSION_MAJOR {v}\n'
1222 .format(m=module.upper(), v=major))
1223 write('#define VL_API_{m}_API_VERSION_MINOR {v}\n'
1224 .format(m=module.upper(), v=minor))
1225 write('#define VL_API_{m}_API_VERSION_PATCH {v}\n'
1226 .format(m=module.upper(), v=patch))
1229 write('/* Imported API files */\n')
1230 for i in s['Import']:
1231 filename = i.filename.replace('plugins/', '')
1232 write('#include <{}_types.h>\n'.format(filename))
1234 for o in s['types'] + s['Define']:
1235 tname = o.__class__.__name__
1236 if tname == 'Using':
1237 if 'length' in o.alias:
1238 write('typedef %s vl_api_%s_t[%s];\n' %
1239 (o.alias['type'], o.name, o.alias['length']))
1241 write('typedef %s vl_api_%s_t;\n' % (o.alias['type'], o.name))
1242 elif tname == 'Enum' or tname == 'EnumFlag':
1243 if o.enumtype == 'u32':
1244 write("typedef enum {\n")
1246 write("typedef enum __attribute__((packed)) {\n")
1249 write(" %s = %s,\n" % (b[0], b[1]))
1250 write('} vl_api_%s_t;\n' % o.name)
1251 if o.enumtype != 'u32':
1252 size1 = 'sizeof(vl_api_%s_t)' % o.name
1253 size2 = 'sizeof(%s)' % o.enumtype
1254 err_str = 'size of API enum %s is wrong' % o.name
1255 write('STATIC_ASSERT(%s == %s, "%s");\n'
1256 % (size1, size2, err_str))
1258 if tname == 'Union':
1259 write("typedef union __attribute__ ((packed)) _vl_api_%s {\n"
1262 write(("typedef struct __attribute__ ((packed)) _vl_api_%s {\n")
1265 if b.type == 'Option':
1267 if b.type == 'Field':
1268 write(" %s %s;\n" % (api2c(b.fieldtype),
1270 elif b.type == 'Array':
1272 write(" %s %s[0];\n" % (api2c(b.fieldtype),
1275 # Fixed length strings decay to nul terminated u8
1276 if b.fieldtype == 'string':
1279 .format(api2c(b.fieldtype),
1282 write(' u8 {}[{}];\n'
1283 .format(b.fieldname, b.length))
1285 write(" %s %s[%s];\n" %
1286 (api2c(b.fieldtype), b.fieldname,
1289 raise ValueError("Error in processing type {} for {}"
1292 write('} vl_api_%s_t;\n' % o.name)
1294 for t in s['Define']:
1295 write('#define VL_API_{ID}_CRC "{n}_{crc:08x}"\n'
1296 .format(n=t.name, ID=t.name.upper(), crc=t.crc))
1301 def generate_c_boilerplate(services, defines, counters, file_crc,
1303 '''VPP side plugin.'''
1304 write = stream.write
1305 define_hash = {d.name: d for d in defines}
1308 #define vl_endianfun /* define message structures */
1309 #include "{module}.api.h"
1312 /* instantiate all the print functions we know about */
1313 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
1315 #include "{module}.api.h"
1320 write(hdr.format(module=module))
1321 write('static u16\n')
1322 write('setup_message_id_table (void) {\n')
1323 write(' api_main_t *am = my_api_main;\n')
1324 write(' vl_msg_api_msg_config_t c;\n')
1325 write(' u16 msg_id_base = vl_msg_api_get_msg_ids ("{}_{crc:08x}", '
1326 'VL_MSG_{m}_LAST);\n'
1327 .format(module, crc=file_crc, m=module.upper()))
1330 write(' vl_msg_api_add_msg_name_crc (am, "{n}_{crc:08x}",\n'
1331 ' VL_API_{ID} + msg_id_base);\n'
1332 .format(n=d.name, ID=d.name.upper(), crc=d.crc))
1334 d = define_hash[s.caller]
1335 write(' c = (vl_msg_api_msg_config_t) '
1336 ' {{.id = VL_API_{ID} + msg_id_base,\n'
1338 ' .handler = vl_api_{n}_t_handler,\n'
1339 ' .cleanup = vl_noop_handler,\n'
1340 ' .endian = vl_api_{n}_t_endian,\n'
1341 ' .print = vl_api_{n}_t_print,\n'
1342 ' .is_autoendian = {auto}}};\n'
1343 .format(n=s.caller, ID=s.caller.upper(),
1345 write(' vl_msg_api_config (&c);\n')
1347 d = define_hash[s.reply]
1348 write(' c = (vl_msg_api_msg_config_t) '
1349 '{{.id = VL_API_{ID} + msg_id_base,\n'
1352 ' .cleanup = vl_noop_handler,\n'
1353 ' .endian = vl_api_{n}_t_endian,\n'
1354 ' .print = vl_api_{n}_t_print,\n'
1355 ' .is_autoendian = {auto}}};\n'
1356 .format(n=s.reply, ID=s.reply.upper(),
1358 write(' vl_msg_api_config (&c);\n')
1362 write(' return msg_id_base;\n')
1365 severity = {'error': 'VL_COUNTER_SEVERITY_ERROR',
1366 'info': 'VL_COUNTER_SEVERITY_INFO',
1367 'warn': 'VL_COUNTER_SEVERITY_WARN'}
1369 for cnt in counters:
1371 write('vl_counter_t {}_error_counters[] = {{\n'.format(csetname))
1374 write(' .name = "{}",\n'.format(c['name']))
1375 write(' .desc = "{}",\n'.format(c['description']))
1376 write(' .severity = {},\n'.format(severity[c['severity']]))
1381 def generate_c_test_boilerplate(services, defines, file_crc, module, plugin,
1383 '''Generate code for legacy style VAT. To be deleted.'''
1384 write = stream.write
1386 define_hash = {d.name: d for d in defines}
1389 #define vl_endianfun /* define message structures */
1390 #include "{module}.api.h"
1393 /* instantiate all the print functions we know about */
1394 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
1396 #include "{module}.api.h"
1401 write(hdr.format(module=module))
1404 d = define_hash[s.reply]
1409 ' * Manual definition requested for: \n'
1410 ' * vl_api_{n}_t_handler()\n'
1414 if not define_hash[s.caller].autoreply:
1415 write('/* Generation not supported (vl_api_{n}_t_handler()) */\n'
1418 write('#ifndef VL_API_{n}_T_HANDLER\n'.format(n=s.reply.upper()))
1419 write('static void\n')
1420 write('vl_api_{n}_t_handler (vl_api_{n}_t * mp) {{\n'
1422 write(' vat_main_t * vam = {}_test_main.vat_main;\n'.format(module))
1423 write(' i32 retval = ntohl(mp->retval);\n')
1424 write(' if (vam->async_mode) {\n')
1425 write(' vam->async_errors += (retval < 0);\n')
1426 write(' } else {\n')
1427 write(' vam->retval = retval;\n')
1428 write(' vam->result_ready = 1;\n')
1434 if define_hash[e].manual_print:
1436 write('static void\n')
1437 write('vl_api_{n}_t_handler (vl_api_{n}_t * mp) {{\n'.format(n=e))
1438 write(' vl_print(0, "{n} event called:");\n'.format(n=e))
1439 write(' vl_api_{n}_t_print(mp, 0);\n'.format(n=e))
1442 write('static void\n')
1443 write('setup_message_id_table (vat_main_t * vam, u16 msg_id_base) {\n')
1445 write(' vl_msg_api_set_handlers(VL_API_{ID} + msg_id_base, '
1447 ' vl_api_{n}_t_handler, '
1448 ' vl_noop_handler,\n'
1449 ' vl_api_{n}_t_endian, '
1450 ' vl_api_{n}_t_print,\n'
1451 ' sizeof(vl_api_{n}_t), 1);\n'
1452 .format(n=s.reply, ID=s.reply.upper()))
1453 write(' hash_set_mem (vam->function_by_name, "{n}", api_{n});\n'
1454 .format(n=s.caller))
1456 write(' hash_set_mem (vam->help_by_name, "{n}", "{help}");\n'
1458 help=define_hash[s.caller].options['vat_help']))
1464 write(' vl_msg_api_set_handlers(VL_API_{ID} + msg_id_base, '
1466 ' vl_api_{n}_t_handler, '
1467 ' vl_noop_handler,\n'
1468 ' vl_api_{n}_t_endian, '
1469 ' vl_api_{n}_t_print,\n'
1470 ' sizeof(vl_api_{n}_t), 1);\n'
1471 .format(n=e, ID=e.upper()))
1475 write('clib_error_t * vat_plugin_register (vat_main_t *vam)\n')
1477 write('clib_error_t * vat_{}_plugin_register (vat_main_t *vam)\n'
1480 write(' {n}_test_main_t * mainp = &{n}_test_main;\n'.format(n=module))
1481 write(' mainp->vat_main = vam;\n')
1482 write(' mainp->msg_id_base = vl_client_get_first_plugin_msg_id '
1483 ' ("{n}_{crc:08x}");\n'
1484 .format(n=module, crc=file_crc))
1485 write(' if (mainp->msg_id_base == (u16) ~0)\n')
1486 write(' return clib_error_return (0, "{} plugin not loaded...");\n'
1488 write(' setup_message_id_table (vam, mainp->msg_id_base);\n')
1489 write('#ifdef VL_API_LOCAL_SETUP_MESSAGE_ID_TABLE\n')
1490 write(' VL_API_LOCAL_SETUP_MESSAGE_ID_TABLE(vam);\n')
1492 write(' return 0;\n')
1497 '''Check if a method is generated already.'''
1498 def _f(module, d, processed, *args):
1499 if d.name in processed:
1501 processed[d.name] = True
1502 return func(module, d, *args)
1506 def c_test_api_service(s, dump, stream):
1507 '''Generate JSON code for a service.'''
1508 write = stream.write
1510 req_reply_template = '''\
1517 mp = vl_api_{n}_t_fromjson(o, &len);
1519 fprintf(stderr, "Failed converting JSON to API\\n");
1523 mp->_vl_msg_id = vac_get_msg_index(VL_API_{N}_CRC);
1524 vl_api_{n}_t_endian(mp);
1525 vac_write((char *)mp, len);
1531 vac_read(&p, &l, 5); // XXX: Fix timeout
1532 // XXX Will fail in case of event received. Do loop
1533 if (ntohs(*((u16 *)p)) != vac_get_msg_index(VL_API_{R}_CRC)) {{
1534 fprintf(stderr, "Mismatched reply\\n");
1537 vl_api_{r}_t *rmp = (vl_api_{r}_t *)p;
1538 vl_api_{r}_t_endian(rmp);
1539 return vl_api_{r}_t_tojson(rmp);
1543 dump_details_template = '''\
1547 u16 msg_id = vac_get_msg_index(VL_API_{N}_CRC);
1550 vl_api_{n}_t *mp = vl_api_{n}_t_fromjson(o, &len);
1552 fprintf(stderr, "Failed converting JSON to API\\n");
1555 mp->_vl_msg_id = msg_id;
1556 vl_api_{n}_t_endian(mp);
1557 vac_write((char *)mp, len);
1560 vat2_control_ping(123); // FIX CONTEXT
1561 cJSON *reply = cJSON_CreateArray();
1563 u16 ping_reply_msg_id = vac_get_msg_index(VL_API_CONTROL_PING_REPLY_CRC);
1564 u16 details_msg_id = vac_get_msg_index(VL_API_{R}_CRC);
1570 vac_read(&p, &l, 5); // XXX: Fix timeout
1572 /* Message can be one of [_details, control_ping_reply
1573 * or unrelated event]
1575 u16 reply_msg_id = ntohs(*((u16 *)p));
1576 if (reply_msg_id == ping_reply_msg_id) {{
1580 if (reply_msg_id == details_msg_id) {{
1581 vl_api_{r}_t *rmp = (vl_api_{r}_t *)p;
1582 vl_api_{r}_t_endian(rmp);
1583 cJSON_AddItemToArray(reply, vl_api_{r}_t_tojson(rmp));
1590 gets_details_reply_template = '''\
1594 u16 msg_id = vac_get_msg_index(VL_API_{N}_CRC);
1597 vl_api_{n}_t *mp = vl_api_{n}_t_fromjson(o, &len);
1599 fprintf(stderr, "Failed converting JSON to API\\n");
1602 mp->_vl_msg_id = msg_id;
1604 vl_api_{n}_t_endian(mp);
1605 vac_write((char *)mp, len);
1608 cJSON *reply = cJSON_CreateArray();
1610 u16 reply_msg_id = vac_get_msg_index(VL_API_{R}_CRC);
1611 u16 details_msg_id = vac_get_msg_index(VL_API_{D}_CRC);
1617 vac_read(&p, &l, 5); // XXX: Fix timeout
1619 /* Message can be one of [_details, control_ping_reply
1620 * or unrelated event]
1622 u16 msg_id = ntohs(*((u16 *)p));
1623 if (msg_id == reply_msg_id) {{
1624 vl_api_{r}_t *rmp = (vl_api_{r}_t *)p;
1625 vl_api_{r}_t_endian(rmp);
1626 cJSON_AddItemToArray(reply, vl_api_{r}_t_tojson(rmp));
1630 if (msg_id == details_msg_id) {{
1631 vl_api_{d}_t *rmp = (vl_api_{d}_t *)p;
1632 vl_api_{d}_t_endian(rmp);
1633 cJSON_AddItemToArray(reply, vl_api_{d}_t_tojson(rmp));
1642 if s.stream_message:
1643 write(gets_details_reply_template
1644 .format(n=s.caller, r=s.reply, N=s.caller.upper(),
1645 R=s.reply.upper(), d=s.stream_message,
1646 D=s.stream_message.upper()))
1648 write(dump_details_template.format(n=s.caller, r=s.reply,
1652 write(req_reply_template.format(n=s.caller, r=s.reply,
1657 def generate_c_test2_boilerplate(services, defines, module, stream):
1658 '''Generate code for VAT2 plugin.'''
1659 write = stream.write
1661 define_hash = {d.name: d for d in defines}
1665 #include <vlibapi/api.h>
1666 #include <vlibmemory/api.h>
1667 #include <vppinfra/error.h>
1668 #include <vnet/ip/ip_format_fns.h>
1669 #include <vnet/ethernet/ethernet_format_fns.h>
1671 #define vl_typedefs /* define message structures */
1672 #include <vpp/api/vpe_all_api_h.h>
1675 #include "{module}.api_enum.h"
1676 #include "{module}.api_types.h"
1678 #define vl_endianfun /* define message structures */
1679 #include "{module}.api.h"
1682 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
1684 #include "{module}.api.h"
1687 #include "{module}.api_tojson.h"
1688 #include "{module}.api_fromjson.h"
1689 #include <vpp-api/client/vppapiclient.h>
1691 #include <vat2/vat2_helpers.h>
1695 write(hdr.format(module=module))
1698 if s.reply not in define_hash:
1700 c_test_api_service(s, s.stream, stream)
1702 write('void vat2_register_function(char *, cJSON * (*)(cJSON *));\n')
1703 # write('__attribute__((constructor))')
1704 write('clib_error_t *\n')
1705 write('vat2_register_plugin (void) {\n')
1707 write(' vat2_register_function("{n}", api_{n});\n'
1708 .format(n=s.caller))
1709 write(' return 0;\n')
1714 # Plugin entry point
1716 def run(args, apifilename, s):
1717 '''Main plugin entry point.'''
1720 if not args.outputdir:
1721 sys.stderr.write('Missing --outputdir argument')
1724 basename = os.path.basename(apifilename)
1725 filename, _ = os.path.splitext(basename)
1726 modulename = filename.replace('.', '_')
1727 filename_enum = os.path.join(args.outputdir + '/' + basename + '_enum.h')
1728 filename_types = os.path.join(args.outputdir + '/' + basename + '_types.h')
1729 filename_c = os.path.join(args.outputdir + '/' + basename + '.c')
1730 filename_c_test = os.path.join(args.outputdir + '/' + basename + '_test.c')
1731 filename_c_test2 = (os.path.join(args.outputdir + '/' + basename +
1733 filename_c_tojson = (os.path.join(args.outputdir +
1734 '/' + basename + '_tojson.h'))
1735 filename_c_fromjson = (os.path.join(args.outputdir + '/' +
1736 basename + '_fromjson.h'))
1738 # Generate separate types file
1740 generate_include_types(s, modulename, st)
1741 with open(filename_types, 'w') as fd:
1743 shutil.copyfileobj(st, fd)
1746 # Generate separate enum file
1748 st.write('#ifndef included_{}_api_enum_h\n'.format(modulename))
1749 st.write('#define included_{}_api_enum_h\n'.format(modulename))
1750 generate_include_enum(s, modulename, st)
1751 generate_include_counters(s['Counters'], st)
1752 st.write('#endif\n')
1753 with open(filename_enum, 'w') as fd:
1755 shutil.copyfileobj(st, fd)
1758 # Generate separate C file
1760 generate_c_boilerplate(s['Service'], s['Define'], s['Counters'],
1761 s['file_crc'], modulename, st)
1762 with open(filename_c, 'w') as fd:
1764 shutil.copyfileobj(st, fd)
1767 # Generate separate C test file
1769 plugin = bool('plugin' in apifilename)
1770 generate_c_test_boilerplate(s['Service'], s['Define'],
1772 modulename, plugin, st)
1773 with open(filename_c_test, 'w') as fd:
1775 shutil.copyfileobj(st, fd)
1778 # Fully autogenerated VATv2 C test file
1780 generate_c_test2_boilerplate(s['Service'], s['Define'],
1782 with open(filename_c_test2, 'w') as fd:
1784 shutil.copyfileobj(st, fd)
1787 # Generate separate JSON file
1789 generate_tojson(s, modulename, st)
1790 with open(filename_c_tojson, 'w') as fd:
1792 shutil.copyfileobj(st, fd)
1795 generate_fromjson(s, modulename, st)
1796 with open(filename_c_fromjson, 'w') as fd:
1798 shutil.copyfileobj(st, fd)
1801 output = TOP_BOILERPLATE.format(datestring=DATESTRING,
1802 input_filename=basename)
1803 output += generate_imports(s['Import'])
1804 output += msg_ids(s)
1805 output += msg_names(s)
1806 output += msg_name_crc_list(s, filename)
1807 output += typedefs(modulename)
1808 printfun_types(s['types'], stream, modulename)
1809 printfun(s['Define'], stream, modulename)
1810 output += stream.getvalue()
1812 output += endianfun(s['types'] + s['Define'], modulename)
1813 output += version_tuple(s, basename)
1814 output += BOTTOM_BOILERPLATE.format(input_filename=basename,
1815 file_crc=s['file_crc'])