From 12405d1bef846691fff0d60670a9bdb06b413071 Mon Sep 17 00:00:00 2001 From: Empire Date: Sun, 12 Apr 2026 22:15:52 +0200 Subject: [PATCH] cleanup --- Software/MainBoard/rust/src/hal/esp.rs | 8 +- .../rust/src/hal/savegame_manager.rs | 73 +++++-------------- 2 files changed, 18 insertions(+), 63 deletions(-) diff --git a/Software/MainBoard/rust/src/hal/esp.rs b/Software/MainBoard/rust/src/hal/esp.rs index 0b9663c..90bb136 100644 --- a/Software/MainBoard/rust/src/hal/esp.rs +++ b/Software/MainBoard/rust/src/hal/esp.rs @@ -565,13 +565,7 @@ impl Esp<'_> { /// Retries once on flash error. pub(crate) async fn save_config(&mut self, config: Vec) -> FatResult<()> { let timestamp = self.get_time().to_rfc3339(); - match self.savegame.save(config.as_slice(), ×tamp) { - Ok(()) => Ok(()), - Err(e) => { - warn!("First save attempt failed: {e:?}. Retrying..."); - self.savegame.save(config.as_slice(), ×tamp) - } - } + self.savegame.save(config.as_slice(), ×tamp) } /// Delete a specific save slot by erasing it on flash. diff --git a/Software/MainBoard/rust/src/hal/savegame_manager.rs b/Software/MainBoard/rust/src/hal/savegame_manager.rs index bd1d76a..a60b99d 100644 --- a/Software/MainBoard/rust/src/hal/savegame_manager.rs +++ b/Software/MainBoard/rust/src/hal/savegame_manager.rs @@ -2,9 +2,9 @@ use alloc::vec::Vec; use bincode::{Decode, Encode}; use embedded_savegame::storage::{Flash, Storage}; use embedded_storage::nor_flash::{NorFlash, ReadNorFlash}; -use esp_bootloader_esp_idf::partitions::{Error as PartitionError, Error, FlashRegion}; +use esp_bootloader_esp_idf::partitions::{Error as PartitionError, FlashRegion}; use log::{error, info}; -use serde::{Deserialize, Serialize}; +use serde::Serialize; use crate::fat_error::{FatError, FatResult}; use crate::hal::shared_flash::MutexFlashStorage; @@ -26,7 +26,7 @@ pub struct SaveInfo { } /// Wrapper that includes both the config data and metadata like creation timestamp. -#[derive(Serialize, Deserialize, Debug, Encode, Decode)] +#[derive(Serialize, Debug, Encode, Decode)] struct SaveWrapper { /// UTC timestamp in RFC3339 format created_at: alloc::string::String, @@ -72,7 +72,7 @@ impl Flash for SavegameFlashAdapter<'_> { let aligned_start = (addr / ERASE_SIZE) * ERASE_SIZE; // Align end address up to erase boundary let end = addr + SAVEGAME_SLOT_SIZE as u32; - let aligned_end = ((end + ERASE_SIZE - 1) / ERASE_SIZE) * ERASE_SIZE; + let aligned_end = end.div_ceil(ERASE_SIZE) * ERASE_SIZE; if aligned_start != addr || aligned_end != end { log::warn!("Flash erase address not aligned: addr=0x{:x}, slot_size=0x{:x}. Aligned to 0x{:x}-0x{:x}", addr, SAVEGAME_SLOT_SIZE, aligned_start, aligned_end); @@ -126,38 +126,18 @@ impl SavegameManager { }; let mut serialized = bincode::encode_to_vec(&wrapper, bincode::config::standard())?; info!("Serialized config with size {}", serialized.len()); - (&mut self.storage).append(&mut serialized)?; + self.storage.append(&mut serialized)?; Ok(()) } /// Load the most recently saved data. Returns `None` if no valid save exists. /// Unwraps the SaveWrapper and returns only the config data. pub fn load_latest(&mut self) -> FatResult>> { - let slot = (&mut self.storage).scan()?; + let slot = self.storage.scan()?; match slot { None => Ok(None), Some(slot) => { - let mut buf = alloc::vec![0u8; SAVEGAME_SLOT_SIZE]; - match (&mut self.storage).read(slot.idx, &mut buf)? { - None => Ok(None), - Some(data) => { - // Try to deserialize as SaveWrapper (new Bincode format) - match bincode::decode_from_slice::( - data, - bincode::config::standard(), - ) { - Ok((wrapper, _)) => Ok(Some(wrapper.data)), - Err(_) => { - // Fallback to JSON SaveWrapper (intermediate format) - match serde_json::from_slice::(data) { - Ok(wrapper) => Ok(Some(wrapper.data)), - // Fallback to raw data for backwards compatibility - Err(_) => Ok(Some(data.to_vec())), - } - } - } - } - } + self.load_slot(slot.idx) } } } @@ -167,31 +147,22 @@ impl SavegameManager { /// Unwraps the SaveWrapper and returns only the config data. pub fn load_slot(&mut self, idx: usize) -> FatResult>> { let mut buf = alloc::vec![0u8; SAVEGAME_SLOT_SIZE]; - match (&mut self.storage).read(idx, &mut buf)? { + match self.storage.read(idx, &mut buf)? { None => Ok(None), Some(data) => { // Try to deserialize as SaveWrapper (new Bincode format) - match bincode::decode_from_slice::( + let (wrapper, _) = bincode::decode_from_slice::( data, bincode::config::standard(), - ) { - Ok((wrapper, _)) => Ok(Some(wrapper.data)), - Err(_) => { - // Fallback to JSON SaveWrapper (intermediate format) - match serde_json::from_slice::(data) { - Ok(wrapper) => Ok(Some(wrapper.data)), - // Fallback to raw data for backwards compatibility - Err(_) => Ok(Some(data.to_vec())), - } - } - } + )?; + Ok(Some(wrapper.data)) } } } /// Erase a specific slot by index, effectively deleting it. pub fn delete_slot(&mut self, idx: usize) -> FatResult<()> { - (&mut self.storage).erase(idx).map_err(Into::into) + self.storage.erase(idx).map_err(Into::into) } /// Iterate all slots and return metadata for every slot that contains a @@ -201,27 +172,17 @@ impl SavegameManager { let mut saves = Vec::new(); let mut buf = alloc::vec![0u8; SAVEGAME_SLOT_SIZE]; for idx in 0..SAVEGAME_SLOT_COUNT { - if let Some(data) = (&mut self.storage).read(idx, &mut buf)? { + if let Some(data) = self.storage.read(idx, &mut buf)? { // Try to deserialize as SaveWrapper (new Bincode format) - let (len, created_at) = match bincode::decode_from_slice::( + let (wrapper, _) = bincode::decode_from_slice::( data, bincode::config::standard(), - ) { - Ok((wrapper, _)) => (wrapper.data.len() as u32, Some(wrapper.created_at)), - Err(_) => { - // Fallback to JSON SaveWrapper (intermediate format) - match serde_json::from_slice::(data) { - Ok(wrapper) => (wrapper.data.len() as u32, Some(wrapper.created_at)), - // Old format without timestamp - Err(_) => (data.len() as u32, None), - } - } - }; + )?; saves.push(SaveInfo { idx, - len, - created_at, + len: wrapper.data.len() as u32, + created_at: Some(wrapper.created_at), }); } }