Debian Codegraph @

build-aux/qmi-codegen/MessageList.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 from Message import Message
24 import utils
25
26 """
27 The MessageList class handles the generation of all messages for a given
28 specific service
29 """
30 class MessageList:
31
32 """
33 Constructor
34 """
35 def __init__(self, collection, objects_dictionary, common_objects_dictionary):
36 # known requests enabled by the collection
37 self.request_list = []
38 # known indications enabled by the collection
39 self.indication_list = []
40 # known requests and indications disabled by the collection
41 self.unsupported_list = []
42
43 self.message_id_enum_name = None
44 self.indication_id_enum_name = None
45 self.service = None
46
47 # Loop items in the list, creating Message objects for the messages
48 # and looking for the special 'Message-ID-Enum' type
49 for object_dictionary in objects_dictionary:
50 if object_dictionary['type'] == 'Message' or \
51 object_dictionary['type'] == 'Indication':
52 message = Message(object_dictionary, common_objects_dictionary)
53 if collection is None or message.id_enum_name in collection:
54 if message.type == 'Message':
55 self.request_list.append(message)
56 else:
57 self.indication_list.append(message)
58 else:
59 self.unsupported_list.append(message)
60 elif object_dictionary['type'] == 'Message-ID-Enum':
61 self.message_id_enum_name = object_dictionary['name']
62 elif object_dictionary['type'] == 'Indication-ID-Enum':
63 self.indication_id_enum_name = object_dictionary['name']
64 elif object_dictionary['type'] == 'Service':
65 self.service = object_dictionary['name']
66
67 # We NEED the Message-ID-Enum field
68 if self.message_id_enum_name is None:
69 raise ValueError('Missing Message-ID-Enum field')
70
71 # We NEED the Service field
72 if self.service is None:
73 raise ValueError('Missing Service field')
74
75 self.request_prefix = utils.build_underscore_name("Qmi Request " + self.service).upper()
76 self.indication_prefix = utils.build_underscore_name("Qmi Indication " + self.service).upper()
77
78 def __emit_message_build_symbols(self, f):
79 template = ''
80 for message in self.request_list:
81 translations = { 'build_symbol' : message.build_symbol }
82 message_template = '#define ${build_symbol}\n'
83 template += string.Template(message_template).substitute(translations)
84 for message in self.indication_list:
85 translations = { 'build_symbol' : message.build_symbol }
86 message_template = '#define ${build_symbol}\n'
87 template += string.Template(message_template).substitute(translations)
88 if len(self.unsupported_list) > 0:
89 template += (
90 '\n'
91 '/* messages unsupported in collection */\n')
92 for message in self.unsupported_list:
93 translations = { 'build_symbol' : message.build_symbol }
94 message_template = '/* ${build_symbol} */\n'
95 template += string.Template(message_template).substitute(translations)
96 f.write(string.Template(template).substitute(translations))
97
98 """
99 Emit the enumeration of the messages found in the specific service
100 """
101 def __emit_message_ids_enum(self, f):
102 # do nothing if nothing in the supported messages list
103 if len(self.request_list) == 0:
104 return
105 translations = { 'enum_type' : utils.build_camelcase_name (self.message_id_enum_name) }
106 template = (
107 '\n'
108 'typedef enum {\n')
109 for message in self.request_list:
110 translations['enum_name'] = message.id_enum_name
111 translations['enum_value'] = message.id
112 if message.vendor is None:
113 enum_template = (
114 ' ${enum_name} = ${enum_value},\n')
115 else:
116 translations['vendor'] = message.vendor
117 enum_template = (
118 ' ${enum_name} = ${enum_value}, /* vendor ${vendor} */\n')
119 template += string.Template(enum_template).substitute(translations)
120
121 template += (
122 '} ${enum_type};\n'
123 '\n')
124 f.write(string.Template(template).substitute(translations))
125
126 """
127 Emit the enumeration of the indications found in the specific service
128 """
129 def __emit_indication_ids_enum(self, f):
130 # do nothing if nothing in the supported indications list
131 if len(self.indication_list) == 0:
132 return
133 translations = { 'enum_type' : utils.build_camelcase_name (self.indication_id_enum_name) }
134 template = (
135 '\n'
136 'typedef enum {\n')
137 for message in self.indication_list:
138 translations['enum_name'] = message.id_enum_name
139 translations['enum_value'] = message.id
140 enum_template = (
141 ' ${enum_name} = ${enum_value},\n')
142 template += string.Template(enum_template).substitute(translations)
143
144 template += (
145 '} ${enum_type};\n'
146 '\n')
147 f.write(string.Template(template).substitute(translations))
148
149
150 """
151 Emit the method responsible for getting a printable representation of all
152 messages of a given service.
153 """
154 def __emit_get_printable(self, hfile, cfile):
155 translations = { 'service' : self.service.lower() }
156
157 template = (
158 '\n'
159 '#if defined (LIBQMI_GLIB_COMPILATION)\n'
160 '\n'
161 'G_GNUC_INTERNAL\n'
162 'gchar *__qmi_message_${service}_get_printable (\n'
163 ' QmiMessage *self,\n'
164 ' QmiMessageContext *context,\n'
165 ' const gchar *line_prefix);\n'
166 '\n'
167 '#endif\n'
168 '\n')
169 hfile.write(string.Template(template).substitute(translations))
170
171 template = (
172 '\n'
173 'gchar *\n'
174 '__qmi_message_${service}_get_printable (\n'
175 ' QmiMessage *self,\n'
176 ' QmiMessageContext *context,\n'
177 ' const gchar *line_prefix)\n'
178 '{\n'
179 ' if (qmi_message_is_indication (self)) {\n'
180 ' switch (qmi_message_get_message_id (self)) {\n')
181
182 for message in self.indication_list:
183 translations['enum_name'] = message.id_enum_name
184 translations['message_underscore'] = utils.build_underscore_name (message.name)
185 inner_template = (
186 ' case ${enum_name}:\n'
187 ' return indication_${message_underscore}_get_printable (self, line_prefix);\n')
188 template += string.Template(inner_template).substitute(translations)
189
190 template += (
191 ' default:\n'
192 ' return NULL;\n'
193 ' }\n'
194 ' } else {\n'
195 ' guint16 vendor_id;\n'
196 '\n'
197 ' vendor_id = (context ? qmi_message_context_get_vendor_id (context) : QMI_MESSAGE_VENDOR_GENERIC);\n'
198 ' if (vendor_id == QMI_MESSAGE_VENDOR_GENERIC) {\n'
199 ' switch (qmi_message_get_message_id (self)) {\n')
200
201 for message in self.request_list:
202 if message.vendor is None:
203 translations['enum_name'] = message.id_enum_name
204 translations['message_underscore'] = utils.build_underscore_name (message.name)
205 inner_template = (
206 ' case ${enum_name}:\n'
207 ' return message_${message_underscore}_get_printable (self, line_prefix);\n')
208 template += string.Template(inner_template).substitute(translations)
209
210 template += (
211 ' default:\n'
212 ' return NULL;\n'
213 ' }\n'
214 ' } else {\n')
215
216 for message in self.request_list:
217 if message.vendor is not None:
218 translations['enum_name'] = message.id_enum_name
219 translations['message_underscore'] = utils.build_underscore_name (message.name)
220 translations['message_vendor'] = message.vendor
221 inner_template = (
222 ' if (vendor_id == ${message_vendor} && (qmi_message_get_message_id (self) == ${enum_name}))\n'
223 ' return message_${message_underscore}_get_printable (self, line_prefix);\n')
224 template += string.Template(inner_template).substitute(translations)
225
226 template += (
227 ' return NULL;\n'
228 ' }\n'
229 ' }\n'
230 '}\n')
231 cfile.write(string.Template(template).substitute(translations))
232
233
234 """
235 Emit the method responsible for checking whether a given message is abortable
236 """
237 def __emit_is_abortable(self, hfile, cfile):
238 # do nothing if no abortable messages in service
239 for message in self.request_list:
240 if message.abort:
241 break
242 else:
243 return
244
245 translations = { 'service' : self.service.lower() }
246
247 template = (
248 '\n'
249 '#if defined (LIBQMI_GLIB_COMPILATION)\n'
250 '\n'
251 'G_GNUC_INTERNAL\n'
252 'gboolean __qmi_message_${service}_is_abortable (\n'
253 ' QmiMessage *self,\n'
254 ' QmiMessageContext *context);\n'
255 '\n'
256 '#endif\n'
257 '\n')
258 hfile.write(string.Template(template).substitute(translations))
259
260 template = (
261 '\n'
262 'gboolean\n'
263 '__qmi_message_${service}_is_abortable (\n'
264 ' QmiMessage *self,\n'
265 ' QmiMessageContext *context)\n'
266 '{\n'
267 ' guint16 vendor_id;\n'
268 '\n'
269 ' vendor_id = (context ? qmi_message_context_get_vendor_id (context) : QMI_MESSAGE_VENDOR_GENERIC);\n'
270 ' if (vendor_id == QMI_MESSAGE_VENDOR_GENERIC) {\n'
271 ' switch (qmi_message_get_message_id (self)) {\n')
272
273 for message in self.request_list:
274 if message.vendor is None and message.abort:
275 translations['enum_name'] = message.id_enum_name
276 inner_template = (
277 ' case ${enum_name}:\n'
278 ' return TRUE;\n')
279 template += string.Template(inner_template).substitute(translations)
280
281 template += (
282 ' default:\n'
283 ' return FALSE;\n'
284 ' }\n'
285 ' } else {\n')
286
287 for message in self.request_list:
288 if message.vendor is not None and message.abort:
289 translations['enum_name'] = message.id_enum_name
290 translations['message_vendor'] = message.vendor
291 inner_template = (
292 ' if (vendor_id == ${message_vendor} && (qmi_message_get_message_id (self) == ${enum_name})) {\n'
293 ' return TRUE;\n'
294 ' }\n')
295 template += string.Template(inner_template).substitute(translations)
296
297 template += (
298 ' return FALSE;\n'
299 ' }\n'
300 '}\n')
301 cfile.write(string.Template(template).substitute(translations))
302
303
304 """
305 Emit the message list handling implementation
306 """
307 def emit(self, hfile, cfile):
308 # Always write build symbols, even for unsupported messages
309 self.__emit_message_build_symbols(hfile)
310
311 # Do nothing else if nothing in the supported lists
312 if len(self.indication_list) == 0 and len(self.request_list) == 0:
313 return
314
315 # Emit the message/indication IDs enum and the build symbols
316 self.__emit_message_ids_enum(cfile)
317 if self.indication_id_enum_name is not None:
318 self.__emit_indication_ids_enum(cfile)
319
320 # Then, emit all message handlers
321 for message in self.indication_list:
322 message.emit(hfile, cfile)
323 for message in self.request_list:
324 message.emit(hfile, cfile)
325
326 # First, emit common class code
327 utils.add_separator(hfile, 'Service-specific utils', self.service);
328 utils.add_separator(cfile, 'Service-specific utils', self.service);
329 self.__emit_get_printable(hfile, cfile)
330 self.__emit_is_abortable(hfile, cfile)
331
332 """
333 Emit the sections
334 """
335 def emit_sections(self, sfile):
336 # do nothing if nothing in the supported lists
337 if len(self.indication_list) == 0 and len(self.request_list) == 0:
338 return
339 # Emit all message sections
340 for message in self.indication_list:
341 message.emit_sections(sfile)
342 for message in self.request_list:
343 message.emit_sections(sfile)
344
345 """
346 Check whether a given message exists in the list
347 """
348 def contains(self, message_id_enum_name):
349 for message in self.request_list:
350 if message.id_enum_name == message_id_enum_name:
351 return True
352 for message in self.indication_list:
353 if message.id_enum_name == message_id_enum_name:
354 return True
355 return False