/**************************************************************
$Workfile:   rfmutil.c  $
***************************************************************
**
** FIRMWARE, Copyright (C) 2000-2002
** Ikanos Communications, Fremont, CA
** All rights reserved.
**
** The information and contents of this file are the
** proprietary information of Ikanos Communication and may not
** be disclosed or used without the formal written approval of
** Ikanos Communication
**
$Revision: 1.1 $
$Date: 2006/10/16 04:34:50 $
***************************************************************/
                                      

#include	<stdio.h>
#include 	"ipos.h"
#include	"rfmdef.h"
#include	"rfmerr.h"
#include 	"rfmifco.h"
#include	"rfmifcpe.h"
#include	"rfmlocal.h"


/* this file defines the utility functions for the RFM	*/
int		MsgCheckClass(U8 *pEocMsg)
{

	U8	msgType, Ack;

	msgType=pEocMsg[IPOSRFM_MSGHDR_MSGTYPE];
	Ack=pEocMsg[IPOSRFM_MSGHDR_ACK];

	if(msgType==IPOSRFM_MSG_DATA_BLOCK||msgType==IPOSRFM_MSG_ACK)
	{
		return	IPOSRFM_MSG_FILE;
	}

    
	if((msgType>= IPOSRFM_MSG_CMDRESP_START)&&(msgType<=IPOSRFM_MSG_CMDRESP_END))
	{

		if(Ack==IPOSRFM_ACK_CMD)
		{
			return IPOSRFM_MSG_COMMAND;
		}


		if(Ack==IPOSRFM_ACK_ACK || Ack==IPOSRFM_ACK_NACK)
		{

			return IPOSRFM_MSG_RESPONSE;
		}

		return	IPOSRFM_ERROR_UNKNOWN_MSG;

	}

	return	IPOSRFM_ERROR_UNKNOWN_MSG;

}


int		msgMatchCmdResp(U8	*pResp, U8	*pCmd)
{

		U8 	uidCmd, uidResp;
		uidCmd=pCmd[IPOSRFM_MSGHDR_UID];
		uidResp=pResp[IPOSRFM_MSGHDR_UID];

		if(MsgCheckClass(pResp)!=IPOSRFM_MSG_RESPONSE)
		{

			return IPOSRFM_ERROR_UNKNOWN_MSG;
		}


		if(MsgCheckClass(pCmd)!=IPOSRFM_MSG_COMMAND)
		{

			return IPOSRFM_ERROR_UNKNOWN_MSG;
		}

		if(uidCmd!=uidResp)
		{
			return	IPOSRFM_ERROR;
		}else
		{
			return IPOS_SUCCESS;
		}

		return IPOS_SUCCESS;

}
						   

/********************************************************************************\
	Name 	   : iposRFMGetCheckSum						 
	Purpose    : To calculate the BME code checksum	
	Parameters : None
	Return	   : 
	Type				Description
	----				-----------
	UINT32			calculated 2's complement checksum				
	
\********************************************************************************/

U32 iposRFMGetCheckSum (U8	*file, U32	len)
{
	U32 index;
	U32 chkSum = 0;

	for (index = 0; index < len; index++)
	{
		/* sum of 1's complement of the file */
		chkSum += ~(file[index]);
	}

	//printf ("\nCalculated CheckSum %lx", chkSum);

	/* 2's complement of Bme code */
	chkSum = ~(chkSum);

	//printf ("\nCalculated CheckSum complement %lx\n", chkSum);

	return chkSum;
}



/********************************************************************************\
	Name 	   : iposRFMVerifyCheckSum						 
	Purpose    : Sample program to verify the BME code checksum	
	Parameters : 
	no		Name		Type			Description
	--		----		----			-----------
	1. chkSum	  		UINT32		calculated 2's complement checksum

	Return	   : 
	Type				Description
	----				-----------
	BYTE				if verification is successful, i.e., if chkSum is zero, TRUE
						else FALSE			
	
\********************************************************************************/

int iposRFMVerifyCheckSum (U32 chkSum, U8 *file, U32 len)
{
	U32 index;

	/* get 1's complement of sum checksum of data */
	chkSum = ~(chkSum);

	/* decrement 1's complement of each byte from the 1's complement sum checksum of data */
	for (index = 0; index < len; index++)
	{
		chkSum -= ~(file[index]);
	}

#ifdef	TEST
	printf ("\nVerified CheckSum %lx\n", chkSum);
#endif

	/* if zero checksum, data is valid */
	if (!chkSum)
		return IPOS_SUCCESS;
	/* else data is not valid */
	else
		return IPOS_ERROR;
}






