-
-
Notifications
You must be signed in to change notification settings - Fork 238
/
Copy pathecu.py
1128 lines (941 loc) · 40.7 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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
import math, string
import options
import elm
import report
import zipfile
from xml.dom.minidom import parse
import xml.dom.minidom
import json, os
import re
import glob
import argparse
from StringIO import StringIO
# 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)
def cleanhtml(raw_html):
cleanr = re.compile('<.*?>')
cleantext = re.sub(cleanr, '', raw_html)
return cleantext
def getChildNodesByName(parent, name):
nodes = []
for node in parent.childNodes:
if node.nodeType == node.ELEMENT_NODE and node.localName == name:
nodes.append(node)
return nodes
class Data_item:
def __init__(self, item, req_endian, name = ''):
self.firstbyte = 0
self.bitoffset = 0
self.ref = False
self.endian = ''
self.req_endian = req_endian
if isinstance(item, dict):
self.name = name
if item.has_key('firstbyte'): self.firstbyte = item['firstbyte']
if item.has_key('bitoffset'): self.bitoffset = item['bitoffset']
if item.has_key('ref'): self.ref = item['ref']
if item.has_key('endian'): self.endian = item['endian']
else:
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
def dump(self):
js = {}
if self.firstbyte != 0: js['firstbyte'] = self.firstbyte
if self.bitoffset != 0: js['bitoffset'] = self.bitoffset
if self.ref != False: js['ref'] = self.ref
if self.endian != '': js['endian'] = self.endian
return js
class Ecu_device:
def __init__(self, dev):
self.dtc = 0
self.dtctype = 0
self.devicedata = {}
self.name = ''
if isinstance(dev, dict):
# Json data
self.name = dev['name']
self.dtctype = dev['dtctype']
self.devicedata = dev['devicedata']
self.dtc = dev['dtc']
else:
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
def dump(self):
js = {}
js['dtc'] = self.dtc
js['dtctype'] = self.dtctype
js['devicedata'] = self.devicedata
js['name'] = self.name
return js
class Ecu_request:
def __init__(self, data, endian):
self.minbytes = 0
self.shiftbytescount = 0
self.replybytes = ''
self.manualsend = False
self.sentbytes = ''
self.dataitems = {}
self.sendbyte_dataitems = {}
self.name = ''
self.endian = endian
if isinstance(data, dict):
if data.has_key('minbytes'): self.minbytes = data['minbytes']
if data.has_key('shiftbytescount'): self.shiftbytescount = data['shiftbytescount']
if data.has_key('replybytes'): self.replybytes = data['replybytes']
if data.has_key('manualsend'): self.manualsend = data['manualsend']
if data.has_key('sentbytes'): self.sentbytes = data['sentbytes']
self.endian = data['endian']
self.name = data['name']
if data.has_key('sendbyte_dataitems'):
sbdi = data['sendbyte_dataitems']
for k, v in sbdi.iteritems():
di = Data_item(v, self.endian, k)
self.sendbyte_dataitems[k] = di
if data.has_key('receivebyte_dataitems'):
rbdi = data['receivebyte_dataitems']
for k, v in rbdi.iteritems():
di = Data_item(v, self.endian, k)
self.dataitems[k] = di
elif isinstance(data, unicode):
# Create a blank, new on
self.name = data
else:
self.xmldoc = data
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.endian)
self.dataitems[di.name] = di
sentdata = self.xmldoc.getElementsByTagName("Sent")
if sentdata:
sent = sentdata.item(0)
sentbytesdata = sent.getElementsByTagName("SentBytes")
if sentbytesdata:
if sentbytesdata.item(0).firstChild:
self.sentbytes = sentbytesdata.item(0).firstChild.nodeValue
dataitems = sent.getElementsByTagName("DataItem")
if dataitems:
for dataitem in dataitems:
di = Data_item(dataitem, self.endian)
self.sendbyte_dataitems[di.name] = di
def dump(self):
js = {}
if self.minbytes != 0: js['minbytes'] = self.minbytes
if self.shiftbytescount != 0: js['shiftbytescount'] = self.shiftbytescount
if self.replybytes != '': js['replybytes'] = self.replybytes
if self.manualsend: js['manualsend'] = self.manualsend
if self.sentbytes != '': js['sentbytes'] = self.sentbytes
js['endian'] = self.endian
js['name'] = self.name
sdi = {}
for key, value in self.sendbyte_dataitems.iteritems():
sdi[key] = value.dump()
if len(sdi):
js['sendbyte_dataitems'] = sdi
rdi = {}
for key, value in self.dataitems.iteritems():
rdi[key] = value.dump()
if len(rdi):
js['receivebyte_dataitems'] = rdi
return js
def dump_dataitems(self):
di = {}
for key, value in self.dataitems.iteritems():
di[key] = value.dump()
return di
def dump_sentdataitems(self):
di = {}
for key, value in self.sendbyte_dataitems.iteritems():
di[key] = value.dump()
return di
class Ecu_data:
def __init__(self, data, name=''):
self.bitscount = 8
self.scaled = False
self.signed = False
self.byte = 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.name = name
if data:
self.init(data)
def init(self, data):
if isinstance(data, dict):
if data.has_key('bitscount'): self.bitscount = data['bitscount']
if data.has_key('scaled'): self.scaled = data['scaled']
if data.has_key('signed'): self.signed = data['signed']
if data.has_key('byte'): self.byte = data['byte']
if data.has_key('binary'): self.binary = data['binary']
if data.has_key('step'): self.step = data['step']
if data.has_key('offset'): self.offset = data['offset']
if data.has_key('divideby'): self.divideby = data['divideby']
if data.has_key('format'): self.format = data['format']
if data.has_key('bytescount'): self.bytescount = data['bytescount']
if data.has_key('unit'): self.unit = data['unit']
if data.has_key('comment'): self.comment = data['comment']
if data.has_key('items'):
for k, v in data['items'].iteritems():
self.items[k] = v
if data.has_key('lists'):
for k, v in data['lists'].iteritems():
self.lists[k] = v
else:
self.xmldoc = data
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 '.' in bytescount:
bytescount = bytescount.split('.')[0]
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:
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
def dump(self):
js = {}
if self.bitscount != 8:
js['bitscount'] = self.bitscount
if self.scaled != False:
js['scaled'] = self.scaled
if self.signed != False:
js['signed'] = self.signed
if self.byte != False:
js['byte'] = self.byte
if self.binary != False:
js['binary'] = self.binary
if self.bytescount != 1:
js['bytescount'] = self.bytescount
if self.bytesascii != False:
js['bytesascii'] = self.bytesascii
if self.step != 1:
js['step'] = self.step
if self.offset != 0:
js['offset'] = self.offset
if self.divideby != 1:
js['divideby'] = self.divideby
if self.format != '':
js['format'] = self.format
if len(self.items) > 0:
itms = {}
for k, v in self.items.iteritems():
itms[k] = v
js['items'] = itms
if len(self.lists) > 0:
itms = {}
for k, v in self.lists.iteritems():
itms[k] = v
js['lists'] = itms
if self.unit != '':
js['unit'] = self.unit
if self.comment != '':
js['comment'] = cleanhtml(self.comment)
return self.name, js
def setValue(self, value, bytes_list, dataitem, request_endian, test_mode=False):
start_byte = dataitem.firstbyte - 1
start_bit = dataitem.bitoffset
little_endian = False
if request_endian == "Little":
little_endian = True
# It seems that DataItem can override Request endianness
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:
# Little endian coding is really weird :/
# Cannot figure out why it's being used
# But tried to do my best to mimic the read/write process
# Actually, need to do it in 3 steps
remainingbits = self.bitscount
# Step 1
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
# Step 2
currentbyte = 1
while remainingbits >= 8:
for i in range(0, 8):
requestasbin[currentbyte * 8 + i] = valueasbin[count]
count += 1
remainingbits -= 8
currentbyte += 1
# Step 3
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, req_endian):
value = self.getHexValue(elm_data, dataitem, req_endian)
if value is None:
return None
if self.bytesascii:
return value.decode('hex')
# I think we want Hex format for non scaled values
if not self.scaled:
val = int('0x' + value, 16)
# Manage mapped values
if val in self.lists:
return self.lists[val]
return value
value = int('0x' + value, 16)
# Manage signed values
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 = (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:
res = int(res)
return str(res)
def getHexValue(self, resp, dataitem, req_endian):
little_endian = False
if req_endian == "Little":
little_endian = True
if dataitem.endian == "Little":
little_endian = True
if dataitem.endian == "Big":
little_endian = False
# 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 Ecu_file:
def __init__(self, data, isfile=False):
self.requests = {}
self.devices = {}
self.data = {}
self.endianness = ''
self.ecu_protocol = ''
self.ecu_send_id = "00"
self.ecu_recv_id = "00"
self.fastinit = False
self.kw1 = ""
self.kw2 = ""
self.funcname = ""
self.funcaddr = ""
if not data:
return
if isfile and '.json' in str(data):
data = "./json/" + data
if os.path.exists(data):
jsfile = open(data, "r")
jsdata = jsfile.read()
jsfile.close()
else:
# Zipped json here
zf = zipfile.ZipFile('json/ecus.zip', mode='r')
jsdata = zf.read(data)
ecudict = json.loads(jsdata)
if "obd" in ecudict:
self.ecu_protocol = ecudict['obd']['protocol']
if self.ecu_protocol == "CAN":
self.ecu_send_id = ecudict['obd']['send_id']
self.ecu_recv_id = ecudict['obd']['recv_id']
if self.ecu_protocol == "KWP2000":
self.fastinit = ecudict['obd']['fastinit']
self.funcaddr = ecudict['obd']['funcaddr']
self.funcname = ecudict['obd']['funcname']
if "kw1" in ecudict['obd']:
self.kw1 = ecudict['obd']['kw1']
self.kw2 = ecudict['obd']['kw2']
if 'endian' in ecudict:
self.endianness = ecudict['endian']
devices = ecudict['devices']
for device in devices:
ecu_dev = Ecu_device(device)
self.devices[ecu_dev.name] = ecu_dev
requests = ecudict['requests']
for request in requests:
ecu_req = Ecu_request(request, self.endianness)
self.requests[ecu_req.name] = ecu_req
datalist = ecudict['data']
for k, v in datalist.iteritems():
self.data[k] = Ecu_data(v, k)
else:
if isfile:
xdom = xml.dom.minidom.parse(data)
self.xmldoc = xdom.documentElement
else:
self.xmldoc = data
if not self.xmldoc:
print("XML not found")
return
target = getChildNodesByName(self.xmldoc, u"Target")
if target:
functions = getChildNodesByName(target[0], u"Function")
if functions:
self.funcaddr = hex(int(functions[0].getAttribute("Address")))[2:].upper()
self.funcname = functions[0].getAttribute("Name")
can = getChildNodesByName(target[0], u"CAN")
if can:
self.ecu_protocol = "CAN"
send_ids = getChildNodesByName(can[0], "SendId")
if send_ids:
send_id = send_ids[0]
can_id = getChildNodesByName(send_id, "CANId")
if can_id:
self.ecu_send_id = hex(int(can_id[0].getAttribute("Value")))[2:].upper()
rcv_ids = getChildNodesByName(can[0], "ReceiveId")
if rcv_ids:
rcv_id = rcv_ids[0]
can_id = getChildNodesByName(rcv_id, "CANId")
if can_id:
self.ecu_recv_id = hex(int(can_id[0].getAttribute("Value")))[2:].upper()
k = getChildNodesByName(target[0], u"K")
if k:
kwp = getChildNodesByName(k[0], u"KWP")
iso8 = getChildNodesByName(k[0], u"ISO8")
if kwp:
kwp = kwp[0]
self.ecu_protocol = u"KWP2000"
fastinit = getChildNodesByName(kwp, u"FastInit")
if fastinit:
self.fastinit = True
self.kw1 = hex(
int(getChildNodesByName(fastinit[0], "KW1")[0].getAttribute("Value")))[
2:].upper()
self.kw2 = hex(
int(getChildNodesByName(fastinit[0], "KW2")[0].getAttribute("Value")))[
2:].upper()
elif iso8:
self.fastinit = False
if iso8:
self.ecu_protocol = "ISO8"
self.kw1 = hex(
int(getChildNodesByName(iso8[0], "KW1")[0].getAttribute("Value")))[
2:].upper()
self.kw2 = hex(
int(getChildNodesByName(iso8[0], "KW2")[0].getAttribute("Value")))[
2:].upper()
devices = self.xmldoc.getElementsByTagName("Device")
for d in devices:
ecu_dev = Ecu_device(d)
self.devices[ecu_dev.name] = ecu_dev
requests_tag = self.xmldoc.getElementsByTagName("Requests")
if requests_tag:
for request_tag in requests_tag:
endian = ''
endian_attr = request_tag.getAttribute("Endian")
if endian_attr:
endian = endian_attr.encode('ascii')
self.endianness = endian
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
def dumpJson(self):
js = {}
js['obd'] = {}
js['obd']['protocol'] = self.ecu_protocol
if self.ecu_protocol == "CAN":
js['obd']['send_id'] = self.ecu_send_id
js['obd']['recv_id'] = self.ecu_recv_id
if self.ecu_protocol == "KWP2000":
js['obd']['fastinit'] = self.fastinit
js['obd']['funcaddr'] = self.funcaddr
js['obd']['funcname'] = self.funcname
js['data'] = {}
js['requests'] = []
js['devices'] = []
if self.kw1:
js['obd']['kw1'] = self.kw1
if self.kw2:
js['obd']['kw2'] = self.kw2
if self.endianness:
js['endian'] = self.endianness
for key, value in self.data.iteritems():
name, d = value.dump()
js['data'][name] = d
for key, value in self.requests.iteritems():
js['requests'].append(value.dump())
for key, value in self.devices.iteritems():
js['devices'].append(value.dump())
dump = json.dumps(js, indent=1)
return re.sub('\n +', lambda match: '\n' + '\t' * (len(match.group().strip('\n')) / 2), dump)
# 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, projects, address):
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.projects = projects
self.href = href
self.addr = address
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 checkApproximate(self, diagversion, supplier, soft, version, addr):
if self.diagversion != diagversion:
return False
if self.supplier != supplier:
return False
if self.soft != soft:
return False
self.addr = addr
return True
def checkProtocol(self):
if not self.protocol in self.protocols:
print "Unknown protocol '", self.protocol, "' "
def dump(self):
js = {}
js['diagnotic_version'] = self.diagversion
js['supplier_code'] = self.supplier
js['soft_version'] = self.soft
js['version'] = self.version
js['group'] = self.group
js['projects'] = [p for p in self.projects]
js['protocol'] = self.protocol
js['address'] = self.addr
return js
class Ecu_database:
jsonfile = "json/ecus.zip"
def __init__(self, forceXML = False):
self.targets = []
self.numecu = 0
xmlfile = options.ecus_dir + "/eculist.xml"
if os.path.exists(self.jsonfile) and not forceXML:
zf = zipfile.ZipFile(self.jsonfile, mode='r')
jsdb = zf.read("db.json")
dbdict = json.loads(jsdb)
for target in dbdict:
ecu_ident = Ecu_ident(target['diagnotic_version'], target['supplier_code'],
target['soft_version'], target['version'],
target['name'], target['group'], target['href'], target['protocol'],
target['projects'])
self.targets.append(ecu_ident)
elif os.path.exists(xmlfile):
xdom = xml.dom.minidom.parse(xmlfile)
self.xmldoc = xdom.documentElement
if not self.xmldoc:
print "Unable to find eculist"
return
functions = self.xmldoc.getElementsByTagName("Function")
for function in functions:
targets = function.getElementsByTagName("Target")
address = function.getAttribute("Address")
group = function.getAttribute("Name")
address = hex(int(address))[2:].zfill(2).upper()
for target in targets:
href = target.getAttribute("href")
name = target.getAttribute("Name")
protnode = target.getElementsByTagName("Protocol")
if protnode:
protocol = protnode[0].firstChild.nodeValue
autoidents = target.getElementsByTagName("AutoIdents")
projectselems = target.getElementsByTagName("Projects")
projects = []
if projectselems:
for c in projectselems[0].childNodes:
projects.append(c.nodeName)
for autoident in autoidents:
self.numecu += 1
if len(autoident.getElementsByTagName("AutoIdent")) == 0:
ecu_ident = Ecu_ident("00", "??????", "0000", "0000", name, group, href, protocol,
projects, address)
self.targets.append(ecu_ident)
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, projects, address)
self.targets.append(ecu_ident)
def getTarget(self, name):
for t in self.targets:
if t.name == name:
return t
return None
def getTargets(self, name):
tgt = []
for t in self.targets:
if t.name == name:
tgt.append(t)
return tgt
def dump(self):
js = []
for t in self.targets:
if t.protocol == u'DiagOnCAN' or u'KWP' in t.protocol or u'ISO8' == t.protocol:
js.append(t.dump())
return json.dumps(js, indent=1)
class Ecu_scanner:
def __init__(self):
self.totalecu = 0
self.ecus = {}
self.approximate_ecus = {}
self.ecu_database = Ecu_database()
self.num_ecu_found = 0
self.report_data = []
self.qapp = None
def getNumEcuDb(self):
return self.ecu_database.numecu
def getNumAddr(self):
return len(elm.dnat)
def addTarget(self, target):
self.ecus[target.name] = target
def clear(self):
self.totalecu = 0
self.ecus = {}
self.approximate_ecus = {}
self.num_ecu_found = 0
self.report_data = []
def send_report(self):
if options.report_data:
# order : diagversion, supplier, soft, addr, can_response, version, href
for reportdata in self.report_data:
report.report_ecu(reportdata[1], reportdata[2], reportdata[5], reportdata[0], reportdata[3], reportdata[4], reportdata[6], reportdata[7])
def scan(self, progress=None, label=None):
i = 0
if not options.simulation_mode:
options.elm.init_can()
for addr in elm.snat.keys():
progress.setValue(i)
self.qapp.processEvents()
i += 1
if not options.simulation_mode:
txa, rxa = options.elm.set_can_addr(addr, {'idTx': '', 'idRx': '', 'ecuname': 'SCAN'})
options.elm.start_session_can('10C0')
else:
txa = addr
if addr == "7A": txa = "7E0"
if addr == "04": txa = "742"
rxa = ""
if options.simulation_mode:
# Give scanner something to eat...
if txa == "742":
can_response = "61 80 82 00 30 64 35 48 30 30 31 00 00 32 03 00 03 22 03 60 00 00 2D 32 14 00 60"
elif txa == "742":