
#include "assert.h"
#include "../../src/include/includes.h"
#include "../../src/include/public.h"
#include "../../src/include/wxc_timer.h"


u32  hlr_timer_flag;
u32  hlr_20ms_flag;

void system_init ( void )
{
    sleep ( 3 );
    hlr_timer_flag = 0;
    hlr_20ms_flag = 0;
}

void timer_callback (  )
{
    hlr_20ms_flag++;
    hlr_timer_flag++;
    printf("10 ms call back : %d\n" , hlr_timer_flag);
}

void set_timer (  )
{
    struct sigaction act;
    struct itimerval value;
    struct itimerval old_value;

    value.it_interval.tv_sec = 0;
    value.it_interval.tv_usec = 10000;  // 10 ms
    value.it_value.tv_sec = 0;
    value.it_value.tv_usec = 10000;     // 10 ms
    act.sa_handler = timer_callback;
    sigemptyset ( &act.sa_mask );
    act.sa_flags = 0;
    if( sigaction ( SIGALRM, &act, NULL ) == -1 )
    {
        printf ( "Timer error\n" );
        exit ( 0 );
    }
    setitimer ( ITIMER_REAL, &value, &old_value );
    iopl ( 3 );
}

void tm_app_callback_10ms ( u16 dSuitId, u16 dData )
{
     printf("10 ms call back\n");
}

void tm_app_callback_1s ( u16 dSuitId, u16 dData )
{
     printf("10 ms call back\n");
}

void tm_app_callback_50s ( u16 dSuitId, u16 dData )
{
     printf("10 ms call back\n");
}

void tm_app_callback_5000s ( u16 dSuitId, u16 dData )
{
     printf("10 ms call back\n");
}


void tm_app_init (  )
{

}

int app_state;
WxcTimer tTimer10ms;
WxcTimer tTimer1s;
WxcTimer tTimer50s;
WxcTimer tTimer5000s;

typedef enum TMTEST_STATE
{
	APP_REGSITER,
	APP_START,
	APP_TIMEROUT,
	APP_STOP,
}
TM_STATE;


void tm_app_test (  )
{
    

    switch ( app_state )
    {
    case APP_REGSITER:
        tTimer10ms.dExpires = 1;
        tTimer10ms.dSuitNumber = 1;
        tTimer10ms.pFunc = &tm_app_callback_10ms;
        tTimer10ms.dData = 0;
        TimerAdd ( &tTimer10ms );
        
        tTimer1s.dExpires = 100;
        tTimer1s.dSuitNumber = 10;
        tTimer1s.pFunc = &tm_app_callback_1s;
        tTimer1s.dData = 0;
        TimerAdd ( &tTimer1s );
        
        tTimer50s.dExpires = 5000;
        tTimer50s.dSuitNumber = 100;
        tTimer50s.pFunc = &tm_app_callback_50s;
        tTimer50s.dData = 0;
        TimerAdd ( &tTimer50s );
        
        tTimer5000s.dExpires = 5000000;
        tTimer5000s.dSuitNumber = 10;
        tTimer5000s.pFunc = &tm_app_callback_5000s;
        tTimer5000s.dData = 0;
        TimerAdd ( &tTimer5000s );
        app_state++;
        break;
        
    case APP_START:
        app_state++;
        break;
        
    case APP_TIMEROUT:

        TimerAdd ( &tTimer50s );
        app_state++;
        break;
    case APP_STOP:

        TimerAdd ( &tTimer50s );
        app_state++;
        break;
    default:
        break;
    }
}


int main ( int argc, char *argv[] )
{

    system_init (  );

    set_timer (  );

    while ( 1 )
    {
        usleep ( 1 );
        tm_app_test (  );
    }
    return 1;
}
