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"
|
2023-09-11 19:23:48 +02:00
|
|
|
#include "mission/sysDefs.h"
|
2022-10-21 11:51:44 +02:00
|
|
|
|
2023-09-07 16:13:46 +02:00
|
|
|
CfdpTmFunnel::CfdpTmFunnel(TmFunnelBase::FunnelCfg cfg,
|
|
|
|
std::optional<MessageQueueId_t> fileStoreDest,
|
2023-03-10 16:49:12 +01:00
|
|
|
StorageManagerIF& ramToFileStore, uint16_t cfdpInCcsdsApid)
|
|
|
|
: TmFunnelBase(cfg),
|
|
|
|
fileStoreDest(fileStoreDest),
|
|
|
|
ramToFileStore(ramToFileStore),
|
|
|
|
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;
|
2023-06-25 12:41:20 +02:00
|
|
|
ReturnValue_t status;
|
2023-02-19 17:06:08 +01:00
|
|
|
unsigned int count = 0;
|
2023-06-25 12:41:20 +02:00
|
|
|
if (saveSequenceCount) {
|
|
|
|
status = saveSequenceCountToFile();
|
|
|
|
if (status != returnvalue::OK) {
|
|
|
|
sif::error << "CfdpTmFunnel: Storing sequence count to file has failed" << std::endl;
|
|
|
|
}
|
|
|
|
saveSequenceCount = false;
|
|
|
|
}
|
|
|
|
status = tmQueue->receiveMessage(¤tMessage);
|
2023-09-06 13:40:38 +02:00
|
|
|
uint32_t handledPackets = 0;
|
2022-10-21 11:51:44 +02:00
|
|
|
while (status == returnvalue::OK) {
|
2023-09-06 13:40:38 +02:00
|
|
|
handledPackets++;
|
2022-10-21 11:51:44 +02:00
|
|
|
status = handlePacket(currentMessage);
|
|
|
|
if (status != returnvalue::OK) {
|
|
|
|
sif::warning << "CfdpTmFunnel packet handling failed" << std::endl;
|
|
|
|
break;
|
|
|
|
}
|
2023-02-19 17:06:08 +01:00
|
|
|
count++;
|
2023-02-21 20:43:16 +01:00
|
|
|
if (count == 500) {
|
2023-02-19 17:06:08 +01:00
|
|
|
sif::error << "CfdpTmFunnel: Possible message storm detected" << std::endl;
|
2022-10-21 11:51:44 +02:00
|
|
|
break;
|
|
|
|
}
|
2022-11-02 10:26:45 +01:00
|
|
|
status = tmQueue->receiveMessage(¤tMessage);
|
2022-10-21 11:51:44 +02:00
|
|
|
}
|
2023-09-06 13:40:38 +02:00
|
|
|
if (handledPackets > 0) {
|
|
|
|
// Very useful for profiling and debugging
|
2023-09-07 16:13:46 +02:00
|
|
|
// sif::debug << "CfdpFunnel: Handled " << handledPackets << " packets in one cycle" <<
|
|
|
|
// std::endl;
|
2023-09-06 13:40:38 +02:00
|
|
|
}
|
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 19:35:36 +01:00
|
|
|
|
2023-02-20 15:02:58 +01:00
|
|
|
size_t packetLen = 0;
|
|
|
|
uint8_t* serPtr = newPacketData;
|
2023-02-20 15:17:31 +01:00
|
|
|
result = 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;
|
2023-03-10 16:49:12 +01:00
|
|
|
|
2023-09-07 16:13:46 +02:00
|
|
|
if (fileStoreDest.has_value()) {
|
2023-09-06 13:40:38 +02:00
|
|
|
store_address_t storageId;
|
|
|
|
result = ramToFileStore.addData(&storageId, newPacketData, packetLen);
|
|
|
|
TmTcMessage fileMsg(storageId);
|
|
|
|
if (result == returnvalue::OK) {
|
|
|
|
tmQueue->sendMessage(fileStoreDest.value(), &fileMsg);
|
2023-09-07 16:13:46 +02:00
|
|
|
} else if (result == StorageManagerIF::DATA_STORAGE_FULL) {
|
|
|
|
sif::error << "CfdpTmFunnel::handlePacket: RAM to File Store too full to create data copy"
|
|
|
|
<< std::endl;
|
2023-09-06 13:40:38 +02:00
|
|
|
}
|
|
|
|
}
|
2023-03-09 11:46:13 +01:00
|
|
|
return demultiplexLivePackets(origStoreId, newPacketData, packetLen);
|
2022-10-21 11:51:44 +02:00
|
|
|
}
|
2023-09-11 19:23:48 +02:00
|
|
|
|
2023-09-11 19:27:39 +02:00
|
|
|
uint32_t CfdpTmFunnel::addLiveDestination(const char* name,
|
|
|
|
const AcceptsTelemetryIF& downlinkDestination,
|
|
|
|
uint8_t vcid) {
|
|
|
|
return TmFunnelBase::addLiveDestination(name, downlinkDestination, vcid);
|
2023-09-11 19:23:48 +02:00
|
|
|
}
|