Be a little more correct by setting CMDF_WRITE flag, in preparation
[comedilib.git] / lib / buffer.c
1 /*
2     lib/buffer.c
3     functions for manipulating buffers
4
5     COMEDILIB - Linux Control and Measurement Device Interface Library
6     Copyright (C) 1997-2001 David A. Schleef <ds@schleef.org>
7
8     This library is free software; you can redistribute it and/or
9     modify it under the terms of the GNU Lesser General Public
10     License as published by the Free Software Foundation, version 2.1
11     of the License.
12
13     This library is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16     Lesser General Public License for more details.
17
18     You should have received a copy of the GNU Lesser General Public
19     License along with this library; if not, write to the Free Software
20     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
21     USA.
22 */
23
24 #include "libinternal.h"
25 #include <string.h>
26
27 EXPORT_ALIAS_DEFAULT(_comedi_set_buffer_size,comedi_set_buffer_size,0.7.18);
28 int _comedi_set_buffer_size(comedi_t *it, unsigned int subdev, unsigned int size)
29 {
30         int ret;
31         comedi_bufconfig bc;
32
33         memset(&bc, 0, sizeof(bc));
34         bc.subdevice = subdev;
35         bc.size = size;
36         ret = comedi_ioctl(it->fd, COMEDI_BUFCONFIG, (unsigned long)&bc);
37         __comedi_errno = errno;
38         if(ret < 0) return ret;
39
40         return bc.size;
41 }
42
43 EXPORT_ALIAS_DEFAULT(_comedi_set_max_buffer_size,comedi_set_max_buffer_size,0.7.18);
44 int _comedi_set_max_buffer_size(comedi_t *it, unsigned int subdev, unsigned int max_size)
45 {
46         int ret;
47         comedi_bufconfig bc;
48
49         memset(&bc, 0, sizeof(bc));
50         bc.subdevice = subdev;
51         bc.maximum_size = max_size;
52         ret = comedi_ioctl(it->fd, COMEDI_BUFCONFIG, (unsigned long)&bc);
53         __comedi_errno = errno;
54         if(ret < 0) return ret;
55
56         return bc.maximum_size;
57 }
58
59 EXPORT_ALIAS_DEFAULT(_comedi_get_max_buffer_size,comedi_get_max_buffer_size,0.7.18);
60 int _comedi_get_max_buffer_size(comedi_t *it, unsigned int subdevice)
61 {
62         return comedi_set_max_buffer_size(it, subdevice, 0);
63 }
64
65 EXPORT_ALIAS_DEFAULT(_comedi_get_buffer_size,comedi_get_buffer_size,0.7.18);
66 int _comedi_get_buffer_size(comedi_t *it, unsigned int subdev)
67 {
68         return comedi_set_buffer_size(it, subdev, 0);
69 }
70
71 EXPORT_ALIAS_DEFAULT(_comedi_get_buffer_contents,comedi_get_buffer_contents,0.7.18);
72 int _comedi_get_buffer_contents(comedi_t *it, unsigned int subdev)
73 {
74         return comedi_mark_buffer_read(it, subdev, 0);
75 }
76
77 EXPORT_ALIAS_DEFAULT(_comedi_mark_buffer_read,comedi_mark_buffer_read,0.7.18);
78 int _comedi_mark_buffer_read(comedi_t *it, unsigned int subdev, unsigned int bytes)
79 {
80         int ret;
81         comedi_bufinfo bi;
82
83         memset(&bi, 0, sizeof(bi));
84         bi.subdevice = subdev;
85         bi.bytes_read = bytes;
86         ret = comedi_ioctl(it->fd, COMEDI_BUFINFO, (unsigned long)&bi);
87         if(ret < 0)
88         {
89                 __comedi_errno = errno;
90                 if(__comedi_errno == EPIPE)__comedi_errno = EBUF_OVR;
91                 return ret;
92         }
93         return bi.buf_write_count - bi.buf_read_count;
94 }
95
96 EXPORT_ALIAS_DEFAULT(_comedi_mark_buffer_written,comedi_mark_buffer_written,0.8.0);
97 int _comedi_mark_buffer_written(comedi_t *it, unsigned int subdev, unsigned int bytes)
98 {
99         int ret;
100         comedi_bufinfo bi;
101
102         memset(&bi, 0, sizeof(bi));
103         bi.subdevice = subdev;
104         bi.bytes_written = bytes;
105         ret = comedi_ioctl(it->fd, COMEDI_BUFINFO, (unsigned long)&bi);
106         if(ret < 0)
107         {
108                 __comedi_errno = errno;
109                 if(__comedi_errno == EPIPE)__comedi_errno = EBUF_UNDR;
110                 return ret;
111         }
112         return bi.buf_write_count - bi.buf_read_count;
113 }
114
115 EXPORT_ALIAS_DEFAULT(_comedi_get_buffer_offset,comedi_get_buffer_offset,0.7.18);
116 int _comedi_get_buffer_offset(comedi_t *it, unsigned int subdev)
117 {
118         int ret;
119         comedi_bufinfo bi;
120
121         memset(&bi, 0, sizeof(bi));
122         bi.subdevice = subdev;
123         ret = comedi_ioctl(it->fd, COMEDI_BUFINFO, (unsigned long)&bi);
124         if(ret < 0) return ret;
125         return bi.buf_read_ptr;
126 }
127
128 EXPORT_ALIAS_DEFAULT(_comedi_get_front_count,comedi_get_front_count,0.7.18);
129 int _comedi_get_front_count(comedi_t *it, unsigned int subdev)
130 {
131         int ret;
132         comedi_bufinfo bi;
133
134         memset(&bi, 0, sizeof(bi));
135         bi.subdevice = subdev;
136         ret = comedi_ioctl(it->fd, COMEDI_BUFINFO, (unsigned long)&bi);
137         if(ret < 0) return ret;
138         return bi.buf_write_count;
139 }
140