doc: add missing -lm option to command line for compiling tut1
[comedilib.git] / lib / get.c
1 /*
2     lib/get.c
3     functions to return information about comedi devices
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 <stdio.h>
25 #include <math.h>
26 #include <stdlib.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <fcntl.h>
30 #include <unistd.h>
31 #include <sys/ioctl.h>
32 #include <errno.h>
33 #include <string.h>
34
35 #include "libinternal.h"
36
37
38 EXPORT_ALIAS_DEFAULT(_comedi_get_n_subdevices,comedi_get_n_subdevices,0.7.18);
39 int _comedi_get_n_subdevices(comedi_t *it)
40 {
41         if(!valid_dev(it))
42                 return -1;
43
44         return it->n_subdevices;
45 }
46
47 EXPORT_ALIAS_DEFAULT(_comedi_get_version_code,comedi_get_version_code,0.7.18);
48 int _comedi_get_version_code(comedi_t *it)
49 {
50         if(!valid_dev(it))
51                 return -1;
52
53         return it->devinfo.version_code;
54 }
55
56 EXPORT_ALIAS_DEFAULT(_comedi_get_driver_name,comedi_get_driver_name,0.7.18);
57 const char* _comedi_get_driver_name(comedi_t *it)
58 {
59         if(!valid_dev(it))
60                 return NULL;
61
62         return it->devinfo.driver_name;
63 }
64
65 EXPORT_ALIAS_DEFAULT(_comedi_get_board_name,comedi_get_board_name,0.7.18);
66 const char* _comedi_get_board_name(comedi_t *it)
67 {
68         if(!valid_dev(it))
69                 return NULL;
70
71         return it->devinfo.board_name;
72 }
73
74 EXPORT_ALIAS_VER(_comedi_get_subdevice_flags_old, comedi_get_subdevice_flags,0.7.18);
75 int _comedi_get_subdevice_flags_old(comedi_t *it,unsigned int subd)
76 {
77         if(!valid_subd(it,subd))
78                 return 0;
79         return it->subdevices[subd].subd_flags;
80 }
81
82 EXPORT_ALIAS_DEFAULT(_comedi_get_subdevice_flags,comedi_get_subdevice_flags,0.8.0);
83 int _comedi_get_subdevice_flags(comedi_t *it,unsigned int subd)
84 {
85         comedi_subdinfo *s;
86         int flags;
87         int ret;
88         if(!valid_subd(it,subd))
89                 return -1;
90         s = calloc(it->n_subdevices, sizeof(comedi_subdinfo));
91         if(s == NULL)
92         {
93                 libc_error();
94                 return -1;
95         }
96         ret = comedi_ioctl(it->fd, COMEDI_SUBDINFO, s);
97         if(ret < 0)
98         {
99                 free(s);
100                 return -1;
101         }
102         flags = s[subd].subd_flags;
103         free(s);
104         return flags;
105 }
106
107 EXPORT_ALIAS_DEFAULT(_comedi_get_subdevice_type,comedi_get_subdevice_type,0.7.18);
108 int _comedi_get_subdevice_type(comedi_t *it,unsigned int subd)
109 {
110         if(!valid_subd(it,subd))
111                 return -1;
112
113         return it->subdevices[subd].type;
114 }
115
116 EXPORT_ALIAS_DEFAULT(_comedi_find_subdevice_by_type,comedi_find_subdevice_by_type,0.7.18);
117 int _comedi_find_subdevice_by_type(comedi_t *it,int type,unsigned int subd)
118 {
119         if(!valid_subd(it,subd))
120                 return -1;
121
122         for(;subd<it->n_subdevices;subd++){
123                 if(it->subdevices[subd].type==type)
124                         return subd;
125         }
126         return -1;
127 }
128
129 EXPORT_ALIAS_DEFAULT(_comedi_get_read_subdevice,comedi_get_read_subdevice,0.7.19);
130 int _comedi_get_read_subdevice(comedi_t *dev)
131 {
132         if(!valid_dev(dev))
133                 return -1;
134
135         return dev->devinfo.read_subdevice;
136 }
137
138 EXPORT_ALIAS_DEFAULT(_comedi_get_write_subdevice,comedi_get_write_subdevice,0.7.19);
139 int _comedi_get_write_subdevice(comedi_t *dev)
140 {
141         if(!valid_dev(dev))
142                 return -1;
143
144         return dev->devinfo.write_subdevice;
145 }
146
147 EXPORT_ALIAS_DEFAULT(_comedi_get_n_channels,comedi_get_n_channels,0.7.18);
148 int _comedi_get_n_channels(comedi_t *it,unsigned int subd)
149 {
150         if(!valid_subd(it,subd))
151                 return -1;
152
153         return it->subdevices[subd].n_chan;
154 }
155
156
157 /* */
158
159 EXPORT_ALIAS_DEFAULT(_comedi_get_maxdata,comedi_get_maxdata,0.7.18);
160 lsampl_t _comedi_get_maxdata(comedi_t *it,unsigned int subdevice,unsigned int chan)
161 {
162         if(!valid_chan(it,subdevice,chan))
163                 return 0;
164
165         if(it->subdevices[subdevice].maxdata_list)
166                 return it->subdevices[subdevice].maxdata_list[chan];
167
168         return it->subdevices[subdevice].maxdata;
169 }
170
171 EXPORT_ALIAS_DEFAULT(_comedi_maxdata_is_chan_specific,comedi_maxdata_is_chan_specific,0.7.18);
172 int _comedi_maxdata_is_chan_specific(comedi_t *it,unsigned int subdevice)
173 {
174         if(!valid_subd(it,subdevice))
175                 return -1;
176         if(it->subdevices[subdevice].maxdata_list)
177                 return 1;
178         return 0;
179 }
180
181 EXPORT_ALIAS_DEFAULT(_comedi_get_rangetype,comedi_get_rangetype,0.7.18);
182 int _comedi_get_rangetype(comedi_t *it,unsigned int subdevice,unsigned int chan)
183 {
184         if(!valid_chan(it,subdevice,chan))
185                 return -1;
186
187         if(it->subdevices[subdevice].range_type_list)
188                 return it->subdevices[subdevice].range_type_list[chan];
189
190         return it->subdevices[subdevice].range_type;
191 }
192
193
194 EXPORT_ALIAS_DEFAULT(_comedi_get_range,comedi_get_range,0.7.18);
195 comedi_range * _comedi_get_range(comedi_t *it,unsigned int subdevice,unsigned int chan,unsigned int range)
196 {
197         int range_type;
198
199         if(!valid_chan(it,subdevice,chan))
200                 return NULL;
201
202         range_type=comedi_get_rangetype(it,subdevice,chan);
203
204         if(range>=RANGE_LENGTH(range_type))
205                 return NULL;
206
207         if(it->subdevices[subdevice].rangeinfo_list)
208                 return it->subdevices[subdevice].rangeinfo_list[chan]+range;
209
210         return it->subdevices[subdevice].rangeinfo+range;
211 }
212
213