registry.cpp
1 #include "registry.h" 2 #include <string.h> 3 4 namespace { 5 6 constexpr uint8_t MAX_ENTRIES = 12; 7 constexpr uint8_t MAX_INSTANCES = 8; 8 constexpr size_t MAX_DATA_SIZE = 48; 9 10 struct Slot { 11 SensorEntry entry; 12 alignas(8) uint8_t snapshots[MAX_INSTANCES][MAX_DATA_SIZE]; 13 bool validity[MAX_INSTANCES]; 14 }; 15 16 Slot slots[MAX_ENTRIES] = {}; 17 uint8_t count = 0; 18 19 Slot *findSlot(SensorKind kind) { 20 for (uint8_t i = 0; i < count; i++) { 21 if (slots[i].entry.kind == kind) return &slots[i]; 22 } 23 return nullptr; 24 } 25 26 } 27 28 void sensors::registry::add(const SensorEntry &entry) { 29 if (findSlot(entry.kind)) return; 30 if (count >= MAX_ENTRIES) return; 31 slots[count].entry = entry; 32 memset(slots[count].snapshots, 0, sizeof(slots[count].snapshots)); 33 memset(slots[count].validity, 0, sizeof(slots[count].validity)); 34 count++; 35 } 36 37 void sensors::registry::pollAll() { 38 for (uint8_t i = 0; i < count; i++) { 39 uint8_t n = slots[i].entry.instanceCount(); 40 for (uint8_t j = 0; j < n && j < MAX_INSTANCES; j++) { 41 slots[i].validity[j] = slots[i].entry.poll( 42 j, slots[i].snapshots[j], slots[i].entry.data_size); 43 } 44 } 45 } 46 47 bool sensors::registry::isAvailable(SensorKind kind) { 48 Slot *s = findSlot(kind); 49 return s && s->entry.isAvailable(); 50 } 51 52 uint8_t sensors::registry::instanceCount(SensorKind kind) { 53 Slot *s = findSlot(kind); 54 return s ? s->entry.instanceCount() : 0; 55 } 56 57 const void *sensors::registry::latest(SensorKind kind, uint8_t index) { 58 Slot *s = findSlot(kind); 59 if (!s || index >= MAX_INSTANCES) return nullptr; 60 return s->snapshots[index]; 61 } 62 63 bool sensors::registry::valid(SensorKind kind, uint8_t index) { 64 Slot *s = findSlot(kind); 65 if (!s || index >= MAX_INSTANCES) return false; 66 return s->validity[index]; 67 } 68 69 uint8_t sensors::registry::entryCount() { 70 return count; 71 } 72 73 const SensorEntry *sensors::registry::entry(uint8_t i) { 74 if (i >= count) return nullptr; 75 return &slots[i].entry; 76 } 77 78 const SensorEntry *sensors::registry::find(SensorKind kind) { 79 Slot *s = findSlot(kind); 80 return s ? &s->entry : nullptr; 81 }