2022-10-24 10:57:30 +02:00
|
|
|
#include "TmStore.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-10-24 10:57:30 +02:00
|
|
|
|
|
|
|
using namespace returnvalue;
|
|
|
|
|
2022-12-12 10:06:30 +01:00
|
|
|
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) {}
|
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
|
|
|
}
|
|
|
|
|
2022-12-12 10:06:30 +01:00
|
|
|
ReturnValue_t TmStore::storePacket(PusTmReader& reader) {
|
|
|
|
using namespace std::filesystem;
|
|
|
|
std::string currentPrefix = sdcMan.getCurrentMountPrefix();
|
2022-12-12 16:18:00 +01:00
|
|
|
path baseDir = path(currentPrefix) / baseName;
|
2022-12-12 10:06:30 +01:00
|
|
|
// It is assumed here that the filesystem is usable.
|
|
|
|
if (not exists(baseDir)) {
|
|
|
|
create_directory(baseDir);
|
|
|
|
}
|
|
|
|
if (not mostRecentFile) {
|
|
|
|
for (auto const& file : directory_iterator(baseDir)) {
|
|
|
|
if (file.is_directory()) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-12-12 14:58:56 +01:00
|
|
|
auto pathStr = file.path().string();
|
|
|
|
Clock::TimeOfDay_t tod;
|
2022-12-12 16:18:00 +01:00
|
|
|
if (pathStr.find(baseName) == std::string::npos) {
|
|
|
|
continue;
|
2022-12-12 14:58:56 +01:00
|
|
|
}
|
2022-12-12 18:17:59 +01:00
|
|
|
unsigned int underscorePos = pathStr.find_last_of('_');
|
|
|
|
std::string stampStr = pathStr.substr(underscorePos + 1);
|
|
|
|
int count =
|
|
|
|
sscanf(stampStr.c_str(),
|
|
|
|
"%4" SCNu32 "-%2" SCNu32 "-%2" SCNu32 "T%2" SCNu32 ":%2" SCNu32 ":%2" 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();
|
|
|
|
}
|
2022-12-12 10:06:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// 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;
|
|
|
|
}
|
2022-11-11 15:39:27 +01:00
|
|
|
|
2022-10-25 18:20:21 +02:00
|
|
|
MessageQueueId_t TmStore::getCommandQueue() { return MessageQueueIF::NO_QUEUE; }
|