1 /*
2 * Real Time Clock Driver Test/Example Program
3 *
4 * Compile with:
5 * gcc -s -Wall -Wstrict-prototypes rtctest.c -o rtctest
6 *
7 * Copyright (C) 1996, Paul Gortmaker.
8 *
9 * Released under the GNU General Public License, version 2,
10 * included herein by reference.
11 *
12 */
13
14 #include <stdio.h>
15 #include <linux/rtc.h>
16 #include <sys/ioctl.h>
17 #include <sys/time.h>
18 #include <sys/types.h>
19 #include <fcntl.h>
20 #include <unistd.h>
21 #include <stdlib.h>
22 #include <errno.h>
23
24 #ifndef ARRAY_SIZE
25 # define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
26 #endif
27
28 /*
29 * This expects the new RTC class driver framework, working with
30 * clocks that will often not be clones of what the PC-AT had.
31 * Use the command line to specify another RTC if you need one.
32 */
33 static const char default_rtc[] = "/dev/rtc0";
34
35 static struct rtc_time cutoff_dates[] = {
36 {
37 .tm_year = 70, /* 1970 -1900 */
38 .tm_mday = 1,
39 },
40 /* signed time_t 19/01/2038 3:14:08 */
41 {
42 .tm_year = 138,
43 .tm_mday = 19,
44 },
45 {
46 .tm_year = 138,
47 .tm_mday = 20,
48 },
49 {
50 .tm_year = 199, /* 2099 -1900 */
51 .tm_mday = 1,
52 },
53 {
54 .tm_year = 200, /* 2100 -1900 */
55 .tm_mday = 1,
56 },
57 /* unsigned time_t 07/02/2106 7:28:15*/
58 {
59 .tm_year = 205,
60 .tm_mon = 1,
61 .tm_mday = 7,
62 },
63 {
64 .tm_year = 206,
65 .tm_mon = 1,
66 .tm_mday = 8,
67 },
68 /* signed time on 64bit in nanoseconds 12/04/2262 01:47:16*/
69 {
70 .tm_year = 362,
71 .tm_mon = 3,
72 .tm_mday = 12,
73 },
74 {
75 .tm_year = 362, /* 2262 -1900 */
76 .tm_mon = 3,
77 .tm_mday = 13,
78 },
79 };
80
compare_dates(struct rtc_time * a,struct rtc_time * b)81 static int compare_dates(struct rtc_time *a, struct rtc_time *b)
82 {
83 if (a->tm_year != b->tm_year ||
84 a->tm_mon != b->tm_mon ||
85 a->tm_mday != b->tm_mday ||
86 a->tm_hour != b->tm_hour ||
87 a->tm_min != b->tm_min ||
88 ((b->tm_sec - a->tm_sec) > 1))
89 return 1;
90
91 return 0;
92 }
93
main(int argc,char ** argv)94 int main(int argc, char **argv)
95 {
96 int i, fd, retval, irqcount = 0, dangerous = 0;
97 unsigned long tmp, data;
98 struct rtc_time rtc_tm;
99 const char *rtc = default_rtc;
100 struct timeval start, end, diff;
101
102 switch (argc) {
103 case 3:
104 if (*argv[2] == 'd')
105 dangerous = 1;
106 case 2:
107 rtc = argv[1];
108 /* FALLTHROUGH */
109 case 1:
110 break;
111 default:
112 fprintf(stderr, "usage: rtctest [rtcdev] [d]\n");
113 return 1;
114 }
115
116 fd = open(rtc, O_RDONLY);
117
118 if (fd == -1) {
119 perror(rtc);
120 exit(errno);
121 }
122
123 fprintf(stderr, "\n\t\t\tRTC Driver Test Example.\n\n");
124
125 #ifdef __ANDROID__
126 /*
127 * Android does not rely on update interrupts so do not test them.
128 */
129 goto test_READ;
130 #endif
131
132 /* Turn on update interrupts (one per second) */
133 retval = ioctl(fd, RTC_UIE_ON, 0);
134 if (retval == -1) {
135 if (errno == EINVAL) {
136 fprintf(stderr,
137 "\n...Update IRQs not supported.\n");
138 goto test_READ;
139 }
140 perror("RTC_UIE_ON ioctl");
141 exit(errno);
142 }
143
144 fprintf(stderr, "Counting 5 update (1/sec) interrupts from reading %s:",
145 rtc);
146 fflush(stderr);
147 for (i=1; i<6; i++) {
148 /* This read will block */
149 retval = read(fd, &data, sizeof(unsigned long));
150 if (retval == -1) {
151 perror("read");
152 exit(errno);
153 }
154 fprintf(stderr, " %d",i);
155 fflush(stderr);
156 irqcount++;
157 }
158
159 fprintf(stderr, "\nAgain, from using select(2) on /dev/rtc:");
160 fflush(stderr);
161 for (i=1; i<6; i++) {
162 struct timeval tv = {5, 0}; /* 5 second timeout on select */
163 fd_set readfds;
164
165 FD_ZERO(&readfds);
166 FD_SET(fd, &readfds);
167 /* The select will wait until an RTC interrupt happens. */
168 retval = select(fd+1, &readfds, NULL, NULL, &tv);
169 if (retval == -1) {
170 perror("select");
171 exit(errno);
172 }
173 /* This read won't block unlike the select-less case above. */
174 retval = read(fd, &data, sizeof(unsigned long));
175 if (retval == -1) {
176 perror("read");
177 exit(errno);
178 }
179 fprintf(stderr, " %d",i);
180 fflush(stderr);
181 irqcount++;
182 }
183
184 /* Turn off update interrupts */
185 retval = ioctl(fd, RTC_UIE_OFF, 0);
186 if (retval == -1) {
187 perror("RTC_UIE_OFF ioctl");
188 exit(errno);
189 }
190
191 test_READ:
192 /* Read the RTC time/date */
193 retval = ioctl(fd, RTC_RD_TIME, &rtc_tm);
194 if (retval == -1) {
195 perror("RTC_RD_TIME ioctl");
196 exit(errno);
197 }
198
199 fprintf(stderr, "\n\nCurrent RTC date/time is %d-%d-%d, %02d:%02d:%02d.\n",
200 rtc_tm.tm_mday, rtc_tm.tm_mon + 1, rtc_tm.tm_year + 1900,
201 rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec);
202
203 /* Set the alarm to 5 sec in the future, and check for rollover */
204 rtc_tm.tm_sec += 5;
205 if (rtc_tm.tm_sec >= 60) {
206 rtc_tm.tm_sec %= 60;
207 rtc_tm.tm_min++;
208 }
209 if (rtc_tm.tm_min == 60) {
210 rtc_tm.tm_min = 0;
211 rtc_tm.tm_hour++;
212 }
213 if (rtc_tm.tm_hour == 24)
214 rtc_tm.tm_hour = 0;
215
216 retval = ioctl(fd, RTC_ALM_SET, &rtc_tm);
217 if (retval == -1) {
218 if (errno == EINVAL) {
219 fprintf(stderr,
220 "\n...Alarm IRQs not supported.\n");
221 goto test_PIE;
222 }
223
224 perror("RTC_ALM_SET ioctl");
225 exit(errno);
226 }
227
228 /* Read the current alarm settings */
229 retval = ioctl(fd, RTC_ALM_READ, &rtc_tm);
230 if (retval == -1) {
231 if (errno == EINVAL) {
232 fprintf(stderr,
233 "\n...EINVAL reading current alarm setting.\n");
234 goto test_PIE;
235 }
236 perror("RTC_ALM_READ ioctl");
237 exit(errno);
238 }
239
240 fprintf(stderr, "Alarm time now set to %02d:%02d:%02d.\n",
241 rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec);
242
243 /* Enable alarm interrupts */
244 retval = ioctl(fd, RTC_AIE_ON, 0);
245 if (retval == -1) {
246 if (errno == EINVAL || errno == EIO) {
247 fprintf(stderr,
248 "\n...Alarm IRQs not supported.\n");
249 goto test_PIE;
250 }
251
252 perror("RTC_AIE_ON ioctl");
253 exit(errno);
254 }
255
256 fprintf(stderr, "Waiting 5 seconds for alarm...");
257 fflush(stderr);
258 /* This blocks until the alarm ring causes an interrupt */
259 retval = read(fd, &data, sizeof(unsigned long));
260 if (retval == -1) {
261 perror("read");
262 exit(errno);
263 }
264 irqcount++;
265 fprintf(stderr, " okay. Alarm rang.\n");
266
267 /* Disable alarm interrupts */
268 retval = ioctl(fd, RTC_AIE_OFF, 0);
269 if (retval == -1) {
270 perror("RTC_AIE_OFF ioctl");
271 exit(errno);
272 }
273
274 #ifdef __ANDROID__
275 /*
276 * Android does not rely on periodic IRQs so do not test them.
277 */
278 goto done;
279 #endif
280
281 test_PIE:
282 /* Read periodic IRQ rate */
283 retval = ioctl(fd, RTC_IRQP_READ, &tmp);
284 if (retval == -1) {
285 /* not all RTCs support periodic IRQs */
286 if (errno == EINVAL) {
287 fprintf(stderr, "\nNo periodic IRQ support\n");
288 goto test_DATE;
289 }
290 perror("RTC_IRQP_READ ioctl");
291 exit(errno);
292 }
293 fprintf(stderr, "\nPeriodic IRQ rate is %ldHz.\n", tmp);
294
295 fprintf(stderr, "Counting 20 interrupts at:");
296 fflush(stderr);
297
298 /* The frequencies 128Hz, 256Hz, ... 8192Hz are only allowed for root. */
299 for (tmp=2; tmp<=64; tmp*=2) {
300
301 retval = ioctl(fd, RTC_IRQP_SET, tmp);
302 if (retval == -1) {
303 /* not all RTCs can change their periodic IRQ rate */
304 if (errno == EINVAL) {
305 fprintf(stderr,
306 "\n...Periodic IRQ rate is fixed\n");
307 goto test_DATE;
308 }
309 perror("RTC_IRQP_SET ioctl");
310 exit(errno);
311 }
312
313 fprintf(stderr, "\n%ldHz:\t", tmp);
314 fflush(stderr);
315
316 /* Enable periodic interrupts */
317 retval = ioctl(fd, RTC_PIE_ON, 0);
318 if (retval == -1) {
319 perror("RTC_PIE_ON ioctl");
320 exit(errno);
321 }
322
323 for (i=1; i<21; i++) {
324 gettimeofday(&start, NULL);
325 /* This blocks */
326 retval = read(fd, &data, sizeof(unsigned long));
327 if (retval == -1) {
328 perror("read");
329 exit(errno);
330 }
331 gettimeofday(&end, NULL);
332 timersub(&end, &start, &diff);
333 if (diff.tv_sec > 0 ||
334 diff.tv_usec > ((1000000L / tmp) * 1.10)) {
335 fprintf(stderr, "\nPIE delta error: %ld.%06ld should be close to 0.%06ld\n",
336 diff.tv_sec, diff.tv_usec,
337 (1000000L / tmp));
338 fflush(stdout);
339 exit(-1);
340 }
341
342 fprintf(stderr, " %d",i);
343 fflush(stderr);
344 irqcount++;
345 }
346
347 /* Disable periodic interrupts */
348 retval = ioctl(fd, RTC_PIE_OFF, 0);
349 if (retval == -1) {
350 perror("RTC_PIE_OFF ioctl");
351 exit(errno);
352 }
353 }
354
355 test_DATE:
356 if (!dangerous)
357 goto done;
358
359 fprintf(stderr, "\nTesting problematic dates\n");
360
361 for (i = 0; i < ARRAY_SIZE(cutoff_dates); i++) {
362 struct rtc_time current;
363
364 /* Write the new date in RTC */
365 retval = ioctl(fd, RTC_SET_TIME, &cutoff_dates[i]);
366 if (retval == -1) {
367 perror("RTC_SET_TIME ioctl");
368 close(fd);
369 exit(errno);
370 }
371
372 /* Read back */
373 retval = ioctl(fd, RTC_RD_TIME, ¤t);
374 if (retval == -1) {
375 perror("RTC_RD_TIME ioctl");
376 exit(errno);
377 }
378
379 if(compare_dates(&cutoff_dates[i], ¤t)) {
380 fprintf(stderr,"Setting date %d failed\n",
381 cutoff_dates[i].tm_year + 1900);
382 goto done;
383 }
384
385 cutoff_dates[i].tm_sec += 5;
386
387 /* Write the new alarm in RTC */
388 retval = ioctl(fd, RTC_ALM_SET, &cutoff_dates[i]);
389 if (retval == -1) {
390 perror("RTC_ALM_SET ioctl");
391 close(fd);
392 exit(errno);
393 }
394
395 /* Read back */
396 retval = ioctl(fd, RTC_ALM_READ, ¤t);
397 if (retval == -1) {
398 perror("RTC_ALM_READ ioctl");
399 exit(errno);
400 }
401
402 if(compare_dates(&cutoff_dates[i], ¤t)) {
403 fprintf(stderr,"Setting alarm %d failed\n",
404 cutoff_dates[i].tm_year + 1900);
405 goto done;
406 }
407
408 fprintf(stderr, "Setting year %d is OK \n",
409 cutoff_dates[i].tm_year + 1900);
410 }
411 done:
412 fprintf(stderr, "\n\n\t\t\t *** Test complete ***\n");
413
414 close(fd);
415
416 return 0;
417 }
418