-
Notifications
You must be signed in to change notification settings - Fork 5
/
DiagnosticCommunication.py
488 lines (427 loc) · 19.9 KB
/
DiagnosticCommunication.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
"""
DiagnosticCommunication.py
Copyright (C) 2024 - 2025 Marc Postema (mpostema09 -at- gmail.com)
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Or, point your browser to http://www.gnu.org/copyleft/gpl.html
"""
import time
import queue
import json
from PySide6.QtCore import Qt, QThread, Signal
from PySide6.QtWidgets import QTextEdit
from SeedKeyAlgorithm import SeedKeyAlgorithm
from CalcCRC16X25 import CalcCRC16X25
class DiagnosticCommunication(QThread):
receivedPacketSignal = Signal(list, float)
outputToTextEditSignal = Signal(str)
updateZoneDataSignal = Signal(str, str)
algo = SeedKeyAlgorithm()
crcx25 = CalcCRC16X25()
writeQ = queue.Queue()
ecuReadZone = ""
zoneName = ""
zoneActive = {}
protocol = ""
keepAlive = ""
stopKeepAlive = ""
startDiagmode = ""
stopDiagmode = ""
unlockServiceConfig = ""
unlockResponseConfig = ""
readSecureTraceability = ""
secureTraceability = ""
readEcuFaultsMode = ""
readZoneTag = ""
writeZoneTag = ""
def __init__(self, serialPort, protocol: str()):
super(DiagnosticCommunication, self).__init__()
self.serialPort = serialPort
self.isRunning = False
self.protocol = protocol
if self.protocol == "uds":
#self.crcx25.testCrc()
self.keepAlive = "KU"
self.stopKeepAlive = "S"
self.startDiagmode = "1003"
self.stopDiagmode = "1001"
self.unlockServiceConfig = "2703"
self.unlockResponseConfig = "2704"
self.readSecureTraceability = "222901"
self.secureTraceability = "2E2901FD000000010101"
self.readEcuFaultsMode = "190209"
self.readZoneTag = "22"
self.writeZoneTag = "2E"
elif self.protocol == "kwp_is":
self.keepAlive = "KK"
self.stopKeepAlive = "S"
self.startDiagmode = "81"
self.stopDiagmode = "82"
self.unlockServiceConfig = "2783"
self.unlockResponseConfig = "2784"
self.readSecureTraceability = ""
self.secureTraceability = ""
self.readEcuFaultsMode = "190209"
self.readZoneTag = "21"
self.writeZoneTag = "34"
else:
print("Incorrect protocol: " + protocol)
exit()
#self.algo.testCalculations()
#receiveData = "670311BF5E67"
#key = "D91C"
#challenge = int(receiveData[4:12], 16)
#seed = ("%0.8X" % self.algo.computeResponse(int(key, 16), challenge))
#reply = "2704" + seed
#print(reply)
def stop(self):
self.isRunning = False
emptyQueue()
def emptyQueue(self):
while not self.writeQ.empty():
try:
self.writeQ.get(block=False)
except:
continue
def writeToOutputView(self, text: str, reply: str = None):
if reply != None:
text = text + " (" + reply + ")"
self.outputToTextEditSignal.emit(text)
def writeECUCommand(self, cmd: str):
self.writeToOutputView("> " + cmd)
receiveData = self.serialPort.sendReceive(cmd)
# Check response we need to retry reading
# 7F3E03 (Custom error)
# 7Fxx78 (Request Correctly Received - Response Pending)
while receiveData == "7F3E03" or (len(receiveData) == 6 and receiveData[:2] == "7F" and receiveData[4:6] == "78"):
self.writeToOutputView("< " + receiveData + " ** Skipping **")
time.sleep(0.2)
receiveData = self.serialPort.readData()
self.writeToOutputView("< " + receiveData)
return receiveData
def startSendingKeepAlive(self):
receiveData = self.writeECUCommand(self.keepAlive)
if receiveData != "OK":
self.writeToOutputView("ECU Send keep-alive: Failed", receiveData)
return False
return True
def stopSendingKeepAlive(self):
receiveData = self.writeECUCommand(self.stopKeepAlive)
if receiveData != "OK":
self.writeToOutputView("Reset ECU Keep Alive: Failed", receiveData)
return False
return True
def startDiagnosticMode(self):
receiveData = self.writeECUCommand(self.startDiagmode)
if len(receiveData) >= 4 and receiveData[:4] == "5003":
return True
elif len(receiveData) == 6 and receiveData[:2] == "C1":
return True
self.writeToOutputView("Open Diagnostic session: Failed", receiveData)
return False
def stopDiagnosticMode(self):
self.stopSendingKeepAlive()
receiveData = self.writeECUCommand(self.stopDiagmode)
if len(receiveData) >= 4 and receiveData[:4] == "5001":
return True
elif len(receiveData) == 2 and receiveData[:2] == "C2":
return True
self.writeToOutputView("Closing Diagnostic session: Failed", receiveData)
return False
def setupSketchSeedForDiagnoticMode(self, key: str):
sketchSeedSetup = ":" + key + ":03:03"
receiveData = self.writeECUCommand(sketchSeedSetup)
tryCnt = 8
while len(receiveData) >= 4 and receiveData[:4] != "6704":
self.writeToOutputView("ECU Seed Request: Waiting", receiveData)
receiveData = self.serialPort.readData()
time.sleep(2)
tryCnt -= 1
if tryCnt == 0:
self.writeToOutputView("Write Configuration Zone: Failed", receiveData)
return False
return True
def unlockingServiceForConfiguration(self, key: str):
tryCnt = 8
while tryCnt:
receiveData = self.writeECUCommand(self.unlockServiceConfig)
if len(receiveData) != 12:
if len(receiveData) >= 6:
# Unlocking - Required time delay not expired
if receiveData[:6] == "7F2737" or receiveData == "7F3E03":
self.writeToOutputView("ECU Unlock Request: Retrying in 2 Seconds", receiveData)
tryCnt -= 1;
time.sleep(2)
else:
tryCnt = 0
else:
tryCnt = 0
elif len(receiveData) == 12:
if receiveData[:4] == "6703" or receiveData[:4] == "6783":
break;
if tryCnt == 0:
self.writeToOutputView("ECU Unlock Request: Failed", receiveData)
return ""
challenge = int(receiveData[4:12], 16)
seed = "%0.8X" % self.algo.computeResponse(int(key, 16), challenge)
return seed
def sendUnlockingResponseForConfiguration(self, seed: str):
reply = self.unlockResponseConfig + seed
receiveData = self.writeECUCommand(reply)
if len(receiveData) == 4:
if receiveData[:4] == "6704" or receiveData[:4] == "6784":
return True
if receiveData == "7F2735":
self.writeToOutputView("ECU unlock: Failed, ECU Reports Invalid Key", receiveData)
else:
self.writeToOutputView("ECU unlock: Failed", receiveData)
return False
def writeUDSZoneConfigurationCommand(self, zone: str(), data: str()):
writeCmd = self.writeZoneTag + zone + data
receiveData = self.writeECUCommand(writeCmd)
if len(receiveData) == 6 and receiveData[:2] == "6E":
return True
# Is Configuration Write in progress? then wait untill finished
if len(receiveData) == 6 and (receiveData == "7F2E78" or receiveData == "7F3E03"):
self.writeToOutputView("Write Configuration Zone in progress", receiveData)
tryCnt = 32
while len(receiveData) == 6 and (receiveData == "7F2E78" or receiveData == "7F3E03"):
receiveData = self.serialPort.readData()
tryCnt -= 1
if tryCnt == 0:
self.writeToOutputView("Write Configuration Zone: Failed", receiveData)
return False
self.writeToOutputView("Write Configuration Zone: Ok", receiveData)
return True
else:
self.writeToOutputView("Write Configuration Zone: Failed", receiveData)
return False
def writeKWPZoneConfigurationCommand(self, zone: str(), data: str()):
addrHigh = "00"
addrMid = "00"
addrLow = "00"
address = addrHigh + addrMid + addrLow
securedTraceability = "FD000000"
indexTelecodage = data[0:2]
zoneData = data[4:6]
subCmd = indexTelecodage + zoneData + securedTraceability
size = "%0.2X" % int((len(subCmd) / 2))
cmd = self.writeZoneTag + zone + address + size + subCmd
crc = self.crcx25.calcCRC16X25(cmd)
cmd += crc[0]
cmd += crc[1]
receiveData = self.writeECUCommand(cmd)
if len(receiveData) >= 4:
if receiveData[0:4] == "7402":
return True
elif receiveData[0:4] == "74A0":
self.writeToOutputView("Write Configuration Zone: Failed (Incorrect Checksum)", receiveData)
self.writeToOutputView("Write Configuration Zone: Failed", receiveData)
return False
def writeZoneList(self, useSketchSeed: bool, ecuID: str, lin: str, key: str, valueList: list, writeSecureTraceability: bool):
if not self.serialPort.isOpen():
self.receivedPacketSignal.emit(["Serial Port Not Open", "", ""], time.time())
return
receiveData = self.writeECUCommand(ecuID)
if receiveData != "OK":
self.writeToOutputView("Selecting ECU: Failed", receiveData)
return
if lin != None and len(lin) > 1:
receiveData = self.writeECUCommand(lin)
if receiveData != "OK":
self.writeToOutputView("Selecting LIN ECU: Failed")
return
# Not needed
# if not self.stopDiagnosticMode():
# return
time.sleep(0.5)
if not self.startSendingKeepAlive():
return
if not self.startDiagnosticMode():
return
time.sleep(0.5)
if useSketchSeed:
if not self.setupSketchSeedForDiagnoticMode(key):
self.stopDiagnosticMode()
return
else:
seed = self.unlockingServiceForConfiguration(key)
if len(seed) == 0:
self.stopDiagnosticMode()
return
self.writeToOutputView("Waiting 2 Sec...")
time.sleep(2)
if not self.sendUnlockingResponseForConfiguration(seed):
self.stopDiagnosticMode()
return
if not self.stopSendingKeepAlive():
return
# Write Zones
for tabList in valueList:
for zone in tabList:
time.sleep(0.2)
readCmd = self.readZoneTag + zone[0]
time.sleep(0.2)
receiveData = self.writeECUCommand(readCmd)
time.sleep(0.2)
if self.protocol == "uds":
self.writeUDSZoneConfigurationCommand(zone[0], zone[1])
elif self.protocol == "kwp_is":
self.writeKWPZoneConfigurationCommand(zone[0], zone[1])
time.sleep(0.2)
receiveData = self.writeECUCommand(readCmd)
if self.protocol == "uds":
receiveData = self.writeECUCommand(self.readSecureTraceability)
if writeSecureTraceability:
receiveData = self.writeECUCommand(self.secureTraceability)
if len(receiveData) != 6 or receiveData[:2] != "6E":
self.writeToOutputView("Configuration Write of Secure Traceability Zone: Failed", receiveData)
else:
self.writeToOutputView("NO Secure Traceability is Written!!")
if not self.stopDiagnosticMode():
return
self.writeToOutputView("Write Successful")
def rebootEcu(self, ecuID: str):
if self.serialPort.isOpen():
self.writeToOutputView("Reboot ECU...")
receiveData = self.serialPort.sendReceive(ecuID)
if receiveData != "OK":
self.writeToOutputView("ECU Not selected!")
return
receiveData = self.ecuZoneReaderThread.sendReceive("1103")
if len(receiveData) != 4 or receiveData[:4] != "5103":
self.writeToOutputView("Reboot: Failed")
return
def readEcuFaults(self, ecuID: str):
if self.serialPort.isOpen():
self.writeToOutputView("Read ECU Faults...")
receiveData = self.writeECUCommand(ecuID)
if receiveData != "OK":
self.writeToOutputView("ECU Not selected!")
return
if not self.startDiagnosticMode():
return
receiveData = self.writeECUCommand(self.readEcuFaultsMode)
if len(receiveData) < 4:
self.writeToOutputView("Reading ECU Faults: Failed")
if not self.stopDiagnosticMode():
return
self.writeToOutputView("Reading ECU Faults: Successful")
def setZonesToRead(self, ecuID: str, lin: str, zoneList: dict):
if not self.serialPort.isOpen():
self.receivedPacketSignal.emit(["Serial Port Not Open", "", ""], time.time())
return
if self.isRunning == False:
self.start();
self.writeQ.put(ecuID)
if lin != None and len(lin) > 1:
self.writeQ.put(lin)
self.writeQ.put(self.keepAlive)
self.writeQ.put(self.startDiagmode)
self.writeQ.put(zoneList)
self.writeQ.put(self.stopKeepAlive)
self.writeQ.put(self.stopDiagmode)
def parseReadResponse(self, data: str):
if len(data) == 0:
self.receivedPacketSignal.emit([self.ecuReadZone, "Timeout", self.zoneName], time.time())
return data
decodedData = data;
if len(decodedData) > 4:
if (decodedData[0:2] == "62" or decodedData[0:2] == "61") and len(decodedData) > 6:
# Get only response data
answerZone = ""
answer = ""
if decodedData[0:2] == "62":
answerZone = decodedData[2:6]
answer = decodedData[6:]
elif decodedData[0:2] == "61":
answerZone = decodedData[2:4]
answer = decodedData[4:]
if answerZone.upper() != self.ecuReadZone.upper():
self.receivedPacketSignal.emit([self.ecuReadZone, "Requesed zone different from received zone", decodedData], time.time())
return data
self.receivedPacketSignal.emit([self.ecuReadZone, answer, self.zoneName], time.time())
self.updateZoneDataSignal.emit(self.ecuReadZone, answer)
elif decodedData[0: + 4] == "5001":
self.receivedPacketSignal.emit([self.ecuReadZone, "Communication closed", self.zoneName], time.time())
elif decodedData[0: + 4] == "5002":
self.receivedPacketSignal.emit([self.ecuReadZone, "Download session opened", self.zoneName], time.time())
elif decodedData[0: + 4] == "5003":
self.receivedPacketSignal.emit([self.ecuReadZone, "Diagnostic session opened", self.zoneName], time.time())
elif decodedData[0: + 4] == "6702":
self.receivedPacketSignal.emit([self.ecuReadZone, "Unlocked successfully for download", self.zoneName], time.time())
elif decodedData[0: + 4] == "6704":
self.receivedPacketSignal.emit([self.ecuReadZone, "Unlocked successfully for configuration", self.zoneName], time.time())
elif decodedData[0: + 2] == "7F":
file = open("./data/ErrorResponse.json", 'r', encoding='utf-8')
jsonFile = file.read()
errorList = json.loads(jsonFile.encode("utf-8"))
if len(decodedData) >= 6:
error = decodedData[4:6]
cmd = decodedData[2:4]
if error in errorList:
error = "Error: (" + cmd + ") " + errorList[error]
self.receivedPacketSignal.emit([self.ecuReadZone, error, self.zoneName], time.time())
self.updateZoneDataSignal.emit(self.ecuReadZone, error)
else:
self.receivedPacketSignal.emit([self.ecuReadZone, "No Response", self.zoneName], time.time())
self.updateZoneDataSignal.emit(self.ecuReadZone, "No Response")
else:
self.receivedPacketSignal.emit([self.ecuReadZone, "No Response", self.zoneName], time.time())
self.updateZoneDataSignal.emit(self.ecuReadZone, "No Response")
else:
self.receivedPacketSignal.emit([self.ecuReadZone, "Unkown Error", self.zoneName], time.time())
self.updateZoneDataSignal.emit(self.ecuReadZone, "Unkown Error")
elif len(decodedData) <= 2:
if decodedData[0:2] == "OK":
self.receivedPacketSignal.emit([self.ecuReadZone, "OK", self.zoneName], time.time())
else:
self.receivedPacketSignal.emit([self.ecuReadZone, "Unkown Error", self.zoneName], time.time())
self.updateZoneDataSignal.emit(self.ecuReadZone, "Unkown Error")
return data
def run(self):
self.isRunning = True
while self.isRunning:
if not self.writeQ.empty():
element = self.writeQ.get()
if isinstance(element, dict):
for zoneIDObject in element:
self.ecuReadZone = str(zoneIDObject).upper()
self.zoneActive = element[str(zoneIDObject)]
self.zoneName = str(self.zoneActive["name"])
# Send and receive data
ecuReadZoneSend = self.readZoneTag + self.ecuReadZone
receiveData = self.writeECUCommand(ecuReadZoneSend)
self.parseReadResponse(receiveData);
self.msleep(100)
else:
# Just empty zone names
self.zoneName = ""
self.ecuReadZone = str(element).upper()
# Send and receive data
if self.ecuReadZone == self.startDiagmode:
# Timeout on open Diag Mode, No ECU? then stop reading
if not self.startDiagnosticMode():
self.writeToOutputView("Open Diagnostic session: Failed/Stopping", receiveData)
self.emptyQueue()
self.isRunning = False
elif self.ecuReadZone == self.stopDiagmode:
self.stopDiagnosticMode()
elif self.ecuReadZone == self.keepAlive:
self.startSendingKeepAlive()
elif self.ecuReadZone == self.stopKeepAlive:
self.stopSendingKeepAlive()
else:
receiveData = self.writeECUCommand(self.ecuReadZone)
else:
self.writeToOutputView("Reading ECU Zones: Successful")
self.isRunning = False