F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
TmFramer.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title TmFramer.cpp
3 // \author thomas-bc
4 // \brief cpp file for TmFramer component implementation class
5 // ======================================================================
6 
10 
11 namespace Svc {
12 
13 namespace CCSDS {
14 
15 // ----------------------------------------------------------------------
16 // Component construction and destruction
17 // ----------------------------------------------------------------------
18 
19 TmFramer ::TmFramer(const char* const compName)
20  : TmFramerComponentBase(compName), m_masterFrameCount(0), m_virtualFrameCount(0) {}
21 
23 
24 // ----------------------------------------------------------------------
25 // Handler implementations for typed input ports
26 // ----------------------------------------------------------------------
27 
28 void TmFramer ::dataIn_handler(FwIndexType portNum, Fw::Buffer& data, const ComCfg::FrameContext& context) {
30  static_cast<FwAssertArgType>(data.getSize()));
31  FW_ASSERT(this->m_bufferState == BufferOwnershipState::OWNED,
32  static_cast<FwAssertArgType>(this->m_bufferState));
33 
34  // -----------------------------------------------
35  // Header
36  // -----------------------------------------------
37  TMHeader header;
38 
39  // GVCID (Global Virtual Channel ID) (Standard 4.1.2.2 and 4.1.2.3)
40  U16 globalVcId = static_cast<U16>(context.getvcId() << TMSubfields::virtualChannelIdOffset);
41  globalVcId |= static_cast<U16>(ComCfg::SpacecraftId << TMSubfields::spacecraftIdOffset);
42  globalVcId |= 0x0; // Operational Control Field: Flag set to 0 (Standard 4.1.2.4)
43 
44  // Data Field Status (Standard 4.1.2.7):
45  // - all flags to 0 except segment length id 0b11 per standard (4.1.2.7)
46  // - First Header Pointer is always 0 since we are always wrapping a single entire packet at offset 0
47  U16 dataFieldStatus = 0;
48  dataFieldStatus |= 0x3 << TMSubfields::segLengthOffset; // Seg Length Id '11' (0x3) per Standard (4.1.2.7.5)
49 
50  header.setglobalVcId(globalVcId);
51  header.setmasterFrameCount(this->m_masterFrameCount);
52  header.setvirtualFrameCount(this->m_virtualFrameCount);
53  header.setdataFieldStatus(dataFieldStatus);
54 
55  // We use only a single Virtual Channel for now, so master and virtual frame counts are the same
56  this->m_masterFrameCount++; // U8 intended to wrap around (modulo 256)
57  this->m_virtualFrameCount++; // U8 intended to wrap around (modulo 256)
58 
59  // -------------------------------------------------
60  // Data field
61  // -------------------------------------------------
62  // Payload packet
63  Fw::SerializeStatus status;
64  // Create frame Fw::Buffer using member data field
65  Fw::Buffer frameBuffer = Fw::Buffer(this->m_frameBuffer, sizeof(this->m_frameBuffer));
66  auto frameSerializer = frameBuffer.getSerializer();
67  status = frameSerializer.serialize(header);
68  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, status);
69  status = frameSerializer.serialize(data.getData(), data.getSize(), Fw::Serialization::OMIT_LENGTH);
70  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, status);
71 
72  // As per TM Standard 4.2.2.5, fill the rest of the data field with an Idle Packet
73  this->fill_with_idle_packet(frameSerializer);
74 
75  // -------------------------------------------------
76  // Trailer (CRC)
77  // -------------------------------------------------
78  TMTrailer trailer;
79  // Compute CRC over the entire frame buffer minus the FECF trailer (Standard 4.1.6)
80  U16 crc =
81  CCSDS::Utils::CRC16::compute(frameBuffer.getData(), sizeof(this->m_frameBuffer) - TMTrailer::SERIALIZED_SIZE);
82  // Set the Frame Error Control Field (FECF)
83  trailer.setfecf(crc);
84  // Move the serializer pointer to the end of the location where the trailer will be serialized
85  frameSerializer.moveSerToOffset(ComCfg::TmFrameFixedSize - TMTrailer::SERIALIZED_SIZE);
86  status = frameSerializer.serialize(trailer);
87  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, status);
88 
89  this->m_bufferState = BufferOwnershipState::NOT_OWNED;
90  this->dataOut_out(0, frameBuffer, context);
91  this->dataReturnOut_out(0, data, context); // return ownership of the original data buffer
92 }
93 
94 void TmFramer ::comStatusIn_handler(FwIndexType portNum, Fw::Success& condition) {
95  if (this->isConnected_comStatusOut_OutputPort(portNum)) {
96  this->comStatusOut_out(portNum, condition);
97  }
98 }
99 
100 void TmFramer ::dataReturnIn_handler(FwIndexType portNum,
101  Fw::Buffer& frameBuffer,
102  const ComCfg::FrameContext& context) {
103  // Assert that the returned buffer is the member, and set ownership state
104  FW_ASSERT(frameBuffer.getData() >= &this->m_frameBuffer[0]);
105  FW_ASSERT(frameBuffer.getData() < &this->m_frameBuffer[0] + sizeof(this->m_frameBuffer));
106  this->m_bufferState = BufferOwnershipState::OWNED;
107 }
108 
109 void TmFramer ::fill_with_idle_packet(Fw::SerializeBufferBase& serializer) {
110  constexpr U16 endIndex = ComCfg::TmFrameFixedSize - TMTrailer::SERIALIZED_SIZE;
111  constexpr U16 idleApid = static_cast<U16>(ComCfg::APID::SPP_IDLE_PACKET);
112  const U16 startIndex = static_cast<U16>(serializer.getBuffLength());
113  const U16 idlePacketSize = static_cast<U16>(endIndex - startIndex);
114  // Length token is defined as the number of bytes of payload data minus 1
115  const U16 lengthToken = static_cast<U16>(idlePacketSize - SpacePacketHeader::SERIALIZED_SIZE - 1);
116 
117  FW_ASSERT(idlePacketSize >= 7, static_cast<FwAssertArgType>(idlePacketSize)); // 7 bytes minimum for idle packet
118  FW_ASSERT(idlePacketSize <= ComCfg::TmFrameFixedSize, static_cast<FwAssertArgType>(idlePacketSize));
119 
120  SpacePacketHeader header;
121  header.setpacketIdentification(idleApid);
122  header.setpacketSequenceControl(
123  0x3 << SpacePacketSubfields::SeqFlagsOffset); // Sequence Flags = 0b11 (unsegmented) & unused Seq count
124  header.setpacketDataLength(lengthToken); // this should be payload length - 1 ???
125  // Serialize header and idle data into the frame
126  serializer.serialize(header);
127  for (U16 i = static_cast<U16>(startIndex + SpacePacketHeader::SERIALIZED_SIZE); i < endIndex; i++) {
128  serializer.serialize(IDLE_DATA_PATTERN); // Idle data
129  }
130 }
131 } // namespace CCSDS
132 } // namespace Svc
Serialization/Deserialization operation was successful.
static U16 compute(const U8 *buffer, U32 length)
compute CRC16 for a buffer
Definition: CRC16.hpp:43
void setmasterFrameCount(U8 masterFrameCount)
Set member masterFrameCount.
void dataOut_out(FwIndexType portNum, Fw::Buffer &data, const ComCfg::FrameContext &context)
Invoke output port dataOut.
SerializeStatus serialize(U8 val)
serialize 8-bit unsigned int
bool isConnected_comStatusOut_OutputPort(FwIndexType portNum)
U8 * getData() const
Definition: Buffer.cpp:68
Describes the frame trailer format for a Telemetry (TM) Transfer Frame.
The size of the serial representation.
SerializeStatus
forward declaration for string
~TmFramer()
Destroy TmFramer object.
Definition: TmFramer.cpp:22
Serializable::SizeType getBuffLength() const
returns current buffer size
void setglobalVcId(U16 globalVcId)
Set member globalVcId.
The size of the serial representation.
void setdataFieldStatus(U16 dataFieldStatus)
Set member dataFieldStatus.
Per Space Packet Standard, all 1s (11bits) is reserved for Idle Packets.
Definition: APIDEnumAc.hpp:51
void comStatusOut_out(FwIndexType portNum, Fw::Success &condition)
Invoke output port comStatusOut.
U8 getvcId() const
Get member vcId.
Omit length from serialization.
PlatformIndexType FwIndexType
Type used to pass context info between components during framing/deframing.
RateGroupDivider component implementation.
SizeType getSize() const
Definition: Buffer.cpp:72
Auto-generated base for TmFramer component.
void dataReturnOut_out(FwIndexType portNum, Fw::Buffer &data, const ComCfg::FrameContext &context)
Invoke output port dataReturnOut.
TmFramer(const char *const compName)
Construct TmFramer object.
Definition: TmFramer.cpp:19
ExternalSerializeBufferWithMemberCopy getSerializer()
Definition: Buffer.cpp:107
#define FW_ASSERT(...)
Definition: Assert.hpp:14
Success/Failure.
void setvirtualFrameCount(U8 virtualFrameCount)
Set member virtualFrameCount.
void setfecf(U16 fecf)
Set member fecf.
PlatformAssertArgType FwAssertArgType
The type of arguments to assert functions.
Describes the frame header format for a Telemetry (TM) Transfer Frame header.