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

110 lines
3.6 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>
2022-05-09 00:09:39 +02:00
#include <fsfw/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());
FSFW_LOGET("handlePacket: Error sending to downlink handler\n");
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());
FSFW_LOGET("handlePacket: Error sending to storage handler\n");
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) {
2022-05-09 01:14:53 +02:00
FSFW_LOGE("{}",
"initialize: TM store not set\n"
"Make sure the tm store is set up properly and implements StorageManagerIF");
2022-05-05 20:55:28 +02:00
return ObjectManagerIF::CHILD_INIT_FAILED;
}
2021-06-08 13:29:49 +02:00
2022-05-09 00:09:39 +02:00
auto* tmTarget = ObjectManager::instance()->get<AcceptsTelemetryIF>(downlinkDestination);
2022-05-05 20:55:28 +02:00
if (tmTarget == nullptr) {
2021-06-08 13:29:49 +02:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-05-09 01:14:53 +02:00
FSFW_LOGE("{}",
"initialize: Downlink Destination not set. Make sure the downlink destination "
"object is set up properly and implements AcceptsTelemetryIF\n");
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
}