eive-obsw/mission/tmtc/TmStore.cpp
Robin Mueller 279697b326
All checks were successful
EIVE/eive-obsw/pipeline/pr-develop This commit looks good
test stamp in filename
2022-12-12 16:18:00 +01:00

92 lines
3.2 KiB
C++

#include "TmStore.h"
#include <mission/memory/SdCardMountedIF.h>
#include <algorithm>
#include <cinttypes>
#include <filesystem>
using namespace returnvalue;
TmStore::TmStore(object_id_t objectId, std::string baseName, RolloverInterval interval,
uint8_t intervalFactor, PacketFilter filter, SdCardMountedIF& sdcMan)
: SystemObject(objectId),
filter(filter),
baseName(std::move(baseName)),
interval(interval),
intervalFactor(intervalFactor),
sdcMan(sdcMan) {}
ReturnValue_t TmStore::passPacket(PusTmReader& reader) {
bool inApidList = false;
if (filter.apid) {
auto& apidFilter = filter.apid.value();
if (std::find(apidFilter.begin(), apidFilter.end(), reader.getApid()) != apidFilter.end()) {
if (not filter.serviceSubservices and not filter.services) {
return storePacket(reader);
}
inApidList = true;
}
}
std::pair<uint8_t, uint8_t> serviceSubservice;
serviceSubservice.first = reader.getService();
serviceSubservice.second = reader.getSubService();
if (filter.services) {
auto& serviceFilter = filter.services.value();
if (std::find(serviceFilter.begin(), serviceFilter.end(), serviceSubservice.first) !=
serviceFilter.end()) {
if (filter.apid and inApidList) {
return storePacket(reader);
}
}
}
if (filter.serviceSubservices) {
auto& serviceSubserviceFilter = filter.serviceSubservices.value();
if (std::find(serviceSubserviceFilter.begin(), serviceSubserviceFilter.end(),
serviceSubservice) != serviceSubserviceFilter.end()) {
if (filter.apid and inApidList) {
return storePacket(reader);
}
}
}
return returnvalue::OK;
}
ReturnValue_t TmStore::storePacket(PusTmReader& reader) {
using namespace std::filesystem;
std::string currentPrefix = sdcMan.getCurrentMountPrefix();
path baseDir = path(currentPrefix) / baseName;
// It is assumed here that the filesystem is usable.
if (not exists(baseDir)) {
create_directory(baseDir);
}
if (not mostRecentFile) {
// TODO: Find most recent file by iterating through all files and remembering the file
// with the most recent timestamp.
for (auto const& file : directory_iterator(baseDir)) {
if (file.is_directory()) {
continue;
}
auto pathStr = file.path().string();
Clock::TimeOfDay_t tod;
if (pathStr.find(baseName) == std::string::npos) {
continue;
}
float seconds = 0.0;
char* prefix = nullptr;
int count = sscanf(pathStr.c_str(),
"%s_%4" SCNu32 "-%2" SCNu32 "-%2" SCNu32 "T%2" SCNu32 ":%2" SCNu32 ":%fZ",
prefix, &tod.year, &tod.month, &tod.day, &tod.hour, &tod.minute, &seconds);
tod.second = std::floor(seconds);
static_cast<void>(count);
}
}
// 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.
return returnvalue::OK;
}
MessageQueueId_t TmStore::getCommandQueue() { return MessageQueueIF::NO_QUEUE; }