Skip to content

Commit

Permalink
python2 -> 3 fixes to pedal flasher (#292)
Browse files Browse the repository at this point in the history
* python2 -> 3 fixes to pedal flasher

* fix Debug flash as well

* Fixes to wifi-test
  • Loading branch information
rbiasini authored Oct 9, 2019
1 parent 78b75ef commit ffbdb87
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 36 deletions.
10 changes: 5 additions & 5 deletions python/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def parse_can_buffer(dat):
address = f1 >> 21
dddat = ddat[8:8+(f2&0xF)]
if DEBUG:
print(" R %x: %s" % (address, str(dddat).encode("hex")))
print(" R %x: %s" % (address, binascii.hexlify(dddat)))
ret.append((address, f2>>16, dddat, (f2>>4)&0xFF))
return ret

Expand Down Expand Up @@ -451,7 +451,7 @@ def can_send_many(self, arr):
for addr, _, dat, bus in arr:
assert len(dat) <= 8
if DEBUG:
print(" W %x: %s" % (addr, dat.encode("hex")))
print(" W %x: %s" % (addr, binascii.hexlify(dat)))
if addr >= 0x800:
rir = (addr << 3) | transmit | extended
else:
Expand Down Expand Up @@ -550,7 +550,7 @@ def kline_drain(self, bus=2):
if len(ret) == 0:
break
elif DEBUG:
print("kline drain: "+str(ret).encode("hex"))
print("kline drain: " + binascii.hexlify(ret))
bret += ret
return bytes(bret)

Expand All @@ -559,7 +559,7 @@ def kline_ll_recv(self, cnt, bus=2):
while len(echo) != cnt:
ret = str(self._handle.controlRead(Panda.REQUEST_OUT, 0xe0, bus, 0, cnt-len(echo)))
if DEBUG and len(ret) > 0:
print("kline recv: "+ret.encode("hex"))
print("kline recv: " + binascii.hexlify(ret))
echo += ret
return str(echo)

Expand All @@ -576,7 +576,7 @@ def get_checksum(dat):
for i in range(0, len(x), 0xf):
ts = x[i:i+0xf]
if DEBUG:
print("kline send: "+ts.encode("hex"))
print("kline send: " + binascii.hexlify(ts))
self._handle.bulkWrite(2, chr(bus).encode()+ts)
echo = self.kline_ll_recv(len(ts), bus=bus)
if echo != ts:
Expand Down
54 changes: 28 additions & 26 deletions python/isotp.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import binascii

DEBUG = False

def msg(x):
if DEBUG:
print("S:",x.encode("hex"))
print("S:", binascii.hexlify(x))
if len(x) <= 7:
ret = chr(len(x)) + x
else:
assert False
return ret.ljust(8, "\x00")
return ret.ljust(8, b"\x00")

kmsgs = []
def recv(panda, cnt, addr, nbus):
Expand All @@ -24,35 +26,35 @@ def recv(panda, cnt, addr, nbus):
# leave around
nmsgs.append((ids, ts, dat, bus))
kmsgs = nmsgs[-256:]
return list(map(str, ret))
return ret

def isotp_recv_subaddr(panda, addr, bus, sendaddr, subaddr):
msg = recv(panda, 1, addr, bus)[0]

# TODO: handle other subaddr also communicating
assert ord(msg[0]) == subaddr
assert msg[0] == subaddr

if ord(msg[1])&0xf0 == 0x10:
if msg[1]&0xf0 == 0x10:
# first
tlen = ((ord(msg[1]) & 0xf) << 8) | ord(msg[2])
tlen = ((msg[1] & 0xf) << 8) | msg[2]
dat = msg[3:]

# 0 block size?
CONTINUE = chr(subaddr) + "\x30" + "\x00"*6
CONTINUE = chr(subaddr).encode("utf8") + b"\x30" + b"\x00"*6
panda.can_send(sendaddr, CONTINUE, bus)

idx = 1
for mm in recv(panda, (tlen-len(dat) + 5)/6, addr, bus):
assert ord(mm[0]) == subaddr
assert ord(mm[1]) == (0x20 | (idx&0xF))
for mm in recv(panda, (tlen-len(dat) + 5)//6, addr, bus):
assert mm[0] == subaddr
assert mm[1] == (0x20 | (idx&0xF))
dat += mm[2:]
idx += 1
elif ord(msg[1])&0xf0 == 0x00:
elif msg[1]&0xf0 == 0x00:
# single
tlen = ord(msg[1]) & 0xf
tlen = msg[1] & 0xf
dat = msg[2:]
else:
print(msg.encode("hex"))
print(binascii.hexlify(msg))
assert False

return dat[0:tlen]
Expand All @@ -69,26 +71,26 @@ def isotp_send(panda, x, addr, bus=0, recvaddr=None, subaddr=None):
panda.can_send(addr, chr(subaddr)+msg(x)[0:7], bus)
else:
if subaddr:
ss = chr(subaddr) + chr(0x10 + (len(x)>>8)) + chr(len(x)&0xFF) + x[0:5]
ss = (chr(subaddr) + chr(0x10 + (len(x)>>8)) + chr(len(x)&0xFF)).encode("utf8") + x[0:5]
x = x[5:]
else:
ss = chr(0x10 + (len(x)>>8)) + chr(len(x)&0xFF) + x[0:6]
ss = (chr(0x10 + (len(x)>>8)) + chr(len(x)&0xFF)).encode("utf8") + x[0:6]
x = x[6:]
idx = 1
sends = []
while len(x) > 0:
if subaddr:
sends.append(((chr(subaddr) + chr(0x20 + (idx&0xF)) + x[0:6]).ljust(8, "\x00")))
sends.append((((chr(subaddr) + chr(0x20 + (idx&0xF))).encode('utf8') + x[0:6]).ljust(8, b"\x00")))
x = x[6:]
else:
sends.append(((chr(0x20 + (idx&0xF)) + x[0:7]).ljust(8, "\x00")))
sends.append(((chr(0x20 + (idx&0xF)).encode("utf8") + x[0:7]).ljust(8, b"\x00")))
x = x[7:]
idx += 1

# actually send
panda.can_send(addr, ss, bus)
rr = recv(panda, 1, recvaddr, bus)[0]
if rr.find("\x30\x01") != -1:
if rr.find(b"\x30\x01") != -1:
for s in sends[:-1]:
panda.can_send(addr, s, 0)
rr = recv(panda, 1, recvaddr, bus)[0]
Expand All @@ -105,31 +107,31 @@ def isotp_recv(panda, addr, bus=0, sendaddr=None, subaddr=None):
else:
msg = recv(panda, 1, addr, bus)[0]

if ord(msg[0])&0xf0 == 0x10:
if msg[0]&0xf0 == 0x10:
# first
tlen = ((ord(msg[0]) & 0xf) << 8) | ord(msg[1])
tlen = ((msg[0] & 0xf) << 8) | msg[1]
dat = msg[2:]

# 0 block size?
CONTINUE = "\x30" + "\x00"*7
CONTINUE = b"\x30" + b"\x00"*7

panda.can_send(sendaddr, CONTINUE, bus)

idx = 1
for mm in recv(panda, (tlen-len(dat) + 6)/7, addr, bus):
assert ord(mm[0]) == (0x20 | (idx&0xF))
for mm in recv(panda, (tlen-len(dat) + 6)//7, addr, bus):
assert mm[0] == (0x20 | (idx&0xF))
dat += mm[1:]
idx += 1
elif ord(msg[0])&0xf0 == 0x00:
elif msg[0]&0xf0 == 0x00:
# single
tlen = ord(msg[0]) & 0xf
tlen = msg[0] & 0xf
dat = msg[1:]
else:
assert False
dat = dat[0:tlen]

if DEBUG:
print("R:",dat.encode("hex"))
print("R:", binascii.hexlify(dat))

return dat

6 changes: 4 additions & 2 deletions tests/automated/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import sys
import time
import random
import binascii
import subprocess
import requests
import _thread
Expand Down Expand Up @@ -49,7 +50,7 @@ def connect_wifi(serial=None):

FNULL = open(os.devnull, 'w')
def _connect_wifi(dongle_id, pw, insecure_okay=False):
ssid = str("panda-" + dongle_id)
ssid = "panda-" + dongle_id.decode("utf8")

r = subprocess.call(["ping", "-W", "4", "-c", "1", "192.168.0.10"], stdout=FNULL, stderr=subprocess.STDOUT)
if not r:
Expand All @@ -75,7 +76,8 @@ def _connect_wifi(dongle_id, pw, insecure_okay=False):
print("WIFI: scanning %d" % cnt)
os.system("iwlist %s scanning > /dev/null" % wlan_interface)
os.system("nmcli device wifi rescan")
wifi_scan = [x for x in subprocess.check_output(["nmcli","dev", "wifi", "list"]).split("\n") if ssid in x]
wifi_networks = [x.decode("utf8") for x in subprocess.check_output(["nmcli","dev", "wifi", "list"]).split(b"\n")]
wifi_scan = [x for x in wifi_networks if ssid in x]
if len(wifi_scan) != 0:
break
time.sleep(0.1)
Expand Down
6 changes: 3 additions & 3 deletions tests/pedal/enter_canloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ def bulkRead(self, endpoint, length, timeout=0):
break

if args.recover:
p.can_send(0x200, "\xce\xfa\xad\xde\x1e\x0b\xb0\x02", 0)
p.can_send(0x200, b"\xce\xfa\xad\xde\x1e\x0b\xb0\x02", 0)
exit(0)
else:
p.can_send(0x200, "\xce\xfa\xad\xde\x1e\x0b\xb0\x0a", 0)
p.can_send(0x200, b"\xce\xfa\xad\xde\x1e\x0b\xb0\x0a", 0)

if args.fn:
time.sleep(0.1)
print("flashing", args.fn)
code = open(args.fn).read()
code = open(args.fn, "rb").read()
Panda.flash_static(CanHandle(p), code)

print("can flash done")
Expand Down

0 comments on commit ffbdb87

Please sign in to comment.