eive-obsw/mission/payload/ScexDeviceHandler.cpp

396 lines
12 KiB
C++
Raw Permalink Normal View History

2022-10-25 11:22:31 +02:00
#include <fsfw/filesystem/HasFileSystemIF.h>
2023-03-26 16:42:00 +02:00
#include <linux/payload/ScexHelper.h>
2022-05-19 11:56:59 +02:00
#include <mission/memory/SdCardMountedIF.h>
2023-03-26 16:42:00 +02:00
#include <mission/payload/ScexDeviceHandler.h>
#include <mission/payload/scexHelpers.h>
2022-05-19 09:59:33 +02:00
2022-04-29 15:46:16 +02:00
#include <algorithm>
#include <ctime>
2022-10-04 23:04:50 +02:00
#include <filesystem>
2022-09-29 19:38:19 +02:00
#include <fstream>
2022-10-04 23:04:50 +02:00
#include <iostream>
2022-05-29 17:54:17 +02:00
#include <random>
2022-04-29 15:46:16 +02:00
2023-04-01 14:23:11 +02:00
#include "eive/definitions.h"
2022-04-29 15:46:16 +02:00
#include "fsfw/globalfunctions/CRC.h"
2022-05-19 10:16:46 +02:00
using std::ofstream;
2022-08-29 16:05:05 +02:00
using namespace returnvalue;
2022-05-19 10:16:46 +02:00
2022-05-19 11:56:59 +02:00
ScexDeviceHandler::ScexDeviceHandler(object_id_t objectId, ScexUartReader& reader, CookieIF* cookie,
SdCardMountedIF& sdcMan)
2023-08-02 09:39:42 +02:00
: DeviceHandlerBase(objectId, reader.getObjectId(), cookie), sdcMan(sdcMan), reader(reader) {
fsUnusableEventCd.timeOut();
}
2022-04-29 15:46:16 +02:00
2022-05-19 09:59:33 +02:00
ScexDeviceHandler::~ScexDeviceHandler() {}
2022-04-29 15:46:16 +02:00
2023-08-02 09:20:36 +02:00
void ScexDeviceHandler::doStartUp() {
filesystemChecks();
setMode(MODE_ON);
}
2022-04-29 15:46:16 +02:00
2022-10-04 23:04:50 +02:00
void ScexDeviceHandler::doShutDown() {
reader.reset();
2022-10-06 18:05:06 +02:00
commandActive = false;
2023-04-01 14:40:34 +02:00
fileNameSet = false;
2022-11-09 13:07:20 +01:00
multiFileFinishOutstanding = false;
2022-10-04 23:04:50 +02:00
setMode(_MODE_POWER_DOWN);
}
2022-04-29 15:46:16 +02:00
2023-10-10 16:18:35 +02:00
ReturnValue_t ScexDeviceHandler::buildNormalDeviceCommand(DeviceCommandId_t* id) {
return NOTHING_TO_SEND;
}
2022-04-29 15:46:16 +02:00
2023-10-10 16:18:35 +02:00
ReturnValue_t ScexDeviceHandler::buildTransitionDeviceCommand(DeviceCommandId_t* id) {
return NOTHING_TO_SEND;
}
2022-04-29 15:46:16 +02:00
ReturnValue_t ScexDeviceHandler::buildCommandFromCommand(DeviceCommandId_t deviceCommand,
const uint8_t* commandData,
size_t commandDataLen) {
2022-04-29 15:46:16 +02:00
using namespace scex;
2022-05-19 11:56:59 +02:00
auto cmdTyped = static_cast<scex::Cmds>(deviceCommand);
if (std::find(VALID_CMDS.begin(), VALID_CMDS.end(), deviceCommand) == VALID_CMDS.end()) {
2022-04-29 15:46:16 +02:00
return DeviceHandlerIF::COMMAND_NOT_IMPLEMENTED;
}
2022-06-21 16:44:14 +02:00
bool tempCheck = false;
2023-07-27 10:10:54 +02:00
if (commandDataLen >= 1) {
2022-06-21 16:44:14 +02:00
tempCheck = commandData[0];
2022-04-29 15:46:16 +02:00
}
2022-06-21 17:46:59 +02:00
if (commandActive) {
return DeviceHandlerIF::BUSY;
}
2022-04-29 15:46:16 +02:00
switch (deviceCommand) {
case (PING): {
2022-07-08 16:57:40 +02:00
finishCountdown.setTimeout(SHORT_CD);
// countdown starten
finishCountdown.resetTimer();
2022-06-21 16:44:14 +02:00
prepareScexCmd(cmdTyped, {cmdBuf.data(), cmdBuf.size()}, rawPacketLen, {nullptr, 0},
tempCheck);
break;
2022-04-29 15:46:16 +02:00
}
case (EXP_STATUS_CMD): {
2022-07-08 16:57:40 +02:00
finishCountdown.setTimeout(SHORT_CD);
// countdown starten
finishCountdown.resetTimer();
2022-06-21 16:44:14 +02:00
prepareScexCmd(cmdTyped, {cmdBuf.data(), cmdBuf.size()}, rawPacketLen, {nullptr, 0},
tempCheck);
break;
2022-04-29 15:46:16 +02:00
}
case (ION_CMD): {
2022-07-08 16:57:40 +02:00
finishCountdown.setTimeout(SHORT_CD);
// countdown starten
finishCountdown.resetTimer();
2022-06-21 16:44:14 +02:00
prepareScexCmd(cmdTyped, {cmdBuf.data(), cmdBuf.size()}, rawPacketLen, {nullptr, 0},
tempCheck);
break;
2022-04-29 15:46:16 +02:00
}
case (TEMP_CMD): {
2022-07-08 16:57:40 +02:00
finishCountdown.setTimeout(SHORT_CD);
// countdown starten
finishCountdown.resetTimer();
2022-06-21 16:44:14 +02:00
prepareScexCmd(cmdTyped, {cmdBuf.data(), cmdBuf.size()}, rawPacketLen, {nullptr, 0},
tempCheck);
break;
2022-04-29 15:46:16 +02:00
}
case (FRAM): {
2022-07-13 15:51:21 +02:00
finishCountdown.setTimeout(LONG_CD);
2022-07-08 16:57:40 +02:00
// countdown starten
finishCountdown.resetTimer();
2022-08-30 16:51:30 +02:00
if (debugMode) {
uint32_t remainingMillis = finishCountdown.getRemainingMillis();
sif::info << "ScexDeviceHandler::buildCommandFromCommand: RemainingMillis: "
<< remainingMillis << std::endl;
}
2022-11-09 13:07:20 +01:00
multiFileFinishOutstanding = true;
2022-06-21 16:44:14 +02:00
prepareScexCmd(cmdTyped, {cmdBuf.data(), cmdBuf.size()}, rawPacketLen,
{commandData + 1, commandDataLen - 1}, tempCheck);
2022-08-31 16:07:38 +02:00
updatePeriodicReply(true, deviceCommand);
2022-06-21 16:44:14 +02:00
break;
2022-04-29 15:46:16 +02:00
}
case (ONE_CELL): {
2022-07-08 16:57:40 +02:00
finishCountdown.setTimeout(LONG_CD);
// countdown starts
finishCountdown.resetTimer();
2022-11-09 13:07:20 +01:00
multiFileFinishOutstanding = true;
2022-06-21 16:44:14 +02:00
prepareScexCmd(cmdTyped, {cmdBuf.data(), cmdBuf.size()}, rawPacketLen,
{commandData + 1, commandDataLen - 1}, tempCheck);
2022-08-31 16:07:38 +02:00
updatePeriodicReply(true, deviceCommand);
2022-06-21 16:44:14 +02:00
break;
2022-04-29 15:46:16 +02:00
}
case (ALL_CELLS_CMD): {
2022-07-08 16:57:40 +02:00
finishCountdown.setTimeout(LONG_CD);
// countdown starts
finishCountdown.resetTimer();
2022-11-09 13:07:20 +01:00
multiFileFinishOutstanding = true;
2022-06-21 16:44:14 +02:00
prepareScexCmd(cmdTyped, {cmdBuf.data(), cmdBuf.size()}, rawPacketLen,
{commandData + 1, commandDataLen - 1}, tempCheck);
2022-08-31 16:07:38 +02:00
updatePeriodicReply(true, deviceCommand);
2022-06-21 16:44:14 +02:00
break;
}
default: {
return DeviceHandlerIF::COMMAND_NOT_IMPLEMENTED;
2022-04-29 15:46:16 +02:00
}
}
2022-06-21 17:46:59 +02:00
commandActive = true;
2022-06-21 16:44:14 +02:00
rawPacket = cmdBuf.data();
2022-08-29 16:05:05 +02:00
return OK;
2022-04-29 15:46:16 +02:00
}
void ScexDeviceHandler::fillCommandAndReplyMap() {
2022-10-06 16:58:43 +02:00
insertInCommandAndReplyMap(scex::Cmds::PING, 5, nullptr, 0, false, false, 0, &finishCountdown);
insertInCommandAndReplyMap(scex::Cmds::ION_CMD, 3, nullptr, 0, false, false, 0, &finishCountdown);
insertInCommandAndReplyMap(scex::Cmds::TEMP_CMD, 3, nullptr, 0, false, false, 0,
&finishCountdown);
insertInCommandAndReplyMap(scex::Cmds::EXP_STATUS_CMD, 3, nullptr, 0, false, false, 0,
&finishCountdown);
2022-04-29 15:46:16 +02:00
2022-08-31 16:07:38 +02:00
insertInCommandAndReplyMap(scex::Cmds::ALL_CELLS_CMD, 0, nullptr, 0, true, false,
2022-08-30 16:51:30 +02:00
scex::Cmds::ALL_CELLS_CMD, &finishCountdown);
2022-08-31 16:12:32 +02:00
insertInCommandAndReplyMap(scex::Cmds::ONE_CELL, 0, nullptr, 0, true, false, scex::Cmds::ONE_CELL,
&finishCountdown);
2022-08-31 16:07:38 +02:00
insertInCommandAndReplyMap(scex::Cmds::FRAM, 0, nullptr, 0, true, false, scex::Cmds::FRAM,
2022-08-30 16:51:30 +02:00
&finishCountdown);
2022-04-29 15:46:16 +02:00
insertInReplyMap(scex::Cmds::ERROR_REPLY, 3);
}
ReturnValue_t ScexDeviceHandler::scanForReply(const uint8_t* start, size_t remainingSize,
DeviceCommandId_t* foundId, size_t* foundLen) {
2022-06-21 17:46:59 +02:00
size_t len = remainingSize;
ReturnValue_t result = helper.deSerialize(&start, &len);
2022-05-19 10:16:46 +02:00
if (result == ScexHelper::INVALID_CRC) {
sif::warning << "ScexDeviceHandler::scanForReply: CRC invalid" << std::endl;
2022-08-30 16:51:30 +02:00
*foundLen = remainingSize;
2022-08-30 18:36:00 +02:00
} else {
result = handleValidReply(remainingSize, foundId, foundLen);
2022-05-19 10:16:46 +02:00
}
2022-08-30 18:36:00 +02:00
return result;
}
2022-08-30 16:51:30 +02:00
2022-08-30 18:36:00 +02:00
ReturnValue_t ScexDeviceHandler::handleValidReply(size_t remSize, DeviceCommandId_t* foundId,
2022-08-30 18:52:50 +02:00
size_t* foundLen) {
2022-08-30 18:36:00 +02:00
using namespace scex;
ReturnValue_t result = OK;
2022-08-31 16:07:38 +02:00
2022-08-30 16:51:30 +02:00
switch (helper.getCmd()) {
case (FRAM): {
if (debugMode) {
uint32_t remainingMillis = finishCountdown.getRemainingMillis();
2022-08-31 11:06:37 +02:00
sif::info << "ScexDeviceHandler::handleValidReply: RemMillis: " << remainingMillis
2022-08-30 16:51:30 +02:00
<< std::endl;
}
2022-08-31 16:07:38 +02:00
result = APERIODIC_REPLY;
2022-08-30 16:51:30 +02:00
break;
}
case (ONE_CELL): {
2022-08-31 16:07:38 +02:00
result = APERIODIC_REPLY;
2022-08-30 16:51:30 +02:00
break;
}
case (ALL_CELLS_CMD): {
2022-08-31 16:07:38 +02:00
result = APERIODIC_REPLY;
2022-08-30 18:36:00 +02:00
break;
}
default: {
2022-08-30 16:51:30 +02:00
break;
}
}
2022-11-09 13:07:20 +01:00
if (result == APERIODIC_REPLY and multiFileFinishOutstanding) {
finishAction(true, helper.getCmd(), OK);
multiFileFinishOutstanding = false;
}
2022-08-31 16:07:38 +02:00
*foundId = helper.getCmd();
2022-08-30 18:36:00 +02:00
*foundLen = remSize;
2022-08-30 16:51:30 +02:00
return result;
2022-04-29 15:46:16 +02:00
}
ReturnValue_t ScexDeviceHandler::interpretDeviceReply(DeviceCommandId_t id, const uint8_t* packet) {
2022-05-19 10:16:46 +02:00
using namespace scex;
2022-05-19 11:56:59 +02:00
2022-08-29 16:05:05 +02:00
ReturnValue_t status = OK;
2023-03-29 11:41:42 +02:00
auto multiFileHandler = [&](const char* cmdName) {
2022-08-31 16:07:38 +02:00
if ((helper.getPacketCounter() == 1) or (not fileNameSet)) {
2023-04-01 14:40:34 +02:00
status = generateNewScexFile(cmdName);
2023-04-01 15:33:02 +02:00
if (status != returnvalue::OK) {
2023-04-01 14:40:34 +02:00
return status;
}
fileNameSet = true;
2022-08-31 16:07:38 +02:00
} else {
2023-08-02 09:39:42 +02:00
if (!sdcMan.isSdCardUsable(std::nullopt)) {
fsUnsableEvent();
return returnvalue::FAILED;
}
// Append to existing file.
ofstream out(fileName, ofstream::binary | ofstream::app);
2022-08-31 16:07:38 +02:00
if (out.bad()) {
sif::error << "ScexDeviceHandler::handleValidReply: Could not open file " << fileName
<< std::endl;
return FAILED;
}
out << helper;
}
return OK;
};
2022-11-09 13:07:20 +01:00
id = helper.getCmd();
2022-05-19 11:56:59 +02:00
switch (id) {
case (PING): {
2023-04-01 14:23:11 +02:00
status = generateNewScexFile(PING_IDLE_BASE_NAME);
2022-06-21 17:46:59 +02:00
break;
2022-05-19 11:56:59 +02:00
}
case (ION_CMD): {
2023-04-01 14:23:11 +02:00
status = generateNewScexFile(ION_BASE_NAME);
2022-06-21 17:46:59 +02:00
break;
2022-05-19 11:56:59 +02:00
}
case (TEMP_CMD): {
2023-04-01 14:23:11 +02:00
status = generateNewScexFile(TEMPERATURE_BASE_NAME);
2022-06-21 17:46:59 +02:00
break;
2022-05-19 11:56:59 +02:00
}
case (EXP_STATUS_CMD): {
2023-04-01 14:23:11 +02:00
status = generateNewScexFile(EXP_STATUS_BASE_NAME);
2022-06-21 17:46:59 +02:00
break;
2022-05-19 11:56:59 +02:00
}
2022-08-31 16:12:32 +02:00
case (FRAM): {
2023-03-29 11:41:42 +02:00
status = multiFileHandler(FRAM_BASE_NAME);
2022-08-31 16:07:38 +02:00
break;
}
2022-08-31 16:12:32 +02:00
case (ONE_CELL): {
2023-03-29 11:41:42 +02:00
status = multiFileHandler(ONE_CELL_BASE_NAME);
2022-08-31 16:07:38 +02:00
break;
}
2022-08-31 16:12:32 +02:00
case (ALL_CELLS_CMD): {
2023-03-29 11:41:42 +02:00
status = multiFileHandler(ALL_CELLS_BASE_NAME);
2022-08-31 16:07:38 +02:00
break;
}
2022-05-19 11:56:59 +02:00
default:
// Unknown DeviceCommand
return DeviceHandlerIF::COMMAND_NOT_IMPLEMENTED;
}
2023-10-10 18:44:13 +02:00
if (helper.getPacketCounter() >= helper.getTotalPacketCounter()) {
2022-05-19 11:56:59 +02:00
reader.finish();
2022-06-21 17:46:59 +02:00
commandActive = false;
if (id != PING) {
fileNameSet = false;
2022-05-19 10:16:46 +02:00
}
2022-08-31 16:12:32 +02:00
if (id == FRAM or id == ALL_CELLS_CMD or id == ONE_CELL) {
2022-08-31 16:07:38 +02:00
triggerEvent(MULTI_PACKET_COMMAND_DONE, id);
updatePeriodicReply(false, id);
}
2022-05-19 10:16:46 +02:00
}
2022-08-30 16:51:30 +02:00
if (debugMode) {
uint32_t remainingMillis = finishCountdown.getRemainingMillis();
sif::info << __FILE__ << __func__ << "(" << __LINE__ << ") RemMillis: " << remainingMillis
<< std::endl;
}
2022-06-21 17:46:59 +02:00
return status;
}
2022-08-31 16:37:15 +02:00
void ScexDeviceHandler::performOperationHook() {
2023-08-02 09:20:36 +02:00
if (getMode() != MODE_OFF) {
filesystemChecks();
2023-02-22 18:06:34 +01:00
}
2022-08-31 16:37:15 +02:00
uint32_t remainingMillis = finishCountdown.getRemainingMillis();
if (commandActive and finishCountdown.hasTimedOut()) {
triggerEvent(scex::EXPERIMENT_TIMEDOUT, currCmd, 0);
reader.finish();
sif::warning << "ScexDeviceHandler::scanForReply: Reader timeout; RemMillis: "
<< remainingMillis << std::endl;
fileNameSet = false;
commandActive = false;
}
}
2022-04-29 15:46:16 +02:00
2023-12-06 11:31:39 +01:00
uint32_t ScexDeviceHandler::getTransitionDelayMs(Mode_t modeFrom, Mode_t modeTo) { return 5000; }
2022-04-29 15:46:16 +02:00
ReturnValue_t ScexDeviceHandler::getSwitches(const uint8_t** switches, uint8_t* numberOfSwitches) {
2022-09-27 18:54:48 +02:00
if (switchId) {
*numberOfSwitches = 1;
*switches = &switchId.value();
}
2022-08-29 16:05:05 +02:00
return OK;
2022-04-29 15:46:16 +02:00
}
ReturnValue_t ScexDeviceHandler::initializeLocalDataPool(localpool::DataPool& localDataPoolMap,
LocalDataPoolManager& poolManager) {
2022-08-29 16:05:05 +02:00
return OK;
2022-04-29 15:46:16 +02:00
}
2023-08-02 09:20:36 +02:00
void ScexDeviceHandler::filesystemChecks() {
auto mntPrefix = sdcMan.getCurrentMountPrefix();
if (mntPrefix == nullptr or !sdcMan.isSdCardUsable(std::nullopt)) {
sif::warning << "SCEX: Filesystem currently unavailable" << std::endl;
2023-08-02 09:44:22 +02:00
fsUnsableEvent();
2023-08-02 09:20:36 +02:00
} else {
std::filesystem::path fullFilePath = mntPrefix;
std::error_code e;
fullFilePath /= "scex";
bool fileExists = std::filesystem::exists(fullFilePath, e);
if (not fileExists) {
bool created = std::filesystem::create_directory(fullFilePath, e);
if (not created) {
sif::error << "Could not create SCEX directory: " << e << std::endl;
}
}
}
}
2023-08-02 09:39:42 +02:00
void ScexDeviceHandler::fsUnsableEvent() {
if (fsUnusableEventCd.isBusy()) {
return;
}
triggerEvent(scex::FS_UNUSABLE);
fsUnusableEventCd.resetTimer();
}
2023-04-01 14:23:11 +02:00
ReturnValue_t ScexDeviceHandler::generateNewScexFile(const char* cmdName) {
char timeString[64]{};
auto activeSd = sdcMan.getActiveSdCard();
if (not activeSd) {
return HasFileSystemIF::FILESYSTEM_INACTIVE;
}
std::ostringstream oss;
auto prefix = sdcMan.getCurrentMountPrefix();
2023-08-02 09:39:42 +02:00
if (prefix == nullptr or !sdcMan.isSdCardUsable(std::nullopt)) {
fsUnsableEvent();
2023-04-01 14:23:11 +02:00
return returnvalue::FAILED;
}
timeval tv;
Clock::getClock_timeval(&tv);
time_t epoch = tv.tv_sec;
struct tm* time = gmtime(&epoch);
size_t writtenBytes = strftime(reinterpret_cast<char*>(timeString), sizeof(timeString),
config::FILE_DATE_FORMAT, time);
if (writtenBytes == 0) {
sif::error << "PersistentTmStore::createMostRecentFile: Could not create file timestamp"
<< std::endl;
return returnvalue::FAILED;
}
oss << prefix << "/scex/scex-" << cmdName << "-" << timeString << ".bin";
fileName = oss.str();
ofstream out(fileName, ofstream::binary);
if (out.bad()) {
sif::error << "ScexDeviceHandler::interpretDeviceReply: Could not open file " << fileName
<< std::endl;
return FAILED;
}
out << helper;
return OK;
}
2022-09-27 18:54:48 +02:00
void ScexDeviceHandler::setPowerSwitcher(PowerSwitchIF& powerSwitcher, power::Switch_t switchId) {
DeviceHandlerBase::setPowerSwitcher(&powerSwitcher);
this->switchId = switchId;
2022-09-29 19:38:19 +02:00
}
ReturnValue_t ScexDeviceHandler::initializeAfterTaskCreation() {
2022-10-04 23:04:50 +02:00
return DeviceHandlerBase::initializeAfterTaskCreation();
}