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

This commit is contained in:
2026-08-26 20:47:17 +00:00
parent 2cd18c4ce1
commit 1f1bdda35b
2 changed files with 138 additions and 53 deletions
+19 -12
View File
@@ -16,7 +16,7 @@ use creaturedex::drivers::nfc_pn532::NfcPn532Driver;
use creaturedex::navigation::inputs::Inputs; use creaturedex::navigation::inputs::Inputs;
use creaturedex::navigation::navigation::{self, CARD_DATA}; use creaturedex::navigation::navigation::{self, CARD_DATA};
use creaturedex::network::wifi::setup_wifi; use creaturedex::network::wifi::setup_wifi;
use creaturedex::storage::cardstore::CardStore; use creaturedex::storage::cardstore::{CardStore, CardStoreError};
use creaturedex::storage::store::Store; use creaturedex::storage::store::Store;
use embassy_executor::Spawner; use embassy_executor::Spawner;
use embassy_time::Instant; use embassy_time::Instant;
@@ -32,6 +32,7 @@ use esp_hal::Blocking;
use esp_hal::clock::CpuClock; use esp_hal::clock::CpuClock;
use esp_hal::gpio::{Input, InputConfig, Level, Output, OutputConfig, Pull}; use esp_hal::gpio::{Input, InputConfig, Level, Output, OutputConfig, Pull};
use esp_hal::i2c::master::{Config as I2cConfig, I2c}; use esp_hal::i2c::master::{Config as I2cConfig, I2c};
use esp_hal::rtc_cntl::Rtc;
use esp_hal::timer::timg::TimerGroup; use esp_hal::timer::timg::TimerGroup;
use log::error; use log::error;
@@ -79,16 +80,10 @@ async fn main(spawner: Spawner) {
peripherals.GPIO12, // shared dc peripherals.GPIO12, // shared dc
))); )));
let _secondary_oled_cs_2 = Output::new( let _secondary_oled_cs_2 =
peripherals.GPIO14, Output::new(peripherals.GPIO14, Level::High, OutputConfig::default());
Level::High, let _secondary_oled_cs_3 =
OutputConfig::default(), Output::new(peripherals.GPIO21, Level::High, OutputConfig::default());
);
let _secondary_oled_cs_3 = Output::new(
peripherals.GPIO21,
Level::High,
OutputConfig::default(),
);
let config = I2cConfig::default().with_frequency(esp_hal::time::Rate::from_khz(400)); let config = I2cConfig::default().with_frequency(esp_hal::time::Rate::from_khz(400));
let i2c = match I2c::new(peripherals.I2C0, config) { let i2c = match I2c::new(peripherals.I2C0, config) {
@@ -112,6 +107,8 @@ async fn main(spawner: Spawner) {
let store = Store::new(peripherals.FLASH).expect("Could not initialize Flash Store"); let store = Store::new(peripherals.FLASH).expect("Could not initialize Flash Store");
let card_store = CardStore::new(store).expect("Could not initialize Card Store"); let card_store = CardStore::new(store).expect("Could not initialize Card Store");
let mut rtc = Rtc::new(peripherals.LPWR);
//setup_wifi(peripherals.WIFI, peripherals.FLASH, &spawner).await; //setup_wifi(peripherals.WIFI, peripherals.FLASH, &spawner).await;
let mut nfc_driver = NfcPn532Driver::new(AtomicDevice::new(shared_i2c)); let mut nfc_driver = NfcPn532Driver::new(AtomicDevice::new(shared_i2c));
@@ -135,12 +132,14 @@ async fn main(spawner: Spawner) {
log::info!("Setup complete, entering main loop"); log::info!("Setup complete, entering main loop");
spawner.spawn(navigation::run(inputs, display).expect("run task failed")); spawner.spawn(navigation::run(inputs, display).expect("run task failed"));
spawner.spawn(nfc_driver_task(nfc_driver).expect("nfc driver task failed")); spawner.spawn(nfc_driver_task(nfc_driver, card_store, rtc).expect("nfc driver task failed"));
} }
#[embassy_executor::task] #[embassy_executor::task]
async fn nfc_driver_task( async fn nfc_driver_task(
mut nfc_driver: NfcPn532Driver<AtomicDevice<'static, I2c<'static, Blocking>>>, mut nfc_driver: NfcPn532Driver<AtomicDevice<'static, I2c<'static, Blocking>>>,
mut card_store: CardStore,
rtc: Rtc<'static>,
) { ) {
loop { loop {
while CARD_DATA.lock().await.is_none() { while CARD_DATA.lock().await.is_none() {
@@ -155,6 +154,14 @@ async fn nfc_driver_task(
.lock() .lock()
.await .await
.replace(Card::try_from(split_data).expect("Invalid card data")); .replace(Card::try_from(split_data).expect("Invalid card data"));
match card_store.save_raw_card(split_data, rtc.current_time_us()) {
Ok(()) => {}
Err(error) => {
log::error!("Error storing card in flash: {:?}", error)
}
}
let elapsed_ms = start.elapsed().as_millis(); let elapsed_ms = start.elapsed().as_millis();
log::info!( log::info!(
"NFC read duration: {} ms, card data length: {}", "NFC read duration: {} ms, card data length: {}",
+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(())
} }
} }