From a263809689bbe7f51e03bafccb9134a3ea036c4e Mon Sep 17 00:00:00 2001 From: Florian Guggi Date: Fri, 3 May 2024 18:03:29 +0200 Subject: [PATCH] Fix resetting of timeout when ACK is not received This was uncovered by the cli tools lack of ACK response --- examples/cli.rs | 2 ++ src/communication/mod.rs | 5 +++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/examples/cli.rs b/examples/cli.rs index 9523cf6..6c6c3f5 100644 --- a/examples/cli.rs +++ b/examples/cli.rs @@ -20,6 +20,7 @@ fn main() { loop { inquire_and_send_command(&mut serial, &scheduler_path).unwrap(); println!("------------------------"); + std::thread::sleep(Duration::from_millis(100)); } } @@ -130,6 +131,7 @@ fn inquire_and_send_command(edu: &mut impl CommunicationHandle, path: &str) -> R match edu.receive_multi_packet() { Ok(data) => { std::fs::write(result_path, data)?; + edu.send_packet(&CEPPacket::Ack)?; println!("Wrote result to file"); }, Err(e) => println!("Received {:?}", e), diff --git a/src/communication/mod.rs b/src/communication/mod.rs index 944d81a..231cde2 100644 --- a/src/communication/mod.rs +++ b/src/communication/mod.rs @@ -115,12 +115,13 @@ pub trait CommunicationHandle: Read + Write { /// Try to receive an ACK packet with a given `timeout`. Resets the timeout to Duration::MAX afterwards fn await_ack(&mut self, timeout: Duration) -> ComResult<()> { self.set_timeout(timeout); - let ret = match self.receive_packet()? { + let result = self.receive_packet(); + self.set_timeout(Self::UNLIMITED_TIMEOUT); + let ret = match result? { CEPPacket::Ack => Ok(()), CEPPacket::Nack => Err(CommunicationError::NotAcknowledged), _ => Err(CommunicationError::PacketInvalidError), }; - self.set_timeout(Self::UNLIMITED_TIMEOUT); ret } }