/******************************************************************************
* Copyright(C) Video Surveillance(China) Co. Ltd
* Version	  : 1.0
* File Name	: ssi.h
* Creation	 : Lawrence 2006-9-6
* Description  : The interface of SSI, including memory management, task
*				 management, message management, debug shell, mutex, semaphore.
* Modification :
******************************************************************************/
#ifndef __VS_SSI_H__
#define __VS_SSI_H__

//#pragma warning(disable: 4786)  // remove compiling warning for VC6


/*=============================================================================
 *							Socket
 *=============================================================================
 */
#if defined(WIN32)

#ifdef __cplusplus
extern "C" {
#endif

	#include <winsock2.h>

#ifdef __cplusplus
}
#endif

	#include <WS2tcpip.h> // can't be surrouded by extern "C" under VS2005
#endif

#ifdef __cplusplus
extern "C" {
#endif

/*------------------------------ include files-------------------------------*/
#ifdef LINUX
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#endif

/*------------------------------ Const definitions --------------------------*/
#ifdef FALSE
	#undef FALSE
	#define FALSE	   0		 /* for booleans */
#else
	#define FALSE	   0		 /* for booleans */
#endif

#ifdef TRUE
	#undef TRUE
	#define TRUE		1		 /* for booleans */
#else
	#define TRUE		1		 /* for booleans */
#endif

/*------------------------------- Type definitions --------------------------*/
#ifndef _DEF_Bool_
#define _DEF_Bool_
typedef unsigned char Bool;
#endif /*_DEF_Bool_*/

#ifndef NULL
#ifdef __cplusplus
   #define NULL  (0)
#else
   #define NULL  ((void *)0)
#endif
#endif /* !NULL */


#if (!defined WIN32 && !defined VXWORKS)
#ifndef _DEF_BOOL_
#define _DEF_BOOL_
typedef unsigned int BOOL;
#endif
#endif

#ifndef _DEF_S8_
#define _DEF_S8_
typedef char S8;
#endif

#ifndef _DEF_U8_
#define _DEF_U8_
typedef unsigned char U8;
#endif

#ifndef _DEF_S16_
#define _DEF_S16_
typedef short int S16;
#endif

#ifndef _DEF_U16_
#define _DEF_U16_
typedef unsigned short U16;
#endif

#ifndef _DEF_S32_
#define _DEF_S32_
typedef int S32;
#endif

#ifndef _DEF_U32_
#define _DEF_U32_
typedef unsigned int U32;
#endif

#ifdef WIN32
typedef __int64		   S64;
typedef unsigned __int64  U64;
#endif

#ifdef LINUX
typedef long long		 S64;
typedef unsigned long long U64;
#endif

#if ( defined(ALPHA) || defined(BIT_64) )
typedef long		  S64;	  /* signed - 64 bits */
typedef unsigned long U64;	  /* unsigned - 64 bits */
#endif /* ALPHA  & BIT_64 */

#ifndef WIN32
#ifndef HAVE_HANDLE
#define HAVE_HANDLE
typedef U32 HANDLE;
#endif

#ifndef HAVE_BYTE
#define HAVE_BYTE
typedef U8 BYTE;
#endif

#ifndef HAVE_WORD
#define HAVE_WORD
typedef U16 WORD;
#endif

#ifndef HAVE_DWORD
#define HAVE_DWORD
typedef U32 DWORD;
#endif
#endif /* #ifndef WIN32 */

#ifdef LINUX
#define ERROR		   (-1)
#define OK			  0
#endif

/* causing compiling error with iostream in gcc, removing it */
/* Define some useful general macros
#if !defined(max)
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#endif
*/

/*-------------------------- SSI type definitions -------------------------*/
#define WS_OK		   0	  /* SSI function return OK */
#define WS_ERROR		(-1)   /* SSI function return ERROR */

typedef S32 WS_STATUS;	/* SSI function return value */

typedef U32 WS_SEM_ID;	/* SSI Semaphore Id */

typedef U32 WS_MUTEX_ID;  /* SSI Mutex Id */

typedef U32 WS_TIMER_ID;  /* SSI Timer ID */

#if (defined(SOLARIS) || defined(LINUX))
/* condtion semaphore id. added by alen on 2007-06-13 */
struct ws_condsem_s
{
	U32 nCount;
	pthread_mutex_t tMutex;
	pthread_cond_t tCond;

};
typedef struct ws_condsem_s* WS_CONDSEM_ID;
#endif

/* Timer callback function prototype */
typedef S32 (* WS_TIMER_FUNC) (void *pUserInfo, S32 nUsrInfoLen);

typedef struct ws_msg_s
{
	U32 nMagic;			 /* WS_SSI_MSG_MAGIC = 'WSSI' */
	U32 nEvent;
	U32 nSrcTask;
	U32 nDstTask;
	U32 nDataLen;
	U32 nParam1;	//src instance
	U32 nParam2;	//dst instance
	void *pData;
}WS_MSG,*WS_MSG_ID;   /* SSI message structure */

typedef U32 WS_TASK_ID;   /* SSI task ID */

/* SSI task Create entry function prototype */
typedef WS_STATUS (* WS_TASK_ENTRY_FUNC)(WS_MSG_ID pMsg, void *pArg);

/* SSI task Spawn entry function prototype */
typedef WS_STATUS (* WS_SPAWN_ROUTINE)(void *pArg);

/*----------------------- Operation definitions (Macro)----------------------*/
/*=============================================================================
 *							 Init SSI
 *=============================================================================
 */
/*****************************************************************************
 *	  W A R N I N G:   P L E A S E   I N I T   S S I   F I R S T		   *
 *****************************************************************************/
/******************************************************************************
* Function Name	   : WS_ssiInit
* Input Parameters	: cacheSize --- cache memory size, unit is byte.
*						heapSize --- heap memory size, unit is byte.
* Output Parameters   :	None.
* Return value		:	WS_OK or WS_ERROR
* Description		 : It set the memory size configure and init the memory
*						library, task library, message library, timer library.
* Modification		: 2006-8-13 created by Lawrence
******************************************************************************/
extern WS_STATUS WS_ssiInit(U32 cacheSize, U32 heapSize, U32 telnetPort);


/*=============================================================================
 *							 Error NO Management
 *=============================================================================
 */
/******************************************************************************
* Function Name		   : WS_errorNoSet
* Input Parameters		: dwErrorNo --- Error number.
*							pFile --- error file name.
*							dwLine --- error code line.
* Output Parameters	   :	None.
* Return value			:	None.
* Description			 : The routine sets the current task error number with
*							the	specified value. The error number must follow
*							above definition rules.
* Modification			:
******************************************************************************/
extern void WS_errorNoSet(U32 dwErrorNo, const S8 *pFile, U32 dwLine);

/******************************************************************************
* Function Name		   : WS_errorNoGet
* Input Parameters		: None.
* Output Parameters	   : None.
* Return value			: Error number of current task.
* Description			 :	The function return the error number of current
*							task. The error number is stored by SSI when a
*							system error occurs or user call WS_errorNoSet()
*							set a error number.
* Modification			:
******************************************************************************/
extern U32 WS_errorNoGet(void);


/*=============================================================================
 *								  Mutex
 *=============================================================================
 */
#ifndef WS_WAIT_FOREVER
	#define WS_WAIT_FOREVER -1	/*mutex will wait forever when use it to take*/
#endif

#ifndef WS_NO_WAIT
	#define WS_NO_WAIT 0		/*mutex will not wait when use it to take*/
#endif

#define WS_MUTEX_EMPTY 0 		/*Mutex will be created into taken state*/
#define WS_MUTEX_FULL  1		/*Mutex will be created into untaken state*/

/******************************************************************************
* Function Name		   : WS_mutexCreate
* Input Parameters		: szMutexName --- name of the mutex
							nInitState  --- Initialized state, WS_MUTEX_FULL or
											WS_MUTEX_EMPTY.
* Output Parameters	   : NULL
* Return value			: The mutex ID
* Description			 : Create a mutex
* Modification			: 2002-3-27	created
******************************************************************************/
extern WS_MUTEX_ID WS_mutexCreate(const S8 *szMutexName, S16 nInitState);

/******************************************************************************
* Function Name		   : WS_mutexDelete
* Input Parameters		: nMutexId --- ID of the mutex
* Output Parameters	   : NULL
* Return value			: WS_OK, WS_ERROR
* Description			 : Delete a mutex
* Modification			: 2002-3-27	created
******************************************************************************/
extern WS_STATUS WS_mutexDelete(WS_MUTEX_ID nMutexId);

/******************************************************************************
* Function Name		   : WS_mutexTake
* Input Parameters		: nMutexId --- ID of the mutex
*							nTimeout  --- Timeout value, WS_NO_WAIT for wait
*											forever.
* Output Parameters	   :	NULL
* Return value			: WS_OK, WS_ERROR
* Description			 : Take the mutex, if the timeout value is not
*							WS_WAIT_FOREVER, the task will be waken up
*							automatically when timeout is reached.
* Modification			: 2002-3-27	created
******************************************************************************/
extern WS_STATUS WS_mutexTake(WS_MUTEX_ID nMutexId, S32 nTimeout);

/******************************************************************************
* Function Name		   : WS_mutexGive
* Input Parameters		: nMutexId --- ID of the mutex
* Output Parameters	   : NULL
* Return value			: WS_OK, WS_ERROR
* Description			 : Release a mutex
* Modification			: 2002-3-27	created
******************************************************************************/
extern WS_STATUS WS_mutexGive(WS_MUTEX_ID nMutexId);


/*=============================================================================
 *							   Semaphore
 *=============================================================================
 */
/************************************************************************
* Function Name	: WS_semCreate
* Input Parameters : szSemName	--- name of this semaphore
*					lTotalCount  --- Number of total semaphore object.
*					lInitCount   --- number
* Output Parameters: NULL
* Return value	 : SemId, if is NULL, sem create failed.
* Description	  : Create a count type semaphore
* Modification	 : 2002-3-27	created
*************************************************************************/
extern WS_SEM_ID WS_semCreate(const S8 *szSemName, S32 lTotalCount, S32 lInitCount);

/*********************************************************************
* Function Name	 : WS_semDelete
* Input Parameters  : nSemId --- the ID of semphore to be deleted
* Output Parameters : NULL
* Return value	  : WS_OK, WS_ERROR
* Description	   : Delete a semaphore
* Modification	  : 2002-3-27	created
**********************************************************************/
extern WS_STATUS WS_semDelete(WS_SEM_ID nSemId);

/*************************************************************************
* Function Name	 : WS_semTake
* Input Parameters  : nSemId   --- The Id of semphore to be take
*					 nTimeout --- Timeout value,
*					 WS_WAIT_FOREVER(-1) for wait forever.
* Output Parameters : NULL
* Return value	  : WS_OK, WS_ERROR
* Description	   : Take a semaphore, if the timeout value is not -1,
*					 the task will be waken up automatically when timeout
*					 is reached.
* Modification	  : 2002-3-27	created
*************************************************************************/
extern WS_STATUS WS_semTake(WS_SEM_ID nSemId, S32 nTimeout);

/*********************************************************************
* Function Name	 : WS_semGive
* Input Parameters  : nSemId --- The ID of semaphore to be release
* Output Parameters : NULL
* Return value	  : WS_OK, WS_ERROR
* Description	   : release a semaphore
* Modification	  : 2002-3-27	created
**********************************************************************/
extern WS_STATUS WS_semGive(WS_SEM_ID nSemId);


#if (defined(SOLARIS) || defined(LINUX))
/***********************************************************************
* Function Name	 : WS_CondSemCreate
* Input Parameters  : condition sem id
* Output Parameters : NULL
* Return value	  : condition semapore id
* Description	   : this function will create and initialize semaphore
* Modification	  :
************************************************************************/
WS_CONDSEM_ID WS_CondSemCreate(void);

/***********************************************************************
* Function Name	 : WS_CondSemGive
* Input Parameters  : condition sem id
* Output Parameters : NULL
* Return value	  : 0 or error code
* Description	   : give  the semapore
* Modification	  :
************************************************************************/
WS_STATUS WS_CondSemGive( WS_CONDSEM_ID CondSemId );

/***********************************************************************
* Function Name	 : WS_CondSemTake
* Input Parameters  : condition sem id
* Output Parameters : NULL
* Return value	  : 0 or error code
* Description	   : Take a semaphore, if the timeout value is not -1,
					  the task will be waken up automatically when timeout
					  is reached.
* Modification	  :
************************************************************************/
WS_STATUS WS_CondSemTake( WS_CONDSEM_ID CondSemId, S32 iTimeOut );


/***********************************************************************
* Function Name	 : WS_CondSemDestroy
* Input Parameters  : condition sem id
* Output Parameters : NULL
* Return value	  : 0 or error code
* Description	   : destroy the semapore
* Modification	  :
************************************************************************/
WS_STATUS WS_CondSemDestroy( WS_CONDSEM_ID CondSemId );
#endif

/*=============================================================================
 *						   Memory Management
 *=============================================================================
 */
/******************************************************************************
* Function Name		   : WS_memAlloc
* Input Parameters		: lSize --- Allocation size.
* Output Parameters	   :	None.
* Return value			: Pointer to allocated buffer.
* Description			 : Allocate a static buffer from current module region.
* Modification			:
******************************************************************************/
extern void *WS_memAlloc(S32 lSize);

/******************************************************************************
* Function Name		   : WS_memFree
* Input Parameters		: *pBuffer --- Freed buffer pointer.
* Output Parameters	   : None.
* Return value			: WS_ROK or WS_ERROR to return sucessful or failure.
* Description			 : Free a static buffer
* Modification			:
******************************************************************************/
extern WS_STATUS WS_memFree(void *pBuffer);


/*=============================================================================
 *						   Message Management
 *=============================================================================
 */
#define WS_SSI_MSG_MAGIC		0x57535349	  /* 'WSSI'*/

#define WS_EVENT_TASK_INIT	  0x00000001
#define WS_EVENT_TASK_TMR	   0x00000002
#define WS_EVENT_USER_BASE		0x00001000

/******************************************************************************
* Function Name		   : WS_msgAlloc
* Input Parameters		: nMsgSize --- the message size
* Output Parameters	   : None.
* Return value			: Id of message header block or NULL for failure.
* Description			 : Allocate a message header: header+data. message
*						   length, message content is following message header,
*						   the content can be static buffer or dynamic buffer.
* Modification			:
******************************************************************************/
extern WS_MSG_ID WS_msgAlloc(U32 nMsgSize);

/******************************************************************************
* Function Name		   : WS_msgFree
* Input Parameters		: pMsg --- Id of Message header
* Output Parameters	   : None.
* Return value			: WS_ROK or WS_RERROR to return sucessful or failure.
* Description			 : Free a message header and its content.
* Modification			:
******************************************************************************/
extern WS_STATUS WS_msgFree(WS_MSG_ID pMsg);

/******************************************************************************
* Function Name		   : WS_msgSend
* Input Parameters		: pMsg --- Id of Message header
*						   destTaskId --- Destination task Id
* Output Parameters	   : None.
* Return value			: WS_ROK or WS_RERROR to return sucessful or failure.
* Description			 : Send a Task to another task. The task maybe within
*						   same host. If send failed, use WS_msgFree(pMsg).
* Modification			:
******************************************************************************/
extern WS_STATUS WS_msgSend(WS_MSG_ID pMsg, WS_TASK_ID destTaskId);

/******************************************************************************
* Function Name		   : WS_msgReceive
* Input Parameters		: taskId - Receiving task Id
*						   ppMsg  - ptr to WS_MSG_ID for return
							nTimeout - Timeout value when receiving
								WS_WAIT_ROREVER:  wait forever if no message
								WS_NO_WAIT:	   return immediately if no msg
								....
* Output Parameters	   : None.
* Return value			: WS_ROK or WS_RERROR to return sucessful or failure.
* Description			 : Receive message from taskId, blocked while none
*						   message User should call WS_msgFree(pMsg) after
*						   getting the message.
* Modification			:
******************************************************************************/
extern WS_STATUS WS_msgReceive(WS_TASK_ID taskId, WS_MSG_ID *ppMsg, U32 nTimeout);


/*=============================================================================
 *						   Task Management
 *=============================================================================
 */
#define WS_TIMER_DEMO  		1		/* Timer demo task id */

/******************************************************************************
* Function Name	  : WS_taskCreate
* Input Parameters   : taskId --- Task Id, the task id is unified lay out in
*					  whole system. 0 <= taskId < WS_MAX_TASKS
*					  taskName --- Name of Task
*					  pEntryFunc --- Task Entry function( should not be NULL )
*					  *pUsrParam --- User parameter
*					  nStackSize --- Stack size of task
*					  nPriority --- Task priority
* Output Parameters  : None.
* Return value	   : WS_ROK or WS_RERROR to return sucessful or failure.
* Description		: It will create a message driven task.
* Modification	   :
******************************************************************************/
extern WS_STATUS WS_taskCreate(
						WS_TASK_ID		taskId,
						S8			  *taskName,
						WS_TASK_ENTRY_FUNC  pEntryFunc,
						void			*pUsrParam,
						S32			 nStackSize,
						U32			 bPriority
						);

/******************************************************************************
* Function Name	  : WS_taskCreateExt
* Input Parameters   : taskName --- Name of Task
*					  pEntryFunc --- Task Entry function( should not be NULL )
*					  *pUsrParam --- User parameter
*					  nStackSize --- Stack size of task
*					  nPriority --- Task priority
* Output Parameters  : None.
* Return value	   : Create task id or MOD_TASK_INVALID_ID for failed.
* Description		: It will create a message driven dynamitic task .
* Modification	   :
******************************************************************************/
WS_TASK_ID WS_taskCreateExt(
						S8			  *taskName,
						WS_TASK_ENTRY_FUNC  pEntryFunc,
						void			*pUsrParam,
						S32			 nStackSize,
						U32			 bPriority
						);

/******************************************************************************
* Function Name	  : WS_taskSpawn
* Input Parameters   : taskId --- Task Id, the task id is unified lay out in
*					  whole system. 0 <= taskId < WS_MAX_TASKS
*					  taskName --- Name of Task
*					  pEntryFunc --- Task Entry function( should not be NULL )
*					  *pUsrParam --- User parameter
*					  nStackSize --- Stack size of task
*					  nPriority --- Task priority
* Output Parameters  : None.
* Return value	   : WS_ROK or WS_RERROR to return sucessful or failure.
* Description		: The API spawns a task. The difference with WS_taskCreate
*					  is that the task manages its messages by itself instead
*					  of common task handler.
* Modification	   :
******************************************************************************/
extern WS_STATUS WS_taskSpawn(
						WS_TASK_ID		   taskId,
						S8				  *taskName,
						WS_SPAWN_ROUTINE	pEntryFunc,
						void				*pUsrParam,
						U32				 nStackSize,
						U8				  bPriority
						);

/******************************************************************************
* Function Name	  : WS_taskSpawnExt
* Input Parameters   : taskName --- Name of Task
*					  pEntryFunc --- Task Entry function( should not be NULL )
*					  *pUsrParam --- User parameter
*					  nStackSize --- Stack size of task
*					  nPriority --- Task priority
* Output Parameters  : None.
* Return value	   : Spawn task id or WS_MOD_INVALID_ID for failed.
* Description		: It will spawn a normal dynamitic task .
* Modification	   :
******************************************************************************/
extern WS_TASK_ID WS_taskSpawnExt(
						S8				  *taskName,
						WS_SPAWN_ROUTINE	pEntryFunc,
						void				*pUsrParam,
						U32				 nStackSize,
						U8				  bPriority
						);

/******************************************************************************
* Function Name	  : WS_taskSleep
* Input Parameters   : nSleepTime -- sleep time of current task in ms.
* Output Parameters  : None.
* Return value	   : WS_ROK or WS_RERROR to return sucessful or failure.
* Description		: Suspend current task in a interval, the task will resume
*					  automatically while time out.
* Modification	   :
******************************************************************************/
extern WS_STATUS WS_taskSleep(U32 nSleepTime);

/******************************************************************************
* Function Name	  : WS_taskSuspend
* Input Parameters   : dTaskId -- Task Id.
* Output Parameters  : None.
* Return value	   : WS_ROK or WS_RERROR to return sucessful or failure.
* Description		: Suspend a task, please note here the 0 is an invalid
*					  parameter, to suspend current task, must use
*					  WS_taskSuspend(WS_taskIdSelf());
* Modification	   :
******************************************************************************/
extern WS_STATUS WS_taskSuspend(WS_TASK_ID dTaskId);

/******************************************************************************
* Function Name	  : WS_taskResume
* Input Parameters   : dTaskId -- Task Id.
* Output Parameters  : None.
* Return value	   : WS_ROK or WS_RERROR to return sucessful or failure.
* Description		: Resume a task, please note, the task Id must not be 0,
*					  or current task Id, because it is imposible to run this
*					  function in a suspended task.
* Modification	   :
******************************************************************************/
extern WS_STATUS WS_taskResume(WS_TASK_ID dTaskId);

/******************************************************************************
* Function Name	  : WS_taskExit
* Input Parameters   : dTaskId -- Task Id.
* Output Parameters  : None.
* Return value	   : WS_ROK or WS_RERROR to return sucessful or failure.
* Description		: Delete a task.
* Modification	   :
******************************************************************************/
extern WS_STATUS WS_taskExit(WS_TASK_ID dTaskId);

/******************************************************************************
* Function Name	  : WS_taskDelete
* Input Parameters   : dTaskId -- Task Id.
* Output Parameters  : None.
* Return value	   : WS_ROK or WS_RERROR to return sucessful or failure.
* Description		: Delete a task. The same with WS_taskExit.
* Modification	   :
******************************************************************************/
extern WS_STATUS WS_taskDelete(WS_TASK_ID dTaskId);

/******************************************************************************
* Function Name	  : WS_taskIdSelf
* Input Parameters   : None.
* Output Parameters  : None.
* Return value	   : Current task id.
* Description		: Get the Id of current running task.
* Modification	   :
******************************************************************************/
extern WS_TASK_ID WS_taskIdSelf(void);


/*=============================================================================
 *						   Timer Management
 *=============================================================================
 */
typedef enum SSI_TMR_UNIT_ENUM
{
	SSI_UNIT_100MS  = 0x00,
	SSI_UNIT_1S	 = 0x01
} SSI_TMR_UNIT;	  /* the timer unit */

/* timer type definition */
#define SSI_TIMERTP_CYC		 0x01   /* cycle timer */
#define SSI_TIMERTP_OUTREL	  0x02   /* once timer,after timeout,delete */
#define SSI_TIMERTP_OUTSTDBY	0x04   /* once timer,after timeout,standby */
#define SSI_NULL_TIMER		  0xffffffff

/******************************************************************************
* Function Name	  : WS_timerCreate
* Input Parameters   : pFun --- timer function
*					   pUserInfo --- user info
*					   nUserInfo --- user info length
*					   opt -- timer type
* Output Parameters  : None.
* Return value	   : timer id or NULL for failture
* Description		: Create a SSI timer.
* Modification	   :
******************************************************************************/
/*extern WS_TIMER_ID WS_timerCreate(S32 nTimeLen, S8 unit, S8 opt,
				WS_TIMER_FUNC pFun, void* pUserInfo, S32 nUserInfo);*/

extern WS_TIMER_ID WS_timerCreate(WS_TIMER_FUNC pFun, void* pUserInfo, U32 nInfoLen, U32 opt);


/******************************************************************************
* Function Name	  : WS_timerDelete
* Input Parameters   : nTimerID --- timer id
* Output Parameters  : None.
* Return value	   : WS_OK or WS_ERROR
* Description		: Delete a SSI timer.
* Modification	   :
******************************************************************************/
extern WS_STATUS WS_timerDelete(WS_TIMER_ID nTimerID);

/******************************************************************************
* Function Name	  : WS_timerStart
* Input Parameters   : nTimerID --- timer id
*					   nTimeLen --- time length
*					   unit --- time length unit, SSI_UNIT_100MS or SSI_UNIT_1S
* Output Parameters  : None.
* Return value	   : WS_OK or WS_ERROR
* Description		: Start a timer. TIMER_ACTV, TIMER_STDBY, TIMER_IDLE,
*					   TIMER_PAUSED -> TIMER_ACTV
* Modification	   :
******************************************************************************/
extern WS_STATUS WS_timerStart(WS_TIMER_ID nTimerID, S8 uint, S32 nTimeLen);

/******************************************************************************
* Function Name	  : WS_timerStart
* Input Parameters   : nTimerID --- timer id
* Output Parameters  : None.
* Return value	   : WS_OK or WS_ERROR
* Description		: Stop a timer. A stopped timer can only be activated by
*					   WS_timerStart. TIMER_ACTV, TIMER_STDBY, IMER_PAUSED,
*					   TIMER_IDLE -> TIMER_IDLE
* Modification	   :
******************************************************************************/
extern WS_STATUS WS_timerStop(WS_TIMER_ID nTimerID);


/*=============================================================================
 *						   System Time
 *=============================================================================
 */
/******************************************************************************
* Function Name		   : WS_tickGet
* Input Parameters		: void
* Output Parameters	   : NULL
* Return value			: S32	--- return the ticks number the machine has run
* Description			 : this will get the tick number of this machine has run
* Modification			:
******************************************************************************/
extern U32 WS_tickGet( void );


/******************************************************************************
* Function Name		   : WS_tickPerSecondGet
* Input Parameters		: void
* Output Parameters	   : NULL
* Return value			: S32	--- ticks per second
* Description			 : this will get the ticks per second
* Modification			:
******************************************************************************/
extern S32 WS_tickPerSecondGet( void );


/*=============================================================================
 *							Socket
 *=============================================================================
 */

#if defined(WIN32)


#elif defined(LINUX) || defined(UNIX)

	#include <sys/socket.h>
	#include <sys/ioctl.h>
	#include <netinet/in.h>

#endif

#define WS_INVALID_SOCKET   (-1)

/*
 * Types
 */
#define WS_SOCK_STREAM	 SOCK_STREAM			/* stream socket */
#define WS_SOCK_DGRAM	  SOCK_DGRAM			 /* datagram socket */
#define WS_SOCK_RAW		SOCK_RAW			   /* raw-protocol interface */
#define WS_SOCK_RDM		SOCK_RDM			   /* reliably-delivered message */
#define WS_SOCK_SEQPACKET  SOCK_SEQPACKET		 /* sequenced packet stream */

/*
 * Commands for ioctlsocket(),  taken from the BSD file fcntl.h.
 */
/* set/clear non-blocking i/o */
#define WS_FIONBIO		 FIONBIO

#define WS_INADDR_ANY	  INADDR_ANY

#define WS_AF_INET		 AF_INET /* internetwork: UDP, TCP, etc. */

/*
 * Level number for (get/set)sockopt() to apply to socket itself.
 */

#define WS_SOL_SOCKET	  SOL_SOCKET		  /* options for socket level */
#define WS_IPPROTO_IP	  IPPROTO_IP

/*
 * Additional options.
 */
#define WS_SO_SNDBUF	   SO_SNDBUF		  /* send buffer size */
#define WS_SO_RCVBUF	   SO_RCVBUF		  /* receive buffer size */
#define WS_SO_REUSEADDR	SO_REUSEADDR	   /* allow local address reuse */

#define WS_IP_ADD_MEMBERSHIP  IP_ADD_MEMBERSHIP
#define WS_IP_DROP_MEMBERSHIP IP_DROP_MEMBERSHIP



#ifdef WIN32
typedef unsigned int	WS_SOCKFD;
typedef char *  WS_CADDR;

typedef struct ws_in_addr {
		union {
				struct { U8 s_b1,s_b2,s_b3,s_b4; } S_un_b;
				struct { U16 s_w1,s_w2; } S_un_w;
				U32 S_addr;
		} S_un;
#define s_addr  S_un.S_addr	   /* can be used for most tcp & ip code */
#define s_host  S_un.S_un_b.s_b2		/* host on imp */
#define s_net   S_un.S_un_b.s_b1	 /* network */
#define s_imp   S_un.S_un_w.s_w2	 /* imp */
#define s_impno S_un.S_un_b.s_b4	/* imp # */
#define s_lh	S_un.S_un_b.s_b3
}WS_IN_ADDR;

#define WS_FD_SETSIZE 64
typedef struct ws_fd_set {
		U32 fd_count;			   /* how many are SET? */
		WS_SOCKFD  fd_array[WS_FD_SETSIZE];   /* an array of SOCKETs */
} WS_fd_set;

/* Removes the descriptor fd from set. */
#define WS_FD_CLR(fd, set) do { \
	unsigned int __i; \
	for (__i = 0; __i < ((WS_fd_set *)(set))->fd_count ; __i++) { \
		if (((WS_fd_set *)(set))->fd_array[__i] == fd) { \
			while (__i < ((WS_fd_set *)(set))->fd_count-1) { \
				((WS_fd_set *)(set))->fd_array[__i] = \
					((WS_fd_set *)(set))->fd_array[__i+1]; \
				__i++; \
			} \
			((WS_fd_set *)(set))->fd_count--; \
			break; \
		} \
	} \
} while(0)

/* Adds descriptor fd to set. */
#define WS_FD_SET(fd, set) do { \
	unsigned int __i; \
	for (__i = 0; __i < ((WS_fd_set *)(set))->fd_count; __i++) { \
		if (((WS_fd_set *)(set))->fd_array[__i] == (fd)) { \
			break; \
		} \
	} \
	if (__i == ((WS_fd_set *)(set))->fd_count) { \
		if (((WS_fd_set *)(set))->fd_count < WS_FD_SETSIZE) { \
			((WS_fd_set *)(set))->fd_array[__i] = (fd); \
			((WS_fd_set *)(set))->fd_count++; \
		} \
	} \
} while(0)

/* Initializes the set to the null set. */
#define WS_FD_ZERO(set) (((WS_fd_set *)(set))->fd_count=0)

/* Nonzero if fd is a member of the set. Otherwise, zero. */
extern int _WS_FDIsSet(WS_SOCKFD fd, WS_fd_set *set);
#define WS_FD_ISSET(fd, set) _WS_FDIsSet((WS_SOCKFD)(fd), (WS_fd_set  *)(set))

#endif

#ifdef LINUX
typedef int		WS_SOCKFD;
typedef char *  WS_CADDR;

typedef struct ws_in_addr
{
	U32 s_addr;
}WS_IN_ADDR;

#define WS_FD_SETSIZE 1024
#define WS_FDSET_LONGS (1024/(8*sizeof(unsigned long)))
typedef struct ws_fd_set
{
	unsigned long fds_bits [WS_FDSET_LONGS];
}WS_fd_set;

/* Removes the descriptor fd from set. */
extern void WS_FD_CLR(WS_SOCKFD fd, WS_fd_set *set);

/* Adds descriptor fd to set. */
extern void WS_FD_SET(WS_SOCKFD fd, WS_fd_set *set);

/* Initializes the set to the null set. */
extern void WS_FD_ZERO(WS_fd_set *set);

/* Nonzero if fd is a member of the set. Otherwise, zero. */
extern int WS_FD_ISSET(WS_SOCKFD fd, WS_fd_set *set);
#endif


typedef struct {
	WS_IN_ADDR imr_multiaddr;   /* IP multicast address of group */
	WS_IN_ADDR imr_interface;   /* local IP address of interface */
} WS_ip_mreq;

/*
 * Structure used in select() call, taken from the BSD file sys/time.h.
 */
struct WS_timeval {
		long	tv_sec;		 /* seconds */
		long	tv_usec;		/* and microseconds */
};

typedef struct  WS_SOCKADDR {
		U16 sa_family;			  /* address family */
		S8	sa_data[14];			/* up to 14 bytes of direct address */
}  WS_SOCKADDR;

typedef struct  WS_SOCKADDR_IN {
		S16   sin_family;
		U16 sin_port;
		WS_IN_ADDR sin_addr;
		S8	sin_zero[8];
} WS_SOCKADDR_IN;

//socket error code
#if defined(LINUX) || defined(UNIX)

	#include <errno.h>

	#define WS_SOCK_EFAULT				   EFAULT
	#define WS_SOCK_EINVAL					EINVAL
	#define WS_SOCK_EMFILE					   EMFILE
	#define WS_SOCK_EWOULDBLOCK		EWOULDBLOCK
	#define WS_SOCK_EINPROGRESS		 EINPROGRESS
	#define WS_SOCK_EALREADY				EALREADY
	#define WS_SOCK_ENOTSOCK				ENOTSOCK
	#define WS_SOCK_EDESTADDRREQ		 EDESTADDRREQ
	#define WS_SOCK_EMSGSIZE				EMSGSIZE
	#define WS_SOCK_EADDRINUSE		 EADDRINUSE
	#define WS_SOCK_EADDRNOTAVAIL	 EADDRNOTAVAIL
	#define WS_SOCK_ENETDOWN				ENETDOWN
	#define WS_SOCK_ENETUNREACH		 ENETUNREACH
	#define WS_ENETRESET					ENETRESET
	#define WS_ECONNABORTED	 		 ECONNABORTED
	#define WS_ECONNRESET					ECONNRESET
	#define WS_ENOBUFS						   ENOBUFS
	#define WS_EISCONN						   EISCONN
	#define WS_ENOTCONN					ENOTCONN
	#define WS_ESHUTDOWN					ESHUTDOWN

	// just available for unix or linux
	#define WS_SOCK_EINTR					EINTR   /* Interrupted system call */
	#define WS_SOCK_EPIPE					EPIPE   /* Broken pipe */
#endif

#if defined(WIN32)

	#include <WinError.h>

	#define WS_SOCK_EFAULT						  WSAEFAULT
	#define WS_SOCK_EINVAL						  WSAEINVAL
	#define WS_SOCK_EMFILE					   WSAEMFILE
	#define WS_SOCK_EWOULDBLOCK 			WSAEWOULDBLOCK
	#define WS_SOCK_EINPROGRESS 	 		WSAEINPROGRESS
	#define WS_SOCK_EALREADY				WSAEALREADY
	#define WS_SOCK_ENOTSOCK				WSAENOTSOCK
	#define WS_SOCK_EDESTADDRREQ			   WSAEDESTADDRREQ
	#define WS_SOCK_EMSGSIZE				WSAEMSGSIZE
	#define WS_SOCK_EADDRINUSE			WSAEADDRINUSE
	#define WS_SOCK_EADDRNOTAVAIL		WSAEADDRNOTAVAIL
	#define WS_SOCK_ENETDOWN				WSAENETDOWN
	#define WS_SOCK_ENETUNREACH 			   WSAENETUNREACH
	#define WS_ENETRESET					WSAENETRESET
	#define WS_ECONNABORTED 				WSAECONNABORTED
	#define WS_ECONNRESET					   WSAECONNRESET
	#define WS_ENOBUFS						   WSAENOBUFS
	#define WS_EISCONN						WSAEISCONN
	#define WS_ENOTCONN 					WSAENOTCONN
	#define WS_ESHUTDOWN					   WSAESHUTDOWN

	// fake define, just available for unix or linux
	#define WS_SOCK_EINTR					(-1)   /* Interrupted system call */
	#define WS_SOCK_EPIPE					(-1)   /* Broken pipe */
#endif


/******************************************************************************
* Function Name	  : WS_socket_init
* Input Parameters   : None.
* Output Parameters  : None.
* Return value	   : WS_OK or WS_ERROR.
* Description		: The Windows Sockets WSAStartup function initiates use of
*					  WS2_32.DLL by a process. (NOTE this function is for
*					  windows, default version 2.2)
* Modification	   :
******************************************************************************/
WS_STATUS WS_socket_init(void);

/******************************************************************************
* Function Name	  : WS_socket
* Input Parameters   : family --- address family (for example, AF_INET)
*					  type	--- SOCK_STREAM, SOCK_DGRAM, or SOCK_RAW(win32
*							   socket not support)
*					  protocol	--- A particular protocol to be used with the
*								   socket that is specific to the indicated
*								   address family.(usually 0)
* Output Parameters  : None.
* Return value	   : A socket descriptor, or WS_ERROR.
* Description		: This routine opens a socket and returns a socket descriptor
* Modification	   :
******************************************************************************/
WS_SOCKFD WS_socket(int family,int type,int protocol);

/*-------------------------------------------------------------------------------
FUNVCTION :   select(int width,fd_set *pReadFds, fd_set *pWriteFds,fd_set *pExceptFds, struct timeval *pTimeOut)
DESCRIPTION:  This routine permits a task to pend until one of a set of file descriptors becomes ready. Three parameters -- pReadFds, pWriteFds, and pExceptFds -- point to file descriptor sets in which each bit corresponds to a particular file descriptor. Bits set in the read file descriptor set (pReadFds) will cause select( ) to pend until data is available on any of the corresponding file descriptors, while bits set in the write file descriptor set (pWriteFds) will cause select( ) to pend until any of the corresponding file descriptors become writable. (The pExceptFds parameter is currently unused, but is provided for UNIX call compatibility.)
Arguments  :  width			number of bits to examine from 0 (this Argument will be ignored in win32)
			  pReadFds		read fds
			  pWriteFds		write fds
			  pExceptFds	exception fds (unsupported)
			  pTimeOut		max time to wait, NULL = forever
Return	 :  The number of file descriptors with activity, 0 if timed out, or ERROR if an error occurred .
-------------------------------------------------------------------------------*/
int WS_select(int width,WS_fd_set *pReadFds, WS_fd_set *pWriteFds,WS_fd_set *pExceptFds, struct WS_timeval *pTimeOut);



/*-------------------------------------------------------------------------------
FUNVCTION :	WS_bind(WS_SOCKFD s,WS_SOCKADDR * addr,int addrlen)
DESCRIPTION:  This routine associates a network address (also referred to as its "name") with a
					   specified socket so that other processes can connect or send to it.
Arguments  :  (IN) s		   socket descriptor
					 (IN) addr  	network address to be bound
			(IN) addrlen	length of address
Return	 :  WS_OK or WS_ERROR if there is an invalid socket, the address is either unavailable
				 or in use, or the socket is already bound.
-------------------------------------------------------------------------------*/
WS_STATUS WS_bind(WS_SOCKFD s,WS_SOCKADDR * addr,int addrlen);

/*-------------------------------------------------------------------------------
FUNVCTION :   WS_listen(WS_SOCKFD s,int backlog)
DESCRIPTION:  This routine enables connections to a socket. It also specifies the maximum
						number of unaccepted connections that can be pending at one time (backlog).
Arguments  :  (IN) s		socket descriptor
					 (IN) backlog	number of connections to queue(for 0 has not the same mean in all platform)
Return	 :  WS_OK or WS_ERROR if the socket is invalid or unable to listen.
-------------------------------------------------------------------------------*/
WS_STATUS WS_listen(WS_SOCKFD s,int backlog);

/*-------------------------------------------------------------------------------
FUNVCTION :   WS_accept(WS_SOCKFD s,WS_SOCKADDR *addr,int  *addrlen)
DESCRIPTION:  This routine accepts a connection on a socket(listenning), and returns a new
					   socket(connection) created for the connection.
Arguments  :  (IN) s		socket descriptor
					 (IN) addr		address
			(IN) addrlen	address length
Return	 :  A socket descriptor, or WS_ERROR if the call fails.
-------------------------------------------------------------------------------*/
WS_SOCKFD WS_accept(WS_SOCKFD s,WS_SOCKADDR *addr,int  *addrlen);

/*-------------------------------------------------------------------------------
FUNVCTION :   WS_connect(WS_SOCKFD s,WS_SOCKADDR * addr,int addrlen)
DESCRIPTION:  If s is a socket of type SOCK_STREAM, this routine establishes a virtual circuit
						between s and another socket specified by name. If s is of type SOCK_DGRAM,
						it permanently specifies the peer to which messages are sent. If s is of type
						SOCK_RAW, it specifies the raw socket upon which data is to be sent and received.
Arguments  :  (IN) s		socket descriptor
					 (IN) addr		address of socket to connect
			(IN) addrlen	length of address, in bytes
Return	 :  WS_OK or WS_ERROR if the call fails
-------------------------------------------------------------------------------*/
WS_STATUS WS_connect(WS_SOCKFD s,WS_SOCKADDR * addr,int addrlen);

/*-------------------------------------------------------------------------------
FUNVCTION :   WS_connectWithTimeout(WS_SOCKFD s,
														   WS_SOCKADDR * addr,
														   int addrlen,
														   struct timeval *  timeVal)
DESCRIPTION:  This routine basically the same as connect( ), except that it lets users specify how
						long to keep trying to make the new connection. If the timeVal is a NULL pointer,
						this routine acts exactly like connect().
Arguments  :  (IN) s		socket descriptor
					 (IN) addr		addr of socket to connect
			(IN) addrlen	length of name, in bytes
			(IN) timeVal	time-out value
Return	 :  WS_OK or WS_ERROR if the call fails
-------------------------------------------------------------------------------*/
WS_STATUS WS_connectWithTimeout(WS_SOCKFD s,WS_SOCKADDR * addr,int addrlen,struct timeval *  timeVal);

/*-------------------------------------------------------------------------------
FUNVCTION :   WS_sendto(WS_SOCKFD s,WS_CADDR buf,int bufLen,int flags,WS_SOCKADDR * to,int tolen)
DESCRIPTION:  This routine sends a message to the datagram socket named by to. The socket s is
						received by the receiver as the sending socket.
Arguments  :  (IN) s  		socket descriptor
					 (IN) buf		pointer to data buffer
			(IN) bufLen	 length of buffer
			(IN) flags		flags to underlying protocols
					 (IN) to			recipient's address
			(IN) tolen		length of to sockaddr
Return	 :  The number of bytes sent, or WS_ERROR if the call fails.
-------------------------------------------------------------------------------*/
int WS_sendto(WS_SOCKFD s,WS_CADDR buf,int bufLen,int flags,WS_SOCKADDR * to,int tolen);

/*-------------------------------------------------------------------------------
FUNVCTION :   WS_send(WS_SOCKFD s,WS_CADDR buf,int bufLen, int flags)
DESCRIPTION:  This routine transmits data to a previously established connection-based (stream)
					   socket.
Arguments  :  (IN) s  		socket descriptor
					 (IN) buf		pointer to data buffer
			(IN) bufLen	 length of buffer
			(IN) flags		flags to underlying protocols
Return	 :  The number of bytes sent, or WS_ERROR if the call fails.
-------------------------------------------------------------------------------*/
int WS_send(WS_SOCKFD s,WS_CADDR buf,int bufLen, int flags);

/*-------------------------------------------------------------------------------
FUNVCTION :   WS_recvfrom(WS_SOCKFD s,
										   char *buf,
										   int bufLen,
										   int flags,
										   WS_SOCKADDR *from,
										   int *pFromLen)
DESCRIPTION:  This routine receives a message from a datagram socket regardless of whether it is
					   connected. If from is non-zero, the address of the sender's socket is copied to it. The
					   value-result parameter pFromLen should be initialized to the size of the from buffer.
Arguments  :  (IN) s  		  socket to receive from
					 (OUT) buf	  pointer to data buffer
			(IN) bufLen	   length of buffer
			(IN) flags		  flags to underlying protocols
			(IN) from		  where to copy sender's addr
			(IN) pFromLen	  length of from socket address
Return	 :  The number of number of bytes received, or WS_ERROR if the call fails
-------------------------------------------------------------------------------*/
int WS_recvfrom(WS_SOCKFD s,char *buf,int bufLen,int flags,WS_SOCKADDR *from,int *pFromLen);

/*-------------------------------------------------------------------------------
FUNVCTION :   WS_recv(WS_SOCKFD s,char * buf,int bufLen,int flags)
DESCRIPTION:  This routine receives data from a connection-based (stream) socket.
Arguments  :  (IN) s  		socket to receive from
					 (OUT) buf		pointer to data buffer
			(IN) bufLen	 length of buffer
			(IN) flags		flags to underlying protocols
Return	 :  The number of number of bytes received, or WS_ERROR if the call fails
-------------------------------------------------------------------------------*/
int WS_recv(WS_SOCKFD s,char * buf,int bufLen,int flags);

/*-------------------------------------------------------------------------------
FUNVCTION :   WS_setsockopt(WS_SOCKFD s,int level,int optname,char *optval,int optlen)
DESCRIPTION:  This routine sets the options associated with a socket. To manipulate options at
						the "socket" level, level should be SOL_SOCKET. Any other levels should use the
						appropriate protocol number.
Arguments  :  (IN) s  		   target socket
					 (IN) level		   protocol level of option
			(IN) optname	 option name
			(IN) optval		 pointer to option value
			(IN) optlen		option length
Return	 :  WS_OK, or WS_ERROR if there is an invalid socket, an unknown option, an option length
				  greater than MLEN, insufficient mbufs, or the call is unable to set the specified option.
-------------------------------------------------------------------------------*/
WS_STATUS WS_setsockopt(WS_SOCKFD s,int level,int optname,char *optval,int optlen);

/*-------------------------------------------------------------------------------
FUNVCTION :   WS_getsockopt(WS_SOCKFD s,int level,int optname,char *optval,int *optlen)
DESCRIPTION:  This routine returns relevant option values associated with a socket. To manipulate
					   options at the "socket" level, level should be SOL_SOCKET. Any other levels should
					   use the appropriate protocol number. The parameter optlen should be initialized to
					   indicate the amount of space referenced by optval.
Arguments  :  (IN) s  		  target socket
					 (IN) level		  protocol level of option
			(IN) optname	option name
			(OUT) optval		where to put option
			(OUT) optlen	   where to put option length
Return	 :  WS_OK, or WS_ERROR if there is an invalid socket, an unknown option, an option length
				  greater than MLEN, insufficient mbufs, or the call is unable to set the specified option.
-------------------------------------------------------------------------------*/
WS_STATUS WS_getsockopt(WS_SOCKFD s,int level,int optname,char *optval,int *optlen);

/*-------------------------------------------------------------------------------
FUNVCTION :   WS_getsockname(WS_SOCKFD s,WS_SOCKADDR *addr,  int *addrlen)
DESCRIPTION:  This routine returns the current name of the specified socket.
Arguments  :  (IN) s  			  target socket
					 (OUT) addr		   where to return address
			(IN OUT) addrlen   space available in address, later filled in  actual address size
Return	 :  WS_OK,or WS_ERROR
-------------------------------------------------------------------------------*/
WS_STATUS WS_getsockname(WS_SOCKFD s,WS_SOCKADDR *addr,  int *addrlen);

/*-------------------------------------------------------------------------------
FUNVCTION :   WS_getpeername(WS_SOCKFD s,WS_SOCKADDR *name,int *namelen)
DESCRIPTION:  This routine gets the name of the peer connected to socket s
Arguments  :  (IN) s  				socket descriptor
					 (OUT) name			where to put name
			(IN OUT) namelen	 space available in name, later filled in actual name size
Return	 :  WS_OK, or WS_ERROR if the socket is invalid or not connected.
-------------------------------------------------------------------------------*/
WS_STATUS WS_getpeername(WS_SOCKFD s,WS_SOCKADDR *name,int *namelen);

/*-------------------------------------------------------------------------------
FUNVCTION :   WS_shutdown(WS_SOCKFD s,int how)
DESCRIPTION:  This routine shuts down all, or part, of a connection-based socket s. If the value of
						how is 0, receives are disallowed. If how is 1, sends are disallowed. If how is 2, both
						sends and receives are disallowed
Arguments  :  (IN) s  		socket to shut down
					 (IN) how		0 = receives disallowed 1 = sends
						   disallowed 2 = sends and disallowed (in win32 SD_RECEIVE SD_SEND
						   SD_BOTH)
Return	 :  WS_OK, or WS_ERROR if the socket is invalid or not connected.
-------------------------------------------------------------------------------*/
WS_STATUS WS_shutdown(WS_SOCKFD s,int how);

/*-------------------------------------------------------------------------------
FUNVCTION :   WS_close(WS_SOCKFD fd)
DESCRIPTION:  This routine closes the specified file and frees the file descriptor. It calls the device
					   driver to do the work.
Arguments  :  (IN) fd  		file descriptor to close
Return	 :  WS_OK, or WS_ERROR if the file descriptor is invalid.
-------------------------------------------------------------------------------*/
WS_STATUS WS_close(WS_SOCKFD fd);

/*
*hostLib
*/
/*-------------------------------------------------------------------------------
FUNVCTION :   WS_hostAdd(char * name,char * addr)
DESCRIPTION:  This routine adds a host name to the local host table.
Arguments  :	(IN) name	host name
			   (IN) addr 	host addr in standard Internet format
Return	   :  WS_OK, or WS_ERROR if add address error.
	-------------------------------------------------------------------------------*/
WS_STATUS WS_hostAdd(char * name,char * addr);

/*-------------------------------------------------------------------------------
FUNVCTION :   WS_hostDelete(char * name,char * addr)
DESCRIPTION:  This routine deletes a host name from the local host table. If name is a host name,
						the host entry is deleted. If name is a host name alias, the alias is deleted.
Arguments  :  (IN) name	host name or alias
			(IN) addr		host addr in standard Internet format
Return	 :  WS_OK, or WS_ERROR if delete error.
-------------------------------------------------------------------------------*/
WS_STATUS WS_hostDelete(char * name,char * addr);

/*-------------------------------------------------------------------------------
FUNVCTION :   WS_hostGetByName(char * hostname)
DESCRIPTION:  This routine returns the Internet address of a host that has been added to the host table by hostAdd( ). If the DNS resolver library resolvLib has been configured in the vxWorks image, a query for the host IP address is sent to the DNS server, if the name was not found in the local host table
Arguments  :  hostname		name of host
Return	 :  The Internet address (as an integer in network byte order), or SSI_ERROR if the host is unknown.
-------------------------------------------------------------------------------*/
WS_STATUS WS_hostGetByName(char * hostname);

/*-------------------------------------------------------------------------------
FUNVCTION :   WS_hostGetByAddr(int addr,char *name)
DESCRIPTION:  This routine finds the host name by its Internet address and copies it to name. The buffer name should be preallocated with (MAXHOSTNAMELEN + 1) bytes of memory and is NULL-terminated unless insufficient space is provided. If the DNS resolver library resolvLib has been configured in the vxWorks image, a query for the host name is sent to the DNS server, if the name was not found in the local host table.
Arguments  :  addr		inet address of host
			  name	  buffer to hold name
Return	 :  SSI_OK, or SSI_ERROR if buffer is invalid or the host is unknown.
-------------------------------------------------------------------------------*/
WS_STATUS WS_hostGetByAddr(int addr,char *name);

/*-------------------------------------------------------------------------------
FUNVCTION :   WS_sethostname(char * name,int nameLen)
DESCRIPTION:  This routine sets the target machine's symbolic name, which can be used for identification.
Arguments  :  addr		machine name
			  nameLen	  length of name
Return	 :  SSI_OK or SSI_ERROR.
-------------------------------------------------------------------------------*/
int WS_sethostname(char * name,int nameLen);

/*-------------------------------------------------------------------------------
FUNVCTION :   WS_sethostname(char * name,int nameLen)
DESCRIPTION:  This routine sets the target machine's symbolic name, which can be used for identification.
Arguments  :  name		machine name
			  namelen   length of name
Return	 :  SSI_OK or SSI_ERROR.
-------------------------------------------------------------------------------*/
int WS_gethostname(char * name,int nameLen);

/*
inetLib
*/

/*-------------------------------------------------------------------------------
FUNVCTION :   WS_inet_addr(char * inetString)
DESCRIPTION:  convert a dot notation Internet address to a long integer
Arguments  :  inetString	string inet address
Return	 :  The Internet address, or SSI_ERROR.
-------------------------------------------------------------------------------*/
U32 WS_inet_addr(char * inetString);

/*-------------------------------------------------------------------------------
FUNVCTION :   WS_inet_lnaof(int inetAddress)
DESCRIPTION:  This routine returns the local network address portion of an Internet address. The routine handles class A, B, and C network number formats
Arguments  :  inetAddress	inet addr from which to extract local portion
Return	 :  The local address portion of inetAddress
-------------------------------------------------------------------------------*/
int WS_inet_lnaof(int inetAddress);

/*-------------------------------------------------------------------------------
FUNVCTION :   WS_inet_makeaddr_b(int netAddr,int hostAddr,WS_IN_ADDR * pInetAddr)
DESCRIPTION:  This routine constructs the Internet address from the network number and local host address. This routine is identical to the UNIX inet_makeaddr( ) routine except that you must provide a buffer for the resulting value.
Arguments  :  netAddr	network part of the inet address
			  hostAddr	host part of the inet address
			  pInetAddr	where to return the inet address
Return	 :  NONE
-------------------------------------------------------------------------------*/
void WS_inet_makeaddr_b(int netAddr,int hostAddr,WS_IN_ADDR * pInetAddr);

/*-------------------------------------------------------------------------------
FUNVCTION :   WS_inet_makeaddr(int netAddr,int hostAddr)
DESCRIPTION:  This routine constructs the Internet address from the network number and local host address. This routine is identical to the UNIX inet_makeaddr( ) routine except that you must provide a buffer for the resulting value.
Arguments  :  netAddr	network part of the address
			  hostAddr	host part of the address
Return	 :  NONE
-------------------------------------------------------------------------------*/
WS_IN_ADDR WS_inet_makeaddr(int netAddr,int hostAddr);

/*-------------------------------------------------------------------------------
FUNVCTION :   WS_inet_netof(WS_IN_ADDR inetAddress )
DESCRIPTION:  This routine extracts the network portion of an Internet address.
Arguments  :  inetAddress	inet address
Return	 :  The network portion of inetAddress.
-------------------------------------------------------------------------------*/
int WS_inet_netof(WS_IN_ADDR inetAddress );

/*-------------------------------------------------------------------------------
FUNVCTION :   WS_inet_netof_string(char * inetString,char * netString)
DESCRIPTION:  This routine extracts the network Internet address from a host Internet address (specified in dotted decimal notation). The routine handles class A, B, and C network addresses. The buffer netString should be INET_ADDR_LEN bytes long.
Arguments  :  inetString	inet addr to extract local portion from
			  netString		net inet address to return
Return	 :  NONE
-------------------------------------------------------------------------------*/
void WS_inet_netof_string(char * inetString,char * netString);

/*-------------------------------------------------------------------------------
FUNVCTION :   WS_inet_network(char * inetString)
DESCRIPTION:  This routine forms a network address from an ASCII string containing an Internet network number.
Arguments  :  inetString	string version of inet addr
Return	 :  The Internet address version of an ASCII string
-------------------------------------------------------------------------------*/
U32 WS_inet_network(char * inetString);

/*-------------------------------------------------------------------------------
FUNVCTION :   WS_inet_ntoa_b(WS_IN_ADDR inetAddress,char *pString)
DESCRIPTION:  This routine converts an Internet address in network format to dotted decimal notation.
Arguments  :  inetAddress	inet address
			  pString		where to return ASCII string
Return	 :  NONE
-------------------------------------------------------------------------------*/
void WS_inet_ntoa_b(WS_IN_ADDR inetAddress,char *pString);

/*-------------------------------------------------------------------------------
FUNVCTION :   WS_inet_ntoa_b(WS_IN_ADDR inetAddress,char *pString)
DESCRIPTION:  This routine converts an Internet address in network format to dotted decimal notation.
Arguments  :  inetAddress	inet address
Return	 :  A pointer to the string version of an Internet address
-------------------------------------------------------------------------------*/
char* WS_inet_ntoa(WS_IN_ADDR inetAddress);

/*-------------------------------------------------------------------------------
FUNVCTION :   WS_inet_aton(char *pString,WS_IN_ADDR * inetAddress)
DESCRIPTION:  This routine interprets an Internet address. All the network library routines call this routine to interpret entries in the data bases that are expected to be an address. The value returned is stored in network byte order in the structure provided.
Arguments  :  pString		string containing address, dot notation
			  inetAddress	struct in which to store address
Return	 :  SSI_OK, or SSI_ERROR.
-------------------------------------------------------------------------------*/
WS_STATUS WS_inet_aton(char *pString,WS_IN_ADDR * inetAddress);

/*-------------------------------------------------------------------------------
FUNVCTION :   WS_htons(U16 host16bitVal)
DESCRIPTION:  This routine converts a U16 from host to TCP/IP network byte order
Arguments  :  host16bitVal	A 16-bit number in host byte order
Return	 :  the value in TCP/IP's network byte order
-------------------------------------------------------------------------------*/
U16 WS_htons(U16 host16bitVal);

/*-------------------------------------------------------------------------------
FUNVCTION :   WS_htonl(U32 host32bitVal)
DESCRIPTION:  This routine converts a U32 from host to TCP/IP network byte order
Arguments  :  host32bitVal	A 32-bit number in host byte order
Return	 :  the value in TCP/IP's network byte order
-------------------------------------------------------------------------------*/
U32  WS_htonl(U32  host32bitVal);

/*-------------------------------------------------------------------------------
FUNVCTION :   WS_ntohs(U16 net16bitVal)
DESCRIPTION:  This routine converts a U16 from TCP/IP network byte order to host byte order
Arguments  :  net16bitVal	A 16-bit number in TCP/IP's network byte order
Return	 :  the value in host byte order
-------------------------------------------------------------------------------*/
U16 WS_ntohs(U16 net16bitVal);

/*-------------------------------------------------------------------------------
FUNVCTION :   WS_htonl(U32 host32bitVal)
DESCRIPTION:  This routine converts a U32 from TCP/IP network byte order to host byte order
Arguments  :  host32bitVal	A 32-bit number in TCP/IP's network byte order
Return	 :  the value in host byte order
-------------------------------------------------------------------------------*/
U32  WS_ntohl(U32  net32bitVal);


/*-------------------------------------------------------------------------------
FUNVCTION :   WS_ioctl(U32 fd, U32 request, void * arg)
DESCRIPTION:  This routine manipulates the underlying device parameters of special files.

Arguments  :  fd				  an open file descriptor
					 request		  a device-dependent request code
					 arg				an untyped pointer to memory

Return	 :  Usually,  on  success zero is returned.  A few ioctls use the return value as an output
				  parameter and return a nonnegative value on success.  On error, -1 is returned.
-------------------------------------------------------------------------------*/
U32  WS_ioctl(U32 fd, U32 request, void * arg);


/*-------------------------------------------------------------------------------
FUNVCTION :   WS_getSocketErr()
DESCRIPTION:  This routine get socket error No.

Arguments  :  none

Return	 : socket error No
-------------------------------------------------------------------------------*/
U32  WS_getSocketErr();


/*-------------------------------------------------------------------------------
FUNVCTION :   WS_getIpFromName()
DESCRIPTION:  Get IP address from WWW address.
Arguments  :  WWW address string
Return	 :  unsigned int in Network sequence 
-------------------------------------------------------------------------------*/
U32 WS_getIpFromName(const char* szName);
S32 WS_IsPubNetIp(U32 nIp);

/*=============================================================================
 *							Debug Shell
 *=============================================================================
 */
/******************************************************************************
* Function Name	  : WS_shellParser
* Input Parameters   : execFileName --- the application execute file name
* Output Parameters  : None.
* Return value	   : None.
* Description		: Start a debug shell. Only support WIN32 cosole app and
*					   Linux app.
* Modification	   :
******************************************************************************/
extern void WS_shellParser(char* execFileName);
extern WS_STATUS WS_shellExecStr(char* str);
extern WS_STATUS WS_initSymbTbl(char* execFileName);

/*=============================================================================
 * Desc: check and clear uniquity flag for only 1 instance
 *============================================================================*/
extern WS_STATUS CheckUniquityFlag(const char *name);
extern void ClearUniquityFlag(void);

/*=============================================================================
 * Fun: WS_shellConfigPathFile
 * Desc: concatenate config file path and name
 *===========================================================================*/
extern S8* WS_shellConfigPathFile(S8* filename);

/*=============================================================================
 * Fun: WS_ShellParseOptions
 * Desc: Parse app option
 *===========================================================================*/
extern WS_STATUS WS_shellParseOptions(S32 argc, S8* argv[]);

extern U32 WS_timeBootGet(void);

#ifndef NOT_USE_GUI
extern unsigned int WinDbgOut(char *fmt, ...);
#define PrintOut WinDbgOut
#else
#define PrintOut printf
#endif

#ifdef __cplusplus
}
#endif

#endif/*__VS_SSI_H__*/

