diff --git a/src/bridge/HostBridge.NXDN.cpp b/src/bridge/HostBridge.NXDN.cpp index 1169b874..f18f10ec 100644 --- a/src/bridge/HostBridge.NXDN.cpp +++ b/src/bridge/HostBridge.NXDN.cpp @@ -79,21 +79,6 @@ void HostBridge::processNXDNNetwork(uint8_t* buffer, uint32_t length) return; } - uint8_t frameLength = buffer[23U]; - if (frameLength == 0U) { - m_network->resetNXDN(); - return; - } - - uint32_t payloadLength = frameLength; - if (payloadLength > NXDN_FRAME_LENGTH_BYTES && payloadLength >= 24U) - payloadLength -= 24U; - - if (payloadLength < NXDN_FRAME_LENGTH_BYTES) { - m_network->resetNXDN(); - return; - } - uint8_t frame[NXDN_FRAME_LENGTH_BYTES + 2U]; ::memset(frame, 0x00U, NXDN_FRAME_LENGTH_BYTES + 2U); ::memcpy(frame + 2U, buffer + 24U, NXDN_FRAME_LENGTH_BYTES); @@ -428,9 +413,25 @@ void HostBridge::encodeNXDNAudioFrame(uint8_t* pcm, uint32_t forcedSrcId, uint32 lich.encode(voiceFrame + 2U); channel::SACCH sacch; - sacch.setData(SACCH_IDLE); + uint8_t lcData[NXDN_RTCH_LC_LENGTH_BYTES]; + ::memset(lcData, 0x00U, sizeof(lcData)); + lc.encode(lcData, NXDN_RTCH_LC_LENGTH_BITS); + + const uint8_t superframeIndex = m_nxdnSeqNo % 4U; + const ChStructure::E structures[] = { + ChStructure::SR_1_4, ChStructure::SR_2_4, + ChStructure::SR_3_4, ChStructure::SR_4_4 + }; + + uint8_t sacchData[3U]; + ::memset(sacchData, 0x00U, sizeof(sacchData)); + for (uint32_t bit = 0U; bit < 18U; bit++) { + WRITE_BIT(sacchData, bit, READ_BIT(lcData, superframeIndex * 18U + bit)); + } + + sacch.setData(sacchData); sacch.setRAN(0U); - sacch.setStructure(ChStructure::SR_SINGLE); + sacch.setStructure(structures[superframeIndex]); sacch.encode(voiceFrame + 2U); ::memcpy(voiceFrame + 2U + NXDN_FSW_LICH_SACCH_LENGTH_BYTES, nxdnAudioPayload, 36U); diff --git a/src/bridge/HostBridge.cpp b/src/bridge/HostBridge.cpp index 731148bf..72f5eaf2 100644 --- a/src/bridge/HostBridge.cpp +++ b/src/bridge/HostBridge.cpp @@ -17,6 +17,11 @@ #include "common/dmr/DMRDefines.h" #include "common/dmr/data/EMB.h" #include "common/nxdn/NXDNDefines.h" +#include "common/nxdn/NXDNUtils.h" +#include "common/nxdn/Sync.h" +#include "common/nxdn/channel/FACCH1.h" +#include "common/nxdn/channel/LICH.h" +#include "common/nxdn/channel/SACCH.h" #include "common/p25/P25Defines.h" #include "common/p25/data/LowSpeedData.h" #include "common/p25/dfsi/DFSIDefines.h" @@ -1840,6 +1845,9 @@ void HostBridge::callEnd(uint32_t srcId, uint32_t dstId) break; case TX_MODE_NXDN: { + using namespace nxdn; + using namespace nxdn::defines; + ::nxdn::lc::RTCH lc = ::nxdn::lc::RTCH(); lc.setMessageType(NXDDEF::MessageType::RTCH_TX_REL); lc.setCallType(NXDDEF::CallType::UNSPECIFIED); @@ -1848,19 +1856,42 @@ void HostBridge::callEnd(uint32_t srcId, uint32_t dstId) lc.setDstId((uint16_t)dstId); lc.setTransmissionMode(NXDDEF::TransmissionMode::MODE_4800); - uint8_t data[NXDDEF::NXDN_FRAME_LENGTH_BYTES]; - ::memset(data, 0x00U, NXDDEF::NXDN_FRAME_LENGTH_BYTES); + uint8_t data[NXDN_FRAME_LENGTH_BYTES]; + ::memset(data, 0x00U, sizeof(data)); + Sync::addNXDNSync(data); + + channel::LICH lich; + lich.setRFCT(RFChannelType::RDCH); + lich.setFCT(FuncChannelType::USC_SACCH_NS); + lich.setOption(ChOption::STEAL_FACCH); + lich.setOutbound(true); + lich.encode(data); + + channel::SACCH sacch; + sacch.setData(SACCH_IDLE); + sacch.setRAN(0U); + sacch.setStructure(ChStructure::SR_SINGLE); + sacch.encode(data); + + uint8_t lcData[NXDN_RTCH_LC_LENGTH_BYTES]; + ::memset(lcData, 0x00U, sizeof(lcData)); + lc.encode(lcData, NXDN_RTCH_LC_LENGTH_BITS); + + channel::FACCH1 facch; + facch.setData(lcData); + facch.encode(data, NXDN_FSW_LENGTH_BITS + NXDN_LICH_LENGTH_BITS + NXDN_SACCH_FEC_LENGTH_BITS); + facch.encode(data, NXDN_FSW_LENGTH_BITS + NXDN_LICH_LENGTH_BITS + NXDN_SACCH_FEC_LENGTH_BITS + NXDN_FACCH1_FEC_LENGTH_BITS); + + NXDNUtils::scrambler(data); LogInfoEx(LOG_HOST, "NXDN, " NXDN_RTCH_MSG_TYPE_TX_REL ", srcId = %u, dstId = %u", srcId, dstId); - m_network->writeNXDN(lc, data, NXDDEF::NXDN_FRAME_LENGTH_BYTES, true); + m_network->writeNXDN(lc, data, NXDN_FRAME_LENGTH_BYTES, true); } break; case TX_MODE_ANALOG: { LogInfoEx(LOG_HOST, ANO_TERMINATOR); - uint8_t controlByte = 0x00U; - data::NetData analogData; analogData.setSeqNo(m_analogN); analogData.setSrcId(srcId); @@ -2980,7 +3011,43 @@ void HostBridge::padSilenceAudio(uint32_t srcId, uint32_t dstId) LogInfoEx(LOG_HOST, "NXDN, " NXDN_RTCH_MSG_TYPE_VCALL ", audio (silence), srcId = %u, dstId = %u", srcId, dstId); - m_network->writeNXDN(lc, m_nxdnAMBE, 36U); + uint8_t frame[NXDN_FRAME_LENGTH_BYTES]; + ::memset(frame, 0x00U, sizeof(frame)); + Sync::addNXDNSync(frame); + + channel::LICH lich; + lich.setRFCT(RFChannelType::RDCH); + lich.setFCT(FuncChannelType::USC_SACCH_SS); + lich.setOption(ChOption::STEAL_NONE); + lich.setOutbound(true); + lich.encode(frame); + + uint8_t lcData[NXDN_RTCH_LC_LENGTH_BYTES]; + ::memset(lcData, 0x00U, sizeof(lcData)); + lc.encode(lcData, NXDN_RTCH_LC_LENGTH_BITS); + + const ChStructure::E structures[] = { + ChStructure::SR_1_4, ChStructure::SR_2_4, + ChStructure::SR_3_4, ChStructure::SR_4_4 + }; + const uint8_t superframeIndex = m_nxdnSeqNo % 4U; + uint8_t sacchData[3U]; + ::memset(sacchData, 0x00U, sizeof(sacchData)); + for (uint32_t bit = 0U; bit < 18U; bit++) { + WRITE_BIT(sacchData, bit, READ_BIT(lcData, superframeIndex * 18U + bit)); + } + + channel::SACCH sacch; + sacch.setData(sacchData); + sacch.setRAN(0U); + sacch.setStructure(structures[superframeIndex]); + sacch.encode(frame); + + ::memcpy(frame + NXDN_FSW_LICH_SACCH_LENGTH_BYTES, m_nxdnAMBE, 36U); + NXDNUtils::scrambler(frame); + + m_network->writeNXDN(lc, frame, NXDN_FRAME_LENGTH_BYTES); + m_nxdnSeqNo++; m_nxdnN = 1U; } } diff --git a/src/common/network/BaseNetwork.cpp b/src/common/network/BaseNetwork.cpp index 51f0f5ed..67042823 100644 --- a/src/common/network/BaseNetwork.cpp +++ b/src/common/network/BaseNetwork.cpp @@ -1704,6 +1704,9 @@ UInt8Array BaseNetwork::createNXDN_Message(uint32_t& length, const nxdn::lc::RTC { assert(data != nullptr); + using namespace nxdn::defines; + length = 0U; + uint8_t* buffer = new uint8_t[NXDN_PACKET_LENGTH + PACKET_PAD]; ::memset(buffer, 0x00U, NXDN_PACKET_LENGTH + PACKET_PAD); @@ -1723,12 +1726,8 @@ UInt8Array BaseNetwork::createNXDN_Message(uint32_t& length, const nxdn::lc::RTC buffer[15U] |= lc.getGroup() ? 0x00U : 0x40U; // Group // pack raw NXDN message bytes - uint32_t count = MSG_HDR_SIZE; - ::memcpy(buffer + 24U, data, len); - count += len; - - buffer[23U] = count; + buffer[23U] = (uint8_t)len; if (m_packetDump) Utils::dump(1U, "BaseNetwork::createNXDN_Message(), Message", buffer, (NXDN_PACKET_LENGTH + PACKET_PAD)); diff --git a/src/common/network/BaseNetwork.h b/src/common/network/BaseNetwork.h index 6ab59ab3..fc10ca9e 100644 --- a/src/common/network/BaseNetwork.h +++ b/src/common/network/BaseNetwork.h @@ -93,7 +93,7 @@ namespace network const uint32_t P25_TSDU_PACKET_LENGTH = 69U; // 24 byte header + TSDU data const uint32_t P25_TDULC_PACKET_LENGTH = 78U; // 24 byte header + TDULC data const uint32_t P25_P2_PACKET_LENGTH = 66U; // 24 byte header + P25_P2_FRAME_LENGTH_BYTES + 2 byte trailer - const uint32_t NXDN_PACKET_LENGTH = 70U; // 20 byte header + NXDN_FRAME_LENGTH_BYTES + 2 byte trailer + const uint32_t NXDN_PACKET_LENGTH = 74U; // 24 byte header + NXDN CAI frame + 2 byte trailer const uint32_t ANALOG_PACKET_LENGTH = 344U; // 20 byte header + AUDIO_SAMPLES_LENGTH_BYTES + 4 byte trailer const uint32_t HA_PARAMS_ENTRY_LEN = 20U; diff --git a/src/common/network/packet/Protocol.cpp b/src/common/network/packet/Protocol.cpp index 01052573..d89eb37e 100644 --- a/src/common/network/packet/Protocol.cpp +++ b/src/common/network/packet/Protocol.cpp @@ -9,6 +9,7 @@ */ #include "Defines.h" #include "common/Log.h" +#include "common/nxdn/NXDNDefines.h" #include "network/Network.h" using namespace network; @@ -377,8 +378,11 @@ bool Network::PacketHandler::protocol(Network* network, uint32_t peerId, uint32_ if (network->m_packetDump) Utils::dump(1U, "Network::clock(), Network Rx, NXDN", buffer, length); - if (length > (int)(NXDN_PACKET_LENGTH + PACKET_PAD)) - LogError(LOG_NET, "NXDN Stream %u, frame oversized? this shouldn't happen, pktSeq = %u, len = %u", streamId, network->m_pktSeq, length); + if (length > (int)(NXDN_PACKET_LENGTH + PACKET_PAD)) { + LogWarning(LOG_NET, "NXDN Stream %u, rejecting oversized frame, pktSeq = %u, len = %u", + streamId, network->m_pktSeq, length); + break; + } uint8_t len = length; network->m_rxNXDNData.addData(&len, 1U); diff --git a/src/common/nxdn/lc/RTCH.cpp b/src/common/nxdn/lc/RTCH.cpp index e558303b..e1d74807 100644 --- a/src/common/nxdn/lc/RTCH.cpp +++ b/src/common/nxdn/lc/RTCH.cpp @@ -171,6 +171,23 @@ void RTCH::reset() m_kId = 0U; m_causeRsp = CauseResponse::VD_ACCEPTED; + ::memset(m_mi, 0x00U, MI_LENGTH_BYTES); +} + +/* Gets the 64-bit on-air initialization vector. */ + +void RTCH::getMI(uint8_t* mi) const +{ + assert(mi != nullptr); + ::memcpy(mi, m_mi, MI_LENGTH_BYTES); +} + +/* Sets the 64-bit on-air initialization vector. */ + +void RTCH::setMI(const uint8_t* mi) +{ + assert(mi != nullptr); + ::memcpy(m_mi, mi, MI_LENGTH_BYTES); } // --------------------------------------------------------------------------- @@ -201,14 +218,11 @@ bool RTCH::decodeLC(const uint8_t* data) m_dstId = (uint16_t)((data[5U] << 8) | data[6U]) & 0xFFFFU; // Target Radio Address m_algId = (data[7U] >> 6) & 0x03U; // Cipher Type m_kId = (data[7U] & 0x3FU); // Key ID + m_encrypted = m_algId != CIPHER_TYPE_NONE; break; case MessageType::RTCH_VCALL_IV: case MessageType::RTCH_SDCALL_IV: - if (m_algId != CIPHER_TYPE_NONE && m_kId > 0U) { - m_mi = new uint8_t[MI_LENGTH_BYTES]; - ::memset(m_mi, 0x00U, MI_LENGTH_BYTES); - ::memcpy(m_mi, data + 1U, MI_LENGTH_BYTES); // Message Indicator - } + ::memcpy(m_mi, data + 1U, MI_LENGTH_BYTES); // Initialization Vector break; case MessageType::RTCH_TX_REL: case MessageType::RTCH_TX_REL_EX: @@ -228,6 +242,7 @@ bool RTCH::decodeLC(const uint8_t* data) m_dstId = (uint16_t)((data[5U] << 8) | data[6U]) & 0xFFFFU; // Target Radio Address m_algId = (data[7U] >> 6) & 0x03U; // Cipher Type m_kId = (data[7U] & 0x3FU); // Key ID + m_encrypted = m_algId != CIPHER_TYPE_NONE; m_packetInfo = PacketInformation(); m_packetInfo.decode(m_messageType, data + 8U); // Packet Information @@ -328,9 +343,8 @@ void RTCH::encodeLC(uint8_t* data) (m_kId & 0x3FU); // Key ID break; case MessageType::RTCH_VCALL_IV: - if (m_algId != CIPHER_TYPE_NONE && m_kId > 0U) { - ::memcpy(data + 1U, m_mi, MI_LENGTH_BYTES); // Message Indicator - } + case MessageType::RTCH_SDCALL_IV: + ::memcpy(data + 1U, m_mi, MI_LENGTH_BYTES); // Initialization Vector break; case MessageType::RTCH_TX_REL: case MessageType::RTCH_TX_REL_EX: diff --git a/src/common/nxdn/lc/RTCH.h b/src/common/nxdn/lc/RTCH.h index bd8d1e0d..9feae8e6 100644 --- a/src/common/nxdn/lc/RTCH.h +++ b/src/common/nxdn/lc/RTCH.h @@ -78,6 +78,21 @@ namespace nxdn */ void reset(); + /** @name Encryption data */ + /** + * @brief Gets the 64-bit on-air initialization vector. + * @param mi Pointer to the 64-bit initialization vector. + */ + void getMI(uint8_t* mi) const; + + /** + * @brief Sets the 64-bit on-air initialization vector carried by + * VCALL_IV/SDCALL_IV. + * @param mi Pointer to the 64-bit initialization vector. + */ + void setMI(const uint8_t* mi); + /** @} */ + /** * @brief Sets the flag indicating verbose log output. * @param verbose Flag indicating verbose log output. diff --git a/src/fne/network/callhandler/TagNXDNData.cpp b/src/fne/network/callhandler/TagNXDNData.cpp index cc50430e..f749d47d 100644 --- a/src/fne/network/callhandler/TagNXDNData.cpp +++ b/src/fne/network/callhandler/TagNXDNData.cpp @@ -95,17 +95,10 @@ bool TagNXDNData::processFrame(const uint8_t* data, uint32_t len, uint32_t peerI lc.setGroup(group); // process raw NXDN data bytes - UInt8Array frame; - uint8_t frameLength = buffer[23U]; - if (frameLength <= 24) { - frame = std::unique_ptr(new uint8_t[frameLength]); - ::memset(frame.get(), 0x00U, frameLength); - } - else { - frame = std::unique_ptr(new uint8_t[frameLength]); - ::memset(frame.get(), 0x00U, frameLength); - ::memcpy(frame.get(), buffer + 24U, frameLength); - } + const uint8_t frameLength = NXDN_FRAME_LENGTH_BYTES + 2U; + UInt8Array frame = std::unique_ptr(new uint8_t[frameLength]); + ::memset(frame.get(), 0x00U, frameLength); + ::memcpy(frame.get() + 2U, buffer + MSG_HDR_SIZE, NXDN_FRAME_LENGTH_BYTES); NXDNUtils::scrambler(frame.get() + 2U); @@ -1255,7 +1248,7 @@ bool TagNXDNData::write_Message_Grant(uint32_t peerId, uint32_t srcId, uint32_t } } - rcch->setMessageType(MessageType::RTCH_VCALL); + rcch->setMessageType(MessageType::RCCH_VCALL_CONN); rcch->setGrpVchNo(0U); rcch->setGroup(grp); rcch->setSrcId(srcId); @@ -1283,7 +1276,8 @@ void TagNXDNData::write_Message_Deny(uint32_t peerId, uint32_t srcId, uint32_t d switch (service) { case MessageType::RTCH_VCALL: rcch = std::make_unique(); - rcch->setMessageType(MessageType::RTCH_VCALL); + rcch->setMessageType(MessageType::RCCH_VCALL_CONN); + break; default: return; } @@ -1338,7 +1332,7 @@ void TagNXDNData::write_Message(uint32_t peerId, lc::RCCH* rcch) lc.setDstId(rcch->getDstId()); uint32_t messageLength = 0U; - UInt8Array message = m_network->createNXDN_Message(messageLength, lc, data, NXDN_FRAME_LENGTH_BYTES + 2U); + UInt8Array message = m_network->createNXDN_Message(messageLength, lc, data + 2U, NXDN_FRAME_LENGTH_BYTES); if (message == nullptr) { return; } diff --git a/src/fne/network/packet/Protocol.cpp b/src/fne/network/packet/Protocol.cpp index 4e68ffa2..ba717a88 100644 --- a/src/fne/network/packet/Protocol.cpp +++ b/src/fne/network/packet/Protocol.cpp @@ -14,6 +14,7 @@ #include "network/callhandler/TagP25Data.h" #include "network/callhandler/TagP25P2Data.h" #include "network/callhandler/TagNXDNData.h" +#include "common/nxdn/NXDNDefines.h" #include "network/callhandler/TagAnalogData.h" #include "fne/ActivityLog.h" #include "HostFNE.h" diff --git a/src/host/nxdn/Control.cpp b/src/host/nxdn/Control.cpp index abfca970..d5dd98cb 100644 --- a/src/host/nxdn/Control.cpp +++ b/src/host/nxdn/Control.cpp @@ -1012,17 +1012,10 @@ void Control::processNetwork() lc.setGroup(group); // process raw NXDN data bytes - UInt8Array data; - uint8_t frameLength = buffer[23U]; - if (frameLength <= 24) { - data = std::unique_ptr(new uint8_t[frameLength]); - ::memset(data.get(), 0x00U, frameLength); - } - else { - data = std::unique_ptr(new uint8_t[frameLength]); - ::memset(data.get(), 0x00U, frameLength); - ::memcpy(data.get(), buffer.get() + 24U, frameLength); - } + const uint8_t frameLength = NXDN_FRAME_LENGTH_BYTES + 2U; + UInt8Array data = std::unique_ptr(new uint8_t[frameLength]); + ::memset(data.get(), 0x00U, frameLength); + ::memcpy(data.get() + 2U, buffer.get() + network::MSG_HDR_SIZE, NXDN_FRAME_LENGTH_BYTES); if (m_debug) { Utils::dump(2U, "* !!! NXDN Network Frame", data.get(), frameLength); diff --git a/src/host/nxdn/packet/ControlSignaling.cpp b/src/host/nxdn/packet/ControlSignaling.cpp index 6b9429fd..cc6fcd84 100644 --- a/src/host/nxdn/packet/ControlSignaling.cpp +++ b/src/host/nxdn/packet/ControlSignaling.cpp @@ -320,7 +320,12 @@ void ControlSignaling::writeNetwork(const uint8_t *data, uint32_t len) if (m_nxdn->m_rfTimeoutTimer.isRunning() && m_nxdn->m_rfTimeoutTimer.hasExpired()) return; - m_nxdn->m_network->writeNXDN(m_nxdn->m_rfLC, data, len, true); + if (len != NXDN_FRAME_LENGTH_BYTES + 2U) { + LogError(LOG_NET, "ControlSignaling::writeNetwork(), invalid modem NXDN frame length, len = %u", len); + return; + } + + m_nxdn->m_network->writeNXDN(m_nxdn->m_rfLC, data + 2U, NXDN_FRAME_LENGTH_BYTES, true); } /* diff --git a/src/host/nxdn/packet/Data.cpp b/src/host/nxdn/packet/Data.cpp index 8cebe313..ba4b31b2 100644 --- a/src/host/nxdn/packet/Data.cpp +++ b/src/host/nxdn/packet/Data.cpp @@ -408,5 +408,10 @@ void Data::writeNetwork(const uint8_t *data, uint32_t len) if (m_nxdn->m_rfTimeoutTimer.isRunning() && m_nxdn->m_rfTimeoutTimer.hasExpired()) return; - m_nxdn->m_network->writeNXDN(m_nxdn->m_rfLC, data, len); + if (len != NXDN_FRAME_LENGTH_BYTES + 2U) { + LogError(LOG_NET, "Data::writeNetwork(), invalid modem NXDN frame length, len = %u", len); + return; + } + + m_nxdn->m_network->writeNXDN(m_nxdn->m_rfLC, data + 2U, NXDN_FRAME_LENGTH_BYTES); } diff --git a/src/host/nxdn/packet/Voice.cpp b/src/host/nxdn/packet/Voice.cpp index e0b0a37e..a34867ed 100644 --- a/src/host/nxdn/packet/Voice.cpp +++ b/src/host/nxdn/packet/Voice.cpp @@ -78,7 +78,7 @@ bool Voice::process(FuncChannelType::E fct, ChOption::E option, uint8_t* data, u facch.getData(buffer); lc::RTCH lc; - lc.decode(buffer, NXDN_FACCH1_FEC_LENGTH_BITS); + lc.decode(buffer, NXDN_FACCH1_LENGTH_BITS); uint16_t dstId = lc.getDstId(); uint16_t srcId = lc.getSrcId(); bool group = lc.getGroup(); @@ -201,6 +201,26 @@ bool Voice::process(FuncChannelType::E fct, ChOption::E option, uint8_t* data, u m_nxdn->m_rfLastSrcId = lc.getSrcId(); m_nxdn->m_rfLC = lc; + // In an encrypted first frame the second FACCH1 carries VCALL_IV. + // Preserve its 64-bit IV with the call state; VCALL_IV intentionally + // has no source/destination or cipher/key fields of its own. + if (lc.getEncrypted()) { + channel::FACCH1 ivFacch; + if (ivFacch.decode(data + 2U, NXDN_FSW_LENGTH_BITS + NXDN_LICH_LENGTH_BITS + + NXDN_SACCH_FEC_LENGTH_BITS + NXDN_FACCH1_FEC_LENGTH_BITS)) { + uint8_t ivLCData[10U]; + ::memset(ivLCData, 0x00U, sizeof(ivLCData)); + ivFacch.getData(ivLCData); + lc::RTCH ivLC; + ivLC.decode(ivLCData, NXDN_FACCH1_LENGTH_BITS); + if (ivLC.getMessageType() == MessageType::RTCH_VCALL_IV) { + uint8_t mi[MI_LENGTH_BYTES]; + ivLC.getMI(mi); + m_nxdn->m_rfLC.setMI(mi); + } + } + } + Sync::addNXDNSync(data + 2U); // generate the LICH @@ -736,6 +756,24 @@ bool Voice::processNetwork(FuncChannelType::E fct, ChOption::E option, lc::RTCH& m_nxdn->m_netLastSrcId = lc.getSrcId(); m_nxdn->m_netLC = lc; + if (lc.getEncrypted()) { + channel::FACCH1 ivFacch; + if (ivFacch.decode(data + 2U, NXDN_FSW_LENGTH_BITS + NXDN_LICH_LENGTH_BITS + + NXDN_SACCH_FEC_LENGTH_BITS + NXDN_FACCH1_FEC_LENGTH_BITS)) { + uint8_t ivLCData[10U]; + ::memset(ivLCData, 0x00U, sizeof(ivLCData)); + ivFacch.getData(ivLCData); + + lc::RTCH ivLC; + ivLC.decode(ivLCData, NXDN_FACCH1_LENGTH_BITS); + if (ivLC.getMessageType() == MessageType::RTCH_VCALL_IV) { + uint8_t mi[MI_LENGTH_BYTES]; + ivLC.getMI(mi); + m_nxdn->m_netLC.setMI(mi); + } + } + } + Sync::addNXDNSync(data + 2U); // generate the LICH @@ -758,14 +796,15 @@ bool Voice::processNetwork(FuncChannelType::E fct, ChOption::E option, lc::RTCH& NXDNUtils::scrambler(data + 2U); - if (m_nxdn->m_duplex) { - data[0U] = type == MessageType::RTCH_TX_REL ? modem::TAG_EOT : modem::TAG_DATA; - data[1U] = 0x00U; + data[0U] = type == MessageType::RTCH_TX_REL || type == MessageType::RTCH_TX_REL_EX ? + modem::TAG_EOT : modem::TAG_DATA; + data[1U] = 0x00U; + if (m_nxdn->m_duplex) { m_nxdn->addFrame(data, true); } - if (data[0U] == modem::TAG_EOT) { + if (type == MessageType::RTCH_TX_REL || type == MessageType::RTCH_TX_REL_EX) { m_netFrames++; ::ActivityLog("NXDN", false, "network end of transmission, %.1f seconds", float(m_netFrames) / 12.5F); @@ -1107,7 +1146,12 @@ void Voice::writeNetwork(const uint8_t *data, uint32_t len) if (m_nxdn->m_rfTimeoutTimer.isRunning() && m_nxdn->m_rfTimeoutTimer.hasExpired()) return; - m_nxdn->m_network->writeNXDN(m_nxdn->m_rfLC, data, len); + if (len != NXDN_FRAME_LENGTH_BYTES + 2U) { + LogError(LOG_NET, "Voice::writeNetwork(), invalid modem NXDN frame length, len = %u", len); + return; + } + + m_nxdn->m_network->writeNXDN(m_nxdn->m_rfLC, data + 2U, NXDN_FRAME_LENGTH_BYTES); } /* Helper to perform RF traffic collision checking. */ diff --git a/tests/nxdn/HostControl_Tests.cpp b/tests/nxdn/HostControl_Tests.cpp index c3d2caa9..3b9421e6 100644 --- a/tests/nxdn/HostControl_Tests.cpp +++ b/tests/nxdn/HostControl_Tests.cpp @@ -151,22 +151,37 @@ public: void close() override {} }; +// --------------------------------------------------------------------------- +// Class Declaration +// --------------------------------------------------------------------------- + /** * @brief Lightweight network test double for NXDN ingress stream-lock tests. */ class NXDNTestNetwork final : public network::Network { public: + /** + * @brief Initializes a new instance of the NXDNTestNetwork class. + * @param localPort The local port number to bind the network socket to. + * @param peerId The peer ID for the network connection. + */ NXDNTestNetwork(uint16_t localPort = 0U, uint32_t peerId = 1U) : network::Network("127.0.0.1", 1U, localPort, peerId, "test", true, true, false, false, true, false, true, true, false, false, false, false), m_resetNXDNCount(0U) { - // keep protocol gates deterministic for this P25-focused harness + // keep protocol gates deterministic for this NXDN-focused harness m_dmrEnabled = false; m_p25Enabled = false; m_nxdnEnabled = true; m_analogEnabled = false; } + /** + * @brief Activates loopback mode for the network connection. + * @param remoteAddress The remote address to connect to. + * @param remotePort The remote port to connect to. + * @returns bool True if loopback mode was successfully activated, false otherwise. + */ bool activateLoopback(const std::string& remoteAddress, uint16_t remotePort) { if (network::udp::Socket::lookup(remoteAddress, remotePort, m_addr, m_addrLen) != 0) { @@ -182,7 +197,18 @@ public: return true; } - bool sendNXDNFrame(uint32_t targetPeerId, uint32_t streamId, uint16_t seq, uint8_t messageType, uint16_t srcId, uint16_t dstId) + /** + * @brief Sends an NXDN control frame over the network. + * @param targetPeerId The target peer ID for the network connection. + * @param streamId The stream ID for the network connection. + * @param seq The sequence number for the network frame. + * @param messageType The message type of the NXDN control frame. + * @param srcId The source ID for the NXDN control frame. + * @param dstId The destination ID for the NXDN control frame. + * @returns bool True if the control frame was successfully sent, false otherwise. + */ + bool sendNXDNControlFrame(uint32_t targetPeerId, uint32_t streamId, uint16_t seq, + uint8_t messageType, uint16_t srcId, uint16_t dstId) { uint8_t frame[nxdn::defines::NXDN_FRAME_LENGTH_BYTES + 2U]; ::memset(frame, 0x00U, sizeof(frame)); @@ -206,8 +232,10 @@ public: sacch.encode(frame + 2U); uint8_t lcBuffer[nxdn::defines::NXDN_RTCH_LC_LENGTH_BYTES]; + ::memset(lcBuffer, 0x00U, sizeof(lcBuffer)); nxdn::lc::RTCH lc; lc.setMessageType(messageType); + lc.setCallType(nxdn::defines::CallType::UNSPECIFIED); lc.setSrcId(srcId); lc.setDstId(dstId); lc.setGroup(true); @@ -222,7 +250,8 @@ public: nxdn::NXDNUtils::scrambler(frame + 2U); uint32_t messageLength = 0U; - UInt8Array message = createNXDN_Message(messageLength, lc, frame, sizeof(frame)); + UInt8Array message = createNXDN_Message(messageLength, lc, frame + 2U, + nxdn::defines::NXDN_FRAME_LENGTH_BYTES); if (message == nullptr || messageLength == 0U) { return false; } @@ -231,17 +260,132 @@ public: { network::NET_FUNC::PROTOCOL, network::NET_SUBFUNC::PROTOCOL_SUBFUNC_NXDN }, seq, m_addr, m_addrLen); } + /** + * @brief Sends an NXDN call header over the network. + * @param targetPeerId The target peer ID for the network connection. + * @param streamId The stream ID for the network connection. + * @param seq The sequence number for the network frame. + * @param srcId The source ID for the NXDN call header. + * @param dstId The destination ID for the NXDN call header. + * @returns bool True if the call header was successfully sent, false otherwise. + */ + bool sendNXDNCallHeader(uint32_t targetPeerId, uint32_t streamId, uint16_t seq, + uint16_t srcId, uint16_t dstId) + { + return sendNXDNControlFrame(targetPeerId, streamId, seq, + nxdn::defines::MessageType::RTCH_VCALL, srcId, dstId); + } + + /** + * @brief Sends an NXDN call terminator over the network. + * @param targetPeerId The target peer ID for the network connection. + * @param streamId The stream ID for the network connection. + * @param srcId The source ID for the NXDN call terminator. + * @param dstId The destination ID for the NXDN call terminator. + * @returns bool True if the call terminator was successfully sent, false otherwise. + */ + bool sendNXDNCallTerminator(uint32_t targetPeerId, uint32_t streamId, + uint16_t srcId, uint16_t dstId) + { + return sendNXDNControlFrame(targetPeerId, streamId, RTP_END_OF_CALL_SEQ, + nxdn::defines::MessageType::RTCH_TX_REL, srcId, dstId); + } + + /** + * @brief Sends an NXDN voice frame over the network. + * @param targetPeerId The target peer ID for the network connection. + * @param streamId The stream ID for the network connection. + * @param seq The sequence number for the network frame. + * @param fragment The fragment number of the voice frame. + * @param srcId The source ID for the NXDN voice frame. + * @param dstId The destination ID for the NXDN voice frame. + * @returns bool True if the voice frame was successfully sent, false otherwise. + */ + bool sendNXDNVoiceFrame(uint32_t targetPeerId, uint32_t streamId, uint16_t seq, + uint8_t fragment, uint16_t srcId, uint16_t dstId) + { + using namespace nxdn; + using namespace nxdn::defines; + + if (fragment >= 4U) + return false; + + uint8_t frame[NXDN_FRAME_LENGTH_BYTES]; + ::memset(frame, 0x00U, sizeof(frame)); + Sync::addNXDNSync(frame); + + channel::LICH lich; + lich.setRFCT(RFChannelType::RTCH); + lich.setFCT(FuncChannelType::USC_SACCH_SS); + lich.setOption(ChOption::STEAL_NONE); + lich.setOutbound(true); + lich.encode(frame); + + lc::RTCH lc; + lc.setMessageType(MessageType::RTCH_VCALL); + lc.setSrcId(srcId); + lc.setDstId(dstId); + lc.setGroup(true); + lc.setTransmissionMode(TransmissionMode::MODE_4800); + + uint8_t lcBuffer[NXDN_RTCH_LC_LENGTH_BYTES]; + ::memset(lcBuffer, 0x00U, sizeof(lcBuffer)); + lc.encode(lcBuffer, NXDN_RTCH_LC_LENGTH_BITS); + + uint8_t sacchData[3U]; + ::memset(sacchData, 0x00U, sizeof(sacchData)); + for (uint32_t bit = 0U; bit < 18U; bit++) + WRITE_BIT(sacchData, bit, READ_BIT(lcBuffer, fragment * 18U + bit)); + + const ChStructure::E structures[] = { + ChStructure::SR_1_4, ChStructure::SR_2_4, + ChStructure::SR_3_4, ChStructure::SR_4_4 + }; + channel::SACCH sacch; + sacch.setData(sacchData); + sacch.setRAN(1U); + sacch.setStructure(structures[fragment]); + sacch.encode(frame); + + // Four null VCH payloads form a complete half-rate voice frame. + ::memcpy(frame + NXDN_FSW_LICH_SACCH_LENGTH_BYTES + 0U, NULL_AMBE, 9U); + ::memcpy(frame + NXDN_FSW_LICH_SACCH_LENGTH_BYTES + 9U, NULL_AMBE, 9U); + ::memcpy(frame + NXDN_FSW_LICH_SACCH_LENGTH_BYTES + 18U, NULL_AMBE, 9U); + ::memcpy(frame + NXDN_FSW_LICH_SACCH_LENGTH_BYTES + 27U, NULL_AMBE, 9U); + + NXDNUtils::scrambler(frame); + + uint32_t messageLength = 0U; + UInt8Array message = createNXDN_Message(messageLength, lc, frame, NXDN_FRAME_LENGTH_BYTES); + if (message == nullptr || messageLength == 0U) + return false; + + return m_frameQueue->write(message.get(), messageLength, streamId, targetPeerId, m_peerId, + { network::NET_FUNC::PROTOCOL, network::NET_SUBFUNC::PROTOCOL_SUBFUNC_NXDN }, seq, m_addr, m_addrLen); + } + + /** + * @brief Resets the NXDN network connection. + */ void resetNXDN() override { ++m_resetNXDNCount; network::Network::resetNXDN(); } + /** + * @brief Retrieves the number of times the NXDN network connection has been reset. + * @returns uint32_t The reset count for the NXDN network connection. + */ uint32_t resetNXDNCount() const { return m_resetNXDNCount; } + /** + * @brief Retrieves the stream ID of the last received NXDN frame. + * @returns uint32_t The stream ID of the last received NXDN frame. + */ uint32_t rxNXDNStreamId() const { return m_rxNXDNStreamId; @@ -292,6 +436,10 @@ public: g_RPC = nullptr; } + /** + * @brief Retrieves the NXDN test network instance associated with the harness. + * @returns NXDNTestNetwork* The NXDN test network instance. + */ NXDNTestNetwork* network() const { return m_network; @@ -328,17 +476,38 @@ public: TEST_CASE("NXDN host e2e loopback handles missed frames without dropping active call", "[nxdn][host][control][net][e2e]") { - NXDNHostHarness harness; - harness.startNetworkVoiceCall(1001U, 2001U); + const uint16_t hostPort = reserveLoopbackPort(); + const uint16_t senderPort = reserveLoopbackPort(); + REQUIRE(hostPort != 0U); + REQUIRE(senderPort != 0U); + REQUIRE(hostPort != senderPort); - nxdn::lc::RTCH followOn; - followOn.setMessageType(nxdn::defines::MessageType::RTCH_VCALL); - followOn.setSrcId(1001U); - followOn.setDstId(2001U); - followOn.setGroup(true); - followOn.setTransmissionMode(nxdn::defines::TransmissionMode::MODE_4800); + const uint32_t hostPeerId = 8001U; + const uint32_t streamId = 0x620001U; + NXDNHostHarness harness(true, true, hostPort, hostPeerId); + NXDNTestNetwork sender(senderPort, 8002U); + REQUIRE(harness.network()->activateLoopback("127.0.0.1", senderPort)); + REQUIRE(sender.activateLoopback("127.0.0.1", hostPort)); + + REQUIRE(sender.sendNXDNCallHeader(hostPeerId, streamId, 100U, 1001U, 2001U)); + for (uint32_t i = 0U; i < 40U && HostTestHooks::nxdnNetState(*harness.m_control) != RS_NET_AUDIO; i++) { + sender.clock(1U); + harness.network()->clock(1U); + harness.m_control->clock(); + std::this_thread::sleep_for(std::chrono::milliseconds(2)); + } + REQUIRE(HostTestHooks::nxdnNetState(*harness.m_control) == RS_NET_AUDIO); + + // Skip RTP sequence 101 while continuing with the first two SACCH fragments. + REQUIRE(sender.sendNXDNVoiceFrame(hostPeerId, streamId, 102U, 0U, 1001U, 2001U)); + REQUIRE(sender.sendNXDNVoiceFrame(hostPeerId, streamId, 103U, 1U, 1001U, 2001U)); + for (uint32_t i = 0U; i < 20U; i++) { + sender.clock(1U); + harness.network()->clock(1U); + harness.m_control->clock(); + std::this_thread::sleep_for(std::chrono::milliseconds(2)); + } - REQUIRE(HostTestHooks::nxdnStartNetCall(*harness.m_control, followOn)); REQUIRE(HostTestHooks::nxdnNetState(*harness.m_control) == RS_NET_AUDIO); REQUIRE(HostTestHooks::nxdnNetworkWatchdog(*harness.m_control).isRunning()); REQUIRE(HostTestHooks::nxdnNetLastSrcId(*harness.m_control) == 1001U); @@ -347,30 +516,155 @@ TEST_CASE("NXDN host e2e loopback handles missed frames without dropping active TEST_CASE("NXDN host e2e loopback handles dropped call terminator and returns idle", "[nxdn][host][control][net][e2e]") { - NXDNHostHarness harness; - harness.startNetworkVoiceCall(1101U, 2101U); + const uint16_t hostPort = reserveLoopbackPort(); + const uint16_t senderPort = reserveLoopbackPort(); + REQUIRE(hostPort != 0U); + REQUIRE(senderPort != 0U); + REQUIRE(hostPort != senderPort); + + const uint32_t hostPeerId = 8003U; + const uint32_t streamId = 0x620002U; + NXDNHostHarness harness(true, true, hostPort, hostPeerId); + NXDNTestNetwork sender(senderPort, 8004U); + REQUIRE(harness.network()->activateLoopback("127.0.0.1", senderPort)); + REQUIRE(sender.activateLoopback("127.0.0.1", hostPort)); + + REQUIRE(sender.sendNXDNCallHeader(hostPeerId, streamId, 200U, 1101U, 2101U)); + for (uint32_t i = 0U; i < 40U && HostTestHooks::nxdnNetState(*harness.m_control) != RS_NET_AUDIO; i++) { + sender.clock(1U); + harness.network()->clock(1U); + harness.m_control->clock(); + std::this_thread::sleep_for(std::chrono::milliseconds(2)); + } REQUIRE(HostTestHooks::nxdnNetState(*harness.m_control) == RS_NET_AUDIO); + // Deliberately omit TX_REL and verify watchdog recovery. HostTestHooks::nxdnNetworkWatchdog(*harness.m_control).clock(expireTimerTicks(HostTestHooks::nxdnNetworkWatchdog(*harness.m_control))); harness.m_control->clock(); REQUIRE(HostTestHooks::nxdnNetState(*harness.m_control) == RS_NET_IDLE); REQUIRE_FALSE(HostTestHooks::nxdnNetworkWatchdog(*harness.m_control).isRunning()); + REQUIRE(harness.network()->rxNXDNStreamId() == 0U); + REQUIRE(harness.network()->resetNXDNCount() == 1U); } TEST_CASE("NXDN host e2e loopback times out stale call and resets stream state", "[nxdn][host][control][net][e2e]") { - NXDNHostHarness harness; - harness.startNetworkVoiceCall(1201U, 2201U); + const uint16_t hostPort = reserveLoopbackPort(); + const uint16_t senderPort = reserveLoopbackPort(); + REQUIRE(hostPort != 0U); + REQUIRE(senderPort != 0U); + REQUIRE(hostPort != senderPort); + + const uint32_t hostPeerId = 8005U; + const uint32_t streamId = 0x620003U; + NXDNHostHarness harness(true, true, hostPort, hostPeerId); + NXDNTestNetwork sender(senderPort, 8006U); + REQUIRE(harness.network()->activateLoopback("127.0.0.1", senderPort)); + REQUIRE(sender.activateLoopback("127.0.0.1", hostPort)); + REQUIRE(sender.sendNXDNCallHeader(hostPeerId, streamId, 300U, 1201U, 2201U)); + for (uint32_t i = 0U; i < 40U && HostTestHooks::nxdnNetState(*harness.m_control) != RS_NET_AUDIO; i++) { + sender.clock(1U); + harness.network()->clock(1U); + harness.m_control->clock(); + std::this_thread::sleep_for(std::chrono::milliseconds(2)); + } REQUIRE(HostTestHooks::nxdnNetState(*harness.m_control) == RS_NET_AUDIO); + REQUIRE(harness.network()->rxNXDNStreamId() == streamId); - HostTestHooks::nxdnNetTGHang(*harness.m_control).clock(expireTimerTicks(HostTestHooks::nxdnNetTGHang(*harness.m_control))); + HostTestHooks::nxdnNetworkWatchdog(*harness.m_control).clock(expireTimerTicks(HostTestHooks::nxdnNetworkWatchdog(*harness.m_control))); harness.m_control->clock(); + REQUIRE(HostTestHooks::nxdnNetState(*harness.m_control) == RS_NET_IDLE); + REQUIRE(harness.network()->rxNXDNStreamId() == 0U); + REQUIRE(harness.network()->resetNXDNCount() == 1U); +} + +TEST_CASE("NXDN host e2e loopback completes the conventional voice call flow", "[nxdn][host][control][net][e2e][call-flow]") +{ + const uint16_t hostPort = reserveLoopbackPort(); + const uint16_t senderPort = reserveLoopbackPort(); + REQUIRE(hostPort != 0U); + REQUIRE(senderPort != 0U); + REQUIRE(hostPort != senderPort); + + const uint32_t hostPeerId = 8013U; + const uint32_t streamId = 0x620301U; + NXDNHostHarness harness(true, true, hostPort, hostPeerId); + NXDNTestNetwork sender(senderPort, 8014U); + REQUIRE(harness.network()->activateLoopback("127.0.0.1", senderPort)); + REQUIRE(sender.activateLoopback("127.0.0.1", hostPort)); + + // TS 1-B conventional flow: FACCH VCALL, one four-frame SACCH + // voice superframe, then a FACCH TX_REL. + REQUIRE(sender.sendNXDNCallHeader(hostPeerId, streamId, 700U, 1601U, 2601U)); + REQUIRE(sender.sendNXDNVoiceFrame(hostPeerId, streamId, 701U, 0U, 1601U, 2601U)); + REQUIRE(sender.sendNXDNVoiceFrame(hostPeerId, streamId, 702U, 1U, 1601U, 2601U)); + REQUIRE(sender.sendNXDNVoiceFrame(hostPeerId, streamId, 703U, 2U, 1601U, 2601U)); + REQUIRE(sender.sendNXDNVoiceFrame(hostPeerId, streamId, 704U, 3U, 1601U, 2601U)); + + for (uint32_t i = 0U; i < 60U && HostTestHooks::nxdnNetState(*harness.m_control) != RS_NET_AUDIO; i++) { + sender.clock(1U); + harness.network()->clock(1U); + harness.m_control->clock(); + std::this_thread::sleep_for(std::chrono::milliseconds(2)); + } + REQUIRE(HostTestHooks::nxdnNetState(*harness.m_control) == RS_NET_AUDIO); - REQUIRE(HostTestHooks::nxdnNetLastDstId(*harness.m_control) == 0U); - REQUIRE(HostTestHooks::nxdnNetLastSrcId(*harness.m_control) == 0U); + REQUIRE(HostTestHooks::nxdnNetLastSrcId(*harness.m_control) == 1601U); + REQUIRE(HostTestHooks::nxdnNetLastDstId(*harness.m_control) == 2601U); + REQUIRE(harness.network()->rxNXDNStreamId() == streamId); + + REQUIRE(sender.sendNXDNCallTerminator(hostPeerId, streamId, 1601U, 2601U)); + for (uint32_t i = 0U; i < 50U && HostTestHooks::nxdnNetState(*harness.m_control) != RS_NET_IDLE; i++) { + sender.clock(1U); + harness.network()->clock(1U); + harness.m_control->clock(); + std::this_thread::sleep_for(std::chrono::milliseconds(2)); + } + + REQUIRE(HostTestHooks::nxdnNetState(*harness.m_control) == RS_NET_IDLE); + REQUIRE_FALSE(HostTestHooks::nxdnNetworkWatchdog(*harness.m_control).isRunning()); + REQUIRE(harness.network()->rxNXDNStreamId() == 0U); +} + +TEST_CASE("NXDN host e2e loopback tolerates out-of-order voice frames", "[nxdn][host][control][net][e2e][call-flow]") +{ + const uint16_t hostPort = reserveLoopbackPort(); + const uint16_t senderPort = reserveLoopbackPort(); + REQUIRE(hostPort != 0U); + REQUIRE(senderPort != 0U); + REQUIRE(hostPort != senderPort); + + const uint32_t hostPeerId = 8015U; + const uint32_t streamId = 0x620302U; + NXDNHostHarness harness(true, true, hostPort, hostPeerId); + NXDNTestNetwork sender(senderPort, 8016U); + REQUIRE(harness.network()->activateLoopback("127.0.0.1", senderPort)); + REQUIRE(sender.activateLoopback("127.0.0.1", hostPort)); + + REQUIRE(sender.sendNXDNCallHeader(hostPeerId, streamId, 800U, 1701U, 2701U)); + for (uint32_t i = 0U; i < 40U && HostTestHooks::nxdnNetState(*harness.m_control) != RS_NET_AUDIO; i++) { + sender.clock(1U); + harness.network()->clock(1U); + harness.m_control->clock(); + std::this_thread::sleep_for(std::chrono::milliseconds(2)); + } + REQUIRE(HostTestHooks::nxdnNetState(*harness.m_control) == RS_NET_AUDIO); + + REQUIRE(sender.sendNXDNVoiceFrame(hostPeerId, streamId, 802U, 1U, 1701U, 2701U)); + REQUIRE(sender.sendNXDNVoiceFrame(hostPeerId, streamId, 801U, 0U, 1701U, 2701U)); + for (uint32_t i = 0U; i < 25U; i++) { + sender.clock(1U); + harness.network()->clock(1U); + harness.m_control->clock(); + std::this_thread::sleep_for(std::chrono::milliseconds(2)); + } + + REQUIRE(HostTestHooks::nxdnNetState(*harness.m_control) == RS_NET_AUDIO); + REQUIRE(HostTestHooks::nxdnNetworkWatchdog(*harness.m_control).isRunning()); + REQUIRE(harness.network()->rxNXDNStreamId() == streamId); } TEST_CASE("NXDN host e2e loopback preserves a continuing network stream across an RF collision", "[nxdn][host][control][net][e2e]") @@ -392,7 +686,7 @@ TEST_CASE("NXDN host e2e loopback preserves a continuing network stream across a REQUIRE(sender.activateLoopback("127.0.0.1", hostPort)); HostTestHooks::nxdnSetRFCall(*harness.m_control, 1401U, 2401U); - REQUIRE(sender.sendNXDNFrame(hostPeerId, streamId, 600U, nxdn::defines::MessageType::RTCH_VCALL, 1501U, 2501U)); + REQUIRE(sender.sendNXDNControlFrame(hostPeerId, streamId, 600U, nxdn::defines::MessageType::RTCH_VCALL, 1501U, 2501U)); for (uint32_t i = 0U; i < 40U; i++) { sender.clock(1U); @@ -410,7 +704,7 @@ TEST_CASE("NXDN host e2e loopback preserves a continuing network stream across a REQUIRE(harness.network()->resetNXDNCount() == 0U); HostTestHooks::nxdnClearRFCall(*harness.m_control); - REQUIRE(sender.sendNXDNFrame(hostPeerId, streamId, 601U, nxdn::defines::MessageType::RTCH_VCALL, 1501U, 2501U)); + REQUIRE(sender.sendNXDNControlFrame(hostPeerId, streamId, 601U, nxdn::defines::MessageType::RTCH_VCALL, 1501U, 2501U)); for (uint32_t i = 0U; i < 40U; i++) { sender.clock(1U); @@ -447,7 +741,7 @@ TEST_CASE("NXDN host e2e loopback enforces stream lock until active stream termi REQUIRE(harness.network()->activateLoopback("127.0.0.1", senderPort)); REQUIRE(sender.activateLoopback("127.0.0.1", hostPort)); - REQUIRE(sender.sendNXDNFrame(hostPeerId, streamA, 400U, nxdn::defines::MessageType::RTCH_VCALL, 1301U, 2301U)); + REQUIRE(sender.sendNXDNControlFrame(hostPeerId, streamA, 400U, nxdn::defines::MessageType::RTCH_VCALL, 1301U, 2301U)); for (uint32_t i = 0U; i < 40U; i++) { sender.clock(1U); @@ -465,7 +759,7 @@ TEST_CASE("NXDN host e2e loopback enforces stream lock until active stream termi REQUIRE(HostTestHooks::nxdnNetLastDstId(*harness.m_control) == 2301U); REQUIRE(harness.network()->rxNXDNStreamId() == streamA); - REQUIRE(sender.sendNXDNFrame(hostPeerId, streamB, 500U, nxdn::defines::MessageType::RTCH_VCALL, 1301U, 2301U)); + REQUIRE(sender.sendNXDNControlFrame(hostPeerId, streamB, 500U, nxdn::defines::MessageType::RTCH_VCALL, 1301U, 2301U)); for (uint32_t i = 0U; i < 30U; i++) { sender.clock(1U); @@ -479,7 +773,7 @@ TEST_CASE("NXDN host e2e loopback enforces stream lock until active stream termi REQUIRE(HostTestHooks::nxdnNetLastDstId(*harness.m_control) == 2301U); REQUIRE(harness.network()->rxNXDNStreamId() == streamA); - REQUIRE(sender.sendNXDNFrame(hostPeerId, streamA, RTP_END_OF_CALL_SEQ, nxdn::defines::MessageType::RTCH_TX_REL, 1301U, 2301U)); + REQUIRE(sender.sendNXDNControlFrame(hostPeerId, streamA, RTP_END_OF_CALL_SEQ, nxdn::defines::MessageType::RTCH_TX_REL, 1301U, 2301U)); for (uint32_t i = 0U; i < 50U; i++) { sender.clock(1U); @@ -495,7 +789,7 @@ TEST_CASE("NXDN host e2e loopback enforces stream lock until active stream termi REQUIRE(HostTestHooks::nxdnNetState(*harness.m_control) == RS_NET_IDLE); REQUIRE(harness.network()->rxNXDNStreamId() == 0U); - REQUIRE(sender.sendNXDNFrame(hostPeerId, streamB, 502U, nxdn::defines::MessageType::RTCH_VCALL, 1301U, 2301U)); + REQUIRE(sender.sendNXDNControlFrame(hostPeerId, streamB, 502U, nxdn::defines::MessageType::RTCH_VCALL, 1301U, 2301U)); for (uint32_t i = 0U; i < 40U; i++) { sender.clock(1U); @@ -618,4 +912,4 @@ TEST_CASE("NXDN rfTGHang zero fallback arms rfLossWatchdog and recovers RF state REQUIRE(HostTestHooks::nxdnRFState(*harness.m_control) == RS_RF_LISTENING); REQUIRE_FALSE(HostTestHooks::nxdnRFLossWatchdog(*harness.m_control).isRunning()); -} \ No newline at end of file +} diff --git a/tests/nxdn/RTCH_Tests.cpp b/tests/nxdn/RTCH_Tests.cpp index 741a17a8..d2c38561 100644 --- a/tests/nxdn/RTCH_Tests.cpp +++ b/tests/nxdn/RTCH_Tests.cpp @@ -15,10 +15,38 @@ #include #include "common/nxdn/lc/RTCH.h" +#include "common/nxdn/channel/FACCH1.h" #include "common/nxdn/NXDNDefines.h" using namespace nxdn; using namespace nxdn::defines; + +TEST_CASE("RTCH TX_REL survives FACCH1 channel coding", "[nxdn][rtch][facch1]") +{ + lc::RTCH txRelease; + txRelease.setMessageType(MessageType::RTCH_TX_REL); + txRelease.setSrcId(1601U); + txRelease.setDstId(2601U); + + uint8_t lcData[NXDN_RTCH_LC_LENGTH_BYTES] = {}; + txRelease.encode(lcData, NXDN_RTCH_LC_LENGTH_BITS); + + channel::FACCH1 encoder; + encoder.setData(lcData); + uint8_t frame[NXDN_FRAME_LENGTH_BYTES] = {}; + encoder.encode(frame, NXDN_FSW_LENGTH_BITS + NXDN_LICH_LENGTH_BITS + NXDN_SACCH_FEC_LENGTH_BITS); + + channel::FACCH1 decoder; + REQUIRE(decoder.decode(frame, NXDN_FSW_LENGTH_BITS + NXDN_LICH_LENGTH_BITS + NXDN_SACCH_FEC_LENGTH_BITS)); + uint8_t decodedData[NXDN_FACCH1_CRC_LENGTH_BYTES - 2U] = {}; + decoder.getData(decodedData); + + lc::RTCH decoded; + decoded.decode(decodedData, NXDN_FACCH1_LENGTH_BITS); + REQUIRE(decoded.getMessageType() == MessageType::RTCH_TX_REL); + REQUIRE(decoded.getSrcId() == 1601U); + REQUIRE(decoded.getDstId() == 2601U); +} using namespace nxdn::lc; TEST_CASE("RTCH encodes and decodes voice call", "[nxdn][rtch]") { @@ -214,3 +242,42 @@ TEST_CASE("RTCH encodes and decodes data frame and block numbers", "[nxdn][rtch] REQUIRE(decoded.getDataFrameNumber() == 0x0AU); REQUIRE(decoded.getDataBlockNumber() == 0x05U); } + +TEST_CASE("RTCH VCALL_IV preserves the 64-bit IV without prior cipher state", "[nxdn][rtch][security]") { + const uint8_t expectedMI[MI_LENGTH_BYTES] = { + 0x01U, 0x23U, 0x45U, 0x67U, 0x89U, 0xABU, 0xCDU, 0xEFU + }; + + RTCH iv; + iv.setMessageType(MessageType::RTCH_VCALL_IV); + iv.setMI(expectedMI); + + uint8_t data[NXDN_RTCH_LC_LENGTH_BYTES] = {}; + iv.encode(data, NXDN_FACCH1_LENGTH_BITS); + REQUIRE(data[0U] == MessageType::RTCH_VCALL_IV); + REQUIRE(::memcmp(data + 1U, expectedMI, MI_LENGTH_BYTES) == 0); + + RTCH decoded; + REQUIRE(decoded.getAlgId() == CIPHER_TYPE_NONE); + decoded.decode(data, NXDN_FACCH1_LENGTH_BITS); + + uint8_t actualMI[MI_LENGTH_BYTES] = {}; + decoded.getMI(actualMI); + REQUIRE(::memcmp(actualMI, expectedMI, MI_LENGTH_BYTES) == 0); +} + +TEST_CASE("RTCH derives encrypted state from VCALL cipher type", "[nxdn][rtch][security]") { + RTCH call; + call.setMessageType(MessageType::RTCH_VCALL); + call.setAlgId(0x02U); + call.setKId(0x15U); + + uint8_t data[NXDN_RTCH_LC_LENGTH_BYTES] = {}; + call.encode(data, NXDN_FACCH1_LENGTH_BITS); + + RTCH decoded; + decoded.decode(data, NXDN_FACCH1_LENGTH_BITS); + REQUIRE(decoded.getEncrypted()); + REQUIRE(decoded.getAlgId() == 0x02U); + REQUIRE(decoded.getKId() == 0x15U); +}