Non Blocking SD Card Initialization #75

Merged
meierj merged 25 commits from mueller/non-blocking-sd-card-init into develop 2021-08-09 14:18:49 +02:00
3 changed files with 121 additions and 106 deletions
Showing only changes of commit 8104ac0df8 - Show all commits

View File

@ -15,8 +15,7 @@
SdCardManager* SdCardManager::factoryInstance = nullptr; SdCardManager* SdCardManager::factoryInstance = nullptr;
SdCardManager::SdCardManager() { SdCardManager::SdCardManager(): cmdExecutor(256) {
waiter.events = POLLIN;
} }
SdCardManager::~SdCardManager() { SdCardManager::~SdCardManager() {
@ -151,7 +150,12 @@ ReturnValue_t SdCardManager::setSdCardState(sd::SdCard sdCard, bool on) {
} }
ostringstream command; ostringstream command;
command << "q7hw sd set " << sdstring << " " << statestring; command << "q7hw sd set " << sdstring << " " << statestring;
return handleCommand(command.str(), "SdCardManager::setSdCardState"); cmdExecutor.load(command.str(), blocking, true);
ReturnValue_t result = cmdExecutor.execute();
if(blocking and result != HasReturnvaluesIF::RETURN_OK) {
utility::handleSystemError(cmdExecutor.getLastError(), "SdCardManager::setSdCardState");
}
return result;
} }
ReturnValue_t SdCardManager::getSdCardActiveStatus(SdStatusPair& active) { ReturnValue_t SdCardManager::getSdCardActiveStatus(SdStatusPair& active) {
@ -202,7 +206,12 @@ ReturnValue_t SdCardManager::mountSdCard(sd::SdCard sdCard) {
} }
string sdMountCommand = "mount " + mountDev + " " + mountPoint; string sdMountCommand = "mount " + mountDev + " " + mountPoint;
return handleCommand(sdMountCommand, "SdCardManager::mountSdCard"); cmdExecutor.load(sdMountCommand, blocking, true);
ReturnValue_t result = cmdExecutor.execute();
if(blocking and result != HasReturnvaluesIF::RETURN_OK) {
utility::handleSystemError(cmdExecutor.getLastError(), "SdCardManager::mountSdCard");
}
return result;
} }
ReturnValue_t SdCardManager::unmountSdCard(sd::SdCard sdCard) { ReturnValue_t SdCardManager::unmountSdCard(sd::SdCard sdCard) {
@ -230,7 +239,12 @@ ReturnValue_t SdCardManager::unmountSdCard(sd::SdCard sdCard) {
sif::warning << "SdCardManager::unmountSdCard: Mount point is empty!" << std::endl; sif::warning << "SdCardManager::unmountSdCard: Mount point is empty!" << std::endl;
} }
string sdUnmountCommand = "umount " + mountPoint; string sdUnmountCommand = "umount " + mountPoint;
return handleCommand(sdUnmountCommand, "SdCardManager::unmountSdCard"); cmdExecutor.load(sdUnmountCommand, blocking, true);
ReturnValue_t result = cmdExecutor.execute();
if(blocking and result != HasReturnvaluesIF::RETURN_OK) {
utility::handleSystemError(cmdExecutor.getLastError(), "SdCardManager::unmountSdCard");
}
return result;
} }
ReturnValue_t SdCardManager::sanitizeState(SdStatusPair* statusPair, sd::SdCard prefSdCard) { ReturnValue_t SdCardManager::sanitizeState(SdStatusPair* statusPair, sd::SdCard prefSdCard) {
@ -327,13 +341,12 @@ ReturnValue_t SdCardManager::setPreferredSdCard(sd::SdCard sdCard) {
ReturnValue_t SdCardManager::updateSdCardStateFile() { ReturnValue_t SdCardManager::updateSdCardStateFile() {
// Use q7hw utility and pipe the command output into the state file // Use q7hw utility and pipe the command output into the state file
std::string updateCmd = "q7hw sd info all > " + std::string(SD_STATE_FILE); std::string updateCmd = "q7hw sd info all > " + std::string(SD_STATE_FILE);
int result = std::system(updateCmd.c_str()); cmdExecutor.load(updateCmd, blocking, true);
if(result == 0) { ReturnValue_t result = cmdExecutor.execute();
return HasReturnvaluesIF::RETURN_OK; if(blocking and result != HasReturnvaluesIF::RETURN_OK) {
utility::handleSystemError(cmdExecutor.getLastError(), "SdCardManager::mountSdCard");
} }
sif::warning << "SdCardManager::updateSdCardStateFile: system call failed with code " << return result;
result << std::endl;
return HasReturnvaluesIF::RETURN_FAILED;
} }
std::string SdCardManager::getCurrentMountPrefix(sd::SdCard prefSdCard) { std::string SdCardManager::getCurrentMountPrefix(sd::SdCard prefSdCard) {
@ -352,103 +365,107 @@ std::string SdCardManager::getCurrentMountPrefix(sd::SdCard prefSdCard) {
} }
SdCardManager::OpStatus SdCardManager::checkCurrentOp(Operations &currentOp) { SdCardManager::OpStatus SdCardManager::checkCurrentOp(Operations &currentOp) {
currentOp = this->currentOp; // currentOp = this->currentOp;
switch(currentOpStatus) { // switch(currentOpStatus) {
case(OpStatus::IDLE): // case(OpStatus::IDLE):
case(OpStatus::FAIL): // case(OpStatus::FAIL):
case(OpStatus::TIMEOUT): // case(OpStatus::TIMEOUT):
case(OpStatus::SUCCESS):{ // case(OpStatus::SUCCESS):{
break; // break;
} // }
case(OpStatus::ONGOING): { // case(OpStatus::ONGOING): {
int result = poll(&waiter, 1, 0); // int result = poll(&waiter, 1, 0);
switch(result) { // switch(result) {
case(0): { // case(0): {
// No data available // // No data available
break; // break;
} // }
case(1): { // case(1): {
if (waiter.revents & POLLIN) { // if (waiter.revents & POLLIN) {
ssize_t readBytes = read(opFileNum, readBuf.data(), readBuf.size()); // ssize_t readBytes = read(opFileNum, readBuf.data(), readBuf.size());
if(readBytes == 0) { // if(readBytes == 0) {
// Should not happen.. // // Should not happen..
sif::warning << "SdCardManager::checkCurrentOp: 0 bytes read" << std::endl; // sif::warning << "SdCardManager::checkCurrentOp: 0 bytes read" << std::endl;
} // }
else if(readBytes > 0) { // else if(readBytes > 0) {
sif::info << currentCmd << " | " << readBuf.data() << std::endl; // sif::info << currentCmd << " | " << readBuf.data() << std::endl;
} // }
else { // else {
sif::error << "SdCardManager::checkCurrentOp: Error code " << errno << // sif::error << "SdCardManager::checkCurrentOp: Error code " << errno <<
": " << strerror(errno) << std::endl; // ": " << strerror(errno) << std::endl;
return OpStatus::FAIL; // return OpStatus::FAIL;
} // }
} // }
else if (waiter.revents & POLLERR) { // else if (waiter.revents & POLLERR) {
sif::warning << "SdCardManager::checkCurrentOp: Poll error.." << std::endl; // sif::warning << "SdCardManager::checkCurrentOp: Poll error.." << std::endl;
} // }
else if(waiter.revents & POLLHUP) { // else if(waiter.revents & POLLHUP) {
int result = pclose(opFile); // int result = pclose(opFile);
if(result != 0) { // if(result != 0) {
sif::error << "SdCardManager::setSdCardState: pclose failed with code " << // sif::error << "SdCardManager::setSdCardState: pclose failed with code " <<
result << ": " << strerror(result) << std::endl; // result << ": " << strerror(result) << std::endl;
} // }
opFile = nullptr; // opFile = nullptr;
opFileNum = 0; // opFileNum = 0;
currentOp = Operations::IDLE; // currentOp = Operations::IDLE;
return OpStatus::SUCCESS; // return OpStatus::SUCCESS;
} // }
break; // break;
} // }
default: { // default: {
sif::error << "SdCardManager::checkCurrentOp: Unexpected returnvalue " << result << // sif::error << "SdCardManager::checkCurrentOp: Unexpected returnvalue " << result <<
"for poll call" << std::endl; // "for poll call" << std::endl;
break; // break;
} // }
} // }
} // }
} // }
return currentOpStatus; // return currentOpStatus;
} }
void SdCardManager::resetState() { //void SdCardManager::resetState() {
currentOp = Operations::IDLE; // currentOp = Operations::IDLE;
currentOpStatus = OpStatus::IDLE; // currentOpStatus = OpStatus::IDLE;
currentOpSdCard = sd::SdCard::NONE; // 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;
} }
void SdCardManager::setBlocking(bool blocking) { void SdCardManager::setBlocking(bool blocking) {
this->blocking = 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;
} }
ReturnValue_t SdCardManager::handleBlockingOperation(std::string funcName) { ReturnValue_t SdCardManager::handleBlockingOperation(std::string funcName) {
while(fgets(readBuf.data(), readBuf.size(), opFile) != nullptr) { // while(fgets(readBuf.data(), readBuf.size(), opFile) != nullptr) {
sif::info << currentCmd << " | " << readBuf.data() << std::endl; // sif::info << currentCmd << " | " << readBuf.data() << std::endl;
} // }
int result = pclose(opFile); // int result = pclose(opFile);
if(result != 0) { // if(result != 0) {
sif::error << "SdCardManager::setSdCardState: pclose failed with code " << // sif::error << "SdCardManager::setSdCardState: pclose failed with code " <<
result << ": " << strerror(result) << std::endl; // result << ": " << strerror(result) << std::endl;
return HasReturnvaluesIF::RETURN_FAILED; // return HasReturnvaluesIF::RETURN_FAILED;
} // }
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }

View File

@ -1,6 +1,7 @@
#ifndef BSP_Q7S_MEMORY_SDCARDACCESSMANAGER_H_ #ifndef BSP_Q7S_MEMORY_SDCARDACCESSMANAGER_H_
#define BSP_Q7S_MEMORY_SDCARDACCESSMANAGER_H_ #define BSP_Q7S_MEMORY_SDCARDACCESSMANAGER_H_
#include <fsfw/osal/linux/CommandExecutor.h>
#include "definitions.h" #include "definitions.h"
#include "returnvalues/classIds.h" #include "returnvalues/classIds.h"
#include "events/subsystemIdRanges.h" #include "events/subsystemIdRanges.h"
@ -191,12 +192,9 @@ public:
void setBlocking(bool blocking); void setBlocking(bool blocking);
private: private:
CommandExecutor cmdExecutor;
Operations currentOp = Operations::IDLE; Operations currentOp = Operations::IDLE;
OpStatus currentOpStatus = OpStatus::IDLE;
sd::SdCard currentOpSdCard = sd::SdCard::NONE;
FILE* opFile = nullptr;
bool blocking = true; bool blocking = true;
struct pollfd waiter {};
std::array<char, 256> readBuf {}; std::array<char, 256> readBuf {};
std::string currentCmd; std::string currentCmd;
int opFileNum = 0; int opFileNum = 0;

2
fsfw

@ -1 +1 @@
Subproject commit 1a4a85ceb2ad665e3cd3c2042925ae2f2aae70c1 Subproject commit c8472beb5f31b8f33491b5fccfecdd18bce26542