eive-obsw/bsp_q7s/core/CoreController.cpp

149 lines
5.3 KiB
C++
Raw Normal View History

2021-05-26 14:16:16 +02:00
#include "CoreController.h"
#include "q7sConfig.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):
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() {
return sdCardInit();
}
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();
// Create update status file
ReturnValue_t result = sdcMan->updateSdCardStateFile();
if(result != HasReturnvaluesIF::RETURN_OK) {
sif::warning << "CoreController::initialize: Updating SD card state file failed"
<< std::endl;
}
auto sdStatus = std::pair<sd::SdStatus, sd::SdStatus>(sd::SdStatus::OFF, sd::SdStatus::OFF);
result = sdcMan->getSdCardActiveStatus(sdStatus);
if(result != HasReturnvaluesIF::RETURN_OK) {
sif::warning << "Getting SD card activity status failed" << std::endl;
}
2021-07-12 15:52:13 +02:00
// Use a lambda to avoid duplicate code
auto setUpSdCard = [&](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, &sdStatus);
}
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;
}
};
#if Q7S_SD_CARD_CONFIG == Q7S_SD_COLD_REDUNDANT
2021-07-08 12:07:39 +02:00
sd::SdCard preferredSdCard = sd::SdCard::SLOT_0;
result = sdcMan->getPreferredSdCard(preferredSdCard);
if(result != HasReturnvaluesIF::RETURN_OK) {
sif::warning << "Could not get prefered SD card information from the scratch buffer"
<< std::endl;
}
2021-07-12 15:52:13 +02:00
std::string preferredString;
sd::SdStatus preferredStatus = sd::SdStatus::OFF;
sd::SdStatus otherStatus = sd::SdStatus::OFF;
std::string otherString;
2021-07-12 15:52:13 +02:00
sd::SdCard otherSdc = sd::SdCard::SLOT_0;
sif::info << "Cold redundant SD card configuration, preferred SD card " <<
preferredString << std::endl;
2021-07-09 17:54:32 +02:00
if(preferredSdCard == sd::SdCard::SLOT_0) {
2021-07-12 15:52:13 +02:00
preferredStatus = sdStatus.first;
preferredString = "0";
otherSdc = sd::SdCard::SLOT_1;
2021-07-12 15:52:13 +02:00
otherStatus = sdStatus.second;
otherString = "1";
2021-07-09 17:54:32 +02:00
}
else {
2021-07-12 15:52:13 +02:00
preferredString = "1";
preferredStatus = sdStatus.second;
otherStatus = sdStatus.first;
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:52:13 +02:00
result = setUpSdCard(preferredSdCard, preferredStatus, preferredString);
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
setUpSdCard(otherSdc, otherStatus, otherString);
result = HasReturnvaluesIF::RETURN_FAILED;
}
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
result = sdcMan->switchOffSdCard(otherSdc, otherStatus, &sdStatus);
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-08 12:07:39 +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;
#elif Q7S_SD_CARD_CONFIG == Q7S_SD_HOT_REDUNDANT
sif::info << "Hot redundant SD card configuration" << std::endl;
2021-07-08 12:07:39 +02:00
setUpSdCard(sd::SdCard::SLOT_0, sdStatus.first, "0");
setUpSdCard(sd::SdCard::SLOT_1, sdStatus.second, "1");
2021-07-12 15:19:06 +02:00
// Update status file
sdcMan->updateSdCardStateFile();
2021-05-26 14:16:16 +02:00
return HasReturnvaluesIF::RETURN_OK;
#endif
#endif /* Q7S_SD_CARD_CONFIG != Q7S_SD_NONE */
2021-05-26 14:16:16 +02:00
}