File: ibdev.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 (174 lines) | stat: -rw-r--r-- 4,360 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
/***************************************************************************
                                 ibdev.c
                             -------------------
    begin                : Tues Feb 12 2002
    copyright            : (C) 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 <stdlib.h>

#if 0
static int is_device_addr(int minor, int pad, int sad)
{
	ibBoard_t *board;
	unsigned int board_pad;
	int board_sad;

	board = &ibBoard[ minor ];

	if (query_pad(board, &board_pad) < 0) {
		fprintf(stderr, "failed to query pad\n");
		return -1;
	}
	if (query_sad(board, &board_sad) < 0) {
		fprintf(stderr, "failed to query sad\n");
		return -1;
	}

	if (gpib_address_equal(board_pad, board_sad, pad, sad) == 0) {
		return 1;
	}

	return 0;
}
#endif

int ibdev(int minor, int pad, int sad, int timo, int eot, int eosmode)
{
	int retval,ud;
	ibConf_t new_conf;
	int no_show_error = getenv("IB_NO_ERROR") ? 1 : 0;
	ibBoard_t *board;
	unsigned  board_pad;
	int my_sad, board_sad;

	if (minor < 0 || minor >=  GPIB_MAX_NUM_BOARDS) {
		if (!no_show_error)
			fprintf(stderr,"ibdev: invalid minor%d\n", minor);
		setIberr(EARG);
		sync_globals();
		return -1;
	}

	retval = ibParseConfigFile(minor);
	if (retval < 0) {
		if (errno) {
			setIberr(EDVR);
			setIbcnt(errno);
		} else {
			setIberr(ECNF);
		}
		setIbsta(ERR);
		sync_globals();
		return -1;
	}

	/*  Check for valid address arguments */

	if (pad < 0 || pad > gpib_addr_max) {
		if (!no_show_error)
			fprintf(stderr,"ibdev: invalid pad %d, expected 0 <= pad <= 30\n", pad);
		setIberr(EARG);
		sync_globals();
		return -1;
	}

	if (!sad) /* 0 means no address */
		my_sad = -1;
	else
		my_sad = sad - sad_offset;

	if (my_sad < -1 || my_sad > gpib_sad_max) {
		if (!no_show_error)
			fprintf(stderr,"ibdev: invalid sad 0x%02x, expected 0, or 0x60 <= sad <= 0x7f\n", sad);
		setIberr(EARG);
		sync_globals();
		return -1;
	}

	init_ibconf(&new_conf);
	new_conf.settings.pad = pad;
	new_conf.settings.sad = my_sad;                     /* device address                   */
	new_conf.settings.board = minor;                    /* board number                     */
	new_conf.settings.eos = eosmode & 0xff;             /* local eos modes                  */
	new_conf.settings.eos_flags = eosmode & 0xff00;
	new_conf.settings.usec_timeout = timeout_to_usec(timo);
	if (eot)
		new_conf.settings.send_eoi = 1;
	else
		new_conf.settings.send_eoi = 0;
	new_conf.defaults = new_conf.settings;
	new_conf.is_interface = 0;
	new_conf.error_msg_disable = no_show_error;

	ud = my_ibdev(new_conf);

	if (ud < 0)
		return -1;

// check for address conflicts with board addresses

	board = interfaceBoard(&new_conf);

	if (query_pad(board, &board_pad) < 0) {
		goto ibdev_err;
	}
	if (query_sad(board, &board_sad) < 0) {
		goto ibdev_err;
	}

	if (board_pad == pad) {
		fprintf(stderr,"ibdev: address conflict with board pad\n");
		setIberr(EARG);
		goto ibdev_err;
	}

	return ud;
ibdev_err:
	retval = close_gpib_handle(&new_conf);
	if (retval < 0) {
		if (!no_show_error)
			fprintf(stderr, "ibdev: cleanup failed\n");
		sync_globals();
		return -1;
	}
	release_descriptor(ud);
	sync_globals();
	return -1;
}

int my_ibdev(ibConf_t new_conf)
{
	int ud;
	ibConf_t *conf;

	ud = insert_descriptor(new_conf, -1);
	if (ud < 0) {
		if (!new_conf.error_msg_disable)
			fprintf(stderr, "libgpib: ibdev failed to get descriptor\n");
		setIbsta(ERR);
		return -1;
	}

	conf = enter_library(ud);
	if (!conf) {
		exit_library(ud, 1);
		release_descriptor(ud);
		return -1;
	}
	// XXX do local lockout if appropriate

	exit_library(ud, 0);
	return ud;
}