eive-obsw/bsp_q7s/core/CoreController.cpp

2617 lines
90 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>
2022-09-26 11:50:35 +02:00
#include <fsfw/filesystem/HasFileSystemIF.h>
#include <fsfw/ipc/QueueFactory.h>
2022-10-25 11:04:24 +02:00
#include <fsfw/tasks/TaskFactory.h>
2023-06-21 19:01:44 +02:00
#include <fsfw_hal/linux/uio/UioMapper.h>
2023-02-08 11:08:38 +01:00
#include "commonConfig.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"
2022-12-02 13:55:19 +01:00
#if OBSW_ADD_TMTC_UDP_SERVER == 1
2021-07-26 11:47:45 +02:00
#include "fsfw/osal/common/UdpTmTcBridge.h"
2022-12-02 13:55:19 +01:00
#endif
#if OBSW_ADD_TMTC_TCP_SERVER == 1
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>
2023-06-21 19:01:44 +02:00
#include "bsp_q7s/boardconfig/busConf.h"
2022-09-16 11:53:33 +02:00
#include "bsp_q7s/fs/SdCardManager.h"
2022-01-17 15:58:27 +01:00
#include "bsp_q7s/memory/scratchApi.h"
2022-03-13 16:32:40 +01:00
#include "bsp_q7s/xadc/Xadc.h"
2022-09-26 11:39:17 +02:00
#include "eive/definitions.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;
2023-04-06 17:49:34 +02:00
CoreController::CoreController(object_id_t objectId, bool enableHkSet)
2023-03-13 18:29:22 +01:00
: ExtendedControllerBase(objectId, 5),
2023-03-26 15:28:00 +02:00
enableHkSet(enableHkSet),
2023-03-13 18:29:22 +01:00
cmdExecutor(4096),
cmdReplyBuf(4096, true),
cmdRepliesSizes(128),
opDivider5(5),
opDivider10(10),
2023-04-12 18:06:04 +02:00
hkSet(this),
paramHelper(this) {
2023-03-12 20:51:33 +01:00
cmdExecutor.setRingBuffer(&cmdReplyBuf, &cmdRepliesSizes);
2022-01-17 15:58:27 +01:00
try {
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);
}
// Set up state of SD card manager and own initial state.
// Stopwatch watch;
sdcMan->updateSdCardStateFile();
SdCardManager::SdStatePair sdStates;
sdcMan->getSdCardsStatus(sdStates);
2023-02-23 23:56:11 +01:00
auto sdCard = sdcMan->getPreferredSdCard();
if (not sdCard.has_value()) {
sif::error << "CoreController::initializeAfterTaskCreation: "
"Issues getting preferred SD card, setting to 0"
<< std::endl;
sdCard = sd::SdCard::SLOT_0;
}
sdInfo.active = sdCard.value();
if (sdStates.first == sd::SdState::MOUNTED) {
sdcMan->setActiveSdCard(sd::SdCard::SLOT_0);
} else if (sdStates.second == sd::SdState::MOUNTED) {
sdcMan->setActiveSdCard(sd::SdCard::SLOT_1);
}
2023-02-23 23:56:11 +01:00
currMntPrefix = sdcMan->getCurrentMountPrefix();
2022-01-17 15:58:27 +01:00
2022-11-09 13:07:20 +01:00
getCurrentBootCopy(CURRENT_CHIP, CURRENT_COPY);
2023-02-24 18:45:04 +01:00
initClockFromTimeFile();
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;
}
2023-02-23 23:56:11 +01:00
// Add script folder to path
char *currentEnvPath = getenv("PATH");
std::string updatedEnvPath = std::string(currentEnvPath) + ":/home/root/scripts:/usr/local/bin";
setenv("PATH", updatedEnvPath.c_str(), true);
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
}
2022-09-27 10:51:07 +02:00
CoreController::~CoreController() {}
2021-05-26 14:16:16 +02:00
ReturnValue_t CoreController::handleCommandMessage(CommandMessage *message) {
2023-04-12 18:06:04 +02:00
ReturnValue_t result = paramHelper.handleParameterMessage(message);
2023-04-12 18:11:41 +02:00
if (result == returnvalue::OK) {
2023-04-12 18:06:04 +02:00
return result;
}
2022-01-17 15:58:27 +01:00
return ExtendedControllerBase::handleCommandMessage(message);
2021-05-26 14:16:16 +02:00
}
void CoreController::performControlOperation() {
2023-02-14 10:59:35 +01:00
#if OBSW_THREAD_TRACING == 1
trace::threadTrace(opCounter, "CORE CTRL");
#endif
2022-04-07 17:23:50 +02:00
EventMessage event;
2022-08-24 17:27:47 +02:00
for (ReturnValue_t result = eventQueue->receiveMessage(&event); result == returnvalue::OK;
2022-04-07 17:23:50 +02:00
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
sdStateMachine();
performMountedSdCardOperations();
2022-03-13 16:32:40 +01:00
readHkData();
2023-06-07 14:20:58 +02:00
if (dumpContext.active) {
dirListingDumpHandler();
}
2023-04-06 17:49:34 +02:00
2023-03-13 18:29:22 +01:00
if (shellCmdIsExecuting) {
2023-03-12 20:51:33 +01:00
bool replyReceived = false;
// TODO: We could read the data in the ring buffer and send it as an action data reply.
2023-03-13 18:29:22 +01:00
if (cmdExecutor.check(replyReceived) == CommandExecutor::EXECUTION_FINISHED) {
2023-04-13 23:24:32 +02:00
actionHelper.finish(true, successRecipient, core::EXECUTE_SHELL_CMD_BLOCKING);
2023-03-12 20:51:33 +01:00
shellCmdIsExecuting = false;
cmdReplyBuf.clear();
2023-03-13 18:29:22 +01:00
while (not cmdRepliesSizes.empty()) {
2023-03-12 20:51:33 +01:00
cmdRepliesSizes.pop();
}
successRecipient = MessageQueueIF::NO_QUEUE;
}
2023-03-13 09:49:35 +01:00
}
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-11-09 13:07:20 +01:00
localDataPoolMap.emplace(core::TEMPERATURE, &tempPoolEntry);
localDataPoolMap.emplace(core::PS_VOLTAGE, &psVoltageEntry);
localDataPoolMap.emplace(core::PL_VOLTAGE, &plVoltageEntry);
2023-04-04 19:18:53 +02:00
poolManager.subscribeForRegularPeriodicPacket({hkSet.getSid(), enableHkSet, 60.0});
2022-08-24 17:27:47 +02:00
return returnvalue::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();
2022-08-24 17:27:47 +02:00
if (result != returnvalue::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);
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
2022-01-17 15:58:27 +01:00
sif::warning << "CoreController::initialize: Setting up alloc failure "
"count failed"
<< std::endl;
}
2023-04-12 18:06:04 +02:00
result = paramHelper.initialize();
2023-04-12 18:11:41 +02:00
if (result != returnvalue::OK) {
2023-04-12 18:06:04 +02:00
return result;
}
2022-01-17 15:58:27 +01:00
sdStateMachine();
2023-04-06 17:49:34 +02:00
triggerEvent(core::REBOOT_SW, CURRENT_CHIP, CURRENT_COPY);
2023-06-22 14:06:34 +02:00
announceCurrentImageInfo();
announceVersionInfo();
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());
2022-08-24 17:27:47 +02:00
if (result != returnvalue::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));
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
2022-04-07 17:23:50 +02:00
sif::warning << "Subscribing for GPS GPS_FIX_CHANGE event failed" << std::endl;
}
2023-06-21 19:01:44 +02:00
if (common::OBSW_VERSION_MAJOR >= 6 or common::OBSW_VERSION_MAJOR == 4) {
UioMapper sysRomMapper(q7s::UIO_SYS_ROM);
result = sysRomMapper.getMappedAdress(&mappedSysRomAddr, UioMapper::Permissions::READ_ONLY);
if (result != returnvalue::OK) {
// TODO: This might be a reason to switch to another image..
sif::error << "Getting mapped SYS ROM UIO address failed" << std::endl;
return ObjectManager::CHILD_INIT_FAILED;
}
2023-06-21 19:01:44 +02:00
}
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
}
2021-08-19 14:05:25 +02:00
ReturnValue_t CoreController::initializeAfterTaskCreation() {
2022-08-24 17:27:47 +02:00
ReturnValue_t result = returnvalue::OK;
2022-01-17 15:58:27 +01:00
if (BLOCKING_SD_INIT) {
2023-02-17 12:19:53 +01:00
result = initSdCardBlocking();
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK and result != SdCardManager::ALREADY_MOUNTED) {
2022-01-17 15:58:27 +01:00
sif::warning << "CoreController::CoreController: SD card init failed" << std::endl;
}
}
sdStateMachine();
performMountedSdCardOperations();
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
2022-01-17 15:58:27 +01:00
sif::warning << "CoreController::initialize: Version initialization failed" << std::endl;
}
updateProtInfo();
2022-11-09 13:07:20 +01:00
return ExtendedControllerBase::initializeAfterTaskCreation();
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) {
2023-04-06 17:49:34 +02:00
using namespace core;
switch (actionId) {
2023-02-08 11:08:38 +01:00
case (ANNOUNCE_VERSION): {
2023-06-22 14:06:34 +02:00
announceVersionInfo();
2023-02-08 11:17:08 +01:00
return HasActionsIF::EXECUTION_FINISHED;
}
2023-03-08 11:37:11 +01:00
case (ANNOUNCE_BOOT_COUNTS): {
announceBootCounts();
return HasActionsIF::EXECUTION_FINISHED;
}
2023-02-08 11:17:08 +01:00
case (ANNOUNCE_CURRENT_IMAGE): {
2023-06-22 14:06:34 +02:00
announceCurrentImageInfo();
2023-02-08 11:08:38 +01:00
return HasActionsIF::EXECUTION_FINISHED;
}
case (LIST_DIRECTORY_INTO_FILE): {
return actionListDirectoryIntoFile(actionId, commandedBy, data, size);
}
2023-04-15 13:01:41 +02:00
case (LIST_DIRECTORY_DUMP_DIRECTLY): {
return actionListDirectoryDumpDirectly(actionId, commandedBy, data, size);
}
case (CP_HELPER): {
CpHelperParser parser(data, size);
ReturnValue_t result = parser.parse();
if (result != returnvalue::OK) {
return result;
}
std::ostringstream oss("cp ", std::ostringstream::ate);
2023-06-06 11:11:16 +02:00
if (parser.isForceOptSet()) {
oss << "-f ";
}
2023-04-15 13:01:41 +02:00
if (parser.isRecursiveOptSet()) {
oss << "-r ";
}
auto &sourceTgt = parser.destTgtPair();
oss << sourceTgt.sourceName << " " << sourceTgt.targetName;
2023-04-15 22:17:50 +02:00
sif::info << "CoreController: Performing copy command: " << oss.str() << std::endl;
2023-04-15 13:01:41 +02:00
int ret = std::system(oss.str().c_str());
if (ret != 0) {
return returnvalue::FAILED;
}
return EXECUTION_FINISHED;
}
case (MV_HELPER): {
MvHelperParser parser(data, size);
ReturnValue_t result = parser.parse();
if (result != returnvalue::OK) {
return result;
}
std::ostringstream oss("mv ", std::ostringstream::ate);
auto &sourceTgt = parser.destTgtPair();
oss << sourceTgt.sourceName << " " << sourceTgt.targetName;
2023-04-15 22:17:50 +02:00
sif::info << "CoreController: Performing move command: " << oss.str() << std::endl;
2023-04-15 13:01:41 +02:00
int ret = std::system(oss.str().c_str());
if (ret != 0) {
return returnvalue::FAILED;
}
return EXECUTION_FINISHED;
}
case (RM_HELPER): {
RmHelperParser parser(data, size);
ReturnValue_t result = parser.parse();
if (result != returnvalue::OK) {
return result;
}
std::ostringstream oss("rm ", std::ostringstream::ate);
if (parser.isRecursiveOptSet() or parser.isForceOptSet()) {
oss << "-";
}
if (parser.isRecursiveOptSet()) {
oss << "r";
}
if (parser.isForceOptSet()) {
oss << "f";
}
size_t removeTargetSize = 0;
const char *removeTgt = parser.getRemoveTarget(removeTargetSize);
oss << " " << removeTgt;
2023-04-15 22:17:50 +02:00
sif::info << "CoreController: Performing remove command: " << oss.str() << std::endl;
int ret = std::system(oss.str().c_str());
if (ret != 0) {
return returnvalue::FAILED;
}
return EXECUTION_FINISHED;
}
case (MKDIR_HELPER): {
if (size < 1) {
return HasActionsIF::INVALID_PARAMETERS;
}
std::string createdDir = std::string(reinterpret_cast<const char *>(data), size);
std::ostringstream oss("mkdir ", std::ostringstream::ate);
oss << createdDir;
sif::info << "CoreController: Performing directory creation: " << oss.str() << std::endl;
2023-04-15 13:01:41 +02:00
int ret = std::system(oss.str().c_str());
if (ret != 0) {
return returnvalue::FAILED;
}
return EXECUTION_FINISHED;
}
case (SWITCH_REBOOT_FILE_HANDLING): {
if (size < 1) {
return HasActionsIF::INVALID_PARAMETERS;
}
2023-06-24 17:14:56 +02:00
std::string path = sdcMan->getCurrentMountPrefix() + REBOOT_WATCHDOG_FILE;
2023-06-24 17:29:23 +02:00
parseRebootWatchdogFile(path, rebootWatchdogFile);
if (data[0] == 0) {
2023-06-24 17:29:23 +02:00
rebootWatchdogFile.enabled = false;
rewriteRebootWatchdogFile(rebootWatchdogFile);
} else if (data[0] == 1) {
2023-06-24 17:29:23 +02:00
rebootWatchdogFile.enabled = true;
rewriteRebootWatchdogFile(rebootWatchdogFile);
} else {
return HasActionsIF::INVALID_PARAMETERS;
}
return HasActionsIF::EXECUTION_FINISHED;
}
2023-06-22 14:25:11 +02:00
case (READ_REBOOT_MECHANISM_INFO): {
2023-06-24 17:14:56 +02:00
std::string path = sdcMan->getCurrentMountPrefix() + REBOOT_WATCHDOG_FILE;
2023-06-24 17:29:23 +02:00
parseRebootWatchdogFile(path, rebootWatchdogFile);
RebootWatchdogPacket packet(rebootWatchdogFile);
2023-06-22 14:25:11 +02:00
ReturnValue_t result = actionHelper.reportData(commandedBy, actionId, &packet);
if (result != returnvalue::OK) {
return result;
}
return HasActionsIF::EXECUTION_FINISHED;
}
2022-03-01 14:44:50 +01:00
case (RESET_REBOOT_COUNTERS): {
if (size == 0) {
2023-06-24 18:06:58 +02:00
resetRebootWatchdogCounters(xsc::ALL_CHIP, xsc::ALL_COPY);
2022-03-01 14:44:50 +01:00
} else if (size == 2) {
if (data[0] > 1 or data[1] > 1) {
return HasActionsIF::INVALID_PARAMETERS;
}
2023-06-24 18:06:58 +02:00
resetRebootWatchdogCounters(static_cast<xsc::Chip>(data[0]),
static_cast<xsc::Copy>(data[1]));
2022-03-01 14:44:50 +01:00
}
return HasActionsIF::EXECUTION_FINISHED;
}
2022-09-26 11:39:17 +02:00
case (OBSW_UPDATE_FROM_SD_0): {
2022-09-26 12:26:08 +02:00
return executeSwUpdate(SwUpdateSources::SD_0, data, size);
}
case (OBSW_UPDATE_FROM_SD_1): {
return executeSwUpdate(SwUpdateSources::SD_1, data, size);
2022-09-26 11:12:41 +02:00
}
case (OBSW_UPDATE_FROM_TMP): {
return executeSwUpdate(SwUpdateSources::TMP_DIR, data, size);
}
2022-09-27 10:51:07 +02:00
case (SWITCH_TO_SD_0): {
2022-09-27 12:19:42 +02:00
if (not startSdStateMachine(sd::SdCard::SLOT_0, SdCfgMode::COLD_REDUNDANT, commandedBy,
actionId)) {
2022-09-27 10:51:07 +02:00
return HasActionsIF::IS_BUSY;
}
2022-09-27 10:51:56 +02:00
// Completion will be reported by SD card state machine
2022-09-27 10:51:07 +02:00
return returnvalue::OK;
}
case (SWITCH_TO_SD_1): {
2022-09-27 12:19:42 +02:00
if (not startSdStateMachine(sd::SdCard::SLOT_1, SdCfgMode::COLD_REDUNDANT, commandedBy,
actionId)) {
2022-09-27 10:51:07 +02:00
return HasActionsIF::IS_BUSY;
}
2022-09-27 10:51:56 +02:00
// Completion will be reported by SD card state machine
2022-09-27 10:51:07 +02:00
return returnvalue::OK;
}
case (SWITCH_TO_BOTH_SD_CARDS): {
// An active SD still needs to be specified because the system needs to know which SD
// card to use for regular operations like telemetry storage.
if (size != 1) {
return HasActionsIF::INVALID_PARAMETERS;
}
2022-09-27 10:55:00 +02:00
if (data[0] != 0 and data[0] != 1) {
2022-09-27 10:51:07 +02:00
return HasActionsIF::INVALID_PARAMETERS;
}
auto active = static_cast<sd::SdCard>(data[0]);
2022-09-27 12:19:42 +02:00
if (not startSdStateMachine(active, SdCfgMode::HOT_REDUNDANT, commandedBy, actionId)) {
2022-09-27 10:51:07 +02:00
return HasActionsIF::IS_BUSY;
}
2022-09-27 10:51:56 +02:00
// Completion will be reported by SD card state machine
2022-09-27 10:51:07 +02:00
return returnvalue::OK;
}
2023-04-13 23:24:32 +02:00
case (SYSTEMCTL_CMD_EXECUTOR): {
// Expect one byte systemctl command type and a unit name with at least one byte as minimum.
if (size < 2) {
return HasActionsIF::INVALID_PARAMETERS;
}
if (data[0] >= core::SystemctlCmd::NUM_CMDS) {
return HasActionsIF::INVALID_PARAMETERS;
}
core::SystemctlCmd cmdType = static_cast<core::SystemctlCmd>(data[0]);
std::string unitName = std::string(reinterpret_cast<const char *>(data + 1), size - 1);
2023-04-14 00:21:28 +02:00
std::ostringstream oss("systemctl ", std::ostringstream::ate);
2023-04-13 23:24:32 +02:00
switch (cmdType) {
case (core::SystemctlCmd::START): {
oss << "start ";
break;
}
case (core::SystemctlCmd::STOP): {
oss << "stop ";
break;
}
case (core::SystemctlCmd::RESTART): {
oss << "restart ";
break;
}
2023-04-14 00:21:28 +02:00
default: {
return HasActionsIF::INVALID_PARAMETERS;
}
2023-04-13 23:24:32 +02:00
}
oss << unitName;
int result = std::system(oss.str().c_str());
if (result != 0) {
return returnvalue::FAILED;
}
return 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;
}
2023-06-24 17:14:56 +02:00
std::string path = sdcMan->getCurrentMountPrefix() + REBOOT_WATCHDOG_FILE;
2022-02-28 16:55:49 +01:00
// Disable the reboot file mechanism
2023-06-24 17:29:23 +02:00
parseRebootWatchdogFile(path, rebootWatchdogFile);
rebootWatchdogFile.maxCount = data[0];
rewriteRebootWatchdogFile(rebootWatchdogFile);
2022-02-28 16:55:49 +01:00
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);
}
2023-04-13 23:24:32 +02:00
case (EXECUTE_SHELL_CMD_BLOCKING): {
2023-04-14 18:31:56 +02:00
std::string cmdToExecute = std::string(reinterpret_cast<const char *>(data), size);
2023-04-14 00:29:21 +02:00
int result = std::system(cmdToExecute.c_str());
2023-04-14 00:21:28 +02:00
if (result != 0) {
2023-04-14 00:29:21 +02:00
// TODO: Data reply with returnalue maybe?
2023-04-14 00:21:28 +02:00
return returnvalue::FAILED;
}
return EXECUTION_FINISHED;
}
case (EXECUTE_SHELL_CMD_NON_BLOCKING): {
2023-04-14 18:31:56 +02:00
std::string cmdToExecute = std::string(reinterpret_cast<const char *>(data), size);
2023-03-13 18:29:22 +01:00
if (cmdExecutor.getCurrentState() == CommandExecutor::States::PENDING or
shellCmdIsExecuting) {
2023-03-12 20:51:33 +01:00
return HasActionsIF::IS_BUSY;
}
2023-04-14 00:29:21 +02:00
cmdExecutor.load(cmdToExecute, false, false);
2023-03-12 20:51:33 +01:00
ReturnValue_t result = cmdExecutor.execute();
2023-03-13 18:29:22 +01:00
if (result != returnvalue::OK) {
2023-03-12 20:51:33 +01:00
return result;
}
shellCmdIsExecuting = true;
successRecipient = commandedBy;
return returnvalue::OK;
}
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) {
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
}
ReturnValue_t CoreController::initSdCardBlocking() {
2022-01-17 15:58:27 +01:00
// Create update status file
ReturnValue_t result = sdcMan->updateSdCardStateFile();
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
2022-01-17 15:58:27 +01:00
sif::warning << "CoreController::initialize: Updating SD card state file failed" << std::endl;
}
2022-09-27 10:51:07 +02:00
if (sdInfo.cfgMode == SdCfgMode::PASSIVE) {
sif::info << "No SD card initialization will be performed" << std::endl;
return returnvalue::OK;
}
2022-05-24 00:20:23 +02:00
result = sdcMan->getSdCardsStatus(sdInfo.currentState);
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
2022-01-17 15:58:27 +01:00
sif::warning << "Getting SD card activity status failed" << std::endl;
}
2022-09-27 10:51:07 +02:00
if (sdInfo.cfgMode == SdCfgMode::COLD_REDUNDANT) {
updateInternalSdInfo();
2022-09-27 10:51:07 +02:00
sif::info << "Cold redundant SD card configuration, preferred SD card: "
<< static_cast<int>(sdInfo.active) << std::endl;
result = sdColdRedundantBlockingInit();
// Update status file
sdcMan->updateSdCardStateFile();
2022-09-27 10:51:07 +02:00
return result;
}
if (sdInfo.cfgMode == SdCfgMode::HOT_REDUNDANT) {
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();
2022-09-27 10:51:07 +02:00
}
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
}
2021-08-08 00:11:21 +02:00
ReturnValue_t CoreController::sdStateMachine() {
2022-08-24 17:27:47 +02:00
ReturnValue_t result = returnvalue::OK;
2022-01-17 15:58:27 +01:00
SdCardManager::Operations operation;
2021-08-06 01:25:36 +02:00
2022-09-27 10:51:07 +02:00
if (sdFsmState == SdStates::IDLE) {
2022-01-17 15:58:27 +01:00
// Nothing to do
return result;
}
2022-09-27 10:51:07 +02:00
if (sdFsmState == SdStates::START) {
2022-01-17 15:58:27 +01:00
// Init will be performed by separate function
if (BLOCKING_SD_INIT) {
2022-09-27 10:51:07 +02:00
sdFsmState = SdStates::IDLE;
2022-01-17 15:58:27 +01:00
sdInfo.initFinished = true;
return result;
} else {
// Still update SD state file
2022-09-27 10:51:07 +02:00
if (sdInfo.cfgMode == SdCfgMode::PASSIVE) {
sdFsmState = SdStates::UPDATE_SD_INFO_END;
2022-09-27 10:51:07 +02:00
} else {
sdInfo.cycleCount = 0;
sdInfo.commandPending = false;
sdFsmState = SdStates::UPDATE_SD_INFO_START;
2022-09-27 10:51:07 +02:00
}
}
2022-01-17 15:58:27 +01:00
}
// This lambda checks the non-blocking operation of the SD card manager and assigns the new
// state on success. It returns true for an operation success and false otherwise
auto nonBlockingSdcOpChecking = [&](SdStates newStateOnSuccess, uint16_t maxCycleCount,
std::string opPrintout) {
2022-01-17 15:58:27 +01:00
SdCardManager::OpStatus status = sdcMan->checkCurrentOp(operation);
if (status == SdCardManager::OpStatus::SUCCESS) {
2022-09-27 10:51:07 +02:00
sdFsmState = newStateOnSuccess;
sdInfo.commandPending = false;
2022-01-17 15:58:27 +01:00
sdInfo.cycleCount = 0;
return true;
} else if (sdInfo.cycleCount > 4) {
sif::warning << "CoreController::sdStateMachine: " << opPrintout << " takes too long"
2022-01-17 15:58:27 +01:00
<< std::endl;
return false;
}
return false;
};
if (sdFsmState == SdStates::UPDATE_SD_INFO_START) {
if (not sdInfo.commandPending) {
2022-09-27 10:51:07 +02:00
// Create updated status file
result = sdcMan->updateSdCardStateFile();
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
sif::warning << "CoreController::sdStateMachine: Updating SD card state file failed"
2022-01-17 15:58:27 +01:00
<< std::endl;
}
2022-05-24 00:20:23 +02:00
result = sdcMan->getSdCardsStatus(sdInfo.currentState);
updateInternalSdInfo();
auto currentlyActiveSdc = sdcMan->getActiveSdCard();
// Used/active SD card switches, so mark SD card unusable so other tasks have some time
// registering the unavailable SD card.
if (not currentlyActiveSdc.has_value() or
((currentlyActiveSdc.value() == sd::SdCard::SLOT_0) and
(sdInfo.active == sd::SdCard::SLOT_1)) or
((currentlyActiveSdc.value() == sd::SdCard::SLOT_1) and
(sdInfo.active == sd::SdCard::SLOT_0))) {
sdInfo.lockSdCardUsage = true;
}
if (sdInfo.lockSdCardUsage) {
sdcMan->markUnusable();
}
2022-09-27 10:51:07 +02:00
if (sdInfo.active != sd::SdCard::SLOT_0 and sdInfo.active != sd::SdCard::SLOT_1) {
2022-01-17 15:58:27 +01:00
sif::warning << "Preferred SD card invalid. Setting to card 0.." << std::endl;
2022-09-27 10:51:07 +02:00
sdInfo.active = sd::SdCard::SLOT_0;
2022-01-17 15:58:27 +01:00
}
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
2022-01-17 15:58:27 +01:00
sif::warning << "Getting SD card activity status failed" << std::endl;
}
2022-09-27 10:51:07 +02:00
if (sdInfo.cfgMode == SdCfgMode::COLD_REDUNDANT) {
2022-09-27 12:19:42 +02:00
sif::info << "Cold redundant SD card configuration, target SD card: "
2022-09-27 10:51:07 +02:00
<< static_cast<int>(sdInfo.active) << std::endl;
}
SdStates tgtState = SdStates::IDLE;
bool skipCycles = sdInfo.lockSdCardUsage;
// Need to do different things depending on state of SD card which will be active.
2022-09-27 10:51:07 +02:00
if (sdInfo.activeState == sd::SdState::MOUNTED) {
// Already mounted, so we can perform handling of the other side.
2021-08-09 11:00:58 +02:00
#if OBSW_VERBOSE_LEVEL >= 1
2022-01-17 15:58:27 +01:00
std::string mountString;
2022-09-27 10:51:07 +02:00
if (sdInfo.active == sd::SdCard::SLOT_0) {
2022-09-26 11:39:17 +02:00
mountString = config::SD_0_MOUNT_POINT;
2022-01-17 15:58:27 +01:00
} else {
2022-09-26 11:39:17 +02:00
mountString = config::SD_1_MOUNT_POINT;
2022-01-17 15:58:27 +01:00
}
2022-09-27 10:51:07 +02:00
sif::info << "SD card " << sdInfo.activeChar << " already on and mounted at " << mountString
2022-01-17 15:58:27 +01:00
<< std::endl;
2021-08-09 11:00:58 +02:00
#endif
2022-09-27 10:51:07 +02:00
sdcMan->setActiveSdCard(sdInfo.active);
currMntPrefix = sdcMan->getCurrentMountPrefix();
tgtState = SdStates::DETERMINE_OTHER;
2022-09-27 10:51:07 +02:00
} else if (sdInfo.activeState == sd::SdState::OFF) {
// It's okay to do the delay after swichting active SD on, no one can use it anyway..
2022-09-27 10:51:07 +02:00
sdCardSetup(sdInfo.active, sd::SdState::ON, sdInfo.activeChar, false);
sdInfo.commandPending = true;
// Do not skip cycles here, would mess up the state machine. We skip the cycles after
// the SD card was switched on.
skipCycles = false;
2023-04-08 17:46:54 +02:00
// Remain on the current state.
tgtState = sdFsmState;
2022-09-27 10:51:07 +02:00
} else if (sdInfo.activeState == sd::SdState::ON) {
// We can do the delay before mounting where applicable.
tgtState = SdStates::MOUNT_SELF;
}
if (skipCycles) {
sdFsmState = SdStates::SKIP_TWO_CYCLES_IF_SD_LOCKED;
fsmStateAfterDelay = tgtState;
sdInfo.skippedCyclesCount = 0;
} else {
sdFsmState = tgtState;
2022-01-17 15:58:27 +01:00
}
} else {
if (nonBlockingSdcOpChecking(SdStates::MOUNT_SELF, 10, "Setting SDC state")) {
2022-09-27 10:51:07 +02:00
sdInfo.activeState = sd::SdState::ON;
currentStateSetter(sdInfo.active, sd::SdState::ON);
// Skip the two cycles now.
if (sdInfo.lockSdCardUsage) {
sdFsmState = SdStates::SKIP_TWO_CYCLES_IF_SD_LOCKED;
fsmStateAfterDelay = SdStates::MOUNT_SELF;
sdInfo.skippedCyclesCount = 0;
}
2022-01-17 15:58:27 +01:00
}
}
}
if (sdFsmState == SdStates::SKIP_TWO_CYCLES_IF_SD_LOCKED) {
sdInfo.skippedCyclesCount++;
// Count to three because this branch will run in the same FSM cycle.
if (sdInfo.skippedCyclesCount == 3) {
sdFsmState = fsmStateAfterDelay;
fsmStateAfterDelay = SdStates::IDLE;
sdInfo.skippedCyclesCount = 0;
}
}
2022-09-27 10:51:07 +02:00
if (sdFsmState == SdStates::MOUNT_SELF) {
if (not sdInfo.commandPending) {
2022-09-27 10:51:07 +02:00
result = sdCardSetup(sdInfo.active, sd::SdState::MOUNTED, sdInfo.activeChar);
sdInfo.commandPending = true;
2022-01-17 15:58:27 +01:00
} else {
if (nonBlockingSdcOpChecking(SdStates::DETERMINE_OTHER, 5, "Mounting SD card")) {
2022-09-27 10:51:07 +02:00
sdcMan->setActiveSdCard(sdInfo.active);
currMntPrefix = sdcMan->getCurrentMountPrefix();
2022-09-27 10:51:07 +02:00
sdInfo.activeState = sd::SdState::MOUNTED;
currentStateSetter(sdInfo.active, sd::SdState::MOUNTED);
2022-01-17 15:58:27 +01:00
}
}
}
2022-09-27 10:51:07 +02:00
if (sdFsmState == SdStates::DETERMINE_OTHER) {
2022-01-17 15:58:27 +01:00
// 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
2022-09-27 10:51:07 +02:00
if (sdInfo.cfgMode == SdCfgMode::COLD_REDUNDANT) {
if (sdInfo.otherState == sd::SdState::ON) {
sdFsmState = SdStates::SET_STATE_OTHER;
} else if (sdInfo.otherState == sd::SdState::MOUNTED) {
sdFsmState = SdStates::MOUNT_UNMOUNT_OTHER;
} else {
// Is already off, update info, but with a small delay
sdFsmState = SdStates::SKIP_CYCLE_BEFORE_INFO_UPDATE;
}
} else if (sdInfo.cfgMode == SdCfgMode::HOT_REDUNDANT) {
if (sdInfo.otherState == sd::SdState::OFF) {
sdFsmState = SdStates::SET_STATE_OTHER;
} else if (sdInfo.otherState == sd::SdState::ON) {
sdFsmState = SdStates::MOUNT_UNMOUNT_OTHER;
} else {
// Is already on and mounted, update info
sdFsmState = SdStates::SKIP_CYCLE_BEFORE_INFO_UPDATE;
}
2021-08-06 01:25:36 +02:00
}
2022-01-17 15:58:27 +01:00
}
2021-08-06 01:25:36 +02:00
2022-09-27 10:51:07 +02:00
if (sdFsmState == SdStates::SET_STATE_OTHER) {
2022-01-17 15:58:27 +01:00
// Set state of other SD card to ON or OFF, depending on redundancy mode
2022-09-27 10:51:07 +02:00
if (sdInfo.cfgMode == SdCfgMode::COLD_REDUNDANT) {
if (not sdInfo.commandPending) {
2022-09-27 10:51:07 +02:00
result = sdCardSetup(sdInfo.other, sd::SdState::OFF, sdInfo.otherChar, false);
sdInfo.commandPending = true;
2022-09-27 10:51:07 +02:00
} else {
if (nonBlockingSdcOpChecking(SdStates::SKIP_CYCLE_BEFORE_INFO_UPDATE, 10,
"Switching off other SD card")) {
2022-09-27 10:51:07 +02:00
sdInfo.otherState = sd::SdState::OFF;
currentStateSetter(sdInfo.other, sd::SdState::OFF);
2023-04-08 17:46:54 +02:00
} else {
// Continue.. avoid being stuck here..
sdFsmState = SdStates::SKIP_CYCLE_BEFORE_INFO_UPDATE;
sdInfo.otherState = sd::SdState::OFF;
currentStateSetter(sdInfo.other, sd::SdState::OFF);
2022-09-27 10:51:07 +02:00
}
2022-01-17 15:58:27 +01:00
}
2022-09-27 10:51:07 +02:00
} else if (sdInfo.cfgMode == SdCfgMode::HOT_REDUNDANT) {
if (not sdInfo.commandPending) {
2022-09-27 10:51:07 +02:00
result = sdCardSetup(sdInfo.other, sd::SdState::ON, sdInfo.otherChar, false);
sdInfo.commandPending = true;
2022-09-27 10:51:07 +02:00
} else {
if (nonBlockingSdcOpChecking(SdStates::MOUNT_UNMOUNT_OTHER, 10,
"Switching on other SD card")) {
2022-09-27 10:51:07 +02:00
sdInfo.otherState = sd::SdState::ON;
currentStateSetter(sdInfo.other, sd::SdState::ON);
2023-04-08 17:46:54 +02:00
} else {
// Contnue.. avoid being stuck here.
sdFsmState = SdStates::MOUNT_UNMOUNT_OTHER;
sdInfo.otherState = sd::SdState::ON;
currentStateSetter(sdInfo.other, sd::SdState::ON);
2022-09-27 10:51:07 +02:00
}
2022-01-17 15:58:27 +01:00
}
}
2022-01-17 15:58:27 +01:00
}
2022-09-27 10:51:07 +02:00
if (sdFsmState == SdStates::MOUNT_UNMOUNT_OTHER) {
2022-01-17 15:58:27 +01:00
// Mount or unmount other SD card, depending on redundancy mode
2022-09-27 10:51:07 +02:00
if (sdInfo.cfgMode == SdCfgMode::COLD_REDUNDANT) {
if (not sdInfo.commandPending) {
2022-09-27 10:51:07 +02:00
result = sdCardSetup(sdInfo.other, sd::SdState::ON, sdInfo.otherChar);
sdInfo.commandPending = true;
2022-09-27 10:51:07 +02:00
} else {
if (nonBlockingSdcOpChecking(SdStates::SET_STATE_OTHER, 10, "Unmounting other SD card")) {
2022-09-27 10:51:07 +02:00
sdInfo.otherState = sd::SdState::ON;
currentStateSetter(sdInfo.other, sd::SdState::ON);
} else {
sdInfo.otherState = sd::SdState::ON;
currentStateSetter(sdInfo.other, sd::SdState::ON);
sdFsmState = SdStates::SET_STATE_OTHER;
2022-09-27 10:51:07 +02:00
}
2022-01-17 15:58:27 +01:00
}
2022-09-27 10:51:07 +02:00
} else if (sdInfo.cfgMode == SdCfgMode::HOT_REDUNDANT) {
if (not sdInfo.commandPending) {
2022-09-27 10:51:07 +02:00
result = sdCardSetup(sdInfo.other, sd::SdState::MOUNTED, sdInfo.otherChar);
sdInfo.commandPending = true;
2022-09-27 10:51:07 +02:00
} else {
if (nonBlockingSdcOpChecking(SdStates::UPDATE_SD_INFO_END, 4, "Mounting other SD card")) {
2022-09-27 10:51:07 +02:00
sdInfo.otherState = sd::SdState::MOUNTED;
currentStateSetter(sdInfo.other, sd::SdState::MOUNTED);
}
2022-01-17 15:58:27 +01:00
}
}
2022-01-17 15:58:27 +01:00
}
2022-09-27 10:51:07 +02:00
if (sdFsmState == SdStates::SKIP_CYCLE_BEFORE_INFO_UPDATE) {
sdFsmState = SdStates::UPDATE_SD_INFO_END;
} else if (sdFsmState == SdStates::UPDATE_SD_INFO_END) {
2022-01-17 15:58:27 +01:00
// Update status file
result = sdcMan->updateSdCardStateFile();
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
2023-04-08 11:53:44 +02:00
sif::warning << "CoreController: Updating SD card state file failed" << std::endl;
2022-01-17 15:58:27 +01:00
}
updateInternalSdInfo();
// Mark usable again in any case.
sdcMan->markUsable();
sdInfo.commandPending = false;
2022-09-27 10:51:07 +02:00
sdFsmState = SdStates::IDLE;
2022-01-17 15:58:27 +01:00
sdInfo.cycleCount = 0;
sdcMan->setBlocking(false);
2022-05-24 00:20:23 +02:00
sdcMan->getSdCardsStatus(sdInfo.currentState);
2022-09-27 10:51:07 +02:00
if (sdCommandingInfo.cmdPending) {
sdCommandingInfo.cmdPending = false;
actionHelper.finish(true, sdCommandingInfo.commander, sdCommandingInfo.actionId,
returnvalue::OK);
}
const char *modeStr = "UNKNOWN";
if (sdInfo.cfgMode == SdCfgMode::COLD_REDUNDANT) {
modeStr = "COLD REDUNDANT";
} else if (sdInfo.cfgMode == SdCfgMode::HOT_REDUNDANT) {
modeStr = "HOT REDUNDANT";
}
sif::info << "SD card update into " << modeStr
<< " mode finished. Active SD: " << sdInfo.activeChar << std::endl;
2022-01-17 15:58:27 +01:00
if (not sdInfo.initFinished) {
updateInternalSdInfo();
2022-01-17 15:58:27 +01:00
sdInfo.initFinished = true;
sif::info << "SD card initialization finished" << std::endl;
}
}
sdInfo.cycleCount++;
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
}
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) {
2022-09-26 11:39:17 +02:00
mountString = config::SD_0_MOUNT_POINT;
2022-01-17 15:58:27 +01:00
} else {
2022-09-26 11:39:17 +02:00
mountString = config::SD_1_MOUNT_POINT;
2022-01-17 15:58:27 +01:00
}
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 {
2023-03-08 14:50:25 +01:00
std::error_code e;
if (std::filesystem::exists(mountString, e)) {
2022-01-17 15:58:27 +01:00
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;
}
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
}
2021-08-06 01:25:36 +02:00
ReturnValue_t CoreController::sdColdRedundantBlockingInit() {
2022-08-24 17:27:47 +02:00
ReturnValue_t result = returnvalue::OK;
2022-01-17 15:58:27 +01:00
2022-09-27 10:51:07 +02:00
result = sdCardSetup(sdInfo.active, sd::SdState::MOUNTED, sdInfo.activeChar);
2022-08-24 17:27:47 +02:00
if (result != SdCardManager::ALREADY_MOUNTED and result != returnvalue::OK) {
2022-01-17 15:58:27 +01:00
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
2022-09-27 10:51:07 +02:00
sdCardSetup(sdInfo.active, sd::SdState::MOUNTED, sdInfo.activeChar);
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::FAILED and sdInfo.otherState != sd::SdState::OFF) {
2022-01-17 15:58:27 +01:00
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);
2022-08-24 17:27:47 +02:00
if (result2 != returnvalue::OK and result2 != SdCardManager::ALREADY_OFF) {
2022-01-17 15:58:27 +01:00
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);
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
2022-01-17 15:58:27 +01:00
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;
}
2023-02-08 11:08:38 +01:00
std::string fullObswVersionString = "OBSW: v" + std::to_string(common::OBSW_VERSION_MAJOR) + "." +
std::to_string(common::OBSW_VERSION_MINOR) + "." +
std::to_string(common::OBSW_VERSION_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;
2023-03-08 14:50:25 +01:00
std::error_code e;
if (not std::filesystem::exists(versionFilePath, e)) {
2022-01-17 15:58:27 +01:00
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;
2022-08-24 17:27:47 +02:00
return returnvalue::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;
}
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
}
2021-07-26 11:47:45 +02:00
2023-04-15 13:01:41 +02:00
ReturnValue_t CoreController::actionListDirectoryDumpDirectly(ActionId_t actionId,
MessageQueueId_t commandedBy,
const uint8_t *data, size_t size) {
core::ListDirectoryCmdBase parser(data, size);
ReturnValue_t result = parser.parse();
if (result != returnvalue::OK) {
return result;
}
std::ostringstream oss("ls -l", std::ostringstream::ate);
if (parser.aFlagSet()) {
oss << "a";
}
if (parser.rFlagSet()) {
oss << "R";
}
size_t repoNameLen = 0;
const char *repoName = parser.getRepoName(repoNameLen);
oss << " " << repoName << " > " << LIST_DIR_DUMP_WORK_FILE;
2023-04-15 23:11:00 +02:00
sif::info << "Executing " << oss.str() << " for direct dump";
if (parser.compressionOptionSet()) {
sif::info << " with compression";
}
sif::info << std::endl;
2023-04-15 13:01:41 +02:00
int ret = std::system(oss.str().c_str());
if (ret != 0) {
utility::handleSystemError(result, "CoreController::actionListDirectoryDumpDirectly");
return returnvalue::FAILED;
}
if (parser.compressionOptionSet()) {
2023-04-15 22:17:50 +02:00
std::string compressedName = LIST_DIR_DUMP_WORK_FILE + std::string(".gz");
2023-04-15 13:01:41 +02:00
oss.str("");
2023-04-15 22:17:50 +02:00
oss << "gzip " << LIST_DIR_DUMP_WORK_FILE;
2023-04-15 13:01:41 +02:00
ret = std::system(oss.str().c_str());
if (ret != 0) {
utility::handleSystemError(result, "CoreController::actionListDirectoryDumpDirectly");
return returnvalue::FAILED;
}
oss.str("");
2023-04-15 22:17:50 +02:00
// Overwrite the work file with the compressed archive.
oss << "mv " << compressedName << " " << LIST_DIR_DUMP_WORK_FILE;
2023-04-15 13:01:41 +02:00
ret = std::system(oss.str().c_str());
if (ret != 0) {
utility::handleSystemError(result, "CoreController::actionListDirectoryDumpDirectly");
return returnvalue::FAILED;
}
}
2023-04-16 02:56:46 +02:00
dirListingBuf[8] = parser.compressionOptionSet();
// First four bytes reserved for segment index. One byte for compression option information
std::strcpy(reinterpret_cast<char *>(dirListingBuf.data() + 2 * sizeof(uint32_t) + 1), repoName);
2023-04-15 13:06:16 +02:00
std::ifstream ifile(LIST_DIR_DUMP_WORK_FILE, std::ios::binary);
if (ifile.bad()) {
return returnvalue::FAILED;
}
2023-04-15 13:01:41 +02:00
std::error_code e;
2023-06-07 14:20:58 +02:00
dumpContext.totalFileSize = std::filesystem::file_size(LIST_DIR_DUMP_WORK_FILE, e);
dumpContext.segmentIdx = 0;
dumpContext.dumpedBytes = 0;
2023-04-15 13:01:41 +02:00
size_t nextDumpLen = 0;
size_t dummy = 0;
2023-06-07 14:20:58 +02:00
dumpContext.maxDumpLen = dirListingBuf.size() - 2 * sizeof(uint32_t) - 1 - repoNameLen - 1;
dumpContext.listingDataOffset = 2 * sizeof(uint32_t) + 1 + repoNameLen + 1;
uint32_t chunks = dumpContext.totalFileSize / dumpContext.maxDumpLen;
if (dumpContext.totalFileSize % dumpContext.maxDumpLen != 0) {
2023-04-16 02:56:46 +02:00
chunks++;
}
SerializeAdapter::serialize(&chunks, dirListingBuf.data() + sizeof(uint32_t), &dummy,
dirListingBuf.size() - sizeof(uint32_t),
SerializeIF::Endianness::NETWORK);
2023-06-07 14:20:58 +02:00
while (dumpContext.dumpedBytes < dumpContext.totalFileSize) {
ifile.seekg(dumpContext.dumpedBytes, std::ios::beg);
nextDumpLen = dumpContext.maxDumpLen;
if (dumpContext.totalFileSize - dumpContext.dumpedBytes < dumpContext.maxDumpLen) {
nextDumpLen = dumpContext.totalFileSize - dumpContext.dumpedBytes;
}
SerializeAdapter::serialize(&dumpContext.segmentIdx, dirListingBuf.data(), &dummy,
dirListingBuf.size(), SerializeIF::Endianness::NETWORK);
ifile.read(reinterpret_cast<char *>(dirListingBuf.data() + dumpContext.listingDataOffset),
nextDumpLen);
2023-04-16 02:56:46 +02:00
result = actionHelper.reportData(commandedBy, actionId, dirListingBuf.data(),
2023-06-07 14:20:58 +02:00
dumpContext.listingDataOffset + nextDumpLen);
2023-04-15 13:01:41 +02:00
if (result != returnvalue::OK) {
// Remove work file when we are done
std::filesystem::remove(LIST_DIR_DUMP_WORK_FILE, e);
return result;
}
2023-06-07 14:20:58 +02:00
dumpContext.segmentIdx++;
dumpContext.dumpedBytes += nextDumpLen;
// Dump takes multiple task cycles, so cache the dump state and continue dump the next cycles.
2023-06-07 16:21:51 +02:00
if (dumpContext.segmentIdx == 10) {
2023-06-07 14:20:58 +02:00
dumpContext.active = true;
2023-06-07 16:21:51 +02:00
dumpContext.firstDump = true;
2023-06-07 14:20:58 +02:00
dumpContext.commander = commandedBy;
dumpContext.actionId = actionId;
return returnvalue::OK;
}
2023-04-15 13:01:41 +02:00
}
// Remove work file when we are done
std::filesystem::remove(LIST_DIR_DUMP_WORK_FILE, e);
2023-04-15 23:00:34 +02:00
return EXECUTION_FINISHED;
2023-04-15 13:01:41 +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) {
2023-04-15 13:01:41 +02:00
core::ListDirectoryIntoFile parser(data, size);
ReturnValue_t result = parser.parse();
if (result != returnvalue::OK) {
return result;
}
std::ostringstream oss("ls -l", std::ostringstream::ate);
if (parser.aFlagSet()) {
2022-01-17 15:58:27 +01:00
oss << "a";
}
2023-04-15 13:01:41 +02:00
if (parser.rFlagSet()) {
2022-01-17 15:58:27 +01:00
oss << "R";
}
2023-04-15 13:01:41 +02:00
size_t repoNameLen = 0;
const char *repoName = parser.getRepoName(repoNameLen);
size_t targetFileNameLen = 0;
const char *targetFileName = parser.getTargetName(targetFileNameLen);
2022-01-17 15:58:27 +01:00
oss << " " << repoName << " > " << targetFileName;
2023-04-15 13:01:41 +02:00
sif::info << "Executing list directory request, command: " << oss.str() << std::endl;
int ret = std::system(oss.str().c_str());
if (ret != 0) {
2022-01-17 15:58:27 +01:00
utility::handleSystemError(result, "CoreController::actionListDirectoryIntoFile");
2023-04-15 13:01:41 +02:00
return returnvalue::FAILED;
}
2023-04-15 23:11:00 +02:00
// Compression will add a .gz ending. I don't have any issue with this, it makes it explicit
// that this is a compressed file.
2023-04-15 13:01:41 +02:00
if (parser.compressionOptionSet()) {
oss.str("");
2023-04-15 23:11:00 +02:00
oss << "gzip " << targetFileName;
2023-04-15 13:01:41 +02:00
sif::info << "Compressing directory listing: " << oss.str() << std::endl;
ret = std::system(oss.str().c_str());
if (ret != 0) {
utility::handleSystemError(result, "CoreController::actionListDirectoryIntoFile");
return returnvalue::FAILED;
}
2022-01-17 15:58:27 +01:00
}
2023-04-15 23:00:34 +02:00
return EXECUTION_FINISHED;
}
2022-11-09 13:07:20 +01:00
ReturnValue_t CoreController::initBootCopyFile() {
2023-03-08 14:50:25 +01:00
std::error_code e;
if (not std::filesystem::exists(CURR_COPY_FILE, e)) {
// 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");
}
}
2022-08-24 17:27:47 +02:00
return returnvalue::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);
}
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");
2022-08-24 17:27:47 +02:00
return returnvalue::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]);
2023-06-24 19:22:25 +02:00
performGracefulShutdown(tgtChip, tgtCopy);
2022-08-24 17:27:47 +02:00
return returnvalue::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");
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2022-04-29 11:16:53 +02:00
}
ReturnValue_t CoreController::gracefulShutdownTasks(xsc::Chip chip, xsc::Copy copy,
bool &protOpPerformed) {
2023-06-25 12:47:28 +02:00
// Store both sequence counters persistently.
core::SAVE_CFDP_SEQUENCE_COUNT = true;
core::SAVE_PUS_SEQUENCE_COUNT = true;
2022-05-24 01:14:04 +02:00
sdcMan->setBlocking(true);
2022-10-25 11:04:24 +02:00
sdcMan->markUnusable();
// Wait two seconds to ensure no one uses the SD cards
TaskFactory::delayTask(2000);
2023-06-24 19:55:56 +02:00
// Ensure that all writes/reads do finish.
sync();
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);
2023-06-24 19:22:25 +02:00
// If any boot copies are unprotected.
// Actually this function only ensures that reboots to the own image are protected..
2022-04-29 11:16:53 +02:00
ReturnValue_t result = setBootCopyProtection(xsc::Chip::SELF_CHIP, xsc::Copy::SELF_COPY, true,
protOpPerformed, false);
2022-08-24 17:27:47 +02:00
if (result == returnvalue::OK and protOpPerformed) {
2022-04-29 11:16:53 +02:00
// 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;
}
void CoreController::updateInternalSdInfo() {
2022-09-27 10:51:07 +02:00
if (sdInfo.active == sd::SdCard::SLOT_0) {
sdInfo.activeChar = "0";
2022-01-17 15:58:27 +01:00
sdInfo.otherChar = "1";
sdInfo.otherState = sdInfo.currentState.second;
2022-09-27 10:51:07 +02:00
sdInfo.activeState = sdInfo.currentState.first;
2022-01-17 15:58:27 +01:00
sdInfo.other = sd::SdCard::SLOT_1;
2022-09-27 10:51:07 +02:00
} else if (sdInfo.active == sd::SdCard::SLOT_1) {
sdInfo.activeChar = "1";
2022-01-17 15:58:27 +01:00
sdInfo.otherChar = "0";
sdInfo.otherState = sdInfo.currentState.first;
2022-09-27 10:51:07 +02:00
sdInfo.activeState = sdInfo.currentState.second;
2022-01-17 15:58:27 +01:00
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");
2022-08-24 17:27:47 +02:00
return returnvalue::FAILED;
2022-01-17 15:58:27 +01:00
}
2022-08-24 17:27:47 +02:00
return returnvalue::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-08-24 17:27:47 +02:00
return returnvalue::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-08-24 17:27:47 +02:00
return returnvalue::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();
}
2022-08-24 17:27:47 +02:00
return returnvalue::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;
2022-08-24 17:27:47 +02:00
ReturnValue_t result = returnvalue::OK;
2022-01-17 15:58:27 +01:00
if (regenerateChipStateFile) {
result = generateChipStateFile();
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
2022-01-17 15:58:27 +01:00
sif::warning << "CoreController::updateProtInfo: Generating chip state file failed"
<< std::endl;
return result;
}
}
2023-03-08 14:50:25 +01:00
std::error_code e;
if (not filesystem::exists(CHIP_STATE_FILE, e)) {
2022-08-24 17:27:47 +02:00
return returnvalue::FAILED;
2022-01-17 15:58:27 +01:00
}
ifstream chipStateFile(CHIP_STATE_FILE);
if (not chipStateFile.good()) {
2022-08-24 17:27:47 +02:00
return returnvalue::FAILED;
2022-01-17 15:58:27 +01:00
}
string nextLine;
uint8_t lineCounter = 0;
string word;
while (getline(chipStateFile, nextLine)) {
2023-02-17 12:19:53 +01:00
result = handleProtInfoUpdateLine(nextLine);
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
2022-01-17 15:58:27 +01:00
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;
}
}
2022-08-24 17:27:47 +02:00
return returnvalue::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++;
2023-03-01 18:06:36 +01:00
if (wordIdx >= 10) {
break;
}
2022-01-17 15:58:27 +01:00
}
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
}
2022-03-01 14:44:50 +01:00
void CoreController::performMountedSdCardOperations() {
2022-10-11 22:58:31 +02:00
auto mountedSdCardOp = [&](sd::SdCard sdCard, std::string mntPoint) {
if (not performOneShotSdCardOpsSwitch) {
std::ostringstream path;
2023-04-13 23:34:11 +02:00
path << mntPoint << "/" << core::CONF_FOLDER;
2023-03-08 14:50:25 +01:00
std::error_code e;
if (not std::filesystem::exists(path.str()), e) {
bool created = std::filesystem::create_directory(path.str(), e);
if (not created) {
2023-03-08 14:59:50 +01:00
sif::error << "Could not create CONF folder at " << path.str() << ": " << e.message()
<< std::endl;
2023-03-08 14:50:25 +01:00
return;
}
2022-05-24 00:20:23 +02:00
}
2022-10-11 22:58:31 +02:00
initVersionFile();
2022-11-09 13:07:20 +01:00
ReturnValue_t result = initBootCopyFile();
if (result != returnvalue::OK) {
sif::warning << "CoreController::CoreController: Boot copy init" << std::endl;
}
2023-02-24 18:45:04 +01:00
if (not timeFileInitDone) {
initClockFromTimeFile();
}
2023-06-24 17:29:23 +02:00
performRebootWatchdogHandling(false);
performRebootCountersHandling(false);
2022-05-24 00:20:23 +02:00
}
2023-02-24 18:45:04 +01:00
backupTimeFileHandler();
2022-05-24 00:20:23 +02:00
};
2022-10-14 15:33:34 +02:00
bool someSdCardActive = false;
2022-10-11 22:58:31 +02:00
if (sdInfo.active == sd::SdCard::SLOT_0 and sdcMan->isSdCardUsable(sd::SdCard::SLOT_0)) {
mountedSdCardOp(sd::SdCard::SLOT_0, config::SD_0_MOUNT_POINT);
2022-10-14 15:33:34 +02:00
someSdCardActive = true;
2022-10-11 22:58:31 +02:00
}
if (sdInfo.active == sd::SdCard::SLOT_1 and sdcMan->isSdCardUsable(sd::SdCard::SLOT_1)) {
mountedSdCardOp(sd::SdCard::SLOT_1, config::SD_1_MOUNT_POINT);
2022-10-14 15:33:34 +02:00
someSdCardActive = true;
2022-10-11 22:58:31 +02:00
}
2022-10-14 15:33:34 +02:00
if (someSdCardActive) {
2022-10-11 22:58:31 +02:00
performOneShotSdCardOpsSwitch = true;
2022-05-24 00:20:23 +02:00
}
}
ReturnValue_t CoreController::performSdCardCheck() {
bool mountedReadOnly = false;
SdCardManager::SdStatePair active;
sdcMan->getSdCardsStatus(active);
2022-09-27 10:51:07 +02:00
if (sdFsmState != SdStates::IDLE) {
return returnvalue::OK;
}
2022-05-24 00:20:23 +02:00
auto sdCardCheck = [&](sd::SdCard sdCard) {
ReturnValue_t result = sdcMan->isSdCardMountedReadOnly(sdCard, mountedReadOnly);
2022-08-24 17:27:47 +02:00
if (result != returnvalue::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
}
if (mountedReadOnly) {
int linuxErrno = 0;
2022-05-24 00:20:23 +02:00
result = sdcMan->performFsck(sdCard, true, linuxErrno);
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
2022-05-24 00:20:23 +02:00
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);
2022-08-24 17:27:47 +02:00
if (result == returnvalue::OK) {
2022-05-24 00:20:23 +02:00
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);
}
2022-10-14 15:33:34 +02:00
#if OBSW_SD_CARD_MUST_BE_ON == 1
// This is FDIR. The core controller will attempt once to get some SD card working
bool someSdCardActive = false;
if ((sdInfo.active == sd::SdCard::SLOT_0 and sdcMan->isSdCardUsable(sd::SdCard::SLOT_0)) or
(sdInfo.active == sd::SdCard::SLOT_1 and sdcMan->isSdCardUsable(sd::SdCard::SLOT_1))) {
someSdCardActive = true;
}
if (not someSdCardActive and remountAttemptFlag) {
2023-04-06 17:49:34 +02:00
triggerEvent(core::NO_SD_CARD_ACTIVE);
2022-10-14 15:33:34 +02:00
initSdCardBlocking();
remountAttemptFlag = false;
}
#endif
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2022-03-01 14:44:50 +01:00
}
2023-06-24 17:29:23 +02:00
void CoreController::performRebootWatchdogHandling(bool recreateFile) {
using namespace std;
2023-06-24 17:14:56 +02:00
std::string path = currMntPrefix + REBOOT_WATCHDOG_FILE;
std::string legacyPath = currMntPrefix + LEGACY_REBOOT_WATCHDOG_FILE;
2023-03-08 14:50:25 +01:00
std::error_code e;
// TODO: Remove at some point in the future.
2023-06-25 10:24:27 +02:00
if (std::filesystem::exists(legacyPath, e)) {
// Old file might still exist, so copy it to new path
2023-06-24 18:48:12 +02:00
std::filesystem::copy(legacyPath, path, std::filesystem::copy_options::overwrite_existing, e);
if (e) {
sif::error << "File copy has failed: " << e.message() << std::endl;
}
}
2023-03-08 14:50:25 +01:00
if (not std::filesystem::exists(path, e) or recreateFile) {
#if OBSW_VERBOSE_LEVEL >= 1
sif::info << "CoreController::performRebootFileHandling: Recreating reboot watchdog file"
<< std::endl;
#endif
2023-06-24 17:29:23 +02:00
rebootWatchdogFile.enabled = false;
rebootWatchdogFile.img00Cnt = 0;
rebootWatchdogFile.img01Cnt = 0;
rebootWatchdogFile.img10Cnt = 0;
rebootWatchdogFile.img11Cnt = 0;
rebootWatchdogFile.lastChip = xsc::Chip::CHIP_0;
rebootWatchdogFile.lastCopy = xsc::Copy::COPY_0;
rebootWatchdogFile.img00Lock = false;
rebootWatchdogFile.img01Lock = false;
rebootWatchdogFile.img10Lock = false;
rebootWatchdogFile.img11Lock = false;
rebootWatchdogFile.mechanismNextChip = xsc::Chip::NO_CHIP;
rebootWatchdogFile.mechanismNextCopy = xsc::Copy::NO_COPY;
rebootWatchdogFile.bootFlag = false;
rewriteRebootWatchdogFile(rebootWatchdogFile);
} else {
2023-06-24 17:29:23 +02:00
if (not parseRebootWatchdogFile(path, rebootWatchdogFile)) {
performRebootWatchdogHandling(true);
return;
}
}
2022-03-01 13:34:12 +01:00
if (CURRENT_CHIP == xsc::CHIP_0) {
if (CURRENT_COPY == xsc::COPY_0) {
2023-06-24 17:29:23 +02:00
rebootWatchdogFile.img00Cnt++;
2022-03-01 13:34:12 +01:00
} else {
2023-06-24 17:29:23 +02:00
rebootWatchdogFile.img01Cnt++;
}
2022-03-01 13:34:12 +01:00
} else {
if (CURRENT_COPY == xsc::COPY_0) {
2023-06-24 17:29:23 +02:00
rebootWatchdogFile.img10Cnt++;
2022-03-01 13:34:12 +01:00
} else {
2023-06-24 17:29:23 +02:00
rebootWatchdogFile.img11Cnt++;
}
}
2022-02-28 19:52:43 +01:00
2023-06-24 17:29:23 +02:00
if (rebootWatchdogFile.bootFlag) {
2022-02-28 19:52:43 +01:00
// Trigger event to inform ground that a reboot was triggered
2023-06-24 17:29:23 +02:00
uint32_t p1 = rebootWatchdogFile.lastChip << 16 | rebootWatchdogFile.lastCopy;
2023-04-06 17:49:34 +02:00
triggerEvent(core::REBOOT_MECHANISM_TRIGGERED, p1, 0);
2022-02-28 19:52:43 +01:00
// Clear the boot flag
2023-06-24 17:29:23 +02:00
rebootWatchdogFile.bootFlag = false;
}
2022-02-28 19:52:43 +01:00
2023-06-24 17:29:23 +02:00
if (rebootWatchdogFile.mechanismNextChip != xsc::NO_CHIP and
rebootWatchdogFile.mechanismNextCopy != xsc::NO_COPY) {
if (CURRENT_CHIP != rebootWatchdogFile.mechanismNextChip or
CURRENT_COPY != rebootWatchdogFile.mechanismNextCopy) {
std::string infoString = std::to_string(rebootWatchdogFile.mechanismNextChip) + " " +
std::to_string(rebootWatchdogFile.mechanismNextCopy);
2022-03-01 14:44:50 +01:00
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
2023-06-24 17:29:23 +02:00
if (rebootWatchdogFile.mechanismNextChip == xsc::CHIP_0) {
if (rebootWatchdogFile.mechanismNextCopy == xsc::COPY_0) {
rebootWatchdogFile.img00Lock = true;
2022-03-01 13:34:12 +01:00
} else {
2023-06-24 17:29:23 +02:00
rebootWatchdogFile.img01Lock = true;
2022-03-01 13:34:12 +01:00
}
} else {
2023-06-24 17:29:23 +02:00
if (rebootWatchdogFile.mechanismNextCopy == xsc::COPY_0) {
rebootWatchdogFile.img10Lock = true;
2022-03-01 13:34:12 +01:00
} else {
2023-06-24 17:29:23 +02:00
rebootWatchdogFile.img11Lock = true;
2022-03-01 13:34:12 +01:00
}
}
}
}
2023-06-24 17:29:23 +02:00
rebootWatchdogFile.lastChip = CURRENT_CHIP;
rebootWatchdogFile.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
2023-06-24 17:29:23 +02:00
if (rebootWatchdogFile.enabled and
(*rebootWatchdogFile.relevantBootCnt >= rebootWatchdogFile.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;
2023-06-25 11:04:13 +02:00
rebootWatchdogAlgorithm(rebootWatchdogFile, doReboot, tgtChip, tgtCopy);
if (doReboot) {
2023-06-24 17:29:23 +02:00
rebootWatchdogFile.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
2023-06-24 17:29:23 +02:00
rebootWatchdogFile.mechanismNextChip = tgtChip;
rebootWatchdogFile.mechanismNextCopy = tgtCopy;
rewriteRebootWatchdogFile(rebootWatchdogFile);
2023-06-24 19:22:25 +02:00
performGracefulShutdown(tgtChip, tgtCopy);
}
} else {
2023-06-24 17:29:23 +02:00
rebootWatchdogFile.mechanismNextChip = xsc::NO_CHIP;
rebootWatchdogFile.mechanismNextCopy = xsc::NO_COPY;
}
2023-06-24 17:29:23 +02:00
rewriteRebootWatchdogFile(rebootWatchdogFile);
}
2023-06-25 11:04:13 +02:00
void CoreController::rebootWatchdogAlgorithm(RebootWatchdogFile &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
}
}
2023-06-24 17:29:23 +02:00
bool CoreController::parseRebootWatchdogFile(std::string path, RebootWatchdogFile &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;
}
2023-06-24 18:06:58 +02:00
bool CoreController::parseRebootCountersFile(std::string path, RebootCountersFile &rf) {
using namespace std;
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("img00:") == string::npos) {
return false;
}
iss >> rf.img00Cnt;
break;
}
case 1: {
iss >> word;
if (word.find("img01:") == string::npos) {
return false;
}
iss >> rf.img01Cnt;
break;
}
case 2: {
iss >> word;
if (word.find("img10:") == string::npos) {
return false;
}
iss >> rf.img10Cnt;
break;
}
case 3: {
iss >> word;
if (word.find("img11:") == string::npos) {
return false;
}
iss >> rf.img11Cnt;
break;
}
}
2023-06-24 19:55:56 +02:00
lineIdx++;
2023-06-24 18:06:58 +02:00
}
return true;
}
void CoreController::resetRebootWatchdogCounters(xsc::Chip tgtChip, xsc::Copy tgtCopy) {
2023-06-24 17:14:56 +02:00
std::string path = currMntPrefix + REBOOT_WATCHDOG_FILE;
2023-06-24 17:29:23 +02:00
parseRebootWatchdogFile(path, rebootWatchdogFile);
if (tgtChip == xsc::ALL_CHIP and tgtCopy == xsc::ALL_COPY) {
2023-06-24 17:29:23 +02:00
rebootWatchdogFile.img00Cnt = 0;
rebootWatchdogFile.img01Cnt = 0;
rebootWatchdogFile.img10Cnt = 0;
rebootWatchdogFile.img11Cnt = 0;
} else {
if (tgtChip == xsc::CHIP_0) {
if (tgtCopy == xsc::COPY_0) {
2023-06-24 17:29:23 +02:00
rebootWatchdogFile.img00Cnt = 0;
} else {
2023-06-24 17:29:23 +02:00
rebootWatchdogFile.img01Cnt = 0;
}
} else {
if (tgtCopy == xsc::COPY_0) {
2023-06-24 17:29:23 +02:00
rebootWatchdogFile.img10Cnt = 0;
} else {
2023-06-24 17:29:23 +02:00
rebootWatchdogFile.img11Cnt = 0;
}
}
}
2023-06-24 17:29:23 +02:00
rewriteRebootWatchdogFile(rebootWatchdogFile);
}
2023-06-24 18:06:58 +02:00
void CoreController::performRebootCountersHandling(bool recreateFile) {
std::string path = currMntPrefix + REBOOT_COUNTERS_FILE;
std::error_code e;
if (not std::filesystem::exists(path, e) or recreateFile) {
#if OBSW_VERBOSE_LEVEL >= 1
sif::info << "CoreController::performRebootFileHandling: Recreating reboot counters file"
<< std::endl;
#endif
rebootCountersFile.img00Cnt = 0;
rebootCountersFile.img01Cnt = 0;
rebootCountersFile.img10Cnt = 0;
rebootCountersFile.img11Cnt = 0;
rewriteRebootCountersFile(rebootCountersFile);
} else {
if (not parseRebootCountersFile(path, rebootCountersFile)) {
performRebootCountersHandling(true);
return;
}
}
if (CURRENT_CHIP == xsc::CHIP_0) {
if (CURRENT_COPY == xsc::COPY_0) {
rebootCountersFile.img00Cnt++;
} else {
rebootCountersFile.img01Cnt++;
}
} else {
if (CURRENT_COPY == xsc::COPY_0) {
rebootCountersFile.img10Cnt++;
} else {
rebootCountersFile.img11Cnt++;
}
}
announceBootCounts();
rewriteRebootCountersFile(rebootCountersFile);
2023-06-24 18:06:58 +02:00
}
2023-06-24 17:29:23 +02:00
void CoreController::rewriteRebootWatchdogFile(RebootWatchdogFile file) {
using namespace std::filesystem;
2023-06-24 17:14:56 +02:00
std::string path = currMntPrefix + REBOOT_WATCHDOG_FILE;
std::string legacyPath = currMntPrefix + LEGACY_REBOOT_WATCHDOG_FILE;
2023-06-24 18:52:29 +02:00
{
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
<< "\nimg00lock: " << file.img00Lock << "\nimg01lock: " << file.img01Lock
<< "\nimg10lock: " << file.img10Lock << "\nimg11lock: " << file.img11Lock
<< "\nbootflag: " << file.bootFlag << "\nlast: " << static_cast<int>(file.lastChip)
<< " " << static_cast<int>(file.lastCopy)
<< "\nnext: " << static_cast<int>(file.mechanismNextChip) << " "
<< static_cast<int>(file.mechanismNextCopy) << "\n";
}
}
2023-06-24 18:48:12 +02:00
std::error_code e;
// TODO: Remove at some point in the future when all images have been updated.
if (std::filesystem::exists(legacyPath)) {
// Keep those two files in sync
2023-06-24 18:48:12 +02:00
std::filesystem::copy(path, legacyPath, std::filesystem::copy_options::overwrite_existing, e);
if (e) {
sif::error << "File copy has failed: " << e.message() << std::endl;
}
}
}
void CoreController::rewriteRebootCountersFile(RebootCountersFile file) {
std::string path = currMntPrefix + REBOOT_COUNTERS_FILE;
std::ofstream rebootFile(path);
if (rebootFile.is_open()) {
2023-06-24 19:00:09 +02:00
rebootFile << "img00: " << file.img00Cnt << "\nimg01: " << file.img01Cnt
<< "\nimg10: " << file.img10Cnt << "\nimg11: " << file.img11Cnt << "\n";
}
}
2022-03-01 14:44:50 +01:00
void CoreController::setRebootMechanismLock(bool lock, xsc::Chip tgtChip, xsc::Copy tgtCopy) {
2023-06-24 17:14:56 +02:00
std::string path = currMntPrefix + REBOOT_WATCHDOG_FILE;
2023-06-24 17:29:23 +02:00
parseRebootWatchdogFile(path, rebootWatchdogFile);
2022-03-01 14:44:50 +01:00
if (tgtChip == xsc::CHIP_0) {
if (tgtCopy == xsc::COPY_0) {
2023-06-24 17:29:23 +02:00
rebootWatchdogFile.img00Lock = lock;
2022-03-01 14:44:50 +01:00
} else {
2023-06-24 17:29:23 +02:00
rebootWatchdogFile.img01Lock = lock;
2022-03-01 14:44:50 +01:00
}
} else {
if (tgtCopy == xsc::COPY_0) {
2023-06-24 17:29:23 +02:00
rebootWatchdogFile.img10Lock = lock;
2022-03-01 14:44:50 +01:00
} else {
2023-06-24 17:29:23 +02:00
rebootWatchdogFile.img11Lock = lock;
2022-03-01 14:44:50 +01:00
}
}
2023-06-24 17:29:23 +02:00
rewriteRebootWatchdogFile(rebootWatchdogFile);
2022-03-01 14:44:50 +01:00
}
2022-03-13 16:32:40 +01:00
2023-02-24 18:45:04 +01:00
ReturnValue_t CoreController::backupTimeFileHandler() {
// 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);
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
return result;
}
2023-02-24 18:45:04 +01:00
std::string fileName = currMntPrefix + BACKUP_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;
2022-08-24 17:27:47 +02:00
return returnvalue::FAILED;
2022-04-07 17:23:50 +02:00
}
2023-02-24 18:45:04 +01:00
timeFile << "UNIX SECONDS: " << currentTime.tv_sec + BOOT_OFFSET_SECONDS << std::endl;
2022-04-07 17:23:50 +02:00
}
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2022-04-07 17:23:50 +02:00
}
ReturnValue_t CoreController::initClockFromTimeFile() {
using namespace GpsHyperion;
using namespace std;
2023-02-24 18:45:04 +01:00
std::string fileName = currMntPrefix + BACKUP_TIME_FILE;
2023-03-08 14:50:25 +01:00
std::error_code e;
if (sdcMan->isSdCardUsable(std::nullopt) and std::filesystem::exists(fileName, e) 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-08-24 17:27:47 +02:00
return returnvalue::FAILED;
2022-04-07 17:23:50 +02:00
}
iss >> nextWord;
if (iss.bad() or nextWord != "SECONDS:") {
2022-08-24 17:27:47 +02:00
return returnvalue::FAILED;
2022-04-07 17:23:50 +02:00
}
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-08-24 17:27:47 +02:00
return returnvalue::FAILED;
2022-04-07 17:23:50 +02:00
}
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
2023-02-24 18:45:04 +01:00
timeFileInitDone = true;
return Clock::setClock(&currentTime);
2022-04-07 17:23:50 +02:00
}
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2022-04-07 17:23:50 +02:00
}
2022-03-13 16:32:40 +01:00
void CoreController::readHkData() {
2022-08-24 17:27:47 +02:00
ReturnValue_t result = returnvalue::OK;
2022-03-13 16:32:40 +01:00
result = hkSet.read(TIMEOUT_TYPE, MUTEX_TIMEOUT);
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
2022-03-13 16:32:40 +01:00
return;
}
Xadc xadc;
result = xadc.getTemperature(hkSet.temperature.value);
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
2022-03-13 16:32:40 +01:00
hkSet.temperature.setValid(false);
} else {
hkSet.temperature.setValid(true);
}
result = xadc.getVccPint(hkSet.psVoltage.value);
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
2022-03-13 16:32:40 +01:00
hkSet.psVoltage.setValid(false);
} else {
hkSet.psVoltage.setValid(true);
}
result = xadc.getVccInt(hkSet.plVoltage.value);
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
2022-03-13 16:32:40 +01:00
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);
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
2022-03-13 16:32:40 +01:00
return;
}
}
2022-04-07 17:23:50 +02:00
2022-09-26 12:00:57 +02:00
const char *CoreController::getXscMountDir(xsc::Chip chip, xsc::Copy copy) {
if (chip == xsc::Chip::CHIP_0) {
if (copy == xsc::Copy::COPY_0) {
return CHIP_0_COPY_0_MOUNT_DIR;
} else if (copy == xsc::Copy::COPY_1) {
return CHIP_0_COPY_1_MOUNT_DIR;
}
} else if (chip == xsc::Chip::CHIP_1) {
if (copy == xsc::Copy::COPY_0) {
return CHIP_1_COPY_0_MOUNT_DIR;
} else if (copy == xsc::Copy::COPY_1) {
return CHIP_1_COPY_1_MOUNT_DIR;
}
}
sif::error << "Invalid chip or copy passed to CoreController::getXscMountDir" << std::endl;
return CHIP_0_COPY_0_MOUNT_DIR;
}
2022-09-26 12:26:08 +02:00
ReturnValue_t CoreController::executeSwUpdate(SwUpdateSources sourceDir, const uint8_t *data,
size_t size) {
using namespace std;
using namespace std::filesystem;
// At the very least, chip and copy ID need to be included in the command
if (size < 2) {
return HasActionsIF::INVALID_PARAMETERS;
}
if (data[0] > 1 or data[1] > 1) {
return HasActionsIF::INVALID_PARAMETERS;
}
auto chip = static_cast<xsc::Chip>(data[0]);
auto copy = static_cast<xsc::Copy>(data[1]);
2022-09-26 23:17:12 +02:00
const char *sourceStr = "unknown";
if (sourceDir == SwUpdateSources::SD_0) {
sourceStr = "SD 0";
} else if (sourceDir == SwUpdateSources::SD_1) {
sourceStr = "SD 1";
} else {
sourceStr = "tmp directory";
}
bool sameChipAndCopy = false;
2022-09-29 14:10:21 +02:00
if (chip == CURRENT_CHIP and copy == CURRENT_COPY) {
// This is problematic if the OBSW is running as a systemd service.
// Do not allow for now.
return HasActionsIF::INVALID_PARAMETERS;
2022-09-29 14:10:21 +02:00
// sameChipAndCopy = true;
}
2022-09-26 23:17:12 +02:00
sif::info << "Executing SW update for Chip " << static_cast<int>(data[0]) << " Copy "
<< static_cast<int>(data[1]) << " from " << sourceStr << std::endl;
2022-09-26 12:26:08 +02:00
path prefixPath;
if (sourceDir == SwUpdateSources::SD_0) {
prefixPath = path(config::SD_0_MOUNT_POINT);
} else if (sourceDir == SwUpdateSources::SD_1) {
prefixPath = path(config::SD_1_MOUNT_POINT);
} else if (sourceDir == SwUpdateSources::TMP_DIR) {
prefixPath = path("/tmp");
}
2023-04-08 11:31:31 +02:00
path archivePath;
// It is optionally possible to supply the source file path
if (size > 2) {
archivePath = prefixPath / std::string(reinterpret_cast<const char *>(data + 2), size - 2);
} else {
archivePath = prefixPath / path(config::OBSW_UPDATE_ARCHIVE_FILE_NAME);
}
sif::info << "Updating with archive path " << archivePath << std::endl;
2023-03-08 14:50:25 +01:00
std::error_code e;
if (not exists(archivePath, e)) {
2022-09-26 12:26:08 +02:00
return HasFileSystemIF::FILE_DOES_NOT_EXIST;
}
2023-04-15 23:00:34 +02:00
// TODO: Decompressing without limiting memory usage with xz is actually a bit risky..
// But has not been an issue so far.
2022-09-26 23:17:12 +02:00
ostringstream cmd("tar -xJf", ios::app);
cmd << " " << archivePath << " -C " << prefixPath;
2022-09-26 12:26:08 +02:00
int result = system(cmd.str().c_str());
if (result != 0) {
utility::handleSystemError(result, "CoreController::executeAction: SW Update Decompression");
}
path strippedImagePath = prefixPath / path(config::STRIPPED_OBSW_BINARY_FILE_NAME);
2023-03-08 14:50:25 +01:00
if (!exists(strippedImagePath, e)) {
2022-09-26 12:26:08 +02:00
// TODO: Custom returnvalue?
return returnvalue::FAILED;
}
path obswVersionFilePath = prefixPath / path(config::OBSW_VERSION_FILE_NAME);
2023-03-08 14:50:25 +01:00
if (!exists(obswVersionFilePath, e)) {
2022-09-26 12:26:08 +02:00
// TODO: Custom returnvalue?
return returnvalue::FAILED;
}
2022-09-26 23:17:12 +02:00
cmd.str("");
2022-09-26 12:26:08 +02:00
cmd.clear();
path obswDestPath;
path obswVersionDestPath;
2022-09-29 14:10:21 +02:00
if (not sameChipAndCopy) {
cmd << "xsc_mount_copy " << std::to_string(data[0]) << " " << std::to_string(data[1]);
result = system(cmd.str().c_str());
if (result != 0) {
std::string contextString = "CoreController::executeAction: SW Update Mounting " +
std::to_string(data[0]) + " " + std::to_string(data[1]);
utility::handleSystemError(result, contextString);
}
cmd.str("");
cmd.clear();
path xscMountDest(getXscMountDir(chip, copy));
obswDestPath = xscMountDest / path(relative(config::OBSW_PATH, "/"));
obswVersionDestPath = xscMountDest / path(relative(config::OBSW_VERSION_FILE_PATH, "/"));
} else {
obswDestPath = path(config::OBSW_PATH);
obswVersionDestPath = path(config::OBSW_VERSION_FILE_PATH);
cmd << "writeprotect " << std::to_string(CURRENT_CHIP) << " " << std::to_string(CURRENT_COPY)
<< " 0";
result = system(cmd.str().c_str());
if (result != 0) {
std::string contextString = "CoreController::executeAction: Unlocking current chip";
utility::handleSystemError(result, contextString);
}
cmd.str("");
cmd.clear();
2022-09-26 12:26:08 +02:00
}
2022-09-26 12:26:08 +02:00
cmd << "cp " << strippedImagePath << " " << obswDestPath;
result = system(cmd.str().c_str());
if (result != 0) {
utility::handleSystemError(result, "CoreController::executeAction: Copying SW update");
}
2022-09-26 23:17:12 +02:00
cmd.str("");
2022-09-26 12:26:08 +02:00
cmd.clear();
2022-09-26 12:26:08 +02:00
cmd << "cp " << obswVersionFilePath << " " << obswVersionDestPath;
result = system(cmd.str().c_str());
if (result != 0) {
utility::handleSystemError(result, "CoreController::executeAction: Copying SW version file");
}
2022-09-26 23:17:12 +02:00
cmd.str("");
2022-09-26 12:26:08 +02:00
cmd.clear();
// Set correct permission for both files
cmd << "chmod 0755 " << obswDestPath;
result = system(cmd.str().c_str());
if (result != 0) {
2022-09-26 23:17:12 +02:00
utility::handleSystemError(result,
"CoreController::executeAction: Setting SW permissions 0755");
}
2022-09-26 23:17:12 +02:00
cmd.str("");
cmd.clear();
cmd << "chmod 0644 " << obswVersionDestPath;
result = system(cmd.str().c_str());
if (result != 0) {
2022-09-26 23:17:12 +02:00
utility::handleSystemError(
result, "CoreController::executeAction: Setting version file permission 0644");
}
2022-09-26 23:17:12 +02:00
cmd.str("");
cmd.clear();
2023-04-08 11:40:23 +02:00
// Remove the extracted files to keep directories clean.
2023-04-11 15:23:44 +02:00
std::filesystem::remove(strippedImagePath, e);
std::filesystem::remove(obswVersionFilePath, e);
2023-04-08 11:40:23 +02:00
2022-09-26 12:26:08 +02:00
// TODO: This takes a long time and will block the core controller.. Maybe use command executor?
// For now dont care..
2022-09-26 23:17:12 +02:00
cmd << "writeprotect " << std::to_string(data[0]) << " " << std::to_string(data[1]) << " 1";
2022-09-26 12:26:08 +02:00
result = system(cmd.str().c_str());
if (result != 0) {
std::string contextString = "CoreController::executeAction: Writeprotecting " +
std::to_string(data[0]) + " " + std::to_string(data[1]);
utility::handleSystemError(result, contextString);
}
2022-09-26 23:17:12 +02:00
sif::info << "SW update complete" << std::endl;
2022-09-26 12:26:08 +02:00
return HasActionsIF::EXECUTION_FINISHED;
}
2022-09-27 12:19:42 +02:00
bool CoreController::startSdStateMachine(sd::SdCard targetActiveSd, SdCfgMode mode,
MessageQueueId_t commander, DeviceCommandId_t actionId) {
2022-09-27 10:51:07 +02:00
if (sdFsmState != SdStates::IDLE or sdCommandingInfo.cmdPending) {
return false;
}
sdFsmState = SdStates::START;
sdInfo.active = targetActiveSd;
// If we are going from 2 SD cards to one, lock SD card usage in any case because 1 SD card is
// going off.
if (sdInfo.cfgMode == SdCfgMode::HOT_REDUNDANT and mode == SdCfgMode::COLD_REDUNDANT) {
sdInfo.lockSdCardUsage = true;
}
2022-09-27 10:51:07 +02:00
sdInfo.cfgMode = mode;
2022-09-27 12:19:42 +02:00
sdCommandingInfo.actionId = actionId;
sdCommandingInfo.commander = commander;
2022-09-27 10:51:07 +02:00
sdCommandingInfo.cmdPending = true;
return true;
}
2023-03-08 11:37:11 +01:00
void CoreController::announceBootCounts() {
uint64_t totalBootCount = rebootCountersFile.img00Cnt + rebootCountersFile.img01Cnt +
rebootCountersFile.img10Cnt + rebootCountersFile.img11Cnt;
2023-06-24 17:29:23 +02:00
uint32_t individualBootCountsP1 =
(rebootCountersFile.img00Cnt << 16) | rebootCountersFile.img01Cnt;
2023-06-24 17:29:23 +02:00
uint32_t individualBootCountsP2 =
(rebootCountersFile.img10Cnt << 16) | rebootCountersFile.img11Cnt;
2023-04-06 17:49:34 +02:00
triggerEvent(core::INDIVIDUAL_BOOT_COUNTS, individualBootCountsP1, individualBootCountsP2);
triggerEvent(core::REBOOT_COUNTER, (totalBootCount >> 32) & 0xffffffff,
totalBootCount & 0xffffffff);
2023-03-08 11:37:11 +01:00
}
2023-04-12 18:11:20 +02:00
MessageQueueId_t CoreController::getCommandQueue() const {
return ExtendedControllerBase::getCommandQueue();
}
2023-06-07 14:20:58 +02:00
void CoreController::dirListingDumpHandler() {
2023-06-07 16:21:51 +02:00
if (dumpContext.firstDump) {
dumpContext.firstDump = false;
return;
}
2023-06-07 14:20:58 +02:00
size_t nextDumpLen = 0;
size_t dummy = 0;
ReturnValue_t result;
std::error_code e;
std::ifstream ifile(LIST_DIR_DUMP_WORK_FILE, std::ios::binary);
if (ifile.bad()) {
return;
}
while (dumpContext.dumpedBytes < dumpContext.totalFileSize) {
ifile.seekg(dumpContext.dumpedBytes, std::ios::beg);
nextDumpLen = dumpContext.maxDumpLen;
if (dumpContext.totalFileSize - dumpContext.dumpedBytes < dumpContext.maxDumpLen) {
nextDumpLen = dumpContext.totalFileSize - dumpContext.dumpedBytes;
}
SerializeAdapter::serialize(&dumpContext.segmentIdx, dirListingBuf.data(), &dummy,
dirListingBuf.size(), SerializeIF::Endianness::NETWORK);
ifile.read(reinterpret_cast<char *>(dirListingBuf.data() + dumpContext.listingDataOffset),
nextDumpLen);
result =
actionHelper.reportData(dumpContext.commander, dumpContext.actionId, dirListingBuf.data(),
dumpContext.listingDataOffset + nextDumpLen);
if (result != returnvalue::OK) {
// Remove work file when we are done
std::filesystem::remove(LIST_DIR_DUMP_WORK_FILE, e);
dumpContext.active = false;
actionHelper.finish(false, dumpContext.commander, dumpContext.actionId, result);
return;
}
dumpContext.segmentIdx++;
dumpContext.dumpedBytes += nextDumpLen;
// Dump takes multiple task cycles, so cache the dump state and continue dump the next cycles.
2023-06-07 16:21:51 +02:00
if (dumpContext.segmentIdx == 10) {
2023-06-07 14:20:58 +02:00
break;
}
}
if (dumpContext.dumpedBytes >= dumpContext.totalFileSize) {
actionHelper.finish(true, dumpContext.commander, dumpContext.actionId, result);
dumpContext.active = false;
// Remove work file when we are done
std::filesystem::remove(LIST_DIR_DUMP_WORK_FILE, e);
}
}
2023-06-22 14:06:34 +02:00
void CoreController::announceVersionInfo() {
using namespace core;
uint32_t p1 = (common::OBSW_VERSION_MAJOR << 24) | (common::OBSW_VERSION_MINOR << 16) |
(common::OBSW_VERSION_REVISION << 8);
uint32_t p2 = 0;
if (strcmp("", common::OBSW_VERSION_CST_GIT_SHA1) != 0) {
p1 |= 1;
auto shaAsStr = std::string(common::OBSW_VERSION_CST_GIT_SHA1);
size_t posDash = shaAsStr.find("-");
auto gitHash = shaAsStr.substr(posDash + 2, 4);
// Only copy first 4 letters of git hash
memcpy(&p2, gitHash.c_str(), 4);
}
triggerEvent(VERSION_INFO, p1, p2);
if (common::OBSW_VERSION_MAJOR >= 6 or common::OBSW_VERSION_MAJOR == 4) {
if (mappedSysRomAddr != nullptr) {
uint32_t p1Firmware = *(reinterpret_cast<uint32_t *>(mappedSysRomAddr));
uint32_t p2Firmware = *(reinterpret_cast<uint32_t *>(mappedSysRomAddr) + 1);
triggerEvent(FIRMWARE_INFO, p1Firmware, p2Firmware);
}
2023-06-22 14:06:34 +02:00
}
}
void CoreController::announceCurrentImageInfo() {
2023-06-22 14:25:11 +02:00
using namespace core;
2023-06-22 14:06:34 +02:00
triggerEvent(CURRENT_IMAGE_INFO, CURRENT_CHIP, CURRENT_COPY);
}
2023-06-24 19:22:25 +02:00
ReturnValue_t CoreController::performGracefulShutdown(xsc::Chip tgtChip, xsc::Copy tgtCopy) {
bool protOpPerformed = false;
// This function can not really fail
gracefulShutdownTasks(tgtChip, tgtCopy, protOpPerformed);
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;
}
return returnvalue::OK;
}
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
}
2023-04-12 18:06:04 +02:00
ReturnValue_t CoreController::getParameter(uint8_t domainId, uint8_t uniqueIdentifier,
ParameterWrapper *parameterWrapper,
const ParameterWrapper *newValues,
uint16_t startAtIndex) {
2023-04-12 18:11:41 +02:00
if (domainId != 0) {
2023-04-12 18:06:04 +02:00
return HasParametersIF::INVALID_DOMAIN_ID;
}
2023-04-12 18:11:41 +02:00
if (uniqueIdentifier >= ParamId::NUM_IDS) {
2023-04-12 18:06:04 +02:00
return HasParametersIF::INVALID_IDENTIFIER_ID;
}
2023-04-12 18:11:41 +02:00
uint8_t newPrefSd;
ReturnValue_t result = newValues->getElement(&newPrefSd);
if (result != returnvalue::OK) {
return result;
}
// Only SD card 0 (0) and 1 (1) are allowed values.
if (newPrefSd > 1) {
return HasParametersIF::INVALID_VALUE;
}
result = sdcMan->setPreferredSdCard(static_cast<sd::SdCard>(newPrefSd));
if (result != returnvalue::OK) {
return returnvalue::FAILED;
}
parameterWrapper->set(prefSdRaw);
return returnvalue::OK;
2023-04-12 18:06:04 +02:00
}