correct CRC-CCITT 162 used for NXDN CAC (inital CRC registers are set to 1);

3.0-rcon_maint 2023-03-08
Bryan Biedenkapp 3 years ago
parent 5f0df4d943
commit 3538c51efb

@ -228,7 +228,7 @@ void CRC::encodeFiveBit(const bool* in, uint32_t& tcrc)
} }
/// <summary> /// <summary>
/// Check 16-bit CRC-CCITT. /// Check 16-bit CRC CCITT-162.
/// </summary> /// </summary>
/// <remarks>This uses polynomial 0x1021.</remarks> /// <remarks>This uses polynomial 0x1021.</remarks>
/// <param name="in">Input byte array.</param> /// <param name="in">Input byte array.</param>
@ -260,7 +260,7 @@ bool CRC::checkCCITT162(const uint8_t *in, uint32_t length)
} }
/// <summary> /// <summary>
/// Encode 16-bit CRC-CCITT. /// Encode 16-bit CRC CCITT-162.
/// </summary> /// </summary>
/// <remarks>This uses polynomial 0x1021.</remarks> /// <remarks>This uses polynomial 0x1021.</remarks>
/// <param name="in">Input byte array.</param> /// <param name="in">Input byte array.</param>
@ -291,7 +291,7 @@ void CRC::addCCITT162(uint8_t* in, uint32_t length)
} }
/// <summary> /// <summary>
/// Check 16-bit CRC-CCITT. /// Check 16-bit CRC CCITT-161.
/// </summary> /// </summary>
/// <remarks>This uses polynomial 0x1189.</remarks> /// <remarks>This uses polynomial 0x1189.</remarks>
/// <param name="in">Input byte array.</param> /// <param name="in">Input byte array.</param>
@ -323,7 +323,7 @@ bool CRC::checkCCITT161(const uint8_t *in, uint32_t length)
} }
/// <summary> /// <summary>
/// Encode 16-bit CRC-CCITT. /// Encode 16-bit CRC CCITT-161.
/// </summary> /// </summary>
/// <remarks>This uses polynomial 0x1189.</remarks> /// <remarks>This uses polynomial 0x1189.</remarks>
/// <param name="in">Input byte array.</param> /// <param name="in">Input byte array.</param>
@ -654,7 +654,7 @@ uint16_t CRC::addCRC15(uint8_t* in, uint32_t bitLength)
} }
/// <summary> /// <summary>
/// Check 16-bit CRC. /// Check 16-bit CRC CCITT-162 w/ initial generator of 1.
/// </summary> /// </summary>
/// <param name="in">Input byte array.</param> /// <param name="in">Input byte array.</param>
/// <param name="bitLength">Length of byte array in bits.</param> /// <param name="bitLength">Length of byte array in bits.</param>
@ -686,13 +686,13 @@ bool CRC::checkCRC16(const uint8_t* in, uint32_t bitLength)
} }
/// <summary> /// <summary>
/// Encode 16-bit CRC. /// Encode 16-bit CRC CCITT-162 w/ initial generator of 1.
/// </summary> /// </summary>
/// <param name="in">Input byte array.</param> /// <param name="in">Input byte array.</param>
/// <param name="bitLength">Length of byte array in bits.</param> /// <param name="bitLength">Length of byte array in bits.</param>
/// <param name="offset">Offset in bits to write CRC.</param> /// <param name="offset">Offset in bits to write CRC.</param>
/// <returns>16-bit CRC.</returns> /// <returns>16-bit CRC.</returns>
uint16_t CRC::addCRC16(uint8_t* in, uint32_t bitLength, uint32_t offset) uint16_t CRC::addCRC16(uint8_t* in, uint32_t bitLength)
{ {
assert(in != nullptr); assert(in != nullptr);
@ -705,7 +705,7 @@ uint16_t CRC::addCRC16(uint8_t* in, uint32_t bitLength, uint32_t offset)
uint32_t n = bitLength; uint32_t n = bitLength;
for (uint32_t i = 0U; i < 16U; i++, n++) { for (uint32_t i = 0U; i < 16U; i++, n++) {
bool b = READ_BIT(temp, i); bool b = READ_BIT(temp, i);
WRITE_BIT(in, n + offset, b); WRITE_BIT(in, n, b);
} }
#if DEBUG_CRC #if DEBUG_CRC
@ -795,7 +795,7 @@ uint16_t CRC::createCRC15(const uint8_t* in, uint32_t bitLength)
/// <returns></returns> /// <returns></returns>
uint16_t CRC::createCRC16(const uint8_t* in, uint32_t bitLength) uint16_t CRC::createCRC16(const uint8_t* in, uint32_t bitLength)
{ {
uint16_t crc = 0x0000U; uint16_t crc = 0xFFFFU;
for (uint32_t i = 0U; i < bitLength; i++) { for (uint32_t i = 0U; i < bitLength; i++) {
bool bit1 = READ_BIT(in, i) != 0x00U; bool bit1 = READ_BIT(in, i) != 0x00U;
@ -807,6 +807,5 @@ uint16_t CRC::createCRC16(const uint8_t* in, uint32_t bitLength)
crc ^= 0x1021U; crc ^= 0x1021U;
} }
crc = ~crc;
return crc & 0xFFFFU; return crc & 0xFFFFU;
} }

@ -47,14 +47,14 @@ namespace edac
/// <summary>Encode 5-bit CRC.</summary> /// <summary>Encode 5-bit CRC.</summary>
static void encodeFiveBit(const bool* in, uint32_t& tcrc); static void encodeFiveBit(const bool* in, uint32_t& tcrc);
/// <summary>Check 16-bit CRC-CCITT.</summary> /// <summary>Check 16-bit CRC CCITT-162.</summary>
static bool checkCCITT162(const uint8_t* in, uint32_t length); static bool checkCCITT162(const uint8_t* in, uint32_t length);
/// <summary>Encode 16-bit CRC-CCITT.</summary> /// <summary>Encode 16-bit CRC CCITT-162.</summary>
static void addCCITT162(uint8_t* in, uint32_t length); static void addCCITT162(uint8_t* in, uint32_t length);
/// <summary>Check 16-bit CRC-CCITT.</summary> /// <summary>Check 16-bit CRC CCITT-161.</summary>
static bool checkCCITT161(const uint8_t* in, uint32_t length); static bool checkCCITT161(const uint8_t* in, uint32_t length);
/// <summary>Encode 16-bit CRC-CCITT.</summary> /// <summary>Encode 16-bit CRC CCITT-161.</summary>
static void addCCITT161(uint8_t* in, uint32_t length); static void addCCITT161(uint8_t* in, uint32_t length);
/// <summary>Check 32-bit CRC.</summary> /// <summary>Check 32-bit CRC.</summary>
@ -83,10 +83,10 @@ namespace edac
/// <summary>Encode 15-bit CRC.</summary> /// <summary>Encode 15-bit CRC.</summary>
static uint16_t addCRC15(uint8_t* in, uint32_t bitLength); static uint16_t addCRC15(uint8_t* in, uint32_t bitLength);
/// <summary>Check 16-bit CRC-CCITT.</summary> /// <summary>Check 16-bit CRC CCITT-162 w/ initial generator of 1.</summary>
static bool checkCRC16(const uint8_t* in, uint32_t bitLength); static bool checkCRC16(const uint8_t* in, uint32_t bitLength);
/// <summary>Encode 16-bit CRC.</summary> /// <summary>Encode 16-bit CRC CCITT-162 w/ initial generator of 1.</summary>
static uint16_t addCRC16(uint8_t* in, uint32_t bitLength, uint32_t offset = 0); static uint16_t addCRC16(uint8_t* in, uint32_t bitLength);
private: private:
/// <summary></summary> /// <summary></summary>

@ -272,7 +272,7 @@ void CAC::encode(uint8_t* data) const
WRITE_BIT(buffer, i, b); WRITE_BIT(buffer, i, b);
} }
uint16_t crc = CRC::addCRC16(buffer, NXDN_CAC_LENGTH_BITS, 5U); uint16_t crc = CRC::addCRC16(buffer, NXDN_CAC_LENGTH_BITS);
#if DEBUG_NXDN_CAC #if DEBUG_NXDN_CAC
Utils::dump(2U, "Encoded CAC", buffer, NXDN_CAC_FEC_LENGTH_BYTES); Utils::dump(2U, "Encoded CAC", buffer, NXDN_CAC_FEC_LENGTH_BYTES);

Loading…
Cancel
Save

Powered by TurnKey Linux.