|
|
@ -37,8 +37,8 @@ typedef struct sSNTPClient* SNTPClient;
|
|
|
|
|
|
|
|
|
|
|
|
/* new time types */
|
|
|
|
/* new time types */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
typedef struct {
|
|
|
|
{
|
|
|
|
uint32_t coarse;
|
|
|
|
uint32_t coarse;
|
|
|
|
uint32_t fine;
|
|
|
|
uint32_t fine;
|
|
|
|
} NtpTime;
|
|
|
|
} NtpTime;
|
|
|
@ -164,7 +164,8 @@ parseResponseMessage(SNTPClient self, uint8_t* buffer, int bufSize)
|
|
|
|
int li = (header & 0xc0)>> 6;
|
|
|
|
int li = (header & 0xc0)>> 6;
|
|
|
|
|
|
|
|
|
|
|
|
/* check for "clock-not-synchronized" */
|
|
|
|
/* check for "clock-not-synchronized" */
|
|
|
|
if (li == 3) {
|
|
|
|
if (li == 3)
|
|
|
|
|
|
|
|
{
|
|
|
|
/* ignore time message */
|
|
|
|
/* ignore time message */
|
|
|
|
if (SNTP_DEBUG)
|
|
|
|
if (SNTP_DEBUG)
|
|
|
|
printf("WARNING: received clock-not-synchronized from server\n");
|
|
|
|
printf("WARNING: received clock-not-synchronized from server\n");
|
|
|
@ -223,7 +224,8 @@ parseResponseMessage(SNTPClient self, uint8_t* buffer, int bufSize)
|
|
|
|
uint64_t trnsTime = ntpTimeToNsTime(coarse, fine);
|
|
|
|
uint64_t trnsTime = ntpTimeToNsTime(coarse, fine);
|
|
|
|
|
|
|
|
|
|
|
|
/* set system time */
|
|
|
|
/* set system time */
|
|
|
|
if (Hal_setTimeInNs(trnsTime) == false) {
|
|
|
|
if (Hal_setTimeInNs(trnsTime) == false)
|
|
|
|
|
|
|
|
{
|
|
|
|
if (SNTP_DEBUG)
|
|
|
|
if (SNTP_DEBUG)
|
|
|
|
printf("SNTP: failed to set system clock!\n");
|
|
|
|
printf("SNTP: failed to set system clock!\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -231,11 +233,13 @@ parseResponseMessage(SNTPClient self, uint8_t* buffer, int bufSize)
|
|
|
|
self->clockSynced = true;
|
|
|
|
self->clockSynced = true;
|
|
|
|
self->outStandingRequest = false;
|
|
|
|
self->outStandingRequest = false;
|
|
|
|
|
|
|
|
|
|
|
|
if (self->userCallback) {
|
|
|
|
if (self->userCallback)
|
|
|
|
|
|
|
|
{
|
|
|
|
self->userCallback(self->userCallbackParameter, true);
|
|
|
|
self->userCallback(self->userCallbackParameter, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (SNTP_DEBUG) {
|
|
|
|
if (SNTP_DEBUG)
|
|
|
|
|
|
|
|
{
|
|
|
|
printf("SNTP: reference time: %u.%.6u\n", nsTimeToSeconds(refTime), getUsPartFromNsTime(refTime));
|
|
|
|
printf("SNTP: reference time: %u.%.6u\n", nsTimeToSeconds(refTime), getUsPartFromNsTime(refTime));
|
|
|
|
printf("SNTP: original time: %u.%.9u\n", nsTimeToSeconds(origTime), getUsPartFromNsTime(origTime));
|
|
|
|
printf("SNTP: original time: %u.%.9u\n", nsTimeToSeconds(origTime), getUsPartFromNsTime(origTime));
|
|
|
|
printf("SNTP: receive time: %u.%.6u\n", nsTimeToSeconds(recvTime), getUsPartFromNsTime(recvTime));
|
|
|
|
printf("SNTP: receive time: %u.%.6u\n", nsTimeToSeconds(recvTime), getUsPartFromNsTime(recvTime));
|
|
|
@ -310,14 +314,17 @@ SNTPClient_create()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
SNTPClient self = (SNTPClient) GLOBAL_CALLOC(1, sizeof(struct sSNTPClient));
|
|
|
|
SNTPClient self = (SNTPClient) GLOBAL_CALLOC(1, sizeof(struct sSNTPClient));
|
|
|
|
|
|
|
|
|
|
|
|
if (self) {
|
|
|
|
if (self)
|
|
|
|
|
|
|
|
{
|
|
|
|
self->socket = UdpSocket_create();
|
|
|
|
self->socket = UdpSocket_create();
|
|
|
|
|
|
|
|
|
|
|
|
if (self->socket == NULL) {
|
|
|
|
if (self->socket == NULL)
|
|
|
|
|
|
|
|
{
|
|
|
|
GLOBAL_FREEMEM(self);
|
|
|
|
GLOBAL_FREEMEM(self);
|
|
|
|
self = NULL;
|
|
|
|
self = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
else
|
|
|
|
|
|
|
|
{
|
|
|
|
self->handleSet = Handleset_new();
|
|
|
|
self->handleSet = Handleset_new();
|
|
|
|
Handleset_addSocket(self->handleSet, (Socket) self->socket);
|
|
|
|
Handleset_addSocket(self->handleSet, (Socket) self->socket);
|
|
|
|
self->pollInterval = 30000000000UL; /* 30 s */
|
|
|
|
self->pollInterval = 30000000000UL; /* 30 s */
|
|
|
@ -351,7 +358,8 @@ SNTPClient_isSynchronized(SNTPClient self)
|
|
|
|
void
|
|
|
|
void
|
|
|
|
SNTPClient_setUserCallback(SNTPClient self, SNTPClient_UserCallback callback, void* parameter)
|
|
|
|
SNTPClient_setUserCallback(SNTPClient self, SNTPClient_UserCallback callback, void* parameter)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (self) {
|
|
|
|
if (self)
|
|
|
|
|
|
|
|
{
|
|
|
|
self->userCallback = callback;
|
|
|
|
self->userCallback = callback;
|
|
|
|
self->userCallbackParameter = parameter;
|
|
|
|
self->userCallbackParameter = parameter;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -375,23 +383,28 @@ SNTPClient_tick(SNTPClient self)
|
|
|
|
if (self->lastRequestTimestamp > now)
|
|
|
|
if (self->lastRequestTimestamp > now)
|
|
|
|
self->lastRequestTimestamp = now;
|
|
|
|
self->lastRequestTimestamp = now;
|
|
|
|
|
|
|
|
|
|
|
|
if (self->lastRequestTimestamp > 0) {
|
|
|
|
if (self->lastRequestTimestamp > 0)
|
|
|
|
|
|
|
|
{
|
|
|
|
/* check for timeout */
|
|
|
|
/* check for timeout */
|
|
|
|
if ((now - self->lastReceivedMessage) > 300000000000UL) {
|
|
|
|
if ((now - self->lastReceivedMessage) > 300000000000UL)
|
|
|
|
|
|
|
|
{
|
|
|
|
if (self->clockSynced) {
|
|
|
|
if (self->clockSynced)
|
|
|
|
|
|
|
|
{
|
|
|
|
self->clockSynced = false;
|
|
|
|
self->clockSynced = false;
|
|
|
|
printf("SNTP: request timeout\n");
|
|
|
|
printf("SNTP: request timeout\n");
|
|
|
|
//TODO when to call user handler?
|
|
|
|
|
|
|
|
if (self->userCallback) {
|
|
|
|
if (self->userCallback)
|
|
|
|
|
|
|
|
{
|
|
|
|
self->userCallback(self->userCallbackParameter, false);
|
|
|
|
self->userCallback(self->userCallbackParameter, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (self->serverAddr) {
|
|
|
|
if (self->serverAddr)
|
|
|
|
if ((now - self->lastRequestTimestamp) > self->pollInterval) {
|
|
|
|
{
|
|
|
|
|
|
|
|
if ((now - self->lastRequestTimestamp) > self->pollInterval)
|
|
|
|
|
|
|
|
{
|
|
|
|
sendRequestMessage(self, self->serverAddr, self->serverPort);
|
|
|
|
sendRequestMessage(self, self->serverAddr, self->serverPort);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -405,17 +418,19 @@ SNTPClient_handleIncomingMessage(SNTPClient self)
|
|
|
|
uint8_t buffer[200];
|
|
|
|
uint8_t buffer[200];
|
|
|
|
int rcvdBytes = UdpSocket_receiveFrom(self->socket, ipAddress, 200, buffer, sizeof(buffer));
|
|
|
|
int rcvdBytes = UdpSocket_receiveFrom(self->socket, ipAddress, 200, buffer, sizeof(buffer));
|
|
|
|
|
|
|
|
|
|
|
|
if (rcvdBytes > 0) {
|
|
|
|
if (rcvdBytes > 0)
|
|
|
|
|
|
|
|
{
|
|
|
|
if (SNTP_DEBUG)
|
|
|
|
if (SNTP_DEBUG)
|
|
|
|
printf("SNTP: received response from %s\n", ipAddress);
|
|
|
|
printf("SNTP: received response from %s\n", ipAddress);
|
|
|
|
|
|
|
|
|
|
|
|
parseResponseMessage(self, buffer, rcvdBytes);
|
|
|
|
parseResponseMessage(self, buffer, rcvdBytes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (rcvdBytes == -1) {
|
|
|
|
else if (rcvdBytes == -1)
|
|
|
|
|
|
|
|
{
|
|
|
|
printf("UDP socket error\n");
|
|
|
|
printf("UDP socket error\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
else
|
|
|
|
|
|
|
|
{
|
|
|
|
printf("No data!\n");
|
|
|
|
printf("No data!\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -442,10 +457,12 @@ handleThread(void* parameter)
|
|
|
|
void
|
|
|
|
void
|
|
|
|
SNTPClient_start(SNTPClient self)
|
|
|
|
SNTPClient_start(SNTPClient self)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (self) {
|
|
|
|
if (self)
|
|
|
|
|
|
|
|
{
|
|
|
|
int sntpPoirt = SNTP_DEFAULT_PORT;
|
|
|
|
int sntpPoirt = SNTP_DEFAULT_PORT;
|
|
|
|
|
|
|
|
|
|
|
|
if (UdpSocket_bind(self->socket, "0.0.0.0", SNTP_DEFAULT_PORT)) {
|
|
|
|
if (UdpSocket_bind(self->socket, "0.0.0.0", SNTP_DEFAULT_PORT))
|
|
|
|
|
|
|
|
{
|
|
|
|
printf("Start NTP thread\n");
|
|
|
|
printf("Start NTP thread\n");
|
|
|
|
|
|
|
|
|
|
|
|
self->thread = Thread_create(handleThread, self, false);
|
|
|
|
self->thread = Thread_create(handleThread, self, false);
|
|
|
@ -453,7 +470,8 @@ SNTPClient_start(SNTPClient self)
|
|
|
|
if (self->thread)
|
|
|
|
if (self->thread)
|
|
|
|
Thread_start(self->thread);
|
|
|
|
Thread_start(self->thread);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
else
|
|
|
|
|
|
|
|
{
|
|
|
|
if (SNTP_DEBUG)
|
|
|
|
if (SNTP_DEBUG)
|
|
|
|
printf("SNTP: Failed to bind to port %i\n", sntpPoirt);
|
|
|
|
printf("SNTP: Failed to bind to port %i\n", sntpPoirt);
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -463,7 +481,8 @@ SNTPClient_start(SNTPClient self)
|
|
|
|
void
|
|
|
|
void
|
|
|
|
SNTPClient_stop(SNTPClient self)
|
|
|
|
SNTPClient_stop(SNTPClient self)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (self->thread) {
|
|
|
|
if (self->thread)
|
|
|
|
|
|
|
|
{
|
|
|
|
self->running = false;
|
|
|
|
self->running = false;
|
|
|
|
Thread_destroy(self->thread);
|
|
|
|
Thread_destroy(self->thread);
|
|
|
|
self->thread = NULL;
|
|
|
|
self->thread = NULL;
|
|
|
@ -473,16 +492,15 @@ SNTPClient_stop(SNTPClient self)
|
|
|
|
void
|
|
|
|
void
|
|
|
|
SNTPClient_destroy(SNTPClient self)
|
|
|
|
SNTPClient_destroy(SNTPClient self)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (self) {
|
|
|
|
if (self)
|
|
|
|
|
|
|
|
{
|
|
|
|
SNTPClient_stop(self);
|
|
|
|
SNTPClient_stop(self);
|
|
|
|
|
|
|
|
|
|
|
|
if (self->serverAddr)
|
|
|
|
if (self->serverAddr)
|
|
|
|
GLOBAL_FREEMEM(self->serverAddr);
|
|
|
|
GLOBAL_FREEMEM(self->serverAddr);
|
|
|
|
|
|
|
|
|
|
|
|
if (self->socket) {
|
|
|
|
if (self->socket)
|
|
|
|
Socket_destroy((Socket) self->socket);
|
|
|
|
Socket_destroy((Socket) self->socket);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GLOBAL_FREEMEM(self);
|
|
|
|
GLOBAL_FREEMEM(self);
|
|
|
|
}
|
|
|
|
}
|
|
|
|