fix timeout handling

was accumulative by mistake, now it's flat incremental
This commit is contained in:
Vladan Popovic 2022-06-20 16:57:23 +02:00
parent 172bdead14
commit a408544e4e

View file

@ -33,12 +33,12 @@ impl std::fmt::Display for ModemError {
pub struct IterableRx<UART: serial::Uart> {
inner: Rx<UART>,
timeout: Option<Duration>,
timeout: Duration,
}
impl<UART: serial::Uart> IterableRx<UART> {
fn reset(&mut self, timeout: Duration) -> &mut Self {
self.timeout = Some(timeout);
self.timeout = timeout;
self
}
}
@ -50,19 +50,19 @@ impl<UART: serial::Uart> Iterator for IterableRx<UART> {
/// interest, so the retry mechanism is triggered on _any_ error every 200ms until a byte is
/// received, or the timeout is reached.
fn next(&mut self) -> Option<Self::Item> {
let now = Instant::now();
let start = Instant::now();
loop {
let timeout = self.timeout.unwrap_or(Duration::from_millis(0));
match self.inner.read() {
Ok(b) => {
self.timeout = self.timeout.saturating_sub(start.elapsed());
break Some(b)
},
_ => {
if now.elapsed() > timeout {
Err(_) => {
if start.elapsed() > self.timeout {
self.timeout = Duration::from_millis(0);
break None
}
thread::sleep(Duration::from_millis(200));
self.timeout = Some(timeout.saturating_sub(now.elapsed()));
}
}
}
@ -72,7 +72,7 @@ impl<UART: serial::Uart> Iterator for IterableRx<UART> {
impl<UART: serial::Uart> Modem<UART> {
pub fn new(tx: Tx<UART>, rx: Rx<UART>) -> Self {
Self {
rx: IterableRx { inner: rx, timeout: None },
rx: IterableRx { inner: rx, timeout: Duration::from_millis(0) },
tx,
}
}