/**************************************************************
$Workfile:   apicommon.c  $
***************************************************************
**
** FIRMWARE, Copyright (C) 2000-2002
** Ikanos Communications, Fremont, CA
** All rights reserved.
**
** The information and contents of this file are the
** proprietary information of Ikanos Communication and may not
** be disclosed or used without the formal written approval of
** Ikanos Communication
**
$Revision: 1.1 $
$Date: 2006/10/16 04:34:51 $
***************************************************************/


/**************************************************************
**
**	  the routines handle callback functions  
**
**
***************************************************************/

#include <stdio.h>
#include "ipos.h"
#include "dapi.h"
#include "apilocal.h"



/* Set callback function and UID to a msg_buffer	*/ 

inline int	iposSetCallback(ipos_msg_buf   *pBuf, iposCbCtl pCb, ipos_port_id pid, U8 UID, U32 uPar)
{

	int	intLevel;							/* process the msg_buf context should be protected	*/

	if(pBuf!=NULL)
	{
		intLevel=iposIntLock();
		
		pBuf->cbSet=IPOS_MSG_CALLBACK_SET;	/* callback is set		*/
		pBuf->tick=IPOS_CALL_EXPIRED_TICK;	/* use global expiration tick */

		pBuf->usrData[0]=(U32) pCb;			/* the first usr defined par is cb */
		pBuf->usrData[1]=(U32) UID;
		pBuf->usrData[2]=(U32) pid.bmeNum;
		pBuf->usrData[3]=(U32) pid.portNum;
		pBuf->usrData[4]=	uPar;
		
		iposIntUnlock(intLevel);
		return (IPOS_SUCCESS);
	}
	return(IPOS_ERROR);
}


/* get callback function and UID from a msg_buffer	*/

inline iposCbCtl iposGetCallback(ipos_msg_buf *pBuf,ipos_port_id *pid, U8 *UID, U32 *uPar)
{

	int	intLevel;							/* process the msg_buf context should be protected	*/
    if(pBuf!=NULL)
	{
		intLevel=iposIntLock();
		
		pBuf->cbSet=IPOS_MSG_CALLBACK_NOTSET;
		
		*UID= (U8) pBuf->usrData[1];
		*uPar=pBuf->usrData[4];

		pid->bmeNum=(int)  pBuf->usrData[2];
		pid->portNum=(int) pBuf->usrData[3];
		
		iposIntUnlock(intLevel);
		return  (iposCbCtl) (pBuf->usrData[0]);
	}

	return NULL ;


}



/* API protection routines	*/

inline void	iposApiEnter(ipos_api_bme *pBme)
{

#ifdef IPOS_USE_RTOS 
	iposSemTake(pBme->semBme, IPOS_WAIT_FOREVER);
#endif    

}

inline void	iposApiLeave(ipos_api_bme *pBme)
{

#ifdef	IPOS_USE_RTOS
	iposSemGive(pBme->semBme);
#endif

}


/* BME state for USR API level	*/

void	iposStateUpdate(ipos_api_bme *pApiBme, eipos_bmeapi_evnt evnt)
{

	switch(evnt)
	{
		case  IPOS_BME_STARTED:
		
		pApiBme->bmeState=IPOS_BME_RUN;
		break;

		case IPOS_BME_RESTART:
		
		pApiBme->bmeState=IPOS_BME_IDLE;
		break;

	default:
		break;
	}

}

/* get state for user API	*/

eipos_bmeapi_state	iposGetState(ipos_api_bme *pApiBme)
{
	return	pApiBme->bmeState;
}




