2022-10-21 11:51:44 +02:00
|
|
|
#include "CfdpTmFunnel.h"
|
|
|
|
|
|
|
|
#include "fsfw/ipc/QueueFactory.h"
|
|
|
|
#include "fsfw/tmtcpacket/ccsds/SpacePacketCreator.h"
|
|
|
|
#include "fsfw/tmtcservices/TmTcMessage.h"
|
|
|
|
|
|
|
|
CfdpTmFunnel::CfdpTmFunnel(object_id_t objectId, uint16_t cfdpInCcsdsApid,
|
2022-11-03 10:34:19 +01:00
|
|
|
StorageManagerIF& tmStore, uint32_t messageDepth)
|
|
|
|
: TmFunnelBase(objectId, tmStore, messageDepth), cfdpInCcsdsApid(cfdpInCcsdsApid) {}
|
2022-10-21 11:51:44 +02:00
|
|
|
|
|
|
|
const char* CfdpTmFunnel::getName() const { return "CFDP TM Funnel"; }
|
|
|
|
|
|
|
|
ReturnValue_t CfdpTmFunnel::performOperation(uint8_t) {
|
|
|
|
TmTcMessage currentMessage;
|
2022-11-02 10:26:45 +01:00
|
|
|
ReturnValue_t status = tmQueue->receiveMessage(¤tMessage);
|
2022-10-21 11:51:44 +02:00
|
|
|
while (status == returnvalue::OK) {
|
|
|
|
status = handlePacket(currentMessage);
|
|
|
|
if (status != returnvalue::OK) {
|
|
|
|
sif::warning << "CfdpTmFunnel packet handling failed" << std::endl;
|
|
|
|
break;
|
|
|
|
}
|
2022-11-02 10:26:45 +01:00
|
|
|
status = tmQueue->receiveMessage(¤tMessage);
|
2022-10-21 11:51:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (status == MessageQueueIF::EMPTY) {
|
|
|
|
return returnvalue::OK;
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t CfdpTmFunnel::initialize() { return returnvalue::OK; }
|
|
|
|
|
|
|
|
ReturnValue_t CfdpTmFunnel::handlePacket(TmTcMessage& msg) {
|
|
|
|
const uint8_t* cfdpPacket = nullptr;
|
|
|
|
size_t cfdpPacketLen = 0;
|
|
|
|
ReturnValue_t result = tmStore.getData(msg.getStorageId(), &cfdpPacket, &cfdpPacketLen);
|
|
|
|
if (result != returnvalue::OK) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
auto spacePacketHeader =
|
|
|
|
SpacePacketCreator(ccsds::PacketType::TM, false, cfdpInCcsdsApid,
|
|
|
|
ccsds::SequenceFlags::UNSEGMENTED, sourceSequenceCount++, 0);
|
|
|
|
sourceSequenceCount = sourceSequenceCount & ccsds::LIMIT_SEQUENCE_COUNT;
|
|
|
|
spacePacketHeader.setCcsdsLenFromTotalDataFieldLen(cfdpPacketLen);
|
|
|
|
uint8_t* newPacketData = nullptr;
|
|
|
|
store_address_t newStoreId{};
|
|
|
|
result =
|
|
|
|
tmStore.getFreeElement(&newStoreId, spacePacketHeader.getFullPacketLen(), &newPacketData);
|
|
|
|
if (result != returnvalue::OK) {
|
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
|
|
|
sif::warning << "CfdpTmFunnel::handlePacket: Error getting TM store element of size "
|
|
|
|
<< spacePacketHeader.getFullPacketLen() << std::endl;
|
|
|
|
#endif
|
|
|
|
return result;
|
|
|
|
}
|
2023-02-20 15:02:58 +01:00
|
|
|
size_t packetLen = 0;
|
|
|
|
uint8_t* serPtr = newPacketData;
|
2022-10-21 11:51:44 +02:00
|
|
|
result =
|
2023-02-20 15:02:58 +01:00
|
|
|
spacePacketHeader.serializeBe(&serPtr, &packetLen, spacePacketHeader.getFullPacketLen());
|
2022-10-21 11:51:44 +02:00
|
|
|
if (result != returnvalue::OK) {
|
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
|
|
|
sif::error << "CfdpTmFunnel::handlePacket: Error serializing packet" << std::endl;
|
|
|
|
#endif
|
|
|
|
return result;
|
|
|
|
}
|
2023-02-20 15:02:58 +01:00
|
|
|
std::memcpy(serPtr, cfdpPacket, cfdpPacketLen);
|
|
|
|
packetLen += cfdpPacketLen;
|
|
|
|
|
2022-10-21 11:51:44 +02:00
|
|
|
// Delete old packet
|
|
|
|
tmStore.deleteData(msg.getStorageId());
|
|
|
|
msg.setStorageId(newStoreId);
|
2022-11-02 10:26:45 +01:00
|
|
|
store_address_t origStoreId = newStoreId;
|
|
|
|
for (unsigned int idx = 0; idx < destinations.size(); idx++) {
|
2023-02-07 17:18:13 +01:00
|
|
|
const auto& dest = destinations[idx];
|
2022-11-02 10:55:40 +01:00
|
|
|
if (destinations.size() > 1) {
|
2022-11-03 15:29:54 +01:00
|
|
|
if (idx < destinations.size() - 1) {
|
2022-11-02 10:55:40 +01:00
|
|
|
// Create copy of data to ensure each TM recipient has its own copy. That way, we don't need
|
|
|
|
// to bother with send order and where the data is deleted.
|
|
|
|
store_address_t storeId;
|
2023-02-20 15:02:58 +01:00
|
|
|
result = tmStore.addData(&storeId, newPacketData, packetLen);
|
2022-11-03 15:29:54 +01:00
|
|
|
if (result == returnvalue::OK) {
|
|
|
|
msg.setStorageId(storeId);
|
|
|
|
} else {
|
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2023-02-20 15:02:58 +01:00
|
|
|
sif::error << "PusTmFunnel::handlePacket: Store too full to create data copy or store "
|
|
|
|
"error" << std::endl;
|
|
|
|
break;
|
2022-11-03 15:29:54 +01:00
|
|
|
#endif
|
|
|
|
}
|
2022-11-02 10:55:40 +01:00
|
|
|
} else {
|
|
|
|
msg.setStorageId(origStoreId);
|
|
|
|
}
|
2022-11-02 10:26:45 +01:00
|
|
|
}
|
2023-02-07 17:18:13 +01:00
|
|
|
result = tmQueue->sendMessage(dest.queueId, &msg);
|
2022-11-02 10:26:45 +01:00
|
|
|
if (result != returnvalue::OK) {
|
2022-10-21 11:51:44 +02:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2023-02-07 17:18:13 +01:00
|
|
|
sif::error << "PusTmFunnel::handlePacket: Error sending TM to downlink handler " << dest.name
|
|
|
|
<< " failed" << std::endl;
|
2022-10-21 11:51:44 +02:00
|
|
|
#endif
|
2022-11-02 10:26:45 +01:00
|
|
|
tmStore.deleteData(msg.getStorageId());
|
|
|
|
}
|
2022-10-21 11:51:44 +02:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|