diff --git a/cross/dns/src/main.rs b/cross/dns/src/main.rs index 0ea0f30..8246120 100644 --- a/cross/dns/src/main.rs +++ b/cross/dns/src/main.rs @@ -29,6 +29,7 @@ use hal::clocks::Clock; use hal::pac; use esp32_wroom_rp::network::IpAddress; +use esp32_wroom_rp::wifi::ConnectionStatus; /// The linker will place this boot block at the start of our program image. We /// need this to help the ROM bootloader get our code up and running. @@ -116,12 +117,12 @@ fn main() -> ! { loop { match wifi.get_connection_status() { - Ok(byte) => { - defmt::info!("Get Connection Result: {:?}", byte); + Ok(status) => { + defmt::info!("Get Connection Result: {:?}", status); let sleep: u32 = 1500; delay.delay_ms(sleep); - if byte == 3 { + if status == ConnectionStatus::Connected { defmt::info!("Connected to Network: {:?}", SSID); // The IPAddresses of two DNS servers to resolve hostnames with. @@ -146,7 +147,7 @@ fn main() -> ! { } wifi.leave().ok().unwrap(); - } else if byte == 6 { + } else if status == ConnectionStatus::Disconnected { defmt::info!("Disconnected from Network: {:?}", SSID); } } diff --git a/cross/join/src/main.rs b/cross/join/src/main.rs index 7ca0ee9..1c23f06 100644 --- a/cross/join/src/main.rs +++ b/cross/join/src/main.rs @@ -10,6 +10,7 @@ #![no_main] extern crate esp32_wroom_rp; +use esp32_wroom_rp::wifi::ConnectionStatus; include!("../../secrets/secrets.rs"); @@ -115,20 +116,22 @@ fn main() -> ! { loop { match wifi.get_connection_status() { - Ok(byte) => { - defmt::info!("Get Connection Result: {:?}", byte); + Ok(status) => { + defmt::info!("Get Connection Result: {:?}", status); let sleep: u32 = 1500; delay.delay_ms(sleep); - if byte == 3 { + if status == ConnectionStatus::Connected { defmt::info!("Connected to Network: {:?}", SSID); defmt::info!("Sleeping for 5 seconds before disconnecting..."); delay.delay_ms(5000); wifi.leave().ok().unwrap(); - } else if byte == 6 { + } else if status == ConnectionStatus::Disconnected { defmt::info!("Disconnected from Network: {:?}", SSID); + } else { + defmt::info!("Unhandled WiFi connection status: {:?}", status); } } Err(e) => { diff --git a/esp32-wroom-rp/src/lib.rs b/esp32-wroom-rp/src/lib.rs index 8179355..3d1f3a0 100644 --- a/esp32-wroom-rp/src/lib.rs +++ b/esp32-wroom-rp/src/lib.rs @@ -101,6 +101,8 @@ use protocol::{ProtocolError, ProtocolInterface}; use defmt::{write, Format, Formatter}; use embedded_hal::blocking::delay::DelayMs; +use self::wifi::ConnectionStatus; + const ARRAY_LENGTH_PLACEHOLDER: usize = 8; /// Highest level error types for this crate. @@ -210,7 +212,7 @@ where self.protocol_handler.disconnect() } - fn get_connection_status(&mut self) -> Result { + fn get_connection_status(&mut self) -> Result { self.protocol_handler.get_conn_status() } diff --git a/esp32-wroom-rp/src/protocol.rs b/esp32-wroom-rp/src/protocol.rs index 07f3c98..c0e0a45 100644 --- a/esp32-wroom-rp/src/protocol.rs +++ b/esp32-wroom-rp/src/protocol.rs @@ -8,6 +8,8 @@ use defmt::{write, Format, Formatter}; use heapless::{String, Vec}; +use super::wifi::ConnectionStatus; + pub(crate) const MAX_NINA_PARAM_LENGTH: usize = 255; #[repr(u8)] @@ -231,7 +233,7 @@ pub(crate) trait ProtocolInterface { fn get_fw_version(&mut self) -> Result; fn set_passphrase(&mut self, ssid: &str, passphrase: &str) -> Result<(), Error>; fn disconnect(&mut self) -> Result<(), Error>; - fn get_conn_status(&mut self) -> Result; + fn get_conn_status(&mut self) -> Result; fn set_dns_config(&mut self, dns1: IpAddress, dns2: Option) -> Result<(), Error>; fn req_host_by_name(&mut self, hostname: &str) -> Result; fn get_host_by_name(&mut self) -> Result<[u8; 8], Error>; diff --git a/esp32-wroom-rp/src/spi.rs b/esp32-wroom-rp/src/spi.rs index a67bfa9..744fc33 100644 --- a/esp32-wroom-rp/src/spi.rs +++ b/esp32-wroom-rp/src/spi.rs @@ -18,6 +18,8 @@ use embedded_hal::blocking::spi::Transfer; use core::convert::Infallible; +use super::wifi::ConnectionStatus; + // FIXME: remove before commit //use defmt_rtt as _; @@ -82,7 +84,7 @@ where /// /// NOTE: A future version will provide a enumerated type instead of the raw integer values /// from the NINA firmware. - pub fn get_connection_status(&mut self) -> Result { + pub fn get_connection_status(&mut self) -> Result { self.common.get_connection_status() } @@ -135,7 +137,7 @@ where Ok(()) } - fn get_conn_status(&mut self) -> Result { + fn get_conn_status(&mut self) -> Result { let operation = Operation::new(NinaCommand::GetConnStatus, 1).with_no_params(NinaNoParams::new("")); @@ -143,7 +145,7 @@ where let result = self.receive(&operation)?; - Ok(result[0]) + Ok(ConnectionStatus::from(result[0])) } fn disconnect(&mut self) -> Result<(), Error> { diff --git a/esp32-wroom-rp/src/wifi.rs b/esp32-wroom-rp/src/wifi.rs index 19790de..488e09c 100644 --- a/esp32-wroom-rp/src/wifi.rs +++ b/esp32-wroom-rp/src/wifi.rs @@ -1 +1,106 @@ pub use crate::spi::Wifi; + +use defmt::{write, Format, Formatter}; + +/// An enumerated type that represents the current WiFi network connection status. +#[repr(u8)] +#[derive(Eq, PartialEq, PartialOrd, Debug)] +pub enum ConnectionStatus { + /// No device is connected to hardware + NoEsp32 = 255, + /// Temporary status while attempting to connect to WiFi network + Idle = 0, + /// No SSID is available + NoActiveSsid, + /// WiFi network scan has finished + ScanCompleted, + /// Device is connected to WiFi network + Connected, + /// Device failed to connect to WiFi network + Failed, + /// Device lost connection to WiFi network + Lost, + /// Device disconnected from WiFi network + Disconnected, + /// Device is listening for connections in Access Point mode + ApListening, + /// Device is connected in Access Point mode + ApConnected, + /// Device failed to make connection in Access Point mode + ApFailed, + /// Unexpected value returned from device, reset may be required + Invalid, +} + +impl From for ConnectionStatus { + fn from(status: u8) -> ConnectionStatus { + match status { + 0 => ConnectionStatus::Idle, + 1 => ConnectionStatus::NoActiveSsid, + 2 => ConnectionStatus::ScanCompleted, + 3 => ConnectionStatus::Connected, + 4 => ConnectionStatus::Failed, + 5 => ConnectionStatus::Lost, + 6 => ConnectionStatus::Disconnected, + 7 => ConnectionStatus::ApListening, + 8 => ConnectionStatus::ApConnected, + 9 => ConnectionStatus::ApFailed, + 255 => ConnectionStatus::NoEsp32, + _ => ConnectionStatus::Invalid, + } + } +} + +impl Format for ConnectionStatus { + fn format(&self, fmt: Formatter) { + match self { + ConnectionStatus::NoEsp32 => write!( + fmt,"No device is connected to hardware" + ), + ConnectionStatus::Idle => write!( + fmt, + "Temporary status while attempting to connect to WiFi network" + ), + ConnectionStatus::NoActiveSsid => write!( + fmt, + "No SSID is available" + ), + ConnectionStatus::ScanCompleted => write!( + fmt, + "WiFi network scan has finished" + ), + ConnectionStatus::Connected => write!( + fmt, + "Device is connected to WiFi network" + ), + ConnectionStatus::Failed => write!( + fmt, + "Device failed to connect to WiFi network" + ), + ConnectionStatus::Lost => write!( + fmt, + "Device lost connection to WiFi network" + ), + ConnectionStatus::Disconnected => write!( + fmt, + "Device disconnected from WiFi network" + ), + ConnectionStatus::ApListening => write!( + fmt, + "Device is lstening for connections in Access Point mode" + ), + ConnectionStatus::ApConnected => write!( + fmt, + "Device is connected in Access Point mode" + ), + ConnectionStatus::ApFailed => write!( + fmt, + "Device failed to make connection in Access Point mode" + ), + ConnectionStatus::Invalid => write!( + fmt, + "Unexpected value returned from device, reset may be required" + ), + } + } +}