summaryrefslogtreecommitdiff
path: root/hwclock/hwclock.c
blob: f662145f0217efe04f8120bcbb08470599feeb25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <linux/rtc.h>

/*
 * This is meant to synchronise the system clock with the RTC at
 * startup. If you know a better way of doing this in Android,
 * drop me an email at kevin.petit@arm.com
 */

static time_t read_rtc(void)
{
        struct tm tm_time;
        int fd;

        fd = open("/dev/rtc0", O_RDONLY);
	printf("Got fd = %d for RTC\n", fd);

        /* Read time from the RTC */
	memset(&tm_time, 0, sizeof(tm_time));
        if (ioctl(fd, RTC_RD_TIME, &tm_time)) {
		fprintf(stderr, "error while doing RTC_RD_TIME: %s\n", strerror(errno));
	}
        tm_time.tm_isdst = -1; /* "not known" */

	printf("TIME FROM RTC: %s\n", asctime(&tm_time));

        close(fd);

        return mktime(&tm_time);
}

static void rtc_to_sys_clock(void)
{
        struct timeval tv;
	uint64_t timelog;

        tv.tv_sec = read_rtc();
	timelog = tv.tv_sec;
	printf("Got tv.tv_sec = %llu\n", timelog);
        tv.tv_usec = 0;
        if (settimeofday(&tv, NULL))
                fprintf(stderr, "settimeofday failed: %s\n", strerror(errno));
}

int main(int argc, char **argv)
{
	(void)argc;
	(void)argv;
	rtc_to_sys_clock();
        return 0;
}