/*============================================================================= Filename: kly_sub.c Abs: This file contains all subroutine support for focus supplies and modulators. Rem: All functions called by the subroutine record get passed one argument: Functions: Description ------------ ---------------------------- kly_init General Initialization kly_limits Set Alarm and Warning Limits Generic Functions Description ----------------- ----------------------------- alarmPeriodInit Alarm period initialization alarmClockInit AlarmClock initialization alarmPeriod Generate a soft event at a programmable period alarmClock Generate a soft event at a programmable time All functions return a long integer. 0 = OK, -1 = ERROR. The subroutine record ignores the status returned by the Init routines. For the calculation routines, the record status (STAT) is set to SOFT_ALARM (unless it is already set to LINK_ALARM due to severity maximization) and the severity (SEVR) is set to psub->brsv (BRSV - set by the user in the database though it is expected to be invalid) Side: Place all functions called by the subroutine records in the kly_sub.dbd file also Auth: 24-Mar-2014, K. Luchini (LUCHINI) Rev: dd-mmm-yyyy, Reviewer's Name (USERNAME) -------------------------------------------------------------- Mod: dd-mmm-yyyy, First Lastname (USERNAME): comment ==============================================================*/ #include #include /* for atol() */ #include /* for strncpy */ #include "dbScan.h" /* for post_event() */ #include "epicsTime.h" /* for epicsTimeStamp */ #include "subRecord.h" /* for struct subRecord */ #include "aSubRecord.h" /* for struct aSubRecord */ #include "epicsTypes.h" /* for epicsUInt32 */ #include "alarm.h" /* for INVALID_ALARM */ #include "recGbl.h" /* for recGblGetTimeStamp() */ #include "registryFunction.h" /* for epicsExport */ #include "epicsExport.h" /* for epicsRegisterFunction */ #ifndef SUCCESS #define SUCCESS 0 #endif #ifndef ERROR #define ERROR -1 #endif #define TIME_FORMAT "%m/%d/%y %H:%M:%S" #define TIME_ONLY_FORMAT "%H:%M:%S" #define HOURS_PER_DAY 24 #define MINUTES_PER_HOUR 60 #define SECONDS_PER_MINUTE 60 /*============================================================================= Name: kly_init Abs: Klystron initialization subroutine record Rem: Side: None Ret: int 0 - Successful (always) ==============================================================================*/ static long kly_init(subRecord *psub) { long status=SUCCESS; /* * General purpose initialization required since all subroutine records * require a non-NULL init routine even if no initialization is required. * Note that most subroutines in this file use this routine as an init * routine. If init logic is needed for a specific subroutine, create a * new routine for it - don't modify this one. */ return(status); } /*============================================================================= Name: kly_limits Abs: Calculate alarm and warning limits from deltas Args: Type Name Access Description ------------------- ----------- ---------- ---------------------------- subRecord * psub read/write pointer to subroutine record Input: -------- A = Desired set point :ISETPT B = Maximum :ISETPT.DRVH C = Minimum :ISETPV.DRVL D = Warning Delta (0 = disable) default = 0.5 E = Alarm Delta (0 = disable) default = 1.0 F = Disable Alarm Checking (0=no, 1=yes) (0=calc limits, 1=set limits to min/max) G = State of PS (0-On,1=Off) :STATE Outputs: --------- I = HIGH value (warning high) J = LOW value (warning low) K = HIHI value (alarm high) L = LOLO value (alarm low) VAL = Not Used Rem: Calculate alarm and warning limits from deltas. If the disable alarms flag is set or the supply is off, then use the drvh/drvl fields of the set point to disable alarms. Side: None Ret: int 0 - Successful (always) ==============================================================================*/ static long kly_limits(struct subRecord *psub) { long status = SUCCESS; /* Disable alarms */ psub->i = psub->b+1; psub->j = psub->c-1; psub->k = psub->b+1; psub->l = psub->c-1; /* If alarming enabled and PS off then * then set the warning and alarm limits. */ if ((psub->f < 0.01) && (psub->g < 0.01)) { /* * If we have a valid warning delta calcualate * the LOW and HIGH values. Otherwise * leave the warnings disabled. */ if (psub->d > 0.0) { psub->i = psub->a + psub->d; psub->j = psub->a - psub->d; } /* * If we have a valid alarm delta calculate * the LOLO and HIHI values. Otherwise, * leave the warnings disabled. */ if (psub->e > 0.0) { psub->k = psub->a + psub->e; psub->l = psub->a - psub->e; } } return(status); } /*============================================================================= Name: alarmPeriodInit Abs: Initalization for alarmPeriod function Args: Type Name Access Description ------------------- ----------- ---------- ---------------------------- subRecord * psub read/write pointer to subroutine record Input: -------- A = period in minutes, defaults is 60 minutes. :PERIOD Outputs: --------- L = timestamp (secPastEpoc) when we last chimed Rem: Check the default chime period, if less then 1 minute, then set to the default of 60 minutes, before setting the prompt string to the target name. Side: None Ret: int 0 - Successful (always) ==============================================================================*/ static long alarmPeriodInit(subRecord *psub) { long status=SUCCESS; epicsTimeStamp now; if (psub->a < 1.0) psub->a = MINUTES_PER_HOUR; /* Default to chiming hourly */ epicsTimeGetCurrent(&now); psub->l = now.secPastEpoch; return(status); } /*============================================================================= Name: alarmPeriod Abs: Generate a soft event at a programmable period in minutes. Args: Type Name Access Description ------------------- ----------- ---------- ---------------------------- subRecord * psub read/write pointer to subroutine record Input: -------- A = chime period in minutes, defaults to 60 if not set :PERIOD B = event number Outputs: --------- L = timestamp (secPastEpoc) when we last event VAL = Not Used Rem: Generate a soft event at the specified period n A. This will trigger any records with SCAN=Event and EVNT= on the IOC. With this subroutine record you can instantiante as many records as you need periods, by setting A to the period in minutes and B to the event number. Side: None Ret: long 0 - Successful (always) Auth: Andrew Johnson (Argonne Nantional Lab) ==============================================================================*/ static long alarmPeriod(subRecord *psub) { long status = SUCCESS; epicsUInt32 period, next; epicsTimeStamp now; if (psub->a < 1.0) psub->a = 1.0; /* Chime at most once a minute */ period = 60 * (epicsUInt32) psub->a; next = period * (1 + (epicsUInt32) psub->l / period); epicsTimeGetCurrent(&now); if (now.secPastEpoch >= next) { /* Bong bong bong bong */ psub->l = now.secPastEpoch; post_event(psub->b); } return(status); } /*============================================================================= Name: alarmClockInit Abs: Initialization for alarmClock function Args: Type Name Access Description ------------------- ----------- ---------- ---------------------------- subRecord * psub read/write pointer to subroutine record Input: -------- A = hour to wake up, range: 0-23 :FLT_RESET_HOUR B = minute to wake up, range: 0-59 :FLT_RESET_MIN C = seconds to wake up, range: 0-59 :FLT_RESET_SEC D = event number :FLT_RESET_EVNT E = debug flag Outputs: --------- VALA = timestamp when alarm clock last sounded (FROM) VALB = timestamp when alarm clock will sound next (TO) Note: timestamp string format 02/02/2010 10:19:25 0123456789012345678 VALC = alarm clock chime (0=sleep, 1=wake_up) VALD = timestamp when alarm clock last sounded in seconds VALE = timestamp when alarm clock will sound next in seconds Note: timestamp in seconds is since 0000 Jan 1, 1990 VAL = Not Used Rem: Side: None Ret: int 0 - Successful (always) ==============================================================================*/ static long alarmClockInit(aSubRecord *psub) { long status=SUCCESS; /* * General purpose initialization required since all subroutine records * require a non-NULL init routine even if no initialization is required. * Note that most subroutines in this file use this routine as an init * routine. If init logic is needed for a specific subroutine, create a * new routine for it - don't modify this one. */ return(status); } /*============================================================================= Name: alarmClock Abs: Generate a soft event at a programmable time of day, specified in hours and minutes. Args: Type Name Access Description ------------------- ----------- ---------- ---------------------------- subRecord * psub read/write pointer to subroutine record Input: -------- A = hour to wake up, range: 0-23 :FLT_RESET_HOUR B = minute to wake up, range: 0-59 :FLT_RESET_MIN C = seconds to wake up, range: 0-59 :FLT_RESET_SEC D = event number :FLT_RESET_EVNT E = debug flag Outputs: --------- VALA = timestamp when alarm clock sounded as a string VALB = timestamp when alarm clock last sounded as a string Note: timestamp string format 02/02/2010 10:19:25 0123456789012345678 VALC = alarm clock chime (0=sleep, 1=wake_up) VALD = timestamp when alarm clock sounded in seconds VALE = timestamp when alarm clock last sounded n seconds Note: timestamp in seconds is since 0000 Jan 1, 1990 VALF = timestamp when next alarm clock will soound as a string VALG = timestamp when next alarm clock will sounnd in seconds VAL = Not Used Rem: Side: None Ret: long OK - Successful ERROR - String length for reset date and is too long. Auth: Andrew Johnson (Argonne Nantional Lab) ==============================================================================*/ static long alarmClock(aSubRecord *psub) { long status = SUCCESS; int len; epicsTimeStamp now_s; epicsUInt32 alarm_hour,alarm_minute,alarm_second; epicsUInt32 now_hour,now_minute,now_second; char now_hour_a[MAX_STRING_SIZE]; char now_minute_a[MAX_STRING_SIZE]; char now_second_a[MAX_STRING_SIZE]; char now_a[MAX_STRING_SIZE]; static const epicsUInt32 seconds_per_day=(SECONDS_PER_MINUTE * MINUTES_PER_HOUR * HOURS_PER_DAY); /* get alarm hour */ if (*(epicsUInt32 *)psub->a < 0) alarm_hour = 0; else if (*(epicsUInt32 *)psub->a >= HOURS_PER_DAY) alarm_hour =HOURS_PER_DAY-1; else alarm_hour=*(epicsUInt32 *)psub->a; /* get alarm minute */ if (*(epicsUInt32 *)psub->b < 0) alarm_minute=0; else if (*(epicsUInt32 *)psub->b > MINUTES_PER_HOUR) alarm_minute=MINUTES_PER_HOUR-1; else alarm_minute=*(epicsUInt32 *)psub->b; /* get alarm second */ if (*(epicsUInt32 *)psub->c < 0) alarm_second=0; else if (*(epicsUInt32 *)psub->c > SECONDS_PER_MINUTE) alarm_second=SECONDS_PER_MINUTE-1; else alarm_second=*(epicsUInt32 *)psub->c; epicsTimeGetCurrent(&now_s); epicsTimeToStrftime(now_hour_a,sizeof(now_hour_a),"%H", &now_s); epicsTimeToStrftime(now_minute_a,sizeof(now_minute_a),"%M", &now_s); epicsTimeToStrftime(now_second_a,sizeof(now_second_a),"%S", &now_s); now_hour = atol(now_hour_a); now_minute = atol(now_minute_a); now_second = atol(now_second_a); /* Is it time to sound the alarm? */ if ((now_hour==alarm_hour) && (now_minute==alarm_minute) && (now_second==alarm_second)) { if ( *(epicsInt32 *)psub->e ) { /* debug flag set? */ epicsTimeToStrftime(now_a,sizeof(now_a),TIME_FORMAT,&now_s); printf("...ding ding ding: %.20s\n",now_a); printf("\tCurrent From: %s To: %s\n",(char *)psub->valb,(char *)psub->vala); } /* Save the last alarm timestamp */ strncpy((char *)psub->valb,(char *)psub->vala,MAX_STRING_SIZE); memcpy((epicsUInt32 *)psub->vale,(epicsUInt32 *)psub->vald,sizeof(epicsTimeStamp)); /* Get the current reset time as a string and in seconds (from Jan 1, 1990) and add 24-hours */ memcpy((epicsUInt32 *)psub->vald,&now_s,sizeof(epicsTimeStamp)); epicsTimeToStrftime((char *)psub->vala,MAX_STRING_SIZE,TIME_FORMAT,&now_s); /* Get the next reset time */ now_s.secPastEpoch += seconds_per_day; len = epicsTimeToStrftime((char *)psub->valf,MAX_STRING_SIZE,TIME_FORMAT,&now_s); if (len < MAX_STRING_SIZE) { /* memcpy((epicsUInt32 *)psub->valg,&now_s,sizeof(epicsTimeStamp)); */ *(epicsInt16 *)psub->valc = 1; /* sound alarm */ post_event(*(epicsInt32 *)psub->d); if ( *(epicsInt32 *)psub->e) printf("\tLatest From: %.20s To: %.20s Next: %.20s\tevent num=%d\n", (char *)psub->valb,(char *)psub->vala,(char *)psub->valf,*(epicsInt32 *)psub->d); } else { psub->udf = TRUE; recGblSetSevr(psub,UDF_ALARM,INVALID_ALARM); status = ERROR; if ( *(epicsUInt32 *)psub->e ) printf("alarmClock: epicsTimeToStrftime() error occurred, len=%d\n",len); } } else *(epicsInt16 *)psub->valc = 0; /* silence alarm */ return(status); } /*============================================================================= Name: kly_fault_cnt_init Abs: Initialization for function kly_fault_cnt Args: Type Name Access Description ------------------- ----------- ---------- ---------------------------- subRecord * psub read/write pointer to subroutine record Input: ------- A = hw fault count :_FLT_CNT B = hw fault count delta :_FLT_CNT_DELTA C = hw fault count timestamp format: %H:%M:%S :_FLT_CNT_TS D = today's fault count :_FLT_CNT_T E = today's fault count total :_FLT_CNT_TOTAL_T F = alarm clock status (0=sleep, 1=wake up) :FLT_RESET G = not used H = debug flag Outputs: --------- VALA = today's fault count VALB = today's fault timestamp or blank if reset VALC = today's fault count total VALD = today's fault count timestamp or blank if reset VAL = Not Used Rem: Side: None Ret: long OK - Successful (always) Auth: 05-Jul-2016, K. Luchini (luchini) ==============================================================================*/ static long kly_fault_cnt_init(aSubRecord *psub) { long status=SUCCESS; /* * General purpose initialization required since all subroutine records * require a non-NULL init routine even if no initialization is required. * Note that most subroutines in this file use this routine as an init * routine. If init logic is needed for a specific subroutine, create a * new routine for it - don't modify this one. */ return(status); } /*============================================================================= Name: kly_fault_cnt Abs: Keep a running total of the fault counts Args: Type Name Access Description ------------------- ----------- ---------- ---------------------------- subRecord * psub read/write pointer to subroutine record Input: ------- A = hw fault count :_FLT_CNT B = hw fault count delta :_FLT_CNT_DELTA C = hw fault count timestamp format: %H:%M:%S :_FLT_CNT_TS D = today's fault count :_FLT_CNT_T E = today's fault count total :_FLT_TOTAL_T F = reset flag (0=no reset,1=reset) :FLT_RESET G = not used H = debug flag Outputs: --------- VALA = today's fault count :_FLT_CNT_T VALB = today's fault timestamp or blank if reset :_FLT_CNT_TS_T VALC = today's fault count total :_FLT_TOTAL_T VALD = today's fault count timestamp or blank if reset :_FLT_TOTAL_TS_T VAL = Not Used Rem: Side: None Ret: long OK - Successful ERROR - String length for reset date and is too long. Auth: 05-Jul-2016, K. Luchini (luchini) ==============================================================================*/ static long kly_fault_cnt(aSubRecord *psub) { long status=SUCCESS; int len=0; epicsUInt32 reset=*(epicsUInt32 *)psub->f; epicsInt32 delta=*(epicsInt32 *)psub->b; epicsTimeStamp now_s; char now_a[MAX_STRING_SIZE]; /* if the fault counter hasn't changed and it's not time to rest today's counters then exit */ if ( !reset && !delta ) { if ( *(epicsInt32 *)psub->h ) printf("kly_fault_cnt: No change cnt=%d(%d) delta=%d total=%d\t%.20s\n", *(epicsInt32 *)psub->a, *(epicsInt32 *)psub->d, *(epicsInt32 *)psub->b, *(epicsInt32 *)psub->e, (char *)psub->c); if ( *(epicsInt32 *)psub->a) { *(epicsInt32 *)psub->vala=*(epicsInt32 *)psub->d; strncpy( (char *)psub->valb,(char *)psub->c,MAX_STRING_SIZE ); *(epicsInt32 *)psub->valc=*(epicsInt32 *)psub->e; strncpy( (char *)psub->vald,(char *)psub->c,MAX_STRING_SIZE ); } return(status); } /* Is it time to reset today's counters? Check the alarm status */ if ( reset ) { *(epicsInt32 *)psub->vala = 0; /* zero fault count */ *(epicsInt32 *)psub->valc = 0; /* zero total faults */ epicsTimeGetCurrent(&now_s); len=epicsTimeToStrftime((char *)psub->valb,sizeof(now_a),TIME_ONLY_FORMAT,&now_s); strncpy((char *)psub->vald,(char *)psub->valb,MAX_STRING_SIZE); if (len <= MAX_STRING_SIZE) { epicsTimeToStrftime(now_a,sizeof(now_a),TIME_FORMAT,&now_s); if ( *(epicsInt32 *)psub->h ) printf("...Resetting today's fault counts at %.20s\n",now_a); } else { psub->udf = TRUE; recGblSetSevr(psub,UDF_ALARM,INVALID_ALARM); status = ERROR; if ( *(epicsInt32 *)psub->h ) printf("...epicsTimeToStrftime() Error\n"); } } /* Looks like the fault counter has changed, so go ahead and update today's counts. */ else if (delta) { if ( *(epicsInt32 *)psub->h ) printf("\t\t\tToday: cnt=%d\ttotal=%d\t%.20s\n", *(epicsInt32 *)psub->vala, *(epicsInt32 *)psub->valc, (char *)psub->valb); *(epicsInt32 *)psub->vala += delta; /* today's fault count */ *(epicsInt32 *)psub->valc += delta; /* today's fault total */ strncpy((char *)psub->valb,(char *)psub->c,MAX_STRING_SIZE); /* time fault occurred */ strncpy((char *)psub->vald,(char *)psub->c,MAX_STRING_SIZE); /* time fault occurred */ } if ( *(epicsInt32 *)psub->h ) { printf("\tHw: cnt=%d\tdelta=%d\tToday: cnt=%d (%d)\ttotal=%d\t%.20s\n", *(epicsInt32 *)psub->a, *(epicsInt32 *)psub->b, *(epicsInt32 *)psub->vala, *(epicsInt32 *)psub->d, *(epicsInt32 *)psub->valc, (char *)psub->valb); } return(status); } epicsRegisterFunction(kly_fault_cnt_init); epicsRegisterFunction(alarmClockInit); epicsRegisterFunction(alarmPeriodInit); epicsRegisterFunction(kly_init); epicsRegisterFunction(kly_limits); epicsRegisterFunction(kly_fault_cnt); epicsRegisterFunction(alarmPeriod); epicsRegisterFunction(alarmClock); /* Note - when adding a funciton, also add to kly_sub.dbd */