Files
sics/asyncprotocol.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

359 lines
9.6 KiB
C

#include <sics.h>
#include <asyncprotocol.h>
#include <asyncqueue.h>
int defaultSendCommand(pAsyncProtocol p, pAsyncTxn txn) {
int i, iRet;
int state;
const char *term = "\r\n";
if (p->sendTerminator)
term = p->sendTerminator;
state = 0;
for (i = 0; i < txn->out_len; ++i) {
if (txn->out_buf[i] == 0x00) { /* end of transmission */
break;
}
else if (txn->out_buf[i] == term[state]) {
++state;
continue;
}
state = 0;
}
txn->txn_state = 0;
iRet = AsyncUnitWrite(txn->unit, txn->out_buf, txn->out_len);
if (iRet <= 0)
return iRet;
if (term[state] != 0)
iRet = AsyncUnitWrite(txn->unit, (void *)term, strlen(term));
return iRet;
}
int defaultHandleInput(pAsyncProtocol p, pAsyncTxn txn, int ch) {
const char *term = "\r\n";
if (txn->txn_state == 0) {
int i;
for (i = 0; i < 10; ++i)
if (p->replyTerminator[i] && ch == p->replyTerminator[i][0]) {
txn->txn_state = i << 16;
break;
}
}
term = p->replyTerminator[txn->txn_state >> 16];
if (ch == term[txn->txn_state & 0xffff])
++txn->txn_state;
else
txn->txn_state = 0;
if (txn->inp_idx < txn->inp_len)
txn->inp_buf[txn->inp_idx++] = ch;
if (term[txn->txn_state & 0xffff] == 0) {
if (txn->inp_idx < txn->inp_len)
txn->inp_buf[txn->inp_idx] = '\0';
return AQU_POP_CMD;
}
return 1;
}
int defaultHandleEvent(pAsyncProtocol p, pAsyncTxn txn, int event) {
/* TODO: what could or should we do to handle the event */
return AQU_POP_CMD;
}
int defaultPrepareTxn(pAsyncProtocol p, pAsyncTxn txn, const char* cmd, int cmd_len, int rsp_len) {
int i;
int state;
const char *term = "\r\n";
if (p->sendTerminator)
term = p->sendTerminator;
state = 0;
for (i = 0; i < cmd_len; ++i) {
if (cmd[i] == term[state]) {
++state;
continue;
}
state = 0;
}
if (term[state] == 0) {
/* outgoing command is correctly terminated */
txn->out_buf = malloc(cmd_len + 1);
if (txn->out_buf == NULL) {
SICSLogWrite("Out of memory in AsyncProtocol::defaultPrepareTxn", eError);
return 0;
}
memcpy(txn->out_buf, cmd, cmd_len + 1);
}
else {
/* outgoing command is NOT correctly terminated */
int tlen = strlen(term);
txn->out_buf = malloc(cmd_len + tlen + 1);
if (txn->out_buf == NULL) {
SICSLogWrite("Out of memory in AsyncProtocol::defaultPrepareTxn", eError);
return 0;
}
memcpy(txn->out_buf, cmd, cmd_len);
memcpy(txn->out_buf + cmd_len, term, tlen + 1);
cmd_len += tlen;
}
txn->out_len = cmd_len;
txn->out_idx = 0;
if(txn->inp_buf != NULL){
free(txn->inp_buf);
}
txn->inp_len = rsp_len;
txn->inp_idx = 0;
txn->txn_state = 0;
txn->txn_status = 0;
return 1;
}
static const char* hex = "0123456789ABCDEF";
/*--------------------------------------------------------------------*/
static void encodeTerminator(char *result, char *terminator)
{
if (terminator)
while (*terminator) {
if (*terminator <= 32 || *terminator >= 127) {
*result++ = '0';
*result++ = 'x';
*result++ = hex[(*terminator >> 4) &0xF];
*result++ = hex[(*terminator) &0xF];
++terminator;
}
else {
*result++ = *terminator++;
}
}
*result = '\0';
return;
}
static int fromHex(const char* code) {
int icode = -1;
int result = -1;
if (code[0] == '0' && (code[1] == 'x' || code[1] == 'X')) {
if (code[2] >= '0' && code[2] <= '9')
icode = (code[2] - '0');
else if (code[2] >= 'a' && code[2] <= 'f')
icode = 10 + (code[2] - 'a');
else if (code[2] >= 'A' && code[2] <= 'F')
icode = 10 + (code[2] - 'A');
if (icode < 0)
return -1;
result = icode << 4;
icode = -1;
if (code[3] >= '0' && code[3] <= '9')
icode = (code[3] - '0');
else if (code[3] >= 'a' && code[3] <= 'f')
icode = 10 + (code[3] - 'a');
else if (code[3] >= 'A' && code[3] <= 'F')
icode = 10 + (code[3] - 'A');
if (icode < 0)
return -1;
result |= icode;
return result;
}
return -1;
}
/*--------------------------------------------------------------------*/
static char *decodeTerminator(char *code)
{
int count = 0, icode;
char *pResult;
char* pCh;
char* pQt = NULL; /* pointer to quote character if found */
if (code == NULL)
return NULL;
count = strlen(code);
pResult = (char *) malloc(count + 1);
if (!pResult) {
SICSLogWrite("Out of memory in AsyncProtocol::decodeTerminator", eError);
return NULL;
}
memset(pResult, 0, count + 1);
pCh = pResult;
if (*code == '\'' || *code == '"') /* check for leading quote */
pQt = code++;
while (*code) {
if (pQt && *code == *pQt) /* check for trailing quote */
break;
if (code[0] == '\\' && code[1] == 'r') { /* CR */
*pCh++ = '\r';
code += 2;
}
else if (code[0] == '\\' && code[1] == 'n') { /* LF */
*pCh++ = '\n';
code += 2;
}
else if ((icode = fromHex(code)) >= 0) { /* Hex: 0xFF */
*pCh++ = icode;
code += 4;
}
else /* literal */
*pCh++ = *code++;
}
*pCh = '\0';
return pResult;
}
int AsyncProtocolNoAction(SConnection *pCon, SicsInterp *pSics,
void *pData, int argc, char *argv[])
{
char line[132];
snprintf(line, 132, "%s does not understand %s", argv[0], argv[1]);
SCWrite(pCon, line, eError);
return 0;
}
int AsyncProtocolAction(SConnection *pCon, SicsInterp *pSics,
void *pData, int argc, char *argv[])
{
pAsyncProtocol self = (pAsyncProtocol) pData;
if (argc > 1) {
/* handle genecic parameters like terminators */
if (strcasecmp(argv[1], "sendterminator") == 0) {
if (argc > 2) {
char* pPtr = decodeTerminator(argv[2]);
if (pPtr) {
if (self->sendTerminator)
free(self->sendTerminator);
self->sendTerminator = pPtr;
}
SCSendOK(pCon);
}
else
{
char term[132];
char line[1024];
encodeTerminator(term, self->sendTerminator);
sprintf(line, "%s.sendTerminator = \"%s\"", argv[0], term);
SCWrite(pCon, line, eValue);
}
return 1;
}
else if (strcasecmp(argv[1], "replyterminator") == 0) {
if (argc > 2) {
int i;
for (i = 0; i < 10; ++i)
if (self->replyTerminator[i]) {
free(self->replyTerminator[i]);
self->replyTerminator[i] = NULL;
}
for (i = 0; i < 10 && i < argc - 2; ++i) {
char* pPtr = decodeTerminator(argv[i + 2]);
if (pPtr) {
self->replyTerminator[i] = pPtr;
}
}
SCSendOK(pCon);
}
else
{
int i;
char term[132];
char line[1024];
term[0] = '\0';
sprintf(line, "%s.replyTerminator =", argv[0]);
for (i = 0; i < 10; ++i) {
if (self->replyTerminator[i] == NULL)
break;
term[0] = ' ';
term[1] = '"';
encodeTerminator(&term[2], self->replyTerminator[i]);
strcat(term, "\"");
strcat(line, term);
}
SCWrite(pCon, line, eValue);
}
return 1;
}
}
else if (strcasecmp(argv[1], "list") == 0) {
int ac = 2;
char* av[3] = { argv[0], 0, 0 };
av[1] = "sendterminator";
AsyncProtocolAction(pCon, pSics, pData, ac, av);
av[1] = "replyterminator";
AsyncProtocolAction(pCon, pSics, pData, ac, av);
return 1;
}
/* handle any other actions here */
return AsyncProtocolNoAction(pCon, pSics, pData, argc,argv);
}
void defaultKillPrivate(pAsyncProtocol p) {
if (p->privateData) {
/* TODO: should we do anything? */
free(p->privateData);
}
}
void AsyncProtocolKill(void *pData) {
pAsyncProtocol self = (pAsyncProtocol) pData;
int i;
if(self->pDes)
DeleteDescriptor(self->pDes);
if(self->sendTerminator != NULL)
free(self->sendTerminator);
for (i = 0; i < 10; ++i)
if(self->replyTerminator[i] != NULL)
free(self->replyTerminator[i]);
if (self->killPrivate)
self->killPrivate(self);
}
pAsyncProtocol AsyncProtocolCreate(SicsInterp *pSics, const char* protocolName,
ObjectFunc pFunc, KillFunc pKFunc) {
int iRet;
pAsyncProtocol self = NULL;
/* try to find an existing queue with this name */
self = (pAsyncProtocol) FindCommandData(pServ->pSics, (char *)protocolName, "AsyncProtocol");
if (self != NULL) {
return self;
}
self = (pAsyncProtocol) malloc(sizeof(AsyncProtocol));
if (self == NULL) {
SICSLogWrite("Out of memory in AsyncProtocolCreate", eError);
return NULL;
}
memset(self, 0, sizeof(AsyncProtocol));
self->pDes = CreateDescriptor("AsyncProtocol");
if (pFunc == NULL)
pFunc = AsyncProtocolNoAction;
if (pKFunc == NULL)
pKFunc = AsyncProtocolKill;
iRet = AddCommand(pSics, (char *)protocolName, pFunc, pKFunc, self);
if (!iRet ) {
SICSLogWrite("AddCommand failed in AsyncProtocolCreate", eError);
AsyncProtocolKill(self);
return NULL;
}
self->sendCommand = defaultSendCommand;
self->handleInput = defaultHandleInput;
self->handleEvent = defaultHandleEvent;
self->prepareTxn = defaultPrepareTxn;
self->killPrivate = defaultKillPrivate;
self->sendTerminator = strdup("\r\n");
self->replyTerminator[0] = strdup("\r\n");
return self;
}
int AsyncProtocolFactory(SConnection *pCon, SicsInterp *pSics,
void *pData, int argc, char *argv[]) {
if (argc < 2) {
SCWrite(pCon,"ERROR: insufficient arguments to AsyncProtocolFactory", eError);
return 0;
}
pAsyncProtocol pNew = AsyncProtocolCreate(pSics, argv[1],
AsyncProtocolAction, AsyncProtocolKill);
/* handle any extra arguments here */
pNew->privateData = NULL;
return 1;
}