eive-obsw/mission/tmtc/TmStoreTaskBase.cpp

132 lines
4.5 KiB
C++
Raw Normal View History

2023-03-09 19:42:20 +01:00
#include "TmStoreTaskBase.h"
2023-03-10 20:32:11 +01:00
#include <fsfw/ipc/CommandMessageIF.h>
#include <fsfw/tasks/TaskFactory.h>
2023-03-10 02:05:51 +01:00
#include <fsfw/timemanager/Stopwatch.h>
2023-03-10 20:32:11 +01:00
#include <fsfw/tmstorage/TmStoreMessage.h>
#include "mission/persistentTmStoreDefs.h"
2023-03-09 19:42:20 +01:00
TmStoreTaskBase::TmStoreTaskBase(object_id_t objectId, StorageManagerIF& ipcStore,
VirtualChannel& channel, SdCardMountedIF& sdcMan)
: SystemObject(objectId), ipcStore(ipcStore), channel(channel), sdcMan(sdcMan) {}
2023-03-09 19:42:20 +01:00
2023-03-11 14:59:55 +01:00
bool TmStoreTaskBase::handleOneStore(PersistentTmStoreWithTmQueue& store,
DumpContext& dumpContext) {
2023-03-10 02:05:51 +01:00
ReturnValue_t result;
2023-03-10 20:13:36 +01:00
bool tmToStoreReceived = false;
bool tcRequestReceived = false;
2023-03-28 14:59:31 +02:00
bool dumpPerformed = false;
2023-03-27 21:23:09 +02:00
fileHasSwapped = false;
2023-03-27 21:39:37 +02:00
dumpContext.packetWasDumped = false;
dumpContext.vcBusyDuringDump = false;
2023-03-27 21:23:09 +02:00
2023-03-09 19:42:20 +01:00
// Store TM persistently
2023-03-10 02:05:51 +01:00
result = store.handleNextTm();
2023-03-10 20:13:36 +01:00
if (result == returnvalue::OK) {
tmToStoreReceived = true;
2023-03-09 19:42:20 +01:00
}
// Dump TMs when applicable
if (store.getState() == PersistentTmStore::State::DUMPING) {
2023-03-28 14:59:31 +02:00
if (handleOneDump(store, dumpContext, dumpPerformed) != returnvalue::OK) {
return result;
2023-03-10 20:32:11 +01:00
}
2023-03-10 02:05:51 +01:00
} else {
2023-03-10 20:32:11 +01:00
Command_t execCmd;
2023-03-10 02:05:51 +01:00
// Handle TC requests, for example deletion or retrieval requests.
2023-03-10 20:32:11 +01:00
result = store.handleCommandQueue(ipcStore, execCmd);
2023-03-10 20:13:36 +01:00
if (result == returnvalue::OK) {
2023-03-10 20:32:11 +01:00
if (execCmd == TmStoreMessage::DOWNLINK_STORE_CONTENT_TIME) {
cancelDumpCd.resetTimer();
tmSinkBusyCd.resetTimer();
2023-03-11 14:59:55 +01:00
dumpContext.reset();
2023-03-10 20:32:11 +01:00
}
2023-03-10 20:13:36 +01:00
tcRequestReceived = true;
2023-03-10 02:05:51 +01:00
}
2023-03-09 19:42:20 +01:00
}
2023-03-28 14:59:31 +02:00
if (tcRequestReceived or tmToStoreReceived or dumpPerformed) {
2023-03-09 19:42:20 +01:00
return true;
}
return false;
}
bool TmStoreTaskBase::cyclicStoreCheck() {
if (not storesInitialized) {
2023-03-10 02:05:51 +01:00
storesInitialized = initStoresIfPossible();
if (not storesInitialized) {
TaskFactory::delayTask(400);
return false;
}
} else if (sdCardCheckCd.hasTimedOut()) {
if (not sdcMan.isSdCardUsable(std::nullopt)) {
// Might be due to imminent shutdown or SD card switch.
storesInitialized = false;
TaskFactory::delayTask(100);
return false;
}
sdCardCheckCd.resetTimer();
}
return true;
}
2023-03-27 21:39:37 +02:00
2023-03-28 10:13:39 +02:00
void TmStoreTaskBase::cancelDump(DumpContext& ctx, PersistentTmStore& store, bool isTxOn) {
2023-03-28 15:15:38 +02:00
triggerEvent(ctx.eventIfCancelled, ctx.numberOfDumpedPackets, ctx.dumpedBytes);
2023-03-28 10:13:39 +02:00
ctx.reset();
store.cancelDump();
2023-03-28 14:59:31 +02:00
if (isTxOn) {
2023-03-28 10:13:39 +02:00
channel.cancelTransfer();
}
}
2023-03-28 14:59:31 +02:00
ReturnValue_t TmStoreTaskBase::handleOneDump(PersistentTmStoreWithTmQueue& store,
DumpContext& dumpContext, bool& dumpPerformed) {
ReturnValue_t result = returnvalue::OK;
2023-03-28 15:03:34 +02:00
// The PTME might have been reset an transmitter state change, so there is no point in continuing
// the dump.
2023-03-28 14:59:31 +02:00
if (not channel.isTxOn()) {
cancelDump(dumpContext, store, false);
return returnvalue::FAILED;
}
size_t dumpedLen = 0;
if (not channel.isBusy()) {
// Dump the next packet into the PTME.
dumpContext.ptmeBusyCounter = 0;
tmSinkBusyCd.resetTimer();
result = store.dumpNextPacket(channel, dumpedLen, fileHasSwapped);
if (result == DirectTmSinkIF::IS_BUSY) {
sif::warning << "Unexpected PAPB busy" << std::endl;
}
if ((result == PersistentTmStore::DUMP_DONE or result == returnvalue::OK)) {
dumpPerformed = true;
if (dumpedLen > 0) {
dumpContext.dumpedBytes += dumpedLen;
dumpContext.numberOfDumpedPackets += 1;
dumpContext.packetWasDumped = true;
}
}
if (result == PersistentTmStore::DUMP_DONE) {
uint32_t startTime;
uint32_t endTime;
store.getStartAndEndTimeCurrentOrLastDump(startTime, endTime);
triggerEvent(dumpContext.eventIfDone, dumpContext.numberOfDumpedPackets,
dumpContext.dumpedBytes);
dumpContext.reset();
}
} else {
// The PTME might be at full load, so it might sense to delay for a bit to let it do
// its work until some more bandwidth is available. Set a flag here so the upper layer can
// do ths.
dumpContext.vcBusyDuringDump = true;
dumpContext.ptmeBusyCounter++;
if (dumpContext.ptmeBusyCounter == 100) {
// If this happens, something is probably wrong.
2023-03-28 15:15:38 +02:00
sif::warning << "PTME busy for longer period. Cancelling dump" << std::endl;
2023-03-28 14:59:31 +02:00
cancelDump(dumpContext, store, channel.isTxOn());
}
}
if (cancelDumpCd.hasTimedOut() or tmSinkBusyCd.hasTimedOut()) {
cancelDump(dumpContext, store, channel.isTxOn());
}
return result;
}