F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
SpacePacketDeframer.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title SpacePacketDeframer.cpp
3 // \author thomas-bc
4 // \brief cpp file for SpacePacketDeframer 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 namespace {
24 
25 bool isValidPacketVersionNumber(const SpacePacketHeader& header) {
26  const U16 packetIdentification = header.get_packetIdentification();
27  const U16 pvn = (packetIdentification & SpacePacketSubfields::PvnMask) >> SpacePacketSubfields::PvnOffset;
28  return pvn == static_cast<U16>(ComCfg::Pvn::SPACE_PACKET_PROTOCOL);
29 }
30 
31 } // namespace
32 
33 // ----------------------------------------------------------------------
34 // Handler implementations for typed input ports
35 // ----------------------------------------------------------------------
36 
37 void SpacePacketDeframer ::dataIn_handler(FwIndexType portNum, Fw::Buffer& data, const ComCfg::FrameContext& context) {
38  // ################################
39  // CCSDS SpacePacket Format:
40  // 6 octets - Primary Header
41  // 0-65536 octets - Data Field (with optional secondary header)
42 
43  // CCSDS SpacePacket Primary Header:
44  // 3b - 000 - (PVN) Packet Version Number
45  // 1b - 0/1 - (PT) Packet Type
46  // 1b - 0/1 - (SHF) Secondary Header Flag
47  // 11b - n/a - (APID) Application Process ID
48  // 2b - 00/01/10/11 - Sequence Flag
49  // 14b - n/a - Sequence Count
50  // 16b - n/a - Packet Data Length
51  // ################################
52 
53  // Check size and drop packets that are too-small
58  }
59  this->dataReturnOut_out(0, data, context); // Drop the packet
60  return;
61  }
62 
63  SpacePacketHeader header;
64  Fw::SerializeStatus status = data.getDeserializer().deserializeTo(header);
65  // Deserialization can still fail if the buffer is malformed despite passing the size check
66  if (status != Fw::FW_SERIALIZE_OK) {
70  }
71  this->dataReturnOut_out(0, data, context); // Drop the packet
72  return;
73  }
74 
75  if (!isValidPacketVersionNumber(header)) {
79  }
80  this->dataReturnOut_out(0, data, context); // Drop the packet
81  return;
82  }
83 
84  // Widen to U32 before adding 1 to prevent U16 truncation to 0 when packetDataLength == 0xFFFF (max U16 value).
85  // This is a undefined behavior condition in C++.
86  const U32 pkt_length = static_cast<U32>(header.get_packetDataLength()) + 1U;
87  if ((pkt_length > data.getSize() - SpacePacketHeader::SERIALIZED_SIZE) ||
88  (pkt_length > std::numeric_limits<FwSizeType>::max())) {
89  FwSizeType maxDataAvailable = data.getSize() - SpacePacketHeader::SERIALIZED_SIZE;
90  this->log_WARNING_HI_InvalidLength(pkt_length, maxDataAvailable);
93  }
94  this->dataReturnOut_out(0, data, context); // Drop the packet
95  return;
96  }
97 
98  U16 apidValue = header.get_packetIdentification() & SpacePacketSubfields::ApidMask;
99  ComCfg::Apid::T apid = static_cast<ComCfg::Apid::T>(apidValue);
100  ComCfg::FrameContext contextCopy = context;
101  contextCopy.set_apid(apid);
102 
103  // Extract secondary header flag
104  bool hasSecHdr = (header.get_packetIdentification() & SpacePacketSubfields::SecHdrMask) != 0;
105  contextCopy.set_hasSecHdr(hasSecHdr);
106 
107  // Validate with the ApidManager that the sequence count is correct
108  U16 receivedSequenceCount = header.get_packetSequenceControl() & SpacePacketSubfields::SeqCountMask;
109  (void)this->validateApidSeqCount_out(0, apid, receivedSequenceCount);
110  contextCopy.set_sequenceCount(receivedSequenceCount);
111 
112  // Set data buffer to be of the encapsulated data: HEADER (6 bytes) | PACKET DATA
114  data.setSize(pkt_length);
115 
116  this->dataOut_out(0, data, contextCopy);
117 }
118 
119 void SpacePacketDeframer ::dataReturnIn_handler(FwIndexType portNum,
120  Fw::Buffer& data,
121  const ComCfg::FrameContext& context) {
122  this->dataReturnOut_out(0, data, context);
123 }
124 
125 } // namespace Ccsds
126 } // namespace Svc
Serialization/Deserialization operation was successful.
void errorNotify_out(FwIndexType portNum, const Svc::Ccsds::FrameError &errorCode) const
Invoke output port errorNotify.
void setData(U8 *data)
Definition: Buffer.cpp:68
Fully Featured CCSDS Space Packet Protocol.
Definition: PvnEnumAc.hpp:33
PlatformSizeType FwSizeType
void setSize(FwSizeType size)
Definition: Buffer.cpp:75
void set_hasSecHdr(bool hasSecHdr)
Set member hasSecHdr.
void set_apid(ComCfg::Apid::T apid)
Set member apid.
U8 * getData() const
Definition: Buffer.cpp:56
U16 validateApidSeqCount_out(FwIndexType portNum, const ComCfg::Apid &apid, U16 sequenceCount) const
Invoke output port validateApidSeqCount.
void dataReturnOut_out(FwIndexType portNum, Fw::Buffer &data, const ComCfg::FrameContext &context) const
Invoke output port dataReturnOut.
SerializeStatus
forward declaration for string
SpacePacketDeframer(const char *const compName)
Construct SpacePacketDeframer object.
ExternalSerializeBufferWithMemberCopy getDeserializer()
Definition: Buffer.cpp:105
T
The raw enum type.
Definition: ApidEnumAc.hpp:31
void set_sequenceCount(U16 sequenceCount)
Set member sequenceCount.
Describes the frame header format for the SpacePacket communications protocol.
U16 get_packetIdentification() const
Get member packetIdentification.
void dataOut_out(FwIndexType portNum, Fw::Buffer &data, const ComCfg::FrameContext &context) const
Invoke output port dataOut.
FwSizeType getSize() const
Definition: Buffer.cpp:60
~SpacePacketDeframer()
Destroy SpacePacketDeframer object.
PlatformIndexType FwIndexType
Type used to pass context info between components during framing/deframing.
RateGroupDivider component implementation.
Auto-generated base for SpacePacketDeframer component.
SerializeStatus deserializeTo(U8 &val, Endianness mode=Endianness::BIG) override
Deserialize an 8-bit unsigned integer value.
void log_WARNING_HI_InvalidLength(FwSizeType transmitted, FwSizeType actual) const