blob: 9d1efb5e438388e06cef35860924b45f0ff12c9e [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 Sekera03e1d552023-08-06 19:33:16 +020026 if self.type.get_c_name() == "string":
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 Sekera03e1d552023-08-06 19:33:16 +020088 def get_vla_parameter_name(self, path):
Klement Sekera34a962b2018-09-06 19:31:36 +020089 return "%s_%s_array_size" % ("_".join(path), self.name)
90
Klement Sekera03e1d552023-08-06 19:33:16 +020091 def get_vla_field_name(self, path):
92 return ".".join(path + [self.nelem_field.name])
93
Klement Sekera34a962b2018-09-06 19:31:36 +020094 def get_alloc_vla_param_names(self, path):
95 if self.is_vla():
Klement Sekera03e1d552023-08-06 19:33:16 +020096 result = [self.get_vla_parameter_name(path)]
Klement Sekera34a962b2018-09-06 19:31:36 +020097 else:
98 result = []
99 if self.type.has_vla():
100 t = self.type.get_alloc_vla_param_names(path + [self.name])
101 result.extend(t)
102 return result
103
Klement Sekera03e1d552023-08-06 19:33:16 +0200104 def get_vla_calc_size_code(self, prefix, path, is_alloc):
Klement Sekera34a962b2018-09-06 19:31:36 +0200105 if self.is_vla():
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200106 result = [
107 "sizeof(%s.%s[0]) * %s"
108 % (
109 ".".join([prefix] + path),
110 self.name,
Dave Wallace940a70f2024-04-03 12:31:42 -0400111 (
112 self.get_vla_parameter_name(path)
113 if is_alloc
114 else "%s.%s" % (prefix, self.get_vla_field_name(path))
115 ),
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200116 )
117 ]
Klement Sekera34a962b2018-09-06 19:31:36 +0200118 else:
119 result = []
120 if self.type.has_vla():
Klement Sekera03e1d552023-08-06 19:33:16 +0200121 t = self.type.get_vla_calc_size_code(prefix, path + [self.name], is_alloc)
Klement Sekera34a962b2018-09-06 19:31:36 +0200122 result.extend(t)
123 return result
124
125 def get_vla_assign_code(self, prefix, path):
126 result = []
127 if self.is_vla():
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200128 result.append(
129 "%s.%s = %s"
130 % (
131 ".".join([prefix] + path),
132 self.nelem_field.name,
Klement Sekera03e1d552023-08-06 19:33:16 +0200133 self.get_vla_parameter_name(path),
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200134 )
135 )
Klement Sekera34a962b2018-09-06 19:31:36 +0200136 if self.type.has_vla():
137 t = self.type.get_vla_assign_code(prefix, path + [self.name])
138 result.extend(t)
139 return result
140
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200141
Ole Troan53fffa12018-11-13 12:36:56 +0100142class CAlias(CField):
143 def get_c_name(self):
Ole Troan8c8acc02018-11-27 10:05:23 +0100144 return "vapi_type_%s" % self.name
Ole Troan53fffa12018-11-13 12:36:56 +0100145
146 def get_c_def(self):
Ole Troan8c8acc02018-11-27 10:05:23 +0100147 if self.len is not None:
Klement Sekera173e20c2018-12-12 12:57:32 +0100148 return "typedef %s vapi_type_%s[%d];" % (
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200149 self.type.get_c_name(),
150 self.name,
151 self.len,
152 )
Ole Troan8c8acc02018-11-27 10:05:23 +0100153 else:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200154 return "typedef %s vapi_type_%s;" % (self.type.get_c_name(), self.name)
Ole Troan53fffa12018-11-13 12:36:56 +0100155
156
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200157class CStruct(Struct):
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200158 def get_c_def(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200159 return "\n".join(
160 [
161 "typedef struct __attribute__((__packed__)) {\n%s"
162 % ("\n".join([" %s" % x.get_c_def() for x in self.fields])),
163 "} %s;" % self.get_c_name(),
164 ]
165 )
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200166
Klement Sekera34a962b2018-09-06 19:31:36 +0200167 def get_vla_assign_code(self, prefix, path):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200168 return [
169 x
170 for f in self.fields
171 if f.has_vla()
172 for x in f.get_vla_assign_code(prefix, path)
173 ]
Klement Sekera34a962b2018-09-06 19:31:36 +0200174
175 def get_alloc_vla_param_names(self, path):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200176 return [
177 x
178 for f in self.fields
179 if f.has_vla()
180 for x in f.get_alloc_vla_param_names(path)
181 ]
Klement Sekera34a962b2018-09-06 19:31:36 +0200182
Klement Sekera03e1d552023-08-06 19:33:16 +0200183 def get_vla_calc_size_code(self, prefix, path, is_alloc):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200184 return [
185 x
186 for f in self.fields
187 if f.has_vla()
Klement Sekera03e1d552023-08-06 19:33:16 +0200188 for x in f.get_vla_calc_size_code(prefix, path, is_alloc)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200189 ]
Klement Sekera34a962b2018-09-06 19:31:36 +0200190
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200191
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200192class CSimpleType(SimpleType):
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200193 swap_to_be_dict = {
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200194 "i16": "htobe16",
195 "u16": "htobe16",
196 "i32": "htobe32",
197 "u32": "htobe32",
198 "i64": "htobe64",
199 "u64": "htobe64",
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200200 }
201
202 swap_to_host_dict = {
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200203 "i16": "be16toh",
204 "u16": "be16toh",
205 "i32": "be32toh",
206 "u32": "be32toh",
207 "i64": "be64toh",
208 "u64": "be64toh",
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200209 }
210
Neale Rannsb5c0d352020-04-22 16:06:45 +0000211 __packed = "__attribute__((packed))"
212 pack_dict = {
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200213 "i8": __packed,
214 "u8": __packed,
215 "i16": __packed,
216 "u16": __packed,
Neale Rannsb5c0d352020-04-22 16:06:45 +0000217 }
218
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200219 def get_c_name(self):
220 return self.name
221
222 def get_swap_to_be_func_name(self):
223 return self.swap_to_be_dict[self.name]
224
225 def get_swap_to_host_func_name(self):
226 return self.swap_to_host_dict[self.name]
227
Neale Rannsb5c0d352020-04-22 16:06:45 +0000228 def get_packed_string(self):
229 return self.pack_dict[self.name]
230
Klement Sekera2108c0c2018-08-24 11:43:20 +0200231 def get_swap_to_be_code(self, struct, var, cast=None):
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200232 x = "%s%s" % (struct, var)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200233 return "%s = %s%s(%s);" % (
234 x,
235 "(%s)" % cast if cast else "",
236 self.get_swap_to_be_func_name(),
237 x,
238 )
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200239
Klement Sekera2108c0c2018-08-24 11:43:20 +0200240 def get_swap_to_host_code(self, struct, var, cast=None):
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200241 x = "%s%s" % (struct, var)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200242 return "%s = %s%s(%s);" % (
243 x,
244 "(%s)" % cast if cast else "",
245 self.get_swap_to_host_func_name(),
246 x,
247 )
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200248
249 def needs_byte_swap(self):
250 try:
251 self.get_swap_to_host_func_name()
252 return True
Ole Troan53fffa12018-11-13 12:36:56 +0100253 except KeyError:
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200254 pass
255 return False
256
Neale Rannsb5c0d352020-04-22 16:06:45 +0000257 def get_packed(self):
258 return self.pack_dict.get(self.name, "")
259
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200260
Klement Sekera2108c0c2018-08-24 11:43:20 +0200261class CEnum(Enum):
262 def get_c_name(self):
263 return "vapi_enum_%s" % self.name
264
265 def get_c_def(self):
Neale Rannsb5c0d352020-04-22 16:06:45 +0000266 return "typedef enum {\n%s\n} %s %s;" % (
Klement Sekera2108c0c2018-08-24 11:43:20 +0200267 "\n".join([" %s = %s," % (i, j) for i, j in self.value_pairs]),
Neale Rannsb5c0d352020-04-22 16:06:45 +0000268 self.type.get_packed(),
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200269 self.get_c_name(),
Klement Sekera2108c0c2018-08-24 11:43:20 +0200270 )
271
272 def needs_byte_swap(self):
273 return self.type.needs_byte_swap()
274
275 def get_swap_to_be_code(self, struct, var):
276 return self.type.get_swap_to_be_code(struct, var, self.get_c_name())
277
278 def get_swap_to_host_code(self, struct, var):
279 return self.type.get_swap_to_host_code(struct, var, self.get_c_name())
280
281
282class CUnion(Union):
283 def get_c_name(self):
284 return "vapi_union_%s" % self.name
285
286 def get_c_def(self):
287 return "typedef union {\n%s\n} %s;" % (
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200288 "\n".join([" %s %s;" % (i.get_c_name(), j) for i, j in self.type_pairs]),
289 self.get_c_name(),
Klement Sekera2108c0c2018-08-24 11:43:20 +0200290 )
291
292 def needs_byte_swap(self):
293 return False
294
295
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200296class CStructType(StructType, CStruct):
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200297 def get_c_name(self):
Klement Sekera03e1d552023-08-06 19:33:16 +0200298 if self.name == "vl_api_string_t":
299 return "vl_api_string_t"
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200300 return "vapi_type_%s" % self.name
301
302 def get_swap_to_be_func_name(self):
303 return "%s_hton" % self.get_c_name()
304
305 def get_swap_to_host_func_name(self):
306 return "%s_ntoh" % self.get_c_name()
307
308 def get_swap_to_be_func_decl(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200309 return "void %s(%s *msg)" % (self.get_swap_to_be_func_name(), self.get_c_name())
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200310
311 def get_swap_to_be_func_def(self):
312 return "%s\n{\n%s\n}" % (
313 self.get_swap_to_be_func_decl(),
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200314 "\n".join(
315 [
316 " %s" % p.get_swap_to_be_code("msg->", "%s" % p.name)
317 for p in self.fields
318 if p.needs_byte_swap()
319 ]
320 ),
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200321 )
322
323 def get_swap_to_host_func_decl(self):
324 return "void %s(%s *msg)" % (
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200325 self.get_swap_to_host_func_name(),
326 self.get_c_name(),
327 )
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200328
329 def get_swap_to_host_func_def(self):
330 return "%s\n{\n%s\n}" % (
331 self.get_swap_to_host_func_decl(),
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200332 "\n".join(
333 [
334 " %s" % p.get_swap_to_host_code("msg->", "%s" % p.name)
335 for p in self.fields
336 if p.needs_byte_swap()
337 ]
338 ),
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200339 )
340
341 def get_swap_to_be_code(self, struct, var):
342 return "%s(&%s%s);" % (self.get_swap_to_be_func_name(), struct, var)
343
344 def get_swap_to_host_code(self, struct, var):
345 return "%s(&%s%s);" % (self.get_swap_to_host_func_name(), struct, var)
346
347 def needs_byte_swap(self):
348 for f in self.fields:
349 if f.needs_byte_swap():
350 return True
351 return False
352
353
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200354class CMessage(Message):
Klement Sekera2108c0c2018-08-24 11:43:20 +0200355 def __init__(self, logger, definition, json_parser):
356 super(CMessage, self).__init__(logger, definition, json_parser)
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200357 self.payload_members = [
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200358 " %s" % p.get_c_def() for p in self.fields if p.type != self.header
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200359 ]
360
361 def has_payload(self):
362 return len(self.payload_members) > 0
363
364 def get_msg_id_name(self):
365 return "vapi_msg_id_%s" % self.name
366
367 def get_c_name(self):
368 return "vapi_msg_%s" % self.name
369
370 def get_payload_struct_name(self):
371 return "vapi_payload_%s" % self.name
372
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200373 def get_alloc_func_name(self):
374 return "vapi_alloc_%s" % self.name
375
Klement Sekeradc15be22017-06-12 06:49:33 +0200376 def get_alloc_vla_param_names(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200377 return [
378 x
379 for f in self.fields
380 if f.has_vla()
381 for x in f.get_alloc_vla_param_names([])
382 ]
Klement Sekeradc15be22017-06-12 06:49:33 +0200383
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200384 def get_alloc_func_decl(self):
385 return "%s* %s(struct vapi_ctx_s *ctx%s)" % (
386 self.get_c_name(),
387 self.get_alloc_func_name(),
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200388 "".join([", size_t %s" % n for n in self.get_alloc_vla_param_names()]),
389 )
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200390
391 def get_alloc_func_def(self):
392 extra = []
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200393 if self.header.has_field("client_index"):
394 extra.append(" msg->header.client_index = vapi_get_client_index(ctx);")
395 if self.header.has_field("context"):
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200396 extra.append(" msg->header.context = 0;")
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200397 return "\n".join(
398 [
399 "%s" % self.get_alloc_func_decl(),
400 "{",
401 " %s *msg = NULL;" % self.get_c_name(),
402 " const size_t size = sizeof(%s)%s;"
403 % (
404 self.get_c_name(),
405 "".join(
406 [
407 " + %s" % x
408 for f in self.fields
409 if f.has_vla()
Klement Sekera03e1d552023-08-06 19:33:16 +0200410 for x in f.get_vla_calc_size_code(
411 "msg->payload", [], is_alloc=True
412 )
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200413 ]
414 ),
415 ),
416 " /* cast here required to play nicely with C++ world ... */",
417 " msg = (%s*)vapi_msg_alloc(ctx, size);" % self.get_c_name(),
418 " if (!msg) {",
419 " return NULL;",
420 " }",
421 ]
422 + extra
423 + [
424 " msg->header._vl_msg_id = vapi_lookup_vl_msg_id(ctx, %s);"
425 % self.get_msg_id_name(),
426 "".join(
427 [
428 " %s;\n" % line
429 for f in self.fields
430 if f.has_vla()
431 for line in f.get_vla_assign_code("msg->payload", [])
432 ]
433 ),
434 " return msg;",
435 "}",
436 ]
437 )
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200438
439 def get_calc_msg_size_func_name(self):
440 return "vapi_calc_%s_msg_size" % self.name
441
442 def get_calc_msg_size_func_decl(self):
443 return "uword %s(%s *msg)" % (
444 self.get_calc_msg_size_func_name(),
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200445 self.get_c_name(),
446 )
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200447
448 def get_calc_msg_size_func_def(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200449 return "\n".join(
450 [
451 "%s" % self.get_calc_msg_size_func_decl(),
452 "{",
453 " return sizeof(*msg)%s;"
454 % "".join(
455 [
Klement Sekera03e1d552023-08-06 19:33:16 +0200456 " + %s" % x
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200457 for f in self.fields
Klement Sekera03e1d552023-08-06 19:33:16 +0200458 if f.has_vla()
459 for x in f.get_vla_calc_size_code(
460 "msg->payload", [], is_alloc=False
461 )
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200462 ]
463 ),
464 "}",
465 ]
466 )
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200467
Klement Sekeraa25ce962021-11-15 15:52:37 +0100468 def get_verify_msg_size_func_name(self):
469 return f"vapi_verify_{self.name}_msg_size"
470
471 def get_verify_msg_size_func_decl(self):
472 return "int %s(%s *msg, uword buf_size)" % (
473 self.get_verify_msg_size_func_name(),
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200474 self.get_c_name(),
475 )
Klement Sekeraa25ce962021-11-15 15:52:37 +0100476
477 def get_verify_msg_size_func_def(self):
478 return inspect.cleandoc(
479 f"""
480 {self.get_verify_msg_size_func_decl()}
481 {{
482 if (sizeof({self.get_c_name()}) > buf_size)
483 {{
484 VAPI_ERR("Truncated '{self.name}' msg received, received %lu"
485 "bytes, expected %lu bytes.", buf_size,
486 sizeof({self.get_c_name()}));
487 return -1;
488 }}
489 if ({self.get_calc_msg_size_func_name()}(msg) > buf_size)
490 {{
491 VAPI_ERR("Truncated '{self.name}' msg received, received %lu"
492 "bytes, expected %lu bytes.", buf_size,
Stanislav Zaikin56777b92022-07-21 19:07:50 +0200493 {self.get_calc_msg_size_func_name()}(msg));
Klement Sekeraa25ce962021-11-15 15:52:37 +0100494 return -1;
495 }}
496 return 0;
497 }}
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200498 """
499 )
Klement Sekeraa25ce962021-11-15 15:52:37 +0100500
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200501 def get_c_def(self):
502 if self.has_payload():
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200503 return "\n".join(
504 [
505 "typedef struct __attribute__ ((__packed__)) {",
506 "%s " % "\n".join(self.payload_members),
507 "} %s;" % self.get_payload_struct_name(),
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 payload;" % self.get_payload_struct_name(),
516 "} %s;" % self.get_c_name(),
517 ]
518 )
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200519 else:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200520 return "\n".join(
521 [
522 "typedef struct __attribute__ ((__packed__)) {",
523 (
524 " %s %s;" % (self.header.get_c_name(), self.fields[0].name)
525 if self.header is not None
526 else ""
527 ),
528 "} %s;" % self.get_c_name(),
529 ]
530 )
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200531
532 def get_swap_payload_to_host_func_name(self):
533 return "%s_payload_ntoh" % self.get_c_name()
534
535 def get_swap_payload_to_be_func_name(self):
536 return "%s_payload_hton" % self.get_c_name()
537
538 def get_swap_payload_to_host_func_decl(self):
539 return "void %s(%s *payload)" % (
540 self.get_swap_payload_to_host_func_name(),
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200541 self.get_payload_struct_name(),
542 )
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200543
544 def get_swap_payload_to_be_func_decl(self):
545 return "void %s(%s *payload)" % (
546 self.get_swap_payload_to_be_func_name(),
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200547 self.get_payload_struct_name(),
548 )
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200549
550 def get_swap_payload_to_be_func_def(self):
551 return "%s\n{\n%s\n}" % (
552 self.get_swap_payload_to_be_func_decl(),
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200553 "\n".join(
554 [
555 " %s" % p.get_swap_to_be_code("payload->", "%s" % p.name)
556 for p in self.fields
557 if p.needs_byte_swap() and p.type != self.header
558 ]
559 ),
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200560 )
561
562 def get_swap_payload_to_host_func_def(self):
563 return "%s\n{\n%s\n}" % (
564 self.get_swap_payload_to_host_func_decl(),
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200565 "\n".join(
566 [
567 " %s" % p.get_swap_to_host_code("payload->", "%s" % p.name)
568 for p in self.fields
569 if p.needs_byte_swap() and p.type != self.header
570 ]
571 ),
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200572 )
573
574 def get_swap_to_host_func_name(self):
575 return "%s_ntoh" % self.get_c_name()
576
577 def get_swap_to_be_func_name(self):
578 return "%s_hton" % self.get_c_name()
579
580 def get_swap_to_host_func_decl(self):
581 return "void %s(%s *msg)" % (
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200582 self.get_swap_to_host_func_name(),
583 self.get_c_name(),
584 )
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200585
586 def get_swap_to_be_func_decl(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200587 return "void %s(%s *msg)" % (self.get_swap_to_be_func_name(), self.get_c_name())
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200588
589 def get_swap_to_be_func_def(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200590 return "\n".join(
591 [
592 "%s" % self.get_swap_to_be_func_decl(),
593 "{",
594 (
595 ' VAPI_DBG("Swapping `%s\'@%%p to big endian", msg);'
596 % self.get_c_name()
597 ),
Dave Wallace940a70f2024-04-03 12:31:42 -0400598 (
599 " %s(&msg->header);" % self.header.get_swap_to_be_func_name()
600 if self.header is not None
601 else ""
602 ),
603 (
604 " %s(&msg->payload);" % self.get_swap_payload_to_be_func_name()
605 if self.has_payload()
606 else ""
607 ),
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200608 "}",
609 ]
610 )
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200611
612 def get_swap_to_host_func_def(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200613 return "\n".join(
614 [
615 "%s" % self.get_swap_to_host_func_decl(),
616 "{",
617 (
618 ' VAPI_DBG("Swapping `%s\'@%%p to host byte order", msg);'
619 % self.get_c_name()
620 ),
Dave Wallace940a70f2024-04-03 12:31:42 -0400621 (
622 " %s(&msg->header);" % self.header.get_swap_to_host_func_name()
623 if self.header is not None
624 else ""
625 ),
626 (
627 " %s(&msg->payload);" % self.get_swap_payload_to_host_func_name()
628 if self.has_payload()
629 else ""
630 ),
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200631 "}",
632 ]
633 )
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200634
635 def get_op_func_name(self):
636 return "vapi_%s" % self.name
637
638 def get_op_func_decl(self):
Stanislav Zaikin56777b92022-07-21 19:07:50 +0200639 stream_param_lines = []
640 if self.has_stream_msg:
641 stream_param_lines = [
642 "vapi_error_e (*details_callback)(struct vapi_ctx_s *ctx",
643 " void *callback_ctx",
644 " vapi_error_e rv",
645 " bool is_last",
646 " %s *details)"
647 % self.stream_msg.get_payload_struct_name(),
648 "void *details_callback_ctx",
649 ]
650
651 return "vapi_error_e %s(%s)" % (
652 self.get_op_func_name(),
653 ",\n ".join(
654 [
655 "struct vapi_ctx_s *ctx",
656 "%s *msg" % self.get_c_name(),
657 "vapi_error_e (*reply_callback)(struct vapi_ctx_s *ctx",
658 " void *callback_ctx",
659 " vapi_error_e rv",
660 " bool is_last",
661 " %s *reply)"
662 % self.reply.get_payload_struct_name(),
663 ]
664 + [
665 "void *reply_callback_ctx",
666 ]
667 + stream_param_lines
668 ),
669 )
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200670
671 def get_op_func_def(self):
Stanislav Zaikin56777b92022-07-21 19:07:50 +0200672 param_check_lines = [" if (!msg || !reply_callback) {"]
673 store_request_lines = [
674 " vapi_store_request(ctx, req_context, %s, %s, "
675 % (
676 self.reply.get_msg_id_name(),
677 "VAPI_REQUEST_DUMP" if self.reply_is_stream else "VAPI_REQUEST_REG",
678 ),
679 " (vapi_cb_t)reply_callback, reply_callback_ctx);",
680 ]
681 if self.has_stream_msg:
682 param_check_lines = [
683 " if (!msg || !reply_callback || !details_callback) {"
684 ]
685 store_request_lines = [
686 f" vapi_store_request(ctx, req_context, {self.stream_msg.get_msg_id_name()}, VAPI_REQUEST_STREAM, ",
687 " (vapi_cb_t)details_callback, details_callback_ctx);",
688 f" vapi_store_request(ctx, req_context, {self.reply.get_msg_id_name()}, VAPI_REQUEST_REG, ",
689 " (vapi_cb_t)reply_callback, reply_callback_ctx);",
690 ]
691
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200692 return "\n".join(
693 [
694 "%s" % self.get_op_func_decl(),
695 "{",
Stanislav Zaikin56777b92022-07-21 19:07:50 +0200696 ]
697 + param_check_lines
698 + [
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200699 " return VAPI_EINVAL;",
700 " }",
701 " if (vapi_is_nonblocking(ctx) && vapi_requests_full(ctx)) {",
702 " return VAPI_EAGAIN;",
703 " }",
704 " vapi_error_e rv;",
705 " if (VAPI_OK != (rv = vapi_producer_lock (ctx))) {",
706 " return rv;",
707 " }",
708 " u32 req_context = vapi_gen_req_context(ctx);",
709 " msg->header.context = req_context;",
710 " %s(msg);" % self.get_swap_to_be_func_name(),
711 (
712 " if (VAPI_OK == (rv = vapi_send_with_control_ping "
713 "(ctx, msg, req_context))) {"
Stanislav Zaikin56777b92022-07-21 19:07:50 +0200714 if (self.reply_is_stream and not self.has_stream_msg)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200715 else " if (VAPI_OK == (rv = vapi_send (ctx, msg))) {"
716 ),
Stanislav Zaikin56777b92022-07-21 19:07:50 +0200717 ]
718 + store_request_lines
719 + [
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200720 " if (VAPI_OK != vapi_producer_unlock (ctx)) {",
721 " abort (); /* this really shouldn't happen */",
722 " }",
723 " if (vapi_is_nonblocking(ctx)) {",
724 " rv = VAPI_OK;",
725 " } else {",
726 " rv = vapi_dispatch(ctx);",
727 " }",
728 " } else {",
729 " %s(msg);" % self.get_swap_to_host_func_name(),
730 " if (VAPI_OK != vapi_producer_unlock (ctx)) {",
731 " abort (); /* this really shouldn't happen */",
732 " }",
733 " }",
734 " return rv;",
735 "}",
736 "",
737 ]
738 )
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200739
740 def get_event_cb_func_decl(self):
Klement Sekera2108c0c2018-08-24 11:43:20 +0200741 if not self.is_reply and not self.is_event:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200742 raise Exception("Cannot register event callback for non-reply message")
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200743 if self.has_payload():
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200744 return "\n".join(
745 [
746 "void vapi_set_%s_event_cb (" % self.get_c_name(),
747 " struct vapi_ctx_s *ctx, ",
748 (
749 " vapi_error_e (*callback)(struct vapi_ctx_s *ctx, "
750 "void *callback_ctx, %s *payload),"
751 % self.get_payload_struct_name()
752 ),
753 " void *callback_ctx)",
754 ]
755 )
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200756 else:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200757 return "\n".join(
758 [
759 "void vapi_set_%s_event_cb (" % self.get_c_name(),
760 " struct vapi_ctx_s *ctx, ",
761 " vapi_error_e (*callback)(struct vapi_ctx_s *ctx, "
762 "void *callback_ctx),",
763 " void *callback_ctx)",
764 ]
765 )
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200766
767 def get_event_cb_func_def(self):
Klement Sekera2108c0c2018-08-24 11:43:20 +0200768 if not self.is_reply and not self.is_event:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200769 raise Exception("Cannot register event callback for non-reply function")
770 return "\n".join(
771 [
772 "%s" % self.get_event_cb_func_decl(),
773 "{",
774 (
775 " vapi_set_event_cb(ctx, %s, (vapi_event_cb)callback, "
776 "callback_ctx);" % self.get_msg_id_name()
777 ),
778 "}",
779 ]
780 )
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200781
782 def get_c_metadata_struct_name(self):
783 return "__vapi_metadata_%s" % self.name
784
785 def get_c_constructor(self):
786 has_context = False
787 if self.header is not None:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200788 has_context = self.header.has_field("context")
789 return "\n".join(
790 [
791 "static void __attribute__((constructor)) __vapi_constructor_%s()"
792 % self.name,
793 "{",
794 ' static const char name[] = "%s";' % self.name,
795 ' static const char name_with_crc[] = "%s_%s";'
796 % (self.name, self.crc[2:]),
797 " static vapi_message_desc_t %s = {"
798 % self.get_c_metadata_struct_name(),
799 " name,",
800 " sizeof(name) - 1,",
801 " name_with_crc,",
802 " sizeof(name_with_crc) - 1,",
803 " true," if has_context else " false,",
Dave Wallace940a70f2024-04-03 12:31:42 -0400804 (
805 " offsetof(%s, context)," % self.header.get_c_name()
806 if has_context
807 else " 0,"
808 ),
809 (
810 (" offsetof(%s, payload)," % self.get_c_name())
811 if self.has_payload()
812 else " VAPI_INVALID_MSG_ID,"
813 ),
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200814 " (verify_msg_size_fn_t)%s," % self.get_verify_msg_size_func_name(),
815 " (generic_swap_fn_t)%s," % self.get_swap_to_be_func_name(),
816 " (generic_swap_fn_t)%s," % self.get_swap_to_host_func_name(),
817 " VAPI_INVALID_MSG_ID,",
818 " };",
819 "",
820 " %s = vapi_register_msg(&%s);"
821 % (self.get_msg_id_name(), self.get_c_metadata_struct_name()),
822 ' VAPI_DBG("Assigned msg id %%d to %s", %s);'
823 % (self.name, self.get_msg_id_name()),
824 "}",
825 ]
826 )
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200827
828
Klement Sekera1732e472018-08-28 17:23:18 +0200829def emit_definition(parser, json_file, emitted, o):
Klement Sekera2108c0c2018-08-24 11:43:20 +0200830 if o in emitted:
831 return
Klement Sekera2108c0c2018-08-24 11:43:20 +0200832 if o.name in ("msg_header1_t", "msg_header2_t"):
833 return
834 if hasattr(o, "depends"):
835 for x in o.depends:
Klement Sekera1732e472018-08-28 17:23:18 +0200836 emit_definition(parser, json_file, emitted, x)
Klement Sekera2108c0c2018-08-24 11:43:20 +0200837 if hasattr(o, "reply"):
Klement Sekera1732e472018-08-28 17:23:18 +0200838 emit_definition(parser, json_file, emitted, o.reply)
Stanislav Zaikin56777b92022-07-21 19:07:50 +0200839 if hasattr(o, "stream_msg"):
840 emit_definition(parser, json_file, emitted, o.stream_msg)
Klement Sekera2108c0c2018-08-24 11:43:20 +0200841 if hasattr(o, "get_c_def"):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200842 if (
843 o not in parser.enums_by_json[json_file]
844 and o not in parser.types_by_json[json_file]
845 and o not in parser.unions_by_json[json_file]
846 and o.name not in parser.messages_by_json[json_file]
847 and o not in parser.aliases_by_json[json_file]
848 ):
Klement Sekera2108c0c2018-08-24 11:43:20 +0200849 return
Klement Sekera2108c0c2018-08-24 11:43:20 +0200850 guard = "defined_%s" % o.get_c_name()
851 print("#ifndef %s" % guard)
852 print("#define %s" % guard)
853 print("%s" % o.get_c_def())
854 print("")
855 function_attrs = "static inline "
856 if o.name in parser.messages_by_json[json_file]:
Klement Sekera2108c0c2018-08-24 11:43:20 +0200857 if o.has_payload():
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200858 print("%s%s" % (function_attrs, o.get_swap_payload_to_be_func_def()))
Klement Sekera2108c0c2018-08-24 11:43:20 +0200859 print("")
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200860 print("%s%s" % (function_attrs, o.get_swap_payload_to_host_func_def()))
Klement Sekera2108c0c2018-08-24 11:43:20 +0200861 print("")
862 print("%s%s" % (function_attrs, o.get_swap_to_be_func_def()))
863 print("")
864 print("%s%s" % (function_attrs, o.get_swap_to_host_func_def()))
865 print("")
866 print("%s%s" % (function_attrs, o.get_calc_msg_size_func_def()))
Klement Sekeraa25ce962021-11-15 15:52:37 +0100867 print("")
868 print("%s%s" % (function_attrs, o.get_verify_msg_size_func_def()))
Stanislav Zaikin56777b92022-07-21 19:07:50 +0200869 if not o.is_reply and not o.is_event and not o.is_stream:
Klement Sekera34a962b2018-09-06 19:31:36 +0200870 print("")
Klement Sekera2108c0c2018-08-24 11:43:20 +0200871 print("%s%s" % (function_attrs, o.get_alloc_func_def()))
872 print("")
873 print("%s%s" % (function_attrs, o.get_op_func_def()))
Klement Sekera2108c0c2018-08-24 11:43:20 +0200874 print("")
Klement Sekera34a962b2018-09-06 19:31:36 +0200875 print("%s" % o.get_c_constructor())
Stanislav Zaikin56777b92022-07-21 19:07:50 +0200876 if (o.is_reply or o.is_event) and not o.is_stream:
Klement Sekera2108c0c2018-08-24 11:43:20 +0200877 print("")
Klement Sekera34a962b2018-09-06 19:31:36 +0200878 print("%s%s;" % (function_attrs, o.get_event_cb_func_def()))
Klement Sekera2108c0c2018-08-24 11:43:20 +0200879 elif hasattr(o, "get_swap_to_be_func_def"):
880 print("%s%s" % (function_attrs, o.get_swap_to_be_func_def()))
881 print("")
882 print("%s%s" % (function_attrs, o.get_swap_to_host_func_def()))
Klement Sekera2108c0c2018-08-24 11:43:20 +0200883 print("#endif")
Klement Sekera34a962b2018-09-06 19:31:36 +0200884 print("")
Klement Sekera2108c0c2018-08-24 11:43:20 +0200885 emitted.append(o)
886
887
Klement Sekeradc15be22017-06-12 06:49:33 +0200888def gen_json_unified_header(parser, logger, j, io, name):
Damjan Marion4c64b6e2018-08-26 18:14:46 +0200889 d, f = os.path.split(j)
Klement Sekeradc15be22017-06-12 06:49:33 +0200890 logger.info("Generating header `%s'" % name)
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200891 orig_stdout = sys.stdout
892 sys.stdout = io
893 include_guard = "__included_%s" % (
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200894 j.replace(".", "_")
895 .replace("/", "_")
896 .replace("-", "_")
897 .replace("+", "_")
898 .replace("@", "_")
899 )
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200900 print("#ifndef %s" % include_guard)
901 print("#define %s" % include_guard)
902 print("")
Klement Sekeradc15be22017-06-12 06:49:33 +0200903 print("#include <stdlib.h>")
904 print("#include <stddef.h>")
905 print("#include <arpa/inet.h>")
906 print("#include <vapi/vapi_internal.h>")
907 print("#include <vapi/vapi.h>")
908 print("#include <vapi/vapi_dbg.h>")
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200909 print("")
Klement Sekeradc15be22017-06-12 06:49:33 +0200910 print("#ifdef __cplusplus")
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200911 print('extern "C" {')
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200912 print("#endif")
Klement Sekera03e1d552023-08-06 19:33:16 +0200913
914 print("#ifndef __vl_api_string_swap_fns_defined__")
915 print("#define __vl_api_string_swap_fns_defined__")
916 print("")
917 print("#include <vlibapi/api_types.h>")
918 print("")
919 function_attrs = "static inline "
920 o = parser.types["vl_api_string_t"]
921 print("%s%s" % (function_attrs, o.get_swap_to_be_func_def()))
922 print("")
923 print("%s%s" % (function_attrs, o.get_swap_to_host_func_def()))
924 print("")
925 print("#endif //__vl_api_string_swap_fns_defined__")
926
Filip Tehlarf0e67d72021-07-23 22:03:05 +0000927 if name == "memclnt.api.vapi.h":
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200928 print("")
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200929 print(
930 "static inline vapi_error_e vapi_send_with_control_ping "
931 "(vapi_ctx_t ctx, void * msg, u32 context);"
932 )
Dave Wallace6081dda2022-02-23 15:36:02 -0500933 elif name == "vlib.api.vapi.h":
934 print("#include <vapi/memclnt.api.vapi.h>")
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200935 else:
Florin Corasa1400ce2021-09-15 09:02:08 -0700936 print("#include <vapi/vlib.api.vapi.h>")
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200937 print("")
938 for m in parser.messages_by_json[j].values():
939 print("extern vapi_msg_id_t %s;" % m.get_msg_id_name())
940 print("")
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200941 print(
942 "#define DEFINE_VAPI_MSG_IDS_%s\\"
943 % f.replace(".", "_").replace("/", "_").replace("-", "_").upper()
944 )
945 print(
946 "\\\n".join(
947 [
948 " vapi_msg_id_t %s;" % m.get_msg_id_name()
949 for m in parser.messages_by_json[j].values()
950 ]
951 )
952 )
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200953 print("")
954 print("")
Klement Sekera2108c0c2018-08-24 11:43:20 +0200955 emitted = []
956 for e in parser.enums_by_json[j]:
Klement Sekera1732e472018-08-28 17:23:18 +0200957 emit_definition(parser, j, emitted, e)
Klement Sekera2108c0c2018-08-24 11:43:20 +0200958 for u in parser.unions_by_json[j]:
Klement Sekera1732e472018-08-28 17:23:18 +0200959 emit_definition(parser, j, emitted, u)
Ole Troan52ca7562018-03-06 17:45:32 +0100960 for t in parser.types_by_json[j]:
Klement Sekera1732e472018-08-28 17:23:18 +0200961 emit_definition(parser, j, emitted, t)
Ole Troan75761b92019-09-11 17:49:08 +0200962 for a in parser.aliases_by_json[j]:
963 emit_definition(parser, j, emitted, a)
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200964 for m in parser.messages_by_json[j].values():
Klement Sekera1732e472018-08-28 17:23:18 +0200965 emit_definition(parser, j, emitted, m)
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200966
967 print("")
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200968
Florin Corasa1400ce2021-09-15 09:02:08 -0700969 if name == "vlib.api.vapi.h":
Dave Wallace6081dda2022-02-23 15:36:02 -0500970 vapi_send_with_control_ping_function = """
971static inline vapi_error_e
972vapi_send_with_control_ping (vapi_ctx_t ctx, void *msg, u32 context)
973{
974 vapi_msg_control_ping *ping = vapi_alloc_control_ping (ctx);
975 if (!ping)
976 {
977 return VAPI_ENOMEM;
978 }
979 ping->header.context = context;
980 vapi_msg_control_ping_hton (ping);
981 return vapi_send2 (ctx, msg, ping);
982}
983"""
984 print("%s" % vapi_send_with_control_ping_function)
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200985 print("")
986
Klement Sekeradc15be22017-06-12 06:49:33 +0200987 print("#ifdef __cplusplus")
988 print("}")
989 print("#endif")
990 print("")
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200991 print("#endif")
992 sys.stdout = orig_stdout
993
994
Klement Sekeradc15be22017-06-12 06:49:33 +0200995def json_to_c_header_name(json_name):
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200996 if json_name.endswith(".json"):
997 return "%s.vapi.h" % os.path.splitext(json_name)[0]
998 raise Exception("Unexpected json name `%s'!" % json_name)
999
1000
Damjan Marion4c64b6e2018-08-26 18:14:46 +02001001def gen_c_unified_headers(parser, logger, prefix, remove_path):
Klement Sekera8f2a4ea2017-05-04 06:15:18 +02001002 if prefix == "" or prefix is None:
1003 prefix = ""
1004 else:
1005 prefix = "%s/" % prefix
1006 for j in parser.json_files:
Damjan Marion4c64b6e2018-08-26 18:14:46 +02001007 if remove_path:
1008 d, f = os.path.split(j)
1009 else:
1010 f = j
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001011 with open("%s%s" % (prefix, json_to_c_header_name(f)), "w") as io:
1012 gen_json_unified_header(parser, logger, j, io, json_to_c_header_name(f))
Klement Sekera8f2a4ea2017-05-04 06:15:18 +02001013
1014
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001015if __name__ == "__main__":
Klement Sekera8f2a4ea2017-05-04 06:15:18 +02001016 try:
1017 verbose = int(os.getenv("V", 0))
1018 except:
1019 verbose = 0
1020
1021 if verbose >= 2:
1022 log_level = 10
1023 elif verbose == 1:
1024 log_level = 20
1025 else:
1026 log_level = 40
1027
1028 logging.basicConfig(stream=sys.stdout, level=log_level)
1029 logger = logging.getLogger("VAPI C GEN")
1030 logger.setLevel(log_level)
1031
Klement Sekeradc15be22017-06-12 06:49:33 +02001032 argparser = argparse.ArgumentParser(description="VPP C API generator")
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001033 argparser.add_argument(
1034 "files",
1035 metavar="api-file",
1036 action="append",
1037 type=str,
1038 help="json api file" "(may be specified multiple times)",
1039 )
1040 argparser.add_argument("--prefix", action="store", default=None, help="path prefix")
1041 argparser.add_argument(
1042 "--remove-path", action="store_true", help="remove path from filename"
1043 )
Klement Sekera8f2a4ea2017-05-04 06:15:18 +02001044 args = argparser.parse_args()
1045
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001046 jsonparser = JsonParser(
1047 logger,
1048 args.files,
1049 simple_type_class=CSimpleType,
1050 enum_class=CEnum,
1051 union_class=CUnion,
1052 struct_type_class=CStructType,
1053 field_class=CField,
1054 message_class=CMessage,
1055 alias_class=CAlias,
1056 )
Klement Sekera8f2a4ea2017-05-04 06:15:18 +02001057
1058 # not using the model of having separate generated header and code files
1059 # with generated symbols present in shared library (per discussion with
1060 # Damjan), to avoid symbol version issues in .so
1061 # gen_c_headers_and_code(jsonparser, logger, args.prefix)
1062
Damjan Marion4c64b6e2018-08-26 18:14:46 +02001063 gen_c_unified_headers(jsonparser, logger, args.prefix, args.remove_path)
Klement Sekera8f2a4ea2017-05-04 06:15:18 +02001064
1065 for e in jsonparser.exceptions:
Damjan Marion4c64b6e2018-08-26 18:14:46 +02001066 logger.warning(e)