eive-obsw/mission/devices/ScexDeviceHandler.cpp

370 lines
12 KiB
C++
Raw Normal View History

2022-04-29 15:46:16 +02:00
#include "ScexDeviceHandler.h"
2022-05-19 09:59:33 +02:00
#include <linux/devices/ScexHelper.h>
2022-05-19 11:56:59 +02:00
#include <mission/memory/SdCardMountedIF.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
#include "fsfw/globalfunctions/CRC.h"
2022-05-19 09:59:33 +02:00
#include "mission/devices/devicedefinitions/ScexDefinitions.h"
2022-04-29 15:46:16 +02:00
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)
2022-06-21 16:44:14 +02:00
: DeviceHandlerBase(objectId, reader.getObjectId(), cookie), sdcMan(sdcMan), reader(reader) {}
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
2022-09-27 18:54:48 +02:00
void ScexDeviceHandler::doStartUp() { 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;
2022-10-04 23:04:50 +02:00
setMode(_MODE_POWER_DOWN);
}
2022-04-29 15:46:16 +02:00
2022-08-30 16:51:30 +02:00
ReturnValue_t ScexDeviceHandler::buildNormalDeviceCommand(DeviceCommandId_t* id) { return OK; }
2022-04-29 15:46:16 +02:00
2022-08-30 16:51:30 +02:00
ReturnValue_t ScexDeviceHandler::buildTransitionDeviceCommand(DeviceCommandId_t* id) { return OK; }
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;
if (commandDataLen == 1) {
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-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-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-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-10-10 18:00:43 +02:00
finishAction(true, helper.getCmd(), OK);
2022-08-31 16:07:38 +02:00
result = APERIODIC_REPLY;
2022-08-30 16:51:30 +02:00
break;
}
case (ONE_CELL): {
2022-10-10 18:00:43 +02:00
finishAction(true, helper.getCmd(), OK);
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-10-10 18:00:43 +02:00
finishAction(true, helper.getCmd(), OK);
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-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;
2022-05-19 11:56:59 +02:00
auto oneFileHandler = [&](std::string cmdName) {
fileId = date_time_string();
std::ostringstream oss;
auto prefix = sdcMan.getCurrentMountPrefix();
2022-09-29 19:38:19 +02:00
oss << prefix << "/scex/scex-" << cmdName << fileId << ".bin";
2022-05-19 11:56:59 +02:00
fileName = oss.str();
ofstream out(fileName, ofstream::binary);
2022-05-19 10:16:46 +02:00
if (out.bad()) {
2022-05-19 11:56:59 +02:00
sif::error << "ScexDeviceHandler::interpretDeviceReply: Could not open file " << fileName
<< std::endl;
2022-08-29 16:05:05 +02:00
return FAILED;
2022-05-19 11:56:59 +02:00
}
2022-08-30 18:36:00 +02:00
out << helper;
2022-08-29 16:05:05 +02:00
return OK;
2022-05-19 11:56:59 +02:00
};
2022-08-31 16:07:38 +02:00
auto multiFileHandler = [&](std::string cmdName) {
if ((helper.getPacketCounter() == 1) or (not fileNameSet)) {
fileId = date_time_string();
std::ostringstream oss;
auto prefix = sdcMan.getCurrentMountPrefix();
2022-09-29 19:38:19 +02:00
oss << prefix << "/scex/scex-" << cmdName << fileId << ".bin";
2022-08-31 16:07:38 +02:00
fileName = oss.str();
fileNameSet = true;
ofstream out(fileName, ofstream::binary);
if (out.bad()) {
sif::error << "ScexDeviceHandler::handleValidReply: Could not open file " << fileName
<< std::endl;
return FAILED;
}
out << helper;
} else {
ofstream out(fileName,
ofstream::binary | ofstream::app); // append
if (out.bad()) {
sif::error << "ScexDeviceHandler::handleValidReply: Could not open file " << fileName
<< std::endl;
return FAILED;
}
out << helper;
}
return OK;
};
2022-05-19 11:56:59 +02:00
switch (id) {
case (PING): {
2022-06-21 17:46:59 +02:00
status = oneFileHandler("ping_");
break;
2022-05-19 11:56:59 +02:00
}
case (ION_CMD): {
2022-06-21 17:46:59 +02:00
status = oneFileHandler("ion_");
break;
2022-05-19 11:56:59 +02:00
}
case (TEMP_CMD): {
2022-06-21 17:46:59 +02:00
status = oneFileHandler("temp_");
break;
2022-05-19 11:56:59 +02:00
}
case (EXP_STATUS_CMD): {
2022-06-21 17:46:59 +02:00
status = oneFileHandler("exp_status_");
break;
2022-05-19 11:56:59 +02:00
}
2022-08-31 16:12:32 +02:00
case (FRAM): {
2022-08-31 16:07:38 +02:00
status = multiFileHandler("fram_");
break;
}
2022-08-31 16:12:32 +02:00
case (ONE_CELL): {
2022-08-31 16:07:38 +02:00
status = multiFileHandler("one_cell_");
break;
}
2022-08-31 16:12:32 +02:00
case (ALL_CELLS_CMD): {
2022-08-31 16:07:38 +02:00
status = multiFileHandler("multi_cell_");
break;
}
2022-05-19 11:56:59 +02:00
default:
// Unknown DeviceCommand
return DeviceHandlerIF::COMMAND_NOT_IMPLEMENTED;
}
if (helper.getPacketCounter() == helper.getTotalPacketCounter()) {
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() {
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
2022-08-30 16:51:30 +02:00
uint32_t ScexDeviceHandler::getTransitionDelayMs(Mode_t modeFrom, Mode_t modeTo) { return OK; }
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
}
std::string ScexDeviceHandler::date_time_string() {
using namespace std;
string date_time;
2022-09-12 17:15:02 +02:00
Clock::TimeOfDay_t tod;
Clock::getDateAndTime(&tod);
ostringstream oss(std::ostringstream::ate);
2022-05-29 17:52:13 +02:00
if (tod.hour < 10) {
oss << tod.year << tod.month << tod.day << "_0" << tod.hour;
} else {
oss << tod.year << tod.month << tod.day << "_" << tod.hour;
}
if (tod.minute < 10) {
oss << 0 << tod.minute;
2022-09-12 17:15:02 +02:00
} else {
oss << tod.minute;
}
if (tod.second < 10) {
oss << 0 << tod.second;
} else {
oss << tod.second;
}
2022-05-29 17:52:13 +02:00
date_time = oss.str();
2022-05-29 17:52:13 +02:00
return date_time;
2022-05-29 17:52:13 +02:00
}
2022-04-29 15:46:16 +02:00
void ScexDeviceHandler::modeChanged() {}
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
auto mntPrefix = sdcMan.getCurrentMountPrefix();
std::filesystem::path fullFilePath = mntPrefix;
fullFilePath /= "scex";
bool fileExists = std::filesystem::exists(fullFilePath);
2022-09-29 19:50:12 +02:00
2022-10-04 23:04:50 +02:00
if (not fileExists) {
std::filesystem::create_directory(fullFilePath);
}
return DeviceHandlerBase::initializeAfterTaskCreation();
}