From d1b389d5cee3d8ddd71913c4f8b697053782db9b Mon Sep 17 00:00:00 2001 From: Jonathan Naylor Date: Wed, 21 Mar 2018 18:44:33 +0000 Subject: [PATCH] Simplify the LICH processing. --- NXDNLICH.cpp | 64 +++++++++++++++++++++++++++------------------------- NXDNLICH.h | 2 ++ 2 files changed, 35 insertions(+), 31 deletions(-) diff --git a/NXDNLICH.cpp b/NXDNLICH.cpp index 4915804..a456103 100644 --- a/NXDNLICH.cpp +++ b/NXDNLICH.cpp @@ -50,40 +50,35 @@ bool CNXDNLICH::decode(const unsigned char* bytes) { assert(bytes != NULL); - bool b[8U]; - unsigned int offset1 = NXDN_FSW_LENGTH_BITS; - unsigned int offset2 = 7U; - for (unsigned int i = 0U; i < (NXDN_LICH_LENGTH_BITS / 2U); i++, offset1 += 2U, offset2--) { - b[offset2] = READ_BIT1(bytes, offset1); - WRITE_BIT1(m_lich, i, b[offset2]); + unsigned int offset = NXDN_FSW_LENGTH_BITS; + for (unsigned int i = 0U; i < (NXDN_LICH_LENGTH_BITS / 2U); i++, offset += 2U) { + bool b = READ_BIT1(bytes, offset); + WRITE_BIT1(m_lich, i, b); } - bool parity = b[7U] ^ b[6U] ^ b[5U] ^ b[4U]; + bool newParity = getParity(); + bool origParity = (m_lich[0U] & 0x01U) == 0x01U; - if (parity != b[0U]) - return false; - - return true; + return origParity == newParity; } void CNXDNLICH::encode(unsigned char* bytes) { assert(bytes != NULL); - bool b[8U]; - unsigned int offset = 7U; - for (unsigned int i = 0U; i < (NXDN_LICH_LENGTH_BITS / 2U); i++, offset--) - b[offset] = READ_BIT1(m_lich, i); + bool parity = getParity(); + if (parity) + m_lich[0U] |= 0x01U; + else + m_lich[0U] &= 0xFEU; - b[0U] = b[7U] ^ b[6U] ^ b[5U] ^ b[4U]; - - unsigned int offset1 = NXDN_FSW_LENGTH_BITS; - unsigned int offset2 = 7U; - for (unsigned int i = 0U; i < (NXDN_LICH_LENGTH_BITS / 2U); i++, offset2--) { - WRITE_BIT1(bytes, offset1, b[offset2]); - offset1++; - WRITE_BIT1(bytes, offset1, true); - offset1++; + unsigned int offset = NXDN_FSW_LENGTH_BITS; + for (unsigned int i = 0U; i < (NXDN_LICH_LENGTH_BITS / 2U); i++) { + bool b = READ_BIT1(m_lich, i); + WRITE_BIT1(bytes, offset, b); + offset++; + WRITE_BIT1(bytes, offset, true); + offset++; } } @@ -109,15 +104,11 @@ unsigned char CNXDNLICH::getDirection() const unsigned char CNXDNLICH::getRaw() const { - switch (m_lich[0U] & 0xF0U) { - case 0x80U: - case 0xB0U: + bool parity = getParity(); + if (parity) m_lich[0U] |= 0x01U; - break; - default: + else m_lich[0U] &= 0xFEU; - break; - } return m_lich[0U]; } @@ -158,3 +149,14 @@ CNXDNLICH& CNXDNLICH::operator=(const CNXDNLICH& lich) return *this; } + +bool CNXDNLICH::getParity() const +{ + switch (m_lich[0U] & 0xF0U) { + case 0x80U: + case 0xB0U: + return true; + default: + return false; + } +} diff --git a/NXDNLICH.h b/NXDNLICH.h index ab220a1..c3fe821 100644 --- a/NXDNLICH.h +++ b/NXDNLICH.h @@ -45,6 +45,8 @@ public: private: unsigned char* m_lich; + + bool getParity() const; }; #endif