#ifndef MISSION_TMTC_TMSTOREBACKEND_H_ #define MISSION_TMTC_TMSTOREBACKEND_H_ #include #include #include #include #include #include #include #include "TmFunnelBase.h" #include "eive/eventSubsystemIds.h" enum class RolloverInterval { MINUTELY, HOURLY, DAILY }; class PersistentTmStore : public TmStoreFrontendSimpleIF, public SystemObject { public: enum class State { IDLE, DUMPING }; static constexpr uint8_t SUBSYSTEM_ID = SUBSYSTEM_ID::PERSISTENT_TM_STORE; //! [EXPORT] : [COMMENT] //! P1: Result code of TM packet parser. //! P2: Timestamp of possibly corrupt file as a unix timestamp. static constexpr Event POSSIBLE_FILE_CORRUPTION = event::makeEvent(SUBSYSTEM_ID, 0, severity::LOW); PersistentTmStore(object_id_t objectId, const char* baseDir, std::string baseName, RolloverInterval intervalUnit, uint32_t intervalCount, StorageManagerIF& tmStore, SdCardMountedIF& sdcMan); ReturnValue_t initializeTmStore(); ReturnValue_t handleCommandQueue(StorageManagerIF& ipcStore, TmFunnelBase& tmFunnel); void deleteUpTo(uint32_t unixSeconds); ReturnValue_t startDumpFrom(uint32_t fromUnixSeconds); ReturnValue_t startDumpFromUpTo(uint32_t fromUnixSeconds, uint32_t upToUnixSeconds); ReturnValue_t dumpNextPacket(size_t& dumpedLen); // ReturnValue_t passPacket(PusTmReader& reader); ReturnValue_t storePacket(PusTmReader& reader); private: static constexpr uint8_t MAX_FILES_IN_ONE_SECOND = 10; static constexpr size_t MAX_FILESIZE = 8192; // ISO8601 timestamp. static constexpr char FILE_DATE_FORMAT[] = "%FT%H%M%SZ"; MessageQueueIF* tcQueue; State state = State::IDLE; // PacketFilter filter; CdsShortTimeStamper timeReader; bool baseDirUninitialized = true; const char* baseDir; std::string baseName; uint8_t currentSameSecNumber = 0; std::filesystem::path basePath; uint32_t rolloverDiffSeconds = 0; std::array fileBuf{}; timeval currentTv; timeval activeFileTv{}; std::filesystem::directory_iterator activeDumpDirIter; std::filesystem::path activeDumpFile; size_t activeDumpFileSize = 0; size_t activeDumpCurrentSize = 0; std::optional activeFile; SdCardMountedIF& sdcMan; StorageManagerIF& tmStore; /** * To get the queue where commands shall be sent. * @return Id of command queue. */ [[nodiscard]] MessageQueueId_t getCommandQueue() const override; void calcDiffSeconds(RolloverInterval intervalUnit, uint32_t intervalCount); ReturnValue_t createMostRecentFile(std::optional suffix); static ReturnValue_t pathToTime(const std::filesystem::path& path, struct tm& time); void fileToPackets(const std::filesystem::path& path, uint32_t unixStamp); bool updateBaseDir(); ReturnValue_t assignAndOrCreateMostRecentFile(); }; #endif /* MISSION_TMTC_TMSTOREBACKEND_H_ */