Skip to content

Commit

Permalink
despite it being bad code, move isotp
Browse files Browse the repository at this point in the history
  • Loading branch information
geohot committed Mar 10, 2018
1 parent 000715b commit be82899
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 32 deletions.
1 change: 1 addition & 0 deletions python/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from flash_release import flash_release
from update import ensure_st_up_to_date
from serial import PandaSerial
from isotp import isotp_send, isotp_recv

__version__ = '0.0.6'

Expand Down
65 changes: 33 additions & 32 deletions examples/isotp.py → python/isotp.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,39 @@ def recv(panda, cnt, addr, nbus):
kmsgs = nmsgs[-256:]
return map(str, 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

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

# 0 block size?
CONTINUE = chr(subaddr) + "\x30" + "\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)
dat += mm[2:]
idx += 1
elif ord(msg[1])&0xf0 == 0x00:
# single
tlen = ord(msg[1]) & 0xf
dat = msg[2:]
else:
print msg.encode("hex")
assert False

return dat[0:tlen]

# **** import below this line ****

def isotp_send(panda, x, addr, bus=0, recvaddr=None, subaddr=None):
if recvaddr is None:
recvaddr = addr+8
Expand Down Expand Up @@ -63,38 +96,6 @@ def isotp_send(panda, x, addr, bus=0, recvaddr=None, subaddr=None):
else:
panda.can_send_many([(addr, None, s, 0) for s in sends])

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

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

# 0 block size?
CONTINUE = chr(subaddr) + "\x30" + "\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)
dat += mm[2:]
idx += 1
elif ord(msg[1])&0xf0 == 0x00:
# single
tlen = ord(msg[1]) & 0xf
dat = msg[2:]
else:
print msg.encode("hex")
assert False

return dat[0:tlen]


def isotp_recv(panda, addr, bus=0, sendaddr=None, subaddr=None):
if sendaddr is None:
sendaddr = addr-8
Expand Down

0 comments on commit be82899

Please sign in to comment.