summaryrefslogtreecommitdiff
path: root/libvxi11client/libvxi11client.c
diff options
context:
space:
mode:
authordaniel <danieruru@gmail.com>2013-01-17 06:08:31 +0900
committerdaniel <danieruru@gmail.com>2013-01-17 06:08:31 +0900
commit68247df5e2a1c6ee1b02834eac49e0b75875d03a (patch)
treeb9b09feb736d79f999d1e2f22ca1f6aa87207df5 /libvxi11client/libvxi11client.c
parent06907995e46e24396ade38042aabf2d382125917 (diff)
add a "context" for handling multiple instruments
Diffstat (limited to 'libvxi11client/libvxi11client.c')
-rw-r--r--libvxi11client/libvxi11client.c140
1 files changed, 69 insertions, 71 deletions
diff --git a/libvxi11client/libvxi11client.c b/libvxi11client/libvxi11client.c
index 60e4609..614f6a6 100644
--- a/libvxi11client/libvxi11client.c
+++ b/libvxi11client/libvxi11client.c
@@ -4,6 +4,7 @@
#include <unistd.h>
#include <glib.h>
#include <poll.h>
+#include "libvxi11client.h"
#include "vxi11.h"
#define DEBUG
@@ -24,9 +25,6 @@
#define VXI11_DEFAULT_TIMEOUT 1000
-static CLIENT* clnt = NULL;
-static CLIENT* abortclnt = NULL;
-static Device_Link devicelink;
static GThread* interruptthread;
static bool interruptchannelopen = false;
static void (*interruptcallback)(void) = NULL;
@@ -109,29 +107,29 @@ static Device_Flags vxi11_generateflags(bool waitlock, bool end, bool termchrset
* $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");
+int vxi11_open(Context* context, char* address, char* device) {
+ context->clnt = clnt_create(address, DEVICE_CORE, DEVICE_CORE_VERSION, "tcp");
- if (clnt == NULL)
+ if (context->clnt == NULL)
return 0;
else {
Create_LinkParms link_parms;
- link_parms.clientId = (long) clnt;
+ link_parms.clientId = (long) context->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);
+ Create_LinkResp* linkresp = create_link_1(&link_parms, context->clnt);
if (linkresp != NULL && linkresp->error == 0) {
- devicelink = linkresp->lid;
+ context->devicelink = linkresp->lid;
#ifdef DEBUG
printf("Link created, lid is %d, abort channel port %d\n", (int) linkresp->lid, linkresp->abortPort);
#endif
struct sockaddr_in serveraddr;
- if (clnt_control(clnt, CLGET_SERVER_ADDR, (char*) &serveraddr)) {
+ if (clnt_control(context->clnt, CLGET_SERVER_ADDR, (char*) &serveraddr)) {
#ifdef DEBUG
char addressstring[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &serveraddr.sin_addr, addressstring, sizeof(addressstring));
@@ -139,8 +137,8 @@ int vxi11_open(char* address, char* device) {
#endif
serveraddr.sin_port = htons(linkresp->abortPort);
int sock = RPC_ANYSOCK;
- abortclnt = clnttcp_create(&serveraddr, DEVICE_ASYNC, DEVICE_ASYNC_VERSION, &sock, 0, 0);
- if (abortclnt == NULL)
+ context->abortclnt = clnttcp_create(&serveraddr, DEVICE_ASYNC, DEVICE_ASYNC_VERSION, &sock, 0, 0);
+ if (context->abortclnt == NULL)
return 0;
}
@@ -161,13 +159,13 @@ int vxi11_open(char* address, char* device) {
* read the status byte of the connected server
*/
-int vxi11_readstatusbyte(bool waitforlock) {
- if (clnt == NULL)
+int vxi11_readstatusbyte(Context* context, bool waitforlock) {
+ if (context->clnt == NULL)
return 0;
- Device_GenericParms params = { .lid = devicelink, .flags = vxi11_generateflags(waitforlock, false, false),
+ Device_GenericParms params = { .lid = context->devicelink, .flags = vxi11_generateflags(waitforlock, false, false),
.lock_timeout = VXI11_DEFAULT_TIMEOUT, .io_timeout = VXI11_DEFAULT_TIMEOUT };
- Device_ReadStbResp* resp = device_readstb_1(&params, clnt);
+ Device_ReadStbResp* resp = device_readstb_1(&params, context->clnt);
if (resp != NULL && resp->error == 0)
return resp->stb;
@@ -181,16 +179,16 @@ int vxi11_readstatusbyte(bool waitforlock) {
* write to the connected device
*/
-int vxi11_write(char* data, unsigned int len, bool waitlock, bool end) {
- if (clnt == NULL)
+int vxi11_write(Context* context, char* data, unsigned int len, bool waitlock, bool end) {
+ if (context->clnt == NULL)
return 0;
- Device_WriteParms params = { .lid = devicelink, .io_timeout = VXI11_DEFAULT_TIMEOUT, .lock_timeout =
+ Device_WriteParms params = { .lid = context->devicelink, .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);
+ Device_WriteResp* resp = device_write_1(&params, context->clnt);
if (resp != NULL && resp->error == 0)
return resp->size;
else if (resp == NULL)
@@ -203,15 +201,15 @@ int vxi11_write(char* data, unsigned int len, bool waitlock, bool end) {
* read from the connected device
*/
-int vxi11_read(char* buffer, unsigned int bufferlen, bool waitlock, bool termchrset, char termchr) {
- if (clnt == NULL)
+int vxi11_read(Context* context, char* buffer, unsigned int bufferlen, bool waitlock, bool termchrset, char termchr) {
+ if (context->clnt == NULL)
return 0;
- Device_ReadParms params = { .lid = devicelink, .requestSize = 256, .io_timeout = VXI11_DEFAULT_TIMEOUT,
+ Device_ReadParms params = { .lid = context->devicelink, .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);
+ Device_ReadResp* resp = device_read_1(&params, context->clnt);
if (resp != NULL && resp->error == 0) {
#ifdef DEBUG
printf("Got \"%s\" from server\n", resp->data.data_val);
@@ -230,18 +228,18 @@ int vxi11_read(char* buffer, unsigned int bufferlen, bool waitlock, bool termchr
*
*/
-int vxi11_docmd(unsigned long cmd, bool waitforlock) {
- if (clnt == NULL)
+int vxi11_docmd(Context* context, unsigned long cmd, bool waitforlock) {
+ if (context->clnt == NULL)
return 0;
- Device_DocmdParms params = { .lid = devicelink, .flags = vxi11_generateflags(waitforlock, false, false),
+ 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 };
params.data_in.data_in_len = 0;
params.data_in.data_in_val = NULL;
- Device_DocmdResp* resp = device_docmd_1(&params, clnt);
+ Device_DocmdResp* resp = device_docmd_1(&params, context->clnt);
if (resp != NULL && resp->error == 0)
return 1;
else if (resp == NULL)
@@ -254,13 +252,13 @@ int vxi11_docmd(unsigned long cmd, bool waitforlock) {
* trigger the connected device
*/
-int vxi11_trigger(bool waitforlock) {
- if (clnt == NULL)
+int vxi11_trigger(Context* context, bool waitforlock) {
+ if (context->clnt == NULL)
return 0;
- Device_GenericParms params = { .lid = devicelink, .flags = vxi11_generateflags(waitforlock, false, false),
+ 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_trigger_1(&params, clnt);
+ Device_Error* error = device_trigger_1(&params, context->clnt);
if (error->error == 0)
return 1;
@@ -272,12 +270,12 @@ int vxi11_trigger(bool waitforlock) {
* clear the connected device
*/
-int vxi11_clear(bool waitforlock) {
- if (clnt == NULL)
+int vxi11_clear(Context* context, bool waitforlock) {
+ if (context->clnt == NULL)
return 0;
- Device_GenericParms params = { .lid = devicelink, .flags = vxi11_generateflags(waitforlock, false, false),
+ 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, clnt);
+ Device_Error* error = device_clear_1(&params, context->clnt);
if (error != NULL && error->error == 0)
return 1;
else if (error == NULL)
@@ -290,12 +288,12 @@ int vxi11_clear(bool waitforlock) {
* remote the connected device
*/
-int vxi11_remote(bool waitforlock) {
- if (clnt == NULL)
+int vxi11_remote(Context* context, bool waitforlock) {
+ if (context->clnt == NULL)
return 0;
- Device_GenericParms params = { .lid = devicelink, .flags = vxi11_generateflags(waitforlock, false, false),
+ 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, clnt);
+ Device_Error* error = device_remote_1(&params, context->clnt);
if (error != NULL && error->error == 0)
return 1;
else if (error == NULL)
@@ -308,12 +306,12 @@ int vxi11_remote(bool waitforlock) {
* local the connected device
*/
-int vxi11_local(bool waitforlock) {
- if (clnt == NULL)
+int vxi11_local(Context* context, bool waitforlock) {
+ if (context->clnt == NULL)
return 0;
- Device_GenericParms params = { .lid = devicelink, .flags = vxi11_generateflags(waitforlock, false, false),
+ 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, clnt);
+ Device_Error* error = device_local_1(&params, context->clnt);
if (error != NULL && error->error == 0)
return 1;
else if (error == NULL)
@@ -326,12 +324,12 @@ int vxi11_local(bool waitforlock) {
* lock the connected device
*/
-int vxi11_lock(bool waitforlock) {
- if (clnt == NULL)
+int vxi11_lock(Context* context, bool waitforlock) {
+ if (context->clnt == NULL)
return 0;
- Device_LockParms params = { .lid = devicelink, .flags = vxi11_generateflags(waitforlock, false, false),
+ Device_LockParms params = { .lid = context->devicelink, .flags = vxi11_generateflags(waitforlock, false, false),
.lock_timeout = VXI11_DEFAULT_TIMEOUT };
- Device_Error* error = device_lock_1(&params, clnt);
+ Device_Error* error = device_lock_1(&params, context->clnt);
if (error != NULL && error->error == 0)
return 1;
else if (error == NULL)
@@ -344,10 +342,10 @@ int vxi11_lock(bool waitforlock) {
* unlock the connected device
*/
-int vxi11_unlock() {
- if (clnt == NULL)
+int vxi11_unlock(Context* context) {
+ if (context->clnt == NULL)
return 0;
- Device_Error* error = device_unlock_1(&devicelink, clnt);
+ Device_Error* error = device_unlock_1(&(context->devicelink), context->clnt);
if (error != NULL && error->error == 0)
return 1;
else if (error == NULL)
@@ -426,10 +424,10 @@ static gpointer interruptthreadfunc(gpointer data) {
/**
* create an interrupt channel from the connected device
*/
-int vxi11_create_intr_chan() {
+int vxi11_create_intr_chan(Context* context) {
if (interruptchannelopen)
return 0;
- else if (clnt == NULL)
+ else if (context->clnt == NULL)
return 0;
else if (interruptthread != NULL)
return 0;
@@ -450,7 +448,7 @@ int vxi11_create_intr_chan() {
Device_RemoteFunc remotefunc = { .hostAddr = myaddress.sin_addr.s_addr, .hostPort = port, .progNum = DEVICE_INTR,
.progVers = DEVICE_INTR_VERSION, .progFamily = DEVICE_TCP };
- Device_Error* error = create_intr_chan_1(&remotefunc, clnt);
+ Device_Error* error = create_intr_chan_1(&remotefunc, context->clnt);
if (error != NULL && error->error == 0)
return 1;
else if (error == NULL) {
@@ -467,15 +465,15 @@ int vxi11_create_intr_chan() {
* destroy an interrupt channel from the connected device
*/
-int vxi11_destroy_intr_chan() {
+int vxi11_destroy_intr_chan(Context* context) {
if (!interruptchannelopen)
return 0;
- else if (clnt == NULL)
+ else if (context->clnt == NULL)
return 0;
else if (interruptthread == NULL)
return 0;
- Device_Error* error = destroy_intr_chan_1(NULL, clnt);
+ Device_Error* error = destroy_intr_chan_1(NULL, context->clnt);
if (error != NULL && error->error == 0) {
killinterruptthreadandwait();
return 1;
@@ -490,15 +488,15 @@ int vxi11_destroy_intr_chan() {
* enable interrupts
*/
-int vxi11_enable_srq(bool enable, char* handle, void (*callback)(void)) {
+int vxi11_enable_srq(Context* context, bool enable, char* handle, void (*callback)(void)) {
if (!interruptchannelopen)
return 0;
- else if (clnt == NULL)
+ else if (context->clnt == NULL)
return 0;
else if (interruptthread == NULL)
return 0;
- Device_EnableSrqParms params = { .lid = devicelink, .enable = enable };
+ Device_EnableSrqParms params = { .lid = context->devicelink, .enable = enable };
if (enable) {
if (handle != NULL) {
params.handle.handle_val = handle;
@@ -510,7 +508,7 @@ int vxi11_enable_srq(bool enable, char* handle, void (*callback)(void)) {
params.handle.handle_val = NULL;
params.handle.handle_len = 0;
}
- Device_Error* error = device_enable_srq_1(&params, clnt);
+ Device_Error* error = device_enable_srq_1(&params, context->clnt);
if (error != NULL && error->error == 0) {
return 1;
}
@@ -524,10 +522,10 @@ int vxi11_enable_srq(bool enable, char* handle, void (*callback)(void)) {
* send an abort to the connected device
*/
-int vxi11_abort() {
- if (abortclnt == NULL)
+int vxi11_abort(Context* context) {
+ if (context->abortclnt == NULL)
return 0;
- Device_Error* error = device_abort_1(&devicelink, abortclnt);
+ Device_Error* error = device_abort_1(&(context->devicelink), context->abortclnt);
if (error != NULL && error->error == 0)
return 1;
else if (error == NULL)
@@ -540,15 +538,15 @@ int vxi11_abort() {
* close the current link and free the RPC client
*/
-int vxi11_close() {
- if (clnt == NULL)
+int vxi11_close(Context* context) {
+ if (context->clnt == NULL)
return 0;
- Device_Error* error = destroy_link_1(&devicelink, clnt);
- clnt_destroy(clnt);
- clnt = NULL;
- clnt_destroy(abortclnt);
- abortclnt = NULL;
+ Device_Error* error = destroy_link_1(&(context->devicelink), context->clnt);
+ clnt_destroy(context->clnt);
+ context->clnt = NULL;
+ clnt_destroy(context->abortclnt);
+ context->abortclnt = NULL;
if (error != NULL && error->error == 0)
return 1;