this might be over-engineered

This commit is contained in:
Robin Müller 2023-02-24 18:10:43 +01:00
parent 1744f1aff0
commit 95ce2c79b9
No known key found for this signature in database
GPG Key ID: 11D4952C8CCEF814
5 changed files with 48 additions and 33 deletions

View File

@ -18,7 +18,6 @@
#include <dummies/PlPcduDummy.h> #include <dummies/PlPcduDummy.h>
#include <dummies/PlocMpsocDummy.h> #include <dummies/PlocMpsocDummy.h>
#include <dummies/PlocSupervisorDummy.h> #include <dummies/PlocSupervisorDummy.h>
#include <dummies/RtdPollingDummy.h>
#include <dummies/RwDummy.h> #include <dummies/RwDummy.h>
#include <dummies/SaDeploymentDummy.h> #include <dummies/SaDeploymentDummy.h>
#include <dummies/ScexDummy.h> #include <dummies/ScexDummy.h>

View File

@ -4,6 +4,7 @@
#include <fsfw/datapoollocal/LocalPoolVector.h> #include <fsfw/datapoollocal/LocalPoolVector.h>
#include <fsfw/globalfunctions/math/VectorOperations.h> #include <fsfw/globalfunctions/math/VectorOperations.h>
#include <math.h> #include <math.h>
#include <iostream> #include <iostream>
bool SusConverter::checkSunSensorData(const uint16_t susChannel[6]) { bool SusConverter::checkSunSensorData(const uint16_t susChannel[6]) {

View File

@ -16,12 +16,11 @@ using namespace returnvalue;
PersistentTmStore::PersistentTmStore(object_id_t objectId, const char* baseDir, PersistentTmStore::PersistentTmStore(object_id_t objectId, const char* baseDir,
std::string baseName, RolloverInterval intervalUnit, std::string baseName, RolloverInterval intervalUnit,
uint32_t intervalCount, timeval& currentTv, uint32_t intervalCount, StorageManagerIF& tmStore,
StorageManagerIF& tmStore, SdCardMountedIF& sdcMan) SdCardMountedIF& sdcMan)
: SystemObject(objectId), : SystemObject(objectId),
baseDir(baseDir), baseDir(baseDir),
baseName(std::move(baseName)), baseName(std::move(baseName)),
currentTv(currentTv),
sdcMan(sdcMan), sdcMan(sdcMan),
tmStore(tmStore) { tmStore(tmStore) {
tcQueue = QueueFactory::instance()->createMessageQueue(); tcQueue = QueueFactory::instance()->createMessageQueue();
@ -59,7 +58,7 @@ ReturnValue_t PersistentTmStore::assignAndOrCreateMostRecentFile() {
} }
} }
if (not activeFile.has_value()) { if (not activeFile.has_value()) {
return createMostRecentFile(); return createMostRecentFile(std::nullopt);
} }
return returnvalue::OK; return returnvalue::OK;
} }
@ -77,6 +76,7 @@ ReturnValue_t PersistentTmStore::handleCommandQueue(StorageManagerIF& ipcStore,
if (cmdMessage.getMessageType() == messagetypes::TM_STORE) { if (cmdMessage.getMessageType() == messagetypes::TM_STORE) {
Command_t cmd = cmdMessage.getCommand(); Command_t cmd = cmdMessage.getCommand();
if (cmd == TmStoreMessage::DELETE_STORE_CONTENT_TIME) { if (cmd == TmStoreMessage::DELETE_STORE_CONTENT_TIME) {
Clock::getClock_timeval(&currentTv);
store_address_t storeId = TmStoreMessage::getStoreId(&cmdMessage); store_address_t storeId = TmStoreMessage::getStoreId(&cmdMessage);
auto accessor = ipcStore.getData(storeId); auto accessor = ipcStore.getData(storeId);
uint32_t deleteUpToUnixSeconds = 0; uint32_t deleteUpToUnixSeconds = 0;
@ -85,6 +85,7 @@ ReturnValue_t PersistentTmStore::handleCommandQueue(StorageManagerIF& ipcStore,
SerializeIF::Endianness::NETWORK); SerializeIF::Endianness::NETWORK);
deleteUpTo(deleteUpToUnixSeconds); deleteUpTo(deleteUpToUnixSeconds);
} else if (cmd == TmStoreMessage::DOWNLINK_STORE_CONTENT_TIME) { } else if (cmd == TmStoreMessage::DOWNLINK_STORE_CONTENT_TIME) {
Clock::getClock_timeval(&currentTv);
store_address_t storeId = TmStoreMessage::getStoreId(&cmdMessage); store_address_t storeId = TmStoreMessage::getStoreId(&cmdMessage);
auto accessor = ipcStore.getData(storeId); auto accessor = ipcStore.getData(storeId);
if (accessor.second.size() < 8) { if (accessor.second.size() < 8) {
@ -147,6 +148,7 @@ ReturnValue_t PersistentTmStore::storePacket(PusTmReader& reader) {
if (baseDirUninitialized) { if (baseDirUninitialized) {
updateBaseDir(); updateBaseDir();
} }
Clock::getClock_timeval(&currentTv);
// It is assumed here that the filesystem is usable. // It is assumed here that the filesystem is usable.
if (not activeFile.has_value()) { if (not activeFile.has_value()) {
ReturnValue_t result = assignAndOrCreateMostRecentFile(); ReturnValue_t result = assignAndOrCreateMostRecentFile();
@ -155,21 +157,25 @@ ReturnValue_t PersistentTmStore::storePacket(PusTmReader& reader) {
} }
} }
if (currentTv.tv_sec < activeFileTv.tv_sec or bool createNewFile = false;
currentTv.tv_sec - activeFileTv.tv_sec > static_cast<int>(rolloverDiffSeconds)) { std::optional<uint8_t> suffix = std::nullopt;
if (file_size(activeFile.value()) + reader.getFullPacketLen() > fileBuf.size()) { if (currentTv.tv_sec > activeFileTv.tv_sec + static_cast<int>(rolloverDiffSeconds)) {
uint8_t appendedCounter = 1; createNewFile = true;
path rolloverName; currentSameSecNumber = 0;
while (true) { } else if (file_size(activeFile.value()) + reader.getFullPacketLen() > fileBuf.size()) {
rolloverName = path(activeFile.value().string() + "." + std::to_string(appendedCounter)); createNewFile = true;
if (not exists(rolloverName)) { if (currentSameSecNumber >= MAX_FILES_IN_ONE_SECOND) {
break; currentSameSecNumber = 0;
}
appendedCounter++;
}
rename(activeFile.value(), rolloverName);
std::ofstream of(activeFile.value(), std::ios::binary);
} }
if (currentTv.tv_sec == activeFileTv.tv_sec) {
suffix = currentSameSecNumber++;
} else {
currentSameSecNumber = 0;
}
}
if (createNewFile) {
createMostRecentFile(currentSameSecNumber++);
} }
// Rollover conditions were handled, write to file now // Rollover conditions were handled, write to file now
@ -311,7 +317,7 @@ void PersistentTmStore::fileToPackets(const std::filesystem::path& path, uint32_
} }
} }
ReturnValue_t PersistentTmStore::createMostRecentFile() { ReturnValue_t PersistentTmStore::createMostRecentFile(std::optional<uint8_t> suffix) {
using namespace std::filesystem; using namespace std::filesystem;
unsigned currentIdx = 0; unsigned currentIdx = 0;
path pathStart = basePath / baseName; path pathStart = basePath / baseName;
@ -329,9 +335,19 @@ ReturnValue_t PersistentTmStore::createMostRecentFile() {
return returnvalue::FAILED; return returnvalue::FAILED;
} }
currentIdx += writtenBytes; currentIdx += writtenBytes;
strncpy(reinterpret_cast<char*>(fileBuf.data() + currentIdx), ".bin", char* res = strcpy(reinterpret_cast<char*>(fileBuf.data() + currentIdx), ".bin");
fileBuf.size() - currentIdx); if (res == nullptr) {
return returnvalue::FAILED;
}
currentIdx += 4; currentIdx += 4;
if (suffix.has_value()) {
std::string fullSuffix = "." + std::to_string(suffix.value());
res = strcpy(reinterpret_cast<char*>(fileBuf.data() + currentIdx), fullSuffix.c_str());
if (res == nullptr) {
return returnvalue::FAILED;
}
currentIdx += fullSuffix.size();
}
path newPath(std::string(reinterpret_cast<const char*>(fileBuf.data()), currentIdx)); path newPath(std::string(reinterpret_cast<const char*>(fileBuf.data()), currentIdx));
std::ofstream of(newPath, std::ios::binary); std::ofstream of(newPath, std::ios::binary);

View File

@ -31,7 +31,7 @@ class PersistentTmStore : public TmStoreFrontendSimpleIF, public SystemObject {
static constexpr Event POSSIBLE_FILE_CORRUPTION = static constexpr Event POSSIBLE_FILE_CORRUPTION =
event::makeEvent(SUBSYSTEM_ID, 0, severity::LOW); event::makeEvent(SUBSYSTEM_ID, 0, severity::LOW);
PersistentTmStore(object_id_t objectId, const char* baseDir, std::string baseName, PersistentTmStore(object_id_t objectId, const char* baseDir, std::string baseName,
RolloverInterval intervalUnit, uint32_t intervalCount, timeval& currentTv, RolloverInterval intervalUnit, uint32_t intervalCount,
StorageManagerIF& tmStore, SdCardMountedIF& sdcMan); StorageManagerIF& tmStore, SdCardMountedIF& sdcMan);
ReturnValue_t initializeTmStore(); ReturnValue_t initializeTmStore();
@ -48,6 +48,7 @@ class PersistentTmStore : public TmStoreFrontendSimpleIF, public SystemObject {
ReturnValue_t passPacket(PusTmReader& reader); ReturnValue_t passPacket(PusTmReader& reader);
private: private:
static constexpr uint8_t MAX_FILES_IN_ONE_SECOND = 10;
static constexpr size_t MAX_FILESIZE = 8192; static constexpr size_t MAX_FILESIZE = 8192;
// ISO8601 timestamp. // ISO8601 timestamp.
static constexpr char FILE_DATE_FORMAT[] = "%FT%H%M%SZ"; static constexpr char FILE_DATE_FORMAT[] = "%FT%H%M%SZ";
@ -58,10 +59,11 @@ class PersistentTmStore : public TmStoreFrontendSimpleIF, public SystemObject {
bool baseDirUninitialized = true; bool baseDirUninitialized = true;
const char* baseDir; const char* baseDir;
std::string baseName; std::string baseName;
uint8_t currentSameSecNumber = 0;
std::filesystem::path basePath; std::filesystem::path basePath;
uint32_t rolloverDiffSeconds = 0; uint32_t rolloverDiffSeconds = 0;
std::array<uint8_t, MAX_FILESIZE> fileBuf{}; std::array<uint8_t, MAX_FILESIZE> fileBuf{};
timeval& currentTv; timeval currentTv;
timeval activeFileTv{}; timeval activeFileTv{};
std::optional<std::filesystem::path> activeFile; std::optional<std::filesystem::path> activeFile;
SdCardMountedIF& sdcMan; SdCardMountedIF& sdcMan;
@ -74,7 +76,7 @@ class PersistentTmStore : public TmStoreFrontendSimpleIF, public SystemObject {
[[nodiscard]] MessageQueueId_t getCommandQueue() const override; [[nodiscard]] MessageQueueId_t getCommandQueue() const override;
void calcDiffSeconds(RolloverInterval intervalUnit, uint32_t intervalCount); void calcDiffSeconds(RolloverInterval intervalUnit, uint32_t intervalCount);
ReturnValue_t createMostRecentFile(); ReturnValue_t createMostRecentFile(std::optional<uint8_t> suffix);
static ReturnValue_t pathToTm(const std::filesystem::path& path, struct tm& time); static ReturnValue_t pathToTm(const std::filesystem::path& path, struct tm& time);
void fileToPackets(const std::filesystem::path& path, uint32_t unixStamp, TmFunnelBase& funnel); void fileToPackets(const std::filesystem::path& path, uint32_t unixStamp, TmFunnelBase& funnel);
bool updateBaseDir(); bool updateBaseDir();

View File

@ -14,14 +14,11 @@ PusTmFunnel::PusTmFunnel(TmFunnelBase::FunnelCfg cfg, TimeReaderIF &timeReader,
SdCardMountedIF &sdcMan) SdCardMountedIF &sdcMan)
: TmFunnelBase(cfg), : TmFunnelBase(cfg),
timeReader(timeReader), timeReader(timeReader),
miscStore(objects::MISC_TM_STORE, "tm", "misc", RolloverInterval::HOURLY, 2, currentTv, miscStore(objects::MISC_TM_STORE, "tm", "misc", RolloverInterval::HOURLY, 2, tmStore, sdcMan),
tmStore, sdcMan), okStore(objects::OK_TM_STORE, "tm", "ok", RolloverInterval::MINUTELY, 30, tmStore, sdcMan),
okStore(objects::OK_TM_STORE, "tm", "ok", RolloverInterval::MINUTELY, 30, currentTv, tmStore, notOkStore(objects::NOT_OK_TM_STORE, "tm", "nok", RolloverInterval::MINUTELY, 30, tmStore,
sdcMan), sdcMan),
notOkStore(objects::NOT_OK_TM_STORE, "tm", "nok", RolloverInterval::MINUTELY, 30, currentTv, hkStore(objects::HK_TM_STORE, "tm", "hk", RolloverInterval::MINUTELY, 15, tmStore, sdcMan),
tmStore, sdcMan),
hkStore(objects::HK_TM_STORE, "tm", "hk", RolloverInterval::MINUTELY, 15, currentTv, tmStore,
sdcMan),
sdcMan(sdcMan) { sdcMan(sdcMan) {
Clock::getClock_timeval(&currentTv); Clock::getClock_timeval(&currentTv);
Clock::getUptime(&lastTvUpdate); Clock::getUptime(&lastTvUpdate);