-
Notifications
You must be signed in to change notification settings - Fork 0
/
EzcaQueue.c
414 lines (360 loc) · 13 KB
/
EzcaQueue.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*************************************************************************\
* Copyright (c) 2002 The University of Chicago, as Operator of Argonne
* National Laboratory.
* Copyright (c) 2002 The Regents of the University of California, as
* Operator of Los Alamos National Laboratory.
* This file is distributed subject to a Software License Agreement found
* in the file LICENSE that is included with this distribution.
\*************************************************************************/
/* EzcaQueue.c
* special monitor queue for scalar type record
*
* Original Author: Ben-chin Cha
* Date: 11-21-95
*
* Monitor queue can operate in 3 modes
* 1 - clear buffer for each get
* 2 - new event added until the buffer is full,
* a user has to use zero to clear the buffer
* 3 - most current nmax values are kept in buffer
*/
#ifdef _WIN32
#include "windows.h"
#endif
#include <dbDefs.h>
#include <cadef.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#ifdef EZCA
#include <ezca.h>
#endif
#define epicsExportSharedSymbols
#include "EzcaScan.h"
//chandata *pchandata;
/**
*/
EVENT_QUEUE * epicsShareAPI Ezca_queueAlloc(int size)
{
EVENT_QUEUE *pevent;
pevent = (EVENT_QUEUE *)calloc(1,sizeof(EVENT_QUEUE));
pevent->pvalues = (double *)calloc(size,sizeof(double));
if (pevent->pvalues == NULL) {
fprintf(stderr,"Error: Ezca_queueAlloc failed to calloc !\n");
exit(CA_ALLOC);
}
pevent->maxvalues = size;
pevent->numvalues = 0;
pevent->overflow = 0;
return(pevent);
}
/******************************************************
queuing value change event callback
******************************************************/
void Ezca_queueValueChangeCallback(args)
struct event_handler_args args;
{
chandata *pchandata;
double time;
pchandata = (chandata *)args.usr;
#ifdef ACCESS_SECURITY
if (args.status == ECA_NORMAL) {
#endif
if (CA.devprflag > 1)
{
fprintf(stderr,"Old: name=%s, value=%f, stat=%d, sevr=%d, event=%d\n",
ca_name(pchandata->chid),
pchandata->value, pchandata->status,
pchandata->severity,pchandata->event);
}
pchandata->value = ((struct dbr_time_double *)args.dbr)->value;
pchandata->status= ((struct dbr_time_double *)args.dbr)->status;
pchandata->severity = ((struct dbr_time_double *)args.dbr)->severity;
pchandata->event = 2;
pchandata->stamp = ((struct dbr_time_double *)args.dbr)->stamp;
if (CA.devprflag > 1) {
time = Ezca_iocClockTime(&pchandata->stamp);
fprintf(stderr,"New: name=%s, value=%f, stat=%d, sevr=%d, event=%d\n",
ca_name(pchandata->chid),
pchandata->value, pchandata->status,
pchandata->severity,pchandata->event);
}
#ifdef ACCESS_SECURITY
}
else
if (CA.devprflag > 0)
fprintf(stderr,"Diagnostic: Read access denied : %s\n",
ca_name(pchandata->chid));
#endif
if (pchandata->p_event_queue->numvalues < pchandata->p_event_queue->maxvalues) {
*(pchandata->p_event_queue->pvalues+pchandata->p_event_queue->numvalues) =
pchandata->value;
pchandata->p_event_queue->numvalues += 1;
if (pchandata->p_event_queue->numvalues >=
pchandata->p_event_queue->maxvalues)
{
pchandata->p_event_queue->overflow = 1;
}
}
}
/******************************************************
circular buffer queuing value change event callback
******************************************************/
void Ezca_queueValueChangeCallback3(args)
struct event_handler_args args;
{
chandata *pchandata;
int ne;
double time;
pchandata = (chandata *)args.usr;
#ifdef ACCESS_SECURITY
if (args.status == ECA_NORMAL) {
#endif
if (CA.devprflag > 1)
{
fprintf(stderr,"Old: name=%s, value=%f, stat=%d, sevr=%d, event=%d\n",
ca_name(pchandata->chid),
pchandata->value, pchandata->status,
pchandata->severity,pchandata->event);
}
pchandata->value = ((struct dbr_time_double *)args.dbr)->value;
pchandata->status= ((struct dbr_time_double *)args.dbr)->status;
pchandata->severity = ((struct dbr_time_double *)args.dbr)->severity;
pchandata->event = 2;
pchandata->stamp = ((struct dbr_time_double *)args.dbr)->stamp;
if (CA.devprflag > 1) {
time = Ezca_iocClockTime(&pchandata->stamp);
fprintf(stderr,"New: name=%s, value=%f, stat=%d, sevr=%d, event=%d\n",
ca_name(pchandata->chid),
pchandata->value, pchandata->status,
pchandata->severity,pchandata->event);
}
#ifdef ACCESS_SECURITY
}
else
if (CA.devprflag > 0) fprintf(stderr,"Diagnostic: Read access denied : %s\n",
ca_name(pchandata->chid));
#endif
if (pchandata->p_event_queue->numvalues <= pchandata->p_event_queue->maxvalues)
{
*(pchandata->p_event_queue->pvalues+pchandata->p_event_queue->numvalues) = pchandata->value;
pchandata->p_event_queue->numvalues += 1;
if (pchandata->p_event_queue->numvalues >=
pchandata->p_event_queue->maxvalues)
{
pchandata->p_event_queue->overflow = 1;
}
}
else
{
pchandata->p_event_queue->numvalues += 1;
pchandata->p_event_queue->overflow = 1;
ne = pchandata->p_event_queue->numvalues%pchandata->p_event_queue->maxvalues;
*(pchandata->p_event_queue->pvalues+ne) = pchandata->value;
}
}
/******************************************************
get event queue values for a monitored pv, return CA.CA_ERR
******************************************************/
int epicsShareAPI Ezca_queueGet(pvName,vals,overflow,mode,num)
int *overflow,mode,*num;
char *pvName;
double *vals;
{
int i,command_error=0,ne,ii;
chandata *pchandata;
chandata *pchan;
*num = CA_FAIL;
ca_pend_event(0.00001);
Ezca_find_dev(pvName,&pchandata);
pchan=pchandata;
pchan->type = ca_field_type(pchan->chid);
if (pchan->state != cs_conn)
{
command_error = CA_FAIL;
}
else if (pchan->evid )
{
if (mode == 1) {
for (i=0;i<pchan->p_event_queue->numvalues;i++)
{
vals[i] = pchan->p_event_queue->pvalues[i];
}
if (pchan->event) {
*num = pchan->p_event_queue->numvalues;
*overflow = pchan->p_event_queue->overflow;
pchan->p_event_queue->numvalues = 0;
pchan->p_event_queue->overflow = 0;
pchan->event = 0; /* reset after read*/
}
}
if (mode == 2)
{
for (i=0;i<pchan->p_event_queue->numvalues;i++) {
vals[i] = pchan->p_event_queue->pvalues[i];
}
/* reset after read*/
*num = pchan->p_event_queue->numvalues;
*overflow = pchan->p_event_queue->overflow;
}
if (mode == 3)
{
if (pchan->p_event_queue->numvalues < pchan->p_event_queue->maxvalues)
{
for (i=0;i<pchan->p_event_queue->numvalues;i++) {
vals[i] = pchan->p_event_queue->pvalues[i];
*num = pchan->p_event_queue->numvalues;
*overflow = pchan->p_event_queue->overflow;
}
}
else
{
ne = pchan->p_event_queue->numvalues%pchan->p_event_queue->maxvalues;
for (i=0;i<pchan->p_event_queue->maxvalues;i++) {
ii = ne + 1 + i;
if (ii >= pchan->p_event_queue->maxvalues)
ii = ii - pchan->p_event_queue->maxvalues;
vals[i] = pchan->p_event_queue->pvalues[ii];
pchan->p_event_queue->overflow = 1;
}
*num = pchan->p_event_queue->maxvalues;
*overflow = pchan->p_event_queue->overflow;
}
pchan->event = 0;
}
if (CA.devprflag > 0) {
fprintf(stderr,"Ezca_queueGet: name=%s, value=%f, status=%d, event=%d\n",
ca_name(pchan->chid),pchan->value,
pchan->status,pchan->event);
fprintf(stderr,"p_event_queue->values: ");
for (i=0;i<*num;i++) {
fprintf(stderr,"%f ",vals[i]);
}
fprintf(stderr,"\n");
}
} else {
if (CA.devprflag >= 0)
fprintf(stderr,"Ezca_queueGet Error: %s is not monitored yet.\n",
ca_name(pchan->chid));
}
return (command_error);
}
/******************************************************
zero a monitor queue list , return CA.CA_ERR
******************************************************/
int epicsShareAPI Ezca_queueZero(int noName,char **pvName)
{
int i,command_error=0,size;
chandata **chanlist;
chandata *list,*snode,*pchan;
chanlist = (chandata **)calloc(noName,sizeof(chandata *));
command_error = Ezca_pvlist_search(noName,pvName,&list, &chanlist);
snode = list;
while (snode) {
pchan = snode;
pchan->type = ca_field_type(pchan->chid);
if (pchan->state != cs_conn)
{
command_error = CA_FAIL;
}
else
{
if (pchan->evid) {
size = pchan->p_event_queue->maxvalues;
for (i=0;i<size;i++) {
pchan->p_event_queue->pvalues[i] = 0.;
}
pchan->p_event_queue->numvalues = 0;
pchan->p_event_queue->overflow = 0;
pchan->event = 0;
}
}
snode = snode->next;
}
ca_pend_event(CA.PEND_EVENT_TIME);
return(command_error);
}
/******************************************************
add a circular buffer monitor queue list only double value, return CA.CA_ERR
******************************************************/
int epicsShareAPI Ezca_queueAdd(mode,maxvalues,noName,pvName)
int noName,mode,maxvalues;
char **pvName;
{
int i=0,status=0,command_error=0;
chandata *list,*snode,*pchan;
chandata **chanlist;
chanlist = (chandata **)calloc(noName,sizeof(chandata *));
command_error = Ezca_pvlist_search(noName,pvName,&list, &chanlist);
snode = list;
while (snode) {
pchan = snode;
pchan->type = ca_field_type(pchan->chid);
if (pchan->state != cs_conn)
command_error = CA_FAIL;
else if (pchan->evid == NULL) {
if (mode < 3) {
status = ca_add_masked_array_event(DBR_TIME_DOUBLE,0,
pchan->chid,
Ezca_queueValueChangeCallback,
pchan,
(float)0,(float)0,(float)0,
&(pchan->evid),
DBE_VALUE | DBE_ALARM);
}
if (mode == 3) {
status = ca_add_masked_array_event(DBR_TIME_DOUBLE,0,
pchan->chid,
Ezca_queueValueChangeCallback3,
pchan,
(float)0,(float)0,(float)0,
&(pchan->evid),
DBE_VALUE | DBE_ALARM);
}
pchan->error = status;
pchan->p_event_queue =
(EVENT_QUEUE *)Ezca_queueAlloc(maxvalues);
/* automatically add a change connection event */
Ezca_connectionAddEvent(pchan);
if (CA.devprflag > 0)
fprintf(stderr,"name=%s, evid=%p\n",
ca_name(pchan->chid),pchan->evid);
}
snode = snode->next;
i++;
}
ca_pend_event(CA.PEND_EVENT_TIME);
return(command_error);
}
/******************************************************
clear a monitor queue list , return CA.CA_ERR
******************************************************/
int epicsShareAPI Ezca_queueClear(noName,pvName)
int noName;
char **pvName;
{
int status,command_error=0;
chandata *list,*snode,*pchan;
chandata **chanlist;
chanlist = (chandata **)calloc(noName,sizeof(chandata *));
command_error = Ezca_pvlist_search(noName,pvName,&list, &chanlist);
snode = list;
while (snode) {
pchan = snode;
pchan->type = ca_field_type(pchan->chid);
if (pchan->state != cs_conn)
command_error = CA_FAIL;
else
if (pchan->evid) {
status = ca_clear_event(pchan->evid);
pchan->error = status;
pchan->evid = NULL;
free(pchan->p_event_queue->pvalues);
free(pchan->p_event_queue);
pchan->p_event_queue = NULL;
}
snode = snode->next;
}
ca_pend_event(CA.PEND_EVENT_TIME);
return(command_error);
}