wifi: implement station change and connect

This commit is contained in:
2026-09-15 20:54:48 +02:00
committed by rhetenor
parent 47b539f364
commit ffc5899c33
4 changed files with 83 additions and 33 deletions
+6 -6
View File
@@ -60,7 +60,7 @@ pub struct CardStoreMetaData {
impl CardStoreMetaData {
fn new(creation_date: u64) -> Self {
Self {
creation_date: creation_date,
creation_date,
last_read_date: creation_date,
read_count: 0,
}
@@ -94,18 +94,18 @@ impl CardStore {
match nvs_lock.get::<bool>(&NVS_NAMESPACE_CARDSTORE, &NVS_CARDSTORE_INITIALIZED_KEY) {
Ok(initialized) => {
log::debug!("initialization_check: initialized {initialized:?}");
return Ok(true);
Ok(true)
}
Err(esp_nvs::error::Error::NamespaceNotFound) => {
log::debug!("initialization_check: error: namespace not found");
return Ok(false);
Ok(false)
}
Err(esp_nvs::error::Error::KeyNotFound) => {
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
+13 -9
View File
@@ -55,18 +55,18 @@ impl WifiStore {
match nvs_lock.get::<bool>(&NVS_NAMESPACE_WIFI, &NVS_WIFI_INITIALIZED_KEY) {
Ok(initialized) => {
log::debug!("initialization_check: initialized {initialized:?}");
return Ok(true);
Ok(true)
}
Err(esp_nvs::error::Error::NamespaceNotFound) => {
log::debug!("initialization_check: error: namespace not found");
return Ok(false);
Ok(false)
}
Err(esp_nvs::error::Error::KeyNotFound) => {
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> {
@@ -74,14 +74,14 @@ impl WifiStore {
let mut nvs_lock = nvs.lock().await;
log::debug!("initialization: write dummy ssid");
log::debug!("initialization: write empty ssid");
nvs_lock.set::<&str>(
&NVS_NAMESPACE_WIFI,
&NVS_WIFI_SSID_KEY,
""
)?;
log::debug!("initialization: write dummy pass");
log::debug!("initialization: write empty pass");
nvs_lock.set::<&str>(
&NVS_NAMESPACE_WIFI,
&NVS_WIFI_PASS_KEY,
@@ -99,18 +99,22 @@ impl WifiStore {
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 ssid");
let mut nvs_lock = self.nvs.lock().await;
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!("loading pass");
let pass = nvs_lock.get::<String>(&NVS_NAMESPACE_WIFI, &NVS_WIFI_PASS_KEY)?;
log::debug!("pass: {pass}");
Ok(WifiCredentials{ssid, pass})
Ok(Some(WifiCredentials{ssid, pass}))
}
pub async fn save_credentials(&self, credentials: &WifiCredentials) -> Result<(), WifiStoreError> {