From 7f327c3900b5778604ac6220a0633a44c7838fe0 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Thu, 5 Aug 2021 16:02:35 +0200 Subject: [PATCH] bugfixes for sdc manager --- bsp_q7s/memory/SdCardManager.cpp | 150 ++++++++++--------------------- bsp_q7s/memory/SdCardManager.h | 15 ++-- fsfw | 2 +- 3 files changed, 55 insertions(+), 112 deletions(-) diff --git a/bsp_q7s/memory/SdCardManager.cpp b/bsp_q7s/memory/SdCardManager.cpp index 83d41c36..932a4217 100644 --- a/bsp_q7s/memory/SdCardManager.cpp +++ b/bsp_q7s/memory/SdCardManager.cpp @@ -129,11 +129,11 @@ ReturnValue_t SdCardManager::switchOffSdCard(sd::SdCard sdCard, bool doUnmountSd ReturnValue_t SdCardManager::setSdCardState(sd::SdCard sdCard, bool on) { using namespace std; + if(cmdExecutor.getCurrentState() == CommandExecutor::States::PENDING) { + return CommandExecutor::COMMAND_PENDING; + } string sdstring = ""; string statestring = ""; - if(currentOp != Operations::IDLE) { - return OP_ONGOING; - } if(sdCard == sd::SdCard::SLOT_0) { sdstring = "0"; } @@ -181,8 +181,8 @@ ReturnValue_t SdCardManager::getSdCardActiveStatus(SdStatusPair& active) { ReturnValue_t SdCardManager::mountSdCard(sd::SdCard sdCard) { using namespace std; - if(currentOp != Operations::IDLE) { - return OP_ONGOING; + if(cmdExecutor.getCurrentState() == CommandExecutor::States::PENDING) { + return CommandExecutor::COMMAND_PENDING; } if(sdCard == sd::SdCard::BOTH) { sif::warning << "SdCardManager::mountSdCard: API does not allow sd::SdStatus::BOTH" @@ -205,6 +205,9 @@ ReturnValue_t SdCardManager::mountSdCard(sd::SdCard sdCard) { return MOUNT_ERROR; } + if(not blocking) { + currentOp = Operations::MOUNTING; + } string sdMountCommand = "mount " + mountDev + " " + mountPoint; cmdExecutor.load(sdMountCommand, blocking, true); ReturnValue_t result = cmdExecutor.execute(); @@ -215,6 +218,9 @@ ReturnValue_t SdCardManager::mountSdCard(sd::SdCard sdCard) { } ReturnValue_t SdCardManager::unmountSdCard(sd::SdCard sdCard) { + if(cmdExecutor.getCurrentState() == CommandExecutor::States::PENDING) { + return CommandExecutor::COMMAND_PENDING; + } using namespace std; if(sdCard == sd::SdCard::BOTH) { sif::warning << "SdCardManager::unmountSdCard: API does not allow sd::SdStatus::BOTH" @@ -239,6 +245,9 @@ ReturnValue_t SdCardManager::unmountSdCard(sd::SdCard sdCard) { sif::warning << "SdCardManager::unmountSdCard: Mount point is empty!" << std::endl; } string sdUnmountCommand = "umount " + mountPoint; + if(not blocking) { + currentOp = Operations::UNMOUNTING; + } cmdExecutor.load(sdUnmountCommand, blocking, true); ReturnValue_t result = cmdExecutor.execute(); if(blocking and result != HasReturnvaluesIF::RETURN_OK) { @@ -266,6 +275,11 @@ ReturnValue_t SdCardManager::sanitizeState(SdStatusPair* statusPair, sd::SdCard return switchOnSdCard(prefSdCard, true, statusPair); } +void SdCardManager::resetState() { + cmdExecutor.reset(); + currentOp = Operations::IDLE; +} + void SdCardManager::processSdStatusLine(std::pair &active, std::string& line, uint8_t& idx, sd::SdCard& currentSd) { using namespace std; @@ -339,6 +353,9 @@ ReturnValue_t SdCardManager::setPreferredSdCard(sd::SdCard sdCard) { } ReturnValue_t SdCardManager::updateSdCardStateFile() { + if(cmdExecutor.getCurrentState() == CommandExecutor::States::PENDING) { + return CommandExecutor::COMMAND_PENDING; + } // Use q7hw utility and pipe the command output into the state file std::string updateCmd = "q7hw sd info all > " + std::string(SD_STATE_FILE); cmdExecutor.load(updateCmd, blocking, true); @@ -365,107 +382,36 @@ std::string SdCardManager::getCurrentMountPrefix(sd::SdCard prefSdCard) { } SdCardManager::OpStatus SdCardManager::checkCurrentOp(Operations ¤tOp) { -// currentOp = this->currentOp; -// switch(currentOpStatus) { -// case(OpStatus::IDLE): -// case(OpStatus::FAIL): -// case(OpStatus::TIMEOUT): -// case(OpStatus::SUCCESS):{ -// break; -// } -// case(OpStatus::ONGOING): { -// int result = poll(&waiter, 1, 0); -// switch(result) { -// case(0): { -// // No data available -// break; -// } -// case(1): { -// if (waiter.revents & POLLIN) { -// ssize_t readBytes = read(opFileNum, readBuf.data(), readBuf.size()); -// if(readBytes == 0) { -// // Should not happen.. -// sif::warning << "SdCardManager::checkCurrentOp: 0 bytes read" << std::endl; -// } -// else if(readBytes > 0) { -// sif::info << currentCmd << " | " << readBuf.data() << std::endl; -// } -// else { -// sif::error << "SdCardManager::checkCurrentOp: Error code " << errno << -// ": " << strerror(errno) << std::endl; -// return OpStatus::FAIL; -// } -// } -// else if (waiter.revents & POLLERR) { -// sif::warning << "SdCardManager::checkCurrentOp: Poll error.." << std::endl; -// } -// else if(waiter.revents & POLLHUP) { -// int result = pclose(opFile); -// if(result != 0) { -// sif::error << "SdCardManager::setSdCardState: pclose failed with code " << -// result << ": " << strerror(result) << std::endl; -// } -// opFile = nullptr; -// opFileNum = 0; -// currentOp = Operations::IDLE; -// return OpStatus::SUCCESS; -// } -// break; -// } -// default: { -// sif::error << "SdCardManager::checkCurrentOp: Unexpected returnvalue " << result << -// "for poll call" << std::endl; -// break; -// } -// } -// } -// } -// return currentOpStatus; -} + CommandExecutor::States state = cmdExecutor.getCurrentState(); + if(state == CommandExecutor::States::IDLE or state == CommandExecutor::States::COMMAND_LOADED) { + return OpStatus::IDLE; + } + currentOp = this->currentOp; + bool bytesRead = false; -//void SdCardManager::resetState() { -// currentOp = Operations::IDLE; -// currentOpStatus = OpStatus::IDLE; -// currentOpSdCard = sd::SdCard::NONE; -//} -// -//void SdCardManager::setBlocking(bool blocking) { -// this->blocking = blocking; -// if(not blocking) { -// currentOp = Operations::IDLE; -// currentOpStatus = OpStatus::IDLE; -// } -//} - -ReturnValue_t SdCardManager::handleCommand(std::string cmd, std::string funcName) { -// currentCmd = cmd; -// opFile = popen(currentCmd.c_str(), "r"); -// if(opFile == nullptr) { -// return POPEN_CALL_ERROR; -// } -// if(blocking) { -// return handleBlockingOperation(funcName); -// } -// else { -// opFileNum = fileno(opFile); -// waiter.fd = opFileNum; -// } - return HasReturnvaluesIF::RETURN_OK; + while(true) { + ReturnValue_t result = cmdExecutor.check(bytesRead); + switch(result) { + case(CommandExecutor::BYTES_READ): { + continue; + } + case(CommandExecutor::EXECUTION_FINISHED): { + return OpStatus::SUCCESS; + } + case(HasReturnvaluesIF::RETURN_OK): { + return OpStatus::ONGOING; + } + case(HasReturnvaluesIF::RETURN_FAILED): { + return OpStatus::FAIL; + } + default: { + sif::warning << "SdCardManager::checkCurrentOp: Unhandled case" << std::endl; + } + } + } } void SdCardManager::setBlocking(bool blocking) { this->blocking = blocking; } -ReturnValue_t SdCardManager::handleBlockingOperation(std::string funcName) { -// while(fgets(readBuf.data(), readBuf.size(), opFile) != nullptr) { -// sif::info << currentCmd << " | " << readBuf.data() << std::endl; -// } -// int result = pclose(opFile); -// if(result != 0) { -// sif::error << "SdCardManager::setSdCardState: pclose failed with code " << -// result << ": " << strerror(result) << std::endl; -// return HasReturnvaluesIF::RETURN_FAILED; -// } - return HasReturnvaluesIF::RETURN_OK; -} diff --git a/bsp_q7s/memory/SdCardManager.h b/bsp_q7s/memory/SdCardManager.h index cd0493cf..a6d9227a 100644 --- a/bsp_q7s/memory/SdCardManager.h +++ b/bsp_q7s/memory/SdCardManager.h @@ -30,14 +30,15 @@ public: SWITCHING_ON, SWITCHING_OFF, MOUNTING, + UNMOUNTING, IDLE }; enum class OpStatus { IDLE, - SUCCESS, TIMEOUT, ONGOING, + SUCCESS, FAIL }; @@ -130,8 +131,10 @@ public: /** * Update the state file or creates one if it does not exist. You need to call this * function before calling #sdCardActive - * @return - RETURN_OK if the state file was updated successfully - * - SYSTEM_CALL_ERROR if the call to create the status file failed + * @return + * - RETURN_OK if the state file was updated successfully + * - CommandExecutor::COMMAND_PENDING: Non-blocking command is pending + * - RETURN_FAILED: blocking command failed */ ReturnValue_t updateSdCardStateFile(); @@ -195,9 +198,6 @@ private: CommandExecutor cmdExecutor; Operations currentOp = Operations::IDLE; bool blocking = true; - std::array readBuf {}; - std::string currentCmd; - int opFileNum = 0; SdCardManager(); @@ -208,9 +208,6 @@ private: std::string currentPrefix; - ReturnValue_t handleCommand(std::string cmd, std::string funcName); - ReturnValue_t handleBlockingOperation(std::string funcName); - static SdCardManager* factoryInstance; }; diff --git a/fsfw b/fsfw index c8472beb..42022051 160000 --- a/fsfw +++ b/fsfw @@ -1 +1 @@ -Subproject commit c8472beb5f31b8f33491b5fccfecdd18bce26542 +Subproject commit 4202205182aa007b9be5279ce40056e18f0b8782