STX and ETX escaping are optional now
This commit is contained in:
parent
80263b647d
commit
20ee0ed2c4
@ -6,7 +6,7 @@ DleEncoder::~DleEncoder() {}
|
||||
|
||||
ReturnValue_t DleEncoder::encode(const uint8_t* sourceStream,
|
||||
size_t sourceLen, uint8_t* destStream, size_t maxDestLen,
|
||||
size_t* encodedLen, bool addStxEtx) {
|
||||
size_t* encodedLen, bool addStxEtx, bool escapeStxEtx, bool escapeCr) {
|
||||
if (maxDestLen < 2) {
|
||||
return STREAM_TOO_SHORT;
|
||||
}
|
||||
@ -17,11 +17,12 @@ ReturnValue_t DleEncoder::encode(const uint8_t* sourceStream,
|
||||
++encodedIndex;
|
||||
}
|
||||
|
||||
while (encodedIndex < maxDestLen and sourceIndex < sourceLen)
|
||||
{
|
||||
while (encodedIndex < maxDestLen and sourceIndex < sourceLen) {
|
||||
nextByte = sourceStream[sourceIndex];
|
||||
// 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
|
||||
(escapeCr and nextByte == CARRIAGE_RETURN)) {
|
||||
if(escapeStxEtx) {
|
||||
if (encodedIndex + 1 >= maxDestLen) {
|
||||
return STREAM_TOO_SHORT;
|
||||
}
|
||||
@ -38,6 +39,7 @@ ReturnValue_t DleEncoder::encode(const uint8_t* sourceStream,
|
||||
destStream[encodedIndex] = nextByte + 0x40;
|
||||
}
|
||||
}
|
||||
}
|
||||
// DLE characters are simply escaped with DLE.
|
||||
else if (nextByte == DLE_CHAR) {
|
||||
if (encodedIndex + 1 >= maxDestLen) {
|
||||
@ -71,7 +73,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 maxDestStreamlen, size_t *decodedLen, bool escapeStxEtx, bool escapeCr) {
|
||||
size_t encodedIndex = 0, decodedIndex = 0;
|
||||
uint8_t nextByte;
|
||||
if (*sourceStream != STX_CHAR) {
|
||||
@ -90,17 +92,23 @@ ReturnValue_t DleEncoder::decode(const uint8_t *sourceStream,
|
||||
destStream[decodedIndex] = nextByte;
|
||||
}
|
||||
else {
|
||||
if(escapeStxEtx) {
|
||||
/* 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 prevent having control chars,
|
||||
* in the stream at all, so we convert it back. */
|
||||
if (nextByte == 0x42 or nextByte == 0x43 or nextByte == 0x4D) {
|
||||
if ((nextByte == STX_CHAR + 0x40 or nextByte == ETX_CHAR + 0x40) or
|
||||
(escapeCr and nextByte == CARRIAGE_RETURN + 0x40)) {
|
||||
destStream[decodedIndex] = nextByte - 0x40;
|
||||
}
|
||||
else {
|
||||
return DECODING_ERROR;
|
||||
}
|
||||
}
|
||||
else {
|
||||
return DECODING_ERROR;
|
||||
}
|
||||
}
|
||||
++encodedIndex;
|
||||
}
|
||||
else {
|
||||
|
@ -13,9 +13,9 @@
|
||||
*
|
||||
* This encoder can be used to achieve a basic transport layer when using
|
||||
* 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
|
||||
* 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
|
||||
* by another char, so STX, ETX and CR should not appear anywhere in the actual
|
||||
* encoded data stream.
|
||||
@ -45,21 +45,28 @@ public:
|
||||
|
||||
/**
|
||||
* 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 decoding process).
|
||||
* and ending it with an ETX marker. DLE characters inside
|
||||
* the stream are escaped by DLE characters. STX, ETX and CR characters can be escaped with a
|
||||
* 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 sourceLen
|
||||
* @param destStream
|
||||
* @param maxDestLen
|
||||
* @param encodedLen
|
||||
* @param addStxEtx
|
||||
* Adding STX and ETX can be omitted, if they are added manually.
|
||||
* @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
|
||||
*/
|
||||
static ReturnValue_t encode(const uint8_t *sourceStream, size_t sourceLen,
|
||||
uint8_t *destStream, size_t maxDestLen, size_t *encodedLen,
|
||||
bool addStxEtx = true);
|
||||
bool addStxEtx = true, bool escapeStxEtx = true, bool escapeCr = false);
|
||||
|
||||
/**
|
||||
* Converts an encoded stream back.
|
||||
@ -69,11 +76,16 @@ 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
|
||||
*/
|
||||
static ReturnValue_t decode(const uint8_t *sourceStream,
|
||||
size_t sourceStreamLen, size_t *readLen, uint8_t *destStream,
|
||||
size_t maxDestStreamlen, size_t *decodedLen);
|
||||
size_t maxDestStreamlen, size_t *decodedLen, bool escapeStxEtx = true,
|
||||
bool escapeCr = false);
|
||||
};
|
||||
|
||||
#endif /* FRAMEWORK_GLOBALFUNCTIONS_DLEENCODER_H_ */
|
||||
|
@ -1,5 +1,5 @@
|
||||
target_sources(${LIB_FSFW_NAME} PRIVATE
|
||||
testMq.cpp
|
||||
testMutex.cpp
|
||||
TestSemaphore.cpp
|
||||
testSemaphore.cpp
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user