From 976235a79f67a2b603b3836f32fafb70da41dd36 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Fri, 10 Mar 2023 18:04:04 +0100 Subject: [PATCH] check busy state --- linux/ipcore/PapbVcInterface.cpp | 5 +++-- linux/ipcore/PapbVcInterface.h | 9 ++++++++- linux/ipcore/Ptme.cpp | 11 +++++++++++ linux/ipcore/Ptme.h | 1 + linux/ipcore/PtmeIF.h | 1 + mission/tmtc/DirectTmSinkIF.h | 2 ++ mission/tmtc/PersistentTmStore.cpp | 9 ++++++--- mission/tmtc/TmStoreTaskBase.cpp | 14 ++++++++------ mission/tmtc/VirtualChannel.cpp | 2 ++ mission/tmtc/VirtualChannel.h | 1 + 10 files changed, 43 insertions(+), 12 deletions(-) diff --git a/linux/ipcore/PapbVcInterface.cpp b/linux/ipcore/PapbVcInterface.cpp index b8b12c7a..51afc15d 100644 --- a/linux/ipcore/PapbVcInterface.cpp +++ b/linux/ipcore/PapbVcInterface.cpp @@ -41,7 +41,7 @@ void PapbVcInterface::startPacketTransfer() { *vcBaseReg = CONFIG_START; } void PapbVcInterface::endPacketTransfer() { *vcBaseReg = CONFIG_END; } -ReturnValue_t PapbVcInterface::pollPapbBusySignal() { +ReturnValue_t PapbVcInterface::pollPapbBusySignal() const { gpio::Levels papbBusyState = gpio::Levels::LOW; ReturnValue_t result = returnvalue::OK; @@ -53,7 +53,6 @@ ReturnValue_t PapbVcInterface::pollPapbBusySignal() { return returnvalue::FAILED; } if (papbBusyState == gpio::Levels::LOW) { - sif::warning << "PapbVcInterface::pollPapbBusySignal: PAPB busy" << std::endl; return PAPB_BUSY; } @@ -80,6 +79,8 @@ void PapbVcInterface::isVcInterfaceBufferEmpty() { return; } +bool PapbVcInterface::isBusy() const { return pollPapbBusySignal() == PAPB_BUSY; } + ReturnValue_t PapbVcInterface::sendTestFrame() { /** Size of one complete transfer frame data field amounts to 1105 bytes */ uint8_t testPacket[1105]; diff --git a/linux/ipcore/PapbVcInterface.h b/linux/ipcore/PapbVcInterface.h index 5fb71340..d4694a62 100644 --- a/linux/ipcore/PapbVcInterface.h +++ b/linux/ipcore/PapbVcInterface.h @@ -32,6 +32,13 @@ class PapbVcInterface : public VirtualChannelIF { std::string uioFile, int mapNum); virtual ~PapbVcInterface(); + bool isBusy() const override; + /** + * + * @param data + * @param size + * @return returnvalue::OK on successfull write, PAPB_BUSY if PAPB is busy. + */ ReturnValue_t write(const uint8_t* data, size_t size) override; ReturnValue_t initialize() override; @@ -95,7 +102,7 @@ class PapbVcInterface : public VirtualChannelIF { * * @return returnvalue::OK when ready to receive data else PAPB_BUSY. */ - ReturnValue_t pollPapbBusySignal(); + ReturnValue_t pollPapbBusySignal() const; /** * @brief This function can be used for debugging to check whether there are packets in diff --git a/linux/ipcore/Ptme.cpp b/linux/ipcore/Ptme.cpp index 714c71be..69d772ff 100644 --- a/linux/ipcore/Ptme.cpp +++ b/linux/ipcore/Ptme.cpp @@ -51,3 +51,14 @@ void Ptme::addVcInterface(VcId_t vcId, VirtualChannelIF* vc) { return; } } + +bool Ptme::isBusy(uint8_t vcId) const { + const auto& vcInterfaceMapIter = vcInterfaceMap.find(vcId); + if (vcInterfaceMapIter == vcInterfaceMap.end()) { + sif::warning << "Ptme::writeToVc: No virtual channel interface found for the virtual " + "channel with id " + << static_cast(vcId) << std::endl; + return UNKNOWN_VC_ID; + } + return vcInterfaceMapIter->second->isBusy(); +} diff --git a/linux/ipcore/Ptme.h b/linux/ipcore/Ptme.h index aec7bcb4..3c076085 100644 --- a/linux/ipcore/Ptme.h +++ b/linux/ipcore/Ptme.h @@ -35,6 +35,7 @@ class Ptme : public PtmeIF, public SystemObject { ReturnValue_t initialize() override; ReturnValue_t writeToVc(uint8_t vcId, const uint8_t* data, size_t size) override; + bool isBusy(uint8_t vcId) const override; /** * @brief This function adds the reference to a virtual channel interface to the vcInterface diff --git a/linux/ipcore/PtmeIF.h b/linux/ipcore/PtmeIF.h index 44aa39f2..06b1cbe7 100644 --- a/linux/ipcore/PtmeIF.h +++ b/linux/ipcore/PtmeIF.h @@ -22,6 +22,7 @@ class PtmeIF { * @param size Number of bytes to write */ virtual ReturnValue_t writeToVc(uint8_t vcId, const uint8_t* data, size_t size) = 0; + virtual bool isBusy(uint8_t vcId) const = 0; }; #endif /* LINUX_OBC_PTMEIF_H_ */ diff --git a/mission/tmtc/DirectTmSinkIF.h b/mission/tmtc/DirectTmSinkIF.h index ca055ea9..ae7997ca 100644 --- a/mission/tmtc/DirectTmSinkIF.h +++ b/mission/tmtc/DirectTmSinkIF.h @@ -16,6 +16,8 @@ class DirectTmSinkIF { * @param size Number of bytes to write */ virtual ReturnValue_t write(const uint8_t* data, size_t size) = 0; + + virtual bool isBusy() const = 0; }; #endif /* MISSION_TMTC_DIRECTTMSINKIF_H_ */ diff --git a/mission/tmtc/PersistentTmStore.cpp b/mission/tmtc/PersistentTmStore.cpp index 81f73751..931102fc 100644 --- a/mission/tmtc/PersistentTmStore.cpp +++ b/mission/tmtc/PersistentTmStore.cpp @@ -191,12 +191,15 @@ ReturnValue_t PersistentTmStore::startDumpFromUpTo(uint32_t fromUnixSeconds, ReturnValue_t PersistentTmStore::loadNextDumpFile() { using namespace std::filesystem; + dumpParams.currentSize = 0; std::error_code e; for (; dumpParams.dirIter != directory_iterator(); dumpParams.dirIter++) { dumpParams.dirEntry = *dumpParams.dirIter; if (dumpParams.dirEntry.is_directory(e)) { continue; } + sif::debug << "handling file " << dumpParams.dirEntry << std::endl; + dumpParams.fileSize = std::filesystem::file_size(dumpParams.dirEntry.path(), e); if (e) { sif::error << "PersistentTmStore: Could not retrieve file size: " << e.message() << std::endl; @@ -221,10 +224,9 @@ ReturnValue_t PersistentTmStore::loadNextDumpFile() { auto fileEpoch = static_cast(timegm(&fileTime)); if ((fileEpoch > dumpParams.fromUnixTime) and (fileEpoch + rolloverDiffSeconds <= dumpParams.untilUnixTime)) { - dumpParams.currentSize = 0; dumpParams.currentFileUnixStamp = fileEpoch; std::ifstream ifile(file, std::ios::binary); - if(ifile.bad()) { + if (ifile.bad()) { sif::error << "PersistentTmStore: File is bad" << std::endl; continue; } @@ -245,6 +247,7 @@ ReturnValue_t PersistentTmStore::dumpNextPacket(DirectTmSinkIF& tmSink, size_t& if (state == State::IDLE) { return returnvalue::FAILED; } + sif::debug << "Current file idx: " << dumpParams.currentSize << std::endl; PusTmReader reader(&timeReader, fileBuf.data() + dumpParams.currentSize, fileBuf.size() - dumpParams.currentSize); // CRC check to fully ensure this is a valid TM @@ -262,7 +265,7 @@ ReturnValue_t PersistentTmStore::dumpNextPacket(DirectTmSinkIF& tmSink, size_t& return loadNextDumpFile(); } } else { - sif::error << "Parsing of PUS TM failed with code " << result << std::endl; + sif::error << "PersistentTmStore: Parsing of PUS TM failed with code " << result << std::endl; triggerEvent(POSSIBLE_FILE_CORRUPTION, result, dumpParams.currentFileUnixStamp); // Delete the file and load next. Could use better algorithm to partially // restore the file dump, but for now do not trust the file. diff --git a/mission/tmtc/TmStoreTaskBase.cpp b/mission/tmtc/TmStoreTaskBase.cpp index e234af1e..6fcbd7fc 100644 --- a/mission/tmtc/TmStoreTaskBase.cpp +++ b/mission/tmtc/TmStoreTaskBase.cpp @@ -21,12 +21,14 @@ bool TmStoreTaskBase::handleOneStore(PersistentTmStoreWithTmQueue& store, Countd if (store.getState() == PersistentTmStore::State::DUMPING) { size_t dumpedLen; bool fileHasSwapped; - // TODO: Maybe do a bit of a delay every 100-200 packets? - // TODO: We could continously dump until a file swap during active downlink.. - // TODO: handle returnvalue? - result = store.dumpNextPacket(channel, dumpedLen, fileHasSwapped); - if (result == returnvalue::OK) { - dumpsPerformed = true; + if (not channel.isBusy()) { + // TODO: Maybe do a bit of a delay every 100-200 packets? + // TODO: We could continously dump until a file swap during active downlink.. + // TODO: handle returnvalue? + result = store.dumpNextPacket(channel, dumpedLen, fileHasSwapped); + if (result == returnvalue::OK) { + dumpsPerformed = true; + } } } else { // Handle TC requests, for example deletion or retrieval requests. diff --git a/mission/tmtc/VirtualChannel.cpp b/mission/tmtc/VirtualChannel.cpp index 0c41ad86..da6ce13f 100644 --- a/mission/tmtc/VirtualChannel.cpp +++ b/mission/tmtc/VirtualChannel.cpp @@ -24,3 +24,5 @@ ReturnValue_t VirtualChannel::write(const uint8_t* data, size_t size) { uint8_t VirtualChannel::getVcid() const { return vcId; } const char* VirtualChannel::getName() const { return vcName.c_str(); } + +bool VirtualChannel::isBusy() const { return ptme.isBusy(vcId); } diff --git a/mission/tmtc/VirtualChannel.h b/mission/tmtc/VirtualChannel.h index b46c099e..983fa448 100644 --- a/mission/tmtc/VirtualChannel.h +++ b/mission/tmtc/VirtualChannel.h @@ -26,6 +26,7 @@ class VirtualChannel : public SystemObject, public VirtualChannelIF { ReturnValue_t initialize() override; ReturnValue_t sendNextTm(const uint8_t* data, size_t size); + bool isBusy() const override; ReturnValue_t write(const uint8_t* data, size_t size) override; uint8_t getVcid() const;