blob: 37f5ac12447660d00acbb08d59711696447c086c [file] [log] [blame]
Ole Troan1b1ccad2019-10-25 18:30:40 +02001#!/usr/bin/env python3
Klement Sekera8f2a4ea2017-05-04 06:15:18 +02002
3import argparse
Klement Sekeraa25ce962021-11-15 15:52:37 +01004import inspect
Klement Sekera8f2a4ea2017-05-04 06:15:18 +02005import os
6import sys
7import logging
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02008from vapi_json_parser import (
9 Field,
10 Struct,
11 Enum,
12 Union,
13 Message,
14 JsonParser,
15 SimpleType,
16 StructType,
17 Alias,
18)
Klement Sekera8f2a4ea2017-05-04 06:15:18 +020019
20
21class CField(Field):
Ole Troan53fffa12018-11-13 12:36:56 +010022 def get_c_name(self):
Ole Troan8c8acc02018-11-27 10:05:23 +010023 return "vapi_type_%s" % self.name
Ole Troan53fffa12018-11-13 12:36:56 +010024
Klement Sekera8f2a4ea2017-05-04 06:15:18 +020025 def get_c_def(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020026 if self.type.get_c_name() == "vl_api_string_t":
Ole Troane5ff5a32019-08-23 22:55:18 +020027 if self.len:
28 return "u8 %s[%d];" % (self.name, self.len)
29 else:
30 return "vl_api_string_t %s;" % (self.name)
Klement Sekera8f2a4ea2017-05-04 06:15:18 +020031 else:
Paul Vinciguerrad7a32eb2020-05-01 10:09:58 -040032 if self.len is not None and type(self.len) != dict:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020033 return "%s %s[%d];" % (self.type.get_c_name(), self.name, self.len)
Ole Troane5ff5a32019-08-23 22:55:18 +020034 else:
35 return "%s %s;" % (self.type.get_c_name(), self.name)
Klement Sekera8f2a4ea2017-05-04 06:15:18 +020036
37 def get_swap_to_be_code(self, struct, var):
Paul Vinciguerrad7a32eb2020-05-01 10:09:58 -040038 if self.len is not None and type(self.len) != dict:
Klement Sekera8f2a4ea2017-05-04 06:15:18 +020039 if self.len > 0:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020040 return (
41 "do { unsigned i; for (i = 0; i < %d; ++i) { %s } }"
42 " while(0);"
43 % (self.len, self.type.get_swap_to_be_code(struct, "%s[i]" % var))
44 )
Klement Sekera8f2a4ea2017-05-04 06:15:18 +020045 else:
46 if self.nelem_field.needs_byte_swap():
47 nelem_field = "%s(%s%s)" % (
48 self.nelem_field.type.get_swap_to_host_func_name(),
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020049 struct,
50 self.nelem_field.name,
51 )
Klement Sekera8f2a4ea2017-05-04 06:15:18 +020052 else:
53 nelem_field = "%s%s" % (struct, self.nelem_field.name)
54 return (
Klement Sekeradc15be22017-06-12 06:49:33 +020055 "do { unsigned i; for (i = 0; i < %s; ++i) { %s } }"
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020056 " while(0);"
57 % (
58 nelem_field,
59 self.type.get_swap_to_be_code(struct, "%s[i]" % var),
60 )
61 )
Klement Sekera8f2a4ea2017-05-04 06:15:18 +020062 return self.type.get_swap_to_be_code(struct, "%s" % var)
63
64 def get_swap_to_host_code(self, struct, var):
Paul Vinciguerrad7a32eb2020-05-01 10:09:58 -040065 if self.len is not None and type(self.len) != dict:
Klement Sekera8f2a4ea2017-05-04 06:15:18 +020066 if self.len > 0:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020067 return (
68 "do { unsigned i; for (i = 0; i < %d; ++i) { %s } }"
69 " while(0);"
70 % (self.len, self.type.get_swap_to_host_code(struct, "%s[i]" % var))
71 )
Klement Sekera8f2a4ea2017-05-04 06:15:18 +020072 else:
73 # nelem_field already swapped to host here...
74 return (
Klement Sekeradc15be22017-06-12 06:49:33 +020075 "do { unsigned i; for (i = 0; i < %s%s; ++i) { %s } }"
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020076 " while(0);"
77 % (
78 struct,
79 self.nelem_field.name,
80 self.type.get_swap_to_host_code(struct, "%s[i]" % var),
81 )
82 )
Klement Sekera8f2a4ea2017-05-04 06:15:18 +020083 return self.type.get_swap_to_host_code(struct, "%s" % var)
84
85 def needs_byte_swap(self):
86 return self.type.needs_byte_swap()
87
Klement Sekera34a962b2018-09-06 19:31:36 +020088 def get_vla_field_length_name(self, path):
89 return "%s_%s_array_size" % ("_".join(path), self.name)
90
91 def get_alloc_vla_param_names(self, path):
92 if self.is_vla():
93 result = [self.get_vla_field_length_name(path)]
94 else:
95 result = []
96 if self.type.has_vla():
97 t = self.type.get_alloc_vla_param_names(path + [self.name])
98 result.extend(t)
99 return result
100
101 def get_vla_calc_size_code(self, prefix, path):
102 if self.is_vla():
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200103 result = [
104 "sizeof(%s.%s[0]) * %s"
105 % (
106 ".".join([prefix] + path),
107 self.name,
108 self.get_vla_field_length_name(path),
109 )
110 ]
Klement Sekera34a962b2018-09-06 19:31:36 +0200111 else:
112 result = []
113 if self.type.has_vla():
114 t = self.type.get_vla_calc_size_code(prefix, path + [self.name])
115 result.extend(t)
116 return result
117
118 def get_vla_assign_code(self, prefix, path):
119 result = []
120 if self.is_vla():
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200121 result.append(
122 "%s.%s = %s"
123 % (
124 ".".join([prefix] + path),
125 self.nelem_field.name,
126 self.get_vla_field_length_name(path),
127 )
128 )
Klement Sekera34a962b2018-09-06 19:31:36 +0200129 if self.type.has_vla():
130 t = self.type.get_vla_assign_code(prefix, path + [self.name])
131 result.extend(t)
132 return result
133
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200134
Ole Troan53fffa12018-11-13 12:36:56 +0100135class CAlias(CField):
136 def get_c_name(self):
Ole Troan8c8acc02018-11-27 10:05:23 +0100137 return "vapi_type_%s" % self.name
Ole Troan53fffa12018-11-13 12:36:56 +0100138
139 def get_c_def(self):
Ole Troan8c8acc02018-11-27 10:05:23 +0100140 if self.len is not None:
Klement Sekera173e20c2018-12-12 12:57:32 +0100141 return "typedef %s vapi_type_%s[%d];" % (
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200142 self.type.get_c_name(),
143 self.name,
144 self.len,
145 )
Ole Troan8c8acc02018-11-27 10:05:23 +0100146 else:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200147 return "typedef %s vapi_type_%s;" % (self.type.get_c_name(), self.name)
Ole Troan53fffa12018-11-13 12:36:56 +0100148
149
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200150class CStruct(Struct):
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200151 def get_c_def(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200152 return "\n".join(
153 [
154 "typedef struct __attribute__((__packed__)) {\n%s"
155 % ("\n".join([" %s" % x.get_c_def() for x in self.fields])),
156 "} %s;" % self.get_c_name(),
157 ]
158 )
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200159
Klement Sekera34a962b2018-09-06 19:31:36 +0200160 def get_vla_assign_code(self, prefix, path):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200161 return [
162 x
163 for f in self.fields
164 if f.has_vla()
165 for x in f.get_vla_assign_code(prefix, path)
166 ]
Klement Sekera34a962b2018-09-06 19:31:36 +0200167
168 def get_alloc_vla_param_names(self, path):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200169 return [
170 x
171 for f in self.fields
172 if f.has_vla()
173 for x in f.get_alloc_vla_param_names(path)
174 ]
Klement Sekera34a962b2018-09-06 19:31:36 +0200175
176 def get_vla_calc_size_code(self, prefix, path):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200177 return [
178 x
179 for f in self.fields
180 if f.has_vla()
181 for x in f.get_vla_calc_size_code(prefix, path)
182 ]
Klement Sekera34a962b2018-09-06 19:31:36 +0200183
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200184
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200185class CSimpleType(SimpleType):
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200186 swap_to_be_dict = {
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200187 "i16": "htobe16",
188 "u16": "htobe16",
189 "i32": "htobe32",
190 "u32": "htobe32",
191 "i64": "htobe64",
192 "u64": "htobe64",
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200193 }
194
195 swap_to_host_dict = {
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200196 "i16": "be16toh",
197 "u16": "be16toh",
198 "i32": "be32toh",
199 "u32": "be32toh",
200 "i64": "be64toh",
201 "u64": "be64toh",
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200202 }
203
Neale Rannsb5c0d352020-04-22 16:06:45 +0000204 __packed = "__attribute__((packed))"
205 pack_dict = {
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200206 "i8": __packed,
207 "u8": __packed,
208 "i16": __packed,
209 "u16": __packed,
Neale Rannsb5c0d352020-04-22 16:06:45 +0000210 }
211
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200212 def get_c_name(self):
213 return self.name
214
215 def get_swap_to_be_func_name(self):
216 return self.swap_to_be_dict[self.name]
217
218 def get_swap_to_host_func_name(self):
219 return self.swap_to_host_dict[self.name]
220
Neale Rannsb5c0d352020-04-22 16:06:45 +0000221 def get_packed_string(self):
222 return self.pack_dict[self.name]
223
Klement Sekera2108c0c2018-08-24 11:43:20 +0200224 def get_swap_to_be_code(self, struct, var, cast=None):
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200225 x = "%s%s" % (struct, var)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200226 return "%s = %s%s(%s);" % (
227 x,
228 "(%s)" % cast if cast else "",
229 self.get_swap_to_be_func_name(),
230 x,
231 )
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200232
Klement Sekera2108c0c2018-08-24 11:43:20 +0200233 def get_swap_to_host_code(self, struct, var, cast=None):
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200234 x = "%s%s" % (struct, var)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200235 return "%s = %s%s(%s);" % (
236 x,
237 "(%s)" % cast if cast else "",
238 self.get_swap_to_host_func_name(),
239 x,
240 )
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200241
242 def needs_byte_swap(self):
243 try:
244 self.get_swap_to_host_func_name()
245 return True
Ole Troan53fffa12018-11-13 12:36:56 +0100246 except KeyError:
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200247 pass
248 return False
249
Neale Rannsb5c0d352020-04-22 16:06:45 +0000250 def get_packed(self):
251 return self.pack_dict.get(self.name, "")
252
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200253
Klement Sekera2108c0c2018-08-24 11:43:20 +0200254class CEnum(Enum):
255 def get_c_name(self):
256 return "vapi_enum_%s" % self.name
257
258 def get_c_def(self):
Neale Rannsb5c0d352020-04-22 16:06:45 +0000259 return "typedef enum {\n%s\n} %s %s;" % (
Klement Sekera2108c0c2018-08-24 11:43:20 +0200260 "\n".join([" %s = %s," % (i, j) for i, j in self.value_pairs]),
Neale Rannsb5c0d352020-04-22 16:06:45 +0000261 self.type.get_packed(),
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200262 self.get_c_name(),
Klement Sekera2108c0c2018-08-24 11:43:20 +0200263 )
264
265 def needs_byte_swap(self):
266 return self.type.needs_byte_swap()
267
268 def get_swap_to_be_code(self, struct, var):
269 return self.type.get_swap_to_be_code(struct, var, self.get_c_name())
270
271 def get_swap_to_host_code(self, struct, var):
272 return self.type.get_swap_to_host_code(struct, var, self.get_c_name())
273
274
275class CUnion(Union):
276 def get_c_name(self):
277 return "vapi_union_%s" % self.name
278
279 def get_c_def(self):
280 return "typedef union {\n%s\n} %s;" % (
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200281 "\n".join([" %s %s;" % (i.get_c_name(), j) for i, j in self.type_pairs]),
282 self.get_c_name(),
Klement Sekera2108c0c2018-08-24 11:43:20 +0200283 )
284
285 def needs_byte_swap(self):
286 return False
287
288
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200289class CStructType(StructType, CStruct):
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200290 def get_c_name(self):
291 return "vapi_type_%s" % self.name
292
293 def get_swap_to_be_func_name(self):
294 return "%s_hton" % self.get_c_name()
295
296 def get_swap_to_host_func_name(self):
297 return "%s_ntoh" % self.get_c_name()
298
299 def get_swap_to_be_func_decl(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200300 return "void %s(%s *msg)" % (self.get_swap_to_be_func_name(), self.get_c_name())
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200301
302 def get_swap_to_be_func_def(self):
303 return "%s\n{\n%s\n}" % (
304 self.get_swap_to_be_func_decl(),
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200305 "\n".join(
306 [
307 " %s" % p.get_swap_to_be_code("msg->", "%s" % p.name)
308 for p in self.fields
309 if p.needs_byte_swap()
310 ]
311 ),
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200312 )
313
314 def get_swap_to_host_func_decl(self):
315 return "void %s(%s *msg)" % (
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200316 self.get_swap_to_host_func_name(),
317 self.get_c_name(),
318 )
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200319
320 def get_swap_to_host_func_def(self):
321 return "%s\n{\n%s\n}" % (
322 self.get_swap_to_host_func_decl(),
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200323 "\n".join(
324 [
325 " %s" % p.get_swap_to_host_code("msg->", "%s" % p.name)
326 for p in self.fields
327 if p.needs_byte_swap()
328 ]
329 ),
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200330 )
331
332 def get_swap_to_be_code(self, struct, var):
333 return "%s(&%s%s);" % (self.get_swap_to_be_func_name(), struct, var)
334
335 def get_swap_to_host_code(self, struct, var):
336 return "%s(&%s%s);" % (self.get_swap_to_host_func_name(), struct, var)
337
338 def needs_byte_swap(self):
339 for f in self.fields:
340 if f.needs_byte_swap():
341 return True
342 return False
343
344
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200345class CMessage(Message):
Klement Sekera2108c0c2018-08-24 11:43:20 +0200346 def __init__(self, logger, definition, json_parser):
347 super(CMessage, self).__init__(logger, definition, json_parser)
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200348 self.payload_members = [
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200349 " %s" % p.get_c_def() for p in self.fields if p.type != self.header
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200350 ]
351
352 def has_payload(self):
353 return len(self.payload_members) > 0
354
355 def get_msg_id_name(self):
356 return "vapi_msg_id_%s" % self.name
357
358 def get_c_name(self):
359 return "vapi_msg_%s" % self.name
360
361 def get_payload_struct_name(self):
362 return "vapi_payload_%s" % self.name
363
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200364 def get_alloc_func_name(self):
365 return "vapi_alloc_%s" % self.name
366
Klement Sekeradc15be22017-06-12 06:49:33 +0200367 def get_alloc_vla_param_names(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200368 return [
369 x
370 for f in self.fields
371 if f.has_vla()
372 for x in f.get_alloc_vla_param_names([])
373 ]
Klement Sekeradc15be22017-06-12 06:49:33 +0200374
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200375 def get_alloc_func_decl(self):
376 return "%s* %s(struct vapi_ctx_s *ctx%s)" % (
377 self.get_c_name(),
378 self.get_alloc_func_name(),
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200379 "".join([", size_t %s" % n for n in self.get_alloc_vla_param_names()]),
380 )
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200381
382 def get_alloc_func_def(self):
383 extra = []
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200384 if self.header.has_field("client_index"):
385 extra.append(" msg->header.client_index = vapi_get_client_index(ctx);")
386 if self.header.has_field("context"):
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200387 extra.append(" msg->header.context = 0;")
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200388 return "\n".join(
389 [
390 "%s" % self.get_alloc_func_decl(),
391 "{",
392 " %s *msg = NULL;" % self.get_c_name(),
393 " const size_t size = sizeof(%s)%s;"
394 % (
395 self.get_c_name(),
396 "".join(
397 [
398 " + %s" % x
399 for f in self.fields
400 if f.has_vla()
401 for x in f.get_vla_calc_size_code("msg->payload", [])
402 ]
403 ),
404 ),
405 " /* cast here required to play nicely with C++ world ... */",
406 " msg = (%s*)vapi_msg_alloc(ctx, size);" % self.get_c_name(),
407 " if (!msg) {",
408 " return NULL;",
409 " }",
410 ]
411 + extra
412 + [
413 " msg->header._vl_msg_id = vapi_lookup_vl_msg_id(ctx, %s);"
414 % self.get_msg_id_name(),
415 "".join(
416 [
417 " %s;\n" % line
418 for f in self.fields
419 if f.has_vla()
420 for line in f.get_vla_assign_code("msg->payload", [])
421 ]
422 ),
423 " return msg;",
424 "}",
425 ]
426 )
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200427
428 def get_calc_msg_size_func_name(self):
429 return "vapi_calc_%s_msg_size" % self.name
430
431 def get_calc_msg_size_func_decl(self):
432 return "uword %s(%s *msg)" % (
433 self.get_calc_msg_size_func_name(),
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200434 self.get_c_name(),
435 )
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200436
437 def get_calc_msg_size_func_def(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200438 return "\n".join(
439 [
440 "%s" % self.get_calc_msg_size_func_decl(),
441 "{",
442 " return sizeof(*msg)%s;"
443 % "".join(
444 [
445 "+ msg->payload.%s * sizeof(msg->payload.%s[0])"
446 % (f.nelem_field.name, f.name)
447 for f in self.fields
448 if f.nelem_field is not None
449 ]
450 ),
451 "}",
452 ]
453 )
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200454
Klement Sekeraa25ce962021-11-15 15:52:37 +0100455 def get_verify_msg_size_func_name(self):
456 return f"vapi_verify_{self.name}_msg_size"
457
458 def get_verify_msg_size_func_decl(self):
459 return "int %s(%s *msg, uword buf_size)" % (
460 self.get_verify_msg_size_func_name(),
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200461 self.get_c_name(),
462 )
Klement Sekeraa25ce962021-11-15 15:52:37 +0100463
464 def get_verify_msg_size_func_def(self):
465 return inspect.cleandoc(
466 f"""
467 {self.get_verify_msg_size_func_decl()}
468 {{
469 if (sizeof({self.get_c_name()}) > buf_size)
470 {{
471 VAPI_ERR("Truncated '{self.name}' msg received, received %lu"
472 "bytes, expected %lu bytes.", buf_size,
473 sizeof({self.get_c_name()}));
474 return -1;
475 }}
476 if ({self.get_calc_msg_size_func_name()}(msg) > buf_size)
477 {{
478 VAPI_ERR("Truncated '{self.name}' msg received, received %lu"
479 "bytes, expected %lu bytes.", buf_size,
Stanislav Zaikin56777b92022-07-21 19:07:50 +0200480 {self.get_calc_msg_size_func_name()}(msg));
Klement Sekeraa25ce962021-11-15 15:52:37 +0100481 return -1;
482 }}
483 return 0;
484 }}
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200485 """
486 )
Klement Sekeraa25ce962021-11-15 15:52:37 +0100487
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200488 def get_c_def(self):
489 if self.has_payload():
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200490 return "\n".join(
491 [
492 "typedef struct __attribute__ ((__packed__)) {",
493 "%s " % "\n".join(self.payload_members),
494 "} %s;" % self.get_payload_struct_name(),
495 "",
496 "typedef struct __attribute__ ((__packed__)) {",
497 (
498 " %s %s;" % (self.header.get_c_name(), self.fields[0].name)
499 if self.header is not None
500 else ""
501 ),
502 " %s payload;" % self.get_payload_struct_name(),
503 "} %s;" % self.get_c_name(),
504 ]
505 )
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200506 else:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200507 return "\n".join(
508 [
509 "typedef struct __attribute__ ((__packed__)) {",
510 (
511 " %s %s;" % (self.header.get_c_name(), self.fields[0].name)
512 if self.header is not None
513 else ""
514 ),
515 "} %s;" % self.get_c_name(),
516 ]
517 )
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200518
519 def get_swap_payload_to_host_func_name(self):
520 return "%s_payload_ntoh" % self.get_c_name()
521
522 def get_swap_payload_to_be_func_name(self):
523 return "%s_payload_hton" % self.get_c_name()
524
525 def get_swap_payload_to_host_func_decl(self):
526 return "void %s(%s *payload)" % (
527 self.get_swap_payload_to_host_func_name(),
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200528 self.get_payload_struct_name(),
529 )
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200530
531 def get_swap_payload_to_be_func_decl(self):
532 return "void %s(%s *payload)" % (
533 self.get_swap_payload_to_be_func_name(),
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200534 self.get_payload_struct_name(),
535 )
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200536
537 def get_swap_payload_to_be_func_def(self):
538 return "%s\n{\n%s\n}" % (
539 self.get_swap_payload_to_be_func_decl(),
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200540 "\n".join(
541 [
542 " %s" % p.get_swap_to_be_code("payload->", "%s" % p.name)
543 for p in self.fields
544 if p.needs_byte_swap() and p.type != self.header
545 ]
546 ),
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200547 )
548
549 def get_swap_payload_to_host_func_def(self):
550 return "%s\n{\n%s\n}" % (
551 self.get_swap_payload_to_host_func_decl(),
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200552 "\n".join(
553 [
554 " %s" % p.get_swap_to_host_code("payload->", "%s" % p.name)
555 for p in self.fields
556 if p.needs_byte_swap() and p.type != self.header
557 ]
558 ),
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200559 )
560
561 def get_swap_to_host_func_name(self):
562 return "%s_ntoh" % self.get_c_name()
563
564 def get_swap_to_be_func_name(self):
565 return "%s_hton" % self.get_c_name()
566
567 def get_swap_to_host_func_decl(self):
568 return "void %s(%s *msg)" % (
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200569 self.get_swap_to_host_func_name(),
570 self.get_c_name(),
571 )
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200572
573 def get_swap_to_be_func_decl(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200574 return "void %s(%s *msg)" % (self.get_swap_to_be_func_name(), self.get_c_name())
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200575
576 def get_swap_to_be_func_def(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200577 return "\n".join(
578 [
579 "%s" % self.get_swap_to_be_func_decl(),
580 "{",
581 (
582 ' VAPI_DBG("Swapping `%s\'@%%p to big endian", msg);'
583 % self.get_c_name()
584 ),
585 " %s(&msg->header);" % self.header.get_swap_to_be_func_name()
586 if self.header is not None
587 else "",
588 " %s(&msg->payload);" % self.get_swap_payload_to_be_func_name()
589 if self.has_payload()
590 else "",
591 "}",
592 ]
593 )
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200594
595 def get_swap_to_host_func_def(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200596 return "\n".join(
597 [
598 "%s" % self.get_swap_to_host_func_decl(),
599 "{",
600 (
601 ' VAPI_DBG("Swapping `%s\'@%%p to host byte order", msg);'
602 % self.get_c_name()
603 ),
604 " %s(&msg->header);" % self.header.get_swap_to_host_func_name()
605 if self.header is not None
606 else "",
607 " %s(&msg->payload);" % self.get_swap_payload_to_host_func_name()
608 if self.has_payload()
609 else "",
610 "}",
611 ]
612 )
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200613
614 def get_op_func_name(self):
615 return "vapi_%s" % self.name
616
617 def get_op_func_decl(self):
Stanislav Zaikin56777b92022-07-21 19:07:50 +0200618 stream_param_lines = []
619 if self.has_stream_msg:
620 stream_param_lines = [
621 "vapi_error_e (*details_callback)(struct vapi_ctx_s *ctx",
622 " void *callback_ctx",
623 " vapi_error_e rv",
624 " bool is_last",
625 " %s *details)"
626 % self.stream_msg.get_payload_struct_name(),
627 "void *details_callback_ctx",
628 ]
629
630 return "vapi_error_e %s(%s)" % (
631 self.get_op_func_name(),
632 ",\n ".join(
633 [
634 "struct vapi_ctx_s *ctx",
635 "%s *msg" % self.get_c_name(),
636 "vapi_error_e (*reply_callback)(struct vapi_ctx_s *ctx",
637 " void *callback_ctx",
638 " vapi_error_e rv",
639 " bool is_last",
640 " %s *reply)"
641 % self.reply.get_payload_struct_name(),
642 ]
643 + [
644 "void *reply_callback_ctx",
645 ]
646 + stream_param_lines
647 ),
648 )
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200649
650 def get_op_func_def(self):
Stanislav Zaikin56777b92022-07-21 19:07:50 +0200651 param_check_lines = [" if (!msg || !reply_callback) {"]
652 store_request_lines = [
653 " vapi_store_request(ctx, req_context, %s, %s, "
654 % (
655 self.reply.get_msg_id_name(),
656 "VAPI_REQUEST_DUMP" if self.reply_is_stream else "VAPI_REQUEST_REG",
657 ),
658 " (vapi_cb_t)reply_callback, reply_callback_ctx);",
659 ]
660 if self.has_stream_msg:
661 param_check_lines = [
662 " if (!msg || !reply_callback || !details_callback) {"
663 ]
664 store_request_lines = [
665 f" vapi_store_request(ctx, req_context, {self.stream_msg.get_msg_id_name()}, VAPI_REQUEST_STREAM, ",
666 " (vapi_cb_t)details_callback, details_callback_ctx);",
667 f" vapi_store_request(ctx, req_context, {self.reply.get_msg_id_name()}, VAPI_REQUEST_REG, ",
668 " (vapi_cb_t)reply_callback, reply_callback_ctx);",
669 ]
670
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200671 return "\n".join(
672 [
673 "%s" % self.get_op_func_decl(),
674 "{",
Stanislav Zaikin56777b92022-07-21 19:07:50 +0200675 ]
676 + param_check_lines
677 + [
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200678 " return VAPI_EINVAL;",
679 " }",
680 " if (vapi_is_nonblocking(ctx) && vapi_requests_full(ctx)) {",
681 " return VAPI_EAGAIN;",
682 " }",
683 " vapi_error_e rv;",
684 " if (VAPI_OK != (rv = vapi_producer_lock (ctx))) {",
685 " return rv;",
686 " }",
687 " u32 req_context = vapi_gen_req_context(ctx);",
688 " msg->header.context = req_context;",
689 " %s(msg);" % self.get_swap_to_be_func_name(),
690 (
691 " if (VAPI_OK == (rv = vapi_send_with_control_ping "
692 "(ctx, msg, req_context))) {"
Stanislav Zaikin56777b92022-07-21 19:07:50 +0200693 if (self.reply_is_stream and not self.has_stream_msg)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200694 else " if (VAPI_OK == (rv = vapi_send (ctx, msg))) {"
695 ),
Stanislav Zaikin56777b92022-07-21 19:07:50 +0200696 ]
697 + store_request_lines
698 + [
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200699 " if (VAPI_OK != vapi_producer_unlock (ctx)) {",
700 " abort (); /* this really shouldn't happen */",
701 " }",
702 " if (vapi_is_nonblocking(ctx)) {",
703 " rv = VAPI_OK;",
704 " } else {",
705 " rv = vapi_dispatch(ctx);",
706 " }",
707 " } else {",
708 " %s(msg);" % self.get_swap_to_host_func_name(),
709 " if (VAPI_OK != vapi_producer_unlock (ctx)) {",
710 " abort (); /* this really shouldn't happen */",
711 " }",
712 " }",
713 " return rv;",
714 "}",
715 "",
716 ]
717 )
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200718
719 def get_event_cb_func_decl(self):
Klement Sekera2108c0c2018-08-24 11:43:20 +0200720 if not self.is_reply and not self.is_event:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200721 raise Exception("Cannot register event callback for non-reply message")
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200722 if self.has_payload():
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200723 return "\n".join(
724 [
725 "void vapi_set_%s_event_cb (" % self.get_c_name(),
726 " struct vapi_ctx_s *ctx, ",
727 (
728 " vapi_error_e (*callback)(struct vapi_ctx_s *ctx, "
729 "void *callback_ctx, %s *payload),"
730 % self.get_payload_struct_name()
731 ),
732 " void *callback_ctx)",
733 ]
734 )
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200735 else:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200736 return "\n".join(
737 [
738 "void vapi_set_%s_event_cb (" % self.get_c_name(),
739 " struct vapi_ctx_s *ctx, ",
740 " vapi_error_e (*callback)(struct vapi_ctx_s *ctx, "
741 "void *callback_ctx),",
742 " void *callback_ctx)",
743 ]
744 )
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200745
746 def get_event_cb_func_def(self):
Klement Sekera2108c0c2018-08-24 11:43:20 +0200747 if not self.is_reply and not self.is_event:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200748 raise Exception("Cannot register event callback for non-reply function")
749 return "\n".join(
750 [
751 "%s" % self.get_event_cb_func_decl(),
752 "{",
753 (
754 " vapi_set_event_cb(ctx, %s, (vapi_event_cb)callback, "
755 "callback_ctx);" % self.get_msg_id_name()
756 ),
757 "}",
758 ]
759 )
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200760
761 def get_c_metadata_struct_name(self):
762 return "__vapi_metadata_%s" % self.name
763
764 def get_c_constructor(self):
765 has_context = False
766 if self.header is not None:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200767 has_context = self.header.has_field("context")
768 return "\n".join(
769 [
770 "static void __attribute__((constructor)) __vapi_constructor_%s()"
771 % self.name,
772 "{",
773 ' static const char name[] = "%s";' % self.name,
774 ' static const char name_with_crc[] = "%s_%s";'
775 % (self.name, self.crc[2:]),
776 " static vapi_message_desc_t %s = {"
777 % self.get_c_metadata_struct_name(),
778 " name,",
779 " sizeof(name) - 1,",
780 " name_with_crc,",
781 " sizeof(name_with_crc) - 1,",
782 " true," if has_context else " false,",
783 " offsetof(%s, context)," % self.header.get_c_name()
784 if has_context
785 else " 0,",
786 (" offsetof(%s, payload)," % self.get_c_name())
787 if self.has_payload()
788 else " VAPI_INVALID_MSG_ID,",
789 " (verify_msg_size_fn_t)%s," % self.get_verify_msg_size_func_name(),
790 " (generic_swap_fn_t)%s," % self.get_swap_to_be_func_name(),
791 " (generic_swap_fn_t)%s," % self.get_swap_to_host_func_name(),
792 " VAPI_INVALID_MSG_ID,",
793 " };",
794 "",
795 " %s = vapi_register_msg(&%s);"
796 % (self.get_msg_id_name(), self.get_c_metadata_struct_name()),
797 ' VAPI_DBG("Assigned msg id %%d to %s", %s);'
798 % (self.name, self.get_msg_id_name()),
799 "}",
800 ]
801 )
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200802
803
Klement Sekera1732e472018-08-28 17:23:18 +0200804def emit_definition(parser, json_file, emitted, o):
Klement Sekera2108c0c2018-08-24 11:43:20 +0200805 if o in emitted:
806 return
Klement Sekera2108c0c2018-08-24 11:43:20 +0200807 if o.name in ("msg_header1_t", "msg_header2_t"):
808 return
809 if hasattr(o, "depends"):
810 for x in o.depends:
Klement Sekera1732e472018-08-28 17:23:18 +0200811 emit_definition(parser, json_file, emitted, x)
Klement Sekera2108c0c2018-08-24 11:43:20 +0200812 if hasattr(o, "reply"):
Klement Sekera1732e472018-08-28 17:23:18 +0200813 emit_definition(parser, json_file, emitted, o.reply)
Stanislav Zaikin56777b92022-07-21 19:07:50 +0200814 if hasattr(o, "stream_msg"):
815 emit_definition(parser, json_file, emitted, o.stream_msg)
Klement Sekera2108c0c2018-08-24 11:43:20 +0200816 if hasattr(o, "get_c_def"):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200817 if (
818 o not in parser.enums_by_json[json_file]
819 and o not in parser.types_by_json[json_file]
820 and o not in parser.unions_by_json[json_file]
821 and o.name not in parser.messages_by_json[json_file]
822 and o not in parser.aliases_by_json[json_file]
823 ):
Klement Sekera2108c0c2018-08-24 11:43:20 +0200824 return
Klement Sekera2108c0c2018-08-24 11:43:20 +0200825 guard = "defined_%s" % o.get_c_name()
826 print("#ifndef %s" % guard)
827 print("#define %s" % guard)
828 print("%s" % o.get_c_def())
829 print("")
830 function_attrs = "static inline "
831 if o.name in parser.messages_by_json[json_file]:
Klement Sekera2108c0c2018-08-24 11:43:20 +0200832 if o.has_payload():
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200833 print("%s%s" % (function_attrs, o.get_swap_payload_to_be_func_def()))
Klement Sekera2108c0c2018-08-24 11:43:20 +0200834 print("")
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200835 print("%s%s" % (function_attrs, o.get_swap_payload_to_host_func_def()))
Klement Sekera2108c0c2018-08-24 11:43:20 +0200836 print("")
837 print("%s%s" % (function_attrs, o.get_swap_to_be_func_def()))
838 print("")
839 print("%s%s" % (function_attrs, o.get_swap_to_host_func_def()))
840 print("")
841 print("%s%s" % (function_attrs, o.get_calc_msg_size_func_def()))
Klement Sekeraa25ce962021-11-15 15:52:37 +0100842 print("")
843 print("%s%s" % (function_attrs, o.get_verify_msg_size_func_def()))
Stanislav Zaikin56777b92022-07-21 19:07:50 +0200844 if not o.is_reply and not o.is_event and not o.is_stream:
Klement Sekera34a962b2018-09-06 19:31:36 +0200845 print("")
Klement Sekera2108c0c2018-08-24 11:43:20 +0200846 print("%s%s" % (function_attrs, o.get_alloc_func_def()))
847 print("")
848 print("%s%s" % (function_attrs, o.get_op_func_def()))
Klement Sekera2108c0c2018-08-24 11:43:20 +0200849 print("")
Klement Sekera34a962b2018-09-06 19:31:36 +0200850 print("%s" % o.get_c_constructor())
Stanislav Zaikin56777b92022-07-21 19:07:50 +0200851 if (o.is_reply or o.is_event) and not o.is_stream:
Klement Sekera2108c0c2018-08-24 11:43:20 +0200852 print("")
Klement Sekera34a962b2018-09-06 19:31:36 +0200853 print("%s%s;" % (function_attrs, o.get_event_cb_func_def()))
Klement Sekera2108c0c2018-08-24 11:43:20 +0200854 elif hasattr(o, "get_swap_to_be_func_def"):
855 print("%s%s" % (function_attrs, o.get_swap_to_be_func_def()))
856 print("")
857 print("%s%s" % (function_attrs, o.get_swap_to_host_func_def()))
Klement Sekera2108c0c2018-08-24 11:43:20 +0200858 print("#endif")
Klement Sekera34a962b2018-09-06 19:31:36 +0200859 print("")
Klement Sekera2108c0c2018-08-24 11:43:20 +0200860 emitted.append(o)
861
862
Klement Sekeradc15be22017-06-12 06:49:33 +0200863def gen_json_unified_header(parser, logger, j, io, name):
Damjan Marion4c64b6e2018-08-26 18:14:46 +0200864 d, f = os.path.split(j)
Klement Sekeradc15be22017-06-12 06:49:33 +0200865 logger.info("Generating header `%s'" % name)
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200866 orig_stdout = sys.stdout
867 sys.stdout = io
868 include_guard = "__included_%s" % (
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200869 j.replace(".", "_")
870 .replace("/", "_")
871 .replace("-", "_")
872 .replace("+", "_")
873 .replace("@", "_")
874 )
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200875 print("#ifndef %s" % include_guard)
876 print("#define %s" % include_guard)
877 print("")
Klement Sekeradc15be22017-06-12 06:49:33 +0200878 print("#include <stdlib.h>")
879 print("#include <stddef.h>")
880 print("#include <arpa/inet.h>")
881 print("#include <vapi/vapi_internal.h>")
882 print("#include <vapi/vapi.h>")
883 print("#include <vapi/vapi_dbg.h>")
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200884 print("")
Klement Sekeradc15be22017-06-12 06:49:33 +0200885 print("#ifdef __cplusplus")
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200886 print('extern "C" {')
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200887 print("#endif")
Filip Tehlarf0e67d72021-07-23 22:03:05 +0000888 if name == "memclnt.api.vapi.h":
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200889 print("")
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200890 print(
891 "static inline vapi_error_e vapi_send_with_control_ping "
892 "(vapi_ctx_t ctx, void * msg, u32 context);"
893 )
Dave Wallace6081dda2022-02-23 15:36:02 -0500894 elif name == "vlib.api.vapi.h":
895 print("#include <vapi/memclnt.api.vapi.h>")
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200896 else:
Florin Corasa1400ce2021-09-15 09:02:08 -0700897 print("#include <vapi/vlib.api.vapi.h>")
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200898 print("")
899 for m in parser.messages_by_json[j].values():
900 print("extern vapi_msg_id_t %s;" % m.get_msg_id_name())
901 print("")
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200902 print(
903 "#define DEFINE_VAPI_MSG_IDS_%s\\"
904 % f.replace(".", "_").replace("/", "_").replace("-", "_").upper()
905 )
906 print(
907 "\\\n".join(
908 [
909 " vapi_msg_id_t %s;" % m.get_msg_id_name()
910 for m in parser.messages_by_json[j].values()
911 ]
912 )
913 )
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200914 print("")
915 print("")
Klement Sekera2108c0c2018-08-24 11:43:20 +0200916 emitted = []
917 for e in parser.enums_by_json[j]:
Klement Sekera1732e472018-08-28 17:23:18 +0200918 emit_definition(parser, j, emitted, e)
Klement Sekera2108c0c2018-08-24 11:43:20 +0200919 for u in parser.unions_by_json[j]:
Klement Sekera1732e472018-08-28 17:23:18 +0200920 emit_definition(parser, j, emitted, u)
Ole Troan52ca7562018-03-06 17:45:32 +0100921 for t in parser.types_by_json[j]:
Klement Sekera1732e472018-08-28 17:23:18 +0200922 emit_definition(parser, j, emitted, t)
Ole Troan75761b92019-09-11 17:49:08 +0200923 for a in parser.aliases_by_json[j]:
924 emit_definition(parser, j, emitted, a)
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200925 for m in parser.messages_by_json[j].values():
Klement Sekera1732e472018-08-28 17:23:18 +0200926 emit_definition(parser, j, emitted, m)
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200927
928 print("")
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200929
Florin Corasa1400ce2021-09-15 09:02:08 -0700930 if name == "vlib.api.vapi.h":
Dave Wallace6081dda2022-02-23 15:36:02 -0500931 vapi_send_with_control_ping_function = """
932static inline vapi_error_e
933vapi_send_with_control_ping (vapi_ctx_t ctx, void *msg, u32 context)
934{
935 vapi_msg_control_ping *ping = vapi_alloc_control_ping (ctx);
936 if (!ping)
937 {
938 return VAPI_ENOMEM;
939 }
940 ping->header.context = context;
941 vapi_msg_control_ping_hton (ping);
942 return vapi_send2 (ctx, msg, ping);
943}
944"""
945 print("%s" % vapi_send_with_control_ping_function)
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200946 print("")
947
Klement Sekeradc15be22017-06-12 06:49:33 +0200948 print("#ifdef __cplusplus")
949 print("}")
950 print("#endif")
951 print("")
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200952 print("#endif")
953 sys.stdout = orig_stdout
954
955
Klement Sekeradc15be22017-06-12 06:49:33 +0200956def json_to_c_header_name(json_name):
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200957 if json_name.endswith(".json"):
958 return "%s.vapi.h" % os.path.splitext(json_name)[0]
959 raise Exception("Unexpected json name `%s'!" % json_name)
960
961
Damjan Marion4c64b6e2018-08-26 18:14:46 +0200962def gen_c_unified_headers(parser, logger, prefix, remove_path):
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200963 if prefix == "" or prefix is None:
964 prefix = ""
965 else:
966 prefix = "%s/" % prefix
967 for j in parser.json_files:
Damjan Marion4c64b6e2018-08-26 18:14:46 +0200968 if remove_path:
969 d, f = os.path.split(j)
970 else:
971 f = j
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200972 with open("%s%s" % (prefix, json_to_c_header_name(f)), "w") as io:
973 gen_json_unified_header(parser, logger, j, io, json_to_c_header_name(f))
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200974
975
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200976if __name__ == "__main__":
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200977 try:
978 verbose = int(os.getenv("V", 0))
979 except:
980 verbose = 0
981
982 if verbose >= 2:
983 log_level = 10
984 elif verbose == 1:
985 log_level = 20
986 else:
987 log_level = 40
988
989 logging.basicConfig(stream=sys.stdout, level=log_level)
990 logger = logging.getLogger("VAPI C GEN")
991 logger.setLevel(log_level)
992
Klement Sekeradc15be22017-06-12 06:49:33 +0200993 argparser = argparse.ArgumentParser(description="VPP C API generator")
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200994 argparser.add_argument(
995 "files",
996 metavar="api-file",
997 action="append",
998 type=str,
999 help="json api file" "(may be specified multiple times)",
1000 )
1001 argparser.add_argument("--prefix", action="store", default=None, help="path prefix")
1002 argparser.add_argument(
1003 "--remove-path", action="store_true", help="remove path from filename"
1004 )
Klement Sekera8f2a4ea2017-05-04 06:15:18 +02001005 args = argparser.parse_args()
1006
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001007 jsonparser = JsonParser(
1008 logger,
1009 args.files,
1010 simple_type_class=CSimpleType,
1011 enum_class=CEnum,
1012 union_class=CUnion,
1013 struct_type_class=CStructType,
1014 field_class=CField,
1015 message_class=CMessage,
1016 alias_class=CAlias,
1017 )
Klement Sekera8f2a4ea2017-05-04 06:15:18 +02001018
1019 # not using the model of having separate generated header and code files
1020 # with generated symbols present in shared library (per discussion with
1021 # Damjan), to avoid symbol version issues in .so
1022 # gen_c_headers_and_code(jsonparser, logger, args.prefix)
1023
Damjan Marion4c64b6e2018-08-26 18:14:46 +02001024 gen_c_unified_headers(jsonparser, logger, args.prefix, args.remove_path)
Klement Sekera8f2a4ea2017-05-04 06:15:18 +02001025
1026 for e in jsonparser.exceptions:
Damjan Marion4c64b6e2018-08-26 18:14:46 +02001027 logger.warning(e)