MMDVMHost-Private/P25NID.cpp

110 lines
2.6 KiB
C++

/*
* Copyright (C) 2016 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 "P25NID.h"
#include "P25Defines.h"
#include "P25Utils.h"
#include "BCH.h"
#include <cstdio>
#include <cassert>
CP25NID::CP25NID(unsigned int nac) :
m_duid(0U),
m_nac(0U),
m_hdr(NULL),
m_ldu1(NULL),
m_ldu2(NULL),
m_termlc(NULL),
m_term(NULL)
{
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
}
CP25NID::~CP25NID()
{
delete[] m_hdr;
delete[] m_ldu1;
delete[] m_ldu2;
delete[] m_termlc;
delete[] m_term;
}
bool CP25NID::process(unsigned char* data)
{
assert(data != NULL);
unsigned char nid[P25_NID_LENGTH_BYTES];
CP25Utils::decode(data, nid, 48U, 114U);
m_duid = nid[1U] & 0x0FU;
m_nac = (nid[0U] << 4) & 0xFF0U;
m_nac |= (nid[1U] >> 4) & 0x00FU;
CP25Utils::encode(nid, data, 48U, 114U);
return true;
}
unsigned char CP25NID::getDUID() const
{
return m_duid;
}
unsigned int CP25NID::getNAC() const
{
return m_nac;
}