CDC Badge OS
Firmware for the CDC Badge v1.0 hardware security key
Loading...
Searching...
No Matches
SlotManager.h
Go to the documentation of this file.
1#pragma once
2
3#include "cdc_core/IModule.h"
5#include <cstdint>
6#include <cstring>
7#include <memory>
8#include <new>
9
10namespace cdc::core {
11
20public:
27 void setSlotRange(uint16_t start, uint16_t end, uint8_t moduleId) {
28 if (start > end || start == 0 || end == 0) {
29 hasSlotRange_ = false;
30 rmemStart_ = 0;
31 rmemEnd_ = 0;
32 moduleId_ = 0;
33 return;
34 }
35 hasSlotRange_ = true;
36 rmemStart_ = start;
37 rmemEnd_ = end;
39 }
40
45 void setSlotRange(const IModule::SlotRange& range) {
46 if (!range.hasRmem) {
47 setSlotRange(0, 0, 0);
48 return;
49 }
50 setSlotRange(range.rmemStart, range.rmemEnd, range.moduleId);
51 }
52
57 uint16_t capacity() const {
58 if (!hasSlotRange_) return 0;
59 return static_cast<uint16_t>(rmemEnd_ - rmemStart_ + 1);
60 }
61
68 bool toPhysicalSlot(uint16_t logicalIndex, uint16_t* slotOut) const {
69 if (!slotOut) return false;
70 if (!hasSlotRange_) return false;
71 uint32_t slot = static_cast<uint32_t>(rmemStart_) + logicalIndex;
72 if (slot > rmemEnd_) return false;
73 *slotOut = static_cast<uint16_t>(slot);
74 return true;
75 }
76
83 bool toLogicalSlot(uint16_t slot, uint16_t* logicalIndexOut) const {
84 if (!logicalIndexOut) return false;
85 if (!hasSlotRange_) return false;
86 if (slot < rmemStart_ || slot > rmemEnd_) return false;
87 *logicalIndexOut = static_cast<uint16_t>(slot - rmemStart_);
88 return true;
89 }
90
96 bool findFreeSlot(uint16_t* slotOut) const {
97 if (!slotOut) return false;
98 if (!hasSlotRange_) return false;
99
100 uint16_t cap = capacity();
101 if (cap == 0) return false;
102 auto used = std::unique_ptr<bool[]>(new (std::nothrow) bool[cap]);
103 if (!used) return false;
104 std::memset(used.get(), 0, cap * sizeof(bool));
105
106 struct Ctx {
107 bool* used;
108 uint16_t base;
109 uint16_t cap;
110 } ctx = { used.get(), rmemStart_, cap };
111
112 auto cb = [](uint16_t slot, const TropicStorage::CacheEntry&, void* user) {
113 auto* c = static_cast<Ctx*>(user);
114 if (slot < c->base) return;
115 uint16_t idx = slot - c->base;
116 if (idx < c->cap) {
117 c->used[idx] = true;
118 }
119 };
120
122 moduleId_, rmemStart_, rmemEnd_, cb, &ctx);
123
124 for (uint16_t i = 0; i < cap; i++) {
125 if (!used[i]) {
126 uint16_t candidate = static_cast<uint16_t>(rmemStart_ + i);
127 if (candidate <= rmemEnd_) {
128 *slotOut = candidate;
129 return true;
130 }
131 return false;
132 }
133 }
134
135 return false;
136 }
137
142 bool hasSlotRange() const { return hasSlotRange_; }
143
148 uint16_t rmemStart() const { return rmemStart_; }
149
154 uint16_t rmemEnd() const { return rmemEnd_; }
155
160 uint8_t moduleId() const { return moduleId_; }
161
162protected:
163 bool hasSlotRange_ = false;
164 uint16_t rmemStart_ = 0;
165 uint16_t rmemEnd_ = 0;
166 uint8_t moduleId_ = 0;
167};
168
169} // namespace cdc::core
Manages logical-to-physical RMEM slot mapping for module storage layers.
Definition SlotManager.h:19
uint16_t rmemStart() const
Returns configured RMEM start slot.
bool toLogicalSlot(uint16_t slot, uint16_t *logicalIndexOut) const
Converts physical RMEM slot to logical index.
Definition SlotManager.h:83
uint16_t capacity() const
Returns available entry capacity from configured slot range.
Definition SlotManager.h:57
bool findFreeSlot(uint16_t *slotOut) const
Finds first free physical slot in configured range.
Definition SlotManager.h:96
void setSlotRange(const IModule::SlotRange &range)
Configures logical-to-physical slot mapping from a SlotRange struct.
Definition SlotManager.h:45
uint8_t moduleId() const
Returns configured owning module identifier.
uint16_t rmemEnd() const
Returns configured RMEM end slot.
bool hasSlotRange() const
Returns whether a valid slot range is configured.
bool toPhysicalSlot(uint16_t logicalIndex, uint16_t *slotOut) const
Converts logical index to physical RMEM slot.
Definition SlotManager.h:68
void setSlotRange(uint16_t start, uint16_t end, uint8_t moduleId)
Configures logical-to-physical slot mapping using raw values.
Definition SlotManager.h:27
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.