2020-09-04 15:40:42 +02:00
|
|
|
#ifndef FSFW_SERIALIZE_ENDIANCONVERTER_H_
|
|
|
|
#define FSFW_SERIALIZE_ENDIANCONVERTER_H_
|
2016-06-15 23:48:41 +02:00
|
|
|
|
2020-08-13 20:53:35 +02:00
|
|
|
#include "../osal/Endiness.h"
|
2018-07-12 16:29:32 +02:00
|
|
|
#include <cstring>
|
2021-01-03 16:42:29 +01:00
|
|
|
#include <cstdint>
|
2016-06-15 23:48:41 +02:00
|
|
|
|
2020-07-03 15:46:00 +02:00
|
|
|
/**
|
|
|
|
* Helper class to convert variables or bitstreams between machine
|
|
|
|
* endian and either big or little endian.
|
|
|
|
* Machine endian is the endianness used by the machine running the
|
|
|
|
* program and is one of big or little endian. As this is portable
|
|
|
|
* code, it is not known at coding time which it is. At compile time
|
|
|
|
* it is however, which is why this is implemented using compiler
|
|
|
|
* macros and translates to a copy operation at runtime.
|
|
|
|
*
|
|
|
|
* This changes the layout of multi-byte variables in the machine's
|
|
|
|
* memory. In most cases, you should not need to use this class.
|
|
|
|
* Probably what you are looking for is the SerializeAdapter.
|
|
|
|
* If you still decide you need this class, please read and understand
|
|
|
|
* the code first.
|
|
|
|
*
|
|
|
|
* The order of the individual bytes of the multi-byte variable is
|
|
|
|
* reversed, the byte at the highest address is moved to the lowest
|
|
|
|
* address and vice versa, same for the bytes in between.
|
|
|
|
*
|
|
|
|
* Note that the conversion is also its inversion, that is converting
|
|
|
|
* from machine to a specified endianness is the same operation as
|
|
|
|
* converting from specified to machine (I looked it up, mathematicians
|
|
|
|
* would call it an involution):
|
|
|
|
*
|
|
|
|
* X == convertBigEndian(convertBigEndian(X))
|
|
|
|
*
|
|
|
|
* Thus, there is only one function supplied to do the conversion.
|
|
|
|
*/
|
2020-04-21 21:34:03 +02:00
|
|
|
class EndianConverter {
|
2016-06-15 23:48:41 +02:00
|
|
|
private:
|
2020-09-04 15:40:42 +02:00
|
|
|
EndianConverter() {};
|
2016-06-15 23:48:41 +02:00
|
|
|
public:
|
2020-07-03 15:46:00 +02:00
|
|
|
/**
|
|
|
|
* Convert a typed variable between big endian and machine endian.
|
|
|
|
* Intended for plain old datatypes.
|
|
|
|
*/
|
2016-06-15 23:48:41 +02:00
|
|
|
template<typename T>
|
2020-04-21 21:34:03 +02:00
|
|
|
static T convertBigEndian(T in) {
|
2018-07-13 18:28:26 +02:00
|
|
|
#ifndef BYTE_ORDER_SYSTEM
|
|
|
|
#error BYTE_ORDER_SYSTEM not defined
|
|
|
|
#elif BYTE_ORDER_SYSTEM == LITTLE_ENDIAN
|
2016-06-15 23:48:41 +02:00
|
|
|
T tmp;
|
2020-04-21 21:34:03 +02:00
|
|
|
uint8_t *pointerOut = (uint8_t*) &tmp;
|
|
|
|
uint8_t *pointerIn = (uint8_t*) ∈
|
2020-07-03 15:46:00 +02:00
|
|
|
for (size_t count = 0; count < sizeof(T); count++) {
|
2016-06-15 23:48:41 +02:00
|
|
|
pointerOut[sizeof(T) - count - 1] = pointerIn[count];
|
|
|
|
}
|
|
|
|
return tmp;
|
2018-07-13 18:28:26 +02:00
|
|
|
#elif BYTE_ORDER_SYSTEM == BIG_ENDIAN
|
2016-06-15 23:48:41 +02:00
|
|
|
return in;
|
2018-07-13 15:56:37 +02:00
|
|
|
#else
|
|
|
|
#error Unknown Byte Order
|
2016-06-15 23:48:41 +02:00
|
|
|
#endif
|
|
|
|
}
|
2020-07-03 15:46:00 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* convert a bytestream representing a single variable between big endian
|
|
|
|
* and machine endian.
|
|
|
|
*/
|
2020-04-21 21:34:03 +02:00
|
|
|
static void convertBigEndian(uint8_t *out, const uint8_t *in,
|
2020-07-03 15:46:00 +02:00
|
|
|
size_t size) {
|
2018-07-13 18:28:26 +02:00
|
|
|
#ifndef BYTE_ORDER_SYSTEM
|
|
|
|
#error BYTE_ORDER_SYSTEM not defined
|
|
|
|
#elif BYTE_ORDER_SYSTEM == LITTLE_ENDIAN
|
2020-07-03 15:46:00 +02:00
|
|
|
for (size_t count = 0; count < size; count++) {
|
2016-06-15 23:48:41 +02:00
|
|
|
out[size - count - 1] = in[count];
|
|
|
|
}
|
|
|
|
return;
|
2018-07-13 18:28:26 +02:00
|
|
|
#elif BYTE_ORDER_SYSTEM == BIG_ENDIAN
|
2016-06-15 23:48:41 +02:00
|
|
|
memcpy(out, in, size);
|
|
|
|
return;
|
2020-04-21 21:34:03 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-07-03 15:46:00 +02:00
|
|
|
/**
|
|
|
|
* Convert a typed variable between little endian and machine endian.
|
|
|
|
* Intended for plain old datatypes.
|
|
|
|
*/
|
2020-04-21 21:34:03 +02:00
|
|
|
template<typename T>
|
|
|
|
static T convertLittleEndian(T in) {
|
|
|
|
#ifndef BYTE_ORDER_SYSTEM
|
|
|
|
#error BYTE_ORDER_SYSTEM not defined
|
|
|
|
#elif BYTE_ORDER_SYSTEM == BIG_ENDIAN
|
|
|
|
T tmp;
|
|
|
|
uint8_t *pointerOut = (uint8_t *) &tmp;
|
|
|
|
uint8_t *pointerIn = (uint8_t *) ∈
|
2020-07-03 15:46:00 +02:00
|
|
|
for (size_t count = 0; count < sizeof(T); count++) {
|
2020-04-21 21:34:03 +02:00
|
|
|
pointerOut[sizeof(T) - count - 1] = pointerIn[count];
|
|
|
|
}
|
|
|
|
return tmp;
|
|
|
|
#elif BYTE_ORDER_SYSTEM == LITTLE_ENDIAN
|
|
|
|
return in;
|
|
|
|
#else
|
|
|
|
#error Unknown Byte Order
|
|
|
|
#endif
|
|
|
|
}
|
2020-07-03 15:46:00 +02:00
|
|
|
/**
|
|
|
|
* convert a bytestream representing a single variable between little endian
|
|
|
|
* and machine endian.
|
|
|
|
*/
|
2020-04-21 21:34:03 +02:00
|
|
|
static void convertLittleEndian(uint8_t *out, const uint8_t *in,
|
2020-07-03 15:46:00 +02:00
|
|
|
size_t size) {
|
2020-04-21 21:34:03 +02:00
|
|
|
#ifndef BYTE_ORDER_SYSTEM
|
|
|
|
#error BYTE_ORDER_SYSTEM not defined
|
|
|
|
#elif BYTE_ORDER_SYSTEM == BIG_ENDIAN
|
2020-07-03 15:46:00 +02:00
|
|
|
for (size_t count = 0; count < size; count++) {
|
2020-04-21 21:34:03 +02:00
|
|
|
out[size - count - 1] = in[count];
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
#elif BYTE_ORDER_SYSTEM == LITTLE_ENDIAN
|
|
|
|
memcpy(out, in, size);
|
|
|
|
return;
|
2016-06-15 23:48:41 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-09-04 15:40:42 +02:00
|
|
|
#endif /* FSFW_SERIALIZE_ENDIANCONVERTER_H_ */
|