/* -*- c++ -*- */
/* 
 * Copyright 2019 Jay Arrowfield.
 * 
 * This 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 3, or (at your option)
 * any later version.
 * 
 * This software 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 software; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street,
 * Boston, MA 02110-1301, USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <gnuradio/io_signature.h>
#include "bit_packer_impl.h"

namespace gr {
namespace jay {

const uint8_t bit_packer_impl::m_pattern[2][18] = 
{
	{0x02, 0x7e, 0x02, 0x7e, 0x02, 0x7e, 0x02, 0x7e, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x7e, 0x7e, 0x7e},
	{0x02, 0x7e, 0x02, 0x7e, 0x02, 0x7e, 0x02, 0x7e, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x7e, 0x02, 0x7e}
};

bit_packer::sptr
bit_packer::make(int numBitsIn, int numBitsOut, int endianess)
{
	return gnuradio::get_initial_sptr
	(new bit_packer_impl(numBitsIn, numBitsOut, endianess));
}

/*
 * The private constructor
 */
bit_packer_impl::bit_packer_impl(int numBitsIn, int numBitsOut, int endianess)
: gr::block("bit_packer"
, gr::io_signature::make(1, 1, sizeof(int))
, gr::io_signature::make(1, 1, sizeof(int)))
, m_inputCounter(0)
, m_numBitsIn(numBitsIn)
, m_numBitsOut(numBitsOut)
, m_endianess(endianess)
, m_gapCount(0)
{}

/*
 * Our virtual destructor.
 */
bit_packer_impl::~bit_packer_impl()
{
}

void
bit_packer_impl::forecast (int noutput_items, gr_vector_int &ninput_items_required)
{
	/* <+forecast+> e.g. ninput_items_required[0] = noutput_items */
	ninput_items_required[0] = m_numBitsIn*noutput_items;
}

int
bit_packer_impl::general_work (int noutput_items,
				   gr_vector_int &ninput_items,
				   gr_vector_const_void_star &input_items,
				   gr_vector_void_star &output_items)
{
	const int *in = (const int *) input_items[0];
	int *out = (int *) output_items[0];

	// Do <+signal processing+>
	size_t i = 0;
	size_t j = 0;
	for (i=0; i < ninput_items[0]; i++)
	{
		if (in[i] == 0xFF)
		{
			reset();
			patternMatchReset();
			continue;
		}
		else if (in[i] == 1)
		{
			m_gapCount++;
		}
		else if (in[i] == 0)
		{
			m_gapCount = 0;
		}

		if (m_gapCount >= 16)
		{
			reset();
			patternMatchReset();
			continue;
		}
		
		if (m_endianess == 0)
		{
			if(m_inputCounter == m_numBitsIn)
			{
				m_output <<= (m_numBitsOut - m_inputCounter);
				out[j++] = m_output;
				patternMatchProcess(m_output);
				m_output = 0;
				m_inputCounter = 0;
			}
			m_output <<= 1;
			if (in[i] != 0)
			{
				m_output |= 1;
			}
		}
		else
		{
			if(m_inputCounter == m_numBitsIn)
			{
				m_output >>= (m_numBitsOut - m_inputCounter);
				out[j++] = m_output;
				patternMatchProcess(m_output);
				m_output = 0;
				m_inputCounter = 0;
			}
			m_output >>= 1;
			if (in[i] != 0)
			{
				m_output |= 1 << (m_numBitsOut-1);
			}
		}
		m_inputCounter++;
	}
		
	// Tell runtime system how many input items we consumed on
	// each input stream.
	consume_each (i);

	// Tell runtime system how many output items we produced.
	return j;
}

void bit_packer_impl::reset()
{
	m_inputCounter = 0;
	m_output = 0;
}

void bit_packer_impl::patternMatchProcess(uint8_t byte)
{
	for (int i=0; i < sizeof(m_patternMatchCount)/sizeof(*m_patternMatchCount); i++)
	{
		int index = m_patternMatchCount[i];
		if (m_pattern[i][index] != byte)
		{
			index = 0;
		}
		else
		{
			index++;
		}
		if (index == sizeof(m_pattern[i])/sizeof(m_pattern[i][0]))
		{
			fprintf(stderr, "Match %d\n", i);
			index = 0;
		}
		m_patternMatchCount[i] = index;
	}
}
void bit_packer_impl::patternMatchReset()
{
	memset(m_patternMatchCount, 0, sizeof(m_patternMatchCount));
}

} /* namespace jay */
} /* namespace gr */

