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 |
|
---|
33 |
|
---|
34 | int main(int argc, char *argv[]) {
|
---|
35 |
|
---|
36 |
|
---|
37 | char config_file[] = DEFAULT_CONFIG, str[MAX_COM_SIZE];
|
---|
38 |
|
---|
39 | pthread_t thread_ConsoleCommand; // Read commands from console
|
---|
40 | pthread_t thread_HVMonitor; // Reads continuously from HV board(s)
|
---|
41 | pthread_t thread_CCCommand; // Thread listening to commands from Central Control
|
---|
42 |
|
---|
43 | int LockDescriptor;
|
---|
44 |
|
---|
45 | // Assure only one instance of the HV control program runs
|
---|
46 | // The flag O_EXCL together with O_CREAT assure that the lock
|
---|
47 | // file cannot be opened by another instance, i.e. there are no parallel write accesses
|
---|
48 | if ((LockDescriptor = open(LOCKFILE,O_RDONLY|O_CREAT|O_EXCL)) == -1) {
|
---|
49 | sprintf(str, "Could not create lock file %s", LOCKFILE);
|
---|
50 | perror(str);
|
---|
51 | exit(EXIT_FAILURE);
|
---|
52 | }
|
---|
53 | close(LockDescriptor);
|
---|
54 |
|
---|
55 |
|
---|
56 | for (int i=1; i<argc; i++) {
|
---|
57 | if (argv[i][0] == '-') {
|
---|
58 | if (argv[i][1] == 'h') {
|
---|
59 | printf("Usage: %s [-c <ConfigFile>] default file is \"%s\"\n", argv[0], config_file);
|
---|
60 | exit(1);
|
---|
61 | }
|
---|
62 | if (argv[i][1] == 'c') {
|
---|
63 | if (argc<i+2) {
|
---|
64 | printf("Error: missing configuration file after -c option\n");
|
---|
65 | exit(1);
|
---|
66 | } else {
|
---|
67 | sprintf(config_file, "%s", argv[i+1]);
|
---|
68 | }
|
---|
69 | }
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | system("clear");
|
---|
74 | printf("\n****************** HV Control %s built %s, %s by S. Commichau ******************\n\n",HV_CONTROL_VERSION,__DATE__,__TIME__);
|
---|
75 |
|
---|
76 |
|
---|
77 | // Construct main instance
|
---|
78 | ProcessIO pio(config_file);
|
---|
79 |
|
---|
80 | // Install signal handler and set signal SIGUSR1 to interrupt blocking system calls
|
---|
81 | signal(SIGUSR1, &SignalHandler);
|
---|
82 | siginterrupt (SIGUSR1, true);
|
---|
83 |
|
---|
84 | // Create threads
|
---|
85 | if ((pthread_create(&thread_ConsoleCommand, NULL, (void * (*)(void *)) ConsoleCommand,(void *) &pio)) != 0) {
|
---|
86 | fprintf(stderr, "pthread_create failed with ConsoleCommand\n");
|
---|
87 | exit(1);
|
---|
88 | }
|
---|
89 |
|
---|
90 | if ((pthread_create(&thread_HVMonitor, NULL, (void * (*)(void *)) HVMonitor,(void *) &pio)) != 0) {
|
---|
91 | fprintf(stderr, "pthread_create failed with HVMonitor\n");
|
---|
92 | exit(1);
|
---|
93 | }
|
---|
94 |
|
---|
95 | if ((pthread_create(&thread_CCCommand, NULL, (void * (*)(void *)) CCCommand,(void *) &pio)) != 0) {
|
---|
96 | fprintf(stderr, "pthread_create failed with CCCommand\n");
|
---|
97 | exit(1);
|
---|
98 | }
|
---|
99 |
|
---|
100 | // Threads should be accessible for sending signals
|
---|
101 | pio.status->HVMonitor = thread_HVMonitor;
|
---|
102 | pio.status->SocketThread = thread_CCCommand;
|
---|
103 |
|
---|
104 | // Wait for threads to quit
|
---|
105 | pthread_join(thread_CCCommand, NULL);
|
---|
106 | pthread_join(thread_ConsoleCommand, NULL);
|
---|
107 | pthread_join(thread_HVMonitor, NULL);
|
---|
108 |
|
---|
109 | // Remove lockfile
|
---|
110 | if (remove(LOCKFILE)==-1) {
|
---|
111 | sprintf(str, "Could not remove lock file %s", LOCKFILE);
|
---|
112 | perror(str);
|
---|
113 | exit(EXIT_FAILURE);
|
---|
114 | }
|
---|
115 |
|
---|
116 | return 0;
|
---|
117 | }
|
---|