eive-obsw/bsp_q7s/core/CoreController.cpp

1852 lines
62 KiB
C++
Raw Normal View History

2021-05-26 14:16:16 +02:00
#include "CoreController.h"
2022-01-17 15:58:27 +01:00
#include <fsfw/events/EventManager.h>
#include <fsfw/ipc/QueueFactory.h>
2021-07-26 11:47:45 +02:00
#include "OBSWConfig.h"
#include "OBSWVersion.h"
2021-07-19 12:44:43 +02:00
#include "fsfw/serviceinterface/ServiceInterface.h"
2022-01-17 15:58:27 +01:00
#include "fsfw/timemanager/Stopwatch.h"
#include "fsfw/version.h"
2022-04-17 01:00:06 +02:00
#include "watchdog/definitions.h"
2021-07-26 11:47:45 +02:00
#if OBSW_USE_TMTC_TCP_BRIDGE == 0
#include "fsfw/osal/common/UdpTmTcBridge.h"
#else
2021-08-09 17:19:10 +02:00
#include "fsfw/osal/common/TcpTmTcServer.h"
2021-07-26 11:47:45 +02:00
#endif
2021-07-29 16:31:04 +02:00
#include <fcntl.h>
#include <unistd.h>
2022-04-08 11:40:35 +02:00
2022-04-08 11:31:29 +02:00
#include <algorithm>
2021-07-23 17:06:10 +02:00
#include <filesystem>
2022-01-17 15:58:27 +01:00
#include "bsp_q7s/memory/SdCardManager.h"
#include "bsp_q7s/memory/scratchApi.h"
2022-03-13 16:32:40 +01:00
#include "bsp_q7s/xadc/Xadc.h"
#include "linux/utility/utility.h"
2022-01-17 15:58:27 +01:00
xsc::Chip CoreController::CURRENT_CHIP = xsc::Chip::NO_CHIP;
xsc::Copy CoreController::CURRENT_COPY = xsc::Copy::NO_COPY;
2022-01-17 15:58:27 +01:00
CoreController::CoreController(object_id_t objectId)
2022-04-17 01:04:53 +02:00
: ExtendedControllerBase(objectId, objects::NO_OBJECT, 5),
opDivider5(5),
opDivider10(10),
hkSet(this) {
2022-01-17 15:58:27 +01:00
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
try {
result = initWatchdogFifo();
if (result != HasReturnvaluesIF::RETURN_OK) {
sif::warning << "CoreController::CoreController: Watchdog FIFO init failed" << std::endl;
}
sdcMan = SdCardManager::instance();
if (sdcMan == nullptr) {
sif::error << "CoreController::CoreController: SD card manager invalid!" << std::endl;
}
2021-08-05 10:14:17 +02:00
2022-01-17 15:58:27 +01:00
if (not BLOCKING_SD_INIT) {
sdcMan->setBlocking(false);
}
2022-01-17 15:58:27 +01:00
sdStateMachine();
result = initBootCopy();
if (result != HasReturnvaluesIF::RETURN_OK) {
sif::warning << "CoreController::CoreController: Boot copy init" << std::endl;
}
2022-01-17 15:58:27 +01:00
} catch (const std::filesystem::filesystem_error &e) {
sif::error << "CoreController::CoreController: Failed with exception " << e.what() << std::endl;
}
2022-05-24 00:20:23 +02:00
sdCardCheckCd.timeOut();
eventQueue = QueueFactory::instance()->createMessageQueue(5, EventMessage::MAX_MESSAGE_SIZE);
2021-05-26 14:16:16 +02:00
}
ReturnValue_t CoreController::handleCommandMessage(CommandMessage *message) {
2022-01-17 15:58:27 +01:00
return ExtendedControllerBase::handleCommandMessage(message);
2021-05-26 14:16:16 +02:00
}
void CoreController::performControlOperation() {
2022-04-07 17:23:50 +02:00
EventMessage event;
for (ReturnValue_t result = eventQueue->receiveMessage(&event); result == RETURN_OK;
result = eventQueue->receiveMessage(&event)) {
switch (event.getEvent()) {
case (GpsHyperion::GPS_FIX_CHANGE): {
gpsFix = static_cast<GpsHyperion::FixMode>(event.getParameter2());
break;
}
2022-04-07 17:23:50 +02:00
}
}
2022-01-17 15:58:27 +01:00
performWatchdogControlOperation();
sdStateMachine();
performMountedSdCardOperations();
2022-05-24 00:20:23 +02:00
if (sdCardCheckCd.hasTimedOut()) {
performSdCardCheck();
sdCardCheckCd.resetTimer();
}
2022-03-13 16:32:40 +01:00
readHkData();
opDivider5.checkAndIncrement();
opDivider10.checkAndIncrement();
2021-05-26 14:16:16 +02:00
}
ReturnValue_t CoreController::initializeLocalDataPool(localpool::DataPool &localDataPoolMap,
2022-01-17 15:58:27 +01:00
LocalDataPoolManager &poolManager) {
2022-03-13 16:32:40 +01:00
localDataPoolMap.emplace(core::TEMPERATURE, new PoolEntry<float>({0}));
localDataPoolMap.emplace(core::PS_VOLTAGE, new PoolEntry<float>({0}));
localDataPoolMap.emplace(core::PL_VOLTAGE, new PoolEntry<float>({0}));
poolManager.subscribeForPeriodicPacket(hkSet.getSid(), false, 10.0, false);
2022-01-17 15:58:27 +01:00
return HasReturnvaluesIF::RETURN_OK;
2021-05-26 14:16:16 +02:00
}
2022-03-13 16:32:40 +01:00
LocalPoolDataSetBase *CoreController::getDataSetHandle(sid_t sid) {
if (sid.ownerSetId == core::HK_SET_ID) {
return &hkSet;
}
return nullptr;
2022-03-13 16:32:40 +01:00
}
2021-05-26 14:16:16 +02:00
2021-07-08 12:07:39 +02:00
ReturnValue_t CoreController::initialize() {
ReturnValue_t result = ExtendedControllerBase::initialize();
if (result != HasReturnvaluesIF::RETURN_OK) {
sif::warning << "CoreController::initialize: Base init failed" << std::endl;
}
2022-01-17 15:58:27 +01:00
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;
}
2022-01-17 15:58:27 +01:00
sdStateMachine();
triggerEvent(REBOOT_SW, CURRENT_CHIP, CURRENT_COPY);
EventManagerIF *eventManager =
ObjectManager::instance()->get<EventManagerIF>(objects::EVENT_MANAGER);
if (eventManager == nullptr or eventQueue == nullptr) {
sif::warning << "CoreController::initialize: No valid event manager found or "
"queue invalid"
<< std::endl;
}
2022-04-07 17:23:50 +02:00
result = eventManager->registerListener(eventQueue->getId());
if (result != HasReturnvaluesIF::RETURN_OK) {
2022-04-07 17:23:50 +02:00
sif::warning << "CoreController::initialize: Registering as event listener failed" << std::endl;
}
result = eventManager->subscribeToEvent(eventQueue->getId(),
event::getEventId(GpsHyperion::GPS_FIX_CHANGE));
if (result != HasReturnvaluesIF::RETURN_OK) {
2022-04-07 17:23:50 +02:00
sif::warning << "Subscribing for GPS GPS_FIX_CHANGE event failed" << std::endl;
}
return RETURN_OK;
}
2021-08-19 14:05:25 +02:00
ReturnValue_t CoreController::initializeAfterTaskCreation() {
2022-01-17 15:58:27 +01:00
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
2022-05-24 00:20:23 +02:00
sdInfo.pref = sdcMan->getPreferredSdCard();
sdcMan->setActiveSdCard(sdInfo.pref);
2022-05-24 00:52:08 +02:00
currMntPrefix = sdcMan->getCurrentMountPrefix();
2022-01-17 15:58:27 +01:00
if (BLOCKING_SD_INIT) {
ReturnValue_t result = initSdCardBlocking();
if (result != HasReturnvaluesIF::RETURN_OK and result != SdCardManager::ALREADY_MOUNTED) {
sif::warning << "CoreController::CoreController: SD card init failed" << std::endl;
}
}
sdStateMachine();
performMountedSdCardOperations();
2022-01-17 15:58:27 +01:00
if (result != HasReturnvaluesIF::RETURN_OK) {
sif::warning << "CoreController::initialize: Version initialization failed" << std::endl;
}
// Add script folder to path
char *currentEnvPath = getenv("PATH");
2022-04-17 01:04:53 +02:00
std::string updatedEnvPath = std::string(currentEnvPath) + ":/home/root/scripts:/usr/local/bin";
2022-01-17 15:58:27 +01:00
setenv("PATH", updatedEnvPath.c_str(), true);
updateProtInfo();
initPrint();
2022-03-13 18:29:14 +01:00
ExtendedControllerBase::initializeAfterTaskCreation();
2022-01-17 15:58:27 +01:00
return result;
2021-08-19 14:05:25 +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): {
return actionListDirectoryIntoFile(actionId, commandedBy, data, size);
}
case (SWITCH_REBOOT_FILE_HANDLING): {
if (size < 1) {
return HasActionsIF::INVALID_PARAMETERS;
}
2022-05-24 00:20:23 +02:00
std::string path = sdcMan->getCurrentMountPrefix() + REBOOT_FILE;
// Disable the reboot file mechanism
parseRebootFile(path, rebootFile);
if (data[0] == 0) {
rebootFile.enabled = false;
rewriteRebootFile(rebootFile);
} else if (data[0] == 1) {
rebootFile.enabled = true;
rewriteRebootFile(rebootFile);
} else {
return HasActionsIF::INVALID_PARAMETERS;
}
return HasActionsIF::EXECUTION_FINISHED;
}
2022-03-01 14:44:50 +01:00
case (RESET_REBOOT_COUNTERS): {
if (size == 0) {
resetRebootCount(xsc::ALL_CHIP, xsc::ALL_COPY);
} else if (size == 2) {
if (data[0] > 1 or data[1] > 1) {
return HasActionsIF::INVALID_PARAMETERS;
}
resetRebootCount(static_cast<xsc::Chip>(data[0]), static_cast<xsc::Copy>(data[1]));
}
return HasActionsIF::EXECUTION_FINISHED;
}
2022-03-01 14:44:50 +01:00
case (SWITCH_IMG_LOCK): {
if (size != 3) {
return HasActionsIF::INVALID_PARAMETERS;
}
if (data[1] > 1 or data[2] > 1) {
return HasActionsIF::INVALID_PARAMETERS;
}
setRebootMechanismLock(data[0], static_cast<xsc::Chip>(data[1]),
static_cast<xsc::Copy>(data[2]));
return HasActionsIF::EXECUTION_FINISHED;
}
2022-02-28 19:52:43 +01:00
case (SET_MAX_REBOOT_CNT): {
if (size < 1) {
2022-02-28 16:55:49 +01:00
return HasActionsIF::INVALID_PARAMETERS;
}
2022-05-24 00:20:23 +02:00
std::string path = sdcMan->getCurrentMountPrefix() + REBOOT_FILE;
2022-02-28 16:55:49 +01:00
// Disable the reboot file mechanism
parseRebootFile(path, rebootFile);
rebootFile.maxCount = data[0];
rewriteRebootFile(rebootFile);
return HasActionsIF::EXECUTION_FINISHED;
}
2022-04-29 11:16:53 +02:00
case (XSC_REBOOT_OBC): {
// Warning: This function will never return, because it reboots the system
return actionXscReboot(data, size);
}
case (REBOOT_OBC): {
2022-04-29 11:16:53 +02:00
// Warning: This function will never return, because it reboots the system
return actionReboot(data, size);
}
default: {
return HasActionsIF::INVALID_ACTION_ID;
}
}
}
ReturnValue_t CoreController::checkModeCommand(Mode_t mode, Submode_t submode,
2022-01-17 15:58:27 +01:00
uint32_t *msToReachTheMode) {
return HasReturnvaluesIF::RETURN_OK;
}
ReturnValue_t CoreController::initSdCardBlocking() {
2022-01-17 15:58:27 +01:00
// 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;
}
2021-08-08 12:59:21 +02:00
#if Q7S_SD_CARD_CONFIG == Q7S_SD_NONE
2022-01-17 15:58:27 +01:00
sif::info << "No SD card initialization will be performed" << std::endl;
return HasReturnvaluesIF::RETURN_OK;
2021-08-08 12:59:21 +02:00
#else
2022-05-24 00:20:23 +02:00
result = sdcMan->getSdCardsStatus(sdInfo.currentState);
2022-01-17 15:58:27 +01:00
if (result != HasReturnvaluesIF::RETURN_OK) {
sif::warning << "Getting SD card activity status failed" << std::endl;
}
#if Q7S_SD_CARD_CONFIG == Q7S_SD_COLD_REDUNDANT
2022-01-17 15:58:27 +01:00
updateSdInfoOther();
sif::info << "Cold redundant SD card configuration, preferred SD card: "
<< static_cast<int>(sdInfo.pref) << std::endl;
result = sdColdRedundantBlockingInit();
// Update status file
sdcMan->updateSdCardStateFile();
return result;
#elif Q7S_SD_CARD_CONFIG == Q7S_SD_HOT_REDUNDANT
2022-01-17 15:58:27 +01:00
sif::info << "Hot redundant SD card configuration" << std::endl;
sdCardSetup(sd::SdCard::SLOT_0, sd::SdState::MOUNTED, "0", false);
sdCardSetup(sd::SdCard::SLOT_1, sd::SdState::MOUNTED, "1", false);
// Update status file
sdcMan->updateSdCardStateFile();
return HasReturnvaluesIF::RETURN_OK;
#endif
2021-07-12 15:52:13 +02:00
#endif /* Q7S_SD_CARD_CONFIG != Q7S_SD_NONE */
}
2021-08-08 00:11:21 +02:00
ReturnValue_t CoreController::sdStateMachine() {
2022-01-17 15:58:27 +01:00
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
SdCardManager::Operations operation;
2021-08-06 01:25:36 +02:00
2022-01-17 15:58:27 +01:00
if (sdInfo.state == SdStates::IDLE) {
// Nothing to do
return result;
}
if (sdInfo.state == SdStates::START) {
// Init will be performed by separate function
if (BLOCKING_SD_INIT) {
sdInfo.state = SdStates::IDLE;
sdInfo.initFinished = true;
return result;
} else {
// Still update SD state file
2021-08-08 12:59:21 +02:00
#if Q7S_SD_CARD_CONFIG == Q7S_SD_NONE
2022-01-17 15:58:27 +01:00
sdInfo.state = SdStates::UPDATE_INFO;
2021-08-08 12:59:21 +02:00
#else
2022-01-17 15:58:27 +01:00
sdInfo.cycleCount = 0;
sdInfo.commandExecuted = false;
sdInfo.state = SdStates::GET_INFO;
2021-08-08 12:43:02 +02:00
#endif
}
2022-01-17 15:58:27 +01:00
}
2022-01-17 15:58:27 +01:00
// This lambda checks the non-blocking operation and assigns the new state on success.
// It returns true for an operation success and false otherwise
auto nonBlockingOpChecking = [&](SdStates newStateOnSuccess, uint16_t maxCycleCount,
std::string opPrintout) {
SdCardManager::OpStatus status = sdcMan->checkCurrentOp(operation);
if (status == SdCardManager::OpStatus::SUCCESS) {
sdInfo.state = newStateOnSuccess;
sdInfo.commandExecuted = false;
sdInfo.cycleCount = 0;
return true;
} else if (sdInfo.cycleCount > 4) {
sif::warning << "CoreController::sdInitStateMachine: " << opPrintout << " takes too long"
<< std::endl;
return false;
}
return false;
};
if (sdInfo.state == SdStates::GET_INFO) {
if (not sdInfo.commandExecuted) {
// Create update status file
result = sdcMan->updateSdCardStateFile();
if (result != HasReturnvaluesIF::RETURN_OK) {
sif::warning << "CoreController::initialize: Updating SD card state file failed"
<< std::endl;
}
sdInfo.commandExecuted = true;
} else {
nonBlockingOpChecking(SdStates::SET_STATE_SELF, 4, "Updating SDC file");
}
}
if (sdInfo.state == SdStates::SET_STATE_SELF) {
if (not sdInfo.commandExecuted) {
2022-05-24 00:20:23 +02:00
result = sdcMan->getSdCardsStatus(sdInfo.currentState);
sdInfo.pref = sdcMan->getPreferredSdCard();
2022-01-17 15:58:27 +01:00
updateSdInfoOther();
if (sdInfo.pref != sd::SdCard::SLOT_0 and sdInfo.pref != sd::SdCard::SLOT_1) {
sif::warning << "Preferred SD card invalid. Setting to card 0.." << std::endl;
sdInfo.pref = sd::SdCard::SLOT_0;
}
if (result != HasReturnvaluesIF::RETURN_OK) {
sif::warning << "Getting SD card activity status failed" << std::endl;
}
2021-08-08 00:11:21 +02:00
#if Q7S_SD_CARD_CONFIG == Q7S_SD_COLD_REDUNDANT
2022-01-17 15:58:27 +01:00
sif::info << "Cold redundant SD card configuration, preferred SD card: "
<< static_cast<int>(sdInfo.pref) << std::endl;
2021-08-08 00:11:21 +02:00
#endif
2022-01-17 15:58:27 +01:00
if (sdInfo.prefState == sd::SdState::MOUNTED) {
2021-08-09 11:00:58 +02:00
#if OBSW_VERBOSE_LEVEL >= 1
2022-01-17 15:58:27 +01:00
std::string mountString;
if (sdInfo.pref == sd::SdCard::SLOT_0) {
mountString = SdCardManager::SD_0_MOUNT_POINT;
} else {
mountString = SdCardManager::SD_1_MOUNT_POINT;
}
sif::info << "SD card " << sdInfo.prefChar << " already on and mounted at " << mountString
<< std::endl;
2021-08-09 11:00:58 +02:00
#endif
2022-01-17 15:58:27 +01:00
sdInfo.state = SdStates::DETERMINE_OTHER;
} else if (sdInfo.prefState == sd::SdState::OFF) {
sdCardSetup(sdInfo.pref, sd::SdState::ON, sdInfo.prefChar, false);
sdInfo.commandExecuted = true;
} else if (sdInfo.prefState == sd::SdState::ON) {
sdInfo.state = SdStates::MOUNT_SELF;
}
} else {
if (nonBlockingOpChecking(SdStates::MOUNT_SELF, 10, "Setting SDC state")) {
sdInfo.prefState = sd::SdState::ON;
currentStateSetter(sdInfo.pref, sd::SdState::ON);
}
}
}
if (sdInfo.state == SdStates::MOUNT_SELF) {
if (not sdInfo.commandExecuted) {
result = sdCardSetup(sdInfo.pref, sd::SdState::MOUNTED, sdInfo.prefChar);
sdInfo.commandExecuted = true;
} else {
if (nonBlockingOpChecking(SdStates::DETERMINE_OTHER, 5, "Mounting SD card")) {
sdInfo.prefState = sd::SdState::MOUNTED;
currentStateSetter(sdInfo.pref, sd::SdState::MOUNTED);
}
}
}
if (sdInfo.state == SdStates::DETERMINE_OTHER) {
// Determine whether any additional operations have to be done for the other SD card
// 1. Cold redundant case: Other SD card needs to be unmounted and switched off
// 2. Hot redundant case: Other SD card needs to be mounted and switched on
2021-08-06 01:25:36 +02:00
#if Q7S_SD_CARD_CONFIG == Q7S_SD_COLD_REDUNDANT
2022-01-17 15:58:27 +01:00
if (sdInfo.otherState == sd::SdState::ON) {
sdInfo.state = SdStates::SET_STATE_OTHER;
} else if (sdInfo.otherState == sd::SdState::MOUNTED) {
sdInfo.state = SdStates::MOUNT_UNMOUNT_OTHER;
} else {
// Is already off, update info, but with a small delay
sdInfo.state = SdStates::SKIP_CYCLE_BEFORE_INFO_UPDATE;
2021-08-06 01:25:36 +02:00
}
#elif Q7S_SD_CARD_CONFIG == Q7S_SD_HOT_REDUNDANT
2022-01-17 15:58:27 +01:00
if (sdInfo.otherState == sd::SdState::OFF) {
sdInfo.state = SdStates::SET_STATE_OTHER;
} else if (sdInfo.otherState == sd::SdState::ON) {
sdInfo.state = SdStates::MOUNT_UNMOUNT_OTHER;
} else {
// Is already on and mounted, update info
sdInfo.state = SdStates::SKIP_CYCLE_BEFORE_INFO_UPDATE;
2021-08-06 01:25:36 +02:00
}
2022-01-17 15:58:27 +01:00
#endif
}
2021-08-06 01:25:36 +02:00
2022-01-17 15:58:27 +01:00
if (sdInfo.state == SdStates::SET_STATE_OTHER) {
// Set state of other SD card to ON or OFF, depending on redundancy mode
2021-08-06 01:25:36 +02:00
#if Q7S_SD_CARD_CONFIG == Q7S_SD_COLD_REDUNDANT
2022-01-17 15:58:27 +01:00
if (not sdInfo.commandExecuted) {
result = sdCardSetup(sdInfo.other, sd::SdState::OFF, sdInfo.otherChar, false);
sdInfo.commandExecuted = true;
} else {
if (nonBlockingOpChecking(SdStates::SKIP_CYCLE_BEFORE_INFO_UPDATE, 10,
"Switching off other SD card")) {
sdInfo.otherState = sd::SdState::OFF;
currentStateSetter(sdInfo.other, sd::SdState::OFF);
}
}
2021-08-06 01:25:36 +02:00
#elif Q7S_SD_CARD_CONFIG == Q7S_SD_HOT_REDUNDANT
2022-01-17 15:58:27 +01:00
if (not sdInfo.commandExecuted) {
result = sdCardSetup(sdInfo.other, sd::SdState::ON, sdInfo.otherChar, false);
sdInfo.commandExecuted = true;
} else {
if (nonBlockingOpChecking(SdStates::MOUNT_UNMOUNT_OTHER, 10, "Switching on other SD card")) {
sdInfo.otherState = sd::SdState::ON;
currentStateSetter(sdInfo.other, sd::SdState::ON);
}
}
2022-01-17 15:58:27 +01:00
#endif
}
2022-01-17 15:58:27 +01:00
if (sdInfo.state == SdStates::MOUNT_UNMOUNT_OTHER) {
// Mount or unmount other SD card, depending on redundancy mode
#if Q7S_SD_CARD_CONFIG == Q7S_SD_COLD_REDUNDANT
if (not sdInfo.commandExecuted) {
result = sdCardSetup(sdInfo.other, sd::SdState::ON, sdInfo.otherChar);
sdInfo.commandExecuted = true;
} else {
if (nonBlockingOpChecking(SdStates::SET_STATE_OTHER, 10, "Unmounting other SD card")) {
sdInfo.otherState = sd::SdState::ON;
currentStateSetter(sdInfo.other, sd::SdState::ON);
}
}
2022-01-17 15:58:27 +01:00
#elif Q7S_SD_CARD_CONFIG == Q7S_SD_HOT_REDUNDANT
if (not sdInfo.commandExecuted) {
result = sdCardSetup(sdInfo.other, sd::SdState::MOUNTED, sdInfo.otherChar);
sdInfo.commandExecuted = true;
} else {
if (nonBlockingOpChecking(SdStates::UPDATE_INFO, 4, "Mounting other SD card")) {
sdInfo.otherState = sd::SdState::MOUNTED;
currentStateSetter(sdInfo.other, sd::SdState::MOUNTED);
}
}
2022-01-17 15:58:27 +01:00
#endif
}
if (sdInfo.state == SdStates::SKIP_CYCLE_BEFORE_INFO_UPDATE) {
sdInfo.state = SdStates::UPDATE_INFO;
} else if (sdInfo.state == SdStates::UPDATE_INFO) {
// It is assumed that all tasks are running by the point this section is reached.
// Therefore, perform this operation in blocking mode because it does not take long
// and the ready state of the SD card is available sooner
sdcMan->setBlocking(true);
// Update status file
result = sdcMan->updateSdCardStateFile();
if (result != HasReturnvaluesIF::RETURN_OK) {
sif::warning << "CoreController::initialize: Updating SD card state file failed" << std::endl;
}
sdInfo.commandExecuted = false;
sdInfo.state = SdStates::IDLE;
sdInfo.cycleCount = 0;
sdcMan->setBlocking(false);
2022-05-24 00:20:23 +02:00
sdcMan->getSdCardsStatus(sdInfo.currentState);
2022-01-17 15:58:27 +01:00
if (not sdInfo.initFinished) {
updateSdInfoOther();
sdInfo.initFinished = true;
sif::info << "SD card initialization finished" << std::endl;
}
}
if (sdInfo.state == SdStates::SET_STATE_FROM_COMMAND) {
if (not sdInfo.commandExecuted) {
executeNextExternalSdCommand();
} else {
checkExternalSdCommandStatus();
}
}
sdInfo.cycleCount++;
return HasReturnvaluesIF::RETURN_OK;
}
void CoreController::executeNextExternalSdCommand() {
2022-01-17 15:58:27 +01:00
std::string sdChar;
sd::SdState currentStateOfCard = sd::SdState::OFF;
if (sdInfo.commandedCard == sd::SdCard::SLOT_0) {
sdChar = "0";
currentStateOfCard = sdInfo.currentState.first;
} else {
sdChar = "1";
currentStateOfCard = sdInfo.currentState.second;
}
if (currentStateOfCard == sd::SdState::OFF) {
if (sdInfo.commandedState == sd::SdState::ON) {
sdInfo.currentlyCommandedState = sdInfo.commandedState;
} else if (sdInfo.commandedState == sd::SdState::MOUNTED) {
sdInfo.currentlyCommandedState = sd::SdState::ON;
} else {
// SD card is already on target state
sdInfo.commandFinished = true;
sdInfo.state = SdStates::IDLE;
}
} else if (currentStateOfCard == sd::SdState::ON) {
if (sdInfo.commandedState == sd::SdState::OFF or
sdInfo.commandedState == sd::SdState::MOUNTED) {
sdInfo.currentlyCommandedState = sdInfo.commandedState;
} else {
// Already on target state
sdInfo.commandFinished = true;
sdInfo.state = SdStates::IDLE;
}
} else if (currentStateOfCard == sd::SdState::MOUNTED) {
if (sdInfo.commandedState == sd::SdState::ON) {
sdInfo.currentlyCommandedState = sdInfo.commandedState;
} else if (sdInfo.commandedState == sd::SdState::OFF) {
// This causes an unmount in sdCardSetup
sdInfo.currentlyCommandedState = sd::SdState::ON;
} else {
sdInfo.commandFinished = true;
}
}
sdCardSetup(sdInfo.commandedCard, sdInfo.commandedState, sdChar);
sdInfo.commandExecuted = true;
}
void CoreController::checkExternalSdCommandStatus() {
2022-01-17 15:58:27 +01:00
SdCardManager::Operations operation;
SdCardManager::OpStatus status = sdcMan->checkCurrentOp(operation);
if (status == SdCardManager::OpStatus::SUCCESS) {
if (sdInfo.currentlyCommandedState == sdInfo.commandedState) {
sdInfo.state = SdStates::SKIP_CYCLE_BEFORE_INFO_UPDATE;
sdInfo.commandFinished = true;
} else {
// stay on same state machine state because the target state was not reached yet.
sdInfo.cycleCount = 0;
}
currentStateSetter(sdInfo.commandedCard, sdInfo.currentlyCommandedState);
sdInfo.commandExecuted = false;
} else if (sdInfo.cycleCount > 4) {
sif::warning << "CoreController::sdStateMachine: Commanding SD state "
"takes too long"
<< std::endl;
}
}
void CoreController::currentStateSetter(sd::SdCard sdCard, sd::SdState newState) {
2022-01-17 15:58:27 +01:00
if (sdCard == sd::SdCard::SLOT_0) {
sdInfo.currentState.first = newState;
} else {
sdInfo.currentState.second = newState;
}
}
2021-08-06 01:25:36 +02:00
ReturnValue_t CoreController::sdCardSetup(sd::SdCard sdCard, sd::SdState targetState,
2022-01-17 15:58:27 +01:00
std::string sdChar, bool printOutput) {
std::string mountString;
sdcMan->setPrintCommandOutput(printOutput);
if (sdCard == sd::SdCard::SLOT_0) {
mountString = SdCardManager::SD_0_MOUNT_POINT;
} else {
mountString = SdCardManager::SD_1_MOUNT_POINT;
}
sd::SdState state = sd::SdState::OFF;
if (sdCard == sd::SdCard::SLOT_0) {
state = sdInfo.currentState.first;
} else {
state = sdInfo.currentState.second;
}
if (state == sd::SdState::MOUNTED) {
if (targetState == sd::SdState::OFF) {
sif::info << "Switching off SD card " << sdChar << std::endl;
return sdcMan->switchOffSdCard(sdCard, true, &sdInfo.currentState);
} else if (targetState == sd::SdState::ON) {
sif::info << "Unmounting SD card " << sdChar << std::endl;
return sdcMan->unmountSdCard(sdCard);
} else {
if (std::filesystem::exists(mountString)) {
sif::info << "SD card " << sdChar << " already on and mounted at " << mountString
<< std::endl;
return SdCardManager::ALREADY_MOUNTED;
}
sif::error << "SD card mounted but expected mount point " << mountString << " not found!"
<< std::endl;
return SdCardManager::MOUNT_ERROR;
}
}
if (state == sd::SdState::OFF) {
if (targetState == sd::SdState::MOUNTED) {
sif::info << "Switching on and mounting SD card " << sdChar << " at " << mountString
<< std::endl;
return sdcMan->switchOnSdCard(sdCard, true, &sdInfo.currentState);
} else if (targetState == sd::SdState::ON) {
sif::info << "Switching on SD card " << sdChar << std::endl;
return sdcMan->switchOnSdCard(sdCard, false, &sdInfo.currentState);
}
}
else if (state == sd::SdState::ON) {
if (targetState == sd::SdState::MOUNTED) {
sif::info << "Mounting SD card " << sdChar << " at " << mountString << std::endl;
return sdcMan->mountSdCard(sdCard);
} else if (targetState == sd::SdState::OFF) {
sif::info << "Switching off SD card " << sdChar << std::endl;
return sdcMan->switchOffSdCard(sdCard, false, &sdInfo.currentState);
}
} else {
sif::warning << "CoreController::sdCardSetup: Invalid state for this call" << std::endl;
}
return HasReturnvaluesIF::RETURN_OK;
}
2021-08-06 01:25:36 +02:00
ReturnValue_t CoreController::sdColdRedundantBlockingInit() {
2022-01-17 15:58:27 +01:00
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
result = sdCardSetup(sdInfo.pref, sd::SdState::MOUNTED, sdInfo.prefChar);
if (result != SdCardManager::ALREADY_MOUNTED and result != HasReturnvaluesIF::RETURN_OK) {
sif::warning << "Setting up preferred card " << sdInfo.otherChar
<< " in cold redundant mode failed" << std::endl;
// Try other SD card and mark set up operation as failed
sdCardSetup(sdInfo.pref, sd::SdState::MOUNTED, sdInfo.prefChar);
result = HasReturnvaluesIF::RETURN_FAILED;
}
if (result != HasReturnvaluesIF::RETURN_FAILED and sdInfo.otherState != sd::SdState::OFF) {
sif::info << "Switching off secondary SD card " << sdInfo.otherChar << std::endl;
// Switch off other SD card in cold redundant mode if setting up preferred one worked
// without issues
ReturnValue_t result2 =
sdcMan->switchOffSdCard(sdInfo.other, sdInfo.otherState, &sdInfo.currentState);
if (result2 != HasReturnvaluesIF::RETURN_OK and result2 != SdCardManager::ALREADY_OFF) {
sif::warning << "Switching off secondary SD card " << sdInfo.otherChar
<< " in cold redundant mode failed" << std::endl;
}
}
return result;
2021-05-26 14:16:16 +02:00
}
2021-07-19 12:44:43 +02:00
ReturnValue_t CoreController::incrementAllocationFailureCount() {
2022-01-17 15:58:27 +01:00
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);
2021-07-19 12:44:43 +02:00
}
ReturnValue_t CoreController::initVersionFile() {
using namespace fsfw;
2022-01-17 15:58:27 +01:00
std::string unameFileName = "/tmp/uname_version.txt";
// TODO: No -v flag for now. If the kernel version is used, need to cut off first few letters
std::string unameCmd = "uname -mnrso > " + unameFileName;
int result = std::system(unameCmd.c_str());
if (result != 0) {
utility::handleSystemError(result, "CoreController::versionFileInit");
}
std::ifstream unameFile(unameFileName);
std::string unameLine;
if (not std::getline(unameFile, unameLine)) {
sif::warning << "CoreController::versionFileInit: Retrieving uname line failed" << std::endl;
}
std::string fullObswVersionString = "OBSW: v" + std::to_string(SW_VERSION) + "." +
std::to_string(SW_SUBVERSION) + "." +
std::to_string(SW_REVISION);
char versionString[16] = {};
fsfw::FSFW_VERSION.getVersion(versionString, sizeof(versionString));
std::string fullFsfwVersionString = "FSFW: v" + std::string(versionString);
2022-01-17 15:58:27 +01:00
std::string systemString = "System: " + unameLine;
std::string versionFilePath = currMntPrefix + VERSION_FILE;
2022-01-17 15:58:27 +01:00
std::fstream versionFile;
if (not std::filesystem::exists(versionFilePath)) {
sif::info << "Writing version file " << versionFilePath << ".." << std::endl;
versionFile.open(versionFilePath, std::ios_base::out);
versionFile << fullObswVersionString << std::endl;
versionFile << fullFsfwVersionString << std::endl;
versionFile << systemString << std::endl;
return HasReturnvaluesIF::RETURN_OK;
2022-01-17 15:58:27 +01:00
}
// Check whether any version has changed
bool createNewFile = false;
versionFile.open(versionFilePath);
std::string currentVersionString;
uint8_t idx = 0;
while (std::getline(versionFile, currentVersionString)) {
if (idx == 0) {
if (currentVersionString != fullObswVersionString) {
sif::info << "OBSW version changed" << std::endl;
sif::info << "From " << currentVersionString << " to " << fullObswVersionString
<< std::endl;
createNewFile = true;
}
} else if (idx == 1) {
if (currentVersionString != fullFsfwVersionString) {
sif::info << "FSFW version changed" << std::endl;
sif::info << "From " << currentVersionString << " to " << fullFsfwVersionString
<< std::endl;
createNewFile = true;
}
} else if (idx == 2) {
if (currentVersionString != systemString) {
sif::info << "System version changed" << std::endl;
sif::info << "Old: " << currentVersionString << std::endl;
sif::info << "New: " << systemString << std::endl;
createNewFile = true;
}
} else {
sif::warning << "Invalid version file! Rewriting it.." << std::endl;
createNewFile = true;
}
idx++;
}
// Overwrite file if necessary
if (createNewFile) {
sif::info << "Rewriting version.txt file with updated versions.." << std::endl;
versionFile.close();
versionFile.open(versionFilePath, std::ios_base::out | std::ios_base::trunc);
versionFile << fullObswVersionString << std::endl;
versionFile << fullFsfwVersionString << std::endl;
versionFile << systemString << std::endl;
}
return HasReturnvaluesIF::RETURN_OK;
}
2021-07-26 11:47:45 +02:00
ReturnValue_t CoreController::actionListDirectoryIntoFile(ActionId_t actionId,
2022-01-17 15:58:27 +01:00
MessageQueueId_t commandedBy,
const uint8_t *data, size_t size) {
// TODO: Packet definition for clean deserialization
// 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) {
return HasActionsIF::INVALID_PARAMETERS;
}
// We could also make -l optional, but I can't think of a reason why to not use -l..
// 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
// strings are not 0 terminated properly
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;
int result = std::system(oss.str().c_str());
if (result != 0) {
utility::handleSystemError(result, "CoreController::actionListDirectoryIntoFile");
actionHelper.finish(false, commandedBy, actionId);
}
return HasReturnvaluesIF::RETURN_OK;
}
ReturnValue_t CoreController::initBootCopy() {
2022-01-17 15:58:27 +01:00
if (not std::filesystem::exists(CURR_COPY_FILE)) {
// This file is created by the systemd service eive-early-config so this should
2022-01-17 15:58:27 +01:00
// not happen normally
std::string cmd = "xsc_boot_copy > " + std::string(CURR_COPY_FILE);
int result = std::system(cmd.c_str());
if (result != 0) {
utility::handleSystemError(result, "CoreController::initBootCopy");
}
}
getCurrentBootCopy(CURRENT_CHIP, CURRENT_COPY);
2022-01-17 15:58:27 +01:00
return HasReturnvaluesIF::RETURN_OK;
}
void CoreController::getCurrentBootCopy(xsc::Chip &chip, xsc::Copy &copy) {
xsc_libnor_chip_t xscChip;
xsc_libnor_copy_t xscCopy;
xsc_boot_get_chip_copy(&xscChip, &xscCopy);
2022-01-17 15:58:27 +01:00
// Not really thread-safe but it does not need to be
chip = static_cast<xsc::Chip>(xscChip);
copy = static_cast<xsc::Copy>(xscCopy);
}
2021-07-29 16:31:04 +02:00
ReturnValue_t CoreController::initWatchdogFifo() {
2022-01-17 15:58:27 +01:00
if (not std::filesystem::exists(watchdog::FIFO_NAME)) {
// Still return RETURN_OK for now
sif::info << "Watchdog FIFO " << watchdog::FIFO_NAME << " does not exist, can't initiate"
<< " watchdog" << std::endl;
2021-07-29 16:31:04 +02:00
return HasReturnvaluesIF::RETURN_OK;
2022-01-17 15:58:27 +01:00
}
// Open FIFO write only and non-blocking to prevent SW from killing itself.
watchdogFifoFd = open(watchdog::FIFO_NAME.c_str(), O_WRONLY | O_NONBLOCK);
if (watchdogFifoFd < 0) {
if (errno == ENXIO) {
watchdogFifoFd = RETRY_FIFO_OPEN;
sif::info << "eive-watchdog not running. FIFO can not be opened" << std::endl;
} else {
sif::error << "Opening pipe " << watchdog::FIFO_NAME << " write-only failed with " << errno
<< ": " << strerror(errno) << std::endl;
return HasReturnvaluesIF::RETURN_FAILED;
}
}
return HasReturnvaluesIF::RETURN_OK;
2021-07-29 16:31:04 +02:00
}
2021-07-26 11:47:45 +02:00
void CoreController::initPrint() {
#if OBSW_VERBOSE_LEVEL >= 1
2022-01-17 15:58:27 +01:00
if (watchdogFifoFd > 0) {
sif::info << "Opened watchdog FIFO successfully.." << std::endl;
}
2021-07-26 11:47:45 +02:00
#endif
}
2021-07-29 18:09:54 +02:00
2022-04-29 11:16:53 +02:00
ReturnValue_t CoreController::actionXscReboot(const uint8_t *data, size_t size) {
2022-01-17 15:58:27 +01:00
if (size < 1) {
return HasActionsIF::INVALID_PARAMETERS;
}
bool rebootSameBootCopy = data[0];
2022-04-29 11:16:53 +02:00
bool protOpPerformed = false;
SdCardManager::instance()->setBlocking(true);
2022-01-17 15:58:27 +01:00
if (rebootSameBootCopy) {
#if OBSW_VERBOSE_LEVEL >= 1
2022-01-17 15:58:27 +01:00
sif::info << "CoreController::actionPerformReboot: Rebooting on current image" << std::endl;
#endif
2022-04-29 11:16:53 +02:00
gracefulShutdownTasks(xsc::Chip::SELF_CHIP, xsc::Copy::SELF_COPY, protOpPerformed);
2022-01-17 15:58:27 +01:00
int result = std::system("xsc_boot_copy -r");
if (result != 0) {
utility::handleSystemError(result, "CoreController::executeAction");
return HasReturnvaluesIF::RETURN_FAILED;
2021-08-03 11:18:24 +02:00
}
2022-01-17 15:58:27 +01:00
return HasActionsIF::EXECUTION_FINISHED;
}
if (size < 3 or (data[1] > 1 or data[2] > 1)) {
2022-01-17 15:58:27 +01:00
return HasActionsIF::INVALID_PARAMETERS;
}
#if OBSW_VERBOSE_LEVEL >= 1
2022-01-17 15:58:27 +01:00
sif::info << "CoreController::actionPerformReboot: Rebooting on " << static_cast<int>(data[1])
<< " " << static_cast<int>(data[2]) << std::endl;
#endif
2022-01-17 15:58:27 +01:00
// Check that the target chip and copy is writeprotected first
generateChipStateFile();
// If any boot copies are unprotected, protect them here
auto tgtChip = static_cast<xsc::Chip>(data[1]);
auto tgtCopy = static_cast<xsc::Copy>(data[2]);
2022-04-29 11:16:53 +02:00
// This function can not really fail
gracefulShutdownTasks(tgtChip, tgtCopy, protOpPerformed);
2022-01-17 15:58:27 +01:00
switch (tgtChip) {
case (xsc::Chip::CHIP_0): {
switch (tgtCopy) {
case (xsc::Copy::COPY_0): {
xsc_boot_copy(XSC_LIBNOR_CHIP_0, XSC_LIBNOR_COPY_NOMINAL);
break;
}
case (xsc::Copy::COPY_1): {
xsc_boot_copy(XSC_LIBNOR_CHIP_0, XSC_LIBNOR_COPY_GOLD);
break;
}
default: {
break;
}
}
break;
}
case (xsc::Chip::CHIP_1): {
switch (tgtCopy) {
case (xsc::Copy::COPY_0): {
xsc_boot_copy(XSC_LIBNOR_CHIP_1, XSC_LIBNOR_COPY_NOMINAL);
break;
}
case (xsc::Copy::COPY_1): {
xsc_boot_copy(XSC_LIBNOR_CHIP_1, XSC_LIBNOR_COPY_GOLD);
break;
}
default: {
break;
}
}
break;
}
default:
break;
2022-01-17 15:58:27 +01:00
}
return HasReturnvaluesIF::RETURN_FAILED;
2021-08-03 11:18:24 +02:00
}
2022-04-29 11:16:53 +02:00
ReturnValue_t CoreController::actionReboot(const uint8_t *data, size_t size) {
bool protOpPerformed = false;
gracefulShutdownTasks(xsc::Chip::CHIP_0, xsc::Copy::COPY_0, protOpPerformed);
std::system("reboot");
return RETURN_OK;
}
ReturnValue_t CoreController::gracefulShutdownTasks(xsc::Chip chip, xsc::Copy copy,
bool &protOpPerformed) {
2022-05-24 01:14:04 +02:00
sdcMan->setBlocking(true);
2022-04-29 11:16:53 +02:00
// Attempt graceful shutdown by unmounting and switching off SD cards
2022-05-24 01:14:04 +02:00
sdcMan->switchOffSdCard(sd::SdCard::SLOT_0);
sdcMan->switchOffSdCard(sd::SdCard::SLOT_1);
2022-04-29 11:16:53 +02:00
// If any boot copies are unprotected
ReturnValue_t result = setBootCopyProtection(xsc::Chip::SELF_CHIP, xsc::Copy::SELF_COPY, true,
protOpPerformed, false);
if (result == HasReturnvaluesIF::RETURN_OK and protOpPerformed) {
// TODO: Would be nice to notify operator. But we can't use the filesystem anymore
// and a reboot is imminent. Use scratch buffer?
sif::info << "Running slot was writeprotected before reboot" << std::endl;
}
return result;
}
2022-01-17 15:58:27 +01:00
CoreController::~CoreController() {}
2021-08-06 01:25:36 +02:00
void CoreController::updateSdInfoOther() {
2022-01-17 15:58:27 +01:00
if (sdInfo.pref == sd::SdCard::SLOT_0) {
sdInfo.prefChar = "0";
sdInfo.otherChar = "1";
sdInfo.otherState = sdInfo.currentState.second;
sdInfo.prefState = sdInfo.currentState.first;
sdInfo.other = sd::SdCard::SLOT_1;
} else if (sdInfo.pref == sd::SdCard::SLOT_1) {
sdInfo.prefChar = "1";
sdInfo.otherChar = "0";
sdInfo.otherState = sdInfo.currentState.first;
sdInfo.prefState = sdInfo.currentState.second;
sdInfo.other = sd::SdCard::SLOT_0;
} else {
sif::warning << "CoreController::updateSdInfoOther: Invalid SD card passed" << std::endl;
}
2021-08-06 01:25:36 +02:00
}
2022-01-17 15:58:27 +01:00
bool CoreController::sdInitFinished() const { return sdInfo.initFinished; }
2021-08-06 01:25:36 +02:00
ReturnValue_t CoreController::generateChipStateFile() {
2022-01-17 15:58:27 +01:00
int result = std::system(CHIP_PROT_SCRIPT);
if (result != 0) {
utility::handleSystemError(result, "CoreController::generateChipStateFile");
return HasReturnvaluesIF::RETURN_FAILED;
}
return HasReturnvaluesIF::RETURN_OK;
}
ReturnValue_t CoreController::setBootCopyProtection(xsc::Chip targetChip, xsc::Copy targetCopy,
bool protect, bool &protOperationPerformed,
2022-01-17 15:58:27 +01:00
bool updateProtFile) {
bool allChips = false;
bool allCopies = false;
bool selfChip = false;
bool selfCopy = false;
protOperationPerformed = false;
switch (targetChip) {
case (xsc::Chip::ALL_CHIP): {
2022-01-17 15:58:27 +01:00
allChips = true;
break;
}
case (xsc::Chip::NO_CHIP): {
2022-01-17 15:58:27 +01:00
return HasReturnvaluesIF::RETURN_OK;
}
case (xsc::Chip::SELF_CHIP): {
2022-01-17 15:58:27 +01:00
selfChip = true;
targetChip = CURRENT_CHIP;
break;
}
default: {
2022-01-17 15:58:27 +01:00
break;
}
2022-01-17 15:58:27 +01:00
}
switch (targetCopy) {
case (xsc::Copy::ALL_COPY): {
2022-01-17 15:58:27 +01:00
allCopies = true;
break;
}
case (xsc::Copy::NO_COPY): {
2022-01-17 15:58:27 +01:00
return HasReturnvaluesIF::RETURN_OK;
}
case (xsc::Copy::SELF_COPY): {
2022-01-17 15:58:27 +01:00
selfCopy = true;
targetCopy = CURRENT_COPY;
break;
}
default: {
2022-01-17 15:58:27 +01:00
break;
}
2022-01-17 15:58:27 +01:00
}
2022-01-17 15:58:27 +01:00
for (uint8_t arrIdx = 0; arrIdx < protArray.size(); arrIdx++) {
int result = handleBootCopyProtAtIndex(targetChip, targetCopy, protect, protOperationPerformed,
selfChip, selfCopy, allChips, allCopies, arrIdx);
if (result != 0) {
break;
2021-08-19 18:10:19 +02:00
}
2022-01-17 15:58:27 +01:00
}
if (protOperationPerformed and updateProtFile) {
updateProtInfo();
}
return HasReturnvaluesIF::RETURN_OK;
2021-08-19 18:10:19 +02:00
}
int CoreController::handleBootCopyProtAtIndex(xsc::Chip targetChip, xsc::Copy targetCopy,
bool protect, bool &protOperationPerformed,
bool selfChip, bool selfCopy, bool allChips,
bool allCopies, uint8_t arrIdx) {
2022-01-17 15:58:27 +01:00
bool currentProt = protArray[arrIdx];
std::ostringstream oss;
bool performOp = false;
if (protect == currentProt) {
return 0;
}
if (protOperationPerformed) {
if ((selfChip and selfCopy) or (not allCopies and not allChips)) {
// No need to continue, only one operation was requested
return 1;
}
}
xsc::Chip currentChip;
xsc::Copy currentCopy;
2022-01-17 15:58:27 +01:00
oss << "writeprotect ";
if (arrIdx == 0 or arrIdx == 1) {
oss << "0 ";
currentChip = xsc::Chip::CHIP_0;
2022-01-17 15:58:27 +01:00
} else {
oss << "1 ";
currentChip = xsc::Chip::CHIP_1;
2022-01-17 15:58:27 +01:00
}
if (arrIdx == 0 or arrIdx == 2) {
oss << "0 ";
currentCopy = xsc::Copy::COPY_0;
2022-01-17 15:58:27 +01:00
} else {
oss << "1 ";
currentCopy = xsc::Copy::COPY_1;
2022-01-17 15:58:27 +01:00
}
if (protect) {
oss << "1";
} else {
oss << "0";
}
int result = 0;
if (allChips and allCopies) {
performOp = true;
} else if (allChips) {
if ((selfCopy and CURRENT_COPY == targetCopy) or (currentCopy == targetCopy)) {
performOp = true;
}
} else if (allCopies) {
if ((selfChip and CURRENT_COPY == targetCopy) or (currentChip == targetChip)) {
performOp = true;
}
} else if (selfChip and (currentChip == targetChip)) {
if (selfCopy) {
if (currentCopy == targetCopy) {
2021-08-19 18:10:19 +02:00
performOp = true;
2022-01-17 15:58:27 +01:00
}
} else {
performOp = true;
2021-08-19 18:10:19 +02:00
}
2022-01-17 15:58:27 +01:00
} else if (selfCopy and (currentCopy == targetCopy)) {
if (selfChip) {
if (currentChip == targetChip) {
2021-08-19 18:10:19 +02:00
performOp = true;
2022-01-17 15:58:27 +01:00
}
} else {
performOp = true;
}
} else if ((targetChip == currentChip) and (targetCopy == currentCopy)) {
performOp = true;
}
if (result != 0) {
utility::handleSystemError(result, "CoreController::checkAndSetBootCopyProtection");
}
if (performOp) {
// TODO: Lock operation take a long time. Use command executor? That would require a
// new state machine..
protOperationPerformed = true;
sif::info << "Executing command: " << oss.str() << std::endl;
result = std::system(oss.str().c_str());
}
return 0;
}
ReturnValue_t CoreController::updateProtInfo(bool regenerateChipStateFile) {
2022-01-17 15:58:27 +01:00
using namespace std;
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
if (regenerateChipStateFile) {
result = generateChipStateFile();
if (result != HasReturnvaluesIF::RETURN_OK) {
sif::warning << "CoreController::updateProtInfo: Generating chip state file failed"
<< std::endl;
return result;
}
}
if (not filesystem::exists(CHIP_STATE_FILE)) {
return HasReturnvaluesIF::RETURN_FAILED;
}
ifstream chipStateFile(CHIP_STATE_FILE);
if (not chipStateFile.good()) {
return HasReturnvaluesIF::RETURN_FAILED;
}
string nextLine;
uint8_t lineCounter = 0;
string word;
while (getline(chipStateFile, nextLine)) {
ReturnValue_t result = handleProtInfoUpdateLine(nextLine);
if (result != HasReturnvaluesIF::RETURN_OK) {
sif::warning << "CoreController::updateProtInfo: Protection info update failed!" << std::endl;
return result;
}
++lineCounter;
if (lineCounter > 4) {
sif::warning << "CoreController::checkAndProtectBootCopy: "
"Line counter larger than 4"
<< std::endl;
}
}
return HasReturnvaluesIF::RETURN_OK;
}
ReturnValue_t CoreController::handleProtInfoUpdateLine(std::string nextLine) {
2022-01-17 15:58:27 +01:00
using namespace std;
string word;
uint8_t wordIdx = 0;
uint8_t arrayIdx = 0;
istringstream iss(nextLine);
xsc::Chip currentChip = xsc::Chip::CHIP_0;
xsc::Copy currentCopy = xsc::Copy::COPY_0;
2022-01-17 15:58:27 +01:00
while (iss >> word) {
if (wordIdx == 1) {
currentChip = static_cast<xsc::Chip>(stoi(word));
2022-01-17 15:58:27 +01:00
}
if (wordIdx == 3) {
currentCopy = static_cast<xsc::Copy>(stoi(word));
2022-01-17 15:58:27 +01:00
}
if (wordIdx == 3) {
if (currentChip == xsc::Chip::CHIP_0) {
if (currentCopy == xsc::Copy::COPY_0) {
2022-01-17 15:58:27 +01:00
arrayIdx = 0;
} else if (currentCopy == xsc::Copy::COPY_1) {
2022-01-17 15:58:27 +01:00
arrayIdx = 1;
}
}
else if (currentChip == xsc::Chip::CHIP_1) {
if (currentCopy == xsc::Copy::COPY_0) {
2022-01-17 15:58:27 +01:00
arrayIdx = 2;
} else if (currentCopy == xsc::Copy::COPY_1) {
2022-01-17 15:58:27 +01:00
arrayIdx = 3;
}
}
}
if (wordIdx == 5) {
if (word == "unlocked.") {
protArray[arrayIdx] = false;
} else {
protArray[arrayIdx] = true;
}
}
wordIdx++;
}
return HasReturnvaluesIF::RETURN_OK;
}
2021-07-29 18:09:54 +02:00
void CoreController::performWatchdogControlOperation() {
2022-01-17 15:58:27 +01:00
// Only perform each fifth iteration
if (watchdogFifoFd != 0 and opDivider5.check()) {
2022-01-17 15:58:27 +01:00
if (watchdogFifoFd == RETRY_FIFO_OPEN) {
// Open FIFO write only and non-blocking
watchdogFifoFd = open(watchdog::FIFO_NAME.c_str(), O_WRONLY | O_NONBLOCK);
if (watchdogFifoFd < 0) {
if (errno == ENXIO) {
watchdogFifoFd = RETRY_FIFO_OPEN;
// No printout for now, would be spam
return;
} else {
sif::error << "Opening pipe " << watchdog::FIFO_NAME << " write-only failed with "
<< errno << ": " << strerror(errno) << std::endl;
return;
}
}
sif::info << "Opened " << watchdog::FIFO_NAME << " successfully" << std::endl;
} else if (watchdogFifoFd > 0) {
// Write to OBSW watchdog FIFO here
const char writeChar = 'a';
ssize_t writtenBytes = write(watchdogFifoFd, &writeChar, 1);
if (writtenBytes < 0) {
sif::error << "Errors writing to watchdog FIFO, code " << errno << ": " << strerror(errno)
<< std::endl;
}
}
}
2021-07-29 18:09:54 +02:00
}
2022-03-01 14:44:50 +01:00
void CoreController::performMountedSdCardOperations() {
2022-05-24 00:52:08 +02:00
auto mountedSdCardOp = [&](bool &mntSwitch, sd::SdCard sdCard, std::string mntPoint) {
2022-05-24 00:20:23 +02:00
if (mntSwitch) {
bool sdCardMounted = sdcMan->isSdCardMounted(sdCard);
if (sdCardMounted and not performOneShotSdCardOpsSwitch) {
std::ostringstream path;
path << mntPoint << "/" << CONF_FOLDER;
if (not std::filesystem::exists(path.str())) {
std::filesystem::create_directory(path.str());
}
initVersionFile();
initClockFromTimeFile();
performRebootFileHandling(false);
performOneShotSdCardOpsSwitch = true;
}
mntSwitch = false;
}
};
2022-05-24 00:52:08 +02:00
if (sdInfo.pref == sd::SdCard::SLOT_1) {
2022-05-24 00:20:23 +02:00
mountedSdCardOp(sdInfo.mountSwitch.second, sd::SdCard::SLOT_1, SdCardManager::SD_1_MOUNT_POINT);
mountedSdCardOp(sdInfo.mountSwitch.first, sd::SdCard::SLOT_0, SdCardManager::SD_0_MOUNT_POINT);
} else {
mountedSdCardOp(sdInfo.mountSwitch.first, sd::SdCard::SLOT_0, SdCardManager::SD_0_MOUNT_POINT);
mountedSdCardOp(sdInfo.mountSwitch.second, sd::SdCard::SLOT_1, SdCardManager::SD_1_MOUNT_POINT);
}
timeFileHandler();
}
ReturnValue_t CoreController::performSdCardCheck() {
bool mountedReadOnly = false;
SdCardManager::SdStatePair active;
sdcMan->getSdCardsStatus(active);
auto sdCardCheck = [&](sd::SdCard sdCard) {
ReturnValue_t result = sdcMan->isSdCardMountedReadOnly(sdCard, mountedReadOnly);
2022-05-11 17:02:57 +02:00
if (result != HasReturnvaluesIF::RETURN_OK) {
2022-05-24 00:20:23 +02:00
sif::error << "CoreController::performSdCardCheck: Could not check "
"read-only mount state"
<< std::endl;
2022-05-11 17:02:57 +02:00
mountedReadOnly = true;
}
if (mountedReadOnly) {
int linuxErrno = 0;
2022-05-24 00:20:23 +02:00
result = sdcMan->performFsck(sdCard, true, linuxErrno);
if (result != HasReturnvaluesIF::RETURN_OK) {
sif::error << "CoreController::performSdCardCheck: fsck command on SD Card "
<< static_cast<uint8_t>(sdCard) << " failed with code " << linuxErrno << " | "
<< strerror(linuxErrno);
2022-05-11 17:02:57 +02:00
}
2022-05-24 00:20:23 +02:00
result = sdcMan->remountReadWrite(sdCard);
if (result == HasReturnvaluesIF::RETURN_OK) {
sif::warning << "CoreController::performSdCardCheck: Remounted SD Card "
<< static_cast<uint8_t>(sdCard) << " read-write";
2022-05-11 17:02:57 +02:00
} else {
2022-05-24 00:20:23 +02:00
sif::error << "CoreController::performSdCardCheck: Remounting SD Card "
<< static_cast<uint8_t>(sdCard) << " read-write failed";
2022-05-11 17:02:57 +02:00
}
}
2022-05-24 00:20:23 +02:00
};
if (active.first == sd::SdState::MOUNTED) {
sdCardCheck(sd::SdCard::SLOT_0);
2022-03-01 14:44:50 +01:00
}
2022-05-24 00:20:23 +02:00
if (active.second == sd::SdState::MOUNTED) {
sdCardCheck(sd::SdCard::SLOT_1);
}
return RETURN_OK;
2022-03-01 14:44:50 +01:00
}
void CoreController::performRebootFileHandling(bool recreateFile) {
using namespace std;
std::string path = currMntPrefix + REBOOT_FILE;
if (not std::filesystem::exists(path) or recreateFile) {
#if OBSW_VERBOSE_LEVEL >= 1
2022-03-01 13:34:12 +01:00
sif::info << "CoreController::performRebootFileHandling: Recreating reboot file" << std::endl;
#endif
rebootFile.enabled = true;
rebootFile.img00Cnt = 0;
rebootFile.img01Cnt = 0;
rebootFile.img10Cnt = 0;
rebootFile.img11Cnt = 0;
rebootFile.lastChip = xsc::Chip::CHIP_0;
rebootFile.lastCopy = xsc::Copy::COPY_0;
2022-03-01 13:34:12 +01:00
rebootFile.img00Lock = false;
rebootFile.img01Lock = false;
rebootFile.img10Lock = false;
rebootFile.img11Lock = false;
rebootFile.mechanismNextChip = xsc::Chip::NO_CHIP;
rebootFile.mechanismNextCopy = xsc::Copy::NO_COPY;
rebootFile.bootFlag = false;
rewriteRebootFile(rebootFile);
} else {
if (not parseRebootFile(path, rebootFile)) {
performRebootFileHandling(true);
}
}
2022-03-01 13:34:12 +01:00
if (CURRENT_CHIP == xsc::CHIP_0) {
if (CURRENT_COPY == xsc::COPY_0) {
rebootFile.img00Cnt++;
} else {
rebootFile.img01Cnt++;
}
2022-03-01 13:34:12 +01:00
} else {
if (CURRENT_COPY == xsc::COPY_0) {
rebootFile.img10Cnt++;
} else {
rebootFile.img11Cnt++;
}
}
2022-02-28 19:52:43 +01:00
if (rebootFile.bootFlag) {
// Trigger event to inform ground that a reboot was triggered
uint32_t p1 = rebootFile.lastChip << 16 | rebootFile.lastCopy;
uint32_t p2 = rebootFile.img00Cnt << 24 | rebootFile.img01Cnt << 16 | rebootFile.img10Cnt << 8 |
rebootFile.img11Cnt;
triggerEvent(REBOOT_MECHANISM_TRIGGERED, p1, p2);
// Clear the boot flag
rebootFile.bootFlag = false;
}
2022-02-28 19:52:43 +01:00
2022-03-01 13:34:12 +01:00
if (rebootFile.mechanismNextChip != xsc::NO_CHIP and
rebootFile.mechanismNextCopy != xsc::NO_COPY) {
if (CURRENT_CHIP != rebootFile.mechanismNextChip or
CURRENT_COPY != rebootFile.mechanismNextCopy) {
2022-03-01 14:44:50 +01:00
std::string infoString = std::to_string(rebootFile.mechanismNextChip) + " " +
std::to_string(rebootFile.mechanismNextCopy);
sif::warning << "CoreController::performRebootFileHandling: Expected to be on image "
<< infoString << " but currently on other image. Locking " << infoString
2022-03-01 13:34:12 +01:00
<< std::endl;
// Firmware or other component might be corrupt and we are on another image then the target
// image specified by the mechanism. We can't really trust the target image anymore.
// Lock it for now
if (rebootFile.mechanismNextChip == xsc::CHIP_0) {
if (rebootFile.mechanismNextCopy == xsc::COPY_0) {
rebootFile.img00Lock = true;
} else {
rebootFile.img01Lock = true;
}
} else {
if (rebootFile.mechanismNextCopy == xsc::COPY_0) {
rebootFile.img10Lock = true;
} else {
rebootFile.img11Lock = true;
}
}
}
}
2022-03-01 14:44:50 +01:00
rebootFile.lastChip = CURRENT_CHIP;
rebootFile.lastCopy = CURRENT_COPY;
2022-02-28 20:24:40 +01:00
// Only reboot if the reboot functionality is enabled.
// The handler will still increment the boot counts
if (rebootFile.enabled and (*rebootFile.relevantBootCnt >= rebootFile.maxCount)) {
// Reboot to other image
bool doReboot = false;
2022-02-28 19:52:43 +01:00
xsc::Chip tgtChip = xsc::NO_CHIP;
xsc::Copy tgtCopy = xsc::NO_COPY;
determineAndExecuteReboot(rebootFile, doReboot, tgtChip, tgtCopy);
if (doReboot) {
rebootFile.bootFlag = true;
#if OBSW_VERBOSE_LEVEL >= 1
2022-03-01 13:34:12 +01:00
sif::info << "Boot counter for image " << CURRENT_CHIP << " " << CURRENT_COPY
2022-02-28 19:52:43 +01:00
<< " too high. Rebooting to " << tgtChip << " " << tgtCopy << std::endl;
#endif
2022-03-01 13:34:12 +01:00
rebootFile.mechanismNextChip = tgtChip;
rebootFile.mechanismNextCopy = tgtCopy;
rewriteRebootFile(rebootFile);
2022-02-28 19:52:43 +01:00
xsc_boot_copy(static_cast<xsc_libnor_chip_t>(tgtChip),
static_cast<xsc_libnor_copy_t>(tgtCopy));
}
} else {
2022-03-01 14:44:50 +01:00
rebootFile.mechanismNextChip = xsc::NO_CHIP;
rebootFile.mechanismNextCopy = xsc::NO_COPY;
}
2022-03-01 14:44:50 +01:00
rewriteRebootFile(rebootFile);
}
void CoreController::determineAndExecuteReboot(RebootFile &rf, bool &needsReboot,
xsc::Chip &tgtChip, xsc::Copy &tgtCopy) {
tgtChip = xsc::CHIP_0;
tgtCopy = xsc::COPY_0;
needsReboot = false;
2022-02-28 19:52:43 +01:00
if ((CURRENT_CHIP == xsc::CHIP_0) and (CURRENT_COPY == xsc::COPY_0) and
(rf.img00Cnt >= rf.maxCount)) {
needsReboot = true;
2022-03-01 13:34:12 +01:00
if (rf.img01Cnt < rf.maxCount and not rf.img01Lock) {
tgtCopy = xsc::COPY_1;
2022-03-01 13:34:12 +01:00
return;
}
if (rf.img10Cnt < rf.maxCount and not rf.img10Lock) {
tgtChip = xsc::CHIP_1;
return;
}
2022-03-01 13:34:12 +01:00
if (rf.img11Cnt < rf.maxCount and not rf.img11Lock) {
tgtChip = xsc::CHIP_1;
tgtCopy = xsc::COPY_1;
return;
}
// Can't really do much here. Stay on image
sif::warning
<< "All reboot counts too high or all fallback images locked, already on fallback image"
<< std::endl;
needsReboot = false;
return;
}
2022-02-28 19:52:43 +01:00
if ((CURRENT_CHIP == xsc::CHIP_0) and (CURRENT_COPY == xsc::COPY_1) and
(rf.img01Cnt >= rf.maxCount)) {
needsReboot = true;
2022-03-01 13:34:12 +01:00
if (rf.img00Cnt < rf.maxCount and not rf.img00Lock) {
// Reboot on fallback image
2022-03-01 13:34:12 +01:00
return;
}
if (rf.img10Cnt < rf.maxCount and not rf.img10Lock) {
tgtChip = xsc::CHIP_1;
return;
}
if (rf.img11Cnt < rf.maxCount and not rf.img11Lock) {
tgtChip = xsc::CHIP_1;
tgtCopy = xsc::COPY_1;
}
if (rf.img00Lock) {
needsReboot = false;
}
2022-03-01 13:34:12 +01:00
// Reboot to fallback image
}
2022-02-28 19:52:43 +01:00
if ((CURRENT_CHIP == xsc::CHIP_1) and (CURRENT_COPY == xsc::COPY_0) and
(rf.img10Cnt >= rf.maxCount)) {
needsReboot = true;
2022-03-01 13:34:12 +01:00
if (rf.img11Cnt < rf.maxCount and not rf.img11Lock) {
tgtChip = xsc::CHIP_1;
tgtCopy = xsc::COPY_1;
2022-03-01 13:34:12 +01:00
return;
}
if (rf.img00Cnt < rf.maxCount and not rf.img00Lock) {
return;
}
if (rf.img01Cnt < rf.maxCount and not rf.img01Lock) {
tgtCopy = xsc::COPY_1;
return;
}
2022-03-01 13:34:12 +01:00
if (rf.img00Lock) {
needsReboot = false;
}
// Reboot to fallback image
}
2022-02-28 19:52:43 +01:00
if ((CURRENT_CHIP == xsc::CHIP_1) and (CURRENT_COPY == xsc::COPY_1) and
(rf.img11Cnt >= rf.maxCount)) {
needsReboot = true;
2022-03-01 13:34:12 +01:00
if (rf.img10Cnt < rf.maxCount and not rf.img10Lock) {
tgtChip = xsc::CHIP_1;
2022-03-01 13:34:12 +01:00
return;
}
if (rf.img00Cnt < rf.maxCount and not rf.img00Lock) {
return;
}
2022-03-01 13:34:12 +01:00
if (rf.img01Cnt < rf.maxCount and not rf.img01Lock) {
tgtCopy = xsc::COPY_1;
return;
}
if (rf.img00Lock) {
needsReboot = false;
}
// Reboot to fallback image
}
}
bool CoreController::parseRebootFile(std::string path, RebootFile &rf) {
using namespace std;
std::string selfMatch;
if (CURRENT_CHIP == xsc::CHIP_0) {
if (CURRENT_COPY == xsc::COPY_0) {
selfMatch = "00";
} else {
selfMatch = "01";
}
} else {
if (CURRENT_COPY == xsc::COPY_0) {
selfMatch = "10";
} else {
selfMatch = "11";
}
}
ifstream file(path);
string word;
string line;
uint8_t lineIdx = 0;
while (std::getline(file, line)) {
istringstream iss(line);
switch (lineIdx) {
case 0: {
iss >> word;
if (word.find("on:") == string::npos) {
// invalid file
return false;
}
iss >> rf.enabled;
break;
}
case 1: {
iss >> word;
if (word.find("maxcnt:") == string::npos) {
return false;
}
iss >> rf.maxCount;
break;
}
case 2: {
iss >> word;
if (word.find("img00:") == string::npos) {
return false;
}
iss >> rf.img00Cnt;
if (word.find(selfMatch) != string::npos) {
2022-02-28 19:52:43 +01:00
rf.relevantBootCnt = &rf.img00Cnt;
}
break;
}
case 3: {
iss >> word;
if (word.find("img01:") == string::npos) {
return false;
}
iss >> rf.img01Cnt;
if (word.find(selfMatch) != string::npos) {
2022-02-28 19:52:43 +01:00
rf.relevantBootCnt = &rf.img01Cnt;
}
break;
}
case 4: {
iss >> word;
if (word.find("img10:") == string::npos) {
return false;
}
iss >> rf.img10Cnt;
if (word.find(selfMatch) != string::npos) {
2022-02-28 19:52:43 +01:00
rf.relevantBootCnt = &rf.img10Cnt;
}
break;
}
case 5: {
iss >> word;
if (word.find("img11:") == string::npos) {
return false;
}
iss >> rf.img11Cnt;
if (word.find(selfMatch) != string::npos) {
2022-02-28 19:52:43 +01:00
rf.relevantBootCnt = &rf.img11Cnt;
}
break;
}
case 6: {
2022-03-01 13:34:12 +01:00
iss >> word;
if (word.find("img00lock:") == string::npos) {
return false;
}
iss >> rf.img00Lock;
break;
}
case 7: {
iss >> word;
if (word.find("img01lock:") == string::npos) {
return false;
}
iss >> rf.img01Lock;
break;
}
case 8: {
iss >> word;
if (word.find("img10lock:") == string::npos) {
return false;
}
iss >> rf.img10Lock;
break;
}
case 9: {
iss >> word;
if (word.find("img11lock:") == string::npos) {
return false;
}
iss >> rf.img11Lock;
break;
}
case 10: {
iss >> word;
if (word.find("bootflag:") == string::npos) {
return false;
}
iss >> rf.bootFlag;
break;
}
2022-03-01 13:34:12 +01:00
case 11: {
iss >> word;
2022-02-28 19:52:43 +01:00
int copyRaw = 0;
int chipRaw = 0;
if (word.find("last:") == string::npos) {
return false;
}
iss >> chipRaw;
if (iss.fail()) {
return false;
}
iss >> copyRaw;
if (iss.fail()) {
return false;
}
if (chipRaw > 1 or copyRaw > 1) {
return false;
}
rf.lastChip = static_cast<xsc::Chip>(chipRaw);
rf.lastCopy = static_cast<xsc::Copy>(copyRaw);
2022-03-01 13:34:12 +01:00
break;
}
case 12: {
iss >> word;
int copyRaw = 0;
int chipRaw = 0;
if (word.find("next:") == string::npos) {
return false;
}
iss >> chipRaw;
if (iss.fail()) {
return false;
}
iss >> copyRaw;
if (iss.fail()) {
return false;
}
if (chipRaw > 2 or copyRaw > 2) {
return false;
}
rf.mechanismNextChip = static_cast<xsc::Chip>(chipRaw);
rf.mechanismNextCopy = static_cast<xsc::Copy>(copyRaw);
break;
}
}
if (iss.fail()) {
return false;
}
lineIdx++;
}
2022-03-01 13:34:12 +01:00
if (lineIdx < 12) {
return false;
}
return true;
}
void CoreController::resetRebootCount(xsc::Chip tgtChip, xsc::Copy tgtCopy) {
std::string path = currMntPrefix + REBOOT_FILE;
// Disable the reboot file mechanism
parseRebootFile(path, rebootFile);
if (tgtChip == xsc::ALL_CHIP and tgtCopy == xsc::ALL_COPY) {
rebootFile.img00Cnt = 0;
rebootFile.img01Cnt = 0;
rebootFile.img10Cnt = 0;
rebootFile.img11Cnt = 0;
} else {
if (tgtChip == xsc::CHIP_0) {
if (tgtCopy == xsc::COPY_0) {
rebootFile.img00Cnt = 0;
} else {
rebootFile.img01Cnt = 0;
}
} else {
if (tgtCopy == xsc::COPY_0) {
rebootFile.img10Cnt = 0;
} else {
rebootFile.img11Cnt = 0;
}
}
}
rewriteRebootFile(rebootFile);
}
void CoreController::rewriteRebootFile(RebootFile file) {
std::string path = currMntPrefix + REBOOT_FILE;
std::ofstream rebootFile(path);
if (rebootFile.is_open()) {
// Initiate reboot file first. Reboot handling will be on on initialization
rebootFile << "on: " << file.enabled << "\nmaxcnt: " << file.maxCount
<< "\nimg00: " << file.img00Cnt << "\nimg01: " << file.img01Cnt
<< "\nimg10: " << file.img10Cnt << "\nimg11: " << file.img11Cnt
2022-03-01 13:34:12 +01:00
<< "\nimg00lock: " << file.img00Lock << "\nimg01lock: " << file.img01Lock
2022-03-01 14:44:50 +01:00
<< "\nimg10lock: " << file.img10Lock << "\nimg11lock: " << file.img11Lock
2022-02-28 16:48:13 +01:00
<< "\nbootflag: " << file.bootFlag << "\nlast: " << static_cast<int>(file.lastChip)
2022-03-01 13:34:12 +01:00
<< " " << static_cast<int>(file.lastCopy)
<< "\nnext: " << static_cast<int>(file.mechanismNextChip) << " "
<< static_cast<int>(file.mechanismNextCopy) << "\n";
}
}
2022-03-01 14:44:50 +01:00
void CoreController::setRebootMechanismLock(bool lock, xsc::Chip tgtChip, xsc::Copy tgtCopy) {
2022-05-24 00:20:23 +02:00
std::string path = currMntPrefix + REBOOT_FILE;
2022-03-01 14:44:50 +01:00
// Disable the reboot file mechanism
parseRebootFile(path, rebootFile);
if (tgtChip == xsc::CHIP_0) {
if (tgtCopy == xsc::COPY_0) {
rebootFile.img00Lock = lock;
} else {
rebootFile.img01Lock = lock;
}
} else {
if (tgtCopy == xsc::COPY_0) {
rebootFile.img10Lock = lock;
} else {
rebootFile.img11Lock = lock;
}
}
rewriteRebootFile(rebootFile);
}
2022-03-13 16:32:40 +01:00
2022-04-07 17:23:50 +02:00
ReturnValue_t CoreController::timeFileHandler() {
// Always set time. We could only set it if it is updated by GPS, but then the backup time would
// become obsolete on GPS problems.
2022-04-17 01:04:53 +02:00
if (opDivider10.check()) {
// It is assumed that the system time is set from the GPS time
timeval currentTime = {};
ReturnValue_t result = Clock::getClock_timeval(&currentTime);
if (result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
std::string fileName = currMntPrefix + TIME_FILE;
std::ofstream timeFile(fileName);
if (not timeFile.good()) {
2022-04-17 01:04:53 +02:00
sif::error << "CoreController::timeFileHandler: Error opening time file: " << strerror(errno)
<< std::endl;
return RETURN_FAILED;
2022-04-07 17:23:50 +02:00
}
timeFile << "UNIX SECONDS: " << currentTime.tv_sec << std::endl;
2022-04-07 17:23:50 +02:00
}
return RETURN_OK;
}
ReturnValue_t CoreController::initClockFromTimeFile() {
using namespace GpsHyperion;
using namespace std;
std::string fileName = currMntPrefix + TIME_FILE;
if (std::filesystem::exists(fileName) and
((gpsFix == FixMode::UNKNOWN or gpsFix == FixMode::NOT_SEEN) or
not utility::timeSanityCheck())) {
ifstream timeFile(fileName);
2022-04-07 17:23:50 +02:00
string nextWord;
getline(timeFile, nextWord);
istringstream iss(nextWord);
iss >> nextWord;
if (iss.bad() or nextWord != "UNIX") {
2022-04-07 17:23:50 +02:00
return RETURN_FAILED;
}
iss >> nextWord;
if (iss.bad() or nextWord != "SECONDS:") {
2022-04-07 17:23:50 +02:00
return RETURN_FAILED;
}
iss >> nextWord;
timeval currentTime = {};
char *checkPtr;
2022-04-07 17:23:50 +02:00
currentTime.tv_sec = strtol(nextWord.c_str(), &checkPtr, 10);
if (iss.bad() or *checkPtr) {
2022-04-07 17:23:50 +02:00
return RETURN_FAILED;
}
2022-04-08 11:40:35 +02:00
#if OBSW_VERBOSE_LEVEL >= 1
time_t timeRaw = currentTime.tv_sec;
std::tm *time = std::gmtime(&timeRaw);
sif::info << "Setting system time from time files: " << std::put_time(time, "%c %Z")
<< std::endl;
#endif
return Clock::setClock(&currentTime);
2022-04-07 17:23:50 +02:00
}
return RETURN_OK;
}
2022-03-13 16:32:40 +01:00
void CoreController::readHkData() {
ReturnValue_t result = RETURN_OK;
result = hkSet.read(TIMEOUT_TYPE, MUTEX_TIMEOUT);
if (result != RETURN_OK) {
return;
}
Xadc xadc;
result = xadc.getTemperature(hkSet.temperature.value);
if (result != RETURN_OK) {
hkSet.temperature.setValid(false);
} else {
hkSet.temperature.setValid(true);
}
result = xadc.getVccPint(hkSet.psVoltage.value);
if (result != RETURN_OK) {
hkSet.psVoltage.setValid(false);
} else {
hkSet.psVoltage.setValid(true);
}
result = xadc.getVccInt(hkSet.plVoltage.value);
if (result != RETURN_OK) {
hkSet.plVoltage.setValid(false);
} else {
hkSet.plVoltage.setValid(true);
}
#if OBSW_PRINT_CORE_HK == 1
hkSet.printSet();
#endif /* OBSW_PRINT_CORE_HK == 1 */
result = hkSet.commit(TIMEOUT_TYPE, MUTEX_TIMEOUT);
if (result != RETURN_OK) {
return;
}
}
2022-04-07 17:23:50 +02:00
bool CoreController::isNumber(const std::string &s) {
return !s.empty() && std::find_if(s.begin(), s.end(),
[](unsigned char c) { return !std::isdigit(c); }) == s.end();
2022-04-07 17:23:50 +02:00
}