Merge pull request 'Non Blocking SD Card Initialization' (#75) from mueller/non-blocking-sd-card-init into develop
All checks were successful
EIVE/eive-obsw/pipeline/head This commit looks good

Reviewed-on: #75
Reviewed-by: Jakob.Meier <meierj@irs.uni-stuttgart.de>
This commit is contained in:
Jakob Meier 2021-08-09 14:18:49 +02:00
commit 6e22c3e017
11 changed files with 781 additions and 186 deletions

View File

@ -4,7 +4,9 @@
#include "watchdogConf.h" #include "watchdogConf.h"
#include "fsfw/FSFWVersion.h" #include "fsfw/FSFWVersion.h"
#include "fsfw/timemanager/Stopwatch.h"
#include "fsfw/serviceinterface/ServiceInterface.h" #include "fsfw/serviceinterface/ServiceInterface.h"
#include "fsfw/osal/linux/Timer.h"
#if OBSW_USE_TMTC_TCP_BRIDGE == 0 #if OBSW_USE_TMTC_TCP_BRIDGE == 0
#include "fsfw/osal/common/UdpTmTcBridge.h" #include "fsfw/osal/common/UdpTmTcBridge.h"
#else #else
@ -32,15 +34,20 @@ CoreController::CoreController(object_id_t objectId):
sif::warning << "CoreController::CoreController: Watchdog FIFO init failed" << sif::warning << "CoreController::CoreController: Watchdog FIFO init failed" <<
std::endl; std::endl;
} }
result = initSdCard(); sdcMan = SdCardManager::instance();
if(result != HasReturnvaluesIF::RETURN_OK) { if(sdcMan == nullptr) {
sif::warning << "CoreController::CoreController: SD card init failed" << std::endl; sif::error << "CoreController::CoreController: SD card manager invalid!" << std::endl;
} }
if(not BLOCKING_SD_INIT) {
sdcMan->setBlocking(false);
}
sdStateMachine();
result = initBootCopy(); result = initBootCopy();
if(result != HasReturnvaluesIF::RETURN_OK) { if(result != HasReturnvaluesIF::RETURN_OK) {
sif::warning << "CoreController::CoreController: Boot copy init" << std::endl; sif::warning << "CoreController::CoreController: Boot copy init" << std::endl;
} }
} }
catch(const std::filesystem::filesystem_error& e) { catch(const std::filesystem::filesystem_error& e) {
sif::error << "CoreController::CoreController: Failed with exception " << sif::error << "CoreController::CoreController: Failed with exception " <<
@ -54,6 +61,7 @@ ReturnValue_t CoreController::handleCommandMessage(CommandMessage *message) {
void CoreController::performControlOperation() { void CoreController::performControlOperation() {
performWatchdogControlOperation(); performWatchdogControlOperation();
sdStateMachine();
} }
ReturnValue_t CoreController::initializeLocalDataPool(localpool::DataPool &localDataPoolMap, ReturnValue_t CoreController::initializeLocalDataPool(localpool::DataPool &localDataPoolMap,
@ -74,6 +82,7 @@ ReturnValue_t CoreController::initialize() {
"count failed" << std::endl; "count failed" << std::endl;
} }
sdStateMachine();
return ExtendedControllerBase::initialize(); return ExtendedControllerBase::initialize();
} }
@ -82,35 +91,36 @@ ReturnValue_t CoreController::checkModeCommand(Mode_t mode, Submode_t submode,
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
ReturnValue_t CoreController::initSdCard() { ReturnValue_t CoreController::initSdCardBlocking() {
#if Q7S_SD_CARD_CONFIG == Q7S_SD_NONE
sif::info << "No SD card initialization will be performed" << std::endl;
return HasReturnvaluesIF::RETURN_OK;
#else
SdCardManager* sdcMan = SdCardManager::instance();
if(sdcMan == nullptr) {
return HasReturnvaluesIF::RETURN_FAILED;
}
// Create update status file // Create update status file
ReturnValue_t result = sdcMan->updateSdCardStateFile(); ReturnValue_t 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;
} }
#if Q7S_SD_CARD_CONFIG == Q7S_SD_NONE
sif::info << "No SD card initialization will be performed" << std::endl;
return HasReturnvaluesIF::RETURN_OK;
#else
auto statusPair = SdCardManager::SdStatusPair(sd::SdStatus::OFF, sd::SdStatus::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
return sdCardColdRedundantInit(sdcMan, statusPair); determinePreferredSdCard();
updateSdInfoOther();
sif::info << "Cold redundant SD card configuration, preferred SD card: " <<
static_cast<int>(sdInfo.pref) << std::endl;
result = sdColdRedundantBlockingInit();
// Update status file
sdcMan->updateSdCardStateFile();
return result;
#elif Q7S_SD_CARD_CONFIG == Q7S_SD_HOT_REDUNDANT #elif Q7S_SD_CARD_CONFIG == Q7S_SD_HOT_REDUNDANT
sif::info << "Hot redundant SD card configuration" << std::endl; sif::info << "Hot redundant SD card configuration" << std::endl;
sdCardSetup(sd::SdCard::SLOT_0, sd::SdState::MOUNTED, "0", false);
setUpSdCard(sd::SdCard::SLOT_0, sdStatus.first, "0"); sdCardSetup(sd::SdCard::SLOT_1, sd::SdState::MOUNTED, "1", false);
setUpSdCard(sd::SdCard::SLOT_1, sdStatus.second, "1");
// Update status file // Update status file
sdcMan->updateSdCardStateFile(); sdcMan->updateSdCardStateFile();
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
@ -120,10 +130,332 @@ ReturnValue_t CoreController::initSdCard() {
} }
ReturnValue_t CoreController::sdCardSetup(SdCardManager& sdcMan, ReturnValue_t CoreController::sdStateMachine() {
SdCardManager::SdStatusPair& statusPair,sd::SdCard sdCard, sd::SdStatus status, ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
std::string sdString) { SdCardManager::Operations operation;
if(sdInfo.state == SdStates::IDLE) {
// Nothing to do
return result;
}
if(sdInfo.state == SdStates::START) {
// Init will be performed by separate function
if(BLOCKING_SD_INIT) {
sdInfo.state = SdStates::IDLE;
sdInfo.initFinished = true;
return result;
}
else {
// Still update SD state file
#if Q7S_SD_CARD_CONFIG == Q7S_SD_NONE
sdInfo.state = SdStates::UPDATE_INFO;
#else
sdInfo.cycleCount = 0;
sdInfo.commandExecuted = false;
sdInfo.state = SdStates::GET_INFO;
#endif
}
}
// This lambda checks the non-blocking operation and assigns the new state on success.
// It returns true for an operation success and false otherwise
auto nonBlockingOpChecking = [&](SdStates newStateOnSuccess,
uint16_t maxCycleCount, std::string opPrintout) {
SdCardManager::OpStatus status = sdcMan->checkCurrentOp(operation);
if(status == SdCardManager::OpStatus::SUCCESS) {
sdInfo.state = newStateOnSuccess;
sdInfo.commandExecuted = false;
sdInfo.cycleCount = 0;
return true;
}
else if(sdInfo.cycleCount > 4) {
sif::warning << "CoreController::sdInitStateMachine: " << opPrintout <<
" takes too long" << std::endl;
return false;
}
return false;
};
if(sdInfo.state == SdStates::GET_INFO) {
if(not sdInfo.commandExecuted) {
// Create update status file
result = sdcMan->updateSdCardStateFile();
if(result != HasReturnvaluesIF::RETURN_OK) {
sif::warning << "CoreController::initialize: Updating SD card state file failed"
<< std::endl;
}
sdInfo.commandExecuted = true;
}
else {
nonBlockingOpChecking(SdStates::SET_STATE_SELF, 4, "Updating SDC file");
}
}
if(sdInfo.state == SdStates::SET_STATE_SELF) {
if(not sdInfo.commandExecuted) {
result = sdcMan->getSdCardActiveStatus(sdInfo.currentState);
determinePreferredSdCard();
updateSdInfoOther();
if(result != HasReturnvaluesIF::RETURN_OK) {
sif::warning << "Getting SD card activity status failed" << std::endl;
}
#if Q7S_SD_CARD_CONFIG == Q7S_SD_COLD_REDUNDANT
sif::info << "Cold redundant SD card configuration, preferred SD card: " <<
static_cast<int>(sdInfo.pref) << std::endl;
#endif
if(sdInfo.prefState == sd::SdState::MOUNTED) {
#if OBSW_VERBOSE_LEVEL >= 1
std::string mountString; std::string mountString;
if(sdInfo.pref == sd::SdCard::SLOT_0) {
mountString = SdCardManager::SD_0_MOUNT_POINT;
}
else {
mountString = SdCardManager::SD_1_MOUNT_POINT;
}
sif::info << "SD card " << sdInfo.prefChar << " already on and mounted at " <<
mountString << std::endl;
#endif
sdInfo.state = SdStates::DETERMINE_OTHER;
}
else if(sdInfo.prefState == sd::SdState::OFF) {
sdCardSetup(sdInfo.pref, sd::SdState::ON, sdInfo.prefChar, false);
sdInfo.commandExecuted = true;
}
else if(sdInfo.prefState == sd::SdState::ON) {
sdInfo.state = SdStates::MOUNT_SELF;
}
}
else {
if(nonBlockingOpChecking(SdStates::MOUNT_SELF, 10, "Setting SDC state")) {
sdInfo.prefState = sd::SdState::ON;
currentStateSetter(sdInfo.pref, sd::SdState::ON);
}
}
}
if(sdInfo.state == SdStates::MOUNT_SELF) {
if(not sdInfo.commandExecuted) {
result = sdCardSetup(sdInfo.pref, sd::SdState::MOUNTED, sdInfo.prefChar);
sdInfo.commandExecuted = true;
}
else {
if(nonBlockingOpChecking(SdStates::DETERMINE_OTHER, 5, "Mounting SD card")) {
sdInfo.prefState = sd::SdState::MOUNTED;
currentStateSetter(sdInfo.pref, sd::SdState::MOUNTED);
}
}
}
if(sdInfo.state == SdStates::DETERMINE_OTHER) {
// 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, but with a small delay
sdInfo.state = SdStates::SKIP_CYCLE_BEFORE_INFO_UPDATE;
}
#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::SKIP_CYCLE_BEFORE_INFO_UPDATE;
}
#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) {
result = sdCardSetup(sdInfo.other, sd::SdState::OFF, sdInfo.otherChar, false);
sdInfo.commandExecuted = true;
}
else {
if(nonBlockingOpChecking(SdStates::SKIP_CYCLE_BEFORE_INFO_UPDATE, 10,
"Switching off other SD card")) {
sdInfo.otherState = sd::SdState::OFF;
currentStateSetter(sdInfo.other, sd::SdState::OFF);
}
}
#elif Q7S_SD_CARD_CONFIG == Q7S_SD_HOT_REDUNDANT
if(not sdInfo.commandExecuted) {
result = sdCardSetup(sdInfo.other, sd::SdState::ON, sdInfo.otherChar, false);
sdInfo.commandExecuted = true;
}
else {
if(nonBlockingOpChecking(SdStates::MOUNT_UNMOUNT_OTHER, 10,
"Switching on other SD card")) {
sdInfo.otherState = sd::SdState::ON;
currentStateSetter(sdInfo.other, sd::SdState::ON);
}
}
#endif
}
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) {
result = sdCardSetup(sdInfo.other, sd::SdState::ON, sdInfo.otherChar);
sdInfo.commandExecuted = true;
}
else {
if(nonBlockingOpChecking(SdStates::SET_STATE_OTHER, 10, "Unmounting other SD card")) {
sdInfo.otherState = sd::SdState::ON;
currentStateSetter(sdInfo.other, sd::SdState::ON);
}
}
#elif Q7S_SD_CARD_CONFIG == Q7S_SD_HOT_REDUNDANT
if(not sdInfo.commandExecuted) {
result = sdCardSetup(sdInfo.other, sd::SdState::MOUNTED, sdInfo.otherChar);
sdInfo.commandExecuted = true;
}
else {
if(nonBlockingOpChecking(SdStates::UPDATE_INFO, 4, "Mounting other SD card")) {
sdInfo.otherState = sd::SdState::MOUNTED;
currentStateSetter(sdInfo.other, sd::SdState::MOUNTED);
}
}
#endif
}
if(sdInfo.state == SdStates::SKIP_CYCLE_BEFORE_INFO_UPDATE) {
sdInfo.state = SdStates::UPDATE_INFO;
}
else if(sdInfo.state == 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();
if(result != HasReturnvaluesIF::RETURN_OK) {
sif::warning << "CoreController::initialize: Updating SD card state file failed"
<< std::endl;
}
sdInfo.commandExecuted = false;
sdInfo.state = SdStates::IDLE;
sdInfo.cycleCount = 0;
sdcMan->setBlocking(false);
sdcMan->getSdCardActiveStatus(sdInfo.currentState);
if(not sdInfo.initFinished) {
updateSdInfoOther();
sdInfo.initFinished = true;
sif::info << "SD card initialization finished" << std::endl;
}
}
if(sdInfo.state == SdStates::SET_STATE_FROM_COMMAND) {
if(not sdInfo.commandExecuted) {
executeNextExternalSdCommand();
}
else {
checkExternalSdCommandStatus();
}
}
sdInfo.cycleCount++;
return HasReturnvaluesIF::RETURN_OK;
}
void CoreController::executeNextExternalSdCommand() {
std::string sdChar;
sd::SdState currentStateOfCard = sd::SdState::OFF;
if(sdInfo.commandedCard == sd::SdCard::SLOT_0) {
sdChar = "0";
currentStateOfCard = sdInfo.currentState.first;
}
else {
sdChar = "1";
currentStateOfCard = sdInfo.currentState.second;
}
if(currentStateOfCard == sd::SdState::OFF) {
if(sdInfo.commandedState == sd::SdState::ON) {
sdInfo.currentlyCommandedState = sdInfo.commandedState;
}
else if(sdInfo.commandedState == sd::SdState::MOUNTED) {
sdInfo.currentlyCommandedState = sd::SdState::ON;
}
else {
// SD card is already on target state
sdInfo.commandFinished = true;
sdInfo.state = SdStates::IDLE;
}
}
else if(currentStateOfCard == sd::SdState::ON) {
if(sdInfo.commandedState == sd::SdState::OFF or
sdInfo.commandedState == sd::SdState::MOUNTED) {
sdInfo.currentlyCommandedState = sdInfo.commandedState;
}
else {
// Already on target state
sdInfo.commandFinished = true;
sdInfo.state = SdStates::IDLE;
}
}
else if(currentStateOfCard == sd::SdState::MOUNTED) {
if(sdInfo.commandedState == sd::SdState::ON) {
sdInfo.currentlyCommandedState = sdInfo.commandedState;
}
else if(sdInfo.commandedState == sd::SdState::OFF) {
// This causes an unmount in sdCardSetup
sdInfo.currentlyCommandedState = sd::SdState::ON;
}
else {
sdInfo.commandFinished = true;
}
}
sdCardSetup(sdInfo.commandedCard, sdInfo.commandedState, sdChar);
sdInfo.commandExecuted = true;
}
void CoreController::checkExternalSdCommandStatus() {
SdCardManager::Operations operation;
SdCardManager::OpStatus status = sdcMan->checkCurrentOp(operation);
if(status == SdCardManager::OpStatus::SUCCESS) {
if(sdInfo.currentlyCommandedState == sdInfo.commandedState) {
sdInfo.state = SdStates::SKIP_CYCLE_BEFORE_INFO_UPDATE;
sdInfo.commandFinished = true;
}
else {
// stay on same state machine state because the target state was not reached yet.
sdInfo.cycleCount = 0;
}
currentStateSetter(sdInfo.commandedCard, sdInfo.currentlyCommandedState);
sdInfo.commandExecuted = false;
}
else if(sdInfo.cycleCount > 4) {
sif::warning << "CoreController::sdStateMachine: Commanding SD state "
"takes too long" << std::endl;
}
}
void CoreController::currentStateSetter(sd::SdCard sdCard, sd::SdState newState) {
if(sdCard == sd::SdCard::SLOT_0) {
sdInfo.currentState.first = newState;
}
else {
sdInfo.currentState.second = newState;
}
}
ReturnValue_t CoreController::sdCardSetup(sd::SdCard sdCard, sd::SdState targetState,
std::string sdChar, bool printOutput) {
std::string mountString;
sdcMan->setPrintCommandOutput(printOutput);
if(sdCard == sd::SdCard::SLOT_0) { if(sdCard == sd::SdCard::SLOT_0) {
mountString = SdCardManager::SD_0_MOUNT_POINT; mountString = SdCardManager::SD_0_MOUNT_POINT;
} }
@ -131,27 +463,63 @@ ReturnValue_t CoreController::sdCardSetup(SdCardManager& sdcMan,
mountString = SdCardManager::SD_1_MOUNT_POINT; mountString = SdCardManager::SD_1_MOUNT_POINT;
} }
if(status == sd::SdStatus::OFF) { sd::SdState state = sd::SdState::OFF;
sif::info << "Switching on and mounting SD card " << sdString << " at " << if(sdCard == sd::SdCard::SLOT_0) {
mountString << std::endl; state = sdInfo.currentState.first;
return sdcMan.switchOnSdCard(sdCard, true, &statusPair);
} }
else if(status == sd::SdStatus::ON) { else {
sif::info << "Mounting SD card " << sdString << " at " << mountString << std::endl; state = sdInfo.currentState.second;
return sdcMan.mountSdCard(sdCard); }
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 { 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;
} }
sif::error << "SD card mounted but expected mount point " << mountString << " not found!" sif::error << "SD card mounted but expected mount point " <<
<< std::endl; mountString << " not found!" << std::endl;
return SdCardManager::MOUNT_ERROR; return SdCardManager::MOUNT_ERROR;
} }
} }
if(state == sd::SdState::OFF) {
if(targetState == sd::SdState::MOUNTED) {
sif::info << "Switching on and mounting SD card " << sdChar << " at " <<
mountString << std::endl;
return sdcMan->switchOnSdCard(sdCard, true, &sdInfo.currentState);
}
else if(targetState == sd::SdState::ON) {
sif::info << "Switching on SD card " << sdChar << std::endl;
return sdcMan->switchOnSdCard(sdCard, false, &sdInfo.currentState);
}
}
else if(state == sd::SdState::ON) {
if(targetState == sd::SdState::MOUNTED) {
sif::info << "Mounting SD card " << sdChar << " at " << mountString << std::endl;
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 {
sif::warning << "CoreController::sdCardSetup: Invalid state for this call" << std::endl;
}
return HasReturnvaluesIF::RETURN_OK;
}
ReturnValue_t CoreController::executeAction(ActionId_t actionId, MessageQueueId_t commandedBy, ReturnValue_t CoreController::executeAction(ActionId_t actionId, MessageQueueId_t commandedBy,
const uint8_t *data, size_t size) { const uint8_t *data, size_t size) {
switch(actionId) { switch(actionId) {
@ -168,7 +536,15 @@ ReturnValue_t CoreController::executeAction(ActionId_t actionId, MessageQueueId_
} }
ReturnValue_t CoreController::initializeAfterTaskCreation() { ReturnValue_t CoreController::initializeAfterTaskCreation() {
ReturnValue_t result = initVersionFile(); ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
if(BLOCKING_SD_INIT) {
ReturnValue_t result = initSdCardBlocking();
if(result != HasReturnvaluesIF::RETURN_OK and result != SdCardManager::ALREADY_MOUNTED) {
sif::warning << "CoreController::CoreController: SD card init failed" << std::endl;
}
}
sdStateMachine();
result = initVersionFile();
if(result != HasReturnvaluesIF::RETURN_OK) { if(result != HasReturnvaluesIF::RETURN_OK) {
sif::warning << "CoreController::initialize: Version initialization failed" << std::endl; sif::warning << "CoreController::initialize: Version initialization failed" << std::endl;
} }
@ -176,69 +552,30 @@ ReturnValue_t CoreController::initializeAfterTaskCreation() {
return result; return result;
} }
ReturnValue_t CoreController::sdCardColdRedundantInit(SdCardManager* sdcMan, ReturnValue_t CoreController::sdColdRedundantBlockingInit() {
SdCardManager::SdStatusPair& statusPair) { ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
sd::SdCard preferredSdCard = sd::SdCard::SLOT_0;
ReturnValue_t result = sdcMan->getPreferredSdCard(preferredSdCard);
if(result != HasReturnvaluesIF::RETURN_OK) {
if(result == scratch::KEY_NOT_FOUND) {
sif::warning << "CoreController::sdCardInit: "
"Preferred SD card not set. Setting to 0" << std::endl;
sdcMan->setPreferredSdCard(preferredSdCard);
}
else {
sif::warning << "CoreController::sdCardInit: Could not get preferred SD card"
"information from the scratch buffer" << std::endl;
}
}
std::string preferredString;
sd::SdStatus preferredStatus = sd::SdStatus::OFF;
sd::SdStatus otherStatus = sd::SdStatus::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) {
preferredStatus = statusPair.first;
preferredString = "0";
otherSdc = sd::SdCard::SLOT_1;
otherStatus = statusPair.second;
otherString = "1";
}
else {
preferredString = "1";
preferredStatus = statusPair.second;
otherStatus = statusPair.first;
otherSdc = sd::SdCard::SLOT_0;
otherString = "0";
}
sif::info << "Cold redundant SD card configuration, preferred SD card " <<
preferredString << std::endl;
result = sdCardSetup(*sdcMan, statusPair, preferredSdCard, preferredStatus, 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(*sdcMan, statusPair, preferredSdCard, preferredStatus, preferredString); sdCardSetup(sdInfo.pref, sd::SdState::MOUNTED, sdInfo.prefChar);
result = HasReturnvaluesIF::RETURN_FAILED; result = HasReturnvaluesIF::RETURN_FAILED;
} }
if(result != HasReturnvaluesIF::RETURN_FAILED and otherStatus != sd::SdStatus::OFF) { if(result != HasReturnvaluesIF::RETURN_FAILED and sdInfo.otherState != sd::SdState::OFF) {
sif::info << "Switching off secondary SD card " << otherString << std::endl; sif::info << "Switching off secondary SD card " << sdInfo.otherChar << std::endl;
// Switch off other SD card in cold redundant mode if setting up preferred one walked // Switch off other SD card in cold redundant mode if setting up preferred one worked
// without issues // without issues
result = sdcMan->switchOffSdCard(otherSdc, otherStatus, &statusPair); ReturnValue_t result2 = sdcMan->switchOffSdCard(sdInfo.other,
if(result != HasReturnvaluesIF::RETURN_OK and result != SdCardManager::ALREADY_OFF) { sdInfo.otherState, &sdInfo.currentState);
sif::warning << "Switching off secondary SD card " << otherString << if(result2 != HasReturnvaluesIF::RETURN_OK and result2 != SdCardManager::ALREADY_OFF) {
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;
// Update status file
sdcMan->updateSdCardStateFile();
return HasReturnvaluesIF::RETURN_OK;
} }
ReturnValue_t CoreController::incrementAllocationFailureCount() { ReturnValue_t CoreController::incrementAllocationFailureCount() {
@ -379,6 +716,7 @@ ReturnValue_t CoreController::actionListDirectoryIntoFile(ActionId_t actionId,
if(RFlag) { if(RFlag) {
oss << "R"; oss << "R";
} }
oss << " " << repoName << " > " << targetFileName; oss << " " << repoName << " > " << targetFileName;
int result = std::system(oss.str().c_str()); int result = std::system(oss.str().c_str());
if(result != 0) { if(result != 0) {
@ -493,6 +831,48 @@ ReturnValue_t CoreController::actionPerformReboot(const uint8_t *data, size_t si
return HasActionsIF::EXECUTION_FINISHED; return HasActionsIF::EXECUTION_FINISHED;
} }
CoreController::~CoreController() {
}
void CoreController::determinePreferredSdCard() {
if(sdInfo.pref == sd::SdCard::NONE) {
ReturnValue_t result = sdcMan->getPreferredSdCard(sdInfo.pref);
if(result != HasReturnvaluesIF::RETURN_OK) {
if(result == scratch::KEY_NOT_FOUND) {
sif::warning << "CoreController::sdCardInit: "
"Preferred SD card not set. Setting to 0" << std::endl;
sdcMan->setPreferredSdCard(sdInfo.pref);
}
else {
sif::warning << "CoreController::sdCardInit: Could not get preferred SD card"
"information from the scratch buffer" << std::endl;
}
}
}
}
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

@ -8,6 +8,8 @@
#include "events/subsystemIdRanges.h" #include "events/subsystemIdRanges.h"
class Timer;
class SdCardManager;
class CoreController: public ExtendedControllerBase { class CoreController: public ExtendedControllerBase {
public: public:
@ -32,6 +34,7 @@ public:
CoreController(object_id_t objectId); CoreController(object_id_t objectId);
virtual~ CoreController();
ReturnValue_t initialize() override; ReturnValue_t initialize() override;
@ -45,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;
@ -56,11 +60,65 @@ private:
ReturnValue_t checkModeCommand(Mode_t mode, Submode_t submode, ReturnValue_t checkModeCommand(Mode_t mode, Submode_t submode,
uint32_t *msToReachTheMode); uint32_t *msToReachTheMode);
ReturnValue_t initSdCard(); // States for SD state machine, which is used in non-blocking mode
ReturnValue_t sdCardSetup(SdCardManager& sdcMan, SdCardManager::SdStatusPair& statusPair, enum class SdStates {
sd::SdCard sdCard, sd::SdStatus status, std::string sdString); NONE,
ReturnValue_t sdCardColdRedundantInit(SdCardManager* sdcMan, START,
SdCardManager::SdStatusPair& statusPair); GET_INFO,
SET_STATE_SELF,
MOUNT_SELF,
// Determine operations for other SD card, depending on redundancy configuration
DETERMINE_OTHER,
SET_STATE_OTHER,
// Mount or unmount other
MOUNT_UNMOUNT_OTHER,
// Skip period because the shell command used to generate the info file sometimes is
// missing the last performed operation if executed too early
SKIP_CYCLE_BEFORE_INFO_UPDATE,
UPDATE_INFO,
// SD initialization done
IDLE,
// Used if SD switches or mount commands are issued via telecommand
SET_STATE_FROM_COMMAND,
};
static constexpr bool BLOCKING_SD_INIT = false;
SdCardManager* sdcMan = nullptr;
ReturnValue_t initSdCardBlocking();
ReturnValue_t sdStateMachine();
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;
// Used to track whether a command was executed
bool commandExecuted = true;
bool initFinished = false;
SdCardManager::SdStatePair currentState;
uint16_t cycleCount = 0;
// These two flags are related to external commanding
bool commandIssued = false;
bool commandFinished = false;
sd::SdState currentlyCommandedState = sd::SdState::OFF;
sd::SdCard commandedCard = sd::SdCard::NONE;
sd::SdState commandedState = sd::SdState::OFF;
};
SdInfo sdInfo;
void updateSdInfoOther();
ReturnValue_t sdCardSetup(sd::SdCard sdCard, sd::SdState targetState, std::string sdChar,
bool printOutput = true);
ReturnValue_t sdColdRedundantBlockingInit();
void currentStateSetter(sd::SdCard sdCard, sd::SdState newState);
void determinePreferredSdCard();
void executeNextExternalSdCommand();
void checkExternalSdCommandStatus();
ReturnValue_t initVersionFile(); ReturnValue_t initVersionFile();
ReturnValue_t initBootCopy(); ReturnValue_t initBootCopy();

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,16 +74,16 @@ 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);
if((preferredSdCard == sd::SdCard::SLOT_0) and if((preferredSdCard == sd::SdCard::SLOT_0) and
(statusPair.first == sd::SdStatus::MOUNTED)) { (statusPair.first == sd::SdState::MOUNTED)) {
currentMountPrefix = SdCardManager::SD_0_MOUNT_POINT; currentMountPrefix = SdCardManager::SD_0_MOUNT_POINT;
} }
else if((preferredSdCard == sd::SdCard::SLOT_1) and else if((preferredSdCard == sd::SdCard::SLOT_1) and
(statusPair.second == sd::SdStatus::MOUNTED)) { (statusPair.second == sd::SdState::MOUNTED)) {
currentMountPrefix = SdCardManager::SD_1_MOUNT_POINT; currentMountPrefix = SdCardManager::SD_1_MOUNT_POINT;
} }
else { else {
@ -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

@ -1,3 +1,4 @@
#include <fsfw/osal/linux/Timer.h>
#include "SdCardManager.h" #include "SdCardManager.h"
#include "scratchApi.h" #include "scratchApi.h"
@ -6,13 +7,16 @@
#include "fsfw/ipc/MutexFactory.h" #include "fsfw/ipc/MutexFactory.h"
#include "fsfw/serviceinterface/ServiceInterface.h" #include "fsfw/serviceinterface/ServiceInterface.h"
#include <unistd.h>
#include <fstream> #include <fstream>
#include <memory> #include <memory>
#include <filesystem> #include <filesystem>
#include <cstring>
SdCardManager* SdCardManager::factoryInstance = nullptr; SdCardManager* SdCardManager::factoryInstance = nullptr;
SdCardManager::SdCardManager() { SdCardManager::SdCardManager(): cmdExecutor(256) {
} }
SdCardManager::~SdCardManager() { SdCardManager::~SdCardManager() {
@ -30,11 +34,19 @@ 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) {
@ -49,20 +61,19 @@ ReturnValue_t SdCardManager::switchOnSdCard(sd::SdCard sdCard, bool doMountSdCar
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }
sd::SdStatus targetStatus; sd::SdState currentState;
if(sdCard == sd::SdCard::SLOT_0) { if(sdCard == sd::SdCard::SLOT_0) {
targetStatus = statusPair->first; currentState = statusPair->first;
} }
else if(sdCard == sd::SdCard::SLOT_1) { else if(sdCard == sd::SdCard::SLOT_1) {
targetStatus = statusPair->second; currentState = statusPair->second;
} }
else { else {
// Should not happen // Should not happen
targetStatus = sd::SdStatus::OFF; currentState = sd::SdState::OFF;
} }
auto switchCall = [&]() { if(currentState == sd::SdState::ON) {
if(targetStatus == sd::SdStatus::ON) {
if(not doMountSdCard) { if(not doMountSdCard) {
return ALREADY_ON; return ALREADY_ON;
} }
@ -70,18 +81,15 @@ ReturnValue_t SdCardManager::switchOnSdCard(sd::SdCard sdCard, bool doMountSdCar
return mountSdCard(sdCard); return mountSdCard(sdCard);
} }
} }
else if(targetStatus == sd::SdStatus::MOUNTED) { else if(currentState == sd::SdState::MOUNTED) {
return ALREADY_MOUNTED; result = ALREADY_MOUNTED;
} }
else if(targetStatus == sd::SdStatus::OFF) { else if(currentState == sd::SdState::OFF) {
return setSdCardState(sdCard, true); result = setSdCardState(sdCard, true);
} }
else { else {
return HasReturnvaluesIF::RETURN_FAILED; result = HasReturnvaluesIF::RETURN_FAILED;
} }
};
result = switchCall();
if(result != HasReturnvaluesIF::RETURN_OK or not doMountSdCard) { if(result != HasReturnvaluesIF::RETURN_OK or not doMountSdCard) {
return result; return result;
@ -91,12 +99,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::SdStatus, sd::SdStatus> 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"
@ -104,12 +119,12 @@ ReturnValue_t SdCardManager::switchOffSdCard(sd::SdCard sdCard, bool doUnmountSd
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }
if(sdCard == sd::SdCard::SLOT_0) { if(sdCard == sd::SdCard::SLOT_0) {
if(active.first == sd::SdStatus::OFF) { if(active.first == sd::SdState::OFF) {
return ALREADY_OFF; return ALREADY_OFF;
} }
} }
else if(sdCard == sd::SdCard::SLOT_1) { else if(sdCard == sd::SdCard::SLOT_1) {
if(active.second == sd::SdStatus::OFF) { if(active.second == sd::SdState::OFF) {
return ALREADY_OFF; return ALREADY_OFF;
} }
} }
@ -126,6 +141,9 @@ 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(sdCard == sd::SdCard::SLOT_0) { if(sdCard == sd::SdCard::SLOT_0) {
@ -135,23 +153,24 @@ ReturnValue_t SdCardManager::setSdCardState(sd::SdCard sdCard, bool on) {
sdstring = "1"; sdstring = "1";
} }
if(on) { if(on) {
currentOp = Operations::SWITCHING_ON;
statestring = "on"; statestring = "on";
} }
else { else {
currentOp = Operations::SWITCHING_OFF;
statestring = "off"; statestring = "off";
} }
ostringstream command; ostringstream command;
command << "q7hw sd set " << sdstring << " " << statestring; command << "q7hw sd set " << sdstring << " " << statestring;
int result = system(command.str().c_str()); cmdExecutor.load(command.str(), blocking, printCmdOutput);
if(result == 0) { ReturnValue_t result = cmdExecutor.execute();
return HasReturnvaluesIF::RETURN_OK; if(blocking and result != HasReturnvaluesIF::RETURN_OK) {
utility::handleSystemError(cmdExecutor.getLastError(), "SdCardManager::setSdCardState");
} }
sif::warning << "SdCardManager::setSdCardState: system call failed with code " << return result;
result << std::endl;
return SYSTEM_CALL_ERROR;
} }
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;
@ -174,6 +193,9 @@ 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(cmdExecutor.getCurrentState() == CommandExecutor::States::PENDING) {
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"
<< std::endl; << std::endl;
@ -195,16 +217,22 @@ ReturnValue_t SdCardManager::mountSdCard(sd::SdCard sdCard) {
return MOUNT_ERROR; return MOUNT_ERROR;
} }
string sdMountCommand = "mount " + mountDev + " " + mountPoint; if(not blocking) {
int result = system(sdMountCommand.c_str()); currentOp = Operations::MOUNTING;
if (result != 0) {
utility::handleSystemError(result, "SdCardManager::mountSdCard");
return SYSTEM_CALL_ERROR;
} }
return HasReturnvaluesIF::RETURN_OK; string sdMountCommand = "mount " + mountDev + " " + mountPoint;
cmdExecutor.load(sdMountCommand, blocking, printCmdOutput);
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) { 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"
@ -229,34 +257,53 @@ 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;
int result = system(sdUnmountCommand.c_str()); if(not blocking) {
if (result != 0) { currentOp = Operations::UNMOUNTING;
utility::handleSystemError(result, "SdCardManager::unmountSdCard");
return SYSTEM_CALL_ERROR;
} }
return HasReturnvaluesIF::RETURN_OK; cmdExecutor.load(sdUnmountCommand, blocking, printCmdOutput);
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) { 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::SdStatus::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::processSdStatusLine(std::pair<sd::SdStatus, sd::SdStatus> &active, void SdCardManager::resetState() {
cmdExecutor.reset();
currentOp = Operations::IDLE;
}
void SdCardManager::processSdStatusLine(std::pair<sd::SdState, sd::SdState> &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;
istringstream iss(line); istringstream iss(line);
@ -278,28 +325,28 @@ void SdCardManager::processSdStatusLine(std::pair<sd::SdStatus, sd::SdStatus> &a
if(word == "on") { if(word == "on") {
if(currentSd == sd::SdCard::SLOT_0) { if(currentSd == sd::SdCard::SLOT_0) {
active.first = sd::SdStatus::ON; active.first = sd::SdState::ON;
} }
else { else {
active.second = sd::SdStatus::ON; active.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::SdStatus::OFF; active.first = sd::SdState::OFF;
} }
else { else {
active.second = sd::SdStatus::OFF; active.second = sd::SdState::OFF;
} }
} }
} }
if(mountLine) { if(mountLine) {
if(currentSd == sd::SdCard::SLOT_0) { if(currentSd == sd::SdCard::SLOT_0) {
active.first = sd::SdStatus::MOUNTED; active.first = sd::SdState::MOUNTED;
} }
else { else {
active.second = sd::SdStatus::MOUNTED; active.second = sd::SdState::MOUNTED;
} }
} }
@ -329,15 +376,17 @@ 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);
int result = std::system(updateCmd.c_str()); cmdExecutor.load(updateCmd, blocking, printCmdOutput);
if(result == 0) { ReturnValue_t result = cmdExecutor.execute();
return HasReturnvaluesIF::RETURN_OK; if(blocking and result != HasReturnvaluesIF::RETURN_OK) {
utility::handleSystemError(cmdExecutor.getLastError(), "SdCardManager::mountSdCard");
} }
sif::warning << "SdCardManager::updateSdCardStateFile: system call failed with code " << return result;
result << std::endl;
return HasReturnvaluesIF::RETURN_FAILED;
} }
std::string SdCardManager::getCurrentMountPrefix(sd::SdCard prefSdCard) { std::string SdCardManager::getCurrentMountPrefix(sd::SdCard prefSdCard) {
@ -354,3 +403,56 @@ std::string SdCardManager::getCurrentMountPrefix(sd::SdCard prefSdCard) {
return SD_1_MOUNT_POINT; return SD_1_MOUNT_POINT;
} }
} }
SdCardManager::OpStatus SdCardManager::checkCurrentOp(Operations &currentOp) {
CommandExecutor::States state = cmdExecutor.getCurrentState();
if(state == CommandExecutor::States::IDLE or state == CommandExecutor::States::COMMAND_LOADED) {
return OpStatus::IDLE;
}
currentOp = this->currentOp;
bool bytesRead = false;
#if OBSW_ENABLE_TIMERS == 1
Timer timer;
timer.setTimer(100);
uint32_t remainingTimeMs = 0;
#endif
while(true) {
ReturnValue_t result = cmdExecutor.check(bytesRead);
// This timer can prevent deadlocks due to missconfigurations
#if OBSW_ENABLE_TIMERS == 1
timer.getTimer(&remainingTimeMs);
if(remainingTimeMs == 0) {
sif::error << "SdCardManager::checkCurrentOp: Timeout!" << std::endl;
return OpStatus::FAIL;
}
#endif
switch(result) {
case(CommandExecutor::BYTES_READ): {
continue;
}
case(CommandExecutor::EXECUTION_FINISHED): {
return OpStatus::SUCCESS;
}
case(HasReturnvaluesIF::RETURN_OK): {
return OpStatus::ONGOING;
}
case(HasReturnvaluesIF::RETURN_FAILED): {
return OpStatus::FAIL;
}
default: {
sif::warning << "SdCardManager::checkCurrentOp: Unhandled case" << std::endl;
}
}
}
}
void SdCardManager::setBlocking(bool blocking) {
this->blocking = blocking;
}
void SdCardManager::setPrintCommandOutput(bool print) {
this->printCmdOutput = print;
}

View File

@ -1,6 +1,7 @@
#ifndef BSP_Q7S_MEMORY_SDCARDACCESSMANAGER_H_ #ifndef BSP_Q7S_MEMORY_SDCARDACCESSMANAGER_H_
#define BSP_Q7S_MEMORY_SDCARDACCESSMANAGER_H_ #define BSP_Q7S_MEMORY_SDCARDACCESSMANAGER_H_
#include <fsfw/osal/linux/CommandExecutor.h>
#include "definitions.h" #include "definitions.h"
#include "returnvalues/classIds.h" #include "returnvalues/classIds.h"
#include "events/subsystemIdRanges.h" #include "events/subsystemIdRanges.h"
@ -8,10 +9,13 @@
#include "fsfw/events/Event.h" #include "fsfw/events/Event.h"
#include "fsfw/returnvalues/HasReturnvaluesIF.h" #include "fsfw/returnvalues/HasReturnvaluesIF.h"
#include <poll.h>
#include <cstdint> #include <cstdint>
#include <utility> #include <utility>
#include <string> #include <string>
#include <optional> #include <optional>
#include <array>
class MutexIF; class MutexIF;
@ -22,16 +26,34 @@ class MutexIF;
class SdCardManager { class SdCardManager {
friend class SdCardAccess; friend class SdCardAccess;
public: public:
using SdStatusPair = std::pair<sd::SdStatus, sd::SdStatus>; enum class Operations {
SWITCHING_ON,
SWITCHING_OFF,
MOUNTING,
UNMOUNTING,
IDLE
};
enum class OpStatus {
IDLE,
TIMEOUT,
ONGOING,
SUCCESS,
FAIL
};
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;
static constexpr ReturnValue_t ALREADY_ON = static constexpr ReturnValue_t OP_ONGOING =
HasReturnvaluesIF::makeReturnCode(INTERFACE_ID, 0); HasReturnvaluesIF::makeReturnCode(INTERFACE_ID, 0);
static constexpr ReturnValue_t ALREADY_MOUNTED = static constexpr ReturnValue_t ALREADY_ON =
HasReturnvaluesIF::makeReturnCode(INTERFACE_ID, 1); HasReturnvaluesIF::makeReturnCode(INTERFACE_ID, 1);
static constexpr ReturnValue_t ALREADY_OFF = static constexpr ReturnValue_t ALREADY_MOUNTED =
HasReturnvaluesIF::makeReturnCode(INTERFACE_ID, 2); HasReturnvaluesIF::makeReturnCode(INTERFACE_ID, 2);
static constexpr ReturnValue_t ALREADY_OFF =
HasReturnvaluesIF::makeReturnCode(INTERFACE_ID, 3);
static constexpr ReturnValue_t STATUS_FILE_NEXISTS = static constexpr ReturnValue_t STATUS_FILE_NEXISTS =
HasReturnvaluesIF::makeReturnCode(INTERFACE_ID, 10); HasReturnvaluesIF::makeReturnCode(INTERFACE_ID, 10);
static constexpr ReturnValue_t STATUS_FILE_FORMAT_INVALID = static constexpr ReturnValue_t STATUS_FILE_FORMAT_INVALID =
@ -42,6 +64,8 @@ public:
HasReturnvaluesIF::makeReturnCode(INTERFACE_ID, 13); HasReturnvaluesIF::makeReturnCode(INTERFACE_ID, 13);
static constexpr ReturnValue_t SYSTEM_CALL_ERROR = static constexpr ReturnValue_t SYSTEM_CALL_ERROR =
HasReturnvaluesIF::makeReturnCode(INTERFACE_ID, 14); HasReturnvaluesIF::makeReturnCode(INTERFACE_ID, 14);
static constexpr ReturnValue_t POPEN_CALL_ERROR =
HasReturnvaluesIF::makeReturnCode(INTERFACE_ID, 15);
static constexpr uint8_t SUBSYSTEM_ID = SUBSYSTEM_ID::FILE_SYSTEM; static constexpr uint8_t SUBSYSTEM_ID = SUBSYSTEM_ID::FILE_SYSTEM;
@ -90,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.
@ -102,13 +126,15 @@ 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
* 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();
@ -123,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.
@ -143,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);
/** /**
@ -159,12 +185,27 @@ public:
* @return * @return
*/ */
std::string getCurrentMountPrefix(sd::SdCard prefSdCardPtr = sd::SdCard::NONE); std::string getCurrentMountPrefix(sd::SdCard prefSdCardPtr = sd::SdCard::NONE);
OpStatus checkCurrentOp(Operations& currentOp);
/**
* If there are issues with the state machine, it can be reset with this function
*/
void resetState();
void setBlocking(bool blocking);
void setPrintCommandOutput(bool print);
private: private:
CommandExecutor cmdExecutor;
Operations currentOp = Operations::IDLE;
bool blocking = false;
bool printCmdOutput = true;
SdCardManager(); SdCardManager();
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;

View File

@ -5,7 +5,7 @@
namespace sd { namespace sd {
enum SdStatus: uint8_t { enum SdState: uint8_t {
OFF = 0, OFF = 0,
ON = 1, ON = 1,
// A mounted SD card is on as well // A mounted SD card is on as well

2
fsfw

@ -1 +1 @@
Subproject commit 133820f463645164c203e72f0acd078eb002e159 Subproject commit 22e29144b6783a824b310204c76fa413eb94f331

View File

@ -22,6 +22,10 @@
debugging. */ debugging. */
#define OBSW_VERBOSE_LEVEL 1 #define OBSW_VERBOSE_LEVEL 1
//! Timers can mess up the code when debugging
//! TODO: Enable for mission code, disable for debug code
#define OBSW_ENABLE_TIMERS 0
#define OBSW_PRINT_MISSED_DEADLINES 1 #define OBSW_PRINT_MISSED_DEADLINES 1
#define OBSW_ADD_TEST_CODE 1 #define OBSW_ADD_TEST_CODE 1
#define OBSW_ADD_TEST_PST 1 #define OBSW_ADD_TEST_PST 1

View File

@ -174,7 +174,7 @@ ReturnValue_t PlocUpdater::getImageLocation(const uint8_t* data, size_t size) {
} }
bool PlocUpdater::isSdCardMounted(sd::SdCard sdCard) { bool PlocUpdater::isSdCardMounted(sd::SdCard sdCard) {
SdCardManager::SdStatusPair active; SdCardManager::SdStatePair active;
ReturnValue_t result = sdcMan->getSdCardActiveStatus(active); ReturnValue_t result = sdcMan->getSdCardActiveStatus(active);
if (result != RETURN_OK) { if (result != RETURN_OK) {
sif::debug << "PlocUpdater::isSdCardMounted: Failed to get SD card active state"; sif::debug << "PlocUpdater::isSdCardMounted: Failed to get SD card active state";

2
tmtc

@ -1 +1 @@
Subproject commit 547b4b213704a72a884c2871debeea0461c7704f Subproject commit 3e8626bfafa561510323bf8fe3963bc2860950ed