This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 679c397401d8665ed5cba2811715d16dc98351d3
Author: Javier Alonso <[email protected]>
AuthorDate: Fri Jul 24 12:16:30 2026 +0200

    WDOG: Output status on read
    
    Currently, the `read` function of the watchdog device is dummy and
    does nothing. Although this is fine, it's not useful at all from a
    userspace perspective, where you may want to check the current WDOG
    status.
    
    A wrapper around the `ioctl` `WDIOC_GETSTATUS` has been added as
    the `read` function for the WDOG.
    
    Signed-off-by: Javier Alonso <[email protected]>
---
 drivers/timers/watchdog.c | 53 ++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 50 insertions(+), 3 deletions(-)

diff --git a/drivers/timers/watchdog.c b/drivers/timers/watchdog.c
index 923672a107d..106abc8eb11 100644
--- a/drivers/timers/watchdog.c
+++ b/drivers/timers/watchdog.c
@@ -34,6 +34,7 @@
 #include <fcntl.h>
 #include <assert.h>
 #include <errno.h>
+#include <stdio.h>
 #include <nuttx/debug.h>
 
 #include <nuttx/fs/fs.h>
@@ -552,16 +553,62 @@ errout:
  * Name: wdog_read
  *
  * Description:
- *   A dummy read method.  This is provided only to satisfy the VFS layer.
+ *   Return the current status of the watchdog timer. Mimics what an `ioctl`
+ *   with `WDIOC_GETSTATUS` would do. This is useful for userspace to get the
+ *   current status of the watchdog timer by just reading from the device.
+ *
+ * Returns:
+ *   The number of bytes read on success, or a negated errno value on failure
  *
  ****************************************************************************/
 
 static ssize_t wdog_read(FAR struct file *filep, FAR char *buffer,
                          size_t buflen)
 {
-  /* Return zero -- usually meaning end-of-file */
+  /* simulate a "get status" on the userspace */
 
-  return 0;
+  ssize_t ret;
+  FAR struct watchdog_status_s status;
+
+  /* the f_pos is not used along this driver.
+   * Use it as a marker to know if the status has been read already.
+   */
+
+  if (filep->f_pos)
+    {
+      filep->f_pos = 0;
+      return 0;
+    }
+
+  ret = wdog_ioctl(filep, WDIOC_GETSTATUS, (uintptr_t)&status);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  /* Copy the watchdog status into the user buffer */
+
+  ret = snprintf(
+          buffer,
+          buflen,
+          "Status       :\n"
+          "  flags      : 0x%08" PRIx32 "\n"
+          "  timeout    : %" PRId32 "\n"
+          "  timeleft   : %" PRId32 "\n",
+          status.flags, status.timeout, status.timeleft
+        );
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  if (ret >= buflen)
+    {
+      return -ENOSPC;
+    }
+
+  filep->f_pos++;
+  return ret;
 }
 
 /****************************************************************************

Reply via email to