2008-07-26 15:12:11 +02:00
|
|
|
/****************************************************************************
|
2009-10-18 15:52:21 +02:00
|
|
|
* drivers/pipe/pipe_common.h
|
2008-07-26 15:12:11 +02:00
|
|
|
*
|
2016-03-21 01:44:44 +01:00
|
|
|
* Copyright (C) 2008-2009, 2015-2016 Gregory Nutt. All rights reserved.
|
2012-07-15 16:56:25 +02:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2008-07-26 15:12:11 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-07-15 16:56:25 +02:00
|
|
|
#ifndef __DRIVERS_PIPES_PIPE_COMMON_H
|
|
|
|
#define __DRIVERS_PIPES_PIPE_COMMON_H
|
2008-07-26 15:12:11 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
#include <sys/types.h>
|
2009-12-14 16:46:55 +01:00
|
|
|
|
|
|
|
#include <stdint.h>
|
2009-12-15 15:25:14 +01:00
|
|
|
#include <stdbool.h>
|
2008-11-17 21:24:28 +01:00
|
|
|
#include <poll.h>
|
2008-07-26 15:12:11 +02:00
|
|
|
|
2016-07-19 21:34:18 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Pipe/FIFO support */
|
|
|
|
|
|
|
|
#ifndef CONFIG_PIPES
|
|
|
|
# undef CONFIG_DEV_PIPE_MAXSIZE
|
|
|
|
# undef CONFIG_DEV_PIPE_SIZE
|
|
|
|
# undef CONFIG_DEV_FIFO_SIZE
|
|
|
|
# define CONFIG_DEV_PIPE_MAXSIZE 0
|
|
|
|
# define CONFIG_DEV_PIPE_SIZE 0
|
|
|
|
# define CONFIG_DEV_FIFO_SIZE 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Pipe/FIFO size */
|
|
|
|
|
|
|
|
#ifndef CONFIG_DEV_PIPE_MAXSIZE
|
|
|
|
# define CONFIG_DEV_PIPE_MAXSIZE 1024
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if CONFIG_DEV_PIPE_MAXSIZE <= 0
|
|
|
|
# undef CONFIG_PIPES
|
|
|
|
# undef CONFIG_DEV_PIPE_SIZE
|
|
|
|
# undef CONFIG_DEV_FIFO_SIZE
|
|
|
|
# define CONFIG_DEV_PIPE_SIZE 0
|
|
|
|
# define CONFIG_DEV_FIFO_SIZE 0
|
|
|
|
#endif
|
|
|
|
|
2008-07-26 16:02:46 +02:00
|
|
|
#ifndef CONFIG_DEV_PIPE_SIZE
|
|
|
|
# define CONFIG_DEV_PIPE_SIZE 1024
|
2008-07-26 15:12:11 +02:00
|
|
|
#endif
|
2008-11-17 21:24:28 +01:00
|
|
|
|
2016-07-19 21:34:18 +02:00
|
|
|
#ifndef CONFIG_DEV_FIFO_SIZE
|
|
|
|
# define CONFIG_DEV_FIFO_SIZE 1024
|
|
|
|
#endif
|
2008-07-26 15:12:11 +02:00
|
|
|
|
2008-11-17 21:24:28 +01:00
|
|
|
/* Maximum number of threads than can be waiting for POLL events */
|
|
|
|
|
|
|
|
#ifndef CONFIG_DEV_PIPE_NPOLLWAITERS
|
|
|
|
# define CONFIG_DEV_PIPE_NPOLLWAITERS 2
|
|
|
|
#endif
|
|
|
|
|
2008-07-26 16:02:46 +02:00
|
|
|
/* Maximum number of open's supported on pipe */
|
2008-07-26 15:12:11 +02:00
|
|
|
|
2008-07-26 16:02:46 +02:00
|
|
|
#define CONFIG_DEV_PIPE_MAXUSER 255
|
2008-07-26 15:12:11 +02:00
|
|
|
|
2015-01-31 19:05:01 +01:00
|
|
|
/* d_flags values */
|
|
|
|
|
|
|
|
#define PIPE_FLAG_POLICY (1 << 0) /* Bit 0: Policy=Free buffer when empty */
|
|
|
|
#define PIPE_FLAG_UNLINKED (1 << 1) /* Bit 1: The driver has been unlinked */
|
|
|
|
|
|
|
|
#define PIPE_POLICY_0(f) do { (f) &= ~PIPE_FLAG_POLICY; } while (0)
|
|
|
|
#define PIPE_POLICY_1(f) do { (f) |= PIPE_FLAG_POLICY; } while (0)
|
|
|
|
#define PIPE_IS_POLICY_0(f) (((f) & PIPE_FLAG_POLICY) == 0)
|
|
|
|
#define PIPE_IS_POLICY_1(f) (((f) & PIPE_FLAG_POLICY) != 0)
|
|
|
|
|
|
|
|
#define PIPE_UNLINK(f) do { (f) |= PIPE_FLAG_UNLINKED; } while (0)
|
|
|
|
#define PIPE_IS_UNLINKED(f) (((f) & PIPE_FLAG_UNLINKED) != 0)
|
|
|
|
|
|
|
|
|
2008-07-26 15:12:11 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
2008-07-26 16:02:46 +02:00
|
|
|
/* Make the buffer index as small as possible for the configured pipe size */
|
2014-04-13 22:32:20 +02:00
|
|
|
|
2016-07-19 21:34:18 +02:00
|
|
|
#if CONFIG_DEV_PIPE_MAXSIZE > 65535
|
2009-12-14 16:46:55 +01:00
|
|
|
typedef uint32_t pipe_ndx_t; /* 32-bit index */
|
2016-07-19 21:34:18 +02:00
|
|
|
#elif CONFIG_DEV_PIPE_MAXSIZE > 255
|
2009-12-14 16:46:55 +01:00
|
|
|
typedef uint16_t pipe_ndx_t; /* 16-bit index */
|
2008-07-26 15:12:11 +02:00
|
|
|
#else
|
2009-12-14 16:46:55 +01:00
|
|
|
typedef uint8_t pipe_ndx_t; /* 8-bit index */
|
2008-07-26 15:12:11 +02:00
|
|
|
#endif
|
|
|
|
|
2008-11-17 21:24:28 +01:00
|
|
|
/* This structure represents the state of one pipe. A reference to this
|
|
|
|
* structure is retained in the i_private field of the inode whenthe pipe/fifo
|
|
|
|
* device is registered.
|
|
|
|
*/
|
|
|
|
|
2008-07-27 16:58:36 +02:00
|
|
|
struct pipe_dev_s
|
2008-07-26 15:12:11 +02:00
|
|
|
{
|
|
|
|
sem_t d_bfsem; /* Used to serialize access to d_buffer and indices */
|
|
|
|
sem_t d_rdsem; /* Empty buffer - Reader waits for data write */
|
|
|
|
sem_t d_wrsem; /* Full buffer - Writer waits for data read */
|
|
|
|
pipe_ndx_t d_wrndx; /* Index in d_buffer to save next byte written */
|
|
|
|
pipe_ndx_t d_rdndx; /* Index in d_buffer to return the next byte read */
|
2016-07-19 21:34:18 +02:00
|
|
|
pipe_ndx_t d_bufsize; /* allocated size of d_buffer in bytes */
|
2009-12-14 16:46:55 +01:00
|
|
|
uint8_t d_refs; /* References counts on pipe (limited to 255) */
|
|
|
|
uint8_t d_nwriters; /* Number of reference counts for write access */
|
2015-05-12 15:41:12 +02:00
|
|
|
uint8_t d_nreaders; /* Number of reference counts for read access */
|
2009-12-14 16:46:55 +01:00
|
|
|
uint8_t d_pipeno; /* Pipe minor number */
|
2015-01-31 19:05:01 +01:00
|
|
|
uint8_t d_flags; /* See PIPE_FLAG_* definitions */
|
2009-12-14 16:46:55 +01:00
|
|
|
uint8_t *d_buffer; /* Buffer allocated when device opened */
|
2008-11-17 21:24:28 +01:00
|
|
|
|
|
|
|
/* The following is a list if poll structures of threads waiting for
|
|
|
|
* driver events. The 'struct pollfd' reference for each open is also
|
|
|
|
* retained in the f_priv field of the 'struct file'.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef CONFIG_DISABLE_POLL
|
|
|
|
struct pollfd *d_fds[CONFIG_DEV_PIPE_NPOLLWAITERS];
|
|
|
|
#endif
|
2008-07-26 15:12:11 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
# define EXTERN extern "C"
|
2016-07-19 21:40:54 +02:00
|
|
|
extern "C"
|
|
|
|
{
|
2008-07-26 15:12:11 +02:00
|
|
|
#else
|
|
|
|
# define EXTERN extern
|
|
|
|
#endif
|
|
|
|
|
2015-01-31 20:24:59 +01:00
|
|
|
struct file; /* Forward reference */
|
2015-02-10 20:41:49 +01:00
|
|
|
struct inode; /* Forward reference */
|
2015-01-31 20:24:59 +01:00
|
|
|
|
2016-07-19 21:34:18 +02:00
|
|
|
FAR struct pipe_dev_s *pipecommon_allocdev(size_t bufsize);
|
2015-01-30 18:14:24 +01:00
|
|
|
void pipecommon_freedev(FAR struct pipe_dev_s *dev);
|
|
|
|
int pipecommon_open(FAR struct file *filep);
|
|
|
|
int pipecommon_close(FAR struct file *filep);
|
|
|
|
ssize_t pipecommon_read(FAR struct file *, FAR char *, size_t);
|
|
|
|
ssize_t pipecommon_write(FAR struct file *, FAR const char *, size_t);
|
|
|
|
int pipecommon_ioctl(FAR struct file *filep, int cmd, unsigned long arg);
|
2008-11-17 21:24:28 +01:00
|
|
|
#ifndef CONFIG_DISABLE_POLL
|
2015-01-30 18:14:24 +01:00
|
|
|
int pipecommon_poll(FAR struct file *filep, FAR struct pollfd *fds,
|
2009-12-15 15:25:14 +01:00
|
|
|
bool setup);
|
2008-11-17 21:24:28 +01:00
|
|
|
#endif
|
2015-02-18 16:34:58 +01:00
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
2015-01-31 20:24:59 +01:00
|
|
|
int pipecommon_unlink(FAR struct inode *priv);
|
2015-02-18 16:34:58 +01:00
|
|
|
#endif
|
2008-07-26 15:12:11 +02:00
|
|
|
|
|
|
|
#undef EXTERN
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-07-15 16:56:25 +02:00
|
|
|
#endif /* __DRIVERS_PIPES_PIPE_COMMON_H */
|