//////////////////////////////////////////////////
//Title    : wxc_timer.h
//Auhtor   : Liu Wei
//Desc     : WXC2 Public Timer Managemnet 
//Created  : 2007-04-27
//Revision :
//
//Revision :
//
//////////////////////////////////////////////////

#ifndef _PUB_TIMER_H_
#define _PUB_TIMER_H_

#include "public.h"
#include "includes.h"
#include "pub_base.h"
#include "pub_malloc.h"

///////////////////////////////////////////////////////////////////////////

typedef void ( *TimerCallBack ) ( u16 dSuitId, u16 dData );

#define TM_CLOCK_MAX (24*60*60*100)     //one day based on wxc2 base timer 10ms

typedef struct WXC_TIMER
{
    u32 dExpires;               //dExpires      : Timer expired number 
    u32 dSuitNumber;            //dSuitNumber   : Timer suit size of member
    TimerCallBack pFunc;        //pFunc         : Timer call back function
    u16 dData;                  //dData         : Pointer for parameter of callback
    void *pTimerNode;           //pTimerNode    : Reserved for timer Managemnet
}
WxcTimer;

///////////////////////////////////////////////////////////////////////////

typedef struct WXC_TIMER_NODE
{
    u32 *dClockArray;           //dClockArray   : Timer set member clock array
    WxcTimer *pTimer;
    struct WXC_TIMER_NODE *pNext;
    struct WXC_TIMER_NODE *pPrev;
}
WxcTimerNode;

///////////////////////////////////////////////////////////////////////////

#define MAX_TM_MODE      512

#define TIMER_NODE_SIZE  ( sizeof( WxcTimerNode ) + sizeof( WxcTimer ) )

#define CLOCK_SZIE       ( sizeof ( u32 ) )

#define TMHEAP_MM_SIZE   ( MAX_TM_MODE * TIMER_NODE_SIZE )

#define GET_TNODE_SIZE(n) ( TIMER_NODE_SIZE + ( n ) *( CLOCK_SZIE ))

///////////////////////////////////////////////////////////////////////////

//u8  TMHeap[TMHEAP_MM_SIZE];

/*WxcTimerNode tTimerListHead;
WxcTimerNode *pTLHead;
WxcTimerNode *pTLCur;*/

///////////////////////////////////////////////////////////////////////////

#ifndef _LINUX_TIMER_H
#define time_after(a,b)		((long)(b) - (long)(a) < 0)
#define time_before(a,b)	time_after(b,a)

#define time_after_eq(a,b)	((long)(a) - (long)(b) >= 0)
#define time_before_eq(a,b)	time_after_eq(b,a)
#endif

///////////////////////////////////////////////////////////////////////////

#define TimerAfter(a,b)    time_after(a,b)
#define TimerBefore(a,b)   time_before(a,b)
#define TimerAfterEq(a,b)  time_after_eq(a,b)
#define TimerBeforeEq(a,b) time_before_eq(a,b)


extern void TimerAdd ( WxcTimer * pTimer );
extern void TimerMod ( WxcTimer * pTimer, u16 dExpires );
extern void TimerDel ( WxcTimer * pTimer );


void TimerStart ( WxcTimer * pTimer, u16 dSuitId );
void TimerStop ( WxcTimer * pTimer, u16 dSuitId );
void TM_Init (  );



#endif
