2021-07-07 20:50:11 +02:00
|
|
|
#include "SdCardManager.h"
|
2021-07-12 14:32:20 +02:00
|
|
|
#include "scratchApi.h"
|
|
|
|
|
2021-07-12 11:37:10 +02:00
|
|
|
#include "linux/utility/utility.h"
|
|
|
|
|
2021-07-07 20:50:11 +02:00
|
|
|
#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>
|
2021-07-12 17:23:32 +02:00
|
|
|
#include <memory>
|
2021-07-07 20:50:11 +02:00
|
|
|
#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;
|
|
|
|
}
|
|
|
|
|
2021-07-12 15:19:06 +02:00
|
|
|
ReturnValue_t SdCardManager::switchOnSdCard(sd::SdCard sdCard, bool doMountSdCard,
|
|
|
|
SdStatusPair* statusPair) {
|
|
|
|
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
|
2021-07-16 20:32:13 +02:00
|
|
|
std::unique_ptr<SdStatusPair> sdStatusPtr;
|
2021-07-12 15:19:06 +02:00
|
|
|
if(statusPair == nullptr) {
|
2021-07-16 20:32:13 +02:00
|
|
|
sdStatusPtr = std::make_unique<SdStatusPair>();
|
|
|
|
statusPair = sdStatusPtr.get();
|
2021-07-12 15:19:06 +02:00
|
|
|
result = getSdCardActiveStatus(*statusPair);
|
|
|
|
if(result != HasReturnvaluesIF::RETURN_OK) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-12 11:37:10 +02:00
|
|
|
// 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;
|
|
|
|
}
|
2021-07-12 15:19:06 +02:00
|
|
|
|
|
|
|
sd::SdStatus targetStatus;
|
2021-07-12 11:37:10 +02:00
|
|
|
if(sdCard == sd::SdCard::SLOT_0) {
|
2021-07-12 15:19:06 +02:00
|
|
|
targetStatus = statusPair->first;
|
|
|
|
}
|
|
|
|
else if(sdCard == sd::SdCard::SLOT_1) {
|
|
|
|
targetStatus = statusPair->second;
|
|
|
|
}
|
2021-07-16 22:20:53 +02:00
|
|
|
else {
|
|
|
|
// Should not happen
|
|
|
|
targetStatus = sd::SdStatus::OFF;
|
|
|
|
}
|
2021-07-12 15:19:06 +02:00
|
|
|
|
|
|
|
auto switchCall = [&]() {
|
|
|
|
if(targetStatus == sd::SdStatus::ON) {
|
2021-07-12 13:26:02 +02:00
|
|
|
if(not doMountSdCard) {
|
|
|
|
return ALREADY_ON;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return mountSdCard(sdCard);
|
|
|
|
}
|
2021-07-12 11:37:10 +02:00
|
|
|
}
|
2021-07-12 15:19:06 +02:00
|
|
|
else if(targetStatus == sd::SdStatus::MOUNTED) {
|
|
|
|
return ALREADY_MOUNTED;
|
2021-07-12 11:37:10 +02:00
|
|
|
}
|
2021-07-12 15:19:06 +02:00
|
|
|
else if(targetStatus == sd::SdStatus::OFF) {
|
|
|
|
return setSdCardState(sdCard, true);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return HasReturnvaluesIF::RETURN_FAILED;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
result = switchCall();
|
|
|
|
|
2021-07-12 11:37:10 +02:00
|
|
|
if(result != HasReturnvaluesIF::RETURN_OK or not doMountSdCard) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mountSdCard(sdCard);
|
2021-07-07 20:50:11 +02:00
|
|
|
}
|
|
|
|
|
2021-07-12 15:19:06 +02:00
|
|
|
ReturnValue_t SdCardManager::switchOffSdCard(sd::SdCard sdCard, bool doUnmountSdCard,
|
|
|
|
SdStatusPair* statusPair) {
|
2021-07-12 11:37:10 +02:00
|
|
|
std::pair<sd::SdStatus, sd::SdStatus> active;
|
2021-07-12 14:32:20 +02:00
|
|
|
ReturnValue_t result = getSdCardActiveStatus(active);
|
2021-07-08 12:07:39 +02:00
|
|
|
if(result != HasReturnvaluesIF::RETURN_OK) {
|
|
|
|
return result;
|
|
|
|
}
|
2021-07-12 15:58:43 +02:00
|
|
|
// Not allowed, this function turns off one SD card
|
2021-07-12 11:37:10 +02:00
|
|
|
if(sdCard == sd::SdCard::BOTH) {
|
|
|
|
sif::warning << "SdCardManager::switchOffSdCard: API does not allow sd::SdStatus::BOTH"
|
|
|
|
<< std::endl;
|
|
|
|
return HasReturnvaluesIF::RETURN_FAILED;
|
2021-07-08 12:07:39 +02:00
|
|
|
}
|
2021-07-12 11:37:10 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-08 11:23:08 +02:00
|
|
|
return setSdCardState(sdCard, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t SdCardManager::setSdCardState(sd::SdCard sdCard, bool on) {
|
2021-07-12 14:32:20 +02:00
|
|
|
using namespace std;
|
|
|
|
string sdstring = "";
|
|
|
|
string statestring = "";
|
2021-07-08 11:23:08 +02:00
|
|
|
if(sdCard == sd::SdCard::SLOT_0) {
|
|
|
|
sdstring = "0";
|
|
|
|
}
|
|
|
|
else if(sdCard == sd::SdCard::SLOT_1) {
|
|
|
|
sdstring = "1";
|
|
|
|
}
|
|
|
|
if(on) {
|
|
|
|
statestring = "on";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
statestring = "off";
|
|
|
|
}
|
2021-07-12 14:32:20 +02:00
|
|
|
ostringstream command;
|
2021-07-09 17:21:26 +02:00
|
|
|
command << "q7hw sd set " << sdstring << " " << statestring;
|
2021-07-12 14:32:20 +02:00
|
|
|
int result = system(command.str().c_str());
|
2021-07-08 11:23:08 +02:00
|
|
|
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-12 15:19:06 +02:00
|
|
|
ReturnValue_t SdCardManager::getSdCardActiveStatus(SdStatusPair& active) {
|
2021-07-08 00:03:17 +02:00
|
|
|
using namespace std;
|
2021-07-12 11:47:23 +02:00
|
|
|
if(not filesystem::exists(SD_STATE_FILE)) {
|
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.
|
2021-07-12 11:47:23 +02:00
|
|
|
fstream sdStatus(SD_STATE_FILE);
|
2021-07-08 00:03:17 +02:00
|
|
|
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-12 11:37:10 +02:00
|
|
|
sd::SdCard currentSd = sd::SdCard::SLOT_0;
|
|
|
|
// Process status file line by line
|
2021-07-08 00:03:17 +02:00
|
|
|
while (std::getline(sdStatus, line)) {
|
2021-07-12 11:37:10 +02:00
|
|
|
processSdStatusLine(active, line, idx, currentSd);
|
|
|
|
}
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t SdCardManager::mountSdCard(sd::SdCard sdCard) {
|
|
|
|
using namespace std;
|
|
|
|
if(sdCard == sd::SdCard::BOTH) {
|
|
|
|
sif::warning << "SdCardManager::mountSdCard: API does not allow sd::SdStatus::BOTH"
|
|
|
|
<< std::endl;
|
|
|
|
return HasReturnvaluesIF::RETURN_FAILED;
|
|
|
|
}
|
2021-07-12 14:32:20 +02:00
|
|
|
string mountDev;
|
|
|
|
string mountPoint;
|
2021-07-12 11:37:10 +02:00
|
|
|
if(sdCard == sd::SdCard::SLOT_0) {
|
|
|
|
mountDev = SD_0_DEV_NAME;
|
|
|
|
mountPoint = SD_0_MOUNT_POINT;
|
|
|
|
}
|
|
|
|
else if(sdCard == sd::SdCard::SLOT_1) {
|
2021-07-12 15:19:06 +02:00
|
|
|
mountDev = SD_1_DEV_NAME;
|
2021-07-12 11:37:10 +02:00
|
|
|
mountPoint = SD_1_MOUNT_POINT;
|
|
|
|
}
|
2021-07-12 14:32:20 +02:00
|
|
|
if(not filesystem::exists(mountDev)) {
|
2021-07-12 11:37:10 +02:00
|
|
|
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;
|
|
|
|
int result = system(sdMountCommand.c_str());
|
|
|
|
if (result != 0) {
|
|
|
|
utility::handleSystemError(result, "SdCardManager::mountSdCard");
|
|
|
|
return SYSTEM_CALL_ERROR;
|
|
|
|
}
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2021-07-23 17:06:10 +02:00
|
|
|
if(not filesystem::exists(mountPoint)) {
|
|
|
|
sif::error << "SdCardManager::unmountSdCard: Default mount point " << mountPoint <<
|
|
|
|
"does not exist" << std::endl;
|
|
|
|
return UNMOUNT_ERROR;
|
|
|
|
}
|
2021-07-12 11:37:10 +02:00
|
|
|
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;
|
|
|
|
int result = system(sdUnmountCommand.c_str());
|
|
|
|
if (result != 0) {
|
|
|
|
utility::handleSystemError(result, "SdCardManager::unmountSdCard");
|
|
|
|
return SYSTEM_CALL_ERROR;
|
|
|
|
}
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
}
|
|
|
|
|
2021-07-16 20:32:13 +02:00
|
|
|
ReturnValue_t SdCardManager::sanitizeState(SdStatusPair* statusPair, sd::SdCard prefSdCard) {
|
|
|
|
std::unique_ptr<SdStatusPair> sdStatusPtr;
|
|
|
|
if(prefSdCard == sd::SdCard::NONE) {
|
|
|
|
ReturnValue_t result = getPreferredSdCard(prefSdCard);
|
|
|
|
if(result != HasReturnvaluesIF::RETURN_OK) {}
|
2021-07-12 17:23:32 +02:00
|
|
|
}
|
|
|
|
if(statusPair == nullptr) {
|
2021-07-16 20:32:13 +02:00
|
|
|
sdStatusPtr = std::make_unique<SdStatusPair>();
|
|
|
|
statusPair = sdStatusPtr.get();
|
2021-07-12 17:23:32 +02:00
|
|
|
getSdCardActiveStatus(*statusPair);
|
|
|
|
}
|
|
|
|
|
2021-07-16 20:32:13 +02:00
|
|
|
if(statusPair->first == sd::SdStatus::ON) {
|
|
|
|
return mountSdCard(prefSdCard);
|
|
|
|
}
|
2021-07-12 17:23:32 +02:00
|
|
|
|
2021-07-16 20:32:13 +02:00
|
|
|
return switchOnSdCard(prefSdCard, true, statusPair);
|
2021-07-12 17:23:32 +02:00
|
|
|
}
|
|
|
|
|
2021-07-12 11:37:10 +02:00
|
|
|
void SdCardManager::processSdStatusLine(std::pair<sd::SdStatus, sd::SdStatus> &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) {
|
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) {
|
2021-07-12 11:37:10 +02:00
|
|
|
active.first = sd::SdStatus::ON;
|
2021-07-09 17:54:32 +02:00
|
|
|
}
|
|
|
|
else {
|
2021-07-12 11:37:10 +02:00
|
|
|
active.second = sd::SdStatus::ON;
|
2021-07-09 17:54:32 +02:00
|
|
|
}
|
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) {
|
2021-07-12 11:37:10 +02:00
|
|
|
active.first = sd::SdStatus::OFF;
|
2021-07-09 17:54:32 +02:00
|
|
|
}
|
|
|
|
else {
|
2021-07-12 11:37:10 +02:00
|
|
|
active.second = sd::SdStatus::OFF;
|
2021-07-09 17:54:32 +02:00
|
|
|
}
|
2021-07-08 11:23:08 +02:00
|
|
|
}
|
2021-07-12 11:37:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(mountLine) {
|
|
|
|
if(currentSd == sd::SdCard::SLOT_0) {
|
|
|
|
active.first = sd::SdStatus::MOUNTED;
|
|
|
|
}
|
2021-07-08 11:23:08 +02:00
|
|
|
else {
|
2021-07-12 11:37:10 +02:00
|
|
|
active.second = sd::SdStatus::MOUNTED;
|
2021-07-08 11:23:08 +02:00
|
|
|
}
|
2021-07-12 11:37:10 +02:00
|
|
|
}
|
2021-07-09 17:54:32 +02:00
|
|
|
|
2021-07-12 11:37:10 +02:00
|
|
|
if(idx > 5) {
|
|
|
|
sif::warning << "SdCardManager::sdCardActive: /tmp/sd_status.txt has more than 6 "
|
|
|
|
"lines and might be invalid!" << std::endl;
|
2021-07-08 00:03:17 +02:00
|
|
|
}
|
|
|
|
}
|
2021-07-12 11:37:10 +02:00
|
|
|
idx++;
|
2021-07-07 20:50:11 +02:00
|
|
|
}
|
|
|
|
|
2021-07-12 14:32:20 +02:00
|
|
|
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<sd::SdCard>(prefSdCard);
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
2021-07-07 20:50:11 +02:00
|
|
|
}
|
|
|
|
|
2021-07-12 14:32:20 +02:00
|
|
|
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<uint8_t>(sdCard));
|
2021-07-07 20:50:11 +02:00
|
|
|
}
|
|
|
|
|
2021-07-08 00:03:17 +02:00
|
|
|
ReturnValue_t SdCardManager::updateSdCardStateFile() {
|
2021-07-12 11:47:23 +02:00
|
|
|
// Use q7hw utility and pipe the command output into the state file
|
2021-07-12 11:49:19 +02:00
|
|
|
std::string updateCmd = "q7hw sd info all > " + std::string(SD_STATE_FILE);
|
2021-07-12 11:47:23 +02:00
|
|
|
int result = std::system(updateCmd.c_str());
|
2021-07-08 00:03:17 +02:00
|
|
|
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
|
|
|
}
|
2021-07-16 20:32:13 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|