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