F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
FprimeFramer.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title FprimeFramer.cpp
3 // \author thomas-bc
4 // \brief cpp file for FprimeFramer component implementation class
5 // ======================================================================
6 
10 #include "Utils/Hash/Hash.hpp"
11 
12 namespace Svc {
13 
14 // ----------------------------------------------------------------------
15 // Component construction and destruction
16 // ----------------------------------------------------------------------
17 
18 FprimeFramer ::FprimeFramer(const char* const compName) : FprimeFramerComponentBase(compName) {}
19 
21 
22 // ----------------------------------------------------------------------
23 // Handler implementations for typed input ports
24 // ----------------------------------------------------------------------
25 
26 void FprimeFramer ::dataIn_handler(FwIndexType portNum, Fw::Buffer& data, const ComCfg::FrameContext& context) {
29 
30  // Full size of the frame will be size of header + data + trailer
31  FwSizeType frameSize =
33  FW_ASSERT(data.getSize() <= std::numeric_limits<FprimeProtocol::TokenType>::max(),
34  static_cast<FwAssertArgType>(frameSize));
35  FW_ASSERT(frameSize <= std::numeric_limits<Fw::Buffer::SizeType>::max(), static_cast<FwAssertArgType>(frameSize));
36 
37  // Allocate frame buffer
38  Fw::Buffer frameBuffer = this->bufferAllocate_out(0, frameSize);
39  // The allocator may return an invalid or smaller-than-requested buffer
40  if ((not frameBuffer.isValid()) || (frameBuffer.getSize() < frameSize)) {
42  if (frameBuffer.isValid()) {
43  this->bufferDeallocate_out(0, frameBuffer);
44  }
45  this->dataReturnOut_out(0, data, context);
46  // No frame produced: report SUCCESS so the upstream ComQueue keeps sending (Framer Status Protocol)
49  this->comStatusOut_out(0, status);
50  }
51  return;
52  }
53  auto frameSerializer = frameBuffer.getSerializer();
54  Fw::SerializeStatus status;
55 
56  // Serialize the header
57  // 0xDEADBEEF is already set as the default value for the header startWord field in the FPP type definition
58  header.set_lengthField(static_cast<FprimeProtocol::TokenType>(data.getSize()));
59  status = frameSerializer.serializeFrom(header);
60  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, status);
61 
62  // Serialize the data
63  status = frameSerializer.serializeFrom(data.getData(), data.getSize(), Fw::Serialization::OMIT_LENGTH);
64  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, status);
65 
66  // Serialize the trailer (with CRC computation)
67  Utils::HashBuffer hashBuffer;
68  Utils::Hash::hash(frameBuffer.getData(), frameSize - HASH_DIGEST_LENGTH, hashBuffer);
69  trailer.set_crcField(hashBuffer.asBigEndianU32());
70  status = frameSerializer.serializeFrom(trailer);
71  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, status);
72  // Trim to actual frame size in case allocator returned a larger buffer
73  frameBuffer.setSize(static_cast<Fw::Buffer::SizeType>(frameSize));
74 
75  // Send the full frame out - this port shall always be connected
76  this->dataOut_out(0, frameBuffer, context);
77  // Return original (unframed) data buffer ownership back to its sender - always connected
78  this->dataReturnOut_out(0, data, context);
79 }
80 
81 void FprimeFramer ::comStatusIn_handler(FwIndexType portNum, Fw::Success& condition) {
82  if (this->isConnected_comStatusOut_OutputPort(portNum)) {
83  this->comStatusOut_out(portNum, condition);
84  }
85 }
86 
87 void FprimeFramer ::dataReturnIn_handler(FwIndexType portNum,
88  Fw::Buffer& frameBuffer,
89  const ComCfg::FrameContext& context) {
90  // dataReturnIn is the allocated buffer coming back from the ComManager (e.g. ComStub) component
91  this->bufferDeallocate_out(0, frameBuffer);
92 }
93 
94 } // namespace Svc
Serialization/Deserialization operation was successful.
void dataOut_out(FwIndexType portNum, Fw::Buffer &data, const ComCfg::FrameContext &context) const
Invoke output port dataOut.
Representing success.
PlatformSizeType FwSizeType
void setSize(FwSizeType size)
Definition: Buffer.cpp:131
Auto-generated base for FprimeFramer component.
U8 * getData() const
Definition: Buffer.cpp:82
Describes the frame header format for the F Prime communications protocol.
SerializeStatus
forward declaration for string
Fw::Buffer bufferAllocate_out(FwIndexType portNum, FwSizeType size) const
Invoke output port bufferAllocate.
void dataReturnOut_out(FwIndexType portNum, Fw::Buffer &data, const ComCfg::FrameContext &context) const
Invoke output port dataReturnOut.
Omit length from serialization.
void comStatusOut_out(FwIndexType portNum, Fw::Success &condition) const
Invoke output port comStatusOut.
bool isValid() const
Definition: Buffer.cpp:78
#define HASH_DIGEST_LENGTH
Definition: Crc32.hpp:26
Describes the frame trailer format for the F Prime communications protocol.
~FprimeFramer()
Destroy FprimeFramer object.
FwSizeType getSize() const
Definition: Buffer.cpp:90
static void hash(const void *data, const FwSizeType len, HashBuffer &buffer)
Definition: HashImpl.cpp:24
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 set_crcField(U32 crcField)
Set member crcField.
void bufferDeallocate_out(FwIndexType portNum, Fw::Buffer &fwBuffer) const
Invoke output port bufferDeallocate.
FprimeFramer(const char *const compName)
Construct FprimeFramer object.
void set_lengthField(Svc::FprimeProtocol::TokenType lengthField)
Set member lengthField.
ExternalSerializeBufferWithMemberCopy getSerializer()
Definition: Buffer.cpp:155
#define FW_ASSERT(...)
Definition: Assert.hpp:14
Success/Failure.
PlatformAssertArgType FwAssertArgType
The type of arguments to assert functions.
bool isConnected_comStatusOut_OutputPort(FwIndexType portNum) const