eive-obsw/bsp_q7s/memory/SdCardManager.cpp

91 lines
2.3 KiB
C++
Raw Normal View History

2021-07-07 20:50:11 +02:00
#include "SdCardManager.h"
#include "fsfw/ipc/MutexFactory.h"
2021-07-08 00:03:17 +02:00
#include "fsfw/serviceinterface/ServiceInterface.h"
2021-07-07 20:50:11 +02:00
#include <fstream>
#include <filesystem>
SdCardManager* SdCardManager::factoryInstance = nullptr;
SdCardManager::SdCardManager() {
}
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) {
if(sdCardActive(sdCard)) {
return ALREADY_ON;
}
return HasReturnvaluesIF::RETURN_OK;
}
ReturnValue_t SdCardManager::switchOffSdCard(sd::SdCard sdCard) {
return HasReturnvaluesIF::RETURN_OK;
}
bool SdCardManager::sdCardActive(sd::SdCard sdCard) {
2021-07-08 00:03:17 +02:00
using namespace std;
if(not filesystem::exists("/tmp/sd_status.txt")) {
ReturnValue_t result = updateSdCardStateFile();
if(result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
2021-07-07 20:50:11 +02:00
}
2021-07-08 00:03:17 +02:00
// 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;
}
string line;
uint8_t idx = 0;
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;
}
}
}
idx++;
}
return HasReturnvaluesIF::RETURN_OK;
2021-07-07 20:50:11 +02:00
return false;
}
sd::SdCard SdCardManager::getPreferredSdCard() const {
return preferredSdCard;
}
void SdCardManager::setPreferredSdCard(sd::SdCard sdCard) {
preferredSdCard = sdCard;
}
2021-07-08 00:03:17 +02:00
ReturnValue_t SdCardManager::updateSdCardStateFile() {
int result = std::system("q7hw sd info all > /tmp/sd_status.txt");
if(result == 0) {
return HasReturnvaluesIF::RETURN_OK;
}
sif::warning << "SdCardManager::updateSdCardStateFile: system call failed with code " <<
result << std::endl;
return HasReturnvaluesIF::RETURN_FAILED;
2021-07-07 20:50:11 +02:00
}