eive-obsw/bsp_q7s/fs/SdCardManager.cpp

585 lines
18 KiB
C++
Raw Permalink Normal View History

2021-07-07 20:50:11 +02:00
#include "SdCardManager.h"
2022-03-01 17:41:58 +01:00
#include <fsfw/ipc/MutexGuard.h>
2022-03-08 09:37:23 +01:00
#include <fsfw/timemanager/Countdown.h>
2023-03-10 02:05:51 +01:00
#include <fsfw/timemanager/Stopwatch.h>
2021-08-05 10:14:17 +02:00
#include <unistd.h>
2022-01-17 15:58:27 +01:00
#include <cstring>
#include <filesystem>
2021-07-07 20:50:11 +02:00
#include <fstream>
2021-07-12 17:23:32 +02:00
#include <memory>
2022-01-17 15:58:27 +01:00
2022-03-17 10:20:39 +01:00
#include "OBSWConfig.h"
2022-09-16 11:53:33 +02:00
#include "bsp_q7s/memory/scratchApi.h"
2022-09-26 11:39:17 +02:00
#include "eive/definitions.h"
2022-09-16 12:28:39 +02:00
#include "eive/objects.h"
2022-01-17 15:58:27 +01:00
#include "fsfw/ipc/MutexFactory.h"
#include "fsfw/serviceinterface/ServiceInterface.h"
#include "linux/utility/utility.h"
2021-07-07 20:50:11 +02:00
2022-05-24 00:20:23 +02:00
SdCardManager* SdCardManager::INSTANCE = nullptr;
2021-07-07 20:50:11 +02:00
2022-03-01 17:41:58 +01:00
SdCardManager::SdCardManager() : SystemObject(objects::SDC_MANAGER), cmdExecutor(256) {
2023-03-02 15:21:45 +01:00
sdLock = MutexFactory::instance()->createMutex();
prefLock = MutexFactory::instance()->createMutex();
defaultLock = MutexFactory::instance()->createMutex();
MutexGuard mg(prefLock, LOCK_TYPE, OTHER_TIMEOUT, LOCK_CTX);
if (mg.getLockResult() != returnvalue::OK) {
2022-05-26 01:56:19 +02:00
sif::error << "SdCardManager::SdCardManager: Mutex lock failed" << std::endl;
}
2022-05-24 00:20:23 +02:00
uint8_t prefSdRaw = 0;
ReturnValue_t result = scratch::readNumber(scratch::PREFERED_SDC_KEY, prefSdRaw);
2022-05-24 00:20:23 +02:00
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
2022-05-24 00:20:23 +02:00
if (result == scratch::KEY_NOT_FOUND) {
sif::warning << "CoreController::sdCardInit: "
"Preferred SD card not set. Setting to 0"
<< std::endl;
scratch::writeNumber(scratch::PREFERED_SDC_KEY, static_cast<uint8_t>(sd::SdCard::SLOT_0));
prefSdRaw = sd::SdCard::SLOT_0;
2022-05-24 00:20:23 +02:00
} else {
// Should not happen.
// TODO: Maybe trigger event?
sif::error << "SdCardManager::SdCardManager: Reading preferred SD card from scratch"
"buffer failed"
<< std::endl;
prefSdRaw = sd::SdCard::SLOT_0;
2022-05-24 00:20:23 +02:00
}
}
sdInfo.pref = static_cast<sd::SdCard>(prefSdRaw);
2022-03-01 17:41:58 +01:00
}
2021-07-07 20:50:11 +02:00
2022-01-17 15:58:27 +01:00
SdCardManager::~SdCardManager() {}
2021-07-07 20:50:11 +02:00
void SdCardManager::create() {
2022-05-24 00:20:23 +02:00
if (INSTANCE == nullptr) {
INSTANCE = new SdCardManager();
2022-01-17 15:58:27 +01:00
}
2021-07-07 20:50:11 +02:00
}
SdCardManager* SdCardManager::instance() {
2022-01-17 15:58:27 +01:00
SdCardManager::create();
2022-05-24 00:20:23 +02:00
return SdCardManager::INSTANCE;
2021-07-07 20:50:11 +02:00
}
2021-07-12 15:19:06 +02:00
ReturnValue_t SdCardManager::switchOnSdCard(sd::SdCard sdCard, bool doMountSdCard,
2022-01-17 15:58:27 +01:00
SdStatePair* statusPair) {
2022-08-24 17:27:47 +02:00
ReturnValue_t result = returnvalue::OK;
2022-01-17 15:58:27 +01:00
if (doMountSdCard) {
if (not blocking) {
sif::warning << "SdCardManager::switchOnSdCard: Two-step command but manager is"
" not configured for blocking operation. "
"Forcing blocking mode.."
<< std::endl;
blocking = true;
}
}
std::unique_ptr<SdStatePair> sdStatusPtr;
if (statusPair == nullptr) {
sdStatusPtr = std::make_unique<SdStatePair>();
statusPair = sdStatusPtr.get();
2022-05-24 00:20:23 +02:00
result = getSdCardsStatus(*statusPair);
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
2022-01-17 15:58:27 +01:00
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;
2022-08-24 17:27:47 +02:00
return returnvalue::FAILED;
2022-01-17 15:58:27 +01:00
}
sd::SdState currentState;
if (sdCard == sd::SdCard::SLOT_0) {
currentState = statusPair->first;
} else if (sdCard == sd::SdCard::SLOT_1) {
currentState = statusPair->second;
} else {
// Should not happen
currentState = sd::SdState::OFF;
}
if (currentState == sd::SdState::ON) {
if (not doMountSdCard) {
return ALREADY_ON;
} else {
return mountSdCard(sdCard);
}
} else if (currentState == sd::SdState::MOUNTED) {
result = ALREADY_MOUNTED;
} else if (currentState == sd::SdState::OFF) {
result = setSdCardState(sdCard, true);
} else {
2022-08-24 17:27:47 +02:00
result = returnvalue::FAILED;
2022-01-17 15:58:27 +01:00
}
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK or not doMountSdCard) {
2022-01-17 15:58:27 +01:00
return result;
}
2021-07-12 11:37:10 +02:00
2022-01-17 15:58:27 +01:00
return mountSdCard(sdCard);
2021-07-07 20:50:11 +02:00
}
2023-07-25 23:48:22 +02:00
ReturnValue_t SdCardManager::switchOffSdCard(sd::SdCard sdCard, SdStatePair& sdStates,
bool doUnmountSdCard) {
2022-01-17 15:58:27 +01:00
if (doUnmountSdCard) {
if (not blocking) {
sif::warning << "SdCardManager::switchOffSdCard: Two-step command but manager is"
" not configured for blocking operation. Forcing blocking mode.."
<< std::endl;
blocking = true;
}
}
// 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;
2022-08-24 17:27:47 +02:00
return returnvalue::FAILED;
2022-01-17 15:58:27 +01:00
}
if (sdCard == sd::SdCard::SLOT_0) {
2023-07-25 23:48:22 +02:00
if (sdStates.first == sd::SdState::OFF) {
2022-01-17 15:58:27 +01:00
return ALREADY_OFF;
}
} else if (sdCard == sd::SdCard::SLOT_1) {
2023-07-25 23:48:22 +02:00
if (sdStates.second == sd::SdState::OFF) {
2022-01-17 15:58:27 +01:00
return ALREADY_OFF;
}
}
if (doUnmountSdCard) {
2023-07-25 23:48:22 +02:00
ReturnValue_t result = unmountSdCard(sdCard);
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
2022-01-17 15:58:27 +01:00
return result;
2021-07-12 11:37:10 +02:00
}
2022-01-17 15:58:27 +01:00
}
2021-07-12 11:37:10 +02:00
2022-01-17 15:58:27 +01:00
return setSdCardState(sdCard, false);
2021-07-08 11:23:08 +02:00
}
ReturnValue_t SdCardManager::setSdCardState(sd::SdCard sdCard, bool on) {
2022-01-17 15:58:27 +01:00
using namespace std;
if (cmdExecutor.getCurrentState() == CommandExecutor::States::PENDING) {
return CommandExecutor::COMMAND_PENDING;
}
string sdstring = "";
string statestring = "";
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, printCmdOutput);
ReturnValue_t result = cmdExecutor.execute();
2023-07-25 23:48:22 +02:00
if (result != returnvalue::OK) {
2022-01-17 15:58:27 +01:00
utility::handleSystemError(cmdExecutor.getLastError(), "SdCardManager::setSdCardState");
}
return result;
2021-07-07 20:50:11 +02:00
}
ReturnValue_t SdCardManager::getSdCardsStatus(SdStatePair& sdStates) {
MutexGuard mg(sdLock, LOCK_TYPE, SD_LOCK_TIMEOUT, LOCK_CTX);
sdStates = this->sdStates;
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2021-07-12 11:37:10 +02:00
}
ReturnValue_t SdCardManager::mountSdCard(sd::SdCard sdCard) {
2022-01-17 15:58:27 +01:00
using namespace std;
if (cmdExecutor.getCurrentState() == CommandExecutor::States::PENDING) {
2023-07-26 00:30:09 +02:00
sif::warning << "SdCardManager::mountSdCard: Command still pending" << std::endl;
2022-01-17 15:58:27 +01:00
return CommandExecutor::COMMAND_PENDING;
}
if (sdCard == sd::SdCard::BOTH) {
sif::warning << "SdCardManager::mountSdCard: API does not allow sd::SdStatus::BOTH"
<< std::endl;
2022-08-24 17:27:47 +02:00
return returnvalue::FAILED;
2022-01-17 15:58:27 +01:00
}
string mountDev;
string mountPoint;
if (sdCard == sd::SdCard::SLOT_0) {
mountDev = SD_0_DEV_NAME;
2022-09-26 11:39:17 +02:00
mountPoint = config::SD_0_MOUNT_POINT;
2022-01-17 15:58:27 +01:00
} else if (sdCard == sd::SdCard::SLOT_1) {
mountDev = SD_1_DEV_NAME;
2022-09-26 11:39:17 +02:00
mountPoint = config::SD_1_MOUNT_POINT;
2022-01-17 15:58:27 +01:00
}
2023-03-08 14:50:25 +01:00
std::error_code e;
if (not filesystem::exists(mountDev, e)) {
2022-01-17 15:58:27 +01:00
sif::warning << "SdCardManager::mountSdCard: Device file does not exists. Make sure to"
" turn on the SD card"
<< std::endl;
return MOUNT_ERROR;
}
if (not blocking) {
currentOp = Operations::MOUNTING;
}
string sdMountCommand = "mount " + mountDev + " " + mountPoint;
cmdExecutor.load(sdMountCommand, blocking, printCmdOutput);
ReturnValue_t result = cmdExecutor.execute();
2022-08-24 17:27:47 +02:00
if (blocking and result != returnvalue::OK) {
2022-01-17 15:58:27 +01:00
utility::handleSystemError(cmdExecutor.getLastError(), "SdCardManager::mountSdCard");
}
return result;
2021-07-12 11:37:10 +02:00
}
ReturnValue_t SdCardManager::unmountSdCard(sd::SdCard sdCard) {
2022-01-17 15:58:27 +01:00
if (cmdExecutor.getCurrentState() == CommandExecutor::States::PENDING) {
return CommandExecutor::COMMAND_PENDING;
}
using namespace std;
if (sdCard == sd::SdCard::BOTH) {
sif::warning << "SdCardManager::unmountSdCard: API does not allow sd::SdStatus::BOTH"
<< std::endl;
2022-08-24 17:27:47 +02:00
return returnvalue::FAILED;
2022-01-17 15:58:27 +01:00
}
string mountPoint;
if (sdCard == sd::SdCard::SLOT_0) {
2022-09-26 11:39:17 +02:00
mountPoint = config::SD_0_MOUNT_POINT;
2022-01-17 15:58:27 +01:00
} else if (sdCard == sd::SdCard::SLOT_1) {
2022-09-26 11:39:17 +02:00
mountPoint = config::SD_1_MOUNT_POINT;
2022-01-17 15:58:27 +01:00
}
2023-03-08 14:50:25 +01:00
std::error_code e;
if (not filesystem::exists(mountPoint, e)) {
2022-01-17 15:58:27 +01:00
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;
if (not blocking) {
currentOp = Operations::UNMOUNTING;
}
cmdExecutor.load(sdUnmountCommand, blocking, printCmdOutput);
ReturnValue_t result = cmdExecutor.execute();
2022-08-24 17:27:47 +02:00
if (blocking and result != returnvalue::OK) {
2022-01-17 15:58:27 +01:00
utility::handleSystemError(cmdExecutor.getLastError(), "SdCardManager::unmountSdCard");
}
return result;
2021-07-12 11:37:10 +02:00
}
2021-08-06 01:25:36 +02:00
ReturnValue_t SdCardManager::sanitizeState(SdStatePair* statusPair, sd::SdCard prefSdCard) {
2022-01-17 15:58:27 +01:00
std::unique_ptr<SdStatePair> sdStatusPtr;
2022-08-24 17:27:47 +02:00
ReturnValue_t result = returnvalue::OK;
2022-01-17 15:58:27 +01:00
// Enforce blocking operation for now. Be careful to reset it when returning prematurely!
bool resetNonBlockingState = false;
if (not this->blocking) {
blocking = true;
resetNonBlockingState = true;
}
if (statusPair == nullptr) {
return returnvalue::FAILED;
2022-01-17 15:58:27 +01:00
}
getSdCardsStatus(*statusPair);
2022-01-17 15:58:27 +01:00
if (statusPair->first == sd::SdState::ON) {
result = mountSdCard(prefSdCard);
}
result = switchOnSdCard(prefSdCard, true, statusPair);
if (resetNonBlockingState) {
blocking = false;
}
return result;
2021-07-12 17:23:32 +02:00
}
2021-08-05 16:02:35 +02:00
void SdCardManager::resetState() {
2022-01-17 15:58:27 +01:00
cmdExecutor.reset();
currentOp = Operations::IDLE;
2021-08-05 16:02:35 +02:00
}
void SdCardManager::processSdStatusLine(std::string& line, uint8_t& idx, sd::SdCard& currentSd) {
2022-01-17 15:58:27 +01:00
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) {
sdStates.first = sd::SdState::ON;
2022-01-17 15:58:27 +01:00
} else {
sdStates.second = sd::SdState::ON;
2021-07-12 11:37:10 +02:00
}
2022-01-17 15:58:27 +01:00
} else if (word == "off") {
2023-03-24 21:29:13 +01:00
MutexGuard mg(sdLock, LOCK_TYPE, SD_LOCK_TIMEOUT, LOCK_CTX);
2022-01-17 15:58:27 +01:00
if (currentSd == sd::SdCard::SLOT_0) {
sdStates.first = sd::SdState::OFF;
2022-01-17 15:58:27 +01:00
} else {
sdStates.second = sd::SdState::OFF;
2021-07-12 11:37:10 +02:00
}
2022-01-17 15:58:27 +01:00
}
}
2021-07-12 11:37:10 +02:00
2022-01-17 15:58:27 +01:00
if (mountLine) {
2023-03-24 21:29:13 +01:00
MutexGuard mg(sdLock, LOCK_TYPE, SD_LOCK_TIMEOUT, LOCK_CTX);
2022-01-17 15:58:27 +01:00
if (currentSd == sd::SdCard::SLOT_0) {
sdStates.first = sd::SdState::MOUNTED;
2022-01-17 15:58:27 +01:00
} else {
sdStates.second = sd::SdState::MOUNTED;
2022-01-17 15:58:27 +01:00
}
}
2021-07-09 17:54:32 +02:00
2022-01-17 15:58:27 +01: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
}
2022-01-17 15:58:27 +01:00
}
idx++;
2021-07-07 20:50:11 +02:00
}
2022-09-12 17:23:43 +02:00
std::optional<sd::SdCard> SdCardManager::getPreferredSdCard() const {
MutexGuard mg(prefLock, LOCK_TYPE, OTHER_TIMEOUT, LOCK_CTX);
2022-05-24 00:20:23 +02:00
auto res = mg.getLockResult();
2022-08-24 17:27:47 +02:00
if (res != returnvalue::OK) {
2022-05-24 00:20:23 +02:00
sif::error << "SdCardManager::getPreferredSdCard: Lock error" << std::endl;
2022-01-17 15:58:27 +01:00
}
2022-05-24 00:20:23 +02:00
return sdInfo.pref;
2021-07-07 20:50:11 +02:00
}
ReturnValue_t SdCardManager::setPreferredSdCard(sd::SdCard sdCard) {
MutexGuard mg(prefLock, LOCK_TYPE, OTHER_TIMEOUT, LOCK_CTX);
2022-01-17 15:58:27 +01:00
if (sdCard == sd::SdCard::BOTH) {
2022-08-24 17:27:47 +02:00
return returnvalue::FAILED;
2022-01-17 15:58:27 +01:00
}
2022-05-24 00:20:23 +02:00
sdInfo.pref = sdCard;
2022-01-17 15:58:27 +01:00
return scratch::writeNumber(scratch::PREFERED_SDC_KEY, static_cast<uint8_t>(sdCard));
2021-07-07 20:50:11 +02:00
}
ReturnValue_t SdCardManager::updateSdCardStateFile() {
using namespace std;
2022-01-17 15:58:27 +01:00
if (cmdExecutor.getCurrentState() == CommandExecutor::States::PENDING) {
return CommandExecutor::COMMAND_PENDING;
}
// 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, true, printCmdOutput);
2022-01-17 15:58:27 +01:00
ReturnValue_t result = cmdExecutor.execute();
if (result != returnvalue::OK) {
2022-01-17 15:58:27 +01:00
utility::handleSystemError(cmdExecutor.getLastError(), "SdCardManager::mountSdCard");
}
std::error_code e;
if (not filesystem::exists(SD_STATE_FILE, e)) {
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(line, idx, currentSd);
}
if (sdStates.first != sd::SdState::MOUNTED && sdStates.second != sd::SdState::MOUNTED) {
sdCardActive = false;
}
return returnvalue::OK;
2021-07-07 20:50:11 +02:00
}
2023-02-22 18:06:34 +01:00
const char* SdCardManager::getCurrentMountPrefix() const {
MutexGuard mg(defaultLock, LOCK_TYPE, OTHER_TIMEOUT, LOCK_CTX);
2023-02-22 18:06:34 +01:00
if (currentPrefix.has_value()) {
return currentPrefix.value().c_str();
}
return nullptr;
}
2021-08-05 10:14:17 +02:00
2022-01-17 15:58:27 +01:00
SdCardManager::OpStatus SdCardManager::checkCurrentOp(Operations& currentOp) {
CommandExecutor::States state = cmdExecutor.getCurrentState();
if (state == CommandExecutor::States::IDLE or state == CommandExecutor::States::COMMAND_LOADED) {
return OpStatus::IDLE;
}
currentOp = this->currentOp;
bool bytesRead = false;
2021-08-05 16:02:35 +02:00
2021-08-05 18:37:14 +02:00
#if OBSW_ENABLE_TIMERS == 1
2022-03-08 11:57:27 +01:00
Countdown timer(1000);
2021-08-05 18:37:14 +02:00
#endif
2022-01-17 15:58:27 +01:00
while (true) {
ReturnValue_t result = cmdExecutor.check(bytesRead);
// This timer can prevent deadlocks due to missconfigurations
2021-08-05 18:37:14 +02:00
#if OBSW_ENABLE_TIMERS == 1
2022-03-08 09:37:23 +01:00
if (timer.hasTimedOut()) {
2022-01-17 15:58:27 +01:00
sif::error << "SdCardManager::checkCurrentOp: Timeout!" << std::endl;
return OpStatus::FAIL;
2021-08-05 16:02:35 +02:00
}
2022-01-17 15:58:27 +01:00
#endif
switch (result) {
case (CommandExecutor::BYTES_READ): {
continue;
}
case (CommandExecutor::EXECUTION_FINISHED): {
return OpStatus::SUCCESS;
}
2022-08-24 17:27:47 +02:00
case (returnvalue::OK): {
2022-01-17 15:58:27 +01:00
return OpStatus::ONGOING;
}
2022-08-24 17:27:47 +02:00
case (returnvalue::FAILED): {
2022-01-17 15:58:27 +01:00
return OpStatus::FAIL;
}
default: {
sif::warning << "SdCardManager::checkCurrentOp: Unhandled case" << std::endl;
}
}
}
2021-08-05 10:14:17 +02:00
}
2022-01-17 15:58:27 +01:00
void SdCardManager::setBlocking(bool blocking) { this->blocking = blocking; }
2021-08-08 00:11:21 +02:00
2022-01-17 15:58:27 +01:00
void SdCardManager::setPrintCommandOutput(bool print) { this->printCmdOutput = print; }
2021-08-08 00:11:21 +02:00
2022-10-25 11:22:31 +02:00
bool SdCardManager::isSdCardUsable(std::optional<sd::SdCard> sdCard) {
2022-10-25 11:04:24 +02:00
{
MutexGuard mg(defaultLock, LOCK_TYPE, OTHER_TIMEOUT, LOCK_CTX);
2022-10-25 11:04:24 +02:00
if (markedUnusable) {
return false;
}
}
MutexGuard mg(sdLock, LOCK_TYPE, SD_LOCK_TIMEOUT, LOCK_CTX);
2022-10-25 11:22:31 +02:00
if (not sdCard) {
if (sdStates.first == sd::MOUNTED or sdStates.second == sd::MOUNTED) {
2022-10-25 11:33:21 +02:00
return true;
}
2022-10-25 11:39:57 +02:00
return false;
2022-10-25 11:22:31 +02:00
}
2022-01-17 15:58:27 +01:00
if (sdCard == sd::SLOT_0) {
if (sdStates.first == sd::MOUNTED) {
2022-01-17 15:58:27 +01:00
return true;
} else {
return false;
}
2022-10-25 11:22:31 +02:00
}
if (sdCard == sd::SLOT_1) {
if (sdStates.second == sd::MOUNTED) {
2022-01-17 15:58:27 +01:00
return true;
} else {
return false;
}
2022-10-25 11:22:31 +02:00
}
if (sdCard == sd::BOTH) {
if (sdStates.first == sd::MOUNTED && sdStates.second == sd::MOUNTED) {
2022-10-25 11:22:31 +02:00
return true;
}
2022-01-17 15:58:27 +01:00
}
return false;
2021-11-30 16:01:02 +01:00
}
2022-05-11 17:02:57 +02:00
ReturnValue_t SdCardManager::isSdCardMountedReadOnly(sd::SdCard sdcard, bool& readOnly) {
std::ostringstream command;
2022-05-24 00:20:23 +02:00
if (sdcard == sd::SdCard::SLOT_0) {
2023-02-28 01:40:27 +01:00
command << "grep -q '" << config::SD_0_MOUNT_POINT << " ext4 rw,' /proc/mounts";
2022-09-27 10:51:07 +02:00
} else if (sdcard == sd::SdCard::SLOT_1) {
2023-02-28 01:40:27 +01:00
command << "grep -q '" << config::SD_1_MOUNT_POINT << " ext4 rw,' /proc/mounts";
2022-05-11 17:02:57 +02:00
} else {
2022-09-27 10:51:07 +02:00
return returnvalue::FAILED;
2022-05-11 17:02:57 +02:00
}
ReturnValue_t result = cmdExecutor.load(command.str(), true, false);
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
2022-05-11 17:02:57 +02:00
return result;
}
result = cmdExecutor.execute();
2023-02-28 01:40:27 +01:00
if (result == returnvalue::OK) {
2022-05-11 17:02:57 +02:00
readOnly = false;
2023-02-28 01:40:27 +01:00
return result;
2022-05-11 17:02:57 +02:00
}
readOnly = true;
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2022-05-11 17:02:57 +02:00
}
ReturnValue_t SdCardManager::remountReadWrite(sd::SdCard sdcard) {
std::ostringstream command;
2022-05-24 00:20:23 +02:00
if (sdcard == sd::SdCard::SLOT_0) {
2022-09-26 11:39:17 +02:00
command << "mount -o remount,rw " << SD_0_DEV_NAME << " " << config::SD_0_MOUNT_POINT;
2022-05-11 17:02:57 +02:00
} else {
2022-09-26 11:39:17 +02:00
command << "mount -o remount,rw " << SD_1_DEV_NAME << " " << config::SD_1_MOUNT_POINT;
2022-05-11 17:02:57 +02:00
}
ReturnValue_t result = cmdExecutor.load(command.str(), true, false);
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
2022-05-11 17:02:57 +02:00
return result;
}
return cmdExecutor.execute();
}
ReturnValue_t SdCardManager::performFsck(sd::SdCard sdcard, bool printOutput, int& linuxError) {
std::ostringstream command;
2022-05-24 00:20:23 +02:00
if (sdcard == sd::SdCard::SLOT_0) {
2022-05-11 17:02:57 +02:00
command << "fsck -y " << SD_0_DEV_NAME;
} else {
command << "fsck -y " << SD_1_DEV_NAME;
}
ReturnValue_t result = cmdExecutor.load(command.str(), true, printOutput);
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
2022-05-11 17:02:57 +02:00
return result;
}
result = cmdExecutor.execute();
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
2022-05-11 17:02:57 +02:00
linuxError = cmdExecutor.getLastError();
}
return result;
}
2022-05-24 00:20:23 +02:00
2022-09-12 17:00:49 +02:00
void SdCardManager::setActiveSdCard(sd::SdCard sdCard) {
MutexGuard mg(defaultLock, LOCK_TYPE, OTHER_TIMEOUT, LOCK_CTX);
2022-09-12 17:25:20 +02:00
sdInfo.active = sdCard;
2022-09-12 17:00:49 +02:00
if (sdInfo.active == sd::SdCard::SLOT_0) {
2022-09-29 13:53:42 +02:00
currentPrefix = config::SD_0_MOUNT_POINT;
2022-09-12 17:00:49 +02:00
} else {
2022-09-29 13:53:42 +02:00
currentPrefix = config::SD_1_MOUNT_POINT;
2022-09-12 17:00:49 +02:00
}
}
2022-05-24 00:20:23 +02:00
2022-09-12 17:23:43 +02:00
std::optional<sd::SdCard> SdCardManager::getActiveSdCard() const {
MutexGuard mg(defaultLock, LOCK_TYPE, OTHER_TIMEOUT, LOCK_CTX);
2022-10-25 11:31:06 +02:00
if (markedUnusable) {
2022-10-25 11:22:31 +02:00
return std::nullopt;
}
2022-09-12 17:23:43 +02:00
return sdInfo.active;
}
2022-10-25 11:04:24 +02:00
void SdCardManager::markUnusable() {
MutexGuard mg(defaultLock, LOCK_TYPE, OTHER_TIMEOUT, LOCK_CTX);
2022-10-25 11:04:24 +02:00
markedUnusable = true;
}
void SdCardManager::markUsable() {
MutexGuard mg(defaultLock, LOCK_TYPE, OTHER_TIMEOUT, LOCK_CTX);
markedUnusable = false;
}