/**************************************************************
$Workfile:   oampfuncs.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.2 $
$Date: 2007/01/22 14:49:09 $
***************************************************************/

#include 	"stdio.h"  
#include 	"ipos.h"

#include 	"appconfig.h"
#include 	"apperrCode.h"
#include 	"oamp.h"
#include "oampdb.h"
#include 	"pprofile.h"
#include 	"report.h"
//#include 	"iniapi.h"
#include "eventlog.h" /* IXDP425 */
#include 	"portmap.h"
#ifdef INI_SUPPORT 
#include 	"HostPtmfApi.h"
#endif //INI_SUPPORT
#include "dapi.h"
#include "dapi_parms.h"

#include "apilocal.h"
#include "linecrc.h"
#include "elmdef.h"
#include "bmetl.h"
#include "portmap.h"
#include "pmhandler.h"

/* Global variables */

//static ProvRequired=0;

oamp_bme oampBme [IPOS_MAX_BME+1];                  /* BME objects for OAMP   */

extern ipos_port_profile sPortProfiles[MAX_PORT_PROFILES];
extern void copySnrAttnToOamp  (ipos_port_nesnrattn *pReport,ipos_port_id pid);
extern void copyCountersToOamp (ipos_port_counters *pReport,ipos_port_id pid);
extern eoamp_bme_state  oampBmeStateUpdate(int bmeNum,eoamp_event   event);
#ifdef INI_SUPPORT 
extern tsIposPtmfConfig        tsIniDefaultConfig;
#endif //INI_SUPPORT
extern void InitPerfPara(int BmeNum, int PortNum);
extern void ResetCrcThresCnt(ipos_port_id pid);
extern void setUtopiaAddr(void);
extern void clearBmeCounters (ipos_port_id pid);
extern void rdm( int bmeNum, U32 addr, U32 len);
extern int  iposHostReset(int bmeNum, U8 UID, U32 uPar, iposCbCtl pCompleteFunc);
extern void oampHost_HotReset_StateUpdate(ipos_port_id pid, oamp_cb_msg *pMsg);
 
extern ipos_pm_counters *pPmCntr24Hrs[IPOS_MAX_BME+1][IPOS_MAX_PORT_PER_BME+1];
extern int iFailStatus[IPOS_MAX_BME+1][IPOS_MAX_PORT_PER_BME+1];
extern int portStartCopy[IPOS_MAX_BME+1][IPOS_MAX_PORT_PER_BME+1];
extern void UtopiaTxEnable (U8 bBmeNum);

#ifdef ALASKA
int oampPortGetLinkRates(ipos_port_id  *pPid, U8 cmdTag, U32 cntRpt);
void portLinkRatesComplete(ipos_port_id portId, U8 UID,U32 cntRpt, int status,
                        ipos_port_status *par);
static  void    portLinkRatesJob(U32 arg1, U32 arg2, ipos_msg_buf  *pBuf);

extern unsigned char  send_bme_linerates(unsigned int up_rate,unsigned int down_rate);
extern int vdsl_link_up(void);
extern int vdsl_link_down(void);
#endif
/* local structures		*/


/* structure for generic message	*/
typedef struct
{
    ipos_port_id    portId;
    U32 par[16];
}oamp_msg;



typedef void  (*completionJob) (U32 arg1, U32 arg2, ipos_msg_buf    *pBuf);


/* local defined definitions	*/



/* Common function for local callback	*/

void    addCompletionJob(ipos_port_id portId, U8 UID, U32 uPar, int status, U8 *par, int len,  completionJob  pFunJob);

/* local callback function prototypes  for OAMP	*/

/* bmeStart: running in the task: TID_BLKFUNC	*/
void    bmeStartJob(U32 arg1, U32 arg2, ipos_msg_buf *pBuf);

/* bmeReset: running in the task: TID_BLKFUNC	*/
void    bmeResetJob(U32 arg1, U32 arg2, ipos_msg_buf *pBuf);

/* BME provision	*/
void bmeProvisionComplete(ipos_port_id portId, U8 UID, U32 uPar, int status, void *par);
static  void    bmeProvisionJob(U32 arg1, U32 arg2, ipos_msg_buf    *pBuf);

/* BME version		*/
void bmeVersionComplete(ipos_port_id portId, U8 UID, U32 uPar, int status,ipos_bme_ver *par);
static  void    bmeVersionJob(U32 arg1, U32 arg2, ipos_msg_buf  *pBuf);

/* port provision	*/
void portProvisionComplete(ipos_port_id portId, U8 UID, U32 uPar, int status, void *par);
static  void    portProvisionJob(U32 arg1, U32 arg2, ipos_msg_buf   *pBuf);

/* port start	*/
void portStartComplete(ipos_port_id portId, U8 UID, U32 uPar, int status, void *par);
static  void    portStartJob(U32 arg1, U32 arg2, ipos_msg_buf   *pBuf);

/* port Stop	*/
void portStopComplete(ipos_port_id portId, U8 UID, U32 uPar, int status, void *par);
static  void    portStopJob(U32 arg1, U32 arg2, ipos_msg_buf    *pBuf);

/* port Reset	*/
void portResetComplete(ipos_port_id portId, U8 UID, U32 uPar, int status, void *par);
static  void    portResetJob(U32 arg1, U32 arg2, ipos_msg_buf   *pBuf);

/* port instantiation	*/
void portInstantiateComplete(ipos_port_id portId, U8 UID, U32 uPar, int status, void *par);
static  void    portInstantiateJob(U32 arg1, U32 arg2, ipos_msg_buf *pBuf);

/* port destroy	*/
void portDestroyComplete(ipos_port_id portId, U8 UID, U32 uPar, int status, void *par);
static  void    portDestroyJob(U32 arg1, U32 arg2, ipos_msg_buf *pBuf);

/* periodic report	*/
void pReportComplete(ipos_port_id portId, U8 UID, U32 uPar, int status, void *par);
static  void    pReportJob(U32 arg1, U32 arg2, ipos_msg_buf *pBuf);

/* EOC control Send	*/
void eocControlSendComplete(ipos_port_id portId, U8 UID,U32 cntRpt, int status,  void *par);
static  void    eocControlSendJob(U32 arg1, U32 arg2, ipos_msg_buf  *pBuf);

/* EOC Set Data		*/
void eocSetDataComplete(ipos_port_id portId, U8 UID,U32 cntRpt, int status,  void *par);
static  void    eocSetDataJob(U32 arg1, U32 arg2, ipos_msg_buf  *pBuf);

/* Bit Swap Req		*/
void bitSwapReqComplete(ipos_port_id portId, U8 UID,U32 cntRpt, int status,  void *par);
static  void    bitSwapReqJob(U32 arg1, U32 arg2, ipos_msg_buf  *pBuf);

/* EOC Get Data		*/
void eocGetDataComplete(ipos_port_id portId, U8 UID,U32 cntRpt, int status,  void *par);
static  void    eocGetDataJob(U32 arg1, U32 arg2, ipos_msg_buf  *pBuf);
/* EOC data reply	*/
void eocDataReplyComplete(ipos_port_id portId, U8 UID,U32 cntRpt, int status,  void *par);
static  void    eocDataReplyJob(U32 arg1, U32 arg2, ipos_msg_buf    *pBuf);

/* EOC send data stream	*/
void eocSendDataStreamComplete(ipos_port_id portId, U8 UID,U32 cntRpt, int status,  void *par);
static  void    eocSendDataStreamJob(U32 arg1, U32 arg2, ipos_msg_buf   *pBuf);

/* EOC Get Protocol Data */
void eocProtocolGetComplete(ipos_port_id portId, U8 UID,U32 cntRpt, int status,  void *par);
static  void    eocProtocolGetJob(U32 arg1, U32 arg2, ipos_msg_buf  *pBuf);
extern void parse_internalEoc_Data(ipos_port_id    pid, oamp_cb_msg *pMsg) ;

/* EOC Set Protocol Data */
void eocProtocolSetComplete(ipos_port_id portId, U8 UID,U32 cntRpt, int status,  void *par);
static  void    eocProtocolSetJob(U32 arg1, U32 arg2, ipos_msg_buf  *pBuf);
	 
/* for port status brief	*/
void portStatusBriefComplete(ipos_port_id portId, U8 UID,U32 cntRpt, int status,  
                             ipos_port_status_brief *par);
static  void    portStatusBriefJob(U32 arg1, U32 arg2, ipos_msg_buf *pBuf);


/* for port status	*/
void portStatusComplete(ipos_port_id portId, U8 UID,U32 cntRpt, int status,  
                        ipos_port_status *par);
static  void    portStatusJob(U32 arg1, U32 arg2, ipos_msg_buf  *pBuf);


/* for counters	*/
void portCountersComplete(ipos_port_id portId, U8 UID,U32 cntRpt, int status,  
                          ipos_port_counters *par);
static  void    portCountersJob(U32 arg1, U32 arg2, ipos_msg_buf    *pBuf);

/* for PM counters	*/
void portPmCountersComplete(ipos_port_id portId, U8 UID, U32 cntRpt, int status,  
                          ipos_pm_counters *par);
static  void    portPMCountersJob(U32 arg1, U32 arg2, ipos_msg_buf    *pBuf);

/* for PM Config	*/
void PmConfigComplete(ipos_port_id portId, U8 UID, U32 uPar, int status, void *par);  
static  void    PmConfigJob(U32 arg1, U32 arg2, ipos_msg_buf *pBuf);

/* for PM Failures Trigger	*/
void portPmTriggerComplete(ipos_port_id portId, U8 UID, U32 cntRpt, int status,
                           void *par); 

static  void    portPmTriggerJob(U32 arg1, U32 arg2, ipos_msg_buf    *pBuf);

/* for near-end SNR	*/
void portNeSnrComplete(ipos_port_id portId, U8 UID,U32 cntRpt, int status,  
                       ipos_port_nesnrattn *par);
static  void    portNeSnrJob(U32 arg1, U32 arg2, ipos_msg_buf   *pBuf);

/* for near-end Clear Counters */
void portNeClrCntrComplete(ipos_port_id portId, U8 UID,U32 cntRpt, int status,  
                       void *par);
static  void    portNeClrCntrJob(U32 arg1, U32 arg2, ipos_msg_buf   *pBuf);
void optionalCmdComplete(ipos_port_id portId, U8 UID, U32 tag, int status,  ipos_opCmd_status *par);
static  void    optionalCmdJob(U32 arg1, U32 arg2, ipos_msg_buf *pBuf);

/* send test signals */
void testSignalsComplete(ipos_port_id portId, U8 UID, U32 uPar, int status, void *par);
void detectSignalsComplete(ipos_port_id portId, U8 UID, U32 uPar, int status, void *par);
static  void    testSignalsJob(U32 arg1, U32 arg2, ipos_msg_buf   *pBuf);
static  void    detectSignalsJob(U32 arg1, U32 arg2, ipos_msg_buf   *pBuf);

void HostResetComplete(ipos_port_id portId, U8 UID,U32 cntRpt, int status,  void *par);
static void HostResetJob(U32 arg1, U32 arg2, ipos_msg_buf  *pBuf);
extern int HostResync[];


/* local function to add completion function in to the completion task */

void    addCompletionJob(ipos_port_id portId, U8 UID, U32 cntRpt, int status, U8 *par, int len, completionJob pFunc)
{
    ipos_msg_buf    *pBuf;
    oamp_cb_msg     *pMsg;
 
    pBuf=iposMsgGet();

    if (pBuf==NULL)
    {
        printf("Failed to allocate the msg buffer in handling IPOS API Callback \n");
        return;
    }

    pBuf->len=sizeof(oamp_cb_msg);

    pMsg=(oamp_cb_msg *) pBuf->pDataBuf;
    pMsg->portId=portId;
    pMsg->uid=UID;
    pMsg->parLen=0;
    pMsg->status=status=status;
    pMsg->cntRpt=cntRpt;

    if (len!=0&&par!=NULL)
    	{
        memcpy(pMsg->par, par, len);
        pMsg->parLen=len;
    	}

    if (appOampJobAdd(TID_OAMPFUNC,pFunc, 0, 0, pBuf)!=IPOS_SUCCESS)
    {
        iposMsgFree(pBuf);
        printf("Failed to add job to the oamp task1\n");
    }


    return;
}

/*********************************************************************
**
**		Utility function: 
**	  	  oamp_bme  *getOampHandle( int bmeNum);
**
**
*********************************************************************/


oamp_bme    *getBmeOampHandle(int bmeNum)
{

    return(&oampBme[bmeNum]);


}



/*************************************************************
**
** oamp functions for BME_START. Used by the shell	
**
*************************************************************/

int oampBmeStart(int bmeNum, U8 *pCodeAddr, U32 codeLen, U8 cmdTag)
{

    ipos_port_id    pid;
    ipos_msg_buf    *pBuf;
    oamp_msg        *pMsg;


    pid.bmeNum=bmeNum;
    pid.portNum=0;

    pBuf=iposMsgGet();

    if (pBuf==NULL)
    {
        printf("Failed to allocate the msg buffer in bmeStart \n");
        return(0);
    }

    pBuf->len=sizeof(oamp_cb_msg);

    pMsg=(oamp_msg *) pBuf->pDataBuf;

    pMsg->portId=pid;
    pMsg->par[0]=(U32) pCodeAddr;
    pMsg->par[1]=(U32) codeLen;
    pMsg->par[2]=(U32) cmdTag;
  //  printf("Fusiv oampBmeStart() \n");
    if (appOampJobAdd(TID_BLKFUNC+bmeNum-1,bmeStartJob, 0, 0, pBuf)!=IPOS_SUCCESS)
    {
        printf("Failed to add job to the oamp task1\n");
    }

    return(0);

}

/*************************************************************
**
**  BME_START job running in TID_BLKFUNC task invoked by the shell	
**
*************************************************************/

void    bmeStartJob(U32 arg1, U32 arg2, ipos_msg_buf *pBuf)
{

    ipos_port_id    pid;
    oamp_msg        *pMsg;
    ipos_bme_load   bmeLoadPar;
    //char 	    getdver[ 16 ];

    int errCode = 0;
    U8  cmdTag;
    char    funcName[]="iposBmeStartBlk";

    if (pBuf==NULL||pBuf->len==0)
    {
        printf("No Msgbuf in bmsStartJob \n");
        return;
    }


    pMsg=(oamp_msg *) pBuf->pDataBuf;

    pid=pMsg->portId;

    bmeLoadPar.pFirmware=(U8 *)pMsg->par[0];
    bmeLoadPar.firmwareLen=pMsg->par[1];
    cmdTag=(U8) pMsg->par[2];

    iposMsgFree(pBuf);

    uiLog(pid,funcName,cmdTag,OAMP_IN_PROGRESS, errCode);
    oampBmeStateUpdate(pid.bmeNum, CMD_IN_PROGRESS);    

    errCode=iposBmeStartBlk(pid.bmeNum,&bmeLoadPar,0,0,NULL);

    if (errCode==IPOS_SUCCESS)
    {
        oampBmeStateUpdate(pid.bmeNum, eBME_START); 
        uiLog(pid,funcName,cmdTag,OAMP_COMPLETE, errCode);
        return;


    } else
    {
        oampBmeStateUpdate(pid.bmeNum, CMD_FAILED); 
        uiLog(pid,funcName,cmdTag,OAMP_FAILED, errCode);
        return;

    }

    return;

}

/*************************************************************
**
** oamp functions for BME_RESET. Used by the shell	
**
*************************************************************/

int oampBmeReset(int bmeNum, U8 *pCodeAddr, U32 codeLen, U8 cmdTag)
{

    ipos_port_id    pid;
    ipos_msg_buf    *pBuf;
    oamp_msg        *pMsg;


    pid.bmeNum=bmeNum;
    pid.portNum=0;

    pBuf=iposMsgGet();

    if (pBuf==NULL)
    {
        printf("Failed to allocate the msg buffer in bmeReset \n");
        return(0);
    }

    pBuf->len=sizeof(oamp_cb_msg);

    pMsg=(oamp_msg *) pBuf->pDataBuf;

    pMsg->portId=pid;
    pMsg->par[0]=(U32) pCodeAddr;
    pMsg->par[1]=(U32) codeLen;
    pMsg->par[2]=(U32) cmdTag;

    if (appOampJobAdd(TID_BLKFUNC,bmeResetJob, 0, 0, pBuf)!=IPOS_SUCCESS)
    {
        printf("Failed to add job to the oamp task1\n");
    }

    return(0);

}

/*************************************************************
**
**  BME_Reset job running in TID_BLKFUNC task invoked by the shell	
**
*************************************************************/

void    bmeResetJob(U32 arg1, U32 arg2, ipos_msg_buf *pBuf)
{

    ipos_port_id    pid;
    oamp_msg        *pMsg;

    ipos_bme_load   bmeLoadPar;

    int errCode = 0;
    U8  cmdTag;
    char    funcName[]="iposBmeResetBlk";

    if (pBuf==NULL||pBuf->len==0)
    {
        printf("No Msgbuf in bmsResetJob \n");
        return;
    }


    pMsg=(oamp_msg *) pBuf->pDataBuf;

    pid=pMsg->portId;

    bmeLoadPar.pFirmware=(U8 *)pMsg->par[0];
    bmeLoadPar.firmwareLen=pMsg->par[1];
    cmdTag=(U8) pMsg->par[2];

    iposMsgFree(pBuf);

    uiLog(pid,funcName,cmdTag,OAMP_IN_PROGRESS, errCode);

//	oampBmeStateUpdate(pid.bmeNum, CMD_IN_PROGRESS);	
    oampBmeStateUpdate(pid.bmeNum, eBME_RESTART_INPROGRESS);    

    errCode=iposBmeResetBlk(pid.bmeNum,&bmeLoadPar,0,0,NULL);

    if (errCode==IPOS_SUCCESS)
    {
        oampBmeStateUpdate(pid.bmeNum, eBME_RESTART_COMPLETE);  
        uiLog(pid,funcName,cmdTag,OAMP_COMPLETE, errCode);
        return;

    } else
    {
//		oampBmeStateUpdate(pid.bmeNum, eBME_RESTART_FAILED);	
        oampBmeStateUpdate(pid.bmeNum, CMD_FAILED); 
        uiLog(pid,funcName,cmdTag,OAMP_FAILED, errCode);
        return;

    }

    return;

}



/*************************************************************
**
**	 oamp functions for bme provision. 
**
**			input:	ipos_bme_provision structure.
**
**			return: IPOS_SUCCESS or IPOS_ERROR_CODE
**
**			Note: Callback function will print the status of the execution	
**			
*************************************************************/
int oampBmeProvision(int    bmeNum, ipos_bme_provision *pBmeProvision, U8 cmdTag, U32 cntRpt)
{

    int errCode=IPOS_SUCCESS;
    ipos_port_id    pid;
    char    funcName[]="iposBmeProvision";

    pid.bmeNum=bmeNum;
    pid.portNum=0;

#ifdef	NOTI

    /* BME state: must be in the idle state to run	*/
    if (oampGetBmeState(bmeNum)!=uIDLE)
    {
        errCode=OAMP_ERROR_BME_NOT_READY;
        uiLog(pid,funcName,cmdTag,OAMP_FAILED, errCode);
        return(errCode);

    }
#endif

    /* issue the API call and print the status	*/

    errCode=iposBmeProvision(bmeNum, pBmeProvision, cmdTag, cntRpt, bmeProvisionComplete);

    if (errCode!=IPOS_SUCCESS)
    {
        uiLog(pid,funcName,cmdTag,OAMP_FAILED, errCode);
        return(errCode);

    }

/* update teh port state	*/

    oampBmeStateUpdate(pid.bmeNum, CMD_IN_PROGRESS);    
    uiLog(pid,funcName,cmdTag,OAMP_IN_PROGRESS, errCode);

    return(IPOS_SUCCESS);

}


/* callback for bme complete	*/
void bmeProvisionComplete(ipos_port_id portId, U8 UID,U32 cntRpt, int status,  void *par)
{
    addCompletionJob(portId, UID, cntRpt, status, par,0,bmeProvisionJob);
}



/* the complete function running in the oamp task	*/
static  void    bmeProvisionJob(U32 arg1, U32 arg2, ipos_msg_buf    *pBuf)
{

    oamp_cb_msg     *pMsg;
    ipos_port_id    pid;

    BYTE pTlStr[20];
    ipos_port_id temp;
    U32     cntRpt;
    U8      cmdTag;
    int     errCode = 0;
    char    funcName[]="iposBmeProvision";

#if defined(INI_SUPPORT)
    tsIposPtmfConfig        tsIniConfig;
#endif

    if (pBuf==NULL)
    {
        printf("Invalid msg buffer in iposBmeProvision Callback \n");
        return;
    }

    /* parse the message	*/
    pMsg=(oamp_cb_msg *) pBuf->pDataBuf;

    pid     =pMsg->portId;
    cntRpt  =pMsg->cntRpt;
    cmdTag  =pMsg->uid;
printf("bmeProvisionJob(): pMsg->status %x\n",pMsg->status);
    /* update the state		*/
    if (pMsg->status==IPOS_SUCCESS)
    {


#ifdef INI_SUPPORT 

#if defined(P2CO) || defined(BME_CPE_20)
        memcpy (&tsIniConfig,&tsIniDefaultConfig,sizeof(tsIposPtmfConfig));
#endif

#ifdef P2CPE
        tsIniConfig.bEnetPhyAddr = PTMF_ENET_PHY_BASE_ADDR;
        tsIniConfig.bUtopiaTrafficMode = UTOP_TRAFF_NO_IDLE_CELL;
#endif

#ifdef CPE_40
            tsIniConfig.bUtopiaCellMode=UTOP_CELL_ALL_DATA;
            tsIniConfig.bUtopiaTrafficMode = UTOP_TRAFF_NO_IDLE_CELL;
#else
        if (oampGetUtopiaMode == 0x80)
        {
            tsIniConfig.bUtopiaCellMode=UTOP_CELL_ALL_DATA;

#if defined(VTU_R)
            tsIniConfig.bUtopiaTrafficMode = UTOP_TRAFF_NO_IDLE_CELL;
#else
            tsIniConfig.bUtopiaTrafficMode = UTOP_TRAFF_IDLE_CELL;
#endif

//    printf ("\n PTM MODE");
        } else if (oampGetUtopiaMode == 0)
        {
            tsIniConfig.bUtopiaCellMode=UTOP_CELL_HEC_HDR;
            tsIniConfig.bUtopiaTrafficMode = UTOP_TRAFF_NO_IDLE_CELL;
//    printf ("\n ATM MODE");
        } else
        {
          tsIniConfig.bUtopiaCellMode=UTOP_CELL_ALL_DATA;

#if defined(VTU_R)
          tsIniConfig.bUtopiaTrafficMode = UTOP_TRAFF_IDLE_CELL;
#else
          tsIniConfig.bUtopiaTrafficMode = UTOP_TRAFF_NO_IDLE_CELL ;
#endif

//    printf ("\n PTM LOW LATENCY MODE");
        }
#endif  // CPE_40

        if (oampGetEnetFlowControl > 2)
        {
            oampSetEnetFlowControl(0);
        }
        tsIniConfig.bFlowControl = oampGetEnetFlowControl;

        iposPtmfSetup (&tsIniConfig);

#if defined(VTU_R) && (defined(P2CPE) || defined(BME_CPE_20))
	      iposPtmfAddrTabInit(&macFilCfgShadow);
#endif
#endif


        UserLog ("Bme %i System Provisioned",pid.bmeNum,pid.portNum);
        uiLog(pid,funcName,cmdTag,OAMP_COMPLETE, errCode);
        oampBmeStateUpdate(pid.bmeNum,eSYSTEM_PROVISION);
        if(oampGetOamOptionMask & 0x3) //If PM Alerts are enabled, call PmConfig
        {
            int portidx, bmeidx, lineNum;

            for (lineNum = 1; lineNum <= IPOS_MAX_PORTS; lineNum++)
                if (getBmePortId (lineNum, &temp) == IPOS_SUCCESS)
                {
		    //Initialize PM Handler
		    InitPMHandler (pid.bmeNum, temp.portNum);

		    //Call pmConfig
                    sprintf (pTlStr,"pmconfig:0:%i:%i;", pid.bmeNum, temp.portNum);
                    ReceiveTLCommand (pTlStr);
                } 
        
            for (bmeidx = 0; bmeidx <= IPOS_MAX_BME; bmeidx++)
                for (portidx = 0; portidx <= IPOS_MAX_PORT_PER_BME; portidx++)
                {
                    iFailStatus[bmeidx][portidx] = 0;
                    portStartCopy[bmeidx][portidx] = 0;
                }
        }

    } else
    {
        uiLog(pid,funcName,cmdTag,OAMP_FAILED, errCode);
        oampBmeStateUpdate(pid.bmeNum,CMD_FAILED);

    }

    /* release the message	*/
    iposMsgFree(pBuf);

}


/* get BME version	*/

int oampBmeGetVersion(int bmeNum, U8 cmdTag, U32 cntRpt)
{

    int errCode=IPOS_SUCCESS;
    ipos_port_id    pid;
    char    funcName[]="iposDeviceVersion";

    pid.bmeNum=bmeNum;
    pid.portNum=0;


    /* issue the API call and print the status	*/

    errCode=iposDeviceVersion(bmeNum, cmdTag, cntRpt, bmeVersionComplete);

    if (errCode!=IPOS_SUCCESS)
    {
        uiLog(pid,funcName,cmdTag,OAMP_FAILED, errCode);
        return(errCode);

    }

    uiLog(pid,funcName,cmdTag,OAMP_IN_PROGRESS, errCode);

    return(IPOS_SUCCESS);
}


/* callback for bme complete	*/
void bmeVersionComplete(ipos_port_id portId, U8 UID,U32 cntRpt, int status,  ipos_bme_ver *par)
{
    int size;
    size=sizeof(ipos_bme_ver);
    addCompletionJob(portId, UID, cntRpt, status, (U8*) par,size,bmeVersionJob);
}



/* the complete function running in the oamp task	*/
static  void    bmeVersionJob(U32 arg1, U32 arg2, ipos_msg_buf  *pBuf)
{

    oamp_cb_msg     *pMsg;
    ipos_port_id    pid;

    U32     cntRpt;
    U8      cmdTag;
    int     errCode = 0;
    ipos_bme_ver    *pVer;

    char    funcName[]="iposDeviceVersion";

    if (pBuf==NULL)
    {
        printf("Invalid msg buffer in iposDeviceVersion Callback \n");
        return;
    }

    /* parse the message	*/
    pMsg=(oamp_cb_msg *) pBuf->pDataBuf;

    pid     =pMsg->portId;
    cntRpt  =pMsg->cntRpt;
    cmdTag  =pMsg->uid;
    pVer    =(ipos_bme_ver *) pMsg->par;

    /* update the state		*/
    if (pMsg->status==IPOS_SUCCESS)
    {

        uiLog(pid,funcName,cmdTag,OAMP_COMPLETE, errCode);
        uiLogDeviceVer(pid.bmeNum, pVer->VersionLen, pVer->VersionStr); 

    } else
    {
        uiLog(pid,funcName,cmdTag,OAMP_FAILED, errCode);

    }

    /* release the message	*/
    iposMsgFree(pBuf);

}


/* oamp functions for Host Reset. */
int oampHostReset(int bmeNum, U8 *pCodeAddr, U8 cmdTag, U32 cntRpt)
{
    int errCode=IPOS_SUCCESS;
    char    funcName[]="iposHostReset";
    ipos_port_id    pid;

    pid.bmeNum=bmeNum;
    pid.portNum=0;

    /* get the port and BME state	*/
    if (oampGetBmeState(bmeNum)!=uDOWN)
    {
        errCode=OAMP_ERROR_BME_RUN;
        uiLog(pid,funcName,cmdTag,OAMP_FAILED, errCode);
        return(errCode);
    }

    /* issue the API call and print the status	*/
    errCode=iposHostReset(bmeNum, cmdTag, cntRpt, HostResetComplete);

    if (errCode!=IPOS_SUCCESS)
    {
        uiLog(pid,funcName,cmdTag,OAMP_FAILED, errCode);
        return(errCode);
    }

    uiLog(pid,funcName,cmdTag,OAMP_IN_PROGRESS, errCode);

    return(IPOS_SUCCESS);
}

void HostResetComplete(ipos_port_id portId, U8 UID,U32 cntRpt, int status,  void *par)
{
    char *x = par;
    addCompletionJob(portId, UID, cntRpt, status, (x + 1), *(x) ,HostResetJob);

}
static void HostResetJob(U32 arg1, U32 arg2, ipos_msg_buf  *pBuf)
{
    oamp_cb_msg     *pMsg;
    ipos_port_id    pid;
    U32     cntRpt;
    U8      cmdTag;
    int     errCode;
    int resync_count = 0;
    char    funcName[]="iposHostReset";
    int bmenum;
    BYTE  perrep_str [50];

    if (pBuf==NULL) {
        printf("Invalid msg buffer in iposHostReset Callback \n");
        return;
    }

    /* parse the message	*/
    pMsg=(oamp_cb_msg *) pBuf->pDataBuf;
    pid     =pMsg->portId;
    cntRpt  =pMsg->cntRpt;
    cmdTag  =pMsg->uid;
    errCode =pMsg->status;

    /* update the state		*/
    if (pMsg->status==IPOS_SUCCESS) {
        uiLog(pid,funcName,cmdTag,OAMP_COMPLETE, errCode);
		    oampHost_HotReset_StateUpdate(pid, pMsg);
	HostResync [pid.bmeNum] = 0;

        /* If all the BMEs are resynced, then enable the periodic reports */
        resync_count = 0;
        for (bmenum=1; bmenum <= IPOS_MAX_BME; bmenum++) { 
          resync_count += HostResync [bmenum];
        }
        if (resync_count == 0) {
          for (bmenum=1; bmenum <=IPOS_MAX_BME; bmenum++) { 
            sprintf(perrep_str,"perrep:99:%i:0:7:20:30;", bmenum);
            ReceiveTLCommand ((U8*)perrep_str);
          }
        }

    } else {
        uiLog(pid,funcName,cmdTag,OAMP_FAILED, errCode);
        oampStateUpdate(pid,CMD_FAILED);
    }

    /* release the message	*/
    iposMsgFree(pBuf);

}

/*************************************************************
**
** oamp functions for port_provision. Used by the shell	
**
*************************************************************/

int oampPortProvision(ipos_port_id  *pPid,ipos_port_profile *pPortProvision, U8 cmdTag, U32 cntRpt)
{

    int errCode=IPOS_SUCCESS;
    char    funcName[]="iposPortProvision";
    int portState;

#if defined(INI_SUPPORT)
    int slow,fast;
    int lineNum;
    int profilenum;
#endif


    /* get the port and BME state	*/
    if (oampGetBmeState(pPid->bmeNum)!=uRUN)
    {

        errCode=OAMP_ERROR_BME_NOT_READY;

        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);

        return(errCode);

    }

/* get the port state	*/
    portState=oampGetPortState(*pPid);

    if (portState==uSHOWTIME||portState==uACTIVATING)
    {
        errCode=OAMP_ERROR_PORT_NOT_READY;

        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);

        return(errCode);

    }

#ifdef INI_SUPPORT

#if (defined(P2CPE) || defined(BME_CPE_20))
// This function swaps the Fast and Interleave Utopia PHY address. For a single port CPE
// hardware, the ethernet data can send/receive only Utopia Phy Addr #0. Hence this swapping
//function is needed. This function was moved from Bme Provision to Port provision for maintaining
//consistency w.r.t CO. (Fix for Bug TTId #1175) 
    setUtopiaAddr();
#endif

	getLineNum(*pPid,&lineNum);
	profilenum=ProfileMapping[lineNum];

	//oampBme[pPid->bmeNum].oampPort[pPid->portNum].pPortProfile->PortProfileNum;

    getUtopiaSlow(lineNum,&slow);
    getUtopiaFast(lineNum,&fast);

    sPortProfiles[profilenum].PortProvParms.InterleavedChannel=slow;
    sPortProfiles[profilenum].PortProvParms.FastChannel=fast;   

#endif  //INI_SUPPORT


    /* issue the API call and print the status	*/

    errCode=iposPortProvision(*pPid, pPortProvision, cmdTag, cntRpt, portProvisionComplete);

    if (errCode!=IPOS_SUCCESS)
    {

        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);
        return(errCode);

    }

/* update teh port state	*/

    oampStateUpdate(*pPid, CMD_IN_PROGRESS);    
    uiLog(*pPid,funcName,cmdTag,OAMP_IN_PROGRESS, errCode);

    return(IPOS_SUCCESS);

}


void portProvisionComplete(ipos_port_id portId, U8 UID,U32 cntRpt, int status,  void *par)
{
    addCompletionJob(portId, UID, cntRpt, status, par,0,portProvisionJob);
}



/* the complete function running in the oamp task	*/
static  void    portProvisionJob(U32 arg1, U32 arg2, ipos_msg_buf   *pBuf)
{

    oamp_cb_msg     *pMsg;

    ipos_port_id    pid;

    U32     cntRpt;
    U8      cmdTag;
    int     errCode;
    char    funcName[]="iposPortProvision";

#ifdef INI_SUPPORT 


#if (defined (P2CO) || defined(CO_20) || defined(CPE_40))
    int  IniIndex, IniSlow, IniFast;
    tsIposPtmfPortConfig  tsIniPortCfg;
    int lineNum;
    int framingmode;
    int profilenum;
#endif
#endif  //INI_SUPPORT

    if (pBuf==NULL)
    {
        printf("Invalid msg buffer in iposPortProvision Callback \n");
        return;
    }

    /* parse the message	*/
    pMsg=(oamp_cb_msg *) pBuf->pDataBuf;

    pid     =pMsg->portId;
    cntRpt  =pMsg->cntRpt;
    cmdTag  =pMsg->uid;
    errCode =pMsg->status;
    /* update the state		*/
    if (pMsg->status==IPOS_SUCCESS)
    {

        UserLog ("Bme %i Port %i Provisioned",pid.bmeNum,pid.portNum);
        uiLog(pid,funcName,cmdTag,OAMP_COMPLETE, errCode);
        oampStateUpdate(pid,ePORT_PROVISION);

#if defined(INI_SUPPORT)

#if (defined(P2CO) || defined(CPE_40))

        getLineNum(pid,&lineNum);
        
        profilenum=ProfileMapping[lineNum];
        
        framingmode = sPortProfiles[profilenum].PortProvParms.framingmode;

        getIniMap(pid, &IniIndex, &IniSlow, &IniFast);

        iposPtmfReadPortConfig(IniIndex, &tsIniPortCfg);

        // If not dual latency, then the function getIniMap returns the 
        // same value in IniSlow and IniFast. That's why the following 
        // assignment.
        tsIniPortCfg.bUtopiaPort = IniFast;
        
        if(framingmode == 0x90 || framingmode == 0x91)
        {
          tsIniPortCfg.bTxHdlcHcf = 0x30;
          tsIniPortCfg.bTxHdlcHaf = 0x7b;
          printf("\n EFM Mode; THAFR = 0x307b");
        }
        else
        {
          tsIniPortCfg.bTxHdlcHcf = 0x03;
          tsIniPortCfg.bTxHdlcHaf = 0xff;
        }

        iposPtmfWritePortConfig(IniIndex, &tsIniPortCfg);

		if (IniFast != IniSlow)
		{
		    iposPtmfReadPortConfig((IniIndex + 1), &tsIniPortCfg);

            tsIniPortCfg.bUtopiaPort = IniSlow;

            iposPtmfWritePortConfig((IniIndex + 1), &tsIniPortCfg);
		}
#endif

#endif //INI_SUPPORT

    } else
    {
        uiLog(pid,funcName,cmdTag,OAMP_FAILED, errCode);
        oampStateUpdate(pid,CMD_FAILED);


#if 0		/* no restart	*/
  
        /* any repeat action add here	*/
        if (cntRpt!=0)
        {
            cntRpt--;
            errCode=iposPortProvision(pid, cmdTag, cntRpt, portProvisionComplete);

            if (errCode!=IPOS_SUCCESS)
            {
                uiLog(pid,funcName,cmdTag,OAMP_FAILED, errCode);
                return;

            }

            uiLog(pid,funcName,cmdTag,OAMP_RESTART, errCode);
            oampStateUpdate(pid,CMD_IN_PROGRESS);
        }
#endif	

    }


    /* release the message	*/
    iposMsgFree(pBuf);

}

/*************************************************************
**
** oamp functions for starting a port.	
**
*************************************************************/
static int oampCmnPortStart(ipos_port_id *pPid, U8 cmdTag, U32 cntRpt, int feStart)
{

    int     errCode=IPOS_SUCCESS;
		char		*funcName;

#if defined (INI_SUPPORT)
//    int     ethPhy=0;
//    int     PortNum;
	int     IniIndex, IniSlow, IniFast;
//	tsIposPtmfPortConfig  tsIniPortCfg;
#endif  //INI_SUPPORT


		if (feStart)
			funcName = "iposPortInstantiate";
		else
			funcName = "iposPortStart";

    /* get the port and BME state	*/
    if (oampGetBmeState(pPid->bmeNum)!=uRUN)
    {

        errCode=OAMP_ERROR_BME_NOT_READY;

        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);

        return(errCode);

    }


#ifdef	NOTI
/* get the port state	*/

    if (oampGetPortState(*pPid)!=uENABLE)
    {
        errCode=OAMP_ERROR_PORT_NOT_READY;

        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);

        return(errCode);

    }

#endif

#if INI_SUPPORT
/************* Start add for Dual Latency - mrrk *************/
#if 0
    getLineNum(*pPid, &PortNum);

    if (getIniPhy(PortNum, &ethPhy)!=IPOS_SUCCESS)
    {

        errCode=OAMP_ERROR_UNKNOWN_LINE_NUM;

        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);

        return(errCode);

    }
#endif
/************* End add for Dual Latency - mrrk *************/


/************* Start add for Dual Latency - mrrk *************/

    getIniMap(*pPid, &IniIndex, &IniSlow, &IniFast);
    
    iposPtmfStartRx ((U8)(IniIndex + 1), NULL);

    if (IniFast != IniSlow)
    {
      iposPtmfStartRx ((U8)(IniIndex + 2), NULL);
    }
/************* End add for Dual Latency - mrrk *************/

//    iposPtmfStartRx ((U8)PortNum, NULL);

#endif	//INI_SUPPORT


    /* issue the API call and print the status	*/

		if (feStart)
			errCode=iposPortInstantiate(*pPid, cmdTag, cntRpt, portInstantiateComplete);
		else
			errCode=iposPortStart(*pPid, cmdTag, cntRpt, portStartComplete);

    if (errCode!=IPOS_SUCCESS)
    {

        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);
        return(errCode);

    }

    /* update teh port state	*/

		if (feStart)
			UserLog ("Bme %i Port %i Attempting Instantiate",pPid->bmeNum,pPid->portNum);
		else
			UserLog ("Bme %i Port %i Attempting Activation",pPid->bmeNum,pPid->portNum);
    oampStateUpdate(*pPid, ePORT_START);    
    oampStateUpdate(*pPid, CMD_IN_PROGRESS);
    uiLog(*pPid,funcName,cmdTag,OAMP_IN_PROGRESS, errCode);
    invalidateShowtimeParams( *pPid );

    return(IPOS_SUCCESS);
}

/* the complete function running in the oamp task	*/
static void cmnPortStartJob(U32 arg1, U32 arg2, ipos_msg_buf   *pBuf, int feStart)
{
  oamp_cb_msg     *pMsg;
  ipos_port_id    pid;
  U32     cntRpt;
  U8      cmdTag;
  int     errCode;
  char    *funcName;
 // int 	  lineNum=1;
  char	str[ 82 ];
  ipos_port_status    *pStatus;
#ifdef VTU_R
  ipos_port_profile   *pPortProfile;
  int 	  lineNum=1;
#endif
//  int i;

#if defined(INI_SUPPORT)
//  int     PortNum=0;
  int  IniIndex, IniSlow, IniFast;
#endif


		if (feStart)
			funcName = "iposPortInstantiate";
		else
			funcName = "iposPortStart";

    if (pBuf==NULL)
    {
        printf("Invalid msg buffer in %s Callback \n", funcName);

        return;
    }

    /* parse the message	*/
    pMsg=(oamp_cb_msg *) pBuf->pDataBuf;
    pid     =pMsg->portId;
    cntRpt  =pMsg->cntRpt;
    cmdTag  =pMsg->uid;
    errCode =pMsg->status;

 
    /* update the state		*/
    if (pMsg->status==IPOS_SUCCESS)
    {
#ifdef INI_SUPPORT

#if defined(P2CO) && defined(SIM16P)
        UtopiaTxEnable(pid.bmeNum);
#endif

/* Start Deletion for Dual Latency   */
/*        getLineNum(pid, &PortNum);

        iposPtmfStartTx ((U8)PortNum, NULL);*/
/* End Deletion for Dual Latency   */
/************* Start add for Dual Latency - mrrk *************/
		getIniMap(pid, &IniIndex, &IniSlow, &IniFast);
             
        iposPtmfStartTx ((U8)(IniIndex + 1), NULL);

        if (IniFast != IniSlow)
        {
          iposPtmfStartTx ((U8)(IniIndex + 2), NULL);
        }
/************* End add for Dual Latency - mrrk *************/
        // Reset BME counters and CRC threshold flags
        ResetCrcThresCnt(pid);

#endif //INI_SUPPORT

				pStatus=(ipos_port_status *) pMsg->par;
#ifdef VTU_O
				setLineCrcThreshold(pid, pStatus->FastUsPayloadRate,
														pStatus->SlowUsPayloadRate);
#else
				setLineCrcThreshold(pid, pStatus->FastDsPayloadRate,
														pStatus->SlowDsPayloadRate);
#endif

        InitPerfPara(pid.bmeNum,pid.portNum);

				if (feStart)
					UserLog ("Bme %i Port %i Instantiate Successfully",pid.bmeNum,pid.portNum);
				else
					UserLog ("Bme %i Port %i Activate Successfully",pid.bmeNum,pid.portNum);
        uiLog(pid,funcName,cmdTag,OAMP_COMPLETE, errCode);

#if ALASKA
	vdsl_link_up();
	oampPortGetLinkRates(&pid, 0, 0);
#endif
        if (oampGetOamOptionMask & 0x1) //If PM Counters are enabled, reset
        {
            iFailStatus[pid.bmeNum][pid.portNum] = 0;
            portStartCopy[pid.bmeNum][pid.portNum] = 1;
        }
        oampStateUpdate(pid,ePORT_START_COMPLETE);
		str[0] = '\0';
		if (blk2Shadow.enetStatusRptEn == 1)
        sprintf( &str[ 0 ], "setaenetrpt::%d:%d:%d;", pid.bmeNum, pid.portNum,blk2Shadow.enetStatusRptEn);
		if (str[ 0 ] != '\0')
	        ReceiveTLCommand( &str[ 0 ] );
#ifdef VTU_R
    pStatus=(ipos_port_status *) pMsg->par;
	 getLineNum(pid, &lineNum);
	pPortProfile = getPortProfile(ProfileMapping[lineNum]);
	
/*	printf ("\nIntr per %x %x  %x %x ",
		pPortProfile->PortProvParms.UpstreamMaxRateFast,
		pPortProfile->PortProvParms.InterleavedPercentage,
		pStatus->FastDsPayloadRate
		,pStatus->SlowDsPayloadRate);
*/
	if ((pStatus->FastDsPayloadRate==0) && (pPortProfile->PortProvParms.InterleavedPercentage==100))
		{
		printf ("\nCO provisioned as Slow mode. CPE provisioned as Fast mode."); 
		printf ("\nSwitching to Slow mode on CPE...\n");

		// CO provisioned as Slow, Switch CPE provisioning to Slow mode
		pPortProfile->PortProvParms.InterleavedPercentage=0;
	    pPortProfile->PortProvParms.UpstreamMaxRateSlow=pPortProfile->PortProvParms.UpstreamMaxRateFast;
		pPortProfile->PortProvParms.UpstreamMinRateSlow=pPortProfile->PortProvParms.UpstreamMinRateFast;
		pPortProfile->PortProvParms.UpstreamMaxRateFast=0;
		pPortProfile->PortProvParms.UpstreamMinRateFast=0;
		updateFlashProf();

		oampBmeStateReset(pid.bmeNum);
		}
	else if ((pStatus->SlowDsPayloadRate==0) && (pPortProfile->PortProvParms.InterleavedPercentage==0))
		{
		printf ("\nCO provisioned as Fast mode. CPE provisioned as Slow mode."); 
		printf ("\nSwitching to Fast mode on CPE...\n");

		// CO provisioned as Fast, Switch CPE provisioning to Fast mode
		pPortProfile->PortProvParms.InterleavedPercentage=100;
	    pPortProfile->PortProvParms.UpstreamMaxRateFast=pPortProfile->PortProvParms.UpstreamMaxRateSlow;
		pPortProfile->PortProvParms.UpstreamMinRateFast=pPortProfile->PortProvParms.UpstreamMinRateSlow;
		pPortProfile->PortProvParms.UpstreamMaxRateSlow=0;
		pPortProfile->PortProvParms.UpstreamMinRateSlow=0;
		updateFlashProf();
		oampBmeStateReset(pid.bmeNum);
 
		}

#endif




	if (oampBmeHasItCrashed( pid.bmeNum ))
       	{
		int bmeErrorCode = oampGetBmeErrorCode( pid.bmeNum );

    		printf( "port start completes with BME error code %d\n", bmeErrorCode );

	    /* Next line needs to be an event log ... */

		printf( "\nBME %d marked as having crashed\n", pid.bmeNum );
#ifdef VTU_R
/* CPE notifies its remote partner */
		iposNotifyBmeCrash( pid, bmeErrorCode, cmdTag, 0, NULL );
#endif
        }

	oampBmeMarkNotCrashed( pid.bmeNum );

    storeShowtimeParams( pid, pStatus );
	
    } else
    {
				if (feStart)
                                { 
					printf ("Bme %i Port %i Instantiate Failed",pid.bmeNum,pid.portNum);
					UserLog ("Bme %i Port %i Instantiate Failed",pid.bmeNum,pid.portNum);
                                }
				else
                                {
					printf ("Bme %i Port %i Activate Failed",pid.bmeNum,pid.portNum);
					UserLog ("Bme %i Port %i Activate Failed",pid.bmeNum,pid.portNum);
                                }
        if (pMsg->parLen == 4)
            printf("\n BME error code %d\n", *((int *)pMsg->par));
        uiLog(pid,funcName,cmdTag,OAMP_FAILED, errCode);
        oampStateUpdate(pid,CMD_FAILED);  

        /* any repeat action add here	*/
        if (cntRpt!=0)
        {
            cntRpt--;
						if (feStart)
							errCode=iposPortInstantiate(pid, cmdTag, cntRpt, portInstantiateComplete);
						else
							errCode=iposPortStart(pid, cmdTag, cntRpt, portStartComplete);

            if (errCode!=IPOS_SUCCESS)
            {
                uiLog(pid,funcName,cmdTag,OAMP_FAILED, errCode);
                return;

            }

            uiLog(pid,funcName,cmdTag,OAMP_RESTART, errCode);
            oampStateUpdate(pid,CMD_IN_PROGRESS);

        }
    }

    /* release the message	*/
    iposMsgFree(pBuf);


}

/*************************************************************
**
** oamp functions for portInstantiate. Used by the shell	
**
*************************************************************/

int oampPortInstantiate(ipos_port_id *pPid, U8 cmdTag, U32 cntRpt)
{
	return (oampCmnPortStart(pPid, cmdTag, cntRpt, 1));
}


/* the complete functions should be short one: add job to oampTask1	*/

void portInstantiateComplete(ipos_port_id portId, U8 UID,U32 cntRpt, int status,  void *par)
{
	int len;

  if (status==IPOS_SUCCESS)
	  len=sizeof(ipos_port_status);
  else
    len = 4;

    if (par != NULL)
        addCompletionJob(portId, UID, cntRpt, status, par, len, portInstantiateJob);
    else
        addCompletionJob(portId, UID, cntRpt, status,NULL, 0, portInstantiateJob);
}



/* the complete function running in the oamp task	*/
static  void    portInstantiateJob(U32 arg1, U32 arg2, ipos_msg_buf *pBuf)
{	
	cmnPortStartJob(arg1, arg2, pBuf, 1);
}

void lbStartCompleteJob(U32 arg1, U32 arg2, ipos_msg_buf   *pBuf)
{

    oamp_cb_msg     *pMsg;

    ipos_port_id    pid;

    U32     cntRpt;
    U8      cmdTag;
    int     errCode;
    char    funcName[]="iposLbStart";
    //int     PortNum=0;
   

    if (pBuf==NULL)
    {
        printf("Invalid msg buffer in iposPortStart Callback \n");
        return;
    }

    /* parse the message	*/
    pMsg=(oamp_cb_msg *) pBuf->pDataBuf;

    pid     =pMsg->portId;
    cntRpt  =pMsg->cntRpt;
    cmdTag  =pMsg->uid;
    errCode =pMsg->status;


    /* update the state		*/
    if (pMsg->status==IPOS_SUCCESS)
    {

#ifdef INI_SUPPORT
		int     PortNum=0;
#endif

#if defined(P2CO) && defined(SIM16P)
        UtopiaTxEnable(pid.bmeNum);
#endif

#ifdef INI_SUPPORT
        getLineNum(pid, &PortNum);

        iposPtmfStartTx ((U8)PortNum, NULL);

        // Reset BME counters and CRC threshold flags
        ResetCrcThresCnt(pid);

#endif //INI_SUPPORT

        InitPerfPara(pid.bmeNum,pid.portNum);


        UserLog ("Bme %i Port %i loopback Activated Successfully",pid.bmeNum,pid.portNum);
        uiLog(pid,funcName,cmdTag,OAMP_COMPLETE, errCode);
        oampStateUpdate(pid,ePORT_LB_START_COMPLETE);

    } else
    {
        UserLog ("Bme %i Port %i loopback Activated Failed",pid.bmeNum,pid.portNum);
        if (pMsg->parLen == 4)
            printf("\n BME error code %d", *((int *)pMsg->par));
        uiLog(pid,funcName,cmdTag,OAMP_FAILED, errCode);
        oampStateUpdate(pid,CMD_FAILED);  

    }

    /* release the message	*/
    iposMsgFree(pBuf);
}

void lbStartComplete(ipos_port_id portId, U8 UID,U32 cntRpt, int status,  void *par)
{   
    addCompletionJob(portId, UID, cntRpt, status, NULL, 0, lbStartCompleteJob);
}

/*************************************************************
**
** oamp functions for loopback. Used by the shell	
**
*************************************************************/

int oampLbStart(ipos_port_id *pPid, U8 cmdTag, U32 cntRpt, int lpbkType)
{

    int     errCode=IPOS_SUCCESS;
    char    funcName[]="iposPortStart";
    
#if INI_SUPPORT
    int     PortNum;
    int     ethPhy=0;
#endif


    /* get the port and BME state	*/
    if (oampGetBmeState(pPid->bmeNum)!=uRUN)
    {
        errCode=OAMP_ERROR_BME_NOT_READY;
        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);
        return(errCode);
    }



#ifdef	NOTI
/* get the port state	*/

    if (oampGetPortState(*pPid)!=uENABLE)
    {
        errCode=OAMP_ERROR_PORT_NOT_READY;

        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);

        return(errCode);

    }

#endif

#if INI_SUPPORT
    getLineNum(*pPid, &PortNum);

    if (getIniPhy(PortNum, &ethPhy)!=IPOS_SUCCESS)
    {

        errCode=OAMP_ERROR_UNKNOWN_LINE_NUM;

        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);

        return(errCode);

    }

    iposPtmfStartRx ((U8)PortNum, NULL);

#endif	//INI_SUPPORT


    /* issue the API call and print the status	*/

    errCode=iposLbStart(*pPid, cmdTag, lpbkType, lbStartComplete);

    if (errCode!=IPOS_SUCCESS)
    {

        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);
        return(errCode);

    }

/* update teh port state	*/

    UserLog ("Bme %i Port %i Attempting loopback",pPid->bmeNum,pPid->portNum);
    oampStateUpdate(*pPid, ePORT_START);    
    oampStateUpdate(*pPid, CMD_IN_PROGRESS);
    uiLog(*pPid,funcName,cmdTag,OAMP_IN_PROGRESS, errCode);

    return(IPOS_SUCCESS);

}


/*************************************************************
**
** oamp functions for test Signal
**
*************************************************************/

int oampSendSignal(ipos_port_id *pPid, ipos_test_signals * pTestSignals, U8 cmdTag, U32 sigType)
{
 int errCode=IPOS_SUCCESS;
 char    funcName[]="iposSendTestSignals";
 int portState;

	/* get the port and BME state	*/
	if (oampGetBmeState(pPid->bmeNum)!=uRUN)
	{
		 errCode=OAMP_ERROR_BME_NOT_READY;

		 uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);

		 return(errCode);
	}

	/* get the port state	*/
	portState=oampGetPortState(*pPid);
	if ( (portState == uSHOWTIME) || (portState==uACTIVATING) )
	{
		 errCode=OAMP_ERROR_PORT_NOT_READY;

		 uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);

		 return(errCode);
	}


	/* issue the API call and print the status	*/
	errCode=iposSendTestSignals (*pPid, pTestSignals, cmdTag, sigType, testSignalsComplete);
	if (errCode!=IPOS_SUCCESS)
	{
		 uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);
		 return(errCode);
	}

	return(IPOS_SUCCESS);
}


void testSignalsComplete(ipos_port_id portId, U8 UID, U32 sigType, int status,  void *par)
{
    addCompletionJob(portId, UID, sigType, status, par, 0, testSignalsJob);
}



/* the complete function running in the oamp task	*/
static void testSignalsJob(U32 arg1, U32 arg2, ipos_msg_buf *pBuf)
{
    oamp_cb_msg     *pMsg;
    ipos_port_id    pid;
    U32     cntRpt;
    U8      cmdTag;
    int     errCode;
    char    funcName[]="testSignals";

    if (pBuf==NULL)
    {
        printf("Invalid msg buffer in testSignals Callback \n");
        return;
    }

    /* parse the message	*/
    pMsg=(oamp_cb_msg *) pBuf->pDataBuf;

    pid     =pMsg->portId;
    cntRpt  =pMsg->cntRpt;
    cmdTag  =pMsg->uid;
    errCode =pMsg->status;

    if (pMsg->status==IPOS_SUCCESS)
    {
        uiLog(pid,funcName,cmdTag,OAMP_COMPLETE, errCode);
    } 
	 else
    {
		 uiLog(pid,funcName,cmdTag,OAMP_FAILED, errCode);
	 }

    iposMsgFree(pBuf);
}


int oampDetectSignal(ipos_port_id *pPid, ipos_test_signals * pTestSignals, U8 cmdTag, U32 sigType)
{
 int errCode=IPOS_SUCCESS;
 char    funcName[]="iposDetectTestSignals";
 int portState;

	/* get the port and BME state	*/
	if (oampGetBmeState(pPid->bmeNum)!=uRUN)
	{
		 errCode=OAMP_ERROR_BME_NOT_READY;

		 uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);

		 return(errCode);
	}

	/* get the port state	*/
	portState=oampGetPortState(*pPid);
	if ( (portState == uSHOWTIME) || (portState==uACTIVATING) )
	{
		 errCode=OAMP_ERROR_PORT_NOT_READY;

		 uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);

		 return(errCode);
	}


	/* issue the API call and print the status	*/
	errCode=iposDetectTestSignals (*pPid, pTestSignals, cmdTag, sigType, detectSignalsComplete);
	if (errCode!=IPOS_SUCCESS)
	{
		 uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);
		 return(errCode);
	}

	return(IPOS_SUCCESS);
}


void detectSignalsComplete(ipos_port_id portId, U8 UID, U32 sigType, int status,  void *par)
{
 int len= 0;

  	if ( par )
	{
		len = 4;
	}

	addCompletionJob(portId, UID, sigType, status, par, len, detectSignalsJob);
}



/* the complete function running in the oamp task	*/
static void detectSignalsJob(U32 arg1, U32 arg2, ipos_msg_buf *pBuf)
{
    oamp_cb_msg     *pMsg;
    ipos_port_id    pid;
    U32     cntRpt;
    U8      cmdTag;
    int     errCode;
    char    funcName[]="DetectSignals";

    if (pBuf==NULL)
    {
        printf("Invalid msg buffer in testSignals Callback \n");
        return;
    }

    /* parse the message	*/
    pMsg=(oamp_cb_msg *) pBuf->pDataBuf;

    pid     =pMsg->portId;
    cntRpt  =pMsg->cntRpt;
    cmdTag  =pMsg->uid;
    errCode =pMsg->status;

    if (pMsg->status==IPOS_SUCCESS)
    {
        if ( (pMsg->parLen == 4) && (*((int *)(pMsg->par))) )
		  {
			  rdm(pid.bmeNum, *((int *)(pMsg->par)), 0x4000);
		  }

        uiLog(pid,funcName,cmdTag,OAMP_COMPLETE, errCode);
    } 
	 else
    {
		 uiLog(pid,funcName,cmdTag,OAMP_FAILED, errCode);
	 }

    iposMsgFree(pBuf);
}



/*************************************************************
**
** oamp functions for port_start. Used by the shell	
**
*************************************************************/

int oampPortStart(ipos_port_id  *pPid, U8 cmdTag, U32 cntRpt)
{
	return (oampCmnPortStart(pPid, cmdTag, cntRpt, 0));
}


/* the complete functions should be short one: add job to oampTask1	*/

void portStartComplete(ipos_port_id portId, U8 UID,U32 cntRpt, int status,  void *par)
{
	int len;

  if (status==IPOS_SUCCESS)
	  len=sizeof(ipos_port_status);
  else
    len = 4;

    if (par != NULL)
        addCompletionJob(portId, UID, cntRpt, status, par, len, portStartJob);
    else
        addCompletionJob(portId, UID, cntRpt, status, NULL, 0, portStartJob);
}



/* the complete function running in the oamp task	*/
static  void    portStartJob(U32 arg1, U32 arg2, ipos_msg_buf   *pBuf)
{
//        printf("portStartJob() \n");
	cmnPortStartJob(arg1, arg2, pBuf, 0);
}

/*************************************************************
**
** oamp functions for port_stop. Used by the shell	
**
*************************************************************/

int oampPortStop(ipos_port_id   *pPid, U8 cmdTag, U32 cntRpt)
{


    int errCode=IPOS_SUCCESS;

#if defined(INI_SUPPORT)
//    int   PortNum;
	int  IniIndex, IniSlow, IniFast;
#endif

    char    funcName[]="iposPortStop";

    /* get the port and BME state	*/
    if (oampGetBmeState(pPid->bmeNum)!=uRUN)
    {

        errCode=OAMP_ERROR_BME_NOT_READY;

        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);

        return(errCode);

    }

/* get the port state	*/
#ifdef	NOTI
    if (oampGetPortState(*pPid)!=uSHOWTIME && oampGetPortState(*pPid)!=uACTIVATING )
    {
        errCode=OAMP_ERROR_PORT_NOT_READY;

        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);

        return(errCode);

    }
#endif

#if INI_SUPPORT
    // Stop INI
/* Start Deletion for Dual Latency - mrrk
    getLineNum(*pPid, &PortNum);

    iposPtmfStopTx ((U8)PortNum);
*/ //End Deletion for Dual Latency

	    getIniMap(*pPid, &IniIndex, &IniSlow, &IniFast);
        iposPtmfStopTx ((U8)(IniIndex + 1));

        if (IniFast != IniSlow)
        {
          iposPtmfStopTx ((U8)(IniIndex + 2));
        }
#endif //INI_SUPPORT

    /* issue the API call and print the status	*/

    errCode=iposPortStop(*pPid, cmdTag, cntRpt, portStopComplete);

    if (errCode!=IPOS_SUCCESS)
    {

        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);
        return(errCode);

    }

/* update teh port state	*/

    oampStateUpdate(*pPid, CMD_IN_PROGRESS);    
    uiLog(*pPid,funcName,cmdTag,OAMP_IN_PROGRESS, errCode);

    return(IPOS_SUCCESS);

}


/* the complete functions should be short one: add job to oampTask1	*/

void portStopComplete(ipos_port_id portId, U8 UID,U32 cntRpt, int status,  void *par)
{
    addCompletionJob(portId, UID, cntRpt, status, NULL,0,portStopJob);
}



/* the complete function running in the oamp task	*/
static  void    portStopJob(U32 arg1, U32 arg2, ipos_msg_buf    *pBuf)
{

    oamp_cb_msg     *pMsg;

    ipos_port_id    pid;

    U32     cntRpt;
    U8      cmdTag;
    int     errCode;
    char    funcName[]="iposPortStop";

#if defined(INI_SUPPORT)
//    int lineNum;
	int  IniIndex, IniSlow, IniFast;
#endif	

    if (pBuf==NULL)
    {
        printf("Invalid msg buffer in iposPortStop Callback \n");
        return;
    }

    /* parse the message	*/
    pMsg=(oamp_cb_msg *) pBuf->pDataBuf;

    pid     =pMsg->portId;
    cntRpt  =pMsg->cntRpt;
    cmdTag  =pMsg->uid;
    errCode =pMsg->status;

    /* update the state		*/
    if (pMsg->status==IPOS_SUCCESS)
    {

#ifdef	BME_CPE_20

        /* Clear LOS detection counters */

        oampBme[pid.bmeNum].oampPort[pid.portNum].portStatus.startCnt=0;
        oampBme[pid.bmeNum].oampPort[pid.portNum].portStatus.losFailure=0;
        oampBme[pid.bmeNum].oampPort[pid.portNum].portStatus.status |= uDOWN;
        oampBme[pid.bmeNum].oampPort[pid.portNum].Performance.NeFails.Los.fail_state=FAIL_INACTIVE;

#else
		/* Reset Performance Failure States */
		resetPerfPara(pid);
    oampBme[pid.bmeNum].oampPort[pid.portNum].portStatus.status |= uDOWN;
#endif

		clearBmeCounters(pid);

        UserLog ("Bme %i Port %i Deactivated",pid.bmeNum,pid.portNum);
        uiLog(pid,funcName,cmdTag,OAMP_COMPLETE, errCode);

        oampStateUpdate(pid,ePORT_STOP);

#if ALASKA
	vdsl_link_down();
#endif


#ifdef INI_SUPPORT

//        getLineNum(pid, &lineNum);

  //      iposPtmfStopRx ((U8)lineNum);
		getIniMap(pid, &IniIndex, &IniSlow, &IniFast);
        iposPtmfStopRx ((U8)(IniIndex + 1));
        if (IniFast != IniSlow)
        {
          iposPtmfStopRx ((U8)(IniIndex + 2));
        }

#endif //INI_SUPPORT

    } else
    {
        uiLog(pid,funcName,cmdTag,OAMP_FAILED, errCode);
        oampStateUpdate(pid,CMD_FAILED);  

        /* any repeat action add here	*/
        if (cntRpt!=0)
        {
            cntRpt--;
            errCode=iposPortStop(pid, cmdTag, cntRpt, portStopComplete);

            if (errCode!=IPOS_SUCCESS)
            {
                uiLog(pid,funcName,cmdTag,OAMP_FAILED, errCode);
                return;

            }

            uiLog(pid,funcName,cmdTag,OAMP_RESTART, errCode);
            oampStateUpdate(pid,CMD_IN_PROGRESS);

        }
    }


#if 0
	if (ProvRequired)
		{
			iposDelay(2000);
			oampStateUpdate(pid,ePORT_DESTROY);
			ProvRequired=0;
		}
#endif
    /* release the message	*/
    iposMsgFree(pBuf);

}


/*************************************************************
**
** oamp functions for port_destroy. Used by the shell	
**
*************************************************************/

int oampPortDestroy(ipos_port_id    *pPid, U8 cmdTag, U32 cntRpt)
{

    int errCode=IPOS_SUCCESS;
    char    funcName[]="iposPortDestroy";

    /* get the port and BME state	*/
    if (oampGetBmeState(pPid->bmeNum)!=uRUN)
    {

        errCode=OAMP_ERROR_BME_NOT_READY;

        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);

        return(errCode);

    }

/* get the port state	*/

#if 0
    if (oampGetPortState(*pPid)!=uENABLE)
    {
        errCode=OAMP_ERROR_PORT_NOT_READY;

        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);

        return(errCode);

    }
#endif

    /* issue the API call and print the status	*/

    errCode=iposPortDestroy(*pPid, cmdTag, cntRpt, portDestroyComplete);

    if (errCode!=IPOS_SUCCESS)
    {

        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);
        return(errCode);

    }

/* update teh port state	*/

    oampStateUpdate(*pPid, CMD_IN_PROGRESS);    
    uiLog(*pPid,funcName,cmdTag,OAMP_IN_PROGRESS, errCode);

    return(IPOS_SUCCESS);

}


/* the complete functions should be short one: add job to oampTask1	*/

void portDestroyComplete(ipos_port_id portId, U8 UID,U32 cntRpt, int status,  void *par)
{
    addCompletionJob(portId, UID, cntRpt, status, NULL,0,portDestroyJob);
}



/* the complete function running in the oamp task	*/
static  void    portDestroyJob(U32 arg1, U32 arg2, ipos_msg_buf *pBuf)
{

    oamp_cb_msg     *pMsg;

    ipos_port_id    pid;

    U32     cntRpt;
    U8      cmdTag;
    int     errCode;
    char    funcName[]="iposPortDestroy";


    if (pBuf==NULL)
    {
        printf("Invalid msg buffer in iposPortDestroy Callback \n");
        return;
    }

    /* parse the message	*/
    pMsg=(oamp_cb_msg *) pBuf->pDataBuf;

    pid     =pMsg->portId;
    cntRpt  =pMsg->cntRpt;
    cmdTag  =pMsg->uid;
    errCode =pMsg->status;

    /* update the state		*/
    if (pMsg->status==IPOS_SUCCESS)
    {

        UserLog ("Bme %i Port %i deactivated abd de-provisioned",pid.bmeNum,pid.portNum);
        uiLog(pid,funcName,cmdTag,OAMP_COMPLETE, errCode);
        oampStateUpdate(pid,ePORT_DESTROY);

    } else
    {
        uiLog(pid,funcName,cmdTag,OAMP_FAILED, errCode);
        oampStateUpdate(pid,CMD_FAILED);  

        /* any repeat action add here	*/
        if (cntRpt!=0)
        {
            cntRpt--;
            errCode=iposPortDestroy(pid, cmdTag, cntRpt, portDestroyComplete);

            if (errCode!=IPOS_SUCCESS)
            {
                uiLog(pid,funcName,cmdTag,OAMP_FAILED, errCode);
                return;

            }

            uiLog(pid,funcName,cmdTag,OAMP_RESTART, errCode);
            oampStateUpdate(pid,CMD_IN_PROGRESS);

        }
    }

    /* release the message	*/
    iposMsgFree(pBuf);

}
/*************************************************************
**
** oamp functions for port_reset. Used by the shell	
**
*************************************************************/

int oampPortReset(ipos_port_id  *pPid, U8 resetParm, U8 cmdTag, U32 cntRpt)
{

    int errCode=IPOS_SUCCESS;
    char    funcName[]="iposPortReset";

    /* get the port and BME state	*/
    if (oampGetBmeState(pPid->bmeNum)!=uRUN)
    {

        errCode=OAMP_ERROR_BME_NOT_READY;

        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);

        return(errCode);

    }

#if 0

/* get the port state	*/

    if (oampGetPortState(*pPid)!=uENABLE)
    {
        errCode=OAMP_ERROR_PORT_NOT_READY;

        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);

        return(errCode);

    }
#endif

    /* issue the API call and print the status	*/

    errCode=iposPortReset(*pPid, resetParm, cmdTag, cntRpt, portResetComplete);

    if (errCode!=IPOS_SUCCESS)
    {

        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);
        return(errCode);

    }

/* update teh port state	*/

    oampStateUpdate(*pPid, CMD_IN_PROGRESS);    
    uiLog(*pPid,funcName,cmdTag,OAMP_IN_PROGRESS, errCode);

    return(IPOS_SUCCESS);

}


/* the complete functions should be short one: add job to oampTask1	*/

void portResetComplete(ipos_port_id portId, U8 UID,U32 cntRpt, int status,  void *par)
{
    addCompletionJob(portId, UID, cntRpt, status, NULL,0, portResetJob);
}



/* the complete function running in the oamp task	*/
static  void    portResetJob(U32 arg1, U32 arg2, ipos_msg_buf   *pBuf)
{

    oamp_cb_msg     *pMsg;

    ipos_port_id    pid;

    U32     cntRpt;
    U8      cmdTag;
    int     errCode;
    char    funcName[]="iposPortReset";


    if (pBuf==NULL)
    {
        printf("Invalid msg buffer in iposPortReset Callback \n");
        return;
    }

    /* parse the message	*/
    pMsg=(oamp_cb_msg *) pBuf->pDataBuf;

    pid     =pMsg->portId;
    cntRpt  =pMsg->cntRpt;
    cmdTag  =pMsg->uid;
    errCode =pMsg->status;

    /* update the state		*/
    if (pMsg->status==IPOS_SUCCESS)
    {

        uiLog(pid,funcName,cmdTag,OAMP_COMPLETE, errCode);
        oampStateUpdate(pid,ePORT_RESET);

    } else
    {
        uiLog(pid,funcName,cmdTag,OAMP_FAILED, errCode);
        oampStateUpdate(pid,CMD_FAILED);  

        /* any repeat action add here	*/
#if 0
        if (cntRpt!=0)
        {
            cntRpt--;
            errCode=iposPortReset(pid, cmdTag, cntRpt, portResetComplete);

            if (errCode!=IPOS_SUCCESS)
            {
                uiLog(pid,funcName,cmdTag,OAMP_FAILED, errCode);
                return;

            }

            uiLog(pid,funcName,cmdTag,OAMP_RESTART, errCode);
            oampStateUpdate(pid,CMD_IN_PROGRESS);

        }
#endif

    }

    /* release the message	*/
    iposMsgFree(pBuf);

}


/*************************************************************
**
** oamp functions for iposPeriodicReportEnable. Used by the shell	
**
*************************************************************/

int oampPeriodicReportsEnable(ipos_port_id  *pPid, ipos_report_config *pReport, U8 cmdTag, U32 cntRpt)
{
    int errCode=IPOS_SUCCESS;
    char    funcName[]="iposPeriodicReportEnable";

    /* get the port and BME state	*/
    if (oampGetBmeState(pPid->bmeNum)==uDOWN)
    {
        errCode=OAMP_ERROR_BME_NOT_READY;
        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);
        return(errCode);
    }

    /* issue the API call and print the status	*/

    errCode=iposPeriodicReportsEnable(*pPid, pReport, cmdTag, cntRpt, pReportComplete);

    if (errCode!=IPOS_SUCCESS)
    {
        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);
        return(errCode);
    }

/* update teh port state	*/

    oampBmeStateUpdate(pPid->bmeNum, CMD_IN_PROGRESS);  
    uiLog(*pPid,funcName,cmdTag,OAMP_IN_PROGRESS, errCode);

    return(IPOS_SUCCESS);

}

/* the complete functions should be short one: add job to oampTask1	*/

void pReportComplete(ipos_port_id portId, U8 UID,U32 cntRpt, int status,  void *par)
{
    addCompletionJob(portId, UID, cntRpt, status, NULL,0, pReportJob);
}



/* the complete function running in the oamp task	*/
static  void    pReportJob(U32 arg1, U32 arg2, ipos_msg_buf *pBuf)
{

    oamp_cb_msg     *pMsg;
    ipos_port_id    pid;

    char 	    getdver[ 16 ];

    U32     cntRpt;
    U8      cmdTag;
    int     errCode;
    char    funcName[]="iposPeriodicReportEnable";


    if (pBuf==NULL)
    {
        printf("Invalid msg buffer in iposPortReset Callback \n");
        return;
    }

    /* parse the message	*/
    pMsg=(oamp_cb_msg *) pBuf->pDataBuf;

    pid     =pMsg->portId;
    cntRpt  =pMsg->cntRpt;
    cmdTag  =pMsg->uid;
    errCode =pMsg->status;

    /* update the state		*/
    if (pMsg->status==IPOS_SUCCESS)
    {

        uiLog(pid,funcName,cmdTag,OAMP_COMPLETE, errCode);
        oampBmeStateUpdate(pid.bmeNum, ePORT_PPREPORT_ENABLE);  

	      sprintf( &getdver[ 0 ], "getdver::%d;", pid.bmeNum );
        ReceiveTLCommand( &getdver[ 0 ] );


    } else
    {
        uiLog(pid,funcName,cmdTag,OAMP_FAILED, errCode);
        oampBmeStateUpdate(pid.bmeNum, CMD_FAILED); 

    }

    /* release the message	*/
    iposMsgFree(pBuf);

}

/* oamp functions for iposEocProtocolSet. Used by the shell */	

int oampEocProtocolSet(ipos_port_id *pPid, U8 opCode, U8 *regData, U8 cmdTag, U32 cntRpt)
{
    int errCode=IPOS_SUCCESS;
    char funcName[]="iposEocProtocolSet";

    /* get the port and BME state	*/
    if (oampGetBmeState(pPid->bmeNum)!=uRUN) {
        errCode=OAMP_ERROR_BME_NOT_READY;
        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);
        return(errCode);
    }
    /* printf("%s: opCode = 0x%x, cmdTag = 0x%x, cntRpt = 0x%x\n", funcName, opCode, cmdTag, cntRpt);
     */
    /* issue the API call and print the status	*/
    errCode=iposEocProtocolSet(*pPid, opCode, regData, cmdTag, cntRpt, eocProtocolSetComplete);

    if (errCode!=IPOS_SUCCESS) {
        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);
        return(errCode);
    }

    /* update the port state	*/
    oampStateUpdate(*pPid, CMD_IN_PROGRESS);    
    uiLog(*pPid,funcName,cmdTag,OAMP_IN_PROGRESS, errCode);
    return(IPOS_SUCCESS);
}

/* the complete function for EocProtocolSet	*/

void eocProtocolSetComplete(ipos_port_id portId, U8 UID,U32 cntRpt, int status,  void *par)
{
    addCompletionJob(portId, UID, cntRpt, status, NULL,0,eocProtocolSetJob);
}

/* the complete function running in the oamp task	*/
static  void    eocProtocolSetJob(U32 arg1, U32 arg2, ipos_msg_buf  *pBuf)
{
    oamp_cb_msg     *pMsg;
    ipos_port_id    pid;
    U32 cntRpt;
    U8  cmdTag;
    int errCode;
    char funcName[]="iposEocProtocolSet";

    if (pBuf==NULL) {
        printf("Invalid msg buffer in iposEocSetData Callback \n");
        return;
    }

    /* parse the message	*/
    pMsg=(oamp_cb_msg *) pBuf->pDataBuf;
    pid     =pMsg->portId;
    cntRpt  =pMsg->cntRpt;
    cmdTag  =pMsg->uid;
    errCode =pMsg->status;

    /* update the state		*/
    if (pMsg->status==IPOS_SUCCESS) {
        uiLog(pid,funcName,cmdTag,OAMP_COMPLETE, errCode);
        oampStateUpdate(pid,eEOC_EVNT);
    } else {
        uiLog(pid,funcName,cmdTag,OAMP_FAILED, errCode);
        oampStateUpdate(pid,CMD_FAILED);
    }
    
		/* release the message	*/
    iposMsgFree(pBuf);
}

/* oamp functions for iposEocGetData. Used by the shell	*/

int oampEocProtocolGet(ipos_port_id *pPid, U8 opCode, U8 cmdTag, U32 cntRpt)
{
    int errCode=IPOS_SUCCESS;
    char    funcName[]="iposEocProtocolGet";

    /* get the port and BME state	*/
    if (oampGetBmeState(pPid->bmeNum)!=uRUN) {
        errCode=OAMP_ERROR_BME_NOT_READY;
        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);
        return(errCode);
    }
    /* issue the API call and print the status	*/
    /* printf("%s: opCode = 0x%x, cmdTag = 0x%x, cntRpt = 0x%x\n", funcName, opCode, cmdTag, cntRpt);
     */
    errCode=iposEocProtocolGet(*pPid, opCode, cmdTag, cntRpt, eocProtocolGetComplete);

    if (errCode!=IPOS_SUCCESS) {
        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);
        return(errCode);
    }

    /* update the port state	*/
    oampStateUpdate(*pPid, CMD_IN_PROGRESS);    
    uiLog(*pPid,funcName,cmdTag,OAMP_IN_PROGRESS, errCode);
    return(IPOS_SUCCESS);
}

/* the complete function for EocProtocolGet	*/

void eocProtocolGetComplete(ipos_port_id portId, U8 UID,U32 cntRpt, int status,  void *par)
{
	  char *x = par;
    addCompletionJob(portId, UID, cntRpt, status, (x + 1), *(x) ,eocProtocolGetJob);
}

/* the complete function running in the oamp task	*/
static  void    eocProtocolGetJob(U32 arg1, U32 arg2, ipos_msg_buf  *pBuf)
{
    oamp_cb_msg     *pMsg;
    ipos_port_id    pid;
    U32     cntRpt;
    U8      cmdTag;
    int     errCode;
		//int count = 0;
    char    funcName[]="iposEocProtocolGet";

    if (pBuf==NULL) {
        printf("Invalid msg buffer in iposEocProtocolGet Callback \n");
        return;
    }

    /* parse the message	*/
    pMsg=(oamp_cb_msg *) pBuf->pDataBuf;
    pid     =pMsg->portId;
    cntRpt  =pMsg->cntRpt;
    cmdTag  =pMsg->uid;
    errCode =pMsg->status;

    /* update the state		*/
    if (pMsg->status==IPOS_SUCCESS) {
        uiLog(pid,funcName,cmdTag,OAMP_COMPLETE, errCode);
        oampStateUpdate(pid,eEOC_EVNT);
		    parse_internalEoc_Data(pid, pMsg);;
    } else {
        uiLog(pid,funcName,cmdTag,OAMP_FAILED, errCode);
        oampStateUpdate(pid,CMD_FAILED);
    }

    /* release the message	*/
    iposMsgFree(pBuf);
}

/*************************************************************
**
** oamp functions for iposEocSetData. Used by the shell	
**
*************************************************************/

int oampEocSetData(ipos_port_id *pPid, U8 opCode, U8 *regData, U8 cmdTag, U32 cntRpt)
{

    int errCode=IPOS_SUCCESS;
    char    funcName[]="iposEocSetData";

    /* get the port and BME state	*/
    if (oampGetBmeState(pPid->bmeNum)!=uRUN)
    {

        errCode=OAMP_ERROR_BME_NOT_READY;

        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);

        return(errCode);

    }

/* get the port state	*/

    if (oampGetPortState(*pPid)!=uSHOWTIME)
    {
        errCode=OAMP_ERROR_PORT_NOT_READY;

        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);

        return(errCode);

    }

    /* issue the API call and print the status	*/

    errCode=iposEocSetData(*pPid, opCode, regData, cmdTag, cntRpt, eocSetDataComplete);

    if (errCode!=IPOS_SUCCESS)
    {

        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);
        return(errCode);

    }

/* update teh port state	*/

    oampStateUpdate(*pPid, CMD_IN_PROGRESS);    
    uiLog(*pPid,funcName,cmdTag,OAMP_IN_PROGRESS, errCode);

    return(IPOS_SUCCESS);

}


/* the complete functions should be short one: add job to oampTask1	*/

void eocSetDataComplete(ipos_port_id portId, U8 UID,U32 cntRpt, int status,  void *par)
{
    addCompletionJob(portId, UID, cntRpt, status, NULL,0,eocSetDataJob);
}



/* the complete function running in the oamp task	*/
static  void    eocSetDataJob(U32 arg1, U32 arg2, ipos_msg_buf  *pBuf)
{

    oamp_cb_msg     *pMsg;

    ipos_port_id    pid;

    U32     cntRpt;
    U8      cmdTag;
    int     errCode;
    char    funcName[]="iposEocSetData";


    if (pBuf==NULL)
    {
        printf("Invalid msg buffer in iposEocSetData Callback \n");
        return;
    }

    /* parse the message	*/
    pMsg=(oamp_cb_msg *) pBuf->pDataBuf;

    pid     =pMsg->portId;
    cntRpt  =pMsg->cntRpt;
    cmdTag  =pMsg->uid;
    errCode =pMsg->status;

    /* update the state		*/
    if (pMsg->status==IPOS_SUCCESS)
    {

        uiLog(pid,funcName,cmdTag,OAMP_COMPLETE, errCode);
        oampStateUpdate(pid,eEOC_EVNT);

    } else
    {
        uiLog(pid,funcName,cmdTag,OAMP_FAILED, errCode);
        oampStateUpdate(pid,CMD_FAILED);

#if 0
  
        /* any repeat action add here	*/
        if (cntRpt!=0)
        {
            cntRpt--;
            errCode=iposEocSetData(pid, cmdTag, cntRpt, eocSetDataComplete);

            if (errCode!=IPOS_SUCCESS)
            {
                uiLog(pid,funcName,cmdTag,OAMP_FAILED, errCode);
                return;

            }

            uiLog(pid,funcName,cmdTag,OAMP_RESTART, errCode);
            oampStateUpdate(pid,CMD_IN_PROGRESS);

        }
#endif
    }

    /* release the message	*/
    iposMsgFree(pBuf);

}



/*************************************************************
**
** oamp functions for iposBitSwapReq. Used by the shell	
**
*************************************************************/

int oampBitSwapReq(ipos_port_id *pPid, U8 opCode, U8 *msg, U8 cmdTag, U32 cntRpt)
{

    int errCode=IPOS_SUCCESS;
    char    funcName[]="iposBitSwapReq";

    /* get the port and BME state	*/
    if (oampGetBmeState(pPid->bmeNum)!=uRUN)
    {

        errCode=OAMP_ERROR_BME_NOT_READY;

        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);

        return(errCode);

    }

/* get the port state	*/

    if (oampGetPortState(*pPid)!=uSHOWTIME)
    {
        errCode=OAMP_ERROR_PORT_NOT_READY;

        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);

        return(errCode);

    }

    /* issue the API call and print the status	*/

    errCode=iposBitSwapReq(*pPid, opCode, msg, cmdTag, cntRpt, bitSwapReqComplete);

    if (errCode!=IPOS_SUCCESS)
    {

        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);
        return(errCode);

    }

/* update the port state	*/

    oampStateUpdate(*pPid, CMD_IN_PROGRESS);    
    uiLog(*pPid,funcName,cmdTag,OAMP_IN_PROGRESS, errCode);

    return(IPOS_SUCCESS);

}


/* the complete functions should be short one: add job to oampTask1	*/

void bitSwapReqComplete(ipos_port_id portId, U8 UID,U32 cntRpt, int status,  void *par)
{
    addCompletionJob(portId, UID, cntRpt, status, NULL,0,bitSwapReqJob);
}



/* the complete function running in the oamp task	*/
static  void    bitSwapReqJob(U32 arg1, U32 arg2, ipos_msg_buf  *pBuf)
{

    oamp_cb_msg     *pMsg;

    ipos_port_id    pid;

    U32     cntRpt;
    U8      cmdTag;
    int     errCode;
    char    funcName[]="iposBitSwapReq";


    if (pBuf==NULL)
    {
        printf("Invalid msg buffer in iposBitSwapReq Callback \n");
        return;
    }

    /* parse the message	*/
    pMsg=(oamp_cb_msg *) pBuf->pDataBuf;

    pid     =pMsg->portId;
    cntRpt  =pMsg->cntRpt;
    cmdTag  =pMsg->uid;
    errCode =pMsg->status;

    /* update the state		*/
    if (pMsg->status==IPOS_SUCCESS)
    {

        uiLog(pid,funcName,cmdTag,OAMP_COMPLETE, errCode);
        oampStateUpdate(pid,eBIT_SWAP_EVNT);

    } else
    {
        uiLog(pid,funcName,cmdTag,OAMP_FAILED, errCode);
        oampStateUpdate(pid,CMD_FAILED);

#if 0
  
        /* any repeat action add here	*/
        if (cntRpt!=0)
        {
            cntRpt--;
            errCode=iposBitSwapData(pid, cmdTag, cntRpt, bitSwapReqComplete);

            if (errCode!=IPOS_SUCCESS)
            {
                uiLog(pid,funcName,cmdTag,OAMP_FAILED, errCode);
                return;

            }

            uiLog(pid,funcName,cmdTag,OAMP_RESTART, errCode);
            oampStateUpdate(pid,CMD_IN_PROGRESS);

        }
#endif
    }

    /* release the message	*/
    iposMsgFree(pBuf);

}



/*************************************************************
**
** oamp functions for iposEocGetData. Used by the shell	
**
*************************************************************/

int oampEocGetData(ipos_port_id *pPid, U8 opCode, U8 cmdTag, U32 cntRpt)
{

    int errCode=IPOS_SUCCESS;
    char    funcName[]="iposEocGetData";

    /* get the port and BME state	*/
    if (oampGetBmeState(pPid->bmeNum)!=uRUN)
    {

        errCode=OAMP_ERROR_BME_NOT_READY;

        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);

        return(errCode);

    }

/* get the port state	*/

    if (oampGetPortState(*pPid)!=uSHOWTIME)
    {
        errCode=OAMP_ERROR_PORT_NOT_READY;

        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);

        return(errCode);

    }

    /* issue the API call and print the status	*/
    /*printf("%s: opCode = 0x%x, cmdTag = 0x%x, cntRpt = 0x%x\n", funcName, opCode, cmdTag, cntRpt);*/

    errCode=iposEocGetData(*pPid, opCode, cmdTag, cntRpt, eocGetDataComplete);

    if (errCode!=IPOS_SUCCESS)
    {

        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);
        return(errCode);

    }

/* update teh port state	*/

    oampStateUpdate(*pPid, CMD_IN_PROGRESS);    
    uiLog(*pPid,funcName,cmdTag,OAMP_IN_PROGRESS, errCode);

    return(IPOS_SUCCESS);

}


/* the complete functions should be short one: add job to oampTask1	*/

void eocGetDataComplete(ipos_port_id portId, U8 UID,U32 cntRpt, int status,  void *par)
{
	  /* This variable is only used for typecasting and the values are not
		 * altered in any way. */
	  char *data = par;
    addCompletionJob(portId, UID, cntRpt, status, (data + 1), *data ,eocGetDataJob);
}

/* the complete function running in the oamp task	*/
static  void    eocGetDataJob(U32 arg1, U32 arg2, ipos_msg_buf  *pBuf)
{

    oamp_cb_msg     *pMsg;

    ipos_port_id    pid;

    U32     cntRpt;
    U8      cmdTag;
    int     errCode/*, count = 0*/;
    char    funcName[]="iposEocGetData";


    if (pBuf==NULL)
    {
        printf("Invalid msg buffer in iposEocGetData Callback \n");
        return;
    }

    /* parse the message	*/
    pMsg=(oamp_cb_msg *) pBuf->pDataBuf;

    pid     =pMsg->portId;
    cntRpt  =pMsg->cntRpt;
    cmdTag  =pMsg->uid;
    errCode =pMsg->status;

#if 0
		for (count = 0; count < pMsg->parLen; count++)
 		    printf("Data received from the other end is %x \n",pMsg->par[count]);
#endif
    /* update the state		*/
    if (pMsg->status==IPOS_SUCCESS)
    {

        uiLog(pid,funcName,cmdTag,OAMP_COMPLETE, errCode);
        oampStateUpdate(pid,eEOC_EVNT);
		    parse_internalEoc_Data(pid, pMsg);;

    } else
    {
        uiLog(pid,funcName,cmdTag,OAMP_FAILED, errCode);
        oampStateUpdate(pid,CMD_FAILED);

#if 0
  
        /* any repeat action add here	*/
        if (cntRpt!=0)
        {
            cntRpt--;
            errCode=iposEocGetData(pid, cmdTag, cntRpt, eocGetDataComplete);

            if (errCode!=IPOS_SUCCESS)
            {
                uiLog(pid,funcName,cmdTag,OAMP_FAILED, errCode);
                return;

            }

            uiLog(pid,funcName,cmdTag,OAMP_RESTART, errCode);
            oampStateUpdate(pid,CMD_IN_PROGRESS);

        }
#endif
    }

    /* release the message	*/
    iposMsgFree(pBuf);

}





/*************************************************************
**
** oamp functions for iposEocControlSend. Used by the shell	
**
*************************************************************/

int oampEocControlSend(ipos_port_id *pPid,U8 opCode, U8 cmdTag, U32 cntRpt)
{

    int errCode=IPOS_SUCCESS;
    char    funcName[]="iposEocControlSend";

    /* get the port and BME state	*/
    if (oampGetBmeState(pPid->bmeNum)!=uRUN)
    {

        errCode=OAMP_ERROR_BME_NOT_READY;

        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);

        return(errCode);

    }

/* get the port state	*/

    if (oampGetPortState(*pPid)!=uSHOWTIME)
    {
        errCode=OAMP_ERROR_PORT_NOT_READY;

        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);

        return(errCode);

    }

    /* issue the API call and print the status	*/
    printf("%s: opCode = 0x%x, cmdTag = 0x%x, cntRpt = 0lx%lx\n", funcName, opCode, cmdTag, (cntRpt));

    errCode=iposEocControlSend(*pPid, opCode, cmdTag, cntRpt, eocControlSendComplete);

    if (errCode!=IPOS_SUCCESS)
    {

        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);
        return(errCode);

    }

/* update teh port state	*/

    oampStateUpdate(*pPid, CMD_IN_PROGRESS);    
    uiLog(*pPid,funcName,cmdTag,OAMP_IN_PROGRESS, errCode);

    return(IPOS_SUCCESS);

}


/* the complete functions should be short one: add job to oampTask1	*/

void eocControlSendComplete(ipos_port_id portId, U8 UID,U32 cntRpt, int status,  void *par)
{
    addCompletionJob(portId, UID, cntRpt, status, NULL,0,eocControlSendJob);
}



/* the complete function running in the oamp task	*/
static  void    eocControlSendJob(U32 arg1, U32 arg2, ipos_msg_buf  *pBuf)
{

    oamp_cb_msg     *pMsg;

    ipos_port_id    pid;

    U32     cntRpt;
    U8      cmdTag;
    int     errCode;
    char    funcName[]="iposEocControlSend";


    if (pBuf==NULL)
    {
        printf("Invalid msg buffer in iposEocControlSend Callback \n");
        return;
    }

    /* parse the message	*/
    pMsg=(oamp_cb_msg *) pBuf->pDataBuf;

    pid     =pMsg->portId;
    cntRpt  =pMsg->cntRpt;
    cmdTag  =pMsg->uid;
    errCode =pMsg->status;

    /* update the state		*/
    if (pMsg->status==IPOS_SUCCESS)
    {

        uiLog(pid,funcName,cmdTag,OAMP_COMPLETE, errCode);
        oampStateUpdate(pid,eEOC_EVNT);

    } else
    {
        uiLog(pid,funcName,cmdTag,OAMP_FAILED, errCode);
        oampStateUpdate(pid,CMD_FAILED);

#if 0
  
        /* any repeat action add here	*/
        if (cntRpt!=0)
        {
            cntRpt--;
            errCode=iposEocControlSend(pid, cmdTag, cntRpt, eocControlSendComplete);

            if (errCode!=IPOS_SUCCESS)
            {
                uiLog(pid,funcName,cmdTag,OAMP_FAILED, errCode);
                return;

            }

            uiLog(pid,funcName,cmdTag,OAMP_RESTART, errCode);
            oampStateUpdate(pid,CMD_IN_PROGRESS);

        }
#endif
    }

    /* release the message	*/
    iposMsgFree(pBuf);

}


/*************************************************************
**
** oamp functions for iposEocDataReply. Used by the shell	
**
*************************************************************/

int oampEocDataReply(ipos_port_id   *pPid,U8 opCode, U8 len, U8 *regData, U8 cmdTag, U32 cntRpt)
{

    int errCode=IPOS_SUCCESS;
    char    funcName[]="iposEocDataReply";

    /* get the port and BME state	*/
    if (oampGetBmeState(pPid->bmeNum)!=uRUN)
    {

        errCode=OAMP_ERROR_BME_NOT_READY;

        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);

        return(errCode);

    }

/* get the port state	*/

    if (oampGetPortState(*pPid)!=uSHOWTIME)
    {
        errCode=OAMP_ERROR_PORT_NOT_READY;

        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);

        return(errCode);

    }

    /* issue the API call and print the status	*/

    errCode=iposEocDataReply(*pPid, opCode,len,regData, cmdTag, cntRpt, eocDataReplyComplete);

    if (errCode!=IPOS_SUCCESS)
    {

        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);
        return(errCode);

    }

/* update teh port state	*/

    oampStateUpdate(*pPid, CMD_IN_PROGRESS);    
    uiLog(*pPid,funcName,cmdTag,OAMP_IN_PROGRESS, errCode);

    return(IPOS_SUCCESS);

}


/* the complete functions should be short one: add job to oampTask1	*/

void eocDataReplyComplete(ipos_port_id portId, U8 UID,U32 cntRpt, int status,  void *par)
{
    addCompletionJob(portId, UID, cntRpt, status, NULL,0,eocDataReplyJob);
}



/* the complete function running in the oamp task	*/
static  void    eocDataReplyJob(U32 arg1, U32 arg2, ipos_msg_buf    *pBuf)
{

    oamp_cb_msg     *pMsg;

    ipos_port_id    pid;

    U32     cntRpt;
    U8      cmdTag;
    int     errCode;
    char    funcName[]="iposEocDataReply";


    if (pBuf==NULL)
    {
        printf("Invalid msg buffer in iposEocDataReply Callback \n");
        return;
    }

    /* parse the message	*/
    pMsg=(oamp_cb_msg *) pBuf->pDataBuf;

    pid     =pMsg->portId;
    cntRpt  =pMsg->cntRpt;
    cmdTag  =pMsg->uid;
    errCode =pMsg->status;

    /* update the state		*/
    if (pMsg->status==IPOS_SUCCESS)
    {

        uiLog(pid,funcName,cmdTag,OAMP_COMPLETE, errCode);
        oampStateUpdate(pid,eEOC_EVNT);

    } else
    {
        uiLog(pid,funcName,cmdTag,OAMP_FAILED, errCode);
        oampStateUpdate(pid,CMD_FAILED);

#if 0
  
        /* any repeat action add here	*/
        if (cntRpt!=0)
        {
            cntRpt--;
            errCode=iposEocDataReply(pid, cmdTag, cntRpt, eocDataReplyComplete);

            if (errCode!=IPOS_SUCCESS)
            {
                uiLog(pid,funcName,cmdTag,OAMP_FAILED, errCode);
                return;

            }

            uiLog(pid,funcName,cmdTag,OAMP_RESTART, errCode);
            oampStateUpdate(pid,CMD_IN_PROGRESS);

        }
#endif
    }

    /* release the message	*/
    iposMsgFree(pBuf);

}




/*************************************************************
**
** oamp functions for iposEocSendDataStream. Used by the shell	
**
*************************************************************/

int oampEocSendDataStream(ipos_port_id  *pPid, U8 len, U8 *regData, U8 cmdTag, U32 cntRpt)
{

    int errCode=IPOS_SUCCESS;
    char    funcName[]="iposEocSendDataStream";

    /* get the port and BME state	*/
    if (oampGetBmeState(pPid->bmeNum)!=uRUN)
    {

        errCode=OAMP_ERROR_BME_NOT_READY;

        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);

        return(errCode);

    }

#ifdef	NOTI
/* get the port state	*/

    if (oampGetPortState(*pPid)!=uSHOWTIME)
    {
        errCode=OAMP_ERROR_PORT_NOT_READY;

        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);

        return(errCode);

    }

#endif
    /* issue the API call and print the status	*/

    errCode=iposEocSendDataStream(*pPid,len,regData, cmdTag, cntRpt, eocSendDataStreamComplete);

    if (errCode!=IPOS_SUCCESS)
    {

        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);
        return(errCode);

    }

/* update teh port state	*/

    oampStateUpdate(*pPid, CMD_IN_PROGRESS);    
    uiLog(*pPid,funcName,cmdTag,OAMP_IN_PROGRESS, errCode);

    return(IPOS_SUCCESS);

}


/* the complete functions should be short one: add job to oampTask1	*/

void eocSendDataStreamComplete(ipos_port_id portId, U8 UID,U32 cntRpt, int status,  void *par)
{
    addCompletionJob(portId, UID, cntRpt, status, NULL,0, eocSendDataStreamJob);
}



/* the complete function running in the oamp task	*/
static  void    eocSendDataStreamJob(U32 arg1, U32 arg2, ipos_msg_buf   *pBuf)
{

    oamp_cb_msg     *pMsg;

    ipos_port_id    pid;

    U32     cntRpt;
    U8      cmdTag;
    int     errCode;
    char    funcName[]="iposEocSendDataStream";


    if (pBuf==NULL)
    {
        printf("Invalid msg buffer in iposEocSendDataStream Callback \n");
        return;
    }

    /* parse the message	*/
    pMsg=(oamp_cb_msg *) pBuf->pDataBuf;

    pid     =pMsg->portId;
    cntRpt  =pMsg->cntRpt;
    cmdTag  =pMsg->uid;
    errCode =pMsg->status;

    /* update the state		*/
    if (pMsg->status==IPOS_SUCCESS)
    {

        uiLog(pid,funcName,cmdTag,OAMP_COMPLETE, errCode);
        oampStateUpdate(pid,eEOC_EVNT);

    } else
    {
        uiLog(pid,funcName,cmdTag,OAMP_FAILED, errCode);
        oampStateUpdate(pid,CMD_FAILED);

    }

    /* release the message	*/
    iposMsgFree(pBuf);

}


/*************************************************************
**
** oamp functions for iposPortGetStatusBrief. Used by the shell	
**
*************************************************************/

int oampPortGetStatusBrief(ipos_port_id *pPid, U8 cmdTag, U32 cntRpt)
{
    int errCode=IPOS_SUCCESS;
    char    funcName[]="iposPortGetStatusBrief";

    /* get the port and BME state	*/
    if (oampGetBmeState(pPid->bmeNum)!=uRUN)
    {
        errCode=OAMP_ERROR_BME_NOT_READY;
        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);
        return(errCode);
    }

/* get the port state	*/
#if	0
    if (oampGetPortState(*pPid)!=uENABLE)
    {
        errCode=OAMP_ERROR_PORT_NOT_READY;
        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);
        return(errCode);

    }
#endif

    /* issue the API call and print the status	*/

    errCode=iposPortStatusBrief(*pPid, cmdTag, cntRpt, portStatusBriefComplete);

    if (errCode!=IPOS_SUCCESS)
    {
        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);
        return(errCode);
    }

/* update teh port state	*/

    oampStateUpdate(*pPid, CMD_IN_PROGRESS);    
    uiLog(*pPid,funcName,cmdTag,OAMP_IN_PROGRESS, errCode);

    return(IPOS_SUCCESS);

}

/* the complete functions should be short one: add job to oampTask1	*/

void portStatusBriefComplete(ipos_port_id portId, U8 UID,U32 cntRpt, int status,  
                             ipos_port_status_brief * par)
{
    int len;
    len=sizeof(ipos_port_status_brief);
    addCompletionJob(portId, UID, cntRpt, status, (U8 *) par, len,portStatusBriefJob);
}



/* the complete function running in the oamp task	*/
static  void    portStatusBriefJob(U32 arg1, U32 arg2, ipos_msg_buf *pBuf)
{

    oamp_cb_msg     *pMsg;
    ipos_port_id    pid;
    ipos_port_status_brief  *pStatus;

    U32     cntRpt;
    U8      cmdTag;
    int     errCode;
    char    funcName[]="iposPortStatusBrief";


    if (pBuf==NULL)
    {
        printf("Invalid msg buffer in iposPortStatusBrief Callback \n");
        return;
    }

    /* parse the message	*/
    pMsg=(oamp_cb_msg *) pBuf->pDataBuf;

    pid     =pMsg->portId;
    cntRpt  =pMsg->cntRpt;
    cmdTag  =pMsg->uid;
    errCode =pMsg->status;

    pStatus=(ipos_port_status_brief *) pMsg->par;

    /* update the state		*/
    if (pMsg->status==IPOS_SUCCESS)
    {

        uiLog(pid,funcName,cmdTag,OAMP_COMPLETE, errCode);
        oampStateUpdate(pid,ePORT_STATUS_BRIEF);
        uiLogPortStatusBrief(pid,pStatus);  

    } else
    {
        uiLog(pid,funcName,cmdTag,OAMP_FAILED, errCode);
        oampStateUpdate(pid,CMD_FAILED);
    }

    /* release the message	*/
    iposMsgFree(pBuf);

}



/*************************************************************
**
** oamp functions for iposPortGetStatus Used by the shell	
**
*************************************************************/

int oampPortGetStatus(ipos_port_id  *pPid, U8 cmdTag, U32 cntRpt)
{
    int errCode=IPOS_SUCCESS;
    char    funcName[]="iposPortGetStatus";

    /* get the port and BME state	*/
    if (oampGetBmeState(pPid->bmeNum)!=uRUN)
    {
        errCode=OAMP_ERROR_BME_NOT_READY;
        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);
        return(errCode);
    }


#if	0
/* get the port state	*/

    if (oampGetPortState(*pPid)!=uENABLE)
    {
        errCode=OAMP_ERROR_PORT_NOT_READY;
        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);
        return(errCode);

    }

#endif

    /* issue the API call and print the status	*/

    errCode=iposPortStatusExtended(*pPid, cmdTag, cntRpt, portStatusComplete);

    if (errCode!=IPOS_SUCCESS)
    {
        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);
        return(errCode);
    }

/* update teh port state	*/

    oampStateUpdate(*pPid, CMD_IN_PROGRESS);    
    uiLog(*pPid,funcName,cmdTag,OAMP_IN_PROGRESS, errCode);

    return(IPOS_SUCCESS);

}
#ifdef ALASKA
int oampPortGetLinkRates(ipos_port_id  *pPid, U8 cmdTag, U32 cntRpt)
{
    int errCode=IPOS_SUCCESS;
    char    funcName[]="iposPortGetStatus";
        /* get the port and BME state       */
    if (oampGetBmeState(pPid->bmeNum)!=uRUN)
    {
        errCode=OAMP_ERROR_BME_NOT_READY;
        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);
        return(errCode);
    }


    /* issue the API call and Get the Link Rates  */

    errCode=iposPortStatusExtended(*pPid, cmdTag, cntRpt, portLinkRatesComplete);

    if (errCode!=IPOS_SUCCESS)
    {
        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);
        return(errCode);
    }

/* update teh port state        */

    oampStateUpdate(*pPid, CMD_IN_PROGRESS);
    uiLog(*pPid,funcName,cmdTag,OAMP_IN_PROGRESS, errCode);

    return(IPOS_SUCCESS);

}
/* the complete functions should be short one: add job to oampTask1     */

void portLinkRatesComplete(ipos_port_id portId, U8 UID,U32 cntRpt, int status,
                        ipos_port_status *par)
{
    int len;
    len=sizeof(ipos_port_status);
    addCompletionJob(portId, UID, cntRpt, status,(U8 *)  par, len,portLinkRatesJob);
}
/* the complete function running in the oamp task       */
static  void    portLinkRatesJob(U32 arg1, U32 arg2, ipos_msg_buf  *pBuf)
{

    oamp_cb_msg     *pMsg;
    ipos_port_id    pid;
    ipos_port_status    *pStatus;

        U32     cntRpt;
       U8      cmdTag;
    int     errCode;
     int     bandRate=64;
    char    funcName[]="iposPortStatus";


    if (pBuf==NULL)
    {
        printf("Invalid msg buffer in iposPortStatus Callback \n");
        return;
    }

    /* parse the message        */
    pMsg=(oamp_cb_msg *) pBuf->pDataBuf;

    pid     =pMsg->portId;
    cntRpt  =pMsg->cntRpt;
    cmdTag  =pMsg->uid;
    errCode =pMsg->status;

    pStatus=(ipos_port_status *) pMsg->par;

    /* update the state         */
    if (pMsg->status==IPOS_SUCCESS)
    {

        uiLog(pid,funcName,cmdTag,OAMP_COMPLETE, errCode);
        oampStateUpdate(pid,ePORT_STATUS_BRIEF);
         if(pStatus->LineType & VDSL1_TYPE_SUPPORTED)
        {
            bandRate = 64;
        }
        else
        {
            bandRate = 16;
        }

        send_bme_linerates(pStatus->UsLineRate*bandRate,pStatus->DsLineRate*bandRate);
       // uiLogPortStatus(pid, pStatus, bandRate);

    } else
    {
        uiLog(pid,funcName,cmdTag,OAMP_FAILED, errCode);
        oampStateUpdate(pid,CMD_FAILED);
    }

    /* release the message      */
    iposMsgFree(pBuf);

}
#endif

/* the complete functions should be short one: add job to oampTask1	*/

void portStatusComplete(ipos_port_id portId, U8 UID,U32 cntRpt, int status,  
                        ipos_port_status *par)
{
    int len;
    len=sizeof(ipos_port_status);
    addCompletionJob(portId, UID, cntRpt, status,(U8 *)  par, len,portStatusJob);
}



/* the complete function running in the oamp task	*/
static  void    portStatusJob(U32 arg1, U32 arg2, ipos_msg_buf  *pBuf)
{

    oamp_cb_msg     *pMsg;
    ipos_port_id    pid;
    ipos_port_status    *pStatus;

    U32     cntRpt;
    U8      cmdTag;
    int     errCode;
    int     bandRate=64;
    char    funcName[]="iposPortStatus";


    if (pBuf==NULL)
    {
        printf("Invalid msg buffer in iposPortStatus Callback \n");
        return;
    }

    /* parse the message	*/
    pMsg=(oamp_cb_msg *) pBuf->pDataBuf;

    pid     =pMsg->portId;
    cntRpt  =pMsg->cntRpt;
    cmdTag  =pMsg->uid;
    errCode =pMsg->status;

    pStatus=(ipos_port_status *) pMsg->par;

    /* update the state		*/
    if (pMsg->status==IPOS_SUCCESS)
    {

        uiLog(pid,funcName,cmdTag,OAMP_COMPLETE, errCode);
        oampStateUpdate(pid,ePORT_STATUS_BRIEF);


//		if (oampGetBandPlan ==BP3_998_4)
//			bandRate=64*94/100;
        if(pStatus->LineType & VDSL1_TYPE_SUPPORTED)
        {
            bandRate = 64;
        }
        else
        {
            bandRate = 16;
        }

        uiLogPortStatus(pid, pStatus, bandRate);    

    } else
    {
        uiLog(pid,funcName,cmdTag,OAMP_FAILED, errCode);
        oampStateUpdate(pid,CMD_FAILED);
    }

    /* release the message	*/
    iposMsgFree(pBuf);

}


/*************************************************************
**
** oamp functions for iposPortGetCounters Used by the shell	
**
*************************************************************/

int oampPortGetCounters(ipos_port_id    *pPid, U8 cmdTag, U32 cntRpt)
{
    int errCode=IPOS_SUCCESS;
    char    funcName[]="iposPortGetCounters";

    /* get the port and BME state	*/
    if (oampGetBmeState(pPid->bmeNum)!=uRUN)
    {
        errCode=OAMP_ERROR_BME_NOT_READY;
        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);
        return(errCode);
    }

/* get the port state	*/
#if 0
    if (oampGetPortState(*pPid)!=uENABLE)
    {
        errCode=OAMP_ERROR_PORT_NOT_READY;
        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);
        return(errCode);

    }

#endif
    /* issue the API call and print the status	*/

    errCode=iposPortGetCounters(*pPid, cmdTag, cntRpt, portCountersComplete);

    if (errCode!=IPOS_SUCCESS)
    {
        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);
        return(errCode);
    }

/* update teh port state	*/

    oampStateUpdate(*pPid, CMD_IN_PROGRESS);    
    uiLog(*pPid,funcName,cmdTag,OAMP_IN_PROGRESS, errCode);

    return(IPOS_SUCCESS);

}

/* the complete functions should be short one: add job to oampTask1	*/

void portCountersComplete(ipos_port_id portId, U8 UID,U32 cntRpt, int status,  
                          ipos_port_counters *par)
{
    int len;
    len=sizeof(ipos_port_counters);
    addCompletionJob(portId, UID, cntRpt, status, (U8 *)par,len,portCountersJob);
}



/* the complete function running in the oamp task	*/
static  void    portCountersJob(U32 arg1, U32 arg2, ipos_msg_buf    *pBuf)
{

    oamp_cb_msg     *pMsg;
    ipos_port_id    pid;
    ipos_port_counters  *pCounters;

    U32     cntRpt;
    U8      cmdTag;
    int     errCode;
    char    funcName[]="iposPortCounters";


    if (pBuf==NULL)
    {
        printf("Invalid msg buffer in iposPortCounters Callback \n");
        return;
    }

    /* parse the message	*/
    pMsg=(oamp_cb_msg *) pBuf->pDataBuf;

    pid     =pMsg->portId;
    cntRpt  =pMsg->cntRpt;
    cmdTag  =pMsg->uid;
    errCode =pMsg->status;

    pCounters=(ipos_port_counters *) pMsg->par;



    /* update the state		*/
    if (pMsg->status==IPOS_SUCCESS)
    {

        copyCountersToOamp (pCounters,pid);

        uiLog(pid,funcName,cmdTag,OAMP_COMPLETE, errCode);
        oampStateUpdate(pid,ePORT_STATUS_BRIEF);

        uiLogPortCounters(pid,pCounters);



    } else
    {
        uiLog(pid,funcName,cmdTag,OAMP_FAILED, errCode);
        oampStateUpdate(pid,CMD_FAILED);
    }

    /* release the message	*/
    iposMsgFree(pBuf);

}



/*************************************************************
**
** oamp functions for iposPortGetNeSnr Used by the shell	
**
*************************************************************/

int oampPortGetNeSnr(ipos_port_id   *pPid, U8 cmdTag, U32 cntRpt)
{
    int errCode=IPOS_SUCCESS;
    char    funcName[]="iposPortGetNeSnr";

    /* get the port and BME state	*/
    if (oampGetBmeState(pPid->bmeNum)!=uRUN)
    {
        errCode=OAMP_ERROR_BME_NOT_READY;
        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);
        return(errCode);
    }

/* get the port state	*/

#if 0
    if (oampGetPortState(*pPid)!=uENABLE)
    {
        errCode=OAMP_ERROR_PORT_NOT_READY;
        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);
        return(errCode);

    }
#endif

    cntRpt=1;                                   /* repeat once to fix the bug in the BME	*/

    /* issue the API call and print the status	*/

    errCode=iposGetNeSNRAttn(*pPid, cmdTag, cntRpt, portNeSnrComplete);

    if (errCode!=IPOS_SUCCESS)
    {
        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);
        return(errCode);
    }

/* update teh port state	*/

    oampStateUpdate(*pPid, CMD_IN_PROGRESS);    
    uiLog(*pPid,funcName,cmdTag,OAMP_IN_PROGRESS, errCode);

    return(IPOS_SUCCESS);

}

/* the complete functions should be short one: add job to oampTask1	*/

void portNeSnrComplete(ipos_port_id portId, U8 UID,U32 cntRpt, int status,  
                       ipos_port_nesnrattn *par)
{
    int len;
    len=sizeof(ipos_port_nesnrattn);
    addCompletionJob(portId, UID, cntRpt, status, (U8 *) par, len,portNeSnrJob);
}



/* the complete function running in the oamp task	*/
static  void    portNeSnrJob(U32 arg1, U32 arg2, ipos_msg_buf   *pBuf)
{

    oamp_cb_msg     *pMsg;
    ipos_port_id    pid;
    ipos_port_nesnrattn *pNeSnr;

    U32     cntRpt;
    U8      cmdTag;
    int     errCode;
    char    funcName[]="iposPortNeSnr";


    if (pBuf==NULL)
    {
        printf("Invalid msg buffer in iposPortNeSnr Callback \n");
        return;
    }

    /* parse the message	*/
    pMsg=(oamp_cb_msg *) pBuf->pDataBuf;

    pid     =pMsg->portId;
    cntRpt  =pMsg->cntRpt;
    cmdTag  =pMsg->uid;
    errCode =pMsg->status;

    pNeSnr=(ipos_port_nesnrattn *) pMsg->par;

    /* update the state		*/
    if (pMsg->status==IPOS_SUCCESS)
    {
        uiLog(pid,funcName,cmdTag,OAMP_COMPLETE, errCode);
        oampStateUpdate(pid,ePORT_STATUS_BRIEF);
        copySnrAttnToOamp(pNeSnr,pid);
        uiLogPortNeSnr(pid,pNeSnr);
    } else
    {
        uiLog(pid,funcName,cmdTag,OAMP_FAILED, errCode);
        oampStateUpdate(pid,CMD_FAILED);
    }



    /* release the message	*/
    iposMsgFree(pBuf);

}

int oampPortClrNeCntr(ipos_port_id   *pPid, U8 cmdTag, U32 cntRpt)
{
    int errCode=IPOS_SUCCESS;
    char    funcName[]="iposPortClrCounter";

    /* get the port and BME state	*/
    if (oampGetBmeState(pPid->bmeNum)!=uRUN)
    {
        errCode=OAMP_ERROR_BME_NOT_READY;
        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);
        return(errCode);
    }

    /* issue the API call and print the status	*/

    errCode=iposBmeClearAllCounters(*pPid, cmdTag, cntRpt, portNeClrCntrComplete);

    if (errCode!=IPOS_SUCCESS)
    {
        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);
        return(errCode);
    }

/* update the port state	*/

    oampStateUpdate(*pPid, CMD_IN_PROGRESS);    
    uiLog(*pPid,funcName,cmdTag,OAMP_IN_PROGRESS, errCode);

    return(IPOS_SUCCESS);

}


void portNeClrCntrComplete(ipos_port_id portId, U8 UID,U32 cntRpt, int status,  
                       void *par)
{
	addCompletionJob(portId, UID, cntRpt, status, (U8 *)par, 0 ,portNeClrCntrJob);
}

/* the complete function running in the oamp task	*/
static  void    portNeClrCntrJob(U32 arg1, U32 arg2, ipos_msg_buf   *pBuf)
{

    oamp_cb_msg     *pMsg;
    ipos_port_id    pid;
    
    U32     cntRpt;
    U8      cmdTag;
    int     errCode;
    char    funcName[]="iposPortClrCounter";


    if (pBuf==NULL)
    {
        printf("Invalid msg buffer in iposPortClrCounter Callback \n");
        return;
    }

    /* parse the message	*/
    pMsg=(oamp_cb_msg *) pBuf->pDataBuf;

    pid     =pMsg->portId;
    cntRpt  =pMsg->cntRpt;
    cmdTag  =pMsg->uid;
    errCode =pMsg->status;

 

    /* update the state		*/
    if (pMsg->status==IPOS_SUCCESS)
    {
		InitPerfPara(pid.bmeNum,pid.portNum);
        if(oampGetOamOptionMask & 0x1) //If PM Counters are enabled, reset
		portStartCopy[pid.bmeNum][pid.portNum] = 1;
#ifdef INI_SUPPORT
        ResetCrcThresCnt(pid);
#endif
        uiLog(pid,funcName,cmdTag,OAMP_COMPLETE, errCode);
        oampStateUpdate(pid,ePORT_STATUS_BRIEF);


    } else
    {
        uiLog(pid,funcName,cmdTag,OAMP_FAILED, errCode);
        oampStateUpdate(pid,CMD_FAILED);
    }



    /* release the message	*/
    iposMsgFree(pBuf);

}

int oampOptionalCmd(ipos_port_id    *pPid, U8 cmdTag, U32 buf, int length)
{
    int         errCode=IPOS_SUCCESS;
    char        funcName[]="iposOptionalCmd";
    ipos_op_cmd opCmd;

    /* get the port and BME state	*/
    if (oampGetBmeState(pPid->bmeNum)!=uRUN)
    {
        errCode=OAMP_ERROR_BME_NOT_READY;
        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);
        return(errCode);
    }

    /* issue the API call and print the status	*/
    opCmd.buf = buf;
    opCmd.len = length;

    errCode=iposOptionalCmd(*pPid, cmdTag, buf, (U32 )&opCmd,  optionalCmdComplete);

    if (errCode!=IPOS_SUCCESS)
    {
        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);
        return(errCode);
    }

    /* update the port state	*/

    oampStateUpdate(*pPid, CMD_IN_PROGRESS);    
    uiLog(*pPid,funcName,cmdTag,OAMP_IN_PROGRESS, errCode);

    return(IPOS_SUCCESS);

}

void optionalCmdComplete(ipos_port_id portId, U8 UID, U32 tag, int status,  ipos_opCmd_status *par)
{
    int len;
    len=sizeof(ipos_opCmd_status);
    addCompletionJob(portId, UID, tag, status, (U8 *) par, len, optionalCmdJob);
}

/* the complete function running in the oamp task	*/
void (*OptionCmdHandler)(void *pMsg) = NULL;

static  void    optionalCmdJob(U32 arg1, U32 arg2, ipos_msg_buf *pBuf)
{

    oamp_cb_msg     *pMsg;
    char            funcName[]="optionalCmdJob";

    if (pBuf==NULL)
    {
        printf("Invalid msg buffer in optionalCmdJob Callback \n");
        return;
    }

    /* parse the message	*/
    pMsg=(oamp_cb_msg *) pBuf->pDataBuf;

    /* update the state		*/
    if (pMsg->status==IPOS_SUCCESS)
    {
        if (OptionCmdHandler!=NULL)
            OptionCmdHandler((void *)pMsg);

        uiLog(pMsg->portId,funcName,pMsg->uid,OAMP_COMPLETE, pMsg->status);
    } else
    {
        uiLog(pMsg->portId,funcName,pMsg->uid,OAMP_FAILED, pMsg->status);
        oampStateUpdate(pMsg->portId,CMD_FAILED);
    }

    /* release the message	*/
    iposMsgFree(pBuf);
}


/*************************************************************
**
** oamp functions for iposPortGetPMCounters Used by the shell	
**
*************************************************************/

int oampPortGetPMCounters(ipos_port_id    *pPid, U8 cmdTag, U32 cntRpt)
{
    int errCode=IPOS_SUCCESS;
    char    funcName[]="iposPortGetPMCounters";

    /* get the port and BME state	*/
    if (oampGetBmeState(pPid->bmeNum)!=uRUN)
    {
        errCode=OAMP_ERROR_BME_NOT_READY;
        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);
        return(errCode);
    }

/* get the port state	*/
#if 0
    if (oampGetPortState(*pPid)!=uENABLE)
    {
        errCode=OAMP_ERROR_PORT_NOT_READY;
        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);
        return(errCode);

    }
#endif

    /* issue the API call and print the status	*/
    errCode=iposPortGetPMCounters(*pPid, cmdTag, cntRpt, portPmCountersComplete);

    if (errCode!=IPOS_SUCCESS)
    {
        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);
        return(errCode);
    }

    /* update the port state	*/

    oampStateUpdate(*pPid, CMD_IN_PROGRESS);
    uiLog(*pPid,funcName,cmdTag,OAMP_IN_PROGRESS, errCode);

    return(IPOS_SUCCESS);

}

/* the complete functions should be short one: add job to oampTask1	*/

void portPmCountersComplete(ipos_port_id portId, U8 UID,U32 cntRpt, int status,  
                          ipos_pm_counters *par)
{
    int len;
    len=sizeof(ipos_pm_counters);
    addCompletionJob(portId, UID, cntRpt, status, (U8 *)par,len, portPMCountersJob);
}



/* the complete function running in the oamp task	*/
static  void    portPMCountersJob(U32 arg1, U32 arg2, ipos_msg_buf    *pBuf)
{

    oamp_cb_msg     *pMsg;
    ipos_port_id    pid;
    ipos_pm_counters  *pPmCntr;

    U32     cntRpt;
    U8      cmdTag;
    int     errCode;
    char    funcName[]="iposPortPMCounters";


    if (pBuf==NULL)
    {
        printf("Invalid msg buffer in iposPortPMCounters Callback \n");
        return;
    }

    /* parse the message	*/
    pMsg=(oamp_cb_msg *) pBuf->pDataBuf;

    pid     =pMsg->portId;
    cntRpt  =pMsg->cntRpt;
    cmdTag  =pMsg->uid;
    errCode =pMsg->status;

    pPmCntr =(ipos_pm_counters *) pMsg->par;

    /* update the state		*/
    if (pMsg->status==IPOS_SUCCESS)
    {

        copyPMCountersToOamp (pPmCntr, pid);

        uiLog(pid,funcName,cmdTag,OAMP_COMPLETE, errCode);

        oampStateUpdate(pid,ePORT_STATUS_BRIEF);
        uiLogPortPmCounters(pid, pPmCntr);

    } else
    {
        uiLog(pid,funcName,cmdTag,OAMP_FAILED, errCode);
        oampStateUpdate(pid,CMD_FAILED);
    }

    /* release the message	*/
    iposMsgFree(pBuf);

}



/*************************************************************
**
** oamp functions for iposPmConfig Used by the shell	
**
*************************************************************/

int oampPmConfig (ipos_port_id *pPid, ipos_pm_config *pmConfig, U8 cmdTag, U32 cntRpt)
{
    int errCode=IPOS_SUCCESS;
    char funcName[]="iposPmConfig";

    /* get the BME state	*/
    if (oampGetBmeState(pPid->bmeNum)!=uRUN)
    {
        errCode = OAMP_ERROR_BME_NOT_READY;
        uiLog (*pPid, funcName, cmdTag, OAMP_FAILED, errCode);
        return(errCode);
    }

    /* get the port state	*/
#if 0
    if (oampGetPortState(*pPid)!=uENABLE)
    {
        errCode=OAMP_ERROR_PORT_NOT_READY;
        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);
        return(errCode);

    }
#endif

    /* issue the API call and print the status	*/
    errCode = iposPmConfig(*pPid, pmConfig, cmdTag, cntRpt, PmConfigComplete);

    if (errCode!=IPOS_SUCCESS)
    {
        uiLog(*pPid, funcName, cmdTag, OAMP_FAILED, errCode);
        return(errCode);
    }

    /* update the port state	*/
    oampStateUpdate(*pPid, CMD_IN_PROGRESS);

    uiLog(*pPid, funcName, cmdTag, OAMP_IN_PROGRESS, errCode);

    return(IPOS_SUCCESS);

}


/* The complete function. Should be short one: adds job to oampTask1	*/

void PmConfigComplete(ipos_port_id portId, U8 UID, U32 cntRpt, int status,  
                      void *par)
{
    int len;
    len = sizeof(ipos_pm_config);
    addCompletionJob(portId, UID, cntRpt, status, (U8 *)par, len, PmConfigJob);
}



/* the complete function running in the oamp task	*/
static void PmConfigJob(U32 arg1, U32 arg2, ipos_msg_buf *pBuf)
{

    oamp_cb_msg     *pMsg;
    ipos_port_id    pid;

    U32     cntRpt;
    U8      cmdTag;
    int     errCode;
    char    funcName[]="iposPmConfig";


    if (pBuf==NULL)
    {
        printf("Invalid msg buffer in iposPmConfig Callback \n");
        return;
    }

    /* parse the message	*/
    pMsg = (oamp_cb_msg *) pBuf->pDataBuf;

    pid     = pMsg->portId;
    cntRpt  = pMsg->cntRpt;
    cmdTag  = pMsg->uid;
    errCode = pMsg->status;

    /* update the state		*/
    if (pMsg->status == IPOS_SUCCESS)
    {

        uiLog(pid,funcName,cmdTag,OAMP_COMPLETE, errCode);

        oampStateUpdate(pid,ePORT_STATUS_BRIEF); // Simha: Check why ePORT_STATUS_BRIEF ?

    } else
    {
        uiLog(pid,funcName,cmdTag,OAMP_FAILED, errCode);
        oampStateUpdate(pid,CMD_FAILED);
    }

    /* release the message	*/
    iposMsgFree(pBuf);

}



/*************************************************************
**
** oamp functions for iposPortPmTrigger Used by the shell	
**
*************************************************************/

int oampPortPmTriggerFailures (ipos_port_id *pPid, ipos_pm_trigger *pmTrigger,
                               U8 cmdTag, U32 cntRpt)
{

    int errCode=IPOS_SUCCESS;
    char    funcName[]="iposPortPmTrigger";

    /* get the port and BME state	*/
    if (oampGetBmeState(pPid->bmeNum)!=uRUN)
    {
        errCode=OAMP_ERROR_BME_NOT_READY;
        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);
        return(errCode);
    }

/* get the port state	*/
#if 0
    if (oampGetPortState(*pPid)!=uENABLE)
    {
        errCode=OAMP_ERROR_PORT_NOT_READY;
        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);
        return(errCode);

    }
#endif

    /* issue the API call and print the status	*/
    errCode = iposPortPmTriggerFailures(*pPid, pmTrigger, cmdTag, cntRpt,
                                        portPmTriggerComplete);

    if (errCode!=IPOS_SUCCESS)
    {
        uiLog(*pPid,funcName,cmdTag,OAMP_FAILED, errCode);
        return(errCode);
    }

    /* update the port state	*/
    oampStateUpdate(*pPid, CMD_IN_PROGRESS);
    uiLog(*pPid,funcName,cmdTag,OAMP_IN_PROGRESS, errCode);

    return(IPOS_SUCCESS);

}

/* the complete functions should be short one: add job to oampTask1	*/

void portPmTriggerComplete(ipos_port_id portId, U8 UID,U32 cntRpt, int status,  
                          void *par)
{
    int len;
    len=sizeof(ipos_pm_trigger);
    addCompletionJob(portId, UID, cntRpt, status, (U8 *)par,len, portPmTriggerJob);
}



/* the complete function running in the oamp task	*/
static void portPmTriggerJob(U32 arg1, U32 arg2, ipos_msg_buf *pBuf)
{

    oamp_cb_msg    *pMsg;
    ipos_port_id    pid;

    U32     cntRpt;
    U8      cmdTag;
    int     errCode;
    char    funcName[]="iposPortPmTrigger";


    if (pBuf==NULL)
    {
        printf("Invalid msg buffer in iposPortPmTrigger Callback \n");
        return;
    }

    /* parse the message	*/
    pMsg=(oamp_cb_msg *) pBuf->pDataBuf;

    pid     =pMsg->portId;
    cntRpt  =pMsg->cntRpt;
    cmdTag  =pMsg->uid;
    errCode =pMsg->status;

    /* update the state		*/
    if (pMsg->status==IPOS_SUCCESS)
    {

        uiLog(pid,funcName,cmdTag,OAMP_COMPLETE, errCode);

        oampStateUpdate(pid,ePORT_STATUS_BRIEF); // Simha: Check

    } else
    {
        uiLog(pid,funcName,cmdTag,OAMP_FAILED, errCode);
        oampStateUpdate(pid,CMD_FAILED);
    }

    /* release the message	*/
    iposMsgFree(pBuf);

}

/* demonstration version just displays the fields in the brief status report from the BME */

static void
updateReportJob(U32 arg1, U32 arg2, ipos_msg_buf *pBuf)
{
	oamp_cb_msg     	*pMsg;
	ipos_port_id     	 pid;
	int			 lineNum = 0;
	ipos_port_status_brief	*pReport;

	(void) arg1; (void) arg2;

	if (pBuf==NULL)
	{
		printf( "Invalid msg buffer in update report job\n\r");
	}

    /* parse the message	*/

	pMsg=(oamp_cb_msg *) pBuf->pDataBuf;

	pid = pMsg->portId;

	getLineNum( pid, &lineNum );

    /* the proxy should not submit a job for a port w/o a line number */

	if (lineNum == 0)
	{
		printf(
	   "update report job, cannot convert b%dp%d to a line number\n\r",
	    pid.bmeNum, pid.portNum
		);
		iposMsgFree( pBuf );
		return;
	}

	pReport = (ipos_port_status_brief *) pMsg->par;
	printf( "brief port status for b%dp%d: current status: 0x%2x, current framing mode: 0x%2x\n",
		pid.bmeNum, pid.portNum, pReport->portStatus, pReport->currentFramingMode
	);

        iposMsgFree( pBuf );
}

static void
ReceiveUpdateReport(ipos_port_id pid, U8 uid, U32 upar, int status, ipos_port_status_brief *pReport)
{
	(void) upar;
	addCompletionJob( pid, uid, 0, status,
		   (U8 *) pReport, sizeof( ipos_port_status_brief ),
			  updateReportJob
	);
}

/*
 * note: interface changed from the reference host application for V6.7.3
 * (the BME firmware delivered to Alcatel for their RCLM)
 *
 * this version registers the callback to be called when the BME sends an update
 * report for a particular port.  Version in the reference host application for
 * V6.7.3 registered the callback for all known ports in the build.
 */

void
oampSetupPhaseChange( ipos_port_id *pPid )
{
	int	lineNum = 0;

	getLineNum( *pPid, &lineNum );
	if (lineNum != 0)
	{
		iposSetCbUReport( *pPid, 0, ReceiveUpdateReport);
	}
}
