improved DLE encoder

This commit is contained in:
Robin Müller 2021-08-17 15:05:29 +02:00
parent d92a796705
commit 4b72e246c3
No known key found for this signature in database
GPG Key ID: 11D4952C8CCEF814
2 changed files with 79 additions and 54 deletions

View File

@ -1,6 +1,7 @@
#include "fsfw/globalfunctions/DleEncoder.h" #include "fsfw/globalfunctions/DleEncoder.h"
DleEncoder::DleEncoder() {} DleEncoder::DleEncoder(bool escapeStxEtx, bool escapeCr): escapeStxEtx(escapeStxEtx),
escapeCr(escapeCr) {}
DleEncoder::~DleEncoder() {} DleEncoder::~DleEncoder() {}
@ -17,26 +18,28 @@ ReturnValue_t DleEncoder::encode(const uint8_t* sourceStream,
++encodedIndex; ++encodedIndex;
} }
while (encodedIndex < maxDestLen and sourceIndex < sourceLen) while (encodedIndex < maxDestLen and sourceIndex < sourceLen) {
{
nextByte = sourceStream[sourceIndex]; nextByte = sourceStream[sourceIndex];
// STX, ETX and CR characters in the stream need to be escaped with DLE // STX, ETX and CR characters in the stream need to be escaped with DLE
if (nextByte == STX_CHAR or nextByte == ETX_CHAR or nextByte == CARRIAGE_RETURN) { if ((nextByte == STX_CHAR or nextByte == ETX_CHAR) or
if (encodedIndex + 1 >= maxDestLen) { (this->escapeCr and nextByte == CARRIAGE_RETURN)) {
return STREAM_TOO_SHORT; if(this->escapeStxEtx) {
} if (encodedIndex + 1 >= maxDestLen) {
else { return STREAM_TOO_SHORT;
destStream[encodedIndex] = DLE_CHAR; }
++encodedIndex; else {
/* Escaped byte will be actual byte + 0x40. This prevents destStream[encodedIndex] = DLE_CHAR;
* STX, ETX, and carriage return characters from appearing ++encodedIndex;
* in the encoded data stream at all, so when polling an /* Escaped byte will be actual byte + 0x40. This prevents
* encoded stream, the transmission can be stopped at ETX. * STX, ETX, and carriage return characters from appearing
* 0x40 was chosen at random with special requirements: * in the encoded data stream at all, so when polling an
* - Prevent going from one control char to another * encoded stream, the transmission can be stopped at ETX.
* - Prevent overflow for common characters */ * 0x40 was chosen at random with special requirements:
destStream[encodedIndex] = nextByte + 0x40; * - Prevent going from one control char to another
} * - Prevent overflow for common characters */
destStream[encodedIndex] = nextByte + 0x40;
}
}
} }
// DLE characters are simply escaped with DLE. // DLE characters are simply escaped with DLE.
else if (nextByte == DLE_CHAR) { else if (nextByte == DLE_CHAR) {
@ -90,16 +93,22 @@ ReturnValue_t DleEncoder::decode(const uint8_t *sourceStream,
destStream[decodedIndex] = nextByte; destStream[decodedIndex] = nextByte;
} }
else { else {
/* The next byte is a STX, DTX or 0x0D character which if(this->escapeStxEtx) {
* was escaped by a DLE character. The actual byte was /* The next byte is a STX, DTX or 0x0D character which
* also encoded by adding + 0x40 to prevent having control chars, * was escaped by a DLE character. The actual byte was
* in the stream at all, so we convert it back. */ * also encoded by adding + 0x40 to prevent having control chars,
if (nextByte == 0x42 or nextByte == 0x43 or nextByte == 0x4D) { * in the stream at all, so we convert it back. */
destStream[decodedIndex] = nextByte - 0x40; if ((nextByte == STX_CHAR + 0x40 or nextByte == ETX_CHAR + 0x40) or
} (this->escapeCr and nextByte == CARRIAGE_RETURN + 0x40)) {
else { destStream[decodedIndex] = nextByte - 0x40;
return DECODING_ERROR; }
} else {
return DECODING_ERROR;
}
}
else {
return DECODING_ERROR;
}
} }
++encodedIndex; ++encodedIndex;
} }

View File

@ -1,7 +1,7 @@
#ifndef FRAMEWORK_GLOBALFUNCTIONS_DLEENCODER_H_ #ifndef FRAMEWORK_GLOBALFUNCTIONS_DLEENCODER_H_
#define FRAMEWORK_GLOBALFUNCTIONS_DLEENCODER_H_ #define FRAMEWORK_GLOBALFUNCTIONS_DLEENCODER_H_
#include "../returnvalues/HasReturnvaluesIF.h" #include "fsfw/returnvalues/HasReturnvaluesIF.h"
#include <cstddef> #include <cstddef>
/** /**
@ -13,9 +13,9 @@
* *
* This encoder can be used to achieve a basic transport layer when using * This encoder can be used to achieve a basic transport layer when using
* char based transmission systems. * char based transmission systems.
* The passed source strean is converted into a encoded stream by adding * The passed source stream is converted into a encoded stream by adding
* a STX marker at the start of the stream and an ETX marker at the end of * a STX marker at the start of the stream and an ETX marker at the end of
* the stream. Any STX, ETX, DLE and CR occurrences in the source stream are * the stream. Any STX, ETX, DLE and CR occurrences in the source stream can be
* escaped by a DLE character. The encoder also replaces escaped control chars * escaped by a DLE character. The encoder also replaces escaped control chars
* by another char, so STX, ETX and CR should not appear anywhere in the actual * by another char, so STX, ETX and CR should not appear anywhere in the actual
* encoded data stream. * encoded data stream.
@ -26,38 +26,45 @@
*/ */
class DleEncoder: public HasReturnvaluesIF { class DleEncoder: public HasReturnvaluesIF {
private: private:
DleEncoder(); DleEncoder(bool escapeStxEtx = true, bool escapeCr = false);
virtual ~DleEncoder(); virtual ~DleEncoder();
public: public:
static constexpr uint8_t INTERFACE_ID = CLASS_ID::DLE_ENCODER; static constexpr uint8_t INTERFACE_ID = CLASS_ID::DLE_ENCODER;
static constexpr ReturnValue_t STREAM_TOO_SHORT = MAKE_RETURN_CODE(0x01); static constexpr ReturnValue_t STREAM_TOO_SHORT = MAKE_RETURN_CODE(0x01);
static constexpr ReturnValue_t DECODING_ERROR = MAKE_RETURN_CODE(0x02); static constexpr ReturnValue_t DECODING_ERROR = MAKE_RETURN_CODE(0x02);
//! Start Of Text character. First character is encoded stream //! Start Of Text character. First character is encoded stream
static constexpr uint8_t STX_CHAR = 0x02; static constexpr uint8_t STX_CHAR = 0x02;
//! End Of Text character. Last character in encoded stream //! End Of Text character. Last character in encoded stream
static constexpr uint8_t ETX_CHAR = 0x03; static constexpr uint8_t ETX_CHAR = 0x03;
//! Data Link Escape character. Used to escape STX, ETX and DLE occurrences //! Data Link Escape character. Used to escape STX, ETX and DLE occurrences
//! in the source stream. //! in the source stream.
static constexpr uint8_t DLE_CHAR = 0x10; static constexpr uint8_t DLE_CHAR = 0x10;
static constexpr uint8_t CARRIAGE_RETURN = 0x0D; static constexpr uint8_t CARRIAGE_RETURN = 0x0D;
/** /**
* Encodes the give data stream by preceding it with the STX marker * Encodes the give data stream by preceding it with the STX marker
* and ending it with an ETX marker. STX, ETX and DLE characters inside * and ending it with an ETX marker. DLE characters inside
* the stream are escaped by DLE characters and also replaced by adding * the stream are escaped by DLE characters. STX, ETX and CR characters can be escaped with a
* 0x40 (which is reverted in the decoding process). * DLE character as well. The escaped characters are also encoded by adding
* 0x40 (which is reverted in the decoding process). This is performed so the source stream
* does not have STX/ETX/CR occurrences anymore, so the receiving side can simply parse for
* start and end markers
* @param sourceStream * @param sourceStream
* @param sourceLen * @param sourceLen
* @param destStream * @param destStream
* @param maxDestLen * @param maxDestLen
* @param encodedLen * @param encodedLen
* @param addStxEtx * @param addStxEtx Adding STX start marker and ETX end marker can be omitted,
* Adding STX and ETX can be omitted, if they are added manually. * if they are added manually
* @param escapeStxEtx STX and ETX occurrences in the given source stream will be escaped and
* encoded by adding 0x40
* @param escapeCr CR characters in the given source stream will be escaped and encoded
* by adding 0x40
* @return * @return
*/ */
static ReturnValue_t encode(const uint8_t *sourceStream, size_t sourceLen, ReturnValue_t encode(const uint8_t *sourceStream, size_t sourceLen,
uint8_t *destStream, size_t maxDestLen, size_t *encodedLen, uint8_t *destStream, size_t maxDestLen, size_t *encodedLen,
bool addStxEtx = true); bool addStxEtx = true);
@ -69,11 +76,20 @@ public:
* @param destStream * @param destStream
* @param maxDestStreamlen * @param maxDestStreamlen
* @param decodedLen * @param decodedLen
* @param escapeStxEtx STX and ETX characters were escaped in the encoded stream and need to
* be decoded back as well
* @param escapeCr CR characters were escaped in the encoded stream and need to
* be decoded back as well by subtracting 0x40
* @return * @return
*/ */
static ReturnValue_t decode(const uint8_t *sourceStream, ReturnValue_t decode(const uint8_t *sourceStream,
size_t sourceStreamLen, size_t *readLen, uint8_t *destStream, size_t sourceStreamLen, size_t *readLen, uint8_t *destStream,
size_t maxDestStreamlen, size_t *decodedLen); size_t maxDestStreamlen, size_t *decodedLen);
private:
bool escapeStxEtx;
bool escapeCr;
}; };
#endif /* FRAMEWORK_GLOBALFUNCTIONS_DLEENCODER_H_ */ #endif /* FRAMEWORK_GLOBALFUNCTIONS_DLEENCODER_H_ */