#ifndef MISSION_TMTC_TMSTOREBACKEND_H_ #define MISSION_TMTC_TMSTOREBACKEND_H_ #include #include #include #include #include #include #include #include #include #include "eive/eventSubsystemIds.h" #include "eive/resultClassIds.h" enum class RolloverInterval { MINUTELY, HOURLY, DAILY }; struct PersistentTmStoreArgs { PersistentTmStoreArgs(object_id_t objectId, const char* baseDir, std::string baseName, RolloverInterval intervalUnit, uint32_t intervalCount, StorageManagerIF& tmStore, SdCardMountedIF& sdcMan) : objectId(objectId), baseDir(baseDir), baseName(baseName), intervalUnit(intervalUnit), intervalCount(intervalCount), tmStore(tmStore), sdcMan(sdcMan) {} object_id_t objectId; const char* baseDir; std::string baseName; RolloverInterval intervalUnit; uint32_t intervalCount; StorageManagerIF& tmStore; SdCardMountedIF& sdcMan; }; class PersistentTmStore : public TmStoreFrontendSimpleIF, public SystemObject { public: enum class State { IDLE, DUMPING }; static constexpr uint8_t INTERFACE_ID = CLASS_ID::PERSISTENT_TM_STORE; static constexpr ReturnValue_t DUMP_DONE = returnvalue::makeCode(INTERFACE_ID, 0); static constexpr ReturnValue_t BUSY_DUMPING = returnvalue::makeCode(INTERFACE_ID, 1); 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); //! [EXPORT] : [COMMENT] File in store too large. P1: Detected file size //! P2: Allowed file size static constexpr Event FILE_TOO_LARGE = event::makeEvent(SUBSYSTEM_ID, 1, severity::LOW); static constexpr Event BUSY_DUMPING_EVENT = event::makeEvent(SUBSYSTEM_ID, 2, severity::INFO); PersistentTmStore(PersistentTmStoreArgs args); ReturnValue_t initializeTmStore(); State getState() const; ReturnValue_t handleCommandQueue(StorageManagerIF& ipcStore); void deleteUpTo(uint32_t unixSeconds); ReturnValue_t startDumpFrom(uint32_t fromUnixSeconds); ReturnValue_t startDumpFromUpTo(uint32_t fromUnixSeconds, uint32_t upToUnixSeconds); ReturnValue_t dumpNextPacket(DirectTmSinkIF& tmSink, size_t& dumpedLen, bool& fileHasSwapped); ReturnValue_t storePacket(PusTmReader& reader); protected: StorageManagerIF& tmStore; 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"; //! [EXPORT] : [SKIP] static constexpr ReturnValue_t INVALID_FILE_DETECTED_AND_DELETED = returnvalue::makeCode(2, 1); 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{}; struct ActiveDumpParams { uint32_t fromUnixTime = 0; uint32_t untilUnixTime = 0; uint32_t currentFileUnixStamp = 0; std::filesystem::directory_iterator dirIter; std::filesystem::directory_entry dirEntry; size_t fileSize = 0; size_t currentSize = 0; }; ActiveDumpParams dumpParams; std::optional activeFile; SdCardMountedIF& sdcMan; /** * 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); ReturnValue_t loadNextDumpFile(); bool updateBaseDir(); ReturnValue_t assignAndOrCreateMostRecentFile(); }; #endif /* MISSION_TMTC_TMSTOREBACKEND_H_ */