涉及到线程调度可能以后有用:
#ifndef __AR2K_NTP_C__
#define __AR2K_NTP_C__
#include "../include/typedef.h"
#include "../include/ntp.h"
#include <time.h>
#define SNTP_PORT 123
#define SNTP_EPOCH 86400U * (365U * 70U + 17U)
#define SNTP_8HOUR 3600U * 8U
typedef unsigned int uint32_t;
extern unsigned int g_RmIp1; // 涓籖MIP
extern unsigned int g_RmIp2; // 澶嘡MIP
struct Sntp_Header
{
unsigned char LiVnMode;
unsigned char Stratum;
unsigned char Poll;
unsigned char Precision;
int RootDelay;
int RootDispersion;
char RefID[4];
int RefTimeInt;
int RefTimeFraction;
int OriTimeInt;
int OriTimeFraction;
int RecvTimeInt;
int RecvTimeFraction;
int TranTimeInt;
int TranTimeFraction;
};
time_t get_utc_time()
{
time_t now;
time(&now);
return now;
}
int ntpsync(uint32_t ip)
{
int i;
int now;
int retValue;
fd_set readfds;
// Structure of SNTP
struct Sntp_Header sntpHeader;
struct Sntp_Header sntpHeader1;
struct Sntp_Header *p;
char *p1;
// vars of network
int sendSock;
struct sockaddr_in toAddr;
int addrLen;
char *pBuf;
int OriTimeInt;
int DestTimeInt;
int difference;
unsigned char tempChar;
struct timeval tv;
sendSock = socket(AF_INET, SOCK_DGRAM, 0);
if (sendSock < 0)
{
return 0;
}
toAddr.sin_family = AF_INET;
toAddr.sin_port = htons(SNTP_PORT);
toAddr.sin_addr.s_addr = ip;
bzero(&(toAddr.sin_zero), 8);
addrLen = sizeof(struct sockaddr);
bzero(&sntpHeader, sizeof(struct Sntp_Header));
sntpHeader.LiVnMode = 0x1b;
OriTimeInt = time(0) + SNTP_EPOCH - SNTP_8HOUR;
now = get_utc_time();
retValue = sendto(sendSock, &sntpHeader, sizeof(struct Sntp_Header), 0,(struct sockaddr *)&toAddr, addrLen);
p1 = (char *)&sntpHeader;
pBuf = (char *)&sntpHeader1;
FD_ZERO(&readfds);
FD_SET(sendSock, &readfds);
tv.tv_sec = 0;
tv.tv_usec = 300000;
select(sendSock + 1, &readfds, NULL, NULL, &tv);
if (FD_ISSET(sendSock, &readfds))
{
retValue = recvfrom(sendSock,pBuf,48,0,(struct sockaddr *)&toAddr,(socklen_t*)&addrLen);
}
else
{
close(sendSock);
return 0;
}
if (retValue <= 0 )
{
close(sendSock);
return 0;
}
for (i = 4; i < 12; i++)
{
tempChar = *(pBuf + i * 4);
*(pBuf + i * 4) = *(pBuf + i * 4 + 3);
*(pBuf + i * 4 + 3) = tempChar;
tempChar = *(pBuf + i * 4 + 1);
*(pBuf + i * 4 + 1) = *(pBuf + i * 4 + 2);
*(pBuf + i * 4 + 2) = tempChar;
}
p = (struct Sntp_Header *)pBuf;
DestTimeInt = time((time_t *)NULL) + SNTP_EPOCH - SNTP_8HOUR;
difference = (p->RecvTimeInt - OriTimeInt) + (p->TranTimeInt - DestTimeInt);
difference = difference / 2;
close(sendSock);
return now + difference;
}
int do_ntp_sync()
{
printf("ntp_sync\n");
struct timeval tv;
int rv;
int now_time;
now_time = ntpsync(g_RmIp1);
if(now_time == 0)
{
now_time = ntpsync(g_RmIp1);
}
if(now_time != 0)
{
tv.tv_sec = now_time;
tv.tv_usec = 0;
}
else
{
return -1;
}
rv = settimeofday(&tv, NULL);
system( "/usr/evsapp/hiclock -w" );
return 0;
}
#if 0
static void* ntp_process(void* arg)
{
uint32_t tick = 24;
while(gbl_quit == 0)
{
sleep(60 * 10);
system("hwclock --hctosys");// Set the System Time from the Hardware Clock.
if(--tick)
{
continue;
}
tick = 24;
if(gbl_ntp_enable)
{
ar2k_wait();
if(0 == do_ntp_sync())
{
log_printf("NTP-sync successed!\n");
}
else
{
log_printf("NTP-sync failed!\n");
}
ar2k_post();
}
}
return arg;
}
int ntp_init()
{
pthread_t ntp_id;
#if CFG_NO_PRIOLEVEL == 0
struct sched_param schedParam;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
schedParam.sched_priority = PRI_NTP_THREAD;
pthread_attr_setschedparam(&attr, &schedParam);
#endif
#if CFG_NO_PRIOLEVEL == 0
if(0 != pthread_create(&ntp_id,&attr,ntp_process,NULL))
#else
if(0 != pthread_create(&ntp_id,NULL,ntp_process,NULL))
#endif
{
log_printf("can not create NTP thread!\n");
return -1;
}
return 0;
}
int ntp_uninit()
{
return 0;
}
#endif
#endif
转载于:https://blog.51cto.com/fengyuzaitu/1575242