2023-02-07 12:23:00 +01:00
|
|
|
#include "PersistentTmStore.h"
|
2022-11-11 15:39:27 +01:00
|
|
|
|
2022-10-25 18:20:21 +02:00
|
|
|
#include <mission/memory/SdCardMountedIF.h>
|
|
|
|
|
2022-11-11 15:39:27 +01:00
|
|
|
#include <algorithm>
|
2022-12-12 14:58:56 +01:00
|
|
|
#include <cinttypes>
|
2022-12-12 16:18:00 +01:00
|
|
|
#include <filesystem>
|
2022-12-12 18:42:51 +01:00
|
|
|
#include <fstream>
|
2022-12-13 15:56:40 +01:00
|
|
|
#include <utility>
|
2022-10-24 10:57:30 +02:00
|
|
|
|
|
|
|
using namespace returnvalue;
|
|
|
|
|
2022-12-14 10:35:30 +01:00
|
|
|
TmStore::TmStore(object_id_t objectId, const char* baseDir, std::string baseName,
|
2022-12-14 10:42:16 +01:00
|
|
|
RolloverInterval intervalUnit, uint32_t intervalCount, timeval& currentTv,
|
2023-02-07 12:19:13 +01:00
|
|
|
StorageManagerIF& tmStore, SdCardMountedIF& sdcMan)
|
2022-12-14 10:42:16 +01:00
|
|
|
: SystemObject(objectId),
|
2023-02-20 17:57:18 +01:00
|
|
|
baseDir(baseDir),
|
2022-12-14 10:42:16 +01:00
|
|
|
baseName(std::move(baseName)),
|
|
|
|
currentTv(currentTv),
|
2023-02-07 12:19:13 +01:00
|
|
|
sdcMan(sdcMan),
|
|
|
|
tmStore(tmStore) {
|
2022-12-12 18:27:01 +01:00
|
|
|
calcDiffSeconds(intervalUnit, intervalCount);
|
|
|
|
}
|
2022-10-24 10:57:30 +02:00
|
|
|
|
2022-10-25 18:20:21 +02:00
|
|
|
ReturnValue_t TmStore::passPacket(PusTmReader& reader) {
|
2022-11-11 15:39:27 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-25 18:20:21 +02:00
|
|
|
return returnvalue::OK;
|
2022-10-24 10:57:30 +02:00
|
|
|
}
|
|
|
|
|
2023-02-08 17:43:43 +01:00
|
|
|
void TmStore::dumpFrom(uint32_t fromUnixSeconds, TmFunnelBase& tmFunnel) {
|
2023-02-07 15:22:01 +01:00
|
|
|
return dumpFromUpTo(fromUnixSeconds, currentTv.tv_sec, tmFunnel);
|
|
|
|
}
|
|
|
|
|
2022-12-12 10:06:30 +01:00
|
|
|
ReturnValue_t TmStore::storePacket(PusTmReader& reader) {
|
|
|
|
using namespace std::filesystem;
|
2022-12-12 18:27:01 +01:00
|
|
|
if (baseDirUninitialized) {
|
|
|
|
updateBaseDir();
|
2022-12-12 10:06:30 +01:00
|
|
|
}
|
2022-12-12 18:27:01 +01:00
|
|
|
// It is assumed here that the filesystem is usable.
|
2022-12-12 10:06:30 +01:00
|
|
|
if (not mostRecentFile) {
|
2022-12-13 10:07:36 +01:00
|
|
|
assignAndOrCreateMostRecentFile();
|
2022-12-12 10:06:30 +01:00
|
|
|
}
|
2022-12-12 18:42:51 +01:00
|
|
|
|
|
|
|
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()) {
|
2022-12-13 10:07:36 +01:00
|
|
|
uint8_t appendedCounter = 1;
|
|
|
|
path rolloverName;
|
|
|
|
while (true) {
|
2022-12-16 13:26:20 +01:00
|
|
|
rolloverName =
|
|
|
|
path(mostRecentFile.value().string() + "." + std::to_string(appendedCounter));
|
2022-12-13 10:07:36 +01:00
|
|
|
if (not exists(rolloverName)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
appendedCounter++;
|
|
|
|
}
|
|
|
|
rename(mostRecentFile.value(), rolloverName);
|
|
|
|
std::ofstream of(mostRecentFile.value(), std::ios::binary);
|
2022-12-12 18:42:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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());
|
2022-12-12 10:06:30 +01:00
|
|
|
return returnvalue::OK;
|
|
|
|
}
|
2022-11-11 15:39:27 +01:00
|
|
|
|
2022-10-25 18:20:21 +02:00
|
|
|
MessageQueueId_t TmStore::getCommandQueue() { return MessageQueueIF::NO_QUEUE; }
|
2022-12-12 18:27:01 +01:00
|
|
|
|
|
|
|
void TmStore::calcDiffSeconds(RolloverInterval intervalUnit, uint32_t intervalCount) {
|
2022-12-13 15:34:13 +01:00
|
|
|
if (intervalUnit == RolloverInterval::MINUTELY) {
|
|
|
|
rolloverDiffSeconds = 60 * intervalCount;
|
|
|
|
} else if (intervalUnit == RolloverInterval::HOURLY) {
|
2022-12-13 13:46:49 +01:00
|
|
|
rolloverDiffSeconds = 60 * 60 * intervalCount;
|
2022-12-12 18:27:01 +01:00
|
|
|
} else if (intervalUnit == RolloverInterval::DAILY) {
|
2022-12-13 13:46:49 +01:00
|
|
|
rolloverDiffSeconds = 60 * 60 * 24 * intervalCount;
|
2022-12-12 18:27:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TmStore::updateBaseDir() {
|
|
|
|
using namespace std::filesystem;
|
|
|
|
std::string currentPrefix = sdcMan.getCurrentMountPrefix();
|
2022-12-14 10:34:23 +01:00
|
|
|
basePath = path(currentPrefix) / baseDir / baseName;
|
|
|
|
if (not exists(basePath)) {
|
|
|
|
create_directories(basePath);
|
2022-12-12 18:27:01 +01:00
|
|
|
}
|
|
|
|
baseDirUninitialized = false;
|
|
|
|
}
|
|
|
|
|
2022-12-13 10:07:36 +01:00
|
|
|
void TmStore::assignAndOrCreateMostRecentFile() {
|
2022-12-12 18:27:01 +01:00
|
|
|
using namespace std::filesystem;
|
2022-12-14 10:34:23 +01:00
|
|
|
for (auto const& file : directory_iterator(basePath)) {
|
2022-12-12 18:27:01 +01:00
|
|
|
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);
|
2022-12-14 10:42:16 +01:00
|
|
|
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);
|
2022-12-12 18:27:01 +01:00
|
|
|
if (count != 6) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
timeval tv{};
|
|
|
|
Clock::convertTimeOfDayToTimeval(&tod, &tv);
|
|
|
|
if (not mostRecentTv || tv > mostRecentTv.value()) {
|
|
|
|
mostRecentTv = tv;
|
|
|
|
mostRecentFile = file.path();
|
|
|
|
}
|
|
|
|
}
|
2022-12-13 10:07:36 +01:00
|
|
|
if (not mostRecentFile) {
|
|
|
|
unsigned currentIdx = 0;
|
2022-12-14 10:34:23 +01:00
|
|
|
path pathStart = basePath / baseName;
|
|
|
|
memcpy(fileBuf.data() + currentIdx, pathStart.c_str(), pathStart.string().length());
|
2022-12-14 10:42:16 +01:00
|
|
|
currentIdx += pathStart.string().length();
|
2022-12-13 10:07:36 +01:00
|
|
|
Clock::TimeOfDay_t tod;
|
|
|
|
Clock::convertTimevalToTimeOfDay(¤tTv, &tod);
|
|
|
|
currentIdx += sprintf(reinterpret_cast<char*>(fileBuf.data() + currentIdx),
|
2022-12-14 10:42:16 +01:00
|
|
|
"_%04" PRIu32 "-%02" PRIu32 "-%02" PRIu32 "T%02" PRIu32 "-%02" PRIu32
|
|
|
|
"-%02" PRIu32 "Z.bin",
|
2022-12-13 10:07:36 +01:00
|
|
|
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);
|
2022-12-13 10:08:31 +01:00
|
|
|
mostRecentFile = newPath;
|
|
|
|
mostRecentTv = currentTv;
|
2022-12-13 10:07:36 +01:00
|
|
|
}
|
2022-12-12 18:27:01 +01:00
|
|
|
}
|
2022-12-12 18:42:51 +01:00
|
|
|
|
2022-12-13 14:19:43 +01:00
|
|
|
void TmStore::addApid(uint16_t apid) {
|
|
|
|
if (not filter.apid) {
|
2022-12-13 15:56:40 +01:00
|
|
|
filter.apid = std::vector<uint16_t>({apid});
|
2022-12-13 14:19:43 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
filter.apid.value().push_back(apid);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TmStore::addService(uint8_t service) {
|
|
|
|
if (not filter.services) {
|
2022-12-13 15:56:40 +01:00
|
|
|
filter.services = std::vector<uint8_t>({service});
|
2022-12-14 10:14:53 +01:00
|
|
|
return;
|
2022-12-13 14:19:43 +01:00
|
|
|
}
|
|
|
|
filter.services.value().push_back(service);
|
|
|
|
}
|
2022-12-13 15:56:40 +01:00
|
|
|
|
|
|
|
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)});
|
2022-12-14 10:14:53 +01:00
|
|
|
return;
|
2022-12-13 15:56:40 +01:00
|
|
|
}
|
|
|
|
filter.serviceSubservices.value().push_back({service, subservice});
|
|
|
|
}
|
2022-12-19 13:25:45 +01:00
|
|
|
|
2023-02-07 12:19:13 +01:00
|
|
|
void TmStore::deleteUpTo(uint32_t unixSeconds) {
|
|
|
|
using namespace std::filesystem;
|
|
|
|
for (auto const& file : directory_iterator(basePath)) {
|
|
|
|
if (file.is_directory() or
|
|
|
|
(mostRecentFile.has_value() and (mostRecentFile.value() == file.path()))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Clock::TimeOfDay_t tod;
|
|
|
|
pathToTod(file.path(), tod);
|
|
|
|
timeval time;
|
|
|
|
ReturnValue_t result = Clock::convertTimeOfDayToTimeval(&tod, &time);
|
|
|
|
if (result != returnvalue::OK) {
|
|
|
|
sif::error << "TOD to time conversion failed for file " << file << std::endl;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (time.tv_sec + rolloverDiffSeconds < unixSeconds) {
|
|
|
|
std::filesystem::remove(file.path());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-12-19 13:25:45 +01:00
|
|
|
|
|
|
|
void TmStore::dumpFromUpTo(uint32_t fromUnixSeconds, uint32_t upToUnixSeconds,
|
2022-12-19 14:40:27 +01:00
|
|
|
TmFunnelBase& funnel) {
|
2023-02-07 12:19:13 +01:00
|
|
|
using namespace std::filesystem;
|
|
|
|
for (auto const& file : directory_iterator(basePath)) {
|
2023-02-07 15:22:01 +01:00
|
|
|
if (file.is_directory()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (mostRecentFile.has_value() and mostRecentTv.has_value() and
|
|
|
|
(file.path() == mostRecentFile.value()) and
|
|
|
|
(upToUnixSeconds < static_cast<uint32_t>(mostRecentTv.value().tv_sec))) {
|
2023-02-07 12:19:13 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Clock::TimeOfDay_t tod;
|
|
|
|
pathToTod(file.path(), tod);
|
|
|
|
timeval time;
|
|
|
|
ReturnValue_t result = Clock::convertTimeOfDayToTimeval(&tod, &time);
|
|
|
|
if (result != returnvalue::OK) {
|
|
|
|
sif::error << "TOD to time conversion failed for file " << file << std::endl;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
uint32_t timeUnsigned = static_cast<uint32_t>(time.tv_sec);
|
|
|
|
if (timeUnsigned > fromUnixSeconds && timeUnsigned + rolloverDiffSeconds < upToUnixSeconds) {
|
|
|
|
fileToPackets(file, timeUnsigned, funnel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TmStore::pathToTod(const std::filesystem::path& path, Clock::TimeOfDay_t& tod) {
|
|
|
|
auto pathStr = path.string();
|
|
|
|
size_t splitChar = pathStr.find("_");
|
|
|
|
auto timeOnlyStr = pathStr.substr(splitChar);
|
|
|
|
sscanf(timeOnlyStr.data(),
|
|
|
|
"%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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TmStore::fileToPackets(const std::filesystem::path& path, uint32_t unixStamp,
|
|
|
|
TmFunnelBase& funnel) {
|
2022-12-19 14:40:27 +01:00
|
|
|
store_address_t storeId;
|
|
|
|
TmTcMessage message;
|
2023-02-07 12:19:13 +01:00
|
|
|
size_t size = std::filesystem::file_size(path);
|
|
|
|
if (size < 6) {
|
|
|
|
// Can't even read the CCSDS header
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
std::ifstream ifile(path, std::ios::binary);
|
|
|
|
ifile.read(reinterpret_cast<char*>(fileBuf.data()), size);
|
|
|
|
size_t currentIdx = 0;
|
|
|
|
while (currentIdx < size) {
|
|
|
|
PusTmReader reader(&timeReader, fileBuf.data(), fileBuf.size());
|
|
|
|
// CRC check to fully ensure this is a valid TM
|
|
|
|
ReturnValue_t result = reader.parseDataWithCrcCheck();
|
|
|
|
if (result == returnvalue::OK) {
|
2023-02-17 17:05:39 +01:00
|
|
|
result = tmStore.addData(&storeId, fileBuf.data() + currentIdx, reader.getFullPacketLen());
|
2023-02-07 12:19:13 +01:00
|
|
|
if (result != returnvalue::OK) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
funnel.sendPacketToDestinations(storeId, message, fileBuf.data() + currentIdx,
|
|
|
|
reader.getFullPacketLen());
|
|
|
|
currentIdx += reader.getFullPacketLen();
|
|
|
|
} else {
|
|
|
|
sif::error << "Parsing of PUS TM failed with code " << result << std::endl;
|
|
|
|
triggerEvent(POSSIBLE_FILE_CORRUPTION, result, unixStamp);
|
|
|
|
// Stop for now, do not really know where to continue and we do not trust the file anymore.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-12-19 14:40:27 +01:00
|
|
|
}
|