Add TODO
[SmartMotor.git] / SmartMotor-2.0-MiniGui / src / rtc.c
blobde7f10e1616647e4edeb62945d29e59371d9931e
1 /*
2 * src/rtc.c
4 * RTC functions
6 * Copyright 2007-2008 @ xianweizeng@gmail.com
8 * Author:
9 * Zeng Xianwei
11 * File Coding system: gb2312
12 * set-buffer-file-coding-system
14 * ChangeLog:
15 * 2009-01-12 V0.1
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <stdarg.h>
20 #include <string.h>
21 #include <sys/stat.h>
22 #include <sys/time.h>
23 #include <sys/types.h>
24 #include <unistd.h>
25 #include <pwd.h>
26 #include <errno.h>
27 #include <time.h>
29 #include <unistd.h>
30 #include <sys/ioctl.h>
31 #include <fcntl.h>
32 #include <sys/select.h>
34 #include <minigui/common.h>
35 #include <minigui/minigui.h>
36 #include <minigui/gdi.h>
37 #include <minigui/window.h>
38 #include <minigui/control.h>
40 #include "motor.h"
42 struct rtc_tm {
43 int tm_sec; /* Seconds. [0-60] (1 leap second) */
44 int tm_min; /* Minutes. [0-59] */
45 int tm_hour; /* Hours. [0-23] */
46 int tm_mday; /* Day. [1-31] */
47 int tm_mon; /* Month. [0-11] */
48 int tm_year; /* Year - 1900. */
49 int tm_wday; /* Day of week. [0-6] */
53 /* PCF8563 device */
54 #define RTC_DEVICE "/dev/rtc"
57 /* RTC ioctl commands macro
58 * See kernel driver source code
60 #define RTC_GETDATETIME 0
61 #define RTC_SETDATE 1
62 #define RTC_SETTIME 2
63 #define RTC_SETDATETIME 3
64 #define RTC_GETCTRL 4
65 #define RTC_SETCTRL 5
68 *get system time from RTC chip
70 static int getSysRtcTime(struct rtc_tm *tm)
72 int fd,ret;
74 fd = open(RTC_DEVICE, O_RDONLY);
75 if(fd < 0){
76 perror("error:can not open rtc devicen");
77 return(-1);
80 ret = ioctl(fd, RTC_GETDATETIME, tm);
81 if(ret < 0){
82 perror("Get time error.\n");
83 close(fd);
84 return -1;
87 close(fd);
88 return(0);
92 * Set system time to RTC chip
94 static int setSysRtcTime(struct rtc_tm *tm)
96 int fd, ret;
98 fd = open(RTC_DEVICE, O_RDWR);
99 if(fd < 0){
100 perror("error: can not open rtc devicen.");
101 return -1;
104 ret = ioctl(fd, RTC_SETDATETIME, tm);
105 if(ret < 0){
106 perror("Get time error.\n");
107 close(fd);
108 return -1;
111 close(fd);
112 return 0;
116 #if 0
118 * This function is obsoleted by setSysRtcTime()
121 /* set RTC date or time.
123 * flag = 0 time 1 date
125 static int setSysRtcDateTime(struct rtc_tm *tm,int flag)
127 int fd, ret;
129 /* printf("Open RTC devices.\n"); */
130 fd=open(RTC_DEVICE, O_RDWR);
131 if(fd < 0){
132 perror("error:can not open rtc device: %s\n");
133 return(-1);
136 ret = ioctl(fd,flag,tm);
137 if(ret < 0){
138 perror("Set system datetime error.\n");
139 close(fd);
140 return -1;
143 close(fd);
144 return 0;
146 #endif
149 * set rtc time to 2009-01-01 00:00:01
150 * call this when do a new release
152 int init_rtc_time(void)
154 struct rtc_tm tm;
155 int ret;
157 tm.tm_sec = 1;
158 tm.tm_min = 0;
159 tm.tm_hour = 0;
160 tm.tm_mday = 1;
161 tm.tm_mon = 1;
162 tm.tm_year = 2009 - 1900;
164 /* set RTC time */
165 ret = setSysRtcTime(&tm);
166 if(!ret) {
167 printf("Set RTC time error: %d \n", ret);
168 return -1;
171 return 0;
174 #ifndef CONFIG_DEBUG_ON_PC
176 * update Linux system time by RTC time.
177 * read RTC and then call settimeofday().
179 * This should be done in Linux kernel. But we can do it here
180 * if the RTC driver is not well writen.
182 int update_systime(void)
184 struct rtc_tm rtc_tm;
185 struct tm tm;
186 struct timeval tv;
187 int ret;
189 /* get system time */
190 gettimeofday(&tv, NULL);
192 ret = getSysRtcTime(&rtc_tm);
193 if(ret < 0) {
194 printf("Read RTC time error.\n");
196 init_rtc_time();
197 return -1;
200 tm.tm_sec = rtc_tm.tm_sec;
201 tm.tm_min = rtc_tm.tm_min;
202 tm.tm_hour = rtc_tm.tm_hour;
203 tm.tm_mday = rtc_tm.tm_mday;
204 tm.tm_mon = rtc_tm.tm_mon - 1;
205 tm.tm_year = rtc_tm.tm_year - 1900;
207 tv.tv_sec = mktime(&tm);
209 if (tv.tv_sec == -1) {
210 printf("mktime error\n");
213 /* tv.tv_usec = 0; */
215 ret = settimeofday(&tv, NULL);
217 if(ret == 0) {
218 printf("update system time successful.\n");
219 } else {
220 printf("update system time failed.\n");
221 return -1;
224 return 0;
228 * update RTC time by system time
230 int update_rtctime(void)
232 struct rtc_tm rtc_tm;
233 struct tm * tm;
234 struct timeval tv;
235 int ret;
236 time_t curtime;
238 /* get system time */
239 ret = gettimeofday(&tv, NULL);
240 if(ret < 0) {
241 printf("get system time error.\n");
242 return -1;
245 curtime = tv.tv_sec;
246 tm = localtime(&curtime);
248 rtc_tm.tm_sec = tm->tm_sec;
249 rtc_tm.tm_min = tm->tm_min;
250 rtc_tm.tm_hour = tm->tm_hour;
251 rtc_tm.tm_mday = tm->tm_mday;
252 rtc_tm.tm_mon = tm->tm_mon + 1;
253 rtc_tm.tm_year = tm->tm_year;
255 ret = setSysRtcTime(&rtc_tm);
256 if(ret < 0) {
257 printf("set RTC time error.\n");
258 return -1;
261 return 0;
263 #else
264 int update_systime(void)
266 return 0;
269 int update_rtctime(void)
271 return 0;
273 #endif
277 * convert time to string
280 char * convertSysTime(char *dispBuf)
282 struct timeval tv;
283 time_t curtime;
285 gettimeofday(&tv, NULL);
286 curtime = tv.tv_sec;
288 strftime(dispBuf, 30, "%Y-%m-%d %T", localtime(&curtime));
290 return(dispBuf);