From aff4937e03f40813215e54945a4ba6a64f755406 Mon Sep 17 00:00:00 2001 From: Ian Abbott Date: Fri, 13 May 2016 15:14:47 +0100 Subject: [PATCH] Added some more buffer position functions for use with mmap Added comedi_get_buffer_read_offset(), comedi_get_buffer_write_offset(), comedi_get_buffer_read_count(), and comedi_get_buffer_write_count(). comedi_get_buffer_read_offset() is the same as comedi_get_buffer_offset(), which has now been documented as deprecated, but is currently still declared in "comedilib.h" by default. It gets the read position within the buffer as an offset from the start of the buffer (modulo the buffer size). comedi_get_buffer_write_offset() gets the write position within the buffer as an offset from the start of the buffer (modulo the buffer size). comedi_get_buffer_read_count() gets the number of bytes read from the buffer, modulo UINT_MAX+1. comedi_get_buffer_write_count() gets the number of bytes written to the buffer, modulo UINT_MAX+1. --- doc/command_funcref.txt | 64 ++++++++++++++++++++++++++++++++---- doc/deprecated_funcref.txt | 8 +++++ include/comedilib.h | 8 +++++ lib/buffer.c | 67 +++++++++++++++++++++++++++++++++++--- lib/version_script | 4 +++ swig/ruby/lib/comedi.rb | 4 +++ 6 files changed, 143 insertions(+), 12 deletions(-) diff --git a/doc/command_funcref.txt b/doc/command_funcref.txt index 6208740..9b9b836 100644 --- a/doc/command_funcref.txt +++ b/doc/command_funcref.txt @@ -119,21 +119,71 @@ Returns: number of unread bytes in the buffer. On failure, -1 is returned. -Function: comedi_get_buffer_offset -- streaming buffer status +Function: comedi_get_buffer_read_offset -- streaming buffer read offset Retval: int Param: comedi_t * device Param: unsigned int subdevice Description: - The function comedi_get_buffer_offset is used on a subdevice - that has a Comedi command in progress to get the current read position in the - streaming buffer as an offset in bytes from the start of the buffer. - The position will wrap around to 0 when it reaches the buffer size. - This offset is only useful for memory mapped buffers. + The function comedi_get_buffer_read_offset is used on + a subdevice that has a Comedi command in progress to get the current read + position in the streaming buffer as an offset in bytes from the start of + the buffer. The position will wrap around to 0 when it reaches the buffer + size. This offset is only useful for memory mapped buffers. + + This function replaces comedi_get_buffer_offset and has the same functionality. Returns: - On success, comedi_get_buffer_offset returns the current + On success, comedi_get_buffer_read_offset returns the current read position as an offset in bytes from the start of the buffer. On failure, -1 is returned. +Function: comedi_get_buffer_write_offset -- streaming buffer write offset +Retval: int +Param: comedi_t * device +Param: unsigned int subdevice +Description: + The function comedi_get_buffer_write_offset is used on + a subdevice that has a Comedi command in progress to get the current write + position in the streaming buffer as an offset in bytes from the start of + the buffer. The position will wrap around to 0 when it reaches the buffer + size. This offset is only useful for memory mapped buffers. +Returns: + On success, comedi_get_buffer_write_offset returns the + current write position as an offset in bytes from the start of the buffer. + On failure, -1 is returned. + +Function: comedi_get_buffer_read_count -- streaming buffer read count +Retval: int +Param: comedi_t * device +Param: unsigned int subdevice +Param: unsigned int * read_count +Description: + The function comedi_get_buffer_read_count is used on + a subdevice that has a Comedi command in progress to get the number of bytes + that have been read from the buffer, modulo + UINT_MAX + 1. The + value is stored in + *read_count. +Returns: + On success, 0 is returned. + On failure, -1 is returned. + +Function: comedi_get_buffer_write_count -- streaming buffer write count +Retval: int +Param: comedi_t * device +Param: unsigned int subdevice +Param: unsigned int * write_count +Description: + The function comedi_get_buffer_write_count is used on + a subdevice that has a Comedi command in progress to get the number of bytes + that have been written to the buffer, modulo + UINT_MAX + 1. The + value is stored in + *write_count. +Returns: + On success, 0 is returned. + On failure, -1 is returned. + Function: comedi_get_buffer_size -- streaming buffer size of subdevice Retval: int Param: comedi_t * device diff --git a/doc/deprecated_funcref.txt b/doc/deprecated_funcref.txt index 9fac2f5..eb523f3 100644 --- a/doc/deprecated_funcref.txt +++ b/doc/deprecated_funcref.txt @@ -13,6 +13,14 @@ Description: with base_channel set to 0. +Function: comedi_get_buffer_offset -- streaming buffer status (deprecated) +Status: deprecated +Description: + This function is deprecated. Use comedi_get_buffer_read_offset + instead. It has the same functionality as + comedi_get_buffer_read_offset. + Function: comedi_get_timer -- timer information (deprecated) Retval: int Param: comedi_t * device diff --git a/include/comedilib.h b/include/comedilib.h index 9bce2a0..60e4e94 100644 --- a/include/comedilib.h +++ b/include/comedilib.h @@ -201,6 +201,14 @@ int comedi_mark_buffer_read(comedi_t *it, unsigned int subdev, unsigned int bytes); int comedi_mark_buffer_written(comedi_t *it, unsigned int subdev, unsigned int bytes); +int comedi_get_buffer_read_offset(comedi_t *it, unsigned int subdev); +int comedi_get_buffer_write_offset(comedi_t *it, unsigned int subdev); +int comedi_get_buffer_read_count(comedi_t *it, unsigned int subdev, + unsigned int *SWIG_OUTPUT(read_count)); +int comedi_get_buffer_write_count(comedi_t *it, unsigned int subdev, + unsigned int *SWIG_OUTPUT(write_count)); + +/* Should be moved to _COMEDILIB_DEPRECATED - use comedi_get_buffer_read_offset instead. */ int comedi_get_buffer_offset(comedi_t *it, unsigned int subdev); #ifdef _COMEDILIB_DEPRECATED diff --git a/lib/buffer.c b/lib/buffer.c index dd286aa..a95e715 100644 --- a/lib/buffer.c +++ b/lib/buffer.c @@ -124,8 +124,8 @@ int _comedi_mark_buffer_written(comedi_t *it, unsigned int subdev, unsigned int return bi.bytes_written; } -EXPORT_ALIAS_DEFAULT(_comedi_get_buffer_offset,comedi_get_buffer_offset,0.7.18); -int _comedi_get_buffer_offset(comedi_t *it, unsigned int subdev) +EXPORT_ALIAS_DEFAULT(_comedi_get_buffer_read_offset,comedi_get_buffer_read_offset,0.11.0); +int _comedi_get_buffer_read_offset(comedi_t *it, unsigned int subdev) { int ret; comedi_bufinfo bi; @@ -138,8 +138,19 @@ int _comedi_get_buffer_offset(comedi_t *it, unsigned int subdev) return bi.buf_read_ptr; } -EXPORT_ALIAS_DEFAULT(_comedi_get_front_count,comedi_get_front_count,0.7.18); -int _comedi_get_front_count(comedi_t *it, unsigned int subdev) +/* + * Keep this for backwards compatibility as a synonym of + * comedi_get_buffer_read_offset(). + * TODO: mark this as deprecated. + */ +EXPORT_ALIAS_DEFAULT(_comedi_get_buffer_offset,comedi_get_buffer_offset,0.7.18); +int _comedi_get_buffer_offset(comedi_t *it, unsigned int subdev) +{ + return comedi_get_buffer_read_offset(it, subdev); +} + +EXPORT_ALIAS_DEFAULT(_comedi_get_buffer_write_offset,comedi_get_buffer_write_offset,0.11.0); +int _comedi_get_buffer_write_offset(comedi_t *it, unsigned int subdev) { int ret; comedi_bufinfo bi; @@ -149,6 +160,52 @@ int _comedi_get_front_count(comedi_t *it, unsigned int subdev) bi.subdevice = subdev; ret = comedi_ioctl(it->fd, COMEDI_BUFINFO, &bi); if(ret < 0) return ret; - return bi.buf_write_count; + return bi.buf_write_ptr; } +EXPORT_ALIAS_DEFAULT(_comedi_get_buffer_read_count,comedi_get_buffer_read_count,0.11.0); +int _comedi_get_buffer_read_count(comedi_t *it, unsigned int subdev, unsigned int *read_count) +{ + int ret; + comedi_bufinfo bi; + + *read_count = 0; + if(!valid_subd(it,subdev)) return -1; + memset(&bi, 0, sizeof(bi)); + bi.subdevice = subdev; + ret = comedi_ioctl(it->fd, COMEDI_BUFINFO, &bi); + if(ret < 0) return ret; + *read_count = bi.buf_read_count; + return 0; +} + +EXPORT_ALIAS_DEFAULT(_comedi_get_buffer_write_count,comedi_get_buffer_write_count,0.11.0); +int _comedi_get_buffer_write_count(comedi_t *it, unsigned int subdev, unsigned int *write_count) +{ + int ret; + comedi_bufinfo bi; + + *write_count = 0; + if(!valid_subd(it,subdev)) return -1; + memset(&bi, 0, sizeof(bi)); + bi.subdevice = subdev; + ret = comedi_ioctl(it->fd, COMEDI_BUFINFO, &bi); + if(ret < 0) return ret; + *write_count = bi.buf_write_count; + return 0; +} + +/* + * Keep _comedi_get_front_count for backwards compatibility. + * It is not in "comedilib.h" and is not documented. + */ +EXPORT_ALIAS_DEFAULT(_comedi_get_front_count,comedi_get_front_count,0.7.18); +int _comedi_get_front_count(comedi_t *it, unsigned int subdev) +{ + unsigned int write_count; + int ret; + + ret = _comedi_get_buffer_write_count(it, subdev, &write_count); + if (ret < 0) return ret; + return write_count; +} diff --git a/lib/version_script b/lib/version_script index 6d76fbc..465c63f 100644 --- a/lib/version_script +++ b/lib/version_script @@ -128,4 +128,8 @@ v0.11.0 { comedi_digital_trigger_enable_levels; comedi_set_read_subdevice; comedi_set_write_subdevice; + comedi_get_buffer_read_offset; + comedi_get_buffer_write_offset; + comedi_get_buffer_read_count; + comedi_get_buffer_write_count; } v0.10.0; diff --git a/swig/ruby/lib/comedi.rb b/swig/ruby/lib/comedi.rb index f53a044..4916f98 100644 --- a/swig/ruby/lib/comedi.rb +++ b/swig/ruby/lib/comedi.rb @@ -211,6 +211,8 @@ private mark_buffer_read mark_buffer_written get_buffer_offset + get_buffer_read_offset + get_buffer_write_offset get_softcal_converter get_hardcal_converter internal_trigger @@ -247,6 +249,8 @@ private get_cmd_generic_timed get_gate_source get_routing + get_buffer_read_count + get_buffer_write_count }), # TODO: add get_clock_source, but it returns status and two values.