Skip to content

Commit 45625cb

Browse files
yangguangcai1xiaoxiang781216
authored andcommitted
timer driver:support poll.
Signed-off-by: yangguangcai <[email protected]>
1 parent dee4c63 commit 45625cb

File tree

1 file changed

+50
-1
lines changed

1 file changed

+50
-1
lines changed

drivers/timers/timer.c

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include <nuttx/fs/fs.h>
4040
#include <nuttx/mutex.h>
4141
#include <nuttx/timers/timer.h>
42+
#include <sys/poll.h>
4243

4344
#ifdef CONFIG_TIMER
4445

@@ -53,6 +54,7 @@ struct timer_upperhalf_s
5354
mutex_t lock; /* Supports mutual exclusion */
5455
uint8_t crefs; /* The number of times the device has been opened */
5556
FAR char *path; /* Registration path */
57+
FAR struct pollfd *fds;
5658

5759
/* The contained signal info */
5860

@@ -77,6 +79,8 @@ static ssize_t timer_write(FAR struct file *filep, FAR const char *buffer,
7779
size_t buflen);
7880
static int timer_ioctl(FAR struct file *filep, int cmd,
7981
unsigned long arg);
82+
static int timer_poll(FAR struct file *filep,
83+
FAR struct pollfd *fds, bool setup);
8084

8185
/****************************************************************************
8286
* Private Data
@@ -90,6 +94,9 @@ static const struct file_operations g_timerops =
9094
timer_write, /* write */
9195
NULL, /* seek */
9296
timer_ioctl, /* ioctl */
97+
NULL, /* mmap */
98+
NULL, /* truncate */
99+
timer_poll, /* poll */
93100
};
94101

95102
/****************************************************************************
@@ -115,6 +122,8 @@ static bool timer_notifier(FAR uint32_t *next_interval_us, FAR void *arg)
115122

116123
/* Signal the waiter.. if there is one */
117124

125+
poll_notify(&upper->fds, 1, POLLIN);
126+
118127
nxsig_notification(notify->pid, &notify->event, SI_QUEUE, &upper->work);
119128

120129
return notify->periodic;
@@ -396,6 +405,46 @@ static int timer_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
396405
return ret;
397406
}
398407

408+
/****************************************************************************
409+
* Name: timer_poll
410+
****************************************************************************/
411+
412+
static int timer_poll(FAR struct file *filep,
413+
FAR struct pollfd *fds, bool setup)
414+
{
415+
FAR struct inode *inode = filep->f_inode;
416+
FAR struct timer_upperhalf_s *upper = inode->i_private;
417+
irqstate_t flags;
418+
int ret = OK;
419+
420+
if (upper == NULL || fds == NULL)
421+
{
422+
return -EINVAL;
423+
}
424+
425+
flags = enter_critical_section();
426+
427+
if (setup)
428+
{
429+
if (upper->fds)
430+
{
431+
ret = -EBUSY;
432+
goto errout;
433+
}
434+
435+
upper->fds = fds;
436+
}
437+
else
438+
{
439+
upper->fds = NULL;
440+
}
441+
442+
errout:
443+
leave_critical_section(flags);
444+
445+
return ret;
446+
}
447+
399448
/****************************************************************************
400449
* Public Functions
401450
****************************************************************************/
@@ -560,7 +609,7 @@ int timer_setcallback(FAR void *handle, tccb_t callback, FAR void *arg)
560609

561610
/* Check if the lower half driver supports the setcallback method */
562611

563-
if (lower->ops->setcallback != NULL) /* Optional */
612+
if (lower->ops->setcallback != NULL)
564613
{
565614
/* Yes.. Defer the handler attachment to the lower half driver */
566615

0 commit comments

Comments
 (0)