summaryrefslogtreecommitdiff
path: root/libvxi11client/libvxi11client.c
diff options
context:
space:
mode:
Diffstat (limited to 'libvxi11client/libvxi11client.c')
-rw-r--r--libvxi11client/libvxi11client.c172
1 files changed, 122 insertions, 50 deletions
diff --git a/libvxi11client/libvxi11client.c b/libvxi11client/libvxi11client.c
index 7b23c43..586e84f 100644
--- a/libvxi11client/libvxi11client.c
+++ b/libvxi11client/libvxi11client.c
@@ -4,6 +4,7 @@
#include <unistd.h>
#include <glib.h>
#include <poll.h>
+#include <errno.h>
#include "libvxi11client.h"
#include "vxi11.h"
@@ -37,7 +38,7 @@ device_intr_srq_1_svc(Device_SrqParms *argp, struct svc_req *rqstp) {
printf("device_intr_srq_1_svc()\n");
#endif
- if (interruptcallback != NULL) {
+ if (interruptcallback != NULL ) {
interruptcallback(argp->handle.handle_val);
}
@@ -54,8 +55,8 @@ static void device_intr_1(struct svc_req *rqstp, register SVCXPRT *transp) {
char *(*local)(char *, struct svc_req *);
switch (rqstp->rq_proc) {
- case NULLPROC:
- (void) svc_sendreply(transp, (xdrproc_t) xdr_void, (char *) NULL);
+ case NULLPROC :
+ (void) svc_sendreply(transp, (xdrproc_t) xdr_void, (char *) NULL );
return;
case device_intr_srq:
@@ -103,8 +104,10 @@ static Device_Flags vxi11_generateflags(bool waitlock, bool end, bool termchrset
int vxi11_open(VXI11Context* context, char* address, char* device) {
context->clnt = clnt_create(address, DEVICE_CORE, DEVICE_CORE_VERSION, "tcp");
- if (context->clnt == NULL)
+ if (context->clnt == NULL ) {
+ errno = ERRNO_BADSTATE;
return 0;
+ }
else {
Create_LinkParms link_parms;
@@ -131,7 +134,7 @@ int vxi11_open(VXI11Context* context, char* address, char* device) {
serveraddr.sin_port = htons(linkresp->abortPort);
int sock = RPC_ANYSOCK;
context->abortclnt = clnttcp_create(&serveraddr, DEVICE_ASYNC, DEVICE_ASYNC_VERSION, &sock, 0, 0);
- if (context->abortclnt == NULL)
+ if (context->abortclnt == NULL )
return 0;
}
@@ -144,8 +147,10 @@ int vxi11_open(VXI11Context* context, char* address, char* device) {
return 1;
}
- else if (linkresp == NULL)
+ else if (linkresp == NULL ) {
+ errno = ERRNO_NULLRESULT;
return 0;
+ }
else
return -(linkresp->error);
}
@@ -158,8 +163,10 @@ int vxi11_open(VXI11Context* context, char* address, char* device) {
*/
int vxi11_readstatusbyte(VXI11Context* context, bool waitforlock) {
- if (context->clnt == NULL)
+ if (context->clnt == NULL ) {
+ errno = ERRNO_BADSTATE;
return 0;
+ }
Device_GenericParms params = { .lid = context->devicelink, .flags = vxi11_generateflags(waitforlock, false, false),
.lock_timeout = VXI11_DEFAULT_TIMEOUT, .io_timeout = VXI11_DEFAULT_TIMEOUT };
@@ -168,8 +175,10 @@ int vxi11_readstatusbyte(VXI11Context* context, bool waitforlock) {
if (resp != NULL && resp->error == 0)
return resp->stb | (1 << 8); // this sets a bit above the byte so that we can tell whether there was a state issue
// or if the instrument returned 0
- else if (resp == NULL)
+ else if (resp == NULL ) {
+ errno = ERRNO_NULLRESULT;
return 0;
+ }
else
return -(resp->error);
}
@@ -180,8 +189,10 @@ int vxi11_readstatusbyte(VXI11Context* context, bool waitforlock) {
*/
int vxi11_write(VXI11Context* context, char* data, int len, bool waitlock, bool end) {
- if (context->clnt == NULL)
+ if (context->clnt == NULL ) {
+ errno = ERRNO_BADSTATE;
return 0;
+ }
Device_WriteParms params = { .lid = context->devicelink, .io_timeout = VXI11_DEFAULT_TIMEOUT, .lock_timeout =
VXI11_DEFAULT_TIMEOUT, .flags = vxi11_generateflags(waitlock, end, false) };
@@ -191,8 +202,10 @@ int vxi11_write(VXI11Context* context, char* data, int len, bool waitlock, bool
Device_WriteResp* resp = device_write_1(&params, context->clnt);
if (resp != NULL && resp->error == 0)
return resp->size;
- else if (resp == NULL)
+ else if (resp == NULL ) {
+ errno = ERRNO_NULLRESULT;
return 0;
+ }
else
return -(resp->error);
}
@@ -203,8 +216,10 @@ int vxi11_write(VXI11Context* context, char* data, int len, bool waitlock, bool
int vxi11_read(VXI11Context* context, char* buffer, unsigned int bufferlen, bool waitlock, bool termchrset,
char termchr, unsigned int* reason) {
- if (context->clnt == NULL)
+ if (context->clnt == NULL ) {
+ errno = ERRNO_BADSTATE;
return 0;
+ }
Device_ReadParms params = { .lid = context->devicelink, .requestSize = 256, .io_timeout = VXI11_DEFAULT_TIMEOUT,
.lock_timeout = VXI11_DEFAULT_TIMEOUT, .flags = vxi11_generateflags(waitlock, false, termchrset),
@@ -215,7 +230,7 @@ int vxi11_read(VXI11Context* context, char* buffer, unsigned int bufferlen, bool
#ifdef DEBUG
printf("Got \"%s\" from server\n", resp->data.data_val);
#endif
- if (buffer != NULL && resp->data.data_val != NULL) {
+ if (buffer != NULL && resp->data.data_val != NULL ) {
int lengthtocopy = ((bufferlen - 1) < resp->data.data_len ? (bufferlen - 1) : resp->data.data_len);
strncpy(buffer, resp->data.data_val, lengthtocopy);
}
@@ -223,12 +238,14 @@ int vxi11_read(VXI11Context* context, char* buffer, unsigned int bufferlen, bool
else
printf("Supplied buffer is null!\n");
#endif
- if (reason != NULL)
+ if (reason != NULL )
*reason = resp->reason;
return resp->data.data_len;
}
- else if (resp == NULL)
+ else if (resp == NULL ) {
+ errno = ERRNO_NULLRESULT;
return 0;
+ }
else
return -(resp->error);
}
@@ -240,14 +257,16 @@ int vxi11_read(VXI11Context* context, char* buffer, unsigned int bufferlen, bool
int vxi11_docmd(VXI11Context* context, char* datain, int datainlen, char* dataout, int outbufferlen, int* dataoutlen,
unsigned long cmd, bool waitforlock) {
- if (context->clnt == NULL)
+ if (context->clnt == NULL ) {
+ errno = ERRNO_BADSTATE;
return 0;
+ }
Device_DocmdParms params = { .lid = context->devicelink, .flags = vxi11_generateflags(waitforlock, false, false),
.io_timeout = VXI11_DEFAULT_TIMEOUT, .lock_timeout = VXI11_DEFAULT_TIMEOUT, .cmd = cmd, .network_order = 0,
.datasize = 0 };
- if (datain == NULL)
+ if (datain == NULL )
datainlen = 0;
else if (datainlen < 0)
datainlen = strlen(datain) + 1;
@@ -257,14 +276,16 @@ int vxi11_docmd(VXI11Context* context, char* datain, int datainlen, char* dataou
Device_DocmdResp* resp = device_docmd_1(&params, context->clnt);
if (resp != NULL && resp->error == 0) {
- if (dataout != NULL)
+ if (dataout != NULL )
strncpy(dataout, resp->data_out.data_out_val,
(resp->data_out.data_out_len > outbufferlen ? outbufferlen : resp->data_out.data_out_len));
*dataoutlen = resp->data_out.data_out_len;
return 1;
}
- else if (resp == NULL)
+ else if (resp == NULL ) {
+ errno = ERRNO_NULLRESULT;
return 0;
+ }
else
return -(resp->error);
}
@@ -274,8 +295,10 @@ int vxi11_docmd(VXI11Context* context, char* datain, int datainlen, char* dataou
*/
int vxi11_trigger(VXI11Context* context, bool waitforlock) {
- if (context->clnt == NULL)
+ if (context->clnt == NULL ) {
+ errno = ERRNO_BADSTATE;
return 0;
+ }
Device_GenericParms params = { .lid = context->devicelink, .flags = vxi11_generateflags(waitforlock, false, false),
.lock_timeout = VXI11_DEFAULT_TIMEOUT, .io_timeout = VXI11_DEFAULT_TIMEOUT };
@@ -283,6 +306,10 @@ int vxi11_trigger(VXI11Context* context, bool waitforlock) {
if (error->error == 0)
return 1;
+ else if (error == NULL ) {
+ errno = ERRNO_NULLRESULT;
+ return 0;
+ }
else
return -(error->error);
}
@@ -292,15 +319,19 @@ int vxi11_trigger(VXI11Context* context, bool waitforlock) {
*/
int vxi11_clear(VXI11Context* context, bool waitforlock) {
- if (context->clnt == NULL)
+ if (context->clnt == NULL ) {
+ errno = ERRNO_BADSTATE;
return 0;
+ }
Device_GenericParms params = { .lid = context->devicelink, .flags = vxi11_generateflags(waitforlock, false, false),
.lock_timeout = VXI11_DEFAULT_TIMEOUT, .io_timeout = VXI11_DEFAULT_TIMEOUT };
Device_Error* error = device_clear_1(&params, context->clnt);
if (error != NULL && error->error == 0)
return 1;
- else if (error == NULL)
+ else if (error == NULL ) {
+ errno = ERRNO_NULLRESULT;
return 0;
+ }
else
return -(error->error);
}
@@ -310,15 +341,19 @@ int vxi11_clear(VXI11Context* context, bool waitforlock) {
*/
int vxi11_remote(VXI11Context* context, bool waitforlock) {
- if (context->clnt == NULL)
+ if (context->clnt == NULL ) {
+ errno = ERRNO_BADSTATE;
return 0;
+ }
Device_GenericParms params = { .lid = context->devicelink, .flags = vxi11_generateflags(waitforlock, false, false),
.lock_timeout = VXI11_DEFAULT_TIMEOUT, .io_timeout = VXI11_DEFAULT_TIMEOUT };
Device_Error* error = device_remote_1(&params, context->clnt);
if (error != NULL && error->error == 0)
return 1;
- else if (error == NULL)
+ else if (error == NULL ) {
+ errno = ERRNO_NULLRESULT;
return 0;
+ }
else
return -(error->error);
}
@@ -328,15 +363,19 @@ int vxi11_remote(VXI11Context* context, bool waitforlock) {
*/
int vxi11_local(VXI11Context* context, bool waitforlock) {
- if (context->clnt == NULL)
+ if (context->clnt == NULL ) {
+ errno = ERRNO_BADSTATE;
return 0;
+ }
Device_GenericParms params = { .lid = context->devicelink, .flags = vxi11_generateflags(waitforlock, false, false),
.lock_timeout = VXI11_DEFAULT_TIMEOUT, .io_timeout = VXI11_DEFAULT_TIMEOUT };
Device_Error* error = device_local_1(&params, context->clnt);
if (error != NULL && error->error == 0)
return 1;
- else if (error == NULL)
+ else if (error == NULL ) {
+ errno = ERRNO_NULLRESULT;
return 0;
+ }
else
return -(error->error);
}
@@ -346,15 +385,19 @@ int vxi11_local(VXI11Context* context, bool waitforlock) {
*/
int vxi11_lock(VXI11Context* context, bool waitforlock) {
- if (context->clnt == NULL)
+ if (context->clnt == NULL ) {
+ errno = ERRNO_BADSTATE;
return 0;
+ }
Device_LockParms params = { .lid = context->devicelink, .flags = vxi11_generateflags(waitforlock, false, false),
.lock_timeout = VXI11_DEFAULT_TIMEOUT };
Device_Error* error = device_lock_1(&params, context->clnt);
if (error != NULL && error->error == 0)
return 1;
- else if (error == NULL)
+ else if (error == NULL ) {
+ errno = ERRNO_NULLRESULT;
return 0;
+ }
else
return -(error->error);
}
@@ -364,13 +407,17 @@ int vxi11_lock(VXI11Context* context, bool waitforlock) {
*/
int vxi11_unlock(VXI11Context* context) {
- if (context->clnt == NULL)
+ if (context->clnt == NULL ) {
+ errno = ERRNO_BADSTATE;
return 0;
+ }
Device_Error* error = device_unlock_1(&(context->devicelink), context->clnt);
if (error != NULL && error->error == 0)
return 1;
- else if (error == NULL)
+ else if (error == NULL ) {
+ errno = ERRNO_NULLRESULT;
return 0;
+ }
else
return -(error->error);
}
@@ -381,7 +428,7 @@ static gpointer interruptthreadfunc(gpointer data) {
#endif
SVCXPRT *transp;
transp = svctcp_create(RPC_ANYSOCK, 0, 0);
- if (transp == NULL) {
+ if (transp == NULL ) {
fprintf(stderr, "%s", "cannot create tcp service.");
return 0;
}
@@ -407,13 +454,13 @@ static gpointer interruptthreadfunc(gpointer data) {
while (interruptserverstarted) {
int max_pollfd = svc_max_pollfd;
- if (max_pollfd == 0 && svc_pollfd == NULL)
+ if (max_pollfd == 0 && svc_pollfd == NULL )
break;
if (last_max_pollfd != max_pollfd) {
struct pollfd *new_pollfd = realloc(my_pollfd, sizeof(struct pollfd) * max_pollfd);
- if (new_pollfd == NULL) {
+ if (new_pollfd == NULL ) {
break;
}
@@ -444,18 +491,18 @@ static gpointer interruptthreadfunc(gpointer data) {
#ifdef DEBUG
printf("Interrupt channel thread ended\n");
#endif
- return NULL;
+ return NULL ;
}
int vxi11_start_interrupt_server(void (*callback)(char* handle)) {
interruptcallback = callback;
interruptserverstarted = true;
- g_thread_init(NULL);
+ g_thread_init(NULL );
mutex = g_mutex_new();
cond = g_cond_new();
g_mutex_lock(mutex);
- interruptthread = g_thread_create(interruptthreadfunc, NULL, true, NULL);
- if (interruptthread == NULL)
+ interruptthread = g_thread_create(interruptthreadfunc, NULL, true, NULL );
+ if (interruptthread == NULL )
return 0;
#ifdef DEBUG
@@ -492,12 +539,15 @@ int vxi11_stop_interrupt_server() {
* create an interrupt channel from the connected device
*/
int vxi11_create_intr_chan(VXI11Context* context) {
- if (context->clnt == NULL || context->interruptchannelopen)
+ if (context->clnt == NULL || context->interruptchannelopen) {
+ errno = ERRNO_BADSTATE;
return 0;
- else if (interruptthread == NULL) {
+ }
+ else if (interruptthread == NULL ) {
#ifdef DEBUG
printf("interrupt thread isn't running\n");
#endif
+ errno = ERRNO_INTERRUPTHANDLERTHREADISDEAD;
return 0;
}
@@ -511,8 +561,10 @@ int vxi11_create_intr_chan(VXI11Context* context) {
context->interruptchannelopen = true;
return 1;
}
- else if (error == NULL)
+ else if (error == NULL ) {
+ errno = ERRNO_NULLRESULT;
return 0;
+ }
else
return -(error->error);
}
@@ -522,18 +574,24 @@ int vxi11_create_intr_chan(VXI11Context* context) {
*/
int vxi11_destroy_intr_chan(VXI11Context* context) {
- if (context->clnt == NULL || !(context->interruptchannelopen))
+ if (context->clnt == NULL || !(context->interruptchannelopen)) {
+ errno = ERRNO_BADSTATE;
return 0;
- else if (interruptthread == NULL)
+ }
+ else if (interruptthread == NULL ) {
+ errno = ERRNO_INTERRUPTHANDLERTHREADISDEAD;
return 0;
+ }
Device_Error* error = destroy_intr_chan_1(NULL, context->clnt);
if (error != NULL && error->error == 0) {
context->interruptchannelopen = false;
return 1;
}
- else if (error == NULL)
+ else if (error == NULL ) {
+ errno = ERRNO_NULLRESULT;
return 0;
+ }
else
return -(error->error);
}
@@ -543,14 +601,18 @@ int vxi11_destroy_intr_chan(VXI11Context* context) {
*/
int vxi11_enable_srq(VXI11Context* context, bool enable, char* handle) {
- if (context->clnt == NULL || !(context->interruptchannelopen))
+ if (context->clnt == NULL || !(context->interruptchannelopen)) {
+ errno = ERRNO_BADSTATE;
return 0;
- else if (interruptthread == NULL)
+ }
+ else if (interruptthread == NULL ) {
+ errno = ERRNO_INTERRUPTHANDLERTHREADISDEAD;
return 0;
+ }
Device_EnableSrqParms params = { .lid = context->devicelink, .enable = enable };
if (enable) {
- if (handle != NULL) {
+ if (handle != NULL ) {
params.handle.handle_val = handle;
params.handle.handle_len = strlen(handle) + 1;
}
@@ -563,8 +625,10 @@ int vxi11_enable_srq(VXI11Context* context, bool enable, char* handle) {
if (error != NULL && error->error == 0) {
return 1;
}
- else if (error == NULL)
+ else if (error == NULL ) {
+ errno = ERRNO_NULLRESULT;
return 0;
+ }
else
return -(error->error);
}
@@ -574,13 +638,17 @@ int vxi11_enable_srq(VXI11Context* context, bool enable, char* handle) {
*/
int vxi11_abort(VXI11Context* context) {
- if (context->abortclnt == NULL)
+ if (context->abortclnt == NULL ) {
+ errno = ERRNO_BADSTATE;
return 0;
+ }
Device_Error* error = device_abort_1(&(context->devicelink), context->abortclnt);
if (error != NULL && error->error == 0)
return 1;
- else if (error == NULL)
+ else if (error == NULL ) {
+ errno = ERRNO_NULLRESULT;
return 0;
+ }
else
return -(error->error);
}
@@ -590,8 +658,10 @@ int vxi11_abort(VXI11Context* context) {
*/
int vxi11_close(VXI11Context* context) {
- if (context->clnt == NULL)
+ if (context->clnt == NULL ) {
+ errno = ERRNO_BADSTATE;
return 0;
+ }
Device_Error* error = destroy_link_1(&(context->devicelink), context->clnt);
clnt_destroy(context->clnt);
@@ -601,8 +671,10 @@ int vxi11_close(VXI11Context* context) {
if (error != NULL && error->error == 0)
return 1;
- else if (error == NULL)
+ else if (error == NULL ) {
+ errno = ERRNO_NULLRESULT;
return 0;
+ }
else
return -(error->error);
}