diff --git a/src/common/edac/RS634717.cpp b/src/common/edac/RS634717.cpp index f560192d..a7351104 100644 --- a/src/common/edac/RS634717.cpp +++ b/src/common/edac/RS634717.cpp @@ -300,7 +300,7 @@ RS634717::~RS634717() = default; /* Decode RS (24,12,13) FEC. */ -bool RS634717::decode241213(uint8_t* data) +bool RS634717::decode241213(uint8_t* data, int8_t* errs) { assert(data != nullptr); @@ -318,6 +318,9 @@ bool RS634717::decode241213(uint8_t* data) for (uint32_t i = 0U; i < 12U; i++, offset += 6) Utils::hex2Bin(codeword[39 + i], data, offset); + if (errs != nullptr) + *errs = (int8_t)ec; + if ((ec == -1) || (ec > 6)) { return false; } @@ -350,7 +353,7 @@ void RS634717::encode241213(uint8_t* data) /* Decode RS (24,16,9) FEC. */ -bool RS634717::decode24169(uint8_t* data) +bool RS634717::decode24169(uint8_t* data, int8_t* errs) { assert(data != nullptr); @@ -368,6 +371,9 @@ bool RS634717::decode24169(uint8_t* data) for (uint32_t i = 0U; i < 16U; i++, offset += 6) Utils::hex2Bin(codeword[39 + i], data, offset); + if (errs != nullptr) + *errs = (int8_t)ec; + if ((ec == -1) || (ec > 4)) { return false; } diff --git a/src/common/edac/RS634717.h b/src/common/edac/RS634717.h index 35739679..37488a72 100644 --- a/src/common/edac/RS634717.h +++ b/src/common/edac/RS634717.h @@ -47,9 +47,10 @@ namespace edac /** * @brief Decode RS (24,12,13) FEC. * @param data Reed-Solomon FEC encoded data to decode. + * @param[out] errs Optional, number of symbol errors corrected (0-6), or -1 if uncorrectable. * @returns bool True, if data was decoded, otherwise false. */ - bool decode241213(uint8_t* data); + bool decode241213(uint8_t* data, int8_t* errs = nullptr); /** * @brief Encode RS (24,12,13) FEC. * @param data Raw data to encode with Reed-Solomon FEC. @@ -59,9 +60,10 @@ namespace edac /** * @brief Decode RS (24,16,9) FEC. * @param data Reed-Solomon FEC encoded data to decode. + * @param[out] errs Optional, number of symbol errors corrected (0-4), or -1 if uncorrectable. * @returns bool True, if data was decoded, otherwise false. */ - bool decode24169(uint8_t* data); + bool decode24169(uint8_t* data, int8_t* errs = nullptr); /** * @brief Encode RS (24,16,9) FEC. * @param data Raw data to encode with Reed-Solomon FEC. diff --git a/src/common/p25/lc/LC.cpp b/src/common/p25/lc/LC.cpp index 18571c00..d51bbd9b 100644 --- a/src/common/p25/lc/LC.cpp +++ b/src/common/p25/lc/LC.cpp @@ -187,6 +187,7 @@ LC::LC() : m_macPduOffset(P2_MAC_HEADER_OFFSET::NO_VOICE_OR_UNK), m_macPartition(P2_MAC_MCO_PARTITION::ABBREVIATED), m_rsValue(0U), + m_fecErrs(-1), p2MCOData(nullptr), m_rs(), m_encryptOverride(false), @@ -399,13 +400,16 @@ bool LC::decodeLDU1(const uint8_t* data, bool rawOnly) // decode RS (24,12,13) FEC try { - bool ret = m_rs.decode241213(rs); + int8_t errs = -1; + bool ret = m_rs.decode241213(rs, &errs); + m_fecErrs = errs; if (!ret) { LogError(LOG_P25, "LC::decodeLDU1(), failed to decode RS (24,12,13) FEC"); return false; } } catch (...) { + m_fecErrs = -1; Utils::dump(2U, "P25, LC::decodeLDU1(), RS excepted with input data", rs, P25_LDU_LC_FEC_LENGTH_BYTES); return false; } @@ -498,13 +502,16 @@ bool LC::decodeLDU2(const uint8_t* data) // decode RS (24,16,9) FEC try { - bool ret = m_rs.decode24169(rs); + int8_t errs = -1; + bool ret = m_rs.decode24169(rs, &errs); + m_fecErrs = errs; if (!ret) { LogError(LOG_P25, "LC::decodeLDU2(), failed to decode RS (24,16,9) FEC"); return false; } } catch (...) { + m_fecErrs = -1; Utils::dump(2U, "P25, LC::decodeLDU2(), RS excepted with input data", rs, P25_LDU_LC_FEC_LENGTH_BYTES); return false; } @@ -1536,6 +1543,7 @@ void LC::copy(const LC& data) m_p2ScrambleOffsetValid = data.m_p2ScrambleOffsetValid; m_rsValue = data.m_rsValue; + m_fecErrs = data.m_fecErrs; m_algId = data.m_algId; if (m_algId != ALGO_UNENCRYPT) { diff --git a/src/common/p25/lc/LC.h b/src/common/p25/lc/LC.h index bf7b9d89..b7c0dbef 100644 --- a/src/common/p25/lc/LC.h +++ b/src/common/p25/lc/LC.h @@ -340,6 +340,15 @@ namespace p25 DECLARE_PROPERTY(ulong64_t, rsValue, RS); /** @} */ + /** @name FEC Decode Confidence */ + /** + * @brief Number of Reed-Solomon symbol errors corrected decoding the most recent + * LDU1 Link Control or LDU2 Encryption Sync word. -1 if not yet decoded, or if the + * last decode attempt was uncorrectable. + */ + DECLARE_PROPERTY(int8_t, fecErrs, FECErrs); + /** @} */ + /** @name Phase 2 Raw MCO Data */ uint8_t* p2MCOData; // ?? - this should probably be private with getters/setters /** @} */ diff --git a/src/host/modem/ModemV24.cpp b/src/host/modem/ModemV24.cpp index d76ec04c..1c07da46 100644 --- a/src/host/modem/ModemV24.cpp +++ b/src/host/modem/ModemV24.cpp @@ -1680,7 +1680,8 @@ void ModemV24::convertToAirV24(const uint8_t *data, uint32_t length) // decode RS (24,12,13) FEC try { - bool ret = m_rs.decode241213(m_rxCall->LDULC); + int8_t rsErrs = -1; + bool ret = m_rs.decode241213(m_rxCall->LDULC, &rsErrs); if (!ret) { LogError(LOG_MODEM, "V.24/DFSI LDU1, failed to decode RS (24,12,13) FEC"); @@ -1724,6 +1725,29 @@ void ModemV24::convertToAirV24(const uint8_t *data, uint32_t length) } } else { valid = true; + + // srcId/dstId/lco/mfId were latched above directly from the raw, pre-correction DFSI + // wire bytes (mirrored into LDULC as they arrived). If the RS decoder just corrected + // symbol errors, re-derive those fields from the now-corrected buffer instead of + // trusting the raw bytes read before correction. + if (rsErrs > 0) { + lc::LC correctedLC = lc::LC(); + if (correctedLC.decodeLC(m_rxCall->LDULC, false)) { + if (m_rxCall->lco != correctedLC.getLCO() || m_rxCall->mfId != correctedLC.getMFId() || + (correctedLC.isStandardMFId() && (m_rxCall->dstId != correctedLC.getDstId() || m_rxCall->srcId != correctedLC.getSrcId()))) { + LogWarning(LOG_MODEM, "V.24/DFSI LDU1, %d FEC corrections changed LC content, lco = $%02X (was $%02X), mfId = $%02X (was $%02X), srcId = %u (was %u), dstId = %u (was %u)", + rsErrs, correctedLC.getLCO(), m_rxCall->lco, correctedLC.getMFId(), m_rxCall->mfId, + correctedLC.getSrcId(), m_rxCall->srcId, correctedLC.getDstId(), m_rxCall->dstId); + } + + m_rxCall->lco = correctedLC.getLCO(); + m_rxCall->mfId = correctedLC.getMFId(); + if (correctedLC.isStandardMFId()) { + m_rxCall->dstId = correctedLC.getDstId(); + m_rxCall->srcId = correctedLC.getSrcId(); + } + } + } } } catch (...) { @@ -1832,7 +1856,8 @@ void ModemV24::convertToAirV24(const uint8_t *data, uint32_t length) // decode RS (24,16,9) FEC try { - bool ret = m_rs.decode24169(m_rxCall->LDULC); + int8_t rsErrs = -1; + bool ret = m_rs.decode24169(m_rxCall->LDULC, &rsErrs); if (!ret) { LogError(LOG_MODEM, "V.24/DFSI LDU2, failed to decode RS (24,16,9) FEC"); @@ -1853,6 +1878,29 @@ void ModemV24::convertToAirV24(const uint8_t *data, uint32_t length) } } else { valid = true; + + // algId/kId/MI were latched above directly from the raw, pre-correction DFSI wire + // bytes. If the RS decoder just corrected symbol errors, re-derive those fields from + // the now-corrected buffer instead of trusting the raw bytes read before correction. + if (rsErrs > 0) { + uint8_t correctedAlgId = m_rxCall->LDULC[9U]; + uint32_t correctedKId = (m_rxCall->LDULC[10U] << 8) + m_rxCall->LDULC[11U]; + + if (correctedAlgId != m_rxCall->algoId || correctedKId != m_rxCall->kId || + ::memcmp(m_rxCall->LDULC, m_rxCall->MI, MI_LENGTH_BYTES) != 0) { + LogWarning(LOG_MODEM, "V.24/DFSI LDU2, %d FEC corrections changed Enc Sync content, algId = $%02X (was $%02X), kId = $%04X (was $%04X)", + rsErrs, correctedAlgId, m_rxCall->algoId, correctedKId, m_rxCall->kId); + } + + m_rxCall->algoId = correctedAlgId; + if (m_rxCall->algoId != ALGO_UNENCRYPT) { + ::memcpy(m_rxCall->MI, m_rxCall->LDULC, MI_LENGTH_BYTES); + m_rxCall->kId = correctedKId; + } else { + ::memset(m_rxCall->MI, 0x00U, MI_LENGTH_BYTES); + m_rxCall->kId = 0x0000U; + } + } } } catch (...) { @@ -2386,7 +2434,8 @@ void ModemV24::convertToAirTIA(const uint8_t *data, uint32_t length) // decode RS (24,12,13) FEC try { - bool ret = m_rs.decode241213(m_rxCall->LDULC); + int8_t rsErrs = -1; + bool ret = m_rs.decode241213(m_rxCall->LDULC, &rsErrs); if (!ret) { LogError(LOG_MODEM, "TIA/DFSI LDU1, failed to decode RS (24,12,13) FEC"); @@ -2430,6 +2479,28 @@ void ModemV24::convertToAirTIA(const uint8_t *data, uint32_t length) } } else { valid = true; + + // srcId/dstId/lco/mfId were latched above directly from the raw, pre-correction DFSI + // wire bytes. If the RS decoder just corrected symbol errors, re-derive those fields + // from the now-corrected buffer instead of trusting the raw bytes read before correction. + if (rsErrs > 0) { + lc::LC correctedLC = lc::LC(); + if (correctedLC.decodeLC(m_rxCall->LDULC, false)) { + if (m_rxCall->lco != correctedLC.getLCO() || m_rxCall->mfId != correctedLC.getMFId() || + (correctedLC.isStandardMFId() && (m_rxCall->dstId != correctedLC.getDstId() || m_rxCall->srcId != correctedLC.getSrcId()))) { + LogWarning(LOG_MODEM, "TIA/DFSI LDU1, %d FEC corrections changed LC content, lco = $%02X (was $%02X), mfId = $%02X (was $%02X), srcId = %u (was %u), dstId = %u (was %u)", + rsErrs, correctedLC.getLCO(), m_rxCall->lco, correctedLC.getMFId(), m_rxCall->mfId, + correctedLC.getSrcId(), m_rxCall->srcId, correctedLC.getDstId(), m_rxCall->dstId); + } + + m_rxCall->lco = correctedLC.getLCO(); + m_rxCall->mfId = correctedLC.getMFId(); + if (correctedLC.isStandardMFId()) { + m_rxCall->dstId = correctedLC.getDstId(); + m_rxCall->srcId = correctedLC.getSrcId(); + } + } + } } } catch (...) { @@ -2538,7 +2609,8 @@ void ModemV24::convertToAirTIA(const uint8_t *data, uint32_t length) // decode RS (24,16,9) FEC try { - bool ret = m_rs.decode24169(m_rxCall->LDULC); + int8_t rsErrs = -1; + bool ret = m_rs.decode24169(m_rxCall->LDULC, &rsErrs); if (!ret) { LogError(LOG_MODEM, "TIA/DFSI LDU2, failed to decode RS (24,16,9) FEC"); @@ -2559,6 +2631,29 @@ void ModemV24::convertToAirTIA(const uint8_t *data, uint32_t length) } } else { valid = true; + + // algId/kId/MI were latched above directly from the raw, pre-correction DFSI wire + // bytes. If the RS decoder just corrected symbol errors, re-derive those fields from + // the now-corrected buffer instead of trusting the raw bytes read before correction. + if (rsErrs > 0) { + uint8_t correctedAlgId = m_rxCall->LDULC[9U]; + uint32_t correctedKId = (m_rxCall->LDULC[10U] << 8) + m_rxCall->LDULC[11U]; + + if (correctedAlgId != m_rxCall->algoId || correctedKId != m_rxCall->kId || + ::memcmp(m_rxCall->LDULC, m_rxCall->MI, MI_LENGTH_BYTES) != 0) { + LogWarning(LOG_MODEM, "TIA/DFSI LDU2, %d FEC corrections changed Enc Sync content, algId = $%02X (was $%02X), kId = $%04X (was $%04X)", + rsErrs, correctedAlgId, m_rxCall->algoId, correctedKId, m_rxCall->kId); + } + + m_rxCall->algoId = correctedAlgId; + if (m_rxCall->algoId != ALGO_UNENCRYPT) { + ::memcpy(m_rxCall->MI, m_rxCall->LDULC, MI_LENGTH_BYTES); + m_rxCall->kId = correctedKId; + } else { + ::memset(m_rxCall->MI, 0x00U, MI_LENGTH_BYTES); + m_rxCall->kId = 0x0000U; + } + } } } catch (...) { diff --git a/src/host/p25/packet/Voice.cpp b/src/host/p25/packet/Voice.cpp index 74e2bfc9..f09a1826 100644 --- a/src/host/p25/packet/Voice.cpp +++ b/src/host/p25/packet/Voice.cpp @@ -675,6 +675,38 @@ bool Voice::process(uint8_t* data, uint32_t len) m_rfUndecodableLC++; } else { + // a P25 voice call does not change its srcId/dstId mid-transmission. Two independent + // corroboration checks guard against a marginal-RF miscorrection silently redirecting + // a call. Neither runs at call grant/establishment -- only once a call's identity is + // already set -- so this never blocks a new (including unregistered/ad hoc) srcId from + // originating a call: + // 1) does this decode disagree with the call's last known-good LDU1, despite needing + // RS correction to get here (weaker signal -- the LDU1 LC is only protected by + // Hamming(10,6,3) + RS(24,12,13)); + // 2) does this decode's dstId disagree with the dstId carried in this call's HDU, which + // is far more strongly protected (Golay(18,6,8) + RS(36,20,17)) and, unlike the LDU1 + // LC, is fixed for the life of the call -- so disagreement here is a protocol + // violation, not just a low-confidence guess, regardless of how "clean" (FEC-wise) + // this LDU1's decode was. + bool fecMismatch = m_rfLC.getFECErrs() > 0 && + ((m_rfLastLDU1.getDstId() != 0U && m_rfLC.getDstId() != m_rfLastLDU1.getDstId()) || + (m_rfLastLDU1.getSrcId() != 0U && m_rfLC.getSrcId() != m_rfLastLDU1.getSrcId())); + + bool hduDstMismatch = m_rfLastHDUValid && m_rfLastHDU.getDstId() != 0U && + m_rfLC.getDstId() != m_rfLastHDU.getDstId(); + + if (fecMismatch || hduDstMismatch) { + uint32_t goodSrcId = m_rfLastLDU1.getSrcId(); + uint32_t goodDstId = hduDstMismatch ? m_rfLastHDU.getDstId() : m_rfLastLDU1.getDstId(); + + LogWarning(LOG_RF, P25_LDU1_STR ", marginal LC decode disagrees with established call identity (%d FEC corrections%s), srcId = %u (was %u), dstId = %u (was %u), keeping established identity", + m_rfLC.getFECErrs(), hduDstMismatch ? ", HDU dstId mismatch" : "", + m_rfLC.getSrcId(), goodSrcId, m_rfLC.getDstId(), goodDstId); + + m_rfLC.setSrcId(goodSrcId); + m_rfLC.setDstId(goodDstId); + } + m_rfLastLDU1 = m_rfLC; } }