eive-obsw/mission/devices/ScexDeviceHandler.cpp

284 lines
8.6 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-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-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
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;
}
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-06-21 16:44:14 +02:00
prepareScexCmd(cmdTyped, {cmdBuf.data(), cmdBuf.size()}, rawPacketLen,
{commandData + 1, commandDataLen - 1}, tempCheck);
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);
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);
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-04-29 15:46:16 +02:00
return RETURN_OK;
}
void ScexDeviceHandler::fillCommandAndReplyMap() {
2022-06-21 17:46:59 +02:00
insertInCommandAndReplyMap(scex::Cmds::PING, 5);
2022-04-29 15:46:16 +02:00
insertInCommandAndReplyMap(scex::Cmds::ION_CMD, 3);
insertInCommandAndReplyMap(scex::Cmds::TEMP_CMD, 3);
insertInCommandAndReplyMap(scex::Cmds::EXP_STATUS_CMD, 3);
2022-07-08 17:11:21 +02:00
insertInCommandAndReplyMap(scex::Cmds::ALL_CELLS_CMD, 0, nullptr, 0, false, false,
2022-07-13 15:51:21 +02:00
scex::Cmds::ALL_CELLS_CMD, &finishCountdown);
2022-07-08 17:11:21 +02:00
insertInCommandAndReplyMap(scex::Cmds::ONE_CELL, 0, nullptr, 0, false, false,
2022-07-13 15:51:21 +02:00
scex::Cmds::ONE_CELL, &finishCountdown);
2022-07-08 17:11:21 +02:00
insertInCommandAndReplyMap(scex::Cmds::FRAM, 0, nullptr, 0, false, false,
2022-07-13 15:51:21 +02:00
scex::Cmds::FRAM, &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-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
2022-06-21 17:46:59 +02:00
ReturnValue_t status = RETURN_OK;
2022-05-19 11:56:59 +02:00
auto oneFileHandler = [&](std::string cmdName) {
fileId = random_string(6);
2022-07-13 11:25:53 +02:00
std::ostringstream oss("/tmp/scex-", std::ostringstream::ate);
2022-05-19 11:56:59 +02:00
oss << cmdName << fileId << ".bin";
fileName = oss.str();
2022-07-13 11:25:53 +02:00
std::cout << fileName << std::endl;
2022-05-19 11:56:59 +02:00
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
fileId = random_string(6);
2022-07-13 11:25:53 +02:00
std::ostringstream oss("/tmp/scex-", std::ostringstream::ate);
2022-05-19 11:56:59 +02:00
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;
}
}
2022-05-19 11:56:59 +02:00
return RETURN_OK;
};
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
}
case (FRAM): {
2022-06-21 17:46:59 +02:00
status = multiFileHandler("fram_");
break;
2022-05-19 11:56:59 +02:00
}
case (ONE_CELL): {
2022-06-21 17:46:59 +02:00
status = multiFileHandler("one_cell_");
break;
2022-05-19 11:56:59 +02:00
}
case (ALL_CELLS_CMD): {
2022-06-21 17:46:59 +02:00
status = multiFileHandler("all_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) {
sif::info << "Reader is finished" << std::endl;
fileNameSet = false;
2022-05-19 10:16:46 +02:00
}
}
2022-06-21 17:46:59 +02:00
return status;
}
void ScexDeviceHandler::performOperationHook() {
if (commandActive and finishCountdown.hasTimedOut()) {
triggerEvent(scex::EXPERIMENT_TIMEDOUT, currCmd, 0);
reader.finish();
2022-06-30 21:53:02 +02:00
sif::warning << "ScexDeviceHandler::performOperationHook: Reader timeout" << std::endl;
2022-06-21 17:46:59 +02:00
fileNameSet = false;
commandActive = false;
}
2022-04-29 15:46:16 +02:00
}
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;
}
2022-05-29 17:52:13 +02:00
std::string ScexDeviceHandler::random_string(std::string::size_type length) {
static auto& chrs =
"0123456789"
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
thread_local static std::mt19937 rg{std::random_device{}()};
thread_local static std::uniform_int_distribution<std::string::size_type> pick(0,
sizeof(chrs) - 2);
std::string s;
s.reserve(length);
while (length--) s += chrs[pick(rg)];
return s;
}
2022-04-29 15:46:16 +02:00
void ScexDeviceHandler::modeChanged() {}