diff --git a/src/common/Defines.h b/src/common/Defines.h index 854d6662..0eb05db1 100644 --- a/src/common/Defines.h +++ b/src/common/Defines.h @@ -169,158 +169,9 @@ const uint8_t IP_COMPRESS_RFC1144_COMPRESS = 0x01U; const uint8_t IP_COMPRESS_RFC1144_UNCOMPRESS = 0x02U; // --------------------------------------------------------------------------- -// Inlines +// Class Helper Macros // --------------------------------------------------------------------------- -/// -/// String from boolean. -/// -/// -/// -inline std::string __BOOL_STR(const bool& value) { - std::stringstream ss; - ss << std::boolalpha << value; - return ss.str(); -} - -/// -/// String from integer number. -/// -/// -/// -inline std::string __INT_STR(const int& value) { - std::stringstream ss; - ss << value; - return ss.str(); -} - -/// -/// String from hex integer number. -/// -/// -/// -inline std::string __INT_HEX_STR(const int& value) { - std::stringstream ss; - ss << std::hex << value; - return ss.str(); -} - -/// -/// String from floating point number. -/// -/// -/// -inline std::string __FLOAT_STR(const float& value) { - std::stringstream ss; - ss << value; - return ss.str(); -} - -/// -/// IP address from ulong64_t value. -/// -/// -/// -inline std::string __IP_FROM_ULONG(const ulong64_t& value) { - std::stringstream ss; - ss << ((value >> 24) & 0xFFU) << "." << ((value >> 16) & 0xFFU) << "." << ((value >> 8) & 0xFFU) << "." << (value & 0xFFU); - return ss.str(); -} - -/// -/// Helper to lower-case an input string. -/// -/// -/// -inline std::string strtolower(const std::string value) { - std::string v = value; - std::transform(v.begin(), v.end(), v.begin(), ::tolower); - return v; -} - -/// -/// Helper to upper-case an input string. -/// -/// -/// -inline std::string strtoupper(const std::string value) { - std::string v = value; - std::transform(v.begin(), v.end(), v.begin(), ::toupper); - return v; -} - -// --------------------------------------------------------------------------- -// Macros -// --------------------------------------------------------------------------- - -/// Pointer magic to get the memory address of a floating point number. -/// Floating Point Variable -#define __FLOAT_ADDR(x) (*(uint32_t*)& x) -/// Pointer magic to get the memory address of a double precision number. -/// Double Precision Variable -#define __DOUBLE_ADDR(x) (*(uint64_t*)& x) - -#define WRITE_BIT(p, i, b) p[(i) >> 3] = (b) ? (p[(i) >> 3] | BIT_MASK_TABLE[(i) & 7]) : (p[(i) >> 3] & ~BIT_MASK_TABLE[(i) & 7]) -#define READ_BIT(p, i) (p[(i) >> 3] & BIT_MASK_TABLE[(i) & 7]) - -/// Sets a uint32_t into 4 bytes. -/// uint32_t value to set -/// uint8_t buffer to set value on -/// Offset within uint8_t buffer -#define __SET_UINT32(val, buffer, offset) \ - buffer[0U + offset] = (val >> 24) & 0xFFU; \ - buffer[1U + offset] = (val >> 16) & 0xFFU; \ - buffer[2U + offset] = (val >> 8) & 0xFFU; \ - buffer[3U + offset] = (val >> 0) & 0xFFU; -/// Gets a uint32_t consisting of 4 bytes. -/// uint8_t buffer to get value from -/// Offset within uint8_t buffer -#define __GET_UINT32(buffer, offset) \ - (buffer[offset + 0U] << 24) | \ - (buffer[offset + 1U] << 16) | \ - (buffer[offset + 2U] << 8) | \ - (buffer[offset + 3U] << 0); -/// Sets a uint32_t into 3 bytes. -/// uint32_t value to set -/// uint8_t buffer to set value on -/// Offset within uint8_t buffer -#define __SET_UINT16(val, buffer, offset) \ - buffer[0U + offset] = (val >> 16) & 0xFFU; \ - buffer[1U + offset] = (val >> 8) & 0xFFU; \ - buffer[2U + offset] = (val >> 0) & 0xFFU; -/// Gets a uint32_t consisting of 3 bytes. (This is a shortened uint32_t). -/// uint8_t buffer to get value from -/// Offset within uint8_t buffer -#define __GET_UINT16(buffer, offset) \ - (buffer[offset + 0U] << 16) | \ - (buffer[offset + 1U] << 8) | \ - (buffer[offset + 2U] << 0); -/// Sets a uint16_t into 2 bytes. -/// uint16_t value to set -/// uint8_t buffer to set value on -/// Offset within uint8_t buffer -#define __SET_UINT16B(val, buffer, offset) \ - buffer[0U + offset] = (val >> 8) & 0xFFU; \ - buffer[1U + offset] = (val >> 0) & 0xFFU; -/// Gets a uint16_t consisting of 2 bytes. -/// uint8_t buffer to get value from -/// Offset within uint8_t buffer -#define __GET_UINT16B(buffer, offset) \ - ((buffer[offset + 0U] << 8) & 0xFF00U) | \ - ((buffer[offset + 1U] << 0) & 0x00FFU); - -#define new_unique(type, ...) std::unique_ptr(new type(__VA_ARGS__)) - -/// Creates a named unique buffer. -#define __UNIQUE_BUFFER(name, type, length) \ - std::unique_ptr name = std::unique_ptr(new type[length]); \ - ::memset(name.get(), 0x00U, length); - -typedef std::unique_ptr UInt8Array; - -/// Creates a named uint8_t array buffer. -#define __UNIQUE_UINT8_ARRAY(name, length) __UNIQUE_BUFFER(name, uint8_t, length) - /** * Class Copy Code Pattern */ diff --git a/src/common/ThreadFunc.h b/src/common/ThreadFunc.h index 2b2f8fc4..5f181d47 100644 --- a/src/common/ThreadFunc.h +++ b/src/common/ThreadFunc.h @@ -30,7 +30,6 @@ #if !defined(__THREAD_FUNC_H__) #define __THREAD_FUNC_H__ -#include "common/Defines.h" #include "common/Thread.h" #include diff --git a/src/common/Utils.cpp b/src/common/Utils.cpp index 346c71ba..d13cef4a 100644 --- a/src/common/Utils.cpp +++ b/src/common/Utils.cpp @@ -285,6 +285,37 @@ void Utils::bitsToByteLE(const bool* bits, uint8_t& byte) byte |= bits[7U] ? 0x80U : 0x00U; } +/// +/// +/// +/// +/// +uint16_t Utils::reverseEndian(uint16_t value) +{ + return (value << 8 & 0xff00) | (value >> 8); +} + +/// +/// +/// +/// +/// +uint32_t Utils::reverseEndian(uint32_t value) +{ + return (value << 24 | (value & 0xFF00U) << 8 | (value & 0xFF0000U) >> 8 | value >> 24); +} + +/// +/// +/// +/// +/// +uint64_t Utils::reverseEndian(uint64_t value) +{ + return (value << 56 | (value & 0xFF00U) << 40 | (value & 0xFF0000U) << 24 | (value & 0xFF000000U) << 8 | + (value & 0xFF00000000U) >> 8 | (value & 0xFF0000000000U) >> 24 | (value & 0xFF000000000000U) >> 40 | value >> 56); +} + /// /// /// @@ -292,6 +323,7 @@ void Utils::bitsToByteLE(const bool* bits, uint8_t& byte) /// /// /// +/// uint32_t Utils::getBits(const uint8_t* in, uint8_t* out, uint32_t start, uint32_t stop) { assert(in != nullptr); @@ -313,6 +345,7 @@ uint32_t Utils::getBits(const uint8_t* in, uint8_t* out, uint32_t start, uint32_ /// /// /// +/// uint32_t Utils::getBitRange(const uint8_t* in, uint8_t* out, uint32_t start, uint32_t length) { return getBits(in, out, start, start + length); @@ -325,6 +358,7 @@ uint32_t Utils::getBitRange(const uint8_t* in, uint8_t* out, uint32_t start, uin /// /// /// +/// uint32_t Utils::setBits(const uint8_t* in, uint8_t* out, uint32_t start, uint32_t stop) { assert(in != nullptr); @@ -346,6 +380,7 @@ uint32_t Utils::setBits(const uint8_t* in, uint8_t* out, uint32_t start, uint32_ /// /// /// +/// uint32_t Utils::setBitRange(const uint8_t* in, uint8_t* out, uint32_t start, uint32_t length) { return setBits(in, out, start, start + length); diff --git a/src/common/Utils.h b/src/common/Utils.h index e65c10ef..7689ee6a 100644 --- a/src/common/Utils.h +++ b/src/common/Utils.h @@ -30,6 +30,159 @@ #include +// --------------------------------------------------------------------------- +// Inlines +// --------------------------------------------------------------------------- + +/// +/// String from boolean. +/// +/// +/// +inline std::string __BOOL_STR(const bool& value) { + std::stringstream ss; + ss << std::boolalpha << value; + return ss.str(); +} + +/// +/// String from integer number. +/// +/// +/// +inline std::string __INT_STR(const int& value) { + std::stringstream ss; + ss << value; + return ss.str(); +} + +/// +/// String from hex integer number. +/// +/// +/// +inline std::string __INT_HEX_STR(const int& value) { + std::stringstream ss; + ss << std::hex << value; + return ss.str(); +} + +/// +/// String from floating point number. +/// +/// +/// +inline std::string __FLOAT_STR(const float& value) { + std::stringstream ss; + ss << value; + return ss.str(); +} + +/// +/// IP address from ulong64_t value. +/// +/// +/// +inline std::string __IP_FROM_ULONG(const ulong64_t& value) { + std::stringstream ss; + ss << ((value >> 24) & 0xFFU) << "." << ((value >> 16) & 0xFFU) << "." << ((value >> 8) & 0xFFU) << "." << (value & 0xFFU); + return ss.str(); +} + +/// +/// Helper to lower-case an input string. +/// +/// +/// +inline std::string strtolower(const std::string value) { + std::string v = value; + std::transform(v.begin(), v.end(), v.begin(), ::tolower); + return v; +} + +/// +/// Helper to upper-case an input string. +/// +/// +/// +inline std::string strtoupper(const std::string value) { + std::string v = value; + std::transform(v.begin(), v.end(), v.begin(), ::toupper); + return v; +} + +// --------------------------------------------------------------------------- +// Macros +// --------------------------------------------------------------------------- + +/// Pointer magic to get the memory address of a floating point number. +/// Floating Point Variable +#define __FLOAT_ADDR(x) (*(uint32_t*)& x) +/// Pointer magic to get the memory address of a double precision number. +/// Double Precision Variable +#define __DOUBLE_ADDR(x) (*(uint64_t*)& x) + +#define WRITE_BIT(p, i, b) p[(i) >> 3] = (b) ? (p[(i) >> 3] | BIT_MASK_TABLE[(i) & 7]) : (p[(i) >> 3] & ~BIT_MASK_TABLE[(i) & 7]) +#define READ_BIT(p, i) (p[(i) >> 3] & BIT_MASK_TABLE[(i) & 7]) + +/// Sets a uint32_t into 4 bytes. +/// uint32_t value to set +/// uint8_t buffer to set value on +/// Offset within uint8_t buffer +#define __SET_UINT32(val, buffer, offset) \ + buffer[0U + offset] = (val >> 24) & 0xFFU; \ + buffer[1U + offset] = (val >> 16) & 0xFFU; \ + buffer[2U + offset] = (val >> 8) & 0xFFU; \ + buffer[3U + offset] = (val >> 0) & 0xFFU; +/// Gets a uint32_t consisting of 4 bytes. +/// uint8_t buffer to get value from +/// Offset within uint8_t buffer +#define __GET_UINT32(buffer, offset) \ + (buffer[offset + 0U] << 24) | \ + (buffer[offset + 1U] << 16) | \ + (buffer[offset + 2U] << 8) | \ + (buffer[offset + 3U] << 0); +/// Sets a uint32_t into 3 bytes. +/// uint32_t value to set +/// uint8_t buffer to set value on +/// Offset within uint8_t buffer +#define __SET_UINT16(val, buffer, offset) \ + buffer[0U + offset] = (val >> 16) & 0xFFU; \ + buffer[1U + offset] = (val >> 8) & 0xFFU; \ + buffer[2U + offset] = (val >> 0) & 0xFFU; +/// Gets a uint32_t consisting of 3 bytes. (This is a shortened uint32_t). +/// uint8_t buffer to get value from +/// Offset within uint8_t buffer +#define __GET_UINT16(buffer, offset) \ + (buffer[offset + 0U] << 16) | \ + (buffer[offset + 1U] << 8) | \ + (buffer[offset + 2U] << 0); +/// Sets a uint16_t into 2 bytes. +/// uint16_t value to set +/// uint8_t buffer to set value on +/// Offset within uint8_t buffer +#define __SET_UINT16B(val, buffer, offset) \ + buffer[0U + offset] = (val >> 8) & 0xFFU; \ + buffer[1U + offset] = (val >> 0) & 0xFFU; +/// Gets a uint16_t consisting of 2 bytes. +/// uint8_t buffer to get value from +/// Offset within uint8_t buffer +#define __GET_UINT16B(buffer, offset) \ + ((buffer[offset + 0U] << 8) & 0xFF00U) | \ + ((buffer[offset + 1U] << 0) & 0x00FFU); + +#define new_unique(type, ...) std::unique_ptr(new type(__VA_ARGS__)) + +/// Creates a named unique buffer. +#define __UNIQUE_BUFFER(name, type, length) \ + std::unique_ptr name = std::unique_ptr(new type[length]); \ + ::memset(name.get(), 0x00U, length); + +typedef std::unique_ptr UInt8Array; + +/// Creates a named uint8_t array buffer. +#define __UNIQUE_UINT8_ARRAY(name, length) __UNIQUE_BUFFER(name, uint8_t, length) + // --------------------------------------------------------------------------- // Class Declaration // Implements various helper utilities. @@ -60,6 +213,13 @@ public: /// static void bitsToByteLE(const bool* bits, uint8_t& byte); + /// + static uint16_t reverseEndian(uint16_t value); + /// + static uint32_t reverseEndian(uint32_t value); + /// + static uint64_t reverseEndian(uint64_t value); + /// static uint32_t getBits(const uint8_t* in, uint8_t* out, uint32_t start, uint32_t stop); /// diff --git a/src/common/dmr/SlotType.cpp b/src/common/dmr/SlotType.cpp index 60ad8376..54ef03e3 100644 --- a/src/common/dmr/SlotType.cpp +++ b/src/common/dmr/SlotType.cpp @@ -27,7 +27,6 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ -#include "common/Defines.h" #include "common/dmr/SlotType.h" #include "common/edac/Golay2087.h" diff --git a/src/common/dmr/data/Data.h b/src/common/dmr/data/Data.h index e746e42c..77108a2e 100644 --- a/src/common/dmr/data/Data.h +++ b/src/common/dmr/data/Data.h @@ -26,7 +26,6 @@ #define __DMR_DATA__DATA_H__ #include "common/Defines.h" -#include "common/dmr/DMRDefines.h" namespace dmr { diff --git a/src/common/dmr/data/EmbeddedData.cpp b/src/common/dmr/data/EmbeddedData.cpp index dada46f2..c6a7e525 100644 --- a/src/common/dmr/data/EmbeddedData.cpp +++ b/src/common/dmr/data/EmbeddedData.cpp @@ -28,6 +28,7 @@ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include "Defines.h" +#include "dmr/DMRDefines.h" #include "dmr/data/EmbeddedData.h" #include "edac/Hamming.h" #include "edac/CRC.h" diff --git a/src/common/dmr/data/EmbeddedData.h b/src/common/dmr/data/EmbeddedData.h index db3c9dfd..d192e9af 100644 --- a/src/common/dmr/data/EmbeddedData.h +++ b/src/common/dmr/data/EmbeddedData.h @@ -31,7 +31,6 @@ #define __DMR_DATA__EMBEDDED_DATA_H__ #include "common/Defines.h" -#include "common/dmr/DMRDefines.h" #include "common/dmr/lc/LC.h" namespace dmr diff --git a/src/common/dmr/lc/CSBK.h b/src/common/dmr/lc/CSBK.h index 77c17b19..a3c05801 100644 --- a/src/common/dmr/lc/CSBK.h +++ b/src/common/dmr/lc/CSBK.h @@ -32,9 +32,9 @@ #define __DMR_LC__CSBK_H__ #include "common/Defines.h" -#include "common/dmr/DMRDefines.h" #include "common/dmr/SiteData.h" #include "common/lookups/IdenTableLookup.h" +#include "common/Utils.h" namespace dmr { diff --git a/src/common/dmr/lc/FullLC.h b/src/common/dmr/lc/FullLC.h index a4099703..48826f0d 100644 --- a/src/common/dmr/lc/FullLC.h +++ b/src/common/dmr/lc/FullLC.h @@ -34,7 +34,6 @@ #include "common/Defines.h" #include "common/dmr/lc/LC.h" #include "common/dmr/lc/PrivacyLC.h" -#include "common/dmr/SlotType.h" #include "common/edac/BPTC19696.h" namespace dmr diff --git a/src/common/dmr/lc/LC.cpp b/src/common/dmr/lc/LC.cpp index 9658a597..8bdfee2b 100644 --- a/src/common/dmr/lc/LC.cpp +++ b/src/common/dmr/lc/LC.cpp @@ -29,6 +29,7 @@ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include "Defines.h" +#include "dmr/DMRDefines.h" #include "dmr/lc/LC.h" #include "Utils.h" diff --git a/src/common/dmr/lc/LC.h b/src/common/dmr/lc/LC.h index 5f6a3f8e..c5745eb2 100644 --- a/src/common/dmr/lc/LC.h +++ b/src/common/dmr/lc/LC.h @@ -32,7 +32,6 @@ #define __DMR_LC__LC_H__ #include "common/Defines.h" -#include "common/dmr/DMRDefines.h" namespace dmr { diff --git a/src/common/dmr/lc/PrivacyLC.cpp b/src/common/dmr/lc/PrivacyLC.cpp index cf3a042b..6ca17489 100644 --- a/src/common/dmr/lc/PrivacyLC.cpp +++ b/src/common/dmr/lc/PrivacyLC.cpp @@ -24,6 +24,7 @@ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include "Defines.h" +#include "dmr/DMRDefines.h" #include "dmr/lc/PrivacyLC.h" #include "edac/CRC.h" #include "Utils.h" diff --git a/src/common/dmr/lc/PrivacyLC.h b/src/common/dmr/lc/PrivacyLC.h index 89d6a8c7..f9a492d0 100644 --- a/src/common/dmr/lc/PrivacyLC.h +++ b/src/common/dmr/lc/PrivacyLC.h @@ -28,7 +28,6 @@ #define __DMR_LC__PRIVACY_LC_H__ #include "common/Defines.h" -#include "common/dmr/DMRDefines.h" namespace dmr { diff --git a/src/common/edac/BCH.cpp b/src/common/edac/BCH.cpp index 0f1a013b..916de305 100644 --- a/src/common/edac/BCH.cpp +++ b/src/common/edac/BCH.cpp @@ -82,6 +82,7 @@ */ #include "Defines.h" #include "edac/BCH.h" +#include "Utils.h" using namespace edac; diff --git a/src/common/edac/Trellis.cpp b/src/common/edac/Trellis.cpp index 5f6f54fb..12d11662 100644 --- a/src/common/edac/Trellis.cpp +++ b/src/common/edac/Trellis.cpp @@ -26,6 +26,7 @@ #include "Defines.h" #include "edac/Trellis.h" #include "Log.h" +#include "Utils.h" using namespace edac; diff --git a/src/common/lookups/IdenTableLookup.h b/src/common/lookups/IdenTableLookup.h index 99fe95a6..13280fae 100644 --- a/src/common/lookups/IdenTableLookup.h +++ b/src/common/lookups/IdenTableLookup.h @@ -28,7 +28,6 @@ #include "common/Defines.h" #include "common/lookups/LookupTable.h" -#include "common/Thread.h" #include #include diff --git a/src/common/lookups/LookupTable.h b/src/common/lookups/LookupTable.h index e52ba9ef..e972feef 100644 --- a/src/common/lookups/LookupTable.h +++ b/src/common/lookups/LookupTable.h @@ -31,7 +31,6 @@ #endif #include "common/Defines.h" -#include "common/Log.h" #include "common/Thread.h" #include "common/Timer.h" diff --git a/src/common/lookups/RadioIdLookup.h b/src/common/lookups/RadioIdLookup.h index a032d5f8..131d22f6 100644 --- a/src/common/lookups/RadioIdLookup.h +++ b/src/common/lookups/RadioIdLookup.h @@ -33,7 +33,6 @@ #include "common/Defines.h" #include "common/lookups/LookupTable.h" -#include "common/Thread.h" #include #include diff --git a/src/common/lookups/TalkgroupRulesLookup.h b/src/common/lookups/TalkgroupRulesLookup.h index f6453abc..ed9fc72d 100644 --- a/src/common/lookups/TalkgroupRulesLookup.h +++ b/src/common/lookups/TalkgroupRulesLookup.h @@ -28,7 +28,6 @@ #include "common/Defines.h" #include "common/lookups/LookupTable.h" -#include "common/Thread.h" #include "common/yaml/Yaml.h" #include diff --git a/src/common/network/BaseNetwork.cpp b/src/common/network/BaseNetwork.cpp index 079a5510..d312f8ae 100644 --- a/src/common/network/BaseNetwork.cpp +++ b/src/common/network/BaseNetwork.cpp @@ -29,6 +29,9 @@ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include "Defines.h" +#include "common/dmr/DMRDefines.h" +#include "common/p25/dfsi/DFSIDefines.h" +#include "common/p25/dfsi/LC.h" #include "edac/SHA256.h" #include "network/BaseNetwork.h" #include "Log.h" diff --git a/src/common/network/BaseNetwork.h b/src/common/network/BaseNetwork.h index ec064c44..a8d616bf 100644 --- a/src/common/network/BaseNetwork.h +++ b/src/common/network/BaseNetwork.h @@ -32,23 +32,16 @@ #define __BASE_NETWORK_H__ #include "common/Defines.h" -#include "common/dmr/DMRDefines.h" -#include "common/p25/P25Defines.h" -#include "common/nxdn/NXDNDefines.h" #include "common/dmr/data/Data.h" #include "common/p25/data/DataHeader.h" #include "common/p25/data/LowSpeedData.h" -#include "common/p25/dfsi/DFSIDefines.h" -#include "common/p25/dfsi/LC.h" #include "common/p25/lc/LC.h" -#include "common/p25/lc/TSBK.h" -#include "common/p25/lc/TDULC.h" #include "common/p25/Audio.h" #include "common/nxdn/lc/RTCH.h" #include "common/network/FrameQueue.h" #include "common/network/UDPSocket.h" #include "common/RingBuffer.h" -#include "common/Timer.h" +#include "common/Utils.h" #include #include diff --git a/src/common/network/FrameQueue.cpp b/src/common/network/FrameQueue.cpp index a3113c53..1af0ddd8 100644 --- a/src/common/network/FrameQueue.cpp +++ b/src/common/network/FrameQueue.cpp @@ -27,6 +27,9 @@ #include "edac/CRC.h" #include "network/BaseNetwork.h" #include "network/FrameQueue.h" +#include "network/RTPHeader.h" +#include "network/RTPExtensionHeader.h" +#include "network/RTPFNEHeader.h" #include "Log.h" #include "Utils.h" @@ -47,11 +50,8 @@ using namespace network::frame; /// /// Local port used to listen for incoming data. /// Unique ID of this modem on the network. -FrameQueue::FrameQueue(UDPSocket* socket, uint32_t peerId, bool debug) : - m_peerId(peerId), - m_socket(socket), - m_buffers(), - m_debug(debug) +FrameQueue::FrameQueue(UDPSocket* socket, uint32_t peerId, bool debug) : RawFrameQueue(socket, debug), + m_peerId(peerId) { assert(peerId < 999999999U); } @@ -232,45 +232,3 @@ void FrameQueue::enqueueMessage(const uint8_t* message, uint32_t length, uint32_ m_buffers.push_back(dgram); } - -/// -/// Flush the message queue. -/// -/// -bool FrameQueue::flushQueue() -{ - if (m_buffers.empty()) { - return false; - } - - // bryanb: this is the same as above -- but for some assinine reason prevents - // weirdness - if (m_buffers.size() == 0U) { - return false; - } - - // LogDebug(LOG_NET, "m_buffers len = %u", m_buffers.size()); - - bool ret = true; - if (!m_socket->write(m_buffers)) { - LogError(LOG_NET, "Failed writing data to the network"); - ret = false; - } - - for (auto& buffer : m_buffers) { - if (buffer != nullptr) { - // LogDebug(LOG_NET, "deleting buffer, addr %p len %u", buffer->buffer, buffer->length); - if (buffer->buffer != nullptr) { - delete buffer->buffer; - buffer->length = 0; - buffer->buffer = nullptr; - } - - delete buffer; - buffer = nullptr; - } - } - m_buffers.clear(); - - return ret; -} diff --git a/src/common/network/FrameQueue.h b/src/common/network/FrameQueue.h index 4f89bee1..5b8209ba 100644 --- a/src/common/network/FrameQueue.h +++ b/src/common/network/FrameQueue.h @@ -27,28 +27,25 @@ #define __FRAME_QUEUE_H__ #include "common/Defines.h" -#include "common/network/UDPSocket.h" #include "common/network/RTPHeader.h" -#include "common/network/RTPExtensionHeader.h" #include "common/network/RTPFNEHeader.h" +#include "common/network/RawFrameQueue.h" namespace network { // --------------------------------------------------------------------------- // Constants // --------------------------------------------------------------------------- - - const uint32_t DATA_PACKET_LENGTH = 8192U; const uint8_t DVM_RTP_PAYLOAD_TYPE = 0x56U; const uint8_t DVM_CTRL_RTP_PAYLOAD_TYPE = 0x57U; // these are still RTP, but do not carry stream IDs or sequence data // --------------------------------------------------------------------------- // Class Declaration - // Implements the network frame queuing logic. + // Implements the network RTP frame queuing logic. // --------------------------------------------------------------------------- - class HOST_SW_API FrameQueue { + class HOST_SW_API FrameQueue : public RawFrameQueue { public: typedef std::pair OpcodePair; public: /// Initializes a new instance of the FrameQueue class. @@ -67,20 +64,9 @@ namespace network void enqueueMessage(const uint8_t* message, uint32_t length, uint32_t streamId, uint32_t peerId, uint32_t ssrc, OpcodePair opcode, uint16_t rtpSeq, sockaddr_storage& addr, uint32_t addrLen); - /// Flush the message queue. - bool flushQueue(); - private: uint32_t m_peerId; - - sockaddr_storage m_addr; - uint32_t m_addrLen; - UDPSocket* m_socket; - - BufferVector m_buffers; - - bool m_debug; }; } // namespace network -#endif // __FRAME_QUEUE_H__ \ No newline at end of file +#endif // __FRAME_QUEUE_H__ diff --git a/src/common/network/RTPFNEHeader.cpp b/src/common/network/RTPFNEHeader.cpp index 865c7b82..6ce5025e 100644 --- a/src/common/network/RTPFNEHeader.cpp +++ b/src/common/network/RTPFNEHeader.cpp @@ -25,6 +25,7 @@ */ #include "Defines.h" #include "network/RTPFNEHeader.h" +#include "Utils.h" using namespace network::frame; diff --git a/src/common/network/RTPHeader.cpp b/src/common/network/RTPHeader.cpp index 8720b702..0df754ff 100644 --- a/src/common/network/RTPHeader.cpp +++ b/src/common/network/RTPHeader.cpp @@ -26,6 +26,7 @@ #include "Defines.h" #include "network/RTPHeader.h" #include "Clock.h" +#include "Utils.h" using namespace system_clock; using namespace network::frame; diff --git a/src/common/network/RawFrameQueue.cpp b/src/common/network/RawFrameQueue.cpp new file mode 100644 index 00000000..45993292 --- /dev/null +++ b/src/common/network/RawFrameQueue.cpp @@ -0,0 +1,168 @@ +/** +* Digital Voice Modem - Common Library +* GPLv2 Open Source. Use is subject to license terms. +* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +* +* @package DVM / Common Library +* +*/ +/* +* Copyright (C) 2024 by Bryan Biedenkapp N2PLL +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ +#include "Defines.h" +#include "network/RawFrameQueue.h" +#include "network/UDPSocket.h" +#include "Log.h" +#include "Utils.h" + +using namespace network; + +#include +#include +#include +#include + +// --------------------------------------------------------------------------- +// Public Class Members +// --------------------------------------------------------------------------- + +/// +/// Initializes a new instance of the RawFrameQueue class. +/// +/// Local port used to listen for incoming data. +/// +RawFrameQueue::RawFrameQueue(UDPSocket* socket, bool debug) : + m_socket(socket), + m_buffers(), + m_debug(debug) +{ + /* stub */ +} + +/// +/// Finalizes a instance of the RawFrameQueue class. +/// +RawFrameQueue::~RawFrameQueue() +{ + /* stub */ +} + +/// +/// Read message from the received UDP packet. +/// +/// Actual length of message read from packet. +/// IP address data read from. +/// +/// Buffer containing message read. +UInt8Array RawFrameQueue::read(int& messageLength, sockaddr_storage& address, uint32_t& addrLen) +{ + messageLength = -1; + + // read message from socket + uint8_t buffer[DATA_PACKET_LENGTH]; + ::memset(buffer, 0x00U, DATA_PACKET_LENGTH); + int length = m_socket->read(buffer, DATA_PACKET_LENGTH, address, addrLen); + if (length < 0) { + LogError(LOG_NET, "Failed reading data from the network"); + return nullptr; + } + + if (length > 0) { + if (m_debug) + Utils::dump(1U, "Network Packet", buffer, length); + + // copy message + messageLength = length; + __UNIQUE_UINT8_ARRAY(message, length); + ::memcpy(message.get(), buffer, length); + + return message; + } + + return nullptr; +} + +/// +/// Cache "message" to frame queue. +/// +/// Message buffer to frame and queue. +/// Length of message. +/// IP address to write data to. +/// +/// +void RawFrameQueue::enqueueMessage(const uint8_t* message, uint32_t length, sockaddr_storage& addr, uint32_t addrLen) +{ + assert(message != nullptr); + assert(length > 0U); + + uint8_t* buffer = new uint8_t[length]; + ::memset(buffer, 0x00U, length); + ::memcpy(buffer, message, length); + + if (m_debug) + Utils::dump(1U, "RawFrameQueue::enqueueMessage() Buffered Message", buffer, length); + + UDPDatagram* dgram = new UDPDatagram; + dgram->buffer = buffer; + dgram->length = length; + dgram->address = addr; + dgram->addrLen = addrLen; + + m_buffers.push_back(dgram); +} + +/// +/// Flush the message queue. +/// +/// +bool RawFrameQueue::flushQueue() +{ + if (m_buffers.empty()) { + return false; + } + + // bryanb: this is the same as above -- but for some assinine reason prevents + // weirdness + if (m_buffers.size() == 0U) { + return false; + } + + // LogDebug(LOG_NET, "m_buffers len = %u", m_buffers.size()); + + bool ret = true; + if (!m_socket->write(m_buffers)) { + LogError(LOG_NET, "Failed writing data to the network"); + ret = false; + } + + for (auto& buffer : m_buffers) { + if (buffer != nullptr) { + // LogDebug(LOG_NET, "deleting buffer, addr %p len %u", buffer->buffer, buffer->length); + if (buffer->buffer != nullptr) { + delete buffer->buffer; + buffer->length = 0; + buffer->buffer = nullptr; + } + + delete buffer; + buffer = nullptr; + } + } + m_buffers.clear(); + + return ret; +} diff --git a/src/common/network/RawFrameQueue.h b/src/common/network/RawFrameQueue.h new file mode 100644 index 00000000..ad3dcf57 --- /dev/null +++ b/src/common/network/RawFrameQueue.h @@ -0,0 +1,73 @@ +/** +* Digital Voice Modem - Common Library +* GPLv2 Open Source. Use is subject to license terms. +* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +* +* @package DVM / Common Library +* +*/ +/* +* Copyright (C) 2024 by Bryan Biedenkapp N2PLL +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ +#if !defined(__RAW_FRAME_QUEUE_H__) +#define __RAW_FRAME_QUEUE_H__ + +#include "common/Defines.h" +#include "common/network/UDPSocket.h" +#include "common/Utils.h" + +namespace network +{ + // --------------------------------------------------------------------------- + // Constants + // --------------------------------------------------------------------------- + + const uint32_t DATA_PACKET_LENGTH = 8192U; + + // --------------------------------------------------------------------------- + // Class Declaration + // Implements the network frame queuing logic. + // --------------------------------------------------------------------------- + + class HOST_SW_API RawFrameQueue { + public: + /// Initializes a new instance of the RawFrameQueue class. + RawFrameQueue(UDPSocket* socket, bool debug); + /// Finalizes a instance of the RawFrameQueue class. + virtual ~RawFrameQueue(); + + /// Read message from the received UDP packet. + UInt8Array read(int& messageLength, sockaddr_storage& address, uint32_t& addrLen); + + /// Cache "message" to frame queue. + void enqueueMessage(const uint8_t* message, uint32_t length, sockaddr_storage& addr, uint32_t addrLen); + + /// Flush the message queue. + bool flushQueue(); + + protected: + sockaddr_storage m_addr; + uint32_t m_addrLen; + UDPSocket* m_socket; + + BufferVector m_buffers; + + bool m_debug; + }; +} // namespace network + +#endif // __FRAME_QUEUE_H__ \ No newline at end of file diff --git a/src/common/network/rest/http/ClientConnection.h b/src/common/network/rest/http/ClientConnection.h index d8bd1bca..0993d5a0 100644 --- a/src/common/network/rest/http/ClientConnection.h +++ b/src/common/network/rest/http/ClientConnection.h @@ -41,7 +41,6 @@ #include "common/network/rest/http/HTTPLexer.h" #include "common/network/rest/http/HTTPPayload.h" #include "common/Log.h" -#include "common/Utils.h" #include #include diff --git a/src/common/network/rest/http/HTTPHeaders.h b/src/common/network/rest/http/HTTPHeaders.h index 54e3ba79..c3ff9515 100644 --- a/src/common/network/rest/http/HTTPHeaders.h +++ b/src/common/network/rest/http/HTTPHeaders.h @@ -39,6 +39,7 @@ #include "common/Defines.h" #include "common/Log.h" +#include "common/Utils.h" #include #include diff --git a/src/common/network/rest/http/ServerConnection.h b/src/common/network/rest/http/ServerConnection.h index 3c9a30e6..1e5e1164 100644 --- a/src/common/network/rest/http/ServerConnection.h +++ b/src/common/network/rest/http/ServerConnection.h @@ -41,7 +41,6 @@ #include "common/network/rest/http/HTTPLexer.h" #include "common/network/rest/http/HTTPPayload.h" #include "common/Log.h" -#include "common/Utils.h" #include #include diff --git a/src/common/nxdn/channel/LICH.cpp b/src/common/nxdn/channel/LICH.cpp index 77b89a98..9fbe5d75 100644 --- a/src/common/nxdn/channel/LICH.cpp +++ b/src/common/nxdn/channel/LICH.cpp @@ -31,6 +31,7 @@ #include "nxdn/NXDNDefines.h" #include "nxdn/channel/LICH.h" #include "Log.h" +#include "Utils.h" using namespace nxdn; using namespace nxdn::channel; diff --git a/src/common/nxdn/lc/RCCH.h b/src/common/nxdn/lc/RCCH.h index f21ba4f1..b09676ef 100644 --- a/src/common/nxdn/lc/RCCH.h +++ b/src/common/nxdn/lc/RCCH.h @@ -31,7 +31,6 @@ #define __NXDN_LC__RCCH_H__ #include "common/Defines.h" -#include "common/nxdn/lc/PacketInformation.h" #include "common/nxdn/SiteData.h" #include "common/lookups/IdenTableLookup.h" diff --git a/src/common/p25/Audio.cpp b/src/common/p25/Audio.cpp index 09a19e5e..cdca748b 100644 --- a/src/common/p25/Audio.cpp +++ b/src/common/p25/Audio.cpp @@ -32,6 +32,7 @@ #include "p25/P25Utils.h" #include "edac/Golay24128.h" #include "edac/Hamming.h" +#include "Utils.h" using namespace p25; diff --git a/src/common/p25/P25Utils.cpp b/src/common/p25/P25Utils.cpp index 6468b40f..bbd6aa11 100644 --- a/src/common/p25/P25Utils.cpp +++ b/src/common/p25/P25Utils.cpp @@ -30,6 +30,7 @@ #include "Defines.h" #include "p25/P25Defines.h" #include "p25/P25Utils.h" +#include "Utils.h" using namespace p25; diff --git a/src/common/p25/dfsi/LC.h b/src/common/p25/dfsi/LC.h index fae5af1c..a346ab93 100644 --- a/src/common/p25/dfsi/LC.h +++ b/src/common/p25/dfsi/LC.h @@ -29,7 +29,6 @@ #include "common/Defines.h" #include "common/p25/data/LowSpeedData.h" #include "common/p25/lc/LC.h" -#include "common/p25/lc/TSBK.h" #include "common/edac/RS634717.h" #include diff --git a/src/common/p25/lc/TDULC.h b/src/common/p25/lc/TDULC.h index 92d70aed..b852b00c 100644 --- a/src/common/p25/lc/TDULC.h +++ b/src/common/p25/lc/TDULC.h @@ -33,6 +33,7 @@ #include "common/p25/SiteData.h" #include "common/edac/RS634717.h" #include "common/lookups/IdenTableLookup.h" +#include "common/Utils.h" #include diff --git a/src/common/p25/lc/TSBK.h b/src/common/p25/lc/TSBK.h index 078812dc..cb1ce7a8 100644 --- a/src/common/p25/lc/TSBK.h +++ b/src/common/p25/lc/TSBK.h @@ -33,6 +33,7 @@ #include "common/p25/SiteData.h" #include "common/edac/RS634717.h" #include "common/lookups/IdenTableLookup.h" +#include "common/Utils.h" #include diff --git a/src/host/nxdn/Audio.cpp b/src/host/nxdn/Audio.cpp index 74d9dc1a..ebd81286 100644 --- a/src/host/nxdn/Audio.cpp +++ b/src/host/nxdn/Audio.cpp @@ -30,6 +30,7 @@ #include "nxdn/Audio.h" #include "common/edac/Golay24128.h" +#include "common/Utils.h" using namespace nxdn; using namespace edac;