/**************************************************************
***************************************************************
**
** Host Application, Copyright (C) 2002-2004
** Ikanos Communication, 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 Files         ***************************************/
#include <stdio.h>
#include <string.h>
#include "ipos.h"
#include "notchBnd.h"
#include "oampdb.h"

extern void updateFlashProf(void);
/*******  Defines               ***************************************/
#define STATIC	static


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

          L O C A L    F U N C T I O N S

************************************************************************
***********************************************************************/
STATIC	int	currOptBandStatus = 0;

int getOptBandType(ipos_port_profile *portProfile, int currOptBand)
{
	char 	tempStr[20];
	int		tempNum;
	int		s, e;
	
	s = 0;			/* s ==> start */
	e = 3;			/* e ==> end */
	do
	{
		printf("\n select optional band type:");
        printf("\n   0 - NO Optional Band Selected");
		printf("\n   1 - ANNEX_A_6_32, 26 to 138 KHz");
		printf("\n   2 - ANNEX_B_32_64, 138 to 276 KHz");
		printf("\n   3 - ANNEX_B_6_64, 26 to 276 KHz");	

		if ((currOptBand < s) || (currOptBand > e))
		  printf("\n\nEnter selection: ");
		else
		  printf("\n\nEnter selection [%d]: ", currOptBand);
    		// gets(tempStr);
                scanf("%s",tempStr);
                fflush(stdout); 
		if (strlen(tempStr)==0)
    		{
      			tempNum=currOptBand;   // Null or CR or exit entered means leave bandPlan alone
    		}
    		else
    		{
      			tempNum = atoi(tempStr);
    		}
	
    		if ((tempNum < s) || (tempNum > e)) 
      		  printf("\nIncorrect selection, must choose an optional band type between %d and %d\n\n", s, e);
  	} while ((tempNum < s) || (tempNum > e));
	
	return(tempNum);	
}

/*
************************************************************************
	Function: 
	
	Description: 
	
	Return Value: void
************************************************************************
*/
STATIC int menu1 (int threeBand, ipos_port_profile * portProfile)
{
  int selNum, tempNum;
  char tempStr[20];

  switch (portProfile->PortProvParms.NumRFIBand)
  {
    case 0:
  
		selNum = 1;
		break;
    case 1:
		selNum = 4;
		if (portProfile->PortProvParms.Band1Start == 0)
		{
			if (threeBand)
			 {
				if (portProfile->PortProvParms.Band1Stop == STOP_BAN_640K_3BAND)
				selNum = 2;
			else
			{
				if (portProfile->PortProvParms.Band1Stop == STOP_BAN_1100K_3BAND)
				selNum = 3;
			}
		}
        else
        {
          if (portProfile->PortProvParms.Band1Stop == STOP_BAN_640K_4BAND)
          {  
			selNum = 2;
          }
		  else
          {
			if (portProfile->PortProvParms.Band1Stop == STOP_BAN_1100K_4BAND)
            selNum = 3;
          }
        }
      }
      break;
    default:

		selNum = 4;
		break;
  
  }
  do
  {
    printf("\nTone Disable Configuration\n");
    printf("\n1)  Enable all tones");
    printf("\n2)  Disable tones 640 KHz and below");
    printf("\n3)  Disable tones 1.1 MHz and below");
//    printf("\n4)  Disable custom selected tones");
    printf("\n");
    printf("\n5)  Exit Tone Disable Configuration");
    printf("\n\nEnter selection [%d]: ", selNum);
    // gets(tempStr);
                scanf("%s",tempStr);
                fflush(stdout); 
    if (strlen(tempStr)==0)
    {
      tempNum=selNum;   // Null or CR entered means leave bandPlan alone
    }
    else
    {
      tempNum = atoi(tempStr);
    }
    if ((tempNum < 1) || (tempNum > 5))
      printf("\nIncorrection selection!!\n\n");
  } while ((tempNum < 1) || (tempNum > 5));
  return (tempNum);
}

/*
************************************************************************
	Function: 
	
	Description: 
	
	Return Value: void
************************************************************************
*/
STATIC int menu2 (int threeBand, ipos_port_profile * portProfile)
{
  printf("\nFeature not supported");
  return (0);
}

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

          F U N C T I O N     I N T E R F A C E S 

************************************************************************
***********************************************************************/
int notchBandConfig (ipos_port_profile * portProfile)
{
  int threeBand = (portProfile->PortProvParms.BandPlan != 0x2);
  int needUpdate = 0;
  U16 stopBand;

  switch (menu1(threeBand, portProfile))
  {
    case 1:
      if (portProfile->PortProvParms.NumRFIBand != 0)
      {
        portProfile->PortProvParms.NumRFIBand = 0;
        needUpdate = 1;
      }
      break;
    case 2:
      if (threeBand)
        stopBand = STOP_BAN_640K_3BAND;
      else
        stopBand = STOP_BAN_640K_4BAND;
      if ((portProfile->PortProvParms.NumRFIBand != 1) ||
          (portProfile->PortProvParms.Band1Start != 0) ||
          (portProfile->PortProvParms.Band1Stop != stopBand))
      {
        portProfile->PortProvParms.NumRFIBand = 0;
        portProfile->PortProvParms.Band1Start = 0;
        portProfile->PortProvParms.Band1Stop = stopBand;
        portProfile->PortProvParms.NumRFIBand = 1;
        needUpdate = 1;
      }
      break;
    case 3:
      if (threeBand)
        stopBand = STOP_BAN_1100K_3BAND;
      else
        stopBand = STOP_BAN_1100K_4BAND;
      if ((portProfile->PortProvParms.NumRFIBand != 1) ||
          (portProfile->PortProvParms.Band1Start != 0) ||
          (portProfile->PortProvParms.Band1Stop != stopBand))
      {
        portProfile->PortProvParms.NumRFIBand = 0;
        portProfile->PortProvParms.Band1Start = 0;
        portProfile->PortProvParms.Band1Stop = stopBand;
        portProfile->PortProvParms.NumRFIBand = 1;
        needUpdate = 1;
      }
      break;
    case 4:
      needUpdate = menu2(threeBand, portProfile);
      break;
    default:
      break;
  }
  return (needUpdate);
}

void notchBandConvert (ipos_port_profile * portProfile)
{

#ifdef	NOTUSED

	printf(" Before: bandplan=%d, NumRFIBand=%d, Band1Start=%d, Band1Stop=%d \n",\
		portProfile->PortProvParms.BandPlan, portProfile->PortProvParms.NumRFIBand, portProfile->PortProvParms.Band1Start, portProfile->PortProvParms.Band1Stop);

#endif

  if (portProfile->PortProvParms.BandPlan != 0x2) /*Previous is 4 band */
  {
    switch (portProfile->PortProvParms.NumRFIBand)
    {
      case 1:
        if (portProfile->PortProvParms.Band1Start == 0)
        {
			if (portProfile->PortProvParms.Band1Stop == STOP_BAN_640K_4BAND)
			{
				portProfile->PortProvParms.Band1Stop = STOP_BAN_640K_3BAND;
				break;
	          }
			if (portProfile->PortProvParms.Band1Stop == STOP_BAN_1100K_4BAND)
			{
				portProfile->PortProvParms.Band1Stop = STOP_BAN_1100K_3BAND;
				break;
			}

			portProfile->PortProvParms.Band1Stop = STOP_BAN_640K_3BAND;	// default value
			break;
		}
		default:
        portProfile->PortProvParms.Band1Start *= 2;
        portProfile->PortProvParms.Band1Stop *= 2;
        portProfile->PortProvParms.Band2Start *= 2;
        portProfile->PortProvParms.Band2Stop *= 2;
        portProfile->PortProvParms.Band3Start *= 2;
        portProfile->PortProvParms.Band3Stop *= 2;
        portProfile->PortProvParms.Band4Start *= 2;
        portProfile->PortProvParms.Band4Stop *= 2;
        portProfile->PortProvParms.Band5Start *= 2;
        portProfile->PortProvParms.Band5Stop *= 2;
        break;
    }
  }
  else
  { /* Previous is three band */
    switch (portProfile->PortProvParms.NumRFIBand)
    {
      case 1:
        if (portProfile->PortProvParms.Band1Start == 0)
        {
			if (portProfile->PortProvParms.Band1Stop == STOP_BAN_640K_3BAND)
			{
				portProfile->PortProvParms.Band1Stop = STOP_BAN_640K_4BAND;
				break;
			}
			if (portProfile->PortProvParms.Band1Stop == STOP_BAN_1100K_3BAND)
            {
				portProfile->PortProvParms.Band1Stop = STOP_BAN_1100K_4BAND;
				break;
            }
			
            portProfile->PortProvParms.Band1Stop = STOP_BAN_1100K_4BAND;		// 4 band default: 1100k
            break;
		}
	
	  break;	
      default:
        portProfile->PortProvParms.Band1Start =
                              (portProfile->PortProvParms.Band1Start + 1) / 2;
        portProfile->PortProvParms.Band1Stop =
                              (portProfile->PortProvParms.Band1Stop + 1) / 2;
        portProfile->PortProvParms.Band2Start =
                              (portProfile->PortProvParms.Band2Start + 1) / 2;
        portProfile->PortProvParms.Band2Stop =
                              (portProfile->PortProvParms.Band2Stop + 1) / 2;
        portProfile->PortProvParms.Band3Start =
                              (portProfile->PortProvParms.Band3Start + 1) / 2;
        portProfile->PortProvParms.Band3Stop =
                              (portProfile->PortProvParms.Band3Stop + 1) / 2;
        portProfile->PortProvParms.Band4Start =
                              (portProfile->PortProvParms.Band4Start + 1) / 2;
        portProfile->PortProvParms.Band4Stop =
                              (portProfile->PortProvParms.Band4Stop + 1) / 2;
        portProfile->PortProvParms.Band5Start =
                              (portProfile->PortProvParms.Band5Start + 1) / 2;
        portProfile->PortProvParms.Band5Stop =
                              (portProfile->PortProvParms.Band5Stop + 1) / 2;
        break;
    }
  }

 
#ifdef	NOTUSED

	printf("After:  bandplan=%d, NumRFIBand=%d, Band1Start=%d, Band1Stop=%d \n",\
		portProfile->PortProvParms.BandPlan, portProfile->PortProvParms.NumRFIBand, portProfile->PortProvParms.Band1Start, portProfile->PortProvParms.Band1Stop);

#endif

  return;
}

char *bandConfigMenu[] = {
							"\n\t 1 - All Tones On\n\t 2 - Disable tones 640 KHz and below\n\t 3 - Disable tones 1.1 MHz and below\n\t 5 - Exit Tone Disable Configuration",
	"\n\t 1 - All Tones On \n\t 2 - Disable tones 640 KHz and below\n\t 3 - Disable tones 1.1 MHz and below\n\t 5 - Exit Tone Disable Configuration",
							"\n\t 1 - All Tones On\n\t 2 - Disable tones 900 KHz and below\n\t 3 - Disable tones 1.1 MHz and below\n\t 5 - Exit Tone Disable Configuration",
	"\n\t 1 - All Tones On \n\t 2 - Disable tones 900 KHz and below\n\t 3 - Disable tones 1.1 MHz and below\n\t 5 - Exit Tone Disable Configuration",
						};

int	displayMenu(int isTx, int is_cpe, int selNum)
{	
	char 	tempStr[20];
	int		tempNum;
        int     menu_idx;
	
	do
	{
		if(isTx) {
			menu_idx = ((is_cpe == 1) ? 1 : 0);
		} else	{
			menu_idx = ((is_cpe == 1) ? 0 : 1);
		}
		
                /* For china bandplans 16 & 17, 640 kHz is replaced by 900 kHz */
		if ((oampGetBandPlan == 16) || (oampGetBandPlan == 17))
			menu_idx += 2;
		printf("%s", bandConfigMenu[menu_idx]);
		printf("\n\nEnter selection [%d]: ", selNum);
    	// gets(tempStr);
                scanf("%s",tempStr);
                fflush(stdout); 
    
		if (strlen(tempStr)==0||atoi(tempStr)==5)
    	{
      		tempNum=selNum;   // Null or CR or exit entered means leave bandPlan alone
    	}
    	else
    	{
      		tempNum = atoi(tempStr);
    	}
	
    	if ((tempNum < 0) || (tempNum > 5) || tempNum == 4) 
      		printf("\nIncorrection selection!!\n\n");
  	} while ((tempNum < 0) || (tempNum > 4));
	
	return(tempNum);	
}


int display_tx_band_config(ipos_port_profile *portProfile, int is_cpe)
{
	int					currSelection;
	int					usrSelection;
			
	currSelection = portProfile->BandMod.bandModifier&0xf; // lower 4 bit is the tx band configuration
	usrSelection = displayMenu(1, is_cpe, currSelection);
	
	return(usrSelection);	
}

extern ipos_port_profile sPortProfiles[];

int	display_rx_band_config(ipos_port_profile *portProfile, int is_cpe)
{	
	int					currSelection;
	int					usrSelection;
	
	currSelection = (portProfile->BandMod.bandModifier&0x30)>>4; // upper 4 bit is the rx band configuration
	usrSelection = displayMenu(0, is_cpe, currSelection);
	
	return(usrSelection);	
}

void configure_band(int profileNum, int is_cpe)
{
	int					txSel;
	int					rxSel;
	unsigned char		val;
	ipos_port_profile  	*portProfile;
	int					optBandType;
	
	portProfile = &sPortProfiles[profileNum];
	
	optBandType =  portProfile->BandMod.bandModifier>>6;
	
	printf("\n Modify the Tx band plan configuration");	
	txSel = display_tx_band_config(portProfile, is_cpe);
	printf("\n Modify the Rx band plan configuration");
	rxSel = display_rx_band_config(portProfile, is_cpe);
	val = ((rxSel&0x3)<<4)|(txSel&0xf);
	
	    optBandType = getOptBandType(portProfile, optBandType);
	
	val = (optBandType<<6)|val;	
		
	if(val!= portProfile->BandMod.bandModifier) {
		portProfile->BandMod.bandModifier = val;
		updateFlashProf();
	}
}

static int isSet = 0;

void setOptBandType(int typeInUse)
{
	if(isSet == 0)
	{
		currOptBandStatus = typeInUse;
		isSet = 1;
	}
}
