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  auto frameSerializer = frameBuffer.getSerializer();
40 
41  // -----------------------------------------------
42  // Header
43  // -----------------------------------------------
44  ComCfg::Apid::T apid = context.get_apid();
46  static_cast<FwAssertArgType>(apid)); // apid must fit in 11 bits
47  const U16 secHdrFlag = context.get_hasSecHdr() ? 1 : 0;
48  // PVN is always 0 per Standard - Packet Type is 0 for Telemetry (downlink)
49  // 11 bit APID, 1 bit SecHdr flag
50  const U16 packetIdentification = static_cast<U16>((static_cast<U16>(apid) & SpacePacketSubfields::ApidMask) |
51  (secHdrFlag << SpacePacketSubfields::SecHdrOffset));
52 
53  U16 sequenceCount = this->getApidSeqCount_out(0, apid, 0); // retrieve the sequence count for this APID
54  U16 packetSequenceControl = 0;
55  packetSequenceControl |=
56  0x3 << SpacePacketSubfields::SeqFlagsOffset; // Sequence Flags 0b11 = unsegmented User Data
57  packetSequenceControl |=
58  sequenceCount & static_cast<U16>(SpacePacketSubfields::SeqCountMask); // 14 bit sequence count
59 
60  FW_ASSERT(data.getSize() <= std::numeric_limits<U16>::max(), static_cast<FwAssertArgType>(data.getSize()));
61  U16 packetDataLength =
62  static_cast<U16>(data.getSize() - 1); // Standard specifies length is number of bytes minus 1
63 
64  header.set_packetIdentification(packetIdentification);
65  header.set_packetSequenceControl(packetSequenceControl);
66  header.set_packetDataLength(packetDataLength);
67 
68  // -----------------------------------------------
69  // Serialize the packet
70  // -----------------------------------------------
71  status = frameSerializer.serializeFrom(header);
72  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, status);
73  status = frameSerializer.serializeFrom(data.getData(), data.getSize(), Fw::Serialization::OMIT_LENGTH);
74  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, status);
75 
76  // Trim to actual frame size in case allocator returned a larger buffer
77  frameBuffer.setSize(static_cast<Fw::Buffer::SizeType>(frameSize));
78 
79  this->dataOut_out(0, frameBuffer, context);
80  this->dataReturnOut_out(0, data, context); // return ownership of the original data buffer
81 }
82 
83 void SpacePacketFramer ::comStatusIn_handler(FwIndexType portNum, Fw::Success& condition) {
84  if (this->isConnected_comStatusOut_OutputPort(portNum)) {
85  this->comStatusOut_out(portNum, condition);
86  }
87 }
88 
89 void SpacePacketFramer ::dataReturnIn_handler(FwIndexType portNum,
90  Fw::Buffer& frameBuffer,
91  const ComCfg::FrameContext& context) {
92  // dataReturnIn is the allocated buffer coming back from the dataOut port
93  this->bufferDeallocate_out(0, frameBuffer);
94 }
95 
96 } // namespace Ccsds
97 } // 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:75
ComCfg::Apid::T get_apid() const
Get member apid.
U8 * getData() const
Definition: Buffer.cpp:56
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:31
Describes the frame header format for the SpacePacket communications protocol.
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.
void set_packetDataLength(U16 packetDataLength)
Set member packetDataLength.
FwSizeType getSize() const
Definition: Buffer.cpp:60
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:95
#define FW_ASSERT(...)
Definition: Assert.hpp:14
Success/Failure.
PlatformAssertArgType FwAssertArgType
The type of arguments to assert functions.