eive-obsw/mission/tmtc/PersistentTmStore.h

155 lines
5.9 KiB
C
Raw Normal View History

2022-10-24 10:57:30 +02:00
#ifndef MISSION_TMTC_TMSTOREBACKEND_H_
#define MISSION_TMTC_TMSTOREBACKEND_H_
2023-03-10 20:32:11 +01:00
#include <fsfw/ipc/CommandMessageIF.h>
2022-10-25 18:20:21 +02:00
#include <fsfw/objectmanager/SystemObject.h>
2023-03-09 17:44:05 +01:00
#include <fsfw/storagemanager/StorageManagerIF.h>
2023-02-21 20:43:16 +01:00
#include <fsfw/tmstorage/TmStoreFrontendSimpleIF.h>
2022-10-25 18:20:21 +02:00
#include <fsfw/tmtcpacket/pus/tm/PusTmReader.h>
2022-10-24 10:57:30 +02:00
#include <fsfw/tmtcservices/AcceptsTelemetryIF.h>
2022-11-11 15:39:27 +01:00
#include <mission/memory/SdCardMountedIF.h>
2022-10-24 10:57:30 +02:00
2022-12-12 18:17:59 +01:00
#include <filesystem>
2023-06-23 14:55:51 +02:00
#include <set>
2022-12-12 18:17:59 +01:00
2023-02-07 12:19:13 +01:00
#include "eive/eventSubsystemIds.h"
2023-03-09 13:15:42 +01:00
#include "eive/resultClassIds.h"
2023-11-15 15:27:12 +01:00
#include "fsfw/ipc/CommandMessage.h"
2022-12-19 14:40:27 +01:00
2022-12-13 15:34:13 +01:00
enum class RolloverInterval { MINUTELY, HOURLY, DAILY };
2022-12-12 10:06:30 +01:00
2023-03-09 19:42:20 +01:00
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;
};
2023-06-23 14:55:51 +02:00
struct DumpIndex {
2023-06-24 20:57:54 +02:00
uint32_t epoch = 0;
2023-06-24 11:46:56 +02:00
// Number of additional files with a suffix like .0, .1 etc.
2023-06-24 20:57:54 +02:00
uint8_t additionalFiles = 0;
2023-06-23 14:55:51 +02:00
// Define a custom comparison function based on the epoch variable
bool operator<(const DumpIndex& other) const { return epoch < other.epoch; }
};
2023-02-21 21:37:30 +01:00
class PersistentTmStore : public TmStoreFrontendSimpleIF, public SystemObject {
2022-10-24 10:57:30 +02:00
public:
2023-03-09 12:20:13 +01:00
enum class State { IDLE, DUMPING };
2023-03-09 13:15:42 +01:00
static constexpr uint8_t INTERFACE_ID = CLASS_ID::PERSISTENT_TM_STORE;
static constexpr ReturnValue_t DUMP_DONE = returnvalue::makeCode(INTERFACE_ID, 0);
2023-03-09 17:44:05 +01:00
static constexpr ReturnValue_t BUSY_DUMPING = returnvalue::makeCode(INTERFACE_ID, 1);
2023-03-09 13:15:42 +01:00
2023-03-09 19:42:20 +01:00
PersistentTmStore(PersistentTmStoreArgs args);
2022-12-13 14:19:43 +01:00
2023-02-22 13:27:16 +01:00
ReturnValue_t initializeTmStore();
2023-03-09 17:44:05 +01:00
State getState() const;
2023-03-10 20:32:11 +01:00
ReturnValue_t handleCommandQueue(StorageManagerIF& ipcStore, Command_t& execCmd);
2023-02-21 20:43:16 +01:00
2022-12-19 13:25:45 +01:00
void deleteUpTo(uint32_t unixSeconds);
2023-11-15 15:21:00 +01:00
void deleteFromUpTo(uint32_t startUnixTime, uint32_t endUnixTime);
2023-03-09 12:20:13 +01:00
ReturnValue_t startDumpFrom(uint32_t fromUnixSeconds);
ReturnValue_t startDumpFromUpTo(uint32_t fromUnixSeconds, uint32_t upToUnixSeconds);
2023-03-10 19:01:31 +01:00
/**
*
2023-03-31 13:17:15 +02:00
* @param tmReader: Next packet will be loaded into the PUS TM reader. A CRC check will be
* performed on the packet. If that check fails, the file is considered corrupted and will
* be deleted for now.
* @param fileHasSwapped: If the CRC check fails, the file will be deleted and a new one has to
* be loaded. The dump can reach completion during that process. If a file is swapped, this
* boolean is set to true
* @return DUMP_DONE if dump is finished, returnvalue::OK if the next packet was loaded into the
* TM reader, and the returnvalue of the file swap operation if the CRC check failed and
* a new file was loaded.
2023-03-10 19:01:31 +01:00
*/
2023-03-31 13:17:15 +02:00
ReturnValue_t getNextDumpPacket(PusTmReader& tmReader, bool& fileHasSwapped);
/**
* Confirm the dump to advance the dump state machine.
* @param tmReader
* @param fileHasSwapped: If the confirmed dumps completes the current file, a new file will
* be loaded and this parameter will be set to true.
* @return If a file is swapped, the retrunvalue of the file swap operation.
*/
ReturnValue_t confirmDump(const PusTmReader& tmReader, bool& fileHasSwapped);
2022-12-19 13:25:45 +01:00
2023-03-10 19:01:31 +01:00
void getStartAndEndTimeCurrentOrLastDump(uint32_t& startTime, uint32_t& endTime) const;
ReturnValue_t storePacket(PusTmReader& reader);
2023-03-10 20:32:11 +01:00
ReturnValue_t cancelDump();
2022-10-24 10:57:30 +02:00
2023-03-09 19:42:20 +01:00
protected:
StorageManagerIF& tmStore;
2022-10-24 10:57:30 +02:00
private:
2023-02-24 18:10:43 +01:00
static constexpr uint8_t MAX_FILES_IN_ONE_SECOND = 10;
2022-12-12 10:06:30 +01:00
static constexpr size_t MAX_FILESIZE = 8192;
2023-03-09 13:15:42 +01:00
//! [EXPORT] : [SKIP]
static constexpr ReturnValue_t INVALID_FILE_DETECTED_AND_DELETED = returnvalue::makeCode(2, 1);
2023-02-21 20:43:16 +01:00
MessageQueueIF* tcQueue;
2023-03-09 12:20:13 +01:00
State state = State::IDLE;
2022-12-12 18:27:01 +01:00
bool baseDirUninitialized = true;
2022-12-14 10:35:30 +01:00
const char* baseDir;
2022-12-12 10:06:30 +01:00
std::string baseName;
2023-02-24 18:10:43 +01:00
uint8_t currentSameSecNumber = 0;
2022-12-14 10:34:23 +01:00
std::filesystem::path basePath;
2023-06-23 14:55:51 +02:00
// std::filesystem::path pathStart = basePath / baseName;
2022-12-12 18:27:01 +01:00
uint32_t rolloverDiffSeconds = 0;
2023-06-23 14:55:51 +02:00
std::array<uint8_t, 524> filePathBuf{};
size_t basePathSize;
2022-12-13 15:34:13 +01:00
std::array<uint8_t, MAX_FILESIZE> fileBuf{};
2023-02-24 18:10:43 +01:00
timeval currentTv;
2023-02-22 13:27:16 +01:00
timeval activeFileTv{};
2023-03-09 13:15:42 +01:00
struct ActiveDumpParams {
2023-03-31 13:17:15 +02:00
bool pendingPacketDump = false;
2023-03-09 13:15:42 +01:00
uint32_t fromUnixTime = 0;
uint32_t untilUnixTime = 0;
uint32_t currentFileUnixStamp = 0;
2023-06-23 16:05:38 +02:00
std::filesystem::path currentFile;
2023-06-23 14:55:51 +02:00
std::set<DumpIndex> orderedDumpFilestamps{};
std::set<DumpIndex>::iterator dumpIter;
2023-06-24 11:46:56 +02:00
std::optional<uint8_t> currentSameFileIdx = 0;
2023-03-09 13:15:42 +01:00
size_t fileSize = 0;
size_t currentSize = 0;
};
ActiveDumpParams dumpParams;
2023-02-22 13:27:16 +01:00
std::optional<std::filesystem::path> activeFile;
2022-10-25 18:20:21 +02:00
SdCardMountedIF& sdcMan;
2022-12-12 18:27:01 +01:00
2023-02-21 20:43:16 +01:00
/**
* To get the queue where commands shall be sent.
* @return Id of command queue.
*/
[[nodiscard]] MessageQueueId_t getCommandQueue() const override;
2022-12-12 18:27:01 +01:00
void calcDiffSeconds(RolloverInterval intervalUnit, uint32_t intervalCount);
2023-06-23 14:55:51 +02:00
ReturnValue_t buildDumpSet(uint32_t fromUnixSeconds, uint32_t upToUnixSeconds);
2023-02-24 18:10:43 +01:00
ReturnValue_t createMostRecentFile(std::optional<uint8_t> suffix);
2023-03-09 12:20:13 +01:00
static ReturnValue_t pathToTime(const std::filesystem::path& path, struct tm& time);
2023-03-09 11:46:13 +01:00
void fileToPackets(const std::filesystem::path& path, uint32_t unixStamp);
2023-03-09 13:15:42 +01:00
ReturnValue_t loadNextDumpFile();
2023-06-23 14:55:51 +02:00
ReturnValue_t createFileName(timeval& tv, std::optional<uint8_t> suffix, size_t& fullPathLength);
2023-06-23 17:07:07 +02:00
std::optional<uint8_t> extractSuffix(const std::string& pathStr);
2023-02-22 18:06:34 +01:00
bool updateBaseDir();
2023-02-22 13:27:16 +01:00
ReturnValue_t assignAndOrCreateMostRecentFile();
2023-11-15 15:27:12 +01:00
ReturnValue_t handleDeletionCmd(StorageManagerIF& ipcStore, CommandMessage& cmdMessage);
ReturnValue_t handleDumpCmd(StorageManagerIF& ipcStore, CommandMessage& cmdMessage);
2022-10-24 10:57:30 +02:00
};
#endif /* MISSION_TMTC_TMSTOREBACKEND_H_ */