This is Unofficial EPICS BASE Doxygen Site
mbboDirectRecord.c
Go to the documentation of this file.
1 /*************************************************************************\
2 * Copyright (c) 2012 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 /* mbboDirectRecord.c - Record Support for mbboDirect records */
11 /*
12  * Original Author: Bob Dalesio
13  * Date: 10-06-93
14  */
15 
16 #include <stddef.h>
17 #include <stdlib.h>
18 #include <stdarg.h>
19 #include <stdio.h>
20 #include <string.h>
21 
22 #include "dbDefs.h"
23 #include "errlog.h"
24 #include "alarm.h"
25 #include "callback.h"
26 #include "dbAccess.h"
27 #include "dbEvent.h"
28 #include "dbFldTypes.h"
29 #include "devSup.h"
30 #include "errMdef.h"
31 #include "recSup.h"
32 #include "recGbl.h"
33 #include "special.h"
34 #include "menuOmsl.h"
35 #include "menuIvoa.h"
36 #include "menuYesNo.h"
37 
38 #define GEN_SIZE_OFFSET
39 #include "mbboDirectRecord.h"
40 #undef GEN_SIZE_OFFSET
41 #include "epicsExport.h"
42 
43 /* Create RSET - Record Support Entry Table*/
44 #define report NULL
45 #define initialize NULL
46 static long init_record(struct dbCommon *, int);
47 static long process(struct dbCommon *);
48 static long special(DBADDR *, int);
49 #define get_value NULL
50 #define cvt_dbaddr NULL
51 #define get_array_info NULL
52 #define put_array_info NULL
53 #define get_units NULL
54 static long get_precision(const DBADDR *, long *);
55 #define get_enum_str NULL
56 #define get_enum_strs NULL
57 #define put_enum_str NULL
58 #define get_graphic_double NULL
59 #define get_control_double NULL
60 #define get_alarm_double NULL
61 
63  RSETNUMBER,
64  report,
65  initialize,
67  process,
68  special,
69  get_value,
70  cvt_dbaddr,
73  get_units,
81 };
82 epicsExportAddress(rset, mbboDirectRSET);
83 
84 static void convert(mbboDirectRecord *);
85 static void monitor(mbboDirectRecord *);
86 static long writeValue(mbboDirectRecord *);
87 
88 #define NUM_BITS 32
89 
90 static long init_record(struct dbCommon *pcommon, int pass)
91 {
92  struct mbboDirectRecord *prec = (struct mbboDirectRecord *)pcommon;
93  mbbodirectdset *pdset = (mbbodirectdset *) prec->dset;
94  long status = 0;
95 
96  if (pass == 0) return 0;
97 
98  if (!pdset) {
99  recGblRecordError(S_dev_noDSET, prec, "mbboDirect: init_record");
100  return S_dev_noDSET;
101  }
102 
103  if ((pdset->common.number < 5) || (pdset->write_mbbo == NULL)) {
104  recGblRecordError(S_dev_missingSup, prec, "mbboDirect: init_record");
105  return S_dev_missingSup;
106  }
107 
108  recGblInitSimm(pcommon, &prec->sscn, &prec->oldsimm, &prec->simm, &prec->siml);
109 
110  if (recGblInitConstantLink(&prec->dol, DBF_ULONG, &prec->val))
111  prec->udf = FALSE;
112 
113  /* Initialize MASK if the user set NOBT instead */
114  if (prec->mask == 0 && prec->nobt <= 32)
115  prec->mask = ((epicsUInt64) 1u << prec->nobt) - 1;
116 
117  if (pdset->common.init_record) {
118  status = pdset->common.init_record(pcommon);
119  if (status == 0) {
120  /* Convert initial read-back */
121  epicsUInt32 rval = prec->rval;
122 
123  if (prec->shft > 0)
124  rval >>= prec->shft;
125 
126  prec->val = rval;
127  prec->udf = FALSE;
128  }
129  else if (status == 2)
130  status = 0;
131  }
132 
133  if (!prec->udf &&
134  prec->omsl == menuOmslsupervisory) {
135  /* Set initial B0 - B1F from VAL */
136  epicsUInt32 val = prec->val;
137  epicsUInt8 *pBn = &prec->b0;
138  int i;
139 
140  for (i = 0; i < NUM_BITS; i++) {
141  *pBn++ = !! (val & 1);
142  val >>= 1;
143  }
144  }
145 
146  prec->mlst = prec->val;
147  prec->oraw = prec->rval;
148  prec->orbv = prec->rbv;
149  return status;
150 }
151 
152 static long process(struct dbCommon *pcommon)
153 {
154  struct mbboDirectRecord *prec = (struct mbboDirectRecord *)pcommon;
155  mbbodirectdset *pdset = (mbbodirectdset *)(prec->dset);
156  long status = 0;
157  int pact = prec->pact;
158 
159  if ((pdset == NULL) || (pdset->write_mbbo == NULL)) {
160  prec->pact = TRUE;
161  recGblRecordError(S_dev_missingSup, prec, "write_mbbo");
162  return S_dev_missingSup;
163  }
164 
165  if (!pact) {
166  if (!dbLinkIsConstant(&prec->dol) &&
167  prec->omsl == menuOmslclosed_loop) {
168  epicsUInt32 val;
169 
170  if (dbGetLink(&prec->dol, DBR_ULONG, &val, 0, 0)) {
171  recGblSetSevr(prec, LINK_ALARM, INVALID_ALARM);
172  goto CONTINUE;
173  }
174  prec->val = val;
175  }
176  else if (prec->omsl == menuOmslsupervisory) {
177  epicsUInt8 *pBn = &prec->b0;
178  epicsUInt32 val = 0;
179  epicsUInt32 bit = 1;
180  int i;
181 
182  /* Construct VAL from B0 - B1F */
183  for (i = 0; i < NUM_BITS; i++, bit <<= 1)
184  if (*pBn++)
185  val |= bit;
186  prec->val = val;
187  }
188  else if (prec->udf) {
189  recGblSetSevr(prec, UDF_ALARM, prec->udfs);
190  goto CONTINUE;
191  }
192 
193  prec->udf = FALSE;
194  /* Convert VAL to RVAL */
195  convert(prec);
196  }
197 
198 CONTINUE:
199  if (prec->nsev < INVALID_ALARM)
200  status = writeValue(prec);
201  else {
202  switch (prec->ivoa) {
203  case menuIvoaSet_output_to_IVOV:
204  if (!prec->pact) {
205  prec->val = prec->ivov;
206  convert(prec);
207  }
208  /* No break, fall through... */
209  case menuIvoaContinue_normally:
210  status = writeValue(prec);
211  break;
212  case menuIvoaDon_t_drive_outputs:
213  break;
214  default:
215  status = -1;
216  recGblRecordError(S_db_badField, prec,
217  "mbboDirect: process Illegal IVOA field");
218  }
219  }
220 
221  /* Done if device support set PACT */
222  if (!pact && prec->pact)
223  return 0;
224 
225  prec->pact = TRUE;
226  recGblGetTimeStampSimm(prec, prec->simm, NULL);
227 
228  monitor(prec);
229 
230  /* Wrap up */
231  recGblFwdLink(prec);
232  prec->pact = FALSE;
233  return status;
234 }
235 
236 static long special(DBADDR *paddr, int after)
237 {
238  mbboDirectRecord *prec = (mbboDirectRecord *) paddr->precord;
239  int fieldIndex = dbGetFieldIndex(paddr);
240 
241  if (paddr->special == SPC_MOD && fieldIndex == mbboDirectRecordSIMM) {
242  if (!after)
243  recGblSaveSimm(prec->sscn, &prec->oldsimm, prec->simm);
244  else
245  recGblCheckSimm((dbCommon *)prec, &prec->sscn, prec->oldsimm, prec->simm);
246  return 0;
247  }
248 
249  if (!after)
250  return 0;
251 
252  switch (paddr->special) {
253  case SPC_MOD: /* Bn field modified */
254  if (prec->omsl == menuOmslsupervisory) {
255  /* Adjust VAL corresponding to the bit changed */
256  epicsUInt8 *pBn = (epicsUInt8 *) paddr->pfield;
257  epicsUInt32 bit = 1 << (pBn - &prec->b0);
258 
259  if (*pBn)
260  prec->val |= bit;
261  else
262  prec->val &= ~bit;
263 
264  prec->udf = FALSE;
265  convert(prec);
266  }
267  break;
268 
269  case SPC_RESET: /* OMSL field modified */
270  if (prec->omsl == menuOmslclosed_loop) {
271  /* Construct VAL from B0 - B1F */
272  epicsUInt8 *pBn = &prec->b0;
273  epicsUInt32 val = 0, bit = 1;
274  int i;
275 
276  for (i = 0; i < NUM_BITS; i++, bit <<= 1)
277  if (*pBn++)
278  val |= bit;
279  prec->val = val;
280  }
281  else if (prec->omsl == menuOmslsupervisory) {
282  /* Set B0 - B1F from VAL and post monitors */
283  epicsUInt32 val = prec->val;
284  epicsUInt8 *pBn = &prec->b0;
285  int i;
286 
287  for (i = 0; i < NUM_BITS; i++, pBn++, val >>= 1) {
288  epicsUInt8 oBn = *pBn;
289 
290  *pBn = !! (val & 1);
291  if (oBn != *pBn)
292  db_post_events(prec, pBn, DBE_VALUE | DBE_LOG);
293  }
294  }
295  break;
296 
297  default:
298  recGblDbaddrError(S_db_badChoice, paddr, "mbboDirect: special");
299  return S_db_badChoice;
300  }
301 
302  prec->udf = FALSE;
303  return 0;
304 }
305 
306 static long get_precision(const DBADDR *paddr,long *precision)
307 {
308  mbboDirectRecord *prec=(mbboDirectRecord *)paddr->precord;
309  if(dbGetFieldIndex(paddr)==mbboDirectRecordVAL)
310  *precision = prec->nobt;
311  else
312  recGblGetPrec(paddr,precision);
313  return 0;
314 }
315 
316 static void monitor(mbboDirectRecord *prec)
317 {
318  epicsUInt16 events = recGblResetAlarms(prec);
319 
320  if (prec->mlst != prec->val) {
321  events |= DBE_VALUE | DBE_LOG;
322  prec->mlst = prec->val;
323  }
324  if (events)
325  db_post_events(prec, &prec->val, events);
326 
327  events |= DBE_VALUE | DBE_LOG;
328  if (prec->oraw != prec->rval) {
329  db_post_events(prec, &prec->rval, events);
330  prec->oraw = prec->rval;
331  }
332  if (prec->orbv != prec->rbv) {
333  db_post_events(prec, &prec->rbv, events);
334  prec->orbv = prec->rbv;
335  }
336 }
337 
338 static void convert(mbboDirectRecord *prec)
339 {
340  /* Convert VAL to RVAL */
341  prec->rval = prec->val;
342 
343  if (prec->shft > 0)
344  prec->rval <<= prec->shft;
345 }
346 
347 static long writeValue(mbboDirectRecord *prec)
348 {
349  mbbodirectdset *pdset = (mbbodirectdset *) prec->dset;
350  long status = 0;
351 
352  if (!prec->pact) {
353  status = recGblGetSimm((dbCommon *)prec, &prec->sscn, &prec->oldsimm, &prec->simm, &prec->siml);
354  if (status) return status;
355  }
356 
357  switch (prec->simm) {
358  case menuYesNoNO:
359  status = pdset->write_mbbo(prec);
360  break;
361 
362  case menuYesNoYES: {
363  recGblSetSevr(prec, SIMM_ALARM, prec->sims);
364  if (prec->pact || (prec->sdly < 0.)) {
365  status = dbPutLink(&prec->siol, DBR_ULONG, &prec->val, 1);
366  prec->pact = FALSE;
367  } else { /* !prec->pact && delay >= 0. */
368  epicsCallback *pvt = prec->simpvt;
369  if (!pvt) {
370  pvt = calloc(1, sizeof(epicsCallback)); /* very lazy allocation of callback structure */
371  prec->simpvt = pvt;
372  }
373  if (pvt) callbackRequestProcessCallbackDelayed(pvt, prec->prio, prec, prec->sdly);
374  prec->pact = TRUE;
375  }
376  break;
377  }
378 
379  default:
380  recGblSetSevr(prec, SOFT_ALARM, INVALID_ALARM);
381  status = -1;
382  }
383 
384  return status;
385 }
#define RSETNUMBER
Definition: recSup.h:92
#define FALSE
Definition: dbDefs.h:32
pvd::Status status
int i
Definition: scan.c:967
#define get_alarm_double
unsigned short epicsUInt16
Definition: epicsTypes.h:41
#define init_record
#define S_dev_missingSup
Definition: devSup.h:170
#define cvt_dbaddr
unsigned char epicsUInt8
Definition: epicsTypes.h:39
#define SOFT_ALARM
Definition: alarm.h:106
#define NULL
Definition: catime.c:38
unsigned int epicsUInt32
Definition: epicsTypes.h:43
Miscellaneous macro definitions.
unsigned long long epicsUInt64
Definition: epicsTypes.h:45
#define SPC_RESET
Definition: special.h:35
#define NUM_BITS
#define DBE_VALUE
Definition: caeventmask.h:38
Device support routines.
#define get_value
#define put_enum_str
#define DBE_LOG
Definition: caeventmask.h:40
#define SIMM_ALARM
Definition: alarm.h:110
#define report
#define get_enum_strs
#define LINK_ALARM
Definition: alarm.h:105
#define get_array_info
#define initialize
#define get_precision
Definition: biRecord.c:53
#define get_graphic_double
#define TRUE
Definition: dbDefs.h:27
#define get_units
if(yy_init)
Definition: scan.c:972
#define put_array_info
Definition: recSup.h:67
#define SPC_MOD
Definition: special.h:33
int prec
Definition: reader.c:29
#define INVALID_ALARM
Definition: alarm.h:53
#define DBR_ULONG
Definition: dbFldTypes.h:82
#define special
Definition: dfanoutRecord.c:50
epicsExportAddress(rset, mbboDirectRSET)
#define get_enum_str
#define S_dev_noDSET
Definition: devSup.h:169
#define UDF_ALARM
Definition: alarm.h:108
rset mbboDirectRSET
#define get_control_double
Exporting IOC objects.