From 2f3db118fc8892aabe5e428ad5c2524a670e539f Mon Sep 17 00:00:00 2001 From: Silvano Seva Date: Sun, 29 Aug 2021 16:53:00 +0200 Subject: [PATCH] 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. --- openrtx/include/protocols/M17/M17Datatypes.h | 21 ++++++++++++------- .../src/protocols/M17/M17LinkSetupFrame.cpp | 12 +++++------ 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/openrtx/include/protocols/M17/M17Datatypes.h b/openrtx/include/protocols/M17/M17Datatypes.h index eed84916..683d5242 100644 --- a/openrtx/include/protocols/M17/M17Datatypes.h +++ b/openrtx/include/protocols/M17/M17Datatypes.h @@ -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; /** diff --git a/openrtx/src/protocols/M17/M17LinkSetupFrame.cpp b/openrtx/src/protocols/M17/M17LinkSetupFrame.cpp index cdad142a..1b6e84c9 100644 --- a/openrtx/src/protocols/M17/M17LinkSetupFrame.cpp +++ b/openrtx/src/protocols/M17/M17LinkSetupFrame.cpp @@ -23,6 +23,7 @@ #include #include + 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()