nuttx-apps/testing/drivertest/drivertest_rtc.c
anjiahao 59855a1a67 remove <nuttx/xxx.h> in stdio need add depend header file
libuv:
CC:  pthread/pthread_mutexattr_setrobust.c libuv/src/unix/thread.c: In function ‘uv_thread_create_ex’:
libuv/src/unix/thread.c:174:24: error: storage size of ‘param’ isn’t known
  174 |     struct sched_param param;
      |                        ^~~~~

btsak_main:
In file included from btsak_main.c:39:
btsak.h:149:1: error: unknown type name ‘bool’
  149 | bool btsak_str2bool(FAR const char *str);

pipe_main.c:
CC:  sim/sim_registerdump.c pipe_main.c:44:30: error: unknown type name ‘pthread_addr_t’
   44 | static void *open_write_only(pthread_addr_t pvarg)
      |                              ^~~~~~~~~~~~~~
pipe_main.c: In function ‘pipe_main’:
pipe_main.c:81:3: error: unknown type name ‘pthread_t’
   81 |   pthread_t writeonly;

redirect_test.c: In function ‘redirection_test’:
redirect_test.c:205:3: error: unknown type name ‘pthread_t’
  205 |   pthread_t readerid;
      |   ^~~~~~~~~
redirect_test.c:206:3: error: unknown type name ‘pthread_t’
  206 |   pthread_t writerid;
      |   ^~~~~~~~~

drivertest_posix_timer.c:48:29: error: ‘optarg’ undeclared (first use in this function)
   48 |       value = (type)strtoul(optarg, &ptr, base);                      \

drivertest_posix_timer.c:208:3: warning: implicit declaration of function ‘sleep’ [-Wimplicit-function-declaration]
  208 |   sleep(SLEEPSECONDS);
      |   ^~~~~

drivertest_uart.c:92:13: warning: implicit declaration of function ‘read’; did you mean ‘fread’? [-Wimplicit-function-declaration]
   92 |       len = read(fd, &tmp_char, 1);
      |             ^~~~
      |             fread

drivertest_uart.c:323:44: error: ‘optopt’ undeclared (first use in this function)
  323 |             printf("Unknown option: %c\n", optopt);

kernel/fs/cases/fs_getfilep_test.c:71:9: warning: implicit declaration of function ‘fs_getfilep’ [-Wimplicit-function-declaration]
   71 |   ret = fs_getfilep(fileno(fp), &filep);
      |         ^~~~~~~~~~~

kernel/mm/cases/mm_test_008.c:148:9: warning: implicit declaration of function ‘task_create’; did you mean ‘timer_create’? [-Wimplicit-function-declaration]
  148 |   pid = task_create("TestNuttx08_routine_1",
      |         ^~~~~~~~~~~
      |         timer_create

/home/ajh/work/nuttxwork/apps/testing/cmocka/cmocka_main.c:171:11: warning: implicit declaration of function ‘setenv’ [-Wimplicit-function-declaration]
  171 |           setenv("CMOCKA_XML_FILE", xml_path, 1);
      |           ^~~~~~

drivertest_pm_runtime.c:123:7: warning: implicit declaration of function ‘usleep’ [-Wimplicit-function-declaration]
  123 |       usleep(210 * 1000);
      |       ^~~~~~

Signed-off-by: anjiahao <anjiahao@xiaomi.com>
2024-11-13 16:49:12 +08:00

445 lines
14 KiB
C

/****************************************************************************
* apps/testing/drivertest/drivertest_rtc.c
*
* 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(&timesp);
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);
}