#include "SdCardManager.h" #include "scratchApi.h" #include "linux/utility/utility.h" #include "fsfw/ipc/MutexFactory.h" #include "fsfw/serviceinterface/ServiceInterface.h" #include #include #include #include #include SdCardManager* SdCardManager::factoryInstance = nullptr; SdCardManager::SdCardManager(): cmdExecutor(256) { } SdCardManager::~SdCardManager() { } void SdCardManager::create() { if(factoryInstance == nullptr) { factoryInstance = new SdCardManager(); } } SdCardManager* SdCardManager::instance() { SdCardManager::create(); return SdCardManager::factoryInstance; } ReturnValue_t SdCardManager::switchOnSdCard(sd::SdCard sdCard, bool doMountSdCard, SdStatusPair* statusPair) { ReturnValue_t result = HasReturnvaluesIF::RETURN_OK; std::unique_ptr sdStatusPtr; if(statusPair == nullptr) { sdStatusPtr = std::make_unique(); statusPair = sdStatusPtr.get(); result = getSdCardActiveStatus(*statusPair); if(result != HasReturnvaluesIF::RETURN_OK) { return result; } } // Not allowed, this function turns on one SD card if(sdCard == sd::SdCard::BOTH) { sif::warning << "SdCardManager::switchOffSdCard: API does not allow sd::SdStatus::BOTH" << std::endl; return HasReturnvaluesIF::RETURN_FAILED; } sd::SdStatus targetStatus; if(sdCard == sd::SdCard::SLOT_0) { targetStatus = statusPair->first; } else if(sdCard == sd::SdCard::SLOT_1) { targetStatus = statusPair->second; } else { // Should not happen targetStatus = sd::SdStatus::OFF; } auto switchCall = [&]() { if(targetStatus == sd::SdStatus::ON) { if(not doMountSdCard) { return ALREADY_ON; } else { return mountSdCard(sdCard); } } else if(targetStatus == sd::SdStatus::MOUNTED) { return ALREADY_MOUNTED; } else if(targetStatus == sd::SdStatus::OFF) { return setSdCardState(sdCard, true); } else { return HasReturnvaluesIF::RETURN_FAILED; } }; result = switchCall(); if(result != HasReturnvaluesIF::RETURN_OK or not doMountSdCard) { return result; } return mountSdCard(sdCard); } ReturnValue_t SdCardManager::switchOffSdCard(sd::SdCard sdCard, bool doUnmountSdCard, SdStatusPair* statusPair) { std::pair active; ReturnValue_t result = getSdCardActiveStatus(active); if(result != HasReturnvaluesIF::RETURN_OK) { return result; } // Not allowed, this function turns off one SD card if(sdCard == sd::SdCard::BOTH) { sif::warning << "SdCardManager::switchOffSdCard: API does not allow sd::SdStatus::BOTH" << std::endl; return HasReturnvaluesIF::RETURN_FAILED; } if(sdCard == sd::SdCard::SLOT_0) { if(active.first == sd::SdStatus::OFF) { return ALREADY_OFF; } } else if(sdCard == sd::SdCard::SLOT_1) { if(active.second == sd::SdStatus::OFF) { return ALREADY_OFF; } } if(doUnmountSdCard) { result = unmountSdCard(sdCard); if(result != HasReturnvaluesIF::RETURN_OK) { return result; } } return setSdCardState(sdCard, false); } ReturnValue_t SdCardManager::setSdCardState(sd::SdCard sdCard, bool on) { using namespace std; string sdstring = ""; string statestring = ""; if(currentOp != Operations::IDLE) { return OP_ONGOING; } if(sdCard == sd::SdCard::SLOT_0) { sdstring = "0"; } else if(sdCard == sd::SdCard::SLOT_1) { sdstring = "1"; } if(on) { currentOp = Operations::SWITCHING_ON; statestring = "on"; } else { currentOp = Operations::SWITCHING_OFF; statestring = "off"; } ostringstream command; command << "q7hw sd set " << sdstring << " " << statestring; cmdExecutor.load(command.str(), blocking, true); ReturnValue_t result = cmdExecutor.execute(); if(blocking and result != HasReturnvaluesIF::RETURN_OK) { utility::handleSystemError(cmdExecutor.getLastError(), "SdCardManager::setSdCardState"); } return result; } ReturnValue_t SdCardManager::getSdCardActiveStatus(SdStatusPair& active) { using namespace std; if(not filesystem::exists(SD_STATE_FILE)) { return STATUS_FILE_NEXISTS; } // Now the file should exist in any case. Still check whether it exists. fstream sdStatus(SD_STATE_FILE); if (not sdStatus.good()) { return STATUS_FILE_NEXISTS; } string line; uint8_t idx = 0; sd::SdCard currentSd = sd::SdCard::SLOT_0; // Process status file line by line while (std::getline(sdStatus, line)) { processSdStatusLine(active, line, idx, currentSd); } return HasReturnvaluesIF::RETURN_OK; } ReturnValue_t SdCardManager::mountSdCard(sd::SdCard sdCard) { using namespace std; if(currentOp != Operations::IDLE) { return OP_ONGOING; } if(sdCard == sd::SdCard::BOTH) { sif::warning << "SdCardManager::mountSdCard: API does not allow sd::SdStatus::BOTH" << std::endl; return HasReturnvaluesIF::RETURN_FAILED; } string mountDev; string mountPoint; if(sdCard == sd::SdCard::SLOT_0) { mountDev = SD_0_DEV_NAME; mountPoint = SD_0_MOUNT_POINT; } else if(sdCard == sd::SdCard::SLOT_1) { mountDev = SD_1_DEV_NAME; mountPoint = SD_1_MOUNT_POINT; } if(not filesystem::exists(mountDev)) { sif::warning << "SdCardManager::mountSdCard: Device file does not exists. Make sure to" " turn on the SD card" << std::endl; return MOUNT_ERROR; } string sdMountCommand = "mount " + mountDev + " " + mountPoint; cmdExecutor.load(sdMountCommand, blocking, true); 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) { using namespace std; if(sdCard == sd::SdCard::BOTH) { sif::warning << "SdCardManager::unmountSdCard: API does not allow sd::SdStatus::BOTH" << std::endl; return HasReturnvaluesIF::RETURN_FAILED; } string mountPoint; if(sdCard == sd::SdCard::SLOT_0) { mountPoint = SD_0_MOUNT_POINT; } else if(sdCard == sd::SdCard::SLOT_1) { mountPoint = SD_1_MOUNT_POINT; } if(not filesystem::exists(mountPoint)) { sif::error << "SdCardManager::unmountSdCard: Default mount point " << mountPoint << "does not exist" << std::endl; return UNMOUNT_ERROR; } if(filesystem::is_empty(mountPoint)) { // The mount point will always exist, but if it is empty, that is strong hint that // the SD card was not mounted properly. Still proceed with operation. sif::warning << "SdCardManager::unmountSdCard: Mount point is empty!" << std::endl; } string sdUnmountCommand = "umount " + mountPoint; cmdExecutor.load(sdUnmountCommand, blocking, true); 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) { std::unique_ptr sdStatusPtr; if(prefSdCard == sd::SdCard::NONE) { ReturnValue_t result = getPreferredSdCard(prefSdCard); if(result != HasReturnvaluesIF::RETURN_OK) {} } if(statusPair == nullptr) { sdStatusPtr = std::make_unique(); statusPair = sdStatusPtr.get(); getSdCardActiveStatus(*statusPair); } if(statusPair->first == sd::SdStatus::ON) { return mountSdCard(prefSdCard); } return switchOnSdCard(prefSdCard, true, statusPair); } void SdCardManager::processSdStatusLine(std::pair &active, std::string& line, uint8_t& idx, sd::SdCard& currentSd) { using namespace std; istringstream iss(line); string word; bool slotLine = false; bool mountLine = false; while(iss >> word) { if (word == "Slot") { slotLine = true; } if(word == "Mounted") { mountLine = true; } if(slotLine) { if (word == "1:") { currentSd = sd::SdCard::SLOT_1; } if(word == "on") { if(currentSd == sd::SdCard::SLOT_0) { active.first = sd::SdStatus::ON; } else { active.second = sd::SdStatus::ON; } } else if (word == "off") { if(currentSd == sd::SdCard::SLOT_0) { active.first = sd::SdStatus::OFF; } else { active.second = sd::SdStatus::OFF; } } } if(mountLine) { if(currentSd == sd::SdCard::SLOT_0) { active.first = sd::SdStatus::MOUNTED; } else { active.second = sd::SdStatus::MOUNTED; } } if(idx > 5) { sif::warning << "SdCardManager::sdCardActive: /tmp/sd_status.txt has more than 6 " "lines and might be invalid!" << std::endl; } } 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; } sdCard = static_cast(prefSdCard); return HasReturnvaluesIF::RETURN_OK; } ReturnValue_t SdCardManager::setPreferredSdCard(sd::SdCard sdCard) { if(sdCard == sd::SdCard::BOTH) { return HasReturnvaluesIF::RETURN_FAILED; } return scratch::writeNumber(scratch::PREFERED_SDC_KEY, static_cast(sdCard)); } ReturnValue_t SdCardManager::updateSdCardStateFile() { // 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, true); ReturnValue_t result = cmdExecutor.execute(); if(blocking and result != HasReturnvaluesIF::RETURN_OK) { utility::handleSystemError(cmdExecutor.getLastError(), "SdCardManager::mountSdCard"); } 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) { return SD_0_MOUNT_POINT; } else { return SD_1_MOUNT_POINT; } } SdCardManager::OpStatus SdCardManager::checkCurrentOp(Operations ¤tOp) { // currentOp = this->currentOp; // switch(currentOpStatus) { // case(OpStatus::IDLE): // case(OpStatus::FAIL): // case(OpStatus::TIMEOUT): // case(OpStatus::SUCCESS):{ // break; // } // case(OpStatus::ONGOING): { // int result = poll(&waiter, 1, 0); // switch(result) { // case(0): { // // No data available // break; // } // case(1): { // if (waiter.revents & POLLIN) { // ssize_t readBytes = read(opFileNum, readBuf.data(), readBuf.size()); // if(readBytes == 0) { // // Should not happen.. // sif::warning << "SdCardManager::checkCurrentOp: 0 bytes read" << std::endl; // } // else if(readBytes > 0) { // sif::info << currentCmd << " | " << readBuf.data() << std::endl; // } // else { // sif::error << "SdCardManager::checkCurrentOp: Error code " << errno << // ": " << strerror(errno) << std::endl; // return OpStatus::FAIL; // } // } // else if (waiter.revents & POLLERR) { // sif::warning << "SdCardManager::checkCurrentOp: Poll error.." << std::endl; // } // else if(waiter.revents & POLLHUP) { // int result = pclose(opFile); // if(result != 0) { // sif::error << "SdCardManager::setSdCardState: pclose failed with code " << // result << ": " << strerror(result) << std::endl; // } // opFile = nullptr; // opFileNum = 0; // currentOp = Operations::IDLE; // return OpStatus::SUCCESS; // } // break; // } // default: { // sif::error << "SdCardManager::checkCurrentOp: Unexpected returnvalue " << result << // "for poll call" << std::endl; // break; // } // } // } // } // return currentOpStatus; } //void SdCardManager::resetState() { // currentOp = Operations::IDLE; // currentOpStatus = OpStatus::IDLE; // currentOpSdCard = sd::SdCard::NONE; //} // //void SdCardManager::setBlocking(bool blocking) { // this->blocking = blocking; // if(not blocking) { // currentOp = Operations::IDLE; // currentOpStatus = OpStatus::IDLE; // } //} ReturnValue_t SdCardManager::handleCommand(std::string cmd, std::string funcName) { // currentCmd = cmd; // opFile = popen(currentCmd.c_str(), "r"); // if(opFile == nullptr) { // return POPEN_CALL_ERROR; // } // if(blocking) { // return handleBlockingOperation(funcName); // } // else { // opFileNum = fileno(opFile); // waiter.fd = opFileNum; // } return HasReturnvaluesIF::RETURN_OK; } void SdCardManager::setBlocking(bool blocking) { this->blocking = blocking; } ReturnValue_t SdCardManager::handleBlockingOperation(std::string funcName) { // while(fgets(readBuf.data(), readBuf.size(), opFile) != nullptr) { // sif::info << currentCmd << " | " << readBuf.data() << std::endl; // } // int result = pclose(opFile); // if(result != 0) { // sif::error << "SdCardManager::setSdCardState: pclose failed with code " << // result << ": " << strerror(result) << std::endl; // return HasReturnvaluesIF::RETURN_FAILED; // } return HasReturnvaluesIF::RETURN_OK; }