-
-
Notifications
You must be signed in to change notification settings - Fork 238
/
Copy pathecu.py
515 lines (414 loc) · 17.5 KB
/
ecu.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
import math, string
import options, elm
from xml.dom.minidom import parse
import xml.dom.minidom
# Returns signed value from 16 bits
def s16(value):
return -(value & 0x8000) | (value & 0x7fff)
# Returns signed value from 8 bits
def s8(value):
return -(value & 0x80) | (value & 0x7f)
class Data_item:
def __init__(self, item):
self.firstbyte = 0
self.bitoffset = 0
self.ref = None
self.endian = "Big"
self.name = item.getAttribute("Name")
fb = item.getAttribute("FirstByte")
if fb: self.firstbyte = int(fb)
bo = item.getAttribute("BitOffset")
if bo: self.bitoffset = int(bo)
endian = item.getAttribute("Endian")
if endian:
self.endian = endian.encode('ascii')
ref = item.getAttribute("Ref")
if ref and ref == '1': self.ref = True
class Ecu_device:
def __init__(self, dev):
self.xmldoc = xml
self.dtc = 0
self.dtctype = 0
self.devicedata = {}
self.name = dev.getAttribute("Name")
dtc = dev.getAttribute("DTC")
if dtc: self.dtc = int(dtc)
dtctype = dev.getAttribute("Type")
if dtctype: self.dtctype = int(dtctype)
devicedata = dev.getElementsByTagName("DeviceData")
if devicedata:
for data in devicedata:
name = data.getAttribute("Name")
failureflag = data.getAttribute("FailureFlag")
self.devicedata[name] = failureflag
class Ecu_request:
def __init__(self, xml, endian):
self.xmldoc = xml
self.minbytes = 0
self.shiftbytescount = 0
self.replybytes = ''
self.manualsend = False
self.sentbytes = 0
self.dataitems = {}
self.sendbyte_dataitems = {}
self.name = 'uninit'
self.endian = endian
self.initEcuReq()
def initEcuReq(self):
self.name = self.xmldoc.getAttribute("Name")
manualsenddata = self.xmldoc.getElementsByTagName("ManuelSend").item(0)
if manualsenddata: self.manualsend = True
shiftbytescount = self.xmldoc.getElementsByTagName("ShiftBytesCount")
if shiftbytescount: self.shiftbytescount = int(shiftbytescount.item(0).firstChild.nodeValue)
replybytes = self.xmldoc.getElementsByTagName("ReplyBytes")
if replybytes: self.replybytes = replybytes.item(0).firstChild.nodeValue
receiveddata = self.xmldoc.getElementsByTagName("Received").item(0)
if receiveddata:
minbytes = receiveddata.getAttribute("MinBytes")
if minbytes: self.minbytes = int(minbytes)
dataitems = receiveddata.getElementsByTagName("DataItem")
if dataitems:
for dataitem in dataitems:
di = Data_item(dataitem)
self.dataitems[di.name] = di
sentdata = self.xmldoc.getElementsByTagName("Sent")
if sentdata:
sent = sentdata.item(0)
sentbytesdata = sent.getElementsByTagName("SentBytes")
if sentbytesdata:
self.sentbytes = sentbytesdata.item(0).firstChild.nodeValue
dataitems = sent.getElementsByTagName("DataItem")
if dataitems:
for dataitem in dataitems:
di = Data_item(dataitem)
self.sendbyte_dataitems[di.name] = di
class Ecu_data:
def __init__(self, xml):
self.xmldoc = xml
self.name = ''
self.bitscount = 8
self.scaled = False
self.signed = False
self.byte = False
self.bits = False
self.binary = False
self.bytescount = 1
self.bytesascii = False
self.step = 1.0
self.offset = 0.0
self.divideby = 1.0
self.format = ""
self.items = {}
self.lists = {}
self.description = ''
self.unit = ""
self.comment = ''
self.initData()
def setValue(self, value, bytes_list, dataitem, force_little_endian = False):
start_byte = dataitem.firstbyte - 1
start_bit = dataitem.bitoffset
little_endian = False
if dataitem.endian == "Little":
little_endian = True
if force_little_endian:
little_endian = True
if self.bytesascii:
if self.bytescount != len(value):
return None
for i in range(self.bytescount):
if not little_endian:
bytes_list[i] = ord(value[i])
else:
bytes_list[self.bytescount - i - 1] = ord(value[i])
return bytes_list
if self.scaled:
if not str(value).isdigit():
return None
value = float(value)
# Value is base 10
value = (value * float(self.divideby) - float(self.offset)) / float(self.step)
else:
if not all(c in string.hexdigits for c in value):
return None
# Value is base 16
value = int('0x' + str(value), 16)
value = int(value)
value = (value << start_bit) & (2**self.bitscount - 1)
hex_value = "{0:#0{1}x}".format(value, self.bytescount * 2 + 2)[2:].upper()
hex_bytes = [hex_value[i:i + 2] for i in range(0, len(hex_value), 2)]
n = 0
hex_len = len(hex_bytes) - 1
for h in hex_bytes:
original_byte = int('0x' + bytes_list[n + start_byte], 16)
original_value = int('0x' + h, 16)
new = original_value #| original_byte
value_formatted = "{0:#0{1}x}".format(new, 4)[2:].upper()
if not little_endian:
bytes_list[start_byte + n] = value_formatted
else:
bytes_list[start_byte + hex_len - n] = value_formatted
n += 1
return bytes_list
def getDisplayValue(self, elm_data, dataitem):
value = self.getValue(elm_data, dataitem)
if value == None:
return None
if not self.scaled and not self.bytesascii:
val = int('0x' + value, 0)
# Manage signed values
if self.signed:
if len(value) == 2:
val = s8(val)
elif len(value) == 4:
val = s16(val)
# Manage text values
if val in self.lists:
return self.lists[val]
return str(val).zfill(self.bytescount * 2)
return value
def getValue(self, elm_data, dataitem):
hv = self.getHex( elm_data, dataitem )
if hv == None:
return None
assert hv is not None
if self.scaled:
res = (int(hv, 16) * 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:
res = int(res)
return str(res)
if self.bytesascii:
res = hv.decode('hex')
return res
return hv
def getHex(self, resp, dataitem):
resp = resp.strip().replace(' ','')
if not all(c in string.hexdigits for c in resp): resp = ''
resp = ' '.join(a + b for a, b in zip(resp[::2], resp[1::2]))
bits = self.bitscount
bytes = bits/8
if bits % 8:
bytes += 1
startByte = dataitem.firstbyte
startBit = dataitem.bitoffset
sb = startByte - 1
if ((sb * 3 + bytes * 3 - 1) > (len(resp))):
return None
hexval = resp[sb * 3:(sb + bytes) * 3 - 1]
hexval = hexval.replace(" ", "")
assert len(hexval) > 0
if dataitem.endian == "Little":
a = hexval
b = ''
if not len(a) % 2:
for i in range(0, len(a), 2):
b = a[i:i + 2] + b
hexval = b
else:
print "Warning, cannot convert little endian value"
exbits = bits % 8
if exbits:
val = int(hexval, 16)
val = (val << int(startBit) >> (8 - exbits)) & (2**bits - 1)
hexval = hex(val)[2:]
if hexval[-1:].upper() == 'L':
hexval = hexval[:-1]
if len(hexval) % 2:
hexval = '0' + hexval
return hexval
def initData(self):
self.name = self.xmldoc.getAttribute("Name")
description = self.xmldoc.getElementsByTagName("Description")
if description:
self.description = description.item(0).firstChild.nodeValue.replace('<![CDATA[', '').replace(']]>', '')
comment = self.xmldoc.getElementsByTagName("Comment")
if comment:
self.comment = comment.item(0).firstChild.nodeValue.replace('<![CDATA[', '').replace(']]>', '')
lst = self.xmldoc.getElementsByTagName("List")
if lst:
for l in lst:
items = l.getElementsByTagName("Item")
for item in items:
key = int(item.getAttribute('Value'))
self.lists[key] = item.getAttribute('Text')
bytes = self.xmldoc.getElementsByTagName("Bytes")
if bytes:
self.byte = True
bytescount = bytes.item(0).getAttribute("count")
if bytescount:
self.bytescount = int(bytescount)
self.bitscount = self.bytescount * 8
bytesascii = bytes.item(0).getAttribute("ascii")
if bytesascii and bytesascii == '1': self.bytesascii = True
bits = self.xmldoc.getElementsByTagName("Bits")
if bits:
self.bits = True
bitscount = bits.item(0).getAttribute("count")
if bitscount:
self.bitscount = int(bitscount)
self.bytescount = int(math.ceil(float(bitscount) / 8.0))
signed = bits.item(0).getAttribute("signed")
if signed: self.signed = True
binary = bits.item(0).getElementsByTagName("Binary")
if binary: self.binary = True
self.items = {}
items = bits.item(0).getElementsByTagName("Item")
if items:
for item in items:
value = int(item.getAttribute("Value"))
text = item.getAttribute("Text")
self.items[text] = value
scaled_value = bits.item(0).getElementsByTagName("Scaled")
if scaled_value:
self.scaled = True
sc = scaled_value.item(0)
step = sc.getAttribute("Step")
if step:
self.step = float(step)
offset = sc.getAttribute("Offset")
if offset:
self.offset = float(offset)
divideby = sc.getAttribute("DivideBy")
if divideby:
self.divideby = float(divideby)
format = sc.getAttribute("Format")
if format:
self.format = format
unit = sc.getAttribute("Unit")
if unit: self.unit = unit
class Ecu_file:
def __init__(self, xmldoc, isfile = False):
self.requests = {}
self.data = {}
if isfile == True:
xdom = xml.dom.minidom.parse(xmldoc)
self.xmldoc = xdom.documentElement
else:
self.xmldoc = xmldoc
self.parseXML()
def parseXML(self):
if not self.xmldoc:
print("XML not found")
return
devices = self.xmldoc.getElementsByTagName("Device")
for d in devices:
ecu_dev = Ecu_device(d)
self.requests[ecu_dev.name] = ecu_dev
requests_tag = self.xmldoc.getElementsByTagName("Requests")
if requests_tag:
for request_tag in requests_tag:
endian = "Big"
endian_attr = request_tag.getAttribute("Endian")
if endian_attr:
endian = endian_attr.encode('ascii')
requests = request_tag.getElementsByTagName("Request")
for f in requests:
ecu_req = Ecu_request(f, endian)
self.requests[ecu_req.name] = ecu_req
data = self.xmldoc.getElementsByTagName("Data")
for f in data:
ecu_data = Ecu_data(f)
self.data[ecu_data.name] = ecu_data
# Protocols:
# KWP2000 FastInit MonoPoint ?ATSP 5?
# KWP2000 FastInit MultiPoint ?ATSP 5?
# KWP2000 Init 5 Baud Type I and II ?ATSP 4?
# DiagOnCAN ATSP 6
# CAN Messaging (125 kbps CAN) ?ATSP B?
# ISO8 ?ATSP 3?
class Ecu_ident:
def __init__(self, diagversion, supplier, soft, version, name, group, href, protocol):
self.protocols = [u"KWP2000 Init 5 Baud Type I and II", u"ISO8",
u"CAN Messaging (125 kbps CAN)", u"KWP2000 FastInit MultiPoint",
u"KWP2000 FastInit MonoPoint", u"DiagOnCAN"]
self.diagversion = diagversion
self.supplier = supplier
self.soft = soft
self.version = version
self.name = name
self.group = group
self.href = href
self.addr = None
self.protocol = protocol
self.hash = diagversion + supplier + soft + version
def checkWith(self, diagversion, supplier, soft, version, addr):
if self.hash != diagversion + supplier + soft + version: return False
self.addr = addr
return True
def checkProtocol(self):
if not self.protocol in self.protocols:
print "Unknown protocol '", self.protocol, "' "
class Ecu_database:
def __init__(self):
xmlfile = "ecus/eculist.xml"
xdom = xml.dom.minidom.parse(xmlfile)
self.xmldoc = xdom.documentElement
self.targets = []
self.numecu = 0
if not self.xmldoc:
print "Unable to find eculist"
return
targets = self.xmldoc.getElementsByTagName("Target")
for target in targets:
href = target.getAttribute("href")
name = target.getAttribute("Name")
group = target.getAttribute("group")
protocol = target.getAttribute("Protocol")
autoidents = target.getElementsByTagName("AutoIdents")
for autoident in autoidents:
self.numecu += 1
for ai in autoident.getElementsByTagName("AutoIdent"):
diagversion = ai.getAttribute("DiagVersion")
supplier = ai.getAttribute("Supplier")
soft = ai.getAttribute("Soft")
version = ai.getAttribute("Version")
ecu_ident = Ecu_ident(diagversion, supplier, soft, version, name, group, href, protocol)
self.targets.append(ecu_ident)
class Ecu_scanner:
def __init__(self):
self.totalecu = 0
self.ecus = {}
self.ecu_database = Ecu_database()
self.num_ecu_found = 0
def getNumEcuDb(self):
return self.ecu_database.numecu
def getNumAddr(self):
return len(elm.dnat)
def scan(self, progress=None, label=None):
self.totalecu = 0
self.ecus = {}
self.num_ecu_found = 0
if options.simulation_mode:
self.ecus["CH_84_J84_04_00"] = Ecu_ident("000", "000", "000", "00", "UCH", "GRP", "UCH_84_J84_04_00.xml", "DiagOnCan")
self.ecus["Tdb_BCEKL84_serie_4emeRev."] = Ecu_ident("000", "000", "000", "00",
"TdB", "GRP", "Tdb_BCEKL84_serie_4emeRev.xml", "DiagOnCan")
self.ecus["ACU4_X84_MK2"] = Ecu_ident("000", "000", "000", "00", "ACU", "GRP", "ACU4_X84_MK2.xml", "DiagOnCan")
# TODO : implement other protocols
i = 0
options.elm.init_can()
for addr in elm.snat.keys():
progress.setValue(i)
i += 1
TXa, RXa = options.elm.set_can_addr(addr, { 'idTx' : '', 'idRx' : '', 'ecuname' : 'SCAN' })
options.elm.start_session_can('10C0')
if options.simulation_mode:
if TXa == "74B": can_response = "61 80 82 00 14 97 39 04 33 33 30 40 50 54 87 04 00 05 00 01 00 00 00 00 00 00 01"
elif TXa == "7E0": can_response = "61 80 82 00 44 66 27 44 32 31 33 82 00 38 71 38 00 A7 74 00 56 05 02 01 00 00"
else: can_response = "61 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
else:
can_response = options.elm.request( req = '2180', positive = '61', cache = False )
if len(can_response) > 59:
diagversion = str(int(can_response[21:23],16))
supplier = can_response[24:32].replace(' ', '').decode('hex')
soft = can_response[48:53].replace(' ', '')
version = can_response[54:59].replace(' ', '')
for target in self.ecu_database.targets:
if target.checkWith(diagversion, supplier, soft, version, addr):
self.ecus[target.name] = target
self.num_ecu_found += 1
label.setText("Found %i ecu" % self.num_ecu_found)
if __name__ == '__main__':
ecur = Ecu_file("ecus/UCH_84_J84_04_00.xml", True)