From 09392cb6ac84a5e4f866d64120e70e55a27dbddc Mon Sep 17 00:00:00 2001 From: Willem Melching Date: Fri, 15 Mar 2024 16:01:46 +0100 Subject: [PATCH] wait between CF --- examples/isotp.rs | 4 +++- src/isotp/mod.rs | 14 +++++++------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/examples/isotp.rs b/examples/isotp.rs index fbe8ec4..5f43f9a 100644 --- a/examples/isotp.rs +++ b/examples/isotp.rs @@ -7,7 +7,9 @@ async fn main() { tracing_subscriber::fmt::init(); let adapter = automotive::adapter::get_adapter().unwrap(); - let config = IsoTPConfig::new(0, Identifier::Standard(0x7a1)); + let mut config = IsoTPConfig::new(0, Identifier::Standard(0x7a1)); + config.timeout = std::time::Duration::from_secs(1); + let isotp = IsoTPAdapter::new(&adapter, config); let response = isotp.recv(); diff --git a/src/isotp/mod.rs b/src/isotp/mod.rs index 53fee00..2f349db 100644 --- a/src/isotp/mod.rs +++ b/src/isotp/mod.rs @@ -29,6 +29,8 @@ use futures_core::stream::Stream; use tokio_stream::{StreamExt, Timeout}; use tracing::debug; +use self::types::FlowControlConfig; + const DEFAULT_TIMEOUT_MS: u64 = 100; /// Configuring passed to the IsoTPAdapter. @@ -146,7 +148,7 @@ impl<'a> IsoTPAdapter<'a> { self.adapter.send(&frame).await; } - fn receive_flow_control(&self, frame: &Frame) -> Result<(), Error> { + fn receive_flow_control(&self, frame: &Frame) -> Result { // Check if Flow Control if FrameType::from_repr(frame.data[0] & FRAME_TYPE_MASK) != Some(FrameType::FlowControl) { return Err(crate::isotp::error::Error::FlowControl.into()); @@ -162,10 +164,9 @@ impl<'a> IsoTPAdapter<'a> { // Parse block size and separation time let config = types::FlowControlConfig::try_from(frame)?; - println!("{:?}", config); - debug!("RX FC, data {}", hex::encode(&frame.data)); - Ok(()) + debug!("RX FC, {:?} data {}", config, hex::encode(&frame.data)); + Ok(config) } async fn send_multiple(&self, data: &[u8]) -> Result<(), Error> { @@ -178,7 +179,7 @@ impl<'a> IsoTPAdapter<'a> { self.send_first_frame(data).await; let frame = stream.next().await.unwrap()?; - self.receive_flow_control(&frame)?; + let config = self.receive_flow_control(&frame)?; debug!("RX FC, data {}", hex::encode(&frame.data)); println!("RX FC, data {}", hex::encode(&frame.data)); @@ -186,6 +187,7 @@ impl<'a> IsoTPAdapter<'a> { let chunks = data[self.config.tx_dl - 2..].chunks(self.config.tx_dl - 1); for (idx, chunk) in chunks.enumerate() { self.send_consecutive_frame(chunk, idx).await; + tokio::time::sleep(config.separation_time_min).await; } Ok(()) @@ -306,8 +308,6 @@ impl<'a> IsoTPAdapter<'a> { } }; - debug!("{} {}", len, buf.len()); - if buf.len() >= len { break; }