fsfw-example-common/example/utility/TmFunnel.cpp

123 lines
3.9 KiB
C++
Raw Normal View History

2021-10-17 23:21:38 +02:00
#include "TmFunnel.h"
2021-06-08 13:29:49 +02:00
#include <fsfw/ipc/QueueFactory.h>
#include <fsfw/objectmanager/ObjectManager.h>
#include <fsfw/serviceinterface/ServiceInterface.h>
2022-05-05 20:55:28 +02:00
#include <fsfw/tmtcpacket/pus/tm.h>
2021-06-08 13:29:49 +02:00
object_id_t TmFunnel::downlinkDestination = objects::NO_OBJECT;
object_id_t TmFunnel::storageDestination = objects::NO_OBJECT;
2022-05-05 20:55:28 +02:00
TmFunnel::TmFunnel(object_id_t objectId, uint32_t messageDepth)
: SystemObject(objectId), messageDepth(messageDepth) {
2022-05-22 15:30:08 +02:00
tmQueue = QueueFactory::instance()->createMessageQueue(
messageDepth, MessageQueueMessage::MAX_MESSAGE_SIZE);
2022-05-05 20:55:28 +02:00
storageQueue = QueueFactory::instance()->createMessageQueue(
messageDepth, MessageQueueMessage::MAX_MESSAGE_SIZE);
2021-06-08 13:29:49 +02:00
}
2022-05-05 20:55:28 +02:00
TmFunnel::~TmFunnel() {}
2021-06-08 13:29:49 +02:00
MessageQueueId_t TmFunnel::getReportReceptionQueue(uint8_t virtualChannel) {
2022-05-05 20:55:28 +02:00
return tmQueue->getId();
2021-06-08 13:29:49 +02:00
}
ReturnValue_t TmFunnel::performOperation(uint8_t operationCode) {
2022-05-05 20:55:28 +02:00
TmTcMessage currentMessage;
ReturnValue_t status = tmQueue->receiveMessage(&currentMessage);
while (status == HasReturnvaluesIF::RETURN_OK) {
status = handlePacket(&currentMessage);
if (status != HasReturnvaluesIF::RETURN_OK) {
break;
2021-06-08 13:29:49 +02:00
}
2022-05-05 20:55:28 +02:00
status = tmQueue->receiveMessage(&currentMessage);
}
if (status == MessageQueueIF::EMPTY) {
return HasReturnvaluesIF::RETURN_OK;
} else {
return status;
}
2021-06-08 13:29:49 +02:00
}
2022-05-22 15:30:08 +02:00
ReturnValue_t TmFunnel::handlePacket(TmTcMessage *message) {
uint8_t *packetData = nullptr;
2022-05-05 20:55:28 +02:00
size_t size = 0;
2022-05-22 15:30:08 +02:00
ReturnValue_t result =
tmPool->modifyData(message->getStorageId(), &packetData, &size);
2022-05-05 20:55:28 +02:00
if (result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
TmPacketPusC packet(packetData);
packet.setPacketSequenceCount(this->sourceSequenceCount);
sourceSequenceCount++;
2022-05-22 15:30:08 +02:00
sourceSequenceCount =
sourceSequenceCount % SpacePacketBase::LIMIT_SEQUENCE_COUNT;
2022-05-05 20:55:28 +02:00
packet.setErrorControl();
result = tmQueue->sendToDefault(message);
if (result != HasReturnvaluesIF::RETURN_OK) {
tmPool->deleteData(message->getStorageId());
2021-06-08 13:29:49 +02:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-05-22 15:30:08 +02:00
sif::error << "TmFunnel::handlePacket: Error sending to downlink handler"
<< std::endl;
2021-06-08 13:29:49 +02:00
#endif
2022-05-05 20:55:28 +02:00
return result;
}
2021-06-08 13:29:49 +02:00
2022-05-05 20:55:28 +02:00
if (storageDestination != objects::NO_OBJECT) {
result = storageQueue->sendToDefault(message);
if (result != HasReturnvaluesIF::RETURN_OK) {
tmPool->deleteData(message->getStorageId());
2021-06-08 13:29:49 +02:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-05-22 15:30:08 +02:00
sif::error << "TmFunnel::handlePacket: Error sending to storage handler"
<< std::endl;
2021-06-08 13:29:49 +02:00
#endif
2022-05-05 20:55:28 +02:00
return result;
2021-06-08 13:29:49 +02:00
}
2022-05-05 20:55:28 +02:00
}
return result;
2021-06-08 13:29:49 +02:00
}
ReturnValue_t TmFunnel::initialize() {
2022-05-05 20:55:28 +02:00
tmPool = ObjectManager::instance()->get<StorageManagerIF>(objects::TM_STORE);
if (tmPool == nullptr) {
2021-06-08 13:29:49 +02:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-05-05 20:55:28 +02:00
sif::error << "TmFunnel::initialize: TM store not set." << std::endl;
2022-05-22 15:30:08 +02:00
sif::error << "Make sure the tm store is set up properly and implements "
"StorageManagerIF"
2022-05-05 20:55:28 +02:00
<< std::endl;
2021-06-08 13:29:49 +02:00
#endif
2022-05-05 20:55:28 +02:00
return ObjectManagerIF::CHILD_INIT_FAILED;
}
2021-06-08 13:29:49 +02:00
2022-05-22 15:30:08 +02:00
AcceptsTelemetryIF *tmTarget =
2022-05-05 20:55:28 +02:00
ObjectManager::instance()->get<AcceptsTelemetryIF>(downlinkDestination);
if (tmTarget == nullptr) {
2021-06-08 13:29:49 +02:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-05-22 15:30:08 +02:00
sif::error << "TmFunnel::initialize: Downlink Destination not set."
<< std::endl;
sif::error << "Make sure the downlink destination object is set up "
"properly and implements "
2022-05-05 20:55:28 +02:00
"AcceptsTelemetryIF"
<< std::endl;
2021-06-08 13:29:49 +02:00
#endif
2022-05-05 20:55:28 +02:00
return ObjectManagerIF::CHILD_INIT_FAILED;
}
tmQueue->setDefaultDestination(tmTarget->getReportReceptionQueue());
2021-06-08 13:29:49 +02:00
2022-05-05 20:55:28 +02:00
// Storage destination is optional.
if (storageDestination == objects::NO_OBJECT) {
return SystemObject::initialize();
}
2021-06-08 13:29:49 +02:00
2022-05-22 15:30:08 +02:00
AcceptsTelemetryIF *storageTarget =
2022-05-05 20:55:28 +02:00
ObjectManager::instance()->get<AcceptsTelemetryIF>(storageDestination);
if (storageTarget != nullptr) {
2022-05-22 15:30:08 +02:00
storageQueue->setDefaultDestination(
storageTarget->getReportReceptionQueue());
2022-05-05 20:55:28 +02:00
}
2021-06-08 13:29:49 +02:00
2022-05-05 20:55:28 +02:00
return SystemObject::initialize();
2021-06-08 13:29:49 +02:00
}