CDC Badge OS
Firmware for the CDC Badge v1.0 hardware security key
Loading...
Searching...
No Matches
TropicStorage.h
Go to the documentation of this file.
1#pragma once
5#include <cstdint>
6
7namespace cdc::core {
8
9class TropicStorage : public IService {
10public:
16
17 static constexpr uint8_t FLAG_USED = 0x01;
18 static constexpr uint16_t CHUNK_SLOTS = 64;
19
20 using SlotCallback = void(*)(uint16_t slot, const CacheEntry& entry, void* ctx);
21 using RebuildLogFn = void(*)(uint16_t slot, const char* message, void* ctx);
22
23 static TropicStorage& instance();
24
25 // IService
26 bool init() override;
27 bool start() override;
28 void stop() override;
29 ServiceState getState() const override { return state_; }
30 const char* getName() const override { return "tropic_storage"; }
31
32 void setSecureElement(cdc::hal::ISecureElement* se) { secureElement_ = se; }
33
34 bool isCacheValid() const { return cacheValid_; }
35
36 // Iteration helpers
37 bool forEachSlot(uint8_t moduleId, SlotCallback cb, void* ctx);
38 bool forEachSlot(uint8_t moduleId, uint16_t fromSlot, uint16_t toSlot,
39 SlotCallback cb, void* ctx);
40 bool getSlot(uint8_t moduleId, uint16_t index, SlotCallback cb, void* ctx);
41
42 // NVS cache updates (does not touch TROPIC01)
43 bool writeSlot(uint8_t moduleId, uint16_t slot, const char* name, uint8_t flags);
44 bool eraseSlot(uint8_t moduleId, uint16_t slot);
45
46 // Maintenance
47 bool rebuild();
48 bool rebuildVerbose(RebuildLogFn logFn, void* ctx);
49 bool cleanup();
50
51private:
52 TropicStorage() = default;
53
54 struct CacheHeader {
55 uint8_t version;
56 uint8_t chunkSlots;
57 uint16_t entrySize;
58 uint32_t mapSignature;
59 } __attribute__((packed));
60
61 bool loadHeader();
62 bool saveHeader();
63 bool loadChunk(uint16_t chunkIndex, CacheEntry* entries);
64 bool saveChunk(uint16_t chunkIndex, const CacheEntry* entries);
65 uint32_t computeMapSignature() const;
66
67 bool setEntry(uint16_t slot, const CacheEntry& entry);
68 bool getEntry(uint16_t slot, CacheEntry* entry);
69 bool isEntryUsed(const CacheEntry& entry) const;
70 bool isEntryAllowed(uint16_t slot, uint8_t moduleId) const;
71
73 cdc::hal::ISecureElement* secureElement_ = nullptr;
74 CacheHeader header_ = {};
75 bool cacheValid_ = false;
76};
77
78} // namespace cdc::core
uint32_t mapSignature
char name[cdc::hal::ISecureElement::RMEM_NAME_LEN]
uint8_t chunkSlots
uint8_t flags
uint8_t version
uint16_t entrySize
uint8_t moduleId
static constexpr uint16_t CHUNK_SLOTS
bool rebuild()
Rebuilds cache contents from secure-element R-MEM without verbose logging.
void stop() override
Stops cache service.
struct cdc::core::TropicStorage::CacheEntry __attribute__((packed))
bool writeSlot(uint8_t moduleId, uint16_t slot, const char *name, uint8_t flags)
Writes or updates cached metadata entry for one slot.
bool eraseSlot(uint8_t moduleId, uint16_t slot)
Clears cached metadata entry for one slot.
static TropicStorage & instance()
Returns singleton instance of TROPIC metadata cache manager.
bool forEachSlot(uint8_t moduleId, SlotCallback cb, void *ctx)
Iterates all cached slots for one module across its allowed range.
void(*)(uint16_t slot, const CacheEntry &entry, void *ctx) SlotCallback
void setSecureElement(cdc::hal::ISecureElement *se)
bool getSlot(uint8_t moduleId, uint16_t index, SlotCallback cb, void *ctx)
Resolves one module-relative index to slot entry and invokes callback.
const char * getName() const override
ServiceState getState() const override
bool start() override
Starts cache service, initializing first if required.
bool init() override
Initializes cache metadata and validates persisted cache header.
void(*)(uint16_t slot, const char *message, void *ctx) RebuildLogFn
static constexpr uint8_t FLAG_USED
bool rebuildVerbose(RebuildLogFn logFn, void *ctx)
Rebuilds cache contents from secure-element R-MEM with optional logging callback.
bool cleanup()
Removes cache entries and chip records that violate slot/module mapping.
static constexpr uint8_t RMEM_NAME_LEN
char name[cdc::hal::ISecureElement::RMEM_NAME_LEN]