Own wifi #65
+1
-3
@@ -130,10 +130,8 @@ async fn main(spawner: Spawner) {
|
|||||||
));
|
));
|
||||||
|
|
||||||
let wifi_store = WifiStore::new(nvs.clone()).await.expect("Could not initialize WiFi store");
|
let wifi_store = WifiStore::new(nvs.clone()).await.expect("Could not initialize WiFi store");
|
||||||
|
|
||||||
let init_credentials = Wifi::initialize_credentials(&wifi_store).await.expect("Could not get init WiFi credentials");
|
let init_credentials = Wifi::initialize_credentials(&wifi_store).await.expect("Could not get init WiFi credentials");
|
||||||
|
let wifi = Wifi::new(esp_peripherals.WIFI, init_credentials).await.expect("Could not initialize WiFi");
|
||||||
let wifi = Wifi::new(esp_peripherals.WIFI, init_credentials).expect("Could not initialize WiFi");
|
|
||||||
|
|
||||||
let card_store = CardStore::new(nvs.clone())
|
let card_store = CardStore::new(nvs.clone())
|
||||||
.await
|
.await
|
||||||
|
|||||||
+63
-15
@@ -1,4 +1,6 @@
|
|||||||
use crate::Mutex;
|
use crate::Mutex;
|
||||||
|
use esp_radio::wifi::sta::StationConfig;
|
||||||
|
use esp_radio::wifi::Config;
|
||||||
use crate::alloc::string::ToString;
|
use crate::alloc::string::ToString;
|
||||||
use crate::Signal;
|
use crate::Signal;
|
||||||
use crate::navigation::outputs::Outputs;
|
use crate::navigation::outputs::Outputs;
|
||||||
@@ -23,9 +25,8 @@ pub static WIFI_CREDENTIALS: Signal<WifiCredentials> = Signal::new();
|
|||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Wifi {
|
pub struct Wifi {
|
||||||
pub controller: WifiController<'static>,
|
controller: WifiController<'static>,
|
||||||
pub interfaces: wifi::Interfaces<'static>,
|
interfaces: wifi::Interfaces<'static>,
|
||||||
pub credentials: WifiCredentials,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@@ -60,30 +61,65 @@ impl From<esp_radio::wifi::WifiError> for WifiError {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Wifi {
|
impl Wifi {
|
||||||
pub fn new(
|
pub async fn new(
|
||||||
wifi: esp_hal::peripherals::WIFI<'static>,
|
wifi: esp_hal::peripherals::WIFI<'static>,
|
||||||
credentials: WifiCredentials
|
credentials: Option<WifiCredentials>
|
||||||
) -> Result<Self, WifiError> {
|
) -> Result<Self, WifiError> {
|
||||||
let wifi = wifi::new(wifi, Default::default())?;
|
let wifi = wifi::new(wifi, Default::default())?;
|
||||||
|
|
||||||
Ok(Self {
|
let mut instance = Self {
|
||||||
controller: wifi.0,
|
controller: wifi.0,
|
||||||
interfaces: wifi.1,
|
interfaces: wifi.1
|
||||||
credentials
|
};
|
||||||
})
|
|
||||||
|
if let Some(creds) = credentials {
|
||||||
|
instance.set_station(creds).await?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(instance)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// get wifi credentials from env WIFI_SSID; WIFI_PASS (priority) or storage
|
||||||
|
|
|||||||
pub async fn initialize_credentials(
|
pub async fn initialize_credentials(
|
||||||
wifi_store: &WifiStore,
|
wifi_store: &WifiStore,
|
||||||
) -> Result<WifiCredentials, WifiError> {
|
) -> Result<Option<WifiCredentials>, WifiError> {
|
||||||
let stored_credentials = wifi_store.load_credentials().await?;
|
let option_stored_credentials = wifi_store.load_credentials().await?;
|
||||||
let ssid = option_env!("WIFI_SSID").map_or(stored_credentials.ssid, |v| v.to_string());
|
|
||||||
let pass = option_env!("WIFI_PASS").map_or(stored_credentials.pass, |v| v.to_string());
|
let env_ssid = option_env!("WIFI_SSID");
|
||||||
|
let env_pass = option_env!("WIFI_PASS");
|
||||||
|
|
||||||
|
if env_ssid.is_none() && option_stored_credentials.is_none() {
|
||||||
|
ede1998
commented
This prevents passwordless wifi networks from working ;D This prevents passwordless wifi networks from working ;D
|
|||||||
|
log::info!("no initial wifi credentials");
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
|
|
||||||
|
let stored_credentials = option_stored_credentials.expect("checked value before");
|
||||||
|
|
||||||
|
let ssid = env_ssid.map_or(stored_credentials.ssid, |v| v.to_string());
|
||||||
|
let pass = env_pass.map_or(stored_credentials.pass, |v| v.to_string());
|
||||||
|
|
||||||
log::info!("initial wifi credentials:\nssid:{ssid}, pass:{pass}");
|
log::info!("initial wifi credentials:\nssid:{ssid}, pass:{pass}");
|
||||||
Ok(WifiCredentials {ssid, pass})
|
|
||||||
|
Ok(Some(WifiCredentials {ssid, pass}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn connect(&mut self) -> Result<(), WifiError>{
|
||||||
|
log::info!("connecting wifi...");
|
||||||
|
|
||||||
|
let stationinfo = self.controller.connect_async().await?;
|
||||||
|
|
||||||
|
log::info!("connected to {}", stationinfo.ssid.as_str());
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn set_station(&mut self, credentials: WifiCredentials) -> Result<(), WifiError> {
|
||||||
|
let station_config = StationConfig::default().with_ssid(credentials.ssid).with_password(credentials.pass);
|
||||||
|
self.controller.set_config(&Config::Station(station_config))?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
#[cfg(feature = "wokwi")]
|
#[cfg(feature = "wokwi")]
|
||||||
pub async fn setup(
|
pub async fn setup(
|
||||||
wifi: esp_hal::peripherals::WIFI<'static>,
|
wifi: esp_hal::peripherals::WIFI<'static>,
|
||||||
@@ -123,9 +159,10 @@ impl Wifi {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#[embassy_executor::task]
|
#[embassy_executor::task]
|
||||||
pub async fn wait_for_wifi_credentials(_outputs: &'static Mutex<Outputs>, peripherals: &'static Peripherals) {
|
pub async fn wait_for_wifi_credentials(_outputs: &'static Mutex<Outputs>, peripherals: &'static Peripherals) {
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
log::info!("Waiting for new WiFi credentials");
|
log::info!("Waiting for new WiFi credentials");
|
||||||
let credentials = WIFI_CREDENTIALS.wait().await;
|
let credentials = WIFI_CREDENTIALS.wait().await;
|
||||||
@@ -138,6 +175,17 @@ pub async fn wait_for_wifi_credentials(_outputs: &'static Mutex<Outputs>, periph
|
|||||||
|
|
||||||
log::info!("Saved new WiFi credentials. Restarting WiFi");
|
log::info!("Saved new WiFi credentials. Restarting WiFi");
|
||||||
|
|
||||||
|
let mut wifi = peripherals.wifi.lock().await;
|
||||||
|
|
||||||
|
if let Err(err) = wifi.set_station(credentials).await {
|
||||||
|
log::error!("Error setting new wifi station: {err}");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Err(err) = wifi.connect().await {
|
||||||
|
log::error!("Error connecting to wifi: {err}");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ pub struct CardStoreMetaData {
|
|||||||
impl CardStoreMetaData {
|
impl CardStoreMetaData {
|
||||||
fn new(creation_date: u64) -> Self {
|
fn new(creation_date: u64) -> Self {
|
||||||
Self {
|
Self {
|
||||||
creation_date: creation_date,
|
creation_date,
|
||||||
last_read_date: creation_date,
|
last_read_date: creation_date,
|
||||||
read_count: 0,
|
read_count: 0,
|
||||||
}
|
}
|
||||||
@@ -94,18 +94,18 @@ impl CardStore {
|
|||||||
match nvs_lock.get::<bool>(&NVS_NAMESPACE_CARDSTORE, &NVS_CARDSTORE_INITIALIZED_KEY) {
|
match nvs_lock.get::<bool>(&NVS_NAMESPACE_CARDSTORE, &NVS_CARDSTORE_INITIALIZED_KEY) {
|
||||||
Ok(initialized) => {
|
Ok(initialized) => {
|
||||||
log::debug!("initialization_check: initialized {initialized:?}");
|
log::debug!("initialization_check: initialized {initialized:?}");
|
||||||
return Ok(true);
|
Ok(true)
|
||||||
}
|
}
|
||||||
Err(esp_nvs::error::Error::NamespaceNotFound) => {
|
Err(esp_nvs::error::Error::NamespaceNotFound) => {
|
||||||
log::debug!("initialization_check: error: namespace not found");
|
log::debug!("initialization_check: error: namespace not found");
|
||||||
return Ok(false);
|
Ok(false)
|
||||||
}
|
}
|
||||||
Err(esp_nvs::error::Error::KeyNotFound) => {
|
Err(esp_nvs::error::Error::KeyNotFound) => {
|
||||||
log::debug!("initialization_check: error: key not found");
|
log::debug!("initialization_check: error: key not found");
|
||||||
return Ok(false);
|
Ok(false)
|
||||||
}
|
}
|
||||||
Err(error) => return Err(error.into()),
|
Err(error) => Err(error.into()),
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// initializing cardstore with 0 cards, so that all namespaces exist and there
|
// initializing cardstore with 0 cards, so that all namespaces exist and there
|
||||||
|
|||||||
@@ -55,18 +55,18 @@ impl WifiStore {
|
|||||||
match nvs_lock.get::<bool>(&NVS_NAMESPACE_WIFI, &NVS_WIFI_INITIALIZED_KEY) {
|
match nvs_lock.get::<bool>(&NVS_NAMESPACE_WIFI, &NVS_WIFI_INITIALIZED_KEY) {
|
||||||
Ok(initialized) => {
|
Ok(initialized) => {
|
||||||
log::debug!("initialization_check: initialized {initialized:?}");
|
log::debug!("initialization_check: initialized {initialized:?}");
|
||||||
return Ok(true);
|
Ok(true)
|
||||||
}
|
}
|
||||||
Err(esp_nvs::error::Error::NamespaceNotFound) => {
|
Err(esp_nvs::error::Error::NamespaceNotFound) => {
|
||||||
log::debug!("initialization_check: error: namespace not found");
|
log::debug!("initialization_check: error: namespace not found");
|
||||||
return Ok(false);
|
Ok(false)
|
||||||
}
|
}
|
||||||
Err(esp_nvs::error::Error::KeyNotFound) => {
|
Err(esp_nvs::error::Error::KeyNotFound) => {
|
||||||
log::debug!("initialization_check: error: key not found");
|
log::debug!("initialization_check: error: key not found");
|
||||||
return Ok(false);
|
Ok(false)
|
||||||
}
|
}
|
||||||
Err(error) => return Err(error.into()),
|
Err(error) => Err(error.into()),
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn initialize(nvs: &Mutex<Nvs>) -> Result<(), WifiStoreError> {
|
async fn initialize(nvs: &Mutex<Nvs>) -> Result<(), WifiStoreError> {
|
||||||
@@ -74,14 +74,14 @@ impl WifiStore {
|
|||||||
|
|
||||||
let mut nvs_lock = nvs.lock().await;
|
let mut nvs_lock = nvs.lock().await;
|
||||||
|
|
||||||
log::debug!("initialization: write dummy ssid");
|
log::debug!("initialization: write empty ssid");
|
||||||
nvs_lock.set::<&str>(
|
nvs_lock.set::<&str>(
|
||||||
&NVS_NAMESPACE_WIFI,
|
&NVS_NAMESPACE_WIFI,
|
||||||
&NVS_WIFI_SSID_KEY,
|
&NVS_WIFI_SSID_KEY,
|
||||||
""
|
""
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
log::debug!("initialization: write dummy pass");
|
log::debug!("initialization: write empty pass");
|
||||||
nvs_lock.set::<&str>(
|
nvs_lock.set::<&str>(
|
||||||
&NVS_NAMESPACE_WIFI,
|
&NVS_NAMESPACE_WIFI,
|
||||||
&NVS_WIFI_PASS_KEY,
|
&NVS_WIFI_PASS_KEY,
|
||||||
@@ -99,18 +99,22 @@ impl WifiStore {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn load_credentials(&self) -> Result<WifiCredentials, WifiStoreError> {
|
pub async fn load_credentials(&self) -> Result<Option<WifiCredentials>, WifiStoreError> {
|
||||||
log::debug!("loading wifi credentials");
|
log::debug!("loading wifi credentials");
|
||||||
log::debug!("loading ssid");
|
log::debug!("loading ssid");
|
||||||
let mut nvs_lock = self.nvs.lock().await;
|
let mut nvs_lock = self.nvs.lock().await;
|
||||||
let ssid = nvs_lock.get::<String>(&NVS_NAMESPACE_WIFI, &NVS_WIFI_SSID_KEY)?;
|
let ssid = nvs_lock.get::<String>(&NVS_NAMESPACE_WIFI, &NVS_WIFI_SSID_KEY)?;
|
||||||
|
if ssid.is_empty() {
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
log::debug!("ssid: {ssid}");
|
log::debug!("ssid: {ssid}");
|
||||||
|
|
||||||
log::debug!("loading pass");
|
log::debug!("loading pass");
|
||||||
let pass = nvs_lock.get::<String>(&NVS_NAMESPACE_WIFI, &NVS_WIFI_PASS_KEY)?;
|
let pass = nvs_lock.get::<String>(&NVS_NAMESPACE_WIFI, &NVS_WIFI_PASS_KEY)?;
|
||||||
|
|
||||||
log::debug!("pass: {pass}");
|
log::debug!("pass: {pass}");
|
||||||
|
|
||||||
Ok(WifiCredentials{ssid, pass})
|
Ok(Some(WifiCredentials{ssid, pass}))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn save_credentials(&self, credentials: &WifiCredentials) -> Result<(), WifiStoreError> {
|
pub async fn save_credentials(&self, credentials: &WifiCredentials) -> Result<(), WifiStoreError> {
|
||||||
|
|||||||
Reference in New Issue
Block a user
Nit: triple
/(///) for doc comments