#include<time.h>
#include "common.h"

extern char *strptime(const char *s, const char *format, struct tm *tm);
void  BcdToAscii (char *ascii_buf, const BYTE *bcd_buf, int len)
{
    int   i;
    char  ch;

    for (i=0; i<len; i++) {
        if (i & 1)  ch = *(bcd_buf++) & 0x0f;
        else  ch = *bcd_buf >> 4;
        ascii_buf[i] = ch + ((ch > 9)? 'A'-10 : '0');
    }
    ascii_buf[i] = '\0';
}

void BcdToAsciiR(char *ascii_buf, const unsigned char *bcd_buf, int len)
{
   char ch;
   int  i;
   for(i=0;i<len;i++)
   {
      sprintf(&ascii_buf[i*2],"%02X",bcd_buf[i]);
      ch = ascii_buf[i*2];
      ascii_buf[i*2] = ascii_buf[i*2+1];
      ascii_buf[i*2+1] = ch;
   }
}


char *GetAsciiTime()
{
	char   *pAscTime;
	time_t tCurTime;
	
	tCurTime  = time(NULL);
	pAscTime  = ctime(&tCurTime);
	
	return (char *)pAscTime;
}

int GetCurrentTime( char *pNowTime )
{
	struct tm *pTMNowTime;
	time_t     tTemptime;
	
	tTemptime  = time(NULL);
	pTMNowTime = localtime(&tTemptime);
	if( pTMNowTime == NULL )
		return 0;
	pNowTime[0] = pTMNowTime->tm_year-100;
	pNowTime[1] = pTMNowTime->tm_mon+1;
	pNowTime[2] = pTMNowTime->tm_mday;
	pNowTime[3] = pTMNowTime->tm_hour;
	pNowTime[4] = pTMNowTime->tm_min;
	pNowTime[5] = pTMNowTime->tm_sec; 
	
	return 1;
}

int GetMonthDay()
{
	struct tm *pTMNowTime;
	time_t     tTemptime;
	
	tTemptime  = time(NULL);
	pTMNowTime = localtime(&tTemptime);
	if( pTMNowTime == NULL )
		return 1;
	return pTMNowTime->tm_mday;

}

ulong TransStrTimeToSecond(const char *str_time)//YYYY-MM-DD HH:MM:YY
{
	struct tm nowTm;
	time_t longT;

	if(str_time == NULL)
		return 0;
	
	strptime(str_time,"%Y-%m-%d %H:%M:%S",&nowTm); 
	longT = mktime(&nowTm);

	return longT;
}

int  print_buf_in_fmt_ascii(char *ascii_buf, unsigned char *bcd_buf, int len)
{
	int i;
	char tmp_buf[128];

	memset(ascii_buf, 0x00, len*3);
	
	for(i=0;i<len%300; i++)
	{
		sprintf(tmp_buf,"%02x ",bcd_buf[i]);
		strcat(ascii_buf, tmp_buf);
		if((i+1)%16 == 0)
		{
			strcat(ascii_buf, "\r\n");		
		}
		else if((i+1) %8 == 0)
		{
			strcat(ascii_buf,"  ");
		}
	}

	return 1;
}


void SetTermSignal(struct termios *ptermio, void (*handler)(int))
{
	/*
	ioctl(0,TCGETA, ptermio);
	ptermio->c_cc[VINTR] = '\x03';
	ioctl(0,TCSETA, ptermio);
        */
	
	signal(SIGINT, handler);
	signal(SIGKILL,handler);
	signal(SIGSTOP,handler);
	signal(SIGTERM,handler);
	signal(SIGSEGV,handler);
}

void Terminate(int sig)
{
	exit(1);
}

void SetFSMTimer()
{
	struct itimerval        *ptv, tv;

	ptv = (struct itimerval *) &tv;
	ptv->it_interval.tv_sec		= 0;
	ptv->it_value.tv_sec		= 0;
	ptv->it_interval.tv_usec		= 10000;
	ptv->it_value.tv_usec		= 10000;

	setitimer(ITIMER_REAL, ptv, NULL);
}

void sysDaemonInit()
{
	int		i;
	pid_t	pid;
	
	if ( (pid = fork()) < 0)	/* parent terminates */
		printf("%s: %s: fork (%d)", __FILE__, __FUNCTION__, __LINE__);
	else if (pid != 0)
		exit(0);				/* parent goes bye-bye */
	
	setsid();					/* become session leader */

	for (i = 0; i<3; i++)
		close(i);
}

extern int sys_timer10ms;
void sysTimer_IRQ()
{
	sys_timer10ms = 1;	
}

void SigactionSystem()
{
	struct sigaction action;//,actsegv;

	action.sa_handler = sysTimer_IRQ;
	sigemptyset(&action.sa_mask);
	action.sa_flags = SA_RESTART;
	if(sigaction(SIGALRM, &action, NULL)<0)
	{
		printf("sigactin sigalarm failed\n");
	}
}

