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.deserializeFrom(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.get_startWord() != defaultValue.get_startWord()) {
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.get_lengthField() +
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  ComCfg::FrameContext contextCopy = context;
62  if (deserializer.getDeserializeSizeLeft() <
64  // Not enough data to read a valid FwPacketDescriptor, emit event and skip attempting to read an APID
66  } else {
67  // If PacketDescriptor translates to an invalid APID, let it default to FW_PACKET_UNKNOWN
68  // and let downstream components (e.g. custom router) handle it
69  FwPacketDescriptorType packetDescriptor;
70  status = deserializer.deserializeTo(packetDescriptor);
72  // If a valid descriptor is deserialized, set it in the context
73  if ((packetDescriptor < ComCfg::Apid::INVALID_UNINITIALIZED)) {
74  contextCopy.set_apid(static_cast<ComCfg::Apid::T>(packetDescriptor));
75  }
76  }
77 
78  // ---------------- Validate Frame Trailer ----------------
79  // Deserialize transmitted trailer: trailer is at offset = len(header) + len(body)
80  status = deserializer.moveDeserToOffset(FprimeProtocol::FrameHeader::SERIALIZED_SIZE + header.get_lengthField());
82  status = trailer.deserializeFrom(deserializer);
84  // Compute CRC over the transmitted data (header + body)
85  Utils::Hash hash;
86  Utils::HashBuffer computedCrc;
87  FwSizeType fieldToHashSize = header.get_lengthField() + FprimeProtocol::FrameHeader::SERIALIZED_SIZE;
88  hash.init();
89  // Add byte by byte to the hash
90  for (FwSizeType i = 0; i < fieldToHashSize; i++) {
91  hash.update(data.getData() + i, 1);
92  }
93  hash.final(computedCrc);
94  // Check that the CRC in the trailer of the frame matches the computed CRC
95  if (trailer.get_crcField() != computedCrc.asBigEndianU32()) {
97  this->dataReturnOut_out(0, data, context); // drop the frame
98  return;
99  }
100 
101  // ---------------- Extract payload from frame ----------------
102  // Shift data pointer to effectively remove the header
104  // Shrink size to effectively remove the trailer (also removes the header)
107  // Emit the deframed data
108  this->dataOut_out(0, data, contextCopy);
109 }
110 
111 void FprimeDeframer ::dataReturnIn_handler(FwIndexType portNum,
112  Fw::Buffer& fwBuffer,
113  const ComCfg::FrameContext& context) {
114  this->dataReturnOut_out(0, fwBuffer, context);
115 }
116 
117 } // namespace Svc
void update(const void *const data, const FwSizeType len)
Definition: CRC32.cpp:45
Serialization/Deserialization operation was successful.
void setData(U8 *data)
Definition: Buffer.cpp:68
U16 FwPacketDescriptorType
The width of packet descriptors when they are serialized by the framework.
PlatformSizeType FwSizeType
void dataReturnOut_out(FwIndexType portNum, Fw::Buffer &data, const ComCfg::FrameContext &context)
Invoke output port dataReturnOut.
void setSize(FwSizeType size)
Definition: Buffer.cpp:75
~FprimeDeframer()
Destroy FprimeDeframer object.
void set_apid(ComCfg::Apid::T apid)
Set member apid.
U8 * getData() const
Definition: Buffer.cpp:56
void init()
Definition: CRC32.cpp:41
SerializeStatus
forward declaration for string
void final(HashBuffer &buffer)
Definition: CRC32.cpp:54
ExternalSerializeBufferWithMemberCopy getDeserializer()
Definition: Buffer.cpp:105
Auto-generated base for FprimeDeframer component.
A generic interface for creating and comparing hash values.
Definition: Hash.hpp:24
FwSizeType getSize() const
Definition: Buffer.cpp:60
Anything equal or higher value is invalid and should not be used.
Definition: ApidEnumAc.hpp:53
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.
FprimeDeframer(const char *const compName)
Construct FprimeDeframer object.
#define FW_ASSERT(...)
Definition: Assert.hpp:14