/**************************************************************
$Workfile:   oamptask.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:50 $
***************************************************************/

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


#include 	"ipos.h"
#include 	"apposmacro.h"				/* use vxWorks port  */
#include	"oamptask.h"


#define		MAX_MSG_LEN	32

#define		OAMP_TASK_PRIORITY	100
#define		OAMP_STACK_SIZE		8192

unsigned int uloampTask[2];
	/* OAMP Task message queue format	*/

typedef	struct	
{
	int	oampid;				/* the target oampid: for error checking	*/
	
	appOampFunc	pFunc;		/* the function be invoked in the task 		*/
	
	int	arg1;				/* arg1 for the function	*/
	
	int	arg2;				/* arg2 for the function	*/
	
	ipos_msg_buf *arg3;		/* argument3 is the ipos buffer */

}	app_oamp_msg;


/* Local functions definition		*/

static	void  appOampTask(int oampId,...);


/* Array for the OAMP tasks id and message queues	*/
/* Each task is associated with a message queue		*/

int			appOampTaskId[APP_OAMP_TASK_NUM];
ipos_msg_id	appOampMsgQ[APP_OAMP_TASK_NUM];



/**************************************
**
** Oamp	tasks initialization 
**
** 	Create MsgQ and Spawn the Task
**
**	return:	IPOS_SUCCESS or IPOS_ERROR
**
***************************************/
int ids[2] ={0,1};

int appOampTaskInit(int numOampTasks)
{

	int		i;
	char	taskName[256];
    
	for(i=0;i<numOampTasks;i++)
	{
		
		/* initialize the message Queue	*/
		
		appOampMsgQ[i]=iposMsgQCreate();
		
		if(appOampMsgQ[i]==NULL)
		{
			return(IPOS_ERROR);
		}


		/* spawn the tasks	*/
		sprintf(taskName,"OampTask%d",i);
               // printf("appOampTaskInit() :taskName %s\n",taskName);
		appOampTaskId[i]=iposTaskSpawn(taskName, OAMP_TASK_PRIORITY, OAMP_STACK_SIZE, appOampTask,i,0,0); 		   


		if(appOampTaskId[i]==OS_ERROR)
		{
                printf("appOampTaskInit() :taskName %s Error\n",taskName);
			return(IPOS_ERROR);
		}
                //printf("appOampTaskInit() :taskName %s success\n",taskName);

	}

	return (IPOS_SUCCESS);

}



/***********************************************************************
**
**	
** 	Add function in the OAMP task with ID= oampId
**	
**	
**	return Value: IPOS_SUCCESS if msg is sent or IPOS_ERROR
**
**
***********************************************************************/ 


int	appOampJobAdd(int oampId, appOampFunc pFunc, U32 arg1, U32 arg2, ipos_msg_buf *par3)
{

	char	msgBuffer[MAX_MSG_LEN];
	app_oamp_msg	*pMsg;

	pMsg=(app_oamp_msg *)msgBuffer;
	
	pMsg->pFunc=pFunc;

	pMsg->arg1=arg1;

	pMsg->arg2=arg2;

	pMsg->arg3=par3;

	if(iposMsgQSend(appOampMsgQ[oampId],(U8*)msgBuffer,MAX_MSG_LEN)==OS_SUCCESS)
	{
		return(IPOS_SUCCESS);
	}

	return(IPOS_ERROR);

}


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

**	Local functions																	  
																	  
*********************************************************************/



static void	appOampTask(int oampId,...)
{

	char	msgBuffer[MAX_MSG_LEN];
	app_oamp_msg	*pMsg;

	while(1)
	{
       //  printf("Fusiv appOampTask() Msg Recv oampId %d\n",oampId);
		
		if(iposMsgQReceive(appOampMsgQ[oampId], (U8*)msgBuffer, MAX_MSG_LEN)==OS_ERROR)
		{
			printf("Bad Message for OAMP task %d \n",oampId);
			break;
		}
       //  printf("Fusiv appOampTask() Msg Recv after\n");

		pMsg=(app_oamp_msg *) msgBuffer;

		/* call the function contained in the msg buffer 	*/
		pMsg->pFunc(pMsg->arg1, pMsg->arg2, pMsg->arg3);
	}

}


