Skip to content

Commit

Permalink
Merge pull request #159 from card-io-ecg/timeout
Browse files Browse the repository at this point in the history
Add timeout to connection
  • Loading branch information
bugadani committed Feb 22, 2024
2 parents b8835f2 + 11a9f4a commit 61e5cad
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
12 changes: 8 additions & 4 deletions src/board/wifi/ap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,17 @@ impl ApController {

pub fn handle_events(&mut self, events: EnumSet<WifiEvent>) -> bool {
if events.contains(WifiEvent::ApStaconnected) {
let old_count = self.state.client_count.fetch_add(1, Ordering::Release);
info!("Client connected, {} total", old_count + 1);
let old_count = self.state.client_count.load(Ordering::Acquire);
let new_count = old_count.saturating_add(1);
self.state.client_count.store(new_count, Ordering::Relaxed);
info!("Client connected, {} total", new_count);
}

if events.contains(WifiEvent::ApStadisconnected) {
let old_count = self.state.client_count.fetch_sub(1, Ordering::Release);
info!("Client disconnected, {} left", old_count - 1);
let old_count = self.state.client_count.load(Ordering::Acquire);
let new_count = old_count.saturating_sub(1);
self.state.client_count.store(new_count, Ordering::Relaxed);
info!("Client disconnected, {} left", new_count);
}

if events.contains(WifiEvent::ApStop) {
Expand Down
10 changes: 7 additions & 3 deletions src/board/wifi/sta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -513,16 +513,20 @@ impl StaController {
controller: &mut WifiController<'_>,
) -> Result<(), ConnectError> {
self.state.update(InternalConnectionState::Connecting);
match controller.connect().await {
Ok(_) => {
match with_timeout(Duration::from_secs(30), controller.connect()).await {
Ok(Ok(_)) => {
self.state.update(InternalConnectionState::WaitingForIp);
Ok(())
}
Err(e) => {
Ok(Err(e)) => {
warn!("Failed to connect to wifi: {:?}", e);

Err(ConnectError)
}
Err(_) => {
warn!("Connection timeout");
Err(ConnectError)
}
}
}

Expand Down

0 comments on commit 61e5cad

Please sign in to comment.