-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod_ddt_data.py
357 lines (302 loc) · 12.2 KB
/
mod_ddt_data.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
import math
import mod_globals
from mod_utils import *
# Returns signed value from 16 bits (2 bytes)
def hex16_tosigned(value):
return -(value & 0x8000) | (value & 0x7fff)
# Returns signed value from 8 bits (1 byte)
def hex8_tosigned(value):
return -(value & 0x80) | (value & 0x7f)
class decu_data:
Name = ""
Comment = ""
Mnemonic = ""
Description = ""
Bytes = False
BytesCount = 1
BytesASCII = False
Bits = False
BitsCount = 8
signed = False
Binary = False
List = {}
Scaled = False
Step = 1.0
Offset = 0.0
DivideBy = 1.0
Format = ''
Unit = ''
def __str__(self):
li = ''
for k in self.List.keys():
li = li + '\n'+'%4s:%s'%(k,self.List[k])
out = '''
Name = %s
Comment = %s
Mnemonic = %s
Description = %s
Bytes = %d
BytesCount = %d
BytesASCII = %d
Bits = %d
BitsCount = %d
signed = %d
Binary = %d
List = %s
Scaled = %d
Step = %f
Offset = %f
DivideBy = %f
Format = %s
Unit = %s
''' % (self.Name, self.Comment, self.Mnemonic, self.Description, self.Bytes, self.BytesCount, self.BytesASCII, self.Bits, self.BitsCount, self.signed, self.Binary, li, self.Scaled, self.Step, self.Offset, self.DivideBy, self.Format, self.Unit)
return pyren_encode(out)
def __init__(self, dt):
ns = {'ns0': 'http://www-diag.renault.com/2002/ECU', 'ns1': 'http://www-diag.renault.com/2002/screens'}
self.Name = dt.attrib["Name"]
self.Comment = ""
Comment = dt.findall("ns0:Comment",ns)
if Comment: self.Comment=Comment[0].text
self.Mnemonic = ""
Mnemonic = dt.findall("ns0:Mnemonic",ns)
if Mnemonic: self.Mnemonic=Mnemonic[0].text
self.Description = ""
Description = dt.findall("ns0:Description",ns)
if Description: self.Description=Description[0].text.replace('<![CDATA[','').replace(']]>','')
Bytes = dt.findall("ns0:Bytes", ns)
if len(Bytes):
by = Bytes[0]
self.Bytes = True
self.BytesCount = 1
if "count" in by.attrib.keys():
self.BytesCount = int(math.ceil(float(by.attrib["count"])))
self.BitsCount = self.BytesCount * 8
self.BytesASCII = False
if "ascii" in by.attrib.keys():
self.BytesASCII = True
Bits = dt.findall("ns0:Bits", ns)
if Bits:
bi = Bits[0]
self.Bits = True
self.BitsCount = 8
self.BytesASCII = False
if "ascii" in bi.attrib.keys():
self.BytesASCII = True
if "count" in bi.attrib.keys():
self.BitsCount = int(bi.attrib["count"])
self.BytesCount = int(math.ceil(self.BitsCount / 8.0))
self.signed = False
if "signed" in bi.attrib.keys():
self.signed = True
self.Binary = False
Binary = bi.findall("ns0:Binary", ns)
if len(Binary): self.Binary = True
self.List = {}
Items = bi.findall("ns0:List/ns0:Item", ns)
if len(Items):
for i in Items:
Value = 0
if "Value" in i.attrib.keys():
Value = int(i.attrib["Value"])
Text = ""
if "Text" in i.attrib.keys():
Text = i.attrib["Text"]
if ';' in Text: Text = Text.split(';')[0].strip()
self.List[Value] = Text
self.Scaled = False
Scaled = bi.findall("ns0:Scaled", ns)
if len(Scaled):
sc = Scaled[0]
self.Scaled = True
self.Step = 1.0
if "Step" in sc.attrib.keys():
self.Step = float(sc.attrib["Step"])
self.Offset = 0.0
if "Offset" in sc.attrib.keys() and sc.attrib["Offset"]!='':
self.Offset = float (sc.attrib["Offset"])
self.DivideBy = 1.0
if "DivideBy" in sc.attrib.keys():
self.DivideBy = float (sc.attrib["DivideBy"])
self.Format = ""
if "Format" in sc.attrib.keys():
self.Format = sc.attrib["Format"]
self.Unit = ""
if "Unit" in sc.attrib.keys():
self.Unit = sc.attrib["Unit"]
def setValue(self, value, bytes_list, dataitem, test_mode=False):
start_byte = dataitem.FirstByte - 1
start_bit = dataitem.BitOffset
little_endian = False
if dataitem.Endian == "Little":
little_endian = True
if dataitem.Endian == "Big":
little_endian = False
if self.BytesASCII:
value = str(value)
if self.BytesCount > len(value):
value = value.ljust(self.BytesCount)
if self.BytesCount < len(value):
value = value[0:self.BytesCount]
asciival = ""
for i in range(self.BytesCount):
if not test_mode:
asciival += hex(ord(value[i]))[2:].upper()
else:
asciival += "FF"
value = asciival
if self.Scaled:
if not test_mode:
try:
value = float(value)
except:
return None
# Input value must be base 10
value = int((value * float(self.DivideBy) - float(self.Offset)) / float(self.Step))
else:
value = int("0x" + value, 16)
else:
if not test_mode:
# Check input length and validity
if not all(c in string.hexdigits for c in value):
return None
# Value is base 16
value = int('0x' + str(value), 16)
else:
value = int("0x" + value, 16)
valueasbin = bin(value)[2:].zfill(self.BitsCount)
numreqbytes = int(math.ceil(float(self.BitsCount + start_bit) / 8.))
request_bytes = bytes_list[start_byte:start_byte + numreqbytes]
requestasbin = ""
for r in request_bytes:
requestasbin += bin(int(r, 16))[2:].zfill(8)
requestasbin = list(requestasbin)
if little_endian:
remainingbits = self.BitsCount
lastbit = 7 - start_bit + 1
firstbit = lastbit - self.BitsCount
if firstbit < 0:
firstbit = 0
count = 0
for i in range(firstbit, lastbit):
requestasbin[i] = valueasbin[count]
count += 1
remainingbits -= count
currentbyte = 1
while remainingbits >= 8:
for i in range(0, 8):
requestasbin[currentbyte * 8 + i] = valueasbin[count]
count += 1
remainingbits -= 8
currentbyte += 1
if remainingbits > 0:
lastbit = 8
firstbit = lastbit - remainingbits
for i in range(firstbit, lastbit):
requestasbin[currentbyte * 8 + i] = valueasbin[count]
count += 1
else:
for i in range(self.BitsCount):
requestasbin[i + start_bit] = valueasbin[i]
requestasbin = "".join(requestasbin)
valueasint = int("0b" + requestasbin, 2)
valueashex = hex(valueasint)[2:].replace("L", "").zfill(numreqbytes * 2).upper()
for i in range(numreqbytes):
bytes_list[i + start_byte] = valueashex[i * 2:i * 2 + 2].zfill(2)
return bytes_list
def getDisplayValue(self, elm_data, dataitem, ecu_endian):
value = self.getHexValue(elm_data, dataitem, ecu_endian)
if value is None:
return None
if self.BytesASCII:
return bytes.fromhex(value).decode('utf-8')
if not self.Scaled:
val = int('0x' + value, 16)
if self.signed:
if self.BytesCount == 1:
val = hex8_tosigned(val)
elif self.BytesCount == 2:
val = hex16_tosigned(val)
else:
print("Warning, cannot get signed value for %s" % dataitem.Name)
if val in self.List:
return self.List[val]
return value
value = int('0x' + value, 16)
if self.signed:
if self.BytesCount == 1:
value = hex8_tosigned(value)
elif self.BytesCount == 2:
value = hex16_tosigned(value)
if self.DivideBy == 0:
print("Division by zero, please check data item : ", dataitem.Name)
return None
res = (float(value) * float(self.Step) + float(self.Offset)) / float(self.DivideBy)
if len(self.Format) and '.' in self.Format:
acc = len(self.Format.split('.')[1])
fmt = '%.' + str(acc) + 'f'
res = fmt % res
else:
if int(res) == res:
return str(int(res))
return str(res)
def getHexValue(self, resp, dataitem, ecu_endian):
little_endian = False
if ecu_endian == "Little":
little_endian = True
# Data cleaning
resp = resp.strip().replace(' ', '')
if not all(c in string.hexdigits for c in resp): resp = ''
resp.replace(' ', '')
res_bytes = [resp[i:i + 2] for i in range(0, len(resp), 2)]
# Data count
startByte = dataitem.FirstByte
startBit = dataitem.BitOffset
bits = self.BitsCount
databytelen = int(math.ceil(float(self.BitsCount) / 8.0))
reqdatabytelen = int(math.ceil(float(self.BitsCount + startBit) / 8.0))
sb = startByte - 1
if (sb * 2 + databytelen * 2) > (len(resp)):
return None
hexval = resp[sb * 2:(sb + reqdatabytelen) * 2]
hextobin = ""
for b in res_bytes[sb:sb + reqdatabytelen]:
hextobin += bin(int(b, 16))[2:].zfill(8)
if len(hexval) == 0:
return None
if little_endian:
# Don't like this method
totalremainingbits = bits
lastbit = 7 - startBit + 1
firstbit = lastbit - bits
if firstbit < 0:
firstbit = 0
tmp_bin = hextobin[firstbit:lastbit]
totalremainingbits -= lastbit - firstbit
if totalremainingbits > 8:
offset1 = 8
offset2 = offset1 + ((reqdatabytelen - 2) * 8)
tmp_bin += hextobin[offset1:offset2]
totalremainingbits -= offset2 - offset1
if totalremainingbits > 0:
offset1 = (reqdatabytelen - 1) * 8
offset2 = offset1 - totalremainingbits
tmp_bin += hextobin[offset2:offset1]
totalremainingbits -= offset1 - offset2
if totalremainingbits != 0:
print("getHexValue >> abnormal remaining bytes ", bits, totalremainingbits)
hexval = hex(int("0b" + tmp_bin, 2))[2:].replace("L", "")
else:
valtmp = "0b" + hextobin[startBit:startBit + bits]
hexval = hex(int(valtmp, 2))[2:].replace("L", "")
# Resize to original length
hexval = hexval.zfill(databytelen * 2)
return hexval
class decu_datas:
def __init__(self, data_list, xdoc):
ns = {'ns0': 'http://www-diag.renault.com/2002/ECU', 'ns1': 'http://www-diag.renault.com/2002/screens'}
data = xdoc.findall('ns0:Target/ns0:Datas', ns)
datas = data[0].findall('ns0:Data', ns)
if datas:
for dt in datas:
data = decu_data( dt )
data_list[data.Name] = data