added doc to dle encoder
This commit is contained in:
parent
9465c8f2b2
commit
2748a8b93b
@ -1,95 +1,124 @@
|
|||||||
#include "DleEncoder.h"
|
#include "../globalfunctions/DleEncoder.h"
|
||||||
|
|
||||||
DleEncoder::DleEncoder() {
|
DleEncoder::DleEncoder() {}
|
||||||
}
|
|
||||||
|
DleEncoder::~DleEncoder() {}
|
||||||
DleEncoder::~DleEncoder() {
|
|
||||||
}
|
ReturnValue_t DleEncoder::encode(const uint8_t* sourceStream,
|
||||||
|
size_t sourceLen, uint8_t* destStream, size_t maxDestLen,
|
||||||
ReturnValue_t DleEncoder::decode(const uint8_t *sourceStream,
|
size_t* encodedLen, bool addStxEtx) {
|
||||||
uint32_t sourceStreamLen, uint32_t *readLen, uint8_t *destStream,
|
if (maxDestLen < 2) {
|
||||||
uint32_t maxDestStreamlen, uint32_t *decodedLen) {
|
return STREAM_TOO_SHORT;
|
||||||
uint32_t encodedIndex = 0, decodedIndex = 0;
|
}
|
||||||
uint8_t nextByte;
|
size_t encodedIndex = 0, sourceIndex = 0;
|
||||||
if (*sourceStream != STX) {
|
uint8_t nextByte;
|
||||||
return RETURN_FAILED;
|
if (addStxEtx) {
|
||||||
}
|
destStream[0] = STX_CHAR;
|
||||||
++encodedIndex;
|
++encodedIndex;
|
||||||
while ((encodedIndex < sourceStreamLen) && (decodedIndex < maxDestStreamlen)
|
}
|
||||||
&& (sourceStream[encodedIndex] != ETX)
|
|
||||||
&& (sourceStream[encodedIndex] != STX)) {
|
while (encodedIndex < maxDestLen and sourceIndex < sourceLen)
|
||||||
if (sourceStream[encodedIndex] == DLE) {
|
{
|
||||||
nextByte = sourceStream[encodedIndex + 1];
|
nextByte = sourceStream[sourceIndex];
|
||||||
if (nextByte == 0x10) {
|
// STX, ETX and CR characters in the stream need to be escaped with DLE
|
||||||
destStream[decodedIndex] = nextByte;
|
if (nextByte == STX_CHAR or nextByte == ETX_CHAR or nextByte == CARRIAGE_RETURN) {
|
||||||
} else {
|
if (encodedIndex + 1 >= maxDestLen) {
|
||||||
if ((nextByte == 0x42) || (nextByte == 0x43)
|
return STREAM_TOO_SHORT;
|
||||||
|| (nextByte == 0x4D)) {
|
}
|
||||||
destStream[decodedIndex] = nextByte - 0x40;
|
else {
|
||||||
} else {
|
destStream[encodedIndex] = DLE_CHAR;
|
||||||
return RETURN_FAILED;
|
++encodedIndex;
|
||||||
}
|
/* Escaped byte will be actual byte + 0x40. This prevents
|
||||||
}
|
* STX, ETX, and carriage return characters from appearing
|
||||||
++encodedIndex;
|
* in the encoded data stream at all, so when polling an
|
||||||
} else {
|
* encoded stream, the transmission can be stopped at ETX.
|
||||||
destStream[decodedIndex] = sourceStream[encodedIndex];
|
* 0x40 was chosen at random with special requirements:
|
||||||
}
|
* - Prevent going from one control char to another
|
||||||
++encodedIndex;
|
* - Prevent overflow for common characters */
|
||||||
++decodedIndex;
|
destStream[encodedIndex] = nextByte + 0x40;
|
||||||
}
|
}
|
||||||
if (sourceStream[encodedIndex] != ETX) {
|
}
|
||||||
return RETURN_FAILED;
|
// DLE characters are simply escaped with DLE.
|
||||||
} else {
|
else if (nextByte == DLE_CHAR) {
|
||||||
*readLen = ++encodedIndex;
|
if (encodedIndex + 1 >= maxDestLen) {
|
||||||
*decodedLen = decodedIndex;
|
return STREAM_TOO_SHORT;
|
||||||
return RETURN_OK;
|
}
|
||||||
}
|
else {
|
||||||
}
|
destStream[encodedIndex] = DLE_CHAR;
|
||||||
|
++encodedIndex;
|
||||||
ReturnValue_t DleEncoder::encode(const uint8_t* sourceStream,
|
destStream[encodedIndex] = DLE_CHAR;
|
||||||
uint32_t sourceLen, uint8_t* destStream, uint32_t maxDestLen,
|
}
|
||||||
uint32_t* encodedLen, bool addStxEtx) {
|
}
|
||||||
if (maxDestLen < 2) {
|
else {
|
||||||
return RETURN_FAILED;
|
destStream[encodedIndex] = nextByte;
|
||||||
}
|
}
|
||||||
uint32_t encodedIndex = 0, sourceIndex = 0;
|
++encodedIndex;
|
||||||
uint8_t nextByte;
|
++sourceIndex;
|
||||||
if (addStxEtx) {
|
}
|
||||||
destStream[0] = STX;
|
|
||||||
++encodedIndex;
|
if (sourceIndex == sourceLen and encodedIndex < maxDestLen) {
|
||||||
}
|
if (addStxEtx) {
|
||||||
while ((encodedIndex < maxDestLen) && (sourceIndex < sourceLen)) {
|
destStream[encodedIndex] = ETX_CHAR;
|
||||||
nextByte = sourceStream[sourceIndex];
|
++encodedIndex;
|
||||||
if ((nextByte == STX) || (nextByte == ETX) || (nextByte == 0x0D)) {
|
}
|
||||||
if (encodedIndex + 1 >= maxDestLen) {
|
*encodedLen = encodedIndex;
|
||||||
return RETURN_FAILED;
|
return RETURN_OK;
|
||||||
} else {
|
}
|
||||||
destStream[encodedIndex] = DLE;
|
else {
|
||||||
++encodedIndex;
|
return STREAM_TOO_SHORT;
|
||||||
destStream[encodedIndex] = nextByte + 0x40;
|
}
|
||||||
}
|
}
|
||||||
} else if (nextByte == DLE) {
|
|
||||||
if (encodedIndex + 1 >= maxDestLen) {
|
ReturnValue_t DleEncoder::decode(const uint8_t *sourceStream,
|
||||||
return RETURN_FAILED;
|
size_t sourceStreamLen, size_t *readLen, uint8_t *destStream,
|
||||||
} else {
|
size_t maxDestStreamlen, size_t *decodedLen) {
|
||||||
destStream[encodedIndex] = DLE;
|
size_t encodedIndex = 0, decodedIndex = 0;
|
||||||
++encodedIndex;
|
uint8_t nextByte;
|
||||||
destStream[encodedIndex] = DLE;
|
if (*sourceStream != STX_CHAR) {
|
||||||
}
|
return DECODING_ERROR;
|
||||||
} else {
|
}
|
||||||
destStream[encodedIndex] = nextByte;
|
++encodedIndex;
|
||||||
}
|
|
||||||
++encodedIndex;
|
while ((encodedIndex < sourceStreamLen) && (decodedIndex < maxDestStreamlen)
|
||||||
++sourceIndex;
|
&& (sourceStream[encodedIndex] != ETX_CHAR)
|
||||||
}
|
&& (sourceStream[encodedIndex] != STX_CHAR)) {
|
||||||
if ((sourceIndex == sourceLen) && (encodedIndex < maxDestLen)) {
|
if (sourceStream[encodedIndex] == DLE_CHAR) {
|
||||||
if (addStxEtx) {
|
nextByte = sourceStream[encodedIndex + 1];
|
||||||
destStream[encodedIndex] = ETX;
|
// The next byte is a DLE character that was escaped by another
|
||||||
++encodedIndex;
|
// DLE character, so we can write it to the destination stream.
|
||||||
}
|
if (nextByte == DLE_CHAR) {
|
||||||
*encodedLen = encodedIndex;
|
destStream[decodedIndex] = nextByte;
|
||||||
return RETURN_OK;
|
}
|
||||||
} else {
|
else {
|
||||||
return RETURN_FAILED;
|
/* The next byte is a STX, DTX or 0x0D character which
|
||||||
}
|
* was escaped by a DLE character. The actual byte was
|
||||||
}
|
* also encoded by adding + 0x40 to preven having control chars,
|
||||||
|
* in the stream at all, so we convert it back. */
|
||||||
|
if (nextByte == 0x42 or nextByte == 0x43 or nextByte == 0x4D) {
|
||||||
|
destStream[decodedIndex] = nextByte - 0x40;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return DECODING_ERROR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
++encodedIndex;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
destStream[decodedIndex] = sourceStream[encodedIndex];
|
||||||
|
}
|
||||||
|
|
||||||
|
++encodedIndex;
|
||||||
|
++decodedIndex;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sourceStream[encodedIndex] != ETX_CHAR) {
|
||||||
|
*readLen = ++encodedIndex;
|
||||||
|
return DECODING_ERROR;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
*readLen = ++encodedIndex;
|
||||||
|
*decodedLen = decodedIndex;
|
||||||
|
return RETURN_OK;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -1,25 +1,79 @@
|
|||||||
#ifndef DLEENCODER_H_
|
#ifndef FRAMEWORK_GLOBALFUNCTIONS_DLEENCODER_H_
|
||||||
#define DLEENCODER_H_
|
#define FRAMEWORK_GLOBALFUNCTIONS_DLEENCODER_H_
|
||||||
|
|
||||||
#include "../returnvalues/HasReturnvaluesIF.h"
|
#include "../returnvalues/HasReturnvaluesIF.h"
|
||||||
|
#include <cstddef>
|
||||||
class DleEncoder: public HasReturnvaluesIF {
|
|
||||||
private:
|
/**
|
||||||
DleEncoder();
|
* @brief This DLE Encoder (Data Link Encoder) can be used to encode and
|
||||||
virtual ~DleEncoder();
|
* decode arbitrary data with ASCII control characters
|
||||||
|
* @details
|
||||||
public:
|
* List of control codes:
|
||||||
static const uint8_t STX = 0x02;
|
* https://en.wikipedia.org/wiki/C0_and_C1_control_codes
|
||||||
static const uint8_t ETX = 0x03;
|
*
|
||||||
static const uint8_t DLE = 0x10;
|
* This encoder can be used to achieve a basic transport layer when using
|
||||||
|
* char based transmission systems.
|
||||||
static ReturnValue_t decode(const uint8_t *sourceStream,
|
* The passed source strean is converted into a encoded stream by adding
|
||||||
uint32_t sourceStreamLen, uint32_t *readLen, uint8_t *destStream,
|
* a STX marker at the start of the stream and an ETX marker at the end of
|
||||||
uint32_t maxDestStreamlen, uint32_t *decodedLen);
|
* the stream. Any STX, ETX, DLE and CR occurences in the source stream are
|
||||||
|
* escaped by a DLE character. The encoder also replaces escaped control chars
|
||||||
static ReturnValue_t encode(const uint8_t *sourceStream, uint32_t sourceLen,
|
* by another char, so STX, ETX and CR should not appear anywhere in the actual
|
||||||
uint8_t *destStream, uint32_t maxDestLen, uint32_t *encodedLen,
|
* encoded data stream.
|
||||||
bool addStxEtx = true);
|
*
|
||||||
};
|
* When using a strictly char based reception of packets enoded with DLE,
|
||||||
|
* STX can be used to notify a reader that actual data will start to arrive
|
||||||
#endif /* DLEENCODER_H_ */
|
* while ETX can be used to notify the reader that the data has ended.
|
||||||
|
*/
|
||||||
|
class DleEncoder: public HasReturnvaluesIF {
|
||||||
|
private:
|
||||||
|
DleEncoder();
|
||||||
|
virtual ~DleEncoder();
|
||||||
|
|
||||||
|
public:
|
||||||
|
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 DECODING_ERROR = MAKE_RETURN_CODE(0x02);
|
||||||
|
|
||||||
|
//! Start Of Text character. First character is encoded stream
|
||||||
|
static constexpr uint8_t STX_CHAR = 0x02;
|
||||||
|
//! End Of Text character. Last character in encoded stream
|
||||||
|
static constexpr uint8_t ETX_CHAR = 0x03;
|
||||||
|
//! Data Link Escape character. Used to escape STX, ETX and DLE occurences
|
||||||
|
//! in the source stream.
|
||||||
|
static constexpr uint8_t DLE_CHAR = 0x10;
|
||||||
|
static constexpr uint8_t CARRIAGE_RETURN = 0x0D;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
||||||
|
* the stream are escaped by DLE characters and also replaced by adding
|
||||||
|
* 0x40 (which is reverted in the decoing process).
|
||||||
|
* @param sourceStream
|
||||||
|
* @param sourceLen
|
||||||
|
* @param destStream
|
||||||
|
* @param maxDestLen
|
||||||
|
* @param encodedLen
|
||||||
|
* @param addStxEtx
|
||||||
|
* Adding STX and ETX can be omitted, if they are added manually.
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
static ReturnValue_t encode(const uint8_t *sourceStream, size_t sourceLen,
|
||||||
|
uint8_t *destStream, size_t maxDestLen, size_t *encodedLen,
|
||||||
|
bool addStxEtx = true);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts an encoded stream back.
|
||||||
|
* @param sourceStream
|
||||||
|
* @param sourceStreamLen
|
||||||
|
* @param readLen
|
||||||
|
* @param destStream
|
||||||
|
* @param maxDestStreamlen
|
||||||
|
* @param decodedLen
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
static ReturnValue_t decode(const uint8_t *sourceStream,
|
||||||
|
size_t sourceStreamLen, size_t *readLen, uint8_t *destStream,
|
||||||
|
size_t maxDestStreamlen, size_t *decodedLen);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif /* FRAMEWORK_GLOBALFUNCTIONS_DLEENCODER_H_ */
|
||||||
|
Loading…
Reference in New Issue
Block a user