From 9eacccb86a52e6b832c028a496cdefdf9b95747d Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Thu, 8 Jul 2021 11:23:08 +0200 Subject: [PATCH] continued sd card manager --- bsp_q7s/memory/SdCardManager.cpp | 82 ++++++++++++++++++++++++-------- bsp_q7s/memory/SdCardManager.h | 30 +++++++++++- bsp_q7s/memory/definitions.h | 3 +- 3 files changed, 91 insertions(+), 24 deletions(-) diff --git a/bsp_q7s/memory/SdCardManager.cpp b/bsp_q7s/memory/SdCardManager.cpp index b6668045..1458ef1f 100644 --- a/bsp_q7s/memory/SdCardManager.cpp +++ b/bsp_q7s/memory/SdCardManager.cpp @@ -25,50 +25,90 @@ SdCardManager* SdCardManager::instance() { } ReturnValue_t SdCardManager::switchOnSdCard(sd::SdCard sdCard) { - if(sdCardActive(sdCard)) { - return ALREADY_ON; - } - return HasReturnvaluesIF::RETURN_OK; +// if(sdCardActive(sdCard)) { +// return ALREADY_ON; +// } + return setSdCardState(sdCard, true); + } ReturnValue_t SdCardManager::switchOffSdCard(sd::SdCard sdCard) { - return HasReturnvaluesIF::RETURN_OK; +// if(not sdCardActive(sdCard)) { +// return ALREADY_OFF; +// } + return setSdCardState(sdCard, false); } -bool SdCardManager::sdCardActive(sd::SdCard sdCard) { +ReturnValue_t SdCardManager::setSdCardState(sd::SdCard sdCard, bool on) { + std::string sdstring = ""; + std::string statestring = ""; + if(sdCard == sd::SdCard::SLOT_0) { + sdstring = "0"; + } + else if(sdCard == sd::SdCard::SLOT_1) { + sdstring = "1"; + } + if(on) { + statestring = "on"; + } + else { + statestring = "off"; + } + std::ostringstream command; + command << "h7hw sd set " << sdstring << " " << statestring; + int result = std::system(command.str().c_str()); + if(result == 0) { + return HasReturnvaluesIF::RETURN_OK; + } + sif::warning << "SdCardManager::setSdCardState: system call failed with code " << + result << std::endl; + return HasReturnvaluesIF::RETURN_FAILED; +} + +ReturnValue_t SdCardManager::sdCardActive(std::pair& active) { using namespace std; if(not filesystem::exists("/tmp/sd_status.txt")) { - ReturnValue_t result = updateSdCardStateFile(); - if(result != HasReturnvaluesIF::RETURN_OK) { - return result; - } + return STATUS_FILE_NEXISTS; } // Now the file should exist in any case. Still check whether it exists. fstream sdStatus("/tmp/sd_status.txt"); if (not sdStatus.good()) { - return false; + return STATUS_FILE_NEXISTS; } string line; uint8_t idx = 0; + ReturnValue_t result = HasReturnvaluesIF::RETURN_OK; + bool on = false; + while (std::getline(sdStatus, line)) { istringstream iss(line); string word; - if((sdCard == sd::SdCard::SLOT_0 and idx == 0) or - (sdCard == sd::SdCard::SLOT_1 and idx == 1)) { - while(iss >> word) { - if(word == "on") { - return true; - } - else { - return false; - } + while(iss >> word) { + if(word == "on") { + on = true; + } + else if (word == "off") { + on = false; + } + else { + continue; + } + if(idx == 0) { + active.first = on; + } + else if(idx == 1) { + active.second = on; + } + else if(idx > 1) { + sif::warning << "SdCardManager::sdCardActive: Status file has more " + "than 2 lines!" << std::endl; + return STATUS_FILE_FORMAT_INVALID; } } idx++; } return HasReturnvaluesIF::RETURN_OK; - return false; } sd::SdCard SdCardManager::getPreferredSdCard() const { diff --git a/bsp_q7s/memory/SdCardManager.h b/bsp_q7s/memory/SdCardManager.h index 33b3db0c..98ecff24 100644 --- a/bsp_q7s/memory/SdCardManager.h +++ b/bsp_q7s/memory/SdCardManager.h @@ -7,6 +7,7 @@ #include "fsfw/returnvalues/HasReturnvaluesIF.h" #include +#include class MutexIF; @@ -21,6 +22,12 @@ public: static constexpr ReturnValue_t ALREADY_ON = HasReturnvaluesIF::makeReturnCode(INTERFACE_ID, 0); static constexpr ReturnValue_t ALREADY_OFF = HasReturnvaluesIF::makeReturnCode(INTERFACE_ID, 1); + static constexpr ReturnValue_t STATUS_FILE_NEXISTS = + HasReturnvaluesIF::makeReturnCode(INTERFACE_ID, 2); + static constexpr ReturnValue_t STATUS_FILE_FORMAT_INVALID = + HasReturnvaluesIF::makeReturnCode(INTERFACE_ID, 3); + static constexpr ReturnValue_t SYSTEM_CALL_ERROR = + HasReturnvaluesIF::makeReturnCode(INTERFACE_ID, 4); virtual ~SdCardManager(); @@ -39,14 +46,33 @@ public: ReturnValue_t switchOffSdCard(sd::SdCard sdCard); - bool sdCardActive(sd::SdCard sdCard); + /** + * Updated the state file or creates one if it does not exist. You need to call this + * function before calling #sdCardActiv + * @return - RETURN_OK if the state file was updated successfully + * - SYSTEM_CALL_ERROR if the call to create the status file failed + */ + ReturnValue_t updateSdCardStateFile(); + + /** + * Get the state of the SD cards. If the state file does not exist, this function will + * take care of updating it. If it does not, the function will use the state file to get + * the status of the SD cards and set the field of the provided boolean pair. + * @param active Pair of booleans, where the first entry is the state of the first SD card + * and the second one the state of the second SD card + * @return - RETURN_OK if the state was read successfully + * - STATUS_FILE_FORMAT_INVALID if there was an issue with the state file. The user + * should call #updateSdCardStateFile again in that case + * - STATUS_FILE_NEXISTS if the status file does not exist + */ + ReturnValue_t sdCardActive(std::pair& active); sd::SdCard getPreferedSdCard() const; private: SdCardManager(); - ReturnValue_t updateSdCardStateFile(); + ReturnValue_t setSdCardState(sd::SdCard sdCard, bool on); sd::SdCard preferredSdCard = sd::SdCard::SLOT_0; diff --git a/bsp_q7s/memory/definitions.h b/bsp_q7s/memory/definitions.h index 460f294b..9a998dcc 100644 --- a/bsp_q7s/memory/definitions.h +++ b/bsp_q7s/memory/definitions.h @@ -5,7 +5,8 @@ namespace sd { enum SdCard { SLOT_0, - SLOT_1 + SLOT_1, + BOTH }; }