Debian Codegraph @

build-aux/qmi-codegen/FieldResult.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 #
20
21 import string
22
23 import utils
24 import TypeFactory
25 from Field import Field
26
27
28 """
29 The FieldResult class takes care of handling the common 'Result' TLV
30 """
31 class FieldResult(Field):
32
33 """
34 Emit the types required to the source file (they will not be exposed in the
35 interface)
36 """
37 def emit_types(self, hfile, cfile):
38 if not TypeFactory.is_type_emitted(self.fullname):
39 TypeFactory.set_type_emitted(self.fullname)
40 self.variable.emit_types(cfile, cfile, self.since, True)
41
42
43 """
44 Emit the method responsible for getting the Result TLV contents. This
45 special TLV will have its own getter implementation, as we want to have
46 proper GErrors built from the QMI result status/code.
47 """
48 def emit_getter(self, hfile, cfile):
49 translations = { 'variable_name' : self.variable_name,
50 'prefix_camelcase' : utils.build_camelcase_name(self.prefix),
51 'prefix_underscore' : utils.build_underscore_name(self.prefix),
52 'since' : self.since }
53
54 # Emit the getter header
55 template = '\n'
56 if not self.static and self.service != 'CTL':
57 template += (
58 '\n'
59 '/**\n'
60 ' * ${prefix_underscore}_get_result:\n'
61 ' * @self: a ${prefix_camelcase}.\n'
62 ' * @error: Return location for error or %NULL.\n'
63 ' *\n'
64 ' * Get the result of the QMI operation.\n'
65 ' *\n'
66 ' * Returns: (skip): %TRUE if the QMI operation succeeded, %FALSE if @error is set.\n'
67 ' *\n'
68 ' * Since: ${since}\n'
69 ' */\n')
70 template += (
71 'gboolean ${prefix_underscore}_get_result (\n'
72 ' ${prefix_camelcase} *self,\n'
73 ' GError **error);\n')
74 hfile.write(string.Template(template).substitute(translations))
75
76 # Emit the getter source
77 template = (
78 '\n'
79 'gboolean\n'
80 '${prefix_underscore}_get_result (\n'
81 ' ${prefix_camelcase} *self,\n'
82 ' GError **error)\n'
83 '{\n'
84 ' g_return_val_if_fail (self != NULL, FALSE);\n'
85 '\n'
86 ' /* We should always have a result set in the response message */\n'
87 ' if (!self->${variable_name}_set) {\n'
88 ' g_set_error (error,\n'
89 ' QMI_CORE_ERROR,\n'
90 ' QMI_CORE_ERROR_INVALID_MESSAGE,\n'
91 ' "No \'Result\' field given in the message");\n'
92 ' return FALSE;\n'
93 ' }\n'
94 '\n'
95 ' if (self->${variable_name}_error_status == QMI_STATUS_SUCCESS) {\n'
96 ' /* Operation succeeded */\n'
97 ' return TRUE;\n'
98 ' }\n'
99 '\n'
100 ' /* Report a QMI protocol error */\n'
101 ' g_set_error (error,\n'
102 ' QMI_PROTOCOL_ERROR,\n'
103 ' (QmiProtocolError) self->${variable_name}_error_code,\n'
104 ' "QMI protocol error (%u): \'%s\'",\n'
105 ' self->${variable_name}_error_code,\n'
106 ' qmi_protocol_error_get_string ((QmiProtocolError) self->${variable_name}_error_code));\n'
107 ' return FALSE;\n'
108 '}\n')
109 cfile.write(string.Template(template).substitute(translations))
110
111
112 """
113 Emit the method responsible for getting a printable representation of this
114 TLV field.
115 """
116 def emit_tlv_helpers(self, f):
117 if TypeFactory.helpers_emitted(self.fullname):
118 return
119
120 TypeFactory.set_helpers_emitted(self.fullname)
121
122 translations = { 'name' : self.name,
123 'tlv_id' : self.id_enum_name,
124 'underscore' : utils.build_underscore_name (self.fullname) }
125
126 template = (
127 '\n'
128 'static gboolean\n'
129 '${underscore}_validate (\n'
130 ' const guint8 *buffer,\n'
131 ' guint16 buffer_len)\n'
132 '{\n'
133 ' static const guint expected_len = 4;\n'
134 '\n'
135 ' if (buffer_len < expected_len) {\n'
136 ' g_warning ("Cannot read the \'${name}\' TLV: expected \'%u\' bytes, but only got \'%u\' bytes",\n'
137 ' expected_len, buffer_len);\n'
138 ' return FALSE;\n'
139 ' }\n'
140 '\n'
141 ' return TRUE;\n'
142 '}\n')
143 f.write(string.Template(template).substitute(translations))
144
145 template = (
146 '\n'
147 'static gchar *\n'
148 '${underscore}_get_printable (\n'
149 ' QmiMessage *self,\n'
150 ' const gchar *line_prefix)\n'
151 '{\n'
152 ' gsize offset = 0;\n'
153 ' gsize init_offset;\n'
154 ' guint16 error_status;\n'
155 ' guint16 error_code;\n'
156 '\n'
157 ' if ((init_offset = qmi_message_tlv_read_init (self, ${tlv_id}, NULL, NULL)) == 0)\n'
158 ' return NULL;\n'
159 ' if (!qmi_message_tlv_read_guint16 (self, init_offset, &offset, QMI_ENDIAN_LITTLE, &error_status, NULL))\n'
160 ' return NULL;\n'
161 ' if (!qmi_message_tlv_read_guint16 (self, init_offset, &offset, QMI_ENDIAN_LITTLE, &error_code, NULL))\n'
162 ' return NULL;\n'
163 ' g_warn_if_fail (qmi_message_tlv_read_remaining_size (self, init_offset, offset) == 0);\n'
164 '\n'
165 ' if (error_status == QMI_STATUS_SUCCESS)\n'
166 ' return g_strdup ("SUCCESS");\n'
167 '\n'
168 ' return g_strdup_printf ("FAILURE: %s", qmi_protocol_error_get_string ((QmiProtocolError) error_code));\n'
169 '}\n')
170 f.write(string.Template(template).substitute(translations))
171
172
173 """
174 Add sections
175 """
176 def add_sections(self, sections):
177 translations = { 'underscore' : utils.build_underscore_name(self.name),
178 'prefix_camelcase' : utils.build_camelcase_name(self.prefix),
179 'prefix_underscore' : utils.build_underscore_name(self.prefix) }
180
181 # Public methods
182 template = (
183 '${prefix_underscore}_get_${underscore}\n')
184 sections['public-methods'] += string.Template(template).substitute(translations)