-
Notifications
You must be signed in to change notification settings - Fork 14
/
can_send12.py
2909 lines (2370 loc) · 140 KB
/
can_send12.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
## pip3 install udsoncan
## pip3 install can-isotp
# pip3 install python-can
# pip3 install pyaes
# (C) Dennis Noermann 2018-2023
# This is free for all
# You are free to support me via Paypal [email protected]
#
# This is my Implementation of following VW used CAN Protocolls
# Mainly used for Debugging Cluster ACC and ABS of VW PQ25 and PQ35 cars via CAN
# Its badly written and not Documented
# But it works for me :)
# Protocols implemented:
# - UDS
# - TP 2.0 & KWP2000
################################################################################
# 31.08.2023 Initial git commit
#
################################################################################
# Interesting Commands for Cluster:
# Engeneering AES KEY needed for all Cluster regarding Commands !!!
# -TachoDumpEeprom
# -TachoDumpFlash
# -TachoDumpCpKeyRAM
# -TachoDumpRam
# -ReadEepromData
# -ReadInfo
# -TachoShowRam
# -WriteCPdata
# Interesting Commands for ACC:
# -WriteCPdata
# -3QFSwap
# Interesting Commands for ABS:
# -TP20_StartDiagnosticSession
# -TP20_SecurityAccess
from __future__ import print_function
import can
import binascii
import pyaes
import codecs
from datetime import datetime
import sys
from time import sleep
import numpy as np
global TP20Counter
TP20Counter = 0
boolQuiet = False
SendSeedRequest = [0x03,0x23,0x5A,0x3F,0x3A,0xA1,0xBD,0x01,0xEE,0xBB,0x32,0xF7,0xC9,0x88,0xB4,0xAC,0x2E,0x65,0x2F,0xB1,0xDE,0x2A,0x2B,0xFF,0xFF,0x07]
# keyhex, the AES Key for Cluster is included from external File Cluster_Aes_Keys.py
from Cluster_Aes_Keys import keyhex
key = codecs.decode(keyhex, 'hex')
def SplitToBytes(integer):
return divmod(integer, 0x100)
def FillUpCanFrame(WorkingFrame):
DataToFill = 8 - len(WorkingFrame)
while DataToFill > 0: # Fill up to Full 8 Byte Can Frame with 0xAA
DataToFill = DataToFill - 1
WorkingFrame = WorkingFrame + [0xAA]
return WorkingFrame
def UDS_Boot_ExitBl(bus,CanID):
WorkingFrame = [0x55,0xAA,0x01,0x0C,0x11,0x22,0x33,0x03]
msg = can.Message(arbitration_id=CanID,data=WorkingFrame,is_extended_id=False)
bus.send(msg)
recv_message = bus.recv(2.0) # 2 s Timeout
# geht so nicht
def TachoReset(bus,CanID):
msg = can.Message(arbitration_id=CanID,data=[0x02, 0x10, 0x60, 0xAA, 0xAA, 0xAA,0xAA,0xAA],is_extended_id=False)
bus.send(msg)
recv_message = bus.recv(2.0) # 2 s Timeout
print (recv_message)
def TachoReset2020(bus: can.bus.BusABC, CanID: int):
msg = can.Message(arbitration_id=CanID,data=[0x02, 0x10, 0x60, 0xAA, 0xAA, 0xAA,0xAA,0xAA],is_extended_id=False)
bus.send(msg)
recv_message = UDS_Receive(bus, CanID)
msg = can.Message(arbitration_id=CanID,data=[0x04, 0x2e, 0xfd, 0x00, 0x01, 0xAA,0xAA,0xAA],is_extended_id=False)
bus.send(msg)
recv_message = bus.recv(2.0) # 2s Timeout
def UDS_ReceiveDecodeAndRemovePadding(SeedVomTacho):
CountFrames=int(len(SeedVomTacho)/8)
CountFrameTmp=CountFrames
#print ("Frames: " + str(CountFrames))
while CountFrames > 1: # Erste Byte der Frames x bis 2 löschen
ByteToDel=((CountFrames-1)*8)
#print("Byte Del Nr: " + str(ByteToDel))
del SeedVomTacho[ByteToDel]
CountFrames = CountFrames -1
if CountFrameTmp == 1:
UDSSize = SeedVomTacho[0]
del SeedVomTacho[0:2] # die ersten 2 Bytes des ersten Frames löschen
else:
UDSSize = SeedVomTacho[1]
del SeedVomTacho[0:3] # die ersten 3 Bytes des ersten Frames löschen
return SeedVomTacho[0:UDSSize-1]
#def UDS_DiagnosticSessionControl(bus,CanID):
# WorkingFrame = [0x02,0x10,0x03]
# WorkingFrame = FillUpCanFrame(WorkingFrame)
# msg = can.Message(arbitration_id=CanID,data=WorkingFrame,is_extended_id=False)
# bus.send(msg)
# recv_message = bus.recv(2.0) # 2 s Timeout
# print (recv_message.data)
def UDS_DiagnosticSessionControl(bus: can.bus.BusABC, CanID: int, diagnosticSessionType: int):
# 0x01 Default Session
# 0x02 Programming session
# 0x03 Extended diagnostic session
# 0x60 ???
WorkingFrame = [0x02,0x10,diagnosticSessionType]
WorkingFrame = FillUpCanFrame(WorkingFrame)
msg = can.Message(arbitration_id=CanID,data=WorkingFrame,is_extended_id=False)
bus.send(msg)
print (str( codecs.encode( bytearray(UDS_Receive(bus,CanID)) ,'hex')) )
#for receive_counter in range (0,10): # 10 frames empfangen
# recv_message = bus.recv(0.01) # 0.2 s Timeout
# if recv_message != None:
# if recv_message.data[1] == 0x7F and recv_message.data[2] == 0x10:
# #print("ID 0x%3.3x " % (CanID),end='')
# print ("error: diagnosticSessionType: 0x%x " % (diagnosticSessionType),end='')
# print (str( codecs.encode( bytearray(recv_message.data) ,'hex')) )
# break
def UDS_RoutineControl(bus: can.bus.BusABC, CanID: int):
# 0x01 Default Session
# 0x02 Programming session
# 0x03 Extended diagnostic session
# 0x60 ???
WorkingFrame = [0x04,0x31,0x01,0x02,0x03]
WorkingFrame = FillUpCanFrame(WorkingFrame)
msg = can.Message(arbitration_id=CanID,data=WorkingFrame,is_extended_id=False)
bus.send(msg)
for receive_counter in range (0,10): # 10 frames empfangen
recv_message = bus.recv(0.01) # 0.2 s Timeout
if recv_message != None:
if recv_message.data[1] == 0x7F and recv_message.data[2] == 0x10:
#print("ID 0x%3.3x " % (CanID),end='')
print("error")
print (str( codecs.encode( bytearray(recv_message.data) ,'hex')) )
break
def BL_Seed(SeedVal):
SeedVal = np.uint32(SeedVal) << 1
#print ("SeedVal: 0x%x type: %s" % (SeedVal,type(SeedVal)))
if SeedVal > 0xFFFFFFFF:
print("SeedVal > 0xFFFFFFFF @ Start")
for i in range(0,0x12):
SeedVal = np.uint32(SeedVal) << 1
if SeedVal > 0xFFFFFFFF:
SeedVal = SeedVal ^ 0x2FB67A9C
SeedVal = np.uint32(SeedVal) << 1
SeedVal = np.uint32(SeedVal)
SeedVal = SeedVal - 0x35658453
if SeedVal > 0xFFFFFFFF:
SeedVal = SeedVal ^ 0x20142BCD
SeedVal = SeedVal + 0x0BFB83250
else:
SeedVal = SeedVal ^ 0x20142BCD
SeedVal = SeedVal + 0x0BFB83250
else:
for i in range(0,0xB):
SeedVal = np.uint32(SeedVal)
SeedVal = SeedVal + 0x0DAE7823C
#print ("SeedVal: 0x%x type: %s" % (SeedVal,type(SeedVal)))
if SeedVal > 0xFFFFFFFF:
SeedVal = SeedVal ^ 0x3DCEE873
#print ("SeedVal: 0x%x type: %s" % (SeedVal,type(SeedVal)))
SeedVal = np.uint32(SeedVal)
SeedVal = SeedVal + 0x48904532
#print ("SeedVal: 0x%x type: %s" % (SeedVal,type(SeedVal)))
if SeedVal > 0xFFFFFFFF:
SeedVal = SeedVal << 1
SeedVal = SeedVal ^ 0x0D68A42B
SeedVal = SeedVal << 1
#print ("SeedVal: 0x%x type: %s" % (SeedVal,type(SeedVal)))
SeedVal = np.uint32(SeedVal) << 1
SeedVal = SeedVal + 1 # macht keinen sinn, ist aber so
#SeedVal = np.uint32(SeedVal)
#print ("SeedVal: 0x%x type: %s" % (SeedVal,type(SeedVal)))
SeedVal = np.uint32(SeedVal) ^ 0x0A16532CD
SeedVal = np.uint32(SeedVal)
#print ("SeedVal: 0x%x type: %s" % (SeedVal,type(SeedVal)))
return SeedVal
def UDS_SecurityAccess(bus,CanID,Magic):
print("UDS_SecurityAccess " + str(hex(Magic)),end='')
WorkingFrame = [0x02,0x27,0x03]
WorkingFrame = FillUpCanFrame(WorkingFrame)
msg = can.Message(arbitration_id=CanID,data=WorkingFrame,is_extended_id=False)
bus.send(msg)
for receive_counter in range (0,10): # 10 frames empfangen
recv_message = bus.recv(1) # 1 s Timeout
if recv_message != None:
if recv_message.data[0] == 0x06 and recv_message.data[1] == 0x67 and recv_message.data[2] == 0x03:
#print (str( codecs.encode( bytearray(recv_message.data) ,'hex')) )
break
else: # timeout
print("timeout")
sys.exit(0)
SeedVal32 = int.from_bytes(bytearray(recv_message.data)[3:7], byteorder='big', signed=False)
print(" ... got Seed: " + str(hex(SeedVal32)),end='')
SeedAnswer32 = SeedVal32 + Magic
print(" ... Answer: " + str(hex(SeedAnswer32)),end='')
SeedValAnswerByteArr = int(SeedAnswer32).to_bytes(4, byteorder='big')
WorkingFrame = [0x06,0x27,0x04] + list(SeedValAnswerByteArr)
WorkingFrame = FillUpCanFrame(WorkingFrame)
msg = can.Message(arbitration_id=CanID,data=WorkingFrame,is_extended_id=False)
bus.send(msg)
for receive_counter in range (0,10): # 10 frames empfangen
recv_message = bus.recv(1) # 1 s Timeout
if recv_message != None:
if recv_message.data[0] == 0x03 and recv_message.data[1] == 0x7F:
print ("Security Access Error")
#print (str( codecs.encode( bytearray(recv_message.data) ,'hex')) )
break
if recv_message.data[0] == 0x02 and recv_message.data[1] == 0x67 and recv_message.data[2] == 0x04:
print (" ... Security Access OK")
return
print("\n\nNot OK :( Exit \n\n")
sys.exit(0)
def UDS_SecurityAccess_SA2(bus: can.bus.BusABC, CanID: int):
SA2_ARRAY = [
['814a246807814a0d87371ab7aa8184a2a371e84a0a879567b455931a38d24749932849ac5d4a1e681293057cd35e4a0d939135faac8703f941784a0781879ade3580814981877d9ab4674c' , '',''],
['814a24680d814a0d8753040384818476cd4b4e4a0a874973b5f193bd4ebe4d4993d39e4afb4a1e681193b05da7404a0d93a47d9fab87534586ea4a07818740a3111f8149818735db54ec4c' , '5K0920863','0725'],
['814a24680d814a0d87653040388184f76cd4b44a0a8724973b5f93ebd4ebe44993cd39e4af4a1e6811931b05da744a0d93ca47d9fa87b534586e4a078187040a311181498187d35db54e4c' , '',''],
['814a24680d814a0d879653040381845f76cd4b4a0a87024973b5935ebd4ebe49930cd39e4a4a1e68119351b05da74a0d87fb53458693bca47d9f4a0781872040a31181498187fd35db544c' , '',''],
['814a24680f814a0d87063bc8068184a2a371e84a0a878f44926c931a38d24749932849ac5d4a1e68089303f6459a4a0d939135faac8703f941784a07818757ad9e238149818736e40b3c4c' , '',''],
['814a246812814a0d872fb67a9c8184356584534a0a8720142bcd93bfb83250499353acefd24a1e680b93dae7823c4a0d873dcee87393489045324a0781870d68a42b81498187a16532cd4c' , '7E0920970S', 'SW1104'],
['814a246812814a0d87fb67a9c18184565845324a0a870142bcd193fb83250a49933acefd244a1e680b93ae7823cc4a0d938904532287dcee87324a078187d68a42bf8149818716532cd94c' , '','' ],
['814a246817814a0d87a312d9e9819339c72acf4a0a87ed9b72a784fad16b7a49932c88d9c84a1e680a93a2acad914a0d841d0796ef87c1a2f9e44a07818730a83c2e81498187bec2dee54c' , '7E0920880J', 'SW0509'],
['814a246817814a0d87a8312d9e81933e9c72ac4a0a87e6d9b72a84f9ad16b7499327c88d9c4a1e680a93a02acad94a0d841ed0796e87c31a2f9e4a0781873d0a83c281498187b4ec2dee4c' , '',''],
['814a246817814a0d87aa312d9e8193039c72ac4a0a878ed9b72a84bfad16b749932c88d9c84a1e680a932a2acad94a0d875c1a2f9e8401d0796e4a078187f30a83c2814981876bec2dee4c' , '7E0920880S 7E0920882A 5N0920883F 5N0920883H 1K8920885L 7N0920880N 7N5920880J','SW1008 SW1018 SW1104 SW1109 SW2030 SW4030'],
]
print("SA2 Array Key Count: %d"%(len(SA2_ARRAY)))
SA2_Key_Counter=0
while SA2_Key_Counter<len(SA2_ARRAY):
SA2_HEX = SA2_ARRAY[SA2_Key_Counter][0]
print("\nUsing SA2 Key[%d %s %s]: %s" % (SA2_Key_Counter, SA2_ARRAY[SA2_Key_Counter][1], SA2_ARRAY[SA2_Key_Counter][2], SA2_HEX))
SA2_Key_Counter=SA2_Key_Counter+1
SA2 = codecs.decode(SA2_HEX, 'hex')
WorkingFrame = [0x02,0x27,0x11]
WorkingFrame = FillUpCanFrame(WorkingFrame)
msg = can.Message(arbitration_id=CanID,data=WorkingFrame,is_extended_id=False)
bus.send(msg)
for receive_counter in range (0,10): # 10 frames empfangen
recv_message = bus.recv(0.01) # 0.2 s Timeout
if recv_message != None:
if recv_message.data[0] == 0x02 and recv_message.data[1] == 0x7E:
#print("ID 0x%3.3x " % (CanID),end='')
print (recv_message)
print (str( codecs.encode( bytearray(recv_message.data) ,'hex')) )
break
print (str( codecs.encode( bytearray(recv_message.data) ,'hex')) )
SeedVal64 = int.from_bytes(bytearray(recv_message.data)[3:7], byteorder='big', signed=False)
SeedVal = np.int32(SeedVal64)
print ("SeedVal: 0x%x " % (SeedVal),end="")
SeedValAnswer = Tacho_SA2_Seed_Calc(SeedVal,SA2)
print("SeedValAnswer: 0x%x " % (SeedValAnswer))
SeedValAnswerByteArr = int(SeedValAnswer).to_bytes(4, byteorder='big')
WorkingFrame = [0x06,0x27,0x12] + list(SeedValAnswerByteArr)
WorkingFrame = FillUpCanFrame(WorkingFrame)
msg = can.Message(arbitration_id=CanID,data=WorkingFrame,is_extended_id=False)
bus.send(msg)
for receive_counter in range (0,10): # 10 frames empfangen
recv_message = bus.recv(0.01) # 0.2 s Timeout
if recv_message != None:
if recv_message.data[0] == 0x03 and recv_message.data[1] == 0x7F:
#print ("Bl Seed Security Access Error")
#print (str( codecs.encode( bytearray(recv_message.data) ,'hex')) )
break
if recv_message.data[0] == 0x02 and recv_message.data[1] == 0x67 and recv_message.data[2] == 0x12:
print ("Bl Seed Security Access OK")
return
#break
print("\n\nNo SA2 Key matched :( Exit \n\n")
sys.exit(1)
def Tacho_SA2_Seed_Calc(SeedVal,SA2,Debug=False):
# 2021.05.08 Implemented from Bootloader Dissassembly, thanks to daku for his input
r10 = 0
r25 = 0
r26 = 0
r27 = 0
r28 = SeedVal
PC = 0
if Debug: print ("SeedVal: 0x%8.8x"%(r28))
while PC < len(SA2):
if SA2[PC] == 0x81:
if Debug: print ("SA2: PC=0x%2.2x Command: RSL (0x%2.2x)"%(PC,SA2[PC]))
r27 = r28
r27 = np.uint32(r27>>0x1F)
r28 = np.uint32(r28<<0x01)
if r27!=0:
r28=r28|1
PC=PC+1
if Debug: print (" r28: 0x%8.8x r27: 0x%8.8x r26: 0x%8.8x r25: 0x%8.8x r10: 0x%8.8x PC: 0x%8.8x"%(r28,r27,r26,r25,r10,PC))
elif SA2[PC] == 0x82:
print ("TEST missing !!!")
print ("SA2: PC=0x%2.2x Command: RSR (0x%2.2x)"%(PC,SA2[PC]))
r27=r28+1
r28 = np.uint32(r28>>0x01)
if r27!=0:
r28=r28|0x80000000
PC=PC+1
elif SA2[PC] == 0x84:
if Debug: print ("SA2: PC=0x%2.2x Command: SUB (0x%2.2x)"%(PC,SA2[PC]))
r10 = np.uint32( ((np.uint32(SA2[PC+1]))<<24) + (np.uint32((SA2[PC+2]))<<16) + (np.uint32((SA2[PC+3]))<<8) + (np.uint32((SA2[PC+4]))<<0) )
if r10 < r28:
r27 = 0
else:
r27 = 1
r28 = np.uint32(r28 - r10)
PC=PC+5
if Debug: print (" r28: 0x%8.8x r27: 0x%8.8x r26: 0x%8.8x r25: 0x%8.8x r10: 0x%8.8x PC: 0x%8.8x"%(r28,r27,r26,r25,r10,PC))
elif SA2[PC] == 0x87:
if Debug: print ("SA2: PC=0x%2.2x Command: XOR (0x%2.2x)"%(PC,SA2[PC]))
r10 = np.uint32( ((np.uint32(SA2[PC+1]))<<24) + (np.uint32((SA2[PC+2]))<<16) + (np.uint32((SA2[PC+3]))<<8) + (np.uint32((SA2[PC+4]))<<0) )
r27 = 0
r28 = np.uint32(r10) ^ np.uint32(r28)
PC=PC+5
if Debug: print (" r28: 0x%8.8x r27: 0x%8.8x r26: 0x%8.8x r25: 0x%8.8x r10: 0x%8.8x PC: 0x%8.8x"%(r28,r27,r26,r25,r10,PC))
elif SA2[PC] == 0x49:
if Debug: print ("SA2: PC=0x%2.2x Command: NEXT (0x%2.2x)"%(PC,SA2[PC]))
PC=PC+1
r26=r26-1
if r26 != 0:
PC=r25
elif SA2[PC] == 0x4A:
if Debug: print ("SA2: PC=0x%2.2x Command: BCC (0x%2.2x)"%(PC,SA2[PC]))
r12 = SA2[PC+1]
PC=PC+2
if r27==0:
PC=PC+r12
elif SA2[PC] == 0x68:
if Debug: print ("SA2: PC=0x%2.2x Command: LOOP (0x%2.2x)"%(PC,SA2[PC]))
r26 = SA2[PC+1]
PC=PC+2
r25=PC
elif SA2[PC] == 0x93:
if Debug: print ("SA2: PC=0x%2.2x Command: ADD (0x%2.2x)"%(PC,SA2[PC]))
#print ("0x%8.8x"%( (np.uint32(SA2[PC+1]))<<24) )
r10 = np.uint32( ((np.uint32(SA2[PC+1]))<<24) + (np.uint32((SA2[PC+2]))<<16) + (np.uint32((SA2[PC+3]))<<8) + (np.uint32((SA2[PC+4]))<<0) )
#r28= np.uint32( np.uint32(r28) + np.uint32(r10) ) # 32 bit aber numpi overflow error, ja ich weiss
#r28= np.uint32( r28 + r10 ) # 32 bit aber numpi overflow error, ja ich weiss
r28 = np.uint32(np.uint64(r28) + r10) # 32 Bit ! dan halt so
if r10 < r28:
r27 = 0
else:
r27 = 1
PC=PC+5
if Debug: print (" r28: 0x%8.8x r27: 0x%8.8x r26: 0x%8.8x r25: 0x%8.8x r10: 0x%8.8x PC: 0x%8.8x"%(r28,r27,r26,r25,r10,PC))
else:
if Debug: print ("SA2: PC=0x%2.2x Command: DONE (0x%2.2x)"%(PC,SA2[PC]))
if Debug: print (" r28: 0x%8.8x r27: 0x%8.8x r26: 0x%8.8x r25: 0x%8.8x r10: 0x%8.8x PC: 0x%8.8x"%(r28,r27,r26,r25,r10,PC))
SeedVal=r28
break
return SeedVal
def BLSeed2(SeedVal64):
LookupArr1 = [0x220B8BE7, 0xCA392FF8, 0xC1B02F0E, 0xD43C6CB3, 0x2D2500C7, 0x24537F70, 0x92DE086A, 0x9775DEAE,
0x0F3541D5, 0x799A6042, 0x27C51066, 0xCF49F556, 0x068AAFE6, 0x05723330, 0xF2DDDC7C, 0x9DFEC0AA]
eax_array = [0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000]
eax_array_ptr = 0
esp_array = [0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000]
EDX=0x00000000
#SeedVal64=0x1DBC61FB #Answer64=0xA0A0CAEC
#SeedVal64=0x1B4C5F8A #Answer64=0xD80A247F
ebx_10=SeedVal64+0x0B2B2B2B2
print ("ebx_10: 0x%x type: %s" % (ebx_10,type(ebx_10)))
edx=0x1C
for esi in range(0,6):
print("loop run: %d\n" % (esi))
Work = ebx_10 >> edx
print ("SeedVal: 0x%x type: %s" % (Work,type(Work)))
Work = Work & 0x0F
print ("SeedVal: 0x%x type: %s" % (Work,type(Work)))
ebx_0 = LookupArr1[Work] # kein *4 weil aarray 4 byte groß ist
print ("ebx_0: 0x%x" % (ebx_0))
edi = edx
edi = edi & 0xFF
ecx = edi
ecx = ecx - 4
Work = ebx_10 >> ecx
ebx_4 = Work & 0x0F
print ("EBX[+4]: 0x%x" % (ebx_4))
ecx = ecx - 4
Work = ebx_10 >> ecx
ebx_8 = Work & 0x0F
print ("EBX[+8]: 0x%x" % (ebx_8))
Work = ebx_0 >> ebx_4
print ("Work: 0x%x 0x%x" % (Work,ebx_4))
Work = (Work * ebx_8) & 0xFFFFFFFF # 32 Bit !
print ("Work: 0x%x 0x%x" % (Work,ebx_8))
Work = (Work + ebx_0) & 0xFFFFFFFF # 32 Bit !
print ("ebx_c: 0x%x 0x%x" % (Work,ebx_8))
ebx_c = Work
ebx_10 = (ebx_10 + ebx_c) & 0xFFFFFFFF # 32 Bit !
print ("ebx_10: 0x%x" % (ebx_10))
eax_array[eax_array_ptr] = ebx_10
edx = edx - (2+2) # "edx" identisch zu "dl"
print ("edx bzw. dl = 0x%x" %(edx))
eax_array_ptr = eax_array_ptr + 1
print ("End Loop\n")
for element in eax_array:
print ("0x%8.8x"%(element))
Work = ebx_10 & 0xFF
Work = Work >> 4
print ("Work: 0x%x" % (Work))
ebx_0 = LookupArr1[Work] # kein *4 weil aarray 4 byte groß ist
print ("ebx_0: 0x%x" % (ebx_0))
ebx_8 = ebx_10 >> 0x1C
print ("ebx_8: 0x%x" % (ebx_8))
ebx_4 = ebx_10 & 0x0F
print ("ebx_4: 0x%x" % (ebx_4))
Work = ebx_0 >> ebx_4
Work = (Work * ebx_8) & 0xFFFFFFFF # 32 Bit !
ebx_c = (Work + ebx_0) & 0xFFFFFFFF # 32 Bit !
print ("ebx_c: 0x%x" % (ebx_c))
ebx_10 = (ebx_10 + ebx_c) & 0xFFFFFFFF # 32 Bit !
print ("ebx_10: 0x%x" % (ebx_10))
esp_array[int(0x14/4)] = ebx_10
Work = ebx_10 & 0x0F
ebx_0 = LookupArr1[Work] # kein *4 weil aarray 4 byte groß ist
print ("ebx_0: 0x%x" % (ebx_0))
ebx_8 = ebx_10 >> 0x1C
print ("ebx_8: 0x%x" % (ebx_8))
ebx_4 = (ebx_10 >> 0x18) & 0x0F
print ("ebx_4: 0x%x" % (ebx_4))
Work = ebx_0 >> ebx_8
print ("Work: 0x%x" % (Work))
Work = (Work * ebx_4) & 0xFFFFFFFF # 32 Bit !
ebx_c = (Work+ebx_0) & 0xFFFFFFFF # 32 Bit !
print ("ebx_c: 0x%x" % (ebx_c))
Work = (eax_array[0] + eax_array[1]) ^ eax_array[2]
Work = (Work + eax_array[3]) ^ eax_array[4]
Work = (Work + eax_array[5]) & 0xFFFFFFFF # 32 Bit !
Work = Work ^ ebx_10
Work = (Work + ebx_c) & 0xFFFFFFFF # 32 Bit !
print ("Work: 0x%x" % (Work))
Work = (Work + ebx_10) & 0xFFFFFFFF # 32 Bit !
print ("Work: 0x%x" % (Work))
Work_edx = 0x25478B3F >> 0x10
print ("Work_edx: 0x%x" % (Work_edx))
Work_edx = Work_edx + 0x00 # var_44
Work_ecx = 0x25478B3F & 0x0FFFF
print ("Work_ecx: 0x%x" % (Work_ecx))
Work_edx = (Work_edx * Work_ecx) & 0xFFFFFFFF # 32 Bit !
print ("Work_edx: 0x%x" % (Work_edx))
Work = Work ^ Work_edx
print ("SeedAnswer: 0x%8.8x" % (Work))
return Work
def UDS_SecurityAccess2(bus,CanID):
WorkingFrame = [0x27,0x01,0x00,0x00,0x00,0x00,0x00,0x00]
msg = can.Message(arbitration_id=CanID,data=WorkingFrame,is_extended_id=False)
bus.send(msg)
recv_message = bus.recv(0.01) # 0.2 s Timeout
if recv_message != None:
print (str( codecs.encode( bytearray(recv_message.data) ,'hex')) )
SeedVal64 = (recv_message.data[2] << 24) + (recv_message.data[3] << 16) + (recv_message.data[4] << 8) + recv_message.data[5]
print("BlSeed2Val: 0x%8.8x" %(SeedVal64))
SeedAnswer = BLSeed2(SeedVal64)
print("BlSeed2Answer: 0x%8.8x" %(SeedAnswer))
WorkingFrame = [0x27,0x02,(SeedAnswer&0xFF000000)>>24,(SeedAnswer&0xFF0000)>>16,(SeedAnswer&0xFF00)>>8,(SeedAnswer&0xFF),0x00,0x00]
msg = can.Message(arbitration_id=CanID,data=WorkingFrame,is_extended_id=False)
bus.send(msg)
recv_message = bus.recv(0.01) # 0.2 s Timeout
if recv_message != None:
print (str( codecs.encode( bytearray(recv_message.data) ,'hex')) )
def UDS_TesterPresent(bus,CanID):
WorkingFrame = [0x02,0x3E,0x00]
WorkingFrame = FillUpCanFrame(WorkingFrame)
msg = can.Message(arbitration_id=CanID,data=WorkingFrame,is_extended_id=False)
bus.send(msg)
for receive_counter in range (0,10): # 10 frames empfangen
recv_message = bus.recv(0.01) # 0.2 s Timeout
if recv_message != None:
if recv_message.data[0] == 0x02 and recv_message.data[1] == 0x7E:
#print("ID 0x%3.3x " % (CanID),end='')
#print (recv_message)
break
def UDS_Receive(bus: can.bus.BusABC, CanID: int, buf=" "):
SingleFrame = False
while True:
recv_message = bus.recv(2.0) # 2 s Timeout
if recv_message == None:
print("Timeout")
return None
if recv_message.arbitration_id == 0x30c: #workaround für MQB X FW
#print("Workaround for MQB 0x30c")
continue
#print (str(binascii.hexlify(recv_message.data)))
#print (str((recv_message.arbitration_id)))
#check for UDS 0x78 requestCorrectlyReceived-ResponsePending
if recv_message.data[0] & 0b11110000 == 0x00 \
and recv_message.data[1] == 0x7F \
and recv_message.data[3] == 0x78:
#print ("requestCorrectlyReceived-ResponsePending retry recv ...")
print (" repeat ",end='')
##recv_message = bus.recv(2.0) # 2 s Timeout
##if recv_message == None:
## print("UDS_Receive requestCorrectlyReceived-ResponsePending retry recv ... Timeout")
## return ["UDS_Receive requestCorrectlyReceived-ResponsePending retry recv ... Timeout"]
else:
if recv_message.data[0] == 0x30 and recv_message.data[1] == 0x0F:
pass
#SingleFrame = True
#SizeDataToReveive = recv_message.data[0]
else:
break
if recv_message.data[0] == 0x10: #(FF) First Frame
SizeDataToReveive = recv_message.data[1]
UDS_SID = recv_message.data[2]
if (recv_message.data[0] & 0b11110000) == 0x0: #(SF) Single Frame
SingleFrame = True
SizeDataToReveive = recv_message.data[0]
#SizeDataToReveive = SizeDataToReveive - 7 # max 7 sind im ersten Frame drinn
UDS_SID = recv_message.data[1]
#if recv_message.data[0] & 0b11110000 == 0x20: #(CF) Consecutive Frame
#print ("DataSize= 0x%4.4x " % (SizeDataToReveive),end='')
#print (" UDS_SID= 0x%2.2x " % (UDS_SID),end='')
DataReceived = recv_message.data
#if UDS_SID == 0x7F: #Error, nur 1 Frame
if recv_message.data[1] == 0x7F: #Error, nur 1 Frame
#if recv_message.data[3] != 0x31:
boolQuiet = True
if not boolQuiet == True: # geht so leider nicht
#pass
print(buf + " ServiceIdRQ 0x%2.2x ErrorCode NRC 0x%2.2x" % (recv_message.data[2],recv_message.data[3]),end='\n')
return []
print (buf,end='')
if SingleFrame == True:
#sigle antwort
#print("Single Antwort")
pass
else:
#ACK #2021 ACC Read Public Key workaround: i guess: read buffer was increaded
#msg = can.Message(arbitration_id=CanID,data=[0x30, 0x10, 0x00, 0x00, 0x00, 0x00,0x00,0x00],is_extended_id=False)
msg = can.Message(arbitration_id=CanID,data=[0x30, 0x00, 0x01, 0x00, 0x00, 0x00,0x00,0x00],is_extended_id=False)
bus.send(msg)
SizeDataToReveive = SizeDataToReveive - 6 # 6 sind im ersten Frame drinn
#multiple antwort frames
while (SizeDataToReveive > 0):
recv_message = bus.recv(2.0) # 2 s Timeout
if recv_message.arbitration_id == 0x30c: #workaround für MQB X FW
#print("Workaround for MQB 0x30c")
continue
#print (recv_message)
#print (str(binascii.hexlify(recv_message.data)))
DataReceived = DataReceived + recv_message.data
SizeDataToReveive = SizeDataToReveive - 7
return UDS_ReceiveDecodeAndRemovePadding(DataReceived)
def UDS_ReadDataByIdentifier(bus,CanID,Identifier,Counter=-1):
buf = ("ReadDataByIdentifier ID: " + str(hex(Identifier)) +" "+ str(hex(Counter))+ " ")
IdentifierHighByte,IdentifierLowByte = SplitToBytes(Identifier)
WorkingFrame = [0x03,0x22,IdentifierHighByte,IdentifierLowByte]
if Counter != -1:
WorkingFrame[0] = 0x04
WorkingFrame = WorkingFrame + [Counter]
WorkingFrame = FillUpCanFrame(WorkingFrame)
msg = can.Message(arbitration_id=CanID,data=WorkingFrame,is_extended_id=False)
bus.send(msg)
return UDS_Receive(bus,CanID,buf)
#def UDS_RequestUpload(bus,CanID,Type):
# WorkingFrame = [0x10,0x0B,0x35,Type,0x44,0x03,0xFF,0x19]
# msg = can.Message(arbitration_id=CanID,data=WorkingFrame,is_extended_id=False)
# bus.send(msg)
# recv_message = bus.recv(2.0) # 2 s Timeout
# print(recv_message)
#
# WorkingFrame = [0x21,0x00,0x00,0x00,0x00,0x20]
# WorkingFrame = FillUpCanFrame(WorkingFrame)
# msg = can.Message(arbitration_id=CanID,data=WorkingFrame,is_extended_id=False)
# bus.send(msg)
# recv_message = bus.recv(2.0) # 2 s Timeout
# print(recv_message)
# return
def UDS_RequestUpload(bus,CanID,DataPayload):
FrameNr=0
DataToSendPtr=0
DataSize = len(DataPayload)
print("UDS_RequestUp DataPayload Size: " + hex(DataSize))
#WorkingFrame = [0x10,0x0B,0x34,0x01,0x44,0x03,0xFF,0x19]
if DataSize > 5:
WorkingFrame = [0x10,DataSize+1,0x35] + DataPayload[DataToSendPtr:DataToSendPtr+5]
DataToSendPtr=DataToSendPtr+5 # 5 Bytes gesendet
else: # =< 5 nur 1 Frame
WorkingFrame = [DataSize+1,0x35] + DataPayload[DataToSendPtr:DataToSendPtr+DataSize]
WorkingFrame = FillUpCanFrame(WorkingFrame)
msg = can.Message(arbitration_id=CanID,data=WorkingFrame,is_extended_id=False)
bus.send(msg)
recv_message = bus.recv(2.0) # 2 s Timeout
if DataSize > 5:
FrameNr=FrameNr+1
WorkingFrame = [0x20+FrameNr] + DataPayload[DataToSendPtr:DataToSendPtr+5]
WorkingFrame = FillUpCanFrame(WorkingFrame)
msg = can.Message(arbitration_id=CanID,data=WorkingFrame,is_extended_id=False)
bus.send(msg)
recv_message = bus.recv(2.0) # 2 s Timeout
else:
pass
def UDS_RequestDownload(bus,CanID,DataPayload):
FrameNr=0
DataToSendPtr=0
DataSize = len(DataPayload)
print("UDS_RequestDownload DataPayload Size: " + hex(DataSize))
#WorkingFrame = [0x10,0x0B,0x34,0x01,0x44,0x03,0xFF,0x19]
WorkingFrame = [0x10,DataSize+1,0x34] + DataPayload[DataToSendPtr:DataToSendPtr+5]
DataToSendPtr=DataToSendPtr+5 # 5 Bytes gesendet
msg = can.Message(arbitration_id=CanID,data=WorkingFrame,is_extended_id=False)
bus.send(msg)
recv_message = bus.recv(2.0) # 2 s Timeout
FrameNr=FrameNr+1
#WorkingFrame = [0x20+FrameNr,0x00 ,0x00 ,0x00 ,0x06 ,0x00]
WorkingFrame = [0x20+FrameNr] + DataPayload[DataToSendPtr:DataToSendPtr+5]
WorkingFrame = FillUpCanFrame(WorkingFrame)
msg = can.Message(arbitration_id=CanID,data=WorkingFrame,is_extended_id=False)
bus.send(msg)
recv_message = bus.recv(2.0) # 2 s Timeout
if recv_message.data[1] == 0x7F:
print(" ServiceIdRQ 0x%2.2x ErrorCode NRC 0x%2.2x" % (recv_message.data[2],recv_message.data[3]),end='\n')
return False
#sys.exit(0)
return True
def UDS_TransferData(bus,CanID,DataPayload):
WorkingFrame = []
DataSize = len(DataPayload)
print("UDS_TransferData DataPayload Size: " + hex(DataSize))
if DataSize>0x70:
DataPosEndThisBlock = 256
DataSizeThisBlock = 256
else:
DataPosEndThisBlock = DataSize
DataSizeThisBlock = DataSize
BlockNumber=1
DataToSendPtr=0
while (DataToSendPtr < DataSize):
print("UDS_TransferData Transfer Block : " +hex(BlockNumber))
print("UDS_TransferData DataPosEndThisBlock: " +hex(DataPosEndThisBlock))
print("UDS_TransferData DataSizeThisBlock : " +hex(DataSizeThisBlock))
print("Data: 0x%2.2x "%(BlockNumber),end='')
for lauf in range (0,DataSizeThisBlock):
print ("0x%2.2x " % (DataPayload[lauf+(BlockNumber-1)*256]),end='')
print("")
FrameNr=0
CanFrameDataSizeHigh,CanFrameDataSizeLow = SplitToBytes(DataSizeThisBlock+2)
WorkingFrame = [0x10+CanFrameDataSizeHigh,CanFrameDataSizeLow,0x36,BlockNumber] + DataPayload[DataToSendPtr:DataToSendPtr+4]
DataToSendPtr=DataToSendPtr+4 # 4 Bytes gesendet
msg = can.Message(arbitration_id=CanID,data=WorkingFrame,is_extended_id=False)
bus.send(msg)
recv_message = bus.recv(2.0) # 2 s Timeout
while(DataToSendPtr < DataPosEndThisBlock):
FrameNr=FrameNr+1
if FrameNr==16: FrameNr=0
#print("Sending Multiple Frames: ConsecutiveFrame (CF) Frame: " + str(FrameNr))
#print("Data Left: " + str(DataPosEndThisBlock-DataToSendPtr))
WorkingFrame = [0x20+FrameNr] + DataPayload[DataToSendPtr:DataToSendPtr+7] #<=== DAAAAAA F E H L E R
WorkingFrame = FillUpCanFrame(WorkingFrame)
#print(WorkingFrame)
DataToSendPtr=DataToSendPtr+7 # 7 Bytes gesendet
msg = can.Message(arbitration_id=CanID,data=WorkingFrame,is_extended_id=False)
bus.send(msg)
sleep(0.005)
UDS_Receive(bus,CanID)
DataPosEndThisBlock = DataPosEndThisBlock + 256
if (DataPosEndThisBlock > DataSize):
DataPosEndThisBlock = DataSize # last block
DataSizeThisBlock = DataPosEndThisBlock-DataToSendPtr
BlockNumber = BlockNumber+1
def UDS_TransferExit(bus,CanID):
WorkingFrame = [0x01,0x37]
WorkingFrame = FillUpCanFrame(WorkingFrame)
msg = can.Message(arbitration_id=CanID,data=WorkingFrame,is_extended_id=False)
bus.send(msg)
recv_message = bus.recv(2.0) # 2 s Timeout
def UDS_RoutineControl2(bus,CanID,Ctrltype,CtrlPayload):
# Ctrltype
# 0x01 Start
# 0x02 Stop
# 0x03 RequestResult
DataSize = len(CtrlPayload)
print("CtrlPayload Size: " + hex(DataSize))
DataToSendPtr=0
FrameNr=0
WorkingFrame = [0x10,DataSize+2,0x31,Ctrltype] + CtrlPayload[DataToSendPtr:DataToSendPtr+4] #,0x02, 0x02, 0x04, 0x03] # 0x8b Size = 6 Byte im ersten frame + 0x13 7 byte Frames
DataToSendPtr=DataToSendPtr+4 # 4 Bytes gesendet
msg = can.Message(arbitration_id=CanID,data=WorkingFrame,is_extended_id=False)
bus.send(msg)
recv_message = bus.recv(2.0) # 2 s Timeout
while(DataToSendPtr < DataSize):
if FrameNr<0xF:
FrameNr=FrameNr+1
else:
FrameNr=0
#print("Sending Multiple Frames: ConsecutiveFrame (CF) Frame: " + str(FrameNr))
WorkingFrame = [0x20+FrameNr] + CtrlPayload[DataToSendPtr:DataToSendPtr+7]
DataToSendPtr=DataToSendPtr+7 # 7 Bytes gesendet
msg = can.Message(arbitration_id=CanID,data=WorkingFrame,is_extended_id=False)
bus.send(msg)
if FrameNr==0xF:
recv_message = bus.recv(2.0) # 2 s Timeout
#sleep(1)
UDS_Receive(bus,CanID)
def UDS_WriteDataByIdentifier(bus,CanID,Identifier,DataList):
WorkingFrame = []
#print(CanID)
#print(Identifier)
#print(DataList)
#for BYTE in DataList:
# print (format(BYTE,'02x'),end = '')
#print("")
DataSize = len(DataList)
#print("Size Data: " + str(DataSize));
IdentifierHighByte,IdentifierLowByte = SplitToBytes(Identifier)
if DataSize > 4: # mehr als 4 bytes ==> mehr als 1 Frame
#print("Sending Multiple Frames: FirstFrame (FF)")
FrameNr=0
DataToSendPtr=0
#TODO Mehr als 0xFF Bytes
WorkingFrame = [0x10,DataSize+3,0x2E,IdentifierHighByte,IdentifierLowByte] + DataList[DataToSendPtr:DataToSendPtr+3]
DataToSendPtr=DataToSendPtr+3 # 3 Bytes gesendet
msg = can.Message(arbitration_id=CanID,data=WorkingFrame,is_extended_id=False)
bus.send(msg)
recv_message = bus.recv(2.0) # 2 s Timeout
#print (recv_message)
while(DataToSendPtr < DataSize):
FrameNr=FrameNr+1
if FrameNr==16: FrameNr=0
#print("Sending Multiple Frames: ConsecutiveFrame (CF) Frame: " + str(FrameNr))
WorkingFrame = [0x20+FrameNr] + DataList[DataToSendPtr:DataToSendPtr+7]
DataToSendPtr=DataToSendPtr+7 # 7 Bytes gesendet
if len(WorkingFrame) < 8:
WorkingFrame = FillUpCanFrame(WorkingFrame)
msg = can.Message(arbitration_id=CanID,data=WorkingFrame,is_extended_id=False)
sleep(0.05)
bus.send(msg)
else: # Single Frame (CS)
#print("Sending Single Frame (CS)")
WorkingFrame = [DataSize+3,0x2E,IdentifierHighByte,IdentifierLowByte] + DataList
WorkingFrame = FillUpCanFrame(WorkingFrame)
msg = can.Message(arbitration_id=CanID,data=WorkingFrame,is_extended_id=False)
bus.send(msg)
recv_message = bus.recv(2.0) # 2 s Timeout
#print (recv_message)
return recv_message
#Message Send, receive Answer
return UDS_Receive(bus,CanID)
def WriteCPData(bus,CPData,CPDatum,CPDevice,CPID):
#TmpData = UDS_ReadDataByIdentifier(bus,0x714,0xF19E)
#del TmpData[0:2] # die zwei Byte response ID müssen hier noch weg
#print(TmpData)
#TmpData = UDS_ReadDataByIdentifier(bus,0x714,0xF1A2)
#del TmpData[0:2] # die zwei Byte response ID müssen hier noch weg
#print(TmpData)
#yxcv
#recv_message = UDS_WriteDataByIdentifier(bus,0x714,0x00BE,CPData)
#if len(recv_message) != 0x00:
# if recv_message[0] == 0x00 and recv_message[1] == 0xbe:
# print(" Write CP Data OK")
#else:
# print(" Write CP Data Error")
# print(recv_message)
#sys.exit(0)
#nicht bei acc
#tmp2arr2 = ( UDS_ReadDataByIdentifier(bus,CPDevice,0x0956) )
#print (tmp2arr2,end='')
#print (str( codecs.encode( tmp2arr2 ,'hex')) )
UDS_TesterPresent(bus,CPDevice)
tmp2arr2 = ( UDS_ReadDataByIdentifier(bus,CPDevice,0xF19E) )
print (tmp2arr2,end='')
print (str( codecs.encode( tmp2arr2 ,'hex')) )
tmp2arr2 = ( UDS_ReadDataByIdentifier(bus,CPDevice,0xF1A2) )
print (tmp2arr2,end='')
print (str( codecs.encode( tmp2arr2 ,'hex')) )
tmp2arr2 = ( UDS_ReadDataByIdentifier(bus,CPDevice,0xF17C) )
print (tmp2arr2,end='')
print (str( codecs.encode( tmp2arr2 ,'hex')) )
UDS_DiagnosticSessionControl(bus,CPDevice,0x03)
UDS_WriteDataByIdentifier(bus,CPDevice,0xF198,[0x00,0x00,0x00,0x00,0x00,0x2E])
UDS_WriteDataByIdentifier(bus,CPDevice,0xF199,CPDatum)
recv_message = UDS_WriteDataByIdentifier(bus,CPDevice,CPID,CPData)
if len(recv_message) != 0x00:
if recv_message[0] == 0x00 and recv_message[1] == 0xbe:
print(" Write CP Data OK")
else:
print(" Write CP Data Error")
print(recv_message)
print("Ret: %x %x %x %x"%(recv_message[0],recv_message[1],recv_message[2],recv_message[3]))
#TmpData = recv_message
#print(TmpData,end='')
#print( str( codecs.encode( bytearray(TmpData) ,'hex') ) )
def UDS_ReadMemoryByAddress(bus,CanID,Addr3,Addr2,Addr,Size):
AddrHi,AddrLo = SplitToBytes(Addr)
WorkingFrame = [0x07,0x23,0x14,Addr3,Addr2,AddrHi,AddrLo,Size]
WorkingFrame = FillUpCanFrame(WorkingFrame)
msg = can.Message(arbitration_id=CanID,data=WorkingFrame,is_extended_id=False)
bus.send(msg)
return UDS_Receive(bus,CanID)
def UDS_WriteMemoryByAddress(bus,CanID,Addr3,Addr2,Addr,DataList):
#AddrHi,AddrLo = SplitToBytes(Addr)
#WorkingFrame = [0x07,0x3D,0x14,Addr3,Addr2,AddrHi,AddrLo,DataList[0]]
#WorkingFrame = FillUpCanFrame(WorkingFrame)
#msg = can.Message(arbitration_id=CanID,data=WorkingFrame,is_extended_id=False)
#bus.send(msg)
#return UDS_Receive(bus,CanID)
WorkingFrame = []
DataSize = len(DataList)
DataSizeHi,DataSizeLo=SplitToBytes(DataSize)
AddrHi,AddrLo = SplitToBytes(Addr)
if DataSize > 0: # mehr als 0 bytes ==> mehr als 1 Frame :)
#print("Sending Multiple Frames: FirstFrame (FF)")
FrameNr=0
DataToSendPtr=0
WorkingFrame = [0x10+DataSizeHi,DataSizeLo+8,0x3D,0x24,Addr3,Addr2,AddrHi,AddrLo]
#DataToSendPtr=DataToSendPtr+0 # 0 Bytes gesendet
msg = can.Message(arbitration_id=CanID,data=WorkingFrame,is_extended_id=False)