My Project
 All Classes Files Functions Variables Enumerations Enumerator Friends Macros Pages
genericcontrol.h
1 #ifndef genericcontrol_h
2 #define genericcontrol_h
3 
4 #include "gcp/util/common/NetStruct.h"
5 #include "gcp/util/common/NetUnion.h"
6 #include "gcp/util/common/NewRtcNetMsg.h"
7 
8 #include "netbuf.h"
9 
10 /*
11  * In order to get the correct include file definitions for posix
12  * threads, _POSIX_C_SOURCE must be defined to be at least 199506.
13  */
14 #if MAC_OSX == 1
15 #define _POSIX_C_SOURCE 199506L
16 #endif
17 
18 #if !defined(_POSIX_C_SOURCE) || _POSIX_C_SOURCE < 199506L
19 #error "Add -D_POSIX_C_SOURCE=199506L to the compile line"
20 #endif
21 
22 #include <stdio.h> /* FILENAME_MAX */
23 #include <time.h> /* struct tm */
24 #include <limits.h> /* PIPE_BUF */
25 #include "lprintf.h"
26 
27 #include "gcp/control/code/unix/libunix_src/common/list.h"
28 
29 #include "gcp/util/common/PipeQueue.h"
30 
31 #include "regmap.h"
32 #include "regdata.h"
33 #include "control.h"
34 
35 #include "gcp/control/code/unix/libunix_src/specific/rtcnetcoms.h"
36 
37 #include "gcp/util/common/AntNum.h"
38 #include "gcp/util/common/Directives.h"
39 
40 #include "gcp/control/code/unix/libtransaction_src/TransactionManager.h"
41 
42 /*
43  * Set the maximum time to wait for threads to shutdown after being
44  * told to do so. The timeout is measured in seconds.
45  */
46 #define CP_SHUTDOWN_TIMEOUT 5
47 
48 /*
49  * Work out the longest pathname to support. Under Linux
50  * FILENAME_MAX bytes is right at the limit of what can be sent by a
51  * pipe (FILENAME_MAX==PIPE_BUF), so the remaining members of the message
52  * structures cause us not to be able send messages with arrays of
53  * this length included.
54  */
55 #if FILENAME_MAX + 16 > PIPE_BUF
56 #define CP_FILENAME_MAX (PIPE_BUF - 16)
57 #else
58 #define CP_FILENAME_MAX FILENAME_MAX
59 #endif
60 
61 #define CP_REGNAME_MAX CP_FILENAME_MAX/2
62 
63 /*
64  * Declare the type of the resource container of the program.
65  */
66 typedef struct ControlProg ControlProg; /* (see genericcontrol.c) */
67 
68 /*
69  * When data arrives on the file-descriptor of a readable communication
70  * channel, or a writeable channel has room for more data, the appropriate
71  * aspect of the channel is removed from the list of active channels, then
72  * a function of the following type is called. If the I/O doesn't complete,
73  * be sure to re-register for completion via add_readable/writeable_channel().
74  *
75  * Input:
76  * cp ControlProg * The control program resource object.
77  * head ComHeader * The communication channel.
78  */
79 #define CHAN_IO_FN(fn) void (fn)(ControlProg *cp, ComHeader *head)
80 
81 /*
82  * When a complete message has been read into sock->nrs, then a
83  * function of the following form is called to process the message.
84  * Before it is called, rem_readable_channel(cp, sock) is called to
85  * remove the socket from the list of channels to be watched
86  * for further messages.
87  *
88  * Input:
89  * cp ControlProg * The control program resource object.
90  * sock SockChan * The socket that received the message.
91  * Output:
92  * return int 0 - OK.
93  * 1 - Error.
94  */
95 #define SOCK_RCVD_FN(fn) int (fn)(ControlProg *cp, SockChan *sock)
96 
97 /*
98  * When the message in sock->nss has been succesfully sent, then a
99  * function of the following form is called.
100  *
101  * Before it is called, rem_writeable_channel(cp, sock) is called to
102  * remove the socket from the list of channels to be watched
103  * for writeability.
104  *
105  * Input:
106  * cp ControlProg * The control program resource object.
107  * sock SockChan * The socket that sent the message.
108  * Output:
109  * return int 0 - OK.
110  * 1 - Error.
111  */
112 #define SOCK_SENT_FN(fn) int (fn)(ControlProg *cp, SockChan *sock)
113 
114 /*
115  * If an I/O error occurs while reading or writing to a socket,
116  * or if the socket connection is lost, then a function of the
117  * following form is called.
118  *
119  * Before it is called, close_socket_channel(cp, sock) is called to
120  * remove the socket from the list of channels to be watched
121  * for I/O and to close the associated file descriptor.
122  *
123  * Input:
124  * cp ControlProg * The control program resource object.
125  * sock SockChan * The communication socket that encountered the
126  * exceptional condition.
127  * cond SockCond The type of exceptional condition that was
128  * encountered.
129  */
130 
131 #define SOCK_COND_FN(fn) void (fn)(ControlProg *cp, SockChan *sock, \
132  SockCond cond)
133 
134 typedef struct ComHeader ComHeader;
135 
136 /*
137  * Each channel has two members of the following type, representing
138  * the two I/O directions.
139  */
140 struct ComAspect {
141  ComHeader *parent; /* The generic header of the containing channel */
142  int fd; /* The file-descriptor to be watched. */
143  fd_set *active_set; /* A pointer to the select set to be checked, or NULL */
144  /* if this channel aspect isn't being watched */
145  CHAN_IO_FN(*io_fn); /* The function used to respond to I/O readiness */
146  ComAspect *next; /* The next channel in the list */
147  ComAspect *prev; /* The previous channel in the list */
148 };
149 
150 /*
151  * Enumerate the known types of communication channels.
152  */
153 enum ChanType {
154  CHAN_SOCK, /* A duplex network socket */
155  CHAN_PIPE /* An unnamed pipe */
156 };
157 
158 /*
159  * All communication channels start with a member of the following type.
160  */
161 struct ComHeader {
162  ChanType type; /* The type of communications channel */
163  void *client_data; /* Client-specific data associated with the channel */
164  ComAspect read; /* The readable aspect of the channel */
165  ComAspect write; /* The writeable aspect of the channel */
166 };
167 
168 typedef enum { /* The type of exceptional condition */
169  SOCK_CLOSE, /* End of file */
170  SOCK_ERROR /* I/O error */
171 } SockCond;
172 
173 /*
174  * Each network communications socket is represented by an iterator object
175  * of the following type.
176  */
177 struct SockChan {
178  ComHeader head; /* The generic header of a communications channel */
179  SOCK_RCVD_FN(*rcvd_fn); /* The method called to process a received message */
180  SOCK_SENT_FN(*sent_fn); /* The method called when a message has been sent */
181  SOCK_COND_FN(*cond_fn); /* The method called on exceptional conditions */
182  gcp::control::NetReadStr *nrs; /* Communications iterator used to read from fd */
183  gcp::control::NetSendStr *nss; /* Communications iterator used to write to fd */
184 };
185 
186 /*
187  * Enumerate the client threads of the control program. The value of
188  * each enumerator is the index of the thread in the table of threads in
189  * genericcontrol.c.
190  */
191 typedef enum {
192  CP_SCHEDULER, /* The scheduler thread */
193  CP_ARCHIVER, /* The archiver thread */
194  CP_LOGGER, /* The logger thread */
195  CP_NAVIGATOR, /* The navigator thread */
196  CP_GRABBER, /* The frame-grabber archiver thread */
197  CP_TERM, /* The terminal I/O thread */
198 } CpThreadId;
199 
200 /*
201  * Look up the resource object of a given thread.
202  */
203 void *cp_ThreadData(ControlProg *cp, CpThreadId id);
204 
205 /*
206  * The following macros declare the method functions of control-program threads.
207  */
208 
209 /* Allocate the resource object of a thread */
210 
211 #define CP_NEW_FN(fn) void *(fn)(ControlProg *cp, gcp::util::Pipe *pipe)
212 
213 /* Delete the resource object of a thread and return NULL */
214 
215 #define CP_DEL_FN(fn) void *(fn)(void *obj)
216 
217 /* The thread start function. The thread resource object is passed via 'arg' */
218 
219 #define CP_THREAD_FN(fn) void *(fn)(void *arg)
220 
221 /*
222  * The following function sends a shutdown message to the thread using
223  * non-blocking I/O. On success it should return 0. If the message couldn't
224  * be sent without blocking, it should return 1.
225  */
226 
227 #define CP_STOP_FN(fn) int (fn)(ControlProg *cp)
228 
229 /*-----------------------------------------------------------------------
230  * Declare the API of the logger thread.
231  *-----------------------------------------------------------------------*/
232 
233 typedef struct Logger Logger; // The type of the thread resource object
234 typedef struct Logger SzaArrayLogger;// The type of the thread resource object
235 
236 CP_THREAD_FN(logger_thread); // The logger thread start function
237 CP_NEW_FN(new_Logger); // The logger resource constructor
238 CP_DEL_FN(del_Logger); // The logger resource destructor
239 CP_STOP_FN(stop_Logger); // Send a shutdown message to the logger
240 
241 
242 // Enumerate the message types that the logger task understands.
243 
244 typedef enum {
245  LOG_SHUTDOWN, // Cleanup and shutdown the logger thread
246  LOG_CHDIR, // Change the default log file directory
247  LOG_OPEN, // Open a new log file in a given place
248  LOG_FLUSH, // Flush unwritten text to the current
249  // log file.(Given that line buffering is
250  // requested when log files are opened
251  // this shouldn't ever be necessary.)
252  LOG_CLOSE, // Close the current log file
253  LOG_MESSAGE, // Write a specified message to the log
254  LOG_ADD_CLIENT, // Add a control client to the output list
255  LOG_REM_CLIENT, // Remove a control client from the output list
256  LOG_TRANS_CATALOG, // Read a transaction catalog
257  LOG_LOG_TRANS // Log a transaction
258 } LoggerMessageType;
259 
260 
261 // Objects of the following form are used to pass messages to the
262 // logger task. Please use pack_logger_<type>() to set up one of these
263 // messages.
264 
265 #define DATE_LEN 20
266 
267 typedef struct {
268  LoggerMessageType type; // The type of logger input message
269  union { // The body of the message
270 
271  struct { // type = LOG_CHDIR
272  char dir[CP_FILENAME_MAX+1];// The directory on which to open the file
273  } chdir;
274 
275  struct { // type = LOG_OPEN
276  char dir[CP_FILENAME_MAX+1];// The directory on which to open the file
277  } open;
278 
279  struct { // type = LOG_MESSAGE
280  unsigned seq; // The sequence number of this message
281  bool end; // True if this is the end of a message
282  bool interactive; // True if this message was generated by an interactive command
283  gcp::control::LogStream nature; // The disposition of the message
284  char text[LOG_MSGLEN+1]; // The text of the message
285  } message;
286 
287  struct { // type = LOG_ADD_CLIENT | LOG_REM_CLIENT
288  gcp::util::Pipe *pipe; // The control-client reply pipe
289  } client;
290 
291  struct { // type = LOG_TRANSACTION_CATALOG
292  char catalog[CP_FILENAME_MAX+1];// The catalog to open
293  bool clear;
294  } transCatalog;
295 
296  struct { // type = LOG_TRANSACTION_CATALOG
297  // The device name
298  char device[gcp::control::TransactionManager::DEV_NAME_MAX+1];
299  // The serial number
300  char serial[gcp::control::TransactionManager::SERIAL_NAME_MAX+1];
301  // The location name
302  char location[gcp::control::TransactionManager::LOCATION_NAME_MAX+1];
303  // The date
304  double date;
305  // Who?
306  char who[gcp::control::TransactionManager::WHO_NAME_MAX+1];
307  // Comment
308  char comment[LOG_MSGLEN-gcp::control::TransactionManager::PREFIX_LEN+1];
309  } logTrans;
310 
311  } body;
312 } LoggerMessage;
313 
314 // The following functions pack messages into a LoggerMessage object.
315 
316 int pack_logger_shutdown(LoggerMessage *msg);
317 int pack_logger_chdir(LoggerMessage *msg, char *dir);
318 int pack_logger_open(LoggerMessage *msg, char *dir);
319 int pack_logger_flush(LoggerMessage *msg);
320 int pack_logger_close(LoggerMessage *msg);
321 int pack_logger_message(LoggerMessage *msg, char *text, gcp::control::LogStream nature,
322  unsigned seq=0, bool end=true, bool interactive=false);
323 int pack_logger_add_client(LoggerMessage *msg, gcp::util::Pipe *client);
324 int pack_logger_rem_client(LoggerMessage *msg, gcp::util::Pipe *client);
325 int pack_logger_transaction_catalog(LoggerMessage *msg, char *path, bool clear);
326 int pack_logger_log_transaction(LoggerMessage *msg, char *device, char* serial,
327  char* location, double date, char* who,
328  char* comment);
329 // Return true if the named device is recognized.
330 
331 bool log_isValidDevice(Logger* log, char* device);
332 
333 /*
334  * The following function attempts to send a packed LoggerMessage to the
335  * logger thread.
336  */
337 PipeState send_LoggerMessage(ControlProg *cp, LoggerMessage *msg,
338  int timeout);
339 
340 /*
341  * The following function redirects the specified stream (stdout or stderr)
342  * of the calling thread to the input pipe of the logger thread.
343  */
344 int log_thread_stream(ControlProg *cp, FILE *stream);
345 
346 /*-----------------------------------------------------------------------
347  * Declare the API of the archiver thread.
348  *-----------------------------------------------------------------------*/
349 
350 typedef struct Archiver Archiver; /* The type of the thread resource object */
351 
352 CP_THREAD_FN(archiver_thread); /* The archiver thread start function */
353 CP_NEW_FN(new_Archiver); /* The archiver resource constructor */
354 CP_DEL_FN(del_Archiver); /* The archiver resource destructor */
355 CP_STOP_FN(stop_Archiver); /* Send a shutdown message to the archiver */
356 
357 /*
358  * Enumerate the archiver message types.
359  */
360 typedef enum {
361  ARC_FRAME, /* Save the latest integration. When done */
362  /* set arc->fb.integrate to 1 and signal */
363  /* arc->fb.start */
364  ARC_SHUTDOWN, /* Cleanup and shutdown the archiver thread */
365  ARC_CHDIR, /* Change the default archive file directory */
366  ARC_OPEN, /* Open a new archive file in a given place */
367  ARC_FLUSH, /* Flush unwritten data to the current file */
368  ARC_CLOSE, /* Close the current archive file */
369  ARC_SAMPLING, /* Set the number of samples per integration */
370  ARC_FILTER, /* Enable/disable feature-based filtering */
371  ARC_FILE_SIZE, /* The number of frames to record in each */
372  /* archive file before automatically opening */
373  /* a new one. */
374  ARC_ADD_CLIENT, /* Add a control client to the list of connected */
375  /* clients */
376  ARC_REM_CLIENT, /* Remove a control client from the list of connected */
377  /* clients */
378  ARC_TV_OFFSET_DONE, /* A message sent by the control program to let
379  the archiver know that a tv_offset command
380  has been successfully completed. This is
381  sent by the scanner task when the new offsets
382  have been written to the latest frame sent to
383  the archiver */
384  ARC_NEW_FRAME /* A message sent by the control program to
385  request the archiver to terminate the current
386  integration and start a new one */
387 } ArchiverMessageType;
388 
389 /*
390  * Objects of the following form are used to pass messages to the
391  * archiver task. Please use pack_archiver_<type>() to set up one of
392  * these messages.
393  */
394 typedef struct {
395  ArchiverMessageType type; /* The type of archiver input message */
396  union { /* The body of the message */
397  struct { /* type = ARC_CHDIR */
398  char dir[CP_FILENAME_MAX+1];/* The current archiving directory */
399  } chdir;
400  struct { /* type = ARC_OPEN */
401  char dir[CP_FILENAME_MAX+1];/* The current archiving directory */
402  } open;
403  struct { /* type = ARC_SAMPLING */
404  unsigned nframe; /* The number of samples per integration */
405  } sampling;
406  struct { /* type = ARC_FILTER */
407  int enable; /* True to tell the archiver only record */
408  /* frames who's frame.features register */
409  /* has a non-zero value. False to tell it */
410  /* to archive everything (the default) */
411  } filter;
412  struct { /* type = ARC_FILE_SIZE */
413  unsigned nframe; /* The number of frames per file, or zero to */
414  /* allow any number of frames per file. */
415  } file_size;
416  struct { /* type = ARC_ADD_CLIENT | ARC_REM_CLIENT */
417  gcp::util::Pipe *pipe; /* The control-client reply pipe */
418  } client;
419  struct {
420  unsigned seq;
421  } tv_offset_done;
422  struct {
423  unsigned seq;
424  } newFrame;
425  } body;
427 
428 /*
429  * The following functions pack messages into an ArchiverMessage object.
430  */
431 int pack_archiver_frame(ArchiverMessage *msg);
432 int pack_archiver_shutdown(ArchiverMessage *msg);
433 int pack_archiver_chdir(ArchiverMessage *msg, char *dir);
434 int pack_archiver_open(ArchiverMessage *msg, char *dir);
435 int pack_archiver_flush(ArchiverMessage *msg);
436 int pack_archiver_close(ArchiverMessage *msg);
437 int pack_archiver_sampling(ArchiverMessage *msg, unsigned nframe);
438 int pack_archiver_filter(ArchiverMessage *msg, int enable);
439 int pack_archiver_file_size(ArchiverMessage *msg, unsigned nframe);
440 int pack_archiver_add_client(ArchiverMessage *msg, gcp::util::Pipe *client);
441 int pack_archiver_rem_client(ArchiverMessage *msg, gcp::util::Pipe *client);
442 int pack_archiver_tv_offset_done(ArchiverMessage *msg, unsigned seq);
443 int pack_archiver_newFrame(ArchiverMessage *msg, unsigned seq);
444 /*
445  * The following function attempts to send a packed ArchiverMessage to the
446  * archiver thread.
447  */
448 PipeState send_ArchiverMessage(ControlProg *cp, ArchiverMessage *msg,
449  int timeout);
450 
451 /*
452  * This function adds a register frame to the current integration.
453  */
454 int arc_integrate_frame(ControlProg *cp, RegRawData *frame);
455 int arc_integrate_frame_old(ControlProg *cp, RegRawData *frame);
456 
457 /*
458  * Set the default archiver integration duration (frames).
459  */
460 enum {ARC_DEF_NFRAME=10};
461 
462 /*-----------------------------------------------------------------------
463  * Declare the API of the scheduler thread.
464  *-----------------------------------------------------------------------*/
465 
466 typedef struct Scheduler Scheduler; /* The type of the thread resource object */
467 
468 Scheduler *cp_Scheduler(ControlProg *cp);
469 
470 CP_THREAD_FN(scheduler_thread); /* The scheduler thread start function */
471 CP_NEW_FN(new_Scheduler); /* The scheduler resource constructor */
472 CP_DEL_FN(del_Scheduler); /* The scheduler resource destructor */
473 CP_STOP_FN(stop_Scheduler); /* Send a shutdown message to the scheduler */
474 
475 /*
476  * Enumerate scheduler message types.
477  */
478 enum SchedulerMessageType {
479  SCH_SHUTDOWN=1, /* An instruction to cleanup and shutdown */
480  SCH_COMMAND, /* A user command string */
481  SCH_RTC_STATE, /* Controller connection status */
482  SCH_ADD_CLIENT, /* Add a control client to the output list */
483  SCH_REM_CLIENT, /* Remove a control client from the output list */
484  SCH_RTCNETMSG, /* A message received from the controller */
485  SCH_MARK_DONE, /* A marker transaction completion message */
486  SCH_GRAB_DONE, /* A marker transaction completion message */
487  SCH_SETREG_DONE, /* A setreg transaction completion message */
488  SCH_TV_OFFSET_DONE, /* A tv_offset transaction completion message */
489  SCH_AUTO_DIR, /* An autoqueue chdir operation */
490  SCH_AUTO_POLL, /* An autoqueue polling inerval change */
491  SCH_AUTO_STATE, /* An autoqueue on/off toggle */
492  SCH_FRAME_DONE /* A new frame has begun */
493 };
494 
495 /*
496  * Objects of the following form are used to pass messages to the
497  * scheduler task. Please use pack_scheduler_<type>() to set up one of
498  * these messages.
499  */
501  public:
502  char string[gcp::control::CC_CMD_MAX+1]; /* The command string */
503  gcp::util::Pipe* client; /* The originating client */
504 
505  SchCommand() {
506  NETSTRUCT_CHAR_ARR(string, gcp::control::CC_CMD_MAX+1); /* The command string */
507  NETSTRUCT_UINT(client); /* The originating client */
508  }
509 };
510 
512  public:
513  gcp::util::Pipe* pipe; /* The control-client reply pipe */
514 
515  SchClient() {
516  NETSTRUCT_UINT(pipe); /* The originating client */
517  }
518 };
519 
521  public:
522  unsigned int id; /* The type of rtc network message */
523  gcp::util::NewRtcNetMsg msg;/* The network message */
524 
525  SchRtcNetMsg() {
526  NETSTRUCT_UINT(id); /* The type of rtc network message */
527  addMember((NetDat*)&msg); /* The network message */
528  }
529 };
530 
532  public:
533  unsigned seq; /* The sequence number of the transaction */
534 
535  SchMarkDone() {
536  NETSTRUCT_UINT(seq); /* The sequence number of the transaction */
537  }
538 };
539 
541  public:
542  unsigned seq; /* The sequence number of the transaction */
543 
544  SchGrabDone() {
545  NETSTRUCT_UINT(seq); /* The sequence number of the transaction */
546  }
547 };
548 
550  public:
551  unsigned seq; /* The sequence number of the transaction */
552 
553  SchSetregDone() {
554  NETSTRUCT_UINT(seq); /* The sequence number of the transaction */
555  }
556 };
557 
559  public:
560  unsigned seq; /* The sequence number of the transaction */
561 
562  SchTvOffsetDone() {
563  NETSTRUCT_UINT(seq); /* The sequence number of the transaction */
564  }
565 };
566 
568  public:
569  unsigned seq; /* The sequence number of the transaction */
570 
571  SchFrameDone() {
572  NETSTRUCT_UINT(seq); /* The sequence number of the transaction */
573  }
574 };
575 
577  public:
578  char dir[CP_FILENAME_MAX+1] ; /* The name of the directory */
579 
580  SchAutoDir() {
581  NETSTRUCT_CHAR_ARR(dir, CP_FILENAME_MAX+1);/*The name of the directory*/
582  }
583 };
584 
586  public:
587  unsigned int poll; /* The interval in ms to poll */
588 
589  SchAutoPoll() {
590  NETSTRUCT_UINT(poll); /* The interval in ms to poll */
591  }
592 };
593 
595  public:
596  unsigned int on; /* The interval in ms to poll */
597 
598  SchAutoState() {
599  NETSTRUCT_UINT(on); /* The interval in ms to poll */
600  }
601 };
602 
603 // The union of all the above messages
604 
606  public:
607 
608  SchCommand command; /* type=SCH_COMMAND */
609  unsigned rtc_online; /* type=SCH_RTC_STATE */
610  SchClient client; /* type = SCH_ADD_CLIENT | SCH_REM_CLIENT */
611  SchRtcNetMsg rtcnetmsg; /* type=SCH_RTCNETMSG */
612  SchMarkDone mark_done; /* type=SCH_MARK_DONE */
613  SchGrabDone grab_done; /* type=SCH_GRAB_DONE */
614  SchSetregDone setreg_done; /* type=SCH_SETREG_DONE */
615  SchTvOffsetDone tv_offset_done; /* type=SCH_TV_OFFSET_DONE */
616  SchFrameDone frame_done; /* type=SCH_FRAME_DONE */
617  SchAutoDir auto_dir; /* type=SCH_AUTO_DIR */
618  SchAutoPoll auto_poll; /* type=SCH_AUTO_POLL */
619  SchAutoState auto_state; /* type=SCH_AUTO_STATE */
620 
621  SchMessageBody() {
622  addMember(SCH_SHUTDOWN); /* type=SCH_COMMAND */
623  addMember(SCH_COMMAND, &command); /* type=SCH_COMMAND */
624  NETUNION_UINT(SCH_RTC_STATE, rtc_online); /* type=SCH_RTC_STATE */
625 
626  // Add this one twice -- doesn't matter, since these won't be destructed
627 
628  addMember(SCH_ADD_CLIENT, &client);/* type=SCH_ADD_CLIENT|SCH_REM_CLIENT */
629  addMember(SCH_REM_CLIENT, &client);/* type=SCH_ADD_CLIENT|SCH_REM_CLIENT */
630 
631  addMember(SCH_RTCNETMSG, &rtcnetmsg); /* type=SCH_RTCNETMSG */
632  addMember(SCH_MARK_DONE, &mark_done); /* type=SCH_MARK_DONE */
633  addMember(SCH_GRAB_DONE, &grab_done); /* type=SCH_GRAB_DONE */
634  addMember(SCH_SETREG_DONE, &setreg_done); /* type=SCH_SETREG_DONE */
635  addMember(SCH_TV_OFFSET_DONE, &tv_offset_done); /* type=SCH_TV_OFFSET_DONE */
636  addMember(SCH_FRAME_DONE, &frame_done); /* type=SCH_FRAME_DONE */
637  addMember(SCH_AUTO_DIR, &auto_dir); /* type=SCH_AUTO_DIR */
638  addMember(SCH_AUTO_POLL, &auto_poll); /* type=SCH_AUTO_POLL */
639  addMember(SCH_AUTO_STATE, &auto_state); /* type=SCH_AUTO_STATE */
640  }
641 };
642 
644  public:
645 
646  // Data members for the SchedulerMessage class
647 
648  SchedulerMessageType type; /* The type of scheduler message */
649  SchMessageBody body;
650 
651  // And the constructor
652 
653  SchedulerMessage() {
654  NETSTRUCT_UINT(type);
655  addMember(&body);
656  }
657 
658  // Method to set the type of this message
659 
660  void setTo(SchedulerMessageType msgType) {
661  type = msgType;
662  body.setTo(msgType);
663  }
664 
665  unsigned int getType() {
666  return body.getType();
667  }
668 
669  // Returns max number of bytes used by a serialized message
670  static unsigned msgSize() {
671  SchedulerMessage msg;
672  return msg.maxSize();
673  }
674 };
675 
676 /*
677  * The following functions pack messages into an SchedulerMessage object.
678  */
679 int pack_scheduler_shutdown(SchedulerMessage* msg);
680 int pack_scheduler_command(SchedulerMessage* msg, char *command, gcp::util::Pipe *client);
681 int pack_scheduler_rtc_state(SchedulerMessage* msg, int online);
682 int pack_scheduler_add_client(SchedulerMessage *msg, gcp::util::Pipe *client);
683 int pack_scheduler_rem_client(SchedulerMessage *msg, gcp::util::Pipe *client);
684 int pack_scheduler_rtcnetmsg(SchedulerMessage* msg, unsigned int id,
685  gcp::util::NewRtcNetMsg& rtcmsg);
686 int pack_scheduler_mark_done(SchedulerMessage *msg, unsigned seq);
687 int pack_scheduler_grab_done(SchedulerMessage *msg, unsigned seq);
688 int pack_scheduler_setreg_done(SchedulerMessage *msg, unsigned seq);
689 int pack_scheduler_tv_offset_done(SchedulerMessage *msg, unsigned seq);
690 int pack_scheduler_frame_done(SchedulerMessage *msg, unsigned seq);
691 
692 /*
693  * The following function attempts to send a packed SchedulerMessage to the
694  * scheduler thread.
695  */
696 PipeState send_SchedulerMessage(ControlProg *cp, SchedulerMessage *msg,
697  int timeout);
698 
699 /*-----------------------------------------------------------------------
700  * Declare the API of the grabber thread.
701  *-----------------------------------------------------------------------*/
702 
703 #ifndef grabber_h
704 typedef struct Grabber Grabber; /* The type of the thread resource object */
705 #endif
706 
707 Grabber *cp_Grabber(ControlProg *cp);
708 
709 /*-----------------------------------------------------------------------
710  * Declare the API of the navigator thread.
711  *-----------------------------------------------------------------------*/
712 
713 #ifndef navigator_h
714 typedef struct Navigator Navigator; /* The type of the thread resource object */
715 #endif
716 
717 Navigator *cp_Navigator(ControlProg *cp);
718 
719 CP_THREAD_FN(navigator_thread); /* The navigator thread start function */
720 CP_NEW_FN(new_Navigator); /* The navigator resource constructor */
721 CP_DEL_FN(del_Navigator); /* The navigator resource destructor */
722 CP_STOP_FN(stop_Navigator); /* Send a shutdown message to the navigator */
723 
724 /*
725  * Enumerate navigator message types.
726  */
727 typedef enum {
728  NAV_SHUTDOWN, /* An instruction to cleanup and shutdown */
729  NAV_RTC_STATE, /* Controller connection status */
730  NAV_CATALOG, /* A "read source catalog" command */
731  NAV_SCAN_CATALOG, /* A "read scan catalog" command */
732  NAV_UT1UTC, /* A "read UT1-UTC ephemeris" command */
733  NAV_SITE /* A "record sza location" command */
734 } NavigatorMessageType;
735 
736 /*
737  * The following container is used to convey site-location messages.
738  */
739 typedef struct {
740  double longitude; /* East longitude (radians) */
741  double latitude; /* Latitude (radians) */
742  double altitude; /* Altitude ASL (m) */
743 } NavSiteMsg;
744 
745 /*
746  * Objects of the following form are used to pass messages to the
747  * navigator task. Please use pack_navigator_<type>() to set up one of
748  * these messages.
749  */
750 typedef struct {
751  NavigatorMessageType type; // The type of navigator message
752  union { // The body of the message
753  int rtc_online; // type=NAV_RTC_STATE
754  char catalog[CP_FILENAME_MAX+1]; // type=NAV_CATALOG
755  char scan_catalog[CP_FILENAME_MAX+1]; // type=NAV_SCAN_CATALOG
756  char ut1utc[CP_FILENAME_MAX+1]; // type=NAV_UT1UTC
757  NavSiteMsg site; // type=NAV_SITE
758  } body;
760 
761 /*
762  * The following functions pack messages into an NavigatorMessage object.
763  */
764 int pack_navigator_shutdown(NavigatorMessage *msg);
765 int pack_navigator_rtc_state(NavigatorMessage *msg, int online);
766 int pack_navigator_catalog(NavigatorMessage *msg, char *path);
767 int pack_navigator_scan_catalog(NavigatorMessage *msg, char *path);
768 int pack_navigator_ut1utc(NavigatorMessage *msg, char *path);
769 int pack_navigator_site(NavigatorMessage *msg, double longitude,
770  double latitude, double altitude);
771 /*
772  * The following function attempts to send a packed NavigatorMessage to the
773  * navigator thread.
774  */
775 PipeState send_NavigatorMessage(ControlProg *cp, NavigatorMessage *msg,
776  int timeout);
777 
778 /*-----------------------------------------------------------------------
779  * Declare the API of the grabber thread.
780  *-----------------------------------------------------------------------*/
781 
782 typedef struct Grabber Grabber; /* The type of the thread resource object */
783 
784 CP_THREAD_FN(grabber_thread); /* The grabber thread start function */
785 CP_NEW_FN(new_Grabber); /* The grabber resource constructor */
786 CP_DEL_FN(del_Grabber); /* The grabber resource destructor */
787 CP_STOP_FN(stop_Grabber); /* Send a shutdown message to the grabber */
788 
789 /*
790  * Enumerate the grabber message types.
791  */
792 typedef enum {
793  GRAB_IMAGE, /* Save the latest image. When done */
794  /* set grab->im.save to 1 and signal */
795  /* grab->im.start */
796  GRAB_SHUTDOWN, /* Cleanup and shutdown the grabber thread */
797  GRAB_CHDIR, /* Change the default grabber file directory */
798  GRAB_OPEN, /* Tell the grabber thread to archive suqsequent
799  image */
800  GRAB_FLUSH, /* Flush unwritten data to the current file */
801  GRAB_CLOSE, /* Tell the grabber thread not to archive
802  subsequent images */
803  GRAB_SEQ /* Tell the gabber thread to update its grabber
804  sequence number */
805 } GrabberMessageType;
806 
807 /*
808  * Objects of the following form are used to pass messages to the
809  * grabber task. Please use pack_grabber_<type>() to set up one of
810  * these messages.
811  */
812 typedef struct {
813  GrabberMessageType type; /* The type of grabber input message */
814  union { /* The body of the message */
815  struct { /* type = GRAB_CHDIR */
816  char dir[CP_FILENAME_MAX+1];/* The current archiving directory */
817  } chdir;
818  struct { /* type = GRAB_OPEN */
819  char dir[CP_FILENAME_MAX+1];/* The current archiving directory */
820  } open;
821  unsigned seq;
822  } body;
824 
825 /*
826  * The following functions pack messages into an GrabberMessage object.
827  */
828 int pack_grabber_image(GrabberMessage *msg);
829 int pack_grabber_shutdown(GrabberMessage *msg);
830 int pack_grabber_chdir(GrabberMessage *msg, char *dir);
831 int pack_grabber_open(GrabberMessage *msg, char *dir);
832 int pack_grabber_flush(GrabberMessage *msg);
833 int pack_grabber_close(GrabberMessage *msg);
834 
835 /*
836  * The following function attempts to send a packed GrabberMessage to the
837  * grabber thread.
838  */
839 PipeState send_GrabberMessage(ControlProg *cp, GrabberMessage *msg,
840  int timeout);
841 
842 /*
843  * This function copies the current image to the grabber image buffer for
844  * archiving.
845  */
846 int grabber_save_image(ControlProg *cp, unsigned short *image,
847  unsigned int utc[2], unsigned short channel, signed actual[3]);
848 
849 /*-----------------------------------------------------------------------
850  * Declare the API of the terminal thread.
851  *-----------------------------------------------------------------------*/
852 
853 typedef struct Term Term; /* The type of the thread resource object */
854 
855 CP_THREAD_FN(term_thread); /* The term thread start function */
856 CP_NEW_FN(new_Term); /* The term resource constructor */
857 CP_DEL_FN(del_Term); /* The term resource destructor */
858 CP_STOP_FN(stop_Term); /* Send a shutdown message to the term */
859 
860 /*
861  * Enumerate the term message types.
862  */
863 typedef enum {
864  TERM_SHUTDOWN, /* Cleanup and shutdown the term thread */
865  TERM_ENABLE, /* Tell the terminal thread to
866  dis/allow subsequent pager commands */
867  TERM_REG_PAGE, /* De/Activate the pager */
868  TERM_MSG_PAGE, /* De/Activate the pager */
869  TERM_IP, /* Set the IP address to use for the pager */
870  TERM_EMAIL, /* Add an email address */
871 } TermMessageType;
872 /*
873  * Enumerate the supported pager devices
874  */
875 typedef enum {
876  PAGER_MAPO_REDLIGHT,
877  PAGER_DOME_PAGER,
878  PAGER_ELDORM_PAGER,
879  PAGER_NONE
880 } PagerDev;
881 /*
882  * Objects of the following form are used to pass messages to the
883  * term task. Please use pack_term_<type>() to set up one of
884  * these messages.
885  */
886 typedef struct {
887 
888  TermMessageType type; /* The type of term input message */
889 
890  union {
891  char ip[CP_FILENAME_MAX+1]; // An IP address for the pager control
892  // device
893  struct {
894  bool add; // If true, add the address, if false, delete it
895  char address[CP_FILENAME_MAX+1]; // An IP address for the pager
896  // control device
897  } email;
898 
899  struct {
900  int on; /* 1 -- turn pager on
901  0 -- turn pager off */
902  char msg[CP_FILENAME_MAX+1]; // The message
903  } page;
904 
905  struct {
906  bool enable; // True to enable the pager, false to disable it
907  } enable;
908 
909 
910  } body;
911 
912  PagerDev dev; /* Which device are we addressing? */
913 
914 } TermMessage;
915 
916 /*
917  * The following functions pack messages into an TermMessage object.
918  */
919 int pack_term_shutdown(TermMessage *msg);
920 int pack_term_reg_page(TermMessage *msg, char* reg=NULL);
921 int pack_term_msg_page(TermMessage *msg, std::string txt);
922 int pack_pager_ip(TermMessage *msg, PagerDev dev, char *ip);
923 int pack_pager_email(TermMessage *msg, bool add, char *email);
924 int pack_pager_enable(TermMessage *msg, bool enable);
925 int send_reg_page_msg(ControlProg* cp, char* reg);
926 
927 MONITOR_CONDITION_HANDLER(sendRegPage);
928 void requestPagerStatus(ControlProg* cp);
929 
930 // The following function attempts to send a packed TermMessage to the
931 // term thread.
932 
933 PipeState send_TermMessage(ControlProg *cp, TermMessage *msg,
934  int timeout);
935 
936 // Return a list of pager email addresses.
937 
938 std::vector<std::string>* getPagerEmailList(ControlProg *cp);
939 
940 /*-----------------------------------------------------------------------
941  * Other functions.
942  *-----------------------------------------------------------------------*/
943 
944 /*
945  * The following function can be called to retrieve the state object of
946  * a given thread.
947  */
948 
949 /*
950  * Get the register map of the control program. Note that
951  * once created by the control program the register map
952  * must be treated as readonly. No locking is then required.
953  */
954 ArrayMap *cp_ArrayMap(ControlProg *cp);
955 
956 /*
957  * Provide access to the default antenna set
958  *
959  * Input:
960  * cp ControlProg * The resource object of the control program.
961  * Output:
962  * return AntNum * The default antenna set
963  */
964 gcp::util::AntNum* cp_AntSet(ControlProg *cp);
965 
966 /*
967  * The following functions send messages to control client output
968  * pipes.
969  */
970 
971 /* Send a text message to a control client (timeout=0) */
972 
973 typedef struct {
974  gcp::control::CcNetMsgId id; /* The type of network message in 'mgs' */
975  gcp::control::CcNetMsg msg; /* The network message to be sent */
976 } CcPipeMsg;
977 
978 int queue_cc_message(gcp::util::Pipe *client, CcPipeMsg *msg);
979 int sendToCcClients(ControlProg* cp, CcPipeMsg *pmsg);
980 
981 /*
982  * Queue a network command object to be sent to the real-time controller.
983  */
984 int queue_rtc_command(ControlProg *cp, gcp::control::RtcNetCmd *cmd,
985  gcp::control::NetCmdId type);
986 
987 /*
988  * Return true if the real-time controller is currently connected.
989  */
990 int cp_rtc_online(ControlProg *cp);
991 
992 /*
993  * The following functions pack messages into an ControlMessage object.
994  */
995 int cp_request_shutdown(ControlProg *cp);
996 int cp_request_restart(ControlProg *cp);
997 int cp_report_exit(ControlProg *cp);
998 int cp_initialized(ControlProg *cp);
999 
1000 int send_scheduler_rtcnetmsg(ControlProg* cp,
1001  gcp::util::NewRtcNetMsg& netmsg);
1002 
1003 // Must be defined by specific experiments
1004 
1005 int processSpecificMsg(ControlProg* cp, SockChan* sock,
1006  gcp::util::NewRtcNetMsg& netmsg);
1007 
1008 int add_readable_channel(ControlProg *cp, ComHeader *head);
1009 
1010 std::string cp_startupScript(ControlProg *cp);
1011 
1012 int sendPeakOffsets(ControlProg* cp, unsigned ipeak, unsigned jpeak,
1013  unsigned chanMask);
1014 
1015 bool cp_dirfileArchive(ControlProg *cp);
1016 bool cp_useModemPager(ControlProg *cp);
1017 
1018 ControlProg* new_ControlProg(bool createScheduler=false,
1019  bool createNavigator=false);
1020 
1021 void configureCmdTimeout(ControlProg* cp, unsigned int seconds);
1022 void configureCmdTimeout(ControlProg* cp, bool activate);
1023 void configureDataTimeout(ControlProg* cp, unsigned int seconds);
1024 void configureDataTimeout(ControlProg* cp, bool activate);
1025 void allowTimeOutPaging(ControlProg* cp, bool allow);
1026 
1027 int sendClearPagerMsg(ControlProg* cp);
1028 int sendResetPagerMsg(ControlProg* cp);
1029 int sendListPagerMsg(ControlProg* cp);
1030 int sendEnablePagerMsg(ControlProg* cp, bool enable);
1031 
1032 int sendAddPagerRegisterMsg(ControlProg* cp, std::string regName,
1033  double min, double max, bool delta, unsigned nFrame,
1034  bool outOfRange, std::string script);
1035 int sendRemPagerRegisterMsg(ControlProg* cp, std::string regName);
1036 
1037 void listPager(ControlProg* cp);
1038 
1039 int sendPagingState(ControlProg* cp, int allow,
1040  unsigned mask=gcp::control::PAGE_ENABLE,
1041  char* msg=0, ListNode* node=0);
1042 
1043 int sendPagerCondition(ControlProg* cp, unsigned mode,
1045 
1046 int sendCmdTimeoutConfiguration(ControlProg* cp, unsigned seconds);
1047 int sendCmdTimeoutConfiguration(ControlProg* cp, bool active);
1048 
1049 int sendDataTimeoutConfiguration(ControlProg* cp, unsigned seconds);
1050 int sendDataTimeoutConfiguration(ControlProg* cp, bool active);
1051 
1052 void printPort(ControlProg* cp);
1053 
1054 LOG_DISPATCHER(lprintf_to_logger);
1055 
1056 ControlProg *del_ControlProg(ControlProg *cp);
1057 
1058 #endif
Definition: genericcontrol.h:161
Definition: archiver.h:58
Definition: genericcontrol.h:605
NetDat()
Definition: NetDat.cc:15
Definition: genericcontrol.h:643
Definition: genericcontrol.h:540
Definition: genericcontrol.c:708
Definition: PagerMonitor.h:45
void setTo(unsigned id)
Definition: NetUnion.cc:184
Definition: genericcontrol.h:531
Definition: genericcontrol.h:585
Definition: NetStruct.h:21
Definition: control.h:230
Definition: genericscheduler.c:69
Definition: genericcontrol.h:500
Definition: Pipe.h:42
Definition: genericcontrol.h:394
Definition: genericcontrol.h:520
Definition: NewRtcNetMsg.h:30
Definition: genericcontrol.h:739
Definition: navigator.c:82
Definition: genericcontrol.h:886
Definition: logger.c:27
void addMember(unsigned id, NetDat *member=0, bool alloc=false)
Definition: NetUnion.cc:117
virtual void addMember(NetDat *netDat, bool alloc=false)
Definition: NetStruct.cc:93
unsigned getType()
Definition: NetUnion.cc:314
Definition: genericcontrol.h:973
Definition: rtcnetcoms.h:2462
Definition: genericcontrol.h:511
Definition: genericcontrol.h:549
Definition: genericcontrol.h:750
Definition: genericcontrol.h:812
Definition: genericcontrol.h:267
Definition: genericcontrol.h:177
Definition: genericcontrol.h:576
Definition: genericcontrol.h:558
Definition: netbuf.h:173
Definition: grabber.c:62
Definition: genericcontrol.h:567
Definition: genericcontrol.h:140
Definition: netbuf.h:129
Definition: terminal.c:50
Definition: list.h:40
Definition: AntNum.h:23
Definition: NetUnion.h:23
Definition: regdata.h:16
Definition: genericcontrol.h:594
Definition: arraymap.h:177
unsigned maxSize()
Definition: NetDat.cc:87