use own wifi implementation to connect to wifi. store credentials in flash. no wifi connection yet
This commit is contained in:
@@ -5,3 +5,4 @@ pub type Nvs = esp_nvs::Nvs<FlashStorage<'static>>;
|
||||
pub mod cardstore;
|
||||
pub mod settings;
|
||||
pub mod store;
|
||||
pub mod wifistore;
|
||||
|
||||
@@ -1,112 +0,0 @@
|
||||
use core::error::Error;
|
||||
use core::fmt::Display;
|
||||
|
||||
use crate::navigation::navigation::Mutex;
|
||||
use embedded_storage::nor_flash::NorFlash;
|
||||
use embedded_storage::nor_flash::ReadNorFlash;
|
||||
use esp_storage::{FlashStorage, FlashStorageError};
|
||||
|
||||
use crate::peripherals::storage::MAGIC_REGION;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct FlashStoreError(FlashStorageError);
|
||||
|
||||
impl From<FlashStorageError> for FlashStoreError {
|
||||
fn from(value: FlashStorageError) -> Self {
|
||||
Self(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl Error for FlashStoreError {}
|
||||
|
||||
impl Display for FlashStoreError {
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||
write!(f, "FlashStoreError:\n{:?}", self.0)
|
||||
}
|
||||
}
|
||||
|
||||
const FLASH_ADDR: u32 = 0x200000;
|
||||
|
||||
const INITIALIZATION_SIZE: usize = 0x1000;
|
||||
const FLASH_INITIALIZE_MAGIC: u32 = 0xde6de6de;
|
||||
|
||||
pub struct FlashStore {
|
||||
pub storage: Mutex<FlashStorage<'static>>,
|
||||
}
|
||||
|
||||
impl FlashStore {
|
||||
pub fn new(flash: esp_hal::peripherals::FLASH<'static>) -> Result<Self, FlashStoreError> {
|
||||
let mut storage = FlashStorage::new(flash);
|
||||
|
||||
let mut magic: [u8; 4] = [0, 0, 0, 0];
|
||||
storage.read(FLASH_ADDR + MAGIC_REGION.offset, &mut magic)?;
|
||||
log::debug!("Magic bytes are {magic:?}");
|
||||
|
||||
if Self::has_magic_bytes(&mut storage)? {
|
||||
log::info!("Skipped flash initialization, magic bytes already present.");
|
||||
} else {
|
||||
log::info!("Initializing flash because no magic bytes found.");
|
||||
Self::initialize_flash(&mut storage)?;
|
||||
}
|
||||
|
||||
Ok(Self {
|
||||
storage: Mutex::new(storage),
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn read(&self, offset: u32, bytes: &mut [u8]) -> Result<(), FlashStoreError> {
|
||||
self.storage.lock().await.read(FLASH_ADDR + offset, bytes)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn write(&self, offset: u32, bytes: &mut [u8]) -> Result<(), FlashStoreError> {
|
||||
log::debug!("Writing {} bytes at offset 0x{offset:08x}", bytes.len());
|
||||
self.storage
|
||||
.lock()
|
||||
.await
|
||||
.write(FLASH_ADDR + offset, bytes)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn write_erase(&self, offset: u32, bytes: &mut [u8]) -> Result<(), FlashStoreError> {
|
||||
log::debug!(
|
||||
"Erasing and Writing {} bytes at offset 0x{offset:08x}",
|
||||
bytes.len()
|
||||
);
|
||||
let mut lock = self.storage.lock().await;
|
||||
let addr = FLASH_ADDR + offset;
|
||||
let to = FLASH_ADDR + offset + bytes.len() as u32;
|
||||
log::debug!("Erasing from 0x{addr:x} to 0x{to:x}");
|
||||
//lock.erase(addr, to)?;
|
||||
|
||||
log::debug!("Writing bytes...");
|
||||
lock.write(addr, bytes)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn has_magic_bytes(storage: &mut FlashStorage) -> Result<bool, FlashStoreError> {
|
||||
let mut magic: [u8; 4] = [0, 0, 0, 0];
|
||||
storage.read(FLASH_ADDR + MAGIC_REGION.offset, &mut magic)?;
|
||||
log::debug!("Magic bytes are {magic:?}");
|
||||
|
||||
Ok(u32::from_ne_bytes(magic) == FLASH_INITIALIZE_MAGIC)
|
||||
}
|
||||
|
||||
pub fn initialize_flash(storage: &mut FlashStorage) -> Result<(), FlashStoreError> {
|
||||
storage.erase(FLASH_ADDR, FLASH_ADDR + INITIALIZATION_SIZE as u32)?;
|
||||
let zeros: [u8; INITIALIZATION_SIZE] = [0; INITIALIZATION_SIZE];
|
||||
storage.write(FLASH_ADDR + MAGIC_REGION.end() as u32, &zeros)?;
|
||||
storage.write(
|
||||
FLASH_ADDR + MAGIC_REGION.offset,
|
||||
&FLASH_INITIALIZE_MAGIC.to_ne_bytes(),
|
||||
)?;
|
||||
|
||||
if Self::has_magic_bytes(storage)? {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(FlashStoreError(FlashStorageError::Other(
|
||||
FLASH_INITIALIZE_MAGIC as i32,
|
||||
)))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
use crate::peripherals::storage::cardstore::CardStore;
|
||||
use crate::peripherals::storage::wifistore::WifiStore;
|
||||
|
||||
pub struct Store {
|
||||
pub card_store: CardStore,
|
||||
pub wifi_store: WifiStore,
|
||||
}
|
||||
|
||||
@@ -0,0 +1,136 @@
|
||||
use alloc::string::String;
|
||||
use crate::network::wifi::WifiCredentials;
|
||||
use crate::Mutex;
|
||||
use crate::peripherals::storage::Nvs;
|
||||
use alloc::sync::Arc;
|
||||
use core::error::Error;
|
||||
use core::fmt::Display;
|
||||
use esp_nvs::Key;
|
||||
|
||||
const NVS_NAMESPACE_WIFI: Key = Key::from_str("WIFI");
|
||||
const NVS_WIFI_SSID_KEY: Key = Key::from_str("WIFI_SSID");
|
||||
const NVS_WIFI_PASS_KEY: Key = Key::from_str("WIFI_PASS");
|
||||
const NVS_WIFI_INITIALIZED_KEY: Key = Key::from_str("INITIALIZED");
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum WifiStoreError {
|
||||
Store(esp_nvs::error::Error),
|
||||
}
|
||||
|
||||
impl Error for WifiStoreError {}
|
||||
|
||||
impl Display for WifiStoreError {
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||
match self {
|
||||
WifiStoreError::Store(e) => e.fmt(f),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<esp_nvs::error::Error> for WifiStoreError {
|
||||
fn from(error: esp_nvs::error::Error) -> WifiStoreError {
|
||||
WifiStoreError::Store(error)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct WifiStore {
|
||||
nvs: Arc<Mutex<Nvs>>,
|
||||
}
|
||||
|
||||
impl WifiStore {
|
||||
pub async fn new(nvs: Arc<Mutex<Nvs>>) -> Result<Self, WifiStoreError> {
|
||||
if !Self::is_initialized(&nvs).await? {
|
||||
log::info!("WifiStore has not been initialized");
|
||||
Self::initialize(&nvs).await?;
|
||||
}
|
||||
|
||||
log::info!("WifiStore has been initialized");
|
||||
|
||||
Ok(Self { nvs })
|
||||
}
|
||||
|
||||
async fn is_initialized(nvs: &Mutex<Nvs>) -> Result<bool, WifiStoreError> {
|
||||
log::info!("check initialization");
|
||||
let mut nvs_lock = nvs.lock().await;
|
||||
match nvs_lock.get::<bool>(&NVS_NAMESPACE_WIFI, &NVS_WIFI_INITIALIZED_KEY) {
|
||||
Ok(initialized) => {
|
||||
log::debug!("initialization_check: initialized {initialized:?}");
|
||||
return Ok(true);
|
||||
}
|
||||
Err(esp_nvs::error::Error::NamespaceNotFound) => {
|
||||
log::debug!("initialization_check: error: namespace not found");
|
||||
return Ok(false);
|
||||
}
|
||||
Err(esp_nvs::error::Error::KeyNotFound) => {
|
||||
log::debug!("initialization_check: error: key not found");
|
||||
return Ok(false);
|
||||
}
|
||||
Err(error) => return Err(error.into()),
|
||||
};
|
||||
}
|
||||
|
||||
async fn initialize(nvs: &Mutex<Nvs>) -> Result<(), WifiStoreError> {
|
||||
log::info!("initializing wifi store");
|
||||
|
||||
let mut nvs_lock = nvs.lock().await;
|
||||
|
||||
log::debug!("initialization: write dummy ssid");
|
||||
nvs_lock.set::<&str>(
|
||||
&NVS_NAMESPACE_WIFI,
|
||||
&NVS_WIFI_SSID_KEY,
|
||||
""
|
||||
)?;
|
||||
|
||||
log::debug!("initialization: write dummy pass");
|
||||
nvs_lock.set::<&str>(
|
||||
&NVS_NAMESPACE_WIFI,
|
||||
&NVS_WIFI_PASS_KEY,
|
||||
""
|
||||
)?;
|
||||
|
||||
// set initialized
|
||||
log::debug!("initialization: write initialized");
|
||||
nvs_lock.set::<bool>(
|
||||
&NVS_NAMESPACE_WIFI,
|
||||
&NVS_WIFI_INITIALIZED_KEY,
|
||||
true,
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn load_credentials(&self) -> Result<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)?;
|
||||
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})
|
||||
}
|
||||
|
||||
pub async fn save_credentials(&self, credentials: &WifiCredentials) -> Result<(), WifiStoreError> {
|
||||
log::debug!("writing wifi credentials");
|
||||
log::debug!("writing ssid...\nssid: {}", credentials.ssid);
|
||||
|
||||
self.nvs.lock().await.set::<&str>(
|
||||
&NVS_NAMESPACE_WIFI,
|
||||
&NVS_WIFI_SSID_KEY,
|
||||
&credentials.ssid,
|
||||
)?;
|
||||
|
||||
log::debug!("writing pass...\npass: {}", credentials.pass);
|
||||
|
||||
self.nvs.lock().await.set::<&str>(
|
||||
&NVS_NAMESPACE_WIFI,
|
||||
&NVS_WIFI_PASS_KEY,
|
||||
&credentials.pass,
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user