2016-09-12 22:26:05 +00:00
|
|
|
/*
|
2018-01-03 20:03:10 +00:00
|
|
|
* Copyright (C) 2016,2018 by Jonathan Naylor G4KLX
|
2018-05-14 16:00:06 +00:00
|
|
|
* Copyright (C) 2018 by Bryan Biedenkapp <gatekeep@gmail.com>
|
2016-09-12 22:26:05 +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 "P25NID.h"
|
|
|
|
#include "P25Defines.h"
|
2016-09-13 17:27:50 +00:00
|
|
|
#include "P25Utils.h"
|
2016-09-19 20:41:34 +00:00
|
|
|
#include "BCH.h"
|
2016-09-12 22:26:05 +00:00
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cassert>
|
|
|
|
|
2016-09-19 21:26:42 +00:00
|
|
|
const unsigned int MAX_NID_ERRS = 5U;
|
|
|
|
|
2016-09-19 20:41:34 +00:00
|
|
|
CP25NID::CP25NID(unsigned int nac) :
|
2016-09-12 22:26:05 +00:00
|
|
|
m_duid(0U),
|
2016-09-19 20:41:34 +00:00
|
|
|
m_hdr(NULL),
|
|
|
|
m_ldu1(NULL),
|
|
|
|
m_ldu2(NULL),
|
|
|
|
m_termlc(NULL),
|
2018-01-03 20:03:10 +00:00
|
|
|
m_term(NULL),
|
2018-07-04 20:41:31 +00:00
|
|
|
m_tsdu(NULL),
|
|
|
|
m_pdu(NULL)
|
2016-09-12 22:26:05 +00:00
|
|
|
{
|
2016-09-19 20:41:34 +00:00
|
|
|
CBCH bch;
|
|
|
|
|
|
|
|
m_hdr = new unsigned char[P25_NID_LENGTH_BYTES];
|
|
|
|
m_hdr[0U] = (nac >> 4) & 0xFFU;
|
|
|
|
m_hdr[1U] = (nac << 4) & 0xF0U;
|
|
|
|
m_hdr[1U] |= P25_DUID_HEADER;
|
|
|
|
bch.encode(m_hdr);
|
|
|
|
m_hdr[7U] &= 0xFEU; // Clear the parity bit
|
|
|
|
|
|
|
|
m_ldu1 = new unsigned char[P25_NID_LENGTH_BYTES];
|
|
|
|
m_ldu1[0U] = (nac >> 4) & 0xFFU;
|
|
|
|
m_ldu1[1U] = (nac << 4) & 0xF0U;
|
|
|
|
m_ldu1[1U] |= P25_DUID_LDU1;
|
|
|
|
bch.encode(m_ldu1);
|
|
|
|
m_ldu1[7U] |= 0x01U; // Set the parity bit
|
|
|
|
|
|
|
|
m_ldu2 = new unsigned char[P25_NID_LENGTH_BYTES];
|
|
|
|
m_ldu2[0U] = (nac >> 4) & 0xFFU;
|
|
|
|
m_ldu2[1U] = (nac << 4) & 0xF0U;
|
|
|
|
m_ldu2[1U] |= P25_DUID_LDU2;
|
|
|
|
bch.encode(m_ldu2);
|
|
|
|
m_ldu2[7U] |= 0x01U; // Set the parity bit
|
|
|
|
|
|
|
|
m_termlc = new unsigned char[P25_NID_LENGTH_BYTES];
|
|
|
|
m_termlc[0U] = (nac >> 4) & 0xFFU;
|
|
|
|
m_termlc[1U] = (nac << 4) & 0xF0U;
|
|
|
|
m_termlc[1U] |= P25_DUID_TERM_LC;
|
|
|
|
bch.encode(m_termlc);
|
|
|
|
m_termlc[7U] &= 0xFEU; // Clear the parity bit
|
|
|
|
|
|
|
|
m_term = new unsigned char[P25_NID_LENGTH_BYTES];
|
|
|
|
m_term[0U] = (nac >> 4) & 0xFFU;
|
|
|
|
m_term[1U] = (nac << 4) & 0xF0U;
|
|
|
|
m_term[1U] |= P25_DUID_TERM;
|
|
|
|
bch.encode(m_term);
|
|
|
|
m_term[7U] &= 0xFEU; // Clear the parity bit
|
2018-01-03 20:03:10 +00:00
|
|
|
|
2018-05-14 16:00:06 +00:00
|
|
|
m_tsdu = new unsigned char[P25_NID_LENGTH_BYTES];
|
|
|
|
m_tsdu[0U] = (nac >> 4) & 0xFFU;
|
|
|
|
m_tsdu[1U] = (nac << 4) & 0xF0U;
|
|
|
|
m_tsdu[1U] |= P25_DUID_TSDU;
|
|
|
|
bch.encode(m_tsdu);
|
|
|
|
m_tsdu[7U] &= 0xFEU; // Clear the parity bit
|
|
|
|
|
2018-01-03 20:03:10 +00:00
|
|
|
m_pdu = new unsigned char[P25_NID_LENGTH_BYTES];
|
|
|
|
m_pdu[0U] = (nac >> 4) & 0xFFU;
|
|
|
|
m_pdu[1U] = (nac << 4) & 0xF0U;
|
|
|
|
m_pdu[1U] |= P25_DUID_PDU;
|
|
|
|
bch.encode(m_pdu);
|
|
|
|
m_pdu[7U] &= 0xFEU; // Clear the parity bit
|
2016-09-12 22:26:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CP25NID::~CP25NID()
|
|
|
|
{
|
2016-09-19 20:41:34 +00:00
|
|
|
delete[] m_hdr;
|
|
|
|
delete[] m_ldu1;
|
|
|
|
delete[] m_ldu2;
|
|
|
|
delete[] m_termlc;
|
|
|
|
delete[] m_term;
|
2018-07-03 07:14:37 +00:00
|
|
|
delete[] m_tsdu;
|
2018-01-03 20:03:10 +00:00
|
|
|
delete[] m_pdu;
|
2016-09-12 22:26:05 +00:00
|
|
|
}
|
|
|
|
|
2016-09-19 21:26:42 +00:00
|
|
|
bool CP25NID::decode(const unsigned char* data)
|
2016-09-12 22:26:05 +00:00
|
|
|
{
|
|
|
|
assert(data != NULL);
|
|
|
|
|
|
|
|
unsigned char nid[P25_NID_LENGTH_BYTES];
|
2016-09-13 17:27:50 +00:00
|
|
|
CP25Utils::decode(data, nid, 48U, 114U);
|
2016-09-12 22:26:05 +00:00
|
|
|
|
2016-09-20 05:53:30 +00:00
|
|
|
unsigned int errs = CP25Utils::compare(nid, m_ldu1, P25_NID_LENGTH_BYTES);
|
2016-09-19 21:26:42 +00:00
|
|
|
if (errs < MAX_NID_ERRS) {
|
|
|
|
m_duid = P25_DUID_LDU1;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-09-20 05:53:30 +00:00
|
|
|
errs = CP25Utils::compare(nid, m_ldu2, P25_NID_LENGTH_BYTES);
|
2016-09-19 21:26:42 +00:00
|
|
|
if (errs < MAX_NID_ERRS) {
|
|
|
|
m_duid = P25_DUID_LDU2;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-09-20 05:53:30 +00:00
|
|
|
errs = CP25Utils::compare(nid, m_term, P25_NID_LENGTH_BYTES);
|
2016-09-19 21:26:42 +00:00
|
|
|
if (errs < MAX_NID_ERRS) {
|
|
|
|
m_duid = P25_DUID_TERM;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-09-20 05:53:30 +00:00
|
|
|
errs = CP25Utils::compare(nid, m_termlc, P25_NID_LENGTH_BYTES);
|
2016-09-19 21:26:42 +00:00
|
|
|
if (errs < MAX_NID_ERRS) {
|
|
|
|
m_duid = P25_DUID_TERM_LC;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-09-20 05:53:30 +00:00
|
|
|
errs = CP25Utils::compare(nid, m_hdr, P25_NID_LENGTH_BYTES);
|
2016-09-19 21:26:42 +00:00
|
|
|
if (errs < MAX_NID_ERRS) {
|
|
|
|
m_duid = P25_DUID_HEADER;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-05-14 16:00:06 +00:00
|
|
|
errs = CP25Utils::compare(nid, m_tsdu, P25_NID_LENGTH_BYTES);
|
|
|
|
if (errs < MAX_NID_ERRS) {
|
|
|
|
m_duid = P25_DUID_TSDU;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-01-03 20:03:10 +00:00
|
|
|
errs = CP25Utils::compare(nid, m_pdu, P25_NID_LENGTH_BYTES);
|
|
|
|
if (errs < MAX_NID_ERRS) {
|
|
|
|
m_duid = P25_DUID_PDU;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-09-19 21:26:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-09-12 22:26:05 +00:00
|
|
|
|
2016-09-19 21:26:42 +00:00
|
|
|
void CP25NID::encode(unsigned char* data, unsigned char duid) const
|
|
|
|
{
|
|
|
|
assert(data != NULL);
|
2016-09-19 20:41:34 +00:00
|
|
|
|
2016-09-19 21:26:42 +00:00
|
|
|
switch (duid) {
|
|
|
|
case P25_DUID_HEADER:
|
|
|
|
CP25Utils::encode(m_hdr, data, 48U, 114U);
|
|
|
|
break;
|
|
|
|
case P25_DUID_LDU1:
|
|
|
|
CP25Utils::encode(m_ldu1, data, 48U, 114U);
|
|
|
|
break;
|
|
|
|
case P25_DUID_LDU2:
|
|
|
|
CP25Utils::encode(m_ldu2, data, 48U, 114U);
|
|
|
|
break;
|
|
|
|
case P25_DUID_TERM:
|
|
|
|
CP25Utils::encode(m_term, data, 48U, 114U);
|
|
|
|
break;
|
|
|
|
case P25_DUID_TERM_LC:
|
|
|
|
CP25Utils::encode(m_termlc, data, 48U, 114U);
|
|
|
|
break;
|
2018-05-14 16:00:06 +00:00
|
|
|
case P25_DUID_TSDU:
|
|
|
|
CP25Utils::encode(m_tsdu, data, 48U, 114U);
|
|
|
|
break;
|
2018-01-03 20:03:10 +00:00
|
|
|
case P25_DUID_PDU:
|
|
|
|
CP25Utils::encode(m_pdu, data, 48U, 114U);
|
|
|
|
break;
|
2016-09-19 21:26:42 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2016-09-12 22:26:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char CP25NID::getDUID() const
|
|
|
|
{
|
|
|
|
return m_duid;
|
|
|
|
}
|