283 lines
8.6 KiB
C++
283 lines
8.6 KiB
C++
#include "SdCardManager.h"
|
|
#include "linux/utility/utility.h"
|
|
|
|
#include "fsfw/ipc/MutexFactory.h"
|
|
#include "fsfw/serviceinterface/ServiceInterface.h"
|
|
|
|
#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, bool doMountSdCard) {
|
|
std::pair<sd::SdStatus, sd::SdStatus> active;
|
|
ReturnValue_t result = sdCardActive(active);
|
|
// 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;
|
|
}
|
|
if(result != HasReturnvaluesIF::RETURN_OK) {
|
|
return result;
|
|
}
|
|
if(sdCard == sd::SdCard::SLOT_0) {
|
|
if(active.first == sd::SdStatus::MOUNTED) {
|
|
return ALREADY_MOUNTED;
|
|
}
|
|
else if(active.first == sd::SdStatus::ON) {
|
|
if(not doMountSdCard) {
|
|
return ALREADY_ON;
|
|
}
|
|
else {
|
|
return mountSdCard(sdCard);
|
|
}
|
|
}
|
|
|
|
}
|
|
else if(sdCard == sd::SdCard::SLOT_1) {
|
|
if(active.second == sd::SdStatus::ON or active.second == sd::SdStatus::MOUNTED) {
|
|
return ALREADY_ON;
|
|
}
|
|
}
|
|
result = setSdCardState(sdCard, true);
|
|
if(result != HasReturnvaluesIF::RETURN_OK or not doMountSdCard) {
|
|
return result;
|
|
}
|
|
|
|
return mountSdCard(sdCard);
|
|
}
|
|
|
|
ReturnValue_t SdCardManager::switchOffSdCard(sd::SdCard sdCard, bool doUnmountSdCard) {
|
|
std::pair<sd::SdStatus, sd::SdStatus> active;
|
|
ReturnValue_t result = sdCardActive(active);
|
|
if(result != HasReturnvaluesIF::RETURN_OK) {
|
|
return result;
|
|
}
|
|
// Not allowed, this function turns on off 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) {
|
|
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 << "q7hw 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 SYSTEM_CALL_ERROR;
|
|
}
|
|
|
|
ReturnValue_t SdCardManager::sdCardActive(std::pair<sd::SdStatus, sd::SdStatus>& 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(sdCard == sd::SdCard::BOTH) {
|
|
sif::warning << "SdCardManager::mountSdCard: API does not allow sd::SdStatus::BOTH"
|
|
<< std::endl;
|
|
return HasReturnvaluesIF::RETURN_FAILED;
|
|
}
|
|
std::string mountDev;
|
|
std::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_0_DEV_NAME;
|
|
mountPoint = SD_1_MOUNT_POINT;
|
|
}
|
|
if(not std::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;
|
|
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;
|
|
}
|
|
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;
|
|
}
|
|
|
|
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) {
|
|
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++;
|
|
}
|
|
|
|
sd::SdCard SdCardManager::getPreferredSdCard() const {
|
|
//int result = std::system("xsc_scratch read PREFSD > /tmp/pref_sd.txt");
|
|
return preferredSdCard;
|
|
}
|
|
|
|
void SdCardManager::setPreferredSdCard(sd::SdCard sdCard) {
|
|
preferredSdCard = 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);
|
|
int result = std::system(updateCmd.c_str());
|
|
if(result == 0) {
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
}
|
|
sif::warning << "SdCardManager::updateSdCardStateFile: system call failed with code " <<
|
|
result << std::endl;
|
|
return HasReturnvaluesIF::RETURN_FAILED;
|
|
}
|