File: cmd_1.c

package info (click to toggle)
comedilib 0.11.0%2B5-1.3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,544 kB
  • sloc: xml: 19,779; ansic: 14,719; sh: 5,679; cpp: 2,211; ruby: 1,658; perl: 700; makefile: 596; yacc: 439; lex: 86; python: 17
file content (363 lines) | stat: -rw-r--r-- 7,483 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363

#include <stdio.h>
#include <comedilib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <getopt.h>
#include <ctype.h>
#include <math.h>
#include <sys/time.h>
#include <string.h>
#include "comedi_test.h"

void probe_max_1chan(comedi_t *it,int s);
char *tobinary(char *s,int bits,int n);
char *cmd_src(int src,char *buf);
int count_bits(unsigned int bits);

int test_cmd_no_cmd(void)
{
	int ret;
	comedi_cmd cmd;

	if(comedi_get_subdevice_flags(device,subdevice)&SDF_CMD){
		printf("not applicable\n");
		return 0;
	}

	ret = comedi_get_cmd_src_mask(device,subdevice,&cmd);
	if(ret<0){
		if(errno == EIO){
			printf("got EIO, good\n");
		}else{
			printf("E: comedi_get_cmd_src_mask: %s\n",
				strerror(errno));
		}
	}else{
		printf("E: comedi_get_cmd_src_mask returned %d\n",ret);
	}

	return 0;
}

int test_cmd_probe_src_mask(void)
{
	comedi_cmd cmd;
	char buf[100];
	int ret;

	if(!(comedi_get_subdevice_flags(device,subdevice)&SDF_CMD)){
		printf("not applicable\n");
		return 0;
	}

	printf("rev 1\n");

	ret = comedi_get_cmd_src_mask(device,subdevice,&cmd);
	if(ret<0){
		printf("E: comedi_get_cmd_src_mask failed %s\n",
			strerror(errno));
		return 0;
	}
	printf("command source mask:\n");
	printf("  start: %s\n",cmd_src(cmd.start_src,buf));
	printf("  scan_begin: %s\n",cmd_src(cmd.scan_begin_src,buf));
	printf("  convert: %s\n",cmd_src(cmd.convert_src,buf));
	printf("  scan_end: %s\n",cmd_src(cmd.scan_end_src,buf));
	printf("  stop: %s\n",cmd_src(cmd.stop_src,buf));

	return 0;
}

int test_cmd_probe_fast_1chan(void)
{
	comedi_cmd cmd;
	char buf[100];

	if(!(comedi_get_subdevice_flags(device,subdevice)&SDF_CMD)){
		printf("not applicable\n");
		return 0;
	}

	printf("command fast 1chan:\n");
	if(comedi_get_cmd_generic_timed(device,subdevice, &cmd, 1, 1)<0){
		printf("  not supported\n");
		return 0;
	}
	printf("  start: %s %d\n",
		cmd_src(cmd.start_src,buf),cmd.start_arg);
	printf("  scan_begin: %s %d\n",
		cmd_src(cmd.scan_begin_src,buf),cmd.scan_begin_arg);
	printf("  convert: %s %d\n",
		cmd_src(cmd.convert_src,buf),cmd.convert_arg);
	printf("  scan_end: %s %d\n",
		cmd_src(cmd.scan_end_src,buf),cmd.scan_end_arg);
	printf("  stop: %s %d\n",
		cmd_src(cmd.stop_src,buf),cmd.stop_arg);

	return 0;
}

#define BUFSZ 2000

int test_cmd_read_fast_1chan(void)
{
	comedi_cmd cmd;
	char buf[BUFSZ];
	unsigned int chanlist[1];
	int go;
	int total=0;
	int ret;
	unsigned int flags = comedi_get_subdevice_flags(device,subdevice);

	/* attempt to make subdevice the current 'read' subdevice */
	if(flags&SDF_CMD_READ) comedi_set_read_subdevice(device,subdevice);
	if(!(flags&SDF_CMD) || (comedi_get_read_subdevice(device)!=subdevice)){
		printf("not applicable\n");
		return 0;
	}

	if(comedi_get_cmd_generic_timed(device, subdevice, &cmd, 1, 1)<0){
		printf("  not supported\n");
		return 0;
	}

	if(realtime)cmd.flags |= TRIG_RT;
	cmd.chanlist = chanlist;
	cmd.scan_end_arg = 1;
	cmd.stop_arg = 100000;
	cmd.chanlist_len = 1;
	chanlist[0] = CR_PACK(0,0,0);

	comedi_command(device,&cmd);

	go=1;
	while(go){
		ret = read(comedi_fileno(device),buf,BUFSZ);
		if(ret<0){
			if(errno==EAGAIN){
				usleep(10000);
			}else{
				go = 0;
				perror("read");
			}
		}else if(ret==0){
			go = 0;
		}else{
			total += ret;
			if(verbose)printf("read %d %d\n",ret,total);
		}
	}

	return 0;
}

int test_cmd_write_fast_1chan(void)
{
	comedi_cmd cmd;
	char buf[BUFSZ];
	unsigned int chanlist[1];
	int go;
	int total=0;
	int ret;
	unsigned int flags = comedi_get_subdevice_flags(device,subdevice);
	static const int num_samples = 100000;
	int num_bytes;
	int wc;

	if((flags & SDF_LSAMPL))
	{
		num_bytes = num_samples * sizeof(lsampl_t);
	}else
	{
		num_bytes = num_samples * sizeof(sampl_t);
	}
	/* attempt to make subdevice the current 'write' subdevice */
	if(flags&SDF_CMD_WRITE) comedi_set_write_subdevice(device,subdevice);
	if(!(flags&SDF_CMD) || (comedi_get_write_subdevice(device)!=subdevice)){
		printf("not applicable\n");
		return 0;
	}

	if(comedi_get_cmd_generic_timed(device, subdevice, &cmd, 1, 1)<0){
		printf("  not supported\n");
		return 0;
	}
	cmd.flags |= CMDF_WRITE;

	if(realtime)cmd.flags |= TRIG_RT;
	cmd.chanlist = chanlist;
	cmd.scan_end_arg = 1;
	cmd.stop_arg = num_samples;
	cmd.chanlist_len = 1;
	chanlist[0] = CR_PACK(0,0,0);

	memset(buf,0,BUFSZ);

	ret = comedi_command(device,&cmd);
	if(ret<0){
		perror("comedi_command");
	}

	go = 1;
	while(go){
		wc = num_bytes-total;
		if(wc>BUFSZ){
			wc = BUFSZ;
		}
		ret = write(comedi_fileno(device), buf, wc);
		if(ret<0){
			perror("write");
			return 0;
		}
		if(ret<wc){
			go = 0;
		}
		total += ret;
		if(total >= num_bytes){
			go = 0;
		}
		if(verbose)printf("write %d %d\n",ret,total);
	}

	ret = comedi_internal_trigger(device, subdevice, 0);
	if(ret<0){
		comedi_perror("E: comedi_internal_trigger");
		comedi_cancel(device, subdevice);
		return 0;
	}
	if(verbose)printf("inttrig\n");

	go=(total<num_bytes);
	while(go){
		wc = num_bytes-total;
		if(wc>BUFSZ){
			wc = BUFSZ;
		}
		ret = write(comedi_fileno(device),buf,wc);
		if(ret<0){
			if(errno==EAGAIN){
				usleep(10000);
			}else{
				go = 0;
				perror("write");
			}
		}else if(ret==0){
			go = 0;
		}else{
			total += ret;
			if(verbose)printf("write %d %d\n",ret,total);
			//deal with case where output doesn't support stop_src=TRIG_COUNT
			if(total >= num_bytes)
			{
				go = 0;
			}
		}
	}
	// make sure all samples have been written out
	while(1)
	{
		int flags = comedi_get_subdevice_flags(device,subdevice);
		if(flags < 0)
		{
			printf("E: comedi_get_subdevice_flags returned %i\n", flags);
			break;
		}
		if((flags & SDF_RUNNING) == 0){
			break;
		}
		usleep(10000);
	}
	// cancel needed in the case of stop_src==TRIG_NONE
	if(comedi_cancel(device, subdevice))
		printf("E: comedi_cancel() failed");
	return 0;
}

int test_cmd_logic_bug(void)
{
	comedi_cmd cmd;
	int ret;
	int ok=0;

	if(!(comedi_get_subdevice_flags(device,subdevice)&SDF_CMD)){
		printf("not applicable\n");
		return 0;
	}

	printf("rev 1\n");

	ret = comedi_get_cmd_src_mask(device,subdevice,&cmd);
	if(ret<0){
		printf("E: comedi_get_cmd_src_mask failed\n");
		return 0;
	}

	if(count_bits(cmd.start_src)>1){ cmd.start_src=0; ok=1; }
	if(count_bits(cmd.scan_begin_src)>1){ cmd.scan_begin_src=0; ok=1; }
	if(count_bits(cmd.convert_src)>1){ cmd.convert_src=0; ok=1; }
	if(count_bits(cmd.scan_end_src)>1){ cmd.scan_end_src=0; ok=1; }
	if(count_bits(cmd.stop_src)>1){ cmd.stop_src=0; ok=1; }

	if(ok==0){
		printf("not applicable (no source choices)\n");
		return 0;
	}

	ret = comedi_command_test(device,&cmd);
	if(ret!=1){
		printf("E: command_test returned %d, expected 1, (allowed src==0)\n",ret);
	}else{
		printf("command_test returned %d, good\n",ret);
	}

	return 0;
}

int count_bits(unsigned int bits)
{
	int ret=0;
	while(bits){
		if(bits&1)ret++;
		bits>>=1;
	}
	return ret;
}

char *tobinary(char *s,int bits,int n)
{
	int bit=1<<n;
	char *t=s;

	for(;bit;bit>>=1)
		*t++=(bits&bit)?'1':'0';
	*t=0;

	return s;
}

char *cmd_src(int src,char *buf)
{
	buf[0]=0;

	if(src&TRIG_NONE)strcat(buf,"none|");
	if(src&TRIG_NOW)strcat(buf,"now|");
	if(src&TRIG_FOLLOW)strcat(buf,"follow|");
	if(src&TRIG_TIME)strcat(buf,"time|");
	if(src&TRIG_TIMER)strcat(buf,"timer|");
	if(src&TRIG_COUNT)strcat(buf,"count|");
	if(src&TRIG_EXT)strcat(buf,"ext|");
	if(src&TRIG_INT)strcat(buf,"int|");

	if(strlen(buf)==0){
		sprintf(buf,"unknown(0x%02x)",src);
	}else{
		buf[strlen(buf)-1]=0;
	}

	return buf;
}