Skip to content

Commit

Permalink
Cleaned up error/bail conditions in wifi driver start function
Browse files Browse the repository at this point in the history
  • Loading branch information
arosspope committed Oct 8, 2023
1 parent 08b75c7 commit 53e1971
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fn main() -> Result<()> {
// Associate to network and obtain DHCP IP
info!("Loading with credentials, ssid:{:?} psk:{:?}", app_config.wifi_ssid, app_config.wifi_psk);
let mut wifi = Wifi::init(peripherals.modem, &app_config.wifi_ssid, &app_config.wifi_psk);
Wifi::start(&mut wifi);
Wifi::start(&mut wifi)?;
let ip = wifi.sta_netif().get_ip_info().unwrap();
info!("IP info: {:?}", ip);

Expand Down
23 changes: 11 additions & 12 deletions src/wifi.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use anyhow::{Result, Error, bail};
use embedded_svc::wifi::{AuthMethod, ClientConfiguration, Configuration, Wifi as SvcWifi};
use esp_idf_hal::modem::Modem;
use esp_idf_svc::{eventloop::EspSystemEventLoop, nvs::EspDefaultNvsPartition, wifi::EspWifi};
Expand Down Expand Up @@ -26,33 +27,31 @@ impl Wifi {
wifi_driver
}

pub fn start(wifi_driver: &mut EspWifi<'_>) {// TODO: Make it a result type -> Result<()> {
wifi_driver.start().expect("Failed to start wifi driver");
wifi_driver.is_started().expect("Failed to start wifi driver");
wifi_driver.connect().expect("Failed to initiate connection");
pub fn start(wifi_driver: &mut EspWifi<'_>) -> Result<(), Error> {
wifi_driver.start()?;
wifi_driver.is_started()?;
wifi_driver.connect()?;

for _ in 0..10 {
if wifi_driver.is_connected().unwrap() {
if wifi_driver.is_connected()? {
break;
}

std::thread::sleep(std::time::Duration::from_secs(1));
}

if !wifi_driver.is_connected().unwrap() {
return; // TODO: Error
if !wifi_driver.is_connected()? {
bail!("Failed to connect to BSS");
}

for _ in 0..10 {
if wifi_driver.sta_netif().is_up().unwrap() {
// Ok(())
break;
if wifi_driver.sta_netif().is_up()? {
return Ok(());
}

std::thread::sleep(std::time::Duration::from_secs(1));
}


// Ok(()) // TODO: Err
bail!("Failed to bring up network interface");
}
}

0 comments on commit 53e1971

Please sign in to comment.