Skip to content

Commit

Permalink
Remove custom with_timeout impl
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani committed Nov 10, 2023
1 parent 3bdac91 commit 9e2e99d
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 48 deletions.
6 changes: 3 additions & 3 deletions src/board/wifi/ap_sta.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand All @@ -15,7 +15,6 @@ use crate::{
},
},
task_control::{TaskControlToken, TaskController},
timeout::Timeout,
};
use embassy_executor::Spawner;
use embassy_futures::{
Expand Down Expand Up @@ -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(),
Expand Down
12 changes: 6 additions & 6 deletions src/board/wifi/sta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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::{
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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(),
Expand Down
20 changes: 9 additions & 11 deletions src/states/firmware_update.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -13,7 +13,6 @@ use crate::{
},
human_readable::{BinarySize, Throughput},
states::menu::AppMenu,
timeout::Timeout,
AppState, SerialNumber,
};

Expand Down Expand Up @@ -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),
};

Expand Down Expand Up @@ -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) => {
Expand Down
17 changes: 8 additions & 9 deletions src/states/throughput.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -10,7 +10,6 @@ use crate::{
board::initialized::{Context, StaMode},
human_readable::{BinarySize, Throughput},
states::menu::AppMenu,
timeout::Timeout,
AppState, SerialNumber,
};

Expand Down Expand Up @@ -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.
Expand All @@ -113,17 +112,17 @@ 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);
}
_ => return TestResult::Failed(TestError::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 TestResult::Failed(TestError::HttpRequestTimeout),
};

Expand Down Expand Up @@ -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();
Expand Down
15 changes: 7 additions & 8 deletions src/states/upload_or_store_measurement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,7 +27,6 @@ use crate::{
},
human_readable::BinarySize,
states::menu::{AppMenuBuilder, MenuScreen},
timeout::Timeout,
uformat, AppState, SerialNumber,
};

Expand Down Expand Up @@ -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(());
}
Expand All @@ -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(());
}
Expand All @@ -466,7 +465,7 @@ where
}
}
}
Some(Err(e)) => warn!("HTTP upload error: {:?}", e),
Ok(Err(e)) => warn!("HTTP upload error: {:?}", e),
_ => warn!("Timeout"),
}
Err(())
Expand Down
12 changes: 1 addition & 11 deletions src/timeout.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -32,11 +29,4 @@ impl Timeout {
pub fn remaining(&self) -> Duration {
self.duration - self.elapsed()
}

pub async fn with<R>(duration: Duration, f: impl Future<Output = R>) -> Option<R> {
match select(Timer::after(duration), f).await {
Either::First(_) => None,
Either::Second(result) => Some(result),
}
}
}

0 comments on commit 9e2e99d

Please sign in to comment.