F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
SpacePacketFramer.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title SpacePacketFramer.cpp
3 // \author thomas-bc
4 // \brief cpp file for SpacePacketFramer component implementation class
5 // ======================================================================
6 
10 
11 namespace Svc {
12 
13 namespace Ccsds {
14 
15 // ----------------------------------------------------------------------
16 // Component construction and destruction
17 // ----------------------------------------------------------------------
18 
20 
22 
23 // ----------------------------------------------------------------------
24 // Handler implementations for typed input ports
25 // ----------------------------------------------------------------------
26 
27 void SpacePacketFramer ::dataIn_handler(FwIndexType portNum, Fw::Buffer& data, const ComCfg::FrameContext& context) {
28  SpacePacketHeader header;
29  Fw::SerializeStatus status;
31  FW_ASSERT(data.getSize() <= std::numeric_limits<Fw::Buffer::SizeType>::max() - SpacePacketHeader::SERIALIZED_SIZE,
32  static_cast<FwAssertArgType>(data.getSize()));
33  FW_ASSERT(
34  data.getSize() > 0,
35  static_cast<FwAssertArgType>(data.getSize())); // Protocol specifies at least 1 byte of data for a valid packet
36 
37  // Allocate frame buffer
38  Fw::Buffer frameBuffer = this->bufferAllocate_out(0, static_cast<Fw::Buffer::SizeType>(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  return;
47  }
48  auto frameSerializer = frameBuffer.getSerializer();
49 
50  // -----------------------------------------------
51  // Header
52  // -----------------------------------------------
53  ComCfg::Apid::T apid = context.get_apid();
55  static_cast<FwAssertArgType>(apid)); // apid must fit in 11 bits
56  const U16 secHdrFlag = context.get_hasSecHdr() ? 1 : 0;
57  // PVN is always 0 per Standard - Packet Type is 0 for Telemetry (downlink)
58  // 11 bit APID, 1 bit SecHdr flag
59  const U16 packetIdentification = static_cast<U16>((static_cast<U16>(apid) & SpacePacketSubfields::ApidMask) |
60  (secHdrFlag << SpacePacketSubfields::SecHdrOffset));
61 
62  U16 sequenceCount = this->getApidSeqCount_out(0, apid, 0); // retrieve the sequence count for this APID
63  const U8 seqFlags = context.get_sequenceFlags();
64  // 2 bit sequence flags | 14 bit sequence count
65  U16 packetSequenceControl = static_cast<U16>(
67  (sequenceCount & SpacePacketSubfields::SeqCountMask));
68 
69  FW_ASSERT(data.getSize() <= std::numeric_limits<U16>::max(), static_cast<FwAssertArgType>(data.getSize()));
70  U16 packetDataLength =
71  static_cast<U16>(data.getSize() - 1); // Standard specifies length is number of bytes minus 1
72 
73  header.set_packetIdentification(packetIdentification);
74  header.set_packetSequenceControl(packetSequenceControl);
75  header.set_packetDataLength(packetDataLength);
76 
77  // -----------------------------------------------
78  // Serialize the packet
79  // -----------------------------------------------
80  status = frameSerializer.serializeFrom(header);
81  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, status);
82  status = frameSerializer.serializeFrom(data.getData(), data.getSize(), Fw::Serialization::OMIT_LENGTH);
83  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, status);
84 
85  // Trim to actual frame size in case allocator returned a larger buffer
86  frameBuffer.setSize(static_cast<Fw::Buffer::SizeType>(frameSize));
87 
88  this->dataOut_out(0, frameBuffer, context);
89  this->dataReturnOut_out(0, data, context); // return ownership of the original data buffer
90 }
91 
92 void SpacePacketFramer ::comStatusIn_handler(FwIndexType portNum, Fw::Success& condition) {
93  if (this->isConnected_comStatusOut_OutputPort(portNum)) {
94  this->comStatusOut_out(portNum, condition);
95  }
96 }
97 
98 void SpacePacketFramer ::dataReturnIn_handler(FwIndexType portNum,
99  Fw::Buffer& frameBuffer,
100  const ComCfg::FrameContext& context) {
101  // dataReturnIn is the allocated buffer coming back from the dataOut port
102  this->bufferDeallocate_out(0, frameBuffer);
103 }
104 
105 } // namespace Ccsds
106 } // namespace Svc
Serialization/Deserialization operation was successful.
~SpacePacketFramer()
Destroy SpacePacketFramer object.
PlatformSizeType FwSizeType
void dataReturnOut_out(FwIndexType portNum, Fw::Buffer &data, const ComCfg::FrameContext &context) const
Invoke output port dataReturnOut.
void setSize(FwSizeType size)
Definition: Buffer.cpp:131
ComCfg::Apid::T get_apid() const
Get member apid.
U8 * getData() const
Definition: Buffer.cpp:82
SpacePacketFramer(const char *const compName)
Construct SpacePacketFramer object.
SerializeStatus
forward declaration for string
void bufferDeallocate_out(FwIndexType portNum, Fw::Buffer &fwBuffer) const
Invoke output port bufferDeallocate.
void comStatusOut_out(FwIndexType portNum, Fw::Success &condition) const
Invoke output port comStatusOut.
bool isConnected_comStatusOut_OutputPort(FwIndexType portNum) const
Omit length from serialization.
T
The raw enum type.
Definition: ApidEnumAc.hpp:32
bool isValid() const
Definition: Buffer.cpp:78
Describes the frame header format for the SpacePacket communications protocol.
U8 get_sequenceFlags() const
Get member sequenceFlags.
bool get_hasSecHdr() const
Get member hasSecHdr.
Fw::Buffer bufferAllocate_out(FwIndexType portNum, FwSizeType size) const
Invoke output port bufferAllocate.
Auto-generated base for SpacePacketFramer component.
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:54
void set_packetDataLength(U16 packetDataLength)
Set member packetDataLength.
FwSizeType getSize() const
Definition: Buffer.cpp:90
void set_packetIdentification(U16 packetIdentification)
Set member packetIdentification.
U16 getApidSeqCount_out(FwIndexType portNum, const ComCfg::Apid &apid, U16 sequenceCount) const
Invoke output port getApidSeqCount.
PlatformIndexType FwIndexType
void set_packetSequenceControl(U16 packetSequenceControl)
Set member packetSequenceControl.
Type used to pass context info between components during framing/deframing.
void dataOut_out(FwIndexType portNum, Fw::Buffer &data, const ComCfg::FrameContext &context) const
Invoke output port dataOut.
RateGroupDivider component implementation.
ExternalSerializeBufferWithMemberCopy getSerializer()
Definition: Buffer.cpp:155
#define FW_ASSERT(...)
Definition: Assert.hpp:14
Success/Failure.
PlatformAssertArgType FwAssertArgType
The type of arguments to assert functions.