Skip to content

Commit

Permalink
FIX Cannot Find Start word for CCSDS packets as we lacked token masking
Browse files Browse the repository at this point in the history
  • Loading branch information
Reg Marr committed Nov 13, 2024
1 parent 8aed5fa commit 3b9a1a6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
11 changes: 8 additions & 3 deletions Svc/FrameAccumulator/FrameDetector/CCSDSFrameDetector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,16 @@ static_assert(CCSDS_SCID <= (std::numeric_limits<U16>::max() & TEN_BIT_MASK), "S
//! - 1 bit of control command flag "0"
//! - 2 bits of reserved "00"
//! - 10 bits of configurable SCID
using CCSDSStartWord = StartToken<U16, static_cast<U16>(0 | (CCSDS_SCID & TEN_BIT_MASK))>;
using CCSDSStartWord = StartToken<U16, static_cast<U16>(0 | (CCSDS_SCID & TEN_BIT_MASK)), TEN_BIT_MASK>;
//! CCSDS length is the last 10 bits of the 3rd and 4th octet
using CCSDSLength = LengthToken<U16, sizeof(U16), CCSDS_SDLTP_TC_MAX_FRAME_LENGTH, TEN_BIT_MASK>;
//! CCSDS checksum is a 16bit CRC with data starting at the 6th octet and the crc following directly
using CCSDSChecksum = CRC<U16, 5, 0, CRC16_CCITT>;
//! CCSDS checksum is a 16bit CRC which is calculated from the first bit in the transfer frame up to (but not including)
//! the Frame Error Control field (aka the crc U16 itself) as per CCSDS 232.0-B-4 4.1.4.2
//! When we perform the calculation during detection we do so by passing the length (found using the Length token)
//! The Frame Length is set as one fewer than the total octets in the Transfer Frame as per CCSDS 232.0-B-4 4.1.4.2
//! Therefore if we're calculating a CRC check on a frame which itself includes a CRC check we need to offset by
//! another 1 fewer than what the frame length describes.
using CCSDSChecksum = CRC<U16, 0, -1, CRC16_CCITT>;

//! CCSDS frame detector is a start/length/crc detector using the configured fprime tokens
using CCSDSFrameDetector = StartLengthCrcDetector<CCSDSStartWord, CCSDSLength, CCSDSChecksum>;
Expand Down
22 changes: 14 additions & 8 deletions Svc/FrameAccumulator/FrameDetector/StartLengthCrcDetector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ class StartToken : public Token<TokenType, TokenMask, TokenEndianness> {
}
return status;
}
TokenType getExpectedStart() {
return StartExpected & TokenMask;
}
};

//! \breif token representing data length
Expand Down Expand Up @@ -225,9 +228,10 @@ class CRC32 : public CRCWrapper<U32> {
//! \tparam CRCFn: function for updating CRC with new byte
template <typename TokenType,
FwSizeType DataOffset,
FwSizeType RelativeTokenOffset = 0,
class CRCHandler = CRC32>
class CRC : public Token<TokenType, std::numeric_limits<TokenType>::max(), BIG> {
FwNativeIntType RelativeTokenOffset = 0,
class CRCHandler = CRC32,
TokenType TokenMask = std::numeric_limits<TokenType>::max()>
class CRC : public Token<TokenType, TokenMask, BIG> {
public:
// Check CRC token and CRC handler match
static_assert(std::is_base_of<CRCWrapper<TokenType>, CRCHandler>::value, "Invalid CRC wrapper supplied");
Expand All @@ -246,12 +250,12 @@ class CRC : public Token<TokenType, std::numeric_limits<TokenType>::max(), BIG>
//! \return: FRAME_DETECTED, or MORE_DATA_NEEDED.
FrameDetector::Status calculate(const Types::CircularBuffer& data, FwSizeType length, FwSizeType& size_out) {
const FwSizeType checksum_length = DataOffset + length + RelativeTokenOffset;
size_out = checksum_length;
CRCHandler crc;
// Loop byte by byte
for (FwSizeType i = 0; i < checksum_length; i++) {
for (size_out = 0; size_out < checksum_length; size_out++) {
U8 byte = 0;
Fw::SerializeStatus status = data.peek(byte, i);
Fw::SerializeStatus status = data.peek(byte, size_out);

if (status != Fw::SerializeStatus::FW_SERIALIZE_OK) {
this->m_stored_offset = 0;
return FrameDetector::Status::MORE_DATA_NEEDED;
Expand All @@ -276,13 +280,15 @@ class CRC : public Token<TokenType, std::numeric_limits<TokenType>::max(), BIG>
FW_ASSERT(this->m_stored_offset != 0); // Must have called calculate before calling read
size_out = this->m_stored_offset;
FrameDetector::Status status =
this->Token<TokenType, std::numeric_limits<TokenType>::max(), BIG>::read(data, size_out, size_out);
this->Token<TokenType, TokenMask, BIG>::read(data, size_out, size_out);
if ((status == FrameDetector::FRAME_DETECTED) && (this->m_value != this->m_expected)) {
status = FrameDetector::NO_FRAME_DETECTED;
}
return status;
}

TokenType getExpected() {
return m_expected;
}
protected:
FwSizeType m_stored_offset;
TokenType m_expected;
Expand Down

0 comments on commit 3b9a1a6

Please sign in to comment.