From ed603f4e48914e7b7133340b266bfdb1c7df4e5d Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Mon, 12 Dec 2022 18:27:01 +0100 Subject: [PATCH] continued TM store impl --- mission/tmtc/TmStore.cpp | 94 ++++++++++++++++++++++++---------------- mission/tmtc/TmStore.h | 13 ++++-- 2 files changed, 65 insertions(+), 42 deletions(-) diff --git a/mission/tmtc/TmStore.cpp b/mission/tmtc/TmStore.cpp index 55a47347..5c3f00c8 100644 --- a/mission/tmtc/TmStore.cpp +++ b/mission/tmtc/TmStore.cpp @@ -8,14 +8,11 @@ using namespace returnvalue; -TmStore::TmStore(object_id_t objectId, std::string baseName, RolloverInterval interval, - uint8_t intervalFactor, PacketFilter filter, SdCardMountedIF& sdcMan) - : SystemObject(objectId), - filter(filter), - baseName(std::move(baseName)), - interval(interval), - intervalFactor(intervalFactor), - sdcMan(sdcMan) {} +TmStore::TmStore(object_id_t objectId, std::string baseName, RolloverInterval intervalUnit, + uint32_t intervalCount, PacketFilter filter, SdCardMountedIF& sdcMan) + : SystemObject(objectId), filter(filter), baseName(std::move(baseName)), sdcMan(sdcMan) { + calcDiffSeconds(intervalUnit, intervalCount); +} ReturnValue_t TmStore::passPacket(PusTmReader& reader) { bool inApidList = false; @@ -54,38 +51,12 @@ ReturnValue_t TmStore::passPacket(PusTmReader& reader) { ReturnValue_t TmStore::storePacket(PusTmReader& reader) { using namespace std::filesystem; - std::string currentPrefix = sdcMan.getCurrentMountPrefix(); - path baseDir = path(currentPrefix) / baseName; - // It is assumed here that the filesystem is usable. - if (not exists(baseDir)) { - create_directory(baseDir); + if (baseDirUninitialized) { + updateBaseDir(); } + // It is assumed here that the filesystem is usable. if (not mostRecentFile) { - for (auto const& file : directory_iterator(baseDir)) { - if (file.is_directory()) { - continue; - } - auto pathStr = file.path().string(); - Clock::TimeOfDay_t tod; - if (pathStr.find(baseName) == std::string::npos) { - continue; - } - unsigned int underscorePos = pathStr.find_last_of('_'); - std::string stampStr = pathStr.substr(underscorePos + 1); - int count = - sscanf(stampStr.c_str(), - "%4" SCNu32 "-%2" SCNu32 "-%2" SCNu32 "T%2" SCNu32 ":%2" SCNu32 ":%2" SCNu32 "Z", - &tod.year, &tod.month, &tod.day, &tod.hour, &tod.minute, &tod.second); - if (count != 6) { - continue; - } - timeval tv{}; - Clock::convertTimeOfDayToTimeval(&tod, &tv); - if (not mostRecentTv || tv > mostRecentTv.value()) { - mostRecentTv = tv; - mostRecentFile = file.path(); - } - } + assignMostRecentFile(); } // TODO: Need to find the file of the most recent file. // TODO: If file exists: Determine whether file rolls over: Maximum file size reached? Interval @@ -95,3 +66,50 @@ ReturnValue_t TmStore::storePacket(PusTmReader& reader) { } MessageQueueId_t TmStore::getCommandQueue() { return MessageQueueIF::NO_QUEUE; } + +void TmStore::calcDiffSeconds(RolloverInterval intervalUnit, uint32_t intervalCount) { + if (intervalUnit == RolloverInterval::HOURLY) { + rolloverDiffSeconds = 60 * intervalCount; + } else if (intervalUnit == RolloverInterval::DAILY) { + rolloverDiffSeconds = 60 * 24 * intervalCount; + } +} + +void TmStore::updateBaseDir() { + using namespace std::filesystem; + std::string currentPrefix = sdcMan.getCurrentMountPrefix(); + baseDir = path(currentPrefix) / baseName; + if (not exists(baseDir)) { + create_directory(baseDir); + } + baseDirUninitialized = false; +} + +void TmStore::assignMostRecentFile() { + using namespace std::filesystem; + for (auto const& file : directory_iterator(baseDir)) { + if (file.is_directory()) { + continue; + } + auto pathStr = file.path().string(); + Clock::TimeOfDay_t tod; + if (pathStr.find(baseName) == std::string::npos) { + continue; + } + unsigned int underscorePos = pathStr.find_last_of('_'); + std::string stampStr = pathStr.substr(underscorePos + 1); + int count = + sscanf(stampStr.c_str(), + "%4" SCNu32 "-%2" SCNu32 "-%2" SCNu32 "T%2" SCNu32 ":%2" SCNu32 ":%2" SCNu32 "Z", + &tod.year, &tod.month, &tod.day, &tod.hour, &tod.minute, &tod.second); + if (count != 6) { + continue; + } + timeval tv{}; + Clock::convertTimeOfDayToTimeval(&tod, &tv); + if (not mostRecentTv || tv > mostRecentTv.value()) { + mostRecentTv = tv; + mostRecentFile = file.path(); + } + } +} diff --git a/mission/tmtc/TmStore.h b/mission/tmtc/TmStore.h index a2638d2b..f26461e7 100644 --- a/mission/tmtc/TmStore.h +++ b/mission/tmtc/TmStore.h @@ -19,8 +19,8 @@ enum class RolloverInterval { HOURLY, DAILY }; class TmStore : public SystemObject { public: - TmStore(object_id_t objectId, std::string baseName, RolloverInterval interval, - uint8_t intervalFactor, PacketFilter filter, SdCardMountedIF& sdcMan); + TmStore(object_id_t objectId, std::string baseName, RolloverInterval intervalUnit, + uint32_t intervalCount, PacketFilter filter, SdCardMountedIF& sdcMan); ReturnValue_t passPacket(PusTmReader& reader); ReturnValue_t storePacket(PusTmReader& reader); @@ -34,14 +34,19 @@ class TmStore : public SystemObject { */ MessageQueueId_t getCommandQueue(); PacketFilter filter; + bool baseDirUninitialized = true; std::string baseName; - RolloverInterval interval; - uint8_t intervalFactor; + std::filesystem::path baseDir; + uint32_t rolloverDiffSeconds = 0; char NAME_BUF[524] = {}; std::array fileBuf{}; std::optional mostRecentTv; std::optional mostRecentFile; SdCardMountedIF& sdcMan; + + void calcDiffSeconds(RolloverInterval intervalUnit, uint32_t intervalCount); + void updateBaseDir(); + void assignMostRecentFile(); }; #endif /* MISSION_TMTC_TMSTOREBACKEND_H_ */