Debian Codegraph @

build-aux/qmi-codegen/VariableInteger.py

raw
1 # -*- Mode: python; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 #
3 # This program is free software; you can redistribute it and/or modify it under
4 # the terms of the GNU Lesser General Public License as published by the Free
5 # Software Foundation; either version 2 of the License, or (at your option) any
6 # later version.
7 #
8 # This program is distributed in the hope that it will be useful, but WITHOUT
9 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
11 # details.
12 #
13 # You should have received a copy of the GNU Lesser General Public License along
14 # with this program; if not, write to the Free Software Foundation, Inc., 51
15 # Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 #
17 # Copyright (C) 2012 Lanedo GmbH
18 # Copyright (C) 2012-2022 Aleksander Morgado <[email protected]>
19 # Copyright (c) 2022 Qualcomm Innovation Center, Inc.
20 #
21
22 import string
23 import utils
24 from Variable import Variable
25
26 """
27 Variable type for signed/unsigned Integers and floating point numbers:
28 'guint8', 'gint8'
29 'guint16', 'gint16'
30 'guint32', 'gint32'
31 'guint64', 'gint64'
32 'guint-sized'
33 'gfloat', 'gdouble'
34 """
35 class VariableInteger(Variable):
36
37 def __init__(self, service, dictionary):
38
39 # Call the parent constructor
40 Variable.__init__(self, service, dictionary)
41
42 self.guint_sized_size = ''
43 if self.format == "guint-sized":
44 if 'guint-size' not in dictionary:
45 raise RuntimeError('Format \'guint-sized\' requires \'guint-size\' parameter')
46 else:
47 self.guint_sized_size = dictionary['guint-size']
48 self.private_format = 'guint64'
49 self.public_format = 'guint64'
50 else:
51 self.private_format = self.format
52 self.public_format = dictionary['public-format'] if 'public-format' in dictionary else self.private_format
53 self.element_type = self.public_format
54
55 # Public format for the GIR array compat methods in 1.32
56 self.private_format_gir = self.private_format
57 self.public_format_gir = self.public_format
58 self.element_type_gir = self.element_type
59
60
61 def emit_buffer_read(self, f, line_prefix, tlv_out, error, variable_name):
62 translations = { 'lp' : line_prefix,
63 'tlv_out' : tlv_out,
64 'variable_name' : variable_name,
65 'error' : error,
66 'public_format' : self.public_format,
67 'private_format' : self.private_format,
68 'len' : self.guint_sized_size }
69
70 if self.private_format not in ('guint8', 'gint8'):
71 translations['endian'] = ' ' + self.endian + ','
72 else:
73 translations['endian'] = ''
74
75 if self.format == 'guint-sized':
76 template = (
77 '${lp}if (!qmi_message_tlv_read_sized_guint (message, init_offset, &offset, ${len},${endian} &(${variable_name}), ${error}))\n'
78 '${lp} goto ${tlv_out};\n')
79 elif self.format == 'gfloat':
80 template = (
81 '${lp}if (!qmi_message_tlv_read_gfloat_endian (message, init_offset, &offset,${endian} &(${variable_name}), ${error}))\n'
82 '${lp} goto ${tlv_out};\n')
83 elif self.private_format == self.public_format:
84 template = (
85 '${lp}if (!qmi_message_tlv_read_${private_format} (message, init_offset, &offset,${endian} &(${variable_name}), ${error}))\n'
86 '${lp} goto ${tlv_out};\n')
87 else:
88 template = (
89 '${lp}{\n'
90 '${lp} ${private_format} tmp;\n'
91 '\n'
92 '${lp} if (!qmi_message_tlv_read_${private_format} (message, init_offset, &offset,${endian} &tmp, ${error}))\n'
93 '${lp} goto ${tlv_out};\n'
94 '${lp} ${variable_name} = (${public_format})tmp;\n'
95 '${lp}}\n')
96 f.write(string.Template(template).substitute(translations))
97
98
99 @staticmethod
100 def fixed_type_byte_size(fmt):
101 if fmt == 'guint8':
102 return 1
103 if fmt == 'guint16':
104 return 2
105 if fmt == 'guint32':
106 return 4
107 if fmt == 'guint64':
108 return 8
109 if fmt == 'gint8':
110 return 1
111 if fmt == 'gint16':
112 return 2
113 if fmt == 'gint32':
114 return 4
115 if fmt == 'gint64':
116 return 8
117 raise Exception("Unsupported format %s" % (fmt))
118
119
120 def emit_buffer_write(self, f, line_prefix, tlv_name, variable_name):
121 translations = { 'lp' : line_prefix,
122 'private_format' : self.private_format,
123 'len' : self.guint_sized_size,
124 'tlv_name' : tlv_name,
125 'variable_name' : variable_name }
126
127 if self.private_format != 'guint8' and self.private_format != 'gint8':
128 translations['endian'] = ' ' + self.endian + ','
129 else:
130 translations['endian'] = ''
131
132 if self.format == 'guint-sized':
133 template = (
134 '${lp}/* Write the ${len}-byte long variable to the buffer */\n'
135 '${lp}if (!qmi_message_tlv_write_sized_guint (self, ${len},${endian} ${variable_name}, error)) {\n'
136 '${lp} g_prefix_error (error, "Cannot write sized integer in TLV \'${tlv_name}\': ");\n'
137 '${lp} return NULL;\n'
138 '${lp}}\n')
139 elif self.private_format == self.public_format:
140 template = (
141 '${lp}/* Write the ${private_format} variable to the buffer */\n'
142 '${lp}if (!qmi_message_tlv_write_${private_format} (self,${endian} ${variable_name}, error)) {\n'
143 '${lp} g_prefix_error (error, "Cannot write integer in TLV \'${tlv_name}\': ");\n'
144 '${lp} return NULL;\n'
145 '${lp}}\n')
146 else:
147 template = (
148 '${lp}{\n'
149 '${lp} ${private_format} tmp;\n'
150 '\n'
151 '${lp} tmp = (${private_format}) ${variable_name};\n'
152 '${lp} /* Write the ${private_format} variable to the buffer */\n'
153 '${lp} if (!qmi_message_tlv_write_${private_format} (self,${endian} tmp, error)) {\n'
154 '${lp} g_prefix_error (error, "Cannot write enum in TLV \'${tlv_name}\': ");\n'
155 '${lp} return NULL;\n'
156 '${lp} }\n'
157 '${lp}}\n')
158 f.write(string.Template(template).substitute(translations))
159
160
161 def emit_get_printable(self, f, line_prefix, is_personal):
162 common_format = ''
163 common_cast = ''
164
165 if self.private_format == 'guint8':
166 common_format = '%u'
167 common_cast = '(guint)'
168 elif self.private_format == 'guint16':
169 common_format = '%" G_GUINT16_FORMAT "'
170 elif self.private_format == 'guint32':
171 common_format = '%" G_GUINT32_FORMAT "'
172 elif self.private_format == 'guint64':
173 common_format = '%" G_GUINT64_FORMAT "'
174 elif self.private_format == 'gint8':
175 common_format = '%d'
176 common_cast = '(gint)'
177 elif self.private_format == 'gint16':
178 common_format = '%" G_GINT16_FORMAT "'
179 elif self.private_format == 'gint32':
180 common_format = '%" G_GINT32_FORMAT "'
181 elif self.private_format == 'gint64':
182 common_format = '%" G_GINT64_FORMAT "'
183 elif self.private_format in ('gfloat', 'gdouble'):
184 common_format = '%lf'
185 common_cast = '(gdouble)'
186
187 translations = { 'lp' : line_prefix,
188 'private_format' : self.private_format,
189 'public_format' : self.public_format,
190 'len' : self.guint_sized_size,
191 'common_format' : common_format,
192 'common_cast' : common_cast }
193
194 if self.private_format not in ('guint8', 'gint8'):
195 translations['endian'] = ' ' + self.endian + ','
196 else:
197 translations['endian'] = ''
198
199 template = (
200 '\n'
201 '${lp}{\n'
202 '${lp} ${private_format} tmp;\n'
203 '\n')
204
205 if self.format == 'guint-sized':
206 template += (
207 '${lp} if (!qmi_message_tlv_read_sized_guint (message, init_offset, &offset, ${len},${endian} &tmp, &error))\n'
208 '${lp} goto out;\n')
209 elif self.format == 'gfloat':
210 template += (
211 '${lp} if (!qmi_message_tlv_read_gfloat_endian (message, init_offset, &offset,${endian} &tmp, &error))\n'
212 '${lp} goto out;\n')
213 else:
214 template += (
215 '${lp} if (!qmi_message_tlv_read_${private_format} (message, init_offset, &offset,${endian} &tmp, &error))\n'
216 '${lp} goto out;\n')
217
218 if self.personal_info or is_personal:
219 translations['if_show_field'] = 'if (qmi_utils_get_show_personal_info ()) '
220 else:
221 translations['if_show_field'] = ''
222
223 template += (
224 '${lp} ${if_show_field}{\n')
225 if self.public_format == 'gboolean':
226 template += (
227 '${lp} g_string_append_printf (printable, "%s", tmp ? "yes" : "no");\n')
228 elif self.public_format != self.private_format:
229 translations['public_type_underscore'] = utils.build_underscore_name_from_camelcase(self.public_format)
230 translations['public_type_underscore_upper'] = utils.build_underscore_name_from_camelcase(self.public_format).upper()
231 template += (
232 '#if defined __${public_type_underscore_upper}_IS_ENUM__\n'
233 '${lp} g_string_append_printf (printable, "%s", ${public_type_underscore}_get_string ((${public_format})tmp));\n'
234 '#elif defined __${public_type_underscore_upper}_IS_FLAGS__\n'
235 '${lp} {\n'
236 '${lp} g_autofree gchar *flags_str = NULL;\n'
237 '\n'
238 '${lp} flags_str = ${public_type_underscore}_build_string_from_mask ((${public_format})tmp);\n'
239 '${lp} g_string_append_printf (printable, "%s", flags_str);\n'
240 '${lp} }\n'
241 '#else\n'
242 '# error unexpected public format: ${public_format}\n'
243 '#endif\n')
244 else:
245 template += (
246 '${lp} g_string_append_printf (printable, "${common_format}", ${common_cast}tmp);\n')
247
248 if self.personal_info or is_personal:
249 template += (
250 '${lp} } else {\n'
251 '${lp} g_string_append_printf (printable, "\'###\'");\n')
252
253 template += (
254 '${lp} }\n'
255 '${lp}}\n')
256
257 f.write(string.Template(template).substitute(translations))
258
259
260 def build_variable_declaration(self, line_prefix, variable_name):
261 translations = { 'lp' : line_prefix,
262 'private_format' : self.private_format,
263 'name' : variable_name }
264 template = (
265 '${lp}${private_format} ${name};\n')
266 return string.Template(template).substitute(translations)
267
268
269 def build_struct_field_declaration(self, line_prefix, variable_name):
270 translations = { 'lp' : line_prefix,
271 'public_format' : self.public_format,
272 'name' : variable_name }
273 template = (
274 '${lp}${public_format} ${name};\n')
275 return string.Template(template).substitute(translations)
276
277
278 def build_getter_declaration(self, line_prefix, variable_name):
279 if not self.visible:
280 return ""
281
282 translations = { 'lp' : line_prefix,
283 'public_format' : self.public_format,
284 'name' : variable_name }
285
286 template = (
287 '${lp}${public_format} *${name},\n')
288 return string.Template(template).substitute(translations)
289
290
291 def build_getter_documentation(self, line_prefix, variable_name):
292 if not self.visible:
293 return ""
294
295 translations = { 'lp' : line_prefix,
296 'public_format' : self.public_format,
297 'name' : variable_name }
298
299 template = (
300 '${lp}@${name}: (out)(optional): a placeholder for the output #${public_format}, or %NULL if not required.\n')
301 return string.Template(template).substitute(translations)
302
303
304 def build_getter_implementation(self, line_prefix, variable_name_from, variable_name_to):
305 if not self.visible:
306 return ""
307
308 needs_cast = True if self.public_format != self.private_format else False
309 translations = { 'lp' : line_prefix,
310 'from' : variable_name_from,
311 'to' : variable_name_to,
312 'cast_ini' : '(' + self.public_format + ')(' if needs_cast else '',
313 'cast_end' : ')' if needs_cast else '' }
314
315 template = (
316 '${lp}if (${to})\n'
317 '${lp} *${to} = ${cast_ini}${from}${cast_end};\n')
318 return string.Template(template).substitute(translations)
319
320
321 def build_setter_declaration(self, line_prefix, variable_name):
322 if not self.visible:
323 return ""
324
325 translations = { 'lp' : line_prefix,
326 'public_format' : self.public_format,
327 'name' : variable_name }
328
329 template = (
330 '${lp}${public_format} ${name},\n')
331 return string.Template(template).substitute(translations)
332
333
334 def build_setter_documentation(self, line_prefix, variable_name):
335 if not self.visible:
336 return ""
337
338 translations = { 'lp' : line_prefix,
339 'public_format' : self.public_format,
340 'name' : variable_name }
341
342 template = (
343 '${lp}@${name}: a #${public_format}.\n')
344 return string.Template(template).substitute(translations)
345
346
347 def build_setter_implementation(self, line_prefix, variable_name_from, variable_name_to):
348 if not self.visible:
349 return ""
350
351 needs_cast = True if self.public_format != self.private_format else False
352 translations = { 'lp' : line_prefix,
353 'from' : variable_name_from,
354 'to' : variable_name_to,
355 'cast_ini' : '(' + self.private_format + ')(' if needs_cast else '',
356 'cast_end' : ')' if needs_cast else '' }
357
358 template = (
359 '${lp}${to} = ${cast_ini}${from}${cast_end};\n')
360 return string.Template(template).substitute(translations)
361
362
363 def build_struct_field_documentation(self, line_prefix, variable_name):
364 translations = { 'lp' : line_prefix,
365 'public_format' : self.public_format,
366 'name' : variable_name }
367
368 template = (
369 '${lp}@${name}: a #${public_format}.\n')
370 return string.Template(template).substitute(translations)
371
372
373 def build_copy_gir(self, line_prefix, variable_name_from, variable_name_to):
374 translations = { 'lp' : line_prefix,
375 'variable_name_from' : variable_name_from,
376 'variable_name_to' : variable_name_to }
377
378 template = (
379 '${lp}${variable_name_to} = ${variable_name_from};\n')
380 return string.Template(template).substitute(translations)
381
382
383 def build_copy_to_gir(self, line_prefix, variable_name_from, variable_name_to):
384 return self.build_copy_gir (line_prefix, variable_name_from, variable_name_to)
385
386
387 def build_copy_from_gir(self, line_prefix, variable_name_from, variable_name_to):
388 return self.build_copy_gir (line_prefix, variable_name_from, variable_name_to)