Skip to content
Open
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
53 changes: 50 additions & 3 deletions drivers/timers/watchdog.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <fcntl.h>
#include <assert.h>
#include <errno.h>
#include <stdio.h>
Comment thread
xiaoxiang781216 marked this conversation as resolved.
#include <nuttx/debug.h>

#include <nuttx/fs/fs.h>
Expand Down Expand Up @@ -552,16 +553,62 @@ static int wdog_close(FAR struct file *filep)
* 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;
}

/****************************************************************************
Expand Down
Loading