
/**************************************************************
$Workfile:   diagMenu.c  $
***************************************************************
**
** FIRMWARE, Copyright (C) 2000-2002        
** 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:50 $
**************************************************************/

#include "stdio.h"
#include "string.h"
#include "bsp.h"

#include "bitmask.h"
#include "dapi.h"
#include "apiproto.h"
#include "ipos.h"
//#include "elmdef.h"
#include "afeIntf.h"

#include "diag.h" 
#include "diag_ver.h"

/***********************************************************************/
/*                   LOCAL TYPES and CONSTS                            */ 
/***********************************************************************/

#define MAX_LINE        160     /* max line length for input */

/* --- Diagnostic Printing ----*/
static int iposDiagPrintLevel[IPOS_MAX_BME][IPOS_MAX_PORT_PER_BME];

/***********************************************************************/
/*                   LOCAL VARIABLES                                   */ 
/***********************************************************************/
static int  errCode=IPOS_SUCCESS;

/* --- Diagnostic Printing ----*/
static int  level; 

/* --- System Tests -- */

static ipos_port_id         pid;
static ipos_bme_provision   sBmeProvision;
static ipos_port_profile    sPortProfile;
static ipos_test_signals    sTestSig;

static int                  nMsecsRxWait;    

static bool                 bTestInProgress; 


//static int BmeNum;
extern int BmeNum = 1; 

/***********************************************************************/
/*                   LOCAL FUNCTIONS                                   */
/***********************************************************************/

/* ---- Menu and Print Functions ---*/
static void diagMenu();
static void diagSystemTestsMenu();
static int  diagMaintenanceMenu (ipos_test_signals *pTestSig);
static void diagBoardDiagsMenu();
static void diagBmeDebugMenu();

static void diagPrintLogo();
static void diagPrintHelp();
static void diagPrintSystemTestsHelp();
static void diagPrintBoardDiagsHelp();
static void diagPrintBmeDebugHelp();
static void diagPrintBmeDebugList();
static void diagPrintBmeDebugRunList(int testBits1, int testBits2);

static void diagPrintLevelSet(char ch);

/***********************************************************************/
/*                   MAIN ENTRY                                        */
/***********************************************************************/
void diag_start()
{
  int bmeNum;
  tBmeStruct *pBme;

  eApiRv ApiRv=FAIL;

  level = -1;  /*Init to invalid values */
  errCode=IPOS_SUCCESS;

  pid.bmeNum = 1;
  pid.portNum = 1;
  BmeNum = 1;    


  diagPrintLogo();

  /* Init routines */
  iposMsgBufInit(IPOS_MAX_MSG);
  iposDiagPrintInit();
  bmeDebugInit(); //Init BME debug so can do 678 command.

  iposDiagSetDefaultBmeProfile(&sBmeProvision);  
  iposDiagSetCoDefaultPortProfile(&sPortProfile);

  iposDiagPrintLevelShow(pid);

  /* Initialize BME Data Structures */
  IPOS_PRINTF("...Init BME Structures\n");
  iposSysInit();

  IPOS_PRINTF("...Init BME\n");
  for (bmeNum = 1; bmeNum <= IPOS_MAX_BME; bmeNum++)
  {
    pBme = &BmeStruct[bmeNum];

#ifdef  BME_CPE_20
    pBme->EMIConfig.memType = IPOS_BME_MEM_SDRAM16;
#else /* CO */
    pBme->EMIConfig.memType = IPOS_BME_MEM_SRAM;
#endif

    InitEMIConfig(pBme);

    afeInit(bmeNum, IPOS_MAX_AFE_PER_BME); 
  }

  for (bmeNum = 1; bmeNum <= IPOS_MAX_BME; bmeNum++)
  {
    bmeRelCpu (&BmeStruct[bmeNum],FALSE); 
    bmeHwResetApi(bmeNum);
  }

  while (1)
  {
    diagMenu();
  }

}



/***********************************************************************
        DIAGNOTISTC - MENU
***********************************************************************/
static void diagMenu()
{
  char test,ch, line[MAX_LINE], *pLine; 
  int  val;
  int  loop=1;

  while (loop)
  {
    diagPrintHelp();  

    getstr (line);
    pLine = line;

    test = line[0];

    switch (test)
    {
      case 'L'://Set Trace Level
      case 'l':
        ch = line[1];
        diagPrintLevelSet(ch);
        break;
      case 'B':
      case 'b':
        val = atoi(pLine+1);
        if (val <= IPOS_MAX_BME && val > 0)
        {
          BmeNum = val; 
          pid.bmeNum = val;
        }
        else
        {
          IPOS_PRINTF("Bme %d is Not Valid\n", val);
        }
        iposDiagPrintLevelShow(pid);
        break;
      case 'P':
      case 'p':
        val = atoi(pLine+1);
        if (val <= IPOS_MAX_PORT_PER_BME && val > 0)
        {
          pid.portNum = val;
        }
        {
          IPOS_PRINTF("Port %d is Not Valid\n", val);
        }
        iposDiagPrintLevelShow(pid);
        break;
      case 'S':
      case 's':
        diagSystemTestsMenu();
#if 1
        bmeHwResetApi(BmeNum);
        //Need to also to sysprov, portProv, and portstop
#else
        BmeStruct[BmeNum].BmeState = INACTIVE;
#endif
        break;
      case 'D':
      case 'd':
        diagBoardDiagsMenu();
        break;
      case '6':  
        {
          int val = atoi(pLine);
          if (val == 678)
          {
            //678 BME Debug
            bmeDebugHandler();
          }
        }
        break;
      case 'X':
      case 'x':
        IPOS_PRINTF ("\nExiting Menu\n");
        loop = 0;
        break;
      default:
        IPOS_PRINTF("\nUnknown Command.");    
        break;
    }

  }                 
}


/***********************************************************************
        DIAGNOTISTC - SYSTEM TESTS MENU
***********************************************************************/
static void diagSystemTestsMenu()
{
  char test,ch, line[MAX_LINE], *pLine; 
  int  val;
  int  loop=1;
  bool bDiagInterface = TRUE;

  U8  UID = 71;
  U32 uPar = 0;
  U8  cmdTag = 71;
  U32 cntRpt = 0; 
  eApiRv ApiRv = FAIL;
  void *pCb = NULL;

  bTestInProgress = FALSE;

  while (loop)
  {
    ApiRv = iposBmeStartBlk(BmeNum, (ipos_bme_load*) NULL,UID, uPar, (iposCbCtl)NULL);

    if (ApiRv != IPOS_SUCCESS)
    {
      return;
    }
    diagPrintSystemTestsHelp();  

    getstr (line);
    pLine = line;

    test = line[0];
    switch (test)
    {
      case 'L'://Set Trace Level
      case 'l':
        ch = line[1];
        diagPrintLevelSet(ch);
        break;
      case 'B':
      case 'b':
        val = atoi(pLine+1);
        if (val <= IPOS_MAX_BME && val > 0)
        {
          BmeNum = val; 
          pid.bmeNum = val;
        }
        else
        {
          IPOS_PRINTF("Bme %d is Not Valid\n", val);
        }
        iposDiagPrintLevelShow(pid);
        break;
      case 'P':
      case 'p':
        val = atoi(pLine+1);
        if (val <= IPOS_MAX_PORT_PER_BME && val > 0)
        {
          pid.portNum = val;
        }
        {
          IPOS_PRINTF("Port %d is Not Valid\n", val);
        }
        iposDiagPrintLevelShow(pid);
        break;
      case 'I':
      case 'i':
        bDiagInterface = FALSE;
        bTestInProgress = FALSE;
        IPOS_PRINTF("iPOS Interface Now in Use\n", val);
        break;
      case 'D':
      case 'd':
        bDiagInterface = TRUE;
        IPOS_PRINTF("Diagnostic Interface Now in Use. (This is the default.)\n", val);
        break;

      case 'X':
      case 'x':
        IPOS_PRINTF ("\nExiting Menu\n");
        loop = 0;
        break;

      default:
        val = atoi(pLine);
        nMsecsRxWait = IPOSD_MSEC_RX_WAIT;
        bTestInProgress = TRUE;
        switch (val)
        {
          case 2:
            IPOS_PRINTF("\nRe-start BME #%d: Are you sure(y/n)?\n> ",pid.bmeNum);
            getstr(line);
            if (line[0] == 'y' || line[0] == 'Y')
            {
              tBmeStruct *pBme = &BmeStruct[pid.bmeNum];
              pBme->BmeState=INACTIVE;

              if (BmeStart(pBme) != COMPLETE)
                IPOS_PRINTF("WARNGING!!! Reset did not complete\n");
            }
            bTestInProgress = FALSE;
            break;
          case 3:
            /* System Provision */
            {
              if (bDiagInterface == TRUE)
              {
                errCode = iposDiagBmeProvision(pid.bmeNum, &sBmeProvision);
              }
              else
              {
                /* BME state: must be in the idle state to run      */
                /* TBD  Check here that the BME is IDLE */
                IPOS_DIAG_PRINTB(pid.bmeNum, IPOSD_PRINT_TRACE,"IPOS_DIAG:BME PROVISION.........");
                errCode = iposBmeProvision(pid.bmeNum, &sBmeProvision, cmdTag, cntRpt, (void *)iposDiagCb);
              }
            }
            break;
          case 4:
            /* Port Provision */
            {
              if (bDiagInterface == TRUE)
              {
                errCode = iposDiagPortProvision(pid, &sPortProfile);
              }
              else
              {
                /* TBD  Add Checks here */
                IPOS_DIAG_PRINT(pid, IPOSD_PRINT_TRACE,"IPOS_DIAG:PORT PROVISION........");
                errCode = iposPortProvision(pid, &sPortProfile, UID, uPar, (void *)iposDiagCb);
              }
            }
            break;

          case 5:
            /* Port Start */
            nMsecsRxWait = IPOSD_MSEC_START_RX_WAIT;
            if (bDiagInterface == TRUE)
            {
              errCode = iposDiagPortStart(pid);
            }
            else
            {
              IPOS_DIAG_PRINT(pid, IPOSD_PRINT_TRACE,"IPOS_DIAG:PORT START............");
              errCode = iposPortStart(pid, UID, uPar, (void *)iposDiagCb);
            }
            break;
          case 6:
            /* Port Reset */
            if (bDiagInterface == TRUE)
            {
              errCode = iposDiagPortReset(pid);
            }
            else
            {
              U8 portResetParm = 1;
              IPOS_DIAG_PRINT(pid, IPOSD_PRINT_TRACE,"IPOS_DIAG:PORT RESET............");
              errCode = iposPortReset(pid, portResetParm, UID, uPar, (void *)iposDiagCb);
            }

            break;
          case 7:
            /* Port Stop */
            if (bDiagInterface == TRUE)
            {
              errCode = iposDiagPortStop(pid);
            }
            else
            {
              IPOS_DIAG_PRINT(pid, IPOSD_PRINT_TRACE,"IPOS_DIAG:PORT STOP.............");
              errCode = iposPortStop(pid, UID, uPar, (void *)iposDiagCb);
            }
            break;
          case 9: 
            /* Loopbacks */
            {
              int lpbkType;

              IPOS_PRINTF("Note: Send signals before starting AFE Loopback!!\n");
              IPOS_PRINTF("\nSelect a Loopback Type(1=Utopia, 2=AFE)>");
              getstr(line);
              lpbkType = atoi(line);

              switch (lpbkType)
              {
                case HPI_LPBK_NTWK_UTOPIA:
                  if (bDiagInterface == TRUE)
                  {
                    errCode = iposDiagUtopiaLbStart(pid);
                  }
                  else
                  {
                    IPOS_DIAG_PRINT(pid, IPOSD_PRINT_TRACE,"IPOS_DIAG:UTOPIA LOOPBACK.......");
                    errCode = iposLbStart(pid, UID, lpbkType, (void *)iposDiagCb);
                  }
                  break;
                case HPI_LPBK_AFE:
                  if (bDiagInterface == TRUE)
                  {
                    errCode = iposDiagAfeLb(pid);
                  }
                  else
                  {
                    // set the AFE-2 loopback register
                    int afeNum = pid.portNum >> 1;
                    IPOS_DIAG_PRINT1(pid, IPOSD_PRINT_TRACE,"IPOS_DIAG:AFE%d LOOPBACK........", afeNum);

                    BmeStruct[pid.bmeNum].BmeParms.DebugParms.Parm1 = (S32)('w' - '0');
                    BmeStruct[pid.bmeNum].BmeParms.DebugParms.Parm2 = afeNum;
                    BmeStruct[pid.bmeNum].BmeParms.DebugParms.Parm3 = 0x8a;//AFE Reg
                    BmeStruct[pid.bmeNum].BmeParms.DebugParms.Parm4 = 0x11;//Value

                    errCode = iposBmeMon(pid, UID, uPar, (void *)iposDiagCb);
                  }
                  break;
                default:
                  IPOS_PRINTF("Invalid Entry\n");
                  bTestInProgress = FALSE;
                  break;
              }
            }
            break;
          case 11:
            /* Port Status */
            {
              IPOS_DIAG_PRINT(pid, IPOSD_PRINT_TRACE,"IPOS_DIAG:PORT STATUS...........");
              errCode = iposPortStatusExtended(pid, UID, uPar, (void *)iposDiagPortStatusCb);                
            }
            break;
          case 42:
            {
              char *pEventLog;

              IPOS_PRINTF("#########################################################\n");
              IPOS_PRINTF("###########       B M E   D U M P      ##################\n");
              IPOS_PRINTF("#########################################################\n");
              if (!getdump(pid.bmeNum))
                dump( 200);
              IPOS_PRINTF("#########################################################\n");
              IPOS_PRINTF("#########################################################\n");
              bTestInProgress = FALSE;

            }
            break;
          case 70:
            {
              int status;
              ipos_test_signals sTestSignals;
              ipos_test_signals *pTestSignals = &sTestSignals;
              int *pData = NULL;

              status = diagMaintenanceMenu(pTestSignals);
              IPOS_PRINTF("\n\t");

              if (bDiagInterface == TRUE)
              {
                switch (status)
                {
                  case 1:
                    errCode =  iposDiagSendTestSignals(pid, pTestSignals);
                    break;
                  case 2:
                    errCode = iposDiagDetectTestSignals(pid, pTestSignals, pData);
                    break;
                  case 3:
                    errCode = iposDiagPortStop(pid);
                    break;
                  case 4://Full Garbite 
                    errCode = iposDiagGarabite(pid, pData);
                    bTestInProgress = FALSE;
                    break;
                  default:
                    bTestInProgress = FALSE;
                    break;
                }
              }
              else
              {
                switch (status)
                {
                  case 1:
                    IPOS_DIAG_PRINT(pid, IPOSD_PRINT_TRACE,"IPOS_DIAG:SEND SIGNALS..........");
                    errCode = iposSendTestSignals(pid, pTestSignals, UID, uPar, (void *)iposDiagCb);
                    break;
                  case 2:
                    IPOS_DIAG_PRINT(pid, IPOSD_PRINT_TRACE,"IPOS_DIAG:DETECT SIGNALS........");
                    errCode = iposDetectTestSignals(pid, pTestSignals, UID, uPar, (void *)iposDiagDetectSignalsCb);
                    break;
                  case 3:
                    IPOS_DIAG_PRINT(pid, IPOSD_PRINT_TRACE,"IPOS_DIAG:PORT STOP.............");
                    errCode = iposPortStop(pid, UID, uPar, (void *)iposDiagCb);
                    break;
                  case 4:
                    IPOS_PRINTF("\nThere is not currently an iPOS Interface for the Garabite\n");
                    bTestInProgress = FALSE;
                    break;
                  default:
                    bTestInProgress = FALSE;
                    break;
                }
              }
            }
            break;
          case 90:
            if (bDiagInterface == TRUE)
            {
              errCode = iposDiagDeviceVersion(pid.bmeNum);
            }
            else
            {


              IPOS_DIAG_PRINT(pid, IPOSD_PRINT_TRACE,"IPOS_DIAG:DEVICE VERSIONS.......");
              BmeStruct[pid.bmeNum].Port[0].PortParms.DeviceVerParms.VersionStr[0] = 'Z';
              BmeStruct[pid.bmeNum].Port[0].PortParms.DeviceVerParms.VersionStr[0] = 'Q';
              errCode = iposDeviceVersion(pid.bmeNum, UID, uPar, (void *)iposDiagBmeVersionCb);
            }
            break;
          case 678:  
            //678 BME Debug
            bmeDebugHandler();
            bTestInProgress = FALSE;
            break;

          default:
            IPOS_PRINTF("\nUnknown Command.");    
            bTestInProgress = FALSE;
            break;
        }
        if (bTestInProgress == TRUE)
        {
          errCode = diagRcvPoll(pid, errCode, nMsecsRxWait);
          diagPutErrCode(pid, errCode);
        }
    }
  }
}






/* 
        diagMaintenance Menu 
        
                It acquires information from user that needed to be send to BME to transmit 
                test signal. 
                
                Input: ptr to variable of type ipos_test_signals
                
                output: int
                        -1      ; no action to be taken by the caller
                        1       ; send test signal request to BME
                        2       ; send port deactivation to BME
*/
static int diagMaintenanceMenu (ipos_test_signals * pTestSig)
{
  int   in, i;
  char  str[256];
  int   status = -1;
  int   loop = 1;
  U16  *pTone;

  memset ((char *)pTestSig, 0, sizeof(ipos_test_signals));
  while (loop)
  {
    IPOS_PRINTF("\nTransmit/Receive Maintenance ");
    IPOS_PRINTF("\n\t 1) Transmit quiet ");
    IPOS_PRINTF("\n\t 2) Transmit tones ");
    IPOS_PRINTF("\n\t 3) Transmit tones REQ");
    IPOS_PRINTF("\n\t 4) Transmit single tone sweep ");
    IPOS_PRINTF("\n\t 5) Transmit op-training ");
    IPOS_PRINTF("\n\t 6) Transmit op-synchro ");
    IPOS_PRINTF("\n\t 7) Transmit op-medley ");
    IPOS_PRINTF("\n\t 8) Transmit Garabit ");
    IPOS_PRINTF("\n\t 9) Terminate transmit maintenance mode ");
    IPOS_PRINTF("\n\t20) Receive Average Power ");
    IPOS_PRINTF("\n\t30) Full Garabit Test ");
    IPOS_PRINTF("\n\t99) Exit from menu ");
    IPOS_PRINTF("\nSelect option: ");

    getstr(str);
    in = atoi(str);

    switch (in)
    {
      case 1:
        pTestSig->SigType = HPI_XMT_QUIET;
        loop = 0;
        status = 1;
        break;
      case 2:
      case 3:
        pTestSig->SigType = (U8)in;
        while (1)
        {
          IPOS_PRINTF("\n\t Number of tones to be transmitted (0-3): ");
          getstr (str);
          in = atoi(str);
          if ((in >= 0) && (in < 4))
          {
            pTestSig->NumTones = (U8)in;
            break;
          }
        }

        pTone = &(pTestSig->Tone1);
        for (i=0 ; i<pTestSig->NumTones ; i++)
        {
          IPOS_PRINTF("\n\t Transmit Tone #%d (freq/4.3125Khz): ", i+1);
          getstr (str);
          in = atoi(str);
          pTone[i] = (U16)in;
        }
        loop = 0;
        status = 1;
        break;
      case 4:
        pTestSig->SigType  = HPI_XMT_TONE_SWEEP;
        pTestSig->NumTones = 1;

        IPOS_PRINTF("\n\t Transmit Tone (freq/4.3125Khz): ");
        getstr (str);
        in = atoi(str);
        pTestSig->Tone1 = (U16)in;

        while (1)
        {
          IPOS_PRINTF("\n\t Sweep Interval in 250 msec increament (max of 120 sec): ");
          getstr (str);
          in = atoi(str);
          if ((in > 0) && (in < 481))
          {
            pTestSig->SweepInterval = (U16)in;
            break;
          }
        }
        loop = 0;
        status = 1;
        break;
      case 5:
        pTestSig->SigType = HPI_XMT_TRAINING_SYMBOL;
        loop = 0;
        status = 1;
        break;
      case 6:
        pTestSig->SigType = HPI_XMT_SYNCHRO_SYMBOL;
        loop = 0;
        status = 1;
        break;
      case 7:
        pTestSig->SigType = HPI_XMT_MEDLEY_SYMBOL;
        loop = 0;
        status = 1;
        break;
      case 8:
        pTestSig->SigType = HPI_XMT_GARABIT;
        loop = 0;
        status = 1;
        break;
      case 9:
        loop = 0;
        status = 3;
        break;
      case 20:
        pTestSig->SigType = HPI_RX_POWER;
        loop = 0;
        status = 2;
        break;
      case 30:
        loop = 0;
        status = 4; //FULL Garabite
        break;
      case 99:
        loop = 0;
        break;
      default:
        break;
    }
  }

  return (status); 
}


/***********************************************************************
        DIAGNOTISTC - BOARD DIAGNOSTICS MENU
***********************************************************************/

static void diagBoardDiagsMenu()
{
  char test,ch, line[MAX_LINE], *pLine; 
  int  val;
  int  loop=1;

  int num1,num2;

  /*Board Diags all run with BME CPU Held in Reset*/
  bmeRelCpu (&BmeStruct[BmeNum],FALSE); 

  while (loop)
  {
    diagPrintBoardDiagsHelp();  

    getstr (line);
    pLine = line;

    test = line[0];
    switch (test)
    {
      case 'L'://Set Trace Level
      case 'l':
        ch = line[1];
        diagPrintLevelSet(ch);
        break;
      case 'B':
      case 'b':
        val = atoi(pLine+1);
        if (val <= IPOS_MAX_BME && val > 0)
        {
          BmeNum = val; 
          pid.bmeNum = val;
        }
        else
        {
          IPOS_PRINTF("Bme %d is Not Valid\n", val);
        }
        iposDiagPrintLevelShow(pid);
        break;
      case 'P':
      case 'p':
        val = atoi(pLine+1);
        if (val <= IPOS_MAX_PORT_PER_BME && val > 0)
        {
          pid.portNum = val;
        }
        {
          IPOS_PRINTF("Port %d is Not Valid\n", val);
        }
        iposDiagPrintLevelShow(pid);
        break;
      case '1':
        /*** BME HIC Interface test***/
        iposDiagHic(BmeNum);
        break;
      case '2':
        /*** DACOUT test ***/
        iposDiagClockOutTest(BmeNum);
        break;
      case '3':
        /*** RESETOUT test****/
        iposDiagReset(BmeNum);
#ifdef BME_CPE_20
        InitSdram (0,1);
#endif
        break;
      case '4':
        /* V-bus  test*/
        IPOS_PRINTF ("\nEnter AFE address: ");
        getstr(line);
        num1 = atoi(line);

        IPOS_PRINTF ("\n");
        printf("num1(%d)\n", num1);
        iposDiagAfeIfeReg(BmeNum, num1);
        break;
      case '5':
        /* BME external test*/
#ifdef BME_CPE_20
          IPOS_PRINTF ("\nEnter bus width(1=16bit/0=32bit):");
        num1 = getch();

        IPOS_PRINTF ("\nEnter Clock Speed(0=one/1=half):\n");
        num2 = getch();
        InitSdram ((num1&0xf), (num2&0xf));
#endif
        iposDiagExtMem(BmeNum);
        break;
      case '6':
        {
          int val = atoi(pLine);
          if (val == 6)
          {
            /* Data Path test*/
            iposDiagHicDataPath(BmeNum);
          }
          else if (val == 678)
          {
            //678 BME Debug
            bmeDebugHandler();
          }
        }
        break;
      case 'A':
      case 'a':
        iposDiagHic(BmeNum);
        iposDiagReset(BmeNum);
        iposDiagAfeIfeReg(BmeNum, 0); //0=afeNum. This only tests afe
#ifdef BME_CPE_20
        InitSdram (0,1);
#endif
        iposDiagExtMem(BmeNum);
        iposDiagHicDataPath(BmeNum);
        IPOS_DIAG_PRINTB(BmeNum, IPOSD_PRINT_INFO,"Test Done!");
        break;
      case 'I':
      case 'i':
        /* Interrupt test*/
        iposDiagInt(BmeNum);
        break;
      case 'D':
      case 'd':
        /* BME Debug Menu */
        diagBmeDebugMenu();
        break;
      case 'X':
      case 'x':
        bmeHwResetApi(BmeNum);
        IPOS_PRINTF ("\nExiting Menu\n");
        loop = 0;
        break;
      default:
        IPOS_PRINTF("\nUnknown Command.");    
        break;
    }
    /* If ran a board test, reload BME Software before running Xtensa */
    if (test >='1' && test <= '6')
    {
      BmeStruct[BmeNum].BmeState = INACTIVE;
    }
  }                 
}






/***********************************************************************
        DIAGNOTISTC - BME DEBUG MENU
***********************************************************************/


LOCAL int getPara 
  (
  char        *strptr,        /* pointer to pointer to string */
  BOOL        defaultHex,     /* TRUE = arg is hex (even w/o 0x) */
  BOOL        optional        /* TRUE = ok if end of line */
  );

typedef struct FunTblDef
{
  char index;
  FUNCPTR func;               
  char *pName;
} tFunTblDef;

const tFunTblDef ft[] = { 
  {1,  (FUNCPTR)xfifo_fast_mem_test,     "XFIFO FAST MEM TEST.........."},
  {2,  (FUNCPTR)xfifo_inter_test,        "XFIFO INTERLEAVER TEST......."},
  {3,  (FUNCPTR)ext_test,                "EXTERNAL MEM TEST............"},
  {4,  (FUNCPTR)fmc_i_test,              "FMC ICACHE MEM TEST.........."},
  {5,  (FUNCPTR)fmc_d_test,              "FMC DCACHE MEM TEST.........."},
  {6,  (FUNCPTR)Yfifo_fast_mem_test,     "YFIFO FAST MEM TEST.........."},
  {7,  (FUNCPTR)tone_table_test,         "MAPPER TONE MEM TEST........."},
  {8,  (FUNCPTR)mapper_gain_test,        "MAPPER GAIN MEM TEST........."},
  {9,  (FUNCPTR)zfifo_mem_test,          "ZFIFO MEM TEST..............."},
  {10, (FUNCPTR)cfifo_mem_test,          "CFIFO MEM TEST..............."},
  {11, (FUNCPTR)dmc_i_test,              "DMC ICACHE MEM TEST.........."},
  {12, (FUNCPTR)dmc_d_test,              "DMC DCACHE MEM TEST.........."},
  {13, (FUNCPTR)smc_snr_test,            "SMC SNR MEM TEST............."},
  {14, (FUNCPTR)smc_tone_test,           "SMC TONE MEM TEST............"},
  {15, (FUNCPTR)smc_thres_test,          "SMC THRESHOLD MEM TEST......."},
  {16, (FUNCPTR)bfifo_inter_test,        "BFIFO INTERLEAVER MEM TEST..."},
  {17, (FUNCPTR)bfifo_fast_mem_test,     "BFIFO FAST MEM TEST.........."},
  {18, (FUNCPTR)bfifo_demapper_test,     "BFIFO DEMAPPER MEM TEST......"},
  {19, (FUNCPTR)demapper_test,           "DEMAPPER (ROT) MEM TEST......"},
  {20, (FUNCPTR)feq_test,                "FEQ MEM TEST................."},
  {21, (FUNCPTR)teq_test,                "TEQ MEM TEST................."},
  {22, (FUNCPTR)rmc_i_test,              "RMC ICACHE MEM TEST.........."},
  {23, (FUNCPTR)rmc_d_test,              "RMC DCACHE MEM TEST.........."},
  {24, (FUNCPTR)afifo_mem_test,          "AFIFO FAST MEM TEST.........."},
  {25, (FUNCPTR)bme_dma_test,            "BME DMA TEST................."},
  {26, (FUNCPTR)vbus0_test,              "VBUS0/AFE TEST..............."},
  {27, (FUNCPTR)vbus1_test,              "VBUS1/AFE TEST..............."},
  {28, (FUNCPTR)TNI_PioTst,              "TNI PIO TEST................."},
  {29, (FUNCPTR)Xfifo_PioTst,            "XFIFO PIO TEST..............."},
  {30, (FUNCPTR)Micro_cont_PioTst,       "MICRO/CONT PIO TEST.........."},
  {31, (FUNCPTR)Yfifo_PioTst,            "YFIFO PIO TEST..............."},
  {32, (FUNCPTR)Mapper_PioTst,           "MAPPER PIO TEST.............."},
  {33, (FUNCPTR)Zfifo_PioTst,            "ZFIFO PIO TEST..............."},
  {34, (FUNCPTR)VBus_PioTst,             "VBUS PIO TEST................"},
  {35, (FUNCPTR)Afifo_PioTst,            "AFIFO PIO TEST..............."},
  {36, (FUNCPTR)Rcv_Micro_cont_PioTst,   "RCV MICROCONTROLLER TEST....."},
  {37, (FUNCPTR)Dmap_PioTst,             "DEMAPPER PIO TEST............"},
  {38, (FUNCPTR)Bfifo_PioTst,            "BFIFO PIO TEST..............."},
  {39, (FUNCPTR)Dfrm_Micro_cont_PioTst,  "DEFRAMER MICRO PIO TEST......"},
  {40, (FUNCPTR)Cfifo_PioTst,            "CFIFO PIO TEST..............."},
  {41, (FUNCPTR)RNI_PioTst,              "RNI PIO TEST................."},
  {42, (FUNCPTR)Pio_Interface_PioTst,    "INTERFACE PIO TEST..........."},
  {43, (FUNCPTR)diagExtMemTest,          "EXTERNAL SRAM TEST..........."},
  {44, (FUNCPTR)iposBmeId,               "BME ID TEST.................."},
};



#define MAX_BITS_IN_BIT_LIST1   32
#define LAST_TEST_NUM           44
#define ALL_TESTS_NUM           45



static void diagPrintBmeDebugRunList(int testBits1, int testBits2)
{
  int testIndex;
  int testMask = 1;

  IPOS_PRINTF("\n\nBME DEBUG TESTS on RUN LIST\n\n");

  for (testIndex = 0; testIndex < MAX_BITS_IN_BIT_LIST1; testIndex++)
  {
    if (testBits1 & testMask)
    {
      IPOS_PRINTF("%s\n",ft[testIndex].pName);
    }
    testMask = testMask << 1;
  }
  testMask = 1;
  for (testIndex = MAX_BITS_IN_BIT_LIST1; testIndex < LAST_TEST_NUM; testIndex++)
  {
    if (testBits2 & testMask)
    {
      IPOS_PRINTF("%s\n",ft[testIndex].pName);
    }
    testMask = testMask << 1;
  }
}

extern U32 bmeMonGetHexFromBuf(char **ppBuf );

static void diagBmeDebugMenu()
{
  char test,ch, line[MAX_LINE], *pLine; 
  int  val;
  int  loop=1;

  int  nRepeat;
  int  runLoop;
  int  testIndex;
  int  errCode;
  int  nRunsFailed;
  bool bRunPassed;
  volatile FUNCPTR funcEntry;

  int  testBits1 = 0;
  int  testBits2 = 0;

  /* BME Debug run with BME CPU Held in Reset*/
  bmeRelCpu (&BmeStruct[BmeNum],FALSE); 

  diagPrintBmeDebugList();  

  while (loop)
  {
    diagPrintBmeDebugHelp();

    getstr (line);
    pLine = line;

    test = line[0];

    switch (test)
    {
      case 'L'://Set Trace Level
      case 'l':
        ch = line[1];
        diagPrintLevelSet(ch);
        break;
      case 'B':
      case 'b':
        val = atoi(pLine+1);
        if (val <= IPOS_MAX_BME && val > 0)
        {
          BmeNum = val; 
          pid.bmeNum = val;
        }
        else
        {
          IPOS_PRINTF("Bme %d is Not Valid\n", val);
        }
        iposDiagPrintLevelShow(pid);
        break;
      case 'P':
      case 'p':
        val = atoi(pLine+1);
        if (val <= IPOS_MAX_PORT_PER_BME && val > 0)
        {
          pid.portNum = val;
        }
        {
          IPOS_PRINTF("Port %d is Not Valid\n", val);
        }
        iposDiagPrintLevelShow(pid);
        break;
      case 'X':
      case 'x':
        bmeHwResetApi(BmeNum);
        IPOS_PRINTF ("\nExiting Menu\n");
        loop = 0;
        break;

      case 'A':
      case 'a':
        val = atoi(&line[1]);

        IPOS_PRINTF("\nAdded Test Number %d to List\n", val);

        if (val <= MAX_BITS_IN_BIT_LIST1)
        {
          testBits1 = testBits1 | (1<<(val-1));
          diagPrintBmeDebugRunList(testBits1,testBits2);
        }
        else if (val > MAX_BITS_IN_BIT_LIST1 && val <= LAST_TEST_NUM)
        {
          testBits2 = testBits2 | (1<<(val-1-MAX_BITS_IN_BIT_LIST1));
          diagPrintBmeDebugRunList(testBits1,testBits2);

        }
        else if (val == ALL_TESTS_NUM)
        {
          testBits1 = 0xFFFFFFFF;
#ifndef VTU_O
          testBits1 = (~_bit26);
#endif
          testBits2 = 0xFFFFFFFF;
        }
        else
        {
          IPOS_PRINTF("\nInvalid Number. Syntax is A<num of test>\n");
          IPOS_PRINTF("\n  Syntax is A<num of test>\n");
          IPOS_PRINTF("\n      where ");
          IPOS_PRINTF("\n          <num of test> = the number of the test from list");
          IPOS_PRINTF("\n          <num of test> = 45 to run all the tests.");
          diagPrintBmeDebugList();  
        }
        break;
      case 'C':
      case 'c':
        testBits1 = testBits2 = 0;
        diagPrintBmeDebugRunList(testBits1,testBits2);
        break;
      case 'R':
      case 'r':
        nRepeat = atoi(&line[1]);

        if (nRepeat > 5)
        {

          IPOS_PRINTF("The list of tests will be run %d times.\n",nRepeat);
          IPOS_PRINTF("Each run is aborted after the first test that fails.\n");
          IPOS_PRINTF("Please verify the number of runs by reentering the number.\n>");
          getstr (line);
          val = atoi(&line[0]);
          if (nRepeat != val)
          {
            IPOS_PRINTF("\nThe choice does not match.\n");
            break;
          }
        }

        IPOS_PRINTF("\n\n             BME DEBUG TESTS\n\n");

        errCode = IPOS_SUCCESS;
        nRunsFailed = 0;
        bRunPassed = TRUE;
        IPOS_DIAG_PRINTNOB(BmeNum, IPOSD_PRINT_INFO, "\n");
        for (runLoop = 0; runLoop < nRepeat; runLoop++)
        {
          int testMask = 1;
          for (testIndex = 0; testIndex < MAX_BITS_IN_BIT_LIST1; testIndex++)
          {
            if (testBits1 & testMask)
            {
              IPOS_DIAG_PRINTB1(BmeNum, IPOSD_PRINT_TRACE,"IPOS_DIAG:%s",ft[testIndex].pName);
              IPOS_DIAG_PRINTNOB(BmeNum, IPOSD_PRINT_INFO,"\n");
              funcEntry = ft[testIndex].func;
              if (funcEntry(BmeNum) != OK)
              {
                errCode = IPOS_ERROR_BME_DEBUG_BASE + testIndex + 1;
                nRunsFailed += 1;
                bRunPassed = FALSE;
                IPOS_DIAG_PRINTB_FAIL(BmeNum, errCode);

                //Forces tests to stop after error.
                testIndex = MAX_BITS_IN_BIT_LIST1; 
              }
              else
                IPOS_DIAG_PRINTB_SUCCESS(BmeNum);

            }
            testMask = testMask << 1;
          }
          if (bRunPassed == TRUE)
          {
            testMask = 1;
            for (testIndex = MAX_BITS_IN_BIT_LIST1; testIndex < LAST_TEST_NUM; testIndex++)
            {
              if (testBits2 & testMask)
              {
                IPOS_DIAG_PRINTB1(BmeNum, IPOSD_PRINT_TRACE,"IPOS_DIAG:%s",ft[testIndex].pName);
                funcEntry = ft[testIndex].func;
                if (funcEntry (BmeNum) != OK)
                {
                  errCode = IPOS_ERROR_BME_DEBUG_BASE + testIndex + 1;
                  nRunsFailed += 1;
                  IPOS_DIAG_PRINTB_FAIL(BmeNum, errCode);

                  //Forces tests to stop after error.
                  testIndex = LAST_TEST_NUM;
                }
                else
                  IPOS_DIAG_PRINTB_SUCCESS(BmeNum);
              }
              testMask = testMask << 1;
            }
          }
        }


        IPOS_PRINTF("\nTotal Failed Runs/Total Runs = %d/%d\n\n", nRunsFailed,nRepeat);


        break;
      case 'S':
      case 's':
        diagPrintBmeDebugList(); 
        diagPrintBmeDebugRunList(testBits1,testBits2);
        break;
      default:
        IPOS_PRINTF("\nUnknown Command.");    
        break;
    }
    BmeStruct[BmeNum].BmeState = INACTIVE;
  }                 
}


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


static void diagPrintLogo()
{
  char diagVer[30]; 

  iposDiagGetVer(diagVer);

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

}



/***********************************************************************/
static void diagPrintHelp()
{
  IPOS_PRINTF("\n\n\nIKANOS DIAGNOSTICS MENU\n");

  IPOS_PRINTF("\nl<m,r,t,i,v>......Set Trace level");
  IPOS_PRINTF("\nb<n>..............Set BME Number 1..MAX_BME");
  IPOS_PRINTF("\np<n>..............Set Port Number 1..MAX_PORT/BME\n");


  IPOS_PRINTF("\nd.................BME Board Diagnostics");
  IPOS_PRINTF("\ns.................BME System Tests\n");

  IPOS_PRINTF("\n678...............BME Debug Menu\n");
  IPOS_PRINTF("\nx.................Exit Tests\n");


  IPOS_PRINTF("\n>");
}


/***********************************************************************/
static void diagPrintSystemTestsHelp()
{
  IPOS_PRINTF("\n\n\nSYSTEM TESTS MENU\n");

  IPOS_PRINTF("\nl<m,r,t,i,v>......Set Trace level");
  IPOS_PRINTF("\nb<n>..............Set BME Number 1..MAX_BME");
  IPOS_PRINTF("\np<n>..............Set Port Number 1..MAX_PORT/BME\n");

  IPOS_PRINTF("\ni.................Test iPOS Interfaces");
  IPOS_PRINTF("\nd.................Test Diagnostic Interfaces(default)\n");

  IPOS_PRINTF("\n    2) Initiate Reset BME command");
  IPOS_PRINTF("\n    3) Configure System Provision (default values)");
  IPOS_PRINTF("\n    4) Configure Port Provision");
  IPOS_PRINTF("\n    5) Initiate Port Start command");
  IPOS_PRINTF("\n    6) Initiate Port Reset command");
  IPOS_PRINTF("\n    7) Initiate Port Stop command");
  IPOS_PRINTF("\n    9) Start Loopback Test");
  IPOS_PRINTF("\n   11) Request Extended Port Status");
  IPOS_PRINTF("\n   42) Event Log Management");
  IPOS_PRINTF("\n   70) Transmit Maintenance Mode");
  IPOS_PRINTF("\n   90) BME Firmware versions");
  IPOS_PRINTF("\n  678) BME Debug Menu\n");

  IPOS_PRINTF("\nx.................Exit Tests\n");

  IPOS_PRINTF("\n!!! Provision the System and Port before running tests !!!");
  IPOS_PRINTF("\n!!! Stop the Port after running a test before running a new test !!!\n");

  IPOS_PRINTF("\n>");
}


/***********************************************************************/
static void diagPrintBoardDiagsHelp()
{
  IPOS_PRINTF("\n\n\BOARD DIAGNOSTICS MENU\n");

  IPOS_PRINTF("\nl<m,r,t,i,v>......Set Trace level");
  IPOS_PRINTF("\nb<n>..............Set BME Number 1..MAX_BME");
  IPOS_PRINTF("\np<n>..............Set Port Number 1..MAX_PORT/BME\n");

  IPOS_PRINTF("\n1.................BME HIC Interface Test");
  IPOS_PRINTF("\n2.................BME CLKOUT Test");
  IPOS_PRINTF("\n3.................BME Reset Test");
  IPOS_PRINTF("\n4.................BME VBus(AFE Register) Test");
  IPOS_PRINTF("\n5.................BME External Memory Test");
  IPOS_PRINTF("\n6.................BME to Host Data Path Test");
  IPOS_PRINTF("\nA.................All Above Tests (except Clock)\n");

  IPOS_PRINTF("\ni.................Host Interrupt Test\n");

  IPOS_PRINTF("\n678...............BME Debug Menu\n");

  IPOS_PRINTF("\nd.................BME Debug Tests\n");

  IPOS_PRINTF("\nx.................Exit Tests\n");

  IPOS_PRINTF("\n>");
}

/***********************************************************************/
static void diagPrintBmeDebugHelp()
{
  IPOS_PRINTF("\n\n\nBME DEBUG MENU\n");

  IPOS_PRINTF("\nl<m,r,t,i,v>......Set Trace level");
  IPOS_PRINTF("\nb<n>..............Set BME Number 1..MAX_BME");
  IPOS_PRINTF("\np<n>..............Set Port Number 1..MAX_PORT/BME\n");

  IPOS_PRINTF("\nA<n>..............Add to List");
  IPOS_PRINTF("\nC.................Clear List");
  IPOS_PRINTF("\nR.................Run List");
  IPOS_PRINTF("\nS.................Show List\n");

  IPOS_PRINTF("\nx.................Exit Tests\n");

  IPOS_PRINTF("\n>");

}

static void diagPrintBmeDebugList()
{
  IPOS_PRINTF("\n\n\nBME DEBUG LIST\n");

  IPOS_PRINTF("\n1.................XFIFO FAST MEM TEST");
  IPOS_PRINTF("\n2.................XFIFO INTERLEAVER TEST");
  IPOS_PRINTF("\n3.................EXTERNAL MEM TEST");
  IPOS_PRINTF("\n4.................FMC ICACHE MEM TEST");
  IPOS_PRINTF("\n5.................FMC DCACHE MEM TEST");
  IPOS_PRINTF("\n6.................YFIFO FAST MEM TEST");
  IPOS_PRINTF("\n7.................MAPPER TONE MEM TEST");
  IPOS_PRINTF("\n8.................MAPPER GAIN MEM TEST");
  IPOS_PRINTF("\n9.................ZFIFO MEM TEST");
  IPOS_PRINTF("\n10................CFIFO MEM TEST");
  IPOS_PRINTF("\n11................DMC ICACHE MEM TEST");
  IPOS_PRINTF("\n12................DMC DCACHE MEM TEST");
  IPOS_PRINTF("\n13................SMC SNR MEM TEST");
  IPOS_PRINTF("\n14................SMC TONE MEM TEST");
  IPOS_PRINTF("\n15................SMC THRESHOLD MEM TEST");
  IPOS_PRINTF("\n16................BFIFO INTERLEAVER MEM TEST");
  IPOS_PRINTF("\n17................BFIFO FAST MEM TEST");
  IPOS_PRINTF("\n18................BFIFO DEMAPPER MEM TEST");
  IPOS_PRINTF("\n19................DEMAPPER (ROT) MEM TEST");
  IPOS_PRINTF("\n20................FEQ MEM TEST");
  IPOS_PRINTF("\n21................TEQ MEM TEST");
  IPOS_PRINTF("\n22................RMC ICACHE MEM TEST");
  IPOS_PRINTF("\n23................RMC DCACHE MEM TEST");
  IPOS_PRINTF("\n24................AFIFO FAST MEM TEST");
  IPOS_PRINTF("\n25................BME DMA TEST");
  IPOS_PRINTF("\n26................VBUS0/AFE TEST");
  IPOS_PRINTF("\n27................VBUS1/AFE TEST");
  IPOS_PRINTF("\n28................TNI PIO TEST");
  IPOS_PRINTF("\n29................XFIFO PIO TEST");
  IPOS_PRINTF("\n30................MICRO/CONT PIO TEST");
  IPOS_PRINTF("\n31................YFIFO PIO TEST");
  IPOS_PRINTF("\n32................MAPPER PIO TEST");
  IPOS_PRINTF("\n33................ZFIFO PIO TEST");
  IPOS_PRINTF("\n34................VBUS PIO TEST");
  IPOS_PRINTF("\n35................AFIFO PIO TEST");
  IPOS_PRINTF("\n36................RCV MICROCONTROLLER TEST");
  IPOS_PRINTF("\n37................DEMAPPER PIO TEST");
  IPOS_PRINTF("\n38................BFIFO PIO TEST");
  IPOS_PRINTF("\n39................DEFRAMER MICRO PIO TEST");
  IPOS_PRINTF("\n40................CFIFO PIO TEST");
  IPOS_PRINTF("\n41................RNI PIO TEST");
  IPOS_PRINTF("\n42................INTERFACE PIO TEST");
  IPOS_PRINTF("\n43................EXTERNAL SRAM TEST");
  IPOS_PRINTF("\n44................BME ID TEST");
  IPOS_PRINTF("\n45................Select all above tests");
}


/***********************************************************************
        DIAGNOTISTC - PRINTS
***********************************************************************/

static void diagPrintLevelSet(char ch)
{
  switch (ch)
  {
    case 'm':
    case 'M':
      level = IPOSD_PRINT_MENU;
      iposDiagPrintLevelSet(pid, level);
      break;
    case 'r':
    case 'R':
      level = IPOSD_PRINT_RESULT;
      iposDiagPrintLevelSet(pid, level);
      break;
    case 't':
    case 'T':
      level = IPOSD_PRINT_TRACE;
      iposDiagPrintLevelSet(pid, level);
      break;
    case 'i':
    case 'I':
      level = IPOSD_PRINT_INFO;
      iposDiagPrintLevelSet(pid, level);
      break;
    case 'v':
    case 'V':
      level = IPOSD_PRINT_VERBOSE;
      iposDiagPrintLevelSet(pid, level);
      break;
    case '?':
      iposDiagPrintLevelShow(pid);
      break;
    default:
      IPOS_PRINTF("    Format is: l<x> \n");
      //IPOS_PRINTF("    Format is: lX <bmeNum> <portNum>\n");
      IPOS_PRINTF("       <x> is level :\n");
      IPOS_PRINTF("          ? : Show current level\n");
      IPOS_PRINTF("          M, m : Menu\n");
      IPOS_PRINTF("          R, r : Result\n");
      IPOS_PRINTF("          T, t : Trace\n");
      IPOS_PRINTF("          I, i : Informational\n");
      IPOS_PRINTF("          V, v : Verbose\n");
      //IPOS_PRINTF("       bmeNum  = -1 : ALL Port on ALL BMES\n");
      //IPOS_PRINTF("       portNum = -1 : ALL Ports on the given BME \n");
      break;
  }
}




void iposDiagPrintInit()
{
  int i,j;

  for (i=0;i<IPOS_MAX_BME;i++)
  {
    for (j=0;j<IPOS_MAX_PORT_PER_BME;j++)
    {
      iposDiagPrintLevel[i][j] = IPOSD_PRINT_TRACE;
    }
  }
}


void iposDiagPrintLevelSet(ipos_port_id pid, int level)
{
  int bmeNum = pid.bmeNum;
  int portNum = pid.portNum;

  iposDiagPrintLevel[bmeNum-1][portNum-1] = level;

  iposDiagPrintLevelShow(pid);
}

void iposDiagPrintLevelShow(ipos_port_id pid)
{
  int bmeNum = pid.bmeNum;
  int portNum = pid.portNum;

  //IPOS_PRINTF("Diag Print Level for BME[%d] is ", bmeNum);
  IPOS_PRINTF("Diag Print Level for BME[%d] Port[%d] is ", bmeNum, portNum);

  if (bmeNum <= IPOS_MAX_BME && bmeNum > 0 && 
      portNum <= IPOS_MAX_PORT_PER_BME && portNum > 0)
  {
    switch (iposDiagPrintLevel[bmeNum-1][portNum-1])
    {
      case IPOSD_PRINT_MENU:
        IPOS_PRINTF("MENU");
        break;
      case IPOSD_PRINT_RESULT:
        IPOS_PRINTF("RESULTS");
        break;
      case IPOSD_PRINT_TRACE:
        IPOS_PRINTF("TRACE");
        break;
      case IPOSD_PRINT_INFO:
        IPOS_PRINTF("INFOMATIONAL");
        break;
      case IPOSD_PRINT_VERBOSE:
        IPOS_PRINTF("VERBOSE");
        break;
      default:
        IPOS_PRINTF("NOT VALID");
        break;
    }
  }

  IPOS_PRINTF("\n\n");

}

void iposDiagPrint(ipos_port_id pid, int level, const char *fmt, int i1, int i2, int i3, int i4, int i5)
{
  int bmeNum = pid.bmeNum;
  int portNum = pid.portNum;

  if (bmeNum <= IPOS_MAX_BME && bmeNum > 0 && portNum <= IPOS_MAX_PORT_PER_BME && portNum > 0)
  {
    if (level <= iposDiagPrintLevel[bmeNum-1][portNum-1])
    {
      if (level > IPOSD_PRINT_RESULT)  /* level != IPOSD_PRINT_MENU && level != IPOSD_PRINT_RESULT */
      {
        IPOS_PRINTF("b%dp%d>", bmeNum, portNum);
      }
      IPOS_PRINTF(fmt,i1,i2,i3,i4,i5); 
    }
  }

}

void iposDiagBPrint(int bmeNum, int level, const char *fmt, int i1, int i2, int i3, int i4, int i5)
{
  if (bmeNum <= IPOS_MAX_BME && bmeNum > 0)
  {
    if (level <= iposDiagPrintLevel[bmeNum-1][0])
    {
      if (level > IPOSD_PRINT_RESULT && level < IPOSD_PRINT_VERBOSE)
      {
        IPOS_PRINTF("b%d>", bmeNum);
      }
      IPOS_PRINTF(fmt,i1,i2,i3,i4,i5); 
    }
  }
}

void iposDiagNoBPrint(int bmeNum, int level, const char *fmt, int i1, int i2, int i3, int i4, int i5)
{
  if (bmeNum <= IPOS_MAX_BME && bmeNum > 0)
  {
    if (level <= iposDiagPrintLevel[bmeNum-1][0])
    {
      IPOS_PRINTF(fmt,i1,i2,i3,i4,i5); 
    }
  }
}



