From be828991ae4a14b82a4ebd8dff1b7890fdb38cb4 Mon Sep 17 00:00:00 2001 From: George Hotz Date: Fri, 9 Mar 2018 17:37:47 -0800 Subject: [PATCH] despite it being bad code, move isotp --- python/__init__.py | 1 + {examples => python}/isotp.py | 65 ++++++++++++++++++----------------- 2 files changed, 34 insertions(+), 32 deletions(-) rename {examples => python}/isotp.py (98%) diff --git a/python/__init__.py b/python/__init__.py index 35ce5248e47e02..09c18e2d6173d0 100644 --- a/python/__init__.py +++ b/python/__init__.py @@ -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' diff --git a/examples/isotp.py b/python/isotp.py similarity index 98% rename from examples/isotp.py rename to python/isotp.py index f0b0b5f387de74..74720b75d9eac1 100644 --- a/examples/isotp.py +++ b/python/isotp.py @@ -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 @@ -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