Changed streamType_t from bitfield struct to union of anonymous bitfield struct and uint16_t to facilitate endianness conversion. Change also fixed a bug in endianness conversion inside M17LinkSetupFrame class due to unaligned memory access.

replace/2c1de5f86270b0a8ae02615d21feb15da895866b
Silvano Seva 2021-08-29 16:53:00 +02:00
rodzic 065c15fb72
commit 2f3db118fc
2 zmienionych plików z 19 dodań i 14 usunięć

Wyświetl plik

@ -39,16 +39,21 @@ using lich_t = std::array< uint8_t, 12 >; // Data type for Golay(24,12) enc
* This structure provides bit field definitions for the "TYPE" field
* contained in an M17 Link Setup Frame.
*/
typedef struct
typedef union
{
uint16_t stream : 1; //< Packet/stream indicator: 0 = packet, 1 = stream
uint16_t dataType : 2; //< Data type indicator
uint16_t encType : 2; //< Encryption type
uint16_t encSubType : 2; //< Encryption subtype
uint16_t CAN : 4; //< Channel Access Number
uint16_t : 4; //< Reserved, padding to 16 bit
struct __attribute__((packed))
{
uint16_t stream : 1; //< Packet/stream indicator: 0 = packet, 1 = stream
uint16_t dataType : 2; //< Data type indicator
uint16_t encType : 2; //< Encryption type
uint16_t encSubType : 2; //< Encryption subtype
uint16_t CAN : 4; //< Channel Access Number
uint16_t : 4; //< Reserved, padding to 16 bit
};
uint16_t value;
}
__attribute__((packed)) streamType_t;
streamType_t;
/**

Wyświetl plik

@ -23,6 +23,7 @@
#include <M17/M17Callsign.h>
#include <M17/M17LinkSetupFrame.h>
M17LinkSetupFrame::M17LinkSetupFrame()
{
clear();
@ -52,17 +53,16 @@ void M17LinkSetupFrame::setDestination(const std::string& callsign)
streamType_t M17LinkSetupFrame::getType()
{
// NOTE: M17 fields are big-endian, we need to swap bytes
uint16_t *a = reinterpret_cast< uint16_t* >(&data.type);
uint16_t b = __builtin_bswap16(*a);
return *reinterpret_cast< streamType_t* >(&b);
streamType_t type = data.type;
type.value = __builtin_bswap16(type.value);
return type;
}
void M17LinkSetupFrame::setType(streamType_t type)
{
// NOTE: M17 fields are big-endian, we need to swap bytes
uint16_t *a = reinterpret_cast< uint16_t* >(&type);
uint16_t b = __builtin_bswap16(*a);
data.type = *reinterpret_cast< streamType_t* >(&b);
type.value = __builtin_bswap16(type.value);
data.type = type;
}
meta_t& M17LinkSetupFrame::metadata()