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 55 additions and 112 deletions
Showing only changes of commit 7f327c3900 - Show all commits

View File

@ -129,11 +129,11 @@ ReturnValue_t SdCardManager::switchOffSdCard(sd::SdCard sdCard, bool doUnmountSd
ReturnValue_t SdCardManager::setSdCardState(sd::SdCard sdCard, bool on) { ReturnValue_t SdCardManager::setSdCardState(sd::SdCard sdCard, bool on) {
using namespace std; using namespace std;
if(cmdExecutor.getCurrentState() == CommandExecutor::States::PENDING) {
return CommandExecutor::COMMAND_PENDING;
}
string sdstring = ""; string sdstring = "";
string statestring = ""; string statestring = "";
if(currentOp != Operations::IDLE) {
return OP_ONGOING;
}
if(sdCard == sd::SdCard::SLOT_0) { if(sdCard == sd::SdCard::SLOT_0) {
sdstring = "0"; sdstring = "0";
} }
@ -181,8 +181,8 @@ ReturnValue_t SdCardManager::getSdCardActiveStatus(SdStatusPair& active) {
ReturnValue_t SdCardManager::mountSdCard(sd::SdCard sdCard) { ReturnValue_t SdCardManager::mountSdCard(sd::SdCard sdCard) {
using namespace std; using namespace std;
if(currentOp != Operations::IDLE) { if(cmdExecutor.getCurrentState() == CommandExecutor::States::PENDING) {
return OP_ONGOING; return CommandExecutor::COMMAND_PENDING;
} }
if(sdCard == sd::SdCard::BOTH) { if(sdCard == sd::SdCard::BOTH) {
sif::warning << "SdCardManager::mountSdCard: API does not allow sd::SdStatus::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; return MOUNT_ERROR;
} }
if(not blocking) {
currentOp = Operations::MOUNTING;
}
string sdMountCommand = "mount " + mountDev + " " + mountPoint; string sdMountCommand = "mount " + mountDev + " " + mountPoint;
cmdExecutor.load(sdMountCommand, blocking, true); cmdExecutor.load(sdMountCommand, blocking, true);
ReturnValue_t result = cmdExecutor.execute(); ReturnValue_t result = cmdExecutor.execute();
@ -215,6 +218,9 @@ ReturnValue_t SdCardManager::mountSdCard(sd::SdCard sdCard) {
} }
ReturnValue_t SdCardManager::unmountSdCard(sd::SdCard sdCard) { ReturnValue_t SdCardManager::unmountSdCard(sd::SdCard sdCard) {
if(cmdExecutor.getCurrentState() == CommandExecutor::States::PENDING) {
return CommandExecutor::COMMAND_PENDING;
}
using namespace std; using namespace std;
if(sdCard == sd::SdCard::BOTH) { if(sdCard == sd::SdCard::BOTH) {
sif::warning << "SdCardManager::unmountSdCard: API does not allow sd::SdStatus::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; sif::warning << "SdCardManager::unmountSdCard: Mount point is empty!" << std::endl;
} }
string sdUnmountCommand = "umount " + mountPoint; string sdUnmountCommand = "umount " + mountPoint;
if(not blocking) {
currentOp = Operations::UNMOUNTING;
}
cmdExecutor.load(sdUnmountCommand, blocking, true); cmdExecutor.load(sdUnmountCommand, blocking, true);
ReturnValue_t result = cmdExecutor.execute(); ReturnValue_t result = cmdExecutor.execute();
if(blocking and result != HasReturnvaluesIF::RETURN_OK) { if(blocking and result != HasReturnvaluesIF::RETURN_OK) {
@ -266,6 +275,11 @@ ReturnValue_t SdCardManager::sanitizeState(SdStatusPair* statusPair, sd::SdCard
return switchOnSdCard(prefSdCard, true, statusPair); return switchOnSdCard(prefSdCard, true, statusPair);
} }
void SdCardManager::resetState() {
cmdExecutor.reset();
currentOp = Operations::IDLE;
}
void SdCardManager::processSdStatusLine(std::pair<sd::SdStatus, sd::SdStatus> &active, void SdCardManager::processSdStatusLine(std::pair<sd::SdStatus, sd::SdStatus> &active,
std::string& line, uint8_t& idx, sd::SdCard& currentSd) { std::string& line, uint8_t& idx, sd::SdCard& currentSd) {
using namespace std; using namespace std;
@ -339,6 +353,9 @@ ReturnValue_t SdCardManager::setPreferredSdCard(sd::SdCard sdCard) {
} }
ReturnValue_t SdCardManager::updateSdCardStateFile() { 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 // 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);
cmdExecutor.load(updateCmd, blocking, true); cmdExecutor.load(updateCmd, blocking, true);
@ -365,107 +382,36 @@ std::string SdCardManager::getCurrentMountPrefix(sd::SdCard prefSdCard) {
} }
SdCardManager::OpStatus SdCardManager::checkCurrentOp(Operations &currentOp) { SdCardManager::OpStatus SdCardManager::checkCurrentOp(Operations &currentOp) {
// currentOp = this->currentOp; CommandExecutor::States state = cmdExecutor.getCurrentState();
// switch(currentOpStatus) { if(state == CommandExecutor::States::IDLE or state == CommandExecutor::States::COMMAND_LOADED) {
// case(OpStatus::IDLE): return 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;
} }
currentOp = this->currentOp;
bool bytesRead = false;
//void SdCardManager::resetState() { while(true) {
// currentOp = Operations::IDLE; ReturnValue_t result = cmdExecutor.check(bytesRead);
// currentOpStatus = OpStatus::IDLE; switch(result) {
// currentOpSdCard = sd::SdCard::NONE; case(CommandExecutor::BYTES_READ): {
//} continue;
// }
//void SdCardManager::setBlocking(bool blocking) { case(CommandExecutor::EXECUTION_FINISHED): {
// this->blocking = blocking; return OpStatus::SUCCESS;
// if(not blocking) { }
// currentOp = Operations::IDLE; case(HasReturnvaluesIF::RETURN_OK): {
// currentOpStatus = OpStatus::IDLE; return OpStatus::ONGOING;
// } }
//} case(HasReturnvaluesIF::RETURN_FAILED): {
return OpStatus::FAIL;
ReturnValue_t SdCardManager::handleCommand(std::string cmd, std::string funcName) { }
// currentCmd = cmd; default: {
// opFile = popen(currentCmd.c_str(), "r"); sif::warning << "SdCardManager::checkCurrentOp: Unhandled case" << std::endl;
// 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;
} }
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;
}

View File

@ -30,14 +30,15 @@ public:
SWITCHING_ON, SWITCHING_ON,
SWITCHING_OFF, SWITCHING_OFF,
MOUNTING, MOUNTING,
UNMOUNTING,
IDLE IDLE
}; };
enum class OpStatus { enum class OpStatus {
IDLE, IDLE,
SUCCESS,
TIMEOUT, TIMEOUT,
ONGOING, ONGOING,
SUCCESS,
FAIL FAIL
}; };
@ -130,8 +131,10 @@ public:
/** /**
* Update the state file or creates one if it does not exist. You need to call this * Update the state file or creates one if it does not exist. You need to call this
* function before calling #sdCardActive * function before calling #sdCardActive
* @return - RETURN_OK if the state file was updated successfully * @return
* - SYSTEM_CALL_ERROR if the call to create the status file failed * - 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(); ReturnValue_t updateSdCardStateFile();
@ -195,9 +198,6 @@ private:
CommandExecutor cmdExecutor; CommandExecutor cmdExecutor;
Operations currentOp = Operations::IDLE; Operations currentOp = Operations::IDLE;
bool blocking = true; bool blocking = true;
std::array<char, 256> readBuf {};
std::string currentCmd;
int opFileNum = 0;
SdCardManager(); SdCardManager();
@ -208,9 +208,6 @@ private:
std::string currentPrefix; std::string currentPrefix;
ReturnValue_t handleCommand(std::string cmd, std::string funcName);
ReturnValue_t handleBlockingOperation(std::string funcName);
static SdCardManager* factoryInstance; static SdCardManager* factoryInstance;
}; };

2
fsfw

@ -1 +1 @@
Subproject commit c8472beb5f31b8f33491b5fccfecdd18bce26542 Subproject commit 4202205182aa007b9be5279ce40056e18f0b8782