From 1a192cd8fb56093b76936453ba177da765c93808 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Tue, 4 Oct 2022 23:04:50 +0200 Subject: [PATCH] found the bug --- bsp_linux_board/ObjectFactory.cpp | 1 - bsp_linux_board/RPiSdCardManager.cpp | 2 +- bsp_linux_board/RPiSdCardManager.h | 2 +- fsfw | 2 +- linux/devices/ScexUartReader.cpp | 61 +++++++++++-------- linux/devices/ScexUartReader.h | 2 + mission/devices/ScexDeviceHandler.cpp | 29 ++++----- .../devicedefinitions/ScexDefinitions.h | 5 +- 8 files changed, 60 insertions(+), 44 deletions(-) diff --git a/bsp_linux_board/ObjectFactory.cpp b/bsp_linux_board/ObjectFactory.cpp index 075d1af6..8ebcdc8c 100644 --- a/bsp_linux_board/ObjectFactory.cpp +++ b/bsp_linux_board/ObjectFactory.cpp @@ -23,7 +23,6 @@ #include "mission/devices/GyroADIS1650XHandler.h" #include "mission/tmtc/TmFunnel.h" #include "objects/systemObjectList.h" -#include "tmtc/apid.h" #include "tmtc/pusIds.h" /* UDP server includes */ diff --git a/bsp_linux_board/RPiSdCardManager.cpp b/bsp_linux_board/RPiSdCardManager.cpp index ccb4f3a9..dfcae8da 100644 --- a/bsp_linux_board/RPiSdCardManager.cpp +++ b/bsp_linux_board/RPiSdCardManager.cpp @@ -4,7 +4,7 @@ RPiSdCardManager::RPiSdCardManager(std::string prefix) : prefix(std::move(prefix const std::string& RPiSdCardManager::getCurrentMountPrefix() const { return prefix; } -bool RPiSdCardManager::isSdCardMounted(sd::SdCard sdCard) { return true; } +bool RPiSdCardManager::isSdCardUsable(sd::SdCard sdCard) { return true; } std::optional RPiSdCardManager::getPreferredSdCard() const { return std::nullopt; } diff --git a/bsp_linux_board/RPiSdCardManager.h b/bsp_linux_board/RPiSdCardManager.h index 28413815..068471c1 100644 --- a/bsp_linux_board/RPiSdCardManager.h +++ b/bsp_linux_board/RPiSdCardManager.h @@ -6,7 +6,7 @@ class RPiSdCardManager : public SdCardMountedIF { public: RPiSdCardManager(std::string prefix); const std::string& getCurrentMountPrefix() const override; - bool isSdCardMounted(sd::SdCard sdCard) override; + bool isSdCardUsable(sd::SdCard sdCard) override; std::optional getPreferredSdCard() const override; void setActiveSdCard(sd::SdCard sdCard) override; std::optional getActiveSdCard() const override; diff --git a/fsfw b/fsfw index d1630cdc..1c53b604 160000 --- a/fsfw +++ b/fsfw @@ -1 +1 @@ -Subproject commit d1630cdc4c3fc801934f1e394efa80e898f8031b +Subproject commit 1c53b60442f5b858b00938169ef1daba41ba272d diff --git a/linux/devices/ScexUartReader.cpp b/linux/devices/ScexUartReader.cpp index 812848e7..c8320a11 100644 --- a/linux/devices/ScexUartReader.cpp +++ b/linux/devices/ScexUartReader.cpp @@ -35,7 +35,7 @@ ReturnValue_t ScexUartReader::performOperation(uint8_t operationCode) { while (true) { semaphore->acquire(); int bytesRead = 0; - debugMode = true; + // debugMode = true; while (true) { bytesRead = read(serialPort, reinterpret_cast(recBuf.data()), static_cast(recBuf.size())); @@ -50,24 +50,11 @@ ReturnValue_t ScexUartReader::performOperation(uint8_t operationCode) { break; } } - size_t bytesRead = 0; ReturnValue_t result = returnvalue::OK; while (result != DleParser::NO_PACKET_FOUND) { - result = dleParser.parseRingBuf(bytesRead); - if (result == returnvalue::OK) { - sif::info << "FOund dle packet, read " << bytesRead << " bytes" << std::endl; - // Packet found, advance read pointer. - auto& decodedPacket = dleParser.getContext().decodedPacket; - handleFoundDlePacket(decodedPacket.first, decodedPacket.second); - dleParser.confirmBytesRead(bytesRead); - } else if (result != DleParser::NO_PACKET_FOUND) { - sif::warning << "ScexUartReader::performOperation: Possible packet loss" << std::endl; - // Markers found at wrong place - // which might be a hint for a possibly lost packet. - dleParser.defaultErrorHandler(); - dleParser.confirmBytesRead(bytesRead); - } + result = tryDleParsing(); } + TaskFactory::delayTask(400); } else if (bytesRead < 0) { sif::warning << "ScexUartReader::performOperation: read call failed with error [" << errno @@ -77,15 +64,16 @@ ReturnValue_t ScexUartReader::performOperation(uint8_t operationCode) { sif::error << "ScexUartReader::performOperation: Receive buffer too small for " << bytesRead << " bytes" << std::endl; } else if (bytesRead > 0) { - ReturnValue_t result = dleParser.passData(recBuf.data(), bytesRead); if (debugMode) { sif::info << "Received " << bytesRead << " bytes from the Solar Cell Experiment:" << std::endl; } + ReturnValue_t result = dleParser.passData(recBuf.data(), bytesRead); if (result != OK) { sif::warning << "ScexUartReader::performOperation: Passing data to DLE parser failed" << std::endl; } + result = tryDleParsing(); } }; } @@ -138,6 +126,7 @@ ReturnValue_t ScexUartReader::initializeInterface(CookieIF *cookie) { ReturnValue_t ScexUartReader::sendMessage(CookieIF *cookie, const uint8_t *sendData, size_t sendLen) { + ReturnValue_t result; if (sendData == nullptr or sendLen == 0) { return FAILED; } @@ -146,13 +135,16 @@ ReturnValue_t ScexUartReader::sendMessage(CookieIF *cookie, const uint8_t *sendD lock->unlockMutex(); return FAILED; } - tcflush(serialPort, TCIOFLUSH); + tcflush(serialPort, TCIFLUSH); state = States::RUNNING; lock->unlockMutex(); + result = semaphore->release(); + if (result != OK) { + std::cout << "ScexUartReader::sendMessage: Releasing semaphore failed" << std::endl; + } size_t encodedLen = 0; - ReturnValue_t result = - dleEncoder.encode(sendData, sendLen, cmdbuf.data(), cmdbuf.size(), &encodedLen, true); + result = dleEncoder.encode(sendData, sendLen, cmdbuf.data(), cmdbuf.size(), &encodedLen, true); if (result != OK) { sif::warning << "ScexUartReader::sendMessage: Encoding failed" << std::endl; return FAILED; @@ -163,10 +155,7 @@ ReturnValue_t ScexUartReader::sendMessage(CookieIF *cookie, const uint8_t *sendD << std::endl; return FAILED; } - result = semaphore->release(); - if (result != OK) { - std::cout << "ScexUartReader::sendMessage: Releasing semaphore failed" << std::endl; - } + return OK; } @@ -199,6 +188,30 @@ void ScexUartReader::handleFoundDlePacket(uint8_t *packet, size_t len) { } } +ReturnValue_t ScexUartReader::tryDleParsing() { + size_t bytesRead = 0; + ReturnValue_t result = dleParser.parseRingBuf(bytesRead); + if (result == returnvalue::OK) { + // Packet found, advance read pointer. + auto &decodedPacket = dleParser.getContext().decodedPacket; + handleFoundDlePacket(decodedPacket.first, decodedPacket.second); + dleParser.confirmBytesRead(bytesRead); + } else if (result != DleParser::NO_PACKET_FOUND) { + sif::warning << "ScexUartReader::performOperation: Possible packet loss" << std::endl; + // Markers found at wrong place + // which might be a hint for a possibly lost packet. + dleParser.defaultErrorHandler(); + dleParser.confirmBytesRead(bytesRead); + } + return result; +} + +void ScexUartReader::reset() { + lock->lockMutex(); + state = States::FINISH; + lock->unlockMutex(); +} + ReturnValue_t ScexUartReader::readReceivedMessage(CookieIF *cookie, uint8_t **buffer, size_t *size) { MutexGuard mg(lock); diff --git a/linux/devices/ScexUartReader.h b/linux/devices/ScexUartReader.h index bdce5b65..1b40adc5 100644 --- a/linux/devices/ScexUartReader.h +++ b/linux/devices/ScexUartReader.h @@ -22,6 +22,7 @@ class ScexUartReader : public SystemObject, // strg+shift+n enum class States { NOT_READY, IDLE, RUNNING, FINISH }; ScexUartReader(object_id_t objectId); + void reset(); ReturnValue_t finish(); void setDebugMode(bool enable); @@ -47,6 +48,7 @@ class ScexUartReader : public SystemObject, // strg+shift+n static void foundDlePacketHandler(const DleParser::Context &ctx); void handleFoundDlePacket(uint8_t *packet, size_t len); + ReturnValue_t tryDleParsing(); ReturnValue_t performOperation(uint8_t operationCode = 0) override; diff --git a/mission/devices/ScexDeviceHandler.cpp b/mission/devices/ScexDeviceHandler.cpp index 156dd9b8..b4aca88d 100644 --- a/mission/devices/ScexDeviceHandler.cpp +++ b/mission/devices/ScexDeviceHandler.cpp @@ -5,10 +5,10 @@ #include #include -#include -#include -#include #include +#include +#include +#include #include "fsfw/globalfunctions/CRC.h" #include "mission/devices/devicedefinitions/ScexDefinitions.h" @@ -24,7 +24,10 @@ ScexDeviceHandler::~ScexDeviceHandler() {} void ScexDeviceHandler::doStartUp() { setMode(MODE_ON); } -void ScexDeviceHandler::doShutDown() { setMode(_MODE_POWER_DOWN); } +void ScexDeviceHandler::doShutDown() { + reader.reset(); + setMode(_MODE_POWER_DOWN); +} ReturnValue_t ScexDeviceHandler::buildNormalDeviceCommand(DeviceCommandId_t* id) { return OK; } @@ -348,18 +351,16 @@ void ScexDeviceHandler::modeChanged() {} void ScexDeviceHandler::setPowerSwitcher(PowerSwitchIF& powerSwitcher, power::Switch_t switchId) { DeviceHandlerBase::setPowerSwitcher(&powerSwitcher); this->switchId = switchId; - } ReturnValue_t ScexDeviceHandler::initializeAfterTaskCreation() { + auto mntPrefix = sdcMan.getCurrentMountPrefix(); + std::filesystem::path fullFilePath = mntPrefix; + fullFilePath /= "scex"; + bool fileExists = std::filesystem::exists(fullFilePath); - auto mntPrefix = sdcMan.getCurrentMountPrefix(); - std::filesystem::path fullFilePath = mntPrefix; - fullFilePath /= "scex"; - bool fileExists = std::filesystem::exists(fullFilePath); - - if(not fileExists){ - std::filesystem::create_directory(fullFilePath); - } - return DeviceHandlerBase::initializeAfterTaskCreation(); + if (not fileExists) { + std::filesystem::create_directory(fullFilePath); + } + return DeviceHandlerBase::initializeAfterTaskCreation(); } diff --git a/mission/devices/devicedefinitions/ScexDefinitions.h b/mission/devices/devicedefinitions/ScexDefinitions.h index 89b14c75..22e3878b 100644 --- a/mission/devices/devicedefinitions/ScexDefinitions.h +++ b/mission/devices/devicedefinitions/ScexDefinitions.h @@ -1,14 +1,15 @@ #ifndef MISSION_DEVICES_DEVICEDEFINITIONS_SCEXDEFINITIONS_H_ #define MISSION_DEVICES_DEVICEDEFINITIONS_SCEXDEFINITIONS_H_ -#include "eive/objects.h" -#include "eive/eventSubsystemIds.h" #include #include #include #include +#include "eive/eventSubsystemIds.h" +#include "eive/objects.h" + // Definitions for the Solar Cell Experiment namespace scex {