testing additions
[comedilib.git] / testing / cmd_3.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 static int comedi_get_cmd_src_mask(comedi_t *it,unsigned int s,comedi_cmd *cmd);
16 static int comedi_get_cmd_fast_1chan(comedi_t *it,unsigned int s,comedi_cmd *cmd);
17 static int do_continuous(int multiplier);
18
19 #define BUFSZ 10000
20
21 int test_cmd_continuous(void)
22 {
23         int mult;
24
25         /* as if doing _one_ infinite loop wasn't slow enough,
26          * we loop through with higher and higher multipliers,
27          * in case the test fails because of latency problems */
28
29         for(mult=1;mult<1024;mult*=2){
30                 do_continuous(mult);
31         }
32
33         return 0;
34 }
35
36 static int do_continuous(int multiplier)
37 {
38         comedi_cmd cmd;
39         char buf[BUFSZ];
40         unsigned int chanlist[1];
41         int go;
42         int total=0;
43         int ret;
44         int chunks=0;
45         unsigned long total_secs = 0;
46         struct timeval tv,start_tv;
47
48         if(comedi_get_cmd_fast_1chan(device,subdevice,&cmd)<0){
49                 printf("  not supported\n");
50                 return 0;
51         }
52
53         cmd.chanlist = chanlist;
54         cmd.scan_end_arg = 1;
55         cmd.stop_src = TRIG_NONE;
56         cmd.stop_arg = 0;
57         cmd.chanlist_len = 1;
58         chanlist[0] = CR_PACK(0,0,0);
59
60         // slow down a bit
61         cmd.scan_begin_arg *= multiplier;
62         printf("multiplier=%d, scan_begin_arg=%d\n",
63                 multiplier,
64                 cmd.scan_begin_arg);
65
66         ret=comedi_command(device,&cmd);
67         if(ret<0){
68                 perror("comedi_command");
69         }else{
70                 printf("ret==%d\n",ret);
71         }
72
73         gettimeofday(&start_tv,NULL);
74
75         go=1;
76         while(go){
77                 ret = read(comedi_fileno(device),buf,BUFSZ);
78                 if(ret<0){
79                         if(errno==EAGAIN){
80                                 usleep(10000);
81                         }else{
82                                 go = 0;
83                                 perror("read");
84                         }
85                 }else if(ret==0){
86                         go = 0;
87                 }else{
88                         total += ret;
89                         chunks++;
90
91                         gettimeofday(&tv,NULL);
92                         tv.tv_sec-=start_tv.tv_sec;
93                         tv.tv_usec-=start_tv.tv_usec;
94                         if(tv.tv_usec<0){
95                                 tv.tv_usec+=1000000;
96                                 tv.tv_sec--;
97                         }
98                         if(tv.tv_sec>total_secs){
99                                 double t;
100
101                                 t=tv.tv_sec+1e-6*tv.tv_usec;
102                                 printf("%0.3f %d (%g) %d (%g)\n",
103                                         t,
104                                         chunks,chunks/t,
105                                         total,total/t);
106                                 total_secs++;
107                         }
108                 }
109         }
110
111         return 0;
112 }
113
114 static int comedi_get_cmd_src_mask(comedi_t *it,unsigned int s,comedi_cmd *cmd)
115 {
116         memset(cmd,0,sizeof(*cmd));
117
118         cmd->subdev = s;
119
120         cmd->flags = 0;
121
122         cmd->start_src = TRIG_ANY;
123         cmd->scan_begin_src = TRIG_ANY;
124         cmd->convert_src = TRIG_ANY;
125         cmd->scan_end_src = TRIG_ANY;
126         cmd->stop_src = TRIG_ANY;
127
128         return comedi_command_test(it,cmd);
129 }
130
131
132 static int comedi_get_cmd_fast_1chan(comedi_t *it,unsigned int s,comedi_cmd *cmd)
133 {
134         int ret;
135
136         ret = comedi_get_cmd_src_mask(it,s,cmd);
137         if(ret<0)return ret;
138
139         cmd->chanlist_len = 1;
140
141         cmd->scan_end_src = TRIG_COUNT;
142         cmd->scan_end_arg = 1;
143
144         if(cmd->convert_src&TRIG_TIMER){
145                 if(cmd->scan_begin_src&TRIG_FOLLOW){
146                         cmd->convert_src = TRIG_TIMER;
147                         cmd->scan_begin_src = TRIG_FOLLOW;
148                 }else{
149                         cmd->convert_src = TRIG_TIMER;
150                         cmd->scan_begin_src = TRIG_TIMER;
151                 }
152         }else{
153                 printf("can't do timed?!?\n");
154                 return -1;
155         }
156         if(cmd->stop_src&TRIG_COUNT){
157                 cmd->stop_src=TRIG_COUNT;
158                 cmd->stop_arg=2;
159         }else if(cmd->stop_src&TRIG_NONE){
160                 cmd->stop_src=TRIG_NONE;
161                 cmd->stop_arg=0;
162         }else{
163                 printf("can't find a good stop_src\n");
164                 return -1;
165         }
166
167         ret=comedi_command_test(it,cmd);
168         if(ret==3){
169                 /* good */
170                 ret=comedi_command_test(it,cmd);
171         }
172         if(ret==4 || ret==0){
173                 return 0;
174         }
175         return -1;
176 }
177