/**************************************************************
$Workfile:   rfmapp.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 $
***************************************************************/


/*************************************************************

	This file include the application code for both CO and CPE
	using IKANOS RFM core functions
	
*************************************************************/

	  
#define	TEST
	  
#include 	<stdio.h>
#include	<string.h>
#include	"apiosmacro.h"
#include	"appconfig.h"
#include	"ipos.h"
#include	"oamptask.h"
#include	"eocapp.h"
		 
#include 	"rfmifcpe.h"
#include 	"rfmifco.h"
#include 	"rfmlocal.h"
#include 	"ipostimer.h"
#include	"rfmapp.h"
#include	"rfmerr.h"

#include	"portmap.h"

#include	"flash.h"
#include	"flashMem.h" /* IXDP425 */


#define	TID_RFM_BLK_JOB	1
													 
//U8		File[160000];	  	/* 1.5 meg file storage	*/

extern	char indexFlagGet(void);
extern	char indexFlagSet(char index);
extern	char imageFlagSet(char index);
extern	char imageFlagReset(char index);
extern	char imageNumSet(char index);

extern	char bootloadInfo(void);


extern	void	reboot(int);
extern	U32 iposRFMGetCheckSum (U8	*file, U32	len);


				 
extern U8 tmpBuf[];
extern	int	tmpFileSize;


/************************************************************************
	
	local functions for displaying message information
	
*************************************************************************/


void	uiActivationInfo(iposrfm_activation_info  *pAcitvationInfo);
void	uiVerInfo(iposrfm_ver_info 		*pVerInfo);
void	uiFileInfo(iposrfm_file_info	*pFileInfo);
void	uiVer(iposrfm_firmware_ver	*pInfo);



// #ifdef	VTU_R

/**************************************************************************

	local definitions for CPE RFM APP
	
	
**************************************************************************/

#ifdef	NOTUSED
static	int		cpeRFMUpdateFirmware(app_cpe_rfm	*pCpeRfmApp	);	/* update the local firmware info	*/
#endif

/* event response information	*/

static	void	cpeRFMActivationEvnt(ipos_port_id pid, U8 uid, iposrfm_activation_info *pActivationInfo);


static	void	cpeRFMFirmwareReqEvnt(ipos_port_id pid, U8 uid);
									   
									   
static	void	cpeRFMFileTransferEvnt(ipos_port_id	pid, U8 uid, iposrfm_file_info	*pNewFileInfo, 
					 iposrfm_firmware_ver	*pReplacedVersion);


static	void	cpeRFMTransferCompleteEvnt(ipos_port_id pid, U8 uid, iposrfm_transfer_info *pInfo);


/* static	void	cpeRFMRebootHandler(int arg1, U32 arg2); */


static	void	cpeRFMWriteImage(U32 imageNum, U32 j, ipos_msg_buf *pBuf);

static	int		getCpeImageSaveIdx(app_cpe_rfm	*pApp);

app_cpe_rfm		cpeAppRfm[MAX_NUM_OF_PORTS];					/* the cpe application object	*/

//#endif									   


// #ifdef	VTU_O


static	app_co_rfm	*getCoApp(ipos_port_id	pid);

static	void	coTrCompleteEvnt(ipos_port_id pid, U8 uid, iposrfm_transfer_complete_resp *pInfo);

static 	void	coFSEvnt(ipos_port_id pid, U8 uid, iposrfm_file_save_req *pInfo);


app_co_rfm		coAppRfm[MAX_NUM_OF_PORTS];					/* the co application object	*/

U8 feDldStatus[MAX_NUM_OF_PORTS];
//#endif									   





/*************************************************************************

	common display routeins
	
*************************************************************************/


void	uiActivationInfo(iposrfm_activation_info  *pActivationInfo)
{

	printf("Activation Info: \n");
	printf("activation option: %d\n",pActivationInfo->activationOption);
	printf("version to be activated: ");
	uiVer(&(pActivationInfo->version));
	printf(PROMPT);

}

void	uiVerInfo(iposrfm_ver_info 		*pVerInfo)
{

	int i;	
	printf("Version Info: \n");
	printf("Number of firmware %d, The active firmware %d, \n", pVerInfo->numFirmware, pVerInfo->activeVer);
	printf("List of the firmware: \n");
		
	for(i=0; i<pVerInfo->numFirmware; i++)
	{

		printf("index %d ", i+1);
		uiVer(&(pVerInfo->version[i]));
	}

	printf(PROMPT);

}

void	uiFileInfo(iposrfm_file_info	*pFileInfo)
{
	printf("File Info: \n");

#if 0
	uiVer(&(pFileInfo->verTransfer));
#endif

	printf("file length=%lu checkSum=%lx \n", pFileInfo->fileLength, pFileInfo->checkSum);
	printf(PROMPT);
}


void	uiVer(iposrfm_firmware_ver	*pInfo)
{
	int i;
	printf("Firmware version: verId=%x, verStr='", pInfo->verId);
	for(i=0; i<pInfo->lenStr;i++)
	{
		printf("%c",pInfo->verStr[i]);

	}
	printf("'\n");
}

									  


//#ifdef   VTU_R

int	cpeRFMAppInit()
{

	int i;
	int	errCode;
	ipos_port_id pid;
	app_cpe_rfm	*pAppCpe;
	ipos_rfm_cpe	*pCpePe;

	/* inititialize the timers	*/
	errCode=iposTmrInit();
	if(errCode!=IPOS_SUCCESS)
	{
		printf("iposRFM: Timer Initialization Failed \n");
		return	errCode;

	}

	/* initialize the rfmCPE	*/
	
    errCode=iposRFMCPEInit();
	if(errCode!=IPOS_SUCCESS)
	{
		printf("iposRFM: CPE RFM Initialization Failed \n");
		return	errCode;

	}
	/* initialize the app object	*/
	for(i=0;i<MAX_NUM_OF_PORTS;i++)
	{

		pAppCpe=&cpeAppRfm[i];
		/* get the app object	*/
		errCode=getBmePortId(i+1, &pid);	/* even if portID is not valid, continue on init */
		pAppCpe->pid=pid;
		
		/* get RFMCPE object	*/
		pCpePe=iposRFMGetCpePe(pid);
#ifndef ALASKA
		pAppCpe->pCpePe=pCpePe;

		/* get the initial version information	*/
//		errCode=imageStringGet(0, sizeof(iposrfm_ver_info), (U8) &(pAppCpe->verInfo));

		/* get the current Image Index and Image Flag	*/

		pAppCpe->currImageIndx=indexFlagGet();

		/* current Connection State is idle	*/
		pAppCpe->currConnectionState=0;
		pAppCpe->cntConnectionAttempt=0;

		/* current displayLevel is 1	*/
		pAppCpe->displayLevel=1;
		
		/* file Addr	*/
		pAppCpe->fileAddr=tmpBuf;

#endif

		/* register the event handler */
		iposRFMCPERegActivationReq(pid,cpeRFMActivationEvnt);
		iposRFMCPERegVersionReq(pid,cpeRFMFirmwareReqEvnt);
		iposRFMCPERegTransferReq(pid,cpeRFMFileTransferEvnt);
		iposRFMCPERegTransferComplete(pid, cpeRFMTransferCompleteEvnt);


	}


#ifndef ALASKA
	cpeRFMBootDone();
#endif
	return	IPOS_SUCCESS;

}

							  
int	cpeRFMBootDone()
{

	int	idx;
  	int	errCode;

	idx=indexFlagGet();
	errCode=imageFlagSet(idx);

	return	errCode;
}


/* get the cpeAPP objects	*/


static	app_cpe_rfm	*getCpeApp(ipos_port_id	pid)
{

    return	&cpeAppRfm[0];

}


/* activation event handler	*/

static	void	cpeRFMActivationEvnt(ipos_port_id pid, U8 uid, iposrfm_activation_info *pActivationInfo)
{

	int	errCode;
	int	imageIdx;

	app_cpe_rfm	*pApp;



	pApp=getCpeApp(pid);
	if(pApp==NULL)
	{

		printf("faield to get CPE RFM manager object \n");
		return;

	}
	memcpy(&(pApp->activationInfo), pActivationInfo, sizeof(iposrfm_activation_info));
	if(pApp->displayLevel!=0)
	{

		printf("\nCPE RFM: activationRequest \n");
		uiActivationInfo(pActivationInfo);

	}

	if(bootloadInfo()==0 )		/* bootloader is compatiable */
	{
		errCode=iposRFMCPEActivationResp(pid,uid,IPOS_SUCCESS);

	
		/* setup the active image	*/
		imageIdx=getCpeImageSaveIdx(pApp);
		imageNumSet(imageIdx);
		imageFlagSet(imageIdx);
		printf("\n CPE RFM: New image index is %d \n", imageIdx);
		/* if activation option is immediate: set the 1sec timer fro reboot	*/

		if(pActivationInfo->activationOption==0)
		{

			printf("Rebooting.....\n");
			iposTaskDelay(CLOCKS_PER_SEC);				/* TT ID 1159 */
			reboot(0);

		}
    }
	else
	{
		errCode=iposRFMCPEActivationResp(pid,uid,(U8) IPOSRFM_ERROR_BOOTLOAD);
		printf("\nCPE RFM: Incompatiable bootloader \n");

	}
}
		 
		 

static	void	cpeRFMFirmwareReqEvnt(ipos_port_id pid, U8 uid)
{


	int	errCode;
	app_cpe_rfm	*pApp;
	iposrfm_ver_info	verInfo;
	char	slen, string[256];


#if	0
	char	ver1[20]="CPE test version 1\n";
	char	ver2[20]="CPE test version 2 \n";	
#endif

	pApp=getCpeApp(pid);
	if(pApp==NULL)
	{

		printf("faield to get CPE RFM manager object \n");
		return;

	}


	verInfo.numFirmware=2;
	verInfo.activeVer=indexFlagGet();
	verInfo.version[0].verId=1;
	imageStringGet(1, string);
	slen=strlen(string);
	verInfo.version[0].lenStr=slen;
	memcpy(verInfo.version[0].verStr, string, 20);

#if 0
	printf("\nThe first image version length=%i, String=%s",slen,string); 
#endif

	verInfo.version[1].verId=2;
	imageStringGet(2, string);
	slen=strlen(string);
	verInfo.version[1].lenStr=slen;
	memcpy(verInfo.version[1].verStr, string, 20);

#if 0
	printf("\nThe second image version length=%i, String=%s",slen,string); 
#endif



#if	0
		verInfo.numFirmware=2;
		verInfo.activeVer=1;
		verInfo.version[0].verId=0;
		verInfo.version[0].lenStr=18;
		memcpy(verInfo.version[0].verStr, ver1, 18);
		verInfo.version[1].verId=1;
		verInfo.version[1].lenStr=18;
		memcpy(verInfo.version[1].verStr, ver2, 18);

#endif
	
	/* get the firmware version from the flash	*/

	errCode=iposRFMCPEVersionResp(pid,1, IPOS_SUCCESS, &verInfo);
	
	if(pApp->displayLevel!=0)
	{

		printf("\nCPE RFM: firmwareVersionRequest \n");
		uiVerInfo(&verInfo);

	}

	return;
}


static	void	cpeRFMFileTransferEvnt(ipos_port_id	pid, U8 uid, iposrfm_file_info	*pNewFileInfo, 
					 iposrfm_firmware_ver	*pReplacedVersion)
{


	int	errCode, respCode;
	app_cpe_rfm	*pApp;
	iposrfm_firmware_ver	any;


	pApp=getCpeApp(pid);
	if(pApp==NULL)
	{

		printf("faield to get CPE RFM manager object \n");
		return;

	}

/* generate an any replaced version: this version does not accept the proposed version */
	if(bootloadInfo()==0 )		/* bootloader is compatiable */
	{
		any.verId=0;
		any.lenStr=3;
		any.verStr[0]='a';any.verStr[1]='n';any.verStr[2]='y';
		respCode=IPOS_SUCCESS;
	}

	else
	{
		any.verId=0xfe;
		any.lenStr=3;
		any.verStr[0]='a';any.verStr[1]='n';any.verStr[2]='y';
		respCode=IPOSRFM_ERROR_BOOTLOAD;
	}

	errCode=iposRFMCPETransferResp(pid, uid, respCode, &any, tmpBuf); 
	if(errCode!=IPOS_SUCCESS)
	{
		printf("\nCPE RFM: failed to send the response \n");

	}

	memcpy(&(pApp->fileInfo), pNewFileInfo, sizeof(iposrfm_file_info));

	if(pApp->displayLevel!=0)
	{

		printf("\nCPE RFM: fileTransferRequest \n");
		uiFileInfo(pNewFileInfo);

	}

	return;
	

}






static	void	cpeRFMTransferCompleteEvnt(ipos_port_id pid, U8 uid, iposrfm_transfer_info *pInfo)
{

	int		errCode;
	U32		cscpe, csco;
	int		imageSaveIdx;
	
	app_cpe_rfm	*pApp;


	pApp=getCpeApp(pid);
	if(pApp==NULL)
	{

		printf("faield to get CPE RFM manager object \n");
		return;

	}

	/* check the file checksum	*/
	cscpe=iposRFMGetCheckSum(tmpBuf, pApp->fileInfo.fileLength);
	csco=pApp->fileInfo.checkSum;

	/* send the  file checksum to CO	*/
	pApp->fileInfo.checkSum=cscpe;
	errCode=iposRFMCPETransferCompleteResp(pid,uid, &(pApp->fileInfo));

	if(pApp->displayLevel!=0)
	{

		printf("\nCPE RFM: fileTransferComplete CO checkSum=%lx cpe checksum=%lx \n", csco, cscpe);

	}

	/* add a job to save the file into the flash	*/
	if(cscpe==csco)					/* checksums match	*/
	{
		imageSaveIdx=getCpeImageSaveIdx(pApp);			/* the index for sving image	*/
		appOampJobAdd(TID_RFM_BLK_JOB,cpeRFMWriteImage,imageSaveIdx,(int) pApp,NULL);

	}
	else
	{
		printf("CPE RFM: Checksums failed to match \n");
	}

	return;
}


static	int		getCpeImageSaveIdx(app_cpe_rfm	*pApp)
{

	pApp->currImageIndx=(int) indexFlagGet();
	printf("The current image index =%d \n",pApp->currImageIndx);

	if(pApp->currImageIndx==(int) 1)
	{
		return	2;
	}

	if(	pApp->currImageIndx== (int) 2)
	{
		return	1;
	}

	printf("\n CPE RFM: Wrong image index \n");
	return	0;

}

static	void	cpeRFMWriteImage(U32 imageNum, U32 pAppIdx, ipos_msg_buf *pBuf)
{

	app_cpe_rfm	*pApp;
	iposrfm_file_save_info	 saveInfo;
	iposrfm_firmware_ver		any;
	int errCode = IPOS_SUCCESS;


/* generate an any replaced version: this version does not accept the proposed version */
	any.verId=0;
	any.lenStr=3;
	any.verStr[0]='a';any.verStr[1]='n';any.verStr[2]='y';


	pApp=(app_cpe_rfm *)pAppIdx;

	if(imageNum!=1&&imageNum!=2)
	{
		printf("CPE RFM: Wrong Image Number Error!\n");
		return;
	}
	printf("Write Image %lu to the flash memory, size=%lu \n",imageNum, pApp->fileInfo.fileLength);
	errCode = flashWriteImage(imageNum, tmpBuf, pApp->fileInfo.fileLength);
	
	/* file save complete: send file save to the CO	*/
	saveInfo.fileSaveStatus = errCode;
	
	memcpy(&(saveInfo.firmwareVerSaved),&(pApp->fileInfo.verTransfer),sizeof(iposrfm_firmware_ver));
	memcpy(&(saveInfo.replacedFirmwareVer), &any, sizeof(iposrfm_firmware_ver));
	errCode=iposRFMCPEFileSave(pApp->pid, 1, &saveInfo,NULL);

}

//#endif
		  
//#ifdef   VTU_O


int	coRFMAppInit()
{

	int i;
	int	errCode;
	ipos_port_id pid;
	app_co_rfm	*pAppCo;
	ipos_rfm_co	*pCoPe;

	/* inititialize the timers	*/
	errCode=iposTmrInit();
	if(errCode!=IPOS_SUCCESS)
	{
		printf("iposRFM: Timer Initialization Failed \n");
		return	errCode;

	}

	/* initialize the rfmco PE	*/
	
    errCode=iposRFMCOInit();
	if(errCode!=IPOS_SUCCESS)
	{
		printf("iposRFM: CO RFM Initialization Failed \n");
		return	errCode;

	}
	/* initialize the app object	*/
	for(i=0;i<MAX_NUM_OF_PORTS;i++)
	{

		feDldStatus[i] = 0;/* Far End Dnld Status Flag Initialization */
		pAppCo=&coAppRfm[i];
		/* get the app object	*/
		errCode=getBmePortId(i+1, &pid);	/* even if portID is not valid, continue on init */
		pAppCo->pid=pid;
		
		/* get RFMCO object	*/
		pCoPe=iposRFMGetCoPe(pid);
		pAppCo->pCoPe=pCoPe;

		/* current displayLevel is 1	*/
		pAppCo->displayLevel=1;
		
		/* file Addr	*/
		pAppCo->fileAddr=tmpBuf;

		/* register the event handler */
		iposRFMCORegTransferComplete(pid,1,coTrCompleteEvnt);
		iposRFMCORegFileSave(pid,coFSEvnt);


	}


	return	IPOS_SUCCESS;

}


/***************************************************************************************



****************************************************************************************/


/* get the remote version	*/
void coGetRMVerComplete(ipos_port_id	pid, U8 uid, int status, iposrfm_firmware_ver_resp *pRespInfo)
{
	
	app_co_rfm	*pApp;
	iposrfm_ver_info	*pInfo;


	pApp=getCoApp(pid);
	if(pApp==NULL)
	{

		printf("faield to get CO RFM manager object \n");
		return;

	}


	pInfo=&(pRespInfo->verInfo);
	printf("\nCO RFM: b%dp%d iposRFMCOGetRemoteVersion Complete  uid=%d status=%d \n",pid.bmeNum, pid.portNum, uid, status);
	
	if(status==IPOS_SUCCESS)
	{
		  memcpy(&(pApp->rmVerInfo),&(pRespInfo->verInfo), sizeof(iposrfm_ver_info));
	}

	if(pApp->displayLevel!=0)
	{
        printf("CO RFM: remote version info: \n");
		uiVerInfo(&(pRespInfo->verInfo));
	}
	printf(PROMPT);

	return;
}


int	coRFMAppGetRMVersion(ipos_port_id pid)
{


	int errCode;
	errCode=iposRFMCOGetRemoteVersion(pid, 1, coGetRMVerComplete);
	if(errCode==IPOS_SUCCESS)
	{
		printf("\nCO RFM: b%dp%d  iposRFMCOGetRemoteVersion status: IN PROGRESS ",pid.bmeNum, pid.portNum);
	}
	else
	{
		printf("\nCO RFM: b%dp%d  iposRFMCOGetRemoteVersion status: FAILED error= 0x%x ",pid.bmeNum, pid.portNum, errCode);
	}
	printf(PROMPT);


	return	errCode;

}


/***************************************************************************************



****************************************************************************************/


void	coTrComplete(ipos_port_id pid, U8 uid, int status, iposrfm_file_transfer_resp *pInfo)
{

	app_co_rfm	*pApp;

	pApp=getCoApp(pid);
	if(pApp==NULL)
	{

		printf("faield to get CO RFM manager object \n");
		return;

	}


	
	if(status==IPOS_SUCCESS)
	{
		  
		if(pInfo->replacedFirmwareVer.verId>=0x80)
		{
			printf("\nCO RFM: b%dp%d iposRFMCFileTransferReq Rejected  uid=%d \n",pid.bmeNum, pid.portNum, uid);
			printf("Reject cause: %d \n", pInfo->replacedFirmwareVer.verId);
		    feDldStatus[pid.portNum] = 0;
			printf(PROMPT);
			return;
		
		}
		printf("\nCO RFM: b%dp%d iposRFMCFileTransferReq Complete  uid=%d \n",pid.bmeNum, pid.portNum, uid);

		if(pApp->displayLevel!=0)
		{

#if 0
			printf("CO RFM: replaced version: ");
			uiVer(&(pInfo->replacedFirmwareVer));
#endif
			printf("\n");
		}
	}

	else
	{
		printf("\nCO RFM: b%dp%d iposRFMCFileTransferReq Failed  uid=%d errCode=%d\n",pid.bmeNum, pid.portNum, uid, status);

	}
	
	printf(PROMPT);
	return;
}



/* start the transfer	*/
int	coRFMAppFileTransfer(ipos_port_id pid, iposrfm_file_info	*pFileInfo, U8 *fileAddr)
{

	
	int errCode=IPOS_SUCCESS;
	app_co_rfm	*pCoApp;
	
	feDldStatus[pid.portNum] = 1;
	pCoApp=getCoApp(pid);
	if(pCoApp==NULL)
	{

 		printf("CO RFM: Failed to get the CO APP object \n");
		feDldStatus[pid.portNum] = 0;
		return	errCode;

	}

	/* set up the CO  */
	memcpy(&(pCoApp->localFileInfo), pFileInfo, sizeof(iposrfm_file_info));
	pCoApp->fileAddr=fileAddr;

	/* coApp state update TBD  */

	
	/* done in the application? cs=iposRFMGetCheckSum(File, i); */


	errCode=iposRFMCOFileTransfer(pid, 1, pFileInfo, fileAddr, NULL, coTrComplete);
	if(errCode==IPOS_SUCCESS)
	{
		printf("\nCO RFM: b%dp%d  iposRFMCOFileTransfer status: IN PROGRESS ",pid.bmeNum, pid.portNum);

	}
	else
	{
		printf("\nCO RFM: b%dp%d  iposRFMCOFileTransfer status: FAILED errcode=%d ",pid.bmeNum, pid.portNum, errCode);

	}

	printf(PROMPT);
	return	errCode;
}



/***************************************************************************************


****************************************************************************************/


void coActComplete(ipos_port_id	pid, U8 uid, int status, iposrfm_activation_resp *pInfo)
{


	app_co_rfm	*pApp;

	pApp=getCoApp(pid);
	
	if(pApp==NULL)
	{
		printf("faield to get CO RFM manager object \n");
		return;
	}

	pApp->activationStatus=pInfo->activationStatus;
	
	if(pApp->activationStatus==IPOS_SUCCESS)
	{
		  
		printf("\nCO RFM: b%dp%d iposRFMCOActivationReq Complete uid=%d status=%d\n",pid.bmeNum, pid.portNum, uid, pInfo->activationStatus);

	}

	else
	{
		printf("\nCO RFM: b%dp%d iposRFMCOActivationReq Failed  uid=%d errCode=%d\n",pid.bmeNum, pid.portNum, uid, status);
		printf("	Cause: Incompatible bootloader \n");
		
	}
	
	printf(PROMPT);
	return;



}

/* activation of the firmware	*/
int	coRFMAppActivate(ipos_port_id	pid, U8	uid, iposrfm_activation_info *pActivationInfo)
{
	
	int errCode=IPOS_SUCCESS;
	app_co_rfm	*pCoApp;
	
	pCoApp=getCoApp(pid);
	if(pCoApp==NULL)
	{
 		printf("CO RFM: Failed to get the CO APP object \n");
		return	errCode;
	}
	/* set up the CO  */
	memcpy(&(pCoApp->actInfo), pActivationInfo, sizeof(iposrfm_activation_info));

	/* coApp state update TBD  */
	errCode=iposRFMCOActivateVersion(pid, 1, pActivationInfo, coActComplete);
	if(errCode==IPOS_SUCCESS)
	{
		printf("\nCO RFM: b%dp%d  iposRFMCOActivation status: IN PROGRESS ",pid.bmeNum, pid.portNum);

	}
	else
	{
		printf("\nCO RFM: b%dp%d  iposRFMCOActivation status: FAILED errcode=%d ",pid.bmeNum, pid.portNum, errCode);

	}

	printf(PROMPT);
	return	errCode;

}





/***************************************************************************************



****************************************************************************************/

/* get the cpeAPP objects	*/

static	app_co_rfm	*getCoApp(ipos_port_id	pid)
{

    int	errCode;
	int	lineNum;
	
	errCode=getLineNum(pid, &lineNum);
	
	if(lineNum>MAX_NUM_OF_PORTS)
	{
		printf("Failed to get the RFM object: LineNum=%d \n", lineNum);
		return	NULL;
	}
	
	lineNum=lineNum-1;
	return(&coAppRfm[lineNum]);
}


/***************************************************************************************



****************************************************************************************/
/* CO event handlers	*/


static	void	coTrCompleteEvnt(ipos_port_id pid, U8 uid, iposrfm_transfer_complete_resp *pInfo)
{

	int	errCode;
	U32		csco, cscpe;
	app_co_rfm	*pApp;

    feDldStatus[pid.portNum] = 0;
	pApp=getCoApp(pid);


	errCode=iposRFMCOTransferCompleteResp(pid, uid);


	memcpy(&(pApp->rmFileInfo),&(pInfo->rcvedFile), sizeof(iposrfm_file_info)); 

	cscpe=pInfo->rcvedFile.checkSum;
	csco=pApp->localFileInfo.checkSum;

	if(pApp->displayLevel!=0)
	{

		printf("\nCO RFM: fileTransferComplete CO checkSum=%lx cpe checksum=%lx \n", csco, cscpe);

	}

	printf(PROMPT);


}




static 	void	coFSEvnt(ipos_port_id pid, U8 uid, iposrfm_file_save_req *pInfo)
{

	switch(pInfo->fileSave.fileSaveStatus)
	{
	case IPOS_SUCCESS:
		printf("\nCO RFM: CPE File Save Successful \n");
		break;
	case IPOSRFM_ERROR_IMAGE_TARGET:
		printf("\nCO RFM: CPE File Save Error: Wrong CPE target \n");
		break;
	case IPOS_RFM_SPACE_ERR:
		printf("\nCO RFM: CPE File Save Error: Not Enough Space to store the Image \n");
		break;

	default:
		printf("\nCO RFM: CPE File Save Error\n");
		break;
	}

#if 0
	printf("CPE saved version info: ");
	uiVer(&(pInfo->fileSave.firmwareVerSaved));

	printf("CPE replaced version info: ");
	uiVer(&(pInfo->fileSave.replacedFirmwareVer));
#endif
    iposRFMCOFileSaveResp(pid, uid, IPOS_SUCCESS);
	printf(PROMPT);

}


/**********************************************************************************

	for initial UI demo	
	
	
**********************************************************************************/
void	uiDnldCpe(ipos_port_id	pid)
{

	int errCode;
	U8	verString[]="Ikanos RFM test version                 ";

	iposrfm_file_info	fileInfo;
	U32					cs;

	cs=iposRFMGetCheckSum(tmpBuf, tmpFileSize);


	fileInfo.fileLength=tmpFileSize;
	fileInfo.checkSum=cs;

	fileInfo.verTransfer.verId=0x0;
	fileInfo.verTransfer.lenStr=24;
	memcpy(fileInfo.verTransfer.verStr,verString,24);

	errCode=coRFMAppFileTransfer(pid, &fileInfo, tmpBuf);

}


/*********************************************************** 

	implemented in this routine:
		1. Activate the image just downloaded
		2. User enter the activation option: 0: immediate, 1: Delayed (until power recycle)
	
************************************************************/	

void	uiActivateImage(ipos_port_id	pid, U8	actOption)
{

	int errCode;
	iposrfm_activation_info	actInfo;
	U8	verString[]="any";


	actInfo.activationOption=actOption;
	actInfo.version.verId=0xff;         /* ff indicate to activate the newest image */
	actInfo.version.lenStr=3;
	memcpy(actInfo.version.verStr,verString,3);
	errCode=coRFMAppActivate(pid, 1, &actInfo);


}

/**************************************************************
	Get the remote version
**************************************************************/
void	uiGetRMVersion(ipos_port_id	pid)
{

	int errCode;
	errCode=coRFMAppGetRMVersion(pid);
}


/*************************************************************
	set the display level
**************************************************************/
void	uiSetDisplay(int newLevel)
{

	int	i;

#ifdef	VTU_O
	app_co_rfm	*pApp;
	
	for(i=0;i<MAX_NUM_OF_PORTS;i++)
	{
		pApp=&coAppRfm[i];
		
		/* set the displayLevel	*/
		pApp->displayLevel=newLevel;
		pApp->pCoPe->displayLevel=newLevel;		

	}

	return;
#endif

#ifdef	VTU_R
	app_cpe_rfm	*pApp;
	
	for(i=0;i<MAX_NUM_OF_PORTS;i++)
	{
		pApp=&cpeAppRfm[i];
		
		/* set the displayLevel	*/
		pApp->displayLevel=newLevel;
		pApp->pCpePe->displayLevel=newLevel;		

	}
	return;

#endif

}
//#endif





	
							
							
							
							
							
