1 /* vi: set sw=4 ts=4: */
3 * Mini hwclock implementation for busybox
5 * Copyright (C) 2002 Robert Griebl <griebl@gmx.de>
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
11 /* After libbb.h, since it needs sys/types.h on some systems */
12 #include <sys/utsname.h>
15 #if ENABLE_FEATURE_HWCLOCK_LONG_OPTIONS
21 static const char *rtcname;
23 static time_t read_rtc(struct timeval *sys_tv, int utc)
29 fd = rtc_xopen(&rtcname, O_RDONLY);
35 gettimeofday(sys_tv, NULL);
36 if (before != tm.tm_sec)
40 if (ENABLE_FEATURE_CLEAN_UP)
43 return rtc_tm2time(&tm, utc);
46 static void show_clock(int utc)
48 struct timeval sys_tv;
53 t = read_rtc(&sys_tv, utc);
56 strchrnul(cp, '\n')[0] = '\0';
58 //printf("%s 0.000000 seconds %s\n", cp, utc ? "" : (ptm->tm_isdst ? tzname[1] : tzname[0]));
59 diff = sys_tv.tv_sec - t;
60 if (diff < 0 /*&& tv.tv_usec != 0*/) {
62 /* diff >= 0 is ok: diff < 0, can't just use tv.tv_usec: */
63 /* 45.520820 43.520820 */
64 /* - 44.000000 - 45.000000 */
65 /* = 0.520820 = -1.479180, not -2.520820! */
67 /* should be 1000000 - tv.tv_usec, but then we must check tv.tv_usec != 0 */
68 sys_tv.tv_usec = 999999 - sys_tv.tv_usec;
70 printf("%s %ld.%06lu seconds\n", cp, diff, (unsigned long)sys_tv.tv_usec);
73 static void to_sys_clock(int utc)
78 tz.tz_minuteswest = timezone/60 - 60*daylight;
81 tv.tv_sec = read_rtc(NULL, utc);
83 if (settimeofday(&tv, &tz))
84 bb_perror_msg_and_die("settimeofday() failed");
87 static void from_sys_clock(int utc)
89 #define TWEAK_USEC 200
92 unsigned adj = TWEAK_USEC;
93 int rtc = rtc_xopen(&rtcname, O_WRONLY);
95 /* Try to catch the moment when whole second is close */
100 gettimeofday(&tv, NULL);
103 rem_usec = 1000000 - tv.tv_usec;
104 if (rem_usec < 1024) {
105 /* Less than 1ms to next second. Good enough */
112 gmtime_r(&t, &tm); /* may read /etc/xxx (it takes time) */
114 localtime_r(&t, &tm); /* same */
117 /* gmtime/localtime took some time, re-get cur time */
118 gettimeofday(&tv, NULL);
120 if (tv.tv_sec < t /* may happen if rem_usec was < 1024 */
121 || (tv.tv_sec == t && tv.tv_usec < 1024)
123 /* We are not too far into next second. Good. */
126 adj += 32; /* 2^(10-5) = 2^5 = 32 iterations max */
128 /* Give up trying to sync */
132 /* Try to sync up by sleeping */
133 rem_usec = 1000000 - tv.tv_usec;
134 if (rem_usec < 1024) {
135 goto small_rem; /* already close, don't sleep */
138 * Note that small adj on slow processors can make us
139 * to always overshoot tv.tv_usec < 1024 check on next
140 * iteration. That's why adj is increased on each iteration.
141 * This also allows it to be reused as a loop limiter.
143 usleep(rem_usec - adj);
146 xioctl(rtc, RTC_SET_TIME, &tm);
148 /* Debug aid to find "good" TWEAK_USEC.
149 * Look for a value which makes tv_usec close to 999999 or 0.
150 * for 2.20GHz Intel Core 2: TWEAK_USEC ~= 200
152 //bb_error_msg("tv.tv_usec:%d adj:%d", (int)tv.tv_usec, adj);
154 if (ENABLE_FEATURE_CLEAN_UP)
158 #define HWCLOCK_OPT_LOCALTIME 0x01
159 #define HWCLOCK_OPT_UTC 0x02
160 #define HWCLOCK_OPT_SHOW 0x04
161 #define HWCLOCK_OPT_HCTOSYS 0x08
162 #define HWCLOCK_OPT_SYSTOHC 0x10
163 #define HWCLOCK_OPT_RTCFILE 0x20
165 int hwclock_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
166 int hwclock_main(int argc UNUSED_PARAM, char **argv)
171 #if ENABLE_FEATURE_HWCLOCK_LONG_OPTIONS
172 static const char hwclock_longopts[] ALIGN1 =
173 "localtime\0" No_argument "l"
174 "utc\0" No_argument "u"
175 "show\0" No_argument "r"
176 "hctosys\0" No_argument "s"
177 "systohc\0" No_argument "w"
178 "file\0" Required_argument "f"
180 applet_long_options = hwclock_longopts;
182 opt_complementary = "r--ws:w--rs:s--wr:l--u:u--l";
183 opt = getopt32(argv, "lurswf:", &rtcname);
185 /* If -u or -l wasn't given check if we are using utc */
186 if (opt & (HWCLOCK_OPT_UTC | HWCLOCK_OPT_LOCALTIME))
187 utc = (opt & HWCLOCK_OPT_UTC);
189 utc = rtc_adjtime_is_utc();
191 if (opt & HWCLOCK_OPT_HCTOSYS)
193 else if (opt & HWCLOCK_OPT_SYSTOHC)
196 /* default HWCLOCK_OPT_SHOW */