From 654b23869f607c6c89b78dc3bebdab8a2b7be291 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Tue, 17 Aug 2021 16:00:39 +0200 Subject: [PATCH] several imporovements --- src/fsfw/globalfunctions/DleEncoder.cpp | 24 ++++++++------ src/fsfw/globalfunctions/DleEncoder.h | 43 +++++++++++-------------- 2 files changed, 34 insertions(+), 33 deletions(-) diff --git a/src/fsfw/globalfunctions/DleEncoder.cpp b/src/fsfw/globalfunctions/DleEncoder.cpp index 81131910..6ad5e5fa 100644 --- a/src/fsfw/globalfunctions/DleEncoder.cpp +++ b/src/fsfw/globalfunctions/DleEncoder.cpp @@ -83,7 +83,7 @@ ReturnValue_t DleEncoder::encode(const uint8_t* sourceStream, ReturnValue_t DleEncoder::decode(const uint8_t *sourceStream, size_t sourceStreamLen, size_t *readLen, uint8_t *destStream, size_t maxDestStreamlen, size_t *decodedLen) { - size_t encodedIndex = 0, decodedIndex = 0; + size_t encodedIndex = 0; if(not escapeStxEtx) { if (*sourceStream != DLE_CHAR) { return DECODING_ERROR; @@ -94,21 +94,22 @@ ReturnValue_t DleEncoder::decode(const uint8_t *sourceStream, return DECODING_ERROR; } - ++encodedIndex; - if(escapeStxEtx) { - return decodeStreamEscaped(encodedIndex, decodedIndex, sourceStream, sourceStreamLen, + return decodeStreamEscaped(sourceStream, sourceStreamLen, readLen, destStream, maxDestStreamlen, decodedLen); } else { - return decodeStreamNonEscaped(encodedIndex, decodedIndex, sourceStream, sourceStreamLen, + return decodeStreamNonEscaped(sourceStream, sourceStreamLen, readLen, destStream, maxDestStreamlen, decodedLen); } } -ReturnValue_t DleEncoder::decodeStreamEscaped(size_t encodedIndex, size_t decodedIndex, - const uint8_t *sourceStream, size_t sourceStreamLen, size_t *readLen, uint8_t *destStream, +ReturnValue_t DleEncoder::decodeStreamEscaped(const uint8_t *sourceStream, size_t sourceStreamLen, + size_t *readLen, uint8_t *destStream, size_t maxDestStreamlen, size_t *decodedLen) { + // Skip start marker, was already checked + size_t encodedIndex = 1; + size_t decodedIndex = 0; uint8_t nextByte; while ((encodedIndex < sourceStreamLen) && (decodedIndex < maxDestStreamlen) && (sourceStream[encodedIndex] != ETX_CHAR) @@ -158,9 +159,12 @@ ReturnValue_t DleEncoder::decodeStreamEscaped(size_t encodedIndex, size_t decode } } -ReturnValue_t DleEncoder::decodeStreamNonEscaped(size_t encodedIndex, size_t decodedIndex, - const uint8_t *sourceStream, size_t sourceStreamLen, size_t *readLen, uint8_t *destStream, +ReturnValue_t DleEncoder::decodeStreamNonEscaped(const uint8_t *sourceStream, + size_t sourceStreamLen, size_t *readLen, uint8_t *destStream, size_t maxDestStreamlen, size_t *decodedLen) { + // Skip start marker, was already checked + size_t encodedIndex = 2; + size_t decodedIndex = 0; uint8_t nextByte; while ((encodedIndex < sourceStreamLen) && (decodedIndex < maxDestStreamlen)) { if (sourceStream[encodedIndex] == DLE_CHAR) { @@ -177,6 +181,8 @@ ReturnValue_t DleEncoder::decodeStreamNonEscaped(size_t encodedIndex, size_t dec } else if(nextByte == ETX_CHAR) { // End of stream reached + *readLen = encodedIndex + 2; + *decodedLen = decodedIndex; return RETURN_OK; } } diff --git a/src/fsfw/globalfunctions/DleEncoder.h b/src/fsfw/globalfunctions/DleEncoder.h index 47bb2a69..cc7dbc22 100644 --- a/src/fsfw/globalfunctions/DleEncoder.h +++ b/src/fsfw/globalfunctions/DleEncoder.h @@ -12,24 +12,29 @@ * https://en.wikipedia.org/wiki/C0_and_C1_control_codes * * This encoder can be used to achieve a basic transport layer when using - * char based transmission systems. - * 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 - * 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 - * by another char, so STX, ETX and CR should not appear anywhere in the actual - * encoded data stream. + * char based transmission systems. There are two implemented variants: * - * When using a strictly char based reception of packets encoded with DLE, + * 1. Escaped variant + * + * The encoded stream starts with a STX marker and ends with an ETX marker. + * STX and ETX occurrences in the stream are escaped and internally encoded as well so the + * receiver side can simply check for STX and ETX markers as frame delimiters. When using a + * strictly char based reception of packets encoded with DLE, * STX can be used to notify a reader that actual data will start to arrive * while ETX can be used to notify the reader that the data has ended. + * + * 2. Non-escaped variant + * + * The encoded stream starts with DLE STX and ends with DLE ETX. All DLE occurrences in the stream + * are escaped with DLE. If the received detects a DLE char, it needs to read the next char + * and to determine whether a start (STX) or end (ETX) of a frame has been detected. */ class DleEncoder: public HasReturnvaluesIF { public: /** * Create an encoder instance with the given configuration. - * @param escapeStxEtx - * @param escapeCr + * @param escapeStxEtx Determines whether the algorithm works in escaped or non-escaped mode + * @param escapeCr In escaped mode, escape all CR occurrences as well */ DleEncoder(bool escapeStxEtx = true, bool escapeCr = false); virtual ~DleEncoder(); @@ -62,10 +67,6 @@ public: * @param encodedLen * @param addStxEtx Adding STX start marker and ETX end marker can be omitted, * 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 */ ReturnValue_t encode(const uint8_t *sourceStream, size_t sourceLen, @@ -80,23 +81,17 @@ public: * @param destStream * @param maxDestStreamlen * @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 */ ReturnValue_t decode(const uint8_t *sourceStream, size_t sourceStreamLen, size_t *readLen, uint8_t *destStream, size_t maxDestStreamlen, size_t *decodedLen); - ReturnValue_t decodeStreamEscaped(size_t encodedIndex, size_t decodedIndex, - const uint8_t *sourceStream, size_t sourceStreamLen, size_t *readLen, - uint8_t *destStream, size_t maxDestStreamlen, size_t *decodedLen); + ReturnValue_t decodeStreamEscaped(const uint8_t *sourceStream, size_t sourceStreamLen, + size_t *readLen, uint8_t *destStream, size_t maxDestStreamlen, size_t *decodedLen); - ReturnValue_t decodeStreamNonEscaped(size_t encodedIndex, size_t decodedIndex, - const uint8_t *sourceStream, size_t sourceStreamLen, size_t *readLen, - uint8_t *destStream, size_t maxDestStreamlen, size_t *decodedLen); + ReturnValue_t decodeStreamNonEscaped(const uint8_t *sourceStream, size_t sourceStreamLen, + size_t *readLen, uint8_t *destStream, size_t maxDestStreamlen, size_t *decodedLen); private: