2012-11-22 16:43:40 +08:00
|
|
|
/*
|
|
|
|
* File : pipe.c
|
|
|
|
* This file is part of RT-Thread RTOS
|
2017-10-15 22:56:46 +08:00
|
|
|
* COPYRIGHT (C) 2012-2017, RT-Thread Development Team
|
2012-11-22 16:43:40 +08:00
|
|
|
*
|
2013-06-28 00:36:54 +08:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2012-11-22 16:43:40 +08:00
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2012-09-30 Bernard first version.
|
2017-11-08 09:38:18 +08:00
|
|
|
* 2017-11-08 JasonJiaJie fix memory leak issue when close a pipe.
|
2012-11-22 16:43:40 +08:00
|
|
|
*/
|
|
|
|
#include <rthw.h>
|
|
|
|
#include <rtdevice.h>
|
2017-12-31 23:51:57 +08:00
|
|
|
#include <stdint.h>
|
2017-10-17 22:27:06 +08:00
|
|
|
|
|
|
|
#if defined(RT_USING_POSIX)
|
2017-10-15 22:56:46 +08:00
|
|
|
#include <dfs_file.h>
|
|
|
|
#include <dfs_posix.h>
|
2017-10-17 22:27:06 +08:00
|
|
|
#include <dfs_poll.h>
|
2012-11-22 16:43:40 +08:00
|
|
|
|
2017-10-17 22:27:06 +08:00
|
|
|
static int pipe_fops_open(struct dfs_fd *fd)
|
2013-08-19 15:35:56 +08:00
|
|
|
{
|
2017-10-15 22:56:46 +08:00
|
|
|
rt_device_t device;
|
|
|
|
rt_pipe_t *pipe;
|
2013-08-19 15:35:56 +08:00
|
|
|
|
2017-10-15 22:56:46 +08:00
|
|
|
pipe = (rt_pipe_t *)fd->data;
|
|
|
|
if (!pipe) return -1;
|
2013-08-19 15:35:56 +08:00
|
|
|
|
2017-10-15 22:56:46 +08:00
|
|
|
device = &(pipe->parent);
|
|
|
|
rt_mutex_take(&(pipe->lock), RT_WAITING_FOREVER);
|
2013-08-19 15:35:56 +08:00
|
|
|
|
2017-10-15 22:56:46 +08:00
|
|
|
if (device->ref_count == 0)
|
|
|
|
{
|
2017-12-29 22:33:39 +08:00
|
|
|
pipe->fifo = rt_ringbuffer_create(pipe->bufsz);
|
2017-10-15 22:56:46 +08:00
|
|
|
}
|
2013-08-19 15:35:56 +08:00
|
|
|
|
2017-10-15 22:56:46 +08:00
|
|
|
switch (fd->flags & O_ACCMODE)
|
|
|
|
{
|
|
|
|
case O_RDONLY:
|
|
|
|
pipe->readers ++;
|
|
|
|
break;
|
|
|
|
case O_WRONLY:
|
|
|
|
pipe->writers ++;
|
|
|
|
break;
|
|
|
|
case O_RDWR:
|
|
|
|
pipe->readers ++;
|
|
|
|
pipe->writers ++;
|
|
|
|
break;
|
2013-08-19 15:35:56 +08:00
|
|
|
}
|
2017-10-15 22:56:46 +08:00
|
|
|
device->ref_count ++;
|
|
|
|
|
|
|
|
rt_mutex_release(&(pipe->lock));
|
|
|
|
|
|
|
|
return 0;
|
2013-08-19 15:35:56 +08:00
|
|
|
}
|
|
|
|
|
2017-10-17 22:27:06 +08:00
|
|
|
static int pipe_fops_close(struct dfs_fd *fd)
|
2012-11-22 16:43:40 +08:00
|
|
|
{
|
2017-10-15 22:56:46 +08:00
|
|
|
rt_device_t device;
|
|
|
|
rt_pipe_t *pipe;
|
2012-11-22 16:43:40 +08:00
|
|
|
|
2017-10-15 22:56:46 +08:00
|
|
|
pipe = (rt_pipe_t *)fd->data;
|
|
|
|
if (!pipe) return -1;
|
2012-11-22 16:43:40 +08:00
|
|
|
|
2017-10-15 22:56:46 +08:00
|
|
|
device = &(pipe->parent);
|
|
|
|
rt_mutex_take(&(pipe->lock), RT_WAITING_FOREVER);
|
|
|
|
|
|
|
|
switch (fd->flags & O_ACCMODE)
|
2013-08-19 15:35:56 +08:00
|
|
|
{
|
2017-10-15 22:56:46 +08:00
|
|
|
case O_RDONLY:
|
|
|
|
pipe->readers --;
|
|
|
|
break;
|
|
|
|
case O_WRONLY:
|
|
|
|
pipe->writers --;
|
|
|
|
break;
|
|
|
|
case O_RDWR:
|
|
|
|
pipe->readers --;
|
|
|
|
pipe->writers --;
|
|
|
|
break;
|
|
|
|
}
|
2013-08-19 15:35:56 +08:00
|
|
|
|
2017-10-15 22:56:46 +08:00
|
|
|
if (pipe->writers == 0)
|
|
|
|
{
|
|
|
|
rt_wqueue_wakeup(&(pipe->reader_queue), (void*)(POLLIN | POLLERR | POLLHUP));
|
|
|
|
}
|
2013-08-19 15:35:56 +08:00
|
|
|
|
2017-10-15 22:56:46 +08:00
|
|
|
if (pipe->readers == 0)
|
|
|
|
{
|
|
|
|
rt_wqueue_wakeup(&(pipe->writer_queue), (void*)(POLLOUT | POLLERR | POLLHUP));
|
|
|
|
}
|
2013-08-19 15:35:56 +08:00
|
|
|
|
2017-10-15 22:56:46 +08:00
|
|
|
if (device->ref_count == 1)
|
|
|
|
{
|
2017-11-08 09:38:18 +08:00
|
|
|
rt_ringbuffer_destroy(pipe->fifo);
|
2017-10-15 22:56:46 +08:00
|
|
|
pipe->fifo = RT_NULL;
|
2013-08-19 15:35:56 +08:00
|
|
|
}
|
2017-10-15 22:56:46 +08:00
|
|
|
device->ref_count --;
|
|
|
|
|
|
|
|
rt_mutex_release(&(pipe->lock));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-17 22:27:06 +08:00
|
|
|
static int pipe_fops_ioctl(struct dfs_fd *fd, int cmd, void *args)
|
2017-10-15 22:56:46 +08:00
|
|
|
{
|
|
|
|
rt_pipe_t *pipe;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
pipe = (rt_pipe_t *)fd->data;
|
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case FIONREAD:
|
|
|
|
*((int*)args) = rt_ringbuffer_data_len(pipe->fifo);
|
|
|
|
break;
|
|
|
|
case FIONWRITE:
|
|
|
|
*((int*)args) = rt_ringbuffer_space_len(pipe->fifo);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-10-17 22:27:06 +08:00
|
|
|
static int pipe_fops_read(struct dfs_fd *fd, void *buf, size_t count)
|
2017-10-15 22:56:46 +08:00
|
|
|
{
|
|
|
|
int len = 0;
|
|
|
|
rt_pipe_t *pipe;
|
2013-08-19 15:35:56 +08:00
|
|
|
|
2017-10-15 22:56:46 +08:00
|
|
|
pipe = (rt_pipe_t *)fd->data;
|
2012-11-22 16:43:40 +08:00
|
|
|
|
2017-10-15 22:56:46 +08:00
|
|
|
/* no process has the pipe open for writing, return end-of-file */
|
|
|
|
if (pipe->writers == 0)
|
|
|
|
return 0;
|
2012-11-22 16:43:40 +08:00
|
|
|
|
2017-10-15 22:56:46 +08:00
|
|
|
rt_mutex_take(&(pipe->lock), RT_WAITING_FOREVER);
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
if (pipe->writers == 0)
|
2012-11-22 16:43:40 +08:00
|
|
|
{
|
2017-10-15 22:56:46 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = rt_ringbuffer_get(pipe->fifo, buf, count);
|
2012-11-22 16:43:40 +08:00
|
|
|
|
2017-10-15 22:56:46 +08:00
|
|
|
if (len > 0)
|
|
|
|
{
|
|
|
|
break;
|
2012-11-22 16:43:40 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-10-15 22:56:46 +08:00
|
|
|
if (fd->flags & O_NONBLOCK)
|
|
|
|
{
|
|
|
|
len = -EAGAIN;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
rt_mutex_release(&pipe->lock);
|
|
|
|
rt_wqueue_wakeup(&(pipe->writer_queue), (void*)POLLOUT);
|
|
|
|
rt_wqueue_wait(&(pipe->reader_queue), 0, -1);
|
|
|
|
rt_mutex_take(&(pipe->lock), RT_WAITING_FOREVER);
|
2012-11-22 16:43:40 +08:00
|
|
|
}
|
2017-10-15 22:56:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* wakeup writer */
|
|
|
|
rt_wqueue_wakeup(&(pipe->writer_queue), (void*)POLLOUT);
|
2012-11-22 16:43:40 +08:00
|
|
|
|
2017-10-15 22:56:46 +08:00
|
|
|
out:
|
|
|
|
rt_mutex_release(&pipe->lock);
|
|
|
|
return len;
|
2012-11-22 16:43:40 +08:00
|
|
|
}
|
|
|
|
|
2017-10-17 22:27:06 +08:00
|
|
|
static int pipe_fops_write(struct dfs_fd *fd, const void *buf, size_t count)
|
2013-08-19 15:35:56 +08:00
|
|
|
{
|
2017-10-15 22:56:46 +08:00
|
|
|
int len;
|
|
|
|
rt_pipe_t *pipe;
|
|
|
|
int wakeup = 0;
|
|
|
|
int ret = 0;
|
|
|
|
uint8_t *pbuf;
|
|
|
|
|
|
|
|
pipe = (rt_pipe_t *)fd->data;
|
2013-08-19 15:57:59 +08:00
|
|
|
|
2017-10-15 22:56:46 +08:00
|
|
|
if (pipe->readers == 0)
|
2013-08-19 15:35:56 +08:00
|
|
|
{
|
2017-10-15 22:56:46 +08:00
|
|
|
ret = -EPIPE;
|
|
|
|
goto out;
|
|
|
|
}
|
2013-08-19 15:35:56 +08:00
|
|
|
|
2017-10-15 22:56:46 +08:00
|
|
|
if (count == 0)
|
|
|
|
return 0;
|
2013-08-19 15:35:56 +08:00
|
|
|
|
2017-10-15 22:56:46 +08:00
|
|
|
pbuf = (uint8_t*)buf;
|
|
|
|
rt_mutex_take(&pipe->lock, -1);
|
2013-08-19 15:35:56 +08:00
|
|
|
|
2017-10-15 22:56:46 +08:00
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
if (pipe->readers == 0)
|
|
|
|
{
|
|
|
|
if (ret == 0)
|
|
|
|
ret = -EPIPE;
|
|
|
|
break;
|
|
|
|
}
|
2013-08-19 15:35:56 +08:00
|
|
|
|
2017-10-15 22:56:46 +08:00
|
|
|
len = rt_ringbuffer_put(pipe->fifo, pbuf, count - ret);
|
|
|
|
ret += len;
|
|
|
|
pbuf += len;
|
|
|
|
wakeup = 1;
|
2013-08-19 15:35:56 +08:00
|
|
|
|
2017-10-15 22:56:46 +08:00
|
|
|
if (ret == count)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (fd->flags & O_NONBLOCK)
|
|
|
|
{
|
|
|
|
if (ret == 0)
|
|
|
|
{
|
|
|
|
ret = -EAGAIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-11-22 16:43:40 +08:00
|
|
|
|
2017-10-15 22:56:46 +08:00
|
|
|
rt_mutex_release(&pipe->lock);
|
|
|
|
rt_wqueue_wakeup(&(pipe->reader_queue), (void*)POLLIN);
|
|
|
|
/* pipe full, waiting on suspended write list */
|
|
|
|
rt_wqueue_wait(&(pipe->writer_queue), 0, -1);
|
|
|
|
rt_mutex_take(&pipe->lock, -1);
|
|
|
|
}
|
|
|
|
rt_mutex_release(&pipe->lock);
|
2017-12-29 22:33:39 +08:00
|
|
|
|
2017-10-15 22:56:46 +08:00
|
|
|
if (wakeup)
|
2013-08-19 15:35:56 +08:00
|
|
|
{
|
2017-10-15 22:56:46 +08:00
|
|
|
rt_wqueue_wakeup(&(pipe->reader_queue), (void*)POLLIN);
|
|
|
|
}
|
2013-08-19 15:35:56 +08:00
|
|
|
|
2017-10-15 22:56:46 +08:00
|
|
|
out:
|
|
|
|
return ret;
|
|
|
|
}
|
2013-08-19 15:35:56 +08:00
|
|
|
|
2017-10-17 22:27:06 +08:00
|
|
|
static int pipe_fops_poll(struct dfs_fd *fd, rt_pollreq_t *req)
|
2017-10-15 22:56:46 +08:00
|
|
|
{
|
|
|
|
int mask = 0;
|
|
|
|
rt_pipe_t *pipe;
|
|
|
|
int mode = 0;
|
|
|
|
pipe = (rt_pipe_t *)fd->data;
|
2013-08-19 15:35:56 +08:00
|
|
|
|
2017-10-15 22:56:46 +08:00
|
|
|
rt_poll_add(&(pipe->reader_queue), req);
|
|
|
|
rt_poll_add(&(pipe->writer_queue), req);
|
2013-08-19 15:35:56 +08:00
|
|
|
|
2017-10-15 22:56:46 +08:00
|
|
|
switch (fd->flags & O_ACCMODE)
|
|
|
|
{
|
|
|
|
case O_RDONLY:
|
|
|
|
mode = 1;
|
|
|
|
break;
|
|
|
|
case O_WRONLY:
|
|
|
|
mode = 2;
|
|
|
|
break;
|
|
|
|
case O_RDWR:
|
|
|
|
mode = 3;
|
|
|
|
break;
|
|
|
|
}
|
2012-11-22 16:43:40 +08:00
|
|
|
|
2017-10-15 22:56:46 +08:00
|
|
|
if (mode & 1)
|
|
|
|
{
|
|
|
|
if (rt_ringbuffer_data_len(pipe->fifo) != 0)
|
|
|
|
{
|
|
|
|
mask |= POLLIN;
|
|
|
|
}
|
|
|
|
if (pipe->writers == 0)
|
|
|
|
{
|
|
|
|
mask |= POLLHUP;
|
|
|
|
}
|
|
|
|
}
|
2012-11-22 16:43:40 +08:00
|
|
|
|
2017-10-15 22:56:46 +08:00
|
|
|
if (mode & 2)
|
|
|
|
{
|
|
|
|
if (rt_ringbuffer_space_len(pipe->fifo) != 0)
|
2012-11-22 16:43:40 +08:00
|
|
|
{
|
2017-10-15 22:56:46 +08:00
|
|
|
mask |= POLLOUT;
|
2012-11-22 16:43:40 +08:00
|
|
|
}
|
2017-10-15 22:56:46 +08:00
|
|
|
if (pipe->readers == 0)
|
2012-11-22 16:43:40 +08:00
|
|
|
{
|
2017-10-15 22:56:46 +08:00
|
|
|
mask |= POLLERR;
|
2012-11-22 16:43:40 +08:00
|
|
|
}
|
2017-10-15 22:56:46 +08:00
|
|
|
}
|
2012-11-22 16:43:40 +08:00
|
|
|
|
2017-10-15 22:56:46 +08:00
|
|
|
return mask;
|
2012-11-22 16:43:40 +08:00
|
|
|
}
|
|
|
|
|
2017-10-15 22:56:46 +08:00
|
|
|
static const struct dfs_file_ops pipe_fops =
|
2012-11-22 16:43:40 +08:00
|
|
|
{
|
2017-10-17 22:27:06 +08:00
|
|
|
pipe_fops_open,
|
|
|
|
pipe_fops_close,
|
|
|
|
pipe_fops_ioctl,
|
|
|
|
pipe_fops_read,
|
|
|
|
pipe_fops_write,
|
2017-10-15 22:56:46 +08:00
|
|
|
RT_NULL,
|
|
|
|
RT_NULL,
|
|
|
|
RT_NULL,
|
2017-10-17 22:27:06 +08:00
|
|
|
pipe_fops_poll,
|
2017-10-15 22:56:46 +08:00
|
|
|
};
|
2017-12-29 22:33:39 +08:00
|
|
|
#endif /* end of RT_USING_POSIX */
|
|
|
|
|
|
|
|
rt_err_t rt_pipe_open (rt_device_t device, rt_uint16_t oflag)
|
|
|
|
{
|
|
|
|
rt_pipe_t *pipe = (rt_pipe_t *)device;
|
|
|
|
|
|
|
|
if (device == RT_NULL) return -RT_EINVAL;
|
|
|
|
rt_mutex_take(&(pipe->lock), RT_WAITING_FOREVER);
|
|
|
|
|
|
|
|
if (pipe->fifo == RT_NULL)
|
|
|
|
{
|
|
|
|
pipe->fifo = rt_ringbuffer_create(pipe->bufsz);
|
|
|
|
}
|
|
|
|
|
|
|
|
rt_mutex_release(&(pipe->lock));
|
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
rt_err_t rt_pipe_close (rt_device_t device)
|
|
|
|
{
|
|
|
|
rt_pipe_t *pipe = (rt_pipe_t *)device;
|
|
|
|
|
|
|
|
if (device == RT_NULL) return -RT_EINVAL;
|
|
|
|
rt_mutex_take(&(pipe->lock), RT_WAITING_FOREVER);
|
|
|
|
|
|
|
|
if (device->ref_count == 1)
|
|
|
|
{
|
|
|
|
rt_ringbuffer_destroy(pipe->fifo);
|
|
|
|
pipe->fifo = RT_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
rt_mutex_release(&(pipe->lock));
|
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
2017-10-15 22:56:46 +08:00
|
|
|
|
2017-12-29 22:33:39 +08:00
|
|
|
rt_size_t rt_pipe_read (rt_device_t device, rt_off_t pos, void *buffer, rt_size_t count)
|
|
|
|
{
|
|
|
|
uint8_t *pbuf;
|
2018-04-09 11:58:11 +08:00
|
|
|
rt_size_t read_bytes = 0;
|
2017-12-29 22:33:39 +08:00
|
|
|
rt_pipe_t *pipe = (rt_pipe_t *)device;
|
|
|
|
|
2017-12-31 14:45:43 +08:00
|
|
|
if (device == RT_NULL)
|
|
|
|
{
|
|
|
|
rt_set_errno(-EINVAL);
|
|
|
|
return 0;
|
|
|
|
}
|
2017-12-29 22:33:39 +08:00
|
|
|
if (count == 0) return 0;
|
|
|
|
|
|
|
|
pbuf = (uint8_t*)buffer;
|
|
|
|
rt_mutex_take(&(pipe->lock), RT_WAITING_FOREVER);
|
|
|
|
|
|
|
|
while (read_bytes < count)
|
|
|
|
{
|
|
|
|
int len = rt_ringbuffer_get(pipe->fifo, &pbuf[read_bytes], count - read_bytes);
|
|
|
|
if (len <= 0) break;
|
|
|
|
|
|
|
|
read_bytes += len;
|
|
|
|
}
|
|
|
|
rt_mutex_release(&pipe->lock);
|
|
|
|
|
|
|
|
return read_bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
rt_size_t rt_pipe_write (rt_device_t device, rt_off_t pos, const void *buffer, rt_size_t count)
|
|
|
|
{
|
|
|
|
uint8_t *pbuf;
|
2018-04-09 11:58:11 +08:00
|
|
|
rt_size_t write_bytes = 0;
|
2017-12-29 22:33:39 +08:00
|
|
|
rt_pipe_t *pipe = (rt_pipe_t *)device;
|
|
|
|
|
2017-12-31 14:45:43 +08:00
|
|
|
if (device == RT_NULL)
|
|
|
|
{
|
|
|
|
rt_set_errno(-EINVAL);
|
|
|
|
return 0;
|
|
|
|
}
|
2017-12-29 22:33:39 +08:00
|
|
|
if (count == 0) return 0;
|
|
|
|
|
|
|
|
pbuf = (uint8_t*)buffer;
|
|
|
|
rt_mutex_take(&pipe->lock, -1);
|
|
|
|
|
|
|
|
while (write_bytes < count)
|
|
|
|
{
|
|
|
|
int len = rt_ringbuffer_put(pipe->fifo, &pbuf[write_bytes], count - write_bytes);
|
|
|
|
if (len <= 0) break;
|
|
|
|
|
|
|
|
write_bytes += len;
|
|
|
|
}
|
|
|
|
rt_mutex_release(&pipe->lock);
|
|
|
|
|
|
|
|
return write_bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
rt_err_t rt_pipe_control(rt_device_t dev, int cmd, void *args)
|
|
|
|
{
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
rt_pipe_t *rt_pipe_create(const char *name, int bufsz)
|
2013-08-19 12:24:15 +08:00
|
|
|
{
|
2017-10-15 22:56:46 +08:00
|
|
|
rt_pipe_t *pipe;
|
|
|
|
rt_device_t dev;
|
2013-08-19 12:24:15 +08:00
|
|
|
|
2017-10-15 22:56:46 +08:00
|
|
|
pipe = rt_malloc(sizeof(rt_pipe_t));
|
|
|
|
if (pipe == RT_NULL) return RT_NULL;
|
2013-08-19 12:24:15 +08:00
|
|
|
|
2017-10-15 22:56:46 +08:00
|
|
|
rt_memset(pipe, 0, sizeof(rt_pipe_t));
|
|
|
|
rt_mutex_init(&(pipe->lock), name, RT_IPC_FLAG_FIFO);
|
|
|
|
rt_list_init(&(pipe->reader_queue));
|
|
|
|
rt_list_init(&(pipe->writer_queue));
|
2013-08-19 12:24:15 +08:00
|
|
|
|
2017-12-29 22:33:39 +08:00
|
|
|
RT_ASSERT(bufsz < 0xFFFF);
|
|
|
|
pipe->bufsz = bufsz;
|
|
|
|
|
2017-10-15 22:56:46 +08:00
|
|
|
dev = &(pipe->parent);
|
|
|
|
dev->type = RT_Device_Class_Pipe;
|
2017-12-29 22:33:39 +08:00
|
|
|
dev->init = RT_NULL;
|
|
|
|
dev->open = rt_pipe_open;
|
|
|
|
dev->read = rt_pipe_read;
|
|
|
|
dev->write = rt_pipe_write;
|
|
|
|
dev->close = rt_pipe_close;
|
|
|
|
dev->control = rt_pipe_control;
|
|
|
|
|
|
|
|
dev->rx_indicate = RT_NULL;
|
|
|
|
dev->tx_complete = RT_NULL;
|
2013-08-19 15:35:56 +08:00
|
|
|
|
2017-10-15 22:56:46 +08:00
|
|
|
if (rt_device_register(&(pipe->parent), name, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE) != 0)
|
|
|
|
{
|
|
|
|
rt_free(pipe);
|
|
|
|
return RT_NULL;
|
|
|
|
}
|
2017-12-29 22:33:39 +08:00
|
|
|
#ifdef RT_USING_POSIX
|
2017-10-15 22:56:46 +08:00
|
|
|
dev->fops = (void*)&pipe_fops;
|
2017-12-29 22:33:39 +08:00
|
|
|
#endif
|
2013-08-19 12:24:15 +08:00
|
|
|
|
2017-10-15 22:56:46 +08:00
|
|
|
return pipe;
|
2013-08-19 12:24:15 +08:00
|
|
|
}
|
|
|
|
|
2017-10-15 22:56:46 +08:00
|
|
|
int rt_pipe_delete(const char *name)
|
2013-08-19 12:24:15 +08:00
|
|
|
{
|
2017-10-15 22:56:46 +08:00
|
|
|
int result = 0;
|
|
|
|
rt_device_t device;
|
|
|
|
|
|
|
|
device = rt_device_find(name);
|
|
|
|
if (device)
|
|
|
|
{
|
|
|
|
if (device->type == RT_Device_Class_Pipe)
|
|
|
|
{
|
|
|
|
rt_pipe_t *pipe;
|
|
|
|
|
|
|
|
if (device->ref_count != 0)
|
|
|
|
{
|
|
|
|
return -RT_EBUSY;
|
|
|
|
}
|
|
|
|
|
|
|
|
pipe = (rt_pipe_t *)device;
|
|
|
|
|
|
|
|
rt_mutex_detach(&(pipe->lock));
|
|
|
|
rt_device_unregister(device);
|
|
|
|
|
2017-12-29 22:33:39 +08:00
|
|
|
/* close fifo ringbuffer */
|
|
|
|
if (pipe->fifo)
|
|
|
|
{
|
|
|
|
rt_ringbuffer_destroy(pipe->fifo);
|
|
|
|
pipe->fifo = RT_NULL;
|
|
|
|
}
|
2017-10-15 22:56:46 +08:00
|
|
|
rt_free(pipe);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-12-29 22:33:39 +08:00
|
|
|
result = -ENODEV;
|
2017-10-15 22:56:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-12-29 22:33:39 +08:00
|
|
|
result = -ENODEV;
|
2017-10-15 22:56:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2013-08-19 12:24:15 +08:00
|
|
|
}
|
|
|
|
|
2017-12-29 22:33:39 +08:00
|
|
|
#ifdef RT_USING_POSIX
|
2017-10-15 22:56:46 +08:00
|
|
|
int pipe(int fildes[2])
|
2012-11-22 16:43:40 +08:00
|
|
|
{
|
2017-10-15 22:56:46 +08:00
|
|
|
rt_pipe_t *pipe;
|
|
|
|
char dname[8];
|
|
|
|
char dev_name[32];
|
|
|
|
static int pipeno = 0;
|
2012-11-22 16:43:40 +08:00
|
|
|
|
2017-10-15 22:56:46 +08:00
|
|
|
rt_snprintf(dname, sizeof(dname), "pipe%d", pipeno++);
|
|
|
|
|
2017-12-29 22:33:39 +08:00
|
|
|
pipe = rt_pipe_create(dname, PIPE_BUFSZ);
|
2013-08-19 12:24:15 +08:00
|
|
|
if (pipe == RT_NULL)
|
2017-10-15 22:56:46 +08:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2012-11-22 16:43:40 +08:00
|
|
|
|
2017-10-15 22:56:46 +08:00
|
|
|
rt_snprintf(dev_name, sizeof(dev_name), "/dev/%s", dname);
|
|
|
|
fildes[0] = open(dev_name, O_RDONLY, 0);
|
|
|
|
if (fildes[0] < 0)
|
2013-08-19 12:24:15 +08:00
|
|
|
{
|
2017-10-15 22:56:46 +08:00
|
|
|
return -1;
|
2013-08-19 12:24:15 +08:00
|
|
|
}
|
2012-11-22 16:43:40 +08:00
|
|
|
|
2017-10-15 22:56:46 +08:00
|
|
|
fildes[1] = open(dev_name, O_WRONLY, 0);
|
|
|
|
if (fildes[1] < 0)
|
|
|
|
{
|
|
|
|
close(fildes[1]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2012-11-22 16:43:40 +08:00
|
|
|
}
|
|
|
|
|
2017-10-15 22:56:46 +08:00
|
|
|
int mkfifo(const char *path, mode_t mode)
|
2012-11-22 16:43:40 +08:00
|
|
|
{
|
2017-10-15 22:56:46 +08:00
|
|
|
rt_pipe_t *pipe;
|
|
|
|
|
2017-12-29 22:33:39 +08:00
|
|
|
pipe = rt_pipe_create(path, PIPE_BUFSZ);
|
2012-11-22 16:43:40 +08:00
|
|
|
if (pipe == RT_NULL)
|
2017-10-15 22:56:46 +08:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2012-11-22 16:43:40 +08:00
|
|
|
|
2017-10-15 22:56:46 +08:00
|
|
|
return 0;
|
2012-11-22 16:43:40 +08:00
|
|
|
}
|
2017-10-15 22:56:46 +08:00
|
|
|
#endif
|