Files
epics-base/src/rec/subRecord.c
2000-02-29 19:34:40 +00:00

460 lines
13 KiB
C
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* recSub.c */
/* base/src/rec $Id$ */
/* recSub.c - Record Support Routines for Subroutine records */
/*
* Original Author: Bob Dalesio
* Current Author: Marty Kraimer
* Date: 01-25-90
*
* Experimental Physics and Industrial Control System (EPICS)
*
* Copyright 1991, the Regents of the University of California,
* and the University of Chicago Board of Governors.
*
* This software was produced under U.S. Government contracts:
* (W-7405-ENG-36) at the Los Alamos National Laboratory,
* and (W-31-109-ENG-38) at Argonne National Laboratory.
*
* Initial development by:
* The Controls and Automation Group (AT-8)
* Ground Test Accelerator
* Accelerator Technology Division
* Los Alamos National Laboratory
*
* Co-developed with
* The Controls and Computing Group
* Accelerator Systems Division
* Advanced Photon Source
* Argonne National Laboratory
*
* Modification Log:
* -----------------
* .01 10-10-90 mrk Made changes for new record support
* .02 11-11-91 jba Moved set and reset of alarm stat and sevr to macros
* .03 01-08-92 jba Added casts in symFindByName to avoid compile warning messages
* .04 02-05-92 jba Changed function arguments from paddr to precord
* .05 02-28-92 jba Changed get_precision,get_graphic_double,get_control_double
* .06 02-28-92 jba ANSI C changes
* .07 04-10-92 jba pact now used to test for asyn processing, not status
* .08 06-02-92 jba changed graphic/control limits for hihi,high,low,lolo
* .09 07-15-92 jba changed VALID_ALARM to INVALID alarm
* .10 07-16-92 jba added invalid alarm fwd link test and chngd fwd lnk to macro
* .11 07-21-92 jba changed alarm limits for non val related fields
* .12 08-06-92 jba New algorithm for calculating analog alarms
* .13 08-06-92 jba monitor now posts events for changes in a-l
* .14 10-10-92 jba replaced code with recGblGetLinkValue call
* .15 03-30-94 mcn converted to fast links
*/
#include <stddef.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include "dbDefs.h"
#include "epicsPrint.h"
#include "registry.h"
#include "alarm.h"
#include "callback.h"
#include "cantProceed.h"
#include "dbAccess.h"
#include "epicsPrint.h"
#include "dbEvent.h"
#include "dbFldTypes.h"
#include "errMdef.h"
#include "recSup.h"
#include "recGbl.h"
#define GEN_SIZE_OFFSET
#include "subRecord.h"
#undef GEN_SIZE_OFFSET
/* Create RSET - Record Support Entry Table*/
#define report NULL
#define initialize NULL
static long init_record();
static long process();
#define special NULL
#define get_value NULL
#define cvt_dbaddr NULL
#define get_array_info NULL
#define put_array_info NULL
static long get_units();
static long get_precision();
#define get_enum_str NULL
#define get_enum_strs NULL
#define put_enum_str NULL
static long get_graphic_double();
static long get_control_double();
static long get_alarm_double();
struct rset subRSET={
RSETNUMBER,
report,
initialize,
init_record,
process,
special,
get_value,
cvt_dbaddr,
get_array_info,
put_array_info,
get_units,
get_precision,
get_enum_str,
get_enum_strs,
put_enum_str,
get_graphic_double,
get_control_double,
get_alarm_double };
static void alarm();
static long do_sub();
static long fetch_values();
static void monitor();
#define ARG_MAX 12
typedef long (*SUBFUNCPTR)();
static long init_record(psub,pass)
struct subRecord *psub;
int pass;
{
SUBFUNCPTR psubroutine;
long status = 0;
struct link *plink;
int i;
double *pvalue;
if (pass==0) return(0);
plink = &psub->inpa;
pvalue = &psub->a;
for(i=0; i<ARG_MAX; i++, plink++, pvalue++) {
if (plink->type==CONSTANT) {
recGblInitConstantLink(plink,DBF_DOUBLE,pvalue);
}
}
if(strlen(psub->inam)!=0) {
/* convert the initialization subroutine name */
psub->sadr = registryFind(0,psub->inam);
if(psub->sadr==0) {
recGblRecordError(S_db_BadSub,(void *)psub,"recSub(init_record)");
return(S_db_BadSub);
}
/* invoke the initialization subroutine */
psubroutine = (SUBFUNCPTR)(psub->sadr);
status = (*psubroutine)(psub,process);
}
if(strlen(psub->snam)==0) {
epicsPrintf("%s snam not specified\n",psub);
psub->pact = TRUE;
return(0);
}
psub->sadr = registryFind(0,psub->snam);
if(psub->sadr==0) {
recGblRecordError(S_db_BadSub,(void *)psub,"recSub(init_record)");
return(S_db_BadSub);
}
return(0);
}
static long process(psub)
struct subRecord *psub;
{
long status=0;
unsigned char pact=psub->pact;
if(!psub->pact || !psub->sadr){
psub->pact = TRUE;
status = fetch_values(psub);
psub->pact = FALSE;
}
if(status==0) status = do_sub(psub);
/* check if device support set pact */
if ( !pact && psub->pact ) return(0);
/*previously we had different rules. Lets support old subs*/
psub->pact = TRUE;
if(status==1) return(0);
recGblGetTimeStamp(psub);
/* check for alarms */
alarm(psub);
/* check event list */
monitor(psub);
/* process the forward scan link record */
recGblFwdLink(psub);
psub->pact = FALSE;
return(0);
}
static long get_units(paddr,units)
struct dbAddr *paddr;
char *units;
{
struct subRecord *psub=(struct subRecord *)paddr->precord;
strncpy(units,psub->egu,DB_UNITS_SIZE);
return(0);
}
static long get_precision(paddr,precision)
struct dbAddr *paddr;
long *precision;
{
struct subRecord *psub=(struct subRecord *)paddr->precord;
*precision = psub->prec;
if(paddr->pfield==(void *)&psub->val) return(0);
recGblGetPrec(paddr,precision);
return(0);
}
static long get_graphic_double(paddr,pgd)
struct dbAddr *paddr;
struct dbr_grDouble *pgd;
{
struct subRecord *psub=(struct subRecord *)paddr->precord;
if(paddr->pfield==(void *)&psub->val
|| paddr->pfield==(void *)&psub->hihi
|| paddr->pfield==(void *)&psub->high
|| paddr->pfield==(void *)&psub->low
|| paddr->pfield==(void *)&psub->lolo){
pgd->upper_disp_limit = psub->hopr;
pgd->lower_disp_limit = psub->lopr;
return(0);
}
if(paddr->pfield>=(void *)&psub->a && paddr->pfield<=(void *)&psub->l){
pgd->upper_disp_limit = psub->hopr;
pgd->lower_disp_limit = psub->lopr;
return(0);
}
if(paddr->pfield>=(void *)&psub->la && paddr->pfield<=(void *)&psub->ll){
pgd->upper_disp_limit = psub->hopr;
pgd->lower_disp_limit = psub->lopr;
return(0);
}
return(0);
}
static long get_control_double(paddr,pcd)
struct dbAddr *paddr;
struct dbr_ctrlDouble *pcd;
{
struct subRecord *psub=(struct subRecord *)paddr->precord;
if(paddr->pfield==(void *)&psub->val
|| paddr->pfield==(void *)&psub->hihi
|| paddr->pfield==(void *)&psub->high
|| paddr->pfield==(void *)&psub->low
|| paddr->pfield==(void *)&psub->lolo){
pcd->upper_ctrl_limit = psub->hopr;
pcd->lower_ctrl_limit = psub->lopr;
return(0);
}
if(paddr->pfield>=(void *)&psub->a && paddr->pfield<=(void *)&psub->l){
pcd->upper_ctrl_limit = psub->hopr;
pcd->lower_ctrl_limit = psub->lopr;
return(0);
}
if(paddr->pfield>=(void *)&psub->la && paddr->pfield<=(void *)&psub->ll){
pcd->upper_ctrl_limit = psub->hopr;
pcd->lower_ctrl_limit = psub->lopr;
return(0);
}
return(0);
}
static long get_alarm_double(paddr,pad)
struct dbAddr *paddr;
struct dbr_alDouble *pad;
{
struct subRecord *psub=(struct subRecord *)paddr->precord;
if(paddr->pfield==(void *)&psub->val){
pad->upper_alarm_limit = psub->hihi;
pad->upper_warning_limit = psub->high;
pad->lower_warning_limit = psub->low;
pad->lower_alarm_limit = psub->lolo;
} else recGblGetAlarmDouble(paddr,pad);
return(0);
}
static void alarm(psub)
struct subRecord *psub;
{
double val;
double hyst, lalm, hihi, high, low, lolo;
unsigned short hhsv, llsv, hsv, lsv;
if(psub->udf == TRUE ){
recGblSetSevr(psub,UDF_ALARM,INVALID_ALARM);
return;
}
hihi = psub->hihi; lolo = psub->lolo; high = psub->high; low = psub->low;
hhsv = psub->hhsv; llsv = psub->llsv; hsv = psub->hsv; lsv = psub->lsv;
val = psub->val; hyst = psub->hyst; lalm = psub->lalm;
/* alarm condition hihi */
if (hhsv && (val >= hihi || ((lalm==hihi) && (val >= hihi-hyst)))){
if (recGblSetSevr(psub,HIHI_ALARM,psub->hhsv)) psub->lalm = hihi;
return;
}
/* alarm condition lolo */
if (llsv && (val <= lolo || ((lalm==lolo) && (val <= lolo+hyst)))){
if (recGblSetSevr(psub,LOLO_ALARM,psub->llsv)) psub->lalm = lolo;
return;
}
/* alarm condition high */
if (hsv && (val >= high || ((lalm==high) && (val >= high-hyst)))){
if (recGblSetSevr(psub,HIGH_ALARM,psub->hsv)) psub->lalm = high;
return;
}
/* alarm condition low */
if (lsv && (val <= low || ((lalm==low) && (val <= low+hyst)))){
if (recGblSetSevr(psub,LOW_ALARM,psub->lsv)) psub->lalm = low;
return;
}
/* we get here only if val is out of alarm by at least hyst */
psub->lalm = val;
return;
}
static void monitor(psub)
struct subRecord *psub;
{
unsigned short monitor_mask;
double delta;
double *pnew;
double *pprev;
int i;
/* get previous stat and sevr and new stat and sevr*/
monitor_mask = recGblResetAlarms(psub);
/* check for value change */
delta = psub->mlst - psub->val;
if(delta<0.0) delta = -delta;
if (delta > psub->mdel) {
/* post events for value change */
monitor_mask |= DBE_VALUE;
/* update last value monitored */
psub->mlst = psub->val;
}
/* check for archive change */
delta = psub->alst - psub->val;
if(delta<0.0) delta = -delta;
if (delta > psub->adel) {
/* post events on value field for archive change */
monitor_mask |= DBE_LOG;
/* update last archive value monitored */
psub->alst = psub->val;
}
/* send out monitors connected to the value field */
if (monitor_mask){
db_post_events(psub,&psub->val,monitor_mask);
}
/* check all input fields for changes*/
for(i=0, pnew=&psub->a, pprev=&psub->la; i<ARG_MAX; i++, pnew++, pprev++) {
if(*pnew != *pprev) {
db_post_events(psub,pnew,monitor_mask|DBE_VALUE|DBE_LOG);
*pprev = *pnew;
}
}
return;
}
static long fetch_values(psub)
struct subRecord *psub;
{
struct link *plink; /* structure of the link field */
double *pvalue;
int i;
long status;
for(i=0, plink=&psub->inpa, pvalue=&psub->a; i<ARG_MAX; i++, plink++, pvalue++) {
status=dbGetLink(plink,DBR_DOUBLE, pvalue,0,0);
if (!RTN_SUCCESS(status)) return(-1);
}
return(0);
}
static long do_sub(psub)
struct subRecord *psub; /* pointer to subroutine record */
{
long status;
SUBFUNCPTR psubroutine;
/* call the subroutine */
psubroutine = (SUBFUNCPTR)(psub->sadr);
if(psubroutine==NULL) {
recGblSetSevr(psub,BAD_SUB_ALARM,INVALID_ALARM);
return(0);
}
status = (*psubroutine)(psub);
if(status < 0){
recGblSetSevr(psub,SOFT_ALARM,psub->brsv);
} else psub->udf = FALSE;
return(status);
}
/* The following is provided for access security*/
/*It allows a CA client to force access security initialization*/
#include "asLib.h"
#include "asDbLib.h"
static void myCallback(CALLBACK *pcallback)
{
ASDBCALLBACK *pasdbcallback = (ASDBCALLBACK *)pcallback;
subRecord *precord;
struct rset *prset;
callbackGetUser(precord,pcallback);
prset=(struct rset *)(precord->rset);
precord->val = 0.0;
if(pasdbcallback->status) {
recGblSetSevr(precord,READ_ALARM,precord->brsv);
recGblRecordError(pasdbcallback->status,precord,"asInit Failed");
}
dbScanLock((dbCommon *)precord);
(*prset->process)((dbCommon *)precord);
dbScanUnlock((dbCommon *)precord);
}
long asSubInit(subRecord *precord,int pass)
{
ASDBCALLBACK *pcallback;
pcallback = (ASDBCALLBACK *)callocMustSucceed(
1,sizeof(ASDBCALLBACK),"asSubInit");
precord->dpvt = (void *)pcallback;
callbackSetCallback(myCallback,&pcallback->callback);
callbackSetUser(precord,&pcallback->callback);
return(0);
}
long asSubProcess(subRecord *precord)
{
ASDBCALLBACK *pcallback = (ASDBCALLBACK *)precord->dpvt;
if(!precord->pact && precord->val==1.0) {
db_post_events(precord,&precord->val,DBE_VALUE);
callbackSetPriority(precord->prio,&pcallback->callback);
asInitAsyn(pcallback);
precord->pact=TRUE;
return(1);
}
db_post_events(precord,&precord->val,DBE_VALUE);
return(0);
}