ipip: refactor ipip.api with explicit types
[vpp.git] / src / tools / vppapigen / vppapigen_c.py
index f0fb008..b34d063 100644 (file)
@@ -3,7 +3,8 @@ import datetime
 import os
 import time
 
-datestring = datetime.datetime.utcfromtimestamp(int(os.environ.get('SOURCE_DATE_EPOCH', time.time())))
+datestring = datetime.datetime.utcfromtimestamp(
+    int(os.environ.get('SOURCE_DATE_EPOCH', time.time())))
 input_filename = 'inputfil'
 top_boilerplate = '''\
 /*
@@ -12,6 +13,7 @@ top_boilerplate = '''\
  * Automatically generated: please edit the input file NOT this file!
  */
 
+#include <stdbool.h>
 #if defined(vl_msg_id)||defined(vl_union_id) \\
     || defined(vl_printfun) ||defined(vl_endianfun) \\
     || defined(vl_api_version)||defined(vl_typedefs) \\
@@ -94,6 +96,13 @@ def duplicate_wrapper_tail():
     return '#endif\n\n'
 
 
+def api2c(fieldtype):
+    mappingtable = {'string': 'vl_api_string_t', }
+    if fieldtype in mappingtable:
+        return mappingtable[fieldtype]
+    return fieldtype
+
+
 def typedefs(objs, aliases, filename):
     name = filename.replace('.', '_')
     output = '''\
@@ -119,10 +128,19 @@ def typedefs(objs, aliases, filename):
         tname = o.__class__.__name__
         output += duplicate_wrapper_head(o.name)
         if tname == 'Enum':
-            output += "typedef enum {\n"
+            if o.enumtype == 'u32':
+                output += "typedef enum {\n"
+            else:
+                output += "typedef enum __attribute__((__packed__)) {\n"
+
             for b in o.block:
                 output += "    %s = %s,\n" % (b[0], b[1])
             output += '} vl_api_%s_t;\n' % o.name
+            if o.enumtype != 'u32':
+                size1 = 'sizeof(vl_api_%s_t)' % o.name
+                size2 = 'sizeof(%s)' % o.enumtype
+                err_str = 'size of API enum %s is wrong' % o.name
+                output += 'STATIC_ASSERT(%s == %s, "%s");\n' % (size1, size2, err_str)
         else:
             if tname == 'Union':
                 output += "typedef VL_API_PACKED(union _vl_api_%s {\n" % o.name
@@ -130,12 +148,12 @@ def typedefs(objs, aliases, filename):
                 output += "typedef VL_API_PACKED(struct _vl_api_%s {\n" % o.name
             for b in o.block:
                 if b.type == 'Field':
-                    output += "    %s %s;\n" % (b.fieldtype, b.fieldname)
+                    output += "    %s %s;\n" % (api2c(b.fieldtype), b.fieldname)
                 elif b.type == 'Array':
                     if b.lengthfield:
-                        output += "    %s %s[0];\n" % (b.fieldtype, b.fieldname)
+                        output += "    %s %s[0];\n" % (api2c(b.fieldtype), b.fieldname)
                     else:
-                        output += "    %s %s[%s];\n" % (b.fieldtype, b.fieldname,
+                        output += "    %s %s[%s];\n" % (api2c(b.fieldtype), b.fieldname,
                                                         b.length)
                 else:
                     raise ValueError("Error in processing array type %s" % b)