1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
/************************************************************************
*
* POW C socket/XPA driver
*
* Use this code to make an XPA connection to a POW process and
* send pow scripting commands.
*
***********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <float.h>
#include <xpa.h>
int openPowConnection ( void );
void closePowConnection ( void );
int sendSetCmdToPow ( char *cmd, char *buf, int buflen );
int sendGetCmdToPow ( char *cmd, char **rtnBuf, int *bufLen );
int postProcessCall ( int got, char **names, char **messages );
#define NXPA 1
static XPA powConnection = NULL;
static char *powAddress = NULL;
int openPowConnection()
{
extern XPA powConnection;
char *rtnBuf;
int bufLen;
if( !powAddress ) {
powAddress = getenv( "POW_DISPLAY" );
if( !powAddress || strlen(powAddress)==0 ) {
/* POW_DISPLAY not defined, must depend on a name server */
powAddress = "pow";
}
}
powConnection = XPAOpen( NULL );
if( sendGetCmdToPow( "version", &rtnBuf, &bufLen )<0 ) {
system("POWplot &");
sendSetCmdToPow( "scope 0", NULL, 0);
sleep(3);
if( sendGetCmdToPow( "version", &rtnBuf, &bufLen )<0 ) {
sleep(3);
if( sendGetCmdToPow( "version", &rtnBuf, &bufLen )<0 ) {
return 1;
}
}
}
free( rtnBuf );
return 0;
}
void closePowConnection()
{
extern XPA powConnection;
XPAClose( powConnection );
powConnection = NULL;
}
int sendGetCmdToPow( char *cmd, char **rtnBuf, int *bufLen)
{
int got, stat=0;
char *bufs[NXPA];
char *names[NXPA];
char *messages[NXPA];
int lens[NXPA];
extern XPA powConnection;
extern char *powAddress;
/* Flush 'sendSet' command cache */
stat = sendSetCmdToPow( NULL, NULL, -1 );
if( !stat ) {
got = XPAGet(powConnection, powAddress, cmd, "",
bufs, lens, names, messages, NXPA);
if( got==0 )
stat = -1;
else
stat = postProcessCall(got, names, messages);
*rtnBuf = bufs[0];
*bufLen = lens[0];
}
return stat;
}
int sendSetCmdToPow( char *cmd, char *buf, int buflen )
{
int got, len, stat=0;
char *names[NXPA];
char *messages[NXPA];
extern XPA powConnection;
extern char *powAddress;
static char *cache = NULL;
static int cachePos = 0;
static int cacheSize = 0;
switch ( buflen ) {
case -2: /* Free the cache */
free(cache);
cache = NULL;
cachePos = cacheSize = 0;
break;
case -1: /* Flush the cache */
if( cachePos ) {
len = cachePos;
cachePos = 0;
stat = sendSetCmdToPow( "tcl", cache, len );
}
break;
case 0: /* No data, so just cache command */
len = strlen(cmd);
if( cachePos + len + 2 > cacheSize ) {
cacheSize += 4096 + len;
if( cache ) {
cache = (char*) realloc( cache, sizeof(char) * cacheSize );
} else {
cache = (char*) malloc( sizeof(char) * cacheSize );
}
}
cache[cachePos++] = '\n';
strcpy( cache+cachePos, cmd );
cachePos += len;
if( cachePos>60000 ) {
/* Flush the cache */
stat = sendSetCmdToPow( NULL, NULL, -1 );
}
break;
default: /* Data being sent */
if( cachePos ) {
/* Flush cache first */
stat = sendSetCmdToPow( NULL, NULL, -1 );
}
if( !stat ) {
got = XPASet(powConnection, powAddress, cmd, "", buf, buflen,
names, messages, NXPA);
if( got==0 )
stat = -1;
else
stat = postProcessCall(got, names, messages);
}
break;
}
return stat;
}
int postProcessCall( int got, char **names, char **messages )
{
int i, status=0;
for(i=0; i<got; i++){
if( messages[i] != NULL ) {
/* error processing */
fprintf(stderr, "ERROR: %s (%s)\n", messages[i], names[i]);
status = 1;
}
if( names[i] ) free(names[i]);
if( messages[i] ) free(messages[i]);
}
return status;
}
|