2018-01-16 20:10:35 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2018 by Jonathan Naylor G4KLX
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "NXDNDefines.h"
|
|
|
|
#include "NXDNLICH.h"
|
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
const unsigned char BIT_MASK_TABLE[] = {0x80U, 0x40U, 0x20U, 0x10U, 0x08U, 0x04U, 0x02U, 0x01U};
|
|
|
|
|
|
|
|
#define WRITE_BIT1(p,i,b) p[(i)>>3] = (b) ? (p[(i)>>3] | BIT_MASK_TABLE[(i)&7]) : (p[(i)>>3] & ~BIT_MASK_TABLE[(i)&7])
|
|
|
|
#define READ_BIT1(p,i) (p[(i)>>3] & BIT_MASK_TABLE[(i)&7])
|
|
|
|
|
|
|
|
CNXDNLICH::CNXDNLICH(const CNXDNLICH& lich) :
|
2018-01-26 07:12:22 +00:00
|
|
|
m_lich(NULL)
|
2018-01-16 20:10:35 +00:00
|
|
|
{
|
2018-01-26 07:12:22 +00:00
|
|
|
m_lich = new unsigned char[1U];
|
|
|
|
m_lich[0U] = lich.m_lich[0U];
|
2018-01-16 20:10:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CNXDNLICH::CNXDNLICH() :
|
2018-01-26 07:12:22 +00:00
|
|
|
m_lich(NULL)
|
2018-01-16 20:10:35 +00:00
|
|
|
{
|
2018-01-26 07:12:22 +00:00
|
|
|
m_lich = new unsigned char[1U];
|
2018-01-16 20:10:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CNXDNLICH::~CNXDNLICH()
|
|
|
|
{
|
2018-01-26 07:12:22 +00:00
|
|
|
delete[] m_lich;
|
2018-01-16 20:10:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CNXDNLICH::decode(const unsigned char* bytes)
|
|
|
|
{
|
|
|
|
assert(bytes != NULL);
|
|
|
|
|
2018-03-21 18:44:33 +00:00
|
|
|
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);
|
2018-01-18 21:26:31 +00:00
|
|
|
}
|
2018-01-16 20:10:35 +00:00
|
|
|
|
2018-03-21 18:44:33 +00:00
|
|
|
bool newParity = getParity();
|
|
|
|
bool origParity = (m_lich[0U] & 0x01U) == 0x01U;
|
2018-03-20 20:36:23 +00:00
|
|
|
|
2018-03-21 18:44:33 +00:00
|
|
|
return origParity == newParity;
|
2018-01-16 20:10:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CNXDNLICH::encode(unsigned char* bytes)
|
|
|
|
{
|
|
|
|
assert(bytes != NULL);
|
|
|
|
|
2018-03-21 18:44:33 +00:00
|
|
|
bool parity = getParity();
|
|
|
|
if (parity)
|
|
|
|
m_lich[0U] |= 0x01U;
|
|
|
|
else
|
|
|
|
m_lich[0U] &= 0xFEU;
|
2018-01-18 21:26:31 +00:00
|
|
|
|
2018-03-21 18:44:33 +00:00
|
|
|
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++;
|
2018-01-18 21:26:31 +00:00
|
|
|
}
|
2018-01-16 20:10:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char CNXDNLICH::getRFCT() const
|
|
|
|
{
|
2018-01-26 07:12:22 +00:00
|
|
|
return (m_lich[0U] >> 6) & 0x03U;
|
2018-01-16 20:10:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char CNXDNLICH::getFCT() const
|
|
|
|
{
|
2018-01-26 07:12:22 +00:00
|
|
|
return (m_lich[0U] >> 4) & 0x03U;
|
2018-01-16 20:10:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char CNXDNLICH::getOption() const
|
|
|
|
{
|
2018-01-26 07:12:22 +00:00
|
|
|
return (m_lich[0U] >> 2) & 0x03U;
|
2018-01-16 20:10:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char CNXDNLICH::getDirection() const
|
|
|
|
{
|
2018-01-26 07:12:22 +00:00
|
|
|
return (m_lich[0U] >> 1) & 0x01U;
|
2018-01-16 20:10:35 +00:00
|
|
|
}
|
|
|
|
|
2018-02-21 07:48:24 +00:00
|
|
|
unsigned char CNXDNLICH::getRaw() const
|
2018-02-15 20:20:07 +00:00
|
|
|
{
|
2018-03-21 18:44:33 +00:00
|
|
|
bool parity = getParity();
|
|
|
|
if (parity)
|
2018-03-20 20:43:48 +00:00
|
|
|
m_lich[0U] |= 0x01U;
|
2018-03-21 18:44:33 +00:00
|
|
|
else
|
2018-03-20 20:43:48 +00:00
|
|
|
m_lich[0U] &= 0xFEU;
|
|
|
|
|
2018-02-15 20:20:07 +00:00
|
|
|
return m_lich[0U];
|
|
|
|
}
|
|
|
|
|
2018-01-16 20:10:35 +00:00
|
|
|
void CNXDNLICH::setRFCT(unsigned char rfct)
|
|
|
|
{
|
2018-01-26 07:12:22 +00:00
|
|
|
m_lich[0U] &= 0x3FU;
|
|
|
|
m_lich[0U] |= (rfct << 6) & 0xC0U;
|
2018-01-16 20:10:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CNXDNLICH::setFCT(unsigned char usc)
|
|
|
|
{
|
2018-01-26 07:12:22 +00:00
|
|
|
m_lich[0U] &= 0xCFU;
|
|
|
|
m_lich[0U] |= (usc << 4) & 0x30U;
|
2018-01-16 20:10:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CNXDNLICH::setOption(unsigned char option)
|
|
|
|
{
|
2018-01-26 07:12:22 +00:00
|
|
|
m_lich[0U] &= 0xF3U;
|
2018-03-20 20:43:48 +00:00
|
|
|
m_lich[0U] |= (option << 2) & 0x0CU;
|
2018-01-16 20:10:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CNXDNLICH::setDirection(unsigned char direction)
|
|
|
|
{
|
2018-01-26 07:12:22 +00:00
|
|
|
m_lich[0U] &= 0xFDU;
|
|
|
|
m_lich[0U] |= (direction << 1) & 0x02U;
|
2018-01-16 20:10:35 +00:00
|
|
|
}
|
|
|
|
|
2018-02-21 07:48:24 +00:00
|
|
|
void CNXDNLICH::setRaw(unsigned char lich)
|
2018-02-15 20:20:07 +00:00
|
|
|
{
|
|
|
|
m_lich[0U] = lich;
|
|
|
|
}
|
|
|
|
|
2018-01-16 20:10:35 +00:00
|
|
|
CNXDNLICH& CNXDNLICH::operator=(const CNXDNLICH& lich)
|
|
|
|
{
|
|
|
|
if (&lich != this)
|
2018-01-26 07:12:22 +00:00
|
|
|
m_lich[0U] = lich.m_lich[0U];
|
2018-01-16 20:10:35 +00:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
2018-03-21 18:44:33 +00:00
|
|
|
|
|
|
|
bool CNXDNLICH::getParity() const
|
|
|
|
{
|
|
|
|
switch (m_lich[0U] & 0xF0U) {
|
|
|
|
case 0x80U:
|
|
|
|
case 0xB0U:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|