source: hvcontrol/hvcontrol.cpp@ 35

Last change on this file since 35 was 35, checked in by lstark, 16 years ago
Calibration table (DAC - HV values) integrated
File size: 4.5 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 Name: hvcontrol.cpp
8
9 Actions: Do global initialization, start threads
10
11 Created by: Sebastian Commichau, November 2008
12 commichau@phys.ethz.ch
13
14\**************************************************************/
15
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <pthread.h>
20#include <signal.h>
21
22#include "ConsoleCommand.h"
23#include "CCCommand.h"
24#include "ProcessIO.h"
25#include "HVMonitor.h"
26#include "Types.h"
27
28#define Sleep(x) usleep(x*1000)
29
30#define DEFAULT_CONFIG "HV.conf" // Default configuration file
31#define LOCKFILE "/tmp/CTX_HV_LOCK"
32
33void CrashHandler(int Signal);
34
35
36int main(int argc, char *argv[]) {
37
38
39 char config_file[] = DEFAULT_CONFIG, str[MAX_COM_SIZE];
40
41 pthread_t thread_ConsoleCommand; // Read commands from console
42 pthread_t thread_HVMonitor; // Reads continuously from HV board(s)
43 pthread_t thread_CCCommand; // Thread listening to commands from Central Control
44
45 int LockDescriptor;
46
47 // Assure only one instance of the HV control program runs
48 // The flag O_EXCL together with O_CREAT assure that the lock
49 // file cannot be opened by another instance, i.e. there are no parallel write accesses
50 /* if ((LockDescriptor = open(LOCKFILE,O_RDONLY|O_CREAT|O_EXCL)) == -1) {
51 sprintf(str, "Could not create lock file %s", LOCKFILE);
52 perror(str);
53 exit(EXIT_FAILURE);
54 }
55 close(LockDescriptor);
56 */
57 if((LockDescriptor = open(LOCKFILE,O_WRONLY|O_CREAT|O_EXCL,S_IWUSR|S_IRUSR|S_IRGRP|S_IROTH)) == -1) {
58 if(errno==EEXIST) {
59 printf("Error: Lock file already existing\n");
60 sprintf(str,"paste %s -s -d ' '",LOCKFILE);
61 system(str);
62 }
63 else {
64 sprintf(str, "Could not create lock file %s", LOCKFILE);
65 perror(str);
66 }
67 exit(EXIT_FAILURE);
68 }
69 close(LockDescriptor);
70 sprintf(str,"echo Created >%s; date >>%s; echo by $USER@$HOSTNAME>>%s",LOCKFILE,LOCKFILE,LOCKFILE);
71 system(str);
72
73
74 for (int i=1; i<argc; i++) {
75 if (argv[i][0] == '-') {
76 if (argv[i][1] == 'h') {
77 printf("Usage: %s [-c <ConfigFile>] default file is \"%s\"\n", argv[0], config_file);
78 exit(1);
79 }
80 if (argv[i][1] == 'c') {
81 if (argc<i+2) {
82 printf("Error: missing configuration file after -c option\n");
83 exit(1);
84 } else {
85 sprintf(config_file, "%s", argv[i+1]);
86 }
87 }
88 }
89 }
90
91 system("clear");
92 printf("\n****************** HV Control %s built %s, %s by S. Commichau ******************\n\n",HV_CONTROL_VERSION,__DATE__,__TIME__);
93
94
95 // Construct main instance
96 ProcessIO pio(config_file);
97
98 // Install signal handler and set signal SIGUSR1 to interrupt blocking system calls
99 signal(SIGUSR1, &SignalHandler);
100 siginterrupt (SIGUSR1, true);
101
102 // Install signals to assure that the lock file is deleted in case of a program crash
103 signal(SIGQUIT, &CrashHandler);
104 signal(SIGILL, &CrashHandler);
105 signal(SIGABRT, &CrashHandler);
106 signal(SIGFPE, &CrashHandler);
107 signal(SIGSEGV, &CrashHandler);
108 signal(SIGBUS, &CrashHandler);
109 signal(SIGTERM, &CrashHandler);
110 signal(SIGINT, &CrashHandler);
111 signal(SIGHUP, &CrashHandler);
112
113
114 // Create threads
115 if ((pthread_create(&thread_ConsoleCommand, NULL, (void * (*)(void *)) ConsoleCommand,(void *) &pio)) != 0) {
116 fprintf(stderr, "pthread_create failed with ConsoleCommand\n");
117 exit(1);
118 }
119
120 if ((pthread_create(&thread_HVMonitor, NULL, (void * (*)(void *)) HVMonitor,(void *) &pio)) != 0) {
121 fprintf(stderr, "pthread_create failed with HVMonitor\n");
122 exit(1);
123 }
124
125 if ((pthread_create(&thread_CCCommand, NULL, (void * (*)(void *)) CCCommand,(void *) &pio)) != 0) {
126 fprintf(stderr, "pthread_create failed with CCCommand\n");
127 exit(1);
128 }
129
130 // Threads should be accessible for sending signals
131 pio.status->HVMonitor = thread_HVMonitor;
132 pio.status->SocketThread = thread_CCCommand;
133
134 // Wait for threads to quit
135 pthread_join(thread_CCCommand, NULL);
136 pthread_join(thread_ConsoleCommand, NULL);
137 pthread_join(thread_HVMonitor, NULL);
138
139 // Remove lockfile
140 if (remove(LOCKFILE)==-1) {
141 sprintf(str, "Could not remove lock file %s", LOCKFILE);
142 perror(str);
143 exit(EXIT_FAILURE);
144 }
145
146 return 0;
147}
148
149// Remove lock file before running default signal code
150void CrashHandler(int Signal) {
151 remove(LOCKFILE);
152 printf("Caught signal number %d. Removing lockfile and performing standard signal action. Good luck.\n",Signal);
153 signal(Signal, SIG_DFL);
154 raise(Signal);
155}
Note: See TracBrowser for help on using the repository browser.