/**
 * \file acd_arp_filter.c
 * \brief
 *
 * \copyright Copyright (c) 2012 Texas Instruments Incorporated ALL RIGHTS RESERVED
 *
*/

/* ========================================================================== */
/*                             Include Files                                  */
/* ========================================================================== */
#include "eip_main.h"
#include <stdio.h>

/* ========================================================================== */
/*                            Global Variables                                */
/* ========================================================================== */

unsigned int countACD=0;
extern unsigned int acdARPFlag;

/* ========================================================================== */
/*                          Function Definitions                              */
/* ========================================================================== */


Uint8 checkGratArp(Uint8 * srcAddress)
{

	Uint16 *typeProt;
	Uint16  typeProt1;
	Uint16  typeProt2;
	Uint16  *isGrac;
	Uint16  isGracIP16;
	Uint32  isGracIPSender;
	Uint32  isGracIPTarget;

	typeProt = (Uint16*)(srcAddress) + 6;
	typeProt1 = (*typeProt << 8);
	typeProt2 = (*typeProt >> 8);
	typeProt1 = typeProt1 | typeProt2;

	if(typeProt1==0x0806)// Check for ARP packets
	{
		if(!countACD)
		{
			if(acdARPFlag)//check who should sent ARPs
			{
				return (1); // tell driver this packet shoouldnot be sent
			}
			else
			{
				isGrac = (Uint16*)(srcAddress) + 19;
				isGracIP16 = *isGrac;
				isGracIPTarget = isGracIP16<<8;
				isGracIPTarget |= isGracIP16>>8;
				isGracIPTarget = isGracIPTarget << 16;
				isGrac++;
				isGracIP16 = *isGrac;


				isGracIPTarget |=  isGracIPTarget | (isGracIP16<<8 & 0x0000FF00);
				isGracIPTarget |=  isGracIPTarget | (isGracIP16>>8 &0x000000FF);


				isGrac = (Uint16*)(srcAddress) + 14;
				isGracIP16 = *isGrac;
				isGracIPSender = isGracIP16<<8;
				isGracIPSender |= isGracIP16>>8;
				isGracIPSender = isGracIPSender << 16;
				isGrac++;
				isGracIP16 = *isGrac;
				isGracIPSender |=  isGracIPSender | (isGracIP16<<8 & 0x0000FF00);
				isGracIPSender |=  isGracIPSender | (isGracIP16>>8 &0x000000FF);

				if(isGracIPTarget==isGracIPSender)
				{
					return (1);
				}
			}
		}
		else
		{
			countACD--;
		}

	}
	return(0);
}
