Skip to content

Commit 4698f2f

Browse files
committed
Fixed #41. Timeout now raised when CF is not received after flow control without waiting on next CAN msg.
1 parent ecbf5c8 commit 4698f2f

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

isotp/protocol.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,9 @@ def process(self):
439439
Function to be called periodically, as fast as possible.
440440
This function is non-blocking.
441441
"""
442+
443+
self.check_timeouts_rx()
444+
442445
msg = True
443446
while msg is not None:
444447
msg = self.rxfn()
@@ -453,6 +456,12 @@ def process(self):
453456
self.logger.debug("Sending : <%03X> (%d)\t %s" % (msg.arbitration_id, len(msg.data), binascii.hexlify(msg.data)))
454457
self.txfn(msg)
455458

459+
def check_timeouts_rx(self):
460+
# Check timeout first
461+
if self.timer_rx_cf.is_timed_out():
462+
self.trigger_error(isotp.errors.ConsecutiveFrameTimeoutError("Reception of CONSECUTIVE_FRAME timed out."))
463+
self.stop_receiving()
464+
456465
def process_rx(self, msg):
457466

458467
if not self.address.is_for_me(msg):
@@ -466,11 +475,6 @@ def process_rx(self, msg):
466475
self.stop_receiving()
467476
return
468477

469-
# Check timeout first
470-
if self.timer_rx_cf.is_timed_out():
471-
self.trigger_error(isotp.errors.ConsecutiveFrameTimeoutError("Reception of CONSECUTIVE_FRAME timed out."))
472-
self.stop_receiving()
473-
474478
# Process Flow Control message
475479
if pdu.type == PDU.Type.FLOW_CONTROL:
476480
self.last_flow_control_frame = pdu # Given to process_tx method. Queue of 1 message depth
@@ -587,7 +591,6 @@ def process_tx(self):
587591

588592

589593
# ======= FSM ======
590-
591594
# Check this first as we may have another isotp frame to send and we need to handle it right away without waiting for next "process()" call
592595
if self.tx_state != self.TxState.IDLE and len(self.tx_buffer) == 0:
593596
self.stop_sending()

test/test_transport_layer.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -235,19 +235,31 @@ def test_receive_multiframe_bad_seqnum(self):
235235
self.assertIsNone(self.get_tx_can_msg()) # Do not send flow control
236236
self.assert_error_triggered(isotp.WrongSequenceNumberError)
237237

238+
def test_receive_timeout_consecutive_frame_after_flow_control(self):
239+
self.stack.params.set('rx_consecutive_frame_timeout', 200)
240+
241+
payload_size = 10
242+
payload = self.make_payload(payload_size)
243+
self.simulate_rx(data = [0x10, payload_size] + payload[0:6])
244+
self.stack.process()
245+
time.sleep(0.2) # Should stop receivving after 200 msec
246+
self.stack.process()
247+
self.assertIsNone(self.rx_isotp_frame()) # No message received indeed
248+
self.assert_error_triggered(isotp.ConsecutiveFrameTimeoutError)
249+
238250
def test_receive_timeout_consecutive_frame_after_first_frame(self):
239251
self.stack.params.set('rx_consecutive_frame_timeout', 200)
240252

241253
payload_size = 10
242254
payload = self.make_payload(payload_size)
243255
self.simulate_rx(data = [0x10, payload_size] + payload[0:6])
244256
self.stack.process()
245-
time.sleep(0.2) # Should stop receivving after 200 msec
257+
time.sleep(0.2) # Should stop receivving after 200 msec
246258
self.simulate_rx(data = [0x21] + payload[6:10])
247259
self.stack.process()
248-
self.assertIsNone(self.rx_isotp_frame()) # No message received indeed
260+
self.assertIsNone(self.rx_isotp_frame()) # No message received indeed
249261
self.assert_error_triggered(isotp.ConsecutiveFrameTimeoutError)
250-
self.assert_error_triggered(isotp.UnexpectedConsecutiveFrameError)
262+
self.assert_error_triggered(isotp.UnexpectedConsecutiveFrameError)
251263

252264
def test_receive_recover_timeout_consecutive_frame(self):
253265
self.stack.params.set('rx_consecutive_frame_timeout', 200)

0 commit comments

Comments
 (0)