diff --git a/src/board/wifi/ap_sta.rs b/src/board/wifi/ap_sta.rs index aaf0f9cb..bb82577f 100644 --- a/src/board/wifi/ap_sta.rs +++ b/src/board/wifi/ap_sta.rs @@ -1,6 +1,6 @@ use alloc::{rc::Rc, vec::Vec}; use embassy_sync::mutex::Mutex; -use embassy_time::Duration; +use embassy_time::{with_timeout, Duration}; use crate::{ board::{ @@ -15,7 +15,6 @@ use crate::{ }, }, task_control::{TaskControlToken, TaskController}, - timeout::Timeout, }; use embassy_executor::Spawner; use embassy_futures::{ @@ -160,11 +159,12 @@ async fn ap_sta_task( if timeout == NO_TIMEOUT { Some(resources.controller.wait_for_events(events, false).await) } else { - Timeout::with( + with_timeout( timeout, resources.controller.wait_for_events(events, false), ) .await + .ok() } }, sta_controller.wait_for_command(), diff --git a/src/board/wifi/sta.rs b/src/board/wifi/sta.rs index 98a1ee30..81b59a9e 100644 --- a/src/board/wifi/sta.rs +++ b/src/board/wifi/sta.rs @@ -7,7 +7,6 @@ use crate::{ wifi::{sta_net_task, StackWrapper}, }, task_control::{TaskControlToken, TaskController}, - timeout::Timeout, Shared, }; use alloc::{boxed::Box, rc::Rc, vec::Vec}; @@ -24,7 +23,7 @@ use embassy_sync::{ mutex::{Mutex, MutexGuard}, signal::Signal, }; -use embassy_time::Duration; +use embassy_time::{with_timeout, Duration}; use embedded_svc::wifi::{AccessPointInfo, ClientConfiguration, Configuration, Wifi as _}; use enumset::EnumSet; use esp_wifi::{ @@ -146,11 +145,11 @@ impl Sta { async { loop { let result = - Timeout::with(Duration::from_secs(10), self.wait_for_state_change()) + with_timeout(Duration::from_secs(10), self.wait_for_state_change()) .await; match result { - Some(WifiClientState::Connected) => break, - Some(_state) => {} + Ok(WifiClientState::Connected) => break, + Ok(_state) => {} _ => { debug!("State change timeout"); break; @@ -673,11 +672,12 @@ async fn sta_task( if timeout == NO_TIMEOUT { Some(resources.controller.wait_for_events(events, false).await) } else { - Timeout::with( + with_timeout( timeout, resources.controller.wait_for_events(events, false), ) .await + .ok() } }, sta_controller.wait_for_command(), diff --git a/src/states/firmware_update.rs b/src/states/firmware_update.rs index fe2c2c8b..50b7ea21 100644 --- a/src/states/firmware_update.rs +++ b/src/states/firmware_update.rs @@ -1,7 +1,7 @@ use core::cell::Cell; use embassy_futures::select::{select, Either}; -use embassy_time::{Duration, Instant, Timer}; +use embassy_time::{with_timeout, Duration, Instant, Timer}; use embedded_io_async::BufRead; use reqwless::{request::Method, response::Status}; use ufmt::uwrite; @@ -13,7 +13,6 @@ use crate::{ }, human_readable::{BinarySize, Throughput}, states::menu::AppMenu, - timeout::Timeout, AppState, SerialNumber, }; @@ -109,19 +108,18 @@ async fn do_update(context: &mut Context) -> UpdateResult { debug!("Looking for update at {}", url.as_str()); - let mut request = match Timeout::with(CONNECT_TIMEOUT, client.request(Method::GET, &url)).await - { - Some(Ok(request)) => request, - Some(Err(e)) => { + let mut request = match with_timeout(CONNECT_TIMEOUT, client.request(Method::GET, &url)).await { + Ok(Ok(request)) => request, + Ok(Err(e)) => { warn!("HTTP connect error: {:?}", e); return UpdateResult::Failed(UpdateError::HttpConnectionFailed); } - None => return UpdateResult::Failed(UpdateError::HttpConnectionTimeout), + Err(_) => return UpdateResult::Failed(UpdateError::HttpConnectionTimeout), }; let mut rx_buffer = [0; 4096]; - let result = match Timeout::with(READ_TIMEOUT, request.send(&mut rx_buffer)).await { - Some(result) => result, + let result = match with_timeout(READ_TIMEOUT, request.send(&mut rx_buffer)).await { + Ok(result) => result, _ => return UpdateResult::Failed(UpdateError::HttpRequestTimeout), }; @@ -165,8 +163,8 @@ async fn do_update(context: &mut Context) -> UpdateResult { let result = select( async { loop { - let received_buffer = match Timeout::with(READ_TIMEOUT, reader.fill_buf()).await { - Some(result) => match result { + let received_buffer = match with_timeout(READ_TIMEOUT, reader.fill_buf()).await { + Ok(result) => match result { Ok(&[]) => break None, Ok(read) => read, Err(e) => { diff --git a/src/states/throughput.rs b/src/states/throughput.rs index 364295e3..19a0cfe4 100644 --- a/src/states/throughput.rs +++ b/src/states/throughput.rs @@ -1,7 +1,7 @@ use core::cell::Cell; use embassy_futures::select::{select, Either}; -use embassy_time::{Duration, Instant, Timer}; +use embassy_time::{with_timeout, Duration, Instant, Timer}; use embedded_io_async::BufRead; use reqwless::{request::Method, response::Status}; use ufmt::{uwrite, uwriteln}; @@ -10,7 +10,6 @@ use crate::{ board::initialized::{Context, StaMode}, human_readable::{BinarySize, Throughput}, states::menu::AppMenu, - timeout::Timeout, AppState, SerialNumber, }; @@ -99,7 +98,7 @@ async fn run_test(context: &mut Context) -> TestResult { debug!("Testing throughput using {}", url.as_str()); - let connect = Timeout::with(CONNECT_TIMEOUT, async { + let connect = with_timeout(CONNECT_TIMEOUT, async { let futures = select(client.request(Method::GET, &url), async { loop { // A message is displayed for at least 300ms so we don't need to wait here. @@ -113,8 +112,8 @@ async fn run_test(context: &mut Context) -> TestResult { }); let mut request = match connect.await { - Some(Ok(request)) => request, - Some(Err(e)) => { + Ok(Ok(request)) => request, + Ok(Err(e)) => { warn!("HTTP connect error: {:?}", e); return TestResult::Failed(TestError::HttpConnectionFailed); } @@ -122,8 +121,8 @@ async fn run_test(context: &mut Context) -> TestResult { }; let mut rx_buffer = [0; 4096]; - let result = match Timeout::with(READ_TIMEOUT, request.send(&mut rx_buffer)).await { - Some(result) => result, + let result = match with_timeout(READ_TIMEOUT, request.send(&mut rx_buffer)).await { + Ok(result) => result, _ => return TestResult::Failed(TestError::HttpRequestTimeout), }; @@ -161,8 +160,8 @@ async fn run_test(context: &mut Context) -> TestResult { let result = select( async { loop { - match Timeout::with(READ_TIMEOUT, reader.fill_buf()).await { - Some(result) => match result { + match with_timeout(READ_TIMEOUT, reader.fill_buf()).await { + Ok(result) => match result { Ok(&[]) => break None, Ok(read) => { let read_len = read.len(); diff --git a/src/states/upload_or_store_measurement.rs b/src/states/upload_or_store_measurement.rs index 7d681e85..eb3b0d9f 100644 --- a/src/states/upload_or_store_measurement.rs +++ b/src/states/upload_or_store_measurement.rs @@ -4,7 +4,7 @@ use core::{ }; use alloc::{boxed::Box, vec::Vec}; -use embassy_time::Duration; +use embassy_time::{with_timeout, Duration}; use embedded_menu::items::NavigationItem; use embedded_nal_async::{Dns, TcpConnect}; use gui::screens::create_menu; @@ -27,7 +27,6 @@ use crate::{ }, human_readable::BinarySize, states::menu::{AppMenuBuilder, MenuScreen}, - timeout::Timeout, uformat, AppState, SerialNumber, }; @@ -436,9 +435,9 @@ where let headers = [("X-Timestamp", timestamp.as_str())]; let mut request = - match Timeout::with(CONNECT_TIMEOUT, client.request(Method::POST, &upload_url)).await { - Some(Ok(request)) => request.headers(&headers).body(samples), - Some(Err(e)) => { + match with_timeout(CONNECT_TIMEOUT, client.request(Method::POST, &upload_url)).await { + Ok(Ok(request)) => request.headers(&headers).body(samples), + Ok(Err(e)) => { warn!("HTTP connect error: {:?}", e); return Err(()); } @@ -449,8 +448,8 @@ where }; let mut rx_buffer = [0; 512]; - match Timeout::with(UPLOAD_TIMEOUT, request.send(&mut rx_buffer)).await { - Some(Ok(response)) => { + match with_timeout(UPLOAD_TIMEOUT, request.send(&mut rx_buffer)).await { + Ok(Ok(response)) => { if [Status::Ok, Status::Created].contains(&response.status) { return Ok(()); } @@ -466,7 +465,7 @@ where } } } - Some(Err(e)) => warn!("HTTP upload error: {:?}", e), + Ok(Err(e)) => warn!("HTTP upload error: {:?}", e), _ => warn!("Timeout"), } Err(()) diff --git a/src/timeout.rs b/src/timeout.rs index cea241ba..64914fa4 100644 --- a/src/timeout.rs +++ b/src/timeout.rs @@ -1,7 +1,4 @@ -use core::future::Future; - -use embassy_futures::select::{select, Either}; -use embassy_time::{Duration, Instant, Timer}; +use embassy_time::{Duration, Instant}; pub struct Timeout { start: Instant, @@ -32,11 +29,4 @@ impl Timeout { pub fn remaining(&self) -> Duration { self.duration - self.elapsed() } - - pub async fn with(duration: Duration, f: impl Future) -> Option { - match select(Timer::after(duration), f).await { - Either::First(_) => None, - Either::Second(result) => Some(result), - } - } }