This is Unofficial EPICS BASE Doxygen Site
cainfo.c
Go to the documentation of this file.
1 /*************************************************************************\
2 * Copyright (c) 2009 Helmholtz-Zentrum Berlin fuer Materialien und Energie.
3 * Copyright (c) 2002 The University of Chicago, as Operator of Argonne
4 * National Laboratory.
5 * Copyright (c) 2002 The Regents of the University of California, as
6 * Operator of Los Alamos National Laboratory.
7 * Copyright (c) 2002 Berliner Elektronenspeicherringgesellschaft fuer
8 * Synchrotronstrahlung.
9 * EPICS BASE is distributed subject to a Software License Agreement found
10 * in file LICENSE that is included with this distribution.
11 \*************************************************************************/
12 
13 /*
14  * Author: Ralph Lange (BESSY)
15  *
16  * Modification History
17  * 2008/04/16 Ralph Lange (BESSY)
18  * Updated usage info
19  * 2009/04/01 Ralph Lange (HZB/BESSY)
20  * Clarified output for native data type
21  *
22  */
23 
24 #include <stdio.h>
25 #include <epicsStdlib.h>
26 #include "epicsVersion.h"
27 
28 #include <cadef.h>
29 #include <epicsGetopt.h>
30 
31 #include "tool_lib.h"
32 
33 static unsigned statLevel = 0; /* ca_client_status() interest level */
34 
35 
36 void usage (void)
37 {
38  fprintf (stderr, "\nUsage: cainfo [options] <PV name> ...\n\n"
39  " -h: Help: Print this message\n"
40  " -V: Version: Show EPICS and CA versions\n"
41  "Channel Access options:\n"
42  " -w <sec>: Wait time, specifies CA timeout, default is %f second(s)\n"
43  " -s <level>: Call ca_client_status with the specified interest level\n"
44  " -p <prio>: CA priority (0-%u, default 0=lowest)\n"
45  "\nExample: cainfo my_channel another_channel\n\n"
47  fprintf (stderr, "\nEPICS Version %s, CA Protocol version %s\n", EPICS_VERSION_STRING, ca_version() );
48 }
49 
50 
51 
52 /*+**************************************************************************
53  *
54  * Function: cainfo
55  *
56  * Description: Print CA info data or call ca_client_status
57  *
58  * Arg(s) In: pvs - Pointer to an array of pv structures
59  * nPvs - Number of elements in the pvs array
60  *
61  * Return(s): Error code: 0 = OK, 1 = Error
62  *
63  **************************************************************************-*/
64 
65 int cainfo (pv *pvs, int nPvs)
66 {
67  int n;
68  long dbfType;
69  long dbrType;
70  unsigned long nElems;
71  enum channel_state state;
72  char *stateStrings[] = {
73  "never connected", "previously connected", "connected", "closed" };
74  char *boolStrings[] = { "no ", "" };
75 
76  if (statLevel) {
77  ca_client_status(statLevel);
78 
79  } else {
80 
81  for (n = 0; n < nPvs; n++) {
82 
83  /* Print the status data */
84  /* --------------------- */
85 
86  state = ca_state(pvs[n].chid);
87  nElems = ca_element_count(pvs[n].chid);
88  dbfType = ca_field_type(pvs[n].chid);
89  dbrType = dbf_type_to_DBR(dbfType);
90 
91  printf("%s\n"
92  " State: %s\n"
93  " Host: %s\n"
94  " Access: %sread, %swrite\n"
95  " Native data type: %s\n"
96  " Request type: %s\n"
97  " Element count: %lu\n"
98  , pvs[n].name,
99  stateStrings[state],
100  ca_host_name(pvs[n].chid),
101  boolStrings[ca_read_access(pvs[n].chid)],
102  boolStrings[ca_write_access(pvs[n].chid)],
103  dbf_type_to_text(dbfType),
104  dbr_type_to_text(dbrType),
105  nElems
106  );
107  }
108  }
109 
110  return 0;
111 }
112 
113 
114 
115 /*+**************************************************************************
116  *
117  * Function: main
118  *
119  * Description: cainfo main()
120  * Evaluate command line options, set up CA, connect the
121  * channels, print the data as requested
122  *
123  * Arg(s) In: [options] <pv-name> ...
124  *
125  * Arg(s) Out: none
126  *
127  * Return(s): Standard return code (0=success, 1=error)
128  *
129  **************************************************************************-*/
130 
131 int main (int argc, char *argv[])
132 {
133  int n;
134  int result; /* CA result */
135 
136  int opt; /* getopt() current option */
137 
138  int nPvs; /* Number of PVs */
139  pv* pvs; /* Array of PV structures */
140 
141  LINE_BUFFER(stdout); /* Configure stdout buffering */
142 
143  while ((opt = getopt(argc, argv, ":nhVw:s:p:")) != -1) {
144  switch (opt) {
145  case 'h': /* Print usage */
146  usage();
147  return 0;
148  case 'V':
149  printf( "\nEPICS Version %s, CA Protocol version %s\n", EPICS_VERSION_STRING, ca_version() );
150  return 0;
151  case 'w': /* Set CA timeout value */
152  if(epicsScanDouble(optarg, &caTimeout) != 1)
153  {
154  fprintf(stderr, "'%s' is not a valid timeout value "
155  "- ignored. ('cainfo -h' for help.)\n", optarg);
157  }
158  break;
159  case 's': /* ca_client_status interest level */
160  if (sscanf(optarg,"%du", &statLevel) != 1)
161  {
162  fprintf(stderr, "'%s' is not a valid interest level "
163  "- ignored. ('cainfo -h' for help.)\n", optarg);
164  statLevel = 0;
165  }
166  break;
167  case 'p': /* CA priority */
168  if (sscanf(optarg,"%u", &caPriority) != 1)
169  {
170  fprintf(stderr, "'%s' is not a valid CA priority "
171  "- ignored. ('cainfo -h' for help.)\n", optarg);
173  }
175  break;
176  case '?':
177  fprintf(stderr,
178  "Unrecognized option: '-%c'. ('cainfo -h' for help.)\n",
179  optopt);
180  return 1;
181  case ':':
182  fprintf(stderr,
183  "Option '-%c' requires an argument. ('cainfo -h' for help.)\n",
184  optopt);
185  return 1;
186  default :
187  usage();
188  return 1;
189  }
190  }
191 
192  nPvs = argc - optind; /* Remaining arg list are PV names */
193 
194  if (!statLevel && nPvs < 1)
195  {
196  fprintf(stderr, "No pv name specified. ('cainfo -h' for help.)\n");
197  return 1;
198  }
199  /* Start up Channel Access */
200 
202  if (result != ECA_NORMAL) {
203  fprintf(stderr, "CA error %s occurred while trying "
204  "to start channel access.\n", ca_message(result));
205  return 1;
206  }
207  /* Allocate PV structure array */
208 
209  pvs = calloc (nPvs, sizeof(pv));
210  if (!pvs)
211  {
212  fprintf(stderr, "Memory allocation for channel structures failed.\n");
213  return 1;
214  }
215  /* Connect channels */
216 
217  for (n = 0; optind < argc; n++, optind++)
218  pvs[n].name = argv[optind] ; /* Copy PV names from command line */
219 
220  result = connect_pvs(pvs, nPvs);
221 
222  /* Print data */
223  if (!result)
224  result = cainfo(pvs, nPvs);
225 
226  /* Shut down Channel Access */
228 
229  return result;
230 }
int getopt(int nargc, char *const *nargv, const char *ostr)
Definition: epicsGetopt.c:65
LIBCA_API unsigned epicsStdCall ca_write_access(chid chan)
pvac::PutEvent result
Definition: clientSync.cpp:117
capri caPriority
Definition: tool_lib.c:56
#define dbf_type_to_text(type)
Definition: db_access.h:677
#define DEFAULT_TIMEOUT
Definition: tool_lib.h:50
int optind
Definition: epicsGetopt.c:50
LIBCA_API unsigned long epicsStdCall ca_element_count(chid chan)
LIBCA_API enum channel_state epicsStdCall ca_state(chid chan)
#define CA_PRIORITY_MAX
Definition: cadef.h:190
Definition: tool_lib.h:67
LIBCA_API const char *epicsStdCall ca_host_name(chid channel)
#define printf
Definition: epicsStdio.h:41
#define dbf_type_to_DBR(type)
Definition: db_access.h:697
dbfType
Definition: dbFldTypes.h:24
channel_state
Definition: cadef.h:166
#define LINE_BUFFER(stream)
Definition: tool_lib.h:53
#define epicsScanDouble(str, to)
Definition: epicsStdlib.h:78
LIBCA_API short epicsStdCall ca_field_type(chid chan)
int epicsStdCall ca_client_status(unsigned level)
Definition: access.cpp:701
#define dbr_type_to_text(type)
Definition: db_access.h:687
int optopt
Definition: epicsGetopt.c:50
int epicsStdCall ca_context_create(ca_preemptive_callback_select premptiveCallbackSelect)
Definition: access.cpp:172
#define ECA_NORMAL
Definition: caerr.h:77
#define DEFAULT_CA_PRIORITY
Definition: tool_lib.h:49
int main(int argc, char *argv[])
Definition: cainfo.c:131
LIBCA_API unsigned epicsStdCall ca_read_access(chid chan)
#define stdout
Definition: epicsStdio.h:30
int connect_pvs(pv *pvs, int nPvs)
Definition: tool_lib.c:621
const char *epicsStdCall ca_message(long ca_status)
Definition: access.cpp:561
const char *epicsStdCall ca_version()
Definition: access.cpp:641
int cainfo(pv *pvs, int nPvs)
Definition: cainfo.c:65
#define stderr
Definition: epicsStdio.h:32
void epicsStdCall ca_context_destroy()
Definition: access.cpp:232
char * optarg
Definition: epicsGetopt.c:55
void usage(void)
Definition: cainfo.c:36
double caTimeout
Definition: tool_lib.c:55