rtc: factor out RTC_Linux_ReadTime_Now
We have code to read RTC time and handle the error associated with having no UIE interrupt, which we currently use as part of maintaining the correction file. In a later commit, we will need the same functionality for using the RTC as reference clock, so export the function and give it a descriptive name appropriate for a globally visible function.
This commit is contained in:
parent
577cf09bc5
commit
5ce34d07fb
2 changed files with 53 additions and 48 deletions
45
rtc_linux.c
45
rtc_linux.c
|
@ -930,6 +930,28 @@ RTC_Linux_WriteParameters(void)
|
|||
return(retval);
|
||||
}
|
||||
|
||||
time_t
|
||||
RTC_Linux_ReadTime_Now(int fd, int utc, struct timespec *old_sys_time)
|
||||
{
|
||||
struct rtc_time rtc_raw, rtc_raw_retry;
|
||||
int status;
|
||||
|
||||
/* Retry reading the rtc until both read attempts give the same sec value.
|
||||
This way the race condition is prevented that the RTC has updated itself
|
||||
during the first read operation. */
|
||||
do {
|
||||
status = ioctl(fd, RTC_RD_TIME, &rtc_raw);
|
||||
if (status >= 0) {
|
||||
status = ioctl(fd, RTC_RD_TIME, &rtc_raw_retry);
|
||||
}
|
||||
} while (status >= 0 && rtc_raw.tm_sec != rtc_raw_retry.tm_sec);
|
||||
|
||||
/* Read system clock */
|
||||
LCL_ReadCookedTime(old_sys_time, NULL);
|
||||
|
||||
return status >= 0 ? t_from_rtc(&rtc_raw, utc) : -1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
/* Try to set the system clock from the RTC, in the same manner as
|
||||
/sbin/hwclock -s would do. We're not as picky about OS version
|
||||
|
@ -939,8 +961,7 @@ RTC_Linux_WriteParameters(void)
|
|||
int
|
||||
RTC_Linux_TimePreInit(time_t driftfile_time)
|
||||
{
|
||||
int fd, status;
|
||||
struct rtc_time rtc_raw, rtc_raw_retry;
|
||||
int fd;
|
||||
time_t rtc_t;
|
||||
double accumulated_error, sys_offset;
|
||||
struct timespec new_sys_time, old_sys_time;
|
||||
|
@ -956,25 +977,10 @@ RTC_Linux_TimePreInit(time_t driftfile_time)
|
|||
return 0; /* Can't open it, and won't be able to later */
|
||||
}
|
||||
|
||||
/* Retry reading the rtc until both read attempts give the same sec value.
|
||||
This way the race condition is prevented that the RTC has updated itself
|
||||
during the first read operation. */
|
||||
do {
|
||||
status = ioctl(fd, RTC_RD_TIME, &rtc_raw);
|
||||
if (status >= 0) {
|
||||
status = ioctl(fd, RTC_RD_TIME, &rtc_raw_retry);
|
||||
}
|
||||
} while (status >= 0 && rtc_raw.tm_sec != rtc_raw_retry.tm_sec);
|
||||
|
||||
/* Read system clock */
|
||||
LCL_ReadCookedTime(&old_sys_time, NULL);
|
||||
rtc_t = RTC_Linux_ReadTime_Now(fd, rtc_on_utc, &old_sys_time);
|
||||
|
||||
close(fd);
|
||||
|
||||
if (status >= 0) {
|
||||
/* Convert to seconds since 1970 */
|
||||
rtc_t = t_from_rtc(&rtc_raw, rtc_on_utc);
|
||||
|
||||
if (rtc_t != (time_t)(-1)) {
|
||||
|
||||
/* Work out approximatation to correct time (to about the
|
||||
|
@ -1009,9 +1015,6 @@ RTC_Linux_TimePreInit(time_t driftfile_time)
|
|||
} else {
|
||||
return 0;
|
||||
}
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
|
|
@ -46,5 +46,7 @@ extern int RTC_Linux_SwitchInterrupt(int fd, int on_off);
|
|||
extern int RTC_Linux_CheckInterrupt(int fd);
|
||||
extern time_t RTC_Linux_ReadTime_AfterInterrupt(int fd, int utc,
|
||||
struct timespec *sys_time_cooked);
|
||||
extern time_t RTC_Linux_ReadTime_Now(int fd, int utc,
|
||||
struct timespec *sys_time_cooked);
|
||||
|
||||
#endif /* _GOT_RTC_LINUX_H */
|
||||
|
|
Loading…
Reference in a new issue