forked from Barracuda09/PyPSADiag
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUDSCommunication.py
304 lines (262 loc) · 13.5 KB
/
UDSCommunication.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
"""
UDSCommunication.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
from PySide6.QtCore import Qt, QThread, Signal
from PySide6.QtWidgets import QTextEdit
from SeedKeyAlgorithm import SeedKeyAlgorithm
class UDSCommunication(QThread):
receivedPacketSignal = Signal(list, float)
outputToTextEditSignal = Signal(str)
updateZoneDataSignal = Signal(str, str, str)
algo = SeedKeyAlgorithm()
writeQ = queue.Queue()
ecuReadZone = str()
zoneName = str()
formType = str()
zoneActive = dict()
def __init__(self, serialPort, simulation: bool):
super(UDSCommunication, self).__init__()
self.serialPort = serialPort
self.simulation = simulation
self.isRunning = False
#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
def __simulateAnswer(self, cmd: str):
if cmd[:1] == ">":
return "OK"
if cmd[:1] == ":":
return "6704"
if cmd == "1003":
return "500300C80014"
if cmd == "1001":
return "500100C80014"
if cmd == "2703":
return "67036B0A71E0"
if cmd[:4] == "2704":
return "6704"
if cmd[:2] == "22":
return "62" + cmd[2:6] + "0000"
if cmd[:2] == "2E":
return "6E" + cmd[2:6]
return "Timeout"
def writeToOutputView(self, text: str):
self.outputToTextEditSignal.emit(text)
def writeECUCommand(self, cmd: str):
self.writeToOutputView("> " + cmd)
receiveData = self.serialPort.sendReceive(cmd)
if self.simulation and receiveData == "Timeout":
receiveData = self.__simulateAnswer(cmd)
self.writeToOutputView("< " + receiveData)
return receiveData
def writeZoneList(self, useSketchSeed: bool, ecuID: str, key: str, valueList: list, writeSecureTraceability: bool):
if self.serialPort.isOpen():
startDiagmode = "1003"
stopDiagmode = "1001"
sketchSeedSetup = ":" + key + ":03:03"
unlockServiceConfig = "2703"
unlockResponseConfig = "2704"
readSsecureTraceability = "222901"
secureTraceability = "2E2901FD000000010101"
receiveData = self.writeECUCommand(ecuID)
if receiveData != "OK":
self.writeToOutputView("Selecting ECU: Failed")
return
receiveData = self.writeECUCommand(startDiagmode)
if len(receiveData) != 12 or receiveData[:4] != "5003":
self.writeToOutputView("Open Diagnostic session: Failed")
return
if useSketchSeed:
receiveData = self.writeECUCommand(sketchSeedSetup)
if len(receiveData) != 12 or receiveData[:4] != "6703":
self.writeToOutputView("ECU Seed Request: Failed")
receiveData = self.writeECUCommand(stopDiagmode)
return
self.writeToOutputView("Waiting 1 Sec...")
time.sleep(1)
else:
receiveData = self.writeECUCommand(unlockServiceConfig)
if len(receiveData) != 12 or receiveData[:4] != "6703":
self.writeToOutputView("ECU Seed Request: Failed")
receiveData = self.writeECUCommand(stopDiagmode)
return
challenge = int(receiveData[4:12], 16)
seed = "%0.8X" % self.algo.computeResponse(int(key, 16), challenge)
reply = unlockResponseConfig + seed
receiveData = self.writeECUCommand("KU")
if receiveData != "OK":
self.writeToOutputView("ECU Send keep-alive: Failed")
receiveData = self.writeECUCommand(stopDiagmode)
return
self.writeToOutputView("Waiting 2 Sec...")
time.sleep(2)
receiveData = self.writeECUCommand(reply)
if len(receiveData) != 4 or receiveData[:4] != "6704":
self.writeToOutputView("ECU unlock: Failed")
receiveData = self.writeECUCommand(stopDiagmode)
return
receiveData = self.writeECUCommand("S")
if receiveData != "OK":
self.writeToOutputView("Reset ECU Keep Alive: Failed")
# Write Zones
for tabList in valueList:
for zone in tabList:
writeCmd = "2E" + zone[0] + zone[1]
readCmd = "22" + zone[0]
receiveData = self.writeECUCommand(readCmd)
receiveData = self.writeECUCommand(writeCmd)
if len(receiveData) != 6 or receiveData[:2] != "6E":
self.writeToOutputView("Configuration Write of Zone: Failed")
receiveData = self.writeECUCommand(readCmd)
receiveData = self.writeECUCommand(readSsecureTraceability)
if writeSecureTraceability:
receiveData = self.writeECUCommand(secureTraceability)
if len(receiveData) != 6 or receiveData[:2] != "6E":
self.writeToOutputView("Configuration Write of Secure Traceability Zone: Failed")
else:
self.writeToOutputView("NO Secure Traceability is Written!!")
receiveData = self.writeECUCommand(stopDiagmode)
if len(receiveData) != 12 or receiveData[:4] != "5001":
self.writeToOutputView("Closing Diagnostic session: Failed")
return
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...")
startDiagmode = "1003"
stopDiagmode = "1001"
readEcuFaults = "190209"
receiveData = self.writeECUCommand(ecuID)
if receiveData != "OK":
self.writeToOutputView("ECU Not selected!")
return
receiveData = self.writeECUCommand(startDiagmode)
if len(receiveData) != 12 or receiveData[:4] != "5003":
self.writeToOutputView("Open Diagnostic session: Failed")
return
receiveData = self.writeECUCommand(readEcuFaults)
if len(receiveData) < 4:
self.writeToOutputView("Reading ECU Faults: Failed")
receiveData = self.writeECUCommand(stopDiagmode)
if len(receiveData) != 12 or receiveData[:4] != "5001":
self.writeToOutputView("Closing Diagnostic session: Failed")
return
def setZonesToRead(self, ecuID: 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)
self.writeQ.put("1003")
self.writeQ.put(zoneList)
self.writeQ.put("1001")
def readResponse(self):
data = self.serialPort.readData()
if len(data) == 0:
self.receivedPacketSignal.emit([self.ecuReadZone, "Timeout", "string", "----", self.zoneName], time.time())
return
i = data.find(b"\r")
decodedData = data[:i].decode("utf-8");
if len(decodedData) > 4:
if decodedData[0: + 2] == "62" and len(decodedData) > 6:
# Get only responce data
answerZone = decodedData[2: + 6]
if answerZone.upper() != self.ecuReadZone.upper():
print(answerZone + " - " + self.ecuReadZone)
self.receivedPacketSignal.emit(["Requesed zone different from received zone", "", "", ""], time.time())
return
answer = decodedData[6:]
answerDecorated = answer
valType = self.zoneActive["type"]
# Check if we can find a "Decorated" answer from Combobox
if self.formType == "combobox":
for paramObject in self.zoneActive["params"]:
if valType == "hex":
if int(paramObject["value"], 16) == int(answer, 16):
self.answerDecorated = str(paramObject["name"])
self.receivedPacketSignal.emit([self.ecuReadZone, answer, valType, answerDecorated, self.zoneName], time.time())
self.updateZoneDataSignal.emit(self.ecuReadZone, answer, valType)
elif decodedData[0: + 4] == "5001":
self.receivedPacketSignal.emit([self.ecuReadZone, "Communication closed", "cmd answer", decodedData, self.zoneName], time.time())
elif decodedData[0: + 4] == "5002":
self.receivedPacketSignal.emit([self.ecuReadZone, "Download session opened", "cmd answer", decodedData, self.zoneName], time.time())
elif decodedData[0: + 4] == "5003":
self.receivedPacketSignal.emit([self.ecuReadZone, "Diagnostic session opened", "cmd answer", decodedData, self.zoneName], time.time())
elif decodedData[0: + 4] == "6702":
self.receivedPacketSignal.emit([self.ecuReadZone, "Unlocked successfully for download", "cmd answer", decodedData, self.zoneName], time.time())
elif decodedData[0: + 4] == "6704":
self.receivedPacketSignal.emit([self.ecuReadZone, "Unlocked successfully for configuration", "cmd answer", decodedData, self.zoneName], time.time())
elif decodedData[0: + 2] == "7F":
if len(decodedData) >= 6 and decodedData[0: + 6] == "7F2231":
self.receivedPacketSignal.emit([self.ecuReadZone, "Request out of range", "cmd answer", decodedData, self.zoneName], time.time())
self.updateZoneDataSignal.emit(self.ecuReadZone, "Request out of range", "cmd answer")
else:
self.receivedPacketSignal.emit([self.ecuReadZone, "No Response", "cmd answer", decodedData, self.zoneName], time.time())
self.updateZoneDataSignal.emit(self.ecuReadZone, "No Response", "cmd answer")
else:
self.receivedPacketSignal.emit([self.ecuReadZone, "Unkown Error", "cmd answer", decodedData, self.zoneName], time.time())
self.updateZoneDataSignal.emit(self.ecuReadZone, "Unkown Error", "cmd answer")
elif len(decodedData) <= 2:
if decodedData[0: + 2] == "OK":
self.receivedPacketSignal.emit([self.ecuReadZone, "OK", "cmd answer", decodedData, self.zoneName], time.time())
else:
self.receivedPacketSignal.emit([self.ecuReadZone, "Unkown Error", "cmd answer", decodedData, self.zoneName], time.time())
self.updateZoneDataSignal.emit(self.ecuReadZone, "Unkown Error", "cmd answer")
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)
self.zoneActive = element[str(zoneIDObject)]
self.zoneName = str(self.zoneActive["name"])
self.formType = str(self.zoneActive["form_type"])
# Send and receive data
ecuReadZoneSend = "22" + str(zoneIDObject) + "\n"
self.serialPort.write(ecuReadZoneSend.encode("utf-8"))
self.readResponse();
else:
print("Empty Zone")
# Just empty zone names
self.zoneName = ""
self.ecuReadZone = str(element)
# Send and receive data
command = str(element) + "\n";
self.serialPort.write(command.encode("utf-8"))
self.readResponse();
else:
self.msleep(100)