Merge branch 'mueller/dle-improvements' into mueller/master

This commit is contained in:
Robin Müller 2021-08-17 15:06:54 +02:00
commit ae3c56a27e
No known key found for this signature in database
GPG Key ID: 11D4952C8CCEF814
2 changed files with 30 additions and 25 deletions

View File

@ -1,6 +1,7 @@
#include "fsfw/globalfunctions/DleEncoder.h"
DleEncoder::DleEncoder() {}
DleEncoder::DleEncoder(bool escapeStxEtx, bool escapeCr): escapeStxEtx(escapeStxEtx),
escapeCr(escapeCr) {}
DleEncoder::~DleEncoder() {}
@ -21,8 +22,8 @@ ReturnValue_t DleEncoder::encode(const uint8_t* sourceStream,
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
(escapeCr and nextByte == CARRIAGE_RETURN)) {
if(escapeStxEtx) {
(this->escapeCr and nextByte == CARRIAGE_RETURN)) {
if(this->escapeStxEtx) {
if (encodedIndex + 1 >= maxDestLen) {
return STREAM_TOO_SHORT;
}
@ -73,7 +74,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, bool escapeStxEtx, bool escapeCr) {
size_t maxDestStreamlen, size_t *decodedLen) {
size_t encodedIndex = 0, decodedIndex = 0;
uint8_t nextByte;
if (*sourceStream != STX_CHAR) {
@ -92,13 +93,13 @@ ReturnValue_t DleEncoder::decode(const uint8_t *sourceStream,
destStream[decodedIndex] = nextByte;
}
else {
if(escapeStxEtx) {
if(this->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 == STX_CHAR + 0x40 or nextByte == ETX_CHAR + 0x40) or
(escapeCr and nextByte == CARRIAGE_RETURN + 0x40)) {
(this->escapeCr and nextByte == CARRIAGE_RETURN + 0x40)) {
destStream[decodedIndex] = nextByte - 0x40;
}
else {

View File

@ -1,7 +1,7 @@
#ifndef FRAMEWORK_GLOBALFUNCTIONS_DLEENCODER_H_
#define FRAMEWORK_GLOBALFUNCTIONS_DLEENCODER_H_
#include "../returnvalues/HasReturnvaluesIF.h"
#include "fsfw/returnvalues/HasReturnvaluesIF.h"
#include <cstddef>
/**
@ -26,22 +26,22 @@
*/
class DleEncoder: public HasReturnvaluesIF {
private:
DleEncoder();
virtual ~DleEncoder();
DleEncoder(bool escapeStxEtx = true, bool escapeCr = false);
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);
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 occurrences
//! in the source stream.
static constexpr uint8_t DLE_CHAR = 0x10;
static constexpr uint8_t CARRIAGE_RETURN = 0x0D;
//! 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 occurrences
//! 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
@ -64,7 +64,7 @@ public:
* by adding 0x40
* @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,
bool addStxEtx = true, bool escapeStxEtx = true, bool escapeCr = false);
@ -82,10 +82,14 @@ public:
* 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, bool escapeStxEtx = true,
bool escapeCr = false);
ReturnValue_t decode(const uint8_t *sourceStream,
size_t sourceStreamLen, size_t *readLen, uint8_t *destStream,
size_t maxDestStreamlen, size_t *decodedLen);
private:
bool escapeStxEtx;
bool escapeCr;
};
#endif /* FRAMEWORK_GLOBALFUNCTIONS_DLEENCODER_H_ */