-rw-r--r-- | core/opiealarm/opiealarm.c | 57 | ||||
-rwxr-xr-x | root/etc/suspend-scripts/S46opiealarm | 2 |
2 files changed, 44 insertions, 15 deletions
diff --git a/core/opiealarm/opiealarm.c b/core/opiealarm/opiealarm.c index ac98832..128929e 100644 --- a/core/opiealarm/opiealarm.c +++ b/core/opiealarm/opiealarm.c @@ -30,9 +30,9 @@ FILE *log; // debug only int resume ( int resuspend ); -int suspend ( void ); +int suspend ( int fix_rtc ); int main ( int argc, char **argv ); int fork_with_pidfile ( void ); int kill_with_pidfile ( void ); void remove_pidfile ( void ); @@ -72,10 +72,11 @@ void sig_handler ( int sig ) } void usage ( void ) { - fprintf ( stderr, "Usage: opiealarm -r|-s [-a]\n\n" ); + fprintf ( stderr, "Usage: opiealarm -s [-f] | -r [-a]\n\n" ); fprintf ( stderr, "\t-s\tSuspend mode: set RTC alarm\n" ); + fprintf ( stderr, "\t-f \tFix RTC, if RTC and system have more than 5sec difference (suspend mode)\n" ); fprintf ( stderr, "\t-r\tResume mode: kill running opiealarm\n" ); fprintf ( stderr, "\t-a <x>\tResuspend in <x> seconds (resume mode)\n\n" ); exit ( 1 ); } @@ -142,11 +143,12 @@ void remove_pidfile ( void ) int main ( int argc, char **argv ) { int mode = 0; int ac_resusp = 0; + int fix_rtc = 0; int opt; - while (( opt = getopt ( argc, argv, "a:rs" )) != EOF ) { + while (( opt = getopt ( argc, argv, "a:frs" )) != EOF ) { switch ( opt ) { case 's': mode = 's'; break; @@ -160,8 +162,11 @@ int main ( int argc, char **argv ) fprintf ( stderr, "Warning: resuspend timeout must be >= 30 sec. -- now set to 120 sec\n" ); } break; + case 'f': + fix_rtc = 1; + break; default: usage ( ); } } @@ -180,22 +185,23 @@ int main ( int argc, char **argv ) switch ( mode ) { case 'r': return resume ( ac_resusp ); case 's': - default : return suspend ( ); + default : return suspend ( fix_rtc ); } return 0; } -int suspend ( void ) +int suspend ( int fix_rtc ) { FILE *fp; char buf [64]; - time_t t; - struct tm *tm; + time_t alrt, syst, rtct; + struct tm alr, sys, rtc; int fd; - + int rtc_sys_diff; + if ( !fork_with_pidfile ( )) return 3; @@ -211,26 +217,49 @@ int suspend ( void ) error_msg_and_die ( 1, "/etc/resumeat" ); fclose ( fp ); - t = atoi ( buf ); + alrt = atoi ( buf ); - if ( t == 0 ) + if ( alrt == 0 ) error_msg_and_die ( 0, "/etc/resumeat contains an invalid time description" ); /* subtract 5 sec from event time... */ - t -= 5; + alrt -= 5; if ( log ) - fprintf ( log, "Setting RTC alarm to %d\n", t ); + fprintf ( log, "Setting RTC alarm to %d\n", alrt ); - tm = gmtime ( &t ); + alr = *gmtime ( &alrt ); + // get system time + time ( &syst ); + sys = *localtime ( &syst ); + // Write alarm time to RTC if (( fd = open ( "/dev/misc/rtc", O_RDWR )) < 0 ) error_msg_and_die ( 1, "/dev/misc/rtc" ); + + // get RTC time + if ( ioctl ( fd, RTC_ALM_SET, &rtc ) < 0 ) + error_msg_and_die ( 1, "ioctl RTC_RD_TIME" ); + rtct = mktime ( &rtc ); + + rtc_sys_diff = ( syst - rtct ) - sys. tm_gmtoff; + + if ( fix_rtc && (( rtc_sys_diff < -4 ) || ( rtc_sys_diff > 4 ))) { + struct tm set; + + set = *gmtime ( &syst ); + + fprintf ( log, "Correcting RTC: %d seconds\n", rtc_sys_diff ); + + if ( ioctl ( fd, RTC_SET_TIME, &set ) < 0 ) + error_msg_and_die ( 1, "ioctl RTC_SET_TIME" ); + } + // set alarm time - if ( ioctl ( fd, RTC_ALM_SET, tm ) < 0 ) + if ( ioctl ( fd, RTC_ALM_SET, &alr ) < 0 ) error_msg_and_die ( 1, "ioctl RTC_ALM_SET" ); // enable alarm irq if ( ioctl ( fd, RTC_AIE_ON, 0 ) < 0 ) error_msg_and_die ( 1, "ioctl RTC_AIE_ON" ); diff --git a/root/etc/suspend-scripts/S46opiealarm b/root/etc/suspend-scripts/S46opiealarm index 9b936e4..a41dae5 100755 --- a/root/etc/suspend-scripts/S46opiealarm +++ b/root/etc/suspend-scripts/S46opiealarm @@ -1,5 +1,5 @@ #!/bin/sh # Starts opiealarm, which only runs while the iPaq sleeps # and wakes it up when the RTC alarm goes off. -/opt/QtPalmtop/bin/opiealarm -s +/opt/QtPalmtop/bin/opiealarm -s -f |