fprime/Svc/FprimeRouter/FprimeRouter.cpp
Thomas Boyer-Chammard c3b2e04880
Use data return pattern on Uplink and standardize port names (#3546)
* First pass at Svc + TcpClient implementation

* Revert FileUplink changes

* Add copy (with allocation/deallocation) to FprimeRouter to simplify buffer management

* Update FprimeRouter UTs

* Update FprimeDeframer UTs

* Update FrameAccumulator UTs

* Update ComStub UTs

* Update missing Drv and UTs

* Update ComInterface to use ComDataWithContext on output

* Update Ref/RPI topology

* Fix spelling

* Fix test typo

* Update Udp component and UTs

* Rename data ports and standardize "Return" naming pattern

* Fix variable name

* Adapt UTs

* Update Communication Adapter Interface docs

* Full SDD updates

* Spelling &  nits and details

* Put formatting back to original

* Update Deframer interface to include bufferReturn

* Address review comments
2025-05-08 10:54:54 -07:00

105 lines
5.2 KiB
C++

// ======================================================================
// \title FprimeRouter.cpp
// \author thomas-bc
// \brief cpp file for FprimeRouter component implementation class
// ======================================================================
#include "Svc/FprimeRouter/FprimeRouter.hpp"
#include "Fw/FPrimeBasicTypes.hpp"
#include "Fw/Com/ComPacket.hpp"
#include "Fw/Logger/Logger.hpp"
namespace Svc {
// ----------------------------------------------------------------------
// Component construction and destruction
// ----------------------------------------------------------------------
FprimeRouter ::FprimeRouter(const char* const compName) : FprimeRouterComponentBase(compName) {}
FprimeRouter ::~FprimeRouter() {}
// ----------------------------------------------------------------------
// Handler implementations for user-defined typed input ports
// ----------------------------------------------------------------------
void FprimeRouter ::dataIn_handler(FwIndexType portNum, Fw::Buffer& packetBuffer, const ComCfg::FrameContext& context) {
// Read the packet type from the packet buffer
FwPacketDescriptorType packetType = Fw::ComPacket::FW_PACKET_UNKNOWN;
Fw::SerializeStatus status = Fw::FW_SERIALIZE_OK;
{
auto esb = packetBuffer.getDeserializer();
status = esb.deserialize(packetType);
}
// Process the packet
if (status == Fw::FW_SERIALIZE_OK) {
U8* const packetData = packetBuffer.getData();
const FwSizeType packetSize = packetBuffer.getSize();
switch (packetType) {
// Handle a command packet
case Fw::ComPacket::FW_PACKET_COMMAND: {
// Allocate a com buffer on the stack
Fw::ComBuffer com;
// Copy the contents of the packet buffer into the com buffer
status = com.setBuff(packetData, packetSize);
if (status == Fw::FW_SERIALIZE_OK) {
// Send the com buffer - critical functionality so it is considered an error not to
// have the port connected. This is why we don't check isConnected() before sending.
this->commandOut_out(0, com, 0);
} else {
this->log_WARNING_HI_SerializationError(status);
}
break;
}
// Handle a file packet
case Fw::ComPacket::FW_PACKET_FILE: {
// If the file uplink output port is connected, send the file packet. Otherwise take no action.
if (this->isConnected_fileOut_OutputPort(0)) {
// Copy buffer into a new allocated buffer. This lets us return the original buffer with dataReturnOut,
// and FprimeRouter can handle the deallocation of the file buffer when it returns on fileBufferReturnIn
Fw::Buffer packetBufferCopy = this->bufferAllocate_out(0, packetBuffer.getSize());
auto copySerializer = packetBufferCopy.getSerializer();
status = copySerializer.serialize(packetBuffer.getData(), packetBuffer.getSize(), Fw::Serialization::OMIT_LENGTH);
FW_ASSERT(status == Fw::FW_SERIALIZE_OK, status);
// Send the copied buffer out. It will come back on fileBufferReturnIn once the receiver is done with it
this->fileOut_out(0, packetBufferCopy);
}
break;
}
default: {
// Packet type is not known to the F Prime protocol. If the unknownDataOut port is
// connected, forward packet and context for further processing
if (this->isConnected_unknownDataOut_OutputPort(0)) {
// Copy buffer into a new allocated buffer. This lets us return the original buffer with dataReturnOut,
// and FprimeRouter can handle the deallocation of the unknown buffer when it returns on bufferReturnIn
Fw::Buffer packetBufferCopy = this->bufferAllocate_out(0, packetBuffer.getSize());
auto copySerializer = packetBufferCopy.getSerializer();
status = copySerializer.serialize(packetBuffer.getData(), packetBuffer.getSize(), Fw::Serialization::OMIT_LENGTH);
FW_ASSERT(status == Fw::FW_SERIALIZE_OK, status);
// Send the copied buffer out. It will come back on fileBufferReturnIn once the receiver is done with it
this->unknownDataOut_out(0, packetBufferCopy, context);
}
}
}
} else {
this->log_WARNING_HI_DeserializationError(status);
}
// Return ownership of the incoming packetBuffer
this->dataReturnOut_out(0, packetBuffer, context);
}
void FprimeRouter ::cmdResponseIn_handler(FwIndexType portNum,
FwOpcodeType opcode,
U32 cmdSeq,
const Fw::CmdResponse& response) {
// Nothing to do
}
void FprimeRouter ::fileBufferReturnIn_handler(FwIndexType portNum, Fw::Buffer& fwBuffer) {
this->bufferDeallocate_out(0, fwBuffer);
}
} // namespace Svc