Simplify the LICH processing.

This commit is contained in:
Jonathan Naylor 2018-03-21 18:44:33 +00:00
parent 35d3ad77a8
commit d1b389d5ce
2 changed files with 35 additions and 31 deletions

View file

@ -50,40 +50,35 @@ bool CNXDNLICH::decode(const unsigned char* bytes)
{ {
assert(bytes != NULL); assert(bytes != NULL);
bool b[8U]; unsigned int offset = NXDN_FSW_LENGTH_BITS;
unsigned int offset1 = NXDN_FSW_LENGTH_BITS; for (unsigned int i = 0U; i < (NXDN_LICH_LENGTH_BITS / 2U); i++, offset += 2U) {
unsigned int offset2 = 7U; bool b = READ_BIT1(bytes, offset);
for (unsigned int i = 0U; i < (NXDN_LICH_LENGTH_BITS / 2U); i++, offset1 += 2U, offset2--) { WRITE_BIT1(m_lich, i, b);
b[offset2] = READ_BIT1(bytes, offset1);
WRITE_BIT1(m_lich, i, b[offset2]);
} }
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 origParity == newParity;
return false;
return true;
} }
void CNXDNLICH::encode(unsigned char* bytes) void CNXDNLICH::encode(unsigned char* bytes)
{ {
assert(bytes != NULL); assert(bytes != NULL);
bool b[8U]; bool parity = getParity();
unsigned int offset = 7U; if (parity)
for (unsigned int i = 0U; i < (NXDN_LICH_LENGTH_BITS / 2U); i++, offset--) m_lich[0U] |= 0x01U;
b[offset] = READ_BIT1(m_lich, i); else
m_lich[0U] &= 0xFEU;
b[0U] = b[7U] ^ b[6U] ^ b[5U] ^ b[4U]; unsigned int offset = NXDN_FSW_LENGTH_BITS;
for (unsigned int i = 0U; i < (NXDN_LICH_LENGTH_BITS / 2U); i++) {
unsigned int offset1 = NXDN_FSW_LENGTH_BITS; bool b = READ_BIT1(m_lich, i);
unsigned int offset2 = 7U; WRITE_BIT1(bytes, offset, b);
for (unsigned int i = 0U; i < (NXDN_LICH_LENGTH_BITS / 2U); i++, offset2--) { offset++;
WRITE_BIT1(bytes, offset1, b[offset2]); WRITE_BIT1(bytes, offset, true);
offset1++; offset++;
WRITE_BIT1(bytes, offset1, true);
offset1++;
} }
} }
@ -109,15 +104,11 @@ unsigned char CNXDNLICH::getDirection() const
unsigned char CNXDNLICH::getRaw() const unsigned char CNXDNLICH::getRaw() const
{ {
switch (m_lich[0U] & 0xF0U) { bool parity = getParity();
case 0x80U: if (parity)
case 0xB0U:
m_lich[0U] |= 0x01U; m_lich[0U] |= 0x01U;
break; else
default:
m_lich[0U] &= 0xFEU; m_lich[0U] &= 0xFEU;
break;
}
return m_lich[0U]; return m_lich[0U];
} }
@ -158,3 +149,14 @@ CNXDNLICH& CNXDNLICH::operator=(const CNXDNLICH& lich)
return *this; return *this;
} }
bool CNXDNLICH::getParity() const
{
switch (m_lich[0U] & 0xF0U) {
case 0x80U:
case 0xB0U:
return true;
default:
return false;
}
}

View file

@ -45,6 +45,8 @@ public:
private: private:
unsigned char* m_lich; unsigned char* m_lich;
bool getParity() const;
}; };
#endif #endif