1 /*
2 * Copyright (c) 2024 Vogl Electronic GmbH
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <stdlib.h>
8 #include <zephyr/device.h>
9 #include <zephyr/shell/shell.h>
10 #include <zephyr/task_wdt/task_wdt.h>
11
cmd_init(const struct shell * sh,size_t argc,char * argv)12 static int cmd_init(const struct shell *sh, size_t argc, char *argv)
13 {
14 ARG_UNUSED(argc);
15 ARG_UNUSED(argv);
16
17 #ifdef CONFIG_TASK_WDT_HW_FALLBACK
18 const struct device *const wdt = DEVICE_DT_GET(DT_ALIAS(watchdog0));
19 #else
20 const struct device *const wdt = NULL;
21 #endif
22
23 shell_fprintf(sh, SHELL_INFO, "Init task watchdog ...\n");
24
25 int ret = task_wdt_init(wdt);
26
27 if (ret < 0) {
28 shell_fprintf(sh, SHELL_ERROR, "Failed to init task watchdog: %d\n", ret);
29 return ret;
30 }
31
32 return 0;
33 }
34
cmd_add(const struct shell * sh,size_t argc,char ** argv)35 static int cmd_add(const struct shell *sh, size_t argc, char **argv)
36 {
37 if (argc != 2) {
38 shell_fprintf(sh, SHELL_ERROR, "Invalid number of arguments\n");
39 return -EINVAL;
40 }
41
42 shell_fprintf(sh, SHELL_INFO, "Add task watchdog channel\n");
43
44 uint32_t period = atoi(argv[1]) * MSEC_PER_SEC;
45
46 int ret = task_wdt_add(period, NULL, NULL);
47
48 if (ret < 0) {
49 shell_fprintf(sh, SHELL_ERROR, "Failed to add task watchdog channel: %d\n", ret);
50 return ret;
51 }
52
53 shell_fprintf(sh, SHELL_INFO, "Task watchdog channel: %d\n", ret);
54
55 shell_fprintf(sh, SHELL_NORMAL,
56 "Use \"task_wdt feed %d\" to feed this channel\n"
57 "and \"task_wdt del %d\" to delete this channel\n",
58 ret, ret);
59
60 return 0;
61 }
62
cmd_feed(const struct shell * sh,size_t argc,char ** argv)63 static int cmd_feed(const struct shell *sh, size_t argc, char **argv)
64 {
65 if (argc != 2) {
66 shell_fprintf(sh, SHELL_ERROR, "Invalid number of arguments\n");
67 return -EINVAL;
68 }
69
70 shell_fprintf(sh, SHELL_INFO, "Feed task watchdog channel %s\n", argv[1]);
71
72 int ret = task_wdt_feed(atoi(argv[1]));
73
74 if (ret < 0) {
75 shell_fprintf(sh, SHELL_ERROR, "Failed to add task watchdog channel: %d\n", ret);
76 return ret;
77 }
78
79 return 0;
80 }
81
cmd_del(const struct shell * sh,size_t argc,char ** argv)82 static int cmd_del(const struct shell *sh, size_t argc, char **argv)
83 {
84 if (argc != 2) {
85 shell_fprintf(sh, SHELL_ERROR, "Invalid number of arguments\n");
86 return -EINVAL;
87 }
88
89 shell_fprintf(sh, SHELL_INFO, "Delete task watchdog channel %s\n", argv[1]);
90
91 int ret = task_wdt_delete(atoi(argv[1]));
92
93 if (ret < 0) {
94 shell_fprintf(sh, SHELL_ERROR, "Failed to delete task watchdog channel: %d\n", ret);
95 return ret;
96 }
97
98 return 0;
99 }
100
101 SHELL_STATIC_SUBCMD_SET_CREATE(
102 sub_task_wdt,
103 SHELL_CMD(init, NULL, "Initialize task watchdog", cmd_init),
104 SHELL_CMD(add, NULL, "Install new timeout (time in seconds)", cmd_add),
105 SHELL_CMD(feed, NULL, "Feed specified watchdog channel", cmd_feed),
106 SHELL_CMD(del, NULL, "Delete task watchdog channel", cmd_del),
107 SHELL_SUBCMD_SET_END);
108
109 SHELL_CMD_REGISTER(task_wdt, &sub_task_wdt, "Task watchdog commands", NULL);
110