-rw-r--r-- | core/opiealarm/opiealarm.c | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/core/opiealarm/opiealarm.c b/core/opiealarm/opiealarm.c new file mode 100644 index 0000000..071cb24 --- a/dev/null +++ b/core/opiealarm/opiealarm.c | |||
@@ -0,0 +1,91 @@ | |||
1 | /* opiealarm.c | ||
2 | * This program is for extracting the event time/date out | ||
3 | * of /etc/resumeat and setting the RTC alarm to that time/date. | ||
4 | * It is designed to run via a script just before the iPaq | ||
5 | * is suspended. | ||
6 | * | ||
7 | * Roughly based on ipaqalarm from Benjamin Long | ||
8 | * | ||
9 | * written by Robert Griebl <sandman@handhelds.org> | ||
10 | */ | ||
11 | |||
12 | #include <stdio.h> | ||
13 | #include <linux/rtc.h> | ||
14 | #include <sys/ioctl.h> | ||
15 | #include <sys/time.h> | ||
16 | #include <sys/types.h> | ||
17 | #include <fcntl.h> | ||
18 | #include <unistd.h> | ||
19 | #include <errno.h> | ||
20 | #include <time.h> | ||
21 | #include <stdlib.h> | ||
22 | |||
23 | |||
24 | void error_msg_and_die ( int perr, const char *msg ) | ||
25 | { | ||
26 | if ( perr ) | ||
27 | perror ( msg ); | ||
28 | else | ||
29 | fprintf ( stderr, "%s\n", msg ); | ||
30 | exit ( 1 ); | ||
31 | } | ||
32 | |||
33 | |||
34 | void extractevent ( ) | ||
35 | { | ||
36 | FILE *fp; | ||
37 | char buf [64]; | ||
38 | time_t t; | ||
39 | struct tm *tm; | ||
40 | int fd; | ||
41 | |||
42 | if (!( fp = fopen ( "/etc/resumeat", "r" ))) | ||
43 | error_msg_and_die ( 1, "/etc/resumeat" ); | ||
44 | |||
45 | if ( !fgets ( buf, sizeof( buf ) - 1, fp )) | ||
46 | error_msg_and_die ( 1, "/etc/resumeat" ); | ||
47 | |||
48 | fclose ( fp ); | ||
49 | |||
50 | t = atoi ( buf ); | ||
51 | |||
52 | if ( t == 0 ) | ||
53 | error_msg_and_die ( 0, "/etc/resumeat contains an invalid time description" ); | ||
54 | |||
55 | /* subtract 5 sec from event time... */ | ||
56 | t -= 5; | ||
57 | tm = gmtime ( &t ); | ||
58 | |||
59 | /* Write alarm time to RTC */ | ||
60 | fd = open ( "/dev/misc/rtc", O_RDWR ); | ||
61 | if ( fd < 0 ) | ||
62 | error_msg_and_die ( 1, "/dev/misc/rtc" ); | ||
63 | |||
64 | // set alarm time | ||
65 | if ( ioctl ( fd, RTC_ALM_SET, tm ) < 0 ) | ||
66 | error_msg_and_die ( 1, "ioctl RTC_ALM_SET" ); | ||
67 | |||
68 | // enable alarm irq | ||
69 | if ( ioctl ( fd, RTC_AIE_ON, 0 ) < 0 ) | ||
70 | error_msg_and_die ( 1, "ioctl RTC_AIE_ON" ); | ||
71 | |||
72 | // wait for alarm irq | ||
73 | if ( read ( fd, buf, sizeof( unsigned long )) < 0 ) | ||
74 | error_msg_and_die ( 1, "read rtc alarm" ); | ||
75 | |||
76 | // disable alarm irq | ||
77 | if ( ioctl ( fd, RTC_AIE_OFF, 0 ) < 0 ) | ||
78 | error_msg_and_die ( 1, "ioctl RTC_AIE_OFF" ); | ||
79 | |||
80 | close ( fd ); | ||
81 | } | ||
82 | |||
83 | |||
84 | int main ( ) | ||
85 | { | ||
86 | if ( geteuid ( ) != 0 ) | ||
87 | error_msg_and_die ( 0, "You need root priviledges to run opiealarm." ); | ||
88 | |||
89 | extractevent ( ); | ||
90 | return 0; | ||
91 | } | ||