-
Notifications
You must be signed in to change notification settings - Fork 0
/
cau.c
2666 lines (2528 loc) · 83.4 KB
/
cau.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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*************************************************************************\
* 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.
\*************************************************************************/
/* @(#)cau.c 1.13 8/11/93
* Author: Roger A. Cole
* Date: 10-11-90
*
* Experimental Physics and Industrial Control System (EPICS)
*
* make options
* -DvxWorks makes a version for VxWorks
* -DNDEBUG don't compile assert() checking
*/
/*+/mod***********************************************************************
* TITLE cau - channel access utility
*
* DESCRIPTION
* `cau' is a utility which provides some commonly used Channel Access
* capabilities. These include:
* o storing a new value for a channel
* o getting the present value for one or more channels
* o sending software signal generator output(s) to one or more
* channels
* o monitoring one or more channels
* o some simple tests for monitored channels
*
* WISH LIST
* o handle waveforms--put and signal generation
* o investigate usefulness of additional commands: snapshot, restore,
* pause (waiting for operator), delay timeInterval, assert condition,
* abort (as script), waitUntil condition
*
*-***************************************************************************/
#include <string.h>
#include <stdlib.h>
#include "genDefs.h"
#include "cmdDefs.h"
#include "cadef.h"
#include "db_access.h"
#include "nextFieldSubrDefs.h"
#include "cvtNumbersDefs.h"
#include "epicsTime.h"
#ifdef vxWorks
/*----------------------------------------------------------------------------
* includes and defines for VxWorks compile
*---------------------------------------------------------------------------*/
# include <vxWorks.h>
# include <stdioLib.h>
# include <ctype.h>
# include <sigLib.h>
# include <setjmp.h>
# include <taskLib.h>
# include <genTasks.h>
# define MAXPRIO 160
#else
/*----------------------------------------------------------------------------
* includes and defines for Sun compile
*---------------------------------------------------------------------------*/
# include <stdio.h>
# include <ctype.h>
# include <math.h>
# include <signal.h>
# include <setjmp.h>
#ifndef _WIN32
# include <unistd.h>
#endif
#endif
#define CAU_ABS(val) ((val) >= 0 ? (val) : -(val))
/*/subhead CAU_CHAN--------------------------------------------------------
* CAU_CHAN
*
* A cau channel descriptor contains the data necessary to
* generate the desired signal and the buffers used for ca_get
*----------------------------------------------------------------------------*/
typedef struct cauSetChannel {
double interval; /* desired interval, in seconds */
double jitter; /* allowed jitter, in seconds */
struct cauSetChannel *pPrev; /* link to previous channel */
struct cauSetChannel *pNext; /* link to next channel */
CX_CMD *pCxCmd; /* ptr to cmd context, for printing */
char name[db_name_dim]; /* channel name (as entered) */
char *units; /* pointer to units, or NULL */
USHORT reqCount; /* requested count, for arrays */
USHORT elCount; /* native count of channel */
chtype dbfType; /* native type of channel */
chtype dbrType; /* desired type for retrieved data */
chid pCh; /* channel pointer */
evid pEv; /* event pointer */
union db_access_val *pBuf; /* pointer to buffer */
union db_access_val *pGRBuf; /* pointer to graphics info buffer */
long (*pFn)(); /* function to call */
TS_STAMP lastMonTime; /* last time handler was called */
int lastMonErr; /* 1 says err msg printed */
struct {
short endVal; /* end value for signal */
short begVal; /* begin value for signal */
short currVal; /* current value for signal */
short addVal; /* amount to add for next value */
char string[db_strval_dim];
} str;
struct {
char endVal; /* end value for signal */
char begVal; /* begin value for signal */
char currVal; /* current value for signal */
char addVal; /* amount to add for next value */
} chr;
struct {
float endVal; /* end value for signal */
float begVal; /* begin value for signal */
float currVal; /* current value for signal */
float addVal; /* amount to add for next value */
} flt;
struct {
short endVal; /* end value for signal */
short begVal; /* begin value for signal */
short currVal; /* current value for signal */
short addVal; /* amount to add for next value */
} shrt;
struct {
double endVal; /* end value for signal */
double begVal; /* begin value for signal */
double currVal; /* current value for signal */
double addVal; /* amount to add for next value */
} dbl;
struct {
long endVal; /* end value for signal */
long begVal; /* begin value for signal */
long currVal; /* current value for signal */
long addVal; /* amount to add for next value */
} lng;
struct {
short endVal; /* end value for signal */
short begVal; /* begin value for signal */
short currVal; /* current value for signal */
short addVal; /* amount to add for next value */
} enm;
short nSteps; /* number of steps in signal */
TS_STAMP nextTime; /* time for next step in signal */
double secPerStep; /* seconds between steps */
} CAU_CHAN;
/*/subhead CAU_DESC-------------------------------------------------------
* CAU_DESC
*
* A cau descriptor is the `master handle' which is used for
* handling channels. The CAU_DESC is created empty, and then filled
* in with additional calls.
*----------------------------------------------------------------------------*/
#ifdef vxWorks
# define CauLock semTake(pglCauDesc->semLock, WAIT_FOREVER)
# define CauUnlock semGive(pglCauDesc->semLock)
# define CauLockCheck semClear(pglCauDesc->semLock)
# define CauLockInitAndLock semInit(pglCauDesc->semLock)
#endif
typedef struct {
#ifdef vxWorks
TASK_ID id; /* ID of task */
#endif
int status; /* status of task--initially ERROR */
int stop; /* task requested to stop if != 0 */
int stopped; /* task has stopped if != 0 */
int serviceNeeded; /* task needs servicing */
int serviceDone; /* task servicing completed */
jmp_buf sigEnv; /* environment for longjmp at signal time */
} CAU_TASK_INFO;
typedef struct cauDesc {
#ifdef vxWorks
SEM_ID semLock;
int showStack; /* show stack stats on task terminate */
#endif
CAU_TASK_INFO cauTaskInfo;
CAU_TASK_INFO cauInTaskInfo;
CX_CMD *pCxCmd; /* pointer to command context */
CAU_CHAN *pChanHead; /* pointer to head of channel list */
CAU_CHAN *pChanTail; /* pointer to tail of channel list */
CAU_CHAN *pChanConnHead; /* pointer to head of channel connect list */
CAU_CHAN *pChanConnTail; /* pointer to tail of channel connect list */
double secPerStep; /* seconds per step for signal generation */
int nSteps; /* number of steps per cycle for sig gen */
double begVal; /* begin value for generated signal */
double endVal; /* end value for generated signal */
} CAU_DESC;
/*-----------------------------------------------------------------------------
* prototypes
*----------------------------------------------------------------------------*/
int cau();
static void cauCaException();
static void cauCmdProcess();
static long cauTask();
#ifdef vxWorks
static long cauTaskCheck();
static long cauInit();
#endif
static void cauTaskSigHandler();
static char *cauInTask();
static void cau_deadband();
static void cau_debug();
static void cau_delete();
static void cau_get();
static void cau_info();
static void cau_interval(), cau_interval_deadTime_test();
static void cau_monitor();
static void cau_put();
static void cau_ramp();
static CAU_CHAN * cauChanAdd();
static long cauChanDel();
static CAU_CHAN *cauChanFind();
static long cauFree();
static void cauGetAndPrint();
static void cauInitAtStartup();
static void cauMonitor();
static void cauPrintBuf();
static void cauPrintBufArray();
static void cauPrintInfo();
static void cauSigGen();
static long cauSigGenGetParams();
static long cauSigGenPut();
static long cauSigGenRamp();
static void cauSigGenRampAdd();
/*-----------------------------------------------------------------------------
* global definitions
*----------------------------------------------------------------------------*/
static CX_CMD glCauCxCmd;
static CX_CMD *pglCauCxCmd=NULL;
static CAU_DESC glCauDesc;
static CAU_DESC *pglCauDesc=NULL;
static int glCauDebug=0;
static HELP_TOPIC helpDebug; /* help info--debug command */
static HELP_TOPIC helpInterval; /* help info--interval command */
static HELP_TOPIC helpRamp; /* help info--ramp command */
static unsigned long glCauDeadband=DBE_VALUE | DBE_ALARM;
static char *glCauMDEL_msg="prior to ca_add_masked_array_event (MDEL)";
static char *glCauADEL_msg="prior to ca_add_masked_array_event (ADEL)";
/*+/subr**********************************************************************
* NAME cauCaDebug...
*-*/
epicsTimeStamp cauDbStamp;
static char cauDbStampTxt[28];
static void cauCaDebug(message, invokeVal)
char *message;
int invokeVal;
{
if (glCauDebug <= invokeVal)
return;
(void)epicsTimeGetCurrent(&cauDbStamp);
(void)epicsTimeToStrftime(cauDbStampTxt,28,"%m-%d-%y %H:%M:%S.%09f",&cauDbStamp);
(void)printf("%s %s\n", &cauDbStampTxt[12], message);
}
static void cauCaDebugDbrAndName(message, type, name, invokeVal)
char *message;
chtype type;
char *name;
int invokeVal;
{
if (glCauDebug <= invokeVal)
return;
(void)epicsTimeGetCurrent(&cauDbStamp);
(void)epicsTimeToStrftime(cauDbStampTxt,28,"%m-%d-%y %H:%M:%S.%09f",&cauDbStamp);
(void)printf("%s %s (%s) for %s\n",
&cauDbStampTxt[12], message, dbr_type_to_text(type), name);
}
static void cauCaDebugName(message, name, invokeVal)
char *message;
char *name;
int invokeVal;
{
if (glCauDebug <= invokeVal)
return;
(void)epicsTimeGetCurrent(&cauDbStamp);
(void)epicsTimeToStrftime(cauDbStampTxt,28,"%m-%d-%y %H:%M:%S.%09f",&cauDbStamp);
(void)printf("%s %s for %s\n", &cauDbStampTxt[12], message, name);
}
static void cauCaDebugStat(message, stat, invokeVal)
char *message;
long stat;
int invokeVal;
{
if (glCauDebug <= invokeVal)
return;
(void)epicsTimeGetCurrent(&cauDbStamp);
(void)epicsTimeToStrftime(cauDbStampTxt,28,"%m-%d-%y %H:%M:%S.%09f",&cauDbStamp);
(void)printf("%s %s %s\n", &cauDbStampTxt[12], message, ca_message(stat));
}
#ifndef vxWorks
int main()
{
return cau();
}
#endif
/*+/subr**********************************************************************
* NAME cau - shell callable interface for cau
*
* DESCRIPTION
* This routine is the only part of cau which is intended to be
* called directly from the shell. Several functions are performed here:
* o if the cauTask doesn't exist, spawn it. Ideally, this
* stage would also detect if the cauTask is suspended
* and take appropriate action.
* o if the cauInTask doesn't exist, spawn it. If it does exist, an
* error exists.
* o wait until the cauInTask quits, then return to the shell. If
* other tasks belonging to cau are being stopped, then this
* routine waits until they, too, are stopped before returning to
* the shell.
*
* RETURNS
* OK, or
* ERROR
*
* BUGS
* o stack size and priority should come from tasks.h
* o there are lots of "holes" in detecting whether tasks exist, are
* suspended, etc.
*
*-*/
int
cau()
{
if (pglCauCxCmd == NULL ||
(pglCauDesc->cauTaskInfo.stop == 1 &&
pglCauDesc->cauInTaskInfo.stop == 1) ) {
glCauDebug = 0;
pglCauDesc = &glCauDesc;
pglCauCxCmd = &glCauCxCmd;
cauInitAtStartup(pglCauDesc, pglCauCxCmd);
#ifdef vxWorks
assert(taskNameToId("cauTask") == ERROR);
assert(taskNameToId("cauInTask") == ERROR);
pglCauDesc->showStack = 0;
#endif
pglCauDesc->cauTaskInfo.status = ERROR;
pglCauDesc->cauTaskInfo.stop = 0;
pglCauDesc->cauTaskInfo.stopped = 1;
pglCauDesc->cauInTaskInfo.status = ERROR;
pglCauDesc->cauInTaskInfo.stop = 0;
pglCauDesc->cauInTaskInfo.stopped = 1;
}
pglCauDesc->cauInTaskInfo.serviceNeeded = 0;
pglCauDesc->cauInTaskInfo.serviceDone = 1;
#ifdef vxWorks
/*-----------------------------------------------------------------------------
* cauTask
* check its status; spawn if it doesn't exist
*----------------------------------------------------------------------------*/
stat = cauTaskCheck(pglCauCxCmd, "cauTask", &pglCauDesc->cauTaskInfo);
if (stat == ERROR)
return ERROR;
if (pglCauDesc->cauTaskInfo.status == ERROR) {
pglCauDesc->cauTaskInfo.id = taskSpawn("cauTask", MAXPRIO,
VX_STDIO | VX_FP_TASK, 50000, cauTask, &pglCauCxCmd);
}
if (GenTaskNull(pglCauDesc->cauTaskInfo.id)) {
(void)printf("error spawning cauTask\n");
return ERROR;
}
pglCauDesc->cauTaskInfo.status = OK;
pglCauDesc->cauTaskInfo.stopped = 0;
/*-----------------------------------------------------------------------------
* cauInTask
* check its status; spawn if it doesn't exist
*----------------------------------------------------------------------------*/
stat = cauTaskCheck(pglCauCxCmd, "cauInTask", &pglCauDesc->cauInTaskInfo);
if (stat == ERROR)
return ERROR;
if (pglCauDesc->cauInTaskInfo.status == ERROR) {
pglCauDesc->cauInTaskInfo.id = taskSpawn("cauInTask", MAXPRIO,
VX_STDIO | VX_FP_TASK, 50000, cauInTask, &pglCauCxCmd);
}
if (GenTaskNull(pglCauDesc->cauInTaskInfo.id)) {
(void)printf("error spawning cauInTask\n");
return ERROR;
}
pglCauDesc->cauInTaskInfo.status = OK;
pglCauDesc->cauInTaskInfo.stopped = 0;
/*-----------------------------------------------------------------------------
* wait for cauInTask to exit and then return to the shell. If other
* "cau tasks" are also exiting, wait until their wrapups are complete.
*----------------------------------------------------------------------------*/
while (!pglCauDesc->cauInTaskInfo.stopped)
taskSleep(SELF, 1, 0);
if (pglCauDesc->cauTaskInfo.stop) {
while (!pglCauDesc->cauTaskInfo.stopped)
taskSleep(SELF, 1, 0);
}
#else
cauTask(&pglCauCxCmd);
#endif
return OK;
}
static void
cauCaException(arg)
struct exception_handler_args arg;
{
chid pCh;
int stat;
pCh = arg.chid;
stat = arg.stat;
(void)printf("CA exception handler entered\n");
if(pCh){
(void)printf("CA channel name=%s\n", ca_name(pCh));
}
(void)printf("CA status=%s\n", ca_message(stat));
(void)printf("CA context=%s\n", arg.ctx);
(void)printf("CA op=%ld data type=%s count=%ld\n",
arg.op,
dbr_type_to_text(arg.type),
arg.count);
}
/*+/subr**********************************************************************
* NAME cauTask - main processing task for cau
*
* DESCRIPTION
*
* RETURNS
* OK, or
* ERROR
*
* BUGS
* o text
*
*-*/
static long
cauTask(ppCxCmd)
CX_CMD **ppCxCmd;
{
long stat;
CX_CMD *pCxCmd;
int loopCount;
int sigNum;
pCxCmd = *ppCxCmd;
#ifdef vxWorks
CauLockInitAndLock;
CauUnlock;
#endif
genSigInit(cauTaskSigHandler);
if ((sigNum = setjmp(pglCauDesc->cauTaskInfo.sigEnv)) != 0) {
printf("cau: signal detected: %d\n", sigNum);
goto cauTaskWrapup;
}
stat = ca_task_initialize();
assert(stat == ECA_NORMAL);
stat = ca_add_exception_event(cauCaException, NULL);
assert(stat == ECA_NORMAL);
/*----------------------------------------------------------------------------
* "processing loop"
*---------------------------------------------------------------------------*/
loopCount = 10;
while (!pglCauDesc->cauTaskInfo.stop) {
cauCaDebug("main loop, prior to ca_pend_event(0.001)", 2);
stat = ca_pend_event(0.001);
cauCaDebugStat("main loop, back from ca_pend_event(0.001)", stat, 2);
assert(stat != ECA_EVDISALLOW);
cauSigGen(pCxCmd, pglCauDesc);
if (--loopCount <= 0) {
loopCount = 10;
cau_interval_deadTime_test(pglCauDesc);
}
#ifndef vxWorks
cauInTask(ppCxCmd);
#endif
if (pglCauDesc->cauInTaskInfo.serviceNeeded) {
cauCmdProcess(ppCxCmd, pglCauDesc);
pCxCmd = *ppCxCmd;
pglCauDesc->cauInTaskInfo.serviceNeeded = 0;
pglCauDesc->cauInTaskInfo.serviceDone = 1;
}
#ifdef vxWorks /* fprintf on vxWorks not flushed on \n */
fflush(stdout);
fflush(pCxCmd->dataOut);
fflush(stderr);
taskSleep(SELF, 0, 100000); /* wait .1 sec */
#else
sleep(1);
/* MDA - usleep isn't POSIX
usleep(100000);
*/
#endif
}
cauTaskWrapup:
stat = cauFree(pCxCmd, pglCauDesc);
assert(stat == OK);
while ((*ppCxCmd)->pPrev != NULL)
cmdCloseContext(ppCxCmd);
pCxCmd = *ppCxCmd;
stat = ca_task_exit();
if (stat != ECA_NORMAL) {
(void)printf("cau: ca_task_exit error: %s\n", ca_message(stat));
}
pglCauDesc->cauInTaskInfo.stop = 1;
#ifdef vxWorks
while (pglCauDesc->cauInTaskInfo.stopped == 0) {
taskSleep(SELF, 1, 0);
}
#endif
if (pCxCmd->dataOutRedir) {
(void)printf("closing dataOut\n");
fclose(pCxCmd->dataOut);
}
#ifdef vxWorks
if (pglCauDesc->showStack)
checkStack(pglCauDesc->cauTaskInfo.id);
#endif
pglCauDesc->cauTaskInfo.stopped = 1;
pglCauDesc->cauTaskInfo.status = ERROR;
return 0;
}
/*+/subr**********************************************************************
* NAME cauTaskCheck - check on a task
*
* DESCRIPTION
* Check to see if a task exists (based on its name). If it does:
* o verify that its status in the CAU_TASK_INFO block is OK
* o verify that it isn't suspended
*
* If the task doesn't exist:
* o verify that its status in the CAU_TASK_INFO block is ERROR
*
* RETURNS
* OK, or
* ERROR if an inconsistency is found
*
* BUGS
* o This isn't a general purpose routine. In particular, under SunOS,
* the assumption is that the task doesn't exist.
*
*-*/
#ifdef vxWorks
static long
cauTaskCheck(pCxCmd, name, pTaskInfo)
CX_CMD *pCxCmd; /* I pointer to command context */
char *name; /* I name of task */
CAU_TASK_INFO *pTaskInfo;/* IO pointer to task info block */
{
if (taskNameToId(name) == ERROR) {
assert(pTaskInfo->status == ERROR);
pTaskInfo->stop = 0;
pTaskInfo->stopped = 0;
}
else {
assert(pTaskInfo->status == OK);
assert(!GenTaskNull(pTaskInfo->id));
if (taskIsSuspended(pTaskInfo->id)) {
(void)printf("%s is suspended\n", name);
return ERROR;
}
}
return OK;
}
#endif
/*+/subr**********************************************************************
* NAME cauTaskSig - signal handling and initialization
*
* DESCRIPTION
* These routines set up for the signals to be caught for cauTask
* and handle the signals when they occur.
*
* RETURNS
* void
*
* BUGS
* o not all signals are caught
* o under VxWorks, taskDeleteHookAdd isn't used
* o it's not clear how useful it is to catch the signals which originate
* from the keyboard
*
*-*/
static void
cauTaskSigHandler(signo)
int signo;
{
signal(signo, SIG_DFL);
longjmp(pglCauDesc->cauTaskInfo.sigEnv, 1);
}
/*+/subr**********************************************************************
* NAME cauInTask - handle the keyboard and keyboard commands
*
* DESCRIPTION
* Gets input text and passes the input to cauTask.
*
* This task exists for two primary purposes: 1) avoid the possibility
* of blocking cauTask while waiting for operator input; and
* 2) allow detaching cauTask from the keyboard while still
* allowing cauTask to run.
*
* It is important to note that this task does no command processing
* on cauTask's behalf--all commands relating to cauTask
* are executed in cauTask's own context. Some
* commands are related to getting input--these commands are processed
* by this task and are never visible to cauTask.
*
* This task waits for input to be available from the keyboard. When
* an input line is ready, this task does some preliminary processing:
* o if the command is "quit" (or ^D), cauTask is signalled
* and this task wraps itself up and returns.
* o if the command is "dataOut", this task sets a new destination
* for data output, closing the previous destination, if appropriate,
* and opening the new destination, if appropriate.
* o if the command is "bg", this task wraps itself up and returns.
* A flag is left in the command context indicating that cauInTask
* doesn't exist any more.
* o if the command is "source", this task pushes down a level in
* the command context and begins reading commands from the file.
* When EOF occurs on the file, cauInTask closes the file, pops
* to the previous level in the command context, and resumes
* reading at that level.
* o otherwise, cauTask is signalled and this task
* goes into a sleeping loop until cauTask signals
* that it is ready for the next command.
*
* Ideally, this task would also support a mechanism for cauTask
* to wait explicitly for keyboard input. An example would be when
* operator permission is needed prior to taking an action.
*
* RETURNS
* OK, or
* ERROR
*
* BUGS
* o doesn't support socket I/O
* o doesn't support flexible redirection of output
* o the implementation of "dataOut" is clumsy
*
*-*/
static char *
cauInTask(ppCxCmd)
CX_CMD **ppCxCmd; /* IO ptr to pointer to command context */
{
char *input = NULL;
/*----------------------------------------------------------------------------
* wait for input from keyboard. When some is received, signal caller,
* wait for caller to process it, and then wait for some more input.
*
* stay in the main loop until get ^D or 'quit'
*---------------------------------------------------------------------------*/
while (1) {
#ifdef vxWorks
while (pglCauDesc->cauInTaskInfo.serviceDone == 0) {
if (pglCauDesc->cauInTaskInfo.stop == 1)
goto cauInTaskDone;
taskSleep(SELF, 0, 500000); /* sleep .5 sec */
}
#endif
input = cmdRead(ppCxCmd);
#ifdef vxWorks
if (pglCauDesc->cauInTaskInfo.stop == 1)
goto cauInTaskDone;
#endif
if (input == NULL)
#ifdef vxWorks
;
#else
goto cauInTaskDone;
#endif
else if (strcmp((*ppCxCmd)->pCommand, "dataOut") == 0) {
if ((*ppCxCmd)->dataOutRedir)
fclose ((*ppCxCmd)->dataOut);
(*ppCxCmd)->dataOutRedir = 0;
if (nextNonSpaceField( &(*ppCxCmd)->pLine, &(*ppCxCmd)->pField,
&(*ppCxCmd)->delim) < 1)
(*ppCxCmd)->dataOut = stdout;
else {
(*ppCxCmd)->dataOut = fopen((*ppCxCmd)->pField, "a");
if ((*ppCxCmd)->dataOut == NULL) {
(void)printf("couldn't open %s\n", (*ppCxCmd)->pField);
(*ppCxCmd)->dataOut = stdout;
}
else
(*ppCxCmd)->dataOutRedir = 1;
}
}
#ifdef vxWorks
else if (strcmp((*ppCxCmd)->pCommand, "bg") == 0) {
if (cmdBgCheck(*ppCxCmd) == OK)
goto cauInTaskDone;
}
else if (strcmp((*ppCxCmd)->pCommand, "quit") == 0) {
pglCauDesc->cauInTaskInfo.serviceDone = 0;
pglCauDesc->cauInTaskInfo.serviceNeeded = 1;
goto cauInTaskDone;
}
#endif
else if (strcmp((*ppCxCmd)->pCommand, "source") == 0) {
cmdSource(ppCxCmd);
}
else {
pglCauDesc->cauInTaskInfo.serviceDone = 0;
pglCauDesc->cauInTaskInfo.serviceNeeded = 1;
#ifndef vxWorks
goto cauInTaskDone;
#endif
}
}
cauInTaskDone:
#ifdef vxWorks
while ((*ppCxCmd)->pPrev != NULL)
cmdCloseContext(ppCxCmd);
if (pglCauDesc->showStack)
checkStack(pglCauDesc->cauInTaskInfo.id);
pglCauDesc->cauInTaskInfo.stop = 1;
pglCauDesc->cauInTaskInfo.stopped = 1;
pglCauDesc->cauInTaskInfo.status = ERROR;
#endif
return input;
}
/*+/subr**********************************************************************
* NAME cauCmdProcess - process a command line
*
* DESCRIPTION
*
* RETURNS
* void
*
* BUGS
* o text
*
*-*/
static void
cauCmdProcess(ppCxCmd, pCauDesc)
CX_CMD **ppCxCmd; /* IO ptr to pointer to command context */
CAU_DESC *pCauDesc; /* IO pointer to cau descriptor */
{
CX_CMD *pCxCmd; /* local copy of pointer, for convenience */
pCxCmd = *ppCxCmd;
if (strcmp(pCxCmd->pCommand, "quit") == 0) {
/* insert wrapup processing here */
pCauDesc->cauTaskInfo.stop = 1;
}
#ifdef vxWorks
else if (strcmp(pCxCmd->pCommand, "showStack") == 0)
pCauDesc->showStack = 1;
#endif
else if (strcmp(pCxCmd->pCommand, "deadband") == 0)
cau_deadband(pCxCmd);
else if (strcmp(pCxCmd->pCommand, "debug") == 0)
cau_debug(pCxCmd);
else if (strcmp(pCxCmd->pCommand, "delete") == 0) {
if (pCauDesc->pChanHead == NULL) goto noChanErr;
cau_delete(pCxCmd, pCauDesc);
}
else if (strcmp(pCxCmd->pCommand, "get") == 0)
cau_get(pCxCmd, pCauDesc);
else if (strcmp(pCxCmd->pCommand, "info") == 0)
cau_info(pCxCmd, pCauDesc);
else if (strcmp(pCxCmd->pCommand, "interval") == 0)
cau_interval(pCxCmd, pCauDesc);
else if (strcmp(pCxCmd->pCommand, "monitor") == 0)
cau_monitor(pCxCmd, pCauDesc);
else if (strcmp(pCxCmd->pCommand, "put") == 0)
cau_put(pCxCmd, pCauDesc);
else if (strcmp(pCxCmd->pCommand, "ramp") == 0)
cau_ramp(pCxCmd, pCauDesc);
else {
/*----------------------------------------------------------------------------
* help (or illegal command)
*----------------------------------------------------------------------------*/
(void)nextNonSpaceField(&pCxCmd->pLine, &pCxCmd->pField,
&pCxCmd->delim);
helpIllegalCommand(stdout, &pCxCmd->helpList, pCxCmd->pCommand,
pCxCmd->pField);
}
goto cmdDone;
noChanErr:
(void)printf("no channels selected\n");
cmdDone:
;
return;
}
/*+/subr**********************************************************************
* NAME cau_deadband
*-*/
static void
cau_deadband(pCxCmd)
CX_CMD *pCxCmd; /* IO pointer to command context */
{
char *opt;
if (nextNonSpaceField(&pCxCmd->pLine, &opt, &pCxCmd->delim) <= 1) {
(void)printf("you must specify either MDEL or ADEL\n");
return;
}
if (strcmp(opt, "MDEL") == 0)
glCauDeadband = DBE_VALUE | DBE_ALARM;
else if (strcmp(opt, "ADEL") == 0)
glCauDeadband = DBE_LOG | DBE_ALARM;
else
(void)printf("you must specify either MDEL or ADEL\n");
}
/*+/subr**********************************************************************
* NAME cau_debug
*-*/
static void
cau_debug(pCxCmd)
CX_CMD *pCxCmd; /* IO pointer to command context */
{
int i;
if (nextIntFieldAsInt(&pCxCmd->pLine, &i, &pCxCmd->delim) > 1)
glCauDebug = i;
else if (pCxCmd->delim != '-')
glCauDebug++;
else
glCauDebug--;
}
/*+/subr**********************************************************************
* NAME cau_delete
*-*/
static void
cau_delete(pCxCmd, pCauDesc)
CX_CMD *pCxCmd; /* IO pointer to command context */
CAU_DESC *pCauDesc; /* IO pointer to cau descriptor */
{
CAU_CHAN *pChan; /* temp for channel pointer */
CAU_CHAN *pChanNext; /* temp for channel pointer */
pCxCmd->fldLen =
nextChanNameField(&pCxCmd->pLine, &pCxCmd->pField, &pCxCmd->delim);
if (pCxCmd->fldLen <= 1) {
(void)printf(
"you must specify one or more channels or else specify all\n");
return;
}
if (strcmp(pCxCmd->pField, "all") == 0) {
pChan = pCauDesc->pChanHead;
while (pChan != NULL) {
pChanNext = pChan->pNext;
if (cauChanDel(pCxCmd, pCauDesc, pChan) != OK) {
(void)printf("error deleting %s \n", pCxCmd->pField);
}
pChan = pChanNext;
}
return;
}
while (pCxCmd->fldLen > 1) {
if ((pChan = cauChanFind(pCauDesc, pCxCmd->pField)) == NULL)
(void)printf("%s not selected\n", pCxCmd->pField);
else {
if (cauChanDel(pCxCmd, pCauDesc, pChan) != OK) {
(void)printf("error deleting %s \n", pCxCmd->pField);
}
}
pCxCmd->fldLen =
nextChanNameField(&pCxCmd->pLine, &pCxCmd->pField, &pCxCmd->delim);
}
}
/*+/subr**********************************************************************
* NAME cau_get
*-*/
static void
cau_get(pCxCmd, pCauDesc)
CX_CMD *pCxCmd; /* IO pointer to command context */
CAU_DESC *pCauDesc; /* IO pointer to cau descriptor */
{
CAU_CHAN *pChan; /* temp for channel pointer */
int count=-1;
if (pCxCmd->delim == ',') {
nextIntFieldAsInt(&pCxCmd->pLine, &count, &pCxCmd->delim);
if (count <= 0) {
(void)printf("error in count field\n");
return;
}
}
pCxCmd->fldLen =
nextChanNameField(&pCxCmd->pLine, &pCxCmd->pField, &pCxCmd->delim);
if (pCxCmd->fldLen <= 1 || strcmp(pCxCmd->pField, "all") == 0) {
if ((pChan = pCauDesc->pChanHead) == NULL) {
(void)printf("no channels selected\n");
return;
}
while (pChan != NULL) {
pChan->dbrType = dbf_type_to_DBR_TIME(pChan->dbfType);
if (count > 0) {
if (count <= (int)pChan->elCount)
pChan->reqCount = count;
else
pChan->reqCount = pChan->elCount;
}
cauGetAndPrint(pCxCmd, pCauDesc, pChan, 1, 0, 0);
pChan = pChan->pNext;
}
return;
}
while (pCxCmd->fldLen > 1) {
if ((pChan = cauChanFind(pCauDesc, pCxCmd->pField)) == NULL) {
pChan = cauChanAdd(pCxCmd, pCauDesc, pCxCmd->pField);
if (pChan == NULL)
(void)printf("couldn't open %s \n", pCxCmd->pField);
}
if (pChan != NULL) {
pChan->dbrType = dbf_type_to_DBR_TIME(pChan->dbfType);
if (count > 0) {
if (count <= (int)pChan->elCount)
pChan->reqCount = count;
else
pChan->reqCount = pChan->elCount;
}
cauGetAndPrint(pCxCmd, pCauDesc, pChan, 1, 0, 0);
}
pCxCmd->fldLen =
nextChanNameField(&pCxCmd->pLine, &pCxCmd->pField, &pCxCmd->delim);
}
}
/*+/subr**********************************************************************
* NAME cau_info
*-*/
static void
cau_info(pCxCmd, pCauDesc)
CX_CMD *pCxCmd; /* IO pointer to command context */
CAU_DESC *pCauDesc; /* IO pointer to cau descriptor */
{
CAU_CHAN *pChan; /* temp for channel pointer */
pCxCmd->fldLen =
nextChanNameField(&pCxCmd->pLine, &pCxCmd->pField, &pCxCmd->delim);
if (pCxCmd->fldLen <= 1 || strcmp(pCxCmd->pField, "all") == 0) {
if ((pChan = pCauDesc->pChanHead) == NULL) {
(void)printf("no channels selected\n");
return;
}
while (pChan != NULL) {
cauPrintInfo(pCxCmd, pChan);
pChan = pChan->pNext;
}
return;
}
while (pCxCmd->fldLen > 1) {
if ((pChan = cauChanFind(pCauDesc, pCxCmd->pField)) == NULL) {
pChan = cauChanAdd(pCxCmd, pCauDesc, pCxCmd->pField);
if (pChan == NULL) {
(void)printf("couldn't open %s \n", pCxCmd->pField);
}
}
if (pChan != NULL)
cauPrintInfo(pCxCmd, pChan);
pCxCmd->fldLen =
nextChanNameField(&pCxCmd->pLine, &pCxCmd->pField, &pCxCmd->delim);