From 5a82fe93d9524004e339d6d35f26c2d68d75d390 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Wed, 11 May 2022 17:02:57 +0200 Subject: [PATCH 1/4] core ctrl remount sd handling --- bsp_q7s/core/CoreController.cpp | 36 ++++++++++++++++---- bsp_q7s/core/CoreController.h | 3 +- bsp_q7s/memory/SdCardManager.cpp | 56 ++++++++++++++++++++++++++++++++ bsp_q7s/memory/SdCardManager.h | 6 ++++ fsfw | 2 +- 5 files changed, 95 insertions(+), 8 deletions(-) diff --git a/bsp_q7s/core/CoreController.cpp b/bsp_q7s/core/CoreController.cpp index 208999c2..00175067 100644 --- a/bsp_q7s/core/CoreController.cpp +++ b/bsp_q7s/core/CoreController.cpp @@ -1235,18 +1235,42 @@ void CoreController::performWatchdogControlOperation() { void CoreController::performMountedSdCardOperations() { currMntPrefix = sdcMan->getCurrentMountPrefix(); - if (doPerformMountedSdCardOps) { + if (performMountedSdCardOpsSwitch) { bool sdCardMounted = false; + bool mountedReadOnly = false; + ReturnValue_t result = sdcMan->isSdCardMountedReadOnly(sdInfo.pref, mountedReadOnly); + if (result != HasReturnvaluesIF::RETURN_OK) { + sif::error << "CoreController::performMountedSdCardOperations: Could not check " + "read-only mount state" << std::endl; + mountedReadOnly = true; + } + if (mountedReadOnly) { + int linuxErrno = 0; + result = sdcMan->performFsck(sdInfo.pref, true, linuxErrno); + if(result != HasReturnvaluesIF::RETURN_OK) { + sif::error << "CoreController::performMountedSdCardOperations: fsck command on SD Card " + << sdInfo.prefChar << " failed with code " << linuxErrno << " | " << strerror(linuxErrno); + } + result = sdcMan->remountReadWrite(sdInfo.pref); + if(result == HasReturnvaluesIF::RETURN_OK) { + sif::warning << "CoreController::performMountedSdCardOperations: Remounted SD Card " << + sdInfo.prefChar << " read-write"; + } else { + sif::error << "CoreController::performMountedSdCardOperations: Remounting SD Card " << + sdInfo.prefChar << " read-write failed"; + } + } sdCardMounted = sdcMan->isSdCardMounted(sdInfo.pref); - if (sdCardMounted) { - std::string path = currMntPrefix + "/" + CONF_FOLDER; - if (not std::filesystem::exists(path)) { - std::filesystem::create_directory(path); + if (sdCardMounted and not performOneShotSdCardOpsSwitch) { + std::ostringstream path; + path << currMntPrefix << "/" << CONF_FOLDER; + if (not std::filesystem::exists(path.str())) { + std::filesystem::create_directory(path.str()); } initVersionFile(); initClockFromTimeFile(); performRebootFileHandling(false); - doPerformMountedSdCardOps = false; + performOneShotSdCardOpsSwitch = true; } } timeFileHandler(); diff --git a/bsp_q7s/core/CoreController.h b/bsp_q7s/core/CoreController.h index fb4f02b0..dd817bff 100644 --- a/bsp_q7s/core/CoreController.h +++ b/bsp_q7s/core/CoreController.h @@ -179,7 +179,8 @@ class CoreController : public ExtendedControllerBase { } sdInfo; RebootFile rebootFile = {}; std::string currMntPrefix; - bool doPerformMountedSdCardOps = true; + bool performOneShotSdCardOpsSwitch = true; + bool performMountedSdCardOpsSwitch = true; /** * Index 0: Chip 0 Copy 0 diff --git a/bsp_q7s/memory/SdCardManager.cpp b/bsp_q7s/memory/SdCardManager.cpp index 4ca11787..d5a91dac 100644 --- a/bsp_q7s/memory/SdCardManager.cpp +++ b/bsp_q7s/memory/SdCardManager.cpp @@ -466,3 +466,59 @@ bool SdCardManager::isSdCardMounted(sd::SdCard sdCard) { } return false; } + +ReturnValue_t SdCardManager::isSdCardMountedReadOnly(sd::SdCard sdcard, bool& readOnly) { + std::ostringstream command; + if(sdcard == sd::SdCard::SLOT_0) { + command << "grep -q \"" << SD_0_DEV_NAME << " vfat ro, \" /proc/mounts"; + } else { + command << "grep -q \"" << SD_1_DEV_NAME << " vfat ro, \" /proc/mounts"; + } + ReturnValue_t result = cmdExecutor.load(command.str(), true, false); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + result = cmdExecutor.execute(); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + auto& readVec = cmdExecutor.getReadVector(); + size_t readLen = strnlen(readVec.data(), readVec.size()); + if(readLen == 0) { + readOnly = false; + } + readOnly = true; + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t SdCardManager::remountReadWrite(sd::SdCard sdcard) { + std::ostringstream command; + if(sdcard == sd::SdCard::SLOT_0) { + command << "mount -o remount,rw " << SD_0_DEV_NAME << " " << SD_0_MOUNT_POINT; + } else { + command << "mount -o remount,rw " << SD_1_DEV_NAME << " " << SD_1_MOUNT_POINT; + } + ReturnValue_t result = cmdExecutor.load(command.str(), true, false); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + return cmdExecutor.execute(); +} + +ReturnValue_t SdCardManager::performFsck(sd::SdCard sdcard, bool printOutput, int& linuxError) { + std::ostringstream command; + if(sdcard == sd::SdCard::SLOT_0) { + command << "fsck -y " << SD_0_DEV_NAME; + } else { + command << "fsck -y " << SD_1_DEV_NAME; + } + ReturnValue_t result = cmdExecutor.load(command.str(), true, printOutput); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + result = cmdExecutor.execute(); + if(result != HasReturnvaluesIF::RETURN_OK) { + linuxError = cmdExecutor.getLastError(); + } + return result; +} diff --git a/bsp_q7s/memory/SdCardManager.h b/bsp_q7s/memory/SdCardManager.h index 37660f75..01a50050 100644 --- a/bsp_q7s/memory/SdCardManager.h +++ b/bsp_q7s/memory/SdCardManager.h @@ -194,6 +194,12 @@ class SdCardManager : public SystemObject, public SdCardMountedIF { */ bool isSdCardMounted(sd::SdCard sdCard) override; + ReturnValue_t isSdCardMountedReadOnly(sd::SdCard sdcard, bool& readOnly); + + ReturnValue_t remountReadWrite(sd::SdCard sdcard); + + ReturnValue_t performFsck(sd::SdCard sdcard, bool printOutput, int& linuxError); + private: CommandExecutor cmdExecutor; Operations currentOp = Operations::IDLE; diff --git a/fsfw b/fsfw index dafcaa60..d1ff32bf 160000 --- a/fsfw +++ b/fsfw @@ -1 +1 @@ -Subproject commit dafcaa60079ba8e57753d389e6a569ee3eb0b7cb +Subproject commit d1ff32bf968cbf769ca8daa37265af70e050d9c0 -- 2.43.0 From b2c286cd7ebe7c7b18405c452b0d462277e18ce0 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Tue, 24 May 2022 00:20:23 +0200 Subject: [PATCH 2/4] improve and optimize SD card handling --- bsp_q7s/core/CoreController.cpp | 126 ++++++++++--------- bsp_q7s/core/CoreController.h | 7 +- bsp_q7s/memory/FileSystemHandler.cpp | 11 +- bsp_q7s/memory/SdCardManager.cpp | 89 ++++++++----- bsp_q7s/memory/SdCardManager.h | 30 ++++- linux/devices/Max31865RtdLowlevelHandler.cpp | 2 +- mission/devices/Max31865EiveHandler.cpp | 2 +- mission/devices/PayloadPcduHandler.cpp | 7 +- mission/memory/SdCardMountedIF.h | 7 +- tmtc | 2 +- 10 files changed, 168 insertions(+), 115 deletions(-) diff --git a/bsp_q7s/core/CoreController.cpp b/bsp_q7s/core/CoreController.cpp index 4181778f..8c617ab4 100644 --- a/bsp_q7s/core/CoreController.cpp +++ b/bsp_q7s/core/CoreController.cpp @@ -56,6 +56,7 @@ CoreController::CoreController(object_id_t objectId) } catch (const std::filesystem::filesystem_error &e) { sif::error << "CoreController::CoreController: Failed with exception " << e.what() << std::endl; } + sdCardCheckCd.timeOut(); eventQueue = QueueFactory::instance()->createMessageQueue(5, EventMessage::MAX_MESSAGE_SIZE); } @@ -77,6 +78,10 @@ void CoreController::performControlOperation() { performWatchdogControlOperation(); sdStateMachine(); performMountedSdCardOperations(); + if (sdCardCheckCd.hasTimedOut()) { + performSdCardCheck(); + sdCardCheckCd.resetTimer(); + } readHkData(); opDivider5.checkAndIncrement(); opDivider10.checkAndIncrement(); @@ -134,6 +139,8 @@ ReturnValue_t CoreController::initialize() { ReturnValue_t CoreController::initializeAfterTaskCreation() { ReturnValue_t result = HasReturnvaluesIF::RETURN_OK; + sdInfo.pref = sdcMan->getPreferredSdCard(); + sdcMan->setActiveSdCard(sdInfo.pref); if (BLOCKING_SD_INIT) { ReturnValue_t result = initSdCardBlocking(); if (result != HasReturnvaluesIF::RETURN_OK and result != SdCardManager::ALREADY_MOUNTED) { @@ -165,7 +172,7 @@ ReturnValue_t CoreController::executeAction(ActionId_t actionId, MessageQueueId_ if (size < 1) { return HasActionsIF::INVALID_PARAMETERS; } - std::string path = sdcMan->getCurrentMountPrefix(sdInfo.pref) + REBOOT_FILE; + std::string path = sdcMan->getCurrentMountPrefix() + REBOOT_FILE; // Disable the reboot file mechanism parseRebootFile(path, rebootFile); if (data[0] == 0) { @@ -205,7 +212,7 @@ ReturnValue_t CoreController::executeAction(ActionId_t actionId, MessageQueueId_ if (size < 1) { return HasActionsIF::INVALID_PARAMETERS; } - std::string path = sdcMan->getCurrentMountPrefix(sdInfo.pref) + REBOOT_FILE; + std::string path = sdcMan->getCurrentMountPrefix() + REBOOT_FILE; // Disable the reboot file mechanism parseRebootFile(path, rebootFile); rebootFile.maxCount = data[0]; @@ -237,13 +244,12 @@ ReturnValue_t CoreController::initSdCardBlocking() { return HasReturnvaluesIF::RETURN_OK; #else - result = sdcMan->getSdCardActiveStatus(sdInfo.currentState); + result = sdcMan->getSdCardsStatus(sdInfo.currentState); if (result != HasReturnvaluesIF::RETURN_OK) { sif::warning << "Getting SD card activity status failed" << std::endl; } #if Q7S_SD_CARD_CONFIG == Q7S_SD_COLD_REDUNDANT - determinePreferredSdCard(); updateSdInfoOther(); sif::info << "Cold redundant SD card configuration, preferred SD card: " << static_cast(sdInfo.pref) << std::endl; @@ -324,8 +330,8 @@ ReturnValue_t CoreController::sdStateMachine() { if (sdInfo.state == SdStates::SET_STATE_SELF) { if (not sdInfo.commandExecuted) { - result = sdcMan->getSdCardActiveStatus(sdInfo.currentState); - determinePreferredSdCard(); + result = sdcMan->getSdCardsStatus(sdInfo.currentState); + sdInfo.pref = sdcMan->getPreferredSdCard(); updateSdInfoOther(); if (sdInfo.pref != sd::SdCard::SLOT_0 and sdInfo.pref != sd::SdCard::SLOT_1) { sif::warning << "Preferred SD card invalid. Setting to card 0.." << std::endl; @@ -468,7 +474,7 @@ ReturnValue_t CoreController::sdStateMachine() { sdInfo.state = SdStates::IDLE; sdInfo.cycleCount = 0; sdcMan->setBlocking(false); - sdcMan->getSdCardActiveStatus(sdInfo.currentState); + sdcMan->getSdCardsStatus(sdInfo.currentState); if (not sdInfo.initFinished) { updateSdInfoOther(); sdInfo.initFinished = true; @@ -933,25 +939,6 @@ ReturnValue_t CoreController::actionPerformReboot(const uint8_t *data, size_t si 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(sd::SdCard::SLOT_0); - sdInfo.pref = sd::SdCard::SLOT_0; - } 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"; @@ -1235,46 +1222,71 @@ void CoreController::performWatchdogControlOperation() { } void CoreController::performMountedSdCardOperations() { - currMntPrefix = sdcMan->getCurrentMountPrefix(); - if (performMountedSdCardOpsSwitch) { - bool sdCardMounted = false; - bool mountedReadOnly = false; - ReturnValue_t result = sdcMan->isSdCardMountedReadOnly(sdInfo.pref, mountedReadOnly); + auto mountedSdCardOp = [&](bool& mntSwitch, sd::SdCard sdCard, std::string mntPoint) { + if (mntSwitch) { + bool sdCardMounted = sdcMan->isSdCardMounted(sdCard); + if (sdCardMounted and not performOneShotSdCardOpsSwitch) { + std::ostringstream path; + path << mntPoint << "/" << CONF_FOLDER; + if (not std::filesystem::exists(path.str())) { + std::filesystem::create_directory(path.str()); + } + initVersionFile(); + initClockFromTimeFile(); + performRebootFileHandling(false); + performOneShotSdCardOpsSwitch = true; + } + mntSwitch = false; + } + }; + if(sdInfo.pref == sd::SdCard::SLOT_1) { + mountedSdCardOp(sdInfo.mountSwitch.second, sd::SdCard::SLOT_1, SdCardManager::SD_1_MOUNT_POINT); + mountedSdCardOp(sdInfo.mountSwitch.first, sd::SdCard::SLOT_0, SdCardManager::SD_0_MOUNT_POINT); + } else { + mountedSdCardOp(sdInfo.mountSwitch.first, sd::SdCard::SLOT_0, SdCardManager::SD_0_MOUNT_POINT); + mountedSdCardOp(sdInfo.mountSwitch.second, sd::SdCard::SLOT_1, SdCardManager::SD_1_MOUNT_POINT); + } + timeFileHandler(); +} + +ReturnValue_t CoreController::performSdCardCheck() { + bool mountedReadOnly = false; + SdCardManager::SdStatePair active; + sdcMan->getSdCardsStatus(active); + auto sdCardCheck = [&](sd::SdCard sdCard) { + ReturnValue_t result = sdcMan->isSdCardMountedReadOnly(sdCard, mountedReadOnly); if (result != HasReturnvaluesIF::RETURN_OK) { - sif::error << "CoreController::performMountedSdCardOperations: Could not check " - "read-only mount state" << std::endl; + sif::error << "CoreController::performSdCardCheck: Could not check " + "read-only mount state" + << std::endl; mountedReadOnly = true; } if (mountedReadOnly) { int linuxErrno = 0; - result = sdcMan->performFsck(sdInfo.pref, true, linuxErrno); - if(result != HasReturnvaluesIF::RETURN_OK) { - sif::error << "CoreController::performMountedSdCardOperations: fsck command on SD Card " - << sdInfo.prefChar << " failed with code " << linuxErrno << " | " << strerror(linuxErrno); + result = sdcMan->performFsck(sdCard, true, linuxErrno); + if (result != HasReturnvaluesIF::RETURN_OK) { + sif::error << "CoreController::performSdCardCheck: fsck command on SD Card " + << static_cast(sdCard) << " failed with code " << linuxErrno << " | " + << strerror(linuxErrno); } - result = sdcMan->remountReadWrite(sdInfo.pref); - if(result == HasReturnvaluesIF::RETURN_OK) { - sif::warning << "CoreController::performMountedSdCardOperations: Remounted SD Card " << - sdInfo.prefChar << " read-write"; + result = sdcMan->remountReadWrite(sdCard); + if (result == HasReturnvaluesIF::RETURN_OK) { + sif::warning << "CoreController::performSdCardCheck: Remounted SD Card " + << static_cast(sdCard) << " read-write"; } else { - sif::error << "CoreController::performMountedSdCardOperations: Remounting SD Card " << - sdInfo.prefChar << " read-write failed"; + sif::error << "CoreController::performSdCardCheck: Remounting SD Card " + << static_cast(sdCard) << " read-write failed"; } } - sdCardMounted = sdcMan->isSdCardMounted(sdInfo.pref); - if (sdCardMounted and not performOneShotSdCardOpsSwitch) { - std::ostringstream path; - path << currMntPrefix << "/" << CONF_FOLDER; - if (not std::filesystem::exists(path.str())) { - std::filesystem::create_directory(path.str()); - } - initVersionFile(); - initClockFromTimeFile(); - performRebootFileHandling(false); - performOneShotSdCardOpsSwitch = true; - } + }; + if (active.first == sd::SdState::MOUNTED) { + sdCardCheck(sd::SdCard::SLOT_0); } - timeFileHandler(); + if (active.second == sd::SdState::MOUNTED) { + sdCardCheck(sd::SdCard::SLOT_1); + } + + return RETURN_OK; } void CoreController::performRebootFileHandling(bool recreateFile) { @@ -1702,7 +1714,7 @@ void CoreController::rewriteRebootFile(RebootFile file) { } void CoreController::setRebootMechanismLock(bool lock, xsc::Chip tgtChip, xsc::Copy tgtCopy) { - std::string path = sdcMan->getCurrentMountPrefix(sdInfo.pref) + REBOOT_FILE; + std::string path = currMntPrefix + REBOOT_FILE; // Disable the reboot file mechanism parseRebootFile(path, rebootFile); if (tgtChip == xsc::CHIP_0) { diff --git a/bsp_q7s/core/CoreController.h b/bsp_q7s/core/CoreController.h index dd817bff..a82280c8 100644 --- a/bsp_q7s/core/CoreController.h +++ b/bsp_q7s/core/CoreController.h @@ -159,11 +159,12 @@ class CoreController : public ExtendedControllerBase { struct SdInfo { sd::SdCard pref = sd::SdCard::NONE; - sd::SdState prefState = sd::SdState::OFF; sd::SdCard other = sd::SdCard::NONE; + sd::SdState prefState = sd::SdState::OFF; sd::SdState otherState = sd::SdState::OFF; std::string prefChar = "0"; std::string otherChar = "1"; + std::pair mountSwitch = {true, true}; SdStates state = SdStates::START; // Used to track whether a command was executed bool commandExecuted = true; @@ -180,7 +181,6 @@ class CoreController : public ExtendedControllerBase { RebootFile rebootFile = {}; std::string currMntPrefix; bool performOneShotSdCardOpsSwitch = true; - bool performMountedSdCardOpsSwitch = true; /** * Index 0: Chip 0 Copy 0 @@ -196,12 +196,14 @@ class CoreController : public ExtendedControllerBase { ReturnValue_t initializeLocalDataPool(localpool::DataPool& localDataPoolMap, LocalDataPoolManager& poolManager) override; + Countdown sdCardCheckCd = Countdown(10000); LocalPoolDataSetBase* getDataSetHandle(sid_t sid) override; ReturnValue_t checkModeCommand(Mode_t mode, Submode_t submode, uint32_t* msToReachTheMode); void performMountedSdCardOperations(); ReturnValue_t initVersionFile(); ReturnValue_t initClockFromTimeFile(); + ReturnValue_t performSdCardCheck(); ReturnValue_t timeFileHandler(); ReturnValue_t initBootCopy(); ReturnValue_t initWatchdogFifo(); @@ -215,7 +217,6 @@ class CoreController : public ExtendedControllerBase { ReturnValue_t sdColdRedundantBlockingInit(); void currentStateSetter(sd::SdCard sdCard, sd::SdState newState); - void determinePreferredSdCard(); void executeNextExternalSdCommand(); void checkExternalSdCommandStatus(); void performRebootFileHandling(bool recreateFile); diff --git a/bsp_q7s/memory/FileSystemHandler.cpp b/bsp_q7s/memory/FileSystemHandler.cpp index 7a4a791c..33849fbe 100644 --- a/bsp_q7s/memory/FileSystemHandler.cpp +++ b/bsp_q7s/memory/FileSystemHandler.cpp @@ -70,9 +70,8 @@ void FileSystemHandler::fileSystemHandlerLoop() { void FileSystemHandler::fileSystemCheckup() { SdCardManager::SdStatePair statusPair; - sdcMan->getSdCardActiveStatus(statusPair); - sd::SdCard preferredSdCard; - sdcMan->getPreferredSdCard(preferredSdCard); + sdcMan->getSdCardsStatus(statusPair); + sd::SdCard preferredSdCard = sdcMan->getPreferredSdCard(); if ((preferredSdCard == sd::SdCard::SLOT_0) and (statusPair.first == sd::SdState::MOUNTED)) { currentMountPrefix = SdCardManager::SD_0_MOUNT_POINT; } else if ((preferredSdCard == sd::SdCard::SLOT_1) and @@ -109,11 +108,7 @@ ReturnValue_t FileSystemHandler::initialize() { << std::endl; } sdcMan = SdCardManager::instance(); - sd::SdCard preferredSdCard; - ReturnValue_t result = sdcMan->getPreferredSdCard(preferredSdCard); - if (result != HasReturnvaluesIF::RETURN_OK) { - return result; - } + sd::SdCard preferredSdCard = sdcMan->getPreferredSdCard(); if (preferredSdCard == sd::SdCard::SLOT_0) { currentMountPrefix = SdCardManager::SD_0_MOUNT_POINT; } else if (preferredSdCard == sd::SdCard::SLOT_1) { diff --git a/bsp_q7s/memory/SdCardManager.cpp b/bsp_q7s/memory/SdCardManager.cpp index d5a91dac..156d8ba8 100644 --- a/bsp_q7s/memory/SdCardManager.cpp +++ b/bsp_q7s/memory/SdCardManager.cpp @@ -16,23 +16,44 @@ #include "linux/utility/utility.h" #include "scratchApi.h" -SdCardManager* SdCardManager::factoryInstance = nullptr; +SdCardManager* SdCardManager::INSTANCE = nullptr; SdCardManager::SdCardManager() : SystemObject(objects::SDC_MANAGER), cmdExecutor(256) { mutex = MutexFactory::instance()->createMutex(); + MutexGuard mg(mutex); + uint8_t prefSdRaw = 0; + ReturnValue_t result = scratch::readNumber(scratch::PREFERED_SDC_KEY, prefSdRaw); + + 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; + setPreferredSdCard(sd::SdCard::SLOT_0); + sdInfo.pref = sd::SdCard::SLOT_0; + } else { + // Should not happen. + // TODO: Maybe trigger event? + sif::error << "SdCardManager::SdCardManager: Reading preferred SD card from scratch" + "buffer failed" + << std::endl; + sdInfo.pref = sd::SdCard::SLOT_0; + } + } + sdInfo.pref = static_cast(prefSdRaw); } SdCardManager::~SdCardManager() {} void SdCardManager::create() { - if (factoryInstance == nullptr) { - factoryInstance = new SdCardManager(); + if (INSTANCE == nullptr) { + INSTANCE = new SdCardManager(); } } SdCardManager* SdCardManager::instance() { SdCardManager::create(); - return SdCardManager::factoryInstance; + return SdCardManager::INSTANCE; } ReturnValue_t SdCardManager::switchOnSdCard(sd::SdCard sdCard, bool doMountSdCard, @@ -51,7 +72,7 @@ ReturnValue_t SdCardManager::switchOnSdCard(sd::SdCard sdCard, bool doMountSdCar if (statusPair == nullptr) { sdStatusPtr = std::make_unique(); statusPair = sdStatusPtr.get(); - result = getSdCardActiveStatus(*statusPair); + result = getSdCardsStatus(*statusPair); if (result != HasReturnvaluesIF::RETURN_OK) { return result; } @@ -98,7 +119,7 @@ ReturnValue_t SdCardManager::switchOnSdCard(sd::SdCard sdCard, bool doMountSdCar ReturnValue_t SdCardManager::switchOffSdCard(sd::SdCard sdCard, bool doUnmountSdCard, SdStatePair* statusPair) { std::pair active; - ReturnValue_t result = getSdCardActiveStatus(active); + ReturnValue_t result = getSdCardsStatus(active); if (result != HasReturnvaluesIF::RETURN_OK) { return result; } @@ -165,7 +186,7 @@ ReturnValue_t SdCardManager::setSdCardState(sd::SdCard sdCard, bool on) { return result; } -ReturnValue_t SdCardManager::getSdCardActiveStatus(SdStatePair& active) { +ReturnValue_t SdCardManager::getSdCardsStatus(SdStatePair& active) { using namespace std; MutexGuard mg(mutex); if (not filesystem::exists(SD_STATE_FILE)) { @@ -273,14 +294,14 @@ ReturnValue_t SdCardManager::sanitizeState(SdStatePair* statusPair, sd::SdCard p resetNonBlockingState = true; } if (prefSdCard == sd::SdCard::NONE) { - result = getPreferredSdCard(prefSdCard); + result = getPreferredSdCard(); if (result != HasReturnvaluesIF::RETURN_OK) { } } if (statusPair == nullptr) { sdStatusPtr = std::make_unique(); statusPair = sdStatusPtr.get(); - getSdCardActiveStatus(*statusPair); + getSdCardsStatus(*statusPair); } if (statusPair->first == sd::SdState::ON) { @@ -351,20 +372,21 @@ void SdCardManager::processSdStatusLine(std::pair& act idx++; } -ReturnValue_t SdCardManager::getPreferredSdCard(sd::SdCard& sdCard) const { - uint8_t prefSdCard = 0; - ReturnValue_t result = scratch::readNumber(scratch::PREFERED_SDC_KEY, prefSdCard); - if (result != HasReturnvaluesIF::RETURN_OK) { - return result; +sd::SdCard SdCardManager::getPreferredSdCard() const { + MutexGuard mg(mutex); + auto res = mg.getLockResult(); + if (res != RETURN_OK) { + sif::error << "SdCardManager::getPreferredSdCard: Lock error" << std::endl; } - sdCard = static_cast(prefSdCard); - return HasReturnvaluesIF::RETURN_OK; + return sdInfo.pref; } ReturnValue_t SdCardManager::setPreferredSdCard(sd::SdCard sdCard) { + MutexGuard mg(mutex); if (sdCard == sd::SdCard::BOTH) { return HasReturnvaluesIF::RETURN_FAILED; } + sdInfo.pref = sdCard; return scratch::writeNumber(scratch::PREFERED_SDC_KEY, static_cast(sdCard)); } @@ -383,14 +405,9 @@ ReturnValue_t SdCardManager::updateSdCardStateFile() { return result; } -std::string SdCardManager::getCurrentMountPrefix(sd::SdCard prefSdCard) { - if (prefSdCard == sd::SdCard::NONE) { - ReturnValue_t result = getPreferredSdCard(prefSdCard); - if (result != HasReturnvaluesIF::RETURN_OK) { - return SD_0_MOUNT_POINT; - } - } - if (prefSdCard == sd::SdCard::SLOT_0) { +std::string SdCardManager::getCurrentMountPrefix() const { + MutexGuard mg(mutex); + if (sdInfo.active == sd::SdCard::SLOT_0) { return SD_0_MOUNT_POINT; } else { return SD_1_MOUNT_POINT; @@ -443,7 +460,7 @@ void SdCardManager::setPrintCommandOutput(bool print) { this->printCmdOutput = p bool SdCardManager::isSdCardMounted(sd::SdCard sdCard) { SdCardManager::SdStatePair active; - ReturnValue_t result = this->getSdCardActiveStatus(active); + ReturnValue_t result = this->getSdCardsStatus(active); if (result != HasReturnvaluesIF::RETURN_OK) { sif::debug << "SdCardManager::isSdCardMounted: Failed to get SD card active state"; @@ -469,10 +486,10 @@ bool SdCardManager::isSdCardMounted(sd::SdCard sdCard) { ReturnValue_t SdCardManager::isSdCardMountedReadOnly(sd::SdCard sdcard, bool& readOnly) { std::ostringstream command; - if(sdcard == sd::SdCard::SLOT_0) { - command << "grep -q \"" << SD_0_DEV_NAME << " vfat ro, \" /proc/mounts"; + if (sdcard == sd::SdCard::SLOT_0) { + command << "grep -q \"" << SD_0_DEV_NAME << " vfat ro, \" /proc/mounts"; } else { - command << "grep -q \"" << SD_1_DEV_NAME << " vfat ro, \" /proc/mounts"; + command << "grep -q \"" << SD_1_DEV_NAME << " vfat ro, \" /proc/mounts"; } ReturnValue_t result = cmdExecutor.load(command.str(), true, false); if (result != HasReturnvaluesIF::RETURN_OK) { @@ -484,7 +501,7 @@ ReturnValue_t SdCardManager::isSdCardMountedReadOnly(sd::SdCard sdcard, bool& re } auto& readVec = cmdExecutor.getReadVector(); size_t readLen = strnlen(readVec.data(), readVec.size()); - if(readLen == 0) { + if (readLen == 0) { readOnly = false; } readOnly = true; @@ -493,7 +510,7 @@ ReturnValue_t SdCardManager::isSdCardMountedReadOnly(sd::SdCard sdcard, bool& re ReturnValue_t SdCardManager::remountReadWrite(sd::SdCard sdcard) { std::ostringstream command; - if(sdcard == sd::SdCard::SLOT_0) { + if (sdcard == sd::SdCard::SLOT_0) { command << "mount -o remount,rw " << SD_0_DEV_NAME << " " << SD_0_MOUNT_POINT; } else { command << "mount -o remount,rw " << SD_1_DEV_NAME << " " << SD_1_MOUNT_POINT; @@ -507,7 +524,7 @@ ReturnValue_t SdCardManager::remountReadWrite(sd::SdCard sdcard) { ReturnValue_t SdCardManager::performFsck(sd::SdCard sdcard, bool printOutput, int& linuxError) { std::ostringstream command; - if(sdcard == sd::SdCard::SLOT_0) { + if (sdcard == sd::SdCard::SLOT_0) { command << "fsck -y " << SD_0_DEV_NAME; } else { command << "fsck -y " << SD_1_DEV_NAME; @@ -517,8 +534,16 @@ ReturnValue_t SdCardManager::performFsck(sd::SdCard sdcard, bool printOutput, in return result; } result = cmdExecutor.execute(); - if(result != HasReturnvaluesIF::RETURN_OK) { + if (result != HasReturnvaluesIF::RETURN_OK) { linuxError = cmdExecutor.getLastError(); } return result; } + +void SdCardManager::setActiveSdCard(sd::SdCard sdCard) { + sdInfo.active = sdCard; +} + +sd::SdCard SdCardManager::getActiveSdCard() const { + return sdInfo.active; +} diff --git a/bsp_q7s/memory/SdCardManager.h b/bsp_q7s/memory/SdCardManager.h index 01a50050..84d2d97b 100644 --- a/bsp_q7s/memory/SdCardManager.h +++ b/bsp_q7s/memory/SdCardManager.h @@ -24,7 +24,7 @@ class MutexIF; * @brief Manages handling of SD cards like switching them on or off or getting the current * state */ -class SdCardManager : public SystemObject, public SdCardMountedIF { +class SdCardManager : public SystemObject, public HasReturnvaluesIF, public SdCardMountedIF { friend class SdCardAccess; public: @@ -36,6 +36,12 @@ class SdCardManager : public SystemObject, public SdCardMountedIF { using SdStatePair = std::pair; + struct SdInfo { + sd::SdCard pref = sd::SdCard::NONE; + sd::SdCard other = sd::SdCard::NONE; + sd::SdCard active = sd::SdCard::NONE; + } sdInfo; + static constexpr uint8_t INTERFACE_ID = CLASS_ID::SD_CARD_MANAGER; static constexpr ReturnValue_t OP_ONGOING = HasReturnvaluesIF::makeReturnCode(INTERFACE_ID, 0); @@ -91,7 +97,7 @@ class SdCardManager : public SystemObject, public SdCardMountedIF { * @param sdCard * @return */ - ReturnValue_t getPreferredSdCard(sd::SdCard& sdCard) const override; + sd::SdCard getPreferredSdCard() const override; /** * Switch on the specified SD card. @@ -138,7 +144,7 @@ class SdCardManager : public SystemObject, public SdCardMountedIF { * should call #updateSdCardStateFile again in that case * - STATUS_FILE_NEXISTS if the status file does not exist */ - ReturnValue_t getSdCardActiveStatus(SdStatePair& active); + ReturnValue_t getSdCardsStatus(SdStatePair& active); /** * Mount the specified SD card. This is necessary to use it. @@ -146,6 +152,20 @@ class SdCardManager : public SystemObject, public SdCardMountedIF { * @return */ ReturnValue_t mountSdCard(sd::SdCard sdCard); + + /** + * Set the currently active SD card. This does not necessarily mean that the SD card is on or + * mounted + * @param sdCard + */ + void setActiveSdCard(sd::SdCard sdCard) override; + /** + * Get the currently active SD card. This does not necessarily mean that the SD card is on or + * mounted + * @return + */ + sd::SdCard getActiveSdCard() const override; + /** * Unmount the specified SD card. This is recommended before switching it off. The SD card * can't be used after it has been unmounted. @@ -173,7 +193,7 @@ class SdCardManager : public SystemObject, public SdCardMountedIF { * @param prefSdCardPtr * @return */ - std::string getCurrentMountPrefix(sd::SdCard prefSdCardPtr = sd::SdCard::NONE) override; + std::string getCurrentMountPrefix() const override; OpStatus checkCurrentOp(Operations& currentOp); @@ -216,7 +236,7 @@ class SdCardManager : public SystemObject, public SdCardMountedIF { std::string currentPrefix; - static SdCardManager* factoryInstance; + static SdCardManager* INSTANCE; }; #endif /* BSP_Q7S_MEMORY_SDCARDACCESSMANAGER_H_ */ diff --git a/linux/devices/Max31865RtdLowlevelHandler.cpp b/linux/devices/Max31865RtdLowlevelHandler.cpp index 6a3af90e..ea038c8b 100644 --- a/linux/devices/Max31865RtdLowlevelHandler.cpp +++ b/linux/devices/Max31865RtdLowlevelHandler.cpp @@ -114,7 +114,7 @@ bool Max31865RtdReader::periodicInitHandling() { #if OBSW_RTD_AUTO_MODE == 0 result = writeBiasSel(Bias::ON, rtd->spiCookie, BASE_CFG); #endif - someRtdUsable = true; + someRtdUsable = true; } } return someRtdUsable; diff --git a/mission/devices/Max31865EiveHandler.cpp b/mission/devices/Max31865EiveHandler.cpp index 7075b685..a77bbd51 100644 --- a/mission/devices/Max31865EiveHandler.cpp +++ b/mission/devices/Max31865EiveHandler.cpp @@ -123,7 +123,7 @@ void Max31865EiveHandler::fillCommandAndReplyMap() { ReturnValue_t Max31865EiveHandler::scanForReply(const uint8_t* start, size_t remainingSize, DeviceCommandId_t* foundId, size_t* foundLen) { - if(mode == _MODE_POWER_ON or mode == _MODE_WAIT_ON) { + if (mode == _MODE_POWER_ON or mode == _MODE_WAIT_ON) { return IGNORE_FULL_PACKET; } if (remainingSize != structLen) { diff --git a/mission/devices/PayloadPcduHandler.cpp b/mission/devices/PayloadPcduHandler.cpp index 85cf62fa..3e18a94e 100644 --- a/mission/devices/PayloadPcduHandler.cpp +++ b/mission/devices/PayloadPcduHandler.cpp @@ -490,10 +490,9 @@ void PayloadPcduHandler::checkAdcValues() { void PayloadPcduHandler::checkJsonFileInit() { if (not jsonFileInitComplete) { - sd::SdCard prefSd; - sdcMan->getPreferredSdCard(prefSd); - if (sdcMan->isSdCardMounted(prefSd)) { - params.initialize(sdcMan->getCurrentMountPrefix(prefSd)); + sd::SdCard activeSd = sdcMan->getActiveSdCard(); + if (sdcMan->isSdCardMounted(activeSd)) { + params.initialize(sdcMan->getCurrentMountPrefix()); jsonFileInitComplete = true; } } diff --git a/mission/memory/SdCardMountedIF.h b/mission/memory/SdCardMountedIF.h index ff6aace3..77fe97e1 100644 --- a/mission/memory/SdCardMountedIF.h +++ b/mission/memory/SdCardMountedIF.h @@ -8,10 +8,11 @@ class SdCardMountedIF { public: virtual ~SdCardMountedIF(){}; - virtual std::string getCurrentMountPrefix(sd::SdCard prefSdCardPtr = sd::SdCard::NONE) = 0; + virtual std::string getCurrentMountPrefix() const = 0; virtual bool isSdCardMounted(sd::SdCard sdCard) = 0; - virtual ReturnValue_t getPreferredSdCard(sd::SdCard& sdCard) const = 0; - + virtual sd::SdCard getPreferredSdCard() const = 0; + virtual void setActiveSdCard(sd::SdCard sdCard) = 0; + virtual sd::SdCard getActiveSdCard() const = 0; private: }; diff --git a/tmtc b/tmtc index 9332fb86..51bbeaa6 160000 --- a/tmtc +++ b/tmtc @@ -1 +1 @@ -Subproject commit 9332fb8690de200070e712a70b1fa339d5b6b8d8 +Subproject commit 51bbeaa693a11bfc4d2cb342354041972dd93c1e -- 2.43.0 From 1519b13b78454024dcfee8390cd185b73cac31e6 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Tue, 24 May 2022 00:52:08 +0200 Subject: [PATCH 3/4] appears to work now --- bsp_q7s/core/CoreController.cpp | 5 +++-- bsp_q7s/memory/SdCardManager.cpp | 17 +++++++++-------- mission/memory/SdCardMountedIF.h | 1 + 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/bsp_q7s/core/CoreController.cpp b/bsp_q7s/core/CoreController.cpp index 8c617ab4..6719a6ee 100644 --- a/bsp_q7s/core/CoreController.cpp +++ b/bsp_q7s/core/CoreController.cpp @@ -141,6 +141,7 @@ ReturnValue_t CoreController::initializeAfterTaskCreation() { ReturnValue_t result = HasReturnvaluesIF::RETURN_OK; sdInfo.pref = sdcMan->getPreferredSdCard(); sdcMan->setActiveSdCard(sdInfo.pref); + currMntPrefix = sdcMan->getCurrentMountPrefix(); if (BLOCKING_SD_INIT) { ReturnValue_t result = initSdCardBlocking(); if (result != HasReturnvaluesIF::RETURN_OK and result != SdCardManager::ALREADY_MOUNTED) { @@ -1222,7 +1223,7 @@ void CoreController::performWatchdogControlOperation() { } void CoreController::performMountedSdCardOperations() { - auto mountedSdCardOp = [&](bool& mntSwitch, sd::SdCard sdCard, std::string mntPoint) { + auto mountedSdCardOp = [&](bool &mntSwitch, sd::SdCard sdCard, std::string mntPoint) { if (mntSwitch) { bool sdCardMounted = sdcMan->isSdCardMounted(sdCard); if (sdCardMounted and not performOneShotSdCardOpsSwitch) { @@ -1239,7 +1240,7 @@ void CoreController::performMountedSdCardOperations() { mntSwitch = false; } }; - if(sdInfo.pref == sd::SdCard::SLOT_1) { + if (sdInfo.pref == sd::SdCard::SLOT_1) { mountedSdCardOp(sdInfo.mountSwitch.second, sd::SdCard::SLOT_1, SdCardManager::SD_1_MOUNT_POINT); mountedSdCardOp(sdInfo.mountSwitch.first, sd::SdCard::SLOT_0, SdCardManager::SD_0_MOUNT_POINT); } else { diff --git a/bsp_q7s/memory/SdCardManager.cpp b/bsp_q7s/memory/SdCardManager.cpp index 156d8ba8..1b03ad75 100644 --- a/bsp_q7s/memory/SdCardManager.cpp +++ b/bsp_q7s/memory/SdCardManager.cpp @@ -487,9 +487,9 @@ bool SdCardManager::isSdCardMounted(sd::SdCard sdCard) { ReturnValue_t SdCardManager::isSdCardMountedReadOnly(sd::SdCard sdcard, bool& readOnly) { std::ostringstream command; if (sdcard == sd::SdCard::SLOT_0) { - command << "grep -q \"" << SD_0_DEV_NAME << " vfat ro, \" /proc/mounts"; + command << "grep -q '" << SD_0_MOUNT_POINT << " vfat ro,' /proc/mounts"; } else { - command << "grep -q \"" << SD_1_DEV_NAME << " vfat ro, \" /proc/mounts"; + command << "grep -q '" << SD_1_MOUNT_POINT << " vfat ro,' /proc/mounts"; } ReturnValue_t result = cmdExecutor.load(command.str(), true, false); if (result != HasReturnvaluesIF::RETURN_OK) { @@ -497,6 +497,11 @@ ReturnValue_t SdCardManager::isSdCardMountedReadOnly(sd::SdCard sdcard, bool& re } result = cmdExecutor.execute(); if (result != HasReturnvaluesIF::RETURN_OK) { + int exitStatus = cmdExecutor.getLastError(); + if (exitStatus == 1) { + readOnly = false; + return RETURN_OK; + } return result; } auto& readVec = cmdExecutor.getReadVector(); @@ -540,10 +545,6 @@ ReturnValue_t SdCardManager::performFsck(sd::SdCard sdcard, bool printOutput, in return result; } -void SdCardManager::setActiveSdCard(sd::SdCard sdCard) { - sdInfo.active = sdCard; -} +void SdCardManager::setActiveSdCard(sd::SdCard sdCard) { sdInfo.active = sdCard; } -sd::SdCard SdCardManager::getActiveSdCard() const { - return sdInfo.active; -} +sd::SdCard SdCardManager::getActiveSdCard() const { return sdInfo.active; } diff --git a/mission/memory/SdCardMountedIF.h b/mission/memory/SdCardMountedIF.h index 77fe97e1..853c7e8d 100644 --- a/mission/memory/SdCardMountedIF.h +++ b/mission/memory/SdCardMountedIF.h @@ -13,6 +13,7 @@ class SdCardMountedIF { virtual sd::SdCard getPreferredSdCard() const = 0; virtual void setActiveSdCard(sd::SdCard sdCard) = 0; virtual sd::SdCard getActiveSdCard() const = 0; + private: }; -- 2.43.0 From 2ebdb5c79714bb9bd3d0634fc63b8a6d9ed14662 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Tue, 24 May 2022 00:53:29 +0200 Subject: [PATCH 4/4] tweak sd card check countdown --- bsp_q7s/core/CoreController.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bsp_q7s/core/CoreController.h b/bsp_q7s/core/CoreController.h index a82280c8..60c1f94e 100644 --- a/bsp_q7s/core/CoreController.h +++ b/bsp_q7s/core/CoreController.h @@ -196,7 +196,7 @@ class CoreController : public ExtendedControllerBase { ReturnValue_t initializeLocalDataPool(localpool::DataPool& localDataPoolMap, LocalDataPoolManager& poolManager) override; - Countdown sdCardCheckCd = Countdown(10000); + Countdown sdCardCheckCd = Countdown(120000); LocalPoolDataSetBase* getDataSetHandle(sid_t sid) override; ReturnValue_t checkModeCommand(Mode_t mode, Submode_t submode, uint32_t* msToReachTheMode); void performMountedSdCardOperations(); -- 2.43.0