2008-07-26 15:12:11 +02:00
|
|
|
/****************************************************************************
|
2009-10-18 15:52:21 +02:00
|
|
|
* drivers/pipes/pipe_common.c
|
2008-07-26 15:12:11 +02:00
|
|
|
*
|
2009-06-17 01:23:31 +02:00
|
|
|
* Copyright (C) 2008-2009 Gregory Nutt. All rights reserved.
|
2008-07-26 15:12:11 +02:00
|
|
|
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
#include <sys/types.h>
|
2009-09-13 18:15:46 +02:00
|
|
|
|
2008-07-26 15:12:11 +02:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sched.h>
|
|
|
|
#include <semaphore.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
2008-11-17 21:24:28 +01:00
|
|
|
#include <debug.h>
|
|
|
|
|
2008-07-26 15:12:11 +02:00
|
|
|
#include <nuttx/fs.h>
|
2009-09-14 00:32:01 +02:00
|
|
|
#if CONFIG_DEBUG
|
|
|
|
# include <nuttx/arch.h>
|
|
|
|
#endif
|
2008-07-26 15:12:11 +02:00
|
|
|
|
2008-08-10 18:57:15 +02:00
|
|
|
#include "pipe_common.h"
|
2008-07-26 15:12:11 +02:00
|
|
|
|
2008-07-26 16:02:46 +02:00
|
|
|
#if CONFIG_DEV_PIPE_SIZE > 0
|
2008-07-26 15:12:11 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
2009-09-15 22:55:06 +02:00
|
|
|
* Pre-processor Definitions
|
2008-07-26 15:12:11 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2009-09-15 22:55:06 +02:00
|
|
|
/* CONFIG_DEV_PIPEDUMP will dump the contents of each transfer into and out
|
|
|
|
* of the pipe.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEV_PIPEDUMP
|
|
|
|
# define pipe_dumpbuffer(m,a,n) lib_dumpbuffer(m,a,n)
|
|
|
|
#else
|
|
|
|
# define pipe_dumpbuffer(m,a,n)
|
|
|
|
#endif
|
|
|
|
|
2008-07-26 15:12:11 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2008-07-27 16:58:36 +02:00
|
|
|
static void pipecommon_semtake(sem_t *sem);
|
2008-07-26 15:12:11 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pipecommon_semtake
|
|
|
|
****************************************************************************/
|
2008-08-10 18:57:15 +02:00
|
|
|
|
2008-07-26 15:12:11 +02:00
|
|
|
static void pipecommon_semtake(sem_t *sem)
|
|
|
|
{
|
|
|
|
while (sem_wait(sem) != 0)
|
|
|
|
{
|
|
|
|
/* The only case that an error should occur here is if the wait was
|
|
|
|
* awakened by a signal.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ASSERT(errno == EINTR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-17 21:24:28 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: pipecommon_pollnotify
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_DISABLE_POLL
|
|
|
|
static void pipecommon_pollnotify(FAR struct pipe_dev_s *dev, pollevent_t eventset)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < CONFIG_DEV_PIPE_NPOLLWAITERS; i++)
|
|
|
|
{
|
|
|
|
struct pollfd *fds = dev->d_fds[i];
|
|
|
|
if (fds)
|
|
|
|
{
|
|
|
|
fds->revents |= (fds->events & eventset);
|
|
|
|
if (fds->revents != 0)
|
|
|
|
{
|
|
|
|
fvdbg("Report events: %02x\n", fds->revents);
|
|
|
|
sem_post(fds->sem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
# define pipecommon_pollnotify(dev,event)
|
|
|
|
#endif
|
|
|
|
|
2008-07-26 15:12:11 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pipecommon_allocdev
|
|
|
|
****************************************************************************/
|
2008-08-10 18:57:15 +02:00
|
|
|
|
2008-07-26 15:12:11 +02:00
|
|
|
FAR struct pipe_dev_s *pipecommon_allocdev(void)
|
|
|
|
{
|
|
|
|
struct pipe_dev_s *dev;
|
|
|
|
|
2008-07-26 16:02:46 +02:00
|
|
|
/* Allocate a private structure to manage the pipe */
|
2008-07-26 15:12:11 +02:00
|
|
|
|
|
|
|
dev = (struct pipe_dev_s *)malloc(sizeof(struct pipe_dev_s));
|
|
|
|
if (dev)
|
|
|
|
{
|
|
|
|
/* Initialize the private structure */
|
|
|
|
|
2008-07-27 16:58:36 +02:00
|
|
|
memset(dev, 0, sizeof(struct pipe_dev_s));
|
|
|
|
sem_init(&dev->d_bfsem, 0, 1);
|
|
|
|
sem_init(&dev->d_rdsem, 0, 0);
|
|
|
|
sem_init(&dev->d_wrsem, 0, 0);
|
2008-07-26 15:12:11 +02:00
|
|
|
}
|
|
|
|
return dev;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pipecommon_freedev
|
|
|
|
****************************************************************************/
|
2008-08-10 18:57:15 +02:00
|
|
|
|
|
|
|
void pipecommon_freedev(FAR struct pipe_dev_s *dev)
|
2008-07-26 15:12:11 +02:00
|
|
|
{
|
2008-07-27 16:58:36 +02:00
|
|
|
sem_destroy(&dev->d_bfsem);
|
|
|
|
sem_destroy(&dev->d_rdsem);
|
|
|
|
sem_destroy(&dev->d_wrsem);
|
2008-07-26 15:12:11 +02:00
|
|
|
free(dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pipecommon_open
|
|
|
|
****************************************************************************/
|
2008-08-10 18:57:15 +02:00
|
|
|
|
2008-07-26 15:12:11 +02:00
|
|
|
int pipecommon_open(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
struct inode *inode = filep->f_inode;
|
|
|
|
struct pipe_dev_s *dev = inode->i_private;
|
2008-07-26 16:24:17 +02:00
|
|
|
int sval;
|
2008-07-26 15:12:11 +02:00
|
|
|
|
|
|
|
/* Some sanity checking */
|
|
|
|
#if CONFIG_DEBUG
|
|
|
|
if (!dev)
|
|
|
|
{
|
|
|
|
return -EBADF;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
/* Make sure that we have exclusive access to the device structure */
|
|
|
|
|
2008-07-27 16:58:36 +02:00
|
|
|
if (sem_wait(&dev->d_bfsem) == 0)
|
2008-07-26 15:12:11 +02:00
|
|
|
{
|
2008-07-27 16:58:36 +02:00
|
|
|
/* If this the first reference on the device, then allocate the buffer */
|
|
|
|
|
|
|
|
if (dev->d_refs == 0)
|
|
|
|
{
|
|
|
|
dev->d_buffer = (ubyte*)malloc(CONFIG_DEV_PIPE_SIZE);
|
|
|
|
if (!dev->d_buffer)
|
|
|
|
{
|
|
|
|
(void)sem_post(&dev->d_bfsem);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-26 15:12:11 +02:00
|
|
|
/* Increment the reference count on the pipe instance */
|
|
|
|
|
2008-07-27 16:58:36 +02:00
|
|
|
dev->d_refs++;
|
2008-07-26 15:12:11 +02:00
|
|
|
|
|
|
|
/* If opened for writing, increment the count of writers on on the pipe instance */
|
|
|
|
|
|
|
|
if ((filep->f_oflags & O_WROK) != 0)
|
|
|
|
{
|
2008-07-27 16:58:36 +02:00
|
|
|
dev->d_nwriters++;
|
2008-07-26 16:24:17 +02:00
|
|
|
|
|
|
|
/* If this this is the first writer, then the read semaphore indicates the
|
|
|
|
* number of readers waiting for the first writer. Wake them all up.
|
|
|
|
*/
|
2009-09-13 18:15:46 +02:00
|
|
|
|
2008-07-27 16:58:36 +02:00
|
|
|
if (dev->d_nwriters == 1)
|
2008-07-26 16:24:17 +02:00
|
|
|
{
|
2008-07-27 16:58:36 +02:00
|
|
|
while (sem_getvalue(&dev->d_rdsem, &sval) == 0 && sval < 0)
|
2008-07-26 16:24:17 +02:00
|
|
|
{
|
2008-07-27 16:58:36 +02:00
|
|
|
sem_post(&dev->d_rdsem);
|
2008-07-26 16:24:17 +02:00
|
|
|
}
|
|
|
|
}
|
2008-07-26 15:12:11 +02:00
|
|
|
}
|
|
|
|
|
2008-07-26 16:02:46 +02:00
|
|
|
/* If opened for read-only, then wait for at least one writer on the pipe */
|
|
|
|
|
2008-07-26 16:24:17 +02:00
|
|
|
sched_lock();
|
2008-07-27 16:58:36 +02:00
|
|
|
(void)sem_post(&dev->d_bfsem);
|
|
|
|
if ((filep->f_oflags & O_RDWR) == O_RDONLY && dev->d_nwriters < 1)
|
2008-07-26 16:24:17 +02:00
|
|
|
{
|
|
|
|
/* NOTE: d_rdsem is normally used when the read logic waits for more
|
|
|
|
* data to be written. But until the first writer has opened the
|
|
|
|
* pipe, the meaning is different: it is used prevent O_RDONLY open
|
|
|
|
* calls from returning until there is at least one writer on the pipe.
|
|
|
|
* This is required both by spec and also because it prevents
|
|
|
|
* subsequent read() calls from returning end-of-file because there is
|
|
|
|
* no writer on the pipe.
|
|
|
|
*/
|
|
|
|
|
2008-07-27 16:58:36 +02:00
|
|
|
pipecommon_semtake(&dev->d_rdsem);
|
2008-07-26 16:24:17 +02:00
|
|
|
}
|
|
|
|
sched_unlock();
|
2008-07-26 15:12:11 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pipecommon_close
|
|
|
|
****************************************************************************/
|
2008-08-10 18:57:15 +02:00
|
|
|
|
2008-07-26 15:12:11 +02:00
|
|
|
int pipecommon_close(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
struct inode *inode = filep->f_inode;
|
|
|
|
struct pipe_dev_s *dev = inode->i_private;
|
2008-07-26 16:02:46 +02:00
|
|
|
int sval;
|
2008-07-26 15:12:11 +02:00
|
|
|
|
|
|
|
/* Some sanity checking */
|
|
|
|
#if CONFIG_DEBUG
|
|
|
|
if (!dev)
|
|
|
|
{
|
|
|
|
return -EBADF;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Make sure that we have exclusive access to the device structure.
|
|
|
|
* NOTE: close() is supposed to return EINTR if interrupted, however
|
|
|
|
* I've never seen anyone check that.
|
|
|
|
*/
|
|
|
|
|
2008-07-27 16:58:36 +02:00
|
|
|
pipecommon_semtake(&dev->d_bfsem);
|
2008-07-26 15:12:11 +02:00
|
|
|
|
2008-07-27 16:58:36 +02:00
|
|
|
/* Check if the decremented reference count would go to zero */
|
2008-07-26 15:12:11 +02:00
|
|
|
|
2008-07-27 16:58:36 +02:00
|
|
|
if (dev->d_refs > 1)
|
2008-07-26 15:12:11 +02:00
|
|
|
{
|
|
|
|
/* No.. then just decrement the reference count */
|
|
|
|
|
2008-07-27 16:58:36 +02:00
|
|
|
dev->d_refs--;
|
2008-07-26 15:12:11 +02:00
|
|
|
|
|
|
|
/* If opened for writing, decrement the count of writers on on the pipe instance */
|
|
|
|
|
|
|
|
if ((filep->f_oflags & O_WROK) != 0)
|
|
|
|
{
|
2008-07-26 16:02:46 +02:00
|
|
|
/* If there are no longer any writers on the pipe, then notify all of the
|
|
|
|
* waiting readers that they must return end-of-file.
|
2008-07-26 15:12:11 +02:00
|
|
|
*/
|
|
|
|
|
2008-07-27 16:58:36 +02:00
|
|
|
if (--dev->d_nwriters <= 0)
|
2008-07-26 15:12:11 +02:00
|
|
|
{
|
2008-07-27 16:58:36 +02:00
|
|
|
while (sem_getvalue(&dev->d_rdsem, &sval) == 0 && sval < 0)
|
2008-07-26 16:02:46 +02:00
|
|
|
{
|
2008-07-27 16:58:36 +02:00
|
|
|
sem_post(&dev->d_rdsem);
|
2008-07-26 16:02:46 +02:00
|
|
|
}
|
2008-07-26 15:12:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-07-27 16:58:36 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Yes... deallocate the buffer */
|
|
|
|
|
|
|
|
free(dev->d_buffer);
|
|
|
|
dev->d_buffer = NULL;
|
|
|
|
|
|
|
|
/* And reset all counts and indices */
|
|
|
|
|
|
|
|
dev->d_wrndx = 0;
|
|
|
|
dev->d_rdndx = 0;
|
|
|
|
dev->d_refs = 0;
|
|
|
|
dev->d_nwriters = 0;
|
|
|
|
}
|
2008-07-26 15:12:11 +02:00
|
|
|
|
2008-07-27 16:58:36 +02:00
|
|
|
sem_post(&dev->d_bfsem);
|
2008-07-26 15:12:11 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pipecommon_read
|
|
|
|
****************************************************************************/
|
2008-08-10 18:57:15 +02:00
|
|
|
|
2008-07-26 15:12:11 +02:00
|
|
|
ssize_t pipecommon_read(FAR struct file *filep, FAR char *buffer, size_t len)
|
|
|
|
{
|
|
|
|
struct inode *inode = filep->f_inode;
|
|
|
|
struct pipe_dev_s *dev = inode->i_private;
|
2009-09-15 22:55:06 +02:00
|
|
|
#ifdef CONFIG_DEV_PIPEDUMP
|
|
|
|
FAR ubyte *start = (ubyte*)buffer;
|
|
|
|
#endif
|
2008-07-26 15:12:11 +02:00
|
|
|
ssize_t nread = 0;
|
2008-07-26 16:02:46 +02:00
|
|
|
int sval;
|
2008-07-26 15:12:11 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Some sanity checking */
|
|
|
|
#if CONFIG_DEBUG
|
|
|
|
if (!dev)
|
|
|
|
{
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Make sure that we have exclusive access to the device structure */
|
|
|
|
|
2008-07-27 16:58:36 +02:00
|
|
|
if (sem_wait(&dev->d_bfsem) < 0)
|
2008-07-26 15:12:11 +02:00
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
2008-07-26 16:02:46 +02:00
|
|
|
/* If the pipe is empty, then wait for something to be written to it */
|
2008-07-26 15:12:11 +02:00
|
|
|
|
2008-07-27 16:58:36 +02:00
|
|
|
while (dev->d_wrndx == dev->d_rdndx)
|
2008-07-26 15:12:11 +02:00
|
|
|
{
|
|
|
|
/* If O_NONBLOCK was set, then return EGAIN */
|
|
|
|
|
|
|
|
if (filep->f_oflags & O_NONBLOCK)
|
|
|
|
{
|
2008-07-27 16:58:36 +02:00
|
|
|
sem_post(&dev->d_bfsem);
|
2008-07-26 15:12:11 +02:00
|
|
|
return -EAGAIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If there are no writers on the pipe, then return end of file */
|
|
|
|
|
2008-07-27 16:58:36 +02:00
|
|
|
if (dev->d_nwriters <= 0)
|
2008-07-26 15:12:11 +02:00
|
|
|
{
|
2008-07-27 16:58:36 +02:00
|
|
|
sem_post(&dev->d_bfsem);
|
2008-07-26 15:12:11 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-07-26 16:02:46 +02:00
|
|
|
/* Otherwise, wait for something to be written to the pipe */
|
2008-07-26 15:12:11 +02:00
|
|
|
|
|
|
|
sched_lock();
|
2008-07-27 16:58:36 +02:00
|
|
|
sem_post(&dev->d_bfsem);
|
|
|
|
ret = sem_wait(&dev->d_rdsem);
|
2008-07-26 15:12:11 +02:00
|
|
|
sched_unlock();
|
2008-11-17 21:24:28 +01:00
|
|
|
|
2008-07-27 16:58:36 +02:00
|
|
|
if (ret < 0 || sem_wait(&dev->d_bfsem) < 0)
|
2008-07-26 15:12:11 +02:00
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-26 16:02:46 +02:00
|
|
|
/* Then return whatever is available in the pipe (which is at least one byte) */
|
2008-07-26 15:12:11 +02:00
|
|
|
|
|
|
|
nread = 0;
|
2008-07-27 16:58:36 +02:00
|
|
|
while (nread < len && dev->d_wrndx != dev->d_rdndx)
|
2008-07-26 15:12:11 +02:00
|
|
|
{
|
2008-07-27 16:58:36 +02:00
|
|
|
*buffer++ = dev->d_buffer[dev->d_rdndx];
|
|
|
|
if (++dev->d_rdndx >= CONFIG_DEV_PIPE_SIZE)
|
2008-07-26 15:12:11 +02:00
|
|
|
{
|
2008-07-27 16:58:36 +02:00
|
|
|
dev->d_rdndx = 0;
|
2008-07-26 15:12:11 +02:00
|
|
|
}
|
|
|
|
nread++;
|
|
|
|
}
|
|
|
|
|
2008-07-26 16:02:46 +02:00
|
|
|
/* Notify all waiting writers that bytes have been removed from the buffer */
|
2008-07-26 15:12:11 +02:00
|
|
|
|
2008-07-27 16:58:36 +02:00
|
|
|
while (sem_getvalue(&dev->d_wrsem, &sval) == 0 && sval < 0)
|
2008-07-26 15:12:11 +02:00
|
|
|
{
|
2008-07-27 16:58:36 +02:00
|
|
|
sem_post(&dev->d_wrsem);
|
2008-07-26 15:12:11 +02:00
|
|
|
}
|
|
|
|
|
2008-11-17 21:24:28 +01:00
|
|
|
/* Notify all poll/select waiters that they can write to the FIFO */
|
|
|
|
|
|
|
|
pipecommon_pollnotify(dev, POLLOUT);
|
|
|
|
|
2008-07-27 16:58:36 +02:00
|
|
|
sem_post(&dev->d_bfsem);
|
2009-09-15 22:55:06 +02:00
|
|
|
pipe_dumpbuffer("From PIPE:", start, nread);
|
2008-11-17 21:24:28 +01:00
|
|
|
return nread;
|
2008-07-26 15:12:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pipecommon_write
|
|
|
|
****************************************************************************/
|
2008-08-10 18:57:15 +02:00
|
|
|
|
2008-07-26 15:12:11 +02:00
|
|
|
ssize_t pipecommon_write(FAR struct file *filep, FAR const char *buffer, size_t len)
|
|
|
|
{
|
|
|
|
struct inode *inode = filep->f_inode;
|
|
|
|
struct pipe_dev_s *dev = inode->i_private;
|
|
|
|
ssize_t nwritten = 0;
|
|
|
|
ssize_t last;
|
|
|
|
int nxtwrndx;
|
2008-07-26 16:02:46 +02:00
|
|
|
int sval;
|
2008-07-26 15:12:11 +02:00
|
|
|
|
|
|
|
/* Some sanity checking */
|
2009-09-15 22:55:06 +02:00
|
|
|
|
2008-07-26 15:12:11 +02:00
|
|
|
#if CONFIG_DEBUG
|
|
|
|
if (!dev)
|
|
|
|
{
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
#endif
|
2009-09-15 22:55:06 +02:00
|
|
|
pipe_dumpbuffer("To PIPE:", (ubyte*)buffer, len);
|
2008-07-26 15:12:11 +02:00
|
|
|
|
2009-09-14 00:32:01 +02:00
|
|
|
/* At present, this method cannot be called from interrupt handlers. That is
|
|
|
|
* because it calls sem_wait (via pipecommon_semtake below) and sem_wait cannot
|
|
|
|
* be called from interrupt level. This actually happens fairly commonly
|
|
|
|
* IF dbg() is called from interrupt handlers and stdout is being redirected
|
|
|
|
* via a pipe. In that case, the debug output will try to go out the pipe
|
|
|
|
* (interrupt handlers should use the lldbg() APIs).
|
|
|
|
*
|
|
|
|
* On the other hand, it would be very valuable to be able to feed the pipe
|
|
|
|
* from an interrupt handler! TODO: Consider disabling interrupts instead
|
|
|
|
* of taking semaphores so that pipes can be written from interupt handlers
|
|
|
|
*/
|
|
|
|
|
|
|
|
DEBUGASSERT(up_interrupt_context() == FALSE)
|
|
|
|
|
2008-07-26 15:12:11 +02:00
|
|
|
/* Make sure that we have exclusive access to the device structure */
|
|
|
|
|
2008-07-27 16:58:36 +02:00
|
|
|
if (sem_wait(&dev->d_bfsem) < 0)
|
2008-07-26 15:12:11 +02:00
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Loop until all of the bytes have been written */
|
|
|
|
|
|
|
|
last = 0;
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
/* Calculate the write index AFTER the next byte is written */
|
|
|
|
|
2008-07-27 16:58:36 +02:00
|
|
|
nxtwrndx = dev->d_wrndx + 1;
|
2008-07-26 16:02:46 +02:00
|
|
|
if (nxtwrndx >= CONFIG_DEV_PIPE_SIZE)
|
2008-07-26 15:12:11 +02:00
|
|
|
{
|
|
|
|
nxtwrndx = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Would the next write overflow the circular buffer? */
|
|
|
|
|
2008-07-27 16:58:36 +02:00
|
|
|
if (nxtwrndx != dev->d_rdndx)
|
2008-07-26 15:12:11 +02:00
|
|
|
{
|
|
|
|
/* No... copy the byte */
|
|
|
|
|
2008-07-27 16:58:36 +02:00
|
|
|
dev->d_buffer[dev->d_wrndx] = *buffer++;
|
|
|
|
dev->d_wrndx = nxtwrndx;
|
2008-07-26 15:12:11 +02:00
|
|
|
|
|
|
|
/* Is the write complete? */
|
|
|
|
|
|
|
|
if (++nwritten >= len)
|
|
|
|
{
|
2008-07-26 16:02:46 +02:00
|
|
|
/* Yes.. Notify all of the waiting readers that more data is available */
|
2008-07-26 15:12:11 +02:00
|
|
|
|
2008-07-27 16:58:36 +02:00
|
|
|
while (sem_getvalue(&dev->d_rdsem, &sval) == 0 && sval < 0)
|
2008-07-26 15:12:11 +02:00
|
|
|
{
|
2008-07-27 16:58:36 +02:00
|
|
|
sem_post(&dev->d_rdsem);
|
2008-07-26 15:12:11 +02:00
|
|
|
}
|
|
|
|
|
2008-11-17 21:24:28 +01:00
|
|
|
/* Notify all poll/select waiters that they can write to the FIFO */
|
|
|
|
|
|
|
|
pipecommon_pollnotify(dev, POLLIN);
|
|
|
|
|
2008-07-26 15:12:11 +02:00
|
|
|
/* Return the number of bytes written */
|
|
|
|
|
2008-07-27 16:58:36 +02:00
|
|
|
sem_post(&dev->d_bfsem);
|
2008-07-26 15:12:11 +02:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* There is not enough room for the next byte. Was anything written in this pass? */
|
|
|
|
|
|
|
|
if (last < nwritten)
|
|
|
|
{
|
2008-07-26 16:02:46 +02:00
|
|
|
/* Yes.. Notify all of the waiting readers that more data is available */
|
2008-07-26 15:12:11 +02:00
|
|
|
|
2008-07-27 16:58:36 +02:00
|
|
|
while (sem_getvalue(&dev->d_rdsem, &sval) == 0 && sval < 0)
|
2008-07-26 15:12:11 +02:00
|
|
|
{
|
2008-07-27 16:58:36 +02:00
|
|
|
sem_post(&dev->d_rdsem);
|
2008-07-26 15:12:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
last = nwritten;
|
|
|
|
|
|
|
|
/* If O_NONBLOCK was set, then return partial bytes written or EGAIN */
|
|
|
|
|
|
|
|
if (filep->f_oflags & O_NONBLOCK)
|
|
|
|
{
|
|
|
|
if (nwritten == 0)
|
|
|
|
{
|
|
|
|
nwritten = -EAGAIN;
|
|
|
|
}
|
2008-07-27 16:58:36 +02:00
|
|
|
sem_post(&dev->d_bfsem);
|
2008-07-26 15:12:11 +02:00
|
|
|
return nwritten;
|
|
|
|
}
|
|
|
|
|
2008-07-26 16:02:46 +02:00
|
|
|
/* There is more to be written.. wait for data to be removed from the pipe */
|
2008-07-26 15:12:11 +02:00
|
|
|
|
|
|
|
sched_lock();
|
2008-07-27 16:58:36 +02:00
|
|
|
sem_post(&dev->d_bfsem);
|
|
|
|
pipecommon_semtake(&dev->d_wrsem);
|
2008-07-26 15:12:11 +02:00
|
|
|
sched_unlock();
|
2008-07-27 16:58:36 +02:00
|
|
|
pipecommon_semtake(&dev->d_bfsem);
|
2008-07-26 15:12:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-17 21:24:28 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: pipecommon_poll
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_DISABLE_POLL
|
2008-11-19 19:43:50 +01:00
|
|
|
int pipecommon_poll(FAR struct file *filep, FAR struct pollfd *fds,
|
|
|
|
boolean setup)
|
2008-11-17 21:24:28 +01:00
|
|
|
{
|
2008-11-18 00:23:45 +01:00
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct pipe_dev_s *dev = inode->i_private;
|
|
|
|
pollevent_t eventset;
|
|
|
|
pipe_ndx_t nbytes;
|
2008-11-19 19:43:50 +01:00
|
|
|
int ret = OK;
|
2008-11-18 00:23:45 +01:00
|
|
|
int i;
|
2008-11-17 21:24:28 +01:00
|
|
|
|
|
|
|
/* Some sanity checking */
|
2008-11-19 19:43:50 +01:00
|
|
|
|
2008-11-17 21:24:28 +01:00
|
|
|
#if CONFIG_DEBUG
|
2008-11-19 19:43:50 +01:00
|
|
|
if (!dev || !fds)
|
2008-11-17 21:24:28 +01:00
|
|
|
{
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-11-19 19:43:50 +01:00
|
|
|
/* Are we setting up the poll? Or tearing it down? */
|
2008-11-17 21:24:28 +01:00
|
|
|
|
|
|
|
pipecommon_semtake(&dev->d_bfsem);
|
2008-11-19 19:43:50 +01:00
|
|
|
if (setup)
|
2008-11-17 21:24:28 +01:00
|
|
|
{
|
2008-11-19 19:43:50 +01:00
|
|
|
/* This is a request to set up the poll. Find an available
|
|
|
|
* slot for the poll structure reference
|
2008-11-17 21:24:28 +01:00
|
|
|
*/
|
|
|
|
|
2008-11-19 19:43:50 +01:00
|
|
|
for (i = 0; i < CONFIG_DEV_PIPE_NPOLLWAITERS; i++)
|
2008-11-17 21:24:28 +01:00
|
|
|
{
|
2008-11-19 19:43:50 +01:00
|
|
|
/* Find an available slot */
|
2008-11-17 21:24:28 +01:00
|
|
|
|
2008-11-19 19:43:50 +01:00
|
|
|
if (!dev->d_fds[i])
|
|
|
|
{
|
|
|
|
/* Bind the poll structure and this slot */
|
2008-11-17 21:24:28 +01:00
|
|
|
|
2008-11-19 19:43:50 +01:00
|
|
|
dev->d_fds[i] = fds;
|
2009-06-17 01:23:31 +02:00
|
|
|
fds->priv = &dev->d_fds[i];
|
2008-11-19 19:43:50 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-11-17 21:24:28 +01:00
|
|
|
|
2008-11-19 19:43:50 +01:00
|
|
|
if (i >= CONFIG_DEV_PIPE_NPOLLWAITERS)
|
|
|
|
{
|
2009-06-17 01:23:31 +02:00
|
|
|
fds->priv = NULL;
|
2008-11-19 19:43:50 +01:00
|
|
|
ret = -EBUSY;
|
|
|
|
goto errout;
|
|
|
|
}
|
2008-11-17 21:24:28 +01:00
|
|
|
|
2008-11-19 19:43:50 +01:00
|
|
|
/* Should immediately notify on any of the requested events?
|
|
|
|
* First, determine how many bytes are in the buffer
|
|
|
|
*/
|
2008-11-17 21:24:28 +01:00
|
|
|
|
|
|
|
if (dev->d_wrndx >= dev->d_rdndx)
|
|
|
|
{
|
|
|
|
nbytes = dev->d_wrndx - dev->d_rdndx;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nbytes = (CONFIG_DEV_PIPE_SIZE-1) + dev->d_wrndx - dev->d_rdndx;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Notify the POLLOUT event if the pipe is not full */
|
|
|
|
|
|
|
|
eventset = 0;
|
|
|
|
if (nbytes < (CONFIG_DEV_PIPE_SIZE-1))
|
|
|
|
{
|
|
|
|
eventset |= POLLOUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Notify the POLLIN event if the pipe is not empty */
|
|
|
|
|
|
|
|
if (nbytes > 0)
|
|
|
|
{
|
|
|
|
eventset |= POLLIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (eventset)
|
|
|
|
{
|
|
|
|
pipecommon_pollnotify(dev, eventset);
|
|
|
|
}
|
|
|
|
}
|
2008-11-19 19:43:50 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* This is a request to tear down the poll. */
|
|
|
|
|
2009-06-17 01:23:31 +02:00
|
|
|
struct pollfd **slot = (struct pollfd **)fds->priv;
|
2008-11-19 19:43:50 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG
|
|
|
|
if (!slot)
|
|
|
|
{
|
|
|
|
ret = -EIO;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Remove all memory of the poll setup */
|
|
|
|
|
|
|
|
*slot = NULL;
|
2009-06-17 01:23:31 +02:00
|
|
|
fds->priv = NULL;
|
2008-11-19 19:43:50 +01:00
|
|
|
}
|
2008-11-17 21:24:28 +01:00
|
|
|
|
2008-11-19 19:43:50 +01:00
|
|
|
errout:
|
2008-11-17 21:24:28 +01:00
|
|
|
sem_post(&dev->d_bfsem);
|
2008-11-19 19:43:50 +01:00
|
|
|
return ret;
|
2008-11-17 21:24:28 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-07-26 16:02:46 +02:00
|
|
|
#endif /* CONFIG_DEV_PIPE_SIZE > 0 */
|