From dc24ce4cf9f9d79220929f5bb665eba11b2db214 Mon Sep 17 00:00:00 2001 From: Willem Melching Date: Tue, 19 Mar 2024 12:51:12 +0100 Subject: [PATCH] [ISO-TP] wait for flow control every block_size frames (#42) --- src/isotp/mod.rs | 18 +++++++++++++----- tests/isotp_tests.rs | 19 +++++++++++-------- 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/isotp/mod.rs b/src/isotp/mod.rs index 57ceaf0..e63901b 100644 --- a/src/isotp/mod.rs +++ b/src/isotp/mod.rs @@ -184,7 +184,7 @@ impl<'a> IsoTPAdapter<'a> { self.send_first_frame(data).await; let frame = stream.next().await.unwrap()?; - let fc_config = self.receive_flow_control(&frame)?; + let mut fc_config = self.receive_flow_control(&frame)?; debug!("RX FC, data {}", hex::encode(&frame.data)); @@ -199,11 +199,19 @@ impl<'a> IsoTPAdapter<'a> { while let Some((idx, chunk)) = it.next() { self.send_consecutive_frame(chunk, idx).await; - // Sleep for separation time between frames - let last = it.peek().is_none(); - if !last { - tokio::time::sleep(st_min).await; + // Wait for flow control every `block_size` frames, except for the first frame + if fc_config.block_size != 0 && idx > 0 && idx % fc_config.block_size as usize == 0 { + // Wait for next flow control + let frame = stream.next().await.unwrap()?; + fc_config = self.receive_flow_control(&frame)?; + } else { + // Sleep for separation time between frames + let last = it.peek().is_none(); + if !last { + tokio::time::sleep(st_min).await; + } } + } Ok(()) diff --git a/tests/isotp_tests.rs b/tests/isotp_tests.rs index 61612b5..f2e7b7f 100644 --- a/tests/isotp_tests.rs +++ b/tests/isotp_tests.rs @@ -118,12 +118,15 @@ async fn isotp_test_stmin() { #[tokio::test] #[serial_test::serial] async fn isotp_test_bs() { - let config = VECUConfig { - bs: 4, - ..Default::default() - }; - - // TODO: can we ensure that we actually wait for the - // flow control between blocks? - isotp_test_echo(64, config).await; + for bs in 1..=8 { + let config = VECUConfig { + bs, + ..Default::default() + }; + isotp_test_echo(64, config).await; + + // TODO: can we ensure that we actually wait for the + // flow control between blocks? + isotp_test_echo(64, config).await; + } }