eive-obsw/mission/devices/ScexDeviceHandler.cpp

224 lines
6.9 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>
2022-05-19 10:16:46 +02:00
#include <iostream>
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-05-19 11:56:59 +02:00
ScexDeviceHandler::ScexDeviceHandler(object_id_t objectId, ScexUartReader& reader, CookieIF* cookie,
SdCardMountedIF* sdcMan)
: DeviceHandlerBase(objectId, reader.getObjectId(), cookie), reader(reader), sdcMan(sdcMan) {}
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
void ScexDeviceHandler::doStartUp() {
// mode on
setMode(MODE_ON);
}
void ScexDeviceHandler::doShutDown() { setMode(_MODE_POWER_DOWN); }
ReturnValue_t ScexDeviceHandler::buildNormalDeviceCommand(DeviceCommandId_t* id) {
return RETURN_OK;
}
ReturnValue_t ScexDeviceHandler::buildTransitionDeviceCommand(DeviceCommandId_t* id) {
return RETURN_OK;
}
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;
}
if (commandDataLen < 1) {
return DeviceHandlerIF::INVALID_COMMAND_PARAMETER;
}
switch (deviceCommand) {
case (PING): {
2022-05-19 11:56:59 +02:00
rawPacket = cmdBuf.data();
prepareScexCmd(cmdTyped, commandData[0], {cmdBuf.data(), cmdBuf.size()}, rawPacketLen,
2022-04-29 15:46:16 +02:00
{nullptr, 0});
return RETURN_OK;
}
case (EXP_STATUS_CMD): {
2022-05-19 11:56:59 +02:00
prepareScexCmd(cmdTyped, commandData[0], {cmdBuf.data(), cmdBuf.size()}, rawPacketLen,
2022-04-29 15:46:16 +02:00
{nullptr, 0});
return RETURN_OK;
}
case (ION_CMD): {
2022-05-19 11:56:59 +02:00
prepareScexCmd(cmdTyped, commandData[0], {cmdBuf.data(), cmdBuf.size()}, rawPacketLen,
2022-04-29 15:46:16 +02:00
{nullptr, 0});
return RETURN_OK;
}
case (TEMP_CMD): {
2022-05-19 11:56:59 +02:00
prepareScexCmd(cmdTyped, commandData[0], {cmdBuf.data(), cmdBuf.size()}, rawPacketLen,
2022-04-29 15:46:16 +02:00
{nullptr, 0});
return RETURN_OK;
}
case (FRAM): {
2022-05-19 11:56:59 +02:00
prepareScexCmd(cmdTyped, commandData[0], {cmdBuf.data(), cmdBuf.size()}, rawPacketLen,
{commandData + 1, commandDataLen - 1});
2022-04-29 15:46:16 +02:00
return RETURN_OK;
}
case (ONE_CELL): {
2022-05-19 11:56:59 +02:00
prepareScexCmd(cmdTyped, commandData[0], {cmdBuf.data(), cmdBuf.size()}, rawPacketLen,
{commandData + 1, commandDataLen - 1});
2022-04-29 15:46:16 +02:00
return RETURN_OK;
}
case (ALL_CELLS_CMD): {
2022-05-19 11:56:59 +02:00
prepareScexCmd(cmdTyped, commandData[0], {cmdBuf.data(), cmdBuf.size()}, rawPacketLen,
{commandData + 1, commandDataLen - 1});
2022-04-29 15:46:16 +02:00
return RETURN_OK;
}
}
return RETURN_OK;
}
void ScexDeviceHandler::fillCommandAndReplyMap() {
insertInCommandAndReplyMap(scex::Cmds::PING, 3);
insertInCommandAndReplyMap(scex::Cmds::ION_CMD, 3);
insertInCommandAndReplyMap(scex::Cmds::TEMP_CMD, 3);
insertInCommandAndReplyMap(scex::Cmds::EXP_STATUS_CMD, 3);
insertInCommandMap(scex::Cmds::ALL_CELLS_CMD);
insertInCommandMap(scex::Cmds::ONE_CELL);
insertInCommandMap(scex::Cmds::FRAM);
insertInReplyMap(scex::Cmds::ERROR_REPLY, 3);
}
ReturnValue_t ScexDeviceHandler::scanForReply(const uint8_t* start, size_t remainingSize,
DeviceCommandId_t* foundId, size_t* foundLen) {
2022-05-19 09:59:33 +02:00
uint8_t* decodedPacket = nullptr;
size_t len = 0;
const uint8_t* helperPtr = decodedPacket;
ReturnValue_t result = helper.deSerialize(&helperPtr, &len);
2022-05-19 10:16:46 +02:00
if (result == ScexHelper::INVALID_CRC) {
sif::warning << "ScexDeviceHandler::scanForReply: CRC invalid" << std::endl;
2022-05-19 11:56:59 +02:00
return result;
2022-05-19 10:16:46 +02:00
}
2022-05-19 09:59:33 +02:00
*foundId = helper.getCmd();
*foundLen = remainingSize;
2022-04-29 15:46:16 +02:00
return RETURN_OK;
}
ReturnValue_t ScexDeviceHandler::interpretDeviceReply(DeviceCommandId_t id, const uint8_t* packet) {
2022-05-19 09:59:33 +02:00
// cmd auswertung (in file reinschreiben)
2022-05-19 10:16:46 +02:00
using namespace scex;
2022-05-19 11:56:59 +02:00
auto oneFileHandler = [&](std::string cmdName) {
fileId = random_string(6);
std::ostringstream oss("/tmp/scex-");
oss << cmdName << fileId << ".bin";
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;
return RETURN_FAILED;
}
if (debugMode) {
out << helper;
}
return RETURN_OK;
};
auto multiFileHandler = [&](std::string cmdName) {
if ((helper.getPacketCounter() == 1) or (not fileNameSet)) {
2022-05-19 11:56:59 +02:00
// countdown starten
finishCountdown.resetTimer();
2022-05-19 11:56:59 +02:00
fileId = random_string(6);
std::ostringstream oss("/tmp/scex-");
oss << cmdName << fileId << ".bin";
fileName = oss.str();
fileNameSet = true;
ofstream out(fileName, ofstream::binary);
if (out.bad()) {
sif::error << "ScexDeviceHandler::interpretDeviceReply: Could not open file " << fileName
<< std::endl;
return RETURN_FAILED;
}
2022-05-19 11:56:59 +02:00
} else {
ofstream out(fileName,
ofstream::binary | ofstream::app); // append
2022-05-19 11:56:59 +02:00
if (debugMode) {
out << helper;
}
if (finishCountdown.hasTimedOut()) {
triggerEvent(scex::EXPERIMENT_TIMEDOUT, id, 0);
reader.finish();
sif::warning << "ScexDeviceHandler::interpretDiviceReply: Reader timeout" << std::endl;
fileNameSet = false;
}
2022-05-19 11:56:59 +02:00
}
2022-05-19 11:56:59 +02:00
return RETURN_OK;
};
switch (id) {
case (PING): {
return oneFileHandler("ping_");
}
case (ION_CMD): {
return oneFileHandler("ion_");
}
case (TEMP_CMD): {
return oneFileHandler("temp_");
}
case (EXP_STATUS_CMD): {
return oneFileHandler("exp_status_");
}
case (FRAM): {
return multiFileHandler("fram_");
}
case (ONE_CELL): {
return multiFileHandler("one_cell_");
}
case (ALL_CELLS_CMD): {
return multiFileHandler("all_cell_");
}
default:
// Unknown DeviceCommand
return DeviceHandlerIF::COMMAND_NOT_IMPLEMENTED;
}
if (helper.getPacketCounter() == helper.getTotalPacketCounter()) {
reader.finish();
if (id != PING) {
sif::info << "Reader is finished" << std::endl;
fileNameSet = false;
2022-05-19 10:16:46 +02:00
}
}
2022-04-29 15:46:16 +02:00
return RETURN_OK;
}
uint32_t ScexDeviceHandler::getTransitionDelayMs(Mode_t modeFrom, Mode_t modeTo) {
return RETURN_OK;
}
ReturnValue_t ScexDeviceHandler::getSwitches(const uint8_t** switches, uint8_t* numberOfSwitches) {
return RETURN_OK;
}
ReturnValue_t ScexDeviceHandler::initializeLocalDataPool(localpool::DataPool& localDataPoolMap,
LocalDataPoolManager& poolManager) {
return RETURN_OK;
}
void ScexDeviceHandler::modeChanged() {}