implement proper LDU2 encode/decode; implement proper HDU (Header) encode/decode

This commit is contained in:
Bryan Biedenkapp
2018-10-03 23:06:56 -04:00
parent f5a70aa6ce
commit 85bc8357aa
6 changed files with 433 additions and 108 deletions

View File

@@ -22,25 +22,13 @@
#include "P25Utils.h"
#include "CRC.h"
#include "Hamming.h"
#include "Golay24128.h"
#include "Utils.h"
#include "Log.h"
#include <cstdio>
#include <cassert>
#include <cstring>
const unsigned char DUMMY_HEADER[] = {
0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U,
0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x08U, 0xDCU, 0x60U,
0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x02U, 0x93U, 0xE7U, 0x73U, 0x77U, 0x57U, 0xD6U, 0xD3U, 0xCFU, 0x77U,
0xEEU, 0x82U, 0x93U, 0xE2U, 0x2FU, 0xF3U, 0xD5U, 0xF5U, 0xBEU, 0xBCU, 0x54U, 0x0DU, 0x9CU, 0x29U, 0x3EU, 0x46U,
0xE3U, 0x28U, 0xB0U, 0xB7U, 0x73U, 0x76U, 0x1EU, 0x26U, 0x0CU, 0x75U, 0x5BU, 0xF7U, 0x4DU, 0x5FU, 0x5AU, 0x37U,
0x18U};
const unsigned char DUMMY_LDU2[] = {
0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x80U, 0x00U, 0x00U, 0xACU, 0xB8U, 0xA4U, 0x9BU,
0xDCU, 0x75U
};
const unsigned char BIT_MASK_TABLE[] = { 0x80U, 0x40U, 0x20U, 0x10U, 0x08U, 0x04U, 0x02U, 0x01U };
@@ -67,11 +55,70 @@ CP25Data::~CP25Data()
delete[] m_mi;
}
void CP25Data::encodeHeader(unsigned char* data)
bool CP25Data::decodeHeader(const unsigned char* data)
{
assert(data != NULL);
CP25Utils::encode(DUMMY_HEADER, data, 114U, 780U);
// deinterleave
unsigned char rs[81U];
unsigned char raw[81U];
CP25Utils::decode(data, raw, 114U, 780U);
// decode Golay (18,6,8) FEC
decodeHeaderGolay(raw, rs);
// decode RS (36,20,17) FEC
try {
bool ret = m_rs241213.decode362017(rs);
if (!ret)
return false;
} catch (...) {
CUtils::dump(2U, "P25, RS crashed with input data", rs, 81U);
return false;
}
m_mfId = rs[9U]; // Mfg Id.
m_algId = rs[10U]; // Algorithm ID
if (m_algId != P25_ALGO_UNENCRYPT) {
m_mi = new unsigned char[P25_MI_LENGTH_BYTES];
::memset(m_mi, 0x00U, P25_MI_LENGTH_BYTES);
::memcpy(m_mi, rs, P25_MI_LENGTH_BYTES); // Message Indicator
m_kId = (rs[11U] << 8) + rs[12U]; // Key ID
}
else {
m_mi = new unsigned char[P25_MI_LENGTH_BYTES];
::memset(m_mi, 0x00U, P25_MI_LENGTH_BYTES);
m_kId = 0x0000U;
}
return true;
}
void CP25Data::encodeHeader(unsigned char* data)
{
assert(data != NULL);
assert(m_mi != NULL);
unsigned char rs[81U];
::memset(rs, 0x00U, 81U);
rs[9U] = m_mfId; // Mfg Id.
rs[13U] = (m_dstId >> 8) & 0xFFU; // Talkgroup Address MSB
rs[14U] = (m_dstId >> 0) & 0xFFU; // Talkgroup Address LSB
// encode RS (36,20,17) FEC
m_rs241213.encode362017(rs);
unsigned char raw[81U];
::memset(raw, 0x00U, 81U);
// encode Golay (18,6,8) FEC
encodeHeaderGolay(raw, rs);
// interleave
CP25Utils::encode(raw, data, 114U, 780U);
}
bool CP25Data::decodeLDU1(const unsigned char* data)
@@ -185,27 +232,98 @@ void CP25Data::encodeLDU1(unsigned char* data)
CP25Utils::encode(raw, data, 1356U, 1398U);
}
void CP25Data::encodeLDU2(unsigned char* data)
bool CP25Data::decodeLDU2(const unsigned char* data)
{
assert(data != NULL);
unsigned char rs[18U];
// deinterleave and decode Hamming (10,6,3) for LC data
unsigned char raw[5U];
encodeLDUHamming(raw, DUMMY_LDU2 + 0U);
CP25Utils::decode(data, raw, 410U, 452U);
decodeLDUHamming(raw, rs + 0U);
CP25Utils::decode(data, raw, 600U, 640U);
decodeLDUHamming(raw, rs + 3U);
CP25Utils::decode(data, raw, 788U, 830U);
decodeLDUHamming(raw, rs + 6U);
CP25Utils::decode(data, raw, 978U, 1020U);
decodeLDUHamming(raw, rs + 9U);
CP25Utils::decode(data, raw, 1168U, 1208U);
decodeLDUHamming(raw, rs + 12U);
CP25Utils::decode(data, raw, 1356U, 1398U);
decodeLDUHamming(raw, rs + 15U);
// CUtils::dump(2U, "LDU2 RS", rs, P25_LDU_LC_LENGTH_BYTES);
// decode RS (24,16,9) FEC
try {
bool ret = m_rs241213.decode24169(rs);
if (!ret)
return false;
} catch (...) {
CUtils::dump(2U, "P25, RS crashed with input data", rs, 18U);
return false;
}
m_algId = rs[9U]; // Algorithm ID
if (m_algId != P25_ALGO_UNENCRYPT) {
m_mi = new unsigned char[P25_MI_LENGTH_BYTES];
::memset(m_mi, 0x00U, P25_MI_LENGTH_BYTES);
::memcpy(m_mi, rs, P25_MI_LENGTH_BYTES); // Message Indicator
m_kId = (rs[10U] << 8) + rs[11U]; // Key ID
}
else {
m_mi = new unsigned char[P25_MI_LENGTH_BYTES];
::memset(m_mi, 0x00U, P25_MI_LENGTH_BYTES);
m_kId = 0x0000U;
}
return true;
}
void CP25Data::encodeLDU2(unsigned char* data)
{
assert(data != NULL);
assert(m_mi != NULL);
unsigned char rs[18U];
::memset(rs, 0x00U, 18U);
for (unsigned int i = 0; i < P25_MI_LENGTH_BYTES; i++)
rs[i] = m_mi[i]; // Message Indicator
rs[9U] = m_algId; // Algorithm ID
rs[10U] = (m_kId >> 8) & 0xFFU; // Key ID MSB
rs[11U] = (m_kId >> 0) & 0xFFU; // Key ID LSB
// encode RS (24,16,9) FEC
m_rs241213.encode24169(rs);
// encode Hamming (10,6,3) FEC and interleave for LC data
unsigned char raw[5U];
encodeLDUHamming(raw, rs + 0U);
CP25Utils::encode(raw, data, 410U, 452U);
encodeLDUHamming(raw, DUMMY_LDU2 + 3U);
encodeLDUHamming(raw, rs + 3U);
CP25Utils::encode(raw, data, 600U, 640U);
encodeLDUHamming(raw, DUMMY_LDU2 + 6U);
encodeLDUHamming(raw, rs + 6U);
CP25Utils::encode(raw, data, 788U, 830U);
encodeLDUHamming(raw, DUMMY_LDU2 + 9U);
encodeLDUHamming(raw, rs + 9U);
CP25Utils::encode(raw, data, 978U, 1020U);
encodeLDUHamming(raw, DUMMY_LDU2 + 12U);
encodeLDUHamming(raw, rs + 12U);
CP25Utils::encode(raw, data, 1168U, 1208U);
encodeLDUHamming(raw, DUMMY_LDU2 + 15U);
encodeLDUHamming(raw, rs + 15U);
CP25Utils::encode(raw, data, 1356U, 1398U);
}
@@ -467,3 +585,59 @@ void CP25Data::encodeLDUHamming(unsigned char* data, const unsigned char* raw)
}
}
}
void CP25Data::decodeHeaderGolay(const unsigned char* data, unsigned char* raw)
{
// shortened Golay (18,6,8) decode
unsigned int n = 0U;
unsigned int m = 0U;
for (unsigned int i = 0U; i < 36U; i++) {
bool golay[18U];
for (unsigned int j = 0U; j < 18U; j++) {
golay[j] = READ_BIT(data, n);
n++;
}
unsigned int g0 = 0U;
for (unsigned int j = 0U; j < 18U; j++)
g0 = (g0 << 1) | (golay[j] ? 0x01U : 0x00U);
unsigned int c0data = CGolay24128::decode24128(g0);
for (int j = 5; j >= 0; j--) {
golay[j] = (c0data & 0x01U) == 0x01U;
c0data >>= 1;
}
for (unsigned int j = 0U; j < 6U; j++) {
WRITE_BIT(raw, m, golay[j]);
m++;
}
}
}
void CP25Data::encodeHeaderGolay(unsigned char* data, const unsigned char* raw)
{
// shortened Golay (18,6,8) encode
unsigned int n = 0U;
unsigned int m = 0U;
for (unsigned int i = 0U; i < 36U; i++) {
bool golay[18U];
for (unsigned int j = 0U; j < 6U; j++) {
golay[j] = READ_BIT(raw, m);
m++;
}
unsigned int c0data = 0U;
for (unsigned int j = 0U; j < 6U; j++)
c0data = (c0data << 1) | (golay[j] ? 0x01U : 0x00U);
unsigned int g0 = CGolay24128::encode24128(c0data);
for (int j = 17; j >= 0; j--) {
golay[j] = (g0 & 0x01U) == 0x01U;
g0 >>= 1;
}
for (unsigned int j = 0U; j < 18U; j++) {
WRITE_BIT(data, n, golay[j]);
n++;
}
}
}