| 1 | /**************************************************************\
|
|---|
| 2 |
|
|---|
| 3 | drsdaq
|
|---|
| 4 |
|
|---|
| 5 | Main program for data acquisition, starts threads for console input.
|
|---|
| 6 |
|
|---|
| 7 | Original program by S. Commichau.
|
|---|
| 8 |
|
|---|
| 9 | Oliver Grimm, May 2010
|
|---|
| 10 |
|
|---|
| 11 | \**************************************************************/
|
|---|
| 12 |
|
|---|
| 13 | #define LOCKFILE "/tmp/CT3_DAQ_LOCK"
|
|---|
| 14 |
|
|---|
| 15 | #include <stdio.h>
|
|---|
| 16 | #include <signal.h>
|
|---|
| 17 | #include <readline/readline.h>
|
|---|
| 18 | #include <readline/history.h>
|
|---|
| 19 |
|
|---|
| 20 | #include "DAQReadout.h"
|
|---|
| 21 | #include "HVFeedback.h"
|
|---|
| 22 |
|
|---|
| 23 | // Function prototypes
|
|---|
| 24 | void CrashHandler(int);
|
|---|
| 25 | void ExitFunction();
|
|---|
| 26 |
|
|---|
| 27 | // ================
|
|---|
| 28 | // Main program
|
|---|
| 29 | // ================
|
|---|
| 30 | //
|
|---|
| 31 | // Several unlikely system call failures are handled via throwing an exception.
|
|---|
| 32 |
|
|---|
| 33 | int main() {
|
|---|
| 34 |
|
|---|
| 35 | char str[MAX_COM_SIZE], *Command;
|
|---|
| 36 | int LockDescriptor;
|
|---|
| 37 |
|
|---|
| 38 | // Assure only one instance of program runs (lock creator written to log file)
|
|---|
| 39 | // Lock file deleted by destructor
|
|---|
| 40 | if((LockDescriptor = open(LOCKFILE,O_WRONLY|O_CREAT|O_EXCL, S_IWUSR|S_IRUSR|S_IRGRP|S_IROTH)) == -1) {
|
|---|
| 41 | if(errno==EEXIST) {
|
|---|
| 42 | printf("Error: Lock file already existing\n");
|
|---|
| 43 | snprintf(str, sizeof(str), "paste %s -s -d ' '",LOCKFILE);
|
|---|
| 44 | system(str);
|
|---|
| 45 | }
|
|---|
| 46 | else printf("Could not create lock file %s (%s)\n", LOCKFILE, strerror(errno));
|
|---|
| 47 | exit(EXIT_FAILURE);
|
|---|
| 48 | }
|
|---|
| 49 | close(LockDescriptor);
|
|---|
| 50 | sprintf(str,"echo Created >%s; date >>%s; echo by $USER@$HOSTNAME >>%s",LOCKFILE,LOCKFILE,LOCKFILE);
|
|---|
| 51 | system(str);
|
|---|
| 52 |
|
|---|
| 53 | // Readline library uses getc() (allows interruption by signal)
|
|---|
| 54 | rl_getc_function = getc;
|
|---|
| 55 |
|
|---|
| 56 | // writev() in DAQ thread needs to be able to write at least 3 chunks
|
|---|
| 57 | if(IOV_MAX < 2) {
|
|---|
| 58 | printf("Fatal error: IOV_MAX is less than 2, cannot use writev() to write event data.\n");
|
|---|
| 59 | exit(EXIT_FAILURE);
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | system("clear");
|
|---|
| 63 | printf("\n*** DRS readout (built %s, %s, revision %s) *** \n\n",__DATE__, __TIME__, REVISION);
|
|---|
| 64 |
|
|---|
| 65 | // Set exit function
|
|---|
| 66 | atexit(&ExitFunction);
|
|---|
| 67 |
|
|---|
| 68 | // Construct main instance (static ensures destructor is called with exit())
|
|---|
| 69 | static DAQReadout M;
|
|---|
| 70 |
|
|---|
| 71 | // Set signal handlers
|
|---|
| 72 | signal(SIGILL, &CrashHandler);
|
|---|
| 73 | signal(SIGABRT, &CrashHandler);
|
|---|
| 74 | signal(SIGFPE, &CrashHandler);
|
|---|
| 75 | signal(SIGSEGV, &CrashHandler);
|
|---|
| 76 | signal(SIGBUS, &CrashHandler);
|
|---|
| 77 |
|
|---|
| 78 | // Command loop
|
|---|
| 79 | while (!M.ExitRequest) {
|
|---|
| 80 | // Assemble prompt
|
|---|
| 81 | if (M.NumBoards == 0) snprintf(M.Prompt,sizeof(M.Prompt),"\rDAQ> ");
|
|---|
| 82 | else if (M.FirstBoard == M.LastBoard) snprintf(M.Prompt,sizeof(M.Prompt),"\rDAQ|B%d> ",M.FirstBoard);
|
|---|
| 83 | else snprintf(M.Prompt,sizeof(M.Prompt),"\rDAQ|B%d-%d> ",M.FirstBoard,M.LastBoard);
|
|---|
| 84 |
|
|---|
| 85 | // Read Command
|
|---|
| 86 | Command = readline(M.Prompt);
|
|---|
| 87 | if (Command == NULL) continue;
|
|---|
| 88 | if(strlen(Command)>0) add_history(Command);
|
|---|
| 89 |
|
|---|
| 90 | // Process command
|
|---|
| 91 | DimClient::sendCommand(SERVER_NAME"/Command", Command);
|
|---|
| 92 | free(Command);
|
|---|
| 93 | }
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 | // Remove lock file before running default signal code
|
|---|
| 98 | void CrashHandler(int Signal) {
|
|---|
| 99 |
|
|---|
| 100 | remove(LOCKFILE);
|
|---|
| 101 | printf("Caught signal number %d. Removed lockfile and performing standard signal action. Good luck.\n",Signal);
|
|---|
| 102 | signal(Signal, SIG_DFL);
|
|---|
| 103 | raise(Signal);
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | // This function will be implicitly called by exit()
|
|---|
| 107 | void ExitFunction() {
|
|---|
| 108 |
|
|---|
| 109 | if (remove(LOCKFILE) == -1) {
|
|---|
| 110 | printf("Could not remove lock file %s (%s)\n", LOCKFILE, strerror(errno));
|
|---|
| 111 | }
|
|---|
| 112 | }
|
|---|