Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /* Real Time Clock Driver Test
0003  *  by: Benjamin Gaignard (benjamin.gaignard@linaro.org)
0004  *
0005  * To build
0006  *  gcc rtctest_setdate.c -o rtctest_setdate
0007  */
0008 
0009 #include <stdio.h>
0010 #include <linux/rtc.h>
0011 #include <sys/ioctl.h>
0012 #include <sys/time.h>
0013 #include <sys/types.h>
0014 #include <fcntl.h>
0015 #include <unistd.h>
0016 #include <stdlib.h>
0017 #include <errno.h>
0018 
0019 static const char default_time[] = "00:00:00";
0020 
0021 int main(int argc, char **argv)
0022 {
0023     int fd, retval;
0024     struct rtc_time new, current;
0025     const char *rtc, *date;
0026     const char *time = default_time;
0027 
0028     switch (argc) {
0029     case 4:
0030         time = argv[3];
0031         /* FALLTHROUGH */
0032     case 3:
0033         date = argv[2];
0034         rtc = argv[1];
0035         break;
0036     default:
0037         fprintf(stderr, "usage: rtctest_setdate <rtcdev> <DD-MM-YYYY> [HH:MM:SS]\n");
0038         return 1;
0039     }
0040 
0041     fd = open(rtc, O_RDONLY);
0042     if (fd == -1) {
0043         perror(rtc);
0044         exit(errno);
0045     }
0046 
0047     sscanf(date, "%d-%d-%d", &new.tm_mday, &new.tm_mon, &new.tm_year);
0048     new.tm_mon -= 1;
0049     new.tm_year -= 1900;
0050     sscanf(time, "%d:%d:%d", &new.tm_hour, &new.tm_min, &new.tm_sec);
0051 
0052     fprintf(stderr, "Test will set RTC date/time to %d-%d-%d, %02d:%02d:%02d.\n",
0053         new.tm_mday, new.tm_mon + 1, new.tm_year + 1900,
0054         new.tm_hour, new.tm_min, new.tm_sec);
0055 
0056     /* Write the new date in RTC */
0057     retval = ioctl(fd, RTC_SET_TIME, &new);
0058     if (retval == -1) {
0059         perror("RTC_SET_TIME ioctl");
0060         close(fd);
0061         exit(errno);
0062     }
0063 
0064     /* Read back */
0065     retval = ioctl(fd, RTC_RD_TIME, &current);
0066     if (retval == -1) {
0067         perror("RTC_RD_TIME ioctl");
0068         exit(errno);
0069     }
0070 
0071     fprintf(stderr, "\n\nCurrent RTC date/time is %d-%d-%d, %02d:%02d:%02d.\n",
0072         current.tm_mday, current.tm_mon + 1, current.tm_year + 1900,
0073         current.tm_hour, current.tm_min, current.tm_sec);
0074 
0075     close(fd);
0076     return 0;
0077 }