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


#include <stdio.h>
#include <string.h>

#include "ipos.h"

#include "ioLib.h"
#include "time.h"

#include "appconfig.h"
#include "hostversion.h"
#include "oampDB.h"

#define OPERATOR_MODE	2

extern	int consoleFd;

	
int UserMode = SUPERVISOR_MODE;

int PasswordVerified=0;

int ConsoleTimer=CONSOLE_TIMEOUT;

int BandPlan=0;

int SysProvDone;

int autoStart=0;

int pmEnable=0;


void	displayMainMenu(char *hostVer);



void	iposShell()
{

	int i;
	
	U8 newPassword[10];
	U8 oldPassword[10];
	U8 confirmPassword[10];
	U8 pTlStr[512];


	char ProfNum[1];
	char ParmType[2];
	char ParmData[8];

	int portnum=0,profilenum=0;

	int bmenum=1;
	
	int displayMenu = 1;			/* display menu for initial startup	*/
	
	int PwdRv=0;
	int Rv=0;
	
	char hostVer[10];

	ipos_port_id Pid;
	
	int dPortNum,dBmeNum;
	
	unsigned char saveAS;

	strcpy (hostVer,VERSION_STR);

	printf ("\n**********************************************************");
	printf ("\n**               Copyright (C) 2000-2002                **");
	printf ("\n**          Ikanos Communication, Fremont, CA           **");
	printf ("\n**                All rights reserved.                  **");
	printf ("\n**                                                      **");
	printf ("\n**     Ikanos Programmable Operating System (iPOS)      **");
	printf ("\n**    Firmware and Application Programming Interface    **");
	printf ("\n**                       for the                        **");
	printf ("\n**         SmartLeap 8100 and CleverConnect 150         **");
	printf ("\n**                EFM/LRFE/VDSL Chipsets                **");
	printf ("\n**                   Version  %-18s        **",hostVer);
	printf ("\n**********************************************************\n\n");

#ifdef	YING

#ifdef PRODUCTION_CODE
	printf ("\nBME 1 is coming up\n");
  /* Download BME 1 code */	

	// Bringup BME 1
	strcpy(pTlStr,"bstart:0:1;");	
	ReceiveTLCommand (pTlStr);

	#ifdef CO_4P_BOARD
	
	printf ("\nBME 2 is coming up\n");
  /* Download BME 2 code */	
    strcpy(pTlStr,"bstart:0:2;");	
    ReceiveTLCommand (pTlStr);

	#endif

	taskDelay (20 * CLOCKS_PER_SEC); // Wait 20 seconds.


#endif	

#endif  /* ifdef YING	*/



	while (1)
	{
   
   #ifndef PRODUCTION_CODE
		PasswordVerified=1;
   #else

		if (PasswordVerified == 0)
		{
			PwdRv=0;
			while (PwdRv==0)
			 {
			   PwdRv=verifyPassword();
			   if (PwdRv==1) { UserMode=SUPERVISOR_MODE;printf ("\nSupervisor mode");}
			   else if (PwdRv==2) { UserMode=OPERATOR_MODE;printf ("\nOperator mode");}
			   else printf ("\nERROR: incorrect password Rv:%d",PwdRv);
			   }

			   PasswordVerified=1;
			   ConsoleTimer=CONSOLE_TIMEOUT;
	   }
   #endif
        

		if (UserMode==SUPERVISOR_MODE)    
		{
			autoStart = 0;
			pmEnable = 0;
	   
		}else
		{
			autoStart = 1;
			pmEnable = 1;
		}	

		if (displayMenu)
		{

			displayMainMenu(hostVer);
		
			displayMenu=0;
		}


		printf(PROMPT);
		//scanf("%s",pTlStr);
		gets (pTlStr);

		if ((pTlStr[0]=='\n') || (pTlStr[0]=='\0')) printf(PROMPT);
		if (PasswordVerified)
		{
			/* Reset password timer */
			ConsoleTimer=CONSOLE_TIMEOUT;

			if ((pTlStr[0] != '?') &&
				(strlen(pTlStr) > 0) && (strlen(pTlStr) <= 3))
			{

				switch(atoi(pTlStr))
				{

				case 20:
				  
					pTlStr[0] = '\0';
					displayMenu = 1;
					break;
				
				default:
					printf ("\nInvalid selection: %d", atoi(pTlStr));
					pTlStr[0] = '\0';
					displayMenu = 1;
					break;

				}
			}

			if(strlen(pTlStr))
			{
				ReceiveTLCommand(pTlStr);

			}
		} 							/* if password verified	*/

	}								/* end of the while loop  */

}									/* end of the function	*/



void	displayMainMenu(char *hostVer)
{

	printf ("\n\n\n      BME Reference board API Menu");
	printf ("\nIkanos Communications Copyright 2000-2002");
	printf ("\n             Version %-16s",hostVer);
	printf ("\n=========================================\n");

	if (UserMode==SUPERVISOR_MODE)
	{			
		printf ("\n    1) Initiate Start BME command");
		printf ("\n    2) Initiate Reset BME command");
		printf ("\n    3) Configure System Provision (default values)");
		printf ("\n    4) Configure Port Provision");
		printf ("\n    5) Initiate Port Start command");
		printf ("\n    6) Initiate Port Reset command");
		printf ("\n    7) Initiate Port Stop command");
		printf ("\n    8) Initiate Port Stop and De-provision port");
	}
	
	printf ("\n   10) Request Brief Port Status");
	printf ("\n   11) Request Extended Port Status");
	printf ("\n   12) Display Performance Counters");
	printf ("\n   13) Get Near End SNR Margin and Attenuation");

#ifdef VTU_O
//			printf ("\n   14) Retrieve Remote Performance Counters");
#endif
	
	printf ("\n   15) Display User Logs");
	printf ("\n   16) Clear User Logs");

#ifdef VTU_O
	
	printf ("\n   17) Get Far End SNR Margin and Attenuation");

#endif
	
	printf ("\n   20) Display this menu");

	if (UserMode==SUPERVISOR_MODE)
	{
		printf ("\n   21) Modify Port Provision");
		printf ("\n   22) Modify console password");
		printf ("\n   23) Tone Disable Configuration");

//      printf ("\n   30) Drop Frame Control");
		printf ("\n   31) INI CRC Threshold");

#ifdef VTU_O

		printf ("\n   40) Perform EOC loopback test");

#endif
		printf ("\n   41) BME Firmware versions");
	
#ifdef VTU_O
		//printf ("\n   80) Download CPE code from serial port");
		//printf ("\n   81) Download CPE code to remote CPE");
#endif
	}

	printf ("\n   99) Log off");

#ifndef PRODUCTION_CODE
	
	printf ("\n  101) Start line 1 in 3 bands mode");
	printf ("\n  102) Start line 1 in 4 bands mode");

#endif
	
	printf ("\n");

}





int verifyPassword()
{
	U8 Password[20];
	int i;
	int Rv=0;

	(void) ioctl (consoleFd, FIOSETOPTIONS, OPT_TERMINAL & ~OPT_ECHO);
	
	printf ("\nPlease enter console password:");
	scanf("%s",Password);
	
	(void) ioctl (consoleFd, FIOSETOPTIONS, OPT_TERMINAL);

#ifdef alf
	if (strcmp(Password,pCorrect)==0) Rv=2; /* Operator mode */
	else if (strcmp(Password,"superikanos")==0) Rv=1; /* Supervisor mode */
		else Rv=0;
#endif
	if (strcmp(Password,oampGetOperatorPassword)==0) Rv=2; /* Operator mode */
	else if (strcmp(Password,oampGetSupervisorPassword)==0) Rv=1; /* Supervisor mode */
		else Rv=0;

return (Rv);

}



void taskCmdTimer (void)
{
	int iBmeIndex, iPortIndex; 

	while (1)
	{
		//processLed();
		
		taskDelay(CLOCKS_PER_SEC); /* 1 sec delay */
		
		/* Decrement ConsoleTimer */
#ifdef PRODUCTION_CODE
		if (ConsoleTimer>0)  ConsoleTimer--;
		
		if ((ConsoleTimer==0) && (PasswordVerified == 1))
		{
			printf("\nInactivity Time Out");
			printf("\nLogged off from system");
			PasswordVerified=0;
			autoStart = 1;
			pmEnable = 1;
		  }
#endif
		}

}

