eive-obsw/bsp_q7s/memory/SdCardManager.cpp

151 lines
4.2 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) {
2021-07-08 12:07:39 +02:00
std::pair<bool, bool> active;
ReturnValue_t result = sdCardActive(active);
if(result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
if((sdCard == sd::SdCard::SLOT_0 and active.first) or
(sdCard == sd::SdCard::SLOT_1 and active.second)) {
return ALREADY_ON;
}
2021-07-08 11:23:08 +02:00
return setSdCardState(sdCard, true);
2021-07-07 20:50:11 +02:00
}
ReturnValue_t SdCardManager::switchOffSdCard(sd::SdCard sdCard) {
2021-07-08 12:07:39 +02:00
std::pair<bool, bool> active;
ReturnValue_t result = sdCardActive(active);
if(result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
if((sdCard == sd::SdCard::SLOT_0 and not active.first) or
(sdCard == sd::SdCard::SLOT_1 and not active.second)) {
return ALREADY_OFF;
}
2021-07-08 11:23:08 +02:00
return setSdCardState(sdCard, false);
}
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;
2021-07-09 17:21:26 +02:00
command << "q7hw sd set " << sdstring << " " << statestring;
2021-07-08 11:23:08 +02:00
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;
2021-07-08 12:07:39 +02:00
return SYSTEM_CALL_ERROR;
2021-07-07 20:50:11 +02:00
}
2021-07-08 11:23:08 +02:00
ReturnValue_t SdCardManager::sdCardActive(std::pair<bool, bool>& active) {
2021-07-08 00:03:17 +02:00
using namespace std;
if(not filesystem::exists("/tmp/sd_status.txt")) {
2021-07-08 11:23:08 +02:00
return STATUS_FILE_NEXISTS;
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()) {
2021-07-08 11:23:08 +02:00
return STATUS_FILE_NEXISTS;
2021-07-08 00:03:17 +02:00
}
string line;
uint8_t idx = 0;
2021-07-08 11:23:08 +02:00
2021-07-08 00:03:17 +02:00
while (std::getline(sdStatus, line)) {
istringstream iss(line);
string word;
2021-07-09 17:54:32 +02:00
sd::SdCard currentSd = sd::SdCard::SLOT_0;
2021-07-08 11:23:08 +02:00
while(iss >> word) {
2021-07-09 17:54:32 +02:00
if (word == "1:") {
currentSd = sd::SdCard::SLOT_1;
}
2021-07-08 11:23:08 +02:00
if(word == "on") {
2021-07-09 17:54:32 +02:00
if(currentSd == sd::SdCard::SLOT_0) {
active.first = true;
}
else {
active.second = true;
}
2021-07-08 11:23:08 +02:00
}
else if (word == "off") {
2021-07-09 17:54:32 +02:00
if(currentSd == sd::SdCard::SLOT_0) {
active.first = false;
}
else {
active.second = false;
}
2021-07-08 11:23:08 +02:00
}
else {
continue;
}
2021-07-09 17:54:32 +02:00
if(idx > 3) {
2021-07-08 11:23:08 +02:00
sif::warning << "SdCardManager::sdCardActive: Status file has more "
2021-07-09 17:54:32 +02:00
"than 4 lines!" << std::endl;
2021-07-08 11:23:08 +02:00
return STATUS_FILE_FORMAT_INVALID;
2021-07-08 00:03:17 +02:00
}
}
idx++;
}
return HasReturnvaluesIF::RETURN_OK;
2021-07-07 20:50:11 +02:00
}
sd::SdCard SdCardManager::getPreferredSdCard() const {
2021-07-09 17:54:32 +02:00
//int result = std::system("xsc_scratch read PREFSD > /tmp/pref_sd.txt");
2021-07-07 20:50:11 +02:00
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
}