eive-obsw/mission/tmtc/PersistentTmStore.cpp
Robin Mueller 9023405b96
All checks were successful
EIVE/eive-obsw/pipeline/head This commit looks good
some fxes
2023-06-23 16:05:38 +02:00

414 lines
15 KiB
C++

#include "PersistentTmStore.h"
#include <mission/memory/SdCardMountedIF.h>
#include <mission/tmtc/DirectTmSinkIF.h>
#include <algorithm>
#include <cinttypes>
#include <filesystem>
#include <fstream>
#include <utility>
#include "eive/definitions.h"
#include "fsfw/ipc/CommandMessage.h"
#include "fsfw/ipc/QueueFactory.h"
#include "fsfw/tmstorage/TmStoreMessage.h"
#include "mission/persistentTmStoreDefs.h"
using namespace returnvalue;
PersistentTmStore::PersistentTmStore(PersistentTmStoreArgs args)
: SystemObject(args.objectId),
tmStore(args.tmStore),
baseDir(args.baseDir),
baseName(std::move(args.baseName)),
sdcMan(args.sdcMan) {
tcQueue = QueueFactory::instance()->createMessageQueue();
calcDiffSeconds(args.intervalUnit, args.intervalCount);
}
ReturnValue_t PersistentTmStore::cancelDump() {
state = State::IDLE;
return returnvalue::OK;
}
ReturnValue_t PersistentTmStore::buildDumpSet(uint32_t fromUnixSeconds, uint32_t upToUnixSeconds) {
using namespace std::filesystem;
std::error_code e;
dumpParams.orderedDumpFilestamps.clear();
for (auto const& fileOrDir : directory_iterator(basePath)) {
if (not fileOrDir.is_regular_file(e)) {
continue;
}
dumpParams.fileSize = std::filesystem::file_size(fileOrDir.path(), e);
if (e) {
sif::error << "PersistentTmStore: Could not retrieve file size: " << e.message() << std::endl;
continue;
}
// sif::debug << "Path: " << dumpParams.dirEntry.path() << std::endl;
// File empty or can't even read CCSDS header.
if (dumpParams.fileSize <= 6) {
continue;
}
if (dumpParams.fileSize > fileBuf.size()) {
sif::error << "PersistentTmStore: File too large, is deleted" << std::endl;
triggerEvent(persTmStore::FILE_TOO_LARGE, dumpParams.fileSize, fileBuf.size());
std::filesystem::remove(fileOrDir.path(), e);
continue;
}
const path& file = fileOrDir.path();
struct tm fileTime {};
if (pathToTime(file, fileTime) != returnvalue::OK) {
sif::error << "Time extraction for file " << file << "failed" << std::endl;
continue;
}
auto fileEpoch = static_cast<uint32_t>(timegm(&fileTime));
if ((fileEpoch > dumpParams.fromUnixTime) and
(fileEpoch + rolloverDiffSeconds <= dumpParams.untilUnixTime)) {
std::ifstream ifile(file, std::ios::binary);
if (ifile.bad()) {
sif::error << "PersistentTmStore: File is bad" << std::endl;
// TODO: Consider deleting file here?
continue;
}
DumpIndex dumpIndex;
dumpIndex.epoch = fileEpoch;
dumpIndex.suffixIdx = extractSuffix(file.string());
dumpParams.orderedDumpFilestamps.emplace(dumpIndex);
return returnvalue::OK;
}
}
return returnvalue::OK;
}
uint8_t PersistentTmStore::extractSuffix(const std::string& pathStr) {
std::string numberStr;
// Find the position of the dot at the end of the file path
size_t dotPos = pathStr.find_last_of('.');
if (dotPos != std::string::npos && dotPos < pathStr.length() - 1) {
// Extract the substring after the dot
numberStr = pathStr.substr(dotPos + 1);
}
int number = std::stoi(numberStr);
if (number < 0 or number > std::numeric_limits<uint8_t>::max()) {
return 0;
}
return static_cast<uint8_t>(number);
}
ReturnValue_t PersistentTmStore::assignAndOrCreateMostRecentFile() {
if (not activeFile.has_value()) {
return createMostRecentFile(std::nullopt);
}
return returnvalue::OK;
}
ReturnValue_t PersistentTmStore::handleCommandQueue(StorageManagerIF& ipcStore,
Command_t& execCmd) {
CommandMessage cmdMessage;
ReturnValue_t result = tcQueue->receiveMessage(&cmdMessage);
if (result != returnvalue::OK) {
return result;
}
if (cmdMessage.getMessageType() == messagetypes::TM_STORE) {
Command_t cmd = cmdMessage.getCommand();
if (cmd == TmStoreMessage::DELETE_STORE_CONTENT_TIME) {
Clock::getClock_timeval(&currentTv);
store_address_t storeId = TmStoreMessage::getStoreId(&cmdMessage);
auto accessor = ipcStore.getData(storeId);
uint32_t deleteUpToUnixSeconds = 0;
size_t size = accessor.second.size();
SerializeAdapter::deSerialize(&deleteUpToUnixSeconds, accessor.second.data(), &size,
SerializeIF::Endianness::NETWORK);
execCmd = cmd;
deleteUpTo(deleteUpToUnixSeconds);
} else if (cmd == TmStoreMessage::DOWNLINK_STORE_CONTENT_TIME) {
Clock::getClock_timeval(&currentTv);
store_address_t storeId = TmStoreMessage::getStoreId(&cmdMessage);
auto accessor = ipcStore.getData(storeId);
if (accessor.second.size() < 8) {
return returnvalue::FAILED;
}
uint32_t dumpFromUnixSeconds = 0;
uint32_t dumpUntilUnixSeconds = 0;
size_t size = 8;
SerializeAdapter::deSerialize(&dumpFromUnixSeconds, accessor.second.data(), &size,
SerializeIF::Endianness::NETWORK);
SerializeAdapter::deSerialize(&dumpUntilUnixSeconds, accessor.second.data() + 4, &size,
SerializeIF::Endianness::NETWORK);
result = startDumpFromUpTo(dumpFromUnixSeconds, dumpUntilUnixSeconds);
if (result == BUSY_DUMPING) {
triggerEvent(persTmStore::BUSY_DUMPING_EVENT);
} else {
execCmd = cmd;
}
}
}
return result;
}
ReturnValue_t PersistentTmStore::startDumpFrom(uint32_t fromUnixSeconds) {
return startDumpFromUpTo(fromUnixSeconds, currentTv.tv_sec);
}
ReturnValue_t PersistentTmStore::storePacket(PusTmReader& reader) {
using namespace std::filesystem;
if (baseDirUninitialized) {
updateBaseDir();
}
Clock::getClock_timeval(&currentTv);
// It is assumed here that the filesystem is usable.
if (not activeFile.has_value()) {
ReturnValue_t result = assignAndOrCreateMostRecentFile();
if (result != returnvalue::OK) {
return result;
}
}
bool createNewFile = false;
std::optional<uint8_t> suffix = std::nullopt;
std::error_code e;
size_t fileSize = file_size(activeFile.value(), e);
if (e) {
sif::error << "PersistentTmStore: Could not retrieve file size, "
"error "
<< e.message() << std::endl;
return returnvalue::FAILED;
}
if (currentTv.tv_sec > activeFileTv.tv_sec + static_cast<int>(rolloverDiffSeconds)) {
createNewFile = true;
currentSameSecNumber = 0;
} else if (fileSize + reader.getFullPacketLen() > fileBuf.size()) {
createNewFile = true;
if (currentSameSecNumber >= MAX_FILES_IN_ONE_SECOND) {
currentSameSecNumber = 0;
}
if (currentTv.tv_sec == activeFileTv.tv_sec) {
suffix = currentSameSecNumber++;
} else {
currentSameSecNumber = 0;
}
}
if (createNewFile) {
createMostRecentFile(suffix);
}
// Rollover conditions were handled, write to file now
std::ofstream of(activeFile.value(), std::ios::app | std::ios::binary);
of.write(reinterpret_cast<const char*>(reader.getFullData()),
static_cast<std::streamsize>(reader.getFullPacketLen()));
return returnvalue::OK;
}
MessageQueueId_t PersistentTmStore::getCommandQueue() const { return tcQueue->getId(); }
void PersistentTmStore::calcDiffSeconds(RolloverInterval intervalUnit, uint32_t intervalCount) {
if (intervalUnit == RolloverInterval::MINUTELY) {
rolloverDiffSeconds = 60 * intervalCount;
} else if (intervalUnit == RolloverInterval::HOURLY) {
rolloverDiffSeconds = 60 * 60 * intervalCount;
} else if (intervalUnit == RolloverInterval::DAILY) {
rolloverDiffSeconds = 60 * 60 * 24 * intervalCount;
}
}
bool PersistentTmStore::updateBaseDir() {
using namespace std::filesystem;
const char* currentPrefix = sdcMan.getCurrentMountPrefix();
if (currentPrefix == nullptr) {
return false;
}
basePath = path(currentPrefix) / baseDir / baseName;
std::error_code e;
if (not exists(basePath, e)) {
create_directories(basePath);
}
// Each file will have the base name as a prefix again
path preparedFullFilePath = basePath / baseName;
basePathSize = preparedFullFilePath.string().length();
std::memcpy(filePathBuf.data(), preparedFullFilePath.c_str(), basePathSize);
baseDirUninitialized = false;
return true;
}
void PersistentTmStore::deleteUpTo(uint32_t unixSeconds) {
using namespace std::filesystem;
for (auto const& file : directory_iterator(basePath)) {
if (file.is_directory() or (activeFile.has_value() and (activeFile.value() == file.path()))) {
continue;
}
// Convert file time to the UNIX epoch
struct tm fileTime {};
if (pathToTime(file.path(), fileTime) != returnvalue::OK) {
sif::error << "Time extraction for " << file << " failed" << std::endl;
continue;
}
time_t fileEpoch = timegm(&fileTime);
if (fileEpoch + rolloverDiffSeconds < unixSeconds) {
std::error_code e;
std::filesystem::remove(file.path(), e);
}
}
}
ReturnValue_t PersistentTmStore::startDumpFromUpTo(uint32_t fromUnixSeconds,
uint32_t upToUnixSeconds) {
using namespace std::filesystem;
if (state == State::DUMPING) {
return returnvalue::FAILED;
}
auto dirIter = directory_iterator(basePath);
// Directory empty case.
if (dirIter == directory_iterator()) {
return returnvalue::FAILED;
}
dumpParams.fromUnixTime = fromUnixSeconds;
dumpParams.untilUnixTime = upToUnixSeconds;
buildDumpSet(fromUnixSeconds, upToUnixSeconds);
// No files in time window found.
if (dumpParams.orderedDumpFilestamps.empty()) {
return DUMP_DONE;
}
dumpParams.dumpIter = dumpParams.orderedDumpFilestamps.begin();
state = State::DUMPING;
return loadNextDumpFile();
}
ReturnValue_t PersistentTmStore::loadNextDumpFile() {
using namespace std::filesystem;
dumpParams.currentSize = 0;
std::error_code e;
for (; dumpParams.dumpIter != dumpParams.orderedDumpFilestamps.end(); dumpParams.dumpIter++) {
DumpIndex dumpIndex = *dumpParams.dumpIter;
timeval tv{};
tv.tv_sec = dumpIndex.epoch;
size_t fullPathLength = 0;
createFileName(tv, dumpIndex.suffixIdx, fullPathLength);
dumpParams.currentFile =
path(std::string(reinterpret_cast<const char*>(filePathBuf.data()), fullPathLength));
std::ifstream ifile(dumpParams.currentFile, std::ios::binary);
if (ifile.bad()) {
sif::error << "PersistentTmStore: File is bad" << std::endl;
continue;
}
sif::debug << baseName << " dump: Loading " << dumpParams.currentFile << std::endl;
ifile.read(reinterpret_cast<char*>(fileBuf.data()),
static_cast<std::streamsize>(dumpParams.fileSize));
// Increment iterator for next cycle.
dumpParams.dumpIter++;
return returnvalue::OK;
}
// Directory iterator was consumed and we are done.
state = State::IDLE;
return DUMP_DONE;
}
ReturnValue_t PersistentTmStore::getNextDumpPacket(PusTmReader& reader, bool& fileHasSwapped) {
if (state == State::IDLE or dumpParams.pendingPacketDump) {
return returnvalue::FAILED;
}
fileHasSwapped = false;
reader.setReadOnlyData(fileBuf.data() + dumpParams.currentSize,
fileBuf.size() - dumpParams.currentSize);
// CRC check to fully ensure this is a valid TM
ReturnValue_t result = reader.parseDataWithCrcCheck();
if (result != returnvalue::OK) {
sif::error << "PersistentTmStore: Parsing of PUS TM failed with code " << result << std::endl;
triggerEvent(persTmStore::POSSIBLE_FILE_CORRUPTION, result, dumpParams.currentFileUnixStamp);
// Delete the file and load next. Could use better algorithm to partially
// restore the file dump, but for now do not trust the file.
std::error_code e;
std::filesystem::remove(dumpParams.currentFile.c_str(), e);
if (dumpParams.currentFile == activeFile) {
activeFile == std::nullopt;
assignAndOrCreateMostRecentFile();
}
fileHasSwapped = true;
return loadNextDumpFile();
}
dumpParams.pendingPacketDump = true;
return returnvalue::OK;
}
ReturnValue_t PersistentTmStore::confirmDump(const PusTmReader& reader, bool& fileHasSwapped) {
dumpParams.pendingPacketDump = false;
dumpParams.currentSize += reader.getFullPacketLen();
if (dumpParams.currentSize >= dumpParams.fileSize) {
fileHasSwapped = true;
return loadNextDumpFile();
}
fileHasSwapped = false;
return returnvalue::OK;
}
ReturnValue_t PersistentTmStore::pathToTime(const std::filesystem::path& path, struct tm& time) {
auto pathStr = path.string();
size_t splitChar = pathStr.find('_');
auto timeOnlyStr = pathStr.substr(splitChar + 1);
if (nullptr == strptime(timeOnlyStr.c_str(), config::FILE_DATE_FORMAT, &time)) {
return returnvalue::FAILED;
}
return returnvalue::OK;
}
ReturnValue_t PersistentTmStore::createMostRecentFile(std::optional<uint8_t> suffix) {
using namespace std::filesystem;
size_t currentIdx;
createFileName(currentTv, suffix, currentIdx);
path newPath(std::string(reinterpret_cast<const char*>(filePathBuf.data()), currentIdx));
std::ofstream of(newPath, std::ios::binary);
activeFile = newPath;
activeFileTv = currentTv;
return returnvalue::OK;
}
ReturnValue_t PersistentTmStore::initializeTmStore() {
Clock::getClock_timeval(&currentTv);
updateBaseDir();
// Reset active file, base directory might have changed.
activeFile = std::nullopt;
return assignAndOrCreateMostRecentFile();
}
PersistentTmStore::State PersistentTmStore::getState() const { return state; }
void PersistentTmStore::getStartAndEndTimeCurrentOrLastDump(uint32_t& startTime,
uint32_t& endTime) const {
startTime = dumpParams.fromUnixTime;
endTime = dumpParams.untilUnixTime;
}
ReturnValue_t PersistentTmStore::createFileName(timeval& tv, std::optional<uint8_t> suffix,
size_t& fullPathLength) {
unsigned currentIdx = basePathSize;
fileBuf[currentIdx] = '_';
currentIdx += 1;
time_t epoch = tv.tv_sec;
struct tm* time = gmtime(&epoch);
size_t writtenBytes = strftime(reinterpret_cast<char*>(filePathBuf.data() + currentIdx),
filePathBuf.size(), config::FILE_DATE_FORMAT, time);
if (writtenBytes == 0) {
sif::error << "PersistentTmStore::createMostRecentFile: Could not create file timestamp"
<< std::endl;
return returnvalue::FAILED;
}
currentIdx += writtenBytes;
char* res = strcpy(reinterpret_cast<char*>(filePathBuf.data() + currentIdx), ".bin");
if (res == nullptr) {
return returnvalue::FAILED;
}
currentIdx += 4;
if (suffix.has_value()) {
std::string fullSuffix = "." + std::to_string(suffix.value());
res = strcpy(reinterpret_cast<char*>(filePathBuf.data() + currentIdx), fullSuffix.c_str());
if (res == nullptr) {
return returnvalue::FAILED;
}
currentIdx += fullSuffix.size();
}
fullPathLength = currentIdx;
return returnvalue::OK;
}