2021-05-26 14:16:16 +02:00
|
|
|
#include "CoreController.h"
|
2021-07-12 14:32:20 +02:00
|
|
|
#include "q7sConfig.h"
|
|
|
|
|
2021-07-19 12:44:43 +02:00
|
|
|
#include "fsfw/serviceinterface/ServiceInterface.h"
|
|
|
|
|
2021-07-16 20:32:13 +02:00
|
|
|
#include "../memory/scratchApi.h"
|
2021-07-08 12:07:39 +02:00
|
|
|
#include "../memory/SdCardManager.h"
|
2021-05-26 14:16:16 +02:00
|
|
|
|
|
|
|
CoreController::CoreController(object_id_t objectId):
|
2021-07-12 15:58:43 +02:00
|
|
|
ExtendedControllerBase(objectId, objects::NO_OBJECT, 5) {
|
2021-05-26 14:16:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t CoreController::handleCommandMessage(CommandMessage *message) {
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CoreController::performControlOperation() {
|
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t CoreController::initializeLocalDataPool(localpool::DataPool &localDataPoolMap,
|
|
|
|
LocalDataPoolManager &poolManager) {
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
LocalPoolDataSetBase* CoreController::getDataSetHandle(sid_t sid) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-07-08 12:07:39 +02:00
|
|
|
ReturnValue_t CoreController::initialize() {
|
2021-07-19 12:44:43 +02:00
|
|
|
ReturnValue_t result = sdCardInit();
|
|
|
|
if(result != HasReturnvaluesIF::RETURN_OK) {
|
|
|
|
sif::warning << "CoreController::initialize: SD card init failed" << std::endl;
|
|
|
|
}
|
|
|
|
result = scratch::writeNumber(scratch::ALLOC_FAILURE_COUNT, 0);
|
|
|
|
if(result != HasReturnvaluesIF::RETURN_OK) {
|
|
|
|
sif::warning << "CoreController::initialize: Setting up alloc failure "
|
|
|
|
"count failed" << std::endl;
|
|
|
|
}
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
2021-07-12 14:32:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t CoreController::checkModeCommand(Mode_t mode, Submode_t submode,
|
|
|
|
uint32_t *msToReachTheMode) {
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t CoreController::sdCardInit() {
|
|
|
|
#if Q7S_SD_CARD_CONFIG == Q7S_SD_NONE
|
|
|
|
sif::info << "No SD card initialization will be performed" << std::endl;
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
#else
|
2021-07-12 15:19:06 +02:00
|
|
|
SdCardManager* sdcMan = SdCardManager::instance();
|
2021-07-16 20:32:13 +02:00
|
|
|
if(sdcMan == nullptr) {
|
|
|
|
return HasReturnvaluesIF::RETURN_FAILED;
|
|
|
|
}
|
2021-07-12 15:19:06 +02:00
|
|
|
// Create update status file
|
|
|
|
ReturnValue_t result = sdcMan->updateSdCardStateFile();
|
2021-07-12 14:32:20 +02:00
|
|
|
if(result != HasReturnvaluesIF::RETURN_OK) {
|
|
|
|
sif::warning << "CoreController::initialize: Updating SD card state file failed"
|
|
|
|
<< std::endl;
|
|
|
|
}
|
|
|
|
|
2021-07-16 20:32:13 +02:00
|
|
|
auto statusPair = SdCardManager::SdStatusPair(sd::SdStatus::OFF, sd::SdStatus::OFF);
|
|
|
|
result = sdcMan->getSdCardActiveStatus(statusPair);
|
2021-07-12 14:32:20 +02:00
|
|
|
if(result != HasReturnvaluesIF::RETURN_OK) {
|
|
|
|
sif::warning << "Getting SD card activity status failed" << std::endl;
|
|
|
|
}
|
|
|
|
|
2021-07-16 20:32:13 +02:00
|
|
|
#if Q7S_SD_CARD_CONFIG == Q7S_SD_COLD_REDUNDANT
|
|
|
|
return sdCardColdRedundantInit(sdcMan, statusPair);
|
|
|
|
#elif Q7S_SD_CARD_CONFIG == Q7S_SD_HOT_REDUNDANT
|
|
|
|
sif::info << "Hot redundant SD card configuration" << std::endl;
|
2021-07-12 15:52:13 +02:00
|
|
|
|
2021-07-16 20:32:13 +02:00
|
|
|
setUpSdCard(sd::SdCard::SLOT_0, sdStatus.first, "0");
|
|
|
|
setUpSdCard(sd::SdCard::SLOT_1, sdStatus.second, "1");
|
|
|
|
// Update status file
|
|
|
|
sdcMan->updateSdCardStateFile();
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
#endif
|
2021-07-12 15:52:13 +02:00
|
|
|
|
2021-07-16 20:32:13 +02:00
|
|
|
#endif /* Q7S_SD_CARD_CONFIG != Q7S_SD_NONE */
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t CoreController::sdCardSetup(SdCardManager& sdcMan,
|
|
|
|
SdCardManager::SdStatusPair& statusPair,sd::SdCard sdCard, sd::SdStatus status,
|
|
|
|
std::string sdString) {
|
|
|
|
std::string mountString;
|
|
|
|
if(sdCard == sd::SdCard::SLOT_0) {
|
|
|
|
mountString = SdCardManager::SD_0_MOUNT_POINT;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mountString = SdCardManager::SD_1_MOUNT_POINT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(status == sd::SdStatus::OFF) {
|
|
|
|
sif::info << "Switching on and mounting SD card " << sdString << " at " <<
|
|
|
|
mountString << std::endl;
|
|
|
|
return sdcMan.switchOnSdCard(sdCard, true, &statusPair);
|
|
|
|
}
|
|
|
|
else if(status == sd::SdStatus::ON) {
|
|
|
|
sif::info << "Mounting SD card " << sdString << " at " << mountString << std::endl;
|
|
|
|
return sdcMan.mountSdCard(sdCard);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sif::info << "SD card " << sdString << " already on and mounted at " <<
|
|
|
|
mountString << std::endl;
|
|
|
|
return SdCardManager::ALREADY_MOUNTED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-19 20:06:28 +02:00
|
|
|
ReturnValue_t CoreController::executeAction(ActionId_t actionId, MessageQueueId_t commandedBy,
|
|
|
|
const uint8_t *data, size_t size) {
|
|
|
|
switch(actionId) {
|
|
|
|
case(LIST_DIRECTORY_INTO_FILE): {
|
|
|
|
// TODO: Packet definition for clean deserialization
|
2021-07-19 20:12:21 +02:00
|
|
|
// 2 bytes for a and R flag, at least 5 bytes for minimum valid path /tmp with
|
|
|
|
// null termination, at least 7 bytes for minimum target file name /tmp/a with
|
|
|
|
// null termination.
|
|
|
|
if(size < 14) {
|
2021-07-19 20:06:28 +02:00
|
|
|
return HasActionsIF::INVALID_PARAMETERS;
|
|
|
|
}
|
2021-07-19 20:12:21 +02:00
|
|
|
// We could also make -l optional, but I can't think of a reason why to not use -l..
|
|
|
|
|
2021-07-19 20:06:28 +02:00
|
|
|
// This flag specifies to run ls with -a
|
|
|
|
bool aFlag = data[0];
|
|
|
|
data += 1;
|
|
|
|
// This flag specifies to run ls with -R
|
|
|
|
bool RFlag = data[1];
|
|
|
|
data += 1;
|
|
|
|
|
|
|
|
size_t remainingSize = size - 2;
|
|
|
|
// One larger for null termination, which prevents undefined behaviour if the sent
|
2021-07-19 20:12:21 +02:00
|
|
|
// strings are not 0 terminated properly
|
2021-07-19 20:06:28 +02:00
|
|
|
std::vector<uint8_t> repoAndTargetFileBuffer(remainingSize + 1, 0);
|
|
|
|
std::memcpy(repoAndTargetFileBuffer.data(), data, remainingSize);
|
|
|
|
const char* currentCharPtr = reinterpret_cast<const char*>(repoAndTargetFileBuffer.data());
|
|
|
|
// Full target file name
|
|
|
|
std::string repoName(currentCharPtr);
|
|
|
|
size_t repoLength = repoName.length();
|
|
|
|
// The other string needs to be at least one letter plus NULL termination to be valid at all
|
|
|
|
// The first string also needs to be NULL terminated, but the termination is not included
|
|
|
|
// in the string length, so this is subtracted from the remaining size as well
|
|
|
|
if(repoLength > remainingSize - 3) {
|
|
|
|
return HasActionsIF::INVALID_PARAMETERS;
|
|
|
|
}
|
|
|
|
// The file length will not include the NULL termination, so we skip it
|
|
|
|
currentCharPtr += repoLength + 1;
|
|
|
|
std::string targetFileName(currentCharPtr);
|
|
|
|
std::ostringstream oss;
|
|
|
|
oss << "ls -l";
|
|
|
|
if(aFlag) {
|
|
|
|
oss << "a";
|
|
|
|
}
|
|
|
|
if(RFlag) {
|
|
|
|
oss << "R";
|
|
|
|
}
|
|
|
|
oss << " " << repoName << " > " << targetFileName;
|
|
|
|
std::system(oss.str().c_str());
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
return HasActionsIF::INVALID_ACTION_ID;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-16 20:32:13 +02:00
|
|
|
ReturnValue_t CoreController::sdCardColdRedundantInit(SdCardManager* sdcMan,
|
|
|
|
SdCardManager::SdStatusPair& statusPair) {
|
2021-07-08 12:07:39 +02:00
|
|
|
sd::SdCard preferredSdCard = sd::SdCard::SLOT_0;
|
2021-07-16 20:32:13 +02:00
|
|
|
ReturnValue_t result = sdcMan->getPreferredSdCard(preferredSdCard);
|
2021-07-12 14:32:20 +02:00
|
|
|
if(result != HasReturnvaluesIF::RETURN_OK) {
|
2021-07-16 20:32:13 +02:00
|
|
|
if(result == scratch::KEY_NOT_FOUND) {
|
|
|
|
sif::warning << "CoreController::sdCardInit: "
|
|
|
|
"Preferred SD card not set. Setting to 0" << std::endl;
|
|
|
|
sdcMan->setPreferredSdCard(preferredSdCard);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sif::warning << "CoreController::sdCardInit: Could not get preferred SD card"
|
|
|
|
"information from the scratch buffer" << std::endl;
|
|
|
|
}
|
2021-07-12 14:32:20 +02:00
|
|
|
}
|
2021-07-12 15:52:13 +02:00
|
|
|
std::string preferredString;
|
|
|
|
sd::SdStatus preferredStatus = sd::SdStatus::OFF;
|
|
|
|
|
|
|
|
sd::SdStatus otherStatus = sd::SdStatus::OFF;
|
2021-07-12 14:32:20 +02:00
|
|
|
std::string otherString;
|
2021-07-12 15:52:13 +02:00
|
|
|
sd::SdCard otherSdc = sd::SdCard::SLOT_0;
|
|
|
|
|
2021-07-09 17:54:32 +02:00
|
|
|
if(preferredSdCard == sd::SdCard::SLOT_0) {
|
2021-07-16 20:32:13 +02:00
|
|
|
preferredStatus = statusPair.first;
|
2021-07-12 15:52:13 +02:00
|
|
|
preferredString = "0";
|
2021-07-12 14:32:20 +02:00
|
|
|
otherSdc = sd::SdCard::SLOT_1;
|
2021-07-16 20:32:13 +02:00
|
|
|
otherStatus = statusPair.second;
|
2021-07-12 14:32:20 +02:00
|
|
|
otherString = "1";
|
2021-07-09 17:54:32 +02:00
|
|
|
}
|
|
|
|
else {
|
2021-07-12 15:52:13 +02:00
|
|
|
preferredString = "1";
|
2021-07-16 20:32:13 +02:00
|
|
|
preferredStatus = statusPair.second;
|
|
|
|
otherStatus = statusPair.first;
|
2021-07-12 14:32:20 +02:00
|
|
|
otherSdc = sd::SdCard::SLOT_0;
|
|
|
|
otherString = "0";
|
2021-07-08 12:07:39 +02:00
|
|
|
}
|
2021-07-09 17:54:32 +02:00
|
|
|
|
2021-07-12 15:58:43 +02:00
|
|
|
sif::info << "Cold redundant SD card configuration, preferred SD card " <<
|
|
|
|
preferredString << std::endl;
|
|
|
|
|
2021-07-16 20:32:13 +02:00
|
|
|
result = sdCardSetup(*sdcMan, statusPair, preferredSdCard, preferredStatus, preferredString);
|
2021-07-12 15:52:13 +02:00
|
|
|
if(result != SdCardManager::ALREADY_MOUNTED and result != HasReturnvaluesIF::RETURN_OK) {
|
|
|
|
sif::warning << "Setting up preferred card " << otherString <<
|
|
|
|
" in cold redundant mode failed" << std::endl;
|
|
|
|
// Try other SD card and mark set up operation as failed
|
2021-07-16 20:32:13 +02:00
|
|
|
sdCardSetup(*sdcMan, statusPair, preferredSdCard, preferredStatus, preferredString);
|
2021-07-12 15:52:13 +02:00
|
|
|
result = HasReturnvaluesIF::RETURN_FAILED;
|
2021-07-12 14:32:20 +02:00
|
|
|
}
|
2021-07-09 17:54:32 +02:00
|
|
|
|
2021-07-12 15:52:13 +02:00
|
|
|
if(result != HasReturnvaluesIF::RETURN_FAILED and otherStatus != sd::SdStatus::OFF) {
|
|
|
|
sif::info << "Switching off secondary SD card " << otherString << std::endl;
|
|
|
|
// Switch off other SD card in cold redundant mode if setting up preferred one walked
|
|
|
|
// without issues
|
2021-07-16 20:32:13 +02:00
|
|
|
result = sdcMan->switchOffSdCard(otherSdc, otherStatus, &statusPair);
|
2021-07-12 15:52:13 +02:00
|
|
|
if(result != HasReturnvaluesIF::RETURN_OK and result != SdCardManager::ALREADY_OFF) {
|
|
|
|
sif::warning << "Switching off secondary SD card " << otherString <<
|
|
|
|
" in cold redundant mode failed" << std::endl;
|
2021-07-12 14:32:20 +02:00
|
|
|
}
|
2021-07-08 12:07:39 +02:00
|
|
|
}
|
2021-07-12 14:32:20 +02:00
|
|
|
|
2021-07-12 15:19:06 +02:00
|
|
|
// Update status file
|
|
|
|
sdcMan->updateSdCardStateFile();
|
2021-07-08 12:07:39 +02:00
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
2021-05-26 14:16:16 +02:00
|
|
|
}
|
2021-07-19 12:44:43 +02:00
|
|
|
|
|
|
|
ReturnValue_t CoreController::incrementAllocationFailureCount() {
|
|
|
|
uint32_t count = 0;
|
|
|
|
ReturnValue_t result = scratch::readNumber(scratch::ALLOC_FAILURE_COUNT, count);
|
|
|
|
if(result != HasReturnvaluesIF::RETURN_OK) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
count++;
|
|
|
|
return scratch::writeNumber(scratch::ALLOC_FAILURE_COUNT, count);
|
|
|
|
}
|