2016-06-15 23:48:41 +02:00
|
|
|
#ifndef ENDIANSWAPPER_H_
|
|
|
|
#define ENDIANSWAPPER_H_
|
|
|
|
|
2018-07-12 16:29:32 +02:00
|
|
|
#include <framework/osal/Endiness.h>
|
|
|
|
#include <cstring>
|
2018-07-13 18:28:26 +02:00
|
|
|
#include <iostream>
|
2016-06-15 23:48:41 +02:00
|
|
|
|
2019-12-08 22:57:03 +01:00
|
|
|
/**
|
|
|
|
* @brief Can be used to swap endianness of data
|
|
|
|
* into big endian
|
|
|
|
*/
|
2016-06-15 23:48:41 +02:00
|
|
|
class EndianSwapper {
|
|
|
|
private:
|
2020-04-13 16:27:05 +02:00
|
|
|
EndianSwapper() {};
|
2016-06-15 23:48:41 +02:00
|
|
|
public:
|
2020-04-15 20:53:03 +02:00
|
|
|
/**
|
|
|
|
* Swap the endianness of a variable with arbitrary type
|
|
|
|
* @tparam T Type of variable
|
|
|
|
* @param in variable
|
|
|
|
* @return Variable with swapped endianness
|
|
|
|
*/
|
2016-06-15 23:48:41 +02:00
|
|
|
template<typename T>
|
|
|
|
static T swap(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;
|
|
|
|
uint8_t *pointerOut = (uint8_t *) &tmp;
|
|
|
|
uint8_t *pointerIn = (uint8_t *) ∈
|
|
|
|
for (uint8_t count = 0; count < sizeof(T); count++) {
|
|
|
|
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-04-15 20:53:03 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Swap the endianness of a buffer.
|
|
|
|
* @param out
|
|
|
|
* @param in
|
|
|
|
* @param size
|
|
|
|
*/
|
|
|
|
static void swap(uint8_t* out, const uint8_t* in, 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
|
2016-06-15 23:48:41 +02:00
|
|
|
for (uint8_t count = 0; count < size; count++) {
|
|
|
|
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;
|
2019-12-09 12:27:14 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-01-17 21:11:39 +01:00
|
|
|
/**
|
|
|
|
* Swap endianness of buffer entries
|
2020-04-15 20:53:03 +02:00
|
|
|
* Template argument specifies buffer type. The number of entries
|
|
|
|
* (not the buffer size!) must be supplied
|
2020-01-17 21:11:39 +01:00
|
|
|
* @param out
|
|
|
|
* @param in
|
2020-04-15 20:53:03 +02:00
|
|
|
* @param size Number of buffer entries (not size of buffer in bytes!)
|
2020-01-17 21:11:39 +01:00
|
|
|
*/
|
2019-12-09 12:27:14 +01:00
|
|
|
template<typename T>
|
2020-04-15 20:53:03 +02:00
|
|
|
static void swap(T * out, const T * in, uint32_t entries) {
|
2019-12-09 12:27:14 +01:00
|
|
|
#ifndef BYTE_ORDER_SYSTEM
|
|
|
|
#error BYTE_ORDER_SYSTEM not defined
|
|
|
|
#elif BYTE_ORDER_SYSTEM == LITTLE_ENDIAN
|
2020-01-17 21:11:39 +01:00
|
|
|
const uint8_t * in_buffer = reinterpret_cast<const uint8_t *>(in);
|
|
|
|
uint8_t * out_buffer = reinterpret_cast<uint8_t *>(out);
|
2020-04-15 20:53:03 +02:00
|
|
|
for (uint8_t count = 0; count < entries; count++) {
|
2019-12-09 12:27:14 +01:00
|
|
|
for(uint8_t i = 0; i < sizeof(T);i++) {
|
2020-04-15 20:53:03 +02:00
|
|
|
out_buffer[sizeof(T)* (count + 1) - i - 1] =
|
|
|
|
in_buffer[count * sizeof(T) + i];
|
2019-12-09 12:27:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
#elif BYTE_ORDER_SYSTEM == BIG_ENDIAN
|
|
|
|
memcpy(out, in, size*sizeof(T));
|
|
|
|
return;
|
2016-06-15 23:48:41 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* ENDIANSWAPPER_H_ */
|