Merge branch 'improve_sd_card_manager' into refactor_tm_handling

This commit is contained in:
Robin Müller 2023-03-10 02:29:44 +01:00
commit 64fd7fb37d
4 changed files with 66 additions and 59 deletions

View File

@ -42,6 +42,11 @@ CoreController::CoreController(object_id_t objectId)
if (not BLOCKING_SD_INIT) {
sdcMan->setBlocking(false);
}
Stopwatch watch;
sdcMan->updateSdCardStateFile(true);
sdcMan->updateSdStatePair();
SdCardManager::SdStatePair sdStates;
sdcMan->getSdCardsStatus(sdStates);
auto sdCard = sdcMan->getPreferredSdCard();
if (not sdCard.has_value()) {
sif::error << "CoreController::initializeAfterTaskCreation: "
@ -50,7 +55,11 @@ CoreController::CoreController(object_id_t objectId)
sdCard = sd::SdCard::SLOT_0;
}
sdInfo.active = sdCard.value();
sdcMan->setActiveSdCard(sdInfo.active);
if (sdStates.first == sd::SdState::MOUNTED) {
sdcMan->setActiveSdCard(sd::SdCard::SLOT_0);
} else if (sdStates.second == sd::SdState::MOUNTED) {
sdcMan->setActiveSdCard(sd::SdCard::SLOT_1);
}
currMntPrefix = sdcMan->getCurrentMountPrefix();
getCurrentBootCopy(CURRENT_CHIP, CURRENT_COPY);
@ -314,7 +323,7 @@ ReturnValue_t CoreController::checkModeCommand(Mode_t mode, Submode_t submode,
ReturnValue_t CoreController::initSdCardBlocking() {
// Create update status file
ReturnValue_t result = sdcMan->updateSdCardStateFile();
ReturnValue_t result = sdcMan->updateSdCardStateFile(true);
if (result != returnvalue::OK) {
sif::warning << "CoreController::initialize: Updating SD card state file failed" << std::endl;
}
@ -334,7 +343,7 @@ ReturnValue_t CoreController::initSdCardBlocking() {
<< static_cast<int>(sdInfo.active) << std::endl;
result = sdColdRedundantBlockingInit();
// Update status file
sdcMan->updateSdCardStateFile();
sdcMan->updateSdCardStateFile(true);
return result;
}
if (sdInfo.cfgMode == SdCfgMode::HOT_REDUNDANT) {
@ -342,7 +351,7 @@ ReturnValue_t CoreController::initSdCardBlocking() {
sdCardSetup(sd::SdCard::SLOT_0, sd::SdState::MOUNTED, "0", false);
sdCardSetup(sd::SdCard::SLOT_1, sd::SdState::MOUNTED, "1", false);
// Update status file
sdcMan->updateSdCardStateFile();
sdcMan->updateSdCardStateFile(true);
}
return returnvalue::OK;
}
@ -395,7 +404,7 @@ ReturnValue_t CoreController::sdStateMachine() {
if (sdFsmState == SdStates::GET_INFO) {
if (not sdInfo.commandExecuted) {
// Create updated status file
result = sdcMan->updateSdCardStateFile();
result = sdcMan->updateSdCardStateFile(false);
if (result != returnvalue::OK) {
sif::warning << "CoreController::sdStateMachine: Updating SD card state file failed"
<< std::endl;
@ -543,12 +552,8 @@ ReturnValue_t CoreController::sdStateMachine() {
if (sdFsmState == SdStates::SKIP_CYCLE_BEFORE_INFO_UPDATE) {
sdFsmState = SdStates::UPDATE_INFO;
} else if (sdFsmState == SdStates::UPDATE_INFO) {
// It is assumed that all tasks are running by the point this section is reached.
// Therefore, perform this operation in blocking mode because it does not take long
// and the ready state of the SD card is available sooner
sdcMan->setBlocking(true);
// Update status file
result = sdcMan->updateSdCardStateFile();
result = sdcMan->updateSdCardStateFile(true);
if (result != returnvalue::OK) {
sif::warning << "CoreController::initialize: Updating SD card state file failed" << std::endl;
}

View File

@ -196,29 +196,9 @@ ReturnValue_t SdCardManager::setSdCardState(sd::SdCard sdCard, bool on) {
return result;
}
ReturnValue_t SdCardManager::getSdCardsStatus(SdStatePair& active) {
using namespace std;
ReturnValue_t SdCardManager::getSdCardsStatus(SdStatePair& sdStates) {
MutexGuard mg(sdLock, LOCK_TYPE, SD_LOCK_TIMEOUT, LOCK_CTX);
std::error_code e;
if (not filesystem::exists(SD_STATE_FILE, e)) {
return STATUS_FILE_NEXISTS;
}
// Now the file should exist in any case. Still check whether it exists.
fstream sdStatus(SD_STATE_FILE);
if (not sdStatus.good()) {
return STATUS_FILE_NEXISTS;
}
string line;
uint8_t idx = 0;
sd::SdCard currentSd = sd::SdCard::SLOT_0;
// Process status file line by line
while (std::getline(sdStatus, line)) {
processSdStatusLine(active, line, idx, currentSd);
}
if (active.first != sd::SdState::MOUNTED && active.second != sd::SdState::MOUNTED) {
sdCardActive = false;
}
sdStates = this->sdStates;
return returnvalue::OK;
}
@ -310,10 +290,9 @@ ReturnValue_t SdCardManager::sanitizeState(SdStatePair* statusPair, sd::SdCard p
resetNonBlockingState = true;
}
if (statusPair == nullptr) {
sdStatusPtr = std::make_unique<SdStatePair>();
statusPair = sdStatusPtr.get();
getSdCardsStatus(*statusPair);
return returnvalue::FAILED;
}
getSdCardsStatus(*statusPair);
if (statusPair->first == sd::SdState::ON) {
result = mountSdCard(prefSdCard);
@ -331,8 +310,34 @@ void SdCardManager::resetState() {
currentOp = Operations::IDLE;
}
void SdCardManager::processSdStatusLine(std::pair<sd::SdState, sd::SdState>& active,
std::string& line, uint8_t& idx, sd::SdCard& currentSd) {
ReturnValue_t SdCardManager::updateSdStatePair() {
using namespace std;
MutexGuard mg(sdLock, LOCK_TYPE, SD_LOCK_TIMEOUT, LOCK_CTX);
std::error_code e;
if (not filesystem::exists(SD_STATE_FILE, e)) {
return STATUS_FILE_NEXISTS;
}
// Now the file should exist in any case. Still check whether it exists.
fstream sdStatus(SD_STATE_FILE);
if (not sdStatus.good()) {
return STATUS_FILE_NEXISTS;
}
string line;
uint8_t idx = 0;
sd::SdCard currentSd = sd::SdCard::SLOT_0;
// Process status file line by line
while (std::getline(sdStatus, line)) {
processSdStatusLine(line, idx, currentSd);
}
if (sdStates.first != sd::SdState::MOUNTED && sdStates.second != sd::SdState::MOUNTED) {
sdCardActive = false;
}
return returnvalue::OK;
}
void SdCardManager::processSdStatusLine(std::string& line, uint8_t& idx, sd::SdCard& currentSd) {
using namespace std;
istringstream iss(line);
string word;
@ -353,24 +358,24 @@ void SdCardManager::processSdStatusLine(std::pair<sd::SdState, sd::SdState>& act
if (word == "on") {
if (currentSd == sd::SdCard::SLOT_0) {
active.first = sd::SdState::ON;
sdStates.first = sd::SdState::ON;
} else {
active.second = sd::SdState::ON;
sdStates.second = sd::SdState::ON;
}
} else if (word == "off") {
if (currentSd == sd::SdCard::SLOT_0) {
active.first = sd::SdState::OFF;
sdStates.first = sd::SdState::OFF;
} else {
active.second = sd::SdState::OFF;
sdStates.second = sd::SdState::OFF;
}
}
}
if (mountLine) {
if (currentSd == sd::SdCard::SLOT_0) {
active.first = sd::SdState::MOUNTED;
sdStates.first = sd::SdState::MOUNTED;
} else {
active.second = sd::SdState::MOUNTED;
sdStates.second = sd::SdState::MOUNTED;
}
}
@ -401,7 +406,7 @@ ReturnValue_t SdCardManager::setPreferredSdCard(sd::SdCard sdCard) {
return scratch::writeNumber(scratch::PREFERED_SDC_KEY, static_cast<uint8_t>(sdCard));
}
ReturnValue_t SdCardManager::updateSdCardStateFile() {
ReturnValue_t SdCardManager::updateSdCardStateFile(bool blocking) {
if (cmdExecutor.getCurrentState() == CommandExecutor::States::PENDING) {
return CommandExecutor::COMMAND_PENDING;
}
@ -476,35 +481,29 @@ bool SdCardManager::isSdCardUsable(std::optional<sd::SdCard> sdCard) {
}
}
SdCardManager::SdStatePair active;
ReturnValue_t result = this->getSdCardsStatus(active);
if (result != returnvalue::OK) {
sif::debug << "SdCardManager::isSdCardMounted: Failed to get SD card active state";
return false;
}
MutexGuard mg(sdLock, LOCK_TYPE, SD_LOCK_TIMEOUT, LOCK_CTX);
if (not sdCard) {
if (active.first == sd::MOUNTED or active.second == sd::MOUNTED) {
if (sdStates.first == sd::MOUNTED or sdStates.second == sd::MOUNTED) {
return true;
}
return false;
}
if (sdCard == sd::SLOT_0) {
if (active.first == sd::MOUNTED) {
if (sdStates.first == sd::MOUNTED) {
return true;
} else {
return false;
}
}
if (sdCard == sd::SLOT_1) {
if (active.second == sd::MOUNTED) {
if (sdStates.second == sd::MOUNTED) {
return true;
} else {
return false;
}
}
if (sdCard == sd::BOTH) {
if (active.first == sd::MOUNTED && active.second == sd::MOUNTED) {
if (sdStates.first == sd::MOUNTED && sdStates.second == sd::MOUNTED) {
return true;
}
}

View File

@ -25,7 +25,7 @@ class MutexIF;
* state
*/
class SdCardManager : public SystemObject, public SdCardMountedIF {
friend class SdCardAccess;
friend class CoreController;
public:
using mountInitCb = ReturnValue_t (*)(void* args);
@ -125,7 +125,7 @@ class SdCardManager : public SystemObject, public SdCardMountedIF {
* - CommandExecutor::COMMAND_PENDING: Non-blocking command is pending
* - returnvalue::FAILED: blocking command failed
*/
ReturnValue_t updateSdCardStateFile();
ReturnValue_t updateSdCardStateFile(bool blocking);
/**
* Get the state of the SD cards. If the state file does not exist, this function will
@ -218,6 +218,7 @@ class SdCardManager : public SystemObject, public SdCardMountedIF {
private:
CommandExecutor cmdExecutor;
SdStatePair sdStates;
Operations currentOp = Operations::IDLE;
bool blocking = false;
bool sdCardActive = true;
@ -233,10 +234,11 @@ class SdCardManager : public SystemObject, public SdCardMountedIF {
SdCardManager();
ReturnValue_t updateSdStatePair();
ReturnValue_t setSdCardState(sd::SdCard sdCard, bool on);
void processSdStatusLine(SdStatePair& active, std::string& line, uint8_t& idx,
sd::SdCard& currentSd);
void processSdStatusLine(std::string& line, uint8_t& idx, sd::SdCard& currentSd);
std::optional<std::string> currentPrefix;

View File

@ -35,6 +35,7 @@ void satsystem::init() {
ModeListEntry entry;
buildSafeSequence(EIVE_SYSTEM, entry);
buildIdleSequence(EIVE_SYSTEM, entry);
EIVE_SYSTEM.setInitialMode(HasModesIF::MODE_OFF, 0);
}
EiveSystem satsystem::EIVE_SYSTEM = EiveSystem(objects::EIVE_SYSTEM, 12, 24);