Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions ntpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
#include <sys/time.h>
#include <signal.h>
#include <syslog.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/rtc.h>
#include <errno.h>

#define I_MISC 0
#define I_ORTIME 6
Expand All @@ -25,6 +29,9 @@
#endif


void syncToRTC();


// 0 = ok, 1 = failed, 2 = permanent failure
static int ntpc(struct in_addr addr)
{
Expand Down Expand Up @@ -127,6 +134,8 @@ static int ntpc(struct in_addr addr)

strftime(s, sizeof(s), "%a, %d %b %Y %H:%M:%S %z", localtime(&tv.tv_sec));
sprintf(q, "Time Updated: %s [%s%lds]", s, diff > 0 ? "+" : "", diff);

syncToRTC();
}
else {
t = time(0);
Expand All @@ -146,6 +155,39 @@ static int ntpc(struct in_addr addr)
}


void syncToRTC()
{
int fd = 0;
struct timeval tv;
struct tm localtm;
//struct rtc_time rtctm;
char s[64];

gettimeofday(&tv, NULL);
localtime_r(&tv.tv_sec, &localtm);

strftime(s, sizeof(s), "%a, %d %b %Y %H:%M:%S %z", &localtm);
printf("syncToRTC(): %s\n", s);

fd = open("/dev/rtc0", O_RDONLY);
if (fd == -1)
{
printf("ERROR: can't open /dev/rtc0 ...\n");
return;
}

int ret = ioctl(fd, RTC_SET_TIME, &localtm);
if(ret == -1)
{
close(fd);
printf("ERROR: can't set RTC...: %s\n", strerror(errno));
return;
}

close(fd);
return;
}

// -----------------------------------------------------------------------------


Expand Down