Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

watchdog test #1940

Merged
merged 4 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions testing/drivertest/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ ifneq ($(CONFIG_SIG_EVTHREAD),)
MAINSRC += drivertest_posix_timer.c
PROGNAME += cmocka_posix_timer
endif

ifneq ($(CONFIG_WATCHDOG),)
ifneq ($(CONFIG_BOARDCTL_RESET_CAUSE),)
MAINSRC += drivertest_watchdog.c
PROGNAME += cmocka_driver_watchdog
endif
endif

ifneq ($(CONFIG_PWM),)
Expand Down
165 changes: 71 additions & 94 deletions testing/drivertest/drivertest_watchdog.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,34 +28,33 @@
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/boardctl.h>

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <debug.h>
#include <inttypes.h>

#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <stdint.h>
#include <cmocka.h>

#include <time.h>
#include <nuttx/timers/watchdog.h>
#include <nuttx/board.h>

/****************************************************************************
* Pre-processor Definitions
****************************************************************************/

#define WDG_DEFAULT_DEV_PATH "/dev/watchdog0"
#define WDG_DEFAULT_INFO_PATH "/data/wdg_info"
#define WDG_DEFAULT_PINGTIMER 5000
#define WDG_DEFAULT_PINGDELAY 500
#define WDG_DEFAULT_TIMEOUT 2000
#define WDG_DEFAULT_TESTCASE 0
#define WDG_DEFAULT_DEVIATION 20
#define WDG_COUNT_TESTCASE 4

#define OPTARG_TO_VALUE(value, type, base) \
do \
Expand All @@ -73,22 +72,14 @@
* Private Types
****************************************************************************/

enum wdg_reset_cause_e
{
WDG_RESET_CAUSE_NONE,
WDG_RESET_CAUSE_NO_KEEP_ALIVE,
WDG_RESET_CAUSE_CRITICAL_SECTION_BUSY_LOOP,
WDG_RESET_CAUSE_IRQ_BUSY_LOOP
};

struct wdg_state_s
{
char devpath[PATH_MAX];
char infopath[PATH_MAX];
uint32_t pingtime;
uint32_t pingdelay;
uint32_t timeout;
enum wdg_reset_cause_e reset_cause;
uint32_t deviation;
int test_case;
};

/****************************************************************************
Expand Down Expand Up @@ -135,52 +126,6 @@ static uint32_t get_time_elaps(uint32_t prev_tick)
return prev_tick;
}

/****************************************************************************
* Name: wdg_write_reset_cause
****************************************************************************/

static void wdg_write_reset_cause(FAR struct wdg_state_s *state)
{
int fd;
int ret;
ssize_t size;

fd = open(state->infopath, O_WRONLY | O_CREAT, 0666);
assert_true(fd > 0);

size = write(fd, &state->reset_cause, sizeof(state->reset_cause));
assert_int_equal(size, sizeof(state->reset_cause));

ret = fsync(fd);
assert_return_code(ret, OK);
ret = close(fd);
assert_return_code(ret, OK);
}

/****************************************************************************
* Name: wdg_read_reset_cause
****************************************************************************/

static void wdg_read_reset_cause(FAR struct wdg_state_s *state)
{
int fd;
int ret;
ssize_t size;

fd = open(state->infopath, O_RDONLY);
if (fd < 0)
{
state->reset_cause = WDG_RESET_CAUSE_NONE;
return;
}

size = read(fd, &state->reset_cause, sizeof(state->reset_cause));
assert_int_equal(size, sizeof(state->reset_cause));

ret = close(fd);
assert_return_code(ret, OK);
}

/****************************************************************************
* Name: wdg_init
****************************************************************************/
Expand Down Expand Up @@ -215,14 +160,14 @@ static int wdg_init(FAR struct wdg_state_s *state)
static void show_usage(FAR const char *progname,
FAR struct wdg_state_s *wdg_state, int exitcode)
{
printf("Usage: %s -d <devpath> -p <infopath> -t <pingtime>"
"-l <pingdelay> -o <timeout>\n", progname);
printf("Usage: %s -d <devpath> -r <test case> -t <pingtime>"
"-l <pingdelay> -o <timeout> -a <deviation>\n", progname);
printf(" [-d devpath] selects the WATCHDOG device.\n"
" Default: %s Current: %s\n", WDG_DEFAULT_DEV_PATH,
wdg_state->devpath);
printf(" [-p infopath] selects the WATCHDOG reset cause path.\n"
" Default: %s Current: %s\n", WDG_DEFAULT_INFO_PATH,
wdg_state->infopath);
printf(" [-r test_case] selects the testcase.\n"
" Default: %d Current: %d\n", WDG_DEFAULT_TESTCASE,
wdg_state->test_case);
printf(" [-t pingtime] Selects the <delay> time in milliseconds.\n"
" Default: %d Current: %" PRIu32 "\n",
WDG_DEFAULT_PINGTIMER, wdg_state->pingtime);
Expand All @@ -232,6 +177,9 @@ static void show_usage(FAR const char *progname,
printf(" [-o timeout] Time in milliseconds that the testcase will\n"
" Default: %d Current: %" PRIu32 "\n",
WDG_DEFAULT_TIMEOUT, wdg_state->timeout);
printf(" [-a deviation] Watchdog getstatus precision.\n"
" Default: %d Current: %" PRIu32 "\n",
WDG_DEFAULT_DEVIATION, wdg_state->deviation);
printf(" [-h] = Shows this message and exits\n");

exit(exitcode);
Expand All @@ -247,7 +195,7 @@ static void parse_commandline(FAR struct wdg_state_s *wdg_state, int argc,
int ch;
int converted;

while ((ch = getopt(argc, argv, "d:p:t:l:o:h")) != ERROR)
while ((ch = getopt(argc, argv, "d:r:t:l:o:a:h")) != ERROR)
{
switch (ch)
{
Expand All @@ -258,6 +206,16 @@ static void parse_commandline(FAR struct wdg_state_s *wdg_state, int argc,
case 'p':
strlcpy(wdg_state->infopath, optarg,
sizeof(wdg_state->infopath));
case 'r':
OPTARG_TO_VALUE(converted, uint32_t, 10);
if (converted < WDG_DEFAULT_TESTCASE ||
converted >= WDG_COUNT_TESTCASE)
{
printf("signal out of range: %d\n", converted);
show_usage(argv[0], wdg_state, EXIT_FAILURE);
}

wdg_state->test_case = converted;
break;

case 't':
Expand Down Expand Up @@ -286,13 +244,24 @@ static void parse_commandline(FAR struct wdg_state_s *wdg_state, int argc,
OPTARG_TO_VALUE(converted, uint32_t, 10);
if (converted < 1 || converted > INT_MAX)
{
printf("signal out of range: %d", converted);
printf("signal out of range: %d\n", converted);
show_usage(argv[0], wdg_state, EXIT_FAILURE);
}

wdg_state->timeout = (uint32_t)converted;
break;

case 'a':
OPTARG_TO_VALUE(converted, uint32_t, 10);
if (converted < 1 || converted > INT_MAX)
{
printf("signal out of range: %d\n", converted);
show_usage(argv[0], wdg_state, EXIT_FAILURE);
}

wdg_state->deviation = (uint32_t)converted;
break;

case '?':
printf("Unsupported option: %s\n", optarg);
show_usage(argv[0], wdg_state, EXIT_FAILURE);
Expand All @@ -315,18 +284,20 @@ static void test_case_wdog_01(FAR void **state)
int ret;
uint32_t start_ms;
FAR struct wdg_state_s *wdg_state;
struct boardioc_reset_cause_s reset_cause;

wdg_state = (FAR struct wdg_state_s *)*state;

wdg_read_reset_cause(wdg_state);

/* If it's not the first step, skip... */

if (wdg_state->reset_cause != WDG_RESET_CAUSE_NONE)
if (wdg_state->test_case != 0)
{
return;
}

boardctl(BOARDIOC_RESET_CAUSE, (uintptr_t)&reset_cause);
assert_int_equal(reset_cause.cause, BOARDIOC_RESETCAUSE_SYS_CHIPPOR);

dev_fd = wdg_init(wdg_state);

/* Get the starting time */
Expand All @@ -347,11 +318,6 @@ static void test_case_wdog_01(FAR void **state)
assert_return_code(ret, OK);
}

/* write the reset cause */

wdg_state->reset_cause = WDG_RESET_CAUSE_NO_KEEP_ALIVE;
wdg_write_reset_cause(wdg_state);

/* Then stop pinging */

/* Sleep for the requested amount of time */
Expand All @@ -372,20 +338,19 @@ static void test_case_wdog_01(FAR void **state)
static void test_case_wdog_02(FAR void **state)
{
FAR struct wdg_state_s *wdg_state;
struct boardioc_reset_cause_s reset_cause;

wdg_state = (FAR struct wdg_state_s *)*state;

wdg_read_reset_cause(wdg_state);

if (wdg_state->reset_cause != WDG_RESET_CAUSE_NO_KEEP_ALIVE)
if (wdg_state->test_case != 1)
{
return;
}

wdg_init(wdg_state);
boardctl(BOARDIOC_RESET_CAUSE, (uintptr_t)&reset_cause);
assert_int_equal(reset_cause.cause, BOARDIOC_RESETCAUSE_SYS_RWDT);

wdg_state->reset_cause = WDG_RESET_CAUSE_CRITICAL_SECTION_BUSY_LOOP;
wdg_write_reset_cause(wdg_state);
wdg_init(wdg_state);

enter_critical_section();

Expand Down Expand Up @@ -418,20 +383,19 @@ static void test_case_wdog_03(FAR void **state)
int ret;
static struct wdog_s wdog;
FAR struct wdg_state_s *wdg_state;
struct boardioc_reset_cause_s reset_cause;

wdg_state = (FAR struct wdg_state_s *)*state;

wdg_read_reset_cause(wdg_state);

if (wdg_state->reset_cause != WDG_RESET_CAUSE_CRITICAL_SECTION_BUSY_LOOP)
if (wdg_state->test_case != 2)
{
return;
}

wdg_init(wdg_state);
boardctl(BOARDIOC_RESET_CAUSE, (uintptr_t)&reset_cause);
assert_int_equal(reset_cause.cause, BOARDIOC_RESETCAUSE_SYS_RWDT);

wdg_state->reset_cause = WDG_RESET_CAUSE_IRQ_BUSY_LOOP;
wdg_write_reset_cause(wdg_state);
wdg_init(wdg_state);

ret = wd_start(&wdog, 1, wdg_wdentry, (wdparm_t)0);
assert_return_code(ret, OK);
Expand All @@ -457,14 +421,15 @@ static void test_case_wdog_04(FAR void **state)
int ret;
uint32_t start_ms;
FAR struct wdg_state_s *wdg_state;
struct watchdog_status_s status;
struct boardioc_reset_cause_s reset_cause;

wdg_state = (FAR struct wdg_state_s *)*state;

wdg_read_reset_cause(wdg_state);
assert_int_equal(wdg_state->reset_cause, WDG_RESET_CAUSE_IRQ_BUSY_LOOP);
assert_int_equal(wdg_state->test_case, 3);

ret = remove(wdg_state->infopath);
assert_return_code(ret, OK);
boardctl(BOARDIOC_RESET_CAUSE, (uintptr_t)&reset_cause);
assert_int_equal(reset_cause.cause, BOARDIOC_RESETCAUSE_SYS_RWDT);

dev_fd = wdg_init(wdg_state);

Expand All @@ -480,6 +445,17 @@ static void test_case_wdog_04(FAR void **state)

usleep(wdg_state->pingdelay * 1000);

/* Get Status */

ret = ioctl(dev_fd, WDIOC_GETSTATUS, &status);
assert_return_code(ret, OK);

assert_int_equal(status.timeout, wdg_state->timeout);
assert_in_range(
status.timeout - status.timeleft,
wdg_state->pingdelay - wdg_state->deviation,
wdg_state->pingdelay + wdg_state->deviation);

/* Then ping */

ret = ioctl(dev_fd, WDIOC_KEEPALIVE, 0);
Expand Down Expand Up @@ -508,10 +484,11 @@ int main(int argc, FAR char *argv[])
struct wdg_state_s wdg_state =
{
.devpath = WDG_DEFAULT_DEV_PATH,
.infopath = WDG_DEFAULT_INFO_PATH,
.pingtime = WDG_DEFAULT_PINGTIMER,
.pingdelay = WDG_DEFAULT_PINGDELAY,
.timeout = WDG_DEFAULT_TIMEOUT
.timeout = WDG_DEFAULT_TIMEOUT,
.test_case = WDG_DEFAULT_TESTCASE,
.deviation = WDG_DEFAULT_DEVIATION
};

parse_commandline(&wdg_state, argc, argv);
Expand Down
Loading