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) { if (not BLOCKING_SD_INIT) {
sdcMan->setBlocking(false); sdcMan->setBlocking(false);
} }
Stopwatch watch;
sdcMan->updateSdCardStateFile(true);
sdcMan->updateSdStatePair();
SdCardManager::SdStatePair sdStates;
sdcMan->getSdCardsStatus(sdStates);
auto sdCard = sdcMan->getPreferredSdCard(); auto sdCard = sdcMan->getPreferredSdCard();
if (not sdCard.has_value()) { if (not sdCard.has_value()) {
sif::error << "CoreController::initializeAfterTaskCreation: " sif::error << "CoreController::initializeAfterTaskCreation: "
@ -50,7 +55,11 @@ CoreController::CoreController(object_id_t objectId)
sdCard = sd::SdCard::SLOT_0; sdCard = sd::SdCard::SLOT_0;
} }
sdInfo.active = sdCard.value(); 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(); currMntPrefix = sdcMan->getCurrentMountPrefix();
getCurrentBootCopy(CURRENT_CHIP, CURRENT_COPY); getCurrentBootCopy(CURRENT_CHIP, CURRENT_COPY);
@ -314,7 +323,7 @@ ReturnValue_t CoreController::checkModeCommand(Mode_t mode, Submode_t submode,
ReturnValue_t CoreController::initSdCardBlocking() { ReturnValue_t CoreController::initSdCardBlocking() {
// Create update status file // Create update status file
ReturnValue_t result = sdcMan->updateSdCardStateFile(); ReturnValue_t result = sdcMan->updateSdCardStateFile(true);
if (result != returnvalue::OK) { if (result != returnvalue::OK) {
sif::warning << "CoreController::initialize: Updating SD card state file failed" << std::endl; 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; << static_cast<int>(sdInfo.active) << std::endl;
result = sdColdRedundantBlockingInit(); result = sdColdRedundantBlockingInit();
// Update status file // Update status file
sdcMan->updateSdCardStateFile(); sdcMan->updateSdCardStateFile(true);
return result; return result;
} }
if (sdInfo.cfgMode == SdCfgMode::HOT_REDUNDANT) { 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_0, sd::SdState::MOUNTED, "0", false);
sdCardSetup(sd::SdCard::SLOT_1, sd::SdState::MOUNTED, "1", false); sdCardSetup(sd::SdCard::SLOT_1, sd::SdState::MOUNTED, "1", false);
// Update status file // Update status file
sdcMan->updateSdCardStateFile(); sdcMan->updateSdCardStateFile(true);
} }
return returnvalue::OK; return returnvalue::OK;
} }
@ -395,7 +404,7 @@ ReturnValue_t CoreController::sdStateMachine() {
if (sdFsmState == SdStates::GET_INFO) { if (sdFsmState == SdStates::GET_INFO) {
if (not sdInfo.commandExecuted) { if (not sdInfo.commandExecuted) {
// Create updated status file // Create updated status file
result = sdcMan->updateSdCardStateFile(); result = sdcMan->updateSdCardStateFile(false);
if (result != returnvalue::OK) { if (result != returnvalue::OK) {
sif::warning << "CoreController::sdStateMachine: Updating SD card state file failed" sif::warning << "CoreController::sdStateMachine: Updating SD card state file failed"
<< std::endl; << std::endl;
@ -543,12 +552,8 @@ ReturnValue_t CoreController::sdStateMachine() {
if (sdFsmState == SdStates::SKIP_CYCLE_BEFORE_INFO_UPDATE) { if (sdFsmState == SdStates::SKIP_CYCLE_BEFORE_INFO_UPDATE) {
sdFsmState = SdStates::UPDATE_INFO; sdFsmState = SdStates::UPDATE_INFO;
} else if (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 // Update status file
result = sdcMan->updateSdCardStateFile(); result = sdcMan->updateSdCardStateFile(true);
if (result != returnvalue::OK) { if (result != returnvalue::OK) {
sif::warning << "CoreController::initialize: Updating SD card state file failed" << std::endl; 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; return result;
} }
ReturnValue_t SdCardManager::getSdCardsStatus(SdStatePair& active) { ReturnValue_t SdCardManager::getSdCardsStatus(SdStatePair& sdStates) {
using namespace std;
MutexGuard mg(sdLock, LOCK_TYPE, SD_LOCK_TIMEOUT, LOCK_CTX); MutexGuard mg(sdLock, LOCK_TYPE, SD_LOCK_TIMEOUT, LOCK_CTX);
std::error_code e; sdStates = this->sdStates;
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;
}
return returnvalue::OK; return returnvalue::OK;
} }
@ -310,10 +290,9 @@ ReturnValue_t SdCardManager::sanitizeState(SdStatePair* statusPair, sd::SdCard p
resetNonBlockingState = true; resetNonBlockingState = true;
} }
if (statusPair == nullptr) { if (statusPair == nullptr) {
sdStatusPtr = std::make_unique<SdStatePair>(); return returnvalue::FAILED;
statusPair = sdStatusPtr.get();
getSdCardsStatus(*statusPair);
} }
getSdCardsStatus(*statusPair);
if (statusPair->first == sd::SdState::ON) { if (statusPair->first == sd::SdState::ON) {
result = mountSdCard(prefSdCard); result = mountSdCard(prefSdCard);
@ -331,8 +310,34 @@ void SdCardManager::resetState() {
currentOp = Operations::IDLE; currentOp = Operations::IDLE;
} }
void SdCardManager::processSdStatusLine(std::pair<sd::SdState, sd::SdState>& active, ReturnValue_t SdCardManager::updateSdStatePair() {
std::string& line, uint8_t& idx, sd::SdCard& currentSd) { 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; using namespace std;
istringstream iss(line); istringstream iss(line);
string word; string word;
@ -353,24 +358,24 @@ void SdCardManager::processSdStatusLine(std::pair<sd::SdState, sd::SdState>& act
if (word == "on") { if (word == "on") {
if (currentSd == sd::SdCard::SLOT_0) { if (currentSd == sd::SdCard::SLOT_0) {
active.first = sd::SdState::ON; sdStates.first = sd::SdState::ON;
} else { } else {
active.second = sd::SdState::ON; sdStates.second = sd::SdState::ON;
} }
} else if (word == "off") { } else if (word == "off") {
if (currentSd == sd::SdCard::SLOT_0) { if (currentSd == sd::SdCard::SLOT_0) {
active.first = sd::SdState::OFF; sdStates.first = sd::SdState::OFF;
} else { } else {
active.second = sd::SdState::OFF; sdStates.second = sd::SdState::OFF;
} }
} }
} }
if (mountLine) { if (mountLine) {
if (currentSd == sd::SdCard::SLOT_0) { if (currentSd == sd::SdCard::SLOT_0) {
active.first = sd::SdState::MOUNTED; sdStates.first = sd::SdState::MOUNTED;
} else { } 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)); 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) { if (cmdExecutor.getCurrentState() == CommandExecutor::States::PENDING) {
return CommandExecutor::COMMAND_PENDING; return CommandExecutor::COMMAND_PENDING;
} }
@ -476,35 +481,29 @@ bool SdCardManager::isSdCardUsable(std::optional<sd::SdCard> sdCard) {
} }
} }
SdCardManager::SdStatePair active; MutexGuard mg(sdLock, LOCK_TYPE, SD_LOCK_TIMEOUT, LOCK_CTX);
ReturnValue_t result = this->getSdCardsStatus(active);
if (result != returnvalue::OK) {
sif::debug << "SdCardManager::isSdCardMounted: Failed to get SD card active state";
return false;
}
if (not sdCard) { 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 true;
} }
return false; return false;
} }
if (sdCard == sd::SLOT_0) { if (sdCard == sd::SLOT_0) {
if (active.first == sd::MOUNTED) { if (sdStates.first == sd::MOUNTED) {
return true; return true;
} else { } else {
return false; return false;
} }
} }
if (sdCard == sd::SLOT_1) { if (sdCard == sd::SLOT_1) {
if (active.second == sd::MOUNTED) { if (sdStates.second == sd::MOUNTED) {
return true; return true;
} else { } else {
return false; return false;
} }
} }
if (sdCard == sd::BOTH) { if (sdCard == sd::BOTH) {
if (active.first == sd::MOUNTED && active.second == sd::MOUNTED) { if (sdStates.first == sd::MOUNTED && sdStates.second == sd::MOUNTED) {
return true; return true;
} }
} }

View File

@ -25,7 +25,7 @@ class MutexIF;
* state * state
*/ */
class SdCardManager : public SystemObject, public SdCardMountedIF { class SdCardManager : public SystemObject, public SdCardMountedIF {
friend class SdCardAccess; friend class CoreController;
public: public:
using mountInitCb = ReturnValue_t (*)(void* args); using mountInitCb = ReturnValue_t (*)(void* args);
@ -125,7 +125,7 @@ class SdCardManager : public SystemObject, public SdCardMountedIF {
* - CommandExecutor::COMMAND_PENDING: Non-blocking command is pending * - CommandExecutor::COMMAND_PENDING: Non-blocking command is pending
* - returnvalue::FAILED: blocking command failed * - 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 * 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: private:
CommandExecutor cmdExecutor; CommandExecutor cmdExecutor;
SdStatePair sdStates;
Operations currentOp = Operations::IDLE; Operations currentOp = Operations::IDLE;
bool blocking = false; bool blocking = false;
bool sdCardActive = true; bool sdCardActive = true;
@ -233,10 +234,11 @@ class SdCardManager : public SystemObject, public SdCardMountedIF {
SdCardManager(); SdCardManager();
ReturnValue_t updateSdStatePair();
ReturnValue_t setSdCardState(sd::SdCard sdCard, bool on); ReturnValue_t setSdCardState(sd::SdCard sdCard, bool on);
void processSdStatusLine(SdStatePair& active, std::string& line, uint8_t& idx, void processSdStatusLine(std::string& line, uint8_t& idx, sd::SdCard& currentSd);
sd::SdCard& currentSd);
std::optional<std::string> currentPrefix; std::optional<std::string> currentPrefix;

View File

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