Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create and use a ConnectionStatus enum #47

Merged
merged 6 commits into from
Nov 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 5 additions & 4 deletions cross/dns/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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);
}
}
Expand Down
11 changes: 7 additions & 4 deletions cross/join/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#![no_main]

extern crate esp32_wroom_rp;
use esp32_wroom_rp::wifi::ConnectionStatus;

include!("../../secrets/secrets.rs");

Expand Down Expand Up @@ -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) => {
Expand Down
4 changes: 3 additions & 1 deletion esp32-wroom-rp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -210,7 +212,7 @@ where
self.protocol_handler.disconnect()
}

fn get_connection_status(&mut self) -> Result<u8, Error> {
fn get_connection_status(&mut self) -> Result<ConnectionStatus, Error> {
self.protocol_handler.get_conn_status()
}

Expand Down
4 changes: 3 additions & 1 deletion esp32-wroom-rp/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -231,7 +233,7 @@ pub(crate) trait ProtocolInterface {
fn get_fw_version(&mut self) -> Result<FirmwareVersion, Error>;
fn set_passphrase(&mut self, ssid: &str, passphrase: &str) -> Result<(), Error>;
fn disconnect(&mut self) -> Result<(), Error>;
fn get_conn_status(&mut self) -> Result<u8, Error>;
fn get_conn_status(&mut self) -> Result<ConnectionStatus, Error>;
fn set_dns_config(&mut self, dns1: IpAddress, dns2: Option<IpAddress>) -> Result<(), Error>;
fn req_host_by_name(&mut self, hostname: &str) -> Result<u8, Error>;
fn get_host_by_name(&mut self) -> Result<[u8; 8], Error>;
Expand Down
8 changes: 5 additions & 3 deletions esp32-wroom-rp/src/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 _;

Expand Down Expand Up @@ -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<u8, Error> {
pub fn get_connection_status(&mut self) -> Result<ConnectionStatus, Error> {
self.common.get_connection_status()
}

Expand Down Expand Up @@ -135,15 +137,15 @@ where
Ok(())
}

fn get_conn_status(&mut self) -> Result<u8, Error> {
fn get_conn_status(&mut self) -> Result<ConnectionStatus, Error> {
let operation =
Operation::new(NinaCommand::GetConnStatus, 1).with_no_params(NinaNoParams::new(""));

self.execute(&operation)?;

let result = self.receive(&operation)?;

Ok(result[0])
Ok(ConnectionStatus::from(result[0]))
}

fn disconnect(&mut self) -> Result<(), Error> {
Expand Down
105 changes: 105 additions & 0 deletions esp32-wroom-rp/src/wifi.rs
Original file line number Diff line number Diff line change
@@ -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<u8> 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 {
dilyn-corner marked this conversation as resolved.
Show resolved Hide resolved
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"
),
}
}
}