F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
CcsdsTcFrameDetector.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title CcsdsTcFrameDetector.hpp
3 // \author thomas-bc
4 // \brief hpp file for fprime frame detector definitions
5 // ======================================================================
6 
9 #include <cstdio>
14 #include "Utils/Hash/Hash.hpp"
15 
16 namespace Svc {
17 namespace FrameDetectors {
18 
20 
23  return Status::MORE_DATA_NEEDED;
24  }
25 
26  // ---------------- Frame Header ----------------
27  // Copy CircularBuffer data into linear buffer, for serialization into FrameHeader object
29  Fw::SerializeStatus status = data.peek(header_data, Ccsds::TCHeader::SERIALIZED_SIZE, 0);
30  if (status != Fw::FW_SERIALIZE_OK) {
31  return Status::NO_FRAME_DETECTED;
32  }
33  Fw::ExternalSerializeBuffer header_ser_buffer(header_data, Ccsds::TCHeader::SERIALIZED_SIZE);
34  status = header_ser_buffer.setBuffLen(Ccsds::TCHeader::SERIALIZED_SIZE);
35  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
36  // Attempt to deserialize data into the FrameHeader object
37  Ccsds::TCHeader header;
38  status = header.deserialize(header_ser_buffer);
39  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, status);
40 
41  if (header.getflagsAndScId() != this->m_expectedFlagsAndScIdToken) {
42  // If the flags and SC ID do not match the expected token, we don't have a valid frame
43  return Status::NO_FRAME_DETECTED;
44  }
45  // TC protocol defines the Frame Length as number of bytes minus 1, so we add 1 back to get length in bytes
46  const FwSizeType expected_frame_length = static_cast<FwSizeType>((header.getvcIdAndLength() & Ccsds::TCSubfields::FrameLengthMask) + 1);
47  const U16 data_to_crc_length = static_cast<U16>(expected_frame_length - Ccsds::TCTrailer::SERIALIZED_SIZE);
48 
49  if (data.get_allocated_size() < expected_frame_length) {
50  size_out = expected_frame_length;
51  return Status::MORE_DATA_NEEDED;
52  }
53 
54  // ---------------- Frame Trailer ----------------
55  // Compute CRC on the received data
57  for (FwSizeType i = 0; i < data_to_crc_length; ++i) {
58  U8 byte = 0;
59  status = data.peek(byte, i);
60  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, status);
61  crc.update(byte);
62  }
63  U16 computed_fecf = crc.finalize();
64  // Retrieve CRC field from the trailer
66  status = data.peek(trailer_data, Ccsds::TCTrailer::SERIALIZED_SIZE, data_to_crc_length);
67  if (status != Fw::FW_SERIALIZE_OK) {
68  return Status::NO_FRAME_DETECTED;
69  }
70  Fw::ExternalSerializeBuffer trailer_ser_buffer(trailer_data, Ccsds::TCTrailer::SERIALIZED_SIZE);
71  status = trailer_ser_buffer.setBuffLen(Ccsds::TCTrailer::SERIALIZED_SIZE);
72  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
73  // Attempt to deserialize data into the FrameTrailer object
74  Ccsds::TCTrailer trailer;
75  status = trailer.deserialize(trailer_ser_buffer);
76  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, status);
77  U16 transmitted_fecf = trailer.getfecf();
78  if (transmitted_fecf != computed_fecf) {
79  // If the computed CRC does not match the transmitted CRC, we don't have a valid frame
80  return Status::NO_FRAME_DETECTED;
81  }
82  // At this point, we have validated the header and CRC - we report a valid frame detected
83  size_out = expected_frame_length;
84  return Status::FRAME_DETECTED;
85 }
86 
87 
88 } // namespace FrameDetectors
89 } // namespace Svc
Serialization/Deserialization operation was successful.
Fw::SerializeStatus deserialize(Fw::SerializeBufferBase &buffer)
Deserialization.
PlatformSizeType FwSizeType
CRC16 CCITT implementation.
Definition: CRC16.hpp:18
U16 getvcIdAndLength() const
Get member vcIdAndLength.
U16 finalize()
finalize and return CRC value
Definition: CRC16.hpp:31
SerializeStatus
forward declaration for string
Describes the frame trailer format for a Telecommand (TC) Transfer Frame.
Status detect(const Types::CircularBuffer &data, FwSizeType &size_out) const override
detect if a frame is available within the circular buffer
Status
status returned from the detection step
const U16 m_expectedFlagsAndScIdToken
expected flags and spacecraft ID token for a valid CCSDS TC frame
External serialize buffer with no copy semantics.
Describes the frame header format for a Telecommand (TC) Transfer Frame header.
The size of the serial representation.
Fw::SerializeStatus deserialize(Fw::SerializeBufferBase &buffer)
Deserialization.
U16 getflagsAndScId() const
Get member flagsAndScId.
FwSizeType get_allocated_size() const
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:56
U16 getfecf() const
Get member fecf.
Fw::SerializeStatus peek(char &value, FwSizeType offset=0) const
void update(U8 new_byte)
update CRC with one new byte
Definition: CRC16.hpp:28
RateGroupDivider component implementation.
The size of the serial representation.
#define FW_ASSERT(...)
Definition: Assert.hpp:14
SerializeStatus setBuffLen(Serializable::SizeType length)
sets buffer length manually after filling with data