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
6 changed files with 308 additions and 197 deletions
Showing only changes of commit 2fb54f7de9 - Show all commits

View File

@ -39,7 +39,8 @@ CoreController::CoreController(object_id_t objectId):
sif::error << "CoreController::CoreController: SD card manager invalid!" << std::endl; sif::error << "CoreController::CoreController: SD card manager invalid!" << std::endl;
} }
if(not BLOCKING_SD_INIT) { if(not BLOCKING_SD_INIT) {
initSdCardNonBlocking(); sdInitStateMachine();
sdcMan->setBlocking(false);
} }
result = initBootCopy(); result = initBootCopy();
@ -58,8 +59,8 @@ ReturnValue_t CoreController::handleCommandMessage(CommandMessage *message) {
} }
void CoreController::performControlOperation() { void CoreController::performControlOperation() {
if(not BLOCKING_SD_INIT and not sdInitFinished) { if(not BLOCKING_SD_INIT and not sdInfo.initFinished) {
initSdCardNonBlocking(); sdInitStateMachine();
} }
performWatchdogControlOperation(); performWatchdogControlOperation();
} }
@ -83,7 +84,7 @@ ReturnValue_t CoreController::initialize() {
} }
if(not BLOCKING_SD_INIT) { if(not BLOCKING_SD_INIT) {
initSdCardNonBlocking(); sdInitStateMachine();
} }
return ExtendedControllerBase::initialize(); return ExtendedControllerBase::initialize();
@ -106,17 +107,17 @@ ReturnValue_t CoreController::initSdCardBlocking() {
<< std::endl; << std::endl;
} }
auto statusPair = SdCardManager::SdStatusPair(sd::SdState::OFF, sd::SdState::OFF); result = sdcMan->getSdCardActiveStatus(sdInfo.currentState);
result = sdcMan->getSdCardActiveStatus(statusPair);
if(result != HasReturnvaluesIF::RETURN_OK) { if(result != HasReturnvaluesIF::RETURN_OK) {
sif::warning << "Getting SD card activity status failed" << std::endl; sif::warning << "Getting SD card activity status failed" << std::endl;
} }
#if Q7S_SD_CARD_CONFIG == Q7S_SD_COLD_REDUNDANT #if Q7S_SD_CARD_CONFIG == Q7S_SD_COLD_REDUNDANT
determinePreferredSdCard(); determinePreferredSdCard();
updateSdInfoOther();
sif::info << "Cold redundant SD card configuration, preferred SD card: " << sif::info << "Cold redundant SD card configuration, preferred SD card: " <<
static_cast<int>(preferredSdCard) << std::endl; static_cast<int>(sdInfo.pref) << std::endl;
result = sdCardColdRedundantInit(sdcMan, statusPair); result = sdColdRedundantBlockingInit();
// Update status file // Update status file
sdcMan->updateSdCardStateFile(); sdcMan->updateSdCardStateFile();
return result; return result;
@ -133,131 +134,193 @@ ReturnValue_t CoreController::initSdCardBlocking() {
} }
ReturnValue_t CoreController::initSdCardNonBlocking() { ReturnValue_t CoreController::sdInitStateMachine() {
#if Q7S_SD_CARD_CONFIG == Q7S_SD_NONE #if Q7S_SD_CARD_CONFIG == Q7S_SD_NONE
sif::info << "No SD card initialization will be performed" << std::endl; sif::info << "No SD card initialization will be performed" << std::endl;
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
#else #else
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK; ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
if(state == SdStates::START) { SdCardManager::Operations operation;
sdcInitCycleCount = 0;
commandExecuted = false; auto nonBlockingOpChecking = [&](SdStates newStateOnSuccess,
state = SdStates::GET_INFO; uint16_t maxCycleCount, std::string opPrintout) {
SdCardManager::OpStatus status = sdcMan->checkCurrentOp(operation);
if(status == SdCardManager::OpStatus::SUCCESS) {
sif::debug << "op success" << std::endl;
sdInfo.state = newStateOnSuccess;
sdInfo.commandExecuted = false;
sdInfo.cycleCount = 0;
}
else if(sdInfo.cycleCount > 4) {
sif::warning << "CoreController::sdInitStateMachine: " << opPrintout <<
" takes too long" << std::endl;
}
};
if(sdInfo.state == SdStates::START) {
sdInfo.cycleCount = 0;
sdInfo.commandExecuted = false;
sdInfo.state = SdStates::GET_INFO;
} }
if(state == SdStates::GET_INFO) { if(sdInfo.state == SdStates::GET_INFO) {
if(not commandExecuted) {
if(not sdInfo.commandExecuted) {
sif::debug << "Getting info.." << std::endl;
// Create update status file // Create update status file
result = sdcMan->updateSdCardStateFile(); result = sdcMan->updateSdCardStateFile();
if(result != HasReturnvaluesIF::RETURN_OK) { if(result != HasReturnvaluesIF::RETURN_OK) {
sif::warning << "CoreController::initialize: Updating SD card state file failed" sif::warning << "CoreController::initialize: Updating SD card state file failed"
<< std::endl; << std::endl;
} }
commandExecuted = true; sdInfo.commandExecuted = true;
} }
else { else {
SdCardManager::Operations operation; nonBlockingOpChecking(SdStates::SET_STATE_SELF, 4, "Updating SDC file");
SdCardManager::OpStatus status = sdcMan->checkCurrentOp(operation);
if(status == SdCardManager::OpStatus::SUCCESS) {
state = SdStates::SET_STATE_SELF;
commandExecuted = false;
sdcInitCycleCount = 0;
}
else if(sdcInitCycleCount > 4) {
sif::warning << "CoreController::initSdCardNonBlocking: "
"Updating SDC file takes too long" << std::endl;
}
} }
} }
if(state == SdStates::SET_STATE_SELF) { if(sdInfo.state == SdStates::SET_STATE_SELF) {
if(not commandExecuted) { if(not sdInfo.commandExecuted) {
sif::debug << "Set self state" << std::endl;
result = sdcMan->getSdCardActiveStatus(sdInfo.currentState);
determinePreferredSdCard(); determinePreferredSdCard();
statusPair = SdCardManager::SdStatusPair(sd::SdState::OFF, sd::SdState::OFF); updateSdInfoOther();
result = sdcMan->getSdCardActiveStatus(statusPair);
if(result != HasReturnvaluesIF::RETURN_OK) { if(result != HasReturnvaluesIF::RETURN_OK) {
sif::warning << "Getting SD card activity status failed" << std::endl; sif::warning << "Getting SD card activity status failed" << std::endl;
} }
sif::info << "Cold redundant SD card configuration, preferred SD card: " << sif::info << "Cold redundant SD card configuration, preferred SD card: " <<
static_cast<int>(preferredSdCard) << std::endl; static_cast<int>(sdInfo.pref) << std::endl;
result = sdCardColdRedundantInit(sdcMan, statusPair); if(sdInfo.prefState == sd::SdState::MOUNTED) {
if(result == SdCardManager::ALREADY_ON) { sdInfo.state = SdStates::DETERMINE_OTHER;
state = SdStates::MOUNT;
} }
if(result == SdCardManager::ALREADY_MOUNTED) { else if(sdInfo.prefState == sd::SdState::OFF) {
state = SdStates::UPDATE_INFO; sdCardSetup(sdInfo.pref, sd::SdState::ON, sdInfo.prefChar);
sdInfo.commandExecuted = true;
} }
else { else if(sdInfo.prefState == sd::SdState::ON) {
commandExecuted = true; sdInfo.state = SdStates::MOUNT_SELF;
} }
} }
else { else {
SdCardManager::Operations operation; nonBlockingOpChecking(SdStates::MOUNT_SELF, 10, "Setting SDC state");
SdCardManager::OpStatus status = sdcMan->checkCurrentOp(operation);
if(status == SdCardManager::OpStatus::SUCCESS) {
state = SdStates::MOUNT;
commandExecuted = false;
sdcInitCycleCount = 0;
}
else if(sdcInitCycleCount > 10) {
sif::warning << "CoreController::initSdCardNonBlocking: "
"Setting SDC state takes too long" << std::endl;
}
} }
} }
if(state == SdStates::MOUNT) { if(sdInfo.state == SdStates::MOUNT_SELF) {
if(not commandExecuted) { if(not sdInfo.commandExecuted) {
result = sdCardColdRedundantInit(sdcMan, statusPair); sif::debug << "Mount self" << std::endl;
commandExecuted = false; result = sdCardSetup(sdInfo.pref, sd::SdState::MOUNTED, sdInfo.prefChar);
sdInfo.commandExecuted = true;
} }
else { else {
SdCardManager::Operations operation; nonBlockingOpChecking(SdStates::DETERMINE_OTHER, 5, "Mounting SD card");
SdCardManager::OpStatus status = sdcMan->checkCurrentOp(operation);
if(status == SdCardManager::OpStatus::SUCCESS) {
state = SdStates::UPDATE_INFO;
commandExecuted = false;
sdcInitCycleCount = 0;
}
else if(sdcInitCycleCount > 5) {
sif::warning << "CoreController::initSdCardNonBlocking: "
"Mounting SD card takes too long" << std::endl;
}
} }
} }
if(state == SdStates::UPDATE_INFO) { if(sdInfo.state == SdStates::DETERMINE_OTHER) {
if(not commandExecuted) { // Determine whether any additional operations have to be done for the other SD card
// 1. Cold redundant case: Other SD card needs to be unmounted and switched off
// 2. Hot redundant case: Other SD card needs to be mounted and switched on
#if Q7S_SD_CARD_CONFIG == Q7S_SD_COLD_REDUNDANT
if(sdInfo.otherState == sd::SdState::ON) {
sdInfo.state = SdStates::SET_STATE_OTHER;
}
else if(sdInfo.otherState == sd::SdState::MOUNTED) {
sdInfo.state = SdStates::MOUNT_UNMOUNT_OTHER;
}
else {
// Is already off, update info
sdInfo.state = SdStates::UPDATE_INFO;
}
#elif Q7S_SD_CARD_CONFIG == Q7S_SD_HOT_REDUNDANT
if(sdInfo.otherState == sd::SdState::OFF) {
sdInfo.state = SdStates::SET_STATE_OTHER;
}
else if(sdInfo.otherState == sd::SdState::ON) {
sdInfo.state = SdStates::MOUNT_UNMOUNT_OTHER;
}
else {
// Is already on and mounted, update info
sdInfo.state = SdStates::UPDATE_INFO;
}
#endif
}
if(sdInfo.state == SdStates::SET_STATE_OTHER) {
// Set state of other SD card to ON or OFF, depending on redundancy mode
#if Q7S_SD_CARD_CONFIG == Q7S_SD_COLD_REDUNDANT
if(not sdInfo.commandExecuted) {
sif::debug << "Set state other off" << std::endl;
result = sdCardSetup(sdInfo.pref, sd::SdState::OFF, sdInfo.prefChar);
sdInfo.commandExecuted = true;
}
#elif Q7S_SD_CARD_CONFIG == Q7S_SD_HOT_REDUNDANT
if(not sdInfo.commandExecuted) {
sif::debug << "Set state other on" << std::endl;
result = sdCardSetup(sdInfo.pref, sd::SdState::ON, sdInfo.prefChar);
sdInfo.commandExecuted = true;
}
#endif
else {
nonBlockingOpChecking(SdStates::MOUNT_UNMOUNT_OTHER, 10, "Switching off other SD card");
}
}
if(sdInfo.state == SdStates::MOUNT_UNMOUNT_OTHER) {
// Mount or unmount other SD card, depending on redundancy mode
#if Q7S_SD_CARD_CONFIG == Q7S_SD_COLD_REDUNDANT
if(not sdInfo.commandExecuted) {
sif::debug << "Unmount other" << std::endl;
result = sdCardSetup(sdInfo.pref, sd::SdState::ON, sdInfo.prefChar);
sdInfo.commandExecuted = true;
}
#elif Q7S_SD_CARD_CONFIG == Q7S_SD_HOT_REDUNDANT
if(not sdInfo.commandExecuted) {
result = sdCardSetup(sdInfo.pref, sd::SdState::MOUNTED, sdInfo.prefChar);
sdInfo.commandExecuted = true;
}
#endif
else {
#if Q7S_SD_CARD_CONFIG == Q7S_SD_COLD_REDUNDANT
std::string opPrint = "Switching off other SD card";
#elif Q7S_SD_CARD_CONFIG == Q7S_SD_HOT_REDUNDANT
std::string opPrint = "Mounting other SD card";
#endif
nonBlockingOpChecking(SdStates::MOUNT_UNMOUNT_OTHER, 10, opPrint);
}
}
if(sdInfo.state == SdStates::UPDATE_INFO) {
if(not sdInfo.commandExecuted) {
sif::debug << "update info" << std::endl;
// 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();
if(result != HasReturnvaluesIF::RETURN_OK) { if(result != HasReturnvaluesIF::RETURN_OK) {
sif::warning << "CoreController::initialize: Updating SD card state file failed" sif::warning << "CoreController::initialize: Updating SD card state file failed"
<< std::endl; << std::endl;
} }
commandExecuted = true; sdInfo.commandExecuted = false;
} sdInfo.state = SdStates::DONE;
else { sdInfo.initFinished = true;
SdCardManager::Operations operation; sdInfo.cycleCount = 0;
SdCardManager::OpStatus status = sdcMan->checkCurrentOp(operation); sdcMan->setBlocking(false);
if(status == SdCardManager::OpStatus::SUCCESS) { sif::debug << "Sd init done" << std::endl;
sdInitFinished = true;
state = SdStates::DONE;
}
else if(sdcInitCycleCount > 2) {
sif::warning << "CoreController::initSdCardNonBlocking: "
"Updating SDC file takes too long" << std::endl;
}
} }
} }
sdcInitCycleCount++; sdInfo.cycleCount++;
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
#endif /* Q7S_SD_CARD_CONFIG != Q7S_SD_NONE */ #endif /* Q7S_SD_CARD_CONFIG != Q7S_SD_NONE */
} }
ReturnValue_t CoreController::sdCardSetup(SdCardManager::SdStatusPair& statusPair, ReturnValue_t CoreController::sdCardSetup(sd::SdCard sdCard, sd::SdState targetState,
sd::SdCard sdCard, std::string sdString) { std::string sdChar) {
std::string mountString; std::string mountString;
if(sdCard == sd::SdCard::SLOT_0) { if(sdCard == sd::SdCard::SLOT_0) {
mountString = SdCardManager::SD_0_MOUNT_POINT; mountString = SdCardManager::SD_0_MOUNT_POINT;
@ -266,16 +329,25 @@ ReturnValue_t CoreController::sdCardSetup(SdCardManager::SdStatusPair& statusPai
mountString = SdCardManager::SD_1_MOUNT_POINT; mountString = SdCardManager::SD_1_MOUNT_POINT;
} }
sd::SdState status = sd::SdState::OFF; sd::SdState state = sd::SdState::OFF;
if(sdCard == sd::SdCard::SLOT_0) { if(sdCard == sd::SdCard::SLOT_0) {
status = statusPair.first; state = sdInfo.currentState.first;
} }
else { else {
status = statusPair.second; state = sdInfo.currentState.second;
} }
if(status == sd::SdState::MOUNTED) { if(state == sd::SdState::MOUNTED) {
if(targetState == sd::SdState::OFF) {
sif::info << "Switching off SD card" << sdChar << std::endl;
return sdcMan->switchOffSdCard(sdCard, true, &sdInfo.currentState);
}
else if(targetState == sd::SdState::ON) {
sif::info << "Unmounting SD card " << sdChar << std::endl;
return sdcMan->unmountSdCard(sdCard);
}
else {
if(std::filesystem::exists(mountString)) { if(std::filesystem::exists(mountString)) {
sif::info << "SD card " << sdString << " already on and mounted at " << sif::info << "SD card " << sdChar << " already on and mounted at " <<
mountString << std::endl; mountString << std::endl;
return SdCardManager::ALREADY_MOUNTED; return SdCardManager::ALREADY_MOUNTED;
} }
@ -283,34 +355,28 @@ ReturnValue_t CoreController::sdCardSetup(SdCardManager::SdStatusPair& statusPai
mountString << " not found!" << std::endl; mountString << " not found!" << std::endl;
return SdCardManager::MOUNT_ERROR; return SdCardManager::MOUNT_ERROR;
} }
}
if(status == sd::SdState::OFF) { if(state == sd::SdState::OFF) {
if(BLOCKING_SD_INIT) { if(targetState == sd::SdState::MOUNTED) {
sif::info << "Switching on and mounting SD card " << sdString << " at " << sif::info << "Switching on and mounting SD card " << sdChar << " at " <<
mountString << std::endl; mountString << std::endl;
return sdcMan->switchOnSdCard(sdCard, true, &statusPair); return sdcMan->switchOnSdCard(sdCard, true, &sdInfo.currentState);
}
else {
if(state == SdStates::SET_STATE_SELF) {
sif::info << "Switching on SD card" << std::endl;
return sdcMan->switchOnSdCard(sdCard, false, &statusPair);
} }
else if(targetState == sd::SdState::ON) {
sif::info << "Switching on SD card" << sdChar << std::endl;
return sdcMan->switchOnSdCard(sdCard, false, &sdInfo.currentState);
} }
} }
else if(status == sd::SdState::ON) { else if(state == sd::SdState::ON) {
if(BLOCKING_SD_INIT) { if(targetState == sd::SdState::MOUNTED) {
sif::info << "Mounting SD card " << sdString << " at " << mountString << std::endl; sif::info << "Mounting SD card " << sdChar << " at " << mountString << std::endl;
return sdcMan->mountSdCard(sdCard);
}
else {
if(state == SdStates::SET_STATE_SELF) {
return SdCardManager::ALREADY_ON;
}
if(state == SdStates::MOUNT) {
sif::info << "Mounting SD card " << sdString << " at " << mountString << std::endl;
return sdcMan->mountSdCard(sdCard); return sdcMan->mountSdCard(sdCard);
} }
else if(targetState == sd::SdState::OFF) {
sif::info << "Switching off SD card" << sdChar << std::endl;
return sdcMan->switchOffSdCard(sdCard, false, &sdInfo.currentState);
} }
} }
else { else {
@ -337,18 +403,16 @@ ReturnValue_t CoreController::executeAction(ActionId_t actionId, MessageQueueId_
ReturnValue_t CoreController::initializeAfterTaskCreation() { ReturnValue_t CoreController::initializeAfterTaskCreation() {
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK; ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
// TODO: Do this here because this can take a longer time. Later, use non-blocking mode
// to start doing stuff in ctor and initialize function.
// This operation can take 1-2 seconds on reboot, and around 50-100ms if the SD
// card is already on.
if(BLOCKING_SD_INIT) { if(BLOCKING_SD_INIT) {
ReturnValue_t result = initSdCardBlocking(); ReturnValue_t result = initSdCardBlocking();
if(result != HasReturnvaluesIF::RETURN_OK) { if(result != HasReturnvaluesIF::RETURN_OK and result != SdCardManager::ALREADY_MOUNTED) {
sif::warning << "CoreController::CoreController: SD card init failed" << std::endl; sif::warning << "CoreController::CoreController: SD card init failed" << std::endl;
} }
} }
else { else {
initSdCardNonBlocking(); if(not sdInfo.initFinished) {
sdInitStateMachine();
}
} }
result = initVersionFile(); result = initVersionFile();
if(result != HasReturnvaluesIF::RETURN_OK) { if(result != HasReturnvaluesIF::RETURN_OK) {
@ -358,54 +422,29 @@ ReturnValue_t CoreController::initializeAfterTaskCreation() {
return result; return result;
} }
ReturnValue_t CoreController::sdCardColdRedundantInit(SdCardManager* sdcMan, ReturnValue_t CoreController::sdColdRedundantBlockingInit() {
SdCardManager::SdStatusPair& statusPair, SdStates step) {
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK; ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
std::string preferredString;
sd::SdState otherStatus = sd::SdState::OFF; result = sdCardSetup(sdInfo.pref, sd::SdState::MOUNTED, sdInfo.prefChar);
std::string otherString;
sd::SdCard otherSdc = sd::SdCard::SLOT_0;
if(preferredSdCard == sd::SdCard::SLOT_0) {
preferredString = "0";
otherSdc = sd::SdCard::SLOT_1;
otherStatus = statusPair.second;
otherString = "1";
}
else {
preferredString = "1";
otherStatus = statusPair.first;
otherSdc = sd::SdCard::SLOT_0;
otherString = "0";
}
if(step != SdStates::SET_STATE_OTHER) {
result = sdCardSetup(statusPair, preferredSdCard, preferredString);
if(result != SdCardManager::ALREADY_MOUNTED and result != HasReturnvaluesIF::RETURN_OK) { if(result != SdCardManager::ALREADY_MOUNTED and result != HasReturnvaluesIF::RETURN_OK) {
sif::warning << "Setting up preferred card " << otherString << sif::warning << "Setting up preferred card " << sdInfo.otherChar <<
" in cold redundant mode failed" << std::endl; " in cold redundant mode failed" << std::endl;
// Try other SD card and mark set up operation as failed // Try other SD card and mark set up operation as failed
sdCardSetup(statusPair, preferredSdCard, preferredString); sdCardSetup(sdInfo.pref, sd::SdState::MOUNTED, sdInfo.prefChar);
result = HasReturnvaluesIF::RETURN_FAILED; result = HasReturnvaluesIF::RETURN_FAILED;
} }
}
if(BLOCKING_SD_INIT or (not BLOCKING_SD_INIT and state == SdStates::SET_STATE_OTHER)) { if(result != HasReturnvaluesIF::RETURN_FAILED and sdInfo.otherState != sd::SdState::OFF) {
// Don't disable other card if enabling the first one already failed sif::info << "Switching off secondary SD card " << sdInfo.otherChar << std::endl;
// Otherwise, try to turn off the other SD card
if(result != HasReturnvaluesIF::RETURN_FAILED and otherStatus != sd::SdState::OFF) {
sif::info << "Switching off secondary SD card " << otherString << std::endl;
// Switch off other SD card in cold redundant mode if setting up preferred one worked // Switch off other SD card in cold redundant mode if setting up preferred one worked
// without issues // without issues
ReturnValue_t result2 = sdcMan->switchOffSdCard(otherSdc, otherStatus, &statusPair); ReturnValue_t result2 = sdcMan->switchOffSdCard(sdInfo.other,
sdInfo.otherState, &sdInfo.currentState);
if(result2 != HasReturnvaluesIF::RETURN_OK and result2 != SdCardManager::ALREADY_OFF) { if(result2 != HasReturnvaluesIF::RETURN_OK and result2 != SdCardManager::ALREADY_OFF) {
sif::warning << "Switching off secondary SD card " << otherString << sif::warning << "Switching off secondary SD card " << sdInfo.otherChar <<
" in cold redundant mode failed" << std::endl; " in cold redundant mode failed" << std::endl;
} }
} }
}
return result; return result;
} }
@ -665,13 +704,13 @@ CoreController::~CoreController() {
} }
void CoreController::determinePreferredSdCard() { void CoreController::determinePreferredSdCard() {
if(preferredSdCard == sd::SdCard::NONE) { if(sdInfo.pref == sd::SdCard::NONE) {
ReturnValue_t result = sdcMan->getPreferredSdCard(preferredSdCard); ReturnValue_t result = sdcMan->getPreferredSdCard(sdInfo.pref);
if(result != HasReturnvaluesIF::RETURN_OK) { if(result != HasReturnvaluesIF::RETURN_OK) {
if(result == scratch::KEY_NOT_FOUND) { if(result == scratch::KEY_NOT_FOUND) {
sif::warning << "CoreController::sdCardInit: " sif::warning << "CoreController::sdCardInit: "
"Preferred SD card not set. Setting to 0" << std::endl; "Preferred SD card not set. Setting to 0" << std::endl;
sdcMan->setPreferredSdCard(preferredSdCard); sdcMan->setPreferredSdCard(sdInfo.pref);
} }
else { else {
sif::warning << "CoreController::sdCardInit: Could not get preferred SD card" sif::warning << "CoreController::sdCardInit: Could not get preferred SD card"
@ -681,6 +720,28 @@ void CoreController::determinePreferredSdCard() {
} }
} }
void CoreController::updateSdInfoOther() {
if(sdInfo.pref == sd::SdCard::SLOT_0) {
sdInfo.prefChar = "0";
sdInfo.otherChar = "1";
sdInfo.otherState = sdInfo.currentState.second;
sdInfo.prefState = sdInfo.currentState.first;
sdInfo.other = sd::SdCard::SLOT_1;
}
else {
sdInfo.prefChar = "1";
sdInfo.otherChar = "0";
sdInfo.otherState = sdInfo.currentState.first;
sdInfo.prefState = sdInfo.currentState.second;
sdInfo.other = sd::SdCard::SLOT_0;
}
}
bool CoreController::sdInitFinished() const {
return sdInfo.initFinished;
}
void CoreController::performWatchdogControlOperation() { void CoreController::performWatchdogControlOperation() {
// Only perform each fifth iteration // Only perform each fifth iteration
if(watchdogFifoFd != 0 and opDivider.checkAndIncrement()) { if(watchdogFifoFd != 0 and opDivider.checkAndIncrement()) {

View File

@ -48,6 +48,7 @@ public:
static ReturnValue_t incrementAllocationFailureCount(); static ReturnValue_t incrementAllocationFailureCount();
static void getCurrentBootCopy(Chip& chip, Copy& copy); static void getCurrentBootCopy(Chip& chip, Copy& copy);
bool sdInitFinished() const;
private: private:
static Chip currentChip; static Chip currentChip;
@ -65,26 +66,40 @@ private:
START, START,
GET_INFO, GET_INFO,
SET_STATE_SELF, SET_STATE_SELF,
MOUNT_SELF,
DETERMINE_OTHER,
SET_STATE_OTHER, SET_STATE_OTHER,
MOUNT, // Mount or unmount other
MOUNT_UNMOUNT_OTHER,
UPDATE_INFO, UPDATE_INFO,
DONE DONE
}; };
static constexpr bool BLOCKING_SD_INIT = false; static constexpr bool BLOCKING_SD_INIT = false;
SdCardManager* sdcMan = nullptr; SdCardManager* sdcMan = nullptr;
uint16_t sdcInitCycleCount = 0;
ReturnValue_t initSdCardBlocking(); ReturnValue_t initSdCardBlocking();
ReturnValue_t initSdCardNonBlocking(); ReturnValue_t sdInitStateMachine();
sd::SdCard preferredSdCard = sd::SdCard::NONE;
struct SdInfo {
sd::SdCard pref = sd::SdCard::NONE;
sd::SdState prefState = sd::SdState::OFF;
sd::SdCard other = sd::SdCard::NONE;
sd::SdState otherState = sd::SdState::OFF;
std::string prefChar = "0";
std::string otherChar = "1";
SdStates state = SdStates::START; SdStates state = SdStates::START;
SdCardManager::SdStatusPair statusPair;
bool commandExecuted = true; bool commandExecuted = true;
bool sdInitFinished = false; bool initFinished = false;
ReturnValue_t sdCardSetup(SdCardManager::SdStatusPair& statusPair, SdCardManager::SdStatePair currentState;
sd::SdCard sdCard, std::string sdString); uint16_t cycleCount = 0;
ReturnValue_t sdCardColdRedundantInit(SdCardManager* sdcMan, };
SdCardManager::SdStatusPair& statusPair, SdStates step = SdStates::NONE);
SdInfo sdInfo;
void updateSdInfoOther();
ReturnValue_t sdCardSetup(sd::SdCard sdCard, sd::SdState targetState, std::string sdChar);
ReturnValue_t sdColdRedundantBlockingInit();
void determinePreferredSdCard(); void determinePreferredSdCard();
ReturnValue_t initVersionFile(); ReturnValue_t initVersionFile();

View File

@ -41,8 +41,10 @@ void FileSystemHandler::fileSystemHandlerLoop() {
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK; ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
while(true) { while(true) {
if(opCounter % 5 == 0) { if(opCounter % 5 == 0) {
if(coreCtrl->sdInitFinished()) {
fileSystemCheckup(); fileSystemCheckup();
} }
}
result = mq->receiveMessage(&filemsg); result = mq->receiveMessage(&filemsg);
if(result == MessageQueueIF::EMPTY) { if(result == MessageQueueIF::EMPTY) {
break; break;
@ -72,7 +74,7 @@ void FileSystemHandler::fileSystemHandlerLoop() {
} }
void FileSystemHandler::fileSystemCheckup() { void FileSystemHandler::fileSystemCheckup() {
SdCardManager::SdStatusPair statusPair; SdCardManager::SdStatePair statusPair;
sdcMan->getSdCardActiveStatus(statusPair); sdcMan->getSdCardActiveStatus(statusPair);
sd::SdCard preferredSdCard; sd::SdCard preferredSdCard;
sdcMan->getPreferredSdCard(preferredSdCard); sdcMan->getPreferredSdCard(preferredSdCard);
@ -111,6 +113,11 @@ MessageQueueId_t FileSystemHandler::getCommandQueue() const {
} }
ReturnValue_t FileSystemHandler::initialize() { ReturnValue_t FileSystemHandler::initialize() {
coreCtrl = ObjectManager::instance()->get<CoreController>(objects::CORE_CONTROLLER);
if(coreCtrl == nullptr) {
sif::error << "FileSystemHandler::initialize: Could not retrieve core controller handle" <<
std::endl;
}
sdcMan = SdCardManager::instance(); sdcMan = SdCardManager::instance();
sd::SdCard preferredSdCard; sd::SdCard preferredSdCard;
ReturnValue_t result = sdcMan->getPreferredSdCard(preferredSdCard); ReturnValue_t result = sdcMan->getPreferredSdCard(preferredSdCard);

View File

@ -11,6 +11,8 @@
#include <string> #include <string>
class CoreController;
class FileSystemHandler: public SystemObject, class FileSystemHandler: public SystemObject,
public ExecutableObjectIF, public ExecutableObjectIF,
public HasFileSystemIF { public HasFileSystemIF {
@ -47,6 +49,7 @@ public:
void* args = nullptr) override; void* args = nullptr) override;
private: private:
CoreController* coreCtrl = nullptr;
MessageQueueIF* mq = nullptr; MessageQueueIF* mq = nullptr;
std::string currentMountPrefix = SdCardManager::SD_0_MOUNT_POINT; std::string currentMountPrefix = SdCardManager::SD_0_MOUNT_POINT;
static constexpr uint32_t FS_MAX_QUEUE_SIZE = config::OBSW_FILESYSTEM_HANDLER_QUEUE_SIZE; static constexpr uint32_t FS_MAX_QUEUE_SIZE = config::OBSW_FILESYSTEM_HANDLER_QUEUE_SIZE;

View File

@ -34,11 +34,18 @@ SdCardManager* SdCardManager::instance() {
} }
ReturnValue_t SdCardManager::switchOnSdCard(sd::SdCard sdCard, bool doMountSdCard, ReturnValue_t SdCardManager::switchOnSdCard(sd::SdCard sdCard, bool doMountSdCard,
SdStatusPair* statusPair) { SdStatePair* statusPair) {
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK; ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
std::unique_ptr<SdStatusPair> sdStatusPtr; if(doMountSdCard) {
if(not blocking) {
sif::warning << "SdCardManager::switchOnSdCard: Two-step command but manager is"
" not configured for blocking operation. Forcing blocking mode.." << std::endl;
blocking = true;
}
}
std::unique_ptr<SdStatePair> sdStatusPtr;
if(statusPair == nullptr) { if(statusPair == nullptr) {
sdStatusPtr = std::make_unique<SdStatusPair>(); sdStatusPtr = std::make_unique<SdStatePair>();
statusPair = sdStatusPtr.get(); statusPair = sdStatusPtr.get();
result = getSdCardActiveStatus(*statusPair); result = getSdCardActiveStatus(*statusPair);
if(result != HasReturnvaluesIF::RETURN_OK) { if(result != HasReturnvaluesIF::RETURN_OK) {
@ -91,12 +98,19 @@ ReturnValue_t SdCardManager::switchOnSdCard(sd::SdCard sdCard, bool doMountSdCar
} }
ReturnValue_t SdCardManager::switchOffSdCard(sd::SdCard sdCard, bool doUnmountSdCard, ReturnValue_t SdCardManager::switchOffSdCard(sd::SdCard sdCard, bool doUnmountSdCard,
SdStatusPair* statusPair) { SdStatePair* statusPair) {
std::pair<sd::SdState, sd::SdState> active; std::pair<sd::SdState, sd::SdState> active;
ReturnValue_t result = getSdCardActiveStatus(active); ReturnValue_t result = getSdCardActiveStatus(active);
if(result != HasReturnvaluesIF::RETURN_OK) { if(result != HasReturnvaluesIF::RETURN_OK) {
return result; return result;
} }
if(doUnmountSdCard) {
if(not blocking) {
sif::warning << "SdCardManager::switchOffSdCard: Two-step command but manager is"
" not configured for blocking operation. Forcing blocking mode.." << std::endl;
blocking = true;
}
}
// Not allowed, this function turns off one SD card // Not allowed, this function turns off one SD card
if(sdCard == sd::SdCard::BOTH) { if(sdCard == sd::SdCard::BOTH) {
sif::warning << "SdCardManager::switchOffSdCard: API does not allow sd::SdStatus::BOTH" sif::warning << "SdCardManager::switchOffSdCard: API does not allow sd::SdStatus::BOTH"
@ -155,7 +169,7 @@ ReturnValue_t SdCardManager::setSdCardState(sd::SdCard sdCard, bool on) {
return result; return result;
} }
ReturnValue_t SdCardManager::getSdCardActiveStatus(SdStatusPair& active) { ReturnValue_t SdCardManager::getSdCardActiveStatus(SdStatePair& active) {
using namespace std; using namespace std;
if(not filesystem::exists(SD_STATE_FILE)) { if(not filesystem::exists(SD_STATE_FILE)) {
return STATUS_FILE_NEXISTS; return STATUS_FILE_NEXISTS;
@ -253,23 +267,34 @@ ReturnValue_t SdCardManager::unmountSdCard(sd::SdCard sdCard) {
return result; return result;
} }
ReturnValue_t SdCardManager::sanitizeState(SdStatusPair* statusPair, sd::SdCard prefSdCard) { ReturnValue_t SdCardManager::sanitizeState(SdStatePair* statusPair, sd::SdCard prefSdCard) {
std::unique_ptr<SdStatusPair> sdStatusPtr; std::unique_ptr<SdStatePair> sdStatusPtr;
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
// Enforce blocking operation for now. Be careful to reset it when returning prematurely!
bool resetNonBlockingState = false;
if(not this->blocking) {
blocking = true;
resetNonBlockingState = true;
}
if(prefSdCard == sd::SdCard::NONE) { if(prefSdCard == sd::SdCard::NONE) {
ReturnValue_t result = getPreferredSdCard(prefSdCard); result = getPreferredSdCard(prefSdCard);
if(result != HasReturnvaluesIF::RETURN_OK) {} if(result != HasReturnvaluesIF::RETURN_OK) {}
} }
if(statusPair == nullptr) { if(statusPair == nullptr) {
sdStatusPtr = std::make_unique<SdStatusPair>(); sdStatusPtr = std::make_unique<SdStatePair>();
statusPair = sdStatusPtr.get(); statusPair = sdStatusPtr.get();
getSdCardActiveStatus(*statusPair); getSdCardActiveStatus(*statusPair);
} }
if(statusPair->first == sd::SdState::ON) { if(statusPair->first == sd::SdState::ON) {
return mountSdCard(prefSdCard); result = mountSdCard(prefSdCard);
} }
return switchOnSdCard(prefSdCard, true, statusPair); result = switchOnSdCard(prefSdCard, true, statusPair);
if(resetNonBlockingState) {
blocking = false;
}
return result;
} }
void SdCardManager::resetState() { void SdCardManager::resetState() {

View File

@ -42,7 +42,7 @@ public:
FAIL FAIL
}; };
using SdStatusPair = std::pair<sd::SdState, sd::SdState>; using SdStatePair = std::pair<sd::SdState, sd::SdState>;
static constexpr uint8_t INTERFACE_ID = CLASS_ID::SD_CARD_MANAGER; static constexpr uint8_t INTERFACE_ID = CLASS_ID::SD_CARD_MANAGER;
@ -114,7 +114,7 @@ public:
* SYSTEM_CALL_ERROR on system error * SYSTEM_CALL_ERROR on system error
*/ */
ReturnValue_t switchOnSdCard(sd::SdCard sdCard, bool doMountSdCard = true, ReturnValue_t switchOnSdCard(sd::SdCard sdCard, bool doMountSdCard = true,
SdStatusPair* statusPair = nullptr); SdStatePair* statusPair = nullptr);
/** /**
* Switch off the specified SD card. * Switch off the specified SD card.
@ -126,7 +126,7 @@ public:
* SYSTEM_CALL_ERROR on system error * SYSTEM_CALL_ERROR on system error
*/ */
ReturnValue_t switchOffSdCard(sd::SdCard sdCard, bool doUnmountSdCard = true, ReturnValue_t switchOffSdCard(sd::SdCard sdCard, bool doUnmountSdCard = true,
SdStatusPair* statusPair = nullptr); SdStatePair* statusPair = nullptr);
/** /**
* 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
@ -149,7 +149,7 @@ public:
* should call #updateSdCardStateFile again in that case * should call #updateSdCardStateFile again in that case
* - STATUS_FILE_NEXISTS if the status file does not exist * - STATUS_FILE_NEXISTS if the status file does not exist
*/ */
ReturnValue_t getSdCardActiveStatus(SdStatusPair& active); ReturnValue_t getSdCardActiveStatus(SdStatePair& active);
/** /**
* Mount the specified SD card. This is necessary to use it. * Mount the specified SD card. This is necessary to use it.
@ -169,13 +169,13 @@ public:
* In case that there is a discrepancy between the preferred SD card and the currently * In case that there is a discrepancy between the preferred SD card and the currently
* mounted one, this function will sanitize the state by attempting to mount the * mounted one, this function will sanitize the state by attempting to mount the
* currently preferred SD card. If the caller already has state information, it can be * currently preferred SD card. If the caller already has state information, it can be
* passed into the function. * passed into the function. For now, this operation will be enforced in blocking mode.
* @param statusPair Current SD card status capture with #getSdCardActiveStatus * @param statusPair Current SD card status capture with #getSdCardActiveStatus
* @param prefSdCard Preferred SD card captured with #getPreferredSdCard * @param prefSdCard Preferred SD card captured with #getPreferredSdCard
* @throws std::bad_alloc if one of the two arguments was a nullptr and an allocation failed * @throws std::bad_alloc if one of the two arguments was a nullptr and an allocation failed
* @return * @return
*/ */
ReturnValue_t sanitizeState(SdStatusPair* statusPair = nullptr, ReturnValue_t sanitizeState(SdStatePair* statusPair = nullptr,
sd::SdCard prefSdCard = sd::SdCard::NONE); sd::SdCard prefSdCard = sd::SdCard::NONE);
/** /**
@ -203,7 +203,7 @@ private:
ReturnValue_t setSdCardState(sd::SdCard sdCard, bool on); ReturnValue_t setSdCardState(sd::SdCard sdCard, bool on);
void processSdStatusLine(SdStatusPair& active, std::string& line, uint8_t& idx, void processSdStatusLine(SdStatePair& active, std::string& line, uint8_t& idx,
sd::SdCard& currentSd); sd::SdCard& currentSd);
std::string currentPrefix; std::string currentPrefix;