continued TM store impl
All checks were successful
EIVE/eive-obsw/pipeline/pr-develop This commit looks good

This commit is contained in:
Robin Müller 2022-12-12 18:27:01 +01:00
parent 293082a7e8
commit ed603f4e48
No known key found for this signature in database
GPG Key ID: 71B58F8A3CDFA9AC
2 changed files with 65 additions and 42 deletions

View File

@ -8,14 +8,11 @@
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) {}
TmStore::TmStore(object_id_t objectId, std::string baseName, RolloverInterval intervalUnit,
uint32_t intervalCount, PacketFilter filter, SdCardMountedIF& sdcMan)
: SystemObject(objectId), filter(filter), baseName(std::move(baseName)), sdcMan(sdcMan) {
calcDiffSeconds(intervalUnit, intervalCount);
}
ReturnValue_t TmStore::passPacket(PusTmReader& reader) {
bool inApidList = false;
@ -54,38 +51,12 @@ ReturnValue_t TmStore::passPacket(PusTmReader& reader) {
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 (baseDirUninitialized) {
updateBaseDir();
}
// It is assumed here that the filesystem is usable.
if (not mostRecentFile) {
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;
}
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();
}
}
assignMostRecentFile();
}
// 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
@ -95,3 +66,50 @@ ReturnValue_t TmStore::storePacket(PusTmReader& reader) {
}
MessageQueueId_t TmStore::getCommandQueue() { return MessageQueueIF::NO_QUEUE; }
void TmStore::calcDiffSeconds(RolloverInterval intervalUnit, uint32_t intervalCount) {
if (intervalUnit == RolloverInterval::HOURLY) {
rolloverDiffSeconds = 60 * intervalCount;
} else if (intervalUnit == RolloverInterval::DAILY) {
rolloverDiffSeconds = 60 * 24 * intervalCount;
}
}
void TmStore::updateBaseDir() {
using namespace std::filesystem;
std::string currentPrefix = sdcMan.getCurrentMountPrefix();
baseDir = path(currentPrefix) / baseName;
if (not exists(baseDir)) {
create_directory(baseDir);
}
baseDirUninitialized = false;
}
void TmStore::assignMostRecentFile() {
using namespace std::filesystem;
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;
}
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();
}
}
}

View File

@ -19,8 +19,8 @@ enum class RolloverInterval { HOURLY, DAILY };
class TmStore : public SystemObject {
public:
TmStore(object_id_t objectId, std::string baseName, RolloverInterval interval,
uint8_t intervalFactor, PacketFilter filter, SdCardMountedIF& sdcMan);
TmStore(object_id_t objectId, std::string baseName, RolloverInterval intervalUnit,
uint32_t intervalCount, PacketFilter filter, SdCardMountedIF& sdcMan);
ReturnValue_t passPacket(PusTmReader& reader);
ReturnValue_t storePacket(PusTmReader& reader);
@ -34,14 +34,19 @@ class TmStore : public SystemObject {
*/
MessageQueueId_t getCommandQueue();
PacketFilter filter;
bool baseDirUninitialized = true;
std::string baseName;
RolloverInterval interval;
uint8_t intervalFactor;
std::filesystem::path baseDir;
uint32_t rolloverDiffSeconds = 0;
char NAME_BUF[524] = {};
std::array<uint8_t, 8192> fileBuf{};
std::optional<timeval> mostRecentTv;
std::optional<std::filesystem::path> mostRecentFile;
SdCardMountedIF& sdcMan;
void calcDiffSeconds(RolloverInterval intervalUnit, uint32_t intervalCount);
void updateBaseDir();
void assignMostRecentFile();
};
#endif /* MISSION_TMTC_TMSTOREBACKEND_H_ */