doc/dio_funcref.txt: Some DocBook mark-up changes.
[comedilib.git] / testing / cmd_1.c
1
2 #include <stdio.h>
3 #include <comedilib.h>
4 #include <fcntl.h>
5 #include <unistd.h>
6 #include <sys/ioctl.h>
7 #include <errno.h>
8 #include <getopt.h>
9 #include <ctype.h>
10 #include <math.h>
11 #include <sys/time.h>
12 #include <string.h>
13 #include "comedi_test.h"
14
15 void probe_max_1chan(comedi_t *it,int s);
16 char *tobinary(char *s,int bits,int n);
17 char *cmd_src(int src,char *buf);
18 int count_bits(unsigned int bits);
19
20 int test_cmd_no_cmd(void)
21 {
22         int ret;
23         comedi_cmd cmd;
24
25         if(comedi_get_subdevice_flags(device,subdevice)&SDF_CMD){
26                 printf("not applicable\n");
27                 return 0;
28         }
29
30         ret = comedi_get_cmd_src_mask(device,subdevice,&cmd);
31         if(ret<0){
32                 if(errno == EIO){
33                         printf("got EIO, good\n");
34                 }else{
35                         printf("E: comedi_get_cmd_src_mask: %s\n",
36                                 strerror(errno));
37                 }
38         }else{
39                 printf("E: comedi_get_cmd_src_mask returned %d\n",ret);
40         }
41
42         return 0;
43 }
44
45 int test_cmd_probe_src_mask(void)
46 {
47         comedi_cmd cmd;
48         char buf[100];
49         int ret;
50
51         if(!(comedi_get_subdevice_flags(device,subdevice)&SDF_CMD)){
52                 printf("not applicable\n");
53                 return 0;
54         }
55
56         printf("rev 1\n");
57
58         ret = comedi_get_cmd_src_mask(device,subdevice,&cmd);
59         if(ret<0){
60                 printf("E: comedi_get_cmd_src_mask failed %s\n",
61                         strerror(errno));
62                 return 0;
63         }
64         printf("command source mask:\n");
65         printf("  start: %s\n",cmd_src(cmd.start_src,buf));
66         printf("  scan_begin: %s\n",cmd_src(cmd.scan_begin_src,buf));
67         printf("  convert: %s\n",cmd_src(cmd.convert_src,buf));
68         printf("  scan_end: %s\n",cmd_src(cmd.scan_end_src,buf));
69         printf("  stop: %s\n",cmd_src(cmd.stop_src,buf));
70
71         return 0;
72 }
73
74 int test_cmd_probe_fast_1chan(void)
75 {
76         comedi_cmd cmd;
77         char buf[100];
78
79         if(!(comedi_get_subdevice_flags(device,subdevice)&SDF_CMD)){
80                 printf("not applicable\n");
81                 return 0;
82         }
83
84         printf("command fast 1chan:\n");
85         if(comedi_get_cmd_generic_timed(device,subdevice, &cmd, 1, 1)<0){
86                 printf("  not supported\n");
87                 return 0;
88         }
89         printf("  start: %s %d\n",
90                 cmd_src(cmd.start_src,buf),cmd.start_arg);
91         printf("  scan_begin: %s %d\n",
92                 cmd_src(cmd.scan_begin_src,buf),cmd.scan_begin_arg);
93         printf("  convert: %s %d\n",
94                 cmd_src(cmd.convert_src,buf),cmd.convert_arg);
95         printf("  scan_end: %s %d\n",
96                 cmd_src(cmd.scan_end_src,buf),cmd.scan_end_arg);
97         printf("  stop: %s %d\n",
98                 cmd_src(cmd.stop_src,buf),cmd.stop_arg);
99
100         return 0;
101 }
102
103 #define BUFSZ 2000
104
105 int test_cmd_read_fast_1chan(void)
106 {
107         comedi_cmd cmd;
108         char buf[BUFSZ];
109         unsigned int chanlist[1];
110         int go;
111         int total=0;
112         int ret;
113         unsigned int flags = comedi_get_subdevice_flags(device,subdevice);
114
115         if(!(flags&SDF_CMD) || (comedi_get_read_subdevice(device)!=subdevice)){
116                 printf("not applicable\n");
117                 return 0;
118         }
119
120         if(comedi_get_cmd_generic_timed(device, subdevice, &cmd, 1, 1)<0){
121                 printf("  not supported\n");
122                 return 0;
123         }
124
125         if(realtime)cmd.flags |= TRIG_RT;
126         cmd.chanlist = chanlist;
127         cmd.scan_end_arg = 1;
128         cmd.stop_arg = 100000;
129         cmd.chanlist_len = 1;
130         chanlist[0] = CR_PACK(0,0,0);
131
132         comedi_command(device,&cmd);
133
134         go=1;
135         while(go){
136                 ret = read(comedi_fileno(device),buf,BUFSZ);
137                 if(ret<0){
138                         if(errno==EAGAIN){
139                                 usleep(10000);
140                         }else{
141                                 go = 0;
142                                 perror("read");
143                         }
144                 }else if(ret==0){
145                         go = 0;
146                 }else{
147                         total += ret;
148                         if(verbose)printf("read %d %d\n",ret,total);
149                 }
150         }
151
152         return 0;
153 }
154
155 int test_cmd_write_fast_1chan(void)
156 {
157         comedi_cmd cmd;
158         char buf[BUFSZ];
159         unsigned int chanlist[1];
160         int go;
161         int total=0;
162         int ret;
163         unsigned int flags = comedi_get_subdevice_flags(device,subdevice);
164         static const int num_samples = 100000;
165         int num_bytes;
166         int wc;
167
168         if((flags & SDF_LSAMPL))
169         {
170                 num_bytes = num_samples * sizeof(lsampl_t);
171         }else
172         {
173                 num_bytes = num_samples * sizeof(sampl_t);
174         }
175         if(!(flags&SDF_CMD) || (comedi_get_write_subdevice(device)!=subdevice)){
176                 printf("not applicable\n");
177                 return 0;
178         }
179
180         if(comedi_get_cmd_generic_timed(device, subdevice, &cmd, 1, 1)<0){
181                 printf("  not supported\n");
182                 return 0;
183         }
184         cmd.flags |= CMDF_WRITE;
185
186         if(realtime)cmd.flags |= TRIG_RT;
187         cmd.chanlist = chanlist;
188         cmd.scan_end_arg = 1;
189         cmd.stop_arg = num_samples;
190         cmd.chanlist_len = 1;
191         chanlist[0] = CR_PACK(0,0,0);
192
193         memset(buf,0,BUFSZ);
194
195         ret = comedi_command(device,&cmd);
196         if(ret<0){
197                 perror("comedi_command");
198         }
199
200         go = 1;
201         while(go){
202                 wc = num_bytes-total;
203                 if(wc>BUFSZ){
204                         wc = BUFSZ;
205                 }
206                 ret = write(comedi_fileno(device), buf, wc);
207                 if(ret<0){
208                         perror("write");
209                         return 0;
210                 }
211                 if(ret<wc){
212                         go = 0;
213                 }
214
215                 total += ret;
216                 if(verbose)printf("write %d %d\n",ret,total);
217         }
218
219         ret = comedi_internal_trigger(device, subdevice, 0);
220         if(ret<0){
221                 comedi_perror("E: comedi_internal_trigger");
222                 comedi_cancel(device, subdevice);
223                 return 0;
224         }
225         if(verbose)printf("inttrig\n");
226
227         go=(total<num_bytes);
228         while(go){
229                 wc = num_bytes-total;
230                 if(wc>BUFSZ){
231                         wc = BUFSZ;
232                 }
233                 ret = write(comedi_fileno(device),buf,wc);
234                 if(ret<0){
235                         if(errno==EAGAIN){
236                                 usleep(10000);
237                         }else{
238                                 go = 0;
239                                 perror("write");
240                         }
241                 }else if(ret==0){
242                         go = 0;
243                 }else{
244                         total += ret;
245                         if(verbose)printf("write %d %d\n",ret,total);
246                         //deal with case where output doesn't support stop_src=TRIG_COUNT
247                         if(total >= num_bytes)
248                         {
249                                 go = 0;
250                         }
251                 }
252         }
253         // make sure all samples have been written out
254         while(1)
255         {
256                 int flags = comedi_get_subdevice_flags(device,subdevice);
257                 if(flags < 0)
258                 {
259                         printf("E: comedi_get_subdevice_flags returned %i\n", flags);
260                         break;
261                 }
262                 if((flags & SDF_RUNNING) == 0){
263                         break;
264                 }
265                 usleep(10000);
266         }
267         // cancel needed in the case of stop_src==TRIG_NONE
268         if(comedi_cancel(device, subdevice))
269                 printf("E: comedi_cancel() failed");
270         return 0;
271 }
272
273 int test_cmd_logic_bug(void)
274 {
275         comedi_cmd cmd;
276         int ret;
277         int ok=0;
278
279         if(!(comedi_get_subdevice_flags(device,subdevice)&SDF_CMD)){
280                 printf("not applicable\n");
281                 return 0;
282         }
283
284         printf("rev 1\n");
285
286         ret = comedi_get_cmd_src_mask(device,subdevice,&cmd);
287         if(ret<0){
288                 printf("E: comedi_get_cmd_src_mask failed\n");
289                 return 0;
290         }
291
292         if(count_bits(cmd.start_src)>1){ cmd.start_src=0; ok=1; }
293         if(count_bits(cmd.scan_begin_src)>1){ cmd.scan_begin_src=0; ok=1; }
294         if(count_bits(cmd.convert_src)>1){ cmd.convert_src=0; ok=1; }
295         if(count_bits(cmd.scan_end_src)>1){ cmd.scan_end_src=0; ok=1; }
296         if(count_bits(cmd.stop_src)>1){ cmd.stop_src=0; ok=1; }
297
298         if(ok==0){
299                 printf("not applicable (no source choices)\n");
300                 return 0;
301         }
302
303         ret = comedi_command_test(device,&cmd);
304         if(ret!=1){
305                 printf("E: command_test returned %d, expected 1, (allowed src==0)\n",ret);
306         }else{
307                 printf("command_test returned %d, good\n",ret);
308         }
309
310         return 0;
311 }
312
313 int count_bits(unsigned int bits)
314 {
315         int ret=0;
316         while(bits){
317                 if(bits&1)ret++;
318                 bits>>=1;
319         }
320         return ret;
321 }
322
323 char *tobinary(char *s,int bits,int n)
324 {
325         int bit=1<<n;
326         char *t=s;
327
328         for(;bit;bit>>=1)
329                 *t++=(bits&bit)?'1':'0';
330         *t=0;
331
332         return s;
333 }
334
335 char *cmd_src(int src,char *buf)
336 {
337         buf[0]=0;
338
339         if(src&TRIG_NONE)strcat(buf,"none|");
340         if(src&TRIG_NOW)strcat(buf,"now|");
341         if(src&TRIG_FOLLOW)strcat(buf,"follow|");
342         if(src&TRIG_TIME)strcat(buf,"time|");
343         if(src&TRIG_TIMER)strcat(buf,"timer|");
344         if(src&TRIG_COUNT)strcat(buf,"count|");
345         if(src&TRIG_EXT)strcat(buf,"ext|");
346         if(src&TRIG_INT)strcat(buf,"int|");
347
348         if(strlen(buf)==0){
349                 sprintf(buf,"unknown(0x%02x)",src);
350         }else{
351                 buf[strlen(buf)-1]=0;
352         }
353
354         return buf;
355 }
356
357