
#include "../../src/include/sccp.h"
#include "signal.h"
#include <unistd.h>

#ifdef _WXC_FSM_TEST
#define spLogFsm printf
#else 
#define spLogFsm
#endif 

static struct itimerval itimer, old_itimer;

u8 sccp_timer_flag;

void mtp3_timer (  );
void send_data (  );

void showbuf ( BYTE * buf, int len )
{
    int i;

    for ( i = 0; i < len; i++ )
        printf ( "%X", buf[i] );
    printf ( "\n" );
}

void showbuf2 ( BYTE * buf, int len )
{
    int i;

    for ( i = 0; i < len; i++ )
        printf ( "%02X", buf[i] );
    printf ( "\n" );
}

void on_data (  )
{
    SCLC_MSG clmsg;
    SCCP_ADDR addr;
    int len, len2;

    if( ( len = sclc_receive ( &clmsg, 0 ) ) > 0 )
    {
        showbuf2 ( clmsg.msg, clmsg.msglen );
        memcpy ( &addr, &clmsg.src_addr, sizeof ( SCCP_ADDR ) );
        memcpy ( &clmsg.src_addr, &clmsg.dst_addr, sizeof ( SCCP_ADDR ) );
        memcpy ( &clmsg.dst_addr, &addr, sizeof ( SCCP_ADDR ) );
        len2 = sclc_send ( &clmsg );
        if( len2 == -1 )
            printf ( "fail to send message" );
    }
}


u8 sccp_20ms_flag = 0;

void On_Timer (  )
{
    //on_data (  );
    sccp_20ms_flag++;
    sccp_timer_flag++;
    spLogFsm("pos plat iprtans start \r\n");
    iptrans_timer (  );
    spLogFsm("pos plat mpt3 start \r\n");
    mtp3_proc (  );
    spLogFsm("pos plat sccp start \r\n");
    spWxcBackTrace();
    sccp_timer (  );
    spLogFsm("pos plat sccp over \r\n");
    spWxcBackTrace();
    if( sccp_20ms_flag >= 2 )
    {
        spWxcBackTrace();
        sccp_20ms_flag = 0;
        spLogFsm("pos plat debug start \r\n");
        debug_rt (  );
        spLogFsm("pos plat snmp start \r\n");
        snmp_timer (  );
        spLogFsm("pos plat heartbeat start \r\n");
        heartbeat_timer (  );
        spLogFsm("pos plat end start \r\n");
        spWxcBackTrace();
    }
}

void SetTimer (  )
{
    struct sigaction act;

    act.sa_handler = On_Timer;
    sigemptyset ( &act.sa_mask );
    act.sa_flags = 0;
    if( sigaction ( SIGALRM, &act, NULL ) < 0 )
    {
        perror ( "Produce Sigaction" );
        exit ( 1 );
    }

    itimer.it_interval.tv_sec = 0;
    itimer.it_interval.tv_usec = 10 * 1000;
    itimer.it_value.tv_sec = 0;
    itimer.it_value.tv_usec = 10 * 1000;

    if( setitimer ( ITIMER_REAL, &itimer, &old_itimer ) != 0 )
    {
        printf ( "Setting Timer  error! \n" );
        exit ( 1 );
    }
}

void spUserInit( )
{
	
}

void spUserFsm( )
{
	
}

u8 delay = 5;
u8 main_state = 0;

void system_init (  )
{		
	sleep ( 3 );
    debug_init (  );
    iptrMainInit (  );
    snmp_init ( 4957 );
    heartbeat_init ( 0x0188 );
    mtp_shm_init (  );
    sccp_init (  );

    sccp_20ms_flag = 0; 
    sccp_timer_flag = 0;	
	
}

int main (  )
{
    system_init();
    SetTimer (  );
    while ( 1 )
    {
        usleep ( 1 );
        
        if( sccp_timer_flag >= delay )
        {
            sccp_timer_flag = 0;
            switch ( main_state )
            {
            case 0:
                ulInit( );
                main_state = 1;
                break;
            case 1:
            	spLogFsm("sccp_test.c: main->ulsapfsm in \n");
                ulSapFsm( );
                spLogFsm("sccp_test.c: main->ulsapfsm out \n");
                break;
            }
        }
    }
    return 1;
}

void send_data (  )
{
    SCLC_MSG clmsg;
    BYTE value[100] = { 1, 2, 3, 4, 5, 6 };
    BYTE number[20] = { 0x68, 0x57, 0x55, 0x17, 0x32, 0x00, 0xF3 };

    clmsg.msglen = 6;
    memcpy ( clmsg.msg, value, clmsg.msglen );
    clmsg.src_addr.DPC = 0x111111;
    clmsg.src_addr.SSN = 8;
    clmsg.src_addr.GTI = 0;

    clmsg.dst_addr.DPC = 0;
    clmsg.dst_addr.SSN = 8;
    clmsg.dst_addr.GTI = 4;
    clmsg.dst_addr.NP = 1;
    clmsg.dst_addr.NAI = 4;
    clmsg.dst_addr.len = 13;
    clmsg.dst_addr.NetID = 0;
    memcpy ( clmsg.dst_addr.GTAI, number, 7 );

    if( sclc_send ( &clmsg ) == -1 )
        printf ( "fail to send message\n" );
}

