forked from apache/nuttx-apps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrivertest_rtc.c
447 lines (363 loc) · 13.7 KB
/
drivertest_rtc.c
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/****************************************************************************
* apps/testing/drivers/drivertest/drivertest_rtc.c
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you 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.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/ioctl.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sched.h>
#include <errno.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <stdint.h>
#include <cmocka.h>
#include <syslog.h>
#include <getopt.h>
#include <nuttx/timers/rtc.h>
#include <nuttx/clock.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define RTC_DEFAULT_DEVPATH "/dev/rtc0"
#define RTC_DEFAULT_DEVIATION 10
#define DEFAULT_TIME_OUT 5
#define SLEEPSECONDS 10
#define RTC_SIGNO 13
#define VENDOR_DELAY 0
#define OPTARG_TO_VALUE(value, type, base) \
do \
{ \
FAR char *ptr; \
value = (type)strtoul(optarg, &ptr, base); \
if (*ptr != '\0') \
{ \
printf("Parameter error: -%c %s\n", ch, optarg); \
show_usage(argv[0], rtc_state, EXIT_FAILURE); \
} \
} while (0)
/****************************************************************************
* Private Types
****************************************************************************/
struct rtc_state_s
{
char devpath[PATH_MAX];
int deviation;
int tim;
int vendor_delay;
};
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: show_usage
****************************************************************************/
static void show_usage(FAR const char *progname,
FAR struct rtc_state_s *rtc_state, int exitcode)
{
printf("Usage: %s -d <devpath> -a <deviation> -v <delay>\n", progname);
printf(" [-d devpath] selects the rtc device.\n"
" Default: %s Current: %s\n", RTC_DEFAULT_DEVPATH,
rtc_state->devpath);
printf(" [-a deviation] input rtc alarm .\n"
" Default: %d Current: %d\n", RTC_DEFAULT_DEVIATION,
rtc_state->deviation);
printf(" [-v delay] delay after set rtc.\n"
" Default: %d Current: %d\n", VENDOR_DELAY,
rtc_state->vendor_delay);
exit(exitcode);
}
/****************************************************************************
* Name: parse_commandline
****************************************************************************/
static void parse_commandline(FAR struct rtc_state_s *rtc_state, int argc,
FAR char **argv)
{
int ch;
int converted;
while ((ch = getopt(argc, argv, "d:a:v:")) != ERROR)
{
switch (ch)
{
case 'd':
strlcpy(rtc_state->devpath, optarg, sizeof(rtc_state->devpath));
break;
case 'a':
OPTARG_TO_VALUE(converted, int, 10);
rtc_state->deviation = converted;
if (converted < 0)
{
printf("deviation out of range: %d\n", converted);
show_usage(argv[0], rtc_state, EXIT_FAILURE);
}
break;
case 'v':
OPTARG_TO_VALUE(converted, int, 10);
rtc_state->vendor_delay = converted;
if (converted < 0)
{
printf("delay out of range: %d\n", converted);
show_usage(argv[0], rtc_state, EXIT_FAILURE);
}
break;
case '?':
printf("Unsupported option: %s\n", optarg);
show_usage(argv[0], rtc_state, EXIT_FAILURE);
}
}
}
/****************************************************************************
* Name: test_case_rtc_01
****************************************************************************/
static void test_case_rtc_01(FAR void **state)
{
int fd;
int ret;
bool have_set_time;
char timbuf[64];
struct rtc_time set_time;
struct rtc_time rd_time;
FAR struct rtc_state_s *rtc_state;
rtc_state = (FAR struct rtc_state_s *)*state;
fd = open(rtc_state->devpath, O_WRONLY);
assert_return_code(fd, 0);
memset(&set_time, 0, sizeof(set_time));
set_time.tm_year = 2000 - TM_YEAR_BASE;
set_time.tm_mon = TM_JANUARY;
set_time.tm_mday = 1;
set_time.tm_wday = TM_SATURDAY;
ret = ioctl(fd, RTC_SET_TIME, (unsigned long)((uintptr_t)&set_time));
assert_return_code(ret, OK);
ret = ioctl(fd, RTC_HAVE_SET_TIME,
(unsigned long)((uintptr_t)&have_set_time));
assert_return_code(ret, OK);
/* Some vendor need sleep a period of time
* after set rtc because of hardware bug.
*/
sleep(rtc_state->vendor_delay);
assert_true(have_set_time);
ret = ioctl(fd, RTC_RD_TIME, (unsigned long)((uintptr_t)&rd_time));
assert_return_code(ret, OK);
assert_int_equal(set_time.tm_year, rd_time.tm_year);
assert_int_equal(set_time.tm_mon, rd_time.tm_mon);
assert_int_equal(set_time.tm_mday, rd_time.tm_mday);
assert_int_equal(set_time.tm_wday, rd_time.tm_wday);
ret = strftime(timbuf, sizeof(timbuf), "%a, %b %d %H:%M:%S %Y",
(FAR struct tm *)&rd_time);
assert_return_code(ret, OK);
close(fd);
}
#if defined(CONFIG_RTC_ALARM) || defined(CONFIG_RTC_PERIODIC)
/****************************************************************************
* Name: get_timestamp
****************************************************************************/
static uint32_t get_timestamp(void)
{
struct timespec ts;
uint32_t ms;
clock_gettime(CLOCK_MONOTONIC, &ts);
ms = ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
return ms;
}
#endif
#ifdef CONFIG_RTC_ALARM
/****************************************************************************
* Name: add_timeout
****************************************************************************/
static void add_timeout(struct rtc_time * rtc_tm, const int delay)
{
time_t timesp;
FAR struct tm *tm;
timesp = timegm((struct tm *)rtc_tm);
timesp += delay;
tm = gmtime(×p);
rtc_tm->tm_sec = tm->tm_sec;
rtc_tm->tm_min = tm->tm_min;
rtc_tm->tm_hour = tm->tm_hour;
rtc_tm->tm_mday = tm->tm_mday;
rtc_tm->tm_mon = tm->tm_mon;
rtc_tm->tm_year = tm->tm_year;
rtc_tm->tm_wday = tm->tm_wday;
rtc_tm->tm_yday = tm->tm_yday;
rtc_tm->tm_isdst = tm->tm_isdst;
}
/****************************************************************************
* Name: test_case_rtc_02
****************************************************************************/
static void test_case_rtc_02(FAR void **state)
{
int fd;
int ret;
int alarmid = 0;
struct rtc_setalarm_s rtc_setalarm;
struct rtc_rdalarm_s rtc_rdalarm;
struct rtc_setrelative_s rtc_setrelative;
struct rtc_time set_time;
uint32_t before_timestamp;
uint32_t range;
sigset_t set;
FAR struct rtc_state_s *rtc_state =
(FAR struct rtc_state_s *)*state;
signal(RTC_SIGNO, SIG_IGN);
sigemptyset(&set);
sigaddset(&set, RTC_SIGNO);
fd = open(rtc_state->devpath, O_RDWR);
assert_return_code(fd, 0);
memset(&set_time, 0, sizeof(set_time));
set_time.tm_year = 2000 - TM_YEAR_BASE;
set_time.tm_mon = TM_JANUARY;
set_time.tm_mday = 1;
set_time.tm_wday = TM_SATURDAY;
/* Set rtc alarm */
rtc_setalarm.id = 0;
rtc_setalarm.pid = getpid();
rtc_setalarm.event.sigev_notify = SIGEV_SIGNAL;
rtc_setalarm.event.sigev_signo = RTC_SIGNO;
rtc_setalarm.event.sigev_value.sival_ptr = NULL;
rtc_setalarm.time = set_time;
add_timeout(&rtc_setalarm.time, DEFAULT_TIME_OUT);
ret = ioctl(fd, RTC_SET_TIME, (unsigned long)((uintptr_t)&set_time));
assert_return_code(ret, OK);
sleep(rtc_state->vendor_delay);
add_timeout(&rtc_setalarm.time, rtc_state->vendor_delay);
ret = ioctl(fd, RTC_SET_ALARM, &rtc_setalarm);
assert_return_code(ret, OK);
before_timestamp = get_timestamp();
sleep(rtc_state->vendor_delay);
/* Read alarm */
rtc_rdalarm.id = 0;
ret = ioctl(fd, RTC_RD_ALARM, &rtc_rdalarm);
assert_return_code(ret, OK);
assert_int_equal(timegm((struct tm *)&rtc_rdalarm.time),
timegm((struct tm *)&rtc_setalarm.time));
ret = sigwaitinfo(&set, NULL);
assert_return_code(ret, RTC_SIGNO);
range = abs(get_timestamp() - before_timestamp);
assert_in_range(range, DEFAULT_TIME_OUT * 1000 - rtc_state->deviation,
DEFAULT_TIME_OUT * 1000 + rtc_state->deviation);
/* Cancel rtc alarm */
ret = ioctl(fd, RTC_SET_TIME, (unsigned long)((uintptr_t)&set_time));
assert_return_code(ret, OK);
sleep(rtc_state->vendor_delay);
rtc_setalarm.time = set_time;
add_timeout(&rtc_setalarm.time, DEFAULT_TIME_OUT);
ret = ioctl(fd, RTC_SET_ALARM, &rtc_setalarm);
assert_return_code(ret, OK);
ret = ioctl(fd, RTC_CANCEL_ALARM, alarmid);
assert_return_code(ret, OK);
/* Set relative */
rtc_setrelative.id = 0;
rtc_setrelative.pid = getpid();
rtc_setrelative.event.sigev_notify = SIGEV_SIGNAL;
rtc_setrelative.event.sigev_signo = RTC_SIGNO;
rtc_setrelative.event.sigev_value.sival_ptr = NULL;
rtc_setrelative.reltime = DEFAULT_TIME_OUT;
ret = ioctl(fd, RTC_SET_RELATIVE, &rtc_setrelative);
assert_return_code(ret, OK);
before_timestamp = get_timestamp();
ret = sigwaitinfo(&set, NULL);
assert_return_code(ret, RTC_SIGNO);
range = abs(get_timestamp() - before_timestamp);
assert_in_range(range, DEFAULT_TIME_OUT * 1000 - rtc_state->deviation,
DEFAULT_TIME_OUT * 1000 + rtc_state->deviation);
close(fd);
}
#endif
#ifdef CONFIG_RTC_PERIODIC
/****************************************************************************
* Name: rtc_periodic_callback
****************************************************************************/
static void rtc_periodic_callback(union sigval arg)
{
FAR struct rtc_state_s * rtc_state =
(FAR struct rtc_state_s *)arg.sival_ptr;
int range = get_timestamp() - rtc_state->tim;
assert_in_range(range, DEFAULT_TIME_OUT * 1000 - rtc_state->deviation,
DEFAULT_TIME_OUT * 1000 + rtc_state->deviation);
syslog(LOG_DEBUG, "rtc periodic callback trigger!!!\n");
rtc_state->tim = get_timestamp();
}
/****************************************************************************
* Name: test_case_rtc_03
****************************************************************************/
static void test_case_rtc_03(FAR void **state)
{
int alarmid = 0;
int ret;
int fd;
struct rtc_setperiodic_s rtc_setperiodic;
FAR struct rtc_state_s *rtc_state =
(FAR struct rtc_state_s *)*state;
fd = open(rtc_state->devpath, O_RDWR);
assert_return_code(fd, OK);
/* Set periodic */
rtc_setperiodic.id = 0;
rtc_setperiodic.pid = getpid();
rtc_setperiodic.event.sigev_notify = SIGEV_THREAD;
rtc_setperiodic.event.sigev_notify_function = rtc_periodic_callback;
rtc_setperiodic.event.sigev_notify_attributes = NULL;
rtc_setperiodic.event.sigev_value.sival_ptr = rtc_state;
rtc_setperiodic.period.tv_sec = DEFAULT_TIME_OUT;
rtc_setperiodic.period.tv_nsec = 0;
ret = ioctl(fd, RTC_SET_PERIODIC, &rtc_setperiodic);
assert_return_code(ret, OK);
rtc_state->tim = get_timestamp();
sleep(SLEEPSECONDS);
/* Cancel periodic */
ret = ioctl(fd, RTC_CANCEL_PERIODIC, alarmid);
assert_return_code(ret, OK);
sleep(SLEEPSECONDS);
close(fd);
}
#endif
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: main
****************************************************************************/
int main(int argc, FAR char *argv[])
{
struct rtc_state_s rtc_state =
{
.devpath = RTC_DEFAULT_DEVPATH,
.deviation = RTC_DEFAULT_DEVIATION,
.vendor_delay = VENDOR_DELAY,
};
parse_commandline(&rtc_state, argc, argv);
const struct CMUnitTest tests[] =
{
cmocka_unit_test_prestate(test_case_rtc_01, &rtc_state),
#ifdef CONFIG_RTC_ALARM
cmocka_unit_test_prestate(test_case_rtc_02, &rtc_state),
#endif
#ifdef CONFIG_RTC_PERIODIC
cmocka_unit_test_prestate(test_case_rtc_03, &rtc_state),
#endif
};
return cmocka_run_group_tests(tests, NULL, NULL);
}