This is an automated email from the ASF dual-hosted git repository.
acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git
The following commit(s) were added to refs/heads/master by this push:
new 873015af78d drivers/timers/watchdog: Optimize ops operation logic and
add null handling.
873015af78d is described below
commit 873015af78d53e6ad24e604f76fab158be88c1e4
Author: likun17 <[email protected]>
AuthorDate: Wed Nov 5 21:56:31 2025 +0800
drivers/timers/watchdog: Optimize ops operation logic and add null handling.
Add WD_START/WD_STOP/WD_KEEPALIVE/WD_GETSTATUS/WD_SETTIMEOUT/WD_CAPTURE/
WD_IOCTL helpers to watchdog.h. They return -ENOSYS (-ENOTTY for ioctl,
NULL for capture) when the lower half does not provide the method, and
the upper half uses them instead of calling the ops directly.
Signed-off-by: likun17 <[email protected]>
---
drivers/timers/watchdog.c | 71 +++++++++++------------------------------
include/nuttx/timers/watchdog.h | 43 +++++++++++++++++++++++++
2 files changed, 61 insertions(+), 53 deletions(-)
diff --git a/drivers/timers/watchdog.c b/drivers/timers/watchdog.c
index ca97c7bc004..cfe087a672f 100644
--- a/drivers/timers/watchdog.c
+++ b/drivers/timers/watchdog.c
@@ -466,7 +466,7 @@ watchdog_automonitor_start(FAR struct watchdog_upperhalf_s
*upper)
{
# if defined(CONFIG_WATCHDOG_AUTOMONITOR_BY_CAPTURE)
watchdog_capture_add(upper);
- lower->ops->capture(lower, watchdog_automonitor_capture);
+ WD_CAPTURE(lower, watchdog_automonitor_capture);
# elif defined(CONFIG_WATCHDOG_AUTOMONITOR_BY_ONESHOT)
struct timespec ts =
{
@@ -491,12 +491,8 @@ watchdog_automonitor_start(FAR struct watchdog_upperhalf_s
*upper)
pm_register(&upper->idle);
# endif
upper->monitor = true;
- if (lower->ops->settimeout)
- {
- lower->ops->settimeout(lower, WATCHDOG_AUTOMONITOR_TIMEOUT);
- }
-
- lower->ops->start(lower);
+ WD_SETTIMEOUT(lower, WATCHDOG_AUTOMONITOR_TIMEOUT);
+ WD_START(lower);
}
}
@@ -507,9 +503,9 @@ static void watchdog_automonitor_stop(FAR struct
watchdog_upperhalf_s *upper)
if (upper->monitor)
{
upper->monitor = false;
- lower->ops->stop(lower);
+ WD_STOP(lower);
# if defined(CONFIG_WATCHDOG_AUTOMONITOR_BY_CAPTURE)
- lower->ops->capture(lower, NULL);
+ WD_CAPTURE(lower, NULL);
watchdog_capture_remove(upper);
# elif defined(CONFIG_WATCHDOG_AUTOMONITOR_BY_ONESHOT)
ONESHOT_CANCEL(upper->oneshot, NULL);
@@ -538,7 +534,7 @@ static int wdog_notifier(FAR struct notifier_block *nb,
unsigned long action,
#ifdef CONFIG_WATCHDOG_AUTOMONITOR
watchdog_automonitor_stop(upper);
#else
- return upper->lower->ops->stop(upper->lower);
+ return WD_STOP(upper->lower);
#endif
}
@@ -844,7 +840,7 @@ static ssize_t wdog_write(FAR struct file *filep, FAR const
char *buffer,
#ifdef CONFIG_WATCHDOG_AUTOMONITOR
watchdog_automonitor_stop(upper);
#else
- err = lower->ops->stop(lower);
+ err = WD_STOP(lower);
#endif
break;
}
@@ -852,7 +848,7 @@ static ssize_t wdog_write(FAR struct file *filep, FAR const
char *buffer,
if (i == buflen)
{
- err = lower->ops->keepalive(lower);
+ err = WD_KEEPALIVE(lower);
}
nxmutex_unlock(&upper->lock);
@@ -916,8 +912,7 @@ static int wdog_ioctl(FAR struct file *filep, int cmd,
unsigned long arg)
* timeout
*/
- DEBUGASSERT(lower->ops->start); /* Required */
- ret = lower->ops->start(lower);
+ ret = WD_START(lower);
}
break;
@@ -930,8 +925,7 @@ static int wdog_ioctl(FAR struct file *filep, int cmd,
unsigned long arg)
{
/* Stop the watchdog timer */
- DEBUGASSERT(lower->ops->stop); /* Required */
- ret = lower->ops->stop(lower);
+ ret = WD_STOP(lower);
}
break;
@@ -946,21 +940,14 @@ static int wdog_ioctl(FAR struct file *filep, int cmd,
unsigned long arg)
/* Get the current watchdog timer status */
- if (lower->ops->getstatus) /* Optional */
+ status = (FAR struct watchdog_status_s *)((uintptr_t)arg);
+ if (status)
{
- status = (FAR struct watchdog_status_s *)((uintptr_t)arg);
- if (status)
- {
- ret = lower->ops->getstatus(lower, status);
- }
- else
- {
- ret = -EINVAL;
- }
+ ret = WD_GETSTATUS(lower, status);
}
else
{
- ret = -ENOSYS;
+ ret = -EINVAL;
}
}
break;
@@ -974,14 +961,7 @@ static int wdog_ioctl(FAR struct file *filep, int cmd,
unsigned long arg)
{
/* Set a new timeout value (and reset the watchdog timer) */
- if (lower->ops->settimeout) /* Optional */
- {
- ret = lower->ops->settimeout(lower, (uint32_t)arg);
- }
- else
- {
- ret = -ENOSYS;
- }
+ ret = WD_SETTIMEOUT(lower, (uint32_t)arg);
}
break;
@@ -1032,14 +1012,7 @@ static int wdog_ioctl(FAR struct file *filep, int cmd,
unsigned long arg)
* "pinging" the watchdog timer or "petting the dog".
*/
- if (lower->ops->keepalive) /* Optional */
- {
- ret = lower->ops->keepalive(lower);
- }
- else
- {
- ret = -ENOSYS;
- }
+ ret = WD_KEEPALIVE(lower);
}
break;
@@ -1056,14 +1029,7 @@ static int wdog_ioctl(FAR struct file *filep, int cmd,
unsigned long arg)
* method.
*/
- if (lower->ops->ioctl) /* Optional */
- {
- ret = lower->ops->ioctl(lower, cmd, arg);
- }
- else
- {
- ret = -ENOTTY;
- }
+ ret = WD_IOCTL(lower, cmd, arg);
}
break;
}
@@ -1293,8 +1259,7 @@ void watchdog_unregister(FAR void *handle)
/* Disable the watchdog timer */
- DEBUGASSERT(lower->ops->stop); /* Required */
- lower->ops->stop(lower);
+ WD_STOP(lower);
/* Unregister the watchdog timer device */
diff --git a/include/nuttx/timers/watchdog.h b/include/nuttx/timers/watchdog.h
index e45230b0d4c..44f7d8a7ef6 100644
--- a/include/nuttx/timers/watchdog.h
+++ b/include/nuttx/timers/watchdog.h
@@ -80,6 +80,49 @@
#define WDIOC_MINTIME _WDIOC(0x080)
+/* Start the watchdog timer, resetting the time to the current timeout */
+
+#define WD_START(l) \
+ (((l)->ops->start) ? ((l)->ops->start(l)) : -ENOSYS)
+
+/* Stop the watchdog timer */
+
+#define WD_STOP(l) \
+ (((l)->ops->stop) ? ((l)->ops->stop(l)) : -ENOSYS)
+
+/* Reset the watchdog timer to the current timeout value, prevent any
+ * imminent watchdog timeouts. This is sometimes referred as "pinging" the
+ * watchdog timer or "petting the dog".
+ */
+
+#define WD_KEEPALIVE(l) \
+ (((l)->ops->keepalive) ? ((l)->ops->keepalive(l)) : -ENOSYS)
+
+/* Get the current watchdog timer status */
+
+#define WD_GETSTATUS(l, s) \
+ (((l)->ops->getstatus) ? ((l)->ops->getstatus(l, s)) : -ENOSYS)
+
+/* Set a new timeout value (and reset the watchdog timer) */
+
+#define WD_SETTIMEOUT(l, t) \
+ (((l)->ops->settimeout) ? ((l)->ops->settimeout(l, t)) : -ENOSYS)
+
+/* Don't reset on watchdog timer timeout; instead, call this user provider
+ * timeout handler. NOTE: Providing handler==NULL will restore the reset
+ * behavior.
+ */
+
+#define WD_CAPTURE(l, h) \
+ (((l)->ops->capture) ? ((l)->ops->capture(l, h)) : NULL)
+
+/* Any ioctl commands that are not recognized by the "upper-half" driver
+ * are forwarded to the lower half driver through this method.
+ */
+
+#define WD_IOCTL(l, c, a) \
+ (((l)->ops->ioctl) ? ((l)->ops->ioctl(l, c, a)) : -ENOTTY)
+
/* Bit Settings *************************************************************/
/* Bit settings for the struct watchdog_status_s flags field */