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 ::framedIn_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->bufferDeallocate_out(0, data); // 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->bufferDeallocate_out(0, data);
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->bufferDeallocate_out(0, data);
58  return;
59  }
60 
61  // ---------------- Validate Frame Trailer ----------------
62  // Deserialize transmitted trailer: trailer is at offset = len(header) + len(body)
63  status = deserializer.moveDeserToOffset(FprimeProtocol::FrameHeader::SERIALIZED_SIZE + header.getlengthField());
65  status = trailer.deserialize(deserializer);
67  // Compute CRC over the transmitted data (header + body)
68  Utils::Hash hash;
69  Utils::HashBuffer computedCrc;
70  FwSizeType fieldToHashSize = header.getlengthField() + FprimeProtocol::FrameHeader::SERIALIZED_SIZE;
71  hash.init();
72  // Add byte by byte to the hash
73  for (FwSizeType i = 0; i < fieldToHashSize; i++) {
74  hash.update(data.getData() + i, 1);
75  }
76  hash.final(computedCrc);
77  // Check that the CRC in the trailer of the frame matches the computed CRC
78  if (trailer.getcrcField() != computedCrc.asBigEndianU32()) {
80  this->bufferDeallocate_out(0, data);
81  return;
82  }
83 
84  // ---------------- Extract payload from frame ----------------
85  // Shift data pointer to effectively remove the header
87  // Shrink size to effectively remove the trailer (also removes the header)
90  // Emit the deframed data
91  this->deframedOut_out(0, data, context);
92 }
93 
94 } // 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
PlatformSizeType FwSizeType
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 bufferDeallocate_out(FwIndexType portNum, Fw::Buffer &fwBuffer)
Invoke output port bufferDeallocate.
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
void deframedOut_out(FwIndexType portNum, Fw::Buffer &data, const ComCfg::FrameContext &context)
Invoke output port deframedOut.
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.
FprimeDeframer(const char *const compName)
Construct FprimeDeframer object.
SizeType getSize() const
Definition: Buffer.cpp:72
#define FW_ASSERT(...)
Definition: Assert.hpp:14