Files
sics/ascon.c
Ferdi Franceschini d9da95a5df sct_protek608.c
Implements a protocol handler for the protek  608 multimeters which just allows us to read the display.
It reports all elements of the display including the bar graph, it does not provide remote control of the multimeter.  The protocol handler broadcasts a warning to all clients if the auto-off function is enabled.

sct_rfamp.c
This is a protocol handler for the Mirrortron 35V 7A AC Generator (ANSFR-83B).

sinqhttpprot.c
Copied the PSI script context http protocol handler.

sct_orhvpsprot.c
Ordela high voltage power supply protocol handler now catches unknown commands.

sct_eurotherm_2000.tcl
Eurotherm controller for the kowari load frame by Douglas Clowes.

sct_lakeshore_3xx.tcl
Latest update from Arndt.  The two control loops are now independent, settletime and tolerance now work properly.

common_instrument_dictionary.tcl
Make instrument/status saveable.

sct_orhvps_common.tcl
Provides voltage ramping and implements the dhv1 command for the Ordela HVPS via the sct_orhpsprot.c protocol handler.

hmm_configuration_common_1.tcl
Adds new "histmem clockscale" subcommand to get and set the clock scale from the fat_clock_scale FAT parameter.
You can now upload the FAT FRAME_BUFFER and FRAME_DUTYCYCLE parameters to the histogram memory.
The veto commands are now "histmem veto on" and "histmem veto off".

hmm_object.tcl
The axis order for the histmem object has been restore to t,y,x

sct_positmotor_common.tcl
Code has been simplified.

nxscripts_common_1.tcl
Removed obsolete ::nexus::data function.  TOF axis now correctly report time_of_flight instead of "time".

plc_common_1.tcl
Make PLC info saveable.

scan_common_1.tcl
SICS-385 The scan command should check the final scan variable value against he soft upper and lower limits, not against the hard limits.
Make sure that the scan variable axis is saved.

platypus, kowari, quokka hmm_configuration.tcl
Use the HOR and VER entries in the new histmem_axes hash to select the horizontal and vertical axes for the histmem.

kowari motor_configuration.tcl secondary_slit_configuration.tcl
Flatten slits motor structure to match old layout in data files.

quokka commands.tcl
SICS-380 EApPosYmm -> EApPosY

quokka detector.tcl
Use new script context controller for Ordela HVPS

quokka hmm_configuration.tcl
Set detector height to 5.08*192 the same as the width

quokka motor_configuration.tcl
Code cleanup

quokka positmotor_configuration.tcl
Use new positmotor code.

quokka aperture_configuration.tcl
Added attenuation factor column to AttRotLookupTable

quokka parameters.tcl
SICS-380 Refactor nexus, remove redundant parameters.

site_ansto.c
Added the following protocols, Httpl, Protek608, aand RFAmp.

scriptcontext.c
SICS-386 SctActionHandler: set "send" string to NULL when a chain of scripts completes with state=idle.
It turns out that if none of the scripts in the "read chain" call [sct send] each time the chain is executed, then SICS will hammer the device with calls to AsconWrite(). This can be avoided if SctActionHandler sets the 'send' string to NULL before "goto finish" in the idle state. This will be safer and still let you have chains with multiple [sct send] and read scripts.

asyncprotocol.c
Fix platypus memory leak.

devser.c
SICS-387 Started adding code to pass signals on to script context drivers.

ascon.c
AsconTask(): Make sure we return to the AsconIdle state when sending a command which expect no response, also only reconnect if there is a Timeout when there has been an error.

r2888 | ffr | 2010-04-19 14:04:41 +1000 (Mon, 19 Apr 2010) | 90 lines
2012-11-15 17:00:29 +11:00

539 lines
13 KiB
C

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include "sics.h"
#include "splitter.h"
#include "ascon.i"
/*
CreateSocketAdress stolen from Tcl. Thanks to John Ousterhout
*/
static int CreateSocketAdress(
struct sockaddr_in *sockaddrPtr, /* Socket address */
char *host, /* Host. NULL implies INADDR_ANY */
int port) /* Port number */
{
struct hostent *hostent; /* Host database entry */
struct in_addr addr; /* For 64/32 bit madness */
(void) memset((char *) sockaddrPtr, '\0', sizeof(struct sockaddr_in));
sockaddrPtr->sin_family = AF_INET;
sockaddrPtr->sin_port = htons((unsigned short) (port & 0xFFFF));
if (host == NULL) {
addr.s_addr = INADDR_ANY;
} else {
hostent = gethostbyname(host);
if (hostent != NULL) {
memcpy((char *) &addr,
(char *) hostent->h_addr_list[0], (size_t) hostent->h_length);
} else {
addr.s_addr = inet_addr(host);
if (addr.s_addr == (unsigned long)-1) {
return 0; /* error */
}
}
}
/*
* There is a rumor that this assignment may require care on
* some 64 bit machines.
*/
sockaddrPtr->sin_addr.s_addr = addr.s_addr;
return 1;
}
double DoubleTime(void) {
struct timeval now;
/* the resolution of this function is usec, if the machine supports this
and the mantissa of a double is 51 bits or more (31 for sec and 20 for micro)
*/
gettimeofday(&now, NULL);
return now.tv_sec + now.tv_usec / 1e6;
}
void AsconError(Ascon *a, char *msg, int errorno) {
static char *stateText[]={
"state 0", "kill", "state 2", "notConnected",
"connect", "start connect", "connect finished", "connect failed",
"write", "start write", "write finished", "write failed",
"read", "start read", "read finished", "read failed",
"state 16", "state 17", "state 18", "idle"
};
char *state;
if (a->state < 0 || a->state > 19) {
state = "bad state";
} else {
state = stateText[a->state];
}
if (errorno != 0) {
a->errList = ErrPutMsg(a->errList, "ASCERR: %s %s (during %s)", msg, strerror(errorno), state);
} else {
a->errList = ErrPutMsg(a->errList, "ASCERR: %s (during %s)", msg, state);
}
a->state |= AsconFailed;
}
static void AsconConnect(Ascon *a) {
/* input state: AsconConnectStart
output state: AsconFailed or AsconConnecting */
int ret;
struct sockaddr_in adr;
char *colon;
int port;
int oldopts;
if (a->fd < 0) {
a->fd = socket(AF_INET,SOCK_STREAM,0);
if (a->fd < 0) {
AsconError(a, "socket failed:", errno);
return;
}
}
colon = strchr(a->hostport, ':');
if (colon == NULL) return;
port = atoi(colon+1);
if (port <= 0) {
AsconError(a, "bad port number", 0);
return;
}
*colon = '\0';
ret = CreateSocketAdress(&adr, a->hostport, port);
*colon = ':';
if (ret == 0) {
AsconError(a, "bad host specification", 0);
return;
}
/* should we insert the workaround for lantronix server ? see network.c */
oldopts = fcntl(a->fd, F_GETFL, 0);
fcntl(a->fd, F_SETFL, oldopts | O_NONBLOCK);
ret = connect(a->fd, (struct sockaddr *)&adr, sizeof(struct sockaddr_in));
if (ret < 0) {
switch(errno) {
case EINPROGRESS:
case EALREADY:
case EISCONN:
a->state = AsconConnecting;
break;
default:
AsconError(a, "connect failed:", errno);
return;
}
}
a->state = AsconConnecting;
return;
}
int AsconStdInit(Ascon *a, SConnection *con,
int argc, char *argv[]) {
a->fd = -1;
a->state = AsconConnectStart;
a->reconnectInterval = 10;
a->hostport = strdup(argv[1]);
if(argc > 2){
a->sendTerminator = strdup(argv[2]);
} else {
a->sendTerminator = strdup("\n");
}
if(argc > 3){
a->timeout = atof(argv[3]);
} else {
a->timeout = 2.0; /* sec */
}
return 1;
}
int AsconReadGarbage(int fd) {
fd_set rmask;
struct timeval tmo = {0,0};
int l, ret, result;
char garbage[100];
FD_ZERO(&rmask);
result = 0;
do {
FD_SET(fd, &rmask);
ret = select(fd + 1, &rmask, NULL, NULL, &tmo);
if (ret > 0) {
l = recv(fd, garbage, sizeof garbage, 0);
if (l > 0) {
/* swallow */
result += l;
} else if (l == 0) {
errno = ECONNRESET;
return -2;
} else if (l < 0) {
return -2;
}
}
} while (ret > 0);
return result;
}
void PrintChar(char chr) {
if (chr <= 32 || chr >= 127) {
printf("%2.2x ", chr);
} else {
printf(" %c ", chr);
}
}
int AsconConnectSuccess(int fd) {
fd_set wmask, rmask;
struct timeval tmo = {0,0};
int oldopts;
int ret;
oldopts = fcntl(fd, F_GETFL, 0);
assert(oldopts | O_NONBLOCK); /* fd must be in non-blocking mode */
FD_ZERO(&wmask);
FD_ZERO(&rmask);
FD_SET(fd, &wmask);
FD_SET(fd, &rmask);
ret = select(fd + 1, &rmask, &wmask, NULL, &tmo);
if (ret > 0) {
assert(FD_ISSET(fd, &wmask));
if (FD_ISSET(fd, &rmask)) { /* there may already be data for read */
if (recv(fd, NULL, 0, 0) < 0) { /* zero length, check only return value */
ret = ASCON_RECV_ERROR; /* first recv failed */
}
} else {
if (send(fd, NULL, 0, 0) < 0) { /* zero length, check only return value */
ret = ASCON_SEND_ERROR; /* first send failed */
}
}
}
fcntl(fd, F_SETFL, oldopts & ~ O_NONBLOCK); /* reset to blocking mode */
return ret;
}
int AsconReadChar(int fd, char *chr) {
fd_set rmask;
struct timeval tmo = {0,0};
int ret;
FD_ZERO(&rmask);
FD_SET(fd, &rmask);
ret = select(fd + 1, &rmask, NULL, NULL, &tmo);
if (ret <= 0) return ret;
ret = recv(fd, chr, 1, 0);
/* PrintChar(*chr); */
fflush(stdout);
if (ret > 0) return 1;
if (ret == 0) {
errno = ECONNRESET;
return ASCON_DISCONNECTED;
}
return ASCON_RECV_ERROR;
}
int AsconWriteChars(int fd, char *data, int length) {
fd_set wmask;
struct timeval tmo = {0,0};
int ret;
if (length <= 0) return 0;
/*
{ int i;
for (i=0; i<length; i++) {
PrintChar(data[i]);
}
}
printf("<written\n");
*/
FD_ZERO(&wmask);
FD_SET(fd, &wmask);
ret = select(fd + 1, NULL, &wmask, NULL, &tmo);
if (ret <= 0) return ASCON_SELECT_ERROR;
ret = send(fd, data, length, 0);
if (ret > 0) return ret;
if (ret == 0) {
errno = ECONNRESET;
return ASCON_DISCONNECTED;
}
return ASCON_SEND_ERROR;
}
static double lastCall = 0;
int AsconStdHandler(Ascon *a) {
int ret;
int l;
char chr;
double now = DoubleTime();
if (now > lastCall + 0.5) { /* AsconStdHandler was not called since a long time (0.5 sec) */
if (lastCall != 0) { /* extend timeout time (for debugging purposes) */
a->start += now - lastCall - 0.5;
}
}
lastCall = now;
switch (a->state) {
case AsconConnectStart:
AsconConnect(a);
break;
case AsconConnecting:
ret = AsconConnectSuccess(a->fd);
if (ret == 0) {
/* in progress */
} else if (ret > 0) {
a->state = AsconConnectDone; /* success */
} else if (ret < 0) {
AsconError(a, "AsconConnectSuccess failed:", errno);
}
break;
case AsconWriteStart:
DynStringConcat(a->wrBuffer, a->sendTerminator);
a->wrPos = 0;
a->state = AsconWriting;
if(strstr(GetCharArray(a->wrBuffer),"@@NOSEND@@") != NULL){
a->state = AsconWriteDone;
}
break;
case AsconWriting:
AsconReadGarbage(a->fd);
l = GetDynStringLength(a->wrBuffer) - a->wrPos;
ret = AsconWriteChars(a->fd, GetCharArray(a->wrBuffer) + a->wrPos, l);
if (ret < 0) {
AsconError(a, "send failed:", errno);
/*
* Ooops: which state shall we go to after a write fail?
* This seems to retry.
*/
} else {
a->wrPos += ret;
if (a->wrPos >= GetDynStringLength(a->wrBuffer)) {
a->state = AsconWriteDone;
}
}
break;
case AsconReadStart:
DynStringClear(a->rdBuffer);
a->start = DoubleTime();
a->state = AsconReading;
break;
case AsconReading:
ret = AsconReadChar(a->fd, &chr);
while (ret > 0) {
a->start = DoubleTime();
if (chr == '\n') {
if (a->readState) {
/* swallow LF after CR */
DynStringClear(a->rdBuffer);
a->readState = 0;
} else {
DynStringConcatChar(a->rdBuffer, '\0');
a->state = AsconReadDone;
break;
}
} else if (chr == '\r') {
a->readState = 1;
DynStringConcatChar(a->rdBuffer, '\0');
a->state = AsconReadDone;
break;
} else {
if (DynStringConcatChar(a->rdBuffer, chr) == 0) {
AsconError(a, "DynStringConcatChar failed:", ENOMEM);
break;
}
a->readState = 0;
}
ret = AsconReadChar(a->fd, &chr);
}
if (ret < 0) {
AsconError(a, "AsconReadChar failed:", errno);
return 1;
}
if (a->state == AsconReadDone) {
DynStringConcatChar(a->rdBuffer, '\0');
} else {
if (a->timeout > 0) {
if (DoubleTime() - a->start > a->timeout) {
AsconError(a, "read timeout", 0);
a->state = AsconTimeout;
}
}
}
break;
default:
return 1;
}
return 1;
}
/* define type AsconProtocolList and functions AsconProtocolAdd etc. */
#define MC_NAME(T) AsconProtocol##T
#include "mclist.c"
static AsconProtocolList protocols={0};
void AsconInsertProtocol(AsconProtocol *protocol) {
AsconProtocolAdd(&protocols, protocol);
}
AsconHandler AsconSetHandler(Ascon *a, SConnection *con,
int argc, char *argv[]) {
AsconProtocol *p;
if (argc < 1) return NULL;
if (strcasecmp(argv[0], "std") == 0) {
if (argc < 2) return NULL;
AsconStdInit(a, con, argc, argv);
return AsconStdHandler;
}
for (p = protocols.head; p!= NULL; p=p->next) {
if (strcasecmp(p->name, argv[0]) == 0) {
if(p->init(a, con, argc, argv)){
return p->handler;
} else {
return NULL;
}
}
}
return NULL;
}
/* --- implementation of higher level interface ---- */
char *ConcatArgs(int argc, char *argv[]) {
return Arg2Tcl(argc, argv, NULL, -1);
}
Ascon *AsconMake(SConnection *con, int argc, char *argv[]) {
Ascon *a;
char *args;
a = calloc(1, sizeof(*a));
if (a == NULL) {
SCWrite(con, "ERROR: no memory", eError);
return NULL;
}
a->handler = AsconSetHandler(a, con, argc, argv);
if (a->handler == NULL) {
args = ConcatArgs(argc, argv);
if (!args) return NULL;
SCPrintf(con, eError, "ERROR: illegal protocol: %s", args);
free(args);
return NULL;
}
a->rdBuffer = CreateDynString(60, 63);
a->wrBuffer = CreateDynString(60, 63);
a->errList = NULL;
a->responseValid = 0;
a->reconnectInterval = 10;
a->lastReconnect = 0;
return a;
}
void AsconKill(Ascon *a) {
if (a->fd > 0) {
close(a->fd);
}
DeleteDynString(a->rdBuffer);
DeleteDynString(a->wrBuffer);
if (a->hostport) {
free(a->hostport);
}
if(a->sendTerminator){
free(a->sendTerminator);
}
if(a->private != NULL && a->killPrivate != NULL){
a->killPrivate(a->private);
}
free(a);
}
AsconStatus AsconTask(Ascon *a) {
double now;
while (a->handler(a)) {
switch (a->state) {
case AsconReading:
case AsconWriting:
return AsconPending;
case AsconNotConnected:
return AsconOffline;
break;
case AsconConnectDone:
a->state = AsconIdle;
return AsconReady;
case AsconWriteDone:
if (a->noResponse) {
a->state = AsconIdle;
return AsconReady;
}
a->state = AsconReadStart;
break;
case AsconReadDone:
a->state = AsconIdle;
a->responseValid = 1;
return AsconReady;
case AsconConnecting:
return AsconUnconnected;
default:
switch (a->state % 4) {
case AsconOnTheWay:
case AsconStart:
return AsconPending;
case AsconFailed:
if (a->state == AsconTimeout) {
now = DoubleTime();
if (now > a->lastReconnect + a->reconnectInterval) {
a->lastReconnect = now;
close(a->fd);
a->fd = -1;
a->state = AsconConnectStart;
}
}
return AsconFailure;
case AsconFinished:
if (a->state < AsconConnectFailed) {
return AsconUnconnected;
}
return AsconReady;
}
}
}
return AsconIdle;
}
int AsconWrite(Ascon *a, char *command, int noResponse) {
if (a->state <= AsconConnectFailed || a->state % 4 < AsconFinished) return 0;
DynStringCopy(a->wrBuffer, command);
a->noResponse = noResponse;
a->state = AsconWriteStart;
a->responseValid = 0;
AsconTask(a);
return 1;
}
char *AsconRead(Ascon *a) {
if (a->noResponse) {
a->noResponse=0;
return NULL;
}
if (a->state % 4 == AsconFailed) {
a->state = AsconIdle;
return NULL;
}
if (a->responseValid) {
a->responseValid = 0;
return GetCharArray(a->rdBuffer);
}
return NULL;
}
ErrMsg *AsconGetErrList(Ascon *a) {
return a->errList;
}