summaryrefslogtreecommitdiff
path: root/libvxi11client/libvxi11client.c
blob: 6234c0f6a2a283a1307e903093b224bc9e3f3fff (plain)
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
#include <stdbool.h>
#include "vxi11.h"

/**
 * This is a thin wrapper around the rpcgen generated code to give it a simpler interface.
 * Only one server with a single link is supported.
 */

#define FLAG_TERMCHRSET (1 << 7)
#define FLAG_END (1 << 3)
#define FLAG_WAITLOCK 1

#define	VXI11_DEFAULT_TIMEOUT	1000

static CLIENT* clnt = NULL;
static Device_Link link;

static Device_Flags vxi11_generateflags(bool waitlock, bool end, bool termchrset) {
	Device_Flags flags = 0;
	if (waitlock)
		flags |= FLAG_WAITLOCK;
	if (end)
		flags |= FLAG_END;
	if (termchrset)
		flags |= FLAG_TERMCHRSET;
	return flags;
}

/**
 * create an RPC client and open a link to the server at $address.
 * $device is apparently used for VXI-11 -> GPIB gateways.. this is untested.
 */

int vxi11_open(char* address, char* device) {
	clnt = clnt_create(address, DEVICE_CORE, DEVICE_CORE_VERSION, "tcp");
	if (clnt == NULL)
		return 0;

	else {
		Create_LinkParms link_parms;
		link_parms.clientId = (long) clnt;
		link_parms.lockDevice = 0;
		link_parms.lock_timeout = VXI11_DEFAULT_TIMEOUT;
		link_parms.device = device != NULL ? device : "device0";

		Create_LinkResp* linkresp = create_link_1(&link_parms, clnt);
		if (linkresp != NULL && linkresp->error == 0) {
			link = linkresp->lid;
			return 1;
		}
		else if (linkresp == NULL)
			return 0;
		else
			return -(linkresp->error);
	}
}

/**
 * read the status byte of the connected server
 */

int vxi11_readstatusbyte(bool waitforlock) {
	if (clnt == NULL)
		return 0;

	Device_GenericParms params = { .lid = link, .flags = vxi11_generateflags(waitforlock, false, false), .lock_timeout =
			VXI11_DEFAULT_TIMEOUT, .io_timeout = VXI11_DEFAULT_TIMEOUT };
	Device_ReadStbResp* resp = device_readstb_1(&params, clnt);

	if (resp != NULL && resp->error == 0)
		return resp->stb;
	else if (resp == NULL)
		return 0;
	else
		return -1;
}

/**
 * write to the connected device
 */

int vxi11_write(char* data, unsigned int len, bool waitlock, bool end) {
	if (clnt == NULL)
		return 0;

	Device_WriteParms params = { .lid = link, .io_timeout = VXI11_DEFAULT_TIMEOUT,
			.lock_timeout = VXI11_DEFAULT_TIMEOUT, .flags = vxi11_generateflags(waitlock, end, false) };
	params.data.data_len = len;
	params.data.data_val = data;

	Device_WriteResp* resp = device_write_1(&params, clnt);
	if (resp != NULL && resp->error == 0)
		return resp->size;
	else if (resp == NULL)
		return 0;
	else
		return -1;
}

/**
 * read from the connected device
 */

int vxi11_read(char* buffer, unsigned int bufferlen, bool waitlock, bool termchrset, char termchr) {
	if (clnt == NULL)
		return 0;

	Device_ReadParms params = { .lid = link, .requestSize = 256, .io_timeout = VXI11_DEFAULT_TIMEOUT, .lock_timeout =
			VXI11_DEFAULT_TIMEOUT, .flags = vxi11_generateflags(waitlock, false, termchrset), .termChar =
			termchrset ? termchr : 0 };

	Device_ReadResp* resp = device_read_1(&params, clnt);
	if (resp != NULL && resp->error == 0)
		return resp->data.data_len;
	else if (resp == NULL)
		return 0;
	else
		return -1;
}

/**
 *
 */

int vxi11_docmd(unsigned long cmd, bool waitforlock) {
	if (clnt == NULL)
		return 0;

	Device_DocmdParms params =
			{ .lid = link, .flags = vxi11_generateflags(waitforlock, false, false), .io_timeout = VXI11_DEFAULT_TIMEOUT,
					.lock_timeout = VXI11_DEFAULT_TIMEOUT, .cmd = cmd, .network_order = 0, .datasize = 0 };

	params.data_in.data_in_len = 0;
	params.data_in.data_in_val = NULL;

	Device_DocmdResp* resp = device_docmd_1(&params, clnt);
	if (resp != NULL && resp->error == 0)
		return 1;
	else if (resp == NULL)
		return 0;
	else
		return -(resp->error);
}

/**
 * trigger the connected device
 */

int vxi11_trigger(bool waitforlock) {
	if (clnt == NULL)
		return 0;

	Device_GenericParms params = { .lid = link, .flags = vxi11_generateflags(waitforlock, false, false), .lock_timeout =
			VXI11_DEFAULT_TIMEOUT, .io_timeout = VXI11_DEFAULT_TIMEOUT };
	Device_Error* error = device_trigger_1(&params, clnt);

	if (error->error == 0)
		return 1;
	else
		return -(error->error);
}

/**
 * clear the connected device
 */

int vxi11_clear(bool waitforlock) {
	if (clnt == NULL)
		return 0;
	Device_GenericParms params = { .lid = link, .flags = vxi11_generateflags(waitforlock, false, false), .lock_timeout =
			VXI11_DEFAULT_TIMEOUT, .io_timeout = VXI11_DEFAULT_TIMEOUT };
	Device_Error* error = device_clear_1(&params, clnt);
	if (error != NULL && error->error == 0)
		return 1;
	else if (error == NULL)
		return 0;
	else
		return -(error->error);
}

/**
 * remote the connected device
 */

int vxi11_remote(bool waitforlock) {
	if (clnt == NULL)
		return 0;
	Device_GenericParms params = { .lid = link, .flags = vxi11_generateflags(waitforlock, false, false), .lock_timeout =
			VXI11_DEFAULT_TIMEOUT, .io_timeout = VXI11_DEFAULT_TIMEOUT };
	Device_Error* error = device_remote_1(&params, clnt);
	if (error != NULL && error->error == 0)
		return 1;
	else if (error == NULL)
		return 0;
	else
		return -(error->error);
}

/**
 * local the connected device
 */

int vxi11_local(bool waitforlock) {
	if (clnt == NULL)
		return 0;
	Device_GenericParms params = { .lid = link, .flags = vxi11_generateflags(waitforlock, false, false), .lock_timeout =
			VXI11_DEFAULT_TIMEOUT, .io_timeout = VXI11_DEFAULT_TIMEOUT };
	Device_Error* error = device_local_1(&params, clnt);
	if (error != NULL && error->error == 0)
		return 1;
	else if (error == NULL)
		return 0;
	else
		return -(error->error);
}

/**
 * lock the connected device
 */

int vxi11_lock(bool waitforlock) {
	if (clnt == NULL)
		return 0;
	Device_LockParms params = { .lid = link, .flags = vxi11_generateflags(waitforlock, false, false), .lock_timeout =
			VXI11_DEFAULT_TIMEOUT };
	Device_Error* error = device_lock_1(&params, clnt);
	if (error != NULL && error->error == 0)
		return 1;
	else if (error == NULL)
		return 0;
	else
		return -(error->error);
}

/**
 * unlock the connected device
 */

int vxi11_unlock() {
	if (clnt == NULL)
		return 0;
	Device_Error* error = device_unlock_1(&link, clnt);
	if (error != NULL && error->error == 0)
		return 1;
	else if (error == NULL)
		return 0;
	else
		return -(error->error);
}

/**
 * create an interrupt channel from the connected device
 */
int vxi11_create_intr_chan() {
	if (clnt == NULL)
		return 0;
	Device_RemoteFunc remotefunc = { .hostAddr = 0x0, .hostPort = 0x0, .progNum = 0x0, .progVers = 0x0, .progFamily =
			DEVICE_TCP };
	Device_Error* error = create_intr_chan_1(&remotefunc, clnt);
	if (error != NULL && error->error == 0)
		return 1;
	else if (error == NULL)
		return 0;
	else
		return -(error->error);
}

/**
 * destroy an interrupt channel from the connected device
 */

int vxi11_destroy_intr_chan() {
	if (clnt == NULL)
		return 0;
	Device_Error* error = destroy_intr_chan_1(NULL, clnt);
	if (error != NULL && error->error == 0)
		return 1;
	else if (error == NULL)
		return 0;
	else
		return -(error->error);
}

/**
 * send an abort to the connected device
 */

int vxi11_abort() {
	if (clnt == NULL)
		return 0;
	Device_Error* error = device_abort_1(&link, clnt);
	if (error != NULL && error->error == 0)
		return 1;
	else if (error == NULL)
		return 0;
	else
		return -(error->error);
}

/**
 * close the current link and free the RPC client
 */

int vxi11_close() {
	if (clnt == NULL)
		return 0;

	Device_Error* error = destroy_link_1(&link, clnt);
	clnt_destroy(clnt);
	clnt = NULL;

	if (error != NULL && error->error == 0)
		return 1;
	else if (error == NULL)
		return 0;
	else
		return -(error->error);
}