F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
FprimeRouter.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title FprimeRouter.cpp
3 // \author thomas-bc
4 // \brief cpp file for FprimeRouter component implementation class
5 // ======================================================================
6 
9 #include "Fw/Com/ComPacket.hpp"
10 #include "Fw/Logger/Logger.hpp"
11 #include "config/APIDEnumAc.hpp"
12 
13 namespace Svc {
14 
15 // ----------------------------------------------------------------------
16 // Component construction and destruction
17 // ----------------------------------------------------------------------
18 
19 FprimeRouter ::FprimeRouter(const char* const compName) : FprimeRouterComponentBase(compName) {}
20 
22 
23 // ----------------------------------------------------------------------
24 // Handler implementations for user-defined typed input ports
25 // ----------------------------------------------------------------------
26 
27 void FprimeRouter ::dataIn_handler(FwIndexType portNum, Fw::Buffer& packetBuffer, const ComCfg::FrameContext& context) {
28  Fw::SerializeStatus status;
29  Fw::ComPacketType packetType = context.getapid();
30  // Route based on received APID (packet type)
31  switch (packetType) {
32  // Handle a command packet
33  case Fw::ComPacketType::FW_PACKET_COMMAND: {
34  // Allocate a com buffer on the stack
35  Fw::ComBuffer com;
36  // Copy the contents of the packet buffer into the com buffer
37  status = com.setBuff(packetBuffer.getData(), packetBuffer.getSize());
38  if (status == Fw::FW_SERIALIZE_OK) {
39  // Send the com buffer - critical functionality so it is considered an error not to
40  // have the port connected. This is why we don't check isConnected() before sending.
41  this->commandOut_out(0, com, 0);
42  } else {
44  }
45  break;
46  }
47  // Handle a file packet
48  case Fw::ComPacketType::FW_PACKET_FILE: {
49  // If the file uplink output port is connected, send the file packet. Otherwise take no action.
50  if (this->isConnected_fileOut_OutputPort(0)) {
51  // Copy buffer into a new allocated buffer. This lets us return the original buffer with dataReturnOut,
52  // and FprimeRouter can handle the deallocation of the file buffer when it returns on fileBufferReturnIn
53  Fw::Buffer packetBufferCopy = this->bufferAllocate_out(0, packetBuffer.getSize());
54  auto copySerializer = packetBufferCopy.getSerializer();
55  status = copySerializer.serialize(packetBuffer.getData(), packetBuffer.getSize(), Fw::Serialization::OMIT_LENGTH);
56  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, status);
57  // Send the copied buffer out. It will come back on fileBufferReturnIn once the receiver is done with it
58  this->fileOut_out(0, packetBufferCopy);
59  }
60  break;
61  }
62  default: {
63  // Packet type is not known to the F Prime protocol. If the unknownDataOut port is
64  // connected, forward packet and context for further processing
66  // Copy buffer into a new allocated buffer. This lets us return the original buffer with dataReturnOut,
67  // and FprimeRouter can handle the deallocation of the unknown buffer when it returns on bufferReturnIn
68  Fw::Buffer packetBufferCopy = this->bufferAllocate_out(0, packetBuffer.getSize());
69  auto copySerializer = packetBufferCopy.getSerializer();
70  status = copySerializer.serialize(packetBuffer.getData(), packetBuffer.getSize(), Fw::Serialization::OMIT_LENGTH);
71  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, status);
72  // Send the copied buffer out. It will come back on fileBufferReturnIn once the receiver is done with it
73  this->unknownDataOut_out(0, packetBufferCopy, context);
74  }
75  }
76  }
77 
78  // Return ownership of the incoming packetBuffer
79  this->dataReturnOut_out(0, packetBuffer, context);
80 }
81 
82 void FprimeRouter ::cmdResponseIn_handler(FwIndexType portNum,
83  FwOpcodeType opcode,
84  U32 cmdSeq,
85  const Fw::CmdResponse& response) {
86  // Nothing to do
87 }
88 
89 void FprimeRouter ::fileBufferReturnIn_handler(FwIndexType portNum, Fw::Buffer& fwBuffer) {
90  this->bufferDeallocate_out(0, fwBuffer);
91 }
92 
93 } // namespace Svc
void commandOut_out(FwIndexType portNum, Fw::ComBuffer &data, U32 context)
Invoke output port commandOut.
Serialization/Deserialization operation was successful.
FwIdType FwOpcodeType
The type of a command opcode.
SerializeStatus serialize(U8 val)
serialize 8-bit unsigned int
U8 * getData() const
Definition: Buffer.cpp:68
Fw::Buffer bufferAllocate_out(FwIndexType portNum, U32 size)
Invoke output port bufferAllocate.
void fileOut_out(FwIndexType portNum, Fw::Buffer &fwBuffer)
Invoke output port fileOut.
Enum representing a command response.
bool isConnected_fileOut_OutputPort(FwIndexType portNum)
ComCfg::APID::T getapid() const
Get member apid.
void bufferDeallocate_out(FwIndexType portNum, Fw::Buffer &fwBuffer)
Invoke output port bufferDeallocate.
SerializeStatus
forward declaration for string
T
The raw enum type.
Definition: APIDEnumAc.hpp:31
FprimeRouter(const char *const compName)
Construct FprimeRouter object.
~FprimeRouter()
Destroy FprimeRouter object.
Auto-generated base for FprimeRouter component.
void unknownDataOut_out(FwIndexType portNum, Fw::Buffer &data, const ComCfg::FrameContext &context)
Invoke output port unknownDataOut.
Omit length from serialization.
PlatformIndexType FwIndexType
SerializeStatus setBuff(const U8 *src, Serializable::SizeType length)
sets buffer contents and size
void log_WARNING_HI_SerializationError(U32 status) const
Type used to pass context info between components during framing/deframing.
RateGroupDivider component implementation.
void dataReturnOut_out(FwIndexType portNum, Fw::Buffer &data, const ComCfg::FrameContext &context)
Invoke output port dataReturnOut.
SizeType getSize() const
Definition: Buffer.cpp:72
bool isConnected_unknownDataOut_OutputPort(FwIndexType portNum)
ExternalSerializeBufferWithMemberCopy getSerializer()
Definition: Buffer.cpp:107
#define FW_ASSERT(...)
Definition: Assert.hpp:14