diff options
Diffstat (limited to 'src/tools/vppapigen/vppapigen_c.py')
-rw-r--r-- | src/tools/vppapigen/vppapigen_c.py | 1126 |
1 files changed, 1007 insertions, 119 deletions
diff --git a/src/tools/vppapigen/vppapigen_c.py b/src/tools/vppapigen/vppapigen_c.py index a8790744054..4369dd86690 100644 --- a/src/tools/vppapigen/vppapigen_c.py +++ b/src/tools/vppapigen/vppapigen_c.py @@ -1,4 +1,28 @@ -# C generation +# +# Copyright (c) 2020 Cisco and/or its affiliates. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at: +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +# +# Provide two classes FromJSON and TOJSON that converts between JSON and VPP's +# binary API format +# + +''' +This module creates C code for core VPP, VPP plugins and client side VAT and +VAT2 tests. +''' + import datetime import os import time @@ -8,10 +32,588 @@ import shutil process_imports = False -datestring = datetime.datetime.utcfromtimestamp( +############################################################################### +class ToJSON(): + '''Class to generate functions converting from VPP binary API to JSON.''' + _dispatch = {} + noprint_fields = {'_vl_msg_id': None, + 'client_index': None, + 'context': None} + is_number = {'u8': None, + 'i8': None, + 'u16': None, + 'i16': None, + 'u32': None, + 'i32': None, + 'u64': None, + 'i64': None, + 'f64': None, + } + + def __init__(self, module, types, defines, imported_types, stream): + self.stream = stream + self.module = module + self.defines = defines + self.types = types + self.types_hash = {'vl_api_'+d.name+'_t': + d for d in types + imported_types} + self.defines_hash = {d.name: d for d in defines} + + def header(self): + '''Output the top boilerplate.''' + write = self.stream.write + write('#ifndef included_{}_api_tojson_h\n'.format(self.module)) + write('#define included_{}_api_tojson_h\n'.format(self.module)) + write('#include <vppinfra/cJSON.h>\n\n') + write('#include <vat2/jsonconvert.h>\n\n') + + def footer(self): + '''Output the bottom boilerplate.''' + write = self.stream.write + write('#endif\n') + + def get_json_func(self, t): + '''Given the type, returns the function to use to create a + cJSON object''' + vt_type = None + try: + vt = self.types_hash[t] + if vt.type == 'Using' and 'length' not in vt.alias: + vt_type = vt.alias['type'] + except KeyError: + vt = t + + if t in self.is_number or vt_type in self.is_number: + return 'cJSON_AddNumberToObject', '', False + if t == 'bool': + return 'cJSON_AddBoolToObject', '', False + + # Lookup type name check if it's enum + if vt.type == 'Enum': + return '{t}_tojson'.format(t=t), '', True + return '{t}_tojson'.format(t=t), '&', True + + def get_json_array_func(self, t): + '''Given a type returns the function to create a cJSON object + for arrays.''' + if t in self.is_number: + return 'cJSON_CreateNumber', '' + if t == 'bool': + return 'cJSON_CreateBool', '' + return '{t}_tojson'.format(t=t), '&' + + def print_string(self, o): + '''Create cJSON object from vl_api_string_t''' + write = self.stream.write + if o.modern_vla: + write(' vl_api_string_cJSON_AddToObject(o, "{n}", &a->{n});\n' + .format(n=o.fieldname)) + else: + + write(' cJSON_AddStringToObject(o, "{n}", (char *)a->{n});\n' + .format(n=o.fieldname)) + + def print_field(self, o): + '''Called for every field in a typedef or define.''' + write = self.stream.write + if o.fieldname in self.noprint_fields: + return + + f, p, newobj = self.get_json_func(o.fieldtype) + + if newobj: + write(' cJSON_AddItemToObject(o, "{n}", {f}({p}a->{n}));\n' + .format(f=f, p=p, n=o.fieldname)) + else: + write(' {f}(o, "{n}", {p}a->{n});\n' + .format(f=f, p=p, n=o.fieldname)) + + _dispatch['Field'] = print_field + + def print_array(self, o): + '''Converts a VPP API array to cJSON array.''' + write = self.stream.write + + forloop = '''\ + {{ + int i; + cJSON *array = cJSON_AddArrayToObject(o, "{n}"); + for (i = 0; i < {lfield}; i++) {{ + cJSON_AddItemToArray(array, {f}({p}a->{n}[i])); + }} + }} +''' + + if o.fieldtype == 'string': + self.print_string(o) + return + + lfield = 'a->' + o.lengthfield if o.lengthfield else o.length + if o.fieldtype == 'u8': + write(' {\n') + # What is length field doing here? + write(' u8 *s = format(0, "0x%U", format_hex_bytes, ' + '&a->{n}, {lfield});\n' + .format(n=o.fieldname, lfield=lfield)) + write(' cJSON_AddStringToObject(o, "{n}", (char *)s);\n' + .format(n=o.fieldname)) + write(' vec_free(s);\n') + write(' }\n') + return + + f, p = self.get_json_array_func(o.fieldtype) + write(forloop.format(lfield=lfield, + t=o.fieldtype, + n=o.fieldname, + f=f, + p=p)) + + _dispatch['Array'] = print_array + + def print_enum(self, o): + '''Create cJSON object (string) for VPP API enum''' + write = self.stream.write + write('static inline cJSON *vl_api_{name}_t_tojson ' + '(vl_api_{name}_t a) {{\n'.format(name=o.name)) + + write(" switch(a) {\n") + for b in o.block: + write(" case %s:\n" % b[1]) + write(' return cJSON_CreateString("{}");\n'.format(b[0])) + write(' default: return cJSON_CreateString("Invalid ENUM");\n') + write(' }\n') + write(' return 0;\n') + write('}\n') + + _dispatch['Enum'] = print_enum + + def print_typedef(self, o): + '''Create cJSON (dictionary) object from VPP API typedef''' + write = self.stream.write + write('static inline cJSON *vl_api_{name}_t_tojson ' + '(vl_api_{name}_t *a) {{\n'.format(name=o.name)) + write(' cJSON *o = cJSON_CreateObject();\n') + + for t in o.block: + self._dispatch[t.type](self, t) + + write(' return o;\n') + write('}\n') + + def print_define(self, o): + '''Create cJSON (dictionary) object from VPP API define''' + write = self.stream.write + write('static inline cJSON *vl_api_{name}_t_tojson ' + '(vl_api_{name}_t *a) {{\n'.format(name=o.name)) + write(' cJSON *o = cJSON_CreateObject();\n') + write(' cJSON_AddStringToObject(o, "_msgname", "{}");\n' + .format(o.name)) + + for t in o.block: + self._dispatch[t.type](self, t) + + write(' return o;\n') + write('}\n') + + def print_using(self, o): + '''Create cJSON (dictionary) object from VPP API aliased type''' + if o.manual_print: + return + + write = self.stream.write + write('static inline cJSON *vl_api_{name}_t_tojson ' + '(vl_api_{name}_t *a) {{\n'.format(name=o.name)) + + write(' u8 *s = format(0, "%U", format_vl_api_{}_t, a);\n' + .format(o.name)) + write(' cJSON *o = cJSON_CreateString((char *)s);\n') + write(' vec_free(s);\n') + write(' return o;\n') + write('}\n') + + _dispatch['Typedef'] = print_typedef + _dispatch['Define'] = print_define + _dispatch['Using'] = print_using + _dispatch['Union'] = print_typedef + + def generate_function(self, t): + '''Main entry point''' + write = self.stream.write + if t.manual_print: + write('/* Manual print {} */\n'.format(t.name)) + return + self._dispatch[t.type](self, t) + + def generate_types(self): + '''Main entry point''' + for t in self.types: + self.generate_function(t) + + def generate_defines(self): + '''Main entry point''' + for t in self.defines: + self.generate_function(t) + + +class FromJSON(): + ''' + Parse JSON objects into VPP API binary message structures. + ''' + _dispatch = {} + noprint_fields = {'_vl_msg_id': None, + 'client_index': None, + 'context': None} + is_number = {'u8': None, + 'i8': None, + 'u16': None, + 'i16': None, + 'u32': None, + 'i32': None, + 'u64': None, + 'i64': None, + 'f64': None, + } + + def __init__(self, module, types, defines, imported_types, stream): + self.stream = stream + self.module = module + self.defines = defines + self.types = types + self.types_hash = {'vl_api_'+d.name+'_t': + d for d in types + imported_types} + self.defines_hash = {d.name: d for d in defines} + + def header(self): + '''Output the top boilerplate.''' + write = self.stream.write + write('#ifndef included_{}_api_fromjson_h\n'.format(self.module)) + write('#define included_{}_api_fromjson_h\n'.format(self.module)) + write('#include <vppinfra/cJSON.h>\n\n') + write('#include <vat2/jsonconvert.h>\n\n') + + def is_base_type(self, t): + '''Check if a type is one of the VPP API base types''' + if t in self.is_number: + return True + if t == 'bool': + return True + return False + + def footer(self): + '''Output the bottom boilerplate.''' + write = self.stream.write + write('#endif\n') + + def print_string(self, o, toplevel=False): + '''Convert JSON string to vl_api_string_t''' + write = self.stream.write + + msgvar = "a" if toplevel else "mp" + msgsize = "l" if toplevel else "*len" + + if o.modern_vla: + write(' char *p = cJSON_GetStringValue(item);\n') + write(' size_t plen = strlen(p);\n') + write(' {msgvar} = realloc({msgvar}, {msgsize} + plen);\n' + .format(msgvar=msgvar, msgsize=msgsize)) + write(' vl_api_c_string_to_api_string(p, (void *){msgvar} + ' + '{msgsize} - sizeof(vl_api_string_t));\n' + .format(msgvar=msgvar, msgsize=msgsize)) + write(' {msgsize} += plen;\n'.format(msgsize=msgsize)) + else: + write(' strncpy_s((char *)a->{n}, sizeof(a->{n}), ' + 'cJSON_GetStringValue(item), sizeof(a->{n}) - 1);\n' + .format(n=o.fieldname)) + + def print_field(self, o, toplevel=False): + '''Called for every field in a typedef or define.''' + write = self.stream.write + write(' // start field {}\n'.format(o.fieldname)) + if o.fieldname in self.noprint_fields: + return + is_bt = self.is_base_type(o.fieldtype) + t = 'vl_api_{}'.format(o.fieldtype) if is_bt else o.fieldtype + + msgvar = "a" if toplevel else "mp" + msgsize = "&l" if toplevel else "len" + + if is_bt: + write(' vl_api_{t}_fromjson(item, &a->{n});\n' + .format(t=o.fieldtype, n=o.fieldname)) + else: + write(' {msgvar} = {t}_fromjson({msgvar}, ' + '{msgsize}, item, &a->{n});\n' + .format(t=t, n=o.fieldname, msgvar=msgvar, msgsize=msgsize)) + write(' if (!{msgvar}) return 0;\n'.format(msgvar=msgvar)) + + write(' // end field {}\n'.format(o.fieldname)) + + _dispatch['Field'] = print_field + + def print_array(self, o, toplevel=False): + '''Convert JSON array to VPP API array''' + write = self.stream.write + + forloop = '''\ + {{ + int i; + cJSON *array = cJSON_GetObjectItem(o, "{n}"); + int size = cJSON_GetArraySize(array); + if (size != {lfield}) return 0; + for (i = 0; i < size; i++) {{ + cJSON *e = cJSON_GetArrayItem(array, i); + {call} + }} + }} +''' + forloop_vla = '''\ + {{ + int i; + cJSON *array = cJSON_GetObjectItem(o, "{n}"); + int size = cJSON_GetArraySize(array); + {lfield} = size; + {msgvar} = realloc({msgvar}, {msgsize} + sizeof({t}) * size); + {t} *d = (void *){msgvar} + {msgsize}; + {msgsize} += sizeof({t}) * size; + for (i = 0; i < size; i++) {{ + cJSON *e = cJSON_GetArrayItem(array, i); + {call} + }} + }} +''' + t = o.fieldtype + if o.fieldtype == 'string': + self.print_string(o, toplevel) + return + + lfield = 'a->' + o.lengthfield if o.lengthfield else o.length + msgvar = "a" if toplevel else "mp" + msgsize = "l" if toplevel else "*len" + + if o.fieldtype == 'u8': + if o.lengthfield: + write(' s = u8string_fromjson(o, "{}");\n' + .format(o.fieldname)) + write(' if (!s) return 0;\n') + write(' {} = vec_len(s);\n'.format(lfield)) + + write(' {msgvar} = realloc({msgvar}, {msgsize} + ' + 'vec_len(s));\n'.format(msgvar=msgvar, msgsize=msgsize)) + write(' memcpy((void *){msgvar} + {msgsize}, s, ' + 'vec_len(s));\n'.format(msgvar=msgvar, msgsize=msgsize)) + write(' {msgsize} += vec_len(s);\n'.format(msgsize=msgsize)) + + write(' vec_free(s);\n') + else: + write(' u8string_fromjson2(o, "{n}", a->{n});\n' + .format(n=o.fieldname)) + return + + is_bt = self.is_base_type(o.fieldtype) + + if o.lengthfield: + if is_bt: + call = ('vl_api_{t}_fromjson(e, &d[i]);' + .format(t=o.fieldtype)) + else: + call = ('{t}_fromjson({msgvar}, len, e, &d[i]); ' + .format(t=o.fieldtype, msgvar=msgvar)) + write(forloop_vla.format(lfield=lfield, + t=o.fieldtype, + n=o.fieldname, + call=call, + msgvar=msgvar, + msgsize=msgsize)) + else: + if is_bt: + call = ('vl_api_{t}_fromjson(e, &a->{n}[i]);' + .format(t=t, n=o.fieldname)) + else: + call = ('a = {}_fromjson({}, len, e, &a->{}[i]);' + .format(t, msgvar, o.fieldname)) + write(forloop.format(lfield=lfield, + t=t, + n=o.fieldname, + call=call, + msgvar=msgvar, + msgsize=msgsize)) + + _dispatch['Array'] = print_array + + def print_enum(self, o): + '''Convert to JSON enum(string) to VPP API enum (int)''' + write = self.stream.write + write('static inline void *vl_api_{n}_t_fromjson ' + '(void *mp, int *len, cJSON *o, vl_api_{n}_t *a) {{\n' + .format(n=o.name)) + write(' char *p = cJSON_GetStringValue(o);\n') + for b in o.block: + write(' if (strcmp(p, "{}") == 0) {{*a = {}; return mp;}}\n' + .format(b[0], b[1])) + write(' return 0;\n') + write('}\n') + + _dispatch['Enum'] = print_enum + + def print_typedef(self, o): + '''Convert from JSON object to VPP API binary representation''' + write = self.stream.write + + write('static inline void *vl_api_{name}_t_fromjson (void *mp, ' + 'int *len, cJSON *o, vl_api_{name}_t *a) {{\n' + .format(name=o.name)) + write(' cJSON *item __attribute__ ((unused));\n') + write(' u8 *s __attribute__ ((unused));\n') + for t in o.block: + if t.type == 'Field' and t.is_lengthfield: + continue + write(' item = cJSON_GetObjectItem(o, "{}");\n' + .format(t.fieldname)) + write(' if (!item) return 0;\n') + + self._dispatch[t.type](self, t) + + write(' return mp;\n') + write('}\n') + + def print_union(self, o): + '''Convert JSON object to VPP API binary union''' + write = self.stream.write + + write('static inline void *vl_api_{name}_t_fromjson (void *mp, ' + 'int *len, cJSON *o, vl_api_{name}_t *a) {{\n' + .format(name=o.name)) + write(' cJSON *item __attribute__ ((unused));\n') + write(' u8 *s __attribute__ ((unused));\n') + for t in o.block: + if t.type == 'Field' and t.is_lengthfield: + continue + write(' item = cJSON_GetObjectItem(o, "{}");\n' + .format(t.fieldname)) + write(' if (item) {\n') + self._dispatch[t.type](self, t) + write(' };\n') + write(' return mp;\n') + write('}\n') + + def print_define(self, o): + '''Convert JSON object to VPP API message''' + write = self.stream.write + write('static inline vl_api_{name}_t *vl_api_{name}_t_fromjson ' + '(cJSON *o, int *len) {{\n'.format(name=o.name)) + write(' cJSON *item __attribute__ ((unused));\n') + write(' u8 *s __attribute__ ((unused));\n') + write(' int l = sizeof(vl_api_{}_t);\n'.format(o.name)) + write(' vl_api_{}_t *a = malloc(l);\n'.format(o.name)) + + for t in o.block: + if t.fieldname in self.noprint_fields: + continue + if t.type == 'Field' and t.is_lengthfield: + continue + write(' // processing {}: {} {}\n' + .format(o.name, t.fieldtype, t.fieldname)) + + write(' item = cJSON_GetObjectItem(o, "{}");\n' + .format(t.fieldname)) + write(' if (!item) return 0;\n') + self._dispatch[t.type](self, t, toplevel=True) + write('\n') + + write('\n') + write(' *len = l;\n') + write(' return a;\n') + write('}\n') + + def print_using(self, o): + '''Convert JSON field to VPP type alias''' + write = self.stream.write + + if o.manual_print: + return + + t = o.using + write('static inline void *vl_api_{name}_t_fromjson (void *mp, ' + 'int *len, cJSON *o, vl_api_{name}_t *a) {{\n' + .format(name=o.name)) + if 'length' in o.alias: + if t.fieldtype != 'u8': + raise ValueError("Error in processing type {} for {}" + .format(t.fieldtype, o.name)) + write(' vl_api_u8_string_fromjson(o, (u8 *)a, {});\n' + .format(o.alias['length'])) + else: + write(' vl_api_{t}_fromjson(o, ({t} *)a);\n' + .format(t=t.fieldtype)) + + write(' return mp;\n') + write('}\n') + + _dispatch['Typedef'] = print_typedef + _dispatch['Define'] = print_define + _dispatch['Using'] = print_using + _dispatch['Union'] = print_union + + def generate_function(self, t): + '''Main entry point''' + write = self.stream.write + if t.manual_print: + write('/* Manual print {} */\n'.format(t.name)) + return + self._dispatch[t.type](self, t) + + def generate_types(self): + '''Main entry point''' + for t in self.types: + self.generate_function(t) + + def generate_defines(self): + '''Main entry point''' + for t in self.defines: + self.generate_function(t) + + +def generate_tojson(s, modulename, stream): + '''Generate all functions to convert from API to JSON''' + write = stream.write + + write('/* Imported API files */\n') + for i in s['Import']: + f = i.filename.replace('plugins/', '') + write('#include <{}_tojson.h>\n'.format(f)) + + pp = ToJSON(modulename, s['types'], s['Define'], s['imported']['types'], + stream) + pp.header() + pp.generate_types() + pp.generate_defines() + pp.footer() + return '' + + +def generate_fromjson(s, modulename, stream): + '''Generate all functions to convert from JSON to API''' + write = stream.write + write('/* Imported API files */\n') + for i in s['Import']: + f = i.filename.replace('plugins/', '') + write('#include <{}_fromjson.h>\n'.format(f)) + + pp = FromJSON(modulename, s['types'], s['Define'], s['imported']['types'], + stream) + pp.header() + pp.generate_types() + pp.generate_defines() + pp.footer() + + return '' + +############################################################################### + + +DATESTRING = datetime.datetime.utcfromtimestamp( int(os.environ.get('SOURCE_DATE_EPOCH', time.time()))) -input_filename = 'inputfil' -top_boilerplate = '''\ +TOP_BOILERPLATE = '''\ /* * VLIB API definitions {datestring} * Input file: {input_filename} @@ -32,7 +634,7 @@ top_boilerplate = '''\ #define VL_API_PACKED(x) x __attribute__ ((packed)) ''' -bottom_boilerplate = '''\ +BOTTOM_BOILERPLATE = '''\ /****** API CRC (whole file) *****/ #ifdef vl_api_version @@ -43,6 +645,7 @@ vl_api_version({input_filename}, {file_crc:#08x}) def msg_ids(s): + '''Generate macro to map API message id to handler''' output = '''\ /****** Message ID / handler enum ******/ @@ -59,6 +662,7 @@ def msg_ids(s): def msg_names(s): + '''Generate calls to name mapping macro''' output = '''\ /****** Message names ******/ @@ -75,6 +679,7 @@ def msg_names(s): def msg_name_crc_list(s, suffix): + '''Generate list of names to CRC mappings''' output = '''\ /****** Message name, crc list ******/ @@ -92,6 +697,7 @@ def msg_name_crc_list(s, suffix): def api2c(fieldtype): + '''Map between API type names and internal VPP type names''' mappingtable = {'string': 'vl_api_string_t', } if fieldtype in mappingtable: return mappingtable[fieldtype] @@ -99,7 +705,7 @@ def api2c(fieldtype): def typedefs(filename): - + '''Include in the main files to the types file''' output = '''\ /****** Typedefs ******/ @@ -111,7 +717,7 @@ def typedefs(filename): return output -format_strings = {'u8': '%u', +FORMAT_STRINGS = {'u8': '%u', 'bool': '%u', 'i8': '%d', 'u16': '%u', @@ -122,18 +728,20 @@ format_strings = {'u8': '%u', 'i64': '%lld', 'f64': '%.2f'} -noprint_fields = {'_vl_msg_id': None, - 'client_index': None, - 'context': None} - class Printfun(): + '''Functions for pretty printing VPP API messages''' _dispatch = {} + noprint_fields = {'_vl_msg_id': None, + 'client_index': None, + 'context': None} def __init__(self, stream): self.stream = stream - def print_string(self, o, stream): + @staticmethod + def print_string(o, stream): + '''Pretty print a vl_api_string_t''' write = stream.write if o.modern_vla: write(' if (vl_api_string_len(&a->{f}) > 0) {{\n' @@ -151,11 +759,12 @@ class Printfun(): .format(f=o.fieldname)) def print_field(self, o, stream): + '''Pretty print API field''' write = stream.write - if o.fieldname in noprint_fields: + if o.fieldname in self.noprint_fields: return - if o.fieldtype in format_strings: - f = format_strings[o.fieldtype] + if o.fieldtype in FORMAT_STRINGS: + f = FORMAT_STRINGS[o.fieldtype] write(' s = format(s, "\\n%U{n}: {f}", ' 'format_white_space, indent, a->{n});\n' .format(n=o.fieldname, f=f)) @@ -168,6 +777,7 @@ class Printfun(): _dispatch['Field'] = print_field def print_array(self, o, stream): + '''Pretty print API array''' write = stream.write forloop = '''\ @@ -185,7 +795,8 @@ class Printfun(): ''' if o.fieldtype == 'string': - return self.print_string(o, stream) + self.print_string(o, stream) + return if o.fieldtype == 'u8': if o.lengthfield: @@ -199,29 +810,33 @@ class Printfun(): return lfield = 'a->' + o.lengthfield if o.lengthfield else o.length - if o.fieldtype in format_strings: + if o.fieldtype in FORMAT_STRINGS: write(forloop_format.format(lfield=lfield, - t=format_strings[o.fieldtype], + t=FORMAT_STRINGS[o.fieldtype], n=o.fieldname)) else: write(forloop.format(lfield=lfield, t=o.fieldtype, n=o.fieldname)) _dispatch['Array'] = print_array - def print_alias(self, k, v, stream): + @staticmethod + def print_alias(k, v, stream): + '''Pretty print type alias''' write = stream.write if ('length' in v.alias and v.alias['length'] and v.alias['type'] == 'u8'): write(' return format(s, "%U", format_hex_bytes, a, {});\n' .format(v.alias['length'])) - elif v.alias['type'] in format_strings: + elif v.alias['type'] in FORMAT_STRINGS: write(' return format(s, "{}", *a);\n' - .format(format_strings[v.alias['type']])) + .format(FORMAT_STRINGS[v.alias['type']])) else: write(' return format(s, "{} (print not implemented)");\n' .format(k)) - def print_enum(self, o, stream): + @staticmethod + def print_enum(o, stream): + '''Pretty print API enum''' write = stream.write write(" switch(*a) {\n") for b in o: @@ -232,6 +847,7 @@ class Printfun(): _dispatch['Enum'] = print_enum def print_obj(self, o, stream): + '''Entry point''' write = stream.write if o.type in self._dispatch: @@ -242,6 +858,7 @@ class Printfun(): def printfun(objs, stream, modulename): + '''Main entry point for pretty print function generation''' write = stream.write h = '''\ @@ -294,6 +911,7 @@ static inline void *vl_api_{name}_t_print (vl_api_{name}_t *a, void *handle) def printfun_types(objs, stream, modulename): + '''Pretty print API types''' write = stream.write pp = Printfun(stream) @@ -345,7 +963,8 @@ static inline u8 *format_vl_api_{name}_t (u8 *s, va_list * args) write("\n#endif /* vl_printfun_types */\n") -def imports(imports): +def generate_imports(imports): + '''Add #include matching the API import statements''' output = '/* Imported API files */\n' output += '#ifndef vl_api_version\n' @@ -356,7 +975,7 @@ def imports(imports): return output -endian_strings = { +ENDIAN_STRINGS = { 'u16': 'clib_net_to_host_u16', 'u32': 'clib_net_to_host_u32', 'u64': 'clib_net_to_host_u64', @@ -368,6 +987,7 @@ endian_strings = { def endianfun_array(o): + '''Generate endian functions for arrays''' forloop = '''\ for (i = 0; i < {length}; i++) {{ a->{name}[i] = {format}(a->{name}[i]); @@ -385,10 +1005,10 @@ def endianfun_array(o): output += ' /* a->{n} = a->{n} (no-op) */\n'.format(n=o.fieldname) else: lfield = 'a->' + o.lengthfield if o.lengthfield else o.length - if o.fieldtype in endian_strings: + if o.fieldtype in ENDIAN_STRINGS: output += (forloop .format(length=lfield, - format=endian_strings[o.fieldtype], + format=ENDIAN_STRINGS[o.fieldtype], name=o.fieldname)) else: output += (forloop_format @@ -396,23 +1016,26 @@ def endianfun_array(o): name=o.fieldname)) return output -no_endian_conversion = {'client_index': None} + +NO_ENDIAN_CONVERSION = {'client_index': None} + def endianfun_obj(o): + '''Generate endian conversion function for type''' output = '' if o.type == 'Array': return endianfun_array(o) - elif o.type != 'Field': + if o.type != 'Field': output += (' s = format(s, "\\n{} {} {} (print not implemented");\n' .format(o.type, o.fieldtype, o.fieldname)) return output - if o.fieldname in no_endian_conversion: + if o.fieldname in NO_ENDIAN_CONVERSION: output += ' /* a->{n} = a->{n} (no-op) */\n'.format(n=o.fieldname) return output - if o.fieldtype in endian_strings: + if o.fieldtype in ENDIAN_STRINGS: output += (' a->{name} = {format}(a->{name});\n' .format(name=o.fieldname, - format=endian_strings[o.fieldtype])) + format=ENDIAN_STRINGS[o.fieldtype])) elif o.fieldtype.startswith('vl_api_'): output += (' {type}_endian(&a->{name});\n' .format(type=o.fieldtype, name=o.fieldname)) @@ -423,6 +1046,7 @@ def endianfun_obj(o): def endianfun(objs, modulename): + '''Main entry point for endian function generation''' output = '''\ /****** Endian swap functions *****/\n\ @@ -449,9 +1073,9 @@ static inline void vl_api_{name}_t_endian (vl_api_{name}_t *a) for t in objs: if t.__class__.__name__ == 'Enum': output += signature.format(name=t.name) - if t.enumtype in endian_strings: + if t.enumtype in ENDIAN_STRINGS: output += (' *a = {}(*a);\n' - .format(endian_strings[t.enumtype])) + .format(ENDIAN_STRINGS[t.enumtype])) else: output += (' /* a->{name} = a->{name} (no-op) */\n' .format(name=t.name)) @@ -469,9 +1093,9 @@ static inline void vl_api_{name}_t_endian (vl_api_{name}_t *a) t.alias['type'] == 'u8'): output += (' /* a->{name} = a->{name} (no-op) */\n' .format(name=t.name)) - elif t.alias['type'] in format_strings: + elif t.alias['type'] in FORMAT_STRINGS: output += (' *a = {}(*a);\n' - .format(endian_strings[t.alias['type']])) + .format(ENDIAN_STRINGS[t.alias['type']])) else: output += ' /* Not Implemented yet {} */'.format(t.name) output += '}\n\n' @@ -490,6 +1114,7 @@ static inline void vl_api_{name}_t_endian (vl_api_{name}_t *a) def version_tuple(s, module): + '''Generate semantic version string''' output = '''\ /****** Version tuple *****/ @@ -508,9 +1133,10 @@ def version_tuple(s, module): def generate_include_enum(s, module, stream): + '''Generate <name>.api_enum.h''' write = stream.write - if len(s['Define']): + if 'Define' in s: write('typedef enum {\n') for t in s['Define']: write(' VL_API_{},\n'.format(t.name.upper())) @@ -518,7 +1144,8 @@ def generate_include_enum(s, module, stream): write('}} vl_api_{}_enum_t;\n'.format(module)) -def generate_include_counters(s, module, stream): +def generate_include_counters(s, stream): + '''Include file for the counter data model types.''' write = stream.write for counters in s: @@ -530,14 +1157,11 @@ def generate_include_counters(s, module, stream): write(' {}_N_ERROR\n'.format(csetname.upper())) write('}} vl_counter_{}_enum_t;\n'.format(csetname)) - # write('extern char *{}_error_strings[];\n'.format(csetname)) - # write('extern char *{}_description_strings[];\n'.format(csetname)) write('extern vl_counter_t {}_error_counters[];\n'.format(csetname)) -# -# Generate separate API _types file. -# + def generate_include_types(s, module, stream): + '''Generate separate API _types file.''' write = stream.write write('#ifndef included_{module}_api_types_h\n'.format(module=module)) @@ -546,11 +1170,14 @@ def generate_include_types(s, module, stream): if 'version' in s['Option']: v = s['Option']['version'] (major, minor, patch) = v.split('.') - write('#define VL_API_{m}_API_VERSION_MAJOR {v}\n'.format(m=module.upper(), v=major)) - write('#define VL_API_{m}_API_VERSION_MINOR {v}\n'.format(m=module.upper(), v=minor)) - write('#define VL_API_{m}_API_VERSION_PATCH {v}\n'.format(m=module.upper(), v=patch)) - - if len(s['Import']): + write('#define VL_API_{m}_API_VERSION_MAJOR {v}\n' + .format(m=module.upper(), v=major)) + write('#define VL_API_{m}_API_VERSION_MINOR {v}\n' + .format(m=module.upper(), v=minor)) + write('#define VL_API_{m}_API_VERSION_PATCH {v}\n' + .format(m=module.upper(), v=patch)) + + if 'Import' in s: write('/* Imported API files */\n') for i in s['Import']: filename = i.filename.replace('plugins/', '') @@ -560,7 +1187,8 @@ def generate_include_types(s, module, stream): tname = o.__class__.__name__ if tname == 'Using': if 'length' in o.alias: - write('typedef %s vl_api_%s_t[%s];\n' % (o.alias['type'], o.name, o.alias['length'])) + write('typedef %s vl_api_%s_t[%s];\n' % + (o.alias['type'], o.name, o.alias['length'])) else: write('typedef %s vl_api_%s_t;\n' % (o.alias['type'], o.name)) elif tname == 'Enum': @@ -580,20 +1208,21 @@ def generate_include_types(s, module, stream): % (size1, size2, err_str)) else: if tname == 'Union': - write("typedef union __attribute__ ((packed)) _vl_api_%s {\n" % o.name) + write("typedef union __attribute__ ((packed)) _vl_api_%s {\n" + % o.name) else: write(("typedef struct __attribute__ ((packed)) _vl_api_%s {\n") - % o.name) + % o.name) for b in o.block: if b.type == 'Option': continue if b.type == 'Field': - write(" %s %s;\n" % (api2c(b.fieldtype), - b.fieldname)) + write(" %s %s;\n" % (api2c(b.fieldtype), + b.fieldname)) elif b.type == 'Array': if b.lengthfield: - write(" %s %s[0];\n" % (api2c(b.fieldtype), - b.fieldname)) + write(" %s %s[0];\n" % (api2c(b.fieldtype), + b.fieldname)) else: # Fixed length strings decay to nul terminated u8 if b.fieldtype == 'string': @@ -623,6 +1252,7 @@ def generate_include_types(s, module, stream): def generate_c_boilerplate(services, defines, counters, file_crc, module, stream): + '''VPP side plugin.''' write = stream.write define_hash = {d.name: d for d in defines} @@ -644,34 +1274,36 @@ def generate_c_boilerplate(services, defines, counters, file_crc, write('setup_message_id_table (void) {\n') write(' api_main_t *am = my_api_main;\n') write(' vl_msg_api_msg_config_t c;\n') - write(' u16 msg_id_base = vl_msg_api_get_msg_ids ("{}_{crc:08x}", VL_MSG_{m}_LAST);\n' + write(' u16 msg_id_base = vl_msg_api_get_msg_ids ("{}_{crc:08x}", ' + 'VL_MSG_{m}_LAST);\n' .format(module, crc=file_crc, m=module.upper())) - for d in defines: write(' vl_msg_api_add_msg_name_crc (am, "{n}_{crc:08x}",\n' ' VL_API_{ID} + msg_id_base);\n' .format(n=d.name, ID=d.name.upper(), crc=d.crc)) for s in services: d = define_hash[s.caller] - write(' c = (vl_msg_api_msg_config_t) {{.id = VL_API_{ID} + msg_id_base,\n' - ' .name = "{n}",\n' - ' .handler = vl_api_{n}_t_handler,\n' - ' .cleanup = vl_noop_handler,\n' - ' .endian = vl_api_{n}_t_endian,\n' - ' .print = vl_api_{n}_t_print,\n' - ' .is_autoendian = 0}};\n' + write(' c = (vl_msg_api_msg_config_t) ' + ' {{.id = VL_API_{ID} + msg_id_base,\n' + ' .name = "{n}",\n' + ' .handler = vl_api_{n}_t_handler,\n' + ' .cleanup = vl_noop_handler,\n' + ' .endian = vl_api_{n}_t_endian,\n' + ' .print = vl_api_{n}_t_print,\n' + ' .is_autoendian = 0}};\n' .format(n=s.caller, ID=s.caller.upper())) write(' vl_msg_api_config (&c);\n') try: d = define_hash[s.reply] - write(' c = (vl_msg_api_msg_config_t) {{.id = VL_API_{ID} + msg_id_base,\n' - ' .name = "{n}",\n' - ' .handler = 0,\n' - ' .cleanup = vl_noop_handler,\n' - ' .endian = vl_api_{n}_t_endian,\n' - ' .print = vl_api_{n}_t_print,\n' - ' .is_autoendian = 0}};\n' + write(' c = (vl_msg_api_msg_config_t) ' + '{{.id = VL_API_{ID} + msg_id_base,\n' + ' .name = "{n}",\n' + ' .handler = 0,\n' + ' .cleanup = vl_noop_handler,\n' + ' .endian = vl_api_{n}_t_endian,\n' + ' .print = vl_api_{n}_t_print,\n' + ' .is_autoendian = 0}};\n' .format(n=s.reply, ID=s.reply.upper())) write(' vl_msg_api_config (&c);\n') except KeyError: @@ -686,16 +1318,6 @@ def generate_c_boilerplate(services, defines, counters, file_crc, for cnt in counters: csetname = cnt.name - ''' - write('char *{}_error_strings[] = {{\n'.format(csetname)) - for c in cnt.block: - write(' "{}",\n'.format(c['name'])) - write('};\n') - write('char *{}_description_strings[] = {{\n'.format(csetname)) - for c in cnt.block: - write(' "{}",\n'.format(c['description'])) - write('};\n') - ''' write('vl_counter_t {}_error_counters[] = {{\n'.format(csetname)) for c in cnt.block: write(' {\n') @@ -705,14 +1327,16 @@ def generate_c_boilerplate(services, defines, counters, file_crc, write(' },\n') write('};\n') -def generate_c_test_boilerplate(services, defines, file_crc, module, plugin, stream): + +def generate_c_test_boilerplate(services, defines, file_crc, module, plugin, + stream): + '''Generate code for legacy style VAT. To be deleted.''' write = stream.write - define_hash = {d.name:d for d in defines} - replies = {} + define_hash = {d.name: d for d in defines} hdr = '''\ -#define vl_endianfun /* define message structures */ +#define vl_endianfun /* define message structures */ #include "{module}.api.h" #undef vl_endianfun @@ -728,19 +1352,23 @@ def generate_c_test_boilerplate(services, defines, file_crc, module, plugin, str for s in services: try: d = define_hash[s.reply] - except: + except KeyError: continue if d.manual_print: - write('/* Manual definition requested for: vl_api_{n}_t_handler() */\n' + write('/*\n' + ' * Manual definition requested for: \n' + ' * vl_api_{n}_t_handler()\n' + ' */\n' .format(n=s.reply)) continue if not define_hash[s.caller].autoreply: - write('/* Only autoreply is supported (vl_api_{n}_t_handler()) */\n' + write('/* Generation not supported (vl_api_{n}_t_handler()) */\n' .format(n=s.reply)) continue write('#ifndef VL_API_{n}_T_HANDLER\n'.format(n=s.reply.upper())) write('static void\n') - write('vl_api_{n}_t_handler (vl_api_{n}_t * mp) {{\n'.format(n=s.reply)) + write('vl_api_{n}_t_handler (vl_api_{n}_t * mp) {{\n' + .format(n=s.reply)) write(' vat_main_t * vam = {}_test_main.vat_main;\n'.format(module)) write(' i32 retval = ntohl(mp->retval);\n') write(' if (vam->async_mode) {\n') @@ -764,23 +1392,31 @@ def generate_c_test_boilerplate(services, defines, file_crc, module, plugin, str write('static void\n') write('setup_message_id_table (vat_main_t * vam, u16 msg_id_base) {\n') for s in services: - write(' vl_msg_api_set_handlers(VL_API_{ID} + msg_id_base, "{n}",\n' - ' vl_api_{n}_t_handler, vl_noop_handler,\n' - ' vl_api_{n}_t_endian, vl_api_{n}_t_print,\n' + write(' vl_msg_api_set_handlers(VL_API_{ID} + msg_id_base, ' + ' "{n}",\n' + ' vl_api_{n}_t_handler, ' + ' vl_noop_handler,\n' + ' vl_api_{n}_t_endian, ' + ' vl_api_{n}_t_print,\n' ' sizeof(vl_api_{n}_t), 1);\n' .format(n=s.reply, ID=s.reply.upper())) - write(' hash_set_mem (vam->function_by_name, "{n}", api_{n});\n'.format(n=s.caller)) + write(' hash_set_mem (vam->function_by_name, "{n}", api_{n});\n' + .format(n=s.caller)) try: write(' hash_set_mem (vam->help_by_name, "{n}", "{help}");\n' - .format(n=s.caller, help=define_hash[s.caller].options['vat_help'])) - except: + .format(n=s.caller, + help=define_hash[s.caller].options['vat_help'])) + except KeyError: pass # Events for e in s.events: - write(' vl_msg_api_set_handlers(VL_API_{ID} + msg_id_base, "{n}",\n' - ' vl_api_{n}_t_handler, vl_noop_handler,\n' - ' vl_api_{n}_t_endian, vl_api_{n}_t_print,\n' + write(' vl_msg_api_set_handlers(VL_API_{ID} + msg_id_base, ' + ' "{n}",\n' + ' vl_api_{n}_t_handler, ' + ' vl_noop_handler,\n' + ' vl_api_{n}_t_endian, ' + ' vl_api_{n}_t_print,\n' ' sizeof(vl_api_{n}_t), 1);\n' .format(n=e, ID=e.upper())) @@ -788,14 +1424,17 @@ def generate_c_test_boilerplate(services, defines, file_crc, module, plugin, str if plugin: write('clib_error_t * vat_plugin_register (vat_main_t *vam)\n') else: - write('clib_error_t * vat_{}_plugin_register (vat_main_t *vam)\n'.format(module)) + write('clib_error_t * vat_{}_plugin_register (vat_main_t *vam)\n' + .format(module)) write('{\n') write(' {n}_test_main_t * mainp = &{n}_test_main;\n'.format(n=module)) write(' mainp->vat_main = vam;\n') - write(' mainp->msg_id_base = vl_client_get_first_plugin_msg_id ("{n}_{crc:08x}");\n' + write(' mainp->msg_id_base = vl_client_get_first_plugin_msg_id ' + ' ("{n}_{crc:08x}");\n' .format(n=module, crc=file_crc)) write(' if (mainp->msg_id_base == (u16) ~0)\n') - write(' return clib_error_return (0, "{} plugin not loaded...");\n'.format(module)) + write(' return clib_error_return (0, "{} plugin not loaded...");\n' + .format(module)) write(' setup_message_id_table (vam, mainp->msg_id_base);\n') write('#ifdef VL_API_LOCAL_SETUP_MESSAGE_ID_TABLE\n') write(' VL_API_LOCAL_SETUP_MESSAGE_ID_TABLE(vam);\n') @@ -803,30 +1442,255 @@ def generate_c_test_boilerplate(services, defines, file_crc, module, plugin, str write(' return 0;\n') write('}\n') + +def apifunc(func): + '''Check if a method is generated already.''' + def _f(module, d, processed, *args): + if d.name in processed: + return None + processed[d.name] = True + return func(module, d, *args) + return _f + + +def c_test_api_service(s, dump, stream): + '''Generate JSON code for a service.''' + write = stream.write + + req_reply_template = '''\ +static cJSON * +api_{n} (cJSON *o) +{{ + vl_api_{n}_t *mp; + int len; + if (!o) return 0; + mp = vl_api_{n}_t_fromjson(o, &len); + if (!mp) {{ + fprintf(stderr, "Failed converting JSON to API\\n"); + return 0; + }} + + mp->_vl_msg_id = vac_get_msg_index(VL_API_{N}_CRC); + vl_api_{n}_t_endian(mp); + vac_write((char *)mp, len); + free(mp); + + /* Read reply */ + char *p; + int l; + vac_read(&p, &l, 5); // XXX: Fix timeout + // XXX Will fail in case of event received. Do loop + if (ntohs(*((u16 *)p)) != vac_get_msg_index(VL_API_{R}_CRC)) {{ + fprintf(stderr, "Mismatched reply\\n"); + return 0; + }} + vl_api_{r}_t *rmp = (vl_api_{r}_t *)p; + vl_api_{r}_t_endian(rmp); + return vl_api_{r}_t_tojson(rmp); +}} + +''' + dump_details_template = '''\ +static cJSON * +api_{n} (cJSON *o) +{{ + u16 msg_id = vac_get_msg_index(VL_API_{N}_CRC); + int len; + if (!o) return 0; + vl_api_{n}_t *mp = vl_api_{n}_t_fromjson(o, &len); + if (!mp) {{ + fprintf(stderr, "Failed converting JSON to API\\n"); + return 0; + }} + mp->_vl_msg_id = msg_id; + vl_api_{n}_t_endian(mp); + vac_write((char *)mp, len); + free(mp); + + vat2_control_ping(123); // FIX CONTEXT + cJSON *reply = cJSON_CreateArray(); + + u16 ping_reply_msg_id = vac_get_msg_index(VL_API_CONTROL_PING_REPLY_CRC); + u16 details_msg_id = vac_get_msg_index(VL_API_{R}_CRC); + + while (1) {{ + /* Read reply */ + char *p; + int l; + vac_read(&p, &l, 5); // XXX: Fix timeout + + /* Message can be one of [_details, control_ping_reply + * or unrelated event] + */ + u16 reply_msg_id = ntohs(*((u16 *)p)); + if (reply_msg_id == ping_reply_msg_id) {{ + break; + }} + + if (reply_msg_id == details_msg_id) {{ + vl_api_{r}_t *rmp = (vl_api_{r}_t *)p; + vl_api_{r}_t_endian(rmp); + cJSON_AddItemToArray(reply, vl_api_{r}_t_tojson(rmp)); + }} + }} + return reply; +}} + +''' + gets_details_reply_template = '''\ +static cJSON * +api_{n} (cJSON *o) +{{ + u16 msg_id = vac_get_msg_index(VL_API_{N}_CRC); + int len = 0; + if (!o) return 0; + vl_api_{n}_t *mp = vl_api_{n}_t_fromjson(o, &len); + if (!mp) {{ + fprintf(stderr, "Failed converting JSON to API\\n"); + return 0; + }} + mp->_vl_msg_id = msg_id; + + vl_api_{n}_t_endian(mp); + vac_write((char *)mp, len); + free(mp); + + cJSON *reply = cJSON_CreateArray(); + + u16 reply_msg_id = vac_get_msg_index(VL_API_{R}_CRC); + u16 details_msg_id = vac_get_msg_index(VL_API_{D}_CRC); + + while (1) {{ + /* Read reply */ + char *p; + int l; + vac_read(&p, &l, 5); // XXX: Fix timeout + + /* Message can be one of [_details, control_ping_reply + * or unrelated event] + */ + u16 msg_id = ntohs(*((u16 *)p)); + if (msg_id == reply_msg_id) {{ + vl_api_{r}_t *rmp = (vl_api_{r}_t *)p; + vl_api_{r}_t_endian(rmp); + cJSON_AddItemToArray(reply, vl_api_{r}_t_tojson(rmp)); + break; + }} + + if (msg_id == details_msg_id) {{ + vl_api_{d}_t *rmp = (vl_api_{d}_t *)p; + vl_api_{d}_t_endian(rmp); + cJSON_AddItemToArray(reply, vl_api_{d}_t_tojson(rmp)); + }} + }} + return reply; +}} + +''' + + if dump: + if s.stream_message: + write(gets_details_reply_template + .format(n=s.caller, r=s.reply, N=s.caller.upper(), + R=s.reply.upper(), d=s.stream_message, + D=s.stream_message.upper())) + else: + write(dump_details_template.format(n=s.caller, r=s.reply, + N=s.caller.upper(), + R=s.reply.upper())) + else: + write(req_reply_template.format(n=s.caller, r=s.reply, + N=s.caller.upper(), + R=s.reply.upper())) + + +def generate_c_test2_boilerplate(services, defines, module, stream): + '''Generate code for VAT2 plugin.''' + write = stream.write + + define_hash = {d.name: d for d in defines} + # replies = {} + + hdr = '''\ +#include <vlibapi/api.h> +#include <vlibmemory/api.h> +#include <vppinfra/error.h> +#include <vnet/ip/ip_format_fns.h> +#include <vnet/ethernet/ethernet_format_fns.h> + +#define vl_typedefs /* define message structures */ +#include <vpp/api/vpe_all_api_h.h> +#undef vl_typedefs + +#include "{module}.api_enum.h" +#include "{module}.api_types.h" + +#define vl_endianfun /* define message structures */ +#include "{module}.api.h" +#undef vl_endianfun + +#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__) +#define vl_printfun +#include "{module}.api.h" +#undef vl_printfun + +#include "{module}.api_tojson.h" +#include "{module}.api_fromjson.h" +#include <vpp-api/client/vppapiclient.h> + +#include <vat2/vat2_helpers.h> + +''' + + write(hdr.format(module=module)) + + for s in services: + if s.reply not in define_hash: + continue + c_test_api_service(s, s.stream, stream) + + write('void vat2_register_function(char *, cJSON * (*)(cJSON *));\n') + # write('__attribute__((constructor))') + write('clib_error_t *\n') + write('vat2_register_plugin (void) {\n') + for s in services: + write(' vat2_register_function("{n}", api_{n});\n' + .format(n=s.caller)) + write(' return 0;\n') + write('}\n') + + # # Plugin entry point # -def run(args, input_filename, s): +def run(args, apifilename, s): + '''Main plugin entry point.''' stream = StringIO() if not args.outputdir: sys.stderr.write('Missing --outputdir argument') return None - basename = os.path.basename(input_filename) - filename, file_extension = os.path.splitext(basename) + basename = os.path.basename(apifilename) + filename, _ = os.path.splitext(basename) modulename = filename.replace('.', '_') filename_enum = os.path.join(args.outputdir + '/' + basename + '_enum.h') filename_types = os.path.join(args.outputdir + '/' + basename + '_types.h') filename_c = os.path.join(args.outputdir + '/' + basename + '.c') filename_c_test = os.path.join(args.outputdir + '/' + basename + '_test.c') + filename_c_test2 = (os.path.join(args.outputdir + '/' + basename + + '_test2.c')) + filename_c_tojson = (os.path.join(args.outputdir + + '/' + basename + '_tojson.h')) + filename_c_fromjson = (os.path.join(args.outputdir + '/' + + basename + '_fromjson.h')) # Generate separate types file st = StringIO() generate_include_types(s, modulename, st) - with open (filename_types, 'w') as fd: - st.seek (0) - shutil.copyfileobj (st, fd) + with open(filename_types, 'w') as fd: + st.seek(0) + shutil.copyfileobj(st, fd) st.close() # Generate separate enum file @@ -834,35 +1698,59 @@ def run(args, input_filename, s): st.write('#ifndef included_{}_api_enum_h\n'.format(modulename)) st.write('#define included_{}_api_enum_h\n'.format(modulename)) generate_include_enum(s, modulename, st) - generate_include_counters(s['Counters'], modulename, st) + generate_include_counters(s['Counters'], st) st.write('#endif\n') - with open (filename_enum, 'w') as fd: - st.seek (0) - shutil.copyfileobj (st, fd) + with open(filename_enum, 'w') as fd: + st.seek(0) + shutil.copyfileobj(st, fd) st.close() # Generate separate C file st = StringIO() generate_c_boilerplate(s['Service'], s['Define'], s['Counters'], s['file_crc'], modulename, st) - with open (filename_c, 'w') as fd: - st.seek (0) + with open(filename_c, 'w') as fd: + st.seek(0) shutil.copyfileobj(st, fd) st.close() # Generate separate C test file st = StringIO() - plugin = True if 'plugin' in input_filename else False - generate_c_test_boilerplate(s['Service'], s['Define'], s['file_crc'], + plugin = bool('plugin' in apifilename) + generate_c_test_boilerplate(s['Service'], s['Define'], + s['file_crc'], modulename, plugin, st) - with open (filename_c_test, 'w') as fd: - st.seek (0) + with open(filename_c_test, 'w') as fd: + st.seek(0) + shutil.copyfileobj(st, fd) + st.close() + + # Fully autogenerated VATv2 C test file + st = StringIO() + generate_c_test2_boilerplate(s['Service'], s['Define'], + modulename, st) + with open(filename_c_test2, 'w') as fd: + st.seek(0) + shutil.copyfileobj(st, fd) + st.close() # + + # Generate separate JSON file + st = StringIO() + generate_tojson(s, modulename, st) + with open(filename_c_tojson, 'w') as fd: + st.seek(0) + shutil.copyfileobj(st, fd) + st.close() + st = StringIO() + generate_fromjson(s, modulename, st) + with open(filename_c_fromjson, 'w') as fd: + st.seek(0) shutil.copyfileobj(st, fd) st.close() - output = top_boilerplate.format(datestring=datestring, + output = TOP_BOILERPLATE.format(datestring=DATESTRING, input_filename=basename) - output += imports(s['Import']) + output += generate_imports(s['Import']) output += msg_ids(s) output += msg_names(s) output += msg_name_crc_list(s, filename) @@ -873,7 +1761,7 @@ def run(args, input_filename, s): stream.close() output += endianfun(s['types'] + s['Define'], modulename) output += version_tuple(s, basename) - output += bottom_boilerplate.format(input_filename=basename, + output += BOTTOM_BOILERPLATE.format(input_filename=basename, file_crc=s['file_crc']) return output |