diff --git a/bsp_q7s/memory/SdCardManager.cpp b/bsp_q7s/memory/SdCardManager.cpp index b9b89bb9..769a7dd7 100644 --- a/bsp_q7s/memory/SdCardManager.cpp +++ b/bsp_q7s/memory/SdCardManager.cpp @@ -1,5 +1,6 @@ #include "SdCardManager.h" +#include #include #include @@ -15,7 +16,9 @@ SdCardManager* SdCardManager::factoryInstance = nullptr; -SdCardManager::SdCardManager() : SystemObject(objects::SDC_MANAGER), cmdExecutor(256) {} +SdCardManager::SdCardManager() : SystemObject(objects::SDC_MANAGER), cmdExecutor(256) { + mutex = MutexFactory::instance()->createMutex(); +} SdCardManager::~SdCardManager() {} @@ -162,6 +165,7 @@ ReturnValue_t SdCardManager::setSdCardState(sd::SdCard sdCard, bool on) { ReturnValue_t SdCardManager::getSdCardActiveStatus(SdStatePair& active) { using namespace std; + MutexGuard mg(mutex); if (not filesystem::exists(SD_STATE_FILE)) { return STATUS_FILE_NEXISTS; } @@ -366,6 +370,7 @@ ReturnValue_t SdCardManager::updateSdCardStateFile() { if (cmdExecutor.getCurrentState() == CommandExecutor::States::PENDING) { return CommandExecutor::COMMAND_PENDING; } + MutexGuard mg(mutex); // Use q7hw utility and pipe the command output into the state file std::string updateCmd = "q7hw sd info all > " + std::string(SD_STATE_FILE); cmdExecutor.load(updateCmd, blocking, printCmdOutput); @@ -440,6 +445,7 @@ void SdCardManager::setPrintCommandOutput(bool print) { this->printCmdOutput = p bool SdCardManager::isSdCardMounted(sd::SdCard sdCard) { SdCardManager::SdStatePair active; ReturnValue_t result = this->getSdCardActiveStatus(active); + if (result != HasReturnvaluesIF::RETURN_OK) { sif::debug << "SdCardManager::isSdCardMounted: Failed to get SD card active state"; return false; diff --git a/bsp_q7s/memory/SdCardManager.h b/bsp_q7s/memory/SdCardManager.h index 51ea3425..37660f75 100644 --- a/bsp_q7s/memory/SdCardManager.h +++ b/bsp_q7s/memory/SdCardManager.h @@ -10,11 +10,12 @@ #include #include -#include "definitions.h" #include "events/subsystemIdRanges.h" #include "fsfw/events/Event.h" #include "fsfw/returnvalues/HasReturnvaluesIF.h" #include "fsfw_hal/linux/CommandExecutor.h" +#include "mission/memory/SdCardMountedIF.h" +#include "mission/memory/definitions.h" #include "returnvalues/classIds.h" class MutexIF; @@ -23,10 +24,12 @@ class MutexIF; * @brief Manages handling of SD cards like switching them on or off or getting the current * state */ -class SdCardManager : public SystemObject { +class SdCardManager : public SystemObject, public SdCardMountedIF { friend class SdCardAccess; public: + using mountInitCb = ReturnValue_t (*)(void* args); + enum class Operations { SWITCHING_ON, SWITCHING_OFF, MOUNTING, UNMOUNTING, IDLE }; enum class OpStatus { IDLE, TIMEOUT, ONGOING, SUCCESS, FAIL }; @@ -88,7 +91,7 @@ class SdCardManager : public SystemObject { * @param sdCard * @return */ - ReturnValue_t getPreferredSdCard(sd::SdCard& sdCard) const; + ReturnValue_t getPreferredSdCard(sd::SdCard& sdCard) const override; /** * Switch on the specified SD card. @@ -170,7 +173,7 @@ class SdCardManager : public SystemObject { * @param prefSdCardPtr * @return */ - std::string getCurrentMountPrefix(sd::SdCard prefSdCardPtr = sd::SdCard::NONE); + std::string getCurrentMountPrefix(sd::SdCard prefSdCardPtr = sd::SdCard::NONE) override; OpStatus checkCurrentOp(Operations& currentOp); @@ -183,19 +186,20 @@ class SdCardManager : public SystemObject { void setPrintCommandOutput(bool print); /** - * @brief Checks if an SD card is mounted + * @brief Checks if an SD card is mounted. * * @param sdCard The SD card to check * * @return true if mounted, otherwise false */ - bool isSdCardMounted(sd::SdCard sdCard); + bool isSdCardMounted(sd::SdCard sdCard) override; private: CommandExecutor cmdExecutor; Operations currentOp = Operations::IDLE; bool blocking = false; bool printCmdOutput = true; + MutexIF* mutex = nullptr; SdCardManager(); diff --git a/mission/memory/SdCardMountedIF.h b/mission/memory/SdCardMountedIF.h new file mode 100644 index 00000000..ff6aace3 --- /dev/null +++ b/mission/memory/SdCardMountedIF.h @@ -0,0 +1,18 @@ +#ifndef MISSION_MEMORY_SDCARDMOUNTERIF_H_ +#define MISSION_MEMORY_SDCARDMOUNTERIF_H_ + +#include + +#include "definitions.h" + +class SdCardMountedIF { + public: + virtual ~SdCardMountedIF(){}; + virtual std::string getCurrentMountPrefix(sd::SdCard prefSdCardPtr = sd::SdCard::NONE) = 0; + virtual bool isSdCardMounted(sd::SdCard sdCard) = 0; + virtual ReturnValue_t getPreferredSdCard(sd::SdCard& sdCard) const = 0; + + private: +}; + +#endif /* MISSION_MEMORY_SDCARDMOUNTERIF_H_ */ diff --git a/bsp_q7s/memory/definitions.h b/mission/memory/definitions.h similarity index 100% rename from bsp_q7s/memory/definitions.h rename to mission/memory/definitions.h