File: ibWait.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 (200 lines) | stat: -rw-r--r-- 5,399 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
/***************************************************************************
                          lib/ibWait.c
                             -------------------

    copyright            : (C) 2001,2002 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 "ib_internal.h"
#include <pthread.h>

static const int device_wait_mask = TIMO | END | CMPL | RQS;
static const int board_wait_mask =  TIMO | END | CMPL | SPOLL |
	EVENT | LOK | REM | CIC | ATN | TACS | LACS | DTAS | DCAS | SRQI;

void fixup_status_bits(const ibConf_t *conf, int *status)
{
	if (!conf->is_interface) {
		*status &= device_wait_mask;
	} else {
		*status &= board_wait_mask;
		if (interfaceBoard(conf)->use_event_queue) {
			*status &= ~DTAS & ~DCAS;
		} else {
			*status &= ~EVENT;
		}
	}
}

int my_wait(ibConf_t *conf, int wait_mask, int clear_mask, int set_mask, int *status)
{
	ibBoard_t *board;
	int retval;
	struct gpib_wait_ioctl cmd;

	board = interfaceBoard(conf);


	if ((!conf->is_interface) && (retval = is_cic(board)) != 1) {
		if (retval == 0)
			setIberr(ECIC);
		return -1;
	}

	cmd.handle = conf->handle;
	cmd.usec_timeout = conf->settings.usec_timeout;
	cmd.wait_mask = wait_mask;
	cmd.clear_mask = clear_mask;
	cmd.set_mask = set_mask;
	cmd.ibsta = 0;
	fixup_status_bits(conf, &cmd.wait_mask);
	if (!conf->is_interface) {
		cmd.pad = conf->settings.pad;
		cmd.sad = conf->settings.sad;
	} else {
		cmd.pad = NOADDR;
		cmd.sad = NOADDR;
//XXX additionally, clear wait mask depending on event queue enabled, etc */
	}

	if (wait_mask != cmd.wait_mask)	{
		setIberr(EARG);
		return -1;
	}

	retval = ioctl(board->fileno, IBWAIT, &cmd);
	if (retval < 0)	{
		setIberr(EDVR);
		setIbcnt(errno);
		return -1;
	}
	fixup_status_bits(conf, &cmd.ibsta);
	if (conf->end) //XXX
		cmd.ibsta |= END;
	setIbsta(cmd.ibsta);
	*status = cmd.ibsta;
	return 0;
}

int ibwait(int ud, int mask)
{
	ibConf_t *conf;
	int retval;
	int status;
	int clear_mask;
	int error = 0;

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

	/** check for invalid mask bits */
	if (!conf->is_interface) {
		if ((mask & device_wait_mask) != mask) {
			fprintf(stderr, "Invalid wait mask for device descriptor, valid wait bits are 0x%x\n",
				device_wait_mask);
			setIberr(EARG);
			return general_exit_library(ud, 1, 0, 0, 0, 0, 1);
		}
	} else {
		if ((mask & board_wait_mask) != mask) {
			fprintf(stderr, "Invalid wait mask for board descriptor, valid wait bits are 0x%x\n",
				board_wait_mask);
			setIberr(EARG);
			return general_exit_library(ud, 1, 0, 0, 0, 0, 1);
		}
	}

	clear_mask = mask & (DTAS | DCAS | SPOLL);
	retval = my_wait(conf, mask, clear_mask, 0, &status);
	if (retval < 0)
		return general_exit_library(ud, 1, 0, 0, 0, 0, 1);

	/* We will only try to resync with async io if
	 the user explicitly asked to wait on CMPL.  That way,
	 they won't be confused by possible errors associated with
	 the async io on random ibwait calls.  If CMPL
	 was specified in the wait mask, then any running
	 async io will be joined and the library status
	 variables will be updated with the results from
	 the most recent async io operation completed on this
	 descriptor.

	 If errors occur both on this ibwait itself and on
	 the most recent async io, then reporting errors from
	 the ibwait takes precedence.
	 */
	if (mask & CMPL) {
		if (status & CMPL) {
			if (conf->async.in_progress) {
				if (gpib_aio_join(&conf->async)) {
					error++;
					general_exit_library(ud, error, 0, 1, 0, 0, 1);
				} else {
					conf->async.in_progress = 0;
				}
			}

			pthread_mutex_lock(&conf->async.lock);
			if (conf->async.ibsta & CMPL) {
				setIbcnt(conf->async.ibcntl);
				setIberr(conf->async.iberr);
				if (conf->async.ibsta & ERR)
					error++;
			}
			pthread_mutex_unlock(&conf->async.lock);
		}
		if (error) {
			status |= ERR;
			setIbsta(status);
		}
	}
	general_exit_library(ud, error, 0, 1, 0, 0, 1);

	return status;
}

void WaitSRQ(int boardID, short *result)
{
	ibConf_t *conf;
	int retval;
	int wait_mask;
	int status;

	conf = general_enter_library(boardID, 1, 0);
	if (!conf) {
		general_exit_library(boardID, 1, 0, 0, 0, 0, 1);
		return;
	}

	if (!conf->is_interface) {
		setIberr(EDVR);
		general_exit_library(boardID, 1, 0, 0, 0, 0, 1);
		return;
	}

	wait_mask = SRQI | TIMO;
	retval = my_wait(conf, wait_mask, 0, 0, &status);
	if (retval < 0)	{
		general_exit_library(boardID, 1, 0, 0, 0, 0, 1);
		return;
	}
	// XXX need better query of service request state, new ioctl?
	// should play nice with autopolling
	if (ThreadIbsta() & SRQI)
		*result = 1;
	else
		*result = 0;

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