diff --git a/src/host/p25/Control.cpp b/src/host/p25/Control.cpp index 2bdbd64a..cb7c261b 100644 --- a/src/host/p25/Control.cpp +++ b/src/host/p25/Control.cpp @@ -7,6 +7,7 @@ * Copyright (C) 2016,2017,2018 Jonathan Naylor, G4KLX * Copyright (C) 2017-2026 Bryan Biedenkapp, N2PLL * Copyright (C) 2026 Charles Bricker, K3NOP + * Copyright (C) 2026 David Andrzejewski * */ #include "Defines.h" @@ -39,6 +40,10 @@ const uint32_t MAX_PREAMBLE_TDU_CNT = 64U; const uint32_t VOICE_CALL_TERM_TIMEOUT = 1000U; // ms +// failsafe for a lost control channel unpermit; twice the CC grant timer so a +// grant refreshed by a repeated SNDCP channel request is never cut short +const uint32_t DATA_CH_PERMIT_HOLD_TIMEOUT = 30U; // s + // --------------------------------------------------------------------------- // Static Class Members // --------------------------------------------------------------------------- @@ -125,6 +130,8 @@ Control::Control(bool authoritative, uint32_t nac, uint32_t callHang, uint32_t q m_adjSiteUpdate(1000U, 75U), m_activeTGUpdate(1000U, 5U), m_ccPacketInterval(1000U, 0U, 15U), + m_dataChKeepAlive(1000U, 0U, 15U), + m_dataChPermitHold(1000U, DATA_CH_PERMIT_HOLD_TIMEOUT), m_rfVoiceCallTermTimeout(1000U, VOICE_CALL_TERM_TIMEOUT), m_interval(), m_hangCount(3U * 8U), @@ -1100,6 +1107,8 @@ void Control::clock() } } + handleDataChannelKeepAlive(ms); + // reset states if we're in a rejected state and we're a control channel if (m_rfState == RS_RF_REJECTED && m_enableControl && m_dedicatedControl) { clearRFReject(); @@ -1324,6 +1333,17 @@ void Control::permittedTG(uint32_t dstId, bool dataPermit, bool telInt, uint32_t if (dataPermit && m_sndcpSupport) { m_data->sndcpInitialize(dstId); } + + // an SU directed here listens for outbound frame sync before it transmits + // a packet; an idle data channel must therefore key up on its own + if (dstId != 0U && dataPermit && m_sndcpSupport) { + m_dataChPermitHold.start(); + m_dataChKeepAlive.start(); + } + else { + m_dataChPermitHold.stop(); + m_dataChKeepAlive.stop(); + } } /* Tests traffic identities against the current voice-channel permit. */ @@ -1946,6 +1966,62 @@ void Control::processFrameLoss(RPT_RF_LOSS_TYPE type) } } +/* Helper to keep the outbound keyed on an idle, SNDCP-permitted data channel. */ + +void Control::handleDataChannelKeepAlive(uint32_t ms) +{ + // never key a data channel without SNDCP support, regardless of the caller + if (!m_sndcpSupport) + return; + + // the permit hold runs for as long as a data permit is held + if (!m_dataChPermitHold.isRunning()) + return; + + m_dataChPermitHold.clock(ms); + if (m_dataChPermitHold.hasExpired()) { + // a lost unpermit must not leave the transmitter keyed indefinitely + LogWarning(LOG_P25, "data channel permit hold expired without a control channel release, dstId = %u", m_permittedDstId); + + m_dataChPermitHold.stop(); + m_dataChKeepAlive.stop(); + return; + } + + // once the SU transmits on this grant it no longer needs outbound frame sync, so + // the keep-alive ends for the rest of the grant + if (m_rfState != RS_RF_LISTENING) { + if (m_dataChKeepAlive.isRunning() && m_debug) { + LogDebugEx(LOG_P25, "Control::handleDataChannelKeepAlive()", "SU transmitting, data channel keep-alive ended, dstId = %u", m_permittedDstId); + } + + m_dataChKeepAlive.stop(); + return; + } + + m_dataChKeepAlive.clock(ms); + if (!m_dataChKeepAlive.isRunning() || !m_dataChKeepAlive.hasExpired()) + return; + + m_dataChKeepAlive.start(); + + // only fill otherwise idle air time; network traffic in progress, and any frame + // already queued (including PDU responses), keep the carrier up + if (m_netState != RS_NET_IDLE) + return; + if (peekFrameLength() != 0U) + return; + + // a PDU held by the native-modem pacing guard must not queue behind keep-alive + // TDUs, and filling the guard would keep the transmitter keyed when the SU answers + if (m_data->isRFPDUPaced()) + return; + + // a TDU carries frame sync and all-idle status symbols, advertising the + // inbound channel as free to the SU waiting to send its packet + writeRF_TDU(true); +} + /* Helper to process an In-Call Control message. */ void Control::processInCallCtrl(network::NET_ICC::ENUM command, uint32_t dstId) diff --git a/src/host/p25/Control.h b/src/host/p25/Control.h index 99b0456f..d6d2b12c 100644 --- a/src/host/p25/Control.h +++ b/src/host/p25/Control.h @@ -6,6 +6,7 @@ * * Copyright (C) 2016,2017 Jonathan Naylor, G4KLX * Copyright (C) 2017-2026 Bryan Biedenkapp, N2PLL + * Copyright (C) 2026 David Andrzejewski * */ /** @@ -389,6 +390,8 @@ namespace p25 Timer m_activeTGUpdate; Timer m_ccPacketInterval; + Timer m_dataChKeepAlive; + Timer m_dataChPermitHold; uint32_t m_rfCallTermSrcId; uint32_t m_rfCallTermDstId; @@ -461,6 +464,11 @@ namespace p25 * @param type Type of RF frame loss. */ void processFrameLoss(RPT_RF_LOSS_TYPE type); + /** + * @brief Helper to keep the outbound keyed on an idle, SNDCP-permitted data channel. + * @param ms Elapsed time in milliseconds since the last clock. + */ + void handleDataChannelKeepAlive(uint32_t ms); /** * @brief Helper to process an In-Call Control message. * @param command In-Call Control Command. diff --git a/src/host/p25/packet/Data.cpp b/src/host/p25/packet/Data.cpp index a8d9c82d..ebf2a21d 100644 --- a/src/host/p25/packet/Data.cpp +++ b/src/host/p25/packet/Data.cpp @@ -6,6 +6,7 @@ * * Copyright (C) 2016,2017,2018 Jonathan Naylor, G4KLX * Copyright (C) 2017-2026 Bryan Biedenkapp, N2PLL + * Copyright (C) 2026 David Andrzejewski * */ #include "Defines.h" @@ -192,8 +193,18 @@ bool Data::process(uint8_t* data, uint32_t len) } } - // rewrite the response to the network - writeNetwork(0U, buffer, P25_PDU_FEC_LENGTH_BYTES, true); + // rewrite the response to the network; the network frame carries the header's + // blocks to follow, so a response with a data block (e.g. an ACK RETRY) must + // send that block too or the receiver waits for a block that never arrives + uint8_t rspBlock[P25_PDU_FEC_LENGTH_BYTES]; + if (m_rfAssembler->dataHeader.getBlocksToFollow() > 0U && + getResponseDataBlock(m_rfPDU, m_rfPDUBits, rspBlock)) { + writeNetwork(0U, buffer, P25_PDU_FEC_LENGTH_BYTES, false); + writeNetwork(1U, rspBlock, P25_PDU_FEC_LENGTH_BYTES, true); + } + else { + writeNetwork(0U, buffer, P25_PDU_FEC_LENGTH_BYTES, true); + } // only repeat the PDU locally if the packet isn't for the FNE if (m_repeatPDU && m_rfAssembler->dataHeader.getLLId() != WUID_FNE) { @@ -643,6 +654,14 @@ void Data::resetReceivedBlocks() m_netTotalBlocks = 0U; } +/* Helper to determine whether an RF PDU is held by, or still spaced by, the native-modem pacing guard. */ + +bool Data::isRFPDUPaced() +{ + std::lock_guard lock(m_pduPacingMutex); + return m_pduPacingRemainingMs > 0U || !m_pendingPDUs.empty(); +} + /* Helper to write user data as a P25 PDU packet. */ bool Data::writeRF_PDU_User(data::DataHeader& dataHeader, bool extendedAddress, bool auxiliaryES, uint8_t* pduUserData, bool imm) @@ -998,6 +1017,22 @@ bool Data::processSNDCPControl(const uint8_t* pduUserData, uint32_t length) return true; } +/* Helper to copy the encoded data block that follows a response header out of a received RF PDU. */ + +bool Data::getResponseDataBlock(const uint8_t* rfPDU, uint32_t rfPDUBits, uint8_t* block) +{ + assert(rfPDU != nullptr); + assert(block != nullptr); + + // the data block follows the preamble and the header block + if (rfPDUBits < P25_PREAMBLE_LENGTH_BITS + (2U * P25_PDU_FEC_LENGTH_BITS)) + return false; + + ::memset(block, 0x00U, P25_PDU_FEC_LENGTH_BYTES); + Utils::getBitRange(rfPDU, block, P25_PREAMBLE_LENGTH_BITS + P25_PDU_FEC_LENGTH_BITS, P25_PDU_FEC_LENGTH_BITS); + return true; +} + /* Write data processed from RF to the network. */ void Data::writeNetwork(const uint8_t currentBlock, const uint8_t* data, uint32_t len, bool lastBlock) diff --git a/src/host/p25/packet/Data.h b/src/host/p25/packet/Data.h index 03cb4f13..8ae8c7c2 100644 --- a/src/host/p25/packet/Data.h +++ b/src/host/p25/packet/Data.h @@ -6,6 +6,7 @@ * * Copyright (C) 2016,2017 Jonathan Naylor, G4KLX * Copyright (C) 2017-2025 Bryan Biedenkapp, N2PLL + * Copyright (C) 2026 David Andrzejewski * */ /** @@ -33,6 +34,14 @@ #include #include +// --------------------------------------------------------------------------- +// Class Prototypes +// --------------------------------------------------------------------------- + +#if defined(CATCH2_TEST_COMPILATION) +class HostTestHooks; +#endif + namespace p25 { // --------------------------------------------------------------------------- @@ -125,6 +134,9 @@ namespace p25 private: friend class p25::Control; +#if defined(CATCH2_TEST_COMPILATION) + friend class ::HostTestHooks; +#endif Control* m_p25; RPT_RF_STATE m_prevRfState; @@ -214,6 +226,16 @@ namespace p25 */ bool processSNDCPControl(const uint8_t* pduUserData, uint32_t length); + /** + * @brief Helper to copy the encoded data block that follows a response header out of a + * received RF PDU (e.g. the selective retry flags of an ACK RETRY response). + * @param[in] rfPDU Received RF PDU bits, starting with the preamble. + * @param rfPDUBits Number of valid bits held in rfPDU. + * @param[out] block Buffer of at least P25_PDU_FEC_LENGTH_BYTES receiving the encoded block. + * @returns bool True if the data block was received and copied; otherwise, false. + */ + static bool getResponseDataBlock(const uint8_t* rfPDU, uint32_t rfPDUBits, uint8_t* block); + /** * @brief Write data processed from RF to the network. * @param currentBlock Current Block ID. @@ -227,6 +249,11 @@ namespace p25 * @brief Helper to reset received network blocks. */ void resetReceivedBlocks(); + /** + * @brief Helper to determine whether an RF PDU is held by, or still spaced by, the native-modem pacing guard. + * @returns bool True, if an RF PDU is held or the pacing guard is running, otherwise false. + */ + bool isRFPDUPaced(); /** * @brief Helper to queue a complete P25 PDU packet. diff --git a/tests/host/HostTestHooks.cpp b/tests/host/HostTestHooks.cpp index d2f6da09..e657e365 100644 --- a/tests/host/HostTestHooks.cpp +++ b/tests/host/HostTestHooks.cpp @@ -24,6 +24,7 @@ #include "common/p25/lc/tsbk/TSBKFactory.h" #include "common/p25/P25Utils.h" #include "host/p25/packet/ControlSignaling.h" +#include "host/p25/packet/Data.h" #include "host/p25/packet/Voice.h" #include @@ -594,6 +595,29 @@ void HostTestHooks::p25ClockU2U(p25::Control& control, uint32_t ms) control.m_control->clock(ms); } +/* Sets whether P25 SNDCP support is enabled. */ + +void HostTestHooks::p25SetSNDCPSupport(p25::Control& control, bool enabled) { control.m_sndcpSupport = enabled; } + +/* Tests whether the P25 voice channel holds an SNDCP data permit. */ + +bool HostTestHooks::p25PermittedData(const p25::Control& control) { return control.m_dataChPermitHold.isRunning(); } + +/* Advances the P25 data channel keep-alive and permit hold timers. */ + +void HostTestHooks::p25HandleDataChannelKeepAlive(p25::Control& control, uint32_t ms) +{ + control.handleDataChannelKeepAlive(ms); +} + +/* Sets the remaining P25 native-modem PDU pacing guard. */ + +void HostTestHooks::p25SetRFPDUPacing(p25::Control& control, uint32_t ms) +{ + std::lock_guard lock(control.m_data->m_pduPacingMutex); + control.m_data->m_pduPacingRemainingMs = ms; +} + /* Queues a tagged DMR frame for transmission to the modem. */ void HostTestHooks::dmrAddFrame(dmr::Control& control, uint32_t slotNo, const uint8_t* data) @@ -609,6 +633,13 @@ bool HostTestHooks::p25AddFrame(p25::Control& control, const uint8_t* data, uint return control.addFrame(data, length); } +/* Copies the encoded data block that follows a response header out of a received RF PDU. */ + +bool HostTestHooks::p25ResponseDataBlock(const uint8_t* rfPDU, uint32_t rfPDUBits, uint8_t* block) +{ + return p25::packet::Data::getResponseDataBlock(rfPDU, rfPDUBits, block); +} + /* Queues a tagged NXDN frame for transmission to the modem. */ void HostTestHooks::nxdnAddFrame(nxdn::Control& control, const uint8_t* data) diff --git a/tests/host/HostTestHooks.h b/tests/host/HostTestHooks.h index c5e7315f..a802c14c 100644 --- a/tests/host/HostTestHooks.h +++ b/tests/host/HostTestHooks.h @@ -388,6 +388,30 @@ public: * @param ms Elapsed milliseconds. */ static void p25ClockU2U(p25::Control& control, uint32_t ms); + /** + * @brief Sets whether P25 SNDCP support is enabled. + * @param control P25 control instance. + * @param enabled True to enable SNDCP support; otherwise, false. + */ + static void p25SetSNDCPSupport(p25::Control& control, bool enabled); + /** + * @brief Tests whether the P25 voice channel holds an SNDCP data permit. + * @param control P25 control instance. + * @returns bool True if a data permit is held; otherwise, false. + */ + static bool p25PermittedData(const p25::Control& control); + /** + * @brief Advances the P25 data channel keep-alive and permit hold timers. + * @param control P25 control instance. + * @param ms Elapsed milliseconds. + */ + static void p25HandleDataChannelKeepAlive(p25::Control& control, uint32_t ms); + /** + * @brief Sets the remaining P25 native-modem PDU pacing guard. + * @param control P25 control instance. + * @param ms Remaining pacing guard in milliseconds (0 clears it). + */ + static void p25SetRFPDUPacing(p25::Control& control, uint32_t ms); /** * @brief Queues a tagged DMR frame for transmission to the modem. * @param control DMR control instance. @@ -403,6 +427,14 @@ public: * @returns bool True if the frame was queued; otherwise, false. */ static bool p25AddFrame(p25::Control& control, const uint8_t* data, uint32_t length); + /** + * @brief Copies the encoded data block that follows a response header out of a received RF PDU. + * @param[in] rfPDU Received RF PDU bits, starting with the preamble. + * @param rfPDUBits Number of valid bits held in rfPDU. + * @param[out] block Buffer of at least P25_PDU_FEC_LENGTH_BYTES receiving the encoded block. + * @returns bool True if the data block was received and copied; otherwise, false. + */ + static bool p25ResponseDataBlock(const uint8_t* rfPDU, uint32_t rfPDUBits, uint8_t* block); /** * @brief Queues a tagged NXDN frame for transmission to the modem. * @param control NXDN control instance. diff --git a/tests/p25/HostControl_Tests.cpp b/tests/p25/HostControl_Tests.cpp index b7e07155..99525138 100644 --- a/tests/p25/HostControl_Tests.cpp +++ b/tests/p25/HostControl_Tests.cpp @@ -17,6 +17,7 @@ #include "common/network/NetRPC.h" #include "common/network/Network.h" #include "common/edac/CRC.h" +#include "common/Utils.h" #include "common/p25/P25Defines.h" #include "common/p25/data/LowSpeedData.h" #include "common/p25/lc/LC.h" @@ -2001,6 +2002,239 @@ TEST_CASE("P25 non-authoritative unpermit clears active network state", "[p25][h REQUIRE_FALSE(HostTestHooks::p25NetworkWatchdog(*harness.m_control).isRunning()); } +/** + * @brief Drains every queued RF frame and counts the TDU frames among them. + * @param control P25 control instance. + * @param[out] tduCount Number of drained TDU frames. + * @returns uint32_t Total number of drained frames. + */ +uint32_t drainRFFrames(p25::Control& control, uint32_t& tduCount) +{ + uint8_t frame[p25::defines::P25_PDU_FRAME_LENGTH_BYTES + 2U] = { 0U }; + uint32_t total = 0U; + tduCount = 0U; + + uint32_t len = 0U; + while ((len = control.getFrame(frame)) > 0U) { + total++; + if (len == p25::defines::P25_TDU_FRAME_LENGTH_BYTES + 2U && frame[0U] == modem::TAG_EOT) + tduCount++; + } + + return total; +} + +TEST_CASE("P25 non-authoritative SNDCP data permit keys an idle data channel", "[p25][host][control][sndcp][data-channel]") +{ + P25HostHarness harness(false, false, 0U, 1U, false, false, true); + HostTestHooks::p25SetSNDCPSupport(*harness.m_control, true); + + harness.m_control->permittedTG(9051U, true); + REQUIRE(HostTestHooks::p25PermittedData(*harness.m_control)); + + uint32_t tduCount = 0U; + drainRFFrames(*harness.m_control, tduCount); + + // first keep-alive tick on an idle channel emits one outbound TDU + HostTestHooks::p25HandleDataChannelKeepAlive(*harness.m_control, 20U); + REQUIRE(drainRFFrames(*harness.m_control, tduCount) == 1U); + REQUIRE(tduCount == 1U); + + // the channel stays keyed on every following tick while the permit is held + HostTestHooks::p25HandleDataChannelKeepAlive(*harness.m_control, 20U); + REQUIRE(drainRFFrames(*harness.m_control, tduCount) == 1U); + REQUIRE(tduCount == 1U); +} + +TEST_CASE("P25 SNDCP data channel keep-alive does not stack behind queued frames", "[p25][host][control][sndcp][data-channel]") +{ + P25HostHarness harness(false, false, 0U, 1U, false, false, true); + HostTestHooks::p25SetSNDCPSupport(*harness.m_control, true); + harness.m_control->permittedTG(9051U, true); + + uint32_t tduCount = 0U; + drainRFFrames(*harness.m_control, tduCount); + + // leave the first keep-alive TDU in the queue; later ticks must not add more + HostTestHooks::p25HandleDataChannelKeepAlive(*harness.m_control, 20U); + HostTestHooks::p25HandleDataChannelKeepAlive(*harness.m_control, 20U); + HostTestHooks::p25HandleDataChannelKeepAlive(*harness.m_control, 20U); + REQUIRE(drainRFFrames(*harness.m_control, tduCount) == 1U); +} + +TEST_CASE("P25 SNDCP data channel keep-alive yields to RF traffic", "[p25][host][control][sndcp][data-channel]") +{ + P25HostHarness harness(false, false, 0U, 1U, false, false, true); + HostTestHooks::p25SetSNDCPSupport(*harness.m_control, true); + harness.m_control->permittedTG(9051U, true); + + uint32_t tduCount = 0U; + drainRFFrames(*harness.m_control, tduCount); + + HostTestHooks::p25SetRFCall(*harness.m_control, 9051U, 9051U); + HostTestHooks::p25HandleDataChannelKeepAlive(*harness.m_control, 20U); + REQUIRE(drainRFFrames(*harness.m_control, tduCount) == 0U); +} + +TEST_CASE("P25 SNDCP data channel keep-alive ends for the rest of the grant once the SU transmits", "[p25][host][control][sndcp][data-channel]") +{ + P25HostHarness harness(false, false, 0U, 1U, false, false, true); + HostTestHooks::p25SetSNDCPSupport(*harness.m_control, true); + harness.m_control->permittedTG(9051U, true); + + uint32_t tduCount = 0U; + drainRFFrames(*harness.m_control, tduCount); + + HostTestHooks::p25HandleDataChannelKeepAlive(*harness.m_control, 20U); + REQUIRE(drainRFFrames(*harness.m_control, tduCount) == 1U); + + // the SU keys up on the grant + HostTestHooks::p25SetRFCall(*harness.m_control, 9051U, 9051U); + HostTestHooks::p25HandleDataChannelKeepAlive(*harness.m_control, 20U); + REQUIRE(drainRFFrames(*harness.m_control, tduCount) == 0U); + + // after the SU unkeys the channel stays unkeyed while the grant is still held + HostTestHooks::p25ClearRFCall(*harness.m_control); + for (uint32_t i = 0U; i < 10U; i++) { + HostTestHooks::p25HandleDataChannelKeepAlive(*harness.m_control, 20U); + REQUIRE(drainRFFrames(*harness.m_control, tduCount) == 0U); + } + REQUIRE(HostTestHooks::p25PermittedData(*harness.m_control)); + + // a new data grant keys the idle channel again + harness.m_control->permittedTG(0U, false); + drainRFFrames(*harness.m_control, tduCount); + harness.m_control->permittedTG(9051U, true); + drainRFFrames(*harness.m_control, tduCount); + HostTestHooks::p25HandleDataChannelKeepAlive(*harness.m_control, 20U); + REQUIRE(drainRFFrames(*harness.m_control, tduCount) == 1U); + REQUIRE(tduCount == 1U); +} + +TEST_CASE("P25 SNDCP data channel keep-alive does not fill the native-modem PDU pacing guard", "[p25][host][control][sndcp][data-channel]") +{ + P25HostHarness harness(false, false, 0U, 1U, false, false, true); + HostTestHooks::p25SetSNDCPSupport(*harness.m_control, true); + harness.m_control->permittedTG(9051U, true); + + uint32_t tduCount = 0U; + drainRFFrames(*harness.m_control, tduCount); + + // a PDU was just transmitted (or is held); the guard must elapse on its own + HostTestHooks::p25SetRFPDUPacing(*harness.m_control, 500U); + for (uint32_t i = 0U; i < 5U; i++) { + HostTestHooks::p25HandleDataChannelKeepAlive(*harness.m_control, 20U); + REQUIRE(drainRFFrames(*harness.m_control, tduCount) == 0U); + } + + // once the guard has elapsed the idle channel is kept keyed again + HostTestHooks::p25SetRFPDUPacing(*harness.m_control, 0U); + HostTestHooks::p25HandleDataChannelKeepAlive(*harness.m_control, 20U); + REQUIRE(drainRFFrames(*harness.m_control, tduCount) == 1U); + REQUIRE(tduCount == 1U); +} + +TEST_CASE("P25 SNDCP data channel keep-alive stops when the control channel releases the grant", "[p25][host][control][sndcp][data-channel]") +{ + P25HostHarness harness(false, false, 0U, 1U, false, false, true); + HostTestHooks::p25SetSNDCPSupport(*harness.m_control, true); + harness.m_control->permittedTG(9051U, true); + + harness.m_control->permittedTG(0U); + REQUIRE_FALSE(HostTestHooks::p25PermittedData(*harness.m_control)); + + uint32_t tduCount = 0U; + drainRFFrames(*harness.m_control, tduCount); + + HostTestHooks::p25HandleDataChannelKeepAlive(*harness.m_control, 20U); + REQUIRE(drainRFFrames(*harness.m_control, tduCount) == 0U); +} + +TEST_CASE("P25 SNDCP data channel permit hold expires without a control channel release", "[p25][host][control][sndcp][data-channel]") +{ + P25HostHarness harness(false, false, 0U, 1U, false, false, true); + HostTestHooks::p25SetSNDCPSupport(*harness.m_control, true); + harness.m_control->permittedTG(9051U, true); + + uint32_t tduCount = 0U; + drainRFFrames(*harness.m_control, tduCount); + + // 30 second failsafe; one second past it the hold must release + HostTestHooks::p25HandleDataChannelKeepAlive(*harness.m_control, 31000U); + REQUIRE_FALSE(HostTestHooks::p25PermittedData(*harness.m_control)); + REQUIRE(drainRFFrames(*harness.m_control, tduCount) == 0U); + + HostTestHooks::p25HandleDataChannelKeepAlive(*harness.m_control, 20U); + REQUIRE(drainRFFrames(*harness.m_control, tduCount) == 0U); +} + +TEST_CASE("P25 repeated SNDCP data permit refreshes the permit hold", "[p25][host][control][sndcp][data-channel]") +{ + P25HostHarness harness(false, false, 0U, 1U, false, false, true); + HostTestHooks::p25SetSNDCPSupport(*harness.m_control, true); + + harness.m_control->permittedTG(9051U, true); + HostTestHooks::p25HandleDataChannelKeepAlive(*harness.m_control, 25000U); + harness.m_control->permittedTG(9051U, true); + HostTestHooks::p25HandleDataChannelKeepAlive(*harness.m_control, 25000U); + + REQUIRE(HostTestHooks::p25PermittedData(*harness.m_control)); +} + +TEST_CASE("P25 voice permits and authoritative hosts never key a data channel keep-alive", "[p25][host][control][sndcp][data-channel]") +{ + uint32_t tduCount = 0U; + + SECTION("voice permit") + { + P25HostHarness harness(false, false, 0U, 1U, false, false, true); + HostTestHooks::p25SetSNDCPSupport(*harness.m_control, true); + harness.m_control->permittedTG(2001U); + drainRFFrames(*harness.m_control, tduCount); + + HostTestHooks::p25HandleDataChannelKeepAlive(*harness.m_control, 20U); + REQUIRE_FALSE(HostTestHooks::p25PermittedData(*harness.m_control)); + REQUIRE(drainRFFrames(*harness.m_control, tduCount) == 0U); + } + + SECTION("SNDCP support disabled") + { + P25HostHarness harness(false, false, 0U, 1U, false, false, true); + harness.m_control->permittedTG(9051U, true); + drainRFFrames(*harness.m_control, tduCount); + + HostTestHooks::p25HandleDataChannelKeepAlive(*harness.m_control, 20U); + REQUIRE_FALSE(HostTestHooks::p25PermittedData(*harness.m_control)); + REQUIRE(drainRFFrames(*harness.m_control, tduCount) == 0U); + } + + SECTION("SNDCP support disabled while a data permit is held") + { + P25HostHarness harness(false, false, 0U, 1U, false, false, true); + HostTestHooks::p25SetSNDCPSupport(*harness.m_control, true); + harness.m_control->permittedTG(9051U, true); + drainRFFrames(*harness.m_control, tduCount); + REQUIRE(HostTestHooks::p25PermittedData(*harness.m_control)); + + // the handler checks SNDCP support itself and does not rely on its caller + HostTestHooks::p25SetSNDCPSupport(*harness.m_control, false); + HostTestHooks::p25HandleDataChannelKeepAlive(*harness.m_control, 20U); + REQUIRE(drainRFFrames(*harness.m_control, tduCount) == 0U); + } + + SECTION("authoritative host") + { + P25HostHarness harness(true, false, 0U, 1U, false, false, true); + HostTestHooks::p25SetSNDCPSupport(*harness.m_control, true); + harness.m_control->permittedTG(9051U, true); + drainRFFrames(*harness.m_control, tduCount); + + HostTestHooks::p25HandleDataChannelKeepAlive(*harness.m_control, 20U); + REQUIRE_FALSE(HostTestHooks::p25PermittedData(*harness.m_control)); + REQUIRE(drainRFFrames(*harness.m_control, tduCount) == 0U); + } +} + TEST_CASE("P25 telephone interconnect permit survives voice hang until explicitly released", "[p25][host][control][telephone]") { P25HostHarness harness(false); @@ -2184,6 +2418,34 @@ TEST_CASE("P25 grant authority follows configuration and FNE availability", REQUIRE(local.m_control->isGrantAuthoritative()); } +TEST_CASE("P25 response data block is copied from behind the response header", "[p25][host][pdu][response]") +{ + using namespace p25::defines; + + // preamble, header block, then the response data block (e.g. selective retry flags) + std::vector rfPDU(P25_PDU_FRAME_LENGTH_BYTES + 2U, 0x00U); + + uint8_t expected[P25_PDU_FEC_LENGTH_BYTES]; + for (uint32_t i = 0U; i < P25_PDU_FEC_LENGTH_BYTES; i++) + expected[i] = uint8_t(0xA5U ^ (i * 7U)); + // the last octet only carries the bits that fit in the encoded block + const uint32_t tailBits = P25_PDU_FEC_LENGTH_BITS % 8U; + if (tailBits != 0U) + expected[P25_PDU_FEC_LENGTH_BYTES - 1U] &= uint8_t(0xFFU << (8U - tailBits)); + + Utils::setBitRange(expected, rfPDU.data(), P25_PREAMBLE_LENGTH_BITS + P25_PDU_FEC_LENGTH_BITS, P25_PDU_FEC_LENGTH_BITS); + + const uint32_t completeBits = P25_PREAMBLE_LENGTH_BITS + (2U * P25_PDU_FEC_LENGTH_BITS); + + uint8_t block[P25_PDU_FEC_LENGTH_BYTES]; + REQUIRE(HostTestHooks::p25ResponseDataBlock(rfPDU.data(), completeBits, block)); + CHECK(::memcmp(block, expected, P25_PDU_FEC_LENGTH_BYTES) == 0); + + // a response whose data block has not been received yet must not be forwarded as one + CHECK_FALSE(HostTestHooks::p25ResponseDataBlock(rfPDU.data(), completeBits - 1U, block)); + CHECK_FALSE(HostTestHooks::p25ResponseDataBlock(rfPDU.data(), P25_PREAMBLE_LENGTH_BITS + P25_PDU_FEC_LENGTH_BITS, block)); +} + TEST_CASE("P25 frame types wait for their complete variable-length modem space", "[p25][host][control][queue][pacing]") { using namespace p25::defines;