20dc2e2d958b9984a1299a2962af40306784f78d
[comedilib.git] / lib / filler.c
1 /*
2     lib/filler.c
3     comedi library routines
4
5     COMEDI - Linux Control and Measurement Device Interface
6     Copyright (C) 1997-8 David A. Schleef <ds@stm.lbl.gov>
7
8     This program is free software; you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by
10     the Free Software Foundation; either version 2 of the License, or
11     (at your option) any later version.
12
13     This program 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
16     GNU General Public License for more details.
17
18     You should have received a copy of the GNU General Public License
19     along with this program; if not, write to the Free Software
20     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
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 <comedi.h>
34 #include <string.h>
35
36 #include <libinternal.h>
37
38
39 /* these functions download information from the comedi module. */
40
41 static int do_test_for_cmd(comedi_t *dev,unsigned int subdevice);
42 static int do_test_for_insn(comedi_t *dev,unsigned int subdevice);
43 static int do_test_for_insn_bits(comedi_t *dev,unsigned int subdevice);
44
45
46 int get_subdevices(comedi_t *it)
47 {
48         int i,j;
49         int ret;
50         comedi_subdinfo *s;
51         subdevice *r;
52
53         s=malloc(sizeof(comedi_subdinfo)*it->n_subdevices);
54         ret=ioctl_subdinfo(it->fd,s);
55         debug_int(ret);
56
57         r=it->subdevices=realloc(it->subdevices,
58                 sizeof(subdevice)*it->n_subdevices);
59         debug_ptr(r);
60         memset(r,0,sizeof(subdevice)*it->n_subdevices);
61
62         for(i=0;i<it->n_subdevices;i++){
63                 r[i].type       = s[i].type;
64                 if(r[i].type==COMEDI_SUBD_UNUSED)continue;
65                 r[i].n_chan     = s[i].n_chan;
66                 r[i].subd_flags = s[i].subd_flags;
67                 r[i].timer_type = s[i].timer_type;
68                 r[i].len_chanlist = s[i].len_chanlist;
69                 r[i].maxdata    = s[i].maxdata;
70                 r[i].flags      = s[i].flags;
71                 r[i].range_type = s[i].range_type;
72
73                 if(r[i].subd_flags&SDF_FLAGS){
74                         r[i].flags_list=malloc(sizeof(*r[i].flags_list)*r[i].n_chan);
75                         debug_ptr(r[i].flags_list);
76                 }
77                 if(r[i].subd_flags&SDF_MAXDATA){
78                         r[i].maxdata_list=malloc(sizeof(*r[i].maxdata_list)*r[i].n_chan);
79                         debug_ptr(r[i].maxdata_list);
80                 }
81                 if(r[i].subd_flags&SDF_RANGETYPE){
82                         r[i].range_type_list=malloc(sizeof(*r[i].range_type_list)*r[i].n_chan);
83                         debug_ptr(r[i].range_type_list);
84                 }
85                 ret=ioctl_chaninfo(it->fd,i,r[i].maxdata_list,r[i].flags_list,r[i].range_type_list);
86                 debug_int(ret);
87
88                 if(r[i].subd_flags&SDF_RANGETYPE){
89                         r[i].rangeinfo_list=malloc(sizeof(*r[i].rangeinfo_list)*r[i].n_chan);
90                         debug_ptr(r[i].rangeinfo_list);
91                         for(j=0;j<r[i].n_chan;j++){
92                                 r[i].rangeinfo_list[j]=get_rangeinfo(it->fd,r[i].range_type_list[j]);
93                         }
94                 }else{
95                         r[i].rangeinfo=get_rangeinfo(it->fd,r[i].range_type);
96                 }
97
98                 r[i].has_cmd = do_test_for_cmd(it,i);
99                 r[i].has_insn = do_test_for_insn(it,i);
100                 if(r[i].has_insn){
101                         r[i].has_insn_bits = do_test_for_insn_bits(it,i);
102                 }else{
103                         r[i].has_insn_bits = 0;
104                 }
105         }
106
107         free(s);
108
109         return 0;
110 }
111
112 comedi_range *get_rangeinfo(int fd,unsigned int range_type)
113 {
114         comedi_krange *kr;
115         comedi_range *r;
116         int ret;
117         int i;
118
119         kr=malloc(sizeof(comedi_krange)*RANGE_LENGTH(range_type));
120         r=malloc(sizeof(comedi_range)*RANGE_LENGTH(range_type));
121
122         ret=ioctl_rangeinfo(fd,range_type,kr);
123         if(ret<0){
124                 fprintf(stderr,"ioctl_rangeinfo(%d,0x%08x,%p)\n",fd,range_type,kr);
125         }
126
127         for(i=0;i<RANGE_LENGTH(range_type);i++){
128                 r[i].min=kr[i].min*1e-6;
129                 r[i].max=kr[i].max*1e-6;
130                 r[i].unit=RF_UNIT(kr[i].flags);
131         }
132         free(kr);
133
134         return r;
135 }
136
137
138 /* some command testing */
139
140 static int do_test_for_cmd(comedi_t *dev,unsigned int subdevice)
141 {
142         comedi_cmd it;
143         int ret;
144
145         memset(&it,0,sizeof(it));
146
147         it.subdev = 0;
148         it.start_src = TRIG_ANY;
149         it.scan_begin_src = TRIG_ANY;
150         it.convert_src = TRIG_ANY;
151         it.scan_end_src = TRIG_ANY;
152         it.stop_src = TRIG_ANY;
153
154         ret = ioctl(dev->fd,COMEDI_CMDTEST,&it);
155
156         if(ret<0 && errno==EIO){
157                 return 0;
158         }
159         if(ret<0){
160                 fprintf(stderr,"BUG in do_test_for_cmd()\n");
161                 return 0;
162         }
163         return 1;
164 }
165
166 static int do_test_for_insn(comedi_t *dev,unsigned int subdevice)
167 {
168         comedi_insn insn;
169         comedi_insnlist il;
170         lsampl_t data[2];
171         int ret;
172
173         memset(&insn,0,sizeof(insn));
174
175         il.n_insns = 1;
176         il.insns = &insn;
177
178         insn.insn = INSN_GTOD;
179         insn.n = 2;
180         insn.data = data;
181         insn.subdev = subdevice;
182
183         ret = comedi_do_insnlist(dev,&il);
184
185         if(ret<0 && errno==EIO){
186                 return 0;
187         }
188         if(ret<0){
189                 fprintf(stderr,"BUG in do_test_for_insn()\n");
190                 return 0;
191         }
192         return 1;
193 }
194
195 static int do_test_for_insn_bits(comedi_t *dev,unsigned int subdevice)
196 {
197         comedi_insn insn;
198         comedi_insnlist il;
199         lsampl_t data[2];
200         int ret;
201
202         memset(&insn,0,sizeof(insn));
203
204         il.n_insns = 1;
205         il.insns = &insn;
206
207         insn.insn = INSN_BITS;
208         insn.n = 2;
209         insn.data = data;
210         insn.subdev = subdevice;
211
212         data[0]=0;
213         data[1]=0;
214
215         ret = comedi_do_insnlist(dev,&il);
216
217         if(ret<0 && (errno==EINVAL || errno==EIO)){
218                 return 0;
219         }
220         if(ret<0){
221                 fprintf(stderr,"BUG in do_test_for_insn_bits()\n");
222                 return 0;
223         }
224         return 1;
225 }
226
227
228