fsfw/src/fsfw_tests/integration/devices/TestDeviceHandler.cpp

794 lines
26 KiB
C++
Raw Normal View History

2021-10-17 23:27:31 +02:00
#include "TestDeviceHandler.h"
#include <cstdlib>
2022-02-02 10:29:30 +01:00
#include "FSFWConfig.h"
#include "fsfw/datapool/PoolReadGuard.h"
TestDevice::TestDevice(object_id_t objectId, object_id_t comIF, CookieIF* cookie,
testdevice::DeviceIndex deviceIdx, bool fullInfoPrintout,
bool changingDataset)
: DeviceHandlerBase(objectId, comIF, cookie),
deviceIdx(deviceIdx),
dataset(this),
fullInfoPrintout(fullInfoPrintout) {}
2021-10-17 23:27:31 +02:00
TestDevice::~TestDevice() = default;
2021-10-17 23:27:31 +02:00
void TestDevice::performOperationHook() {
2022-02-02 10:29:30 +01:00
if (periodicPrintout) {
2021-10-17 23:27:31 +02:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::info << "TestDevice" << deviceIdx << "::performOperationHook: Alive!" << std::endl;
2021-10-17 23:27:31 +02:00
#else
sif::printInfo("TestDevice%d::performOperationHook: Alive!\n", deviceIdx);
2021-10-17 23:27:31 +02:00
#endif
2022-02-02 10:29:30 +01:00
}
2021-10-17 23:27:31 +02:00
2022-02-02 10:29:30 +01:00
if (oneShot) {
oneShot = false;
}
2021-10-17 23:27:31 +02:00
}
void TestDevice::doStartUp() {
2022-02-02 10:29:30 +01:00
if (fullInfoPrintout) {
2021-10-17 23:27:31 +02:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::info << "TestDevice" << deviceIdx << "::doStartUp: Switching On" << std::endl;
2021-10-17 23:27:31 +02:00
#else
2022-02-02 10:29:30 +01:00
sif::printInfo("TestDevice%d::doStartUp: Switching On\n", static_cast<int>(deviceIdx));
2021-10-17 23:27:31 +02:00
#endif
2022-02-02 10:29:30 +01:00
}
2021-10-17 23:27:31 +02:00
2022-02-02 10:29:30 +01:00
setMode(_MODE_TO_ON);
return;
2021-10-17 23:27:31 +02:00
}
void TestDevice::doShutDown() {
2022-02-02 10:29:30 +01:00
if (fullInfoPrintout) {
2021-10-17 23:27:31 +02:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::info << "TestDevice" << deviceIdx << "::doShutDown: Switching Off" << std::endl;
2021-10-17 23:27:31 +02:00
#else
2022-02-02 10:29:30 +01:00
sif::printInfo("TestDevice%d::doShutDown: Switching Off\n", static_cast<int>(deviceIdx));
2021-10-17 23:27:31 +02:00
#endif
2022-02-02 10:29:30 +01:00
}
2021-10-17 23:27:31 +02:00
2022-02-02 10:29:30 +01:00
setMode(_MODE_SHUT_DOWN);
return;
2021-10-17 23:27:31 +02:00
}
ReturnValue_t TestDevice::buildNormalDeviceCommand(DeviceCommandId_t* id) {
2022-02-02 10:29:30 +01:00
using namespace testdevice;
*id = TEST_NORMAL_MODE_CMD;
if (DeviceHandlerBase::isAwaitingReply()) {
return NOTHING_TO_SEND;
}
return buildCommandFromCommand(*id, nullptr, 0);
2021-10-17 23:27:31 +02:00
}
ReturnValue_t TestDevice::buildTransitionDeviceCommand(DeviceCommandId_t* id) {
2022-02-02 10:29:30 +01:00
if (mode == _MODE_TO_ON) {
if (fullInfoPrintout) {
2021-10-17 23:27:31 +02:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::info << "TestDevice" << deviceIdx
<< "::buildTransitionDeviceCommand: Was called"
" from _MODE_TO_ON mode"
<< std::endl;
2021-10-17 23:27:31 +02:00
#else
2022-02-02 10:29:30 +01:00
sif::printInfo(
"TestDevice%d::buildTransitionDeviceCommand: "
"Was called from _MODE_TO_ON mode\n",
deviceIdx);
2021-10-17 23:27:31 +02:00
#endif
}
2022-02-02 10:29:30 +01:00
}
if (mode == _MODE_TO_NORMAL) {
if (fullInfoPrintout) {
2021-10-17 23:27:31 +02:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::info << "TestDevice" << deviceIdx
<< "::buildTransitionDeviceCommand: Was called "
"from _MODE_TO_NORMAL mode"
<< std::endl;
2021-10-17 23:27:31 +02:00
#else
2022-02-02 10:29:30 +01:00
sif::printInfo(
"TestDevice%d::buildTransitionDeviceCommand: Was called from "
" _MODE_TO_NORMAL mode\n",
deviceIdx);
2021-10-17 23:27:31 +02:00
#endif
}
2022-02-02 10:29:30 +01:00
setMode(MODE_NORMAL);
}
if (mode == _MODE_SHUT_DOWN) {
if (fullInfoPrintout) {
2021-10-17 23:27:31 +02:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::info << "TestDevice" << deviceIdx
<< "::buildTransitionDeviceCommand: Was called "
"from _MODE_SHUT_DOWN mode"
<< std::endl;
2021-10-17 23:27:31 +02:00
#else
2022-02-02 10:29:30 +01:00
sif::printInfo(
"TestDevice%d::buildTransitionDeviceCommand: Was called from "
"_MODE_SHUT_DOWN mode\n",
deviceIdx);
2021-10-17 23:27:31 +02:00
#endif
}
2022-02-02 10:29:30 +01:00
setMode(MODE_OFF);
}
return NOTHING_TO_SEND;
2021-10-17 23:27:31 +02:00
}
void TestDevice::doTransition(Mode_t modeFrom, Submode_t submodeFrom) {
2022-02-02 10:29:30 +01:00
if (mode == _MODE_TO_NORMAL) {
if (fullInfoPrintout) {
2021-10-17 23:27:31 +02:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::info << "TestDevice" << deviceIdx
<< "::doTransition: Custom transition to "
"normal mode"
<< std::endl;
2021-10-17 23:27:31 +02:00
#else
2022-02-02 10:29:30 +01:00
sif::printInfo("TestDevice%d::doTransition: Custom transition to normal mode\n", deviceIdx);
2021-10-17 23:27:31 +02:00
#endif
}
2022-02-02 10:29:30 +01:00
} else {
DeviceHandlerBase::doTransition(modeFrom, submodeFrom);
}
}
2021-10-17 23:27:31 +02:00
2022-02-02 10:29:30 +01:00
ReturnValue_t TestDevice::buildCommandFromCommand(DeviceCommandId_t deviceCommand,
const uint8_t* commandData,
size_t commandDataLen) {
using namespace testdevice;
2022-08-16 01:08:26 +02:00
ReturnValue_t result = returnvalue::OK;
2022-02-02 10:29:30 +01:00
switch (deviceCommand) {
case (TEST_NORMAL_MODE_CMD): {
commandSent = true;
result = buildNormalModeCommand(deviceCommand, commandData, commandDataLen);
break;
}
case (TEST_COMMAND_0): {
commandSent = true;
result = buildTestCommand0(deviceCommand, commandData, commandDataLen);
break;
}
case (TEST_COMMAND_1): {
commandSent = true;
result = buildTestCommand1(deviceCommand, commandData, commandDataLen);
break;
}
case (TEST_NOTIF_SNAPSHOT_VAR): {
if (changingDatasets) {
changingDatasets = false;
}
PoolReadGuard readHelper(&dataset.testUint8Var);
if (deviceIdx == testdevice::DeviceIndex::DEVICE_0) {
/* This will trigger a variable notification to the demo controller */
dataset.testUint8Var = 220;
dataset.testUint8Var.setValid(true);
} else if (deviceIdx == testdevice::DeviceIndex::DEVICE_1) {
/* This will trigger a variable snapshot to the demo controller */
dataset.testUint8Var = 30;
dataset.testUint8Var.setValid(true);
}
break;
}
case (TEST_NOTIF_SNAPSHOT_SET): {
if (changingDatasets) {
changingDatasets = false;
}
PoolReadGuard readHelper(&dataset.testFloat3Vec);
if (deviceIdx == testdevice::DeviceIndex::DEVICE_0) {
/* This will trigger a variable notification to the demo controller */
dataset.testFloat3Vec.value[0] = 60;
dataset.testFloat3Vec.value[1] = 70;
dataset.testFloat3Vec.value[2] = 55;
dataset.testFloat3Vec.setValid(true);
} else if (deviceIdx == testdevice::DeviceIndex::DEVICE_1) {
/* This will trigger a variable notification to the demo controller */
dataset.testFloat3Vec.value[0] = -60;
dataset.testFloat3Vec.value[1] = -70;
dataset.testFloat3Vec.value[2] = -55;
dataset.testFloat3Vec.setValid(true);
}
break;
2021-10-17 23:27:31 +02:00
}
default:
2022-02-02 10:29:30 +01:00
result = DeviceHandlerIF::COMMAND_NOT_SUPPORTED;
}
return result;
2021-10-17 23:27:31 +02:00
}
ReturnValue_t TestDevice::buildNormalModeCommand(DeviceCommandId_t deviceCommand,
2022-02-02 10:29:30 +01:00
const uint8_t* commandData,
size_t commandDataLen) {
if (fullInfoPrintout) {
2022-03-07 15:54:56 +01:00
#if FSFW_VERBOSE_LEVEL >= 3
2021-10-17 23:27:31 +02:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::info << "TestDevice::buildTestCommand1: Building normal command" << std::endl;
2021-10-17 23:27:31 +02:00
#else
2022-02-02 10:29:30 +01:00
sif::printInfo("TestDevice::buildTestCommand1: Building command from TEST_COMMAND_1\n");
2021-10-17 23:27:31 +02:00
#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */
#endif /* OBSW_VERBOSE_LEVEL >= 3 */
2022-02-02 10:29:30 +01:00
}
if (commandDataLen > MAX_BUFFER_SIZE - sizeof(DeviceCommandId_t)) {
return DeviceHandlerIF::INVALID_NUMBER_OR_LENGTH_OF_PARAMETERS;
}
/* The command is passed on in the command buffer as it is */
passOnCommand(deviceCommand, commandData, commandDataLen);
2022-08-16 01:08:26 +02:00
return returnvalue::OK;
2021-10-17 23:27:31 +02:00
}
ReturnValue_t TestDevice::buildTestCommand0(DeviceCommandId_t deviceCommand,
2022-02-02 10:29:30 +01:00
const uint8_t* commandData, size_t commandDataLen) {
using namespace testdevice;
if (fullInfoPrintout) {
2021-10-17 23:27:31 +02:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::info << "TestDevice" << deviceIdx
<< "::buildTestCommand0: Executing simple command "
" with completion reply"
<< std::endl;
2021-10-17 23:27:31 +02:00
#else
2022-02-02 10:29:30 +01:00
sif::printInfo(
"TestDevice%d::buildTestCommand0: Executing simple command with "
"completion reply\n",
deviceIdx);
2021-10-17 23:27:31 +02:00
#endif
2022-02-02 10:29:30 +01:00
}
2021-10-17 23:27:31 +02:00
2022-02-02 10:29:30 +01:00
if (commandDataLen > MAX_BUFFER_SIZE - sizeof(DeviceCommandId_t)) {
return DeviceHandlerIF::INVALID_NUMBER_OR_LENGTH_OF_PARAMETERS;
}
2021-10-17 23:27:31 +02:00
2022-02-02 10:29:30 +01:00
/* The command is passed on in the command buffer as it is */
passOnCommand(deviceCommand, commandData, commandDataLen);
2022-08-16 01:08:26 +02:00
return returnvalue::OK;
2021-10-17 23:27:31 +02:00
}
ReturnValue_t TestDevice::buildTestCommand1(DeviceCommandId_t deviceCommand,
2022-02-02 10:29:30 +01:00
const uint8_t* commandData, size_t commandDataLen) {
using namespace testdevice;
if (commandDataLen < 7) {
return DeviceHandlerIF::INVALID_NUMBER_OR_LENGTH_OF_PARAMETERS;
}
if (fullInfoPrintout) {
2021-10-17 23:27:31 +02:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::info << "TestDevice" << deviceIdx
<< "::buildTestCommand1: Executing command with "
"data reply"
<< std::endl;
2021-10-17 23:27:31 +02:00
#else
2022-02-02 10:29:30 +01:00
sif::printInfo("TestDevice%d:buildTestCommand1: Executing command with data reply\n",
deviceIdx);
2021-10-17 23:27:31 +02:00
#endif
2022-02-02 10:29:30 +01:00
}
2021-10-17 23:27:31 +02:00
2022-02-02 10:29:30 +01:00
deviceCommand = EndianConverter::convertBigEndian(deviceCommand);
memcpy(commandBuffer, &deviceCommand, sizeof(deviceCommand));
2021-10-17 23:27:31 +02:00
2022-02-02 10:29:30 +01:00
/* Assign and check parameters */
uint16_t parameter1 = 0;
size_t size = commandDataLen;
ReturnValue_t result =
SerializeAdapter::deSerialize(&parameter1, &commandData, &size, SerializeIF::Endianness::BIG);
2022-08-16 01:08:26 +02:00
if (result == returnvalue::FAILED) {
2022-02-02 10:29:30 +01:00
return result;
}
/* Parameter 1 needs to be correct */
if (parameter1 != testdevice::COMMAND_1_PARAM1) {
return DeviceHandlerIF::INVALID_COMMAND_PARAMETER;
}
uint64_t parameter2 = 0;
result =
SerializeAdapter::deSerialize(&parameter2, &commandData, &size, SerializeIF::Endianness::BIG);
if (parameter2 != testdevice::COMMAND_1_PARAM2) {
return DeviceHandlerIF::INVALID_COMMAND_PARAMETER;
}
/* Pass on the parameters to the Echo IF */
commandBuffer[4] = (parameter1 & 0xFF00) >> 8;
commandBuffer[5] = (parameter1 & 0xFF);
parameter2 = EndianConverter::convertBigEndian(parameter2);
memcpy(commandBuffer + 6, &parameter2, sizeof(parameter2));
rawPacket = commandBuffer;
rawPacketLen = sizeof(deviceCommand) + sizeof(parameter1) + sizeof(parameter2);
2022-08-16 01:08:26 +02:00
return returnvalue::OK;
2021-10-17 23:27:31 +02:00
}
2022-02-02 10:29:30 +01:00
void TestDevice::passOnCommand(DeviceCommandId_t command, const uint8_t* commandData,
size_t commandDataLen) {
DeviceCommandId_t deviceCommandBe = EndianConverter::convertBigEndian(command);
memcpy(commandBuffer, &deviceCommandBe, sizeof(deviceCommandBe));
memcpy(commandBuffer + 4, commandData, commandDataLen);
rawPacket = commandBuffer;
rawPacketLen = sizeof(deviceCommandBe) + commandDataLen;
2021-10-17 23:27:31 +02:00
}
void TestDevice::fillCommandAndReplyMap() {
2022-02-02 10:29:30 +01:00
namespace td = testdevice;
insertInCommandAndReplyMap(testdevice::TEST_NORMAL_MODE_CMD, 5, &dataset);
insertInCommandAndReplyMap(testdevice::TEST_COMMAND_0, 5);
insertInCommandAndReplyMap(testdevice::TEST_COMMAND_1, 5);
/* No reply expected for these commands */
insertInCommandMap(td::TEST_NOTIF_SNAPSHOT_SET);
insertInCommandMap(td::TEST_NOTIF_SNAPSHOT_VAR);
2021-10-17 23:27:31 +02:00
}
2022-02-02 10:29:30 +01:00
ReturnValue_t TestDevice::scanForReply(const uint8_t* start, size_t len, DeviceCommandId_t* foundId,
size_t* foundLen) {
using namespace testdevice;
/* Unless a command was sent explicitely, we don't expect any replies and ignore this
the packet. On a real device, there might be replies which are sent without a previous
command. */
if (not commandSent) {
return DeviceHandlerBase::IGNORE_FULL_PACKET;
} else {
commandSent = false;
}
if (len < sizeof(object_id_t)) {
return DeviceHandlerIF::LENGTH_MISSMATCH;
}
size_t size = len;
ReturnValue_t result =
SerializeAdapter::deSerialize(foundId, &start, &size, SerializeIF::Endianness::BIG);
2022-08-16 01:08:26 +02:00
if (result != returnvalue::OK) {
2022-02-02 10:29:30 +01:00
return result;
}
2021-10-17 23:27:31 +02:00
2022-02-02 10:29:30 +01:00
DeviceCommandId_t pendingCmd = this->getPendingCommand();
2021-10-17 23:27:31 +02:00
2022-02-02 10:29:30 +01:00
switch (pendingCmd) {
case (TEST_NORMAL_MODE_CMD): {
if (fullInfoPrintout) {
2022-03-07 15:54:56 +01:00
#if FSFW_VERBOSE_LEVEL >= 3
2021-10-17 23:27:31 +02:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::info << "TestDevice::scanForReply: Reply for normal commnand (ID "
<< TEST_NORMAL_MODE_CMD << ") received!" << std::endl;
2021-10-17 23:27:31 +02:00
#else
2022-02-02 10:29:30 +01:00
sif::printInfo(
"TestDevice%d::scanForReply: Reply for normal command (ID %d) "
"received!\n",
deviceIdx, TEST_NORMAL_MODE_CMD);
2021-10-17 23:27:31 +02:00
#endif
#endif
2022-02-02 10:29:30 +01:00
}
2021-10-17 23:27:31 +02:00
2022-02-02 10:29:30 +01:00
*foundLen = len;
*foundId = pendingCmd;
2022-08-16 01:08:26 +02:00
return returnvalue::OK;
2021-10-17 23:27:31 +02:00
}
2022-02-02 10:29:30 +01:00
case (TEST_COMMAND_0): {
if (len < TEST_COMMAND_0_SIZE) {
return DeviceHandlerIF::LENGTH_MISSMATCH;
}
if (fullInfoPrintout) {
2021-10-17 23:27:31 +02:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::info << "TestDevice" << deviceIdx
<< "::scanForReply: Reply for simple command "
"(ID "
<< TEST_COMMAND_0 << ") received!" << std::endl;
2021-10-17 23:27:31 +02:00
#else
2022-02-02 10:29:30 +01:00
sif::printInfo(
"TestDevice%d::scanForReply: Reply for simple command (ID %d) "
"received!\n",
deviceIdx, TEST_COMMAND_0);
2021-10-17 23:27:31 +02:00
#endif
2022-02-02 10:29:30 +01:00
}
2021-10-17 23:27:31 +02:00
2022-02-02 10:29:30 +01:00
*foundLen = TEST_COMMAND_0_SIZE;
*foundId = pendingCmd;
2022-08-16 01:08:26 +02:00
return returnvalue::OK;
2021-10-17 23:27:31 +02:00
}
2022-02-02 10:29:30 +01:00
case (TEST_COMMAND_1): {
if (fullInfoPrintout) {
2021-10-17 23:27:31 +02:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::info << "TestDevice" << deviceIdx
<< "::scanForReply: Reply for data command "
"(ID "
<< TEST_COMMAND_1 << ") received!" << std::endl;
2021-10-17 23:27:31 +02:00
#else
2022-02-02 10:29:30 +01:00
sif::printInfo(
"TestDevice%d::scanForReply: Reply for data command (ID %d) "
"received\n",
deviceIdx, TEST_COMMAND_1);
2021-10-17 23:27:31 +02:00
#endif
2022-02-02 10:29:30 +01:00
}
2021-10-17 23:27:31 +02:00
2022-02-02 10:29:30 +01:00
*foundLen = len;
*foundId = pendingCmd;
2022-08-16 01:08:26 +02:00
return returnvalue::OK;
2021-10-17 23:27:31 +02:00
}
default:
2022-02-02 10:29:30 +01:00
return DeviceHandlerIF::DEVICE_REPLY_INVALID;
}
2021-10-17 23:27:31 +02:00
}
2022-02-02 10:29:30 +01:00
ReturnValue_t TestDevice::interpretDeviceReply(DeviceCommandId_t id, const uint8_t* packet) {
2022-08-16 01:08:26 +02:00
ReturnValue_t result = returnvalue::OK;
2022-02-02 10:29:30 +01:00
switch (id) {
2021-10-17 23:27:31 +02:00
/* Periodic replies */
case testdevice::TEST_NORMAL_MODE_CMD: {
2022-02-02 10:29:30 +01:00
result = interpretingNormalModeReply();
break;
2021-10-17 23:27:31 +02:00
}
/* Simple reply */
case testdevice::TEST_COMMAND_0: {
2022-02-02 10:29:30 +01:00
result = interpretingTestReply0(id, packet);
break;
2021-10-17 23:27:31 +02:00
}
/* Data reply */
case testdevice::TEST_COMMAND_1: {
2022-02-02 10:29:30 +01:00
result = interpretingTestReply1(id, packet);
break;
2021-10-17 23:27:31 +02:00
}
default:
2022-02-02 10:29:30 +01:00
return DeviceHandlerIF::DEVICE_REPLY_INVALID;
}
return result;
2021-10-17 23:27:31 +02:00
}
ReturnValue_t TestDevice::interpretingNormalModeReply() {
2022-02-02 10:29:30 +01:00
CommandMessage directReplyMessage;
if (changingDatasets) {
2021-10-17 23:27:31 +02:00
PoolReadGuard readHelper(&dataset);
2022-02-02 10:29:30 +01:00
if (dataset.testUint8Var.value == 0) {
dataset.testUint8Var.value = 10;
dataset.testUint32Var.value = 777;
dataset.testFloat3Vec.value[0] = 2.5;
dataset.testFloat3Vec.value[1] = -2.5;
dataset.testFloat3Vec.value[2] = 2.5;
dataset.setValidity(true, true);
} else {
dataset.testUint8Var.value = 0;
dataset.testUint32Var.value = 0;
dataset.testFloat3Vec.value[0] = 0.0;
dataset.testFloat3Vec.value[1] = 0.0;
dataset.testFloat3Vec.value[2] = 0.0;
dataset.setValidity(false, true);
2021-10-17 23:27:31 +02:00
}
2022-08-16 01:08:26 +02:00
return returnvalue::OK;
2022-02-02 10:29:30 +01:00
}
PoolReadGuard readHelper(&dataset);
if (dataset.testUint8Var.value == 0) {
/* Reset state */
dataset.testUint8Var.value = 128;
} else if (dataset.testUint8Var.value > 200) {
if (not resetAfterChange) {
/* This will trigger an update notification to the controller */
dataset.testUint8Var.setChanged(true);
resetAfterChange = true;
/* Decrement by 30 automatically. This will prevent any additional notifications. */
dataset.testUint8Var.value -= 30;
}
}
/* If the value is greater than 0, it will be decremented in a linear way */
else if (dataset.testUint8Var.value > 128) {
size_t sizeToDecrement = 0;
if (dataset.testUint8Var.value > 128 + 30) {
sizeToDecrement = 30;
} else {
sizeToDecrement = dataset.testUint8Var.value - 128;
resetAfterChange = false;
}
dataset.testUint8Var.value -= sizeToDecrement;
} else if (dataset.testUint8Var.value < 50) {
if (not resetAfterChange) {
/* This will trigger an update snapshot to the controller */
dataset.testUint8Var.setChanged(true);
resetAfterChange = true;
} else {
/* Increment by 30 automatically. */
dataset.testUint8Var.value += 30;
}
}
/* Increment in linear way */
else if (dataset.testUint8Var.value < 128) {
size_t sizeToIncrement = 0;
if (dataset.testUint8Var.value < 128 - 20) {
sizeToIncrement = 20;
} else {
sizeToIncrement = 128 - dataset.testUint8Var.value;
resetAfterChange = false;
}
dataset.testUint8Var.value += sizeToIncrement;
}
/* TODO: Same for vector */
float vectorMean = (dataset.testFloat3Vec.value[0] + dataset.testFloat3Vec.value[1] +
dataset.testFloat3Vec.value[2]) /
3.0;
/* Lambda (private local function) */
auto sizeToAdd = [](bool tooHigh, float currentVal) {
if (tooHigh) {
if (currentVal - 20.0 > 10.0) {
return -10.0;
} else {
return 20.0 - currentVal;
}
} else {
if (std::abs(currentVal + 20.0) > 10.0) {
return 10.0;
} else {
return -20.0 - currentVal;
}
}
};
if (vectorMean > 20.0 and std::abs(vectorMean - 20.0) > 1.0) {
if (not resetAfterChange) {
dataset.testFloat3Vec.setChanged(true);
resetAfterChange = true;
} else {
float sizeToDecrementVal0 = 0;
float sizeToDecrementVal1 = 0;
float sizeToDecrementVal2 = 0;
sizeToDecrementVal0 = sizeToAdd(true, dataset.testFloat3Vec.value[0]);
sizeToDecrementVal1 = sizeToAdd(true, dataset.testFloat3Vec.value[1]);
sizeToDecrementVal2 = sizeToAdd(true, dataset.testFloat3Vec.value[2]);
dataset.testFloat3Vec.value[0] += sizeToDecrementVal0;
dataset.testFloat3Vec.value[1] += sizeToDecrementVal1;
dataset.testFloat3Vec.value[2] += sizeToDecrementVal2;
}
} else if (vectorMean < -20.0 and std::abs(vectorMean + 20.0) < 1.0) {
if (not resetAfterChange) {
dataset.testFloat3Vec.setChanged(true);
resetAfterChange = true;
} else {
float sizeToDecrementVal0 = 0;
float sizeToDecrementVal1 = 0;
float sizeToDecrementVal2 = 0;
sizeToDecrementVal0 = sizeToAdd(false, dataset.testFloat3Vec.value[0]);
sizeToDecrementVal1 = sizeToAdd(false, dataset.testFloat3Vec.value[1]);
sizeToDecrementVal2 = sizeToAdd(false, dataset.testFloat3Vec.value[2]);
dataset.testFloat3Vec.value[0] += sizeToDecrementVal0;
dataset.testFloat3Vec.value[1] += sizeToDecrementVal1;
dataset.testFloat3Vec.value[2] += sizeToDecrementVal2;
}
} else {
if (resetAfterChange) {
resetAfterChange = false;
}
}
2022-08-16 01:08:26 +02:00
return returnvalue::OK;
2021-10-17 23:27:31 +02:00
}
ReturnValue_t TestDevice::interpretingTestReply0(DeviceCommandId_t id, const uint8_t* packet) {
2022-02-02 10:29:30 +01:00
CommandMessage commandMessage;
if (fullInfoPrintout) {
2021-10-17 23:27:31 +02:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::info << "TestDevice::interpretingTestReply0: Generating step and finish reply"
<< std::endl;
2021-10-17 23:27:31 +02:00
#else
2022-02-02 10:29:30 +01:00
sif::printInfo("TestDevice::interpretingTestReply0: Generating step and finish reply\n");
2021-10-17 23:27:31 +02:00
#endif
2022-02-02 10:29:30 +01:00
}
2021-10-17 23:27:31 +02:00
2022-02-02 10:29:30 +01:00
MessageQueueId_t commander = getCommanderQueueId(id);
/* Generate one step reply and the finish reply */
actionHelper.step(1, commander, id);
actionHelper.finish(true, commander, id);
2021-10-17 23:27:31 +02:00
2022-08-16 01:08:26 +02:00
return returnvalue::OK;
2021-10-17 23:27:31 +02:00
}
2022-02-02 10:29:30 +01:00
ReturnValue_t TestDevice::interpretingTestReply1(DeviceCommandId_t id, const uint8_t* packet) {
CommandMessage directReplyMessage;
if (fullInfoPrintout) {
2021-10-17 23:27:31 +02:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::info << "TestDevice" << deviceIdx << "::interpretingReply1: Setting data reply"
<< std::endl;
2021-10-17 23:27:31 +02:00
#else
2022-02-02 10:29:30 +01:00
sif::printInfo("TestDevice%d::interpretingReply1: Setting data reply\n", deviceIdx);
2021-10-17 23:27:31 +02:00
#endif
2022-02-02 10:29:30 +01:00
}
2021-10-17 23:27:31 +02:00
2022-02-02 10:29:30 +01:00
MessageQueueId_t commander = getCommanderQueueId(id);
/* Send reply with data */
ReturnValue_t result =
actionHelper.reportData(commander, id, packet, testdevice::TEST_COMMAND_1_SIZE, false);
2021-10-17 23:27:31 +02:00
2022-08-16 01:08:26 +02:00
if (result != returnvalue::OK) {
2021-10-17 23:27:31 +02:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::error << "TestDevice" << deviceIdx
<< "::interpretingReply1: Sending data "
"reply failed!"
<< std::endl;
2021-10-17 23:27:31 +02:00
#else
2022-02-02 10:29:30 +01:00
sif::printError("TestDevice%d::interpretingReply1: Sending data reply failed!\n", deviceIdx);
2021-10-17 23:27:31 +02:00
#endif
2022-02-02 10:29:30 +01:00
return result;
}
2021-10-17 23:27:31 +02:00
2022-08-16 01:08:26 +02:00
if (result == returnvalue::OK) {
2022-02-02 10:29:30 +01:00
/* Finish reply */
actionHelper.finish(true, commander, id);
} else {
/* Finish reply */
actionHelper.finish(false, commander, id, result);
}
2021-10-17 23:27:31 +02:00
2022-08-16 01:08:26 +02:00
return returnvalue::OK;
2021-10-17 23:27:31 +02:00
}
2022-02-02 10:29:30 +01:00
uint32_t TestDevice::getTransitionDelayMs(Mode_t modeFrom, Mode_t modeTo) { return 5000; }
2021-10-17 23:27:31 +02:00
2022-02-02 10:29:30 +01:00
void TestDevice::enableFullDebugOutput(bool enable) { this->fullInfoPrintout = enable; }
2021-10-17 23:27:31 +02:00
2022-02-02 10:29:30 +01:00
ReturnValue_t TestDevice::initializeLocalDataPool(localpool::DataPool& localDataPoolMap,
LocalDataPoolManager& poolManager) {
namespace td = testdevice;
localDataPoolMap.emplace(td::PoolIds::TEST_UINT8_ID, new PoolEntry<uint8_t>({0}));
localDataPoolMap.emplace(td::PoolIds::TEST_UINT32_ID, new PoolEntry<uint32_t>({0}));
localDataPoolMap.emplace(td::PoolIds::TEST_FLOAT_VEC_3_ID, new PoolEntry<float>({0.0, 0.0, 0.0}));
2021-10-17 23:27:31 +02:00
2022-02-02 10:29:30 +01:00
sid_t sid(this->getObjectId(), td::TEST_SET_ID);
/* Subscribe for periodic HK packets but do not enable reporting for now.
Non-diangostic with a period of one second */
poolManager.subscribeForRegularPeriodicPacket({sid, false, 1.0});
2022-08-24 17:25:45 +02:00
return returnvalue::OK;
2022-02-02 10:29:30 +01:00
}
2021-10-17 23:27:31 +02:00
ReturnValue_t TestDevice::getParameter(uint8_t domainId, uint8_t uniqueId,
2022-02-02 10:29:30 +01:00
ParameterWrapper* parameterWrapper,
const ParameterWrapper* newValues, uint16_t startAtIndex) {
using namespace testdevice;
switch (uniqueId) {
2021-10-17 23:27:31 +02:00
case ParameterUniqueIds::TEST_UINT32_0: {
2022-02-02 10:29:30 +01:00
if (fullInfoPrintout) {
uint32_t newValue = 0;
ReturnValue_t result = newValues->getElement<uint32_t>(&newValue, 0, 0);
2022-08-16 01:08:26 +02:00
if (result == returnvalue::OK) {
2021-10-17 23:27:31 +02:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::info << "TestDevice" << deviceIdx
<< "::getParameter: Setting parameter 0 to "
"new value "
<< newValue << std::endl;
2021-10-17 23:27:31 +02:00
#else
2022-02-02 10:29:30 +01:00
sif::printInfo("TestDevice%d::getParameter: Setting parameter 0 to new value %lu\n",
deviceIdx, static_cast<unsigned long>(newValue));
2021-10-17 23:27:31 +02:00
#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */
}
2022-02-02 10:29:30 +01:00
}
parameterWrapper->set(testParameter0);
break;
2021-10-17 23:27:31 +02:00
}
case ParameterUniqueIds::TEST_INT32_1: {
2022-02-02 10:29:30 +01:00
if (fullInfoPrintout) {
int32_t newValue = 0;
ReturnValue_t result = newValues->getElement<int32_t>(&newValue, 0, 0);
2022-08-16 01:08:26 +02:00
if (result == returnvalue::OK) {
2021-10-17 23:27:31 +02:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::info << "TestDevice" << deviceIdx
<< "::getParameter: Setting parameter 1 to "
"new value "
<< newValue << std::endl;
2021-10-17 23:27:31 +02:00
#else
2022-02-02 10:29:30 +01:00
sif::printInfo("TestDevice%d::getParameter: Setting parameter 1 to new value %lu\n",
deviceIdx, static_cast<unsigned long>(newValue));
2021-10-17 23:27:31 +02:00
#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */
}
2022-02-02 10:29:30 +01:00
}
parameterWrapper->set(testParameter1);
break;
2021-10-17 23:27:31 +02:00
}
case ParameterUniqueIds::TEST_FLOAT_VEC3_2: {
2022-02-02 10:29:30 +01:00
if (fullInfoPrintout) {
float newVector[3];
2022-08-16 01:08:26 +02:00
if (newValues->getElement<float>(newVector, 0, 0) != returnvalue::OK or
newValues->getElement<float>(newVector + 1, 0, 1) != returnvalue::OK or
newValues->getElement<float>(newVector + 2, 0, 2) != returnvalue::OK) {
return returnvalue::FAILED;
2022-02-02 10:29:30 +01:00
}
2021-10-17 23:27:31 +02:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::info << "TestDevice" << deviceIdx
<< "::getParameter: Setting parameter 3 to "
"(float vector with 3 entries) to new values ["
<< newVector[0] << ", " << newVector[1] << ", " << newVector[2] << "]"
<< std::endl;
2021-10-17 23:27:31 +02:00
#else
2022-02-02 10:29:30 +01:00
sif::printInfo(
"TestDevice%d::getParameter: Setting parameter 3 to new values "
"[%f, %f, %f]\n",
deviceIdx, newVector[0], newVector[1], newVector[2]);
2021-10-17 23:27:31 +02:00
#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */
2022-02-02 10:29:30 +01:00
}
parameterWrapper->setVector(vectorFloatParams2);
break;
2021-10-17 23:27:31 +02:00
}
2022-02-02 10:29:30 +01:00
case (ParameterUniqueIds::PERIODIC_PRINT_ENABLED): {
if (fullInfoPrintout) {
uint8_t enabled = 0;
ReturnValue_t result = newValues->getElement<uint8_t>(&enabled, 0, 0);
2022-08-16 01:08:26 +02:00
if (result != returnvalue::OK) {
2022-02-02 10:29:30 +01:00
return result;
}
char const* printout = nullptr;
if (enabled) {
printout = "enabled";
} else {
printout = "disabled";
}
2021-10-17 23:27:31 +02:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::info << "TestDevice" << deviceIdx << "::getParameter: Periodic printout " << printout
<< std::endl;
2021-10-17 23:27:31 +02:00
#else
2022-02-02 10:29:30 +01:00
sif::printInfo("TestDevice%d::getParameter: Periodic printout %s", printout);
2021-10-17 23:27:31 +02:00
#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */
2022-02-02 10:29:30 +01:00
}
2021-10-17 23:27:31 +02:00
2022-02-02 10:29:30 +01:00
parameterWrapper->set(periodicPrintout);
break;
2021-10-17 23:27:31 +02:00
}
2022-02-02 10:29:30 +01:00
case (ParameterUniqueIds::CHANGING_DATASETS): {
uint8_t enabled = 0;
ReturnValue_t result = newValues->getElement<uint8_t>(&enabled, 0, 0);
2022-08-16 01:08:26 +02:00
if (result != returnvalue::OK) {
2022-02-02 10:29:30 +01:00
return result;
}
if (not enabled) {
PoolReadGuard readHelper(&dataset);
dataset.testUint8Var.value = 0;
dataset.testUint32Var.value = 0;
dataset.testFloat3Vec.value[0] = 0.0;
dataset.testFloat3Vec.value[0] = 0.0;
dataset.testFloat3Vec.value[1] = 0.0;
}
if (fullInfoPrintout) {
char const* printout = nullptr;
if (enabled) {
printout = "enabled";
} else {
printout = "disabled";
2021-10-17 23:27:31 +02:00
}
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::info << "TestDevice" << deviceIdx << "::getParameter: Changing datasets " << printout
<< std::endl;
2021-10-17 23:27:31 +02:00
#else
2022-02-02 10:29:30 +01:00
sif::printInfo("TestDevice%d::getParameter: Changing datasets %s", printout);
2021-10-17 23:27:31 +02:00
#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */
2022-02-02 10:29:30 +01:00
}
2021-10-17 23:27:31 +02:00
2022-02-02 10:29:30 +01:00
parameterWrapper->set(changingDatasets);
break;
2021-10-17 23:27:31 +02:00
}
default:
2022-02-02 10:29:30 +01:00
return INVALID_IDENTIFIER_ID;
}
2022-08-16 01:08:26 +02:00
return returnvalue::OK;
2021-10-17 23:27:31 +02:00
}
LocalPoolObjectBase* TestDevice::getPoolObjectHandle(lp_id_t localPoolId) {
2022-02-02 10:29:30 +01:00
namespace td = testdevice;
if (localPoolId == td::PoolIds::TEST_UINT8_ID) {
return &dataset.testUint8Var;
} else if (localPoolId == td::PoolIds::TEST_UINT32_ID) {
return &dataset.testUint32Var;
} else if (localPoolId == td::PoolIds::TEST_FLOAT_VEC_3_ID) {
return &dataset.testFloat3Vec;
} else {
return nullptr;
}
2021-10-17 23:27:31 +02:00
}