xref: /linux/tools/testing/selftests/rtc/setdate.c (revision a12ab9e125f184454fc484483fe2029962fe6a34)
1*a12ab9e1SAlexandre Belloni /* Real Time Clock Driver Test
2*a12ab9e1SAlexandre Belloni  *	by: Benjamin Gaignard (benjamin.gaignard@linaro.org)
3*a12ab9e1SAlexandre Belloni  *
4*a12ab9e1SAlexandre Belloni  * To build
5*a12ab9e1SAlexandre Belloni  *	gcc rtctest_setdate.c -o rtctest_setdate
6*a12ab9e1SAlexandre Belloni  *
7*a12ab9e1SAlexandre Belloni  *   This program is free software: you can redistribute it and/or modify
8*a12ab9e1SAlexandre Belloni  *   it under the terms of the GNU General Public License as published by
9*a12ab9e1SAlexandre Belloni  *   the Free Software Foundation, either version 2 of the License, or
10*a12ab9e1SAlexandre Belloni  *   (at your option) any later version.
11*a12ab9e1SAlexandre Belloni  *
12*a12ab9e1SAlexandre Belloni  *   This program is distributed in the hope that it will be useful,
13*a12ab9e1SAlexandre Belloni  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14*a12ab9e1SAlexandre Belloni  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*a12ab9e1SAlexandre Belloni  *   GNU General Public License for more details.
16*a12ab9e1SAlexandre Belloni  */
17*a12ab9e1SAlexandre Belloni 
18*a12ab9e1SAlexandre Belloni #include <stdio.h>
19*a12ab9e1SAlexandre Belloni #include <linux/rtc.h>
20*a12ab9e1SAlexandre Belloni #include <sys/ioctl.h>
21*a12ab9e1SAlexandre Belloni #include <sys/time.h>
22*a12ab9e1SAlexandre Belloni #include <sys/types.h>
23*a12ab9e1SAlexandre Belloni #include <fcntl.h>
24*a12ab9e1SAlexandre Belloni #include <unistd.h>
25*a12ab9e1SAlexandre Belloni #include <stdlib.h>
26*a12ab9e1SAlexandre Belloni #include <errno.h>
27*a12ab9e1SAlexandre Belloni 
28*a12ab9e1SAlexandre Belloni static const char default_time[] = "00:00:00";
29*a12ab9e1SAlexandre Belloni 
30*a12ab9e1SAlexandre Belloni int main(int argc, char **argv)
31*a12ab9e1SAlexandre Belloni {
32*a12ab9e1SAlexandre Belloni 	int fd, retval;
33*a12ab9e1SAlexandre Belloni 	struct rtc_time new, current;
34*a12ab9e1SAlexandre Belloni 	const char *rtc, *date;
35*a12ab9e1SAlexandre Belloni 	const char *time = default_time;
36*a12ab9e1SAlexandre Belloni 
37*a12ab9e1SAlexandre Belloni 	switch (argc) {
38*a12ab9e1SAlexandre Belloni 	case 4:
39*a12ab9e1SAlexandre Belloni 		time = argv[3];
40*a12ab9e1SAlexandre Belloni 		/* FALLTHROUGH */
41*a12ab9e1SAlexandre Belloni 	case 3:
42*a12ab9e1SAlexandre Belloni 		date = argv[2];
43*a12ab9e1SAlexandre Belloni 		rtc = argv[1];
44*a12ab9e1SAlexandre Belloni 		break;
45*a12ab9e1SAlexandre Belloni 	default:
46*a12ab9e1SAlexandre Belloni 		fprintf(stderr, "usage: rtctest_setdate <rtcdev> <DD-MM-YYYY> [HH:MM:SS]\n");
47*a12ab9e1SAlexandre Belloni 		return 1;
48*a12ab9e1SAlexandre Belloni 	}
49*a12ab9e1SAlexandre Belloni 
50*a12ab9e1SAlexandre Belloni 	fd = open(rtc, O_RDONLY);
51*a12ab9e1SAlexandre Belloni 	if (fd == -1) {
52*a12ab9e1SAlexandre Belloni 		perror(rtc);
53*a12ab9e1SAlexandre Belloni 		exit(errno);
54*a12ab9e1SAlexandre Belloni 	}
55*a12ab9e1SAlexandre Belloni 
56*a12ab9e1SAlexandre Belloni 	sscanf(date, "%d-%d-%d", &new.tm_mday, &new.tm_mon, &new.tm_year);
57*a12ab9e1SAlexandre Belloni 	new.tm_mon -= 1;
58*a12ab9e1SAlexandre Belloni 	new.tm_year -= 1900;
59*a12ab9e1SAlexandre Belloni 	sscanf(time, "%d:%d:%d", &new.tm_hour, &new.tm_min, &new.tm_sec);
60*a12ab9e1SAlexandre Belloni 
61*a12ab9e1SAlexandre Belloni 	fprintf(stderr, "Test will set RTC date/time to %d-%d-%d, %02d:%02d:%02d.\n",
62*a12ab9e1SAlexandre Belloni 		new.tm_mday, new.tm_mon + 1, new.tm_year + 1900,
63*a12ab9e1SAlexandre Belloni 		new.tm_hour, new.tm_min, new.tm_sec);
64*a12ab9e1SAlexandre Belloni 
65*a12ab9e1SAlexandre Belloni 	/* Write the new date in RTC */
66*a12ab9e1SAlexandre Belloni 	retval = ioctl(fd, RTC_SET_TIME, &new);
67*a12ab9e1SAlexandre Belloni 	if (retval == -1) {
68*a12ab9e1SAlexandre Belloni 		perror("RTC_SET_TIME ioctl");
69*a12ab9e1SAlexandre Belloni 		close(fd);
70*a12ab9e1SAlexandre Belloni 		exit(errno);
71*a12ab9e1SAlexandre Belloni 	}
72*a12ab9e1SAlexandre Belloni 
73*a12ab9e1SAlexandre Belloni 	/* Read back */
74*a12ab9e1SAlexandre Belloni 	retval = ioctl(fd, RTC_RD_TIME, &current);
75*a12ab9e1SAlexandre Belloni 	if (retval == -1) {
76*a12ab9e1SAlexandre Belloni 		perror("RTC_RD_TIME ioctl");
77*a12ab9e1SAlexandre Belloni 		exit(errno);
78*a12ab9e1SAlexandre Belloni 	}
79*a12ab9e1SAlexandre Belloni 
80*a12ab9e1SAlexandre Belloni 	fprintf(stderr, "\n\nCurrent RTC date/time is %d-%d-%d, %02d:%02d:%02d.\n",
81*a12ab9e1SAlexandre Belloni 		current.tm_mday, current.tm_mon + 1, current.tm_year + 1900,
82*a12ab9e1SAlexandre Belloni 		current.tm_hour, current.tm_min, current.tm_sec);
83*a12ab9e1SAlexandre Belloni 
84*a12ab9e1SAlexandre Belloni 	close(fd);
85*a12ab9e1SAlexandre Belloni 	return 0;
86*a12ab9e1SAlexandre Belloni }
87