summaryrefslogtreecommitdiff
path: root/libvxi11client/client.c
blob: f712f29b86de2ba3286a3d01c67af8f7df2ac9ae (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
#include <stdio.h>
#include <stdlib.h>
#include "libvxi11client.h"

#define IDENTIFY "*IDN?"

static char* geterrorstring(int errorcode) {
	switch (errorcode) {
		case 0:
			return "invalid state (not connected) or no response from server";
		case -4:
			return "invalid link identifier";
		case -6:
			return "channel not established";
		case -8:
			return "operation not supported";
		case -9:
			return "out of resources";
		case -11:
			return "device locked by another link";
		case -12:
			return "no lock held by this link";
		case -29:
			return "channel already established";
		default:
			return "unknown error code";
	}
}

static void interruptcallback(char* handle) {
	printf("Interrupt fired\n");
}

int main(int argc, char *argv[]) {

	printf("VXI-11 test client\n");

	if (argc != 2) {
		printf("usage; %s <host>\n", argv[0]);
		exit(1);
	}

	vxi11_start_interrupt_server(interruptcallback);

	Context ctx;

	int err = 0;

	if ((err = vxi11_open(&ctx, argv[1], NULL)) > 0) {

		/**
		 * Basic tests
		 */

		// write some bytes
		int byteswritten = vxi11_write(&ctx, IDENTIFY, sizeof(IDENTIFY), false, false);
		if (byteswritten >= 0)
			printf("Wrote %d bytes\n", byteswritten);
		else
			printf("Error writing data\n");

		// read some bytes
		char buffer[1024];
		int bytesread = vxi11_read(&ctx, buffer, sizeof(buffer), false, false, 0);
		if (bytesread >= 0)
			printf("Read %d bytes, %s\n", bytesread, buffer);
		else
			printf("Error reading data\n");

		// trigger
		if (vxi11_trigger(&ctx, false) > 0)
			printf("triggered\n");

		// clear
		if (vxi11_clear(&ctx, false) > 0)
			printf("cleared\n");

		// abort
		if ((err = vxi11_abort(&ctx)) > 0)
			printf("aborted\n");
		else
			printf("abort failed; %s\n", geterrorstring(err));

		// lock
		if ((err = vxi11_lock(&ctx, false)) > 0)
			printf("locked\n");

		// unlock
		if ((err = vxi11_unlock(&ctx)) > 0)
			printf("unlocked\n");

		// remote
		if ((err = vxi11_remote(&ctx, false)) > 0)
			printf("remote'd\n");

		// local
		if ((err = vxi11_local(&ctx, false)) > 0)
			printf("local'd\n");

		// read the status byte
		int statusbyte = vxi11_readstatusbyte(&ctx, false);
		if (statusbyte >= 0)
			printf("Status byte is 0x%02x\n", statusbyte);
		else
			printf("Error reading status byte\n");

		/**
		 * Locking tests
		 */

		// try locking twice
		printf("-- Locking twice --\n");
		if ((err = vxi11_lock(&ctx, false)) > 0) {
			printf("locked\n");
			if ((err = vxi11_lock(&ctx, false)) > 0) {
				printf("locked again!!\n");
				exit(1);
			}
			else {
				printf("Second lock failed; %s\n", geterrorstring(err));
			}
		}
		if ((err = vxi11_unlock(&ctx)) > 0)
			printf("unlocked\n");
		else {
			printf("error unlocking; %s\n", geterrorstring(err));
			exit(1);
		}

		printf("\n");

		// try unlocking unlocked device
		printf("-- Unlocking when unlocked --\n");
		if ((err = vxi11_unlock(&ctx)) > 0) {
			printf("Unlocked!!\n");
			exit(1);
		}
		else
			printf("error unlocking; %s\n", geterrorstring(err));
		printf("\n");

		// Interrupt channel tests
		printf("-- Testing interrupt channel --\n");
		// create interrupt channel
		if ((err = vxi11_create_intr_chan(&ctx)) > 0) {
			printf("Created interrupt channel\n");

			// enable interrupts
			if ((err = vxi11_enable_srq(&ctx, true, "handle")) > 0)
				printf("Enabled interrupts\n");
			else
				printf("Error enabling interrupts; %s\n", geterrorstring(err));

			sleep(10);

			// disable interrupts
			if ((err = vxi11_enable_srq(&ctx, false, NULL)) > 0)
				printf("Disabled interrupts\n");
			else
				printf("Error disabling interrupts; %s\n", geterrorstring(err));

			// destroy interrupt channel
			if ((err = vxi11_destroy_intr_chan(&ctx)) > 0)
				printf("Destroyed interrupt channel\n");
			else
				printf("Error destroying interrupt channel; %s\n", geterrorstring(err));
		}
		else
			printf("Error creating interrupt channel; %s\n", geterrorstring(err));
		printf("\n");

		// docmd
		if ((err = vxi11_docmd(&ctx, 0x00, false)) > 0)
			printf("did command, should fail!\n");
		else
			printf("Error calling docmd; %s\n", geterrorstring(err));

		// close
		if ((err = vxi11_close(&ctx) > 0))
			printf("Closed\n");

		vxi11_stop_interrupt_server();
	}
	else {
		printf("Error opening device; %s\n", geterrorstring(err));
		exit(1);
	}
	return 0;
}