From 4d177908c1c68bedb01fb0c9146b06b249fd1b5e Mon Sep 17 00:00:00 2001
From: daniel <danieruru@gmail.com>
Date: Wed, 9 Jan 2013 14:25:02 +0900
Subject: More clean ups, add some more tests

---
 libvxi11client/client.c         | 45 +++++++++++++++++++++++++++++++++++++++++
 libvxi11client/libvxi11client.c |  2 +-
 vxi11_server.c                  | 41 ++++++++++++++++++++-----------------
 3 files changed, 68 insertions(+), 20 deletions(-)

diff --git a/libvxi11client/client.c b/libvxi11client/client.c
index eb224f2..799df57 100644
--- a/libvxi11client/client.c
+++ b/libvxi11client/client.c
@@ -7,8 +7,14 @@ 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 -8:
 			return "operation not supported";
+		case -11:
+			return "device locked by another link";
+		case -12:
+			return "no lock held by this link";
 		default:
 			return "unknown error code";
 	}
@@ -27,6 +33,10 @@ int main(int argc, char *argv[]) {
 
 	if ((err = vxi11_open(argv[1], NULL)) > 0) {
 
+		/**
+		 * Basic tests
+		 */
+
 		// write some bytes
 		int byteswritten = vxi11_write(IDENTIFY, sizeof(IDENTIFY), false);
 		if (byteswritten >= 0)
@@ -78,6 +88,41 @@ int main(int argc, char *argv[]) {
 		else
 			printf("Error reading status byte\n");
 
+		/**
+		 * Locking tests
+		 */
+
+		// try locking twice
+		printf("-- Locking twice --\n");
+		if ((err = vxi11_lock()) > 0) {
+			printf("locked\n");
+			if ((err = vxi11_lock()) > 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
 		err = vxi11_create_intr_chan();
 
diff --git a/libvxi11client/libvxi11client.c b/libvxi11client/libvxi11client.c
index ba7d7b4..4e3efd0 100644
--- a/libvxi11client/libvxi11client.c
+++ b/libvxi11client/libvxi11client.c
@@ -41,7 +41,7 @@ int vxi11_open(char* address, char* device) {
 		link_parms.clientId = (long) clnt;
 		link_parms.lockDevice = 0;
 		link_parms.lock_timeout = VXI11_DEFAULT_TIMEOUT;
-		link_parms.device = device;
+		link_parms.device = device != NULL ? device : "device0";
 
 		Create_LinkResp* linkresp = create_link_1(&link_parms, clnt);
 		if (linkresp != NULL && linkresp->error == 0) {
diff --git a/vxi11_server.c b/vxi11_server.c
index 3272b32..950a31d 100644
--- a/vxi11_server.c
+++ b/vxi11_server.c
@@ -36,21 +36,12 @@ static void waitForLock(long timeout) {
 
 }
 
-static bool lock(int lid) {
-	if ((globals.VxiLocks.locked_network_server = NO_SERVER_LOCKED))
-		return false;
+static void lock(int lid) {
 	globals.VxiLocks.locked_network_server = lid;
 }
 
-static bool unlock(int lid) {
-	if ((globals.VxiLocks.locked_network_server = NO_SERVER_LOCKED))
-		return false;
-	else if (globals.VxiLocks.locked_network_server != lid)
-		return false;
-	else {
-		globals.VxiLocks.locked_network_server = NO_SERVER_LOCKED;
-		return true;
-	}
+static void unlock() {
+	globals.VxiLocks.locked_network_server = NO_SERVER_LOCKED;
 }
 
 Device_Error *
@@ -168,11 +159,20 @@ device_local_1_svc(Device_GenericParms *argp, struct svc_req *rqstp) {
 
 Device_Error *
 device_lock_1_svc(Device_LockParms *argp, struct svc_req *rqstp) {
-	printf("device_lock_1_svc()\n");
-
 	static Device_Error result;
 
-	result.error = 0;
+	printf("device_lock_1_svc()\n");
+	if (isLocked(argp->lid)) {
+		result.error = ERR_DEVICELOCKEDBYANOTHERLINK;
+		if (haveLock(argp->lid)) {
+			// maybe do something here to warn about a device trying to lock multiple times?
+		}
+	}
+	else {
+		lock(argp->lid);
+		result.error = 0;
+	}
+
 	return &result;
 }
 
@@ -180,10 +180,13 @@ Device_Error *
 device_unlock_1_svc(Device_Link *argp, struct svc_req *rqstp) {
 	static Device_Error result;
 	printf("device_unlock_1_svc()\n");
-	/*
-	 * insert server code here
-	 */
-	result.error = 0;
+
+	if (!isLocked(*argp) || !haveLock(*argp))
+		result.error = ERR_NOLOCKHELDBYTHISLINK;
+	else {
+		result.error = 0;
+		unlock();
+	}
 	return &result;
 }
 
-- 
cgit