Robin Mueller
58d6b59b7c
All checks were successful
EIVE/eive-obsw/pipeline/pr-develop This commit looks good
183 lines
6.1 KiB
C++
183 lines
6.1 KiB
C++
#include "TmStore.h"
|
|
|
|
#include <mission/memory/SdCardMountedIF.h>
|
|
|
|
#include <algorithm>
|
|
#include <cinttypes>
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <utility>
|
|
|
|
using namespace returnvalue;
|
|
|
|
TmStore::TmStore(object_id_t objectId, const char* baseDir, std::string baseName,
|
|
RolloverInterval intervalUnit, uint32_t intervalCount, timeval& currentTv,
|
|
SdCardMountedIF& sdcMan)
|
|
: SystemObject(objectId),
|
|
baseDir(std::move(baseDir)),
|
|
baseName(std::move(baseName)),
|
|
currentTv(currentTv),
|
|
sdcMan(sdcMan) {
|
|
calcDiffSeconds(intervalUnit, intervalCount);
|
|
}
|
|
|
|
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;
|
|
if (baseDirUninitialized) {
|
|
updateBaseDir();
|
|
}
|
|
// It is assumed here that the filesystem is usable.
|
|
if (not mostRecentFile) {
|
|
assignAndOrCreateMostRecentFile();
|
|
}
|
|
|
|
if (currentTv.tv_sec < mostRecentTv.value().tv_sec or
|
|
currentTv.tv_sec - mostRecentTv.value().tv_sec > static_cast<int>(rolloverDiffSeconds)) {
|
|
if (file_size(mostRecentFile.value()) + reader.getFullPacketLen() > fileBuf.size()) {
|
|
uint8_t appendedCounter = 1;
|
|
path rolloverName;
|
|
while (true) {
|
|
rolloverName = path(mostRecentFile.value().string() + std::to_string(appendedCounter));
|
|
if (not exists(rolloverName)) {
|
|
break;
|
|
}
|
|
appendedCounter++;
|
|
}
|
|
rename(mostRecentFile.value(), rolloverName);
|
|
std::ofstream of(mostRecentFile.value(), std::ios::binary);
|
|
}
|
|
}
|
|
|
|
// Rollover conditions were handled, write to file now
|
|
std::ofstream of(mostRecentFile.value(), std::ios::app | std::ios::binary);
|
|
of.write(reinterpret_cast<const char*>(reader.getFullData()), reader.getFullPacketLen());
|
|
return returnvalue::OK;
|
|
}
|
|
|
|
MessageQueueId_t TmStore::getCommandQueue() { return MessageQueueIF::NO_QUEUE; }
|
|
|
|
void TmStore::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;
|
|
}
|
|
}
|
|
|
|
void TmStore::updateBaseDir() {
|
|
using namespace std::filesystem;
|
|
std::string currentPrefix = sdcMan.getCurrentMountPrefix();
|
|
basePath = path(currentPrefix) / baseDir / baseName;
|
|
if (not exists(basePath)) {
|
|
create_directories(basePath);
|
|
}
|
|
baseDirUninitialized = false;
|
|
}
|
|
|
|
void TmStore::assignAndOrCreateMostRecentFile() {
|
|
using namespace std::filesystem;
|
|
for (auto const& file : directory_iterator(basePath)) {
|
|
if (file.is_directory()) {
|
|
continue;
|
|
}
|
|
auto pathStr = file.path().string();
|
|
Clock::TimeOfDay_t tod;
|
|
if (pathStr.find(baseName) == std::string::npos) {
|
|
continue;
|
|
}
|
|
unsigned int underscorePos = pathStr.find_last_of('_');
|
|
std::string stampStr = pathStr.substr(underscorePos + 1);
|
|
int count = sscanf(stampStr.c_str(),
|
|
"%04" SCNu32 "-%02" SCNu32 "-%02" SCNu32 "T%02" SCNu32 "-%02" SCNu32
|
|
"-%02" SCNu32 "Z",
|
|
&tod.year, &tod.month, &tod.day, &tod.hour, &tod.minute, &tod.second);
|
|
if (count != 6) {
|
|
continue;
|
|
}
|
|
timeval tv{};
|
|
Clock::convertTimeOfDayToTimeval(&tod, &tv);
|
|
if (not mostRecentTv || tv > mostRecentTv.value()) {
|
|
mostRecentTv = tv;
|
|
mostRecentFile = file.path();
|
|
}
|
|
}
|
|
if (not mostRecentFile) {
|
|
unsigned currentIdx = 0;
|
|
path pathStart = basePath / baseName;
|
|
memcpy(fileBuf.data() + currentIdx, pathStart.c_str(), pathStart.string().length());
|
|
currentIdx += pathStart.string().length();
|
|
Clock::TimeOfDay_t tod;
|
|
Clock::convertTimevalToTimeOfDay(¤tTv, &tod);
|
|
currentIdx += sprintf(reinterpret_cast<char*>(fileBuf.data() + currentIdx),
|
|
"_%04" PRIu32 "-%02" PRIu32 "-%02" PRIu32 "T%02" PRIu32 "-%02" PRIu32
|
|
"-%02" PRIu32 "Z.bin",
|
|
tod.year, tod.month, tod.day, tod.hour, tod.minute, tod.second);
|
|
path newPath(std::string(reinterpret_cast<const char*>(fileBuf.data()), currentIdx));
|
|
std::ofstream of(newPath, std::ios::binary);
|
|
mostRecentFile = newPath;
|
|
mostRecentTv = currentTv;
|
|
}
|
|
}
|
|
|
|
void TmStore::addApid(uint16_t apid) {
|
|
if (not filter.apid) {
|
|
filter.apid = std::vector<uint16_t>({apid});
|
|
return;
|
|
}
|
|
filter.apid.value().push_back(apid);
|
|
}
|
|
|
|
void TmStore::addService(uint8_t service) {
|
|
if (not filter.services) {
|
|
filter.services = std::vector<uint8_t>({service});
|
|
return;
|
|
}
|
|
filter.services.value().push_back(service);
|
|
}
|
|
|
|
void TmStore::addServiceSubservice(uint8_t service, uint8_t subservice) {
|
|
if (not filter.serviceSubservices) {
|
|
filter.serviceSubservices =
|
|
std::vector<std::pair<uint8_t, uint8_t>>({std::pair(service, subservice)});
|
|
return;
|
|
}
|
|
filter.serviceSubservices.value().push_back({service, subservice});
|
|
}
|