#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 -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";
	}
}

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

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

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

	int err = 0;

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

		/**
		 * Basic tests
		 */

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

		// read some bytes
		int bytesread = vxi11_read(NULL, 0, false, false, 0);
		if (bytesread >= 0)
			printf("Read %d bytes\n", bytesread);
		else
			printf("Error reading data\n");

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

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

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

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

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

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

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

		// read the status byte
		int statusbyte = vxi11_readstatusbyte(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(false)) > 0) {
			printf("locked\n");
			if ((err = vxi11_lock(false)) > 0) {
				printf("locked again!!\n");
				exit(1);
			}
			else {
				printf("Second lock failed; %s\n", geterrorstring(err));
			}
		}
		if ((err = vxi11_unlock()) > 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()) > 0) {
			printf("Unlocked!!\n");
			exit(1);
		}
		else
			printf("error unlocking; %s\n", geterrorstring(err));
		printf("\n");

		// create interrupt channel
		printf("-- Testing interrupt channel --\n");
		if ((err = vxi11_create_intr_chan()) > 0) {
			printf("Created interrupt channel\n");
			// destroy interrupt channel
			if ((err = vxi11_destroy_intr_chan()) > 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(0x00, false)) > 0)
			printf("did command, should fail!\n");
		else
			printf("Error calling docmd; %s\n", geterrorstring(err));

		// close
		if ((err = vxi11_close() > 0))
			printf("Closed\n");
	}
	else {
		printf("Error opening device; %s\n", geterrorstring(err));
		exit(1);
	}
	return 0;
}