/***********************************************************************************************************************
*                                                                                                                      *
* libscopeprotocols                                                                                                    *
*                                                                                                                      *
* Copyright (c) 2012-2022 Andrew D. Zonenberg and contributors                                                         *
* All rights reserved.                                                                                                 *
*                                                                                                                      *
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the     *
* following conditions are met:                                                                                        *
*                                                                                                                      *
*    * Redistributions of source code must retain the above copyright notice, this list of conditions, and the         *
*      following disclaimer.                                                                                           *
*                                                                                                                      *
*    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the       *
*      following disclaimer in the documentation and/or other materials provided with the distribution.                *
*                                                                                                                      *
*    * Neither the name of the author nor the names of any contributors may be used to endorse or promote products     *
*      derived from this software without specific prior written permission.                                           *
*                                                                                                                      *
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED   *
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL *
* THE AUTHORS BE HELD LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES        *
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR       *
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE       *
* POSSIBILITY OF SUCH DAMAGE.                                                                                          *
*                                                                                                                      *
***********************************************************************************************************************/

/**
	@file
	@author Andrew D. Zonenberg
	@brief Implementation of EthernetAutonegotiationDecoder
 */

#include "../scopehal/scopehal.h"
#include "EthernetAutonegotiationDecoder.h"

using namespace std;

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Construction / destruction

EthernetAutonegotiationDecoder::EthernetAutonegotiationDecoder(const string& color)
	: Filter(color, CAT_SERIAL)
{
	AddProtocolStream("data");
	CreateInput("din");
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory methods

bool EthernetAutonegotiationDecoder::ValidateChannel(size_t i, StreamDescriptor stream)
{
	if(stream.m_channel == NULL)
		return false;

	if( (i == 0) && (stream.GetType() == Stream::STREAM_TYPE_ANALOG) )
		return true;

	return false;
}

string EthernetAutonegotiationDecoder::GetProtocolName()
{
	return "Ethernet Autonegotiation";
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Actual decoder logic

void EthernetAutonegotiationDecoder::Refresh()
{
	if(!VerifyAllInputsOK())
	{
		SetData(NULL, 0);
		return;
	}

	//Get the input data
	auto din = GetInputWaveform(0);
	auto sdin = dynamic_cast<SparseAnalogWaveform*>(din);
	auto udin = dynamic_cast<UniformAnalogWaveform*>(din);
	din->PrepareForCpuAccess();

	//Create the outbound data
	auto* cap = new EthernetAutonegotiationWaveform;
	cap->m_timescale = din->m_timescale;
	cap->m_startTimestamp = din->m_startTimestamp;
	cap->m_startFemtoseconds = din->m_startFemtoseconds;
	cap->PrepareForCpuAccess();

	//Crunch it
	bool old_value = false;
	int64_t last_pulse = 0;
	bool code[16];
	int nbit = 0;
	int64_t frame_start = 0;
	bool last_was_data = false;
	auto len = din->size();
	for(size_t i = 0; i < len; i ++)
	{
		bool sample_value = GetValue(sdin, udin, i) > 0.5;
		int64_t tm = GetOffsetScaled(sdin, udin, i);
		float dt = (tm - last_pulse) * 1e-9f;

		if(sample_value && !old_value)
		{
			//If delta is more than 150 us, we're starting a new frame and this is a clock pulse
			if(dt > 150)
			{
				nbit = 0;
				last_was_data = false;
				frame_start = ::GetOffset(sdin, udin, i);
			}

			//If delta is less than 30 us, it's a glitch - skip it
			else if(dt < 30)
			{
			}

			//If we got a data pulse in the last cycle, this is a clock pulse. Don't touch the data
			else if(last_was_data)
			{
				last_was_data = false;
			}

			//If delta is more than 75 us, it's a clock pulse and the code bit was a zero
			else if(dt > 75)
			{
				code[nbit ++] = false;
				last_was_data = false;
			}

			//Delta is between 30 and 75 us. It's a "1" code bit
			else
			{
				code[nbit ++] = true;
				last_was_data = true;
			}

			//If we just read the 16th bit, crunch it
			if(nbit == 16)
			{
				uint16_t ncode = 0;
				for(int j=0; j<16; j++)
					ncode |= (code[j] << j);

				cap->m_offsets.push_back(frame_start);
				cap->m_durations.push_back(::GetOffset(sdin, udin, i) + GetDuration(sdin, udin, i) - frame_start);
				cap->m_samples.push_back(ncode);

				nbit = 0;
			}


			last_pulse = tm;
		}
		old_value = sample_value;
	}

	SetData(cap, 0);
	cap->MarkModifiedFromCpu();
}

std::string EthernetAutonegotiationWaveform::GetColor(size_t /*i*/)
{
	return StandardColors::colors[StandardColors::COLOR_DATA];
}

string EthernetAutonegotiationWaveform::GetText(size_t i)
{
	auto s = m_samples[i];
	char tmp[128];
	{
		snprintf(tmp, sizeof(tmp), "%04x", (int)s);
		return tmp;
	}
}
