#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "wsms.h"


static struct itimerval itimer, old_itimer;
static void On_Timer();
static void SetTimer();


/*************************************************
Function:        // main
Description:     // The main function of the module
Calls:           // 
Called By:       // 
Table Accessed:  // 
Table Updated:   // 
Input:           // 
Output:          // 
Return:          // 
Others:          // 
*************************************************/
int main(int argc, char *argv[])
{
	pid_t pid;

	if (argc == 2 && strstr(argv[1], "-d")) {
		if ((pid = fork()) != 0)
			exit(0);
		setsid();
	}
	
	wsms_init();
	
	SetTimer();
	while (1) {
		usleep(50);
	}
	return 1;
}


/*************************************************
Function:        // On_Timer
Description:     // Timer function
Calls:           // 
Called By:       // 
Table Accessed:  // 
Table Updated:   // 
Input:           // 
Output:          // 
Return:          // 
Others:          // 
*************************************************/
void On_Timer()
{
	wsms_timer();
}

/*************************************************
Function:        // SetTimer
Description:     // Timer function
Calls:           // On_Timer
Called By:       // 
Table Accessed:  // 
Table Updated:   // 
Input:           // 
Output:          // 
Return:          // 
Others:          // 
*************************************************/
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");
		exitLog("wsms:SetTimer, sigaction function error");
		exit(1);
	}

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

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





