summaryrefslogtreecommitdiff
path: root/build-aux/qmi-codegen/VariableString.py
blob: 53fe2b7c9b2ced9c3863c6b478f04b25f860651e (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
# -*- 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-2022 Aleksander Morgado <aleksander@aleksander.es>
# Copyright (c) 2022 Qualcomm Innovation Center, Inc.
#

import string
import utils
from Variable import Variable

"""
Variable type for Strings ('string' format)
"""
class VariableString(Variable):

    def __init__(self, service, dictionary):

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

        self.private_format = 'gchar *'
        self.public_format = self.private_format
        self.element_type = 'utf8'

        # Public format for the GIR array compat methods in 1.32
        self.private_format_gir = self.private_format
        self.public_format_gir = self.public_format
        self.element_type_gir = self.element_type

        if 'fixed-size' in dictionary:
            self.is_fixed_size = True
            # Fixed-size strings
            self.needs_dispose = False
            self.length_prefix_size = 0
            self.n_size_prefix_bytes = 0
            self.fixed_size = dictionary['fixed-size']
            self.max_size = ''
        else:
            self.is_fixed_size = False
            self.fixed_size = '-1'
            # Variable-length strings in heap
            self.needs_dispose = True
            self.clear_method = 'qmi_helpers_clear_string'
            self.free_method_gir = 'g_free'
            if 'size-prefix-format' in dictionary:
                if dictionary['size-prefix-format'] == 'guint8':
                    self.length_prefix_size = 8
                    self.n_size_prefix_bytes = 1
                elif dictionary['size-prefix-format'] == 'guint16':
                    self.length_prefix_size = 16
                    self.n_size_prefix_bytes = 2
                else:
                    raise ValueError('Invalid size prefix format (%s): not guint8 or guint16' % dictionary['size-prefix-format'])
            # Strings which are given as the full value of a TLV and which don't have
            # a explicit 'size-prefix-format' will NOT have a length prefix
            elif 'type' in dictionary and dictionary['type'] == 'TLV':
                self.length_prefix_size = 0
                self.n_size_prefix_bytes = 0
            else:
                # Default to UINT8
                self.length_prefix_size = 8
                self.n_size_prefix_bytes = 1
            self.max_size = dictionary['max-size'] if 'max-size' in dictionary else ''


    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 }

        if self.is_fixed_size:
            translations['fixed_size'] = self.fixed_size

            # Fixed sized strings exposed in public fields will need to be
            # explicitly allocated in heap
            if self.public:
                translations['fixed_size_plus_one'] = int(self.fixed_size) + 1
                template = (
                    '${lp}${variable_name} = g_malloc (${fixed_size_plus_one});\n'
                    '${lp}if (!qmi_message_tlv_read_fixed_size_string (message, init_offset, &offset, ${fixed_size}, &${variable_name}[0], ${error})) {\n'
                    '${lp}    g_free (${variable_name});\n'
                    '${lp}    ${variable_name} = NULL;\n'
                    '${lp}    goto ${tlv_out};\n'
                    '${lp}}\n'
                    '${lp}${variable_name}[${fixed_size}] = \'\\0\';\n')
            else:
                template = (
                    '${lp}if (!qmi_message_tlv_read_fixed_size_string (message, init_offset, &offset, ${fixed_size}, &${variable_name}[0], ${error}))\n'
                    '${lp}    goto ${tlv_out};\n'
                    '${lp}${variable_name}[${fixed_size}] = \'\\0\';\n')
        else:
            translations['n_size_prefix_bytes'] = self.n_size_prefix_bytes
            translations['max_size'] = self.max_size if self.max_size != '' else '0'
            template = (
                '${lp}if (!qmi_message_tlv_read_string (message, init_offset, &offset, ${n_size_prefix_bytes}, ${max_size}, &(${variable_name}), ${error}))\n'
                '${lp}    goto ${tlv_out};\n')
        f.write(string.Template(template).substitute(translations))


    def emit_buffer_write(self, f, line_prefix, tlv_name, variable_name):
        translations = { 'lp'                  : line_prefix,
                         'tlv_name'            : tlv_name,
                         'variable_name'       : variable_name,
                         'fixed_size'          : self.fixed_size,
                         'n_size_prefix_bytes' : self.n_size_prefix_bytes }

        template = (
            '${lp}if (!qmi_message_tlv_write_string (self, ${n_size_prefix_bytes}, ${variable_name}, ${fixed_size}, error)) {\n'
            '${lp}    g_prefix_error (error, "Cannot write string in TLV \'${tlv_name}\': ");\n'
            '${lp}    return NULL;\n'
            '${lp}}\n')

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


    def emit_get_printable(self, f, line_prefix, is_personal):
        translations = { 'lp' : line_prefix }

        if self.is_fixed_size:
            translations['fixed_size'] = self.fixed_size
            translations['fixed_size_plus_one'] = int(self.fixed_size) + 1
            template = (
                '\n'
                '${lp}{\n'
                '${lp}    gchar tmp[${fixed_size_plus_one}] = { \'\\0\' };\n'
                '\n'
                '${lp}    if (!qmi_message_tlv_read_fixed_size_string (message, init_offset, &offset, ${fixed_size}, &tmp[0], &error))\n'
                '${lp}        goto out;\n')
        else:
            translations['n_size_prefix_bytes'] = self.n_size_prefix_bytes
            translations['max_size'] = self.max_size if self.max_size != '' else '0'
            template = (
                '\n'
                '${lp}{\n'
                '${lp}    g_autofree gchar *tmp = NULL;\n'
                '\n'
                '${lp}    if (!qmi_message_tlv_read_string (message, init_offset, &offset, ${n_size_prefix_bytes}, ${max_size}, &tmp, &error))\n'
                '${lp}        goto out;\n')

        if self.personal_info or is_personal:
            translations['if_show_field'] = 'if (qmi_utils_get_show_personal_info ()) '
        else:
            translations['if_show_field'] = ''

        template += (
            '${lp}    ${if_show_field}{\n'
            '${lp}        g_string_append (printable, tmp);\n')

        if self.personal_info or is_personal:
            template += (
                '${lp}    } else {\n'
                '${lp}        g_string_append_printf (printable, "\'###\'");\n')

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

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


    def build_variable_declaration(self, line_prefix, variable_name):
        translations = { 'lp'   : line_prefix,
                         'name' : variable_name }

        if self.is_fixed_size:
            translations['fixed_size_plus_one'] = int(self.fixed_size) + 1
            template = (
                '${lp}gchar ${name}[${fixed_size_plus_one}];\n')
        else:
            template = (
                '${lp}gchar *${name};\n')
        return string.Template(template).substitute(translations)


    def build_struct_field_declaration(self, line_prefix, variable_name):
        translations = { 'lp'   : line_prefix,
                         'name' : variable_name }

        # Fixed sized strings given in public structs are given as pointers,
        # instead of as fixed-sized arrays directly in the struct.
        template = (
            '${lp}gchar *${name};\n')
        return string.Template(template).substitute(translations)


    def build_getter_declaration(self, line_prefix, variable_name):
        if not self.visible:
            return ""

        translations = { 'lp'   : line_prefix,
                         'name' : variable_name }

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


    def build_getter_documentation(self, line_prefix, variable_name):
        if not self.visible:
            return ""

        translations = { 'lp'   : line_prefix,
                         'name' : variable_name }

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


    def build_getter_implementation(self, line_prefix, variable_name_from, variable_name_to):
        if not self.visible:
            return ""

        translations = { 'lp'   : line_prefix,
                         'from' : variable_name_from,
                         'to'   : variable_name_to }

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


    def build_setter_declaration(self, line_prefix, variable_name):
        if not self.visible:
            return ""

        translations = { 'lp'   : line_prefix,
                         'name' : variable_name }

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


    def build_setter_documentation(self, line_prefix, variable_name):
        if not self.visible:
            return ""

        translations = { 'lp'   : line_prefix,
                         'name' : variable_name }

        if self.is_fixed_size:
            translations['fixed_size'] = self.fixed_size
            template = (
                '${lp}@${name}: a constant string of exactly ${fixed_size} characters.\n')
        elif self.max_size != '':
            translations['max_size'] = self.max_size
            template = (
                '${lp}@${name}: a constant string with a maximum length of ${max_size} characters.\n')
        else:
            template = (
                '${lp}@${name}: a constant string.\n')
        return string.Template(template).substitute(translations)


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

        translations = { 'lp'   : line_prefix,
                         'from' : variable_name_from,
                         'to'   : variable_name_to }

        if self.is_fixed_size:
            translations['fixed_size'] = self.fixed_size
            template = (
                '${lp}if (!${from} || strlen (${from}) != ${fixed_size}) {\n'
                '${lp}    g_set_error (error,\n'
                '${lp}                 QMI_CORE_ERROR,\n'
                '${lp}                 QMI_CORE_ERROR_INVALID_ARGS,\n'
                '${lp}                 "Input variable \'${from}\' must be ${fixed_size} characters long");\n'
                '${lp}    return FALSE;\n'
                '${lp}}\n'
                '${lp}memcpy (${to}, ${from}, ${fixed_size});\n'
                '${lp}${to}[${fixed_size}] = \'\\0\';\n')
        else:
            template = ''
            if self.max_size != '':
                translations['max_size'] = self.max_size
                template += (
                    '${lp}if (${from} && strlen (${from}) > ${max_size}) {\n'
                    '${lp}    g_set_error (error,\n'
                    '${lp}                 QMI_CORE_ERROR,\n'
                    '${lp}                 QMI_CORE_ERROR_INVALID_ARGS,\n'
                    '${lp}                 "Input variable \'${from}\' must be less than ${max_size} characters long");\n'
                    '${lp}    return FALSE;\n'
                    '${lp}}\n')
            template += (
                '${lp}g_free (${to});\n'
                '${lp}${to} = g_strdup (${from} ? ${from} : "");\n')

        return string.Template(template).substitute(translations)


    def build_struct_field_documentation(self, line_prefix, variable_name):
        translations = { 'lp'   : line_prefix,
                         'name' : variable_name }

        if self.is_fixed_size:
            translations['fixed_size'] = self.fixed_size
            template = (
                '${lp}@${name}: a string of exactly ${fixed_size} characters.\n')
        elif self.max_size != '':
            translations['max_size'] = self.max_size
            template = (
                '${lp}@${name}: a string with a maximum length of ${max_size} characters.\n')
        else:
            template = (
                '${lp}@${name}: a string.\n')
        return string.Template(template).substitute(translations)


    def build_dispose(self, line_prefix, variable_name):
        # Fixed-size strings don't need dispose
        if self.is_fixed_size and not self.public:
            return ''

        translations = { 'lp'            : line_prefix,
                         'variable_name' : variable_name }

        template = (
            '${lp}g_clear_pointer (&${variable_name}, (GDestroyNotify)g_free);\n')
        return string.Template(template).substitute(translations)


    def build_copy_gir(self, line_prefix, variable_name_from, variable_name_to):
        translations = { 'lp'                 : line_prefix,
                         'variable_name_from' : variable_name_from,
                         'variable_name_to'   : variable_name_to }

        template = (
            '${lp}${variable_name_to} = g_strdup (${variable_name_from});\n')
        return string.Template(template).substitute(translations)


    def build_copy_to_gir(self, line_prefix, variable_name_from, variable_name_to):
        return self.build_copy_gir (line_prefix, variable_name_from, variable_name_to)


    def build_copy_from_gir(self, line_prefix, variable_name_from, variable_name_to):
        return self.build_copy_gir (line_prefix, variable_name_from, variable_name_to)


    def flag_public(self):
        # Call the parent method
        Variable.flag_public(self)
        # Fixed-sized strings will need dispose if they are in the public header
        if self.is_fixed_size:
            self.needs_dispose = True