dde1e0e095b6838ad43793339483c8014b1616e0
[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
42
43 int get_subdevices(comedi_t *it)
44 {
45         int i,j;
46         int ret;
47         comedi_subdinfo *s;
48         subdevice *r;
49
50         s=malloc(sizeof(comedi_subdinfo)*it->n_subdevices);
51         ret=ioctl_subdinfo(it->fd,s);
52         debug_int(ret);
53
54         r=it->subdevices=realloc(it->subdevices,
55                 sizeof(subdevice)*it->n_subdevices);
56         debug_ptr(r);
57         memset(r,0,sizeof(subdevice)*it->n_subdevices);
58
59         for(i=0;i<it->n_subdevices;i++){
60                 r[i].type       = s[i].type;
61                 if(r[i].type==COMEDI_SUBD_UNUSED)continue;
62                 r[i].n_chan     = s[i].n_chan;
63                 r[i].subd_flags = s[i].subd_flags;
64                 r[i].timer_type = s[i].timer_type;
65                 r[i].len_chanlist = s[i].len_chanlist;
66                 r[i].maxdata    = s[i].maxdata;
67                 r[i].flags      = s[i].flags;
68                 r[i].range_type = s[i].range_type;
69
70                 if(r[i].subd_flags&SDF_FLAGS){
71                         r[i].flags_list=malloc(sizeof(*r[i].flags_list)*r[i].n_chan);
72                         debug_ptr(r[i].flags_list);
73                 }
74                 if(r[i].subd_flags&SDF_MAXDATA){
75                         r[i].maxdata_list=malloc(sizeof(*r[i].maxdata_list)*r[i].n_chan);
76                         debug_ptr(r[i].maxdata_list);
77                 }
78                 if(r[i].subd_flags&SDF_RANGETYPE){
79                         r[i].range_type_list=malloc(sizeof(*r[i].range_type_list)*r[i].n_chan);
80                         debug_ptr(r[i].range_type_list);
81                 }
82                 ret=ioctl_chaninfo(it->fd,i,r[i].maxdata_list,r[i].flags_list,r[i].range_type_list);
83                 debug_int(ret);
84
85                 if(r[i].subd_flags&SDF_RANGETYPE){
86                         r[i].rangeinfo_list=malloc(sizeof(*r[i].rangeinfo_list)*r[i].n_chan);
87                         debug_ptr(r[i].rangeinfo_list);
88                         for(j=0;j<r[i].n_chan;j++){
89                                 r[i].rangeinfo_list[j]=get_rangeinfo(it->fd,r[i].range_type_list[j]);
90                         }
91                 }else{
92                         r[i].rangeinfo=get_rangeinfo(it->fd,r[i].range_type);
93                 }
94         }
95
96         free(s);
97
98         return 0;
99 }
100
101 comedi_range *get_rangeinfo(int fd,unsigned int range_type)
102 {
103         comedi_krange *kr;
104         comedi_range *r;
105         int i;
106
107         kr=malloc(sizeof(comedi_krange)*RANGE_LENGTH(range_type));
108         r=malloc(sizeof(comedi_range)*RANGE_LENGTH(range_type));
109
110         ioctl_rangeinfo(fd,range_type,kr);
111
112         for(i=0;i<RANGE_LENGTH(range_type);i++){
113                 r[i].min=kr[i].min*1e-6;
114                 r[i].max=kr[i].max*1e-6;
115                 r[i].unit=RF_UNIT(kr[i].flags);
116         }
117         free(kr);
118
119         return r;
120 }
121
122