Merge pull request #142 from grissiom/pipe-device
device/pipe: add rt_pipe_{init,detach}
This commit is contained in:
commit
90c9c8a27a
|
@ -172,8 +172,15 @@ rt_inline rt_uint16_t rt_ringbuffer_get_size(struct rt_ringbuffer *rb)
|
||||||
/**
|
/**
|
||||||
* Pipe Device
|
* Pipe Device
|
||||||
*/
|
*/
|
||||||
|
rt_err_t rt_pipe_init(struct rt_pipe_device *pipe,
|
||||||
|
const char *name,
|
||||||
|
rt_uint8_t *buf,
|
||||||
|
rt_size_t size);
|
||||||
|
rt_err_t rt_pipe_detach(struct rt_pipe_device *pipe);
|
||||||
|
#ifdef RT_USING_HEAP
|
||||||
rt_err_t rt_pipe_create(const char *name, rt_size_t size);
|
rt_err_t rt_pipe_create(const char *name, rt_size_t size);
|
||||||
void rt_pipe_destroy(struct rt_pipe_device *pipe);
|
void rt_pipe_destroy(struct rt_pipe_device *pipe);
|
||||||
|
#endif
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* DataQueue for DeviceDriver
|
* DataQueue for DeviceDriver
|
||||||
|
|
|
@ -151,32 +151,33 @@ static rt_err_t rt_pipe_control(rt_device_t dev, rt_uint8_t cmd, void *args)
|
||||||
return RT_EOK;
|
return RT_EOK;
|
||||||
}
|
}
|
||||||
|
|
||||||
rt_err_t rt_pipe_create(const char *name, rt_size_t size)
|
/**
|
||||||
|
* This function will initialize a pipe device and put it under control of
|
||||||
|
* resource management.
|
||||||
|
*
|
||||||
|
* @param pipe the pipe device
|
||||||
|
* @param name the name of pipe device
|
||||||
|
* @param buf the buffer of pipe device
|
||||||
|
* @param size the size of pipe device buffer
|
||||||
|
*
|
||||||
|
* @return the operation status, RT_EOK on successful
|
||||||
|
*/
|
||||||
|
rt_err_t rt_pipe_init(struct rt_pipe_device *pipe,
|
||||||
|
const char *name,
|
||||||
|
rt_uint8_t *buf,
|
||||||
|
rt_size_t size)
|
||||||
{
|
{
|
||||||
rt_err_t result = RT_EOK;
|
RT_ASSERT(pipe);
|
||||||
rt_uint8_t *rb_memptr = RT_NULL;
|
RT_ASSERT(buf);
|
||||||
struct rt_pipe_device *pipe = RT_NULL;
|
|
||||||
|
|
||||||
/* get aligned size */
|
|
||||||
size = RT_ALIGN(size, RT_ALIGN_SIZE);
|
|
||||||
pipe = (struct rt_pipe_device *)rt_calloc(1, sizeof(struct rt_pipe_device));
|
|
||||||
if (pipe != RT_NULL)
|
|
||||||
{
|
|
||||||
/* create ring buffer of pipe */
|
|
||||||
rb_memptr = rt_malloc(size);
|
|
||||||
if (rb_memptr == RT_NULL)
|
|
||||||
{
|
|
||||||
result = -RT_ENOMEM;
|
|
||||||
goto __exit;
|
|
||||||
}
|
|
||||||
/* initialize suspended list */
|
/* initialize suspended list */
|
||||||
rt_list_init(&pipe->suspended_read_list);
|
rt_list_init(&pipe->suspended_read_list);
|
||||||
rt_list_init(&pipe->suspended_write_list);
|
rt_list_init(&pipe->suspended_write_list);
|
||||||
|
|
||||||
/* initialize ring buffer */
|
/* initialize ring buffer */
|
||||||
rt_ringbuffer_init(&pipe->ringbuffer, rb_memptr, size);
|
rt_ringbuffer_init(&pipe->ringbuffer, buf, size);
|
||||||
|
|
||||||
/* create device */
|
/* create pipe */
|
||||||
pipe->parent.type = RT_Device_Class_Char;
|
pipe->parent.type = RT_Device_Class_Char;
|
||||||
pipe->parent.init = RT_NULL;
|
pipe->parent.init = RT_NULL;
|
||||||
pipe->parent.open = RT_NULL;
|
pipe->parent.open = RT_NULL;
|
||||||
|
@ -187,18 +188,42 @@ rt_err_t rt_pipe_create(const char *name, rt_size_t size)
|
||||||
|
|
||||||
return rt_device_register(&(pipe->parent), name, RT_DEVICE_FLAG_RDWR);
|
return rt_device_register(&(pipe->parent), name, RT_DEVICE_FLAG_RDWR);
|
||||||
}
|
}
|
||||||
else
|
RTM_EXPORT(rt_pipe_init);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This function will detach a pipe device from resource management
|
||||||
|
*
|
||||||
|
* @param pipe the pipe device
|
||||||
|
*
|
||||||
|
* @return the operation status, RT_EOK on successful
|
||||||
|
*/
|
||||||
|
rt_err_t rt_pipe_detach(struct rt_pipe_device *pipe)
|
||||||
{
|
{
|
||||||
result = -RT_ENOMEM;
|
return rt_device_unregister(&pipe->parent);
|
||||||
|
}
|
||||||
|
RTM_EXPORT(rt_pipe_detach);
|
||||||
|
|
||||||
|
#ifdef RT_USING_HEAP
|
||||||
|
rt_err_t rt_pipe_create(const char *name, rt_size_t size)
|
||||||
|
{
|
||||||
|
rt_uint8_t *rb_memptr = RT_NULL;
|
||||||
|
struct rt_pipe_device *pipe = RT_NULL;
|
||||||
|
|
||||||
|
/* get aligned size */
|
||||||
|
size = RT_ALIGN(size, RT_ALIGN_SIZE);
|
||||||
|
pipe = (struct rt_pipe_device *)rt_calloc(1, sizeof(struct rt_pipe_device));
|
||||||
|
if (pipe == RT_NULL)
|
||||||
|
return -RT_ENOMEM;
|
||||||
|
|
||||||
|
/* create ring buffer of pipe */
|
||||||
|
rb_memptr = rt_malloc(size);
|
||||||
|
if (rb_memptr == RT_NULL)
|
||||||
|
{
|
||||||
|
rt_free(pipe);
|
||||||
|
return -RT_ENOMEM;
|
||||||
}
|
}
|
||||||
|
|
||||||
__exit:
|
return rt_pipe_init(pipe, name, rb_memptr, size);
|
||||||
if (pipe != RT_NULL)
|
|
||||||
rt_free(pipe);
|
|
||||||
if (rb_memptr != RT_NULL)
|
|
||||||
rt_free(rb_memptr);
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
RTM_EXPORT(rt_pipe_create);
|
RTM_EXPORT(rt_pipe_create);
|
||||||
|
|
||||||
|
@ -208,7 +233,7 @@ void rt_pipe_destroy(struct rt_pipe_device *pipe)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
/* un-register pipe device */
|
/* un-register pipe device */
|
||||||
rt_device_unregister(&(pipe->parent));
|
rt_pipe_detach(pipe);
|
||||||
|
|
||||||
/* release memory */
|
/* release memory */
|
||||||
rt_free(pipe->ringbuffer.buffer_ptr);
|
rt_free(pipe->ringbuffer.buffer_ptr);
|
||||||
|
@ -217,3 +242,4 @@ void rt_pipe_destroy(struct rt_pipe_device *pipe)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
RTM_EXPORT(rt_pipe_destroy);
|
RTM_EXPORT(rt_pipe_destroy);
|
||||||
|
#endif /* RT_USING_HEAP */
|
||||||
|
|
Loading…
Reference in New Issue