storage: implement get card by uuid

This commit is contained in:
2026-08-26 20:03:01 +00:00
parent 7b378b89ab
commit 0fe45687d1
3 changed files with 59 additions and 31 deletions
+34 -9
View File
@@ -1,10 +1,11 @@
use alloc::collections::BTreeMap; use embedded_storage::nor_flash::ReadNorFlash;
use alloc::collections::BTreeMap;
use alloc::vec::Vec;
use binary_serde::{BinarySerde, Endianness};
use binary_serde::{BinarySerde, Endianness, DeserializeError};
//use crate::card::model::NfcPayload;
use crate::card::model::NfcPayload;
use crate::storage::store::{Store, StoreError};
use crate::storage::{MemoryRegion,CARDSTORE_REGION};
@@ -15,7 +16,7 @@ const COUNT_REGION: MemoryRegion = MemoryRegion { offset: CARDSTORE_REGION.offse
#[derive(Debug, BinarySerde, Default, PartialEq, Eq)]
#[repr(C)]
struct AllocationTableEntry {
pub struct AllocationTableEntry {
uuid: u32,
offset: u32,
@@ -36,14 +37,33 @@ const ALLOCATION_TABLE_REGION: MemoryRegion = MemoryRegion {offset: COUNT_REGION
const CARDS_REGION: MemoryRegion = MemoryRegion { offset: ALLOCATION_TABLE_REGION.end() as u32, size: CARDSTORE_REGION.size - ALLOCATION_TABLE_REGION.end()};
#[derive(Debug)]
pub enum CardStoreError {
Store(StoreError),
CorruptedEntry(DeserializeError),
NoEntry
}
impl From<StoreError> for CardStoreError {
fn from(error: StoreError) -> CardStoreError {
CardStoreError::Store(error)
}
}
impl From<DeserializeError> for CardStoreError {
fn from(error: DeserializeError) -> CardStoreError {
CardStoreError::CorruptedEntry(error)
}
}
pub struct CardStore {
store: Store,
count: usize,
allocation_table: BTreeMap<u32, AllocationTableEntry>
pub allocation_table: BTreeMap<u32, AllocationTableEntry>
}
impl CardStore {
pub fn new(mut store: Store) -> Result<Self, StoreError> {
pub fn new(mut store: Store) -> Result<Self, CardStoreError> {
let mut count_bytes: [u8; COUNT_REGION.size] = [0; COUNT_REGION.size];
store.read(COUNT_REGION.offset, &mut count_bytes)?;
@@ -59,15 +79,20 @@ impl CardStore {
let mut allocation_table = BTreeMap::<u32, AllocationTableEntry>::new();
for chunk in chunks {
let allocation_table_entry = AllocationTableEntry::binary_deserialize(chunk.as_ref(), Endianness::Little).unwrap();
let allocation_table_entry = AllocationTableEntry::binary_deserialize(chunk.as_ref(), Endianness::Little)?;
allocation_table.insert(allocation_table_entry.uuid, allocation_table_entry);
}
Ok(Self { store, count, allocation_table })
}
//fn get_card_by_uuid(uuid: &str) {}
pub fn get_card_by_uuid(&mut self, uuid: u32) -> Result<NfcPayload, CardStoreError> {
let entry = self.allocation_table.get(&uuid).ok_or(CardStoreError::NoEntry)?;
let mut payload: [u8; size_of::<NfcPayload>()] = [0; size_of::<NfcPayload>()];
self.store.read(entry.offset, &mut payload)?;
Ok(NfcPayload::binary_deserialize(payload.as_ref(), Endianness::Little)?)
}
//fn get_card_by_position(position: usize) {}
}