Debian Codegraph @

utils/qmidb/Structs.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 General Public License as published by the Free Software
5 # Foundation, version 2.
6 #
7 # This program is distributed in the hope that it will be useful, but WITHOUT
8 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
9 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
10 # details.
11 #
12 # You should have received a copy of the GNU General Public License along with
13 # this program; if not, write to the Free Software Foundation, Inc., 51
14 # Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15 #
16 # Copyright (C) 2011 - 2012 Red Hat, Inc.
17 #
18
19 import utils
20
21 # eDB2FragmentType
22 TYPE_FIELD = 0 # Simple field fragment
23 TYPE_STRUCT = 1 # Structure fragment
24 TYPE_CONSTANT_PAD = 2 # Pad fragment, fixed length (bits)
25 TYPE_VARIABLE_PAD_BITS = 3 # Pad fragment, variable (bits)
26 TYPE_VARIABLE_PAD_BYTES = 4 # Pad fragment, variable (bytes)
27 TYPE_FULL_BYTE_PAD = 5 # Pad fragment, pad to a full byte
28 TYPE_MSB_2_LSB = 6 # Switch to MSB -> LSB order
29 TYPE_LSB_2_MSB = 7 # Switch to LSB -> MSB order
30
31 def is_pad_type(t):
32 if t == TYPE_CONSTANT_PAD or t == TYPE_VARIABLE_PAD_BITS or \
33 t == TYPE_VARIABLE_PAD_BYTES or t == TYPE_FULL_BYTE_PAD:
34 return True
35 return False
36
37 # eDB2ModifierType
38 MOD_NONE = 0 # Modifier is not used
39 MOD_CONSTANT_ARRAY = 1 # Constant (elements) array
40 MOD_VARIABLE_ARRAY = 2 # Variable (elements) array (modval gives #elements field)
41 MOD_OBSOLETE_3 = 3 # Constant (bits) array [OBS]
42 MOD_OBSOLETE_4 = 4 # Variable (bits) array [OBS]
43 MOD_OPTIONAL = 5 # Fragment is optional
44 MOD_VARIABLE_ARRAY2 = 6 # Variable (elements) array, start/stop given
45 MOD_VARIABLE_ARRAY3 = 7 # Variable (elements) array, simple expression
46 MOD_VARIABLE_STRING1 = 8 # Variable length string (bit length)
47 MOD_VARIABLE_STRING2 = 9 # Variable length string (byte length)
48 MOD_VARIABLE_STRING3 = 10 # Variable length string (character length)
49
50
51 class FragmentBase:
52 # Struct fragments (ie, each line in Struct.txt describe each member of
53 # a struct. The format is as follows:
54 #
55 # id^order^type^fieldid^name^offset^modtype^modval
56 #
57 # 50409^1^0^54024^""^-1^2^"54023"
58 #
59 # This describes the second element (ie, element #1) of struct 50409,
60 # which is of type FIELD (0) and the value is described by Field #54024.
61 # Additionally, it's "modtype" is VARIABLE_ARRAY which is described by
62 # Field #54023. Basically, this fragment is a variable array of
63 # "Medium Preference" values, each element of which is one of the
64 # "QMI PDS Mediums" (Enum.txt, #50407 given by Field.txt #54024). The
65 # number of elements in the array is given by the "modtype" and "modval"
66 # pointers, which say that Field #54023 (which is also the first member
67 # of this struct) specifies the number of elements in this variable array.
68
69 def __init__(self, line):
70 parts = line.split('^')
71 if len(parts) < 8:
72 raise Exception("Invalid struct fragment '%s'" % line)
73 self.id = int(parts[0])
74 self.order = int(parts[1])
75 self.type = int(parts[2]) # eDB2FragmentType
76 self.value = int(parts[3]) # id of field in Fields.txt
77 self.name = parts[4].replace('"', '')
78 self.offset = int(parts[5])
79 self.modtype = int(parts[6]) # eDB2ModifierType
80 self.modval = parts[7].replace('"', '')
81
82 #Struct.txt:50021^0^0^50118^""^-1^1^"4"
83
84 def validate(self, fields, structs):
85 raise Exception("Surprising struct %d:%d field: %d" % (self.id, self.order, self.type))
86
87 # Should return size in *bits* of this struct fragment, including all
88 # sub-fragments
89 def emit(self, do_print, entity_name, indent, reserved_bits, fields, structs, enums, cur_structsize):
90 return 0
91
92 class Msb2LsbFragment(FragmentBase):
93 # Subclass for TYPE_MSB_2_LSB
94 def validate(self, fields, structs):
95 pass
96
97 class FieldFragment(FragmentBase):
98 # Subclass for TYPE_FIELD
99 def validate(self, fields, structs):
100 self.field = fields.get_child(self.value)
101
102 def emit(self, do_print, entity_name, indent, reserved_bits, fields, structs, enums, cur_structsize):
103 # modify the field like cProtocolEntityNav::ProcessFragment()
104 num_elements = 0
105 comment = ""
106 isarray = False
107
108 if self.modtype == MOD_CONSTANT_ARRAY:
109 # modval is number of elements
110 num_elements = int(self.modval)
111 elif self.modtype == MOD_VARIABLE_ARRAY or \
112 self.modtype == MOD_VARIABLE_STRING1 or \
113 self.modtype == MOD_VARIABLE_STRING2 or \
114 self.modtype == MOD_VARIABLE_STRING3:
115 # The "modval" is the field id that gives the # of elements
116 # of this variable array. That field is usually the immediately
117 # previous fragment of this struct. ie it's something like:
118 #
119 # struct foobar {
120 # u8 arraylen; <- specified by modval
121 # char array[]; <- current struct fragment
122 # }
123 #
124 fdesc = fields.get_child(int(self.modval))
125 comment = "size given by %s" % utils.nicename(fdesc.name)
126 isarray = True
127
128 return self.field.emit(do_print, indent, enums, num_elements, comment, isarray)
129
130
131 class StructFragment(FragmentBase):
132 # Subclass for TYPE_STRUCT
133 def validate(self, fields, structs):
134 self.struct = structs.get_child(self.value)
135
136 def emit(self, do_print, entity_name, indent, reserved_bits, fields, structs, enums, cur_structsize):
137 # embedded structs often won't have a name of their own
138 structname = "a_item"
139 if self.name:
140 structname = "%s_item" % utils.nicename(self.name)
141
142 bits = self.struct.emit(structname, indent, reserved_bits, fields, structs, enums)
143
144 # Ignore the condition on some structs' modvals
145 fdesc_id = 0
146 if len(self.modval) and self.modval.find("=") < 0:
147 fdesc_id = int(self.modval)
148
149 comment = ""
150 arraybits = ""
151 try:
152 fdesc = fields.get_child(fdesc_id)
153 comment = "\t /* size given by %s */" % utils.nicename(fdesc.name)
154 arraybits = "[0]"
155 except KeyError:
156 pass
157
158 if do_print:
159 varname = utils.nicename(self.name)
160 if not varname:
161 varname = "item"
162 print "%sstruct %s %s%s;%s" % ("\t" * indent, structname, utils.nicename(self.name), arraybits, comment)
163
164 return bits
165
166 class ConstantPadFragment(FragmentBase):
167 # Subclass for TYPE_CONSTANT_PAD
168 def __init__(self, line):
169 FragmentBase.__init__(self, line)
170 # padsize is total struct size (in bits) including this fragment; ie
171 # given the current struct size, pad the struct out to padsize
172 self.padsize = self.value
173
174 def validate(self, fields, structs):
175 if self.value < 0 or self.value > 1000:
176 raise Exception("Invalid constant pad size %d" % self.value)
177
178 def emit(self, do_print, entity_name, indent, reserved_bits, fields, structs, enums, cur_structsize):
179 # cur_structsize is in bits
180 if cur_structsize > self.padsize:
181 raise ValueError("Current structure size (%d) is larger than pad size (%d)!")
182
183 padbits = self.padsize - cur_structsize
184 padtype = "uint8"
185 if padbits > 8 and padbits <= 16:
186 padtype = "uint16"
187 elif padbits > 16 and padbits <= 32:
188 padtype = "uint32"
189 elif padbits > 32 and padbits <= 64:
190 padtype = "uint64"
191 else:
192 raise ValueError("FIXME: handle multi-long padding")
193
194 if do_print:
195 if padbits in [8, 16, 32]:
196 print "%s%s padding;" % ("\t" * indent, padtype)
197 else:
198 print "%s%s padding:%d;" % ("\t" * indent, padtype, padbits)
199 return padbits
200
201 class Struct:
202 def __init__(self, sid):
203 self.id = sid
204 self.fragments = []
205 self.name = "struct_%d" % sid
206
207 def add_fragment(self, fragment):
208 for f in self.fragments:
209 if fragment.order == f.order:
210 raise Exception("Struct %d already has fragment order %d" % (self.id, fragment.order))
211 self.fragments.append(fragment)
212 self.fragments.sort(lambda x, y: cmp(x.order, y.order))
213
214 def validate(self, fields, structs):
215 for f in self.fragments:
216 f.validate(fields, structs)
217
218 def emit_header(self, entity_name, cmdno, tlvno):
219 if not entity_name:
220 entity_name = self.name
221 svcname = "Unknown"
222 cmdname = "Unknown"
223 tlvname = "Unknown"
224 else:
225 parts = entity_name.split("/")
226 svcname = parts[0].upper()
227 cmdname = parts[1].upper().replace(" ", "_")
228 tlvname = parts[2]
229
230 print '/**'
231 print ' * SVC: %s' % svcname
232 print ' * CMD: 0x%04x (%s)' % (cmdno, cmdname)
233 print ' * TLV: 0x%02x (%s)' % (tlvno, tlvname)
234 print ' * ID: %d' % self.id
235 print ' */'
236
237 def need_union(self, frag_idx):
238 # scan to see if there is any pad fragment in this struct; if there is,
239 # and if there's a struct fragment before the pad fragment, we'll need
240 # to make a union
241 found_struct = False
242 for i in range(frag_idx, len(self.fragments)):
243 if self.fragments[i].type == TYPE_STRUCT:
244 found_struct = True
245 if is_pad_type(self.fragments[i].type):
246 if found_struct:
247 return True
248 break
249 return False
250
251 def emit(self, entity_name, indent, pad_bits, fields, structs, enums):
252 print '%sstruct %s {' % ("\t" * indent, utils.nicename(entity_name))
253
254 # current structure size in *bits*
255 size_in_bits = 0
256
257 # if there was a previous member we're supposed to add some padding
258 # for (ie, this struct is in a union and something is before it) do
259 # that now
260 if pad_bits:
261 print '%suint8 padding:%d;' % ("\t" * (indent + 1), pad_bits)
262 size_in_bits += pad_bits
263
264 union_frag = None
265 reserved_bits = 0
266
267 for i in range(0, len(self.fragments)):
268 frag = self.fragments[i]
269
270 if not union_frag:
271 if self.need_union(i):
272 union_frag = frag
273 indent += 1
274 print '%sunion u_%s {' % ("\t" * indent, utils.nicename(frag.field.name))
275
276 bits = frag.emit(True, entity_name, indent + 1, reserved_bits, fields, structs, enums, size_in_bits)
277 if frag == union_frag:
278 # Track the first union fragment's reserved bits; we'll ignore
279 # them for total struct size since the following struct(s) will
280 # include it's size
281 reserved_bits = bits
282 elif is_pad_type(frag.type):
283 # When we reach the pad fragment, the union terminates so
284 # clear out union-specific state
285 reserved_bits = 0
286 if union_frag:
287 print '%s};' % ("\t" * indent)
288 indent -= 1
289 union_frag = None
290 size_in_bits += bits
291 else:
292 size_in_bits += bits
293
294 print "%s};\n" % ("\t" * indent)
295 return size_in_bits
296
297 class Structs(utils.DbFile):
298 def __init__(self, path):
299 self.structs = {}
300 f = file(path + "Struct.txt")
301 for line in f:
302 if len(line.strip()) == 0:
303 continue
304
305 # parse enough of the line to get struct ID and type
306 parts = line.split('^')
307 if len(parts) < 3:
308 raise Exception("Invalid struct fragment '%s'" % line)
309
310 # make a new struct if we don't know about this one already
311 struct_id = int(parts[0])
312 try:
313 struct = self.structs[struct_id]
314 except KeyError:
315 struct = Struct(struct_id)
316 self.structs[struct.id] = struct
317
318 frag_type = int(parts[2])
319 try:
320 frag_class = { TYPE_FIELD: FieldFragment,
321 TYPE_STRUCT: StructFragment,
322 TYPE_CONSTANT_PAD: ConstantPadFragment,
323 TYPE_MSB_2_LSB: Msb2LsbFragment
324 }[frag_type]
325 except KeyError:
326 # fall back to base class
327 frag_class = FragmentBase
328
329 frag = frag_class(line.strip())
330 struct.add_fragment(frag)
331 f.close()
332
333 def validate(self, fields):
334 for s in self.structs.values():
335 s.validate(fields, self)
336
337 def has_child(self, sid):
338 return sid in self.structs
339
340 def get_child(self, sid):
341 return self.structs[sid]
342
343 def emit_unused(self, used, fields, enums):
344 print '/**** UNKNOWN TLVs ****/\n'
345 for s in self.structs.values():
346 if not s.id in used:
347 s.emit_header(None, 0, 0)
348 s.emit(None, 0, fields, self, enums)
349