This is Unofficial EPICS BASE Doxygen Site
int64inRecord.c
Go to the documentation of this file.
1 /*************************************************************************\
2 * Copyright (c) 2016 UChicago Argonne LLC, as Operator of Argonne
3 * National Laboratory.
4 * Copyright (c) 2002 The Regents of the University of California, as
5 * Operator of Los Alamos National Laboratory.
6 * EPICS BASE is distributed subject to a Software License Agreement found
7 * in file LICENSE that is included with this distribution.
8 \*************************************************************************/
9 
10 /* int64inRecord.c - Record Support Routines for int64in records */
11 /*
12  * Original Author: Janet Anderson
13  * Date: 9/23/91
14  */
15 
16 #include <stddef.h>
17 #include <stdlib.h>
18 #include <stdarg.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <math.h>
22 
23 #include "dbDefs.h"
24 #include "epicsPrint.h"
25 #include "alarm.h"
26 #include "callback.h"
27 #include "dbAccess.h"
28 #include "dbEvent.h"
29 #include "dbFldTypes.h"
30 #include "devSup.h"
31 #include "errMdef.h"
32 #include "recSup.h"
33 #include "recGbl.h"
34 #include "special.h"
35 #include "menuYesNo.h"
36 
37 #define GEN_SIZE_OFFSET
38 #include "int64inRecord.h"
39 #undef GEN_SIZE_OFFSET
40 #include "epicsExport.h"
41 
42  /* Hysterisis for alarm filtering: 1-1/e */
43 #define THRESHOLD 0.6321
44 /* Create RSET - Record Support Entry Table*/
45 #define report NULL
46 #define initialize NULL
47 static long init_record(dbCommon *, int);
48 static long process(dbCommon *);
49 static long special(DBADDR *, int);
50 #define get_value NULL
51 #define cvt_dbaddr NULL
52 #define get_array_info NULL
53 #define put_array_info NULL
54 static long get_units(DBADDR *, char *);
55 #define get_precision NULL
56 #define get_enum_str NULL
57 #define get_enum_strs NULL
58 #define put_enum_str NULL
59 static long get_graphic_double(DBADDR *, struct dbr_grDouble *);
60 static long get_control_double(DBADDR *, struct dbr_ctrlDouble *);
61 static long get_alarm_double(DBADDR *, struct dbr_alDouble *);
62 
64  RSETNUMBER,
65  report,
66  initialize,
68  process,
69  special,
70  get_value,
71  cvt_dbaddr,
74  get_units,
82 };
83 epicsExportAddress(rset,int64inRSET);
84 
85 
86 static void checkAlarms(int64inRecord *prec, epicsTimeStamp *timeLast);
87 static void monitor(int64inRecord *prec);
88 static long readValue(int64inRecord *prec);
89 
90 
91 static long init_record(dbCommon *pcommon, int pass)
92 {
93  int64inRecord *prec = (int64inRecord*)pcommon;
94  int64indset *pdset;
95  long status;
96 
97  if (pass == 0) return 0;
98 
99  /* int64in.siml and .siol must be a CONSTANT or a PV_LINK or a DB_LINK */
100  recGblInitSimm(pcommon, &prec->sscn, &prec->oldsimm, &prec->simm, &prec->siml);
101  recGblInitConstantLink(&prec->siol, DBF_INT64, &prec->sval);
102 
103  if(!(pdset = (int64indset *)(prec->dset))) {
104  recGblRecordError(S_dev_noDSET,(void *)prec,"int64in: init_record");
105  return(S_dev_noDSET);
106  }
107  /* must have read_int64in function defined */
108  if ((pdset->common.number < 5) || (pdset->read_int64in == NULL)) {
109  recGblRecordError(S_dev_missingSup,(void *)prec,"int64in: init_record");
110  return(S_dev_missingSup);
111  }
112  if (pdset->common.init_record) {
113  if ((status = pdset->common.init_record(pcommon))) return status;
114  }
115  prec->mlst = prec->val;
116  prec->alst = prec->val;
117  prec->lalm = prec->val;
118  return(0);
119 }
120 
121 static long process(dbCommon *pcommon)
122 {
123  int64inRecord *prec = (int64inRecord*)pcommon;
124  int64indset *pdset = (int64indset *)(prec->dset);
125  long status;
126  unsigned char pact=prec->pact;
127  epicsTimeStamp timeLast;
128 
129  if( (pdset==NULL) || (pdset->read_int64in==NULL) ) {
130  prec->pact=TRUE;
131  recGblRecordError(S_dev_missingSup,(void *)prec,"read_int64in");
132  return(S_dev_missingSup);
133  }
134  timeLast = prec->time;
135 
136  status=readValue(prec); /* read the new value */
137  /* check if device support set pact */
138  if ( !pact && prec->pact ) return(0);
139  prec->pact = TRUE;
140 
141  recGblGetTimeStampSimm(prec, prec->simm, &prec->siol);
142 
143  if (status==0) prec->udf = FALSE;
144 
145  /* check for alarms */
146  checkAlarms(prec, &timeLast);
147  /* check event list */
148  monitor(prec);
149  /* process the forward scan link record */
150  recGblFwdLink(prec);
151 
152  prec->pact=FALSE;
153  return(status);
154 }
155 
156 static long special(DBADDR *paddr, int after)
157 {
158  int64inRecord *prec = (int64inRecord *)(paddr->precord);
159  int special_type = paddr->special;
160 
161  switch(special_type) {
162  case(SPC_MOD):
163  if (dbGetFieldIndex(paddr) == int64inRecordSIMM) {
164  if (!after)
165  recGblSaveSimm(prec->sscn, &prec->oldsimm, prec->simm);
166  else
167  recGblCheckSimm((dbCommon *)prec, &prec->sscn, prec->oldsimm, prec->simm);
168  return(0);
169  }
170  default:
171  recGblDbaddrError(S_db_badChoice, paddr, "int64in: special");
172  return(S_db_badChoice);
173  }
174 }
175 
176 #define indexof(field) int64inRecord##field
177 
178 static long get_units(DBADDR *paddr, char *units)
179 {
180  int64inRecord *prec = (int64inRecord *) paddr->precord;
181 
182  if (paddr->pfldDes->field_type == DBF_INT64) {
183  strncpy(units, prec->egu, DB_UNITS_SIZE);
184  }
185  return 0;
186 }
187 
188 static long get_graphic_double(DBADDR *paddr, struct dbr_grDouble *pgd)
189 {
190  int64inRecord *prec=(int64inRecord *)paddr->precord;
191 
192  switch (dbGetFieldIndex(paddr)) {
193  case indexof(VAL):
194  case indexof(HIHI):
195  case indexof(HIGH):
196  case indexof(LOW):
197  case indexof(LOLO):
198  case indexof(LALM):
199  case indexof(ALST):
200  case indexof(MLST):
201  case indexof(SVAL):
202  pgd->upper_disp_limit = prec->hopr;
203  pgd->lower_disp_limit = prec->lopr;
204  break;
205  default:
206  recGblGetGraphicDouble(paddr,pgd);
207  }
208  return(0);
209 }
210 
211 static long get_control_double(DBADDR *paddr, struct dbr_ctrlDouble *pcd)
212 {
213  int64inRecord *prec=(int64inRecord *)paddr->precord;
214 
215  switch (dbGetFieldIndex(paddr)) {
216  case indexof(VAL):
217  case indexof(HIHI):
218  case indexof(HIGH):
219  case indexof(LOW):
220  case indexof(LOLO):
221  case indexof(LALM):
222  case indexof(ALST):
223  case indexof(MLST):
224  case indexof(SVAL):
225  pcd->upper_ctrl_limit = prec->hopr;
226  pcd->lower_ctrl_limit = prec->lopr;
227  break;
228  default:
229  recGblGetControlDouble(paddr,pcd);
230  }
231  return(0);
232 }
233 
234 static long get_alarm_double(DBADDR *paddr, struct dbr_alDouble *pad)
235 {
236  int64inRecord *prec=(int64inRecord *)paddr->precord;
237 
238  if(dbGetFieldIndex(paddr) == indexof(VAL)){
239  pad->upper_alarm_limit = prec->hihi;
240  pad->upper_warning_limit = prec->high;
241  pad->lower_warning_limit = prec->low;
242  pad->lower_alarm_limit = prec->lolo;
243  } else recGblGetAlarmDouble(paddr,pad);
244  return(0);
245 }
246 
247 static void checkAlarms(int64inRecord *prec, epicsTimeStamp *timeLast)
248 {
249  enum {
250  range_Lolo = 1,
251  range_Low,
252  range_Normal,
253  range_High,
254  range_Hihi
255  } alarmRange;
256  static const epicsEnum16 range_stat[] = {
259  };
260 
261  double aftc, afvl;
262  epicsInt64 val, hyst, lalm;
263  epicsInt64 alev;
264  epicsEnum16 asev;
265 
266  if (prec->udf) {
267  recGblSetSevr(prec, UDF_ALARM, prec->udfs);
268  prec->afvl = 0;
269  return;
270  }
271 
272  val = prec->val;
273  hyst = prec->hyst;
274  lalm = prec->lalm;
275 
276  /* check VAL against alarm limits */
277  if ((asev = prec->hhsv) &&
278  (val >= (alev = prec->hihi) ||
279  ((lalm == alev) && (val >= alev - hyst))))
280  alarmRange = range_Hihi;
281  else
282  if ((asev = prec->llsv) &&
283  (val <= (alev = prec->lolo) ||
284  ((lalm == alev) && (val <= alev + hyst))))
285  alarmRange = range_Lolo;
286  else
287  if ((asev = prec->hsv) &&
288  (val >= (alev = prec->high) ||
289  ((lalm == alev) && (val >= alev - hyst))))
290  alarmRange = range_High;
291  else
292  if ((asev = prec->lsv) &&
293  (val <= (alev = prec->low) ||
294  ((lalm == alev) && (val <= alev + hyst))))
295  alarmRange = range_Low;
296  else {
297  alev = val;
298  asev = NO_ALARM;
299  alarmRange = range_Normal;
300  }
301 
302  aftc = prec->aftc;
303  afvl = 0;
304 
305  if (aftc > 0) {
306  /* Apply level filtering */
307  afvl = prec->afvl;
308  if (afvl == 0) {
309  afvl = (double)alarmRange;
310  } else {
311  double t = epicsTimeDiffInSeconds(&prec->time, timeLast);
312  double alpha = aftc / (t + aftc);
313 
314  /* The sign of afvl indicates whether the result should be
315  * rounded up or down. This gives the filter hysteresis.
316  * If afvl > 0 the floor() function rounds to a lower alarm
317  * level, otherwise to a higher.
318  */
319  afvl = alpha * afvl +
320  ((afvl > 0) ? (1 - alpha) : (alpha - 1)) * alarmRange;
321  if (afvl - floor(afvl) > THRESHOLD)
322  afvl = -afvl; /* reverse rounding */
323 
324  alarmRange = abs((int)floor(afvl));
325  switch (alarmRange) {
326  case range_Hihi:
327  asev = prec->hhsv;
328  alev = prec->hihi;
329  break;
330  case range_High:
331  asev = prec->hsv;
332  alev = prec->high;
333  break;
334  case range_Normal:
335  asev = NO_ALARM;
336  break;
337  case range_Low:
338  asev = prec->lsv;
339  alev = prec->low;
340  break;
341  case range_Lolo:
342  asev = prec->llsv;
343  alev = prec->lolo;
344  break;
345  }
346  }
347  }
348  prec->afvl = afvl;
349 
350  if (asev) {
351  /* Report alarm condition, store LALM for future HYST calculations */
352  if (recGblSetSevr(prec, range_stat[alarmRange], asev))
353  prec->lalm = alev;
354  } else {
355  /* No alarm condition, reset LALM */
356  prec->lalm = val;
357  }
358 }
359 
360 /* DELTA calculates the absolute difference between its arguments
361  * expressed as an unsigned 32-bit integer */
362 #define DELTA(last, val) \
363  ((epicsUInt32) ((last) > (val) ? (last) - (val) : (val) - (last)))
364 
365 static void monitor(int64inRecord *prec)
366 {
367  unsigned short monitor_mask = recGblResetAlarms(prec);
368 
369  if (prec->mdel < 0 ||
370  DELTA(prec->mlst, prec->val) > (epicsUInt32) prec->mdel) {
371  /* post events for value change */
372  monitor_mask |= DBE_VALUE;
373  /* update last value monitored */
374  prec->mlst = prec->val;
375  }
376 
377  if (prec->adel < 0 ||
378  DELTA(prec->alst, prec->val) > (epicsUInt32) prec->adel) {
379  /* post events for archive value change */
380  monitor_mask |= DBE_LOG;
381  /* update last archive value monitored */
382  prec->alst = prec->val;
383  }
384 
385  /* send out monitors connected to the value field */
386  if (monitor_mask)
387  db_post_events(prec, &prec->val, monitor_mask);
388 }
389 
390 static long readValue(int64inRecord *prec)
391 {
392  int64indset *pdset = (int64indset *) prec->dset;
393  long status = 0;
394 
395  if (!prec->pact) {
396  status = recGblGetSimm((dbCommon *)prec, &prec->sscn, &prec->oldsimm, &prec->simm, &prec->siml);
397  if (status) return status;
398  }
399 
400  switch (prec->simm) {
401  case menuYesNoNO:
402  status = pdset->read_int64in(prec);
403  break;
404 
405  case menuYesNoYES: {
406  recGblSetSevr(prec, SIMM_ALARM, prec->sims);
407  if (prec->pact || (prec->sdly < 0.)) {
408  status = dbGetLink(&prec->siol, DBR_INT64, &prec->sval, 0, 0);
409  if (status == 0) {
410  prec->val = prec->sval;
411  prec->udf = FALSE;
412  }
413  prec->pact = FALSE;
414  } else { /* !prec->pact && delay >= 0. */
415  epicsCallback *pvt = prec->simpvt;
416  if (!pvt) {
417  pvt = calloc(1, sizeof(epicsCallback)); /* very lazy allocation of callback structure */
418  prec->simpvt = pvt;
419  }
420  if (pvt) callbackRequestProcessCallbackDelayed(pvt, prec->prio, prec, prec->sdly);
421  prec->pact = TRUE;
422  }
423  break;
424  }
425 
426  default:
427  recGblSetSevr(prec, SOFT_ALARM, INVALID_ALARM);
428  status = -1;
429  }
430 
431  return status;
432 }
#define HIHI_ALARM
Definition: alarm.h:94
#define RSETNUMBER
Definition: recSup.h:92
#define FALSE
Definition: dbDefs.h:32
pvd::Status status
#define initialize
Definition: int64inRecord.c:46
#define report
Definition: int64inRecord.c:45
#define init_record
#define S_dev_missingSup
Definition: devSup.h:170
#define DBR_INT64
Definition: dbFldTypes.h:83
#define SOFT_ALARM
Definition: alarm.h:106
#define NULL
Definition: catime.c:38
unsigned int epicsUInt32
Definition: epicsTypes.h:43
#define DELTA(last, val)
Miscellaneous macro definitions.
#define get_control_double
Definition: biRecord.c:58
#define DBE_VALUE
Definition: caeventmask.h:38
#define HIGH_ALARM
Definition: alarm.h:95
#define get_enum_strs
Definition: int64inRecord.c:57
Device support routines.
#define get_array_info
Definition: int64inRecord.c:52
#define DBE_LOG
Definition: caeventmask.h:40
#define THRESHOLD
Definition: int64inRecord.c:43
#define get_units
Definition: biRecord.c:52
#define SIMM_ALARM
Definition: alarm.h:110
#define NO_ALARM
The NO_ALARM value can be used as both a severity and a status.
Definition: alarm.h:32
rset int64inRSET
Definition: int64inRecord.c:63
epicsUInt16 epicsEnum16
Definition: epicsTypes.h:47
#define get_value
Definition: int64inRecord.c:50
#define get_precision
Definition: int64inRecord.c:55
#define get_enum_str
Definition: int64inRecord.c:56
EPICS time stamp, for use from C code.
Definition: epicsTime.h:33
epicsExportAddress(rset, int64inRSET)
#define TRUE
Definition: dbDefs.h:27
if(yy_init)
Definition: scan.c:972
#define cvt_dbaddr
Definition: int64inRecord.c:51
Definition: recSup.h:67
LIBCOM_API double epicsStdCall epicsTimeDiffInSeconds(const epicsTimeStamp *pLeft, const epicsTimeStamp *pRight)
Time difference between left and right in seconds.
Definition: epicsTime.cpp:1048
#define SPC_MOD
Definition: special.h:33
int prec
Definition: reader.c:29
#define put_enum_str
Definition: int64inRecord.c:58
#define INVALID_ALARM
Definition: alarm.h:53
#define LOLO_ALARM
Definition: alarm.h:96
#define LOW_ALARM
Definition: alarm.h:97
#define get_graphic_double
Definition: biRecord.c:57
#define indexof(field)
#define put_array_info
Definition: int64inRecord.c:53
#define special
Definition: dfanoutRecord.c:50
#define S_dev_noDSET
Definition: devSup.h:169
#define get_alarm_double
Definition: aaiRecord.c:69
long long epicsInt64
Definition: epicsTypes.h:44
#define UDF_ALARM
Definition: alarm.h:108
Exporting IOC objects.