Files
sics/callback.c
koennecke b3138f1197 - Added Sycamore protocol and command context to SICS
- Added sinfo to SICS
- Added driver for TCP/IP Astrium velocity selector
- Added driver for TCP/IP Astrium chopper controller


SKIPPED:
	psi/amor2t.c
	psi/amorstat.c
	psi/dornier2.c
	psi/ecb.c
	psi/el734hp.c
	psi/fowrite.c
	psi/libpsi.a
	psi/make_gen
	psi/nextrics.c
	psi/pardef.c
	psi/pimotor.c
	psi/pipiezo.c
	psi/polterwrite.c
	psi/psi.c
	psi/scontroller.c
	psi/serial.c
	psi/tasinit.c
	psi/tasscan.c
	psi/tcpdocho.c
	psi/tcpdornier.c
	psi/tricssupport.c
	psi/velodornier.c
2005-12-22 22:16:10 +00:00

380 lines
10 KiB
C

/*--------------------------------------------------------------------------
S I C S C A L L B A C K
Functions needed to deal with the SICSCallback interface. Description is
in file interface.h, interface.w and interface.w.
Mark Koennecke, Juli 1997
Added ScriptCallback, Mark Koennecke, June 2003
Copyright:
Labor fuer Neutronenstreuung
Paul Scherrer Institut
CH-5423 Villigen-PSI
The authors hereby grant permission to use, copy, modify, distribute,
and license this software and its documentation for any purpose, provided
that existing copyright notices are retained in all copies and that this
notice is included verbatim in any distributions. No written agreement,
license, or royalty fee is required for any of the authorized uses.
Modifications to this software may be copyrighted by their authors
and need not follow the licensing terms described here, provided that
the new terms are clearly indicated on the first page of each file where
they apply.
IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY
FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY
DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. THIS SOFTWARE
IS PROVIDED ON AN "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE
NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
MODIFICATIONS.
----------------------------------------------------------------------------*/
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <tcl.h>
#include "fortify.h"
#include "lld.h"
#include "sics.h"
#include "macro.h"
#include "splitter.h"
#define CALLBACK 17777
/*--------------------- The interface datastructure ---------------------*/
typedef struct __ICallBack {
int iID;
int iList;
} ICallBack;
/*-------------- The data stored for a single callback ------------------*/
typedef struct {
long iID;
SICSCallBack pFunc;
void *pUserData;
KillFuncIT pKill;
int iEvent;
commandContext comCon;
} CallBackItem, *pCallBackItem;
/*------------------------------------------------------------------------*/
static int CheckPointer(pICallBack self)
{
if(self == NULL) return 0;
if(self->iID != CALLBACK)
{
return 0;
}
return 1;
}
/*-------------------------------------------------------------------------*/
pICallBack CreateCallBackInterface(void)
{
pICallBack pNew = NULL;
pNew = (pICallBack)malloc(sizeof(ICallBack));
if(!pNew)
{
return 0;
}
pNew->iID = CALLBACK;
pNew->iList = LLDcreate(sizeof(CallBackItem));
if(pNew->iList < 0)
{
free(pNew);
return NULL;
}
return pNew;
}
/*--------------------------------------------------------------------------*/
void DeleteCallBackInterface(pICallBack self)
{
int iRet;
CallBackItem sItem;
if(!CheckPointer(self))
{
return;
}
/* kill all userdata associated with callbacks */
iRet = LLDnodePtr2First(self->iList);
while(iRet != 0)
{
LLDnodeDataTo(self->iList,&sItem);
if(sItem.pKill != NULL)
{
sItem.pKill(sItem.pUserData);
}
iRet = LLDnodePtr2Next(self->iList);
}
LLDdelete(self->iList);
free(self);
}
/*--------------------------------------------------------------------------*/
int InvokeCallBack(pICallBack self, int iEvent, void *pEventData)
{
CallBackItem sItem;
int iCurrent, iRet;
int iResult = 1;
if(!CheckPointer(self))
{
return 0;
}
iCurrent = LLDnodePtr2First(self->iList);
while(iCurrent != 0)
{
LLDnodeDataTo(self->iList,&sItem);
if(sItem.iEvent == iEvent)
{
iRet = sItem.pFunc(iEvent, pEventData,sItem.pUserData,sItem.comCon);
if(!iRet)
{
iResult = 0;
}
}
iCurrent = LLDnodePtr2Next(self->iList);
}
return iResult;
}
/*--------------------------------------------------------------------------*/
static long lCount = 1L;
long RegisterCallback(pICallBack self, commandContext comCon, int iEvent,
SICSCallBack pFunc,
void *pUserData, KillFunc pKFunc)
{
CallBackItem sItem;
if(!CheckPointer(self))
{
return 0;
}
sItem.iID = lCount++;
assert(pFunc);
sItem.pFunc = pFunc;
sItem.iEvent = iEvent;
sItem.pUserData = pUserData;
sItem.pKill = pKFunc;
sItem.comCon = comCon;
LLDnodeAppendFrom(self->iList,&sItem);
return sItem.iID;
}
/*-------------------------------------------------------------------------*/
int RemoveCallback(pICallBack self, long lID)
{
CallBackItem sItem;
int iCurrent;
if(!CheckPointer(self))
{
return 0;
}
iCurrent = LLDnodePtr2First(self->iList);
while(iCurrent != 0)
{
LLDnodeDataTo(self->iList,&sItem);
if(sItem.iID == lID)
{
if(sItem.pKill != NULL)
{
sItem.pKill(sItem.pUserData);
}
LLDnodeDelete(self->iList);
return 1;
}
iCurrent = LLDnodePtr2Next(self->iList);
}
return 0;
}
/*--------------------------------------------------------------------------*/
int RemoveCallback2(pICallBack self, void *pUserData)
{
CallBackItem sItem;
int iCurrent;
if(!CheckPointer(self))
{
return 0;
}
iCurrent = LLDnodePtr2First(self->iList);
while(iCurrent != 0)
{
LLDnodeDataTo(self->iList,&sItem);
if(sItem.pUserData == pUserData)
{
if(sItem.pKill != NULL)
{
sItem.pKill(sItem.pUserData);
}
LLDnodeDelete(self->iList);
}
iCurrent = LLDnodePtr2Next(self->iList);
}
return 1;
}
/*-------------------------------------------------------------------
a write function for the connection which writes to stdout
-------------------------------------------------------------------*/
static int CallbackWrite(SConnection *pCon,char *message, int outCode)
{
if(outCode >= eWarning)
{
fputs(message,stdout);
fputs("\n",stdout);
}
return 1;
}
/*-----------------------------------------------------------------------
the actual callback function invoking the script
------------------------------------------------------------------------*/
static int ScriptCallback(int iEvent, void *pEventData, void *pUserData,
commandContext cc)
{
SConnection *pCon = NULL;
Tcl_Interp *pTcl;
int status;
pCon = SCCreateDummyConnection(pServ->pSics);
if(!pCon)
{
fprintf(stdout,"ERROR: failed to create dummy connection\n");
return 0;
}
if(pUserData == NULL)
{
fprintf(stdout,"ERROR: ScriptCallback: no script to execute\n");
return 0;
}
/*
SCSetWriteFunc(pCon,CallbackWrite);
MacroPush(pCon);
pTcl = InterpGetTcl(pServ->pSics);
status = Tcl_Eval(pTcl,(char *)pUserData);
if(status != TCL_OK)
{
fprintf(stdout,"ERROR: in CallbackScript: %s\n",(char *)pUserData);
fprintf(stdout,"Tcl-error: %s\n",pTcl->result);
}
MacroPop();
*/
SCSetRights(pCon,usInternal);
status = InterpExecute(pServ->pSics,pCon,(char *)pUserData);
SCDeleteConnection(pCon);
return 1;
}
/*------------------------------------------------------------------------*/
int CallbackScript(SConnection *pCon, SicsInterp *pSics, void *pData,
int argc, char *argv[])
{
long lID;
int iEvent, status;
pICallBack pCall = NULL;
CommandList *pCom = NULL;
char pBuffer[132];
if(argc < 2)
{
SCWrite(pCon,"ERROR: insufficient number of arguments to callbackScript",
eError);
return 0;
}
/*
only managers may do this
*/
if(!SCMatchRights(pCon,usMugger))
{
return 0;
}
strtolower(argv[1]);
if(strcmp(argv[1],"connect") == 0)
{
if(argc < 4)
{
SCWrite(pCon,"ERROR: not enough arguments to CallbackScript connect",
eError);
return 0;
}
strtolower(argv[2]);
pCom = FindCommand(pSics,argv[2]);
if(!pCom)
{
SCWrite(pCon,"ERROR: object to connect to not found",eError);
return 0;
}
pCall = GetCallbackInterface(pCom->pData);
if(!pCall)
{
SCWrite(pCon,"ERROR: object has no callback interface",eError);
return 0;
}
iEvent = Text2Event(argv[3]);
if(iEvent < 0)
{
SCWrite(pCon,"ERROR: event type not known",eError);
return 0;
}
Arg2Text(argc-4,&argv[4],pBuffer,131);
lID = RegisterCallback(pCall,SCGetContext(pCon),
iEvent,ScriptCallback,
strdup(pBuffer),free);
sprintf(pBuffer,"callback = %ld", lID);
SCWrite(pCon,pBuffer,eValue);
return 1;
}
else if(strcmp(argv[1],"remove") == 0)
{
if(argc < 4)
{
SCWrite(pCon,"ERROR: not enough arguments to CallbackScript remove",
eError);
return 0;
}
strtolower(argv[2]);
pCom = FindCommand(pSics,argv[2]);
if(!pCom)
{
SCWrite(pCon,"ERROR: object to remove to not found",eError);
return 0;
}
pCall = GetCallbackInterface(pCom->pData);
if(!pCall)
{
SCWrite(pCon,"ERROR: object has no callback interface",eError);
return 0;
}
status = Tcl_GetInt(InterpGetTcl(pSics),argv[3],&iEvent);
if(status != TCL_OK)
{
SCWrite(pCon,"ERROR: failed to convert callback ID to int",eError);
return 0;
}
RemoveCallback(pCall,(long)iEvent);
SCSendOK(pCon);
return 1;
}
SCWrite(pCon,"ERROR: subcommand to CallbackScript not known",eError);
return 0;
}