/*
*   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 "P25LowSpeedData.h"
#include "P25Utils.h"

#include <cstdio>
#include <cassert>

const unsigned char CCS_PARITY[] = {
	0x00U, 0x39U, 0x72U, 0x4BU, 0xE4U, 0xDDU, 0x96U, 0xAFU, 0xF1U, 0xC8U, 0x83U, 0xBAU, 0x15U, 0x2CU, 0x67U, 0x5EU,
	0xDBU, 0xE2U, 0xA9U, 0x90U, 0x3FU, 0x06U, 0x4DU, 0x74U, 0x2AU, 0x13U, 0x58U, 0x61U, 0xCEU, 0xF7U, 0xBCU, 0x85U,
	0x8FU, 0xB6U, 0xFDU, 0xC4U, 0x6BU, 0x52U, 0x19U, 0x20U, 0x7EU, 0x47U, 0x0CU, 0x35U, 0x9AU, 0xA3U, 0xE8U, 0xD1U,
	0x54U, 0x6DU, 0x26U, 0x1FU, 0xB0U, 0x89U, 0xC2U, 0xFBU, 0xA5U, 0x9CU, 0xD7U, 0xEEU, 0x41U, 0x78U, 0x33U, 0x0AU,
	0x27U, 0x1EU, 0x55U, 0x6CU, 0xC3U, 0xFAU, 0xB1U, 0x88U, 0xD6U, 0xEFU, 0xA4U, 0x9DU, 0x32U, 0x0BU, 0x40U, 0x79U,
	0xFCU, 0xC5U, 0x8EU, 0xB7U, 0x18U, 0x21U, 0x6AU, 0x53U, 0x0DU, 0x34U, 0x7FU, 0x46U, 0xE9U, 0xD0U, 0x9BU, 0xA2U,
	0xA8U, 0x91U, 0xDAU, 0xE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 0x5AU, 0x63U};
	
const unsigned int MAX_CCS_ERRS = 4U;

CP25LowSpeedData::CP25LowSpeedData() :
m_lsd1(0x00U),
m_lsd2(0x00U)
{
}

CP25LowSpeedData::~CP25LowSpeedData()
{
}

void CP25LowSpeedData::process(unsigned char* data)
{
	assert(data != NULL);

	unsigned char lsd[4U];
	CP25Utils::decode(data, lsd, 1546U, 1578U);

	for (unsigned int a = 0x00U; a < 0x100U; a++) {
		unsigned char ccs[2U];
		ccs[0U] = a;
		ccs[1U] = encode(ccs[0U]);

		unsigned int errs = CP25Utils::compare(ccs, lsd + 0U, 2U);
		if (errs < MAX_CCS_ERRS) {
			lsd[0U] = ccs[0U];
			lsd[1U] = ccs[1U];
			break;
		}
	}

	for (unsigned int a = 0x00U; a < 0x100U; a++) {
		unsigned char ccs[2U];
		ccs[0U] = a;
		ccs[1U] = encode(ccs[0U]);

		unsigned int errs = CP25Utils::compare(ccs, lsd + 2U, 2U);
		if (errs < MAX_CCS_ERRS) {
			lsd[2U] = ccs[0U];
			lsd[3U] = ccs[1U];
			break;
		}
	}

	m_lsd1 = lsd[0U];
	m_lsd2 = lsd[2U];

	CP25Utils::encode(lsd, data, 1546U, 1578U);
}

void CP25LowSpeedData::encode(unsigned char* data) const
{
	assert(data != NULL);

	unsigned char lsd[4U];
	lsd[0U] = m_lsd1;
	lsd[1U] = encode(m_lsd1);
	lsd[2U] = m_lsd2;
	lsd[3U] = encode(m_lsd2);

	CP25Utils::encode(lsd, data, 1546U, 1578U);
}

unsigned char CP25LowSpeedData::getLSD1() const
{
	return m_lsd1;
}

void CP25LowSpeedData::setLSD1(unsigned char lsd1)
{
	m_lsd1 = lsd1;
}

unsigned char CP25LowSpeedData::getLSD2() const
{
	return m_lsd2;
}

void CP25LowSpeedData::setLSD2(unsigned char lsd2)
{
	m_lsd2 = lsd2;
}

unsigned char CP25LowSpeedData::encode(unsigned char in) const
{
	return CCS_PARITY[in];
}
