From abd7b07ee060efc0f39c9d32384bcc3fc8447b1b Mon Sep 17 00:00:00 2001 From: Bryan Biedenkapp Date: Sun, 22 Nov 2026 08:49:34 -0500 Subject: [PATCH] implement secondary MMDVM DMRGateway patching support; add Homebrew DMR framing, stream handling, ping support, and paced DMR burst delivery; --- configs/patch-config.example.yml | 10 ++ src/patch/HostPatch.cpp | 184 ++++++++++++++++++++--- src/patch/HostPatch.h | 23 ++- src/patch/mmdvm/DMRNetwork.cpp | 250 +++++++++++++++++++++++++++++++ src/patch/mmdvm/DMRNetwork.h | 92 ++++++++++++ 5 files changed, 539 insertions(+), 20 deletions(-) create mode 100644 src/patch/mmdvm/DMRNetwork.cpp create mode 100644 src/patch/mmdvm/DMRNetwork.h diff --git a/configs/patch-config.example.yml b/configs/patch-config.example.yml index 4794b77c..27678d2e 100644 --- a/configs/patch-config.example.yml +++ b/configs/patch-config.example.yml @@ -82,8 +82,10 @@ network: debug: false # Source Talkgroup ID for transmitted/received audio frames. + # In an MMDVM gateway mode, this is the DVM FNE-facing talkgroup. sourceTGID: 1 # Source Slot for received/transmitted audio frames. + # In an MMDVM DMRGateway mode, this is the DVM FNE-facing slot. sourceSlot: 1 # Source Traffic Encryption @@ -98,8 +100,10 @@ network: tekKeyId: 1 # Destination Talkgroup ID for transmitted/received audio frames. + # In an MMDVM gateway mode, this is the gateway-facing talkgroup. destinationTGID: 1 # Destination Slot for received/transmitted audio frames. + # In an MMDVM DMRGateway mode, this is the gateway-facing slot. destinationSlot: 1 # Destination Traffic Encryption @@ -128,6 +132,9 @@ network: mmdvmGatewayPort: 42020 # Local port number for gateway to connect to. localGatewayPort: 32010 + # Numeric repeater ID presented to DMRGateway when MMDVM DMRGateway patching is enabled. + # If omitted, the network peer ID above is used. + mmdvmGatewayId: 9000123 system: # Textual Name @@ -146,6 +153,9 @@ system: # Flag indicating whether or not the patch is from/to a MMDVM P25 reflector. mmdvmP25Reflector: false + # Flag indicating whether or not the patch is from/to a MMDVM DMRGateway. + # This mode is mutually exclusive with mmdvmP25Reflector and requires digiMode 1. + mmdvmDMRGateway: false # Flag indicating whether or not trace logging is enabled. trace: false diff --git a/src/patch/HostPatch.cpp b/src/patch/HostPatch.cpp index 573ddc17..4c23cae5 100644 --- a/src/patch/HostPatch.cpp +++ b/src/patch/HostPatch.cpp @@ -74,6 +74,8 @@ HostPatch::HostPatch(const std::string& confFile) : m_dstTGId(0U), m_dstSlot(1U), m_twoWayPatch(false), + m_mmdvmDMRGateway(false), + m_mmdvmDMRNet(nullptr), m_mmdvmP25Reflector(false), m_mmdvmP25Net(nullptr), m_mmdvmCallEndTimer(1000U, 0U, 500U), @@ -214,7 +216,13 @@ int HostPatch::run() if (!ret) return EXIT_FAILURE; - // initialize MMDVM P25 reflector networking + // initialize optional MMDVM networking + if (m_mmdvmDMRGateway) { + ret = createMMDVMDMRNetwork(); + if (!ret) + return EXIT_FAILURE; + } + if (m_mmdvmP25Reflector) { ret = createMMDVMP25Network(); if (!ret) @@ -228,7 +236,7 @@ int HostPatch::run() if (!Thread::runAsThread(this, threadNetworkProcess)) return EXIT_FAILURE; - if (m_mmdvmP25Reflector) { + if (m_mmdvmDMRGateway || m_mmdvmP25Reflector) { if (!Thread::runAsThread(this, threadMMDVMProcess)) return EXIT_FAILURE; } @@ -256,6 +264,11 @@ int HostPatch::run() m_network->clock(ms); } + if (m_mmdvmDMRGateway) { + std::lock_guard lock(HostPatch::s_networkMutex); + m_mmdvmDMRNet->clock(ms); + } + if (m_mmdvmP25Reflector) { std::lock_guard lock(HostPatch::s_networkMutex); m_mmdvmP25Net->clock(ms); @@ -290,6 +303,16 @@ int HostPatch::run() delete m_network; } + if (m_mmdvmDMRNet != nullptr) { + m_mmdvmDMRNet->close(); + delete m_mmdvmDMRNet; + } + + if (m_mmdvmP25Net != nullptr) { + m_mmdvmP25Net->close(); + delete m_mmdvmP25Net; + } + return EXIT_SUCCESS; } @@ -344,12 +367,23 @@ bool HostPatch::readParams() m_grantDemand = systemConf["grantDemand"].as(false); m_mmdvmP25Reflector = systemConf["mmdvmP25Reflector"].as(false); + m_mmdvmDMRGateway = systemConf["mmdvmDMRGateway"].as(false); + + if (m_mmdvmP25Reflector && m_mmdvmDMRGateway) { + LogError(LOG_HOST, "MMDVM P25 reflector and DMRGateway patch modes are mutually exclusive."); + return false; + } if (m_mmdvmP25Reflector && m_digiMode != TX_MODE_P25) { LogError(LOG_HOST, "Patch does not currently support MMDVM patching in any mode other then P25."); return false; } + if (m_mmdvmDMRGateway && m_digiMode != TX_MODE_DMR) { + LogError(LOG_HOST, "Patch does not support MMDVM DMRGateway patching outside DMR mode."); + return false; + } + m_dropTimeMS = (uint16_t)systemConf["dropTimeMs"].as(1000U); m_callDropTime = Timer(1000U, 0U, m_dropTimeMS); @@ -363,6 +397,7 @@ bool HostPatch::readParams() LogInfo(" Grant Demands: %s", m_grantDemand ? "yes" : "no"); LogInfo(" Drop Time: %ums", m_dropTimeMS); LogInfo(" MMDVM P25 Reflector Patch: %s", m_mmdvmP25Reflector ? "yes" : "no"); + LogInfo(" MMDVM DMRGateway Patch: %s", m_mmdvmDMRGateway ? "yes" : "no"); if (m_debug) { LogInfo(" Debug: yes"); @@ -686,9 +721,47 @@ bool HostPatch::createMMDVMP25Network() return true; } +/* Initializes MMDVM DMRGateway connectivity. */ + +bool HostPatch::createMMDVMDMRNetwork() +{ + yaml::Node networkConf = m_conf["network"]; + + std::string address = networkConf["mmdvmGatewayAddress"].as(); + uint16_t port = (uint16_t)networkConf["mmdvmGatewayPort"].as(62031U); + uint16_t localPort = (uint16_t)networkConf["localGatewayPort"].as(62032U); + uint32_t repeaterId = networkConf["mmdvmGatewayId"].as(networkConf["id"].as(1000U)); + bool debug = networkConf["debug"].as(false); + + LogInfo("MMDVM Network Parameters"); + LogInfo(" Address: %s", address.c_str()); + LogInfo(" Port: %u", port); + LogInfo(" Local Port: %u", localPort); + LogInfo(" Repeater ID: %u", repeaterId); + + if (repeaterId == 0U || repeaterId > 999999999U) { + LogError(LOG_HOST, "MMDVM repeater ID must be between 1 and 999999999."); + return false; + } + + if (debug) + LogInfo(" Debug: yes"); + + m_mmdvmDMRNet = new mmdvm::DMRNetwork(address, port, localPort, repeaterId, debug); + bool ret = m_mmdvmDMRNet->open(); + if (!ret) { + delete m_mmdvmDMRNet; + m_mmdvmDMRNet = nullptr; + LogError(LOG_HOST, "failed to initialize MMDVM networking!"); + return false; + } + + return true; +} + /* Helper to process DMR network traffic. */ -void HostPatch::processDMRNetwork(uint8_t* buffer, uint32_t length) +void HostPatch::processDMRNetwork(uint8_t* buffer, uint32_t length, bool fromMMDVM) { assert(buffer != nullptr); using namespace dmr; @@ -740,6 +813,7 @@ void HostPatch::processDMRNetwork(uint8_t* buffer, uint32_t length) bool dataSync = (buffer[15U] & 0x20U) == 0x20U; bool voiceSync = (buffer[15U] & 0x10U) == 0x10U; + uint8_t dmrN = buffer[15U] & 0x0FU; if (m_debug) { LogDebug(LOG_NET, "DMR, seqNo = %u, srcId = %u, dstId = %u, flco = $%02X, slotNo = %u, len = %u", seqNo, srcId, dstId, flco, slotNo, length); @@ -789,8 +863,17 @@ void HostPatch::processDMRNetwork(uint8_t* buffer, uint32_t length) } } + bool toMMDVM = m_mmdvmDMRGateway && !fromMMDVM; + uint8_t actualDstSlot = m_dstSlot; + if (m_mmdvmDMRGateway && fromMMDVM) + actualDstSlot = m_srcSlot; + + uint32_t incomingStreamId = m_network->getDMRStreamId(slotNo); + if (fromMMDVM) + ::memcpy(&incomingStreamId, buffer + 16U, sizeof(uint32_t)); + // is this a new call stream? - if (m_network->getDMRStreamId(slotNo) != m_rxStreamId) { + if (incomingStreamId != m_rxStreamId) { m_callInProgress = true; uint64_t now = std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()).count(); @@ -805,14 +888,14 @@ void HostPatch::processDMRNetwork(uint8_t* buffer, uint32_t length) if (dataSync && (dataType == DataType::TERMINATOR_WITH_LC)) { // generate DMR network frame data::NetData dmrData; - dmrData.setSlotNo(m_dstSlot); + dmrData.setSlotNo(actualDstSlot); dmrData.setDataType(DataType::TERMINATOR_WITH_LC); dmrData.setSrcId(srcId); dmrData.setDstId(actualDstId); dmrData.setFLCO(flco); dmrData.setControl(controlByte); - uint8_t n = data[15U] & 0x0FU; + uint8_t n = m_mmdvmDMRGateway ? dmrN : data[15U] & 0x0FU; dmrData.setN(n); dmrData.setSeqNo(seqNo); @@ -821,12 +904,33 @@ void HostPatch::processDMRNetwork(uint8_t* buffer, uint32_t length) dmrData.setData(data.get()); - m_network->writeDMRTerminator(dmrData, &seqNo, &n, m_dmrEmbeddedData); + if (toMMDVM) { + lc::LC lc = lc::LC(); + lc.setFLCO(flco); + lc.setSrcId(srcId); + lc.setDstId(actualDstId); + + uint8_t terminator[DMR_FRAME_LENGTH_BYTES]; + ::memset(terminator, 0x00U, DMR_FRAME_LENGTH_BYTES); + + SlotType slotType = SlotType(); + slotType.setDataType(DataType::TERMINATOR_WITH_LC); + slotType.encode(terminator); + + lc::FullLC fullLC = lc::FullLC(); + fullLC.encode(lc, terminator, DataType::TERMINATOR_WITH_LC); + dmrData.setData(terminator); + + writeDMR(dmrData, true); + } + else { + m_network->writeDMRTerminator(dmrData, &seqNo, &n, m_dmrEmbeddedData); + } resetDMRCall(srcId, dmrData.getSlotNo()); return; } - m_rxStreamId = m_network->getDMRStreamId(slotNo); + m_rxStreamId = incomingStreamId; m_callDropTime.start(); uint8_t* buffer = nullptr; @@ -855,7 +959,7 @@ void HostPatch::processDMRNetwork(uint8_t* buffer, uint32_t length) // generate DMR network frame data::NetData dmrData; - dmrData.setSlotNo(m_dstSlot); + dmrData.setSlotNo(actualDstSlot); dmrData.setDataType(DataType::VOICE_LC_HEADER); dmrData.setSrcId(srcId); dmrData.setDstId(actualDstId); @@ -867,7 +971,7 @@ void HostPatch::processDMRNetwork(uint8_t* buffer, uint32_t length) dmrData.setControl(0U); } - uint8_t n = data[15U] & 0x0FU; + uint8_t n = m_mmdvmDMRGateway ? dmrN : data[15U] & 0x0FU; dmrData.setN(n); dmrData.setSeqNo(seqNo); @@ -876,7 +980,7 @@ void HostPatch::processDMRNetwork(uint8_t* buffer, uint32_t length) dmrData.setData(buffer); - m_network->writeDMR(dmrData, false); + writeDMR(dmrData, toMMDVM); delete[] buffer; } @@ -903,7 +1007,7 @@ void HostPatch::processDMRNetwork(uint8_t* buffer, uint32_t length) // generate DMR network frame data::NetData dmrData; - dmrData.setSlotNo(m_dstSlot); + dmrData.setSlotNo(actualDstSlot); dmrData.setDataType(DataType::VOICE_PI_HEADER); dmrData.setSrcId(srcId); dmrData.setDstId(actualDstId); @@ -915,7 +1019,7 @@ void HostPatch::processDMRNetwork(uint8_t* buffer, uint32_t length) dmrData.setControl(0U); } - uint8_t n = data[15U] & 0x0FU; + uint8_t n = m_mmdvmDMRGateway ? dmrN : data[15U] & 0x0FU; dmrData.setN(n); dmrData.setSeqNo(seqNo); @@ -924,7 +1028,7 @@ void HostPatch::processDMRNetwork(uint8_t* buffer, uint32_t length) dmrData.setData(buffer); - m_network->writeDMR(dmrData, false); + writeDMR(dmrData, toMMDVM); delete[] buffer; } @@ -933,7 +1037,7 @@ void HostPatch::processDMRNetwork(uint8_t* buffer, uint32_t length) buffer = new uint8_t[DMR_FRAME_LENGTH_BYTES]; ::memcpy(buffer, data.get(), DMR_FRAME_LENGTH_BYTES); - uint8_t n = data[15U] & 0x0FU; + uint8_t n = m_mmdvmDMRGateway ? dmrN : data[15U] & 0x0FU; DataType::E dataType = DataType::VOICE_SYNC; if (n == 0) @@ -954,7 +1058,7 @@ void HostPatch::processDMRNetwork(uint8_t* buffer, uint32_t length) // generate DMR network frame data::NetData dmrData; - dmrData.setSlotNo(m_dstSlot); + dmrData.setSlotNo(actualDstSlot); dmrData.setDataType(dataType); dmrData.setSrcId(srcId); dmrData.setDstId(actualDstId); @@ -966,11 +1070,21 @@ void HostPatch::processDMRNetwork(uint8_t* buffer, uint32_t length) dmrData.setData(buffer); - m_network->writeDMR(dmrData, false); + writeDMR(dmrData, toMMDVM); } } } +/* Writes a rewritten DMR burst to its selected destination transport. */ + +bool HostPatch::writeDMR(dmr::data::NetData& data, bool toMMDVM) +{ + if (toMMDVM) + return m_mmdvmDMRNet->write(data); + + return m_network->writeDMR(data, false); +} + /* Helper to process P25 network traffic. */ void HostPatch::processP25Network(uint8_t* buffer, uint32_t length) @@ -2215,6 +2329,42 @@ void* HostPatch::threadMMDVMProcess(void* arg) patch->m_rxStreamId = 0U; } + if (patch->m_digiMode == TX_MODE_DMR && patch->m_mmdvmDMRGateway) { + std::lock_guard lock(HostPatch::s_networkMutex); + + dmr::data::NetData dmrData; + uint32_t streamId = 0U; + if (patch->m_mmdvmDMRNet->read(dmrData, streamId)) { + uint8_t buffer[network::DMR_PACKET_LENGTH]; + ::memset(buffer, 0x00U, network::DMR_PACKET_LENGTH); + ::memcpy(buffer, TAG_DMR_DATA, 4U); + + buffer[4U] = dmrData.getSeqNo(); + SET_UINT24(dmrData.getSrcId(), buffer, 5U); + SET_UINT24(dmrData.getDstId(), buffer, 8U); + + uint32_t slotNo = dmrData.getSlotNo(); + buffer[15U] = slotNo == 2U ? 0x80U : 0x00U; + if (dmrData.getFLCO() != dmr::defines::FLCO::GROUP) + buffer[15U] |= 0x40U; + + dmr::defines::DataType::E dataType = dmrData.getDataType(); + if (dataType == dmr::defines::DataType::VOICE_SYNC) + buffer[15U] |= 0x10U; + else if (dataType == dmr::defines::DataType::VOICE) + buffer[15U] |= dmrData.getN() & 0x0FU; + else + buffer[15U] |= 0x20U | dataType; + + ::memcpy(buffer + 16U, &streamId, sizeof(uint32_t)); + dmrData.getData(buffer + 20U); + buffer[53U] = dmrData.getBER(); + buffer[54U] = dmrData.getRSSI(); + + patch->processDMRNetwork(buffer, network::DMR_PACKET_LENGTH, true); + } + } + if (patch->m_digiMode == TX_MODE_P25) { std::lock_guard lock(HostPatch::s_networkMutex); diff --git a/src/patch/HostPatch.h b/src/patch/HostPatch.h index 53dce5d3..0b8b0af7 100644 --- a/src/patch/HostPatch.h +++ b/src/patch/HostPatch.h @@ -26,6 +26,7 @@ #include "common/yaml/Yaml.h" #include "common/Timer.h" #include "network/PeerNetwork.h" +#include "mmdvm/DMRNetwork.h" #include "mmdvm/P25Network.h" #include @@ -76,6 +77,8 @@ private: uint8_t m_dstSlot; bool m_twoWayPatch; + bool m_mmdvmDMRGateway; + mmdvm::DMRNetwork* m_mmdvmDMRNet; bool m_mmdvmP25Reflector; mmdvm::P25Network* m_mmdvmP25Net; Timer m_mmdvmCallEndTimer; @@ -141,13 +144,27 @@ private: * @returns bool True, if network connectivity was initialized, otherwise false. */ bool createMMDVMP25Network(); + /** + * @brief Initializes MMDVM DMRGateway connectivity. + * @returns bool True, if network connectivity was initialized, otherwise false. + */ + bool createMMDVMDMRNetwork(); /** * @brief Helper to process DMR network traffic. - * @param buffer - * @param length + * @param buffer DVM DMRD frame to process. + * @param length Length of the frame in bytes. + * @param fromMMDVM Flag indicating that the frame originated from DMRGateway. + */ + void processDMRNetwork(uint8_t* buffer, uint32_t length, bool fromMMDVM = false); + + /** + * @brief Writes a rewritten DMR burst to its selected destination transport. + * @param[in] data DMR burst and metadata to write. + * @param toMMDVM Flag indicating that the destination is DMRGateway. + * @return bool True if the burst was written or queued, otherwise false. */ - void processDMRNetwork(uint8_t* buffer, uint32_t length); + bool writeDMR(dmr::data::NetData& data, bool toMMDVM); /** * @brief Helper to process P25 network traffic. diff --git a/src/patch/mmdvm/DMRNetwork.cpp b/src/patch/mmdvm/DMRNetwork.cpp new file mode 100644 index 00000000..e7b27fbe --- /dev/null +++ b/src/patch/mmdvm/DMRNetwork.cpp @@ -0,0 +1,250 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Digital Voice Modem - TG Patch + * GPLv2 Open Source. Use is subject to license terms. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright (C) 2015,2016,2017,2018,2020,2026 by Jonathan Naylor G4KLX + * Copyright (C) 2026 Bryan Biedenkapp, N2PLL + * + */ +#include "patch/Defines.h" +#include "common/dmr/DMRDefines.h" +#include "common/Log.h" +#include "common/Utils.h" +#include "mmdvm/DMRNetwork.h" + +using namespace dmr::defines; +using namespace network::udp; +using namespace mmdvm; + +#include +#include + +// --------------------------------------------------------------------------- +// Constants +// --------------------------------------------------------------------------- + +const uint32_t HOMEBREW_DMRD_LENGTH = 55U; +const uint32_t HOMEBREW_BUFFER_LENGTH = 100U; +const uint32_t HOMEBREW_TX_BUFFER_LENGTH = 11000U; +const uint32_t DMR_BURST_INTERVAL_MS = 60U; +const uint32_t DMR_PING_INTERVAL_MS = 10000U; + +// --------------------------------------------------------------------------- +// Public Class Members +// --------------------------------------------------------------------------- + +/* Initializes a new instance of the DMRNetwork class. */ + +DMRNetwork::DMRNetwork(const std::string& gatewayAddress, uint16_t gatewayPort, uint16_t localPort, uint32_t repeaterId, bool debug) : + m_socket(localPort), + m_addr(), + m_addrLen(0U), + m_repeaterId(repeaterId), + m_debug(debug), + m_rxBuffer(6000U, "MMDVM DMR Rx Network"), + m_txBuffer(HOMEBREW_TX_BUFFER_LENGTH, "MMDVM DMR Tx Network"), + m_txElapsed(DMR_BURST_INTERVAL_MS), + m_pingElapsed(0U), + m_streamId{ 0U, 0U }, + m_random(std::random_device{}()) +{ + if (Socket::lookup(gatewayAddress, gatewayPort, m_addr, m_addrLen) != 0) + m_addrLen = 0U; +} + +/* Finalizes an instance of the DMRNetwork class. */ + +DMRNetwork::~DMRNetwork() = default; + +/* Reads one received Homebrew DMRD record. */ + +bool DMRNetwork::read(dmr::data::NetData& data, uint32_t& streamId) +{ + if (m_rxBuffer.isEmpty()) + return false; + + uint8_t length = 0U; + m_rxBuffer.get(&length, 1U); + if (length != HOMEBREW_DMRD_LENGTH) + return false; + + uint8_t buffer[HOMEBREW_DMRD_LENGTH]; + m_rxBuffer.get(buffer, length); + + data.setSeqNo(buffer[4U]); + uint32_t srcId = GET_UINT24(buffer, 5U); + uint32_t dstId = GET_UINT24(buffer, 8U); + data.setSrcId(srcId); + data.setDstId(dstId); + data.setSlotNo((buffer[15U] & 0x80U) == 0x80U ? 2U : 1U); + data.setFLCO((buffer[15U] & 0x40U) == 0x40U ? FLCO::PRIVATE : FLCO::GROUP); + data.setBER(buffer[53U]); + data.setRSSI(buffer[54U]); + data.setData(buffer + 20U); + + if ((buffer[15U] & 0x20U) == 0x20U) { + data.setDataType((DataType::E)(buffer[15U] & 0x0FU)); + data.setN(0U); + } + else if ((buffer[15U] & 0x10U) == 0x10U) { + data.setDataType(DataType::VOICE_SYNC); + data.setN(0U); + } + else { + data.setDataType(DataType::VOICE); + data.setN(buffer[15U] & 0x0FU); + } + + ::memcpy(&streamId, buffer + 16U, sizeof(uint32_t)); + return true; +} + +/* Queues one DMR burst for paced Homebrew transmission. */ + +bool DMRNetwork::write(const dmr::data::NetData& data) +{ + uint32_t slotNo = data.getSlotNo(); + if (slotNo < 1U || slotNo > 2U) + return false; + + uint32_t slotIndex = slotNo - 1U; + DataType::E dataType = data.getDataType(); + if (dataType == DataType::VOICE_LC_HEADER || m_streamId[slotIndex] == 0U) + m_streamId[slotIndex] = createStreamId(); + + uint8_t buffer[HOMEBREW_DMRD_LENGTH]; + ::memset(buffer, 0x00U, HOMEBREW_DMRD_LENGTH); + ::memcpy(buffer, "DMRD", 4U); + + buffer[4U] = data.getSeqNo(); + SET_UINT24(data.getSrcId(), buffer, 5U); + SET_UINT24(data.getDstId(), buffer, 8U); + SET_UINT32(m_repeaterId, buffer, 11U); + + buffer[15U] = slotNo == 2U ? 0x80U : 0x00U; + if (data.getFLCO() != FLCO::GROUP) + buffer[15U] |= 0x40U; + + if (dataType == DataType::VOICE_SYNC) + buffer[15U] |= 0x10U; + else if (dataType == DataType::VOICE) + buffer[15U] |= data.getN() & 0x0FU; + else + buffer[15U] |= 0x20U | dataType; + + ::memcpy(buffer + 16U, &m_streamId[slotIndex], sizeof(uint32_t)); + data.getData(buffer + 20U); + buffer[53U] = data.getBER(); + buffer[54U] = data.getRSSI(); + + if (!m_txBuffer.hasSpace(HOMEBREW_DMRD_LENGTH)) { + LogError(LOG_NET, "MMDVM DMR, transmit queue overflow"); + return false; + } + + if (m_txBuffer.isEmpty()) + m_txElapsed = DMR_BURST_INTERVAL_MS; + + bool ret = m_txBuffer.addData(buffer, HOMEBREW_DMRD_LENGTH); + if (dataType == DataType::TERMINATOR_WITH_LC) + m_streamId[slotIndex] = 0U; + + return ret; +} + +/* Clocks receive, ping, and paced transmit processing. */ + +void DMRNetwork::clock(uint32_t ms) +{ + if (m_txBuffer.isEmpty()) + m_txElapsed = DMR_BURST_INTERVAL_MS; + else + m_txElapsed += ms; + + if (m_txElapsed >= DMR_BURST_INTERVAL_MS && !m_txBuffer.isEmpty()) { + uint8_t buffer[HOMEBREW_DMRD_LENGTH]; + m_txBuffer.get(buffer, HOMEBREW_DMRD_LENGTH); + + if (m_debug) + Utils::dump(1U, "MMDVM DMR Network Data Sent", buffer, HOMEBREW_DMRD_LENGTH); + + if (!m_socket.write(buffer, HOMEBREW_DMRD_LENGTH, m_addr, m_addrLen)) + LogError(LOG_NET, "MMDVM DMR, failed to write paced DMRD record"); + + m_txElapsed = 0U; + } + + m_pingElapsed += ms; + if (m_pingElapsed >= DMR_PING_INTERVAL_MS) { + const uint8_t ping[] = { 'D', 'M', 'R', 'P' }; + m_socket.write(ping, sizeof(ping), m_addr, m_addrLen); + m_pingElapsed = 0U; + } + + uint8_t buffer[HOMEBREW_BUFFER_LENGTH]; + sockaddr_storage address; + uint32_t addrLen = 0U; + int length = m_socket.read(buffer, HOMEBREW_BUFFER_LENGTH, address, addrLen); + if (length <= 0) + return; + + if (!Socket::match(m_addr, address)) { + LogWarning(LOG_NET, "MMDVM DMR, packet received from an invalid source"); + return; + } + + if (length == 4 && ::memcmp(buffer, "DMRP", 4U) == 0) + return; + + if (length != (int)HOMEBREW_DMRD_LENGTH || ::memcmp(buffer, "DMRD", 4U) != 0) { + LogWarning(LOG_NET, "MMDVM DMR, invalid packet, length = %d", length); + return; + } + + if (m_debug) + Utils::dump(1U, "MMDVM DMR Network Data Received", buffer, length); + + uint8_t recordLength = (uint8_t)length; + if (!m_rxBuffer.hasSpace(recordLength + 1U)) { + LogError(LOG_NET, "MMDVM DMR, receive queue overflow"); + return; + } + + m_rxBuffer.addData(&recordLength, 1U); + m_rxBuffer.addData(buffer, recordLength); +} + +/* Opens the UDP transport. */ + +bool DMRNetwork::open() +{ + if (m_addrLen == 0U) { + LogError(LOG_NET, "MMDVM DMR, unable to resolve DMRGateway address"); + return false; + } + + LogInfoEx(LOG_NET, "MMDVM DMR, opening DMRGateway connection"); + return m_socket.open(m_addr); +} + +/* Closes the UDP transport. */ + +void DMRNetwork::close() +{ + m_socket.close(); + LogInfoEx(LOG_NET, "MMDVM DMR, closing DMRGateway connection"); +} + +// --------------------------------------------------------------------------- +// Private Class Members +// --------------------------------------------------------------------------- + +/* Creates a non-zero Homebrew stream identifier. */ + +uint32_t DMRNetwork::createStreamId() +{ + std::uniform_int_distribution distribution(1U, UINT32_MAX); + return distribution(m_random); +} diff --git a/src/patch/mmdvm/DMRNetwork.h b/src/patch/mmdvm/DMRNetwork.h new file mode 100644 index 00000000..0805ed9d --- /dev/null +++ b/src/patch/mmdvm/DMRNetwork.h @@ -0,0 +1,92 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Digital Voice Modem - TG Patch + * GPLv2 Open Source. Use is subject to license terms. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright (C) 2015,2016,2017,2018,2020,2026 by Jonathan Naylor G4KLX + * Copyright (C) 2026 Bryan Biedenkapp, N2PLL + * + */ +/** + * @file DMRNetwork.h + * @ingroup patch_mmdvm + */ +#if !defined(__MMDVM_DMR_NETWORK_H__) +#define __MMDVM_DMR_NETWORK_H__ + +#include "common/dmr/data/NetData.h" +#include "common/network/udp/Socket.h" +#include "common/RingBuffer.h" + +#include +#include +#include + +namespace mmdvm +{ + // --------------------------------------------------------------------------- + // Class Declaration + // --------------------------------------------------------------------------- + + /** + * @brief Implements the MMDVM Homebrew DMR transport used by DMRGateway. + * @ingroup patch_mmdvm + */ + class DMRNetwork { + public: + /** + * @brief Initializes a new instance of the DMRNetwork class. + * @param gatewayAddress Network hostname or address of DMRGateway. + * @param gatewayPort DMRGateway local UDP port. + * @param localPort Local UDP port presented as the MMDVM endpoint. + * @param repeaterId Numeric repeater ID placed in outbound DMRD records. + * @param debug Flag indicating whether packet debugging is enabled. + */ + DMRNetwork(const std::string& gatewayAddress, uint16_t gatewayPort, uint16_t localPort, uint32_t repeaterId, bool debug); + /** @brief Finalizes an instance of the DMRNetwork class. */ + ~DMRNetwork(); + + /** + * @brief Reads one received Homebrew DMRD record. + * @param[out] data Decoded DMR burst and metadata. + * @param[out] streamId Homebrew stream identifier from the record. + * @return bool True if a record was read, otherwise false. + */ + bool read(dmr::data::NetData& data, uint32_t& streamId); + /** + * @brief Queues one DMR burst for paced Homebrew transmission. + * @param[in] data DMR burst and metadata to transmit. + * @return bool True if the record was queued, otherwise false. + */ + bool write(const dmr::data::NetData& data); + + /** + * @brief Clocks receive, ping, and paced transmit processing. + * @param ms Elapsed milliseconds. + */ + void clock(uint32_t ms); + /** @brief Opens the UDP transport. @return bool True on success. */ + bool open(); + /** @brief Closes the UDP transport. */ + void close(); + + private: + network::udp::Socket m_socket; //!< UDP transport bound to the MMDVM-facing port. + sockaddr_storage m_addr; //!< Resolved DMRGateway endpoint. + uint32_t m_addrLen; //!< Length of the resolved endpoint structure. + uint32_t m_repeaterId; //!< Repeater ID serialized in DMRD records. + bool m_debug; //!< Enables packet dumps. + RingBuffer m_rxBuffer; //!< Length-prefixed received DMRD records. + RingBuffer m_txBuffer; //!< Complete DMRD records awaiting paced transmission. + uint32_t m_txElapsed; //!< Milliseconds accumulated toward the next DMR burst. + uint32_t m_pingElapsed; //!< Milliseconds accumulated toward the next DMRP ping. + uint32_t m_streamId[2U]; //!< Outbound Homebrew stream identifier for each slot. + std::mt19937 m_random; //!< Random source used to create Homebrew stream identifiers. + + /** @brief Creates a non-zero Homebrew stream identifier. @return uint32_t Stream identifier. */ + uint32_t createStreamId(); + }; +} // namespace mmdvm + +#endif // __MMDVM_DMR_NETWORK_H__