/* * functionUtil.c * * * Created from Stephen Norum's mpsLinkProcessor * Copyright 2007 Stanford Linear Accelerator Center. All rights reserved. * */ #include "debugPrint.h" #ifdef DEBUG_PRINT int functionUtilFlag = DP_DEBUG; #include #endif #include /* epics includes */ #include #include #include #include /*#include #include #include */ #include #include #include #include "ffGlob.h" #include "ffStructs.h" #include "functionTypes.h" #include "functionUtil.h" #include "ffTypes.h" LOCAL int functionUtilInitFreeList (void); LOCAL void initInitFunctions(void); LOCAL void initIterateFunctions(void); LOCAL void initReconfigFunctions(void); static epicsMutexId functionListMutex; static ELLLIST functionList; /* Global variables for function list; declared extern in functionTypes.h */ void * functionFreeList_p = NULL; /* pointer to functionElement_ts free list */ int initializeFunctions() { int status = OKOK; /* Initialize function mutex and list */ functionListMutex = epicsMutexMustCreate(); epicsMutexLock(functionListMutex); ellInit(&functionList); epicsMutexUnlock(functionListMutex); /* Initialize functionData free list */ status = functionUtilInitFreeList(); initInitFunctions(); initIterateFunctions(); initReconfigFunctions(); return status; } /* ============================================================================= Name: functionUtilInitFreeList Abs: Initializes free list Args: None Rem: Side: Utilizies epics libCom freeListLib Ret: void ==============================================================================*/ LOCAL int functionUtilInitFreeList (void) { int status = OKOK; if (functionFreeList_p == NULL) { freeListInitPvt(&functionFreeList_p, sizeof(functionElement_ts),MAX_FUNCTIONS); if (functionFreeList_p == NULL) { DEBUGPRINT(DP_ERROR,functionUtilFlag, ("functionUtilInitFreeList: freeList ptr = %p\n", functionFreeList_p)); status = NOT_OK; } } return status; } /* Must lock functionListMutex before calling this function */ functionElement_ts * findFunctionByName ( const char *funcName) { functionElement_ts *fd; fd = (functionElement_ts *) ellFirst(&functionList); while (fd != NULL) { if (strcmp( (const char *)(fd->functionName_ca), (const char *)funcName) == 0) { /* Supplied data matches node */ DEBUGPRINT(DP_DEBUG,functionUtilFlag, ("functionUtil: found function name %s in array...\n", (char * ) funcName)); break; } fd = (functionElement_ts *) ellNext((ELLNODE *) fd); } return fd; } void printFunctionInfo() { functionElement_ts *fd; epicsMutexLock(functionListMutex); fd = (functionElement_ts *) ellFirst(&functionList); printf("Number of functions loaded: %d\n\n", ellCount(&functionList)); while(fd != NULL) { printf("Function: %s (%p)\n", fd->functionName_ca, fd->function); fd = (functionElement_ts *) ellNext((ELLNODE *) fd); } epicsMutexUnlock(functionListMutex); } int addFunctionToList(functionElement_ts *newFd_ps) { int err; functionElement_ts *fd; if (newFd_ps == NULL) { return NOT_OK; } /* See if function object is a loadable version */ err = NOT_OK; /* See if the function is already loaded */ epicsMutexLock(functionListMutex); fd = findFunctionByName(newFd_ps->functionName_ca); DEBUGPRINT(DP_DEBUG, functionUtilFlag, ("functionUtil: addFunctionToList call with newFd_ps->functionName = %s\n", (char *)newFd_ps->functionName_ca )); if (fd == NULL) { /* Add the object to the list */ ellAdd(&functionList, (ELLNODE *) newFd_ps); err = OKOK; DEBUGPRINT(DP_DEBUG, functionUtilFlag, ("functionUtil: Adding %s to functionList\n", (char *)newFd_ps->functionName_ca )); } else { /* Already in list */ errlogPrintf("Error: addFunctionToList cannot reload existing function."); err = NOT_OK; } epicsMutexUnlock(functionListMutex); return err; } void functionFreeListCleanup (void) { void * temp_p; if (functionFreeList_p) { temp_p = functionFreeList_p; functionFreeList_p = NULL; freeListCleanup(functionFreeList_p); } else DEBUGPRINT(DP_ERROR,functionUtilFlag,("functionFreeListCleanup failure for functionFreeList_p\n")); } /*============================================================================= Name: putFunctionsToPV Abs: put the function name into the functions waveform PV Args: Rem: Side: Ret: =============================================================================*/ LOCAL void putFunctionsToPV(char *pvName, char *functionNames ) { DBADDR dbAddr; long nRequest = MAX_WAVEFORM_FUNCTIONS; /* always write all entries in waveform */ if (dbNameToAddr(pvName, &dbAddr)) { epicsPrintf ("functionUtil: dbNameToAddr of %s failed\n",pvName ); } else { DEBUGPRINT(DP_DEBUG, functionUtilFlag, ("functionUtil: dbNameToAddr of %s successful\n", pvName )); } /* put the function name into the PV */ if (dbPutField(&dbAddr, DBR_STRING, functionNames, nRequest)) { epicsPrintf ("functionUtil: Error for dbPutField of function names [first -> %s]", functionNames); } else { DEBUGPRINT(DP_DEBUG, functionUtilFlag, ("functionUtil: Successful dbPutField of %ld strings; %s[0] = %s\n", nRequest, pvName, functionNames)); } } /*============================================================================= Name: initInitFuncs Abs: Init all callable, configurable, function pointers. Args: Rem: places names of, and pointers to, the built-in configurable functions into a linked-list of names and a PV waveform of strings Side: Ret: =============================================================================*/ LOCAL void initInitFunctions(void) { /* Place init INITFUNC functions here */ functionElement_ts * func_ps = NULL; char pvName_ca[MAX_CHAR_EPICS_NAME]; char funcNames_ca[10][MAX_FUNCNAME_SIZE]; long fnum = 0; memset(funcNames_ca, 0, sizeof(funcNames_ca)); /* insert for each new init function */ func_ps = freeListCalloc (functionFreeList_p); if (!func_ps){ DEBUGPRINT(DP_ERROR, functionUtilFlag, ("initInitFuncs error getting functionElement_ts mem from functionFreeList_p!")); } else { strcpy(func_ps->functionName_ca, "transInit1"); func_ps->function = transInit1; /* insert into function linked list */ addFunctionToList(func_ps); /* add name to funcName array */ strcpy(funcNames_ca[fnum],func_ps->functionName_ca); fnum++; } /* insert for each new init function */ func_ps = freeListCalloc (functionFreeList_p); if (!func_ps){ DEBUGPRINT(DP_ERROR, functionUtilFlag, (" initInitFuncs error getting functionElement_ts mem from functionFreeList_p!")); } else { strcpy(func_ps->functionName_ca, "transInit2"); func_ps->function = transInit2; /* insert into function linked list via functionUtil */ addFunctionToList(func_ps); /* add name to funcName array */ strcpy(funcNames_ca[fnum],func_ps->functionName_ca); fnum++; } /* insert for each new init function */ func_ps = freeListCalloc (functionFreeList_p); if (!func_ps){ DEBUGPRINT(DP_ERROR, functionUtilFlag, (" initInitFuncs error getting functionElement_ts mem from functionFreeList_p!")); } else { strcpy(func_ps->functionName_ca, "transInit3"); func_ps->function = transInit3; /* insert into function linked list via functionUtil */ addFunctionToList(func_ps); /* add name to funcName array */ strcpy(funcNames_ca[fnum],func_ps->functionName_ca); fnum++; } /* insert for each new init function */ func_ps = freeListCalloc (functionFreeList_p); if (!func_ps){ DEBUGPRINT(DP_ERROR, functionUtilFlag, (" initInitFuncs error getting functionElement_ts mem from functionFreeList_p!")); } else { strcpy(func_ps->functionName_ca, "transInit4"); func_ps->function = transInit4; /* insert into function linked list via functionUtil */ addFunctionToList(func_ps); /* add name to funcName array */ strcpy(funcNames_ca[fnum],func_ps->functionName_ca); fnum++; } /* insert for each new init function */ func_ps = freeListCalloc (functionFreeList_p); if (!func_ps){ DEBUGPRINT(DP_ERROR, functionUtilFlag, (" initInitFuncs error getting functionElement_ts mem from functionFreeList_p!")); } else { strcpy(func_ps->functionName_ca, "defaultInit"); func_ps->function = defaultInit; /* insert into function linked list via functionUtil */ addFunctionToList(func_ps); /* add name to funcName array */ strcpy(funcNames_ca[fnum],func_ps->functionName_ca); fnum++; } /* get the function names waveform PV and write the function names into the PV */ if (fnum>0) { sprintf (pvName_ca, "IOC:SYS0:%s:INITFUNCS", ffGetLOCAName() ); putFunctionsToPV((char *)&pvName_ca, (char *)&(funcNames_ca)); } } /*============================================================================= Name: transInit1-x Configurable init functions here Abs: execInitFuncs contains the transverse loop thread framework The functions herein MUST BE REENTRANT - no globals allowed - because it is the same code for all. Auth: 28-July-2009, Debbie Rogind -----------------------------------------------------------------------------*/ int transInit1 (ffThread_te ffThread_e ) { int status = OKOK; return status; } int transInit2 (ffThread_te ffThread_e ) { int status = OKOK; return status; } int transInit3 (ffThread_te ffThread_e ) { int status = OKOK; return status; } int transInit4 (ffThread_te ffThread_e ) { int status = OKOK; return status; } int defaultInit (ffThread_te ffThread_e ) { int status = OKOK; return status; } /*============================================================================= Name: initIterateFuncs Abs: Init all callable, configurable, iterate function pointers. Args: Rem: Side: Ret: =============================================================================*/ LOCAL void initIterateFunctions(void) { /* Place iterate ITERATEFUNC functions here */ functionElement_ts * func_ps = NULL; char pvName_ca[MAX_CHAR_EPICS_NAME]; char funcNames_ca[MAX_WAVEFORM_FUNCTIONS][MAX_FUNCNAME_SIZE]; long fnum =0; memset(funcNames_ca, 0, sizeof(funcNames_ca)); /* insert for each new init function */ func_ps = freeListCalloc (functionFreeList_p); if (!func_ps){ DEBUGPRINT(DP_ERROR, functionUtilFlag, ("initIterFuncs error getting functionElement_ts mem from functionFreeList_p!")); } else { strcpy(func_ps->functionName_ca, "transIterate1"); func_ps->function = transIterate1; /* insert into function linked list via functionUtil */ addFunctionToList(func_ps); /* add name to funcName array */ strcpy(funcNames_ca[fnum],func_ps->functionName_ca); fnum++; } /* insert for each new init function */ func_ps = freeListCalloc (functionFreeList_p); if (!func_ps){ DEBUGPRINT(DP_ERROR, functionUtilFlag, ("initIterFuncs error getting functionElement_ts mem from functionFreeList_p!")); } else { strcpy(func_ps->functionName_ca, "transIterate2"); func_ps->function = transIterate2; /* insert into function linked list via functionUtil */ addFunctionToList(func_ps); /* add name to funcName array */ strcpy(funcNames_ca[fnum],func_ps->functionName_ca); fnum++; } /* insert for each new init function */ func_ps = freeListCalloc (functionFreeList_p); if (!func_ps){ DEBUGPRINT(DP_ERROR, functionUtilFlag, ("initIterFuncs error getting functionElement_ts mem from functionFreeList_p!")); } else { strcpy(func_ps->functionName_ca, "transIterate3"); func_ps->function = transIterate3; /* insert into function linked list via functionUtil */ addFunctionToList(func_ps); /* add name to funcName array */ strcpy(funcNames_ca[fnum],func_ps->functionName_ca); fnum++; } /* insert for each new init function */ func_ps = freeListCalloc (functionFreeList_p); if (!func_ps){ DEBUGPRINT(DP_ERROR, functionUtilFlag, ("initIterFuncs error getting functionElement_ts mem from functionFreeList_p!")); } else { strcpy(func_ps->functionName_ca, "transIterate4"); func_ps->function = transIterate4; /* insert into function linked list via functionUtil */ addFunctionToList(func_ps); /* add name to funcName array */ strcpy(funcNames_ca[fnum],func_ps->functionName_ca); fnum++; } /* insert for each new init function */ func_ps = freeListCalloc (functionFreeList_p); if (!func_ps){ DEBUGPRINT(DP_ERROR, functionUtilFlag, ("initIterFuncs error getting functionElement_ts mem from functionFreeList_p!")); } else { strcpy(func_ps->functionName_ca, "defaultIterate"); func_ps->function = defaultIterate; /* insert into function linked list via functionUtil */ addFunctionToList(func_ps); /* add name to funcName array */ strcpy(funcNames_ca[fnum],func_ps->functionName_ca); fnum++; } /* get the function names waveform PV and write the function names into the PV */ if (fnum>0) { sprintf (pvName_ca, "IOC:SYS0:%s:ITERFUNCS", ffGetLOCAName() ); putFunctionsToPV((char *)&pvName_ca, (char *)&(funcNames_ca)); } } /*============================================================================= Name: transIterate Configurable Iterate functions here Abs: loopIterateFuncs contains the transverse loop thread framework The functions herein MUST BE REENTRANT - no globals allowed - because it is the same code for all. Auth: 28-July-2009, Debbie Rogind -----------------------------------------------------------------------------*/ int transIterate1 (ffThread_te ffThread_e ) { int status = OKOK; /* check ON flag */ /* check machine conditions */ /* if conditions changed then reconfigure */ /* read data from FCOM or from PVs */ /* check data */ /* if good data */ /* preprocess data */ /* calculate states */ /* calculate actuator values */ /* check states and actuators */ /* if good states & actuators */ /* if not COMPUTE send commands */ /* write data to storage */ /* not good states & actuators - show error */ /* not good data - show error */ return status; } int transIterate2 (ffThread_te ffThread_e ) { int status = OKOK; return status; } int transIterate3 (ffThread_te ffThread_e ) { int status = OKOK; return status; } int transIterate4 (ffThread_te ffThread_e ) { int status = OKOK; return status; } int defaultIterate (ffThread_te ffThread_e ) { int status = OKOK; return status; } /*============================================================================= Name: initReconfigFunctions Abs: Init all callable, configurable, reconfiguration function pointers. Args: Rem: Side: Ret: =============================================================================*/ LOCAL void initReconfigFunctions(void) { /* Place iterate RECONFIGFUNCS functions here */ functionElement_ts * func_ps = NULL; char pvName_ca[MAX_CHAR_EPICS_NAME]; char funcNames_ca[MAX_WAVEFORM_FUNCTIONS][MAX_FUNCNAME_SIZE]; long fnum =0; memset(funcNames_ca, 0, sizeof(funcNames_ca)); /* insert for each new init function */ func_ps = freeListCalloc (functionFreeList_p); if (!func_ps){ DEBUGPRINT(DP_ERROR, functionUtilFlag, ("initReconfigFuncs error getting functionElement_ts mem from functionFreeList_p!")); } else { strcpy(func_ps->functionName_ca, "transReconfig"); func_ps->function = transIterate1; /* insert into function linked list via functionUtil */ addFunctionToList(func_ps); /* add name to funcName array */ strcpy(funcNames_ca[fnum],func_ps->functionName_ca); fnum++; } /* insert for each new init function */ func_ps = freeListCalloc (functionFreeList_p); if (!func_ps){ DEBUGPRINT(DP_ERROR, functionUtilFlag, ("initReconfigFuncs error getting functionElement_ts mem from functionFreeList_p!")); } else { strcpy(func_ps->functionName_ca, "longReconfig"); func_ps->function = longReconfig; /* insert into function linked list via functionUtil */ addFunctionToList(func_ps); /* add name to funcName array */ strcpy(funcNames_ca[fnum],func_ps->functionName_ca); fnum++; } /* insert for each new init function */ func_ps = freeListCalloc (functionFreeList_p); if (!func_ps){ DEBUGPRINT(DP_ERROR, functionUtilFlag, ("initReconfigFuncs error getting functionElement_ts mem from functionFreeList_p!")); } else { strcpy(func_ps->functionName_ca, "defaultReconfig"); func_ps->function = defaultReconfig; /* insert into function linked list via functionUtil */ addFunctionToList(func_ps); /* add name to funcName array */ strcpy(funcNames_ca[fnum],func_ps->functionName_ca); fnum++; } /* get the function names waveform PV and write the function names into the PV */ if (fnum>0) { sprintf (pvName_ca, "IOC:SYS0:%s:RECONFIGFUNCS", ffGetLOCAName() ); putFunctionsToPV((char *)&pvName_ca, (char *)&(funcNames_ca)); } } /*============================================================================= Name: transReconfig reconfigure functions Abs: initReconfigFuncs contains the loop thread framework The functions herein MUST BE REENTRANT - no globals allowed - because it is the same code for all. Auth: 28-July-2009, Debbie Rogind -----------------------------------------------------------------------------*/ int transReconfig (ffThread_te ffThread_e ) { int status = OKOK; /* check ON flag */ /* check machine conditions */ /* if conditions changed then reconfigure */ /* read data from FCOM or from PVs */ /* check data */ /* if good data */ /* preprocess data */ /* calculate states */ /* calculate actuator values */ /* check states and actuators */ /* if good states & actuators */ /* if not COMPUTE send commands */ /* write data to storage */ /* not good states & actuators - show error */ /* not good data - show error */ return status; } int longReconfig (ffThread_te ffThread_e ) { int status = OKOK; return status; } int defaultReconfig (ffThread_te ffThread_e ) { int status = OKOK; return status; }