source: hvcontrol/hvcontrol.cpp@ 201

Last change on this file since 201 was 183, checked in by ogrimm, 15 years ago
Various updates
File size: 6.0 KB
Line 
1
2/**************************************************************\
3
4 Main program for the CTX G-APD HV supply, sends commands,
5 reads and monitors status of the G-APD HV supply
6
7 Sebastian Commichau, Sabrina Stark, Oliver Grimm
8
9\**************************************************************/
10
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14#include <pthread.h>
15#include <signal.h>
16
17#include "ProcessIO.h"
18
19#include <readline/readline.h>
20#include <readline/history.h>
21
22#define LOCKFILE "/tmp/CTX_HV_LOCK"
23
24// Function prototypes
25void ConsoleCommand(ProcessIO *);
26void HVMonitor(ProcessIO *);
27void DummyHandler(int);
28void CrashHandler(int);
29void ExitFunction();
30
31// ================
32// Main program
33// ================
34//
35// Several unlikely system call failures are handled via throwing an exception.
36
37int main(int argc, char *argv[]) {
38
39 char str[MAX_COM_SIZE];
40 pthread_t thread_ConsoleCommand,thread_HVMonitor;
41 int LockDescriptor;
42
43 // Assure only one instance of the HV control program runs
44 // The flag O_EXCL together with O_CREAT assure that the lock
45 // file cannot be opened by another instance, i.e. there are no parallel write accesses
46 if((LockDescriptor = open(LOCKFILE,O_WRONLY|O_CREAT|O_EXCL,S_IWUSR|S_IRUSR|S_IRGRP|S_IROTH)) == -1) {
47 if(errno==EEXIST) {
48 printf("Error: Lock file already existing\n");
49 sprintf(str,"paste %s -s -d ' '",LOCKFILE);
50 system(str);
51 }
52 else printf("Could not create lock file %s (%s)\n", LOCKFILE, strerror(errno));
53 exit(EXIT_FAILURE);
54 }
55 close(LockDescriptor);
56 sprintf(str,"echo Created >%s; date >>%s; echo by $USER@$HOSTNAME>>%s",LOCKFILE,LOCKFILE,LOCKFILE);
57 system(str);
58
59 system("clear");
60 printf("\n*** Bias control (%s, %s, O. Grimm, S.Commichau, S.Stark) ***\n\n",__DATE__,__TIME__);
61
62 // Install signal handler and set signal SIGUSR1 to interrupt blocking system calls
63 signal(SIGUSR1, &DummyHandler);
64 siginterrupt (SIGUSR1, true);
65
66 // Assure lock file is deleted in case of a program crash or call to exit()
67 signal(SIGILL, &CrashHandler);
68 signal(SIGABRT, &CrashHandler);
69 signal(SIGFPE, &CrashHandler);
70 signal(SIGSEGV, &CrashHandler);
71 signal(SIGBUS, &CrashHandler);
72 atexit(&ExitFunction);
73
74 // Construct main instance and create mutex for thread synchronization
75 ProcessIO pio;
76 if (pthread_mutex_init(&pio.control_mutex, NULL) != 0) {
77 perror("pthread_mutex_init failed");
78 throw;
79 }
80
81 // These signals were set during construction of EvidenceServer
82 signal(SIGQUIT, &CrashHandler); // CTRL-Backspace
83 signal(SIGINT, &CrashHandler); // CTRL-C
84 signal(SIGHUP, &CrashHandler); // Terminal closed
85 signal(SIGTERM, &CrashHandler);
86
87 // Create threads
88 if ((pthread_create(&thread_ConsoleCommand, NULL, (void * (*)(void *)) ConsoleCommand,(void *) &pio)) != 0) {
89 perror("pthread_create failed with console thread");
90 throw;
91 }
92 if ((pthread_create(&thread_HVMonitor, NULL, (void * (*)(void *)) HVMonitor,(void *) &pio)) != 0) {
93 perror("pthread_create failed with HVMonitor thread");
94 throw;
95 }
96
97 // Threads should be accessible for sending signals
98 pio.HVMonitor = thread_HVMonitor;
99
100 // Wait for threads to quit
101 pthread_join(thread_ConsoleCommand, NULL);
102 pthread_join(thread_HVMonitor, NULL);
103
104 // Destruct mutex and main instance
105 pthread_mutex_destroy (&pio.control_mutex);
106 pio.~ProcessIO();
107
108 // Remove lockfile
109 if (remove(LOCKFILE)==-1) {
110 sprintf(str, "Could not remove lock file %s", LOCKFILE);
111 perror(str);
112 exit(EXIT_FAILURE);
113 }
114
115 exit(EXIT_SUCCESS);
116}
117
118
119/********************************************************************\
120
121 ConsoleCommand thread
122
123 Handle console input using readline library functions to allow
124 line editing and history capability
125
126\********************************************************************/
127
128void ConsoleCommand(ProcessIO *m) {
129
130 char *Command;
131
132 while (!m->Exit) {
133
134 // Assemble prompt
135 snprintf(m->Prompt, sizeof(m->Prompt),"\rHV");
136 if (m->NumHVBoards == 0) sprintf(m->Prompt+strlen(m->Prompt),"> ");
137 else {
138 if (m->FirstChain == m->LastChain) sprintf(m->Prompt+strlen(m->Prompt),"|C%d",m->FirstChain);
139 else sprintf(m->Prompt+strlen(m->Prompt),"|C%d-%d",m->FirstChain,m->LastChain);
140
141 if (m->NumHVBoards == 0) sprintf(m->Prompt+strlen(m->Prompt),"> ");
142 else if (m->FirstBoard == m->LastBoard) sprintf(m->Prompt+strlen(m->Prompt),"|B%d> ",m->FirstBoard);
143 else snprintf(m->Prompt,sizeof(m->Prompt),"\rDAQ|B%d-%d> ",m->FirstBoard,m->LastBoard);
144 }
145
146 // Read Command
147 Command = readline(m->Prompt);
148 if (Command==NULL) {
149 m->PrintMessage("Error reading command line input\n");
150 continue;
151 }
152 if(strlen(Command)>0) add_history(Command);
153
154 // Process command (via DIM gives automatic serialisation)
155 DimClient::sendCommand("Bias/Command", Command);
156 free(Command);
157 }
158}
159
160
161/********************************************************************\
162
163 HVMonitor
164
165 Monitor HV board status
166 Sebastian Commichau, November 2008
167
168\********************************************************************/
169
170void HVMonitor(ProcessIO *m) {
171
172 while (!m->Exit) {
173 if (m->state == active) {
174 pthread_mutex_lock(&m->control_mutex);
175 m->Monitor();
176 pthread_mutex_unlock(&m->control_mutex);
177 }
178 usleep((unsigned long)floor(1000000./(m->NumHVBoards*m->fStatusRefreshRate)));
179 }
180}
181
182
183/********************************************************************\
184
185 Signal handlers
186
187\********************************************************************/
188
189// Remove lock file before running default signal code
190void CrashHandler(int Signal) {
191 remove(LOCKFILE);
192 printf("Caught signal number %d. Removing lockfile and performing standard signal action. Good luck.\n",Signal);
193 signal(Signal, SIG_DFL);
194 raise(Signal);
195}
196
197// Dummy signal handler to return from blocking syscalls
198void DummyHandler(int Signal) {
199 return;
200}
201
202// This function will be implicitly called by exit()
203void ExitFunction() {
204 remove(LOCKFILE);
205 return;
206}
Note: See TracBrowser for help on using the repository browser.