eive-obsw/bsp_q7s/core/CoreController.h

325 lines
13 KiB
C
Raw Normal View History

2021-05-26 14:16:16 +02:00
#ifndef BSP_Q7S_CORE_CORECONTROLLER_H_
#define BSP_Q7S_CORE_CORECONTROLLER_H_
2023-03-12 20:51:33 +01:00
#include <fsfw/container/DynamicFIFO.h>
#include <fsfw/container/SimpleRingBuffer.h>
2021-07-29 18:09:54 +02:00
#include <fsfw/globalfunctions/PeriodicOperationDivider.h>
#include <libxiphos.h>
#include <cstddef>
2021-05-26 14:16:16 +02:00
#include "CoreDefinitions.h"
2022-10-14 15:33:34 +02:00
#include "OBSWConfig.h"
2022-09-16 11:53:33 +02:00
#include "bsp_q7s/fs/SdCardManager.h"
2021-07-19 12:44:43 +02:00
#include "events/subsystemIdRanges.h"
2022-01-17 15:58:27 +01:00
#include "fsfw/controller/ExtendedControllerBase.h"
#include "mission/devices/devicedefinitions/GPSDefinitions.h"
2023-02-14 11:10:18 +01:00
#include "mission/trace.h"
2023-03-13 09:49:35 +01:00
#include <atomic>
class Timer;
class SdCardManager;
namespace xsc {
2022-02-28 19:52:43 +01:00
enum Chip : int { CHIP_0, CHIP_1, NO_CHIP, SELF_CHIP, ALL_CHIP };
enum Copy : int { COPY_0, COPY_1, NO_COPY, SELF_COPY, ALL_COPY };
} // namespace xsc
struct RebootFile {
static constexpr uint8_t DEFAULT_MAX_BOOT_CNT = 10;
2022-03-01 14:51:12 +01:00
bool enabled = true;
size_t maxCount = DEFAULT_MAX_BOOT_CNT;
2022-02-28 16:47:36 +01:00
uint32_t img00Cnt = 0;
uint32_t img01Cnt = 0;
uint32_t img10Cnt = 0;
uint32_t img11Cnt = 0;
2022-03-01 13:34:12 +01:00
bool img00Lock = false;
bool img01Lock = false;
bool img10Lock = false;
bool img11Lock = false;
2022-02-28 19:52:43 +01:00
uint32_t* relevantBootCnt = &img00Cnt;
bool bootFlag = false;
xsc::Chip lastChip = xsc::Chip::CHIP_0;
xsc::Copy lastCopy = xsc::Copy::COPY_0;
2022-03-01 13:34:12 +01:00
xsc::Chip mechanismNextChip = xsc::Chip::NO_CHIP;
xsc::Copy mechanismNextCopy = xsc::Copy::NO_COPY;
};
2022-01-17 15:58:27 +01:00
class CoreController : public ExtendedControllerBase {
public:
static xsc::Chip CURRENT_CHIP;
static xsc::Copy CURRENT_COPY;
2022-01-17 15:58:27 +01:00
2022-04-17 00:56:31 +02:00
static constexpr char CHIP_PROT_SCRIPT[] = "get-chip-prot-status.sh";
2022-01-17 15:58:27 +01:00
static constexpr char CHIP_STATE_FILE[] = "/tmp/chip_prot_status.txt";
static constexpr char CURR_COPY_FILE[] = "/tmp/curr_copy.txt";
2022-09-26 11:15:37 +02:00
static constexpr char CONF_FOLDER[] = "conf";
2022-09-26 11:15:37 +02:00
static constexpr char VERSION_FILE_NAME[] = "version.txt";
static constexpr char REBOOT_FILE_NAME[] = "reboot.txt";
2023-02-24 18:45:04 +01:00
static constexpr char TIME_FILE_NAME[] = "time_backup.txt";
2022-09-26 11:15:37 +02:00
2022-02-28 16:47:36 +01:00
const std::string VERSION_FILE =
"/" + std::string(CONF_FOLDER) + "/" + std::string(VERSION_FILE_NAME);
const std::string REBOOT_FILE =
"/" + std::string(CONF_FOLDER) + "/" + std::string(REBOOT_FILE_NAME);
2023-02-24 18:45:04 +01:00
const std::string BACKUP_TIME_FILE =
"/" + std::string(CONF_FOLDER) + "/" + std::string(TIME_FILE_NAME);
2022-01-17 15:58:27 +01:00
2022-09-26 11:31:29 +02:00
static constexpr char CHIP_0_COPY_0_MOUNT_DIR[] = "/tmp/mntupdate-xdi-qspi0-nom-rootfs";
static constexpr char CHIP_0_COPY_1_MOUNT_DIR[] = "/tmp/mntupdate-xdi-qspi0-gold-rootfs";
static constexpr char CHIP_1_COPY_0_MOUNT_DIR[] = "/tmp/mntupdate-xdi-qspi1-nom-rootfs";
static constexpr char CHIP_1_COPY_1_MOUNT_DIR[] = "/tmp/mntupdate-xdi-qspi1-gold-rootfs";
2022-09-26 11:12:41 +02:00
static constexpr dur_millis_t INIT_SD_CARD_CHECK_TIMEOUT = 5000;
static constexpr dur_millis_t DEFAULT_SD_CARD_CHECK_TIMEOUT = 60000;
2022-01-17 15:58:27 +01:00
static constexpr ActionId_t LIST_DIRECTORY_INTO_FILE = 0;
2023-02-08 11:08:38 +01:00
static constexpr ActionId_t ANNOUNCE_VERSION = 1;
static constexpr ActionId_t ANNOUNCE_CURRENT_IMAGE = 2;
2023-03-08 11:37:11 +01:00
static constexpr ActionId_t ANNOUNCE_BOOT_COUNTS = 3;
static constexpr ActionId_t SWITCH_REBOOT_FILE_HANDLING = 5;
2022-03-01 14:44:50 +01:00
static constexpr ActionId_t RESET_REBOOT_COUNTERS = 6;
static constexpr ActionId_t SWITCH_IMG_LOCK = 7;
static constexpr ActionId_t SET_MAX_REBOOT_CNT = 8;
2022-09-26 10:56:57 +02:00
static constexpr ActionId_t OBSW_UPDATE_FROM_SD_0 = 10;
static constexpr ActionId_t OBSW_UPDATE_FROM_SD_1 = 11;
static constexpr ActionId_t OBSW_UPDATE_FROM_TMP = 12;
2022-09-16 18:48:05 +02:00
2022-09-27 10:51:07 +02:00
static constexpr ActionId_t SWITCH_TO_SD_0 = 16;
static constexpr ActionId_t SWITCH_TO_SD_1 = 17;
static constexpr ActionId_t SWITCH_TO_BOTH_SD_CARDS = 18;
2022-04-29 11:16:53 +02:00
//! Reboot using the xsc_boot_copy command
static constexpr ActionId_t XSC_REBOOT_OBC = 32;
2022-01-17 15:58:27 +01:00
static constexpr ActionId_t MOUNT_OTHER_COPY = 33;
2022-04-29 11:16:53 +02:00
//! Reboot using the reboot command
static constexpr ActionId_t REBOOT_OBC = 34;
2022-01-17 15:58:27 +01:00
2023-03-12 20:51:33 +01:00
static constexpr ActionId_t EXECUTE_SHELL_CMD = 40;
2022-01-17 15:58:27 +01:00
static constexpr uint8_t SUBSYSTEM_ID = SUBSYSTEM_ID::CORE;
static constexpr Event ALLOC_FAILURE = event::makeEvent(SUBSYSTEM_ID, 0, severity::MEDIUM);
2022-04-24 12:34:08 +02:00
//! [EXPORT] : [COMMENT] Software reboot occurred. Can also be a systemd reboot.
//! P1: Current Chip, P2: Current Copy
2023-03-08 11:31:04 +01:00
static constexpr Event REBOOT_SW = event::makeEvent(SUBSYSTEM_ID, 1, severity::LOW);
//! [EXPORT] : [COMMENT] The reboot mechanism was triggered.
//! P1: First 16 bits: Last Chip, Last 16 bits: Last Copy,
//! P2: Each byte is the respective reboot count for the slots
static constexpr Event REBOOT_MECHANISM_TRIGGERED =
event::makeEvent(SUBSYSTEM_ID, 2, severity::MEDIUM);
//! Trying to find a way how to determine that the reboot came from ProASIC3 or PCDU..
static constexpr Event REBOOT_HW = event::makeEvent(SUBSYSTEM_ID, 3, severity::MEDIUM);
2022-10-14 15:33:34 +02:00
//! [EXPORT] : [COMMENT] No SD card was active. Core controller will attempt to re-initialize
//! a SD card.
static constexpr Event NO_SD_CARD_ACTIVE = event::makeEvent(SUBSYSTEM_ID, 4, severity::HIGH);
2023-02-08 11:08:38 +01:00
//! [EXPORT] : [COMMENT]
//! P1: Byte 0: Major, Byte 1: Minor, Byte 2: Patch, Byte 3: Has Git Hash
//! P2: First four letters of Git SHA is the last byte of P1 is set.
static constexpr Event VERSION_INFO = event::makeEvent(SUBSYSTEM_ID, 5, severity::INFO);
//! [EXPORT] : [COMMENT] P1: Current Chip, P2: Current Copy
static constexpr Event CURRENT_IMAGE_INFO = event::makeEvent(SUBSYSTEM_ID, 6, severity::INFO);
2023-03-08 11:31:04 +01:00
//! [EXPORT] : [COMMENT] Total reboot counter, which is the sum of the boot count of all
//! individual images.
static constexpr Event REBOOT_COUNTER = event::makeEvent(SUBSYSTEM_ID, 7, severity::INFO);
//! [EXPORT] : [COMMENT] Get the boot count of the individual images.
//! P1: First 16 bits boot count of image 0 0, last 16 bits boot count of image 0 1.
//! P2: First 16 bits boot count of image 1 0, last 16 bits boot count of image 1 1.
static constexpr Event INDIVIDUAL_BOOT_COUNTS = event::makeEvent(SUBSYSTEM_ID, 8, severity::INFO);
2023-03-13 09:49:35 +01:00
static constexpr Event I2C_UNAVAILABLE_REBOOT = event::makeEvent(SUBSYSTEM_ID, 10, severity::MEDIUM);
2022-01-17 15:58:27 +01:00
2023-03-13 09:49:35 +01:00
CoreController(object_id_t objectId, const std::atomic_uint16_t& i2cErrors);
2022-01-17 15:58:27 +01:00
virtual ~CoreController();
ReturnValue_t initialize() override;
ReturnValue_t initializeAfterTaskCreation() override;
ReturnValue_t executeAction(ActionId_t actionId, MessageQueueId_t commandedBy,
const uint8_t* data, size_t size) override;
ReturnValue_t handleCommandMessage(CommandMessage* message) override;
void performControlOperation() override;
/**
* Generate a file containing the chip lock/unlock states inside /tmp/chip_prot_status.txt
* @return
*/
static ReturnValue_t generateChipStateFile();
static ReturnValue_t incrementAllocationFailureCount();
static void getCurrentBootCopy(xsc::Chip& chip, xsc::Copy& copy);
2022-09-26 12:00:57 +02:00
static const char* getXscMountDir(xsc::Chip chip, xsc::Copy copy);
2022-01-17 15:58:27 +01:00
ReturnValue_t updateProtInfo(bool regenerateChipStateFile = true);
/**
* Checks whether the target chip and copy are write protected and protect set them to a target
* state where applicable.
* @param targetChip
* @param targetCopy
* @param protect Target state
* @param protOperationPerformed [out] Can be used to determine whether any operation
* was performed
* @param updateProtFile Specify whether the protection info file is updated
* @return
*/
ReturnValue_t setBootCopyProtection(xsc::Chip targetChip, xsc::Copy targetCopy, bool protect,
2022-01-17 15:58:27 +01:00
bool& protOperationPerformed, bool updateProtFile = true);
bool sdInitFinished() const;
private:
2023-02-24 18:45:04 +01:00
static constexpr uint32_t BOOT_OFFSET_SECONDS = 15;
2022-03-13 16:32:40 +01:00
static constexpr MutexIF::TimeoutType TIMEOUT_TYPE = MutexIF::TimeoutType::WAITING;
static constexpr uint32_t MUTEX_TIMEOUT = 20;
2022-04-07 17:23:50 +02:00
GpsHyperion::FixMode gpsFix = GpsHyperion::FixMode::UNKNOWN;
2022-01-17 15:58:27 +01:00
// States for SD state machine, which is used in non-blocking mode
enum class SdStates {
NONE,
START,
GET_INFO,
SET_STATE_SELF,
MOUNT_SELF,
// Determine operations for other SD card, depending on redundancy configuration
DETERMINE_OTHER,
SET_STATE_OTHER,
// Mount or unmount other
MOUNT_UNMOUNT_OTHER,
// Skip period because the shell command used to generate the info file sometimes is
// missing the last performed operation if executed too early
SKIP_CYCLE_BEFORE_INFO_UPDATE,
UPDATE_INFO,
// SD initialization done
2022-09-27 10:51:07 +02:00
IDLE
2022-01-17 15:58:27 +01:00
};
2022-09-26 12:26:08 +02:00
enum class SwUpdateSources { SD_0, SD_1, TMP_DIR };
2022-01-17 15:58:27 +01:00
static constexpr bool BLOCKING_SD_INIT = false;
SdCardManager* sdcMan = nullptr;
2022-04-07 17:23:50 +02:00
MessageQueueIF* eventQueue = nullptr;
2022-01-17 15:58:27 +01:00
2022-09-27 10:51:07 +02:00
SdStates sdFsmState = SdStates::START;
enum SdCfgMode { PASSIVE, COLD_REDUNDANT, HOT_REDUNDANT };
struct SdFsmParams {
SdCfgMode cfgMode = SdCfgMode::COLD_REDUNDANT;
sd::SdCard active = sd::SdCard::NONE;
2022-01-17 15:58:27 +01:00
sd::SdCard other = sd::SdCard::NONE;
2022-09-27 10:51:07 +02:00
sd::SdState activeState = sd::SdState::OFF;
2022-01-17 15:58:27 +01:00
sd::SdState otherState = sd::SdState::OFF;
2022-09-27 10:51:07 +02:00
std::string activeChar = "0";
2022-01-17 15:58:27 +01:00
std::string otherChar = "1";
2022-05-24 00:20:23 +02:00
std::pair<bool, bool> mountSwitch = {true, true};
2022-01-17 15:58:27 +01:00
// Used to track whether a command was executed
bool commandExecuted = true;
bool initFinished = false;
SdCardManager::SdStatePair currentState;
uint16_t cycleCount = 0;
2022-02-28 14:13:31 +01:00
} sdInfo;
2022-09-27 10:51:07 +02:00
struct SdCommanding {
bool cmdPending = false;
MessageQueueId_t commander = MessageQueueIF::NO_QUEUE;
DeviceCommandId_t actionId;
} sdCommandingInfo;
RebootFile rebootFile = {};
2023-03-12 20:51:33 +01:00
CommandExecutor cmdExecutor;
SimpleRingBuffer cmdReplyBuf;
DynamicFIFO<uint16_t> cmdRepliesSizes;
bool shellCmdIsExecuting = false;
MessageQueueId_t successRecipient = MessageQueueIF::NO_QUEUE;
std::string currMntPrefix;
2023-02-24 18:45:04 +01:00
bool timeFileInitDone = false;
2022-10-11 22:58:31 +02:00
bool performOneShotSdCardOpsSwitch = false;
uint8_t shortSdCardCdCounter = 0;
2023-02-14 10:59:35 +01:00
#if OBSW_THREAD_TRACING == 1
uint32_t opCounter;
#endif
Countdown sdCardCheckCd = Countdown(INIT_SD_CARD_CHECK_TIMEOUT);
2022-01-17 15:58:27 +01:00
/**
* Index 0: Chip 0 Copy 0
* Index 1: Chip 0 Copy 1
* Index 2: Chip 1 Copy 0
* Index 3: Chip 1 Copy 1
*/
std::array<bool, 4> protArray;
PeriodicOperationDivider opDivider5;
PeriodicOperationDivider opDivider10;
2022-01-17 15:58:27 +01:00
2022-11-09 13:07:20 +01:00
PoolEntry<float> tempPoolEntry = PoolEntry<float>(0.0);
PoolEntry<float> psVoltageEntry = PoolEntry<float>(0.0);
PoolEntry<float> plVoltageEntry = PoolEntry<float>(0.0);
2022-03-13 16:32:40 +01:00
core::HkSet hkSet;
2023-03-13 09:49:35 +01:00
const std::atomic_uint16_t& i2cErrors;
2022-03-13 16:32:40 +01:00
2022-10-14 15:33:34 +02:00
#if OBSW_SD_CARD_MUST_BE_ON == 1
bool remountAttemptFlag = true;
#endif
2022-01-17 15:58:27 +01:00
ReturnValue_t initializeLocalDataPool(localpool::DataPool& localDataPoolMap,
LocalDataPoolManager& poolManager) override;
2022-01-17 15:58:27 +01:00
LocalPoolDataSetBase* getDataSetHandle(sid_t sid) override;
ReturnValue_t checkModeCommand(Mode_t mode, Submode_t submode, uint32_t* msToReachTheMode);
void performMountedSdCardOperations();
2022-01-17 15:58:27 +01:00
ReturnValue_t initVersionFile();
2022-04-07 17:23:50 +02:00
ReturnValue_t initClockFromTimeFile();
2022-05-24 00:20:23 +02:00
ReturnValue_t performSdCardCheck();
2023-02-24 18:45:04 +01:00
ReturnValue_t backupTimeFileHandler();
2022-11-09 13:07:20 +01:00
ReturnValue_t initBootCopyFile();
2022-01-17 15:58:27 +01:00
ReturnValue_t initSdCardBlocking();
2022-09-27 12:19:42 +02:00
bool startSdStateMachine(sd::SdCard targetActiveSd, SdCfgMode mode, MessageQueueId_t commander,
DeviceCommandId_t actionId);
2022-01-17 15:58:27 +01:00
void initPrint();
ReturnValue_t sdStateMachine();
void updateSdInfoOther();
ReturnValue_t sdCardSetup(sd::SdCard sdCard, sd::SdState targetState, std::string sdChar,
bool printOutput = true);
2022-09-26 12:26:08 +02:00
ReturnValue_t executeSwUpdate(SwUpdateSources sourceDir, const uint8_t* data, size_t size);
2022-01-17 15:58:27 +01:00
ReturnValue_t sdColdRedundantBlockingInit();
2022-01-17 15:58:27 +01:00
void currentStateSetter(sd::SdCard sdCard, sd::SdState newState);
void executeNextExternalSdCommand();
void checkExternalSdCommandStatus();
void performRebootFileHandling(bool recreateFile);
2022-01-17 15:58:27 +01:00
ReturnValue_t actionListDirectoryIntoFile(ActionId_t actionId, MessageQueueId_t commandedBy,
const uint8_t* data, size_t size);
2022-04-29 11:16:53 +02:00
ReturnValue_t actionXscReboot(const uint8_t* data, size_t size);
ReturnValue_t actionReboot(const uint8_t* data, size_t size);
ReturnValue_t gracefulShutdownTasks(xsc::Chip chip, xsc::Copy copy, bool& protOpPerformed);
2022-01-17 15:58:27 +01:00
ReturnValue_t handleProtInfoUpdateLine(std::string nextLine);
int handleBootCopyProtAtIndex(xsc::Chip targetChip, xsc::Copy targetCopy, bool protect,
2022-01-17 15:58:27 +01:00
bool& protOperationPerformed, bool selfChip, bool selfCopy,
bool allChips, bool allCopies, uint8_t arrIdx);
void determineAndExecuteReboot(RebootFile& rf, bool& needsReboot, xsc::Chip& tgtChip,
xsc::Copy& tgtCopy);
void resetRebootCount(xsc::Chip tgtChip, xsc::Copy tgtCopy);
2022-03-01 14:44:50 +01:00
void setRebootMechanismLock(bool lock, xsc::Chip tgtChip, xsc::Copy tgtCopy);
bool parseRebootFile(std::string path, RebootFile& file);
void rewriteRebootFile(RebootFile file);
2023-03-08 11:37:11 +01:00
void announceBootCounts();
2022-03-13 16:32:40 +01:00
void readHkData();
2022-04-07 17:23:50 +02:00
bool isNumber(const std::string& s);
};
2021-05-26 14:16:16 +02:00
#endif /* BSP_Q7S_CORE_CORECONTROLLER_H_ */