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};
+ {realloc} = realloc({realloc}, {msgsize} + sizeof({t}) * size);
+ {t} *d = (void *){realloc} + {msgsize};
{msgsize} += sizeof({t}) * size;
for (i = 0; i < size; i++) {{
cJSON *e = cJSON_GetArrayItem(array, i);
lfield = 'a->' + o.lengthfield if o.lengthfield else o.length
msgvar = "(void **)&a" if toplevel else "mp"
+ realloc = "a" if toplevel else "*mp"
msgsize = "l" if toplevel else "*len"
if o.fieldtype == 'u8':
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(' {realloc} = realloc({realloc}, {msgsize} + '
+ 'vec_len(s));\n'.format(msgvar=msgvar, msgsize=msgsize, realloc=realloc))
+ write(' memcpy((void *){realloc} + {msgsize}, s, '
+ 'vec_len(s));\n'.format(realloc=realloc, msgsize=msgsize))
write(' {msgsize} += vec_len(s);\n'.format(msgsize=msgsize))
write(' vec_free(s);\n')
t=o.fieldtype,
n=o.fieldname,
call=call,
- msgvar=msgvar,
+ realloc=realloc,
msgsize=msgsize))
else:
if is_bt:
n=o.fieldname,
call=call,
msgvar=msgvar,
+ realloc=realloc,
msgsize=msgsize))
_dispatch['Array'] = print_array
def print_enum_flag(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'
+ write('static inline int vl_api_{n}_t_fromjson '
+ '(void **mp, int *len, cJSON *o, vl_api_{n}_t *a) {{\n'
.format(n=o.name))
write(' int i;\n')
write(' *a = 0;\n')
write(' for (i = 0; i < cJSON_GetArraySize(o); i++) {\n')
write(' cJSON *e = cJSON_GetArrayItem(o, i);\n')
write(' char *p = cJSON_GetStringValue(e);\n')
- write(' if (!p) return 0;\n')
+ write(' if (!p) return -1;\n')
for b in o.block:
write(' if (strcmp(p, "{}") == 0) *a |= {};\n'
.format(b[0], b[1]))
write(' }\n')
- write(' return mp;\n')
+ write(' return 0;\n')
write('}\n')
_dispatch['EnumFlag'] = print_enum_flag
write('\n item = cJSON_GetObjectItem(o, "{}");\n'
.format(t.fieldname))
write(' if (!item) goto error;\n')
-
self._dispatch[t.type](self, t)
write('\n return 0;\n')
return
t = o.using
- write('static inline void *vl_api_{name}_t_fromjson (void *mp, '
+ write('static inline int 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:
write(' vl_api_{t}_fromjson(o, ({t} *)a);\n'
.format(t=t.fieldtype))
- write(' return mp;\n')
+ write(' return 0;\n')
write('}\n')
_dispatch['Typedef'] = print_typedef
autoreply define test_string {
vl_api_test_stringtype_t str;
};
+
autoreply define test_string2 {
string str[];
};
+
+/* Test of toplevel VLA with basetype */
+autoreply define test_vla {
+ u32 count;
+ u32 vla[count];
+};
+
+/* Test of toplevel VLA with u8 */
+autoreply define test_vla2 {
+ u32 count;
+ u8 vla[count];
+};
+/* Test of toplevel VLA with user type */
+typedef test_vlatype {
+ u32 data;
+};
+autoreply define test_vla3 {
+ u32 count;
+ vl_api_test_vlatype_t vla[count];
+};
+/* Test of typedefed VLA */
+typedef test_vlatype2 {
+ u32 count;
+ u32 vla[count];
+};
+autoreply define test_vla4 {
+ vl_api_test_vlatype2_t data;
+};
+/* u8 string in typedef */
+typedef test_vlatype3 {
+ u32 count;
+ u8 vla[count];
+};
+autoreply define test_vla5 {
+ vl_api_test_vlatype3_t data;
+};
+
assert(name);
uword *p = hash_get_mem(function_by_name_tojson, name);
+ printf ("Message name: %s\n", name);
assert(p);
tojson_fn_t tojson = (tojson_fn_t)p[0];
.tojson = (tojson_fn_t) vl_api_test_string2_t_tojson,
.fromjson = (fromjson_fn_t) vl_api_test_string2_t_fromjson,
},
+ {
+ .name = "test_vla",
+ .tojson = (tojson_fn_t) vl_api_test_vla_t_tojson,
+ .fromjson = (fromjson_fn_t) vl_api_test_vla_t_fromjson,
+ },
+ {
+ .name = "test_vla2",
+ .tojson = (tojson_fn_t) vl_api_test_vla2_t_tojson,
+ .fromjson = (fromjson_fn_t) vl_api_test_vla2_t_fromjson,
+ },
+ {
+ .name = "test_vla3",
+ .tojson = (tojson_fn_t) vl_api_test_vla3_t_tojson,
+ .fromjson = (fromjson_fn_t) vl_api_test_vla3_t_fromjson,
+ },
+ {
+ .name = "test_vla4",
+ .tojson = (tojson_fn_t) vl_api_test_vla4_t_tojson,
+ .fromjson = (fromjson_fn_t) vl_api_test_vla4_t_fromjson,
+ },
+ {
+ .name = "test_vla5",
+ .tojson = (tojson_fn_t) vl_api_test_vla5_t_tojson,
+ .fromjson = (fromjson_fn_t) vl_api_test_vla5_t_fromjson,
+ },
};
struct tests tests[] = {
"type\"}}" },
{ .s =
"{\"_msgname\": \"test_string2\", \"str\": \"Test string toplevel\"}" },
+ { .s = "{\"_msgname\": \"test_vla\", \"count\": 5, \"vla\": [1,2,3,4,5]}" },
+ { .s = "{\"_msgname\": \"test_vla2\", \"count\": 5, \"vla\": "
+ "\"0xaabbccddee\"}" },
+ { .s = "{\"_msgname\": \"test_vla3\", \"count\": 2, \"vla\": [{\"data\": 1} "
+ ", {\"data\": 2} ] }" },
+ { .s = "{\"_msgname\": \"test_vla4\", \"data\": { \"count\": 5, \"vla\": "
+ "[1,2,3,4,5] }}" },
+ { .s = "{\"_msgname\": \"test_vla5\", \"data\": { \"count\": 5, \"vla\": "
+ "\"0xaabbccddee\" }}" },
};
int main (int argc, char **argv)