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
246 lines
6.4 KiB
C
246 lines
6.4 KiB
C
/**
|
|
* This is an asynchronous protocol implementation for HTTP.
|
|
* It includes special features to store binary data coming
|
|
* from a SINQ http histogram memory in a sinqdata object.
|
|
* Which has to be specified on initialisation.
|
|
*
|
|
* copyright: see file COPYRIGHT
|
|
*
|
|
* Mark Koennecke, June 2008
|
|
*/
|
|
#include <ascon.h>
|
|
#include <ascon.i>
|
|
#include <ghttp.h>
|
|
#include <sicsdata.h>
|
|
#include <HistMem.h>
|
|
/*---------------------------------------------------------------------*/
|
|
typedef struct {
|
|
ghttp_request *request;
|
|
char *userName;
|
|
char *password;
|
|
pSICSData binData;
|
|
}HttpProt, *pHttpProt;
|
|
/*---------------------------------------------------------------------*/
|
|
static int configRequest(Ascon *a){
|
|
pHttpProt pHttp = (pHttpProt)a->private;
|
|
pDynString request;
|
|
char *uri = NULL;
|
|
|
|
request = CreateDynString(64,64);
|
|
if(request == NULL){
|
|
AsconError(a,"Out of memory", 122);
|
|
return 0;
|
|
}
|
|
DynStringConcat(request,"http://");
|
|
DynStringConcat(request,a->hostport);
|
|
DynStringConcatChar(request,'/');
|
|
DynStringConcat(request,GetCharArray(a->wrBuffer));
|
|
uri = GetCharArray(request);
|
|
|
|
ghttp_clean(pHttp->request);
|
|
ghttp_set_type(pHttp->request,ghttp_type_get);
|
|
ghttp_set_header(pHttp->request,"connection", "keep-alive");
|
|
if(ghttp_set_uri(pHttp->request,uri) < 0){
|
|
AsconError(a,"Bad URL", a->state);
|
|
return 0;
|
|
}
|
|
if(pHttp->userName != NULL && pHttp->password != NULL){
|
|
ghttp_set_authinfo(pHttp->request,
|
|
pHttp->userName, pHttp->password);
|
|
}
|
|
ghttp_set_sync(pHttp->request,ghttp_async);
|
|
ghttp_prepare(pHttp->request);
|
|
DeleteDynString(request);
|
|
|
|
return 1;
|
|
}
|
|
/*---------------------------------------------------------------------*/
|
|
static void handleReply(Ascon *a){
|
|
char *pPtr = NULL, *pType = NULL;
|
|
int len, i, *dataPtr = NULL;
|
|
HistInt *hmData = NULL;
|
|
pHttpProt pHttp = (pHttpProt)a->private;
|
|
|
|
pPtr = ghttp_get_body(pHttp->request);
|
|
len = ghttp_get_body_len(pHttp->request);
|
|
if(strstr(pPtr,"ERROR") != NULL){
|
|
AsconError(a,pPtr, a->state);
|
|
DynStringConcat(a->rdBuffer,pPtr);
|
|
} else if(strstr(pPtr,"Authentication Error") != NULL){
|
|
DynStringConcat(a->rdBuffer,pPtr);
|
|
AsconError(a,pPtr, a->state);
|
|
} else {
|
|
pType = (char *)ghttp_get_header(pHttp->request,"Content-Type");
|
|
if(strstr(pType,"sinqhm") == NULL){
|
|
/* text data */
|
|
for(i = 0; i < len; i++){
|
|
DynStringConcatChar(a->rdBuffer, pPtr[i]);
|
|
}
|
|
} else {
|
|
hmData = (HistInt *)pPtr;
|
|
clearSICSData(pHttp->binData);
|
|
len = len/sizeof(HistInt);
|
|
dataPtr = getSICSDataPointer(pHttp->binData, 0, len);
|
|
for(i = 0; i < len; i++){
|
|
dataPtr[i] = htonl(hmData[i]);
|
|
}
|
|
assignSICSType(pHttp->binData, 0, len, INTTYPE);
|
|
DynStringCopy(a->rdBuffer,"SICSDATA");
|
|
}
|
|
}
|
|
}
|
|
/*---------------------------------------------------------------------*/
|
|
static int HttpHandler(Ascon *a) {
|
|
ghttp_status status;
|
|
pHttpProt pHttp = (pHttpProt)a->private;
|
|
int socke, selStat;
|
|
fd_set rmask;
|
|
struct timeval tmo = {0,0};
|
|
|
|
switch (a->state) {
|
|
case AsconConnectStart:
|
|
a->state = AsconConnecting;
|
|
break;
|
|
case AsconConnecting:
|
|
a->state = AsconConnectDone; /* success */
|
|
break;
|
|
case AsconWriteStart:
|
|
if(configRequest(a)){
|
|
a->state = AsconWriting;
|
|
}
|
|
break;
|
|
case AsconWriting:
|
|
status = ghttp_process(pHttp->request);
|
|
if(status == ghttp_error){
|
|
ghttp_close(pHttp->request);
|
|
configRequest(a);
|
|
status = ghttp_process(pHttp->request);
|
|
}
|
|
if(status == ghttp_error){
|
|
AsconError(a,"Server error", 123);
|
|
DynStringConcat(a->rdBuffer,"Server error");
|
|
a->state = AsconReadDone;
|
|
} else {
|
|
a->state = AsconWriteDone;
|
|
}
|
|
a->start = DoubleTime();
|
|
DynStringClear(a->rdBuffer);
|
|
break;
|
|
case AsconReadStart:
|
|
socke = ghttp_get_socket(pHttp->request);
|
|
FD_ZERO(&rmask);
|
|
FD_SET(socke,&rmask);
|
|
selStat = select(socke+1,&rmask, NULL, NULL, &tmo);
|
|
if(selStat != 0){
|
|
status = ghttp_process(pHttp->request);
|
|
a->state = AsconReading;
|
|
} else {
|
|
if(DoubleTime() > a->start + a->timeout){
|
|
AsconError(a," read timeout", 0);
|
|
a->state = AsconTimeout;
|
|
/* this to clear the line */
|
|
ghttp_close(pHttp->request);
|
|
}
|
|
}
|
|
return 1;
|
|
break;
|
|
case AsconReading:
|
|
status = ghttp_process(pHttp->request);
|
|
switch(status){
|
|
case ghttp_not_done:
|
|
if(DoubleTime() > a->start + a->timeout){
|
|
AsconError(a," read timeout", 0);
|
|
a->state = AsconTimeout;
|
|
/* this to clear the line */
|
|
ghttp_close(pHttp->request);
|
|
}
|
|
return 1;
|
|
case ghttp_done:
|
|
handleReply(a);
|
|
a->state = AsconReadDone;
|
|
break;
|
|
case ghttp_error:
|
|
/*
|
|
* A first error may not be an error but a
|
|
* reconnect
|
|
*/
|
|
ghttp_close(pHttp->request);
|
|
configRequest(a);
|
|
status = ghttp_process(pHttp->request);
|
|
if(status == ghttp_error){
|
|
AsconError(a,"Server error", a->state);
|
|
DynStringConcat(a->rdBuffer,"Server error");
|
|
a->state = AsconReadDone;
|
|
}
|
|
return 1;
|
|
break;
|
|
}
|
|
break;
|
|
default:
|
|
return AsconStdHandler(a);
|
|
}
|
|
return 1;
|
|
}
|
|
/*------------------------------------------------------------------------*/
|
|
static void killHttp(void *data){
|
|
pHttpProt prot = (pHttpProt)data;
|
|
if(prot == NULL){
|
|
return;
|
|
}
|
|
if(prot->request != NULL){
|
|
ghttp_close(prot->request);
|
|
}
|
|
if(prot->password != NULL){
|
|
free(prot->password);
|
|
}
|
|
if(prot->userName != NULL){
|
|
free(prot->userName);
|
|
}
|
|
free(prot);
|
|
}
|
|
/*------------------------------------------------------------------------*/
|
|
static int HttpProtInit(Ascon *a, SConnection *con,
|
|
int argc, char *argv[]){
|
|
pHttpProt pHttp = NULL;
|
|
|
|
pHttp = calloc(sizeof(HttpProt), 1);
|
|
if(pHttp == NULL){
|
|
SCWrite(con,"ERROR: out of memory in HttpProtInit", eError);
|
|
return 0;
|
|
}
|
|
if(argc < 3){
|
|
return 0;
|
|
}
|
|
a->hostport = strdup(argv[1]);
|
|
pHttp->binData = (pSICSData)FindCommandData(pServ->pSics,
|
|
argv[2], "SICSData");
|
|
if(pHttp->binData == NULL){
|
|
SCWrite(con,"ERROR: SICSData objct not found", eError);
|
|
return 0;
|
|
}
|
|
if(argc > 3){
|
|
a->timeout = atof(argv[3]);
|
|
} else {
|
|
a->timeout = 10.;
|
|
}
|
|
if(argc > 5){
|
|
pHttp->userName = strdup(argv[4]);
|
|
pHttp->password = strdup(argv[5]);
|
|
}
|
|
pHttp->request = ghttp_request_new();
|
|
a->private = pHttp;
|
|
a->killPrivate = killHttp;
|
|
a->state = AsconConnectStart;
|
|
return 1;
|
|
}
|
|
/*-------------------------------------------------------------------------*/
|
|
void AddHttpProtocoll(){
|
|
AsconProtocol *prot = NULL;
|
|
|
|
prot = calloc(sizeof(AsconProtocol), 1);
|
|
prot->name = strdup("sinqhttp");
|
|
prot->init = HttpProtInit;
|
|
prot->handler = HttpHandler;
|
|
AsconInsertProtocol(prot);
|
|
}
|