2018-01-17 21:21:25 +00:00
|
|
|
/*
|
2018-01-22 23:22:32 +00:00
|
|
|
* Copyright (C) 2018 by Jonathan Naylor G4KLX
|
2018-01-17 21:21:25 +00:00
|
|
|
*
|
|
|
|
* 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 "NXDNCRC.h"
|
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cassert>
|
|
|
|
|
2018-01-22 23:22:32 +00:00
|
|
|
const uint8_t BIT_MASK_TABLE1[] = { 0x80U, 0x40U, 0x20U, 0x10U, 0x08U, 0x04U, 0x02U, 0x01U };
|
2018-01-17 21:21:25 +00:00
|
|
|
|
2018-01-22 23:22:32 +00:00
|
|
|
#define WRITE_BIT1(p,i,b) p[(i)>>3] = (b) ? (p[(i)>>3] | BIT_MASK_TABLE1[(i)&7]) : (p[(i)>>3] & ~BIT_MASK_TABLE1[(i)&7])
|
|
|
|
#define READ_BIT1(p,i) (p[(i)>>3] & BIT_MASK_TABLE1[(i)&7])
|
2018-01-17 21:21:25 +00:00
|
|
|
|
2018-01-23 20:06:14 +00:00
|
|
|
bool CNXDNCRC::checkCRC6(const unsigned char* in, unsigned int length)
|
2018-01-17 21:21:25 +00:00
|
|
|
{
|
|
|
|
assert(in != NULL);
|
|
|
|
|
2018-02-01 20:42:29 +00:00
|
|
|
uint8_t crc = createCRC6(in, length);
|
2018-01-17 21:21:25 +00:00
|
|
|
|
2018-02-01 19:07:55 +00:00
|
|
|
uint8_t temp[1U];
|
|
|
|
temp[0U] = 0x00U;
|
|
|
|
unsigned int j = length;
|
2018-02-01 20:42:29 +00:00
|
|
|
for (unsigned int i = 2U; i < 8U; i++, j++) {
|
2018-02-01 19:07:55 +00:00
|
|
|
bool b = READ_BIT1(in, j);
|
|
|
|
WRITE_BIT1(temp, i, b);
|
|
|
|
}
|
|
|
|
|
2018-02-01 20:42:29 +00:00
|
|
|
return crc == temp[0U];
|
2018-01-17 21:21:25 +00:00
|
|
|
}
|
|
|
|
|
2018-01-23 20:06:14 +00:00
|
|
|
void CNXDNCRC::encodeCRC6(unsigned char* in, unsigned int length)
|
2018-01-17 21:21:25 +00:00
|
|
|
{
|
|
|
|
assert(in != NULL);
|
|
|
|
|
2018-01-22 23:22:32 +00:00
|
|
|
uint8_t crc[1U];
|
2018-01-23 20:06:14 +00:00
|
|
|
crc[0U] = createCRC6(in, length);
|
2018-01-17 21:21:25 +00:00
|
|
|
|
2018-01-23 20:06:14 +00:00
|
|
|
unsigned int n = length;
|
2018-02-01 21:28:48 +00:00
|
|
|
for (unsigned int i = 2U; i < 8U; i++, n++) {
|
2018-01-22 23:22:32 +00:00
|
|
|
bool b = READ_BIT1(crc, i);
|
|
|
|
WRITE_BIT1(in, n, b);
|
|
|
|
}
|
2018-01-17 21:21:25 +00:00
|
|
|
}
|
|
|
|
|
2018-01-23 20:06:14 +00:00
|
|
|
bool CNXDNCRC::checkCRC12(const unsigned char* in, unsigned int length)
|
2018-01-17 21:21:25 +00:00
|
|
|
{
|
|
|
|
assert(in != NULL);
|
|
|
|
|
2018-02-01 20:42:29 +00:00
|
|
|
uint16_t crc = createCRC12(in, length);
|
2018-02-01 21:28:48 +00:00
|
|
|
uint8_t temp1[2U];
|
|
|
|
temp1[0U] = (crc >> 8) & 0xFFU;
|
|
|
|
temp1[1U] = (crc >> 0) & 0xFFU;
|
2018-01-17 21:21:25 +00:00
|
|
|
|
2018-02-01 21:28:48 +00:00
|
|
|
uint8_t temp2[2U];
|
|
|
|
temp2[0U] = 0x00U;
|
|
|
|
temp2[1U] = 0x00U;
|
2018-02-01 19:07:55 +00:00
|
|
|
unsigned int j = length;
|
2018-02-01 20:42:29 +00:00
|
|
|
for (unsigned int i = 4U; i < 16U; i++, j++) {
|
2018-02-01 19:07:55 +00:00
|
|
|
bool b = READ_BIT1(in, j);
|
2018-02-01 21:28:48 +00:00
|
|
|
WRITE_BIT1(temp2, i, b);
|
2018-02-01 19:07:55 +00:00
|
|
|
}
|
|
|
|
|
2018-02-01 21:28:48 +00:00
|
|
|
return temp1[0U] == temp2[0U] && temp1[1U] == temp2[1U];
|
2018-01-17 21:21:25 +00:00
|
|
|
}
|
|
|
|
|
2018-01-23 20:06:14 +00:00
|
|
|
void CNXDNCRC::encodeCRC12(unsigned char* in, unsigned int length)
|
2018-01-17 21:21:25 +00:00
|
|
|
{
|
|
|
|
assert(in != NULL);
|
|
|
|
|
2018-02-01 21:28:48 +00:00
|
|
|
uint16_t crc = createCRC12(in, length);
|
|
|
|
|
|
|
|
uint8_t temp[2U];
|
|
|
|
temp[0U] = (crc >> 8) & 0xFFU;
|
|
|
|
temp[1U] = (crc >> 0) & 0xFFU;
|
|
|
|
|
2018-01-23 20:06:14 +00:00
|
|
|
unsigned int n = length;
|
2018-02-01 21:28:48 +00:00
|
|
|
for (unsigned int i = 4U; i < 16U; i++, n++) {
|
|
|
|
bool b = READ_BIT1(temp, i);
|
2018-01-22 23:22:32 +00:00
|
|
|
WRITE_BIT1(in, n, b);
|
|
|
|
}
|
|
|
|
}
|
2018-01-17 21:21:25 +00:00
|
|
|
|
2018-01-23 20:06:14 +00:00
|
|
|
bool CNXDNCRC::checkCRC15(const unsigned char* in, unsigned int length)
|
2018-01-22 23:22:32 +00:00
|
|
|
{
|
|
|
|
assert(in != NULL);
|
2018-01-17 21:21:25 +00:00
|
|
|
|
2018-02-01 20:42:29 +00:00
|
|
|
uint16_t crc = createCRC15(in, length);
|
2018-02-01 21:28:48 +00:00
|
|
|
uint8_t temp1[2U];
|
|
|
|
temp1[0U] = (crc >> 8) & 0xFFU;
|
|
|
|
temp1[1U] = (crc >> 0) & 0xFFU;
|
2018-01-17 21:21:25 +00:00
|
|
|
|
2018-02-01 21:28:48 +00:00
|
|
|
uint16_t temp2[2U];
|
|
|
|
temp2[0U] = 0x00U;
|
|
|
|
temp2[1U] = 0x00U;
|
2018-02-01 19:07:55 +00:00
|
|
|
unsigned int j = length;
|
2018-02-01 20:42:29 +00:00
|
|
|
for (unsigned int i = 1U; i < 16U; i++, j++) {
|
2018-02-01 19:07:55 +00:00
|
|
|
bool b = READ_BIT1(in, j);
|
2018-02-01 21:28:48 +00:00
|
|
|
WRITE_BIT1(temp2, i, b);
|
2018-02-01 19:07:55 +00:00
|
|
|
}
|
|
|
|
|
2018-02-01 21:28:48 +00:00
|
|
|
return temp1[0U] == temp2[0U] && temp1[1U] == temp2[1U];
|
2018-01-17 21:21:25 +00:00
|
|
|
}
|
|
|
|
|
2018-01-23 20:06:14 +00:00
|
|
|
void CNXDNCRC::encodeCRC15(unsigned char* in, unsigned int length)
|
2018-01-17 21:21:25 +00:00
|
|
|
{
|
|
|
|
assert(in != NULL);
|
|
|
|
|
2018-02-01 21:28:48 +00:00
|
|
|
uint16_t crc = createCRC15(in, length);
|
|
|
|
|
|
|
|
uint8_t temp[2U];
|
|
|
|
temp[0U] = (crc >> 8) & 0xFFU;
|
|
|
|
temp[1U] = (crc >> 0) & 0xFFU;
|
|
|
|
|
2018-01-23 20:06:14 +00:00
|
|
|
unsigned int n = length;
|
2018-02-01 21:28:48 +00:00
|
|
|
for (unsigned int i = 1U; i < 16U; i++, n++) {
|
|
|
|
bool b = READ_BIT1(temp, i);
|
2018-01-22 23:22:32 +00:00
|
|
|
WRITE_BIT1(in, n, b);
|
|
|
|
}
|
|
|
|
}
|
2018-01-17 21:21:25 +00:00
|
|
|
|
2018-01-23 20:06:14 +00:00
|
|
|
uint8_t CNXDNCRC::createCRC6(const unsigned char* in, unsigned int length)
|
2018-01-22 23:22:32 +00:00
|
|
|
{
|
2018-02-01 19:07:55 +00:00
|
|
|
uint8_t crc = 0x3FU;
|
2018-01-17 21:21:25 +00:00
|
|
|
|
2018-01-23 20:06:14 +00:00
|
|
|
for (unsigned int i = 0U; i < length; i++) {
|
|
|
|
bool bit1 = READ_BIT1(in, i) != 0x00U;
|
|
|
|
bool bit2 = (crc & 0x20U) == 0x20U;
|
|
|
|
|
|
|
|
crc <<= 1;
|
|
|
|
|
2018-02-01 19:07:55 +00:00
|
|
|
if (bit1 ^ bit2)
|
2018-01-23 20:06:14 +00:00
|
|
|
crc ^= 0x27U;
|
|
|
|
}
|
|
|
|
|
2018-02-05 21:21:08 +00:00
|
|
|
return crc & 0x3FU;
|
2018-01-17 21:21:25 +00:00
|
|
|
}
|
|
|
|
|
2018-01-23 20:06:14 +00:00
|
|
|
uint16_t CNXDNCRC::createCRC12(const unsigned char* in, unsigned int length)
|
2018-01-17 21:21:25 +00:00
|
|
|
{
|
2018-02-01 19:07:55 +00:00
|
|
|
uint16_t crc = 0x0FFFU;
|
2018-01-23 20:06:14 +00:00
|
|
|
|
|
|
|
for (unsigned int i = 0U; i < length; i++) {
|
|
|
|
bool bit1 = READ_BIT1(in, i) != 0x00U;
|
|
|
|
bool bit2 = (crc & 0x0800U) == 0x0800U;
|
|
|
|
|
|
|
|
crc <<= 1;
|
|
|
|
|
2018-02-01 19:07:55 +00:00
|
|
|
if (bit1 ^ bit2)
|
2018-01-23 20:06:14 +00:00
|
|
|
crc ^= 0x080FU;
|
|
|
|
}
|
2018-01-17 21:21:25 +00:00
|
|
|
|
2018-02-01 19:07:55 +00:00
|
|
|
return crc & 0x0FFFU;
|
2018-01-22 23:22:32 +00:00
|
|
|
}
|
2018-01-17 21:21:25 +00:00
|
|
|
|
2018-01-23 20:06:14 +00:00
|
|
|
uint16_t CNXDNCRC::createCRC15(const unsigned char* in, unsigned int length)
|
2018-01-22 23:22:32 +00:00
|
|
|
{
|
2018-02-01 19:07:55 +00:00
|
|
|
uint16_t crc = 0x7FFFU;
|
2018-01-23 20:06:14 +00:00
|
|
|
|
|
|
|
for (unsigned int i = 0U; i < length; i++) {
|
|
|
|
bool bit1 = READ_BIT1(in, i) != 0x00U;
|
|
|
|
bool bit2 = (crc & 0x4000U) == 0x4000U;
|
|
|
|
|
|
|
|
crc <<= 1;
|
|
|
|
|
2018-02-01 19:07:55 +00:00
|
|
|
if (bit1 ^ bit2)
|
2018-01-23 20:06:14 +00:00
|
|
|
crc ^= 0x4CC5U;
|
|
|
|
}
|
2018-01-17 21:21:25 +00:00
|
|
|
|
2018-02-01 19:07:55 +00:00
|
|
|
return crc & 0x7FFFU;
|
2018-01-17 21:21:25 +00:00
|
|
|
}
|