-
-
Notifications
You must be signed in to change notification settings - Fork 236
/
sniffer.py
202 lines (156 loc) · 5.86 KB
/
sniffer.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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import string
import time
import PyQt5.QtCore as core
import PyQt5.QtWidgets as widgets
import ecu
import options
import version
_ = options.translator('ddt4all')
class snifferThread(core.QThread):
# Use a thread to avoid ELM buffer flooding
try:
# TODO:// basestring not defined use bytes.
# dataready = core.pyqtSignal(basestring)
dataready = core.pyqtSignal(bytes)
except:
dataready = core.pyqtSignal(str)
def __init__(self, address, br):
super(snifferThread, self).__init__()
self.filter = address
self.running = True
if not options.simulation_mode:
options.elm.monitorstop = False
options.elm.init_can_sniffer(self.filter, br)
def stop(self):
if not options.simulation_mode:
options.elm.monitorstop = True
else:
return
while self.running:
time.sleep(.1)
def senddata(self, data):
self.dataready.emit(data)
def run(self):
if options.simulation_mode:
if options.simulation_mode:
while 1:
time.sleep(.1)
# Test data
self.dataready.emit("0300000000400000")
return
while not options.elm.monitorstop:
options.elm.monitor_can_bus(self.senddata)
self.running = False
class sniffer(widgets.QWidget):
def __init__(self, parent=None):
super(sniffer, self).__init__(parent)
self.ecu_file = None
self.ecurequests = None
self.snifferthread = None
self.currentrequest = None
self.names = []
self.oktostart = False
self.ecu_filter = ""
hlayout = widgets.QHBoxLayout()
self.framecombo = widgets.QComboBox()
self.startbutton = widgets.QPushButton(">>")
self.addressinfo = widgets.QLabel("0000")
self.startbutton.setCheckable(True)
self.startbutton.toggled.connect(self.startmonitoring)
self.addressinfo.setFixedWidth(90)
self.startbutton.setFixedWidth(90)
hlayout.addWidget(self.addressinfo)
hlayout.addWidget(self.framecombo)
hlayout.addWidget(self.startbutton)
vlayout = widgets.QVBoxLayout()
self.setLayout(vlayout)
vlayout.addLayout(hlayout)
self.table = widgets.QTableWidget()
vlayout.addWidget(self.table)
self.framecombo.activated.connect(self.change_frame)
def startmonitoring(self, onoff):
if onoff:
if self.oktostart:
self.startthread(self.ecu_filter)
else:
self.stopthread()
def change_frame(self):
self.stopthread()
self.startbutton.setChecked(False)
self.names = []
framename = self.framecombo.currentText()
self.currentrequest = self.ecurequests.requests[framename]
self.ecu_filter = self.currentrequest.sentbytes
self.addressinfo.setText(self.ecu_filter)
self.names = self.currentrequest.dataitems.keys()
headernames = ";".join([n for n in self.names])
self.table.clear()
self.table.setColumnCount(1)
self.table.setRowCount(len(self.names))
headerstrings = headernames.split(";")
self.table.setVerticalHeaderLabels(headerstrings)
self.table.setHorizontalHeaderLabels([_("Values")])
for i in range(0, len(self.names)):
item = widgets.QTableWidgetItem(_("Waiting..."))
item.setFlags(item.flags() ^ core.Qt.ItemIsEditable)
self.table.setItem(i, 0, item)
self.table.resizeColumnsToContents()
self.table.resizeRowsToContents()
self.table.horizontalHeader().setSectionResizeMode(0, widgets.QHeaderView.Stretch)
def stopthread(self):
if self.snifferthread:
self.snifferthread.stop()
self.snifferthread.dataready.disconnect()
self.snifferthread.quit()
self.snifferthread = None
self.framecombo.setEnabled(True)
def startthread(self, ecu_filter):
self.framecombo.setEnabled(False)
self.stopthread()
self.snifferthread = snifferThread(ecu_filter, self.ecurequests.baudrate)
self.snifferthread.dataready.connect(self.callback)
self.snifferthread.start()
def set_file(self, ecufile):
self.stopthread()
self.ecu_file = ecufile
return self.init()
def callback(self, stream):
data = str(stream).replace(" ", "").strip()
if '0:' in data:
return
if len(data) > 16:
print(_("Frame length error: "), data)
return
if not all(c in string.hexdigits for c in data):
print(_("Frame hex error: "), data)
return
data = data.replace(' ', '').ljust(16, "0")
if self.currentrequest:
values = self.currentrequest.get_values_from_stream(data)
i = 0
for name in self.names:
if name in values:
value = values[name]
if value is not None:
self.table.item(i, 0).setText(value)
i += 1
def init(self):
self.ecurequests = ecu.Ecu_file(self.ecu_file, True)
self.framecombo.clear()
self.table.clear()
self.table.setRowCount(0)
self.currentrequest = None
self.oktostart = False
self.startbutton.setEnabled(False)
if not (self.ecurequests.funcaddr == "00" or self.ecurequests.funcaddr == "FF"):
self.ecu_file = None
self.ecurequests = None
return False
for req in sorted(self.ecurequests.requests.keys()):
if 'DTOOL' not in req.upper():
self.framecombo.addItem(req)
self.oktostart = True
self.startbutton.setEnabled(True)
return True