xref: /OK3568_Linux_fs/external/rk_pcba_test/echo_rtc_test.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  *  rtc_test.c  --  rtc test application
3  *
4  *  Copyright (c) 2017 Rockchip Electronics Co. Ltd.
5  *  Author: Panzhenzhuan Wang <randy.wang@rock-chips.com>
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * 	 http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 //RTC�����ܳ�����������ϵͳʱ��ͻ�ȡϵͳʱ��
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 //time���ͷ�ļ�
24 #include <time.h>
25 #include <sys/time.h>
26 //open()���ͷ�ļ�
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <fcntl.h>
30 //error���ͷ�ļ�
31 #include <errno.h>
32 //RTC�й�ͷ�ļ�
33 #include <linux/rtc.h>
34 #include <sys/ioctl.h>
35 #include <unistd.h>
36 #include "rtc_test.h"
37 
38 #define LOG_TAG "rtc_test"
39 #include "common.h"
40 #define RTC_PROC_ERR -95
41 
42 #define RTC_TIME_DIFF 1
43 
44 #define MAJOR_RTC_PATH "/dev/rtc"
45 #define MINOR_RTC_PATH "/dev/rtc0"
46 #define MAX_TEST 2
47 
48 //* ��ȡRTC��ȡ·��
rtc_xopen(int flags)49 int rtc_xopen(int flags)
50 {
51 	int rtc_fd;
52 
53 	rtc_fd = open(MAJOR_RTC_PATH, flags);
54 	if (rtc_fd < 0) {
55 		rtc_fd = open(MINOR_RTC_PATH, flags);
56 		if (rtc_fd < 0) {
57 			printf("open %s failed:%s\n", MINOR_RTC_PATH,
58 			       strerror(errno));
59 		}
60 	}
61 
62 	else {
63 		printf("open %s\n", MAJOR_RTC_PATH);
64 	}
65 	return rtc_fd;
66 }
67 
68 //* ����ϵͳʱ��
set_system_time(struct tm * tm_p)69 int set_system_time(struct tm *tm_p)
70 {
71     int ret;
72     int fd;
73 
74     //fd = rtc_xopen(O_RDWR|O_NOCTTY|O_NDELAY);
75     fd = open(MINOR_RTC_PATH, O_RDWR|O_NOCTTY|O_NDELAY);
76     if (fd < 0)
77     {
78         printf("open %s failed:%s\n", MINOR_RTC_PATH,strerror(errno));
79         return -1;
80     }
81 
82     //ʹ��RTC��Ӧ��ioctl��������ʱ��
83     ret = ioctl(fd, RTC_SET_TIME, tm_p);
84     if (ret < 0)
85     {
86         printf("set rtc failed:%s\n", strerror(errno));
87         return -1;
88     }
89 
90     close(fd);
91 
92     return 0;
93 }
94 
95 //1��rtc����ϵͳʱ�����
rtc_set_system_time_test(char * time_set,time_t * local_time)96 int rtc_set_system_time_test(char *time_set, time_t *local_time)
97 {
98     char *s;
99     int day, hour;
100     struct tm tm_p;
101 
102     printf("==========rtc_set_system_time_test start===========\n");
103     s = time_set;
104     //�����ꡢ�¡���,�õ����Ǹ���ʱ��
105     day = atoi(s);
106     //printf("day value is %d\n",day);
107     tm_p.tm_year = day/10000 - 1900;
108     tm_p.tm_mon = (day%10000)/100 - 1;
109     tm_p.tm_mday = (day%100);
110 
111     //ѭ������С����������ֵ����ʱ����ֵ
112     while (*s && *s != '.')
113         s++;
114     if (*s)
115         s++;
116 
117     //����ʱ���֡���
118     hour = atoi(s);
119     //printf("hour value is %d\n",hour);
120 
121     tm_p.tm_hour = hour/10000;
122     tm_p.tm_min = (hour%10000)/100;
123     tm_p.tm_sec = (hour%100);
124     tm_p.tm_isdst = -1;
125 
126     //mktime()�����γɱ���ʱ��
127     *local_time = mktime(&tm_p);
128 
129     //struct tm *��ʽ���룬��ӡ���õı���ʱ��
130     //printf("asctime(&tm_p) is %s\n",asctime(&tm_p));
131     //time_t *��ʽ���룬��ӡ���õı���ʱ��
132     //printf("ctime(local_time) is %s\n",ctime(local_time));
133 
134     /*1������ϵͳʱ��*/
135     int ret;
136     ret = set_system_time(&tm_p);
137     if (ret < 0)
138     {
139         //printf("test rtc failed:set_system_time failed.\n");
140         ret = -1;
141     }
142     else
143     {
144         //printf("rtc test: rtc_set_system_time success.\n");
145         ret = 0;
146     }
147     printf("==========rtc_set_system_time_test finish===========\n\n");
148     return ret;
149 }
150 
151 //2����ȡϵͳʱ��
rtc_get_system_time_test(time_t * local_time)152 int rtc_get_system_time_test(time_t *local_time)
153 {
154     int fd,ret;
155     struct tm tm_p;
156 
157     //printf("==========rtc_get_system_time_test start===========\n");
158     //fd = rtc_xopen(O_RDONLY|O_NOCTTY|O_NDELAY);
159     fd = open(MINOR_RTC_PATH, O_RDONLY);
160     if (fd < 0)
161     {
162         printf("open %s failed:%s\n", MINOR_RTC_PATH,strerror(errno));
163         return -1;
164     }
165 
166     //ʹ��RTC��Ӧ��ioctl������ȡϵͳʱ��
167     memset(&tm_p,0,sizeof(tm_p));
168 
169     ret = ioctl(fd, RTC_RD_TIME, &tm_p);
170     if (ret < 0)
171     {
172         printf("test rtc failed:get_system_time failed: %s\n",strerror(errno));
173         return -1;
174     }
175 	else
176     {
177         //printf("rtc test: rtc_get_system_time success.\n");
178         *local_time = mktime(&tm_p);  //ʹ��mktime�����õ�time_t��ʽ�����
179 
180         //printf("test time is: %04d-%02d-%02d %02d:%02d:%02d\n", 1900+tm_p.tm_year,
181         //       1 + tm_p.tm_mon, tm_p.tm_mday, tm_p.tm_hour, tm_p.tm_min, tm_p.tm_sec);
182     }
183 
184     //printf("System time is :%s\n",asctime(&tm_p));
185     close(fd);
186     //printf("==========rtc_get_system_time_test finish.===========\n");
187     return 0;
188 }
189 
rtc_test(void * argv)190 void *rtc_test(void *argv)
191 {
192     char time_set[32] = {"20180101.120000" };   //��Ҫ���õ�ϵͳʱ����š�.��ǰ���������գ�������ʱ����
193     int ret =0;
194     time_t lt_set,lt_get1,lt_get2;
195     char cmd[128];
196     int count = 0;
197 
198     //printf("=================rtc test start=================\n\n");
199     //sprintf(cmd,"aplay %s/rtc_test_start.wav",AUDIO_PATH);
200     //system(cmd);
201     //system("aplay /data/test/rtc_test_start.wav");
202     //* 1����������ϵͳʱ��*/
203     ret = rtc_set_system_time_test(time_set,&lt_set);
204     if(ret<0)
205         goto fail;
206     sleep(RTC_TIME_DIFF); //����RTC_TIME_DIFF s
207     lt_get1 = lt_set;
208     //* 2��Ȼ���ȡϵͳʱ�䡢�Ӷ�֪��RTC�Ƿ���������*/
209     ret = rtc_get_system_time_test(&lt_get2);
210     if(ret<0)
211         goto fail;
212     //printf("lt_get2-lt_get1 = %d\n",lt_get2-lt_get1);
213     //printf("lt_set is \t: %s\n",ctime(&lt_set));
214     //printf("lt_get2 is \t: %s\n",ctime(&lt_get2));
215 
216     //��������s����RTC�����������ӣ�������ǣ������ʧ��
217     if(RTC_TIME_DIFF>(lt_get2%60-lt_get1%60)){
218         ret = -1;
219         goto fail;
220     }
221     //printf("=================rtc test success=================\n");
222 
223     return (void*)ret;
224     fail:
225         printf("=================rtc test failed=================\n");
226         return (void*)ret;
227 }
228 
229 
main(int argc,char * argv[])230 int main(int argc, char *argv[])
231 {
232     int test_flag = 0,err_code = 0;
233     char buf[COMMAND_VALUESIZE] = "rtc_test";
234     char result[COMMAND_VALUESIZE] = RESULT_PASS;
235     test_flag = (int)rtc_test(argv[0]);
236     if(test_flag < 0)
237     {
238         strcpy(result,RESULT_FAIL);
239         err_code = RTC_PROC_ERR;
240     }
241     send_msg_to_server(buf, result, err_code);
242 }
243 
244