diff options
Diffstat (limited to 'vxi11_server.c')
-rw-r--r-- | vxi11_server.c | 224 |
1 files changed, 125 insertions, 99 deletions
diff --git a/vxi11_server.c b/vxi11_server.c index 878f4aa..c5024a7 100644 --- a/vxi11_server.c +++ b/vxi11_server.c @@ -56,11 +56,13 @@ static ActiveLink* links[MAX_SESSIONS] = { NULL }; static void freelink(ActiveLink* link); -static void touchlink(int lid) { +static void touchlink(int lid) +{ g_get_current_time(&(links[lid]->lastactivity)); } -static bool isValidLink(int linkid) { +static bool isValidLink(int linkid) +{ if (linkid > MAX_SESSIONS - 1) { #ifdef DEBUG printf("Crazy link id %d\n", linkid); @@ -71,7 +73,8 @@ static bool isValidLink(int linkid) { } // test if a link has expired, this is to avoid dead locks -static bool linkexpired(ActiveLink* link) { +static bool linkexpired(ActiveLink* link) +{ GTimeVal cutoff; g_get_current_time(&cutoff); g_time_val_add(&cutoff, -LINKLONGSINCEDEADTIMEOUT); @@ -81,29 +84,33 @@ static bool linkexpired(ActiveLink* link) { #endif freelink(link); return true; - } - else + } else { return false; + } } // locking wrappers -static void lock(int lid) { +static void lock(int lid) +{ globals.VxiLocks.locked_network_server = lid; } -static void unlock() { +static void unlock() +{ globals.VxiLocks.locked_network_server = NO_SERVER_LOCKED; } -static bool haveLock(int lid) { +static bool haveLock(int lid) +{ return globals.VxiLocks.locked_network_server == lid; } -static bool isLocked() { - if (globals.VxiLocks.locked_network_server == NO_SERVER_LOCKED) +static bool isLocked() +{ + if (globals.VxiLocks.locked_network_server == NO_SERVER_LOCKED) { return false; - else { + } else { ActiveLink* link = links[globals.VxiLocks.locked_network_server]; return !(linkexpired(link)); } @@ -112,15 +119,18 @@ static bool isLocked() { #define FLAG_WAITLOCK 1 // todo -static bool waitForLock(Device_Flags flags, long timeout) { - if (!(flags & FLAG_WAITLOCK)) +static bool waitForLock(Device_Flags flags, long timeout) +{ + if (!(flags & FLAG_WAITLOCK)) { return false; + } return false; } // -static void freelink(ActiveLink* link) { +static void freelink(ActiveLink* link) +{ links[link->lid] = NULL; globals.Remote.vxi_connections--; @@ -142,7 +152,8 @@ static void freelink(ActiveLink* link) { g_free(link); } -void vxi11_deadsocketcallback(int socket) { +void vxi11_deadsocketcallback(int socket) +{ int linkid; for (linkid = 0; linkid < SIZEOFARRAY(links); linkid++) { if (links[linkid] != NULL ) { @@ -156,10 +167,12 @@ void vxi11_deadsocketcallback(int socket) { } } -static bool waitforio(long timeout) { +static bool waitforio(long timeout) +{ do { - if (!globals.VxiLocks.command_in_progress) + if (!globals.VxiLocks.command_in_progress) { return true; + } usleep(250); timeout -= 250; } while (timeout < 0); @@ -170,14 +183,15 @@ static bool waitforio(long timeout) { } Device_Error * -device_abort_1_svc(Device_Link *argp, struct svc_req *rqstp) { +device_abort_1_svc(Device_Link *argp, struct svc_req *rqstp) +{ static Device_Error result; #ifdef DEBUG printf("device_abort_1_svc()\n"); #endif - if (!isValidLink(*argp)) + if (!isValidLink(*argp)) { result.error = ERR_INVALIDLINKINDENTIFIER; - else { + } else { links[*argp]->aborted = true; result.error = 0; } @@ -185,7 +199,8 @@ device_abort_1_svc(Device_Link *argp, struct svc_req *rqstp) { } Create_LinkResp * -create_link_1_svc(Create_LinkParms *argp, struct svc_req *rqstp) { +create_link_1_svc(Create_LinkParms *argp, struct svc_req *rqstp) +{ static Create_LinkResp result; memset(&result, 0, sizeof(result)); #ifdef DEBUG @@ -193,20 +208,18 @@ create_link_1_svc(Create_LinkParms *argp, struct svc_req *rqstp) { #endif if (globals.Remote.vxi_connections == MAX_SESSIONS) { result.error = ERR_OUTOFRESOURCES; - } - else if (argp->lockDevice && isLocked() && !waitForLock(FLAG_WAITLOCK, argp->lock_timeout)) { + } else if (argp->lockDevice && isLocked() && !waitForLock(FLAG_WAITLOCK, argp->lock_timeout)) { result.error = ERR_DEVICELOCKEDBYANOTHERLINK; - } - else { + } else { ActiveLink* link = g_malloc(sizeof(ActiveLink)); if (link == NULL ) { result.error = ERR_OUTOFRESOURCES; - } - else { + } else { int linkid; for (linkid = 0; linkid < SIZEOFARRAY(links); linkid++) { - if (links[linkid] != NULL ) // clean up any dead links + if (links[linkid] != NULL ) { // clean up any dead links linkexpired(links[linkid]); + } if (links[linkid] == NULL ) { links[linkid] = link; break; @@ -239,29 +252,32 @@ create_link_1_svc(Create_LinkParms *argp, struct svc_req *rqstp) { } Device_WriteResp * -device_write_1_svc(Device_WriteParms *argp, struct svc_req *rqstp) { +device_write_1_svc(Device_WriteParms *argp, struct svc_req *rqstp) +{ static Device_WriteResp result; memset(&result, 0, sizeof(result)); #ifdef DEBUG printf("device_write_1_svc()\n"); #endif - if (!isValidLink(argp->lid)) + if (!isValidLink(argp->lid)) { result.error = ERR_INVALIDLINKINDENTIFIER; - else if (isLocked() && !haveLock(argp->lid) && !waitForLock(argp->flags, argp->lock_timeout)) + } else if (isLocked() && !haveLock(argp->lid) && !waitForLock(argp->flags, argp->lock_timeout)) { result.error = ERR_DEVICELOCKEDBYANOTHERLINK; - else if (!waitforio(argp->io_timeout)) + } else if (!waitforio(argp->io_timeout)) { result.error = ERR_IOTIMEOUT; - else { + } else { touchlink(argp->lid); links[argp->lid]->aborted = false; gchar* str = NULL; - if (argp->data.data_val != NULL ) + if (argp->data.data_val != NULL ) { str = g_strndup(argp->data.data_val, argp->data.data_len); + } if (str != NULL ) { #ifdef DEBUG int n; - for (n = 0; str[n] != '\0'; n++) + for (n = 0; str[n] != '\0'; n++) { printf("%02x ", (unsigned char) str[n]); + } printf("\n"); printf("got <<%s>> (reported length %d)on link %d.\n", str, (int) argp->data.data_len, (int) argp->lid); #endif @@ -275,26 +291,27 @@ device_write_1_svc(Device_WriteParms *argp, struct svc_req *rqstp) { } Device_ReadResp * -device_read_1_svc(Device_ReadParms *argp, struct svc_req *rqstp) { +device_read_1_svc(Device_ReadParms *argp, struct svc_req *rqstp) +{ static Device_ReadResp result; #ifdef DEBUG printf("device_read_1_svc()\n"); #endif // this free's the data from the last call - if (result.data.data_val != NULL ) + if (result.data.data_val != NULL ) { g_free(result.data.data_val); + } memset(&result, 0, sizeof(result)); - if (!isValidLink(argp->lid)) + if (!isValidLink(argp->lid)) { result.error = ERR_INVALIDLINKINDENTIFIER; - else if (isLocked() && !haveLock(argp->lid) && !waitForLock(argp->flags, argp->lock_timeout)) + } else if (isLocked() && !haveLock(argp->lid) && !waitForLock(argp->flags, argp->lock_timeout)) { result.error = ERR_DEVICELOCKEDBYANOTHERLINK; - else if (!waitforio(argp->io_timeout)) { + } else if (!waitforio(argp->io_timeout)) { result.error = ERR_IOTIMEOUT; - } - else { + } else { touchlink(argp->lid); if (globals.Registers.pending_output_message != NULL ) { result.data.data_len = strlen(globals.Registers.pending_output_message); @@ -304,9 +321,9 @@ device_read_1_svc(Device_ReadParms *argp, struct svc_req *rqstp) { #ifdef DEBUG printf("sending ---%s---\n", result.data.data_val); #endif - } - else + } else { result.data.data_val = NULL; + } if (links[argp->lid]->aborted) { result.error = ERR_ABORT; @@ -319,17 +336,18 @@ device_read_1_svc(Device_ReadParms *argp, struct svc_req *rqstp) { } Device_ReadStbResp * -device_readstb_1_svc(Device_GenericParms *argp, struct svc_req *rqstp) { +device_readstb_1_svc(Device_GenericParms *argp, struct svc_req *rqstp) +{ static Device_ReadStbResp result; memset(&result, 0, sizeof(result)); #ifdef DEBUG printf("device_readstb_1_svc()\n"); #endif - if (!isValidLink(argp->lid)) + if (!isValidLink(argp->lid)) { result.error = ERR_INVALIDLINKINDENTIFIER; - else if (isLocked() && !haveLock(argp->lid) && !waitForLock(argp->flags, argp->lock_timeout)) + } else if (isLocked() && !haveLock(argp->lid) && !waitForLock(argp->flags, argp->lock_timeout)) { result.error = ERR_DEVICELOCKEDBYANOTHERLINK; - else { + } else { touchlink(argp->lid); result.stb = GPIB_and_VXI_get_STB(); } @@ -337,16 +355,17 @@ device_readstb_1_svc(Device_GenericParms *argp, struct svc_req *rqstp) { } Device_Error * -device_trigger_1_svc(Device_GenericParms *argp, struct svc_req *rqstp) { +device_trigger_1_svc(Device_GenericParms *argp, struct svc_req *rqstp) +{ static Device_Error result; #ifdef DEBUG printf("device_trigger_1_svc()\n"); #endif - if (!isValidLink(argp->lid)) + if (!isValidLink(argp->lid)) { result.error = ERR_INVALIDLINKINDENTIFIER; - else if (isLocked() && !haveLock(argp->lid) && !waitForLock(argp->flags, argp->lock_timeout)) + } else if (isLocked() && !haveLock(argp->lid) && !waitForLock(argp->flags, argp->lock_timeout)) { result.error = ERR_DEVICELOCKEDBYANOTHERLINK; - else { + } else { touchlink(argp->lid); result.error = ERR_OPERATIONNOTSUPPORTED; } @@ -354,16 +373,17 @@ device_trigger_1_svc(Device_GenericParms *argp, struct svc_req *rqstp) { } Device_Error * -device_clear_1_svc(Device_GenericParms *argp, struct svc_req *rqstp) { +device_clear_1_svc(Device_GenericParms *argp, struct svc_req *rqstp) +{ static Device_Error result; #ifdef DEBUG printf("device_clear_1_svc()\n"); #endif - if (!isValidLink(argp->lid)) + if (!isValidLink(argp->lid)) { result.error = ERR_INVALIDLINKINDENTIFIER; - else if (isLocked() && !haveLock(argp->lid) && !waitForLock(argp->flags, argp->lock_timeout)) + } else if (isLocked() && !haveLock(argp->lid) && !waitForLock(argp->flags, argp->lock_timeout)) { result.error = ERR_DEVICELOCKEDBYANOTHERLINK; - else { + } else { touchlink(argp->lid); result.error = 0; GPIB_and_VXI_device_clear(); @@ -372,19 +392,20 @@ device_clear_1_svc(Device_GenericParms *argp, struct svc_req *rqstp) { } Device_Error * -device_remote_1_svc(Device_GenericParms *argp, struct svc_req *rqstp) { +device_remote_1_svc(Device_GenericParms *argp, struct svc_req *rqstp) +{ static Device_Error result; #ifdef DEBUG printf("device_remote_1_svc()\n"); #endif - if (!isValidLink(argp->lid)) + if (!isValidLink(argp->lid)) { result.error = ERR_INVALIDLINKINDENTIFIER; - else if (isLocked() && !haveLock(argp->lid) && !waitForLock(argp->flags, argp->lock_timeout)) + } else if (isLocked() && !haveLock(argp->lid) && !waitForLock(argp->flags, argp->lock_timeout)) { result.error = ERR_DEVICELOCKEDBYANOTHERLINK; - else { - if (isLocked() && !haveLock(argp->lid)) + } else { + if (isLocked() && !haveLock(argp->lid)) { result.error = ERR_DEVICELOCKEDBYANOTHERLINK; - else { + } else { touchlink(argp->lid); result.error = 0; globals.Remote.vxi_panel_lock = 1; @@ -394,19 +415,20 @@ device_remote_1_svc(Device_GenericParms *argp, struct svc_req *rqstp) { } Device_Error * -device_local_1_svc(Device_GenericParms *argp, struct svc_req *rqstp) { +device_local_1_svc(Device_GenericParms *argp, struct svc_req *rqstp) +{ static Device_Error result; #ifdef DEBUG printf("device_local_1_svc()\n"); #endif - if (!isValidLink(argp->lid)) + if (!isValidLink(argp->lid)) { result.error = ERR_INVALIDLINKINDENTIFIER; - else if (isLocked() && !haveLock(argp->lid) && !waitForLock(argp->flags, argp->lock_timeout)) + } else if (isLocked() && !haveLock(argp->lid) && !waitForLock(argp->flags, argp->lock_timeout)) { result.error = ERR_DEVICELOCKEDBYANOTHERLINK; - else { - if (isLocked() && !haveLock(argp->lid)) + } else { + if (isLocked() && !haveLock(argp->lid)) { result.error = ERR_DEVICELOCKEDBYANOTHERLINK; - else { + } else { touchlink(argp->lid); result.error = 0; globals.Remote.vxi_panel_lock = 0; @@ -416,21 +438,21 @@ device_local_1_svc(Device_GenericParms *argp, struct svc_req *rqstp) { } Device_Error * -device_lock_1_svc(Device_LockParms *argp, struct svc_req *rqstp) { +device_lock_1_svc(Device_LockParms *argp, struct svc_req *rqstp) +{ static Device_Error result; #ifdef DEBUG printf("device_lock_1_svc()\n"); #endif - if (!isValidLink(argp->lid)) + if (!isValidLink(argp->lid)) { result.error = ERR_INVALIDLINKINDENTIFIER; - else { + } else { 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 { + } else { touchlink(argp->lid); lock(argp->lid); result.error = 0; @@ -440,17 +462,18 @@ device_lock_1_svc(Device_LockParms *argp, struct svc_req *rqstp) { } Device_Error * -device_unlock_1_svc(Device_Link *argp, struct svc_req *rqstp) { +device_unlock_1_svc(Device_Link *argp, struct svc_req *rqstp) +{ static Device_Error result; #ifdef DEBUG printf("device_unlock_1_svc()\n"); #endif - if (!isValidLink(*argp)) + if (!isValidLink(*argp)) { result.error = ERR_INVALIDLINKINDENTIFIER; - else { - if (!isLocked(*argp) || !haveLock(*argp)) + } else { + if (!isLocked(*argp) || !haveLock(*argp)) { result.error = ERR_NOLOCKHELDBYTHISLINK; - else { + } else { touchlink(*argp); result.error = 0; unlock(); @@ -459,7 +482,8 @@ device_unlock_1_svc(Device_Link *argp, struct svc_req *rqstp) { return &result; } -void vxi11_fireinterrupt() { +void vxi11_fireinterrupt() +{ #ifdef DEBUG printf("vxi11_fireinterrupt()\n"); #endif @@ -472,14 +496,15 @@ void vxi11_fireinterrupt() { } Device_Error * -device_enable_srq_1_svc(Device_EnableSrqParms *argp, struct svc_req *rqstp) { +device_enable_srq_1_svc(Device_EnableSrqParms *argp, struct svc_req *rqstp) +{ static Device_Error result; #ifdef DEBUG printf("device_enable_srq_1_svc()\n"); #endif - if (!isValidLink(argp->lid)) + if (!isValidLink(argp->lid)) { result.error = ERR_INVALIDLINKINDENTIFIER; - else { + } else { touchlink(argp->lid); if (inthandler != NULL ) { g_free(inthandler); @@ -506,17 +531,18 @@ device_enable_srq_1_svc(Device_EnableSrqParms *argp, struct svc_req *rqstp) { } Device_DocmdResp * -device_docmd_1_svc(Device_DocmdParms *argp, struct svc_req *rqstp) { +device_docmd_1_svc(Device_DocmdParms *argp, struct svc_req *rqstp) +{ static Device_DocmdResp result; memset(&result, 0, sizeof(result)); #ifdef DEBUG printf("device_docmd_1_svc()\n"); #endif - if (!isValidLink(argp->lid)) + if (!isValidLink(argp->lid)) { result.error = ERR_INVALIDLINKINDENTIFIER; - else if (isLocked() && !haveLock(argp->lid) && !waitForLock(argp->flags, argp->lock_timeout)) + } else if (isLocked() && !haveLock(argp->lid) && !waitForLock(argp->flags, argp->lock_timeout)) { result.error = ERR_DEVICELOCKEDBYANOTHERLINK; - else { + } else { touchlink(argp->lid); result.error = ERR_OPERATIONNOTSUPPORTED; } @@ -524,15 +550,16 @@ device_docmd_1_svc(Device_DocmdParms *argp, struct svc_req *rqstp) { } Device_Error * -destroy_link_1_svc(Device_Link *argp, struct svc_req *rqstp) { +destroy_link_1_svc(Device_Link *argp, struct svc_req *rqstp) +{ static Device_Error result; #ifdef DEBUG printf("destroy_link_1_svc()\n"); #endif int lid = *argp; - if (!isValidLink(lid)) + if (!isValidLink(lid)) { result.error = ERR_INVALIDLINKINDENTIFIER; - else { + } else { freelink(links[lid]); result.error = 0; } @@ -541,7 +568,8 @@ destroy_link_1_svc(Device_Link *argp, struct svc_req *rqstp) { } Device_Error * -create_intr_chan_1_svc(Device_RemoteFunc *argp, struct svc_req *rqstp) { +create_intr_chan_1_svc(Device_RemoteFunc *argp, struct svc_req *rqstp) +{ static Device_Error result; in_port_t port = htons(argp->hostPort); @@ -569,15 +597,14 @@ create_intr_chan_1_svc(Device_RemoteFunc *argp, struct svc_req *rqstp) { } } - if (argp->progFamily != DEVICE_TCP || argp->progNum != DEVICE_INTR || argp->progVers != DEVICE_INTR_VERSION) + if (argp->progFamily != DEVICE_TCP || argp->progNum != DEVICE_INTR || argp->progVers != DEVICE_INTR_VERSION) { result.error = ERR_OPERATIONNOTSUPPORTED; - else if (intclient != NULL ) { + } else if (intclient != NULL ) { #ifdef DEBUG printf("Not creating interrupt channel, already exists\n"); #endif result.error = ERR_CHANNELALREADYESTABLISHED; - } - else { + } else { struct sockaddr_in clientaddr; clientaddr.sin_family = AF_INET; clientaddr.sin_port = port; @@ -589,8 +616,7 @@ create_intr_chan_1_svc(Device_RemoteFunc *argp, struct svc_req *rqstp) { printf("Couldn't create interrupt channel client\n"); #endif result.error = ERR_CHANNELNOTESTABLISHED; - } - else { + } else { intclient = clnt; result.error = 0; #ifdef DEBUG @@ -602,15 +628,15 @@ create_intr_chan_1_svc(Device_RemoteFunc *argp, struct svc_req *rqstp) { } Device_Error * -destroy_intr_chan_1_svc(void *argp, struct svc_req *rqstp) { +destroy_intr_chan_1_svc(void *argp, struct svc_req *rqstp) +{ static Device_Error result; #ifdef DEBUG printf("destroy_intr_chan_1_svc()\n"); #endif if (intclient == NULL ) { result.error = ERR_CHANNELNOTESTABLISHED; - } - else { + } else { clnt_destroy(intclient); intclient = NULL; result.error = 0; |