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
|
#!/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
#
import string
import utils
from Struct import Struct
from Field import Field
class FieldStruct(Field):
"""
The FieldStruct class takes care of handling 'struct' format based
Input and Output TLVs
"""
def __init__(self, prefix, dictionary, common_objects_dictionary):
# Call the parent constructor
Field.__init__(self, prefix, dictionary, common_objects_dictionary)
# The field type will be the given struct name
self.field_type = utils.build_camelcase_name(self.fullname)
# Set a struct as content
self.contents = Struct(self.field_type,
dictionary['contents'])
def emit_types(self, hfile, cfile):
'''
Emit the Struct type info
'''
self.contents.emit_packed(cfile)
self.contents.emit(hfile)
def emit_input_tlv_add(self, cfile, line_prefix):
translations = { 'name' : self.name,
'tlv_id' : self.id_enum_name,
'field_type' : self.field_type,
'variable_name' : self.variable_name,
'lp' : line_prefix }
template = (
'${lp}${field_type}Packed tmp;\n'
'\n')
cfile.write(string.Template(template).substitute(translations))
# uint32 and uint16 fields need to be converted to host-endianness
for struct_field in self.contents.members:
cfile.write('%s%s;\n' % (line_prefix,
utils.le_from_he ('input->' + self.variable_name + '.' + utils.build_underscore_name(struct_field['name']),
'tmp.' + utils.build_underscore_name(struct_field['name']),
struct_field['format'])))
template = (
'\n'
'${lp}if (!qmi_message_tlv_add (self,\n'
'${lp} (guint8)${tlv_id},\n'
'${lp} sizeof (tmp),\n'
'${lp} &tmp,\n'
'${lp} error)) {\n'
'${lp} g_prefix_error (error, \"Couldn\'t set the ${name} TLV: \");\n'
'${lp} qmi_message_unref (self);\n'
'${lp} return NULL;\n'
'${lp}}\n')
cfile.write(string.Template(template).substitute(translations))
def emit_output_tlv_get(self, f, line_prefix):
translations = { 'name' : self.name,
'container_underscore' : utils.build_underscore_name (self.prefix),
'field_type' : self.field_type,
'tlv_id' : self.id_enum_name,
'variable_name' : self.variable_name,
'lp' : line_prefix,
'error' : 'error' if self.mandatory == 'yes' else 'NULL'}
template = (
'${lp}${field_type}Packed tmp;\n'
'\n'
'${lp}if (qmi_message_tlv_get (message,\n'
'${lp} ${tlv_id},\n'
'${lp} sizeof (tmp),\n'
'${lp} &tmp,\n'
'${lp} ${error})) {\n'
'${lp} self->${variable_name}_set = TRUE;\n')
f.write(string.Template(template).substitute(translations))
for struct_field in self.contents.members:
f.write('%s %s;\n' % (line_prefix,
utils.he_from_le ('tmp.' + utils.build_underscore_name(struct_field['name']),
'self->' + self.variable_name + '.' + utils.build_underscore_name(struct_field['name']),
struct_field['format'])))
if self.mandatory == 'yes':
template = (
'${lp}} else {\n'
'${lp} g_prefix_error (error, \"Couldn\'t get the ${name} TLV: \");\n'
'${lp} ${container_underscore}_unref (self);\n'
'${lp} return NULL;\n'
'${lp}}\n')
else:
template = (
'${lp}}\n')
f.write(string.Template(template).substitute(translations))
def emit_output_tlv_get_printable(self, f):
translations = { 'name' : self.name,
'underscore' : utils.build_underscore_name (self.fullname),
'container_underscore' : utils.build_underscore_name (self.prefix),
'field_type' : self.field_type,
'tlv_id' : self.id_enum_name,
'variable_name' : self.variable_name }
template = (
'\n'
'static gchar *\n'
'${underscore}_get_printable (\n'
' QmiMessage *self,\n'
' const gchar *line_prefix)\n'
'{\n'
' GString *printable;\n'
' ${field_type}Packed tmp;\n'
'\n'
' printable = g_string_new ("");\n'
' g_assert (qmi_message_tlv_get (self,\n'
' ${tlv_id},\n'
' sizeof (tmp),\n'
' &tmp,\n'
' NULL));\n')
f.write(string.Template(template).substitute(translations))
first = False
for struct_field in self.contents.members:
translations['name_struct_field'] = struct_field['name']
translations['underscore_struct_field'] = utils.build_underscore_name(struct_field['name'])
translations['endianfix'] = utils.he_from_le ('tmp.' + utils.build_underscore_name(struct_field['name']),
'tmp.' + utils.build_underscore_name(struct_field['name']),
struct_field['format'])
template = (
' ${endianfix};'
' g_string_append_printf (printable,\n')
if struct_field['format'] == 'guint8' or \
struct_field['format'] == 'guint16' or \
struct_field['format'] == 'guin32':
template += (
' "\\n"\n'
' "%s [${name_struct_field} = %u] ",\n'
' line_prefix,\n'
' (guint)tmp.${underscore_struct_field});\n')
else:
template += (
' "\\n"\n'
' "%s [${name_struct_field} = %d] ",\n'
' line_prefix,\n'
' (gint)tmp.${underscore_struct_field});\n')
f.write(string.Template(template).substitute(translations))
f.write(
'\n'
' return g_string_free (printable, FALSE);\n'
'}\n')
|