-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmed_attr.py
280 lines (246 loc) · 10 KB
/
med_attr.py
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
import struct
import med_endian
import copy
from bitmap import Bitmap
MED_COMM_TYPE_END = 0x00 # end of attribute list
MED_COMM_TYPE_UNSIGNED = 0x01 # unsigned integer attr
MED_COMM_TYPE_SIGNED = 0x02 # signed integer attr
MED_COMM_TYPE_STRING = 0x03 # string attr
MED_COMM_TYPE_BITMAP = 0x04 # bitmap attr, bitmap formed from dwords
MED_COMM_TYPE_BYTES = 0x05 # sequence of bytes
med_comm_type = {
MED_COMM_TYPE_END: 'none type',
MED_COMM_TYPE_UNSIGNED: 'unsigned',
MED_COMM_TYPE_SIGNED: 'signed',
MED_COMM_TYPE_STRING: 'string',
MED_COMM_TYPE_BITMAP: 'bitmap',
MED_COMM_TYPE_BYTES: 'bytes',
}
MED_COMM_TYPE_READ_ONLY = 0x80 # this attribute is read-only
MED_COMM_TYPE_PRIMARY_KEY = 0x40 # this attribute is used to lookup object
MED_COMM_TYPE_LITTLE_ENDIAN = 0x30 # fixed endianness: little
MED_COMM_TYPE_BIG_ENDIAN = 0x20 # fixed endianness: big
MED_COMM_TYPE_MASK = 0x0f # clear read-only, primary key, little and big endian bits
MED_COMM_TYPE_MASK_ENDIAN = 0x3f # get by mask only little and big endian bits
class Attr(object):
def __init__(self, val = None):
self.val = val
def __str__(self):
s = self.name
if self.isReadonly or self.isPrimary:
s += ' ('
if self.isReadonly:
s += 'RO'
if self.isReadonly and self.isPrimary:
s += ','
if self.isPrimary:
s += 'P'
s += ')'
s += ' = '
# val type is SIGNED or UNSIGNED
if isinstance(self.val, int):
ss = '{:0'+str(self.length)+'x}'
s += ss.format(self.val)
# val type is STRING
elif isinstance(self.val, str):
s += "'"+self.val+"'"
# val type is BITMAP
elif isinstance(self.val, Bitmap):
s += '(Bitmap at '+str(self.offset)+') '
s += str(self.val)
# val type is BYTES
elif isinstance(self.val, (bytearray, bytes)):
s += '(Bytes) '
s += str(self.val)
elif isinstance(self.val, list):
# val type is array of STRING
if self.type & MED_COMM_TYPE_MASK == MED_COMM_TYPE_STRING:
s += str(self.val)
# val type is array of SIGNED or UNSIGNED
else:
for i in range(0,len(self.val)):
s += '{:016x}'.format(self.val[i])
if i < len(self.val) - 1:
s += ':'
elif type(self.val) == type(None):
s += 'Not defined yet'
else:
s += 'UNKNOWN ' + str(type(self.val))
return s
''' medusa attribute definition in 'include/linux/medusa/l4/comm.h'
struct medusa_comm_attribute_s {
u_int16_t offset; // offset of attribute in object
u_int16_t length; // bytes consumed by this attribute data in object
u_int8_t type; // data type (MED_COMM_TYPE_xxx)
char name[MEDUSA_COMM_ATTRNAME_MAX]; // string: attribute name
}
'''
MEDUSA_COMM_ATTRNAME_MAX = 32-5
def handleBitmap(x, convertBitmap):
"""Handling AfterUnpack & BeforePack functionality"""
if not isinstance(x, Bitmap) or (isinstance(x, Bitmap) and convertBitmap):
x = Bitmap(x)
if convertBitmap:
x.bytereverse()
return x
def attributeDef(medusa, endian):
medusa_comm_attribute_s = (endian+"HHB"+str(MEDUSA_COMM_ATTRNAME_MAX)+"s", 2+2+1+MEDUSA_COMM_ATTRNAME_MAX)
aoffset,alength,atype,aname = \
struct.unpack(medusa_comm_attribute_s[0], \
medusa.read(medusa_comm_attribute_s[1]))
# finish if read end attr type
if atype == MED_COMM_TYPE_END:
return None
# decode name in ascii coding
aname = aname.decode('ascii').split('\x00',1)[0]
# create Attr object
#attr = Attr(aname, atype, aoffset, alength)
attr = type(aname, (Attr,), dict())
attr.name = aname
attr.type = atype
attr.offset = aoffset
attr.length = alength
attr.isReadonly = False
attr.isPrimary = False
attr.typeStr = '' # for printing
attr.pythonType = '' # for struct (un)packing
attr.afterUnpack = None # if needed some tranformation after unpacking (i.e. for strings)
attr.beforePack = None # if needed some transformation before packing (i. e. for strings)
# parse attr type and fill other Attr attributes
atypeStr = ''
if atype & MED_COMM_TYPE_READ_ONLY:
atypeStr += 'readonly '
attr.isReadonly = True
if atype & MED_COMM_TYPE_PRIMARY_KEY:
atypeStr += 'primary '
attr.isPrimary = True
# clear READ_ONLY and PRIMARY_KEY bits
atypeStr += med_comm_type[atype & MED_COMM_TYPE_MASK]
attr.typeStr = atypeStr
# set default endianness
pythonType = endian
# if it is specified by attribute, set this one
if atype & MED_COMM_TYPE_MASK_ENDIAN:
if atype & MED_COMM_TYPE_MASK_ENDIAN == MED_COMM_TYPE_LITTLE_ENDIAN:
pythonType = med_endian.LITTLE_ENDIAN
elif atype & MED_COMM_TYPE_MASK_ENDIAN == MED_COMM_TYPE_BIG_ENDIAN:
pythonType = med_endian.BIG_ENDIAN
defaultVal = None
if atype & MED_COMM_TYPE_MASK == MED_COMM_TYPE_SIGNED:
# 16 bytes int only for acctype notify_change, '[a|c|m]time' attrs
# time struct in kernel consists from two long values
types = {'1':'b','2':'h','4':'i','8':'q','16':'2q'}
pythonType += types[str(alength)]
defaultVal = 0
elif atype & MED_COMM_TYPE_MASK == MED_COMM_TYPE_UNSIGNED:
types = {'1':'B','2':'H','4':'I','8':'Q','16':'2Q'}
pythonType += types[str(alength)]
defaultVal = 0
elif atype & MED_COMM_TYPE_MASK == MED_COMM_TYPE_STRING:
pythonType += str(alength)+'s'
attr.afterUnpack = (lambda x, *args: ' '.join([i.decode() for i in x.split(b'\0')]).strip(),)
attr.beforePack = (str.encode, 'ascii')
defaultVal = ''
elif atype & MED_COMM_TYPE_MASK == MED_COMM_TYPE_BITMAP:
# check endian of attribute vs. default endian of bitmap
if endian[0] == pythonType[0]:
convertBitmap = False
else:
convertBitmap = True
# endiannes is equal
attr.afterUnpack = (lambda x, *args: handleBitmap(x, convertBitmap),)
attr.beforePack = (lambda x, *args: handleBitmap(x, convertBitmap).tobytes(),)
pythonType += str(alength)+'s'
defaultVal = Bitmap(alength*8)
elif atype & MED_COMM_TYPE_MASK == MED_COMM_TYPE_BYTES:
pythonType += str(alength)+'s'
defaultVal = bytearray(alength)
attr.pythonType = pythonType
attr.defaultVal = defaultVal
return attr
def convert_endian(endian):
"""Converts endian representation between medusa and module bitarray
endian representation in module bitarray: 'little', 'big'
endian representation in medusa: '<' (little), '>' (big)
function performs conversion between this representations.
e.g. 'little' -> '<', 'big' -> '>' and vice versa.
"""
if endian == med_endian.LITTLE_ENDIAN: # '<' -> 'little'
return 'little'
if endian == med_endian.BIG_ENDIAN: # '>' -> 'big'
return 'big'
if endian == 'little': # 'little' -> '<'
return med_endian.LITTLE_ENDIAN
if endian == 'big': # 'big' -> '>'
return med_endian.BIG_ENDIAN
# attributes handing (pack, unpack, print format) for derived class (kclass, evtype)
class Attrs(object):
'''
initializer reads from 'medusa' interface to initialize objects values
TODO: create object factory for this purpose, because we need empty initializer
for 'UPDATE' medusa command
'''
def __init__(self, buf=None, **kwargs):
Attrs._unpack(self,buf)
for k, v in kwargs.items():
if k in self._attrDef:
self._attr[k] = v
else:
raise AttributeError(k)
def __getattr__(self, key):
if key.startswith("_") or key in ['update', 'fetch']:
return object.__getattr__(self, key)
ret = self._attr.get(key, None)
if ret is None:
raise AttributeError(key)
return ret.val
def __setattr__(self, key, val):
if key.startswith("_") or key in ['update', 'fetch']:
object.__setattr__(self, key, val)
return
ret = self._attr.get(key)
if ret is None:
print(self._attr)
raise AttributeError(key)
ret.val = val
def _unpack(self, buf=None):
self._orig = dict()
for a in sorted(self._attrDef):
attr = self._attrDef[a]
offset = attr.offset
length = attr.length
data = None
if buf != None:
data = struct.unpack(attr.pythonType,bytes(buf[offset:offset+length]))
self._orig[attr.name] = data
if data and len(data) == 1:
data = data[0]
if data and attr.afterUnpack:
# data can be an array (i.e. strings)
data = attr.afterUnpack[0](data, attr.afterUnpack[1:])
if data is None:
raise ValueError("Method afterUnpack for attribute '{}' returned empty data.".format(attr.name))
if len(data) == 1:
data = data[0]
if data is None:
data = copy.deepcopy(attr.defaultVal)
self._attr[a] = attr(data)
def _pack(self, size):
data = bytearray(size)
for a in sorted(self._attrDef):
attr = self._attrDef[a]
offset = attr.offset
length = attr.length
val = self._attr[a].val
if attr.beforePack:
val = attr.beforePack[0](val,*attr.beforePack[1:])
struct.pack_into(attr.pythonType, data, offset, val)
return data
def __str__(self):
s = str(self.__class__) + ' = {'
for a in sorted(self._attr):
s += '\n\t' + str(self._attr[a])
if self._attr:
s += '\n'
s += '}'
return str(s)