-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod_ddt_utils.py
282 lines (233 loc) · 9.35 KB
/
mod_ddt_utils.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
#!/usr/bin/env python
import os
import xml.etree.ElementTree as et
import mod_globals
import mod_db_manager
from operator import itemgetter
from copy import deepcopy
from kivy.utils import platform
if platform != 'android':
import serial
from serial.tools import list_ports
else:
from jnius import autoclass
mod_globals.os = 'android'
BluetoothAdapter = autoclass('android.bluetooth.BluetoothAdapter')
BluetoothDevice = autoclass('android.bluetooth.BluetoothDevice')
BluetoothSocket = autoclass('android.bluetooth.BluetoothSocket')
UUID = autoclass('java.util.UUID')
try:
import cPickle as pickle
except:
import pickle
def searchddtroot():
if not os.path.exists('../DDT2000data/ecus'):
mod_globals.ddtroot = '..'
else:
mod_globals.ddtroot = '../DDT2000data'
return
class settings():
path = ''
port = ''
lang = 'RU'
speed = '38400'
logName = 'log.txt'
log = False
cfc = False
n1c = False
si = False
dump = False
can2 = False
options = ''
def __init__(self):
self.load()
def __del__(self):
pass
def load(self):
if not os.path.isfile("../settings.p"):
self.save()
f = open('../settings.p', 'rb')
tmp_dict = pickle.load(f)
f.close()
self.__dict__.update(tmp_dict)
def save(self):
f = open('../settings.p', 'wb')
pickle.dump(self.__dict__, f)
f.close()
def multikeysort(items, columns):
comparers = [ ((itemgetter(col[1:].strip()), -1) if col.startswith('-') else (itemgetter(col.strip()), 1)) for col in columns]
def comparer(left, right):
for fn, mult in comparers:
result = cmp(fn(left), fn(right))
if result:
return mult * result
else:
return 0
return sorted(items, cmp=comparer)
def getPortList():
devs = {}
if mod_globals.os != 'android':
iterator = sorted(list(serial.tools.list_ports.comports()))
for port, desc, hwid in iterator:
devs[desc] = port
return devs
paired_devices = BluetoothAdapter.getDefaultAdapter().getBondedDevices().toArray()
for device in paired_devices:
deviceName = device.getName()
if deviceName:
deviceAddress = device.getAddress()
devs[deviceName] = deviceAddress
return devs
def loadECUlist():
eculistcache = os.path.join(mod_globals.cache_dir, "ddt_eculist.p")
if os.path.isfile(eculistcache):
eculist = pickle.load(open(eculistcache, "rb"))
else:
eculistfilename = 'ecus/eculist.xml'
if not mod_db_manager.file_in_ddt(eculistfilename):
return None
ns = {'ns0': 'http://www-diag.renault.com/2002/ECU',
'ns1': 'http://www-diag.renault.com/2002/screens'}
tree = et.parse(mod_db_manager.get_file_from_ddt(eculistfilename))
root = tree.getroot()
eculist = {}
functions = root.findall("Function")
if len(functions):
for function in functions:
Address = hex(int(function.attrib["Address"])).replace("0x", "").zfill(2).upper()
eculist[Address] = {}
FuncName = function.attrib["Name"]
targets = function.findall("Target")
eculist[Address]["FuncName"] = FuncName
eculist[Address]["targets"] = {}
if len(targets):
for target in targets:
href = target.attrib["href"]
eculist[Address]["targets"][href] = {}
pjc = target.findall("Projects")
if len(pjc) > 0:
pjcl = [elem.tag.upper() for elem in pjc[0].iter()][1:]
else:
pjcl = []
eculist[Address]["targets"][href]['Projects'] = pjcl
ail = []
Prot = target.findall("Protocol")
eculist[Address]["targets"][href]['Protocol'] = Prot[0].text
ais = target.findall("ns0:AutoIdents", ns)
if len(ais)==0:
ais = target.findall("AutoIdents")
if len(ais):
for ai in ais:
AutoIdents = ai.findall("ns0:AutoIdent", ns)
if len(AutoIdents)==0:
AutoIdents = ai.findall("AutoIdent")
if len(AutoIdents):
for AutoIdent in AutoIdents:
air = {}
air['DiagVersion'] = AutoIdent.attrib["DiagVersion"].strip()
air['Supplier'] = AutoIdent.attrib["Supplier"].strip()
air['Soft'] = AutoIdent.attrib["Soft"].strip()
air['Version'] = AutoIdent.attrib["Version"].strip()
ail.append(air)
eculist[Address]["targets"][href]['AutoIdents'] = ail
pickle.dump(eculist, open(eculistcache, "wb"))
return eculist
class ddtProjects():
def __init__(self):
self.proj_path = 'vehicles/projects.xml'
self.plist = []
if not mod_db_manager.file_in_ddt(self.proj_path):
return
tree = et.parse(mod_db_manager.get_file_from_ddt(self.proj_path))
root = tree.getroot()
DefaultAddressing = root.findall('DefaultAddressing')
if DefaultAddressing:
defaddrsheme = DefaultAddressing[0].text
Manufacturer = root.findall('Manufacturer')
if Manufacturer:
for ma in Manufacturer:
name = ma.findall('name')
if name:
ma_name = ma[0].text
else:
ma_name = 'Unknown'
pl_ma = {}
pl_ma['name'] = ma_name
pl_ma['list'] = []
project = ma.findall('project')
if project:
for pr in project:
cartype = {}
addressing = pr.findall('addressing')
if addressing:
cartype['addr'] = addressing[0].text
else:
cartype['addr'] = defaddrsheme
if 'code' in pr.attrib:
cartype['code'] = pr.attrib['code']
else:
cartype['code'] = ''
if 'name' in pr.attrib:
cartype['name'] = pr.attrib['name']
else:
cartype['name'] = ''
if 'segment' in pr.attrib:
cartype['segment'] = pr.attrib['segment']
else:
cartype['segment'] = ''
pl_ma['list'].append(cartype)
self.plist.append(pl_ma)
class ddtAddressing():
def __init__(self, filename ):
self.addr_path = 'vehicles/' + filename
self.alist = []
if not mod_db_manager.file_in_ddt(self.addr_path):
return
tree = et.parse(mod_db_manager.get_file_from_ddt(self.addr_path))
root = tree.getroot()
ns = {'ns0': 'DiagnosticAddressingSchema.xml',
'ns1': 'http://www.w3.org/XML/1998/namespace'}
Function = root.findall('ns0:Function', ns)
if Function:
for fu in Function:
fun = {}
fun['Address'] = int(fu.attrib['Address'])
fun['Name'] = fu.attrib['Name']
baudRate = fu.findall('ns0:baudRate',ns)
if baudRate:
fun['baudRate'] = baudRate[0].text
else:
fun['baudRate'] = '0'
Names = fu.findall('ns0:Name',ns)
fun['longname'] = {}
if Names:
for Name in Names:
fun['longname'][Name.attrib.values()[0]] = Name.text
XId = fu.findall('ns0:XId',ns)
fun['XId'] = ''
if XId:
fun['XId'] = XId[0].text
RId = fu.findall('ns0:RId',ns)
fun['RId'] = ''
if XId:
fun['RId'] = RId[0].text
RId = fu.findall('ns0:RId',ns)
fun['RId'] = ''
if XId:
fun['RId'] = RId[0].text
ISO8 = fu.findall('ns0:ISO8',ns)
fun['ISO8'] = ''
if ISO8:
fun['ISO8'] = ISO8[0].text
protocolList = fu.findall('ns0:ProtocolList',ns)
if protocolList:
protocols = protocolList[0].findall('ns0:Protocol',ns)
for proto in protocols:
fun['protocol'] = proto.attrib['Code']
self.alist.append(fun)
tmp = deepcopy(fun)
fun = tmp
else:
fun['protocol'] = ''
self.alist.append(fun)
fun['xml'] = ''