Skip to content

Commit

Permalink
IsoTpMessage: extend timeout on first frame response (#1933)
Browse files Browse the repository at this point in the history
* extend on first frame too

* debug/checks

* fix able to go from single to first to single etc forever

* more clean up

* more clean up

* comments!
  • Loading branch information
sshane authored Apr 18, 2024
1 parent 714642e commit edcd0fe
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions python/uds.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,8 @@ def recv(self, timeout=None) -> tuple[bytes | None, bool]:
for msg in self._can_client.recv():
frame_type = self._isotp_rx_next(msg)
start_time = time.monotonic()
rx_in_progress = frame_type == ISOTP_FRAME_TYPE.CONSECUTIVE
# Anything that signifies we're building a response
rx_in_progress = frame_type in (ISOTP_FRAME_TYPE.FIRST, ISOTP_FRAME_TYPE.CONSECUTIVE)
if self.tx_done and self.rx_done:
return self.rx_dat, False
# no timeout indicates non-blocking
Expand All @@ -473,6 +474,7 @@ def _isotp_rx_next(self, rx_data: bytes) -> ISOTP_FRAME_TYPE:
# assert len(rx_data) == self.max_len, f"isotp - rx: invalid CAN frame length: {len(rx_data)}"

if rx_data[0] >> 4 == ISOTP_FRAME_TYPE.SINGLE:
assert self.rx_dat == b"" or self.rx_done, "isotp - rx: single frame with active frame"
self.rx_len = rx_data[0] & 0x0F
assert self.rx_len < self.max_len, f"isotp - rx: invalid single frame length: {self.rx_len}"
self.rx_dat = rx_data[1:1 + self.rx_len]
Expand All @@ -483,8 +485,11 @@ def _isotp_rx_next(self, rx_data: bytes) -> ISOTP_FRAME_TYPE:
return ISOTP_FRAME_TYPE.SINGLE

elif rx_data[0] >> 4 == ISOTP_FRAME_TYPE.FIRST:
# Once a first frame is received, further frames must be consecutive
assert self.rx_dat == b"" or self.rx_done, "isotp - rx: first frame with active frame"
self.rx_len = ((rx_data[0] & 0x0F) << 8) + rx_data[1]
assert self.max_len <= self.rx_len, f"isotp - rx: invalid first frame length: {self.rx_len}"
assert self.rx_len >= self.max_len, f"isotp - rx: invalid first frame length: {self.rx_len}"
assert len(rx_data) == self.max_len, f"isotp - rx: invalid CAN frame length: {len(rx_data)}"
self.rx_dat = rx_data[2:]
self.rx_idx = 0
self.rx_done = False
Expand Down

0 comments on commit edcd0fe

Please sign in to comment.