~ubuntu-branches/ubuntu/intrepid/comedilib/intrepid

« back to all changes in this revision

Viewing changes to lib/cmd.c

  • Committer: Bazaar Package Importer
  • Author(s): David Schleef
  • Date: 2003-09-23 18:11:12 UTC
  • Revision ID: james.westby@ubuntu.com-20030923181112-sat05jyh702rb1at
Tags: upstream-0.7.21
ImportĀ upstreamĀ versionĀ 0.7.21

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    lib/cmd.c
 
3
    support functions for commands
 
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 <fcntl.h>
 
26
#include <unistd.h>
 
27
#include <sys/ioctl.h>
 
28
#include <errno.h>
 
29
#include <getopt.h>
 
30
#include <ctype.h>
 
31
#include <math.h>
 
32
#include <sys/time.h>
 
33
#include <string.h>
 
34
#include <stdlib.h>
 
35
 
 
36
#include "libinternal.h"
 
37
 
 
38
 
 
39
EXPORT_ALIAS_DEFAULT(_comedi_get_cmd_src_mask,comedi_get_cmd_src_mask,0.7.18);
 
40
int _comedi_get_cmd_src_mask(comedi_t *it,unsigned int subd,comedi_cmd *cmd)
 
41
{
 
42
        subdevice *s;
 
43
        int ret;
 
44
 
 
45
        if(!valid_subd(it,subd))return -1;
 
46
 
 
47
        s=it->subdevices+subd;
 
48
 
 
49
        if(s->cmd_mask_errno){
 
50
                errno = s->cmd_mask_errno;
 
51
                return -1;
 
52
        }
 
53
 
 
54
        if(!s->cmd_mask){
 
55
                comedi_cmd *mask;
 
56
 
 
57
                mask = malloc(sizeof(comedi_cmd));
 
58
 
 
59
                memset(mask,0,sizeof(*cmd));
 
60
 
 
61
                mask->subdev = subd;
 
62
                mask->flags = 0;
 
63
                mask->start_src = TRIG_ANY;
 
64
                mask->scan_begin_src = TRIG_ANY;
 
65
                mask->convert_src = TRIG_ANY;
 
66
                mask->scan_end_src = TRIG_ANY;
 
67
                mask->stop_src = TRIG_ANY;
 
68
 
 
69
                s->cmd_mask = mask;
 
70
 
 
71
                ret = comedi_command_test(it,mask);
 
72
                if(ret<0){
 
73
                        s->cmd_mask_errno = errno;
 
74
                        return -1;
 
75
                }
 
76
        }
 
77
        *cmd=*s->cmd_mask;
 
78
        return 0;
 
79
}
 
80
 
 
81
static int __generic_timed(comedi_t *it,unsigned int s,
 
82
        comedi_cmd *cmd, unsigned int ns)
 
83
{
 
84
        int ret;
 
85
 
 
86
        ret = comedi_get_cmd_src_mask(it,s,cmd);
 
87
        if(ret<0)return ret;
 
88
 
 
89
        __comedi_errno = ENOTSUPPORTED;
 
90
 
 
91
        if(cmd->start_src&TRIG_NOW){
 
92
                cmd->start_src=TRIG_NOW;
 
93
                cmd->start_arg=0;
 
94
        }else if(cmd->start_src&TRIG_INT){
 
95
                cmd->start_src=TRIG_INT;
 
96
                cmd->start_arg=0;
 
97
        }else{
 
98
                COMEDILIB_DEBUG(3,"can't find good start_src\n");
 
99
                return -1;
 
100
        }
 
101
 
 
102
        /* Potential bug: there is a possibility that the source mask may
 
103
         * have * TRIG_TIMER set for both convert_src and scan_begin_src,
 
104
         * but they may not be supported together. */
 
105
        if(cmd->convert_src&TRIG_TIMER){
 
106
                if(cmd->scan_begin_src&TRIG_FOLLOW){
 
107
                        cmd->convert_src = TRIG_TIMER;
 
108
                        cmd->convert_arg = ns;
 
109
                        cmd->scan_begin_src = TRIG_FOLLOW;
 
110
                        cmd->scan_begin_arg = 0;
 
111
                }else{
 
112
                        cmd->convert_src = TRIG_TIMER;
 
113
                        cmd->convert_arg = ns;
 
114
                        cmd->scan_begin_src = TRIG_TIMER;
 
115
                        cmd->scan_begin_arg = ns;
 
116
                }
 
117
        }else if(cmd->convert_src & TRIG_NOW &&
 
118
                cmd->scan_begin_src & TRIG_TIMER)
 
119
        {
 
120
                cmd->convert_src = TRIG_NOW;
 
121
                cmd->convert_arg = 0;
 
122
                cmd->scan_begin_src = TRIG_TIMER;
 
123
                cmd->scan_begin_arg = ns;
 
124
        }else{
 
125
                COMEDILIB_DEBUG(3,"comedi_get_cmd_generic_timed: can't do timed?\n");
 
126
                return -1;
 
127
        }
 
128
 
 
129
        cmd->scan_end_src = TRIG_COUNT;
 
130
        cmd->scan_end_arg = 1;
 
131
 
 
132
        if(cmd->stop_src&TRIG_COUNT){
 
133
                cmd->stop_src=TRIG_COUNT;
 
134
                cmd->stop_arg=2;
 
135
        }else if(cmd->stop_src&TRIG_NONE){
 
136
                cmd->stop_src=TRIG_NONE;
 
137
                cmd->stop_arg=0;
 
138
        }else{
 
139
                COMEDILIB_DEBUG(3,"comedi_get_cmd_generic_timed: can't find a good stop_src\n");
 
140
                return -1;
 
141
        }
 
142
 
 
143
        cmd->chanlist_len = 1;
 
144
 
 
145
        ret=comedi_command_test(it,cmd);
 
146
        COMEDILIB_DEBUG(3,"comedi_get_cmd_generic_timed: test 1 returned %d\n",ret);
 
147
        if(ret==3){
 
148
                /* good */
 
149
                ret=comedi_command_test(it,cmd);
 
150
                COMEDILIB_DEBUG(3,"comedi_get_cmd_generic_timed: test 2 returned %d\n",ret);
 
151
        }
 
152
        if(ret==4 || ret==0){
 
153
                __comedi_errno = 0;
 
154
                return 0;
 
155
        }
 
156
        return -1;
 
157
}
 
158
 
 
159
EXPORT_ALIAS_DEFAULT(_comedi_get_cmd_generic_timed,comedi_get_cmd_generic_timed,0.7.18);
 
160
int _comedi_get_cmd_generic_timed(comedi_t *it,unsigned int subd,comedi_cmd *cmd,
 
161
        unsigned int ns)
 
162
{
 
163
        subdevice *s;
 
164
        int ret;
 
165
 
 
166
        if(!valid_subd(it,subd))return -1;
 
167
 
 
168
        s=it->subdevices+subd;
 
169
 
 
170
        if(s->cmd_timed_errno){
 
171
                errno = s->cmd_mask_errno;
 
172
                return -1;
 
173
        }
 
174
 
 
175
        if(!s->cmd_timed){
 
176
                s->cmd_timed = malloc(sizeof(comedi_cmd));
 
177
 
 
178
                ret = __generic_timed(it,subd,s->cmd_timed,ns);
 
179
                if(ret<0){
 
180
                        s->cmd_mask_errno = errno;
 
181
                        return -1;
 
182
                }
 
183
        }
 
184
        *cmd=*s->cmd_timed;
 
185
        return 0;
 
186
}
 
187