From fc47e733b04bfa174a19cb5eb7932af0af894c3c Mon Sep 17 00:00:00 2001 From: Dilyn Corner Date: Mon, 21 Nov 2022 19:30:49 -0500 Subject: [PATCH 1/6] Create and use the ConnectionStatus enum --- cross/dns/src/main.rs | 9 +++++---- cross/join/src/main.rs | 11 +++++++---- esp32-wroom-rp/src/lib.rs | 4 +++- esp32-wroom-rp/src/protocol.rs | 4 +++- esp32-wroom-rp/src/spi.rs | 9 ++++++--- esp32-wroom-rp/src/wifi.rs | 17 +++++++++++++++++ 6 files changed, 41 insertions(+), 13 deletions(-) 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..a6f2895 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,8 @@ where let result = self.receive(&operation)?; - Ok(result[0]) + // TODO: + Ok(ConnectionStatus::Connected) } fn disconnect(&mut self) -> Result<(), Error> { diff --git a/esp32-wroom-rp/src/wifi.rs b/esp32-wroom-rp/src/wifi.rs index 19790de..8fd16df 100644 --- a/esp32-wroom-rp/src/wifi.rs +++ b/esp32-wroom-rp/src/wifi.rs @@ -1 +1,18 @@ pub use crate::spi::Wifi; + +/// Defines the WiFi network connection status +#[repr(u8)] +#[derive(Eq, PartialEq, PartialOrd, Debug)] +pub enum ConnectionStatus { + NoEsp32 = 255, + Idle = 0, // Assigned by WiFi.begin() in the Reference Library, is this relevant? + NoActiveSsid, + ScanCompleted, + Connected, + Failed, + Lost, + Disconnected, + ApListening, + ApConnected, + ApFailed, // Not in the Reference Library +} \ No newline at end of file From 48515ae3410746eb10a932f8094421c1961438b8 Mon Sep 17 00:00:00 2001 From: Dilyn Corner Date: Sat, 19 Nov 2022 18:03:52 -0500 Subject: [PATCH 2/6] Implement From trait for Connection Status --- esp32-wroom-rp/src/spi.rs | 3 +-- esp32-wroom-rp/src/wifi.rs | 21 ++++++++++++++++++++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/esp32-wroom-rp/src/spi.rs b/esp32-wroom-rp/src/spi.rs index a6f2895..744fc33 100644 --- a/esp32-wroom-rp/src/spi.rs +++ b/esp32-wroom-rp/src/spi.rs @@ -145,8 +145,7 @@ where let result = self.receive(&operation)?; - // TODO: - Ok(ConnectionStatus::Connected) + 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 8fd16df..64d4887 100644 --- a/esp32-wroom-rp/src/wifi.rs +++ b/esp32-wroom-rp/src/wifi.rs @@ -15,4 +15,23 @@ pub enum ConnectionStatus { ApListening, ApConnected, ApFailed, // Not in the Reference Library -} \ No newline at end of file +} + +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, + _ => panic!("Unexpected value: {}", status), + } + } +} From 854e49da6c7ba5398ef9358b41fe0f6d3b6e93f3 Mon Sep 17 00:00:00 2001 From: Dilyn Corner Date: Mon, 21 Nov 2022 19:07:10 -0500 Subject: [PATCH 3/6] Add catch-all error in ConnectionStatus enum instead of panicing when device returns unexpected value --- esp32-wroom-rp/src/wifi.rs | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/esp32-wroom-rp/src/wifi.rs b/esp32-wroom-rp/src/wifi.rs index 64d4887..6774379 100644 --- a/esp32-wroom-rp/src/wifi.rs +++ b/esp32-wroom-rp/src/wifi.rs @@ -15,23 +15,24 @@ pub enum ConnectionStatus { ApListening, ApConnected, ApFailed, // Not in the Reference Library + 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, + 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, - _ => panic!("Unexpected value: {}", status), + _ => ConnectionStatus::Invalid, } } } From 7ab8881177366098f6fe17cdafda4afb15b9c32c Mon Sep 17 00:00:00 2001 From: Dilyn Corner Date: Mon, 21 Nov 2022 19:09:43 -0500 Subject: [PATCH 4/6] Document variants in ConnectionStatus enum --- esp32-wroom-rp/src/wifi.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/esp32-wroom-rp/src/wifi.rs b/esp32-wroom-rp/src/wifi.rs index 6774379..d0d3c4c 100644 --- a/esp32-wroom-rp/src/wifi.rs +++ b/esp32-wroom-rp/src/wifi.rs @@ -1,20 +1,32 @@ pub use crate::spi::Wifi; -/// Defines the WiFi network connection status +/// 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, // Assigned by WiFi.begin() in the Reference Library, is this relevant? + /// 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, // Not in the Reference Library + /// Unexpected value returned from device, reset may be required Invalid, } From 1ed06ec0bb76e1799a9941079b11fcbe8d09ff17 Mon Sep 17 00:00:00 2001 From: Dilyn Corner Date: Tue, 22 Nov 2022 15:27:49 -0500 Subject: [PATCH 5/6] Remove comments on some variants in ConnectionStatus enum See PR#47, https://github.com/Jim-Hodapp-Coaching/esp32-wroom-rp/pull/47 --- esp32-wroom-rp/src/wifi.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/esp32-wroom-rp/src/wifi.rs b/esp32-wroom-rp/src/wifi.rs index d0d3c4c..2fa581f 100644 --- a/esp32-wroom-rp/src/wifi.rs +++ b/esp32-wroom-rp/src/wifi.rs @@ -7,7 +7,7 @@ pub enum ConnectionStatus { /// No device is connected to hardware NoEsp32 = 255, /// Temporary status while attempting to connect to WiFi network - Idle = 0, // Assigned by WiFi.begin() in the Reference Library, is this relevant? + Idle = 0, /// No SSID is available NoActiveSsid, /// WiFi network scan has finished @@ -25,7 +25,7 @@ pub enum ConnectionStatus { /// Device is connected in Access Point mode ApConnected, /// Device failed to make connection in Access Point mode - ApFailed, // Not in the Reference Library + ApFailed, /// Unexpected value returned from device, reset may be required Invalid, } From 39f0a2286a0b86579c838f3826238d2ea0629df0 Mon Sep 17 00:00:00 2001 From: Dilyn Corner Date: Tue, 22 Nov 2022 15:29:08 -0500 Subject: [PATCH 6/6] Implement basic Format trait for ConnectionStatus --- esp32-wroom-rp/src/wifi.rs | 56 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/esp32-wroom-rp/src/wifi.rs b/esp32-wroom-rp/src/wifi.rs index 2fa581f..488e09c 100644 --- a/esp32-wroom-rp/src/wifi.rs +++ b/esp32-wroom-rp/src/wifi.rs @@ -1,5 +1,7 @@ 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)] @@ -48,3 +50,57 @@ impl From for ConnectionStatus { } } } + +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" + ), + } + } +}