F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
FprimeDeframer.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title FprimeDeframer.cpp
3 // \author thomas-bc
4 // \brief cpp file for FprimeDeframer component implementation class
5 // ======================================================================
6 
9 #include "Fw/Types/Assert.hpp"
10 
13 
14 namespace Svc {
15 
16 // ----------------------------------------------------------------------
17 // Component construction and destruction
18 // ----------------------------------------------------------------------
19 
20 FprimeDeframer ::FprimeDeframer(const char* const compName) : FprimeDeframerComponentBase(compName) {}
21 
23 
24 // ----------------------------------------------------------------------
25 // Handler implementations for user-defined typed input ports
26 // ----------------------------------------------------------------------
27 
28 void FprimeDeframer ::dataIn_handler(FwIndexType portNum, Fw::Buffer& data, const ComCfg::FrameContext& context) {
30  // Incoming buffer is not long enough to contain a valid frame (header+trailer)
32  this->dataReturnOut_out(0, data, context); // drop the frame
33  return;
34  }
35 
36  // Header and Trailer objects to hold the deserialized data (types are autocoded by FPP)
37  FprimeProtocol::FrameHeader header;
38  FprimeProtocol::FrameTrailer trailer;
39 
40  // ---------------- Validate Frame Header ----------------
41  // Deserialize transmitted header into the header object
42  auto deserializer = data.getDeserializer();
43  Fw::SerializeStatus status = header.deserialize(deserializer);
45  // Check that deserialized start_word token matches expected value (default start_word value in the FPP object)
46  const FprimeProtocol::FrameHeader defaultValue;
47  if (header.getstartWord() != defaultValue.getstartWord()) {
49  this->dataReturnOut_out(0, data, context); // drop the frame
50  return;
51  }
52  // We expect the frame size to be size of header + body (of size specified in header) + trailer
53  const FwSizeType expectedFrameSize = FprimeProtocol::FrameHeader::SERIALIZED_SIZE + header.getlengthField() +
55  if (data.getSize() < expectedFrameSize) {
57  this->dataReturnOut_out(0, data, context); // drop the frame
58  return;
59  }
60  // -------- Attempt to extract APID from Payload --------
61  // If PacketDescriptor translates to an invalid APID, let it default to FW_PACKET_UNKNOWN
62  // and let downstream components (e.g. custom router) handle it
63  FwPacketDescriptorType packetDescriptor;
64  status = deserializer.deserialize(packetDescriptor);
66  ComCfg::FrameContext contextCopy = context;
67  // If a valid descriptor is deserialized, set it in the context
68  if (packetDescriptor < ComCfg::APID::INVALID_UNINITIALIZED) {
69  contextCopy.setapid(static_cast<ComCfg::APID::T>(packetDescriptor));
70  }
71 
72  // ---------------- Validate Frame Trailer ----------------
73  // Deserialize transmitted trailer: trailer is at offset = len(header) + len(body)
74  status = deserializer.moveDeserToOffset(FprimeProtocol::FrameHeader::SERIALIZED_SIZE + header.getlengthField());
76  status = trailer.deserialize(deserializer);
78  // Compute CRC over the transmitted data (header + body)
79  Utils::Hash hash;
80  Utils::HashBuffer computedCrc;
81  FwSizeType fieldToHashSize = header.getlengthField() + FprimeProtocol::FrameHeader::SERIALIZED_SIZE;
82  hash.init();
83  // Add byte by byte to the hash
84  for (FwSizeType i = 0; i < fieldToHashSize; i++) {
85  hash.update(data.getData() + i, 1);
86  }
87  hash.final(computedCrc);
88  // Check that the CRC in the trailer of the frame matches the computed CRC
89  if (trailer.getcrcField() != computedCrc.asBigEndianU32()) {
91  this->dataReturnOut_out(0, data, context); // drop the frame
92  return;
93  }
94 
95  // ---------------- Extract payload from frame ----------------
96  // Shift data pointer to effectively remove the header
98  // Shrink size to effectively remove the trailer (also removes the header)
101  // Emit the deframed data
102  this->dataOut_out(0, data, contextCopy);
103 }
104 
105 void FprimeDeframer ::dataReturnIn_handler(FwIndexType portNum, Fw::Buffer& fwBuffer, const ComCfg::FrameContext& context) {
106  this->dataReturnOut_out(0, fwBuffer, context);
107 }
108 
109 
110 } // namespace Svc
void update(const void *const data, const FwSizeType len)
Definition: CRC32.cpp:56
Serialization/Deserialization operation was successful.
void setData(U8 *data)
Definition: Buffer.cpp:80
void setapid(ComCfg::APID::T apid)
Set member apid.
FwIdType FwPacketDescriptorType
The type of a com packet descriptor.
PlatformSizeType FwSizeType
void dataReturnOut_out(FwIndexType portNum, Fw::Buffer &data, const ComCfg::FrameContext &context)
Invoke output port dataReturnOut.
void setSize(SizeType size)
Definition: Buffer.cpp:87
~FprimeDeframer()
Destroy FprimeDeframer object.
U8 * getData() const
Definition: Buffer.cpp:68
void init()
Definition: CRC32.cpp:50
SerializeStatus
forward declaration for string
void final(HashBuffer &buffer)
Definition: CRC32.cpp:67
ExternalSerializeBufferWithMemberCopy getDeserializer()
Definition: Buffer.cpp:117
Auto-generated base for FprimeDeframer component.
A generic interface for creating and comparing hash values.
Definition: Hash.hpp:24
SerializeStatus deserialize(U8 &val)
deserialize 8-bit unsigned int
PlatformIndexType FwIndexType
U32 asBigEndianU32() const
Convert bytes 0 through 3 of the hash data to a big-Endian U32 value.
A container class for holding a hash buffer.
Definition: HashBuffer.hpp:26
Type used to pass context info between components during framing/deframing.
RateGroupDivider component implementation.
void dataOut_out(FwIndexType portNum, Fw::Buffer &data, const ComCfg::FrameContext &context)
Invoke output port dataOut.
Anything equal or higher value is invalid and should not be used.
Definition: APIDEnumAc.hpp:53
FprimeDeframer(const char *const compName)
Construct FprimeDeframer object.
SizeType getSize() const
Definition: Buffer.cpp:72
#define FW_ASSERT(...)
Definition: Assert.hpp:14