File: ibRd.c

package info (click to toggle)
linux-gpib-user 4.3.7-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,760 kB
  • sloc: ansic: 10,381; perl: 1,120; xml: 375; makefile: 335; yacc: 335; tcl: 308; python: 173; php: 157; lex: 144; sh: 134; lisp: 94
file content (353 lines) | stat: -rw-r--r-- 7,980 bytes parent folder | download | duplicates (2)
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
/***************************************************************************
                          lib/ibRd.c
                             -------------------

    copyright            : (C) 2001,2002,2003 by Frank Mori Hess
    email                : fmhess@users.sourceforge.net
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include "ib_internal.h"

// sets up bus to receive data from device with address pad/sad
int InternalReceiveSetup(ibConf_t *conf, unsigned int usec_timeout, Addr4882_t address)
{
	ibBoard_t *board;
	uint8_t cmdString[8];
	unsigned int i = 0;
	unsigned int pad, board_pad;
	int sad, board_sad;

	if (addressIsValid(address) == 0 || address == NOADDR) {
		setIberr(EARG);
		return -1;
	}
	board = interfaceBoard(conf);

	if (query_pad(board, &board_pad) < 0)
		return -1;
	if (query_sad(board, &board_sad) < 0)
		return -1;

	pad = extractPAD(address);
	sad = extractSAD(address);

	cmdString[ i++ ] = UNL;

	cmdString[ i++ ] = MLA(board_pad);	/* controller's listen address */
	if (board_sad >= 0)
		cmdString[ i++ ] = MSA(board_sad);
	cmdString[ i++ ] = MTA(pad);
	if (sad >= 0)
		cmdString[ i++ ] = MSA(sad);

	if (my_ibcmd(conf, usec_timeout, cmdString, i) < 0) {
		fprintf(stderr, "%s: command failed\n", __FUNCTION__);
		return -1;
	}

	return 0;
}

static int read_data(ibConf_t *conf, unsigned int usec_timeout, uint8_t *buffer, size_t count, size_t *bytes_read)
{
	ibBoard_t *board;
	struct gpib_read_write_ioctl read_cmd;
	int retval;

	board = interfaceBoard(conf);

	assert(sizeof(buffer) <= sizeof(read_cmd.buffer_ptr));
	read_cmd.buffer_ptr = (uintptr_t)buffer;
	read_cmd.requested_transfer_count = count;
	read_cmd.completed_transfer_count = 0;
	read_cmd.handle = conf->handle;
	read_cmd.end = 0;

	set_timeout(board, usec_timeout);
	conf->end = 0;

	retval = ioctl(board->fileno, IBRD, &read_cmd);
	if (retval < 0)	{
		switch (errno) {
			case ETIMEDOUT:
				conf->timed_out = 1;
				setIberr(EABO);
				break;
			case EINTR:
				setIberr(EABO);
				break;
			default:
				setIberr(EDVR);
				setIbcnt(errno);
				break;
		}
	}

	if (read_cmd.end)
		conf->end = 1;

	*bytes_read = read_cmd.completed_transfer_count;

	return retval;
}

int my_ibrd(ibConf_t *conf, unsigned int usec_timeout, uint8_t *buffer, size_t count, size_t *bytes_read)
{
	int retval;
	*bytes_read = 0;
	// set eos mode
	iblcleos(conf);

	if (conf->is_interface == 0) {
		// set up addressing
		if (InternalReceiveSetup(conf, usec_timeout, packAddress(conf->settings.pad, conf->settings.sad)) < 0)
			return -1;
	}

	retval =  read_data(conf, usec_timeout, buffer, count, bytes_read);

	if (!conf->is_interface && conf->settings.send_unt_unl)
		retval = unlisten_untalk(conf);

	return retval;
}

int ibrd(int ud, void *rd, long cnt)
{
	ibConf_t *conf;
	ssize_t retval;
	size_t bytes_read;

	conf = enter_library(ud);
	if (conf == NULL)
		return exit_library(ud, 1);

	retval = my_ibrd(conf, conf->settings.usec_timeout, rd, cnt, &bytes_read);
	if (retval < 0) {
		if (ThreadIberr() != EDVR)
			setIbcnt(bytes_read);
		return exit_library(ud, 1);
	} else {
		setIbcnt(bytes_read);
	}

	return general_exit_library(ud, 0, 0, 0, DCAS, 0, 0);
}

int ibrda(int ud, void *buffer, long cnt)
{
	ibConf_t *conf;
	int retval;

	conf = general_enter_library(ud, 1, 0);
	if (conf == NULL)
		return general_exit_library(ud, 1, 0, 0, 0, 0, 1);

	retval = gpib_aio_launch(ud, conf, GPIB_AIO_READ,
		buffer, cnt);
	if (retval < 0)
		return general_exit_library(ud, 1, 0, 0, 0, 0, 1);

	return general_exit_library(ud, 0, 0, 0, 0, 0, 1);
}

int ibrdf(int ud, const char *file_path)
{
	ibConf_t *conf;
	int retval;
	uint8_t buffer[ 0x4000 ];
	unsigned long byte_count;
	FILE *save_file;
	int error;

	conf = enter_library(ud);
	if (conf == NULL)
		return exit_library(ud, 1);

	save_file = fopen(file_path, "a");
	if (save_file == NULL) {
		setIberr(EFSO);
		setIbcnt(errno);
		return exit_library(ud, 1);
	}

	if (conf->is_interface == 0) {
		// set up addressing
		if (InternalReceiveSetup(conf, conf->settings.usec_timeout, packAddress(conf->settings.pad, conf->settings.sad)) < 0)
			return exit_library(ud, 1);
	}

	// set eos mode
	iblcleos(conf);

	byte_count = error = 0;
	do {
		int fwrite_count;
		size_t bytes_read;

		retval = read_data(conf, conf->settings.usec_timeout,  buffer, sizeof(buffer), &bytes_read);
		fwrite_count = fwrite(buffer, 1, bytes_read, save_file);
		if (fwrite_count != bytes_read)		{
			setIberr(EFSO);
			setIbcnt(errno);
			error++;
		}
		byte_count += fwrite_count;
		if (retval < 0)
		{
			error++;
			break;
		}
	} while (conf->end == 0 && error == 0);

	if (!conf->is_interface && conf->settings.send_unt_unl) {
		retval = unlisten_untalk(conf);
		if (retval < 0)
			error++;
	}

	if (!error)
		setIbcnt(byte_count);

	if (fclose(save_file)) {
		setIberr(EFSO);
		setIbcnt(errno);
		return exit_library(ud, 1);
	}
	if (error)
		return exit_library(ud, error);

	return general_exit_library(ud, 0, 0, 0, DCAS, 0, 0);
}

int InternalRcvRespMsg(ibConf_t *conf, void *buffer, long count, int termination)
{
	ibBoard_t *board;
	int retval;
	int use_eos;
	size_t bytes_read;

	if (conf->is_interface == 0) {
		setIberr(EARG);
		return -1;
	}

	board = interfaceBoard(conf);

	retval = is_cic(board);
	if (retval <= 0) {
		if (retval == 0)
			setIberr(ECIC);
		return -1;
	}

	if (termination != (termination & 0xff) && termination != STOPend) {
		setIberr(EARG);
		return -1;
	}
	// XXX check for listener active state

	//XXX detect no listeners (EBUS) error
	use_eos = (termination != STOPend);
	retval = config_read_eos(board, use_eos, termination, 1);
	if (retval < 0)
		return retval;

	retval = read_data(conf, conf->settings.usec_timeout, buffer, count, &bytes_read);
	setIbcnt(bytes_read);
	if (retval < 0)
		return -1;

	return 0;
}

void RcvRespMsg(int boardID, void *buffer, long count, int termination)
{
	ibConf_t *conf;
	int retval;

	conf = enter_library(boardID);
	if (!conf) {
		exit_library(boardID, 1);
		return;
	}

	retval = InternalRcvRespMsg(conf, buffer, count, termination);
	if (retval < 0)	{
		exit_library(boardID, 1);
		return;
	}

	general_exit_library(boardID, 0, 0, 0, DCAS, 0, 0);
}

void ReceiveSetup(int boardID, Addr4882_t address)
{
	ibConf_t *conf;
	int retval;

	conf = enter_library(boardID);
	if (!conf) {
		exit_library(boardID, 1);
		return;
	}

	retval = InternalReceiveSetup(conf, conf->settings.usec_timeout, address);
	if (retval < 0)	{
		exit_library(boardID, 1);
		return;
	}

	exit_library(boardID, 0);
}

int InternalReceive(ibConf_t *conf, Addr4882_t address,
	void *buffer, long count, int termination)
{
	int retval;

	retval = InternalReceiveSetup(conf, conf->settings.usec_timeout, address);
	if (retval < 0)
		return retval;

	retval = InternalRcvRespMsg(conf, buffer, count, termination);
	if (retval < 0)
		return retval;

	return 0;
}

void Receive(int boardID, Addr4882_t address,
	void *buffer, long count, int termination)
{
	ibConf_t *conf;
	int retval;

	conf = enter_library(boardID);
	if (!conf) {
		exit_library(boardID, 1);
		return;
	}

	retval = InternalReceive(conf, address, buffer, count, termination);
	if (retval < 0)	{
		exit_library(boardID, 1);
		return;
	}

	general_exit_library(boardID, 0, 0, 0, DCAS, 0, 0);
}