aboutsummaryrefslogtreecommitdiffstats
path: root/build-aux/qmi-codegen/VariableInteger.py
blob: 12d2a77b7b9a162dda619804ffb4a50269421cbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#!/usr/bin/env python
# -*- Mode: python; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright (C) 2012 Lanedo GmbH
# Copyright (C) 2012-2017 Aleksander Morgado <aleksander@aleksander.es>
#

import string
import utils
from Variable import Variable

"""
Variable type for signed/unsigned Integers and floating point numbers:
 'guint8', 'gint8'
 'guint16', 'gint16'
 'guint32', 'gint32'
 'guint64', 'gint64'
 'guint-sized'
 'gfloat'
"""
class VariableInteger(Variable):

    """
    Constructor
    """
    def __init__(self, dictionary):

        # Call the parent constructor
        Variable.__init__(self, dictionary)

        self.guint_sized_size = ''
        if self.format == "guint-sized":
            if 'guint-size' not in dictionary:
                raise RuntimeError('Format \'guint-sized\' requires \'guint-size\' parameter')
            else:
                self.guint_sized_size = dictionary['guint-size']
            self.private_format = 'guint64'
            self.public_format  = 'guint64'
        else:
            self.private_format = self.format
            self.public_format = dictionary['public-format'] if 'public-format' in dictionary else self.private_format

    """
    Read a single integer from the raw byte buffer
    """
    def emit_buffer_read(self, f, line_prefix, tlv_out, error, variable_name):
        translations = { 'lp'             : line_prefix,
                         'tlv_out'        : tlv_out,
                         'variable_name'  : variable_name,
                         'error'          : error,
                         'public_format'  : self.public_format,
                         'private_format' : self.private_format,
                         'len'            : self.guint_sized_size }

        if self.private_format != 'guint8' and self.private_format != 'gint8' and self.private_format != 'gfloat':
            translations['endian'] = ' ' + self.endian + ','
        else:
            translations['endian'] = ''

        if self.format == 'guint-sized':
            template = (
                '${lp}if (!qmi_message_tlv_read_sized_guint (message, init_offset, &offset, ${len},${endian} &(${variable_name}), ${error}))\n'
                '${lp}    goto ${tlv_out};\n')
        elif self.private_format == self.public_format:
            template = (
                '${lp}if (!qmi_message_tlv_read_${private_format} (message, init_offset, &offset,${endian} &(${variable_name}), ${error}))\n'
                '${lp}    goto ${tlv_out};\n')
        else:
            template = (
                '${lp}{\n'
                '${lp}    ${private_format} tmp;\n'
                '\n'
                '${lp}    if (!qmi_message_tlv_read_${private_format} (message, init_offset, &offset,${endian} &tmp, ${error}))\n'
                '${lp}        goto ${tlv_out};\n'
                '${lp}    ${variable_name} = (${public_format})tmp;\n'
                '${lp}}\n')
        f.write(string.Template(template).substitute(translations))


    """
    Return the data type size of fixed c-types
    """
    @staticmethod
    def fixed_type_byte_size(fmt):
        if fmt == 'guint8':
            return 1
        if fmt == 'guint16':
            return 2
        if fmt == 'guint32':
            return 4
        if fmt == 'guint64':
            return 8
        if fmt == 'gint8':
            return 1
        if fmt == 'gint16':
            return 2
        if fmt == 'gint32':
            return 4
        if fmt == 'gint64':
            return 8
        raise Exception("Unsupported format %s" % (fmt))

    """
    Write a single integer to the raw byte buffer
    """
    def emit_buffer_write(self, f, line_prefix, tlv_name, variable_name):
        translations = { 'lp'             : line_prefix,
                         'private_format' : self.private_format,
                         'len'            : self.guint_sized_size,
                         'tlv_name'       : tlv_name,
                         'variable_name'  : variable_name }

        if self.private_format != 'guint8' and self.private_format != 'gint8':
            translations['endian'] = ' ' + self.endian + ','
        else:
            translations['endian'] = ''

        if self.format == 'guint-sized':
            template = (
                '${lp}/* Write the ${len}-byte long variable to the buffer */\n'
                '${lp}if (!qmi_message_tlv_write_sized_guint (self, ${len},${endian} ${variable_name}, error)) {\n'
                '${lp}    g_prefix_error (error, "Cannot write sized integer in TLV \'${tlv_name}\': ");\n'
                '${lp}    goto error_out;\n'
                '${lp}}\n')
        elif self.private_format == self.public_format:
            template = (
                '${lp}/* Write the ${private_format} variable to the buffer */\n'
                '${lp}if (!qmi_message_tlv_write_${private_format} (self,${endian} ${variable_name}, error)) {\n'
                '${lp}    g_prefix_error (error, "Cannot write integer in TLV \'${tlv_name}\': ");\n'
                '${lp}    goto error_out;\n'
                '${lp}}\n')
        else:
            template = (
                '${lp}{\n'
                '${lp}    ${private_format} tmp;\n'
                '\n'
                '${lp}    tmp = (${private_format}) ${variable_name};\n'
                '${lp}    /* Write the ${private_format} variable to the buffer */\n'
                '${lp}    if (!qmi_message_tlv_write_${private_format} (self,${endian} tmp, error)) {\n'
                '${lp}        g_prefix_error (error, "Cannot write enum in TLV \'${tlv_name}\': ");\n'
                '${lp}        goto error_out;\n'
                '${lp}    }\n'
                '${lp}}\n')
        f.write(string.Template(template).substitute(translations))


    """
    Get the integer as a printable string.
    """
    def emit_get_printable(self, f, line_prefix):
        common_format = ''
        common_cast = ''

        if self.private_format == 'guint8':
            common_format = '%u'
            common_cast = '(guint)'
        elif self.private_format == 'guint16':
            common_format = '%" G_GUINT16_FORMAT "'
        elif self.private_format == 'guint32':
            common_format = '%" G_GUINT32_FORMAT "'
        elif self.private_format == 'guint64':
            common_format = '%" G_GUINT64_FORMAT "'
        elif self.private_format == 'gint8':
            common_format = '%d'
            common_cast = '(gint)'
        elif self.private_format == 'gint16':
            common_format = '%" G_GINT16_FORMAT "'
        elif self.private_format == 'gint32':
            common_format = '%" G_GINT32_FORMAT "'
        elif self.private_format == 'gint64':
            common_format = '%" G_GINT64_FORMAT "'
        elif self.private_format == 'gfloat':
            common_format = '%f'

        translations = { 'lp'             : line_prefix,
                         'private_format' : self.private_format,
                         'public_format'  : self.public_format,
                         'len'            : self.guint_sized_size,
                         'common_format'  : common_format,
                         'common_cast'    : common_cast }

        if self.private_format != 'guint8' and self.private_format != 'gint8' and self.private_format != 'gfloat':
            translations['endian'] = ' ' + self.endian + ','
        else:
            translations['endian'] = ''

        template = (
            '\n'
            '${lp}{\n'
            '${lp}    ${private_format} tmp;\n'
            '\n')

        if self.format == 'guint-sized':
            template += (
                '${lp}    if (!qmi_message_tlv_read_sized_guint (message, init_offset, &offset, ${len},${endian} &tmp, &error))\n'
                '${lp}        goto out;\n')
        else:
            template += (
                '${lp}    if (!qmi_message_tlv_read_${private_format} (message, init_offset, &offset,${endian} &tmp, &error))\n'
                '${lp}        goto out;\n')

        if self.public_format == 'gboolean':
            template += (
                '${lp}    g_string_append_printf (printable, "%s", tmp ? "yes" : "no");\n')
        elif self.public_format != self.private_format:
            translations['public_type_underscore'] = utils.build_underscore_name_from_camelcase(self.public_format)
            translations['public_type_underscore_upper'] = utils.build_underscore_name_from_camelcase(self.public_format).upper()
            template += (
                '#if defined  __${public_type_underscore_upper}_IS_ENUM__\n'
                '${lp}    g_string_append_printf (printable, "%s", ${public_type_underscore}_get_string ((${public_format})tmp));\n'
                '#elif defined  __${public_type_underscore_upper}_IS_FLAGS__\n'
                '${lp}    {\n'
                '${lp}        gchar *flags_str;\n'
                '\n'
                '${lp}        flags_str = ${public_type_underscore}_build_string_from_mask ((${public_format})tmp);\n'
                '${lp}        g_string_append_printf (printable, "%s", flags_str);\n'
                '${lp}        g_free (flags_str);\n'
                '${lp}    }\n'
                '#else\n'
                '# error unexpected public format: ${public_format}\n'
                '#endif\n')
        else:
            template += (
                '${lp}    g_string_append_printf (printable, "${common_format}", ${common_cast}tmp);\n')

        template += (
            '${lp}}\n')

        f.write(string.Template(template).substitute(translations))


    """
    Variable declaration
    """
    def build_variable_declaration(self, public, line_prefix, variable_name):
        translations = { 'lp'             : line_prefix,
                         'private_format' : self.private_format,
                         'public_format'  : self.public_format,
                         'name'           : variable_name }
        template = ''
        if public:
            template += (
                '${lp}${public_format} ${name};\n')
        else:
            template += (
                '${lp}${private_format} ${name};\n')
        return string.Template(template).substitute(translations)


    """
    Getter for the integer type
    """
    def build_getter_declaration(self, line_prefix, variable_name):
        if not self.visible:
            return ""

        translations = { 'lp'            : line_prefix,
                         'public_format' : self.public_format,
                         'name'          : variable_name }

        template = (
            '${lp}${public_format} *${name},\n')
        return string.Template(template).substitute(translations)


    """
    Documentation for the getter
    """
    def build_getter_documentation(self, line_prefix, variable_name):
        if not self.visible:
            return ""

        translations = { 'lp'            : line_prefix,
                         'public_format' : self.public_format,
                         'name'          : variable_name }

        template = (
            '${lp}@${name}: a placeholder for the output #${public_format}, or %NULL if not required.\n')
        return string.Template(template).substitute(translations)

    """
    Builds the Integer getter implementation
    """
    def build_getter_implementation(self, line_prefix, variable_name_from, variable_name_to, to_is_reference):
        if not self.visible:
            return ""

        needs_cast = True if self.public_format != self.private_format else False
        translations = { 'lp'       : line_prefix,
                         'from'     : variable_name_from,
                         'to'       : variable_name_to,
                         'cast_ini' : '(' + self.public_format + ')(' if needs_cast else '',
                         'cast_end' : ')' if needs_cast else '' }

        if to_is_reference:
            template = (
                '${lp}if (${to})\n'
                '${lp}    *${to} = ${cast_ini}${from}${cast_end};\n')
            return string.Template(template).substitute(translations)
        else:
            template = (
                '${lp}${to} = ${cast_ini}${from}${cast_end};\n')
            return string.Template(template).substitute(translations)


    """
    Setter for the integer type
    """
    def build_setter_declaration(self, line_prefix, variable_name):
        if not self.visible:
            return ""

        translations = { 'lp'            : line_prefix,
                         'public_format' : self.public_format,
                         'name'          : variable_name }

        template = (
            '${lp}${public_format} ${name},\n')
        return string.Template(template).substitute(translations)


    """
    Documentation for the setter
    """
    def build_setter_documentation(self, line_prefix, variable_name):
        if not self.visible:
            return ""

        translations = { 'lp'            : line_prefix,
                         'public_format' : self.public_format,
                         'name'          : variable_name }

        template = (
            '${lp}@${name}: a #${public_format}.\n')
        return string.Template(template).substitute(translations)


    """
    Implementation of the setter
    """
    def build_setter_implementation(self, line_prefix, variable_name_from, variable_name_to):
        if not self.visible:
            return ""

        needs_cast = True if self.public_format != self.private_format else False
        translations = { 'lp'       : line_prefix,
                         'from'     : variable_name_from,
                         'to'       : variable_name_to,
                         'cast_ini' : '(' + self.private_format + ')(' if needs_cast else '',
                         'cast_end' : ')' if needs_cast else '' }

        template = (
            '${lp}${to} = ${cast_ini}${from}${cast_end};\n')
        return string.Template(template).substitute(translations)


    """
    Documentation for the struct field
    """
    def build_struct_field_documentation(self, line_prefix, variable_name):
        translations = { 'lp'            : line_prefix,
                         'public_format' : self.public_format,
                         'name'          : variable_name }

        template = (
            '${lp}@${name}: a #${public_format}.\n')
        return string.Template(template).substitute(translations)