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.getBuffLeft() < FprimeProtocol::FrameTrailer::SERIALIZED_SIZE + sizeof(FwPacketDescriptorType)) {
63  // Not enough data to read a valid FwPacketDescriptor, emit event and skip attempting to read an APID
65  } else {
66  // If PacketDescriptor translates to an invalid APID, let it default to FW_PACKET_UNKNOWN
67  // and let downstream components (e.g. custom router) handle it
68  FwPacketDescriptorType packetDescriptor;
69  status = deserializer.deserializeTo(packetDescriptor);
71  // If a valid descriptor is deserialized, set it in the context
72  if ((packetDescriptor < ComCfg::Apid::INVALID_UNINITIALIZED)) {
73  contextCopy.set_apid(static_cast<ComCfg::Apid::T>(packetDescriptor));
74  }
75  }
76 
77  // ---------------- Validate Frame Trailer ----------------
78  // Deserialize transmitted trailer: trailer is at offset = len(header) + len(body)
79  status = deserializer.moveDeserToOffset(FprimeProtocol::FrameHeader::SERIALIZED_SIZE + header.get_lengthField());
81  status = trailer.deserializeFrom(deserializer);
83  // Compute CRC over the transmitted data (header + body)
84  Utils::Hash hash;
85  Utils::HashBuffer computedCrc;
86  FwSizeType fieldToHashSize = header.get_lengthField() + FprimeProtocol::FrameHeader::SERIALIZED_SIZE;
87  hash.init();
88  // Add byte by byte to the hash
89  for (FwSizeType i = 0; i < fieldToHashSize; i++) {
90  hash.update(data.getData() + i, 1);
91  }
92  hash.final(computedCrc);
93  // Check that the CRC in the trailer of the frame matches the computed CRC
94  if (trailer.get_crcField() != computedCrc.asBigEndianU32()) {
96  this->dataReturnOut_out(0, data, context); // drop the frame
97  return;
98  }
99 
100  // ---------------- Extract payload from frame ----------------
101  // Shift data pointer to effectively remove the header
103  // Shrink size to effectively remove the trailer (also removes the header)
106  // Emit the deframed data
107  this->dataOut_out(0, data, contextCopy);
108 }
109 
110 void FprimeDeframer ::dataReturnIn_handler(FwIndexType portNum,
111  Fw::Buffer& fwBuffer,
112  const ComCfg::FrameContext& context) {
113  this->dataReturnOut_out(0, fwBuffer, context);
114 }
115 
116 } // 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