2008-07-26 21:12:11 +08:00
|
|
|
/****************************************************************************
|
2021-03-09 05:39:04 +08:00
|
|
|
* drivers/pipes/pipe_common.h
|
2008-07-26 21:12:11 +08:00
|
|
|
*
|
2021-03-04 14:10:42 +08:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2008-07-26 21:12:11 +08:00
|
|
|
*
|
2021-03-04 14:10:42 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2008-07-26 21:12:11 +08:00
|
|
|
*
|
2021-03-04 14:10:42 +08:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2008-07-26 21:12:11 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-07-15 22:56:25 +08:00
|
|
|
#ifndef __DRIVERS_PIPES_PIPE_COMMON_H
|
|
|
|
#define __DRIVERS_PIPES_PIPE_COMMON_H
|
2008-07-26 21:12:11 +08:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2022-09-06 14:18:45 +08:00
|
|
|
#include <nuttx/mutex.h>
|
2023-08-08 12:48:40 +08:00
|
|
|
#include <nuttx/mm/circbuf.h>
|
2008-07-26 21:12:11 +08:00
|
|
|
#include <sys/types.h>
|
2009-12-14 23:46:55 +08:00
|
|
|
|
|
|
|
#include <stdint.h>
|
2009-12-15 22:25:14 +08:00
|
|
|
#include <stdbool.h>
|
2008-11-18 04:24:28 +08:00
|
|
|
#include <poll.h>
|
2008-07-26 21:12:11 +08:00
|
|
|
|
2016-07-20 03:34:18 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* 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 22:02:46 +08:00
|
|
|
#ifndef CONFIG_DEV_PIPE_SIZE
|
|
|
|
# define CONFIG_DEV_PIPE_SIZE 1024
|
2008-07-26 21:12:11 +08:00
|
|
|
#endif
|
2008-11-18 04:24:28 +08:00
|
|
|
|
2016-07-20 03:34:18 +08:00
|
|
|
#ifndef CONFIG_DEV_FIFO_SIZE
|
|
|
|
# define CONFIG_DEV_FIFO_SIZE 1024
|
|
|
|
#endif
|
2008-07-26 21:12:11 +08:00
|
|
|
|
2023-06-07 21:57:30 +08:00
|
|
|
/* 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
|
|
|
|
|
2008-11-18 04:24:28 +08: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 22:02:46 +08:00
|
|
|
/* Maximum number of open's supported on pipe */
|
2008-07-26 21:12:11 +08:00
|
|
|
|
2008-07-26 22:02:46 +08:00
|
|
|
#define CONFIG_DEV_PIPE_MAXUSER 255
|
2008-07-26 21:12:11 +08:00
|
|
|
|
2015-02-01 02:05:01 +08: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 21:12:11 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
2008-07-26 22:02:46 +08:00
|
|
|
/* Make the buffer index as small as possible for the configured pipe size */
|
2014-04-14 04:32:20 +08:00
|
|
|
|
2016-07-20 03:34:18 +08:00
|
|
|
#if CONFIG_DEV_PIPE_MAXSIZE > 65535
|
2009-12-14 23:46:55 +08:00
|
|
|
typedef uint32_t pipe_ndx_t; /* 32-bit index */
|
2016-07-20 03:34:18 +08:00
|
|
|
#elif CONFIG_DEV_PIPE_MAXSIZE > 255
|
2009-12-14 23:46:55 +08:00
|
|
|
typedef uint16_t pipe_ndx_t; /* 16-bit index */
|
2008-07-26 21:12:11 +08:00
|
|
|
#else
|
2009-12-14 23:46:55 +08:00
|
|
|
typedef uint8_t pipe_ndx_t; /* 8-bit index */
|
2008-07-26 21:12:11 +08:00
|
|
|
#endif
|
|
|
|
|
2008-11-18 04:24:28 +08:00
|
|
|
/* This structure represents the state of one pipe. A reference to this
|
2021-03-04 15:02:21 +08:00
|
|
|
* structure is retained in the i_private field of the inode whenthe
|
|
|
|
* pipe/fifo device is registered.
|
2008-11-18 04:24:28 +08:00
|
|
|
*/
|
|
|
|
|
2008-07-27 22:58:36 +08:00
|
|
|
struct pipe_dev_s
|
2008-07-26 21:12:11 +08:00
|
|
|
{
|
2023-08-08 12:48:40 +08:00
|
|
|
mutex_t d_bflock; /* Used to serialize access to d_buffer and indices */
|
|
|
|
sem_t d_rdsem; /* Empty buffer - Reader waits for data write AND
|
|
|
|
* block O_RDONLY open until there is at least one writer */
|
|
|
|
sem_t d_wrsem; /* Full buffer - Writer waits for data read AND
|
|
|
|
* block O_WRONLY open until there is at least one reader */
|
|
|
|
pipe_ndx_t d_bufsize; /* allocated size of d_buffer in bytes */
|
|
|
|
pipe_ndx_t d_pollinthrd; /* Buffer threshold for POLLIN to occur */
|
|
|
|
pipe_ndx_t d_polloutthrd; /* Buffer threshold for POLLOUT to occur */
|
|
|
|
uint8_t d_nwriters; /* Number of reference counts for write access */
|
|
|
|
uint8_t d_nreaders; /* Number of reference counts for read access */
|
|
|
|
uint8_t d_flags; /* See PIPE_FLAG_* definitions */
|
|
|
|
struct circbuf_s d_buffer; /* Buffer allocated when device opened */
|
2008-11-18 04:24:28 +08: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'.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct pollfd *d_fds[CONFIG_DEV_PIPE_NPOLLWAITERS];
|
2008-07-26 21:12:11 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
# define EXTERN extern "C"
|
2016-07-20 03:40:54 +08:00
|
|
|
extern "C"
|
|
|
|
{
|
2008-07-26 21:12:11 +08:00
|
|
|
#else
|
|
|
|
# define EXTERN extern
|
|
|
|
#endif
|
|
|
|
|
2015-02-01 03:24:59 +08:00
|
|
|
struct file; /* Forward reference */
|
2015-02-11 03:41:49 +08:00
|
|
|
struct inode; /* Forward reference */
|
2015-02-01 03:24:59 +08:00
|
|
|
|
2016-07-20 03:34:18 +08:00
|
|
|
FAR struct pipe_dev_s *pipecommon_allocdev(size_t bufsize);
|
2015-01-31 01:14:24 +08: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);
|
|
|
|
int pipecommon_poll(FAR struct file *filep, FAR struct pollfd *fds,
|
2009-12-15 22:25:14 +08:00
|
|
|
bool setup);
|
2015-02-18 23:34:58 +08:00
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
2015-02-01 03:24:59 +08:00
|
|
|
int pipecommon_unlink(FAR struct inode *priv);
|
2015-02-18 23:34:58 +08:00
|
|
|
#endif
|
2008-07-26 21:12:11 +08:00
|
|
|
|
|
|
|
#undef EXTERN
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-07-15 22:56:25 +08:00
|
|
|
#endif /* __DRIVERS_PIPES_PIPE_COMMON_H */
|