summaryrefslogtreecommitdiffstats
path: root/commands/wd.c
blob: c186244b2b3a1c726c63df32aae9ffb15b1b9b47 (plain)
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
// SPDX-License-Identifier: GPL-2.0-or-later
// SPDX-FileCopyrightText: © 2012 Juergen Beisert <kernel@pengutronix.de>

#include <common.h>
#include <command.h>
#include <errno.h>
#include <linux/ctype.h>
#include <getopt.h>
#include <complete.h>
#include <watchdog.h>

/* default timeout in [sec] */
static unsigned timeout = CONFIG_CMD_WD_DEFAULT_TIMOUT;

static int do_wd(int argc, char *argv[])
{
	struct watchdog *wd = watchdog_get_default();
	int opt;
	int rc;

	while ((opt = getopt(argc, argv, "d:")) > 0) {
		switch (opt) {
		case 'd':
			wd = watchdog_get_by_name(optarg);
			break;
		default:
			return COMMAND_ERROR_USAGE;
		}
	}

	if (optind < argc) {
		if (isdigit(*argv[optind])) {
			timeout = simple_strtoul(argv[optind], NULL, 0);
		} else {
			printf("numerical parameter expected\n");
			return COMMAND_ERROR_USAGE;
		}
	}

	rc = watchdog_set_timeout(wd, timeout);
	if (rc < 0) {
		switch (rc) {
		case -EINVAL:
			printf("Timeout value out of range\n");
			break;
		case -ENOSYS:
			printf("Watchdog cannot be disabled\n");
			break;
		case -ENODEV:
			printf("Watchdog device doesn't exist.\n");
			break;
		default:
			printf("Watchdog fails: '%s'\n", strerror(-rc));
			break;
		}

		return COMMAND_ERROR;
	}

	return 0;
}

BAREBOX_CMD_HELP_START(wd)
BAREBOX_CMD_HELP_TEXT("Enable the watchdog to bark in TIME seconds.")
BAREBOX_CMD_HELP_TEXT("When TIME is 0, the watchdog gets disabled,")
BAREBOX_CMD_HELP_TEXT("Without a parameter the watchdog will be re-triggered.")
BAREBOX_CMD_HELP_TEXT("Options:")
BAREBOX_CMD_HELP_OPT("-d DEVICE\t", "watchdog name (default is highest priority watchdog)")
BAREBOX_CMD_HELP_END

BAREBOX_CMD_START(wd)
	.cmd = do_wd,
	BAREBOX_CMD_DESC("enable/disable/trigger the watchdog")
	BAREBOX_CMD_OPTS("[-d DEVICE] [TIME]")
	BAREBOX_CMD_GROUP(CMD_GRP_HWMANIP)
	BAREBOX_CMD_HELP(cmd_wd_help)
	BAREBOX_CMD_COMPLETE(device_complete)
BAREBOX_CMD_END