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() {
waiter.events = POLLIN;
SdCardManager::SdCardManager(): cmdExecutor(256) {
}
SdCardManager::~SdCardManager() {
@ -151,7 +150,12 @@ ReturnValue_t SdCardManager::setSdCardState(sd::SdCard sdCard, bool on) {
}
ostringstream command;
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) {
@ -202,7 +206,12 @@ ReturnValue_t SdCardManager::mountSdCard(sd::SdCard sdCard) {
}
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) {
@ -230,7 +239,12 @@ ReturnValue_t SdCardManager::unmountSdCard(sd::SdCard sdCard) {
sif::warning << "SdCardManager::unmountSdCard: Mount point is empty!" << std::endl;
}
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) {
@ -327,13 +341,12 @@ ReturnValue_t SdCardManager::setPreferredSdCard(sd::SdCard sdCard) {
ReturnValue_t SdCardManager::updateSdCardStateFile() {
// Use q7hw utility and pipe the command output into the state file
std::string updateCmd = "q7hw sd info all > " + std::string(SD_STATE_FILE);
int result = std::system(updateCmd.c_str());
if(result == 0) {
return HasReturnvaluesIF::RETURN_OK;
cmdExecutor.load(updateCmd, blocking, true);
ReturnValue_t result = cmdExecutor.execute();
if(blocking and result != HasReturnvaluesIF::RETURN_OK) {
utility::handleSystemError(cmdExecutor.getLastError(), "SdCardManager::mountSdCard");
}
sif::warning << "SdCardManager::updateSdCardStateFile: system call failed with code " <<
result << std::endl;
return HasReturnvaluesIF::RETURN_FAILED;
return result;
}
std::string SdCardManager::getCurrentMountPrefix(sd::SdCard prefSdCard) {
@ -352,103 +365,107 @@ std::string SdCardManager::getCurrentMountPrefix(sd::SdCard prefSdCard) {
}
SdCardManager::OpStatus SdCardManager::checkCurrentOp(Operations &currentOp) {
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;
// 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;
}
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;
}
}
//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;
}
// 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) {
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;
}
// 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

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

2
fsfw

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