-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod_utils.py
114 lines (100 loc) · 3.06 KB
/
mod_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
import sys, string
import mod_globals
def pyren_encode(inp):
return inp.encode('utf-8', errors='replace')
def pyren_decode(inp):
try:
if mod_globals.os == 'android':
return inp.decode('utf-8', errors='replace')
else:
return inp.decode(sys.stdout.encoding, errors='replace')
except:
return inp
def pyren_decode_i(inp):
if mod_globals.os == 'android':
return inp.decode('utf-8', errors='ignore')
else:
return inp.decode(sys.stdout.encoding, errors='ignore')
def clearScreen():
sys.stdout.write(chr(27) + '[2J' + chr(27) + '[;H')
def hex_VIN_plus_CRC(VIN, plusCRC=True):
VIN = VIN.upper()
hexVIN = ''
CRC = 65535
for c in VIN:
b = ord(c)
hexVIN = hexVIN + hex(b)[2:].upper()
for i in range(8):
if (CRC ^ b) & 1:
CRC = CRC >> 1
CRC = CRC ^ 33800
b = b >> 1
else:
CRC = CRC >> 1
b = b >> 1
CRC = CRC ^ 65535
b1 = CRC >> 8 & 255
b2 = CRC & 255
CRC = (b2 << 8 | b1) & 65535
sCRC = hex(CRC)[2:].upper()
sCRC = '0' * (4 - len(sCRC)) + sCRC
if plusCRC:
return hexVIN + sCRC
else:
return hexVIN
def ASCIITOHEX(ATH):
ATH = ATH.upper()
hexATH = ''.join(('{:02x}'.format(ord(c)) for c in ATH))
return hexATH
def StringToIntToHex(DEC):
DEC = int(DEC)
hDEC = hex(DEC)
return hDEC[2:].zfill(2).upper()
def getVIN(de, elm, getFirst = False):
import os
m_vin = set([])
for e in de:
if mod_globals.opt_demo:
loadDumpToELM(e['ecuname'], elm)
else:
if e['pin'].lower() == 'can':
elm.init_can()
elm.set_can_addr(e['dst'], e)
else:
elm.init_iso()
elm.set_iso_addr(e['dst'], e)
elm.start_session(e['startDiagReq'])
if e['stdType'].lower() == 'uds':
rsp = elm.request(req='22F190', positive='62', cache=False)[9:59]
else:
rsp = elm.request(req='2181', positive='61', cache=False)[6:56]
if True:
vin = rsp.replace(' ', '').decode('HEX')
else:
continue
if len(vin) == 17:
m_vin.add(vin)
if getFirst:
return vin
l_vin = m_vin
if os.path.exists('savedVIN.txt'):
with open('savedVIN.txt') as vinfile:
vinlines = vinfile.readlines()
for l in vinlines:
l = l.strip()
if '#' in l:
continue
if len(l) == 17:
l_vin.add(l.upper())
if len(l_vin) == 0 and not getFirst:
exit()
if len(l_vin) < 2:
if True:
ret = next(iter(l_vin))
else:
ret = ''
return ret
choice = Choice(l_vin, 'Choose VIN : ')
return choice[0]
def isHex(s):
return all(c in string.hexdigits for c in s)