diff --git a/mission/tmtc/TmStore.cpp b/mission/tmtc/TmStore.cpp index 5c3f00c8..d29538ed 100644 --- a/mission/tmtc/TmStore.cpp +++ b/mission/tmtc/TmStore.cpp @@ -5,6 +5,7 @@ #include #include #include +#include using namespace returnvalue; @@ -58,10 +59,18 @@ ReturnValue_t TmStore::storePacket(PusTmReader& reader) { if (not mostRecentFile) { 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 - // since last timestamp exceeds rollover interval? - // TODO: If file does not exist or rollover criteria met: Create new file with current timestamp. + + if (currentTv.tv_sec < mostRecentTv.value().tv_sec or + currentTv.tv_sec - mostRecentTv.value().tv_sec > static_cast(rolloverDiffSeconds)) { + if (file_size(mostRecentFile.value()) + reader.getFullPacketLen() > fileBuf.size()) { + // TODO: Rename old file to XYZ.1..z , create new file with the same name as old one, + // update most recent file with that name + } + } + + // Rollover conditions were handled, write to file now + std::ofstream of(mostRecentFile.value(), std::ios::app | std::ios::binary); + of.write(reinterpret_cast(reader.getFullData()), reader.getFullPacketLen()); return returnvalue::OK; } @@ -85,6 +94,8 @@ void TmStore::updateBaseDir() { baseDirUninitialized = false; } +ReturnValue_t TmStore::updateCurrentTimestamp() { return Clock::getClock_timeval(¤tTv); } + void TmStore::assignMostRecentFile() { using namespace std::filesystem; for (auto const& file : directory_iterator(baseDir)) { @@ -113,3 +124,5 @@ void TmStore::assignMostRecentFile() { } } } + +ReturnValue_t TmStore::storePacketInternal(PusTmReader& reader) { return returnvalue::OK; } diff --git a/mission/tmtc/TmStore.h b/mission/tmtc/TmStore.h index f26461e7..3151a818 100644 --- a/mission/tmtc/TmStore.h +++ b/mission/tmtc/TmStore.h @@ -22,6 +22,8 @@ class TmStore : public SystemObject { TmStore(object_id_t objectId, std::string baseName, RolloverInterval intervalUnit, uint32_t intervalCount, PacketFilter filter, SdCardMountedIF& sdcMan); + void updateBaseDir(); + ReturnValue_t updateCurrentTimestamp(); ReturnValue_t passPacket(PusTmReader& reader); ReturnValue_t storePacket(PusTmReader& reader); @@ -38,15 +40,15 @@ class TmStore : public SystemObject { std::string baseName; std::filesystem::path baseDir; uint32_t rolloverDiffSeconds = 0; - char NAME_BUF[524] = {}; std::array fileBuf{}; + timeval currentTv{}; std::optional mostRecentTv; std::optional mostRecentFile; SdCardMountedIF& sdcMan; void calcDiffSeconds(RolloverInterval intervalUnit, uint32_t intervalCount); - void updateBaseDir(); void assignMostRecentFile(); + ReturnValue_t storePacketInternal(PusTmReader& reader); }; #endif /* MISSION_TMTC_TMSTOREBACKEND_H_ */