WIP: SCEX Init #288
@ -35,6 +35,7 @@ ReturnValue_t ScexHelper::deSerialize(const uint8_t** buffer, size_t* size,
|
|||||||
payloadLen = (**buffer << 8) | *(*buffer + 1);
|
payloadLen = (**buffer << 8) | *(*buffer + 1);
|
||||||
|
|
||||||
*buffer += 2;
|
*buffer += 2;
|
||||||
|
payloadStart = *buffer;
|
||||||
totalPacketLen = payloadLen + scex::HEADER_LEN + scex::CRC_LEN;
|
totalPacketLen = payloadLen + scex::HEADER_LEN + scex::CRC_LEN;
|
||||||
if (totalPacketLen >= *size) {
|
if (totalPacketLen >= *size) {
|
||||||
return STREAM_TOO_SHORT;
|
return STREAM_TOO_SHORT;
|
||||||
|
@ -41,6 +41,7 @@ class ScexHelper : public HasReturnvaluesIF, public SerializeIF {
|
|||||||
int packetCounter = 0;
|
int packetCounter = 0;
|
||||||
int totalPacketCounter = 0;
|
int totalPacketCounter = 0;
|
||||||
uint16_t payloadLen = 0;
|
uint16_t payloadLen = 0;
|
||||||
|
const uint8_t *payloadStart = 0;
|
||||||
size_t totalPacketLen = 0;
|
size_t totalPacketLen = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#include "ScexDeviceHandler.h"
|
#include "ScexDeviceHandler.h"
|
||||||
|
|
||||||
#include <linux/devices/ScexHelper.h>
|
#include <linux/devices/ScexHelper.h>
|
||||||
|
#include <mission/memory/SdCardMountedIF.h>
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
@ -10,8 +11,9 @@
|
|||||||
|
|
||||||
using std::ofstream;
|
using std::ofstream;
|
||||||
|
|
||||||
ScexDeviceHandler::ScexDeviceHandler(object_id_t objectId, ScexUartReader& reader, CookieIF* cookie)
|
ScexDeviceHandler::ScexDeviceHandler(object_id_t objectId, ScexUartReader& reader, CookieIF* cookie,
|
||||||
: DeviceHandlerBase(objectId, reader.getObjectId(), cookie), reader(reader) {}
|
SdCardMountedIF* sdcMan)
|
||||||
|
: DeviceHandlerBase(objectId, reader.getObjectId(), cookie), reader(reader), sdcMan(sdcMan) {}
|
||||||
|
|
||||||
ScexDeviceHandler::~ScexDeviceHandler() {}
|
ScexDeviceHandler::~ScexDeviceHandler() {}
|
||||||
|
|
||||||
@ -31,11 +33,12 @@ ReturnValue_t ScexDeviceHandler::buildTransitionDeviceCommand(DeviceCommandId_t*
|
|||||||
}
|
}
|
||||||
|
|
||||||
ReturnValue_t ScexDeviceHandler::buildCommandFromCommand(
|
ReturnValue_t ScexDeviceHandler::buildCommandFromCommand(
|
||||||
scex::Cmds deviceCommand, // DeviceCommandId_t
|
DeviceCommandId_t deviceCommand, // DeviceCommandId_t
|
||||||
const uint8_t* commandData, size_t commandDataLen) {
|
const uint8_t* commandData, size_t commandDataLen) {
|
||||||
using namespace scex;
|
using namespace scex;
|
||||||
|
|
||||||
if (not std::find(VALID_CMDS.begin(), VALID_CMDS.end(), deviceCommand) != VALID_CMDS.end()) {
|
auto cmdTyped = static_cast<scex::Cmds>(deviceCommand);
|
||||||
|
if (std::find(VALID_CMDS.begin(), VALID_CMDS.end(), deviceCommand) == VALID_CMDS.end()) {
|
||||||
return DeviceHandlerIF::COMMAND_NOT_IMPLEMENTED;
|
return DeviceHandlerIF::COMMAND_NOT_IMPLEMENTED;
|
||||||
}
|
}
|
||||||
if (commandDataLen < 1) {
|
if (commandDataLen < 1) {
|
||||||
@ -44,39 +47,38 @@ ReturnValue_t ScexDeviceHandler::buildCommandFromCommand(
|
|||||||
|
|
||||||
switch (deviceCommand) {
|
switch (deviceCommand) {
|
||||||
case (PING): {
|
case (PING): {
|
||||||
rawPacket = cmdBuf.size();
|
rawPacket = cmdBuf.data();
|
||||||
|
prepareScexCmd(cmdTyped, commandData[0], {cmdBuf.data(), cmdBuf.size()}, rawPacketLen,
|
||||||
prepareScexCmd(deviceCommand, commandData[0], {cmdBuf.data(), cmdBuf.size()}, rawPacketLen,
|
|
||||||
{nullptr, 0});
|
{nullptr, 0});
|
||||||
return RETURN_OK;
|
return RETURN_OK;
|
||||||
}
|
}
|
||||||
case (FRAM): {
|
case (FRAM): {
|
||||||
prepareScexCmd(deviceCommand, commandData[0], {cmdBuf.data(), cmdBuf.size()}, rawPacketLen,
|
prepareScexCmd(cmdTyped, commandData[0], {cmdBuf.data(), cmdBuf.size()}, rawPacketLen,
|
||||||
{nullptr, 0});
|
{nullptr, 0});
|
||||||
return RETURN_OK;
|
return RETURN_OK;
|
||||||
}
|
}
|
||||||
case (ION_CMD): {
|
case (ION_CMD): {
|
||||||
prepareScexCmd(deviceCommand, commandData[0], {cmdBuf.data(), cmdBuf.size()}, rawPacketLen,
|
prepareScexCmd(cmdTyped, commandData[0], {cmdBuf.data(), cmdBuf.size()}, rawPacketLen,
|
||||||
{nullptr, 0});
|
{nullptr, 0});
|
||||||
return RETURN_OK;
|
return RETURN_OK;
|
||||||
}
|
}
|
||||||
case (TEMP_CMD): {
|
case (TEMP_CMD): {
|
||||||
prepareScexCmd(deviceCommand, commandData[0], {cmdBuf.data(), cmdBuf.size()}, rawPacketLen,
|
prepareScexCmd(cmdTyped, commandData[0], {cmdBuf.data(), cmdBuf.size()}, rawPacketLen,
|
||||||
{nullptr, 0});
|
{nullptr, 0});
|
||||||
return RETURN_OK;
|
return RETURN_OK;
|
||||||
}
|
}
|
||||||
case (ONE_CELL): {
|
case (ONE_CELL): {
|
||||||
prepareScexCmd(deviceCommand, commandData[0], {cmdBuf.data(), cmdBuf.size()}, rawPacketLen,
|
prepareScexCmd(cmdTyped, commandData[0], {cmdBuf.data(), cmdBuf.size()}, rawPacketLen,
|
||||||
{commandData + 1, commandData - 1});
|
{commandData + 1, commandDataLen - 1});
|
||||||
return RETURN_OK;
|
return RETURN_OK;
|
||||||
}
|
}
|
||||||
case (ALL_CELLS_CMD): {
|
case (ALL_CELLS_CMD): {
|
||||||
prepareScexCmd(deviceCommand, commandData[0], {cmdBuf.data(), cmdBuf.size()}, rawPacketLen,
|
prepareScexCmd(cmdTyped, commandData[0], {cmdBuf.data(), cmdBuf.size()}, rawPacketLen,
|
||||||
{commandData + 1, commandData - 1});
|
{commandData + 1, commandDataLen - 1});
|
||||||
return RETURN_OK;
|
return RETURN_OK;
|
||||||
}
|
}
|
||||||
case (EXP_STATUS_CMD): {
|
case (EXP_STATUS_CMD): {
|
||||||
prepareScexCmd(deviceCommand, commandData[0], {cmdBuf.data(), cmdBuf.size()}, rawPacketLen,
|
prepareScexCmd(cmdTyped, commandData[0], {cmdBuf.data(), cmdBuf.size()}, rawPacketLen,
|
||||||
{nullptr, 0});
|
{nullptr, 0});
|
||||||
return RETURN_OK;
|
return RETURN_OK;
|
||||||
}
|
}
|
||||||
@ -111,9 +113,8 @@ ReturnValue_t ScexDeviceHandler::scanForReply(const uint8_t* start, size_t remai
|
|||||||
// crc check
|
// crc check
|
||||||
if (result == ScexHelper::INVALID_CRC) {
|
if (result == ScexHelper::INVALID_CRC) {
|
||||||
sif::warning << "CRC invalid" << std::endl;
|
sif::warning << "CRC invalid" << std::endl;
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
sif::info << helper << std::endl;
|
|
||||||
|
|
||||||
*foundId = helper.getCmd();
|
*foundId = helper.getCmd();
|
||||||
*foundLen = remainingSize;
|
*foundLen = remainingSize;
|
||||||
|
|
||||||
@ -123,14 +124,86 @@ ReturnValue_t ScexDeviceHandler::scanForReply(const uint8_t* start, size_t remai
|
|||||||
ReturnValue_t ScexDeviceHandler::interpretDeviceReply(DeviceCommandId_t id, const uint8_t* packet) {
|
ReturnValue_t ScexDeviceHandler::interpretDeviceReply(DeviceCommandId_t id, const uint8_t* packet) {
|
||||||
// cmd auswertung (in file reinschreiben)
|
// cmd auswertung (in file reinschreiben)
|
||||||
using namespace scex;
|
using namespace scex;
|
||||||
if (helper.getCmd() == PING) {
|
|
||||||
ofstream out("/tmp/scex-ping.bin", ofstream::binary);
|
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);
|
||||||
if (out.bad()) {
|
if (out.bad()) {
|
||||||
sif::warning << "bad" << std::endl;
|
sif::error << "ScexDeviceHandler::interpretDeviceReply: Could not open file " << fileName
|
||||||
|
<< std::endl;
|
||||||
|
return RETURN_FAILED;
|
||||||
}
|
}
|
||||||
|
if (debugMode) {
|
||||||
out << helper;
|
out << helper;
|
||||||
}
|
}
|
||||||
// was alles hier rein?
|
return RETURN_OK;
|
||||||
|
};
|
||||||
|
auto multiFileHandler = [&](std::string cmdName) {
|
||||||
|
if (helper.getPacketCounter() == 1) {
|
||||||
|
// countdown starten
|
||||||
|
finishCountdown.resetTimer();
|
||||||
|
fileId = random_string(6);
|
||||||
|
std::ostringstream oss("/tmp/scex-");
|
||||||
|
oss << cmdName << fileId << ".bin";
|
||||||
|
fileName = oss.str();
|
||||||
|
ofstream out(fileName,
|
||||||
|
ofstream::binary); // neues file anlegen
|
||||||
|
} else {
|
||||||
|
ofstream out(fileName,
|
||||||
|
ofstream::binary | ofstream::app); // an bestehendes file appenden
|
||||||
|
if (debugMode) {
|
||||||
|
out << helper;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (finishCountdown.hasTimedOut()) {
|
||||||
|
triggerEvent(scex::EXPERIMENT_TIMEDOUT, id, 0);
|
||||||
|
reader.finish();
|
||||||
|
sif::warning << "ScexDeviceHandler: Reader timeout" << std::endl;
|
||||||
|
// cmdDone = true;
|
||||||
|
fileNameSet = false;
|
||||||
|
}
|
||||||
|
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();
|
||||||
|
sif::info << "Reader is finished" << std::endl;
|
||||||
|
// cmdDone = true;
|
||||||
|
fileNameSet = false;
|
||||||
|
if (helper.getCmd() == scex::Cmds::PING) {
|
||||||
|
// cmdSent = false;
|
||||||
|
fileNameSet = true; // to not generate everytime new file
|
||||||
|
}
|
||||||
|
}
|
||||||
return RETURN_OK;
|
return RETURN_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,15 +5,29 @@
|
|||||||
#include <linux/devices/ScexHelper.h>
|
#include <linux/devices/ScexHelper.h>
|
||||||
#include <linux/devices/ScexUartReader.h>
|
#include <linux/devices/ScexUartReader.h>
|
||||||
|
|
||||||
|
class SdCardMountedIF;
|
||||||
|
|
||||||
class ScexDeviceHandler : public DeviceHandlerBase {
|
class ScexDeviceHandler : public DeviceHandlerBase {
|
||||||
public:
|
public:
|
||||||
// ctor vervollständigen
|
// ctor vervollständigen
|
||||||
ScexDeviceHandler(object_id_t objectId, ScexUartReader &reader, CookieIF *cookie);
|
ScexDeviceHandler(object_id_t objectId, ScexUartReader &reader, CookieIF *cookie,
|
||||||
|
SdCardMountedIF *sdcMan);
|
||||||
virtual ~ScexDeviceHandler();
|
virtual ~ScexDeviceHandler();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::array<uint8_t, 64> cmdBuf = {};
|
std::array<uint8_t, 64> cmdBuf = {};
|
||||||
|
|
||||||
|
std::string fileId = "";
|
||||||
|
std::string fileName = "";
|
||||||
|
bool fileNameSet = false;
|
||||||
|
bool debugMode = true;
|
||||||
|
|
||||||
|
scex::Cmds currCmd = scex::Cmds::PING;
|
||||||
|
SdCardMountedIF *sdcMan = nullptr;
|
||||||
|
Countdown finishCountdown = Countdown(180 * 1000);
|
||||||
|
|
||||||
|
std::string random_string(std::string::size_type length);
|
||||||
|
|
||||||
// DeviceHandlerBase private function implementation
|
// DeviceHandlerBase private function implementation
|
||||||
void doStartUp() override;
|
void doStartUp() override;
|
||||||
void doShutDown() override;
|
void doShutDown() override;
|
||||||
@ -21,7 +35,7 @@ class ScexDeviceHandler : public DeviceHandlerBase {
|
|||||||
ScexUartReader &reader;
|
ScexUartReader &reader;
|
||||||
ReturnValue_t buildNormalDeviceCommand(DeviceCommandId_t *id) override;
|
ReturnValue_t buildNormalDeviceCommand(DeviceCommandId_t *id) override;
|
||||||
ReturnValue_t buildTransitionDeviceCommand(DeviceCommandId_t *id) override;
|
ReturnValue_t buildTransitionDeviceCommand(DeviceCommandId_t *id) override;
|
||||||
ReturnValue_t buildCommandFromCommand(scex::Cmds deviceCommand, const uint8_t *commandData,
|
ReturnValue_t buildCommandFromCommand(DeviceCommandId_t deviceCommand, const uint8_t *commandData,
|
||||||
size_t commandDataLen) override;
|
size_t commandDataLen) override;
|
||||||
void fillCommandAndReplyMap() override;
|
void fillCommandAndReplyMap() override;
|
||||||
ReturnValue_t scanForReply(const uint8_t *start, size_t remainingSize, DeviceCommandId_t *foundId,
|
ReturnValue_t scanForReply(const uint8_t *start, size_t remainingSize, DeviceCommandId_t *foundId,
|
||||||
|
Loading…
Reference in New Issue
Block a user