card storage: make more robust, handle cards that are already seen, implement in nfc read

This commit is contained in:
2026-08-29 15:00:18 +00:00
parent b9136242ba
commit 69663bc968
2 changed files with 122 additions and 41 deletions
+3
View File
@@ -22,6 +22,7 @@ use creaturedex::network::wifi::setup_wifi;
use creaturedex::storage::cardstore::CardStore; use creaturedex::storage::cardstore::CardStore;
use creaturedex::storage::store::Store; use creaturedex::storage::store::Store;
use embassy_executor::Spawner; use embassy_executor::Spawner;
use embassy_time::Instant;
use embedded_graphics::pixelcolor::BinaryColor; use embedded_graphics::pixelcolor::BinaryColor;
use embedded_graphics::{ use embedded_graphics::{
mono_font::{MonoTextStyle, ascii::FONT_6X10}, mono_font::{MonoTextStyle, ascii::FONT_6X10},
@@ -29,6 +30,8 @@ use embedded_graphics::{
text::Text, text::Text,
}; };
use embedded_hal_bus::i2c::AtomicDevice; use embedded_hal_bus::i2c::AtomicDevice;
use embedded_hal_bus::util::AtomicCell;
use esp_hal::Blocking;
use esp_hal::clock::CpuClock; use esp_hal::clock::CpuClock;
use esp_hal::gpio::{Input, InputConfig, Pull}; use esp_hal::gpio::{Input, InputConfig, Pull};
use esp_hal::timer::timg::TimerGroup; use esp_hal::timer::timg::TimerGroup;
+119 -41
View File
@@ -1,24 +1,27 @@
use alloc::collections::BTreeMap; use alloc::collections::BTreeMap;
use alloc::vec::Vec; use alloc::vec::Vec;
use binary_serde::{BinarySerde, Endianness, DeserializeError}; use binary_serde::{BinarySerde, DeserializeError, Endianness};
//use crate::card::model::Nfcraw_card; //use crate::card::model::Nfcraw_card;
use crate::card::model::RawCard; use crate::card::model::RawCard;
use crate::storage::store::{Store, StoreError}; use crate::storage::store::{Store, StoreError};
use crate::storage::{MemoryRegion,CARDSTORE_REGION}; use crate::storage::{CARDSTORE_REGION, MemoryRegion};
const MAX_CARDS: usize = 20000; const MAX_CARDS: usize = 20000;
const COUNT_REGION: MemoryRegion = MemoryRegion { offset: CARDSTORE_REGION.offset, size: size_of::<u32>()}; const COUNT_REGION: MemoryRegion = MemoryRegion {
offset: CARDSTORE_REGION.offset,
size: size_of::<u32>(),
};
#[derive(Debug, BinarySerde, PartialEq, Eq, Clone)] #[derive(Debug, BinarySerde, PartialEq, Eq, Clone)]
#[repr(C)] #[repr(C)]
pub struct AllocationTableEntry { pub struct AllocationTableEntry {
uuid: u32, uuid: u32,
// this is the numeric offset of the AllocationTableEntry // this is the numeric offset of the AllocationTableEntry
// as well as the card entry in flash memory // as well as the card entry in flash memory
// size_of::<AllocationTableEntry>() * offset // size_of::<AllocationTableEntry>() * offset
// size_of::<RawCard>() * offset // size_of::<RawCard>() * offset
@@ -26,7 +29,7 @@ pub struct AllocationTableEntry {
creation_date: u64, creation_date: u64,
last_read_date: u64, last_read_date: u64,
// how often a card has been read by nfc // how often a card has been read by nfc
read_count: u32, read_count: u32,
@@ -38,96 +41,171 @@ pub struct AllocationTableEntry {
impl AllocationTableEntry { impl AllocationTableEntry {
fn new(uuid: u32, offset: u32, timestamp: u64) -> Self { fn new(uuid: u32, offset: u32, timestamp: u64) -> Self {
Self { Self {
uuid, offset, creation_date: timestamp, last_read_date: timestamp, read_count: 0, deleted: true, reserved: [0; 4] uuid,
offset,
creation_date: timestamp,
last_read_date: timestamp,
read_count: 0,
deleted: true,
reserved: [0; 4],
} }
} }
} }
const ALLOCATION_TABLE_REGION: MemoryRegion = MemoryRegion {
offset: COUNT_REGION.end() as u32,
size: MAX_CARDS * AllocationTableEntry::SERIALIZED_SIZE,
};
const ALLOCATION_TABLE_REGION: MemoryRegion = MemoryRegion {offset: COUNT_REGION.end() as u32, size: MAX_CARDS * AllocationTableEntry::SERIALIZED_SIZE}; const CARDS_REGION: MemoryRegion = MemoryRegion {
offset: ALLOCATION_TABLE_REGION.end() as u32,
const CARDS_REGION: MemoryRegion = MemoryRegion { offset: ALLOCATION_TABLE_REGION.end() as u32, size: CARDSTORE_REGION.size - ALLOCATION_TABLE_REGION.end()}; size: CARDSTORE_REGION.size - ALLOCATION_TABLE_REGION.end(),
};
#[derive(Debug)] #[derive(Debug)]
pub enum CardStoreError { pub enum CardStoreError {
Store(StoreError), Store(StoreError),
CorruptedEntry(DeserializeError), CorruptedEntry(DeserializeError),
NoEntry NoEntry,
} }
impl From<StoreError> for CardStoreError { impl From<StoreError> for CardStoreError {
fn from(error: StoreError) -> CardStoreError { fn from(error: StoreError) -> CardStoreError {
CardStoreError::Store(error) CardStoreError::Store(error)
} }
} }
impl From<DeserializeError> for CardStoreError { impl From<DeserializeError> for CardStoreError {
fn from(error: DeserializeError) -> CardStoreError { fn from(error: DeserializeError) -> CardStoreError {
CardStoreError::CorruptedEntry(error) CardStoreError::CorruptedEntry(error)
} }
} }
pub struct CardStore { pub struct CardStore {
store: Store, store: Store,
count: u32, count: u32,
pub allocation_table: BTreeMap<u32, AllocationTableEntry> pub allocation_table: BTreeMap<u32, AllocationTableEntry>,
} }
impl CardStore { impl CardStore {
pub fn new(mut store: Store) -> Result<Self, CardStoreError> { pub fn new(mut store: Store) -> Result<Self, CardStoreError> {
let mut count_bytes: [u8; COUNT_REGION.size] = [0; COUNT_REGION.size]; let mut count_bytes: [u8; COUNT_REGION.size] = [0; COUNT_REGION.size];
store.read(COUNT_REGION.offset, &mut count_bytes)?; store.read(COUNT_REGION.offset, &mut count_bytes)?;
let count: u32 = u32::from_ne_bytes(count_bytes); let count: u32 = u32::from_ne_bytes(count_bytes);
let mut allocation_table_entries: Vec<u8> = Vec::with_capacity(AllocationTableEntry::SERIALIZED_SIZE * count as usize); let mut allocation_table_entries: Vec<u8> =
Vec::with_capacity(AllocationTableEntry::SERIALIZED_SIZE * count as usize);
store store.read(
.read(ALLOCATION_TABLE_REGION.offset, &mut allocation_table_entries)?; ALLOCATION_TABLE_REGION.offset,
&mut allocation_table_entries,
)?;
let chunks = allocation_table_entries.into_chunks::<{ AllocationTableEntry::SERIALIZED_SIZE }>(); let chunks =
allocation_table_entries.into_chunks::<{ AllocationTableEntry::SERIALIZED_SIZE }>();
let mut allocation_table = BTreeMap::<u32, AllocationTableEntry>::new(); let mut allocation_table = BTreeMap::<u32, AllocationTableEntry>::new();
for chunk in chunks { for chunk in chunks {
let allocation_table_entry = AllocationTableEntry::binary_deserialize(chunk.as_ref(), Endianness::Little)?; let allocation_table_entry =
AllocationTableEntry::binary_deserialize(chunk.as_ref(), Endianness::Little)?;
allocation_table.insert(allocation_table_entry.uuid, allocation_table_entry); allocation_table.insert(allocation_table_entry.uuid, allocation_table_entry);
} }
Ok(Self { store, count, allocation_table }) Ok(Self {
store,
count,
allocation_table,
})
} }
pub fn get_card_by_uuid(&mut self, uuid: u32) -> Result<RawCard, CardStoreError> { pub fn get_card_by_uuid(&mut self, uuid: u32) -> Result<RawCard, CardStoreError> {
let entry = self.allocation_table.get(&uuid).ok_or(CardStoreError::NoEntry)?; let entry = self
.allocation_table
.get(&uuid)
.ok_or(CardStoreError::NoEntry)?;
let mut raw_card: [u8; size_of::<RawCard>()] = [0; size_of::<RawCard>()]; let mut raw_card: [u8; size_of::<RawCard>()] = [0; size_of::<RawCard>()];
self.store.read(entry.offset, &mut raw_card)?; self.store.read(entry.offset, &mut raw_card)?;
Ok(RawCard::binary_deserialize(raw_card.as_ref(), Endianness::Little)?) Ok(RawCard::binary_deserialize(
raw_card.as_ref(),
Endianness::Little,
)?)
} }
pub fn save_raw_card(&mut self, raw_card: RawCard, timestamp: u64) -> Result<(), CardStoreError> { pub fn save_raw_card(
&mut self,
raw_card: RawCard,
timestamp: u64,
) -> Result<(), CardStoreError> {
let uuid: u32 = (u16::from_ne_bytes(raw_card.event_encoding) as u32) << 16
| (u16::from_ne_bytes(raw_card.card_uuid) as u32);
log::info!("Saving card with uuid {:x}", uuid);
let uuid: u32 = (u16::from_ne_bytes(raw_card.event_encoding) as u32) << 16 | (u16::from_ne_bytes(raw_card.card_uuid) as u32); // check if we already got the card
let entry = match self.allocation_table.values_mut().find(|entry| entry.deleted) { // if not find the next deleted entry,
Some(entry) => entry, // if none create a new one at the end
None => { let (entry, is_new) = match self.allocation_table.get_mut(&uuid) {
let entry = AllocationTableEntry::new(uuid, self.count + 1, timestamp); Some(entry) => {
self.allocation_table.insert(uuid, entry); log::debug!("old entry");
self.allocation_table.get_mut(&uuid).expect("entry was just inserted") (entry, false)
} }
None => match self
.allocation_table
.values_mut()
.find(|entry| entry.deleted)
{
Some(entry) => {
log::debug!("deleted entry");
(entry, true)
}
None => {
log::debug!("new entry");
let entry = AllocationTableEntry::new(uuid, self.count + 1, timestamp);
self.allocation_table.insert(uuid, entry);
(
self.allocation_table
.get_mut(&uuid)
.expect("entry was just inserted"),
true,
)
}
},
}; };
// only write card data if it is new. else it is already written in flash
if is_new {
log::debug!("writing RawCard to flash");
let mut serialized_card: [u8; RawCard::SERIALIZED_SIZE] = [0; RawCard::SERIALIZED_SIZE];
raw_card.binary_serialize(&mut serialized_card, Endianness::Little);
self.store.write(
CARDS_REGION.offset + entry.offset * RawCard::SERIALIZED_SIZE as u32,
&mut serialized_card,
)?;
} else {
entry.last_read_date = timestamp;
entry.read_count += 1;
}
entry.deleted = false; entry.deleted = false;
let mut serialized_card: [u8; RawCard::SERIALIZED_SIZE] = [0; RawCard::SERIALIZED_SIZE]; log::debug!("writing AllocationTableEntry to flash");
raw_card.binary_serialize(&mut serialized_card, Endianness::Little); let mut serialized_entry: [u8; AllocationTableEntry::SERIALIZED_SIZE] =
self.store.write(CARDS_REGION.offset + entry.offset * RawCard::SERIALIZED_SIZE as u32, &mut serialized_card)?; [0; AllocationTableEntry::SERIALIZED_SIZE];
let mut serialized_entry: [u8; AllocationTableEntry::SERIALIZED_SIZE] = [0; AllocationTableEntry::SERIALIZED_SIZE];
entry.binary_serialize(&mut serialized_entry, Endianness::Little); entry.binary_serialize(&mut serialized_entry, Endianness::Little);
self.store.write(ALLOCATION_TABLE_REGION.offset + entry.offset * AllocationTableEntry::SERIALIZED_SIZE as u32, &mut serialized_entry)?; self.store.write(
ALLOCATION_TABLE_REGION.offset
+ entry.offset * AllocationTableEntry::SERIALIZED_SIZE as u32,
&mut serialized_entry,
)?;
self.count += 1; self.count += 1;
log::debug!("writing card count to flash");
let mut serialized_count: [u8; 4] = self.count.to_le_bytes();
self.store
.write(COUNT_REGION.offset, serialized_count.as_mut_slice())?;
Ok(()) Ok(())
} }
} }