-
-
Notifications
You must be signed in to change notification settings - Fork 236
/
sniffer.py
172 lines (134 loc) · 4.9 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
# -*- coding: utf-8 -*-
import time
import ecu
from uiutils import *
import PyQt4.QtGui as gui
import PyQt4.QtCore as core
import options
__author__ = "Cedric PAILLE"
__copyright__ = "Copyright 2016-2017"
__credits__ = []
__license__ = "GPL"
__version__ = "1.0.0"
__maintainer__ = "Cedric PAILLE"
__email__ = "cedricpaille@gmail.com"
__status__ = "Beta"
class snifferThread(core.QThread):
dataready = core.pyqtSignal(basestring)
def __init__(self, address):
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)
def stop(self):
options.elm.monitorstop = True
while self.running:
time.sleep(.1)
def senddata(self, data):
self.dataready.emit(data)
def run(self):
while not options.elm.monitorstop:
if options.simulation_mode:
time.sleep(.1)
self.dataready.emit("0: 03 00 00 00 00 40 00 00")
else:
options.elm.monitor_can_bus(self.senddata)
print "Thread end"
self.running = False
class sniffer(gui.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 = gui.QHBoxLayout()
self.framecombo = gui.QComboBox()
self.startbutton = gui.QPushButton(">>")
self.addressinfo = gui.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 = gui.QVBoxLayout()
self.setLayout(vlayout)
vlayout.addLayout(hlayout)
self.table = gui.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()
framename = unicode(self.framecombo.currentText().toUtf8(), encoding="UTF-8")
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.setColumnCount(1)
self.table.setRowCount(len(self.names))
headerstrings = core.QString(headernames).split(";")
self.table.setVerticalHeaderLabels(headerstrings)
self.table.setHorizontalHeaderLabels(["Values"])
self.table.resizeColumnsToContents()
self.table.resizeRowsToContents()
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.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)
if '0:' not in data:
return
data = data[3:24].replace(' ', '')
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.setItem(i, 0, gui.QTableWidgetItem(value))
i += 1
def init(self):
self.ecurequests = ecu.Ecu_file(self.ecu_file, True)
self.framecombo.clear()
self.table.clear()
self.currentrequest = None
self.oktostart = False
self.startbutton.setEnabled(False)
if self.ecurequests.funcaddr != "00":
self.ecu_file = None
self.ecurequests = None
return False
for req in self.ecurequests.requests.keys():
self.framecombo.addItem(req)
self.oktostart = True
self.startbutton.setEnabled(True)
return True