-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
233 lines (225 loc) · 8.55 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
* This file is part of the loccorr project.
* Copyright 2021 Edward V. Emelianov <[email protected]>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <omp.h>
#include <math.h>
#include <pthread.h>
#include <signal.h> // signal
#include <stdio.h>
#include <string.h> // strdup
#include <sys/prctl.h> //prctl
#include <sys/wait.h> // wait
#include "cmdlnopts.h"
#include "config.h"
#include "debug.h"
#include "grasshopper.h"
#include "improc.h"
#include "pusirobo.h"
#include "socket.h"
static InputType tp;
static pid_t childpid;
/**
* We REDEFINE the default WEAK function of signal processing
*/
void signals(int sig){
if(childpid) exit(sig); // father -> do nothin @ end
if(sig){
signal(sig, SIG_IGN);
DBG("Get signal %d.", sig);
}
stopwork = TRUE;
DBG("exit %d", sig);
saveconf(NULL);
if(GP && GP->pidfile){ // remove unnesessary PID file
DBG("unlink(GP->pidfile)");
unlink(GP->pidfile);
}
if(theSteppers && theSteppers->stepdisconnect) theSteppers->stepdisconnect();
DBG("closeXYlog()");
closeXYlog();
DBG("EXIT %d", sig);
LOGERR("Exit with status %d", sig);
exit(sig);
}
void iffound_default(pid_t pid){
ERRX("Another copy of this process found, pid=%d. Exit.", pid);
}
static void *procinp_thread(_U_ void* arg){
int p = process_input(tp, GP->inputname);
LOGDBG("process_input=%d", p);
return NULL;
}
static InputType chk_inp(const char *name){
if(!name) ERRX("Point file or directory name to monitor");
InputType itp = chkinput(GP->inputname);
if(T_WRONG == itp) return T_WRONG;
green("\n%s is a ", name);
switch(itp){
case T_DIRECTORY:
printf("directory");
break;
case T_JPEG:
printf("jpeg");
break;
case T_PNG:
printf("png");
break;
case T_GIF:
printf("gif");
break;
case T_FITS:
printf("fits");
break;
case T_BMP:
printf("bmp");
break;
case T_GZIP:
printf("maybe fits.gz?");
break;
case T_CAPT_GRASSHOPPER:
printf("capture grasshopper camera");
break;
case T_CAPT_BASLER:
printf("capture basler camera");
break;
default:
printf("Unsupported type\n");
return T_WRONG;
}
printf("\n");
return itp;
}
int main(int argc, char *argv[]){
initial_setup();
int cpunumber = sysconf(_SC_NPROCESSORS_ONLN);
if(omp_get_max_threads() != cpunumber)
omp_set_num_threads(cpunumber);
char *self = strdup(argv[0]);
GP = parse_args(argc, argv);
if(GP->throwpart < 0. || GP->throwpart > 0.99){
ERRX("Fraction of black pixels should be in [0., 0.99]");
}
if(GP->Naveraging < 1 || GP->Naveraging > NAVER_MAX)
ERRX("Averaging amount should be from 1 to %d", NAVER_MAX);
tp = chk_inp(GP->inputname);
if(tp == T_WRONG) ERRX("Enter correct image file or directory name");
// check ability of saving file
{
FILE *f = fopen(GP->outputjpg, "w");
if(!f) ERR("Can't create %s", GP->outputjpg);
fclose(f);
}
if(GP->logfile){
sl_loglevel lvl = LOGLEVEL_ERR; // default log level - errors
int v = GP->verb;
while(v--){ // increase loglevel for each "-v"
if(++lvl == LOGLEVEL_ANY) break;
}
OPENLOG(GP->logfile, lvl, 1);
DBG("Opened log file @ level %d", lvl);
}
int C = chkconfig(GP->configname);
if(!C){
LOGWARN("Wrong/absent configuration file");
WARNX("Wrong/absent configuration file");
}
// change `theconf` parameters to user values
{
if(GP->maxarea != DEFAULT_MAXAREA || theconf.maxarea == 0) theconf.maxarea = GP->maxarea;
if(GP->minarea != DEFAULT_MINAREA || theconf.minarea == 0) theconf.minarea = GP->minarea;
if(GP->xtarget > 0.) theconf.xtarget = GP->xtarget;
if(GP->ytarget > 0.) theconf.ytarget = GP->ytarget;
if(GP->nerosions != DEFAULT_EROSIONS || theconf.Nerosions == 0){
if(GP->nerosions < 1 || GP->nerosions > MAX_NEROS) ERRX("Amount of erosions should be from 1 to %d", MAX_NEROS);
theconf.Nerosions = GP->nerosions;
}
if(GP->ndilations != DEFAULT_DILATIONS || theconf.Ndilations == 0){
if(GP->ndilations < 1 || GP->ndilations > MAX_NDILAT) ERRX("Amount of erosions should be from 1 to %d", MAX_NDILAT);
theconf.Ndilations = GP->ndilations;
}
if(fabs(GP->throwpart - DEFAULT_THROWPART) > DBL_EPSILON || theconf.throwpart < DBL_EPSILON){
if(GP->throwpart < 0. || GP->throwpart > MAX_THROWPART) ERRX("'thworpart' should be from 0 to %g", MAX_THROWPART);
theconf.throwpart = GP->throwpart;
}
if(GP->xoff && GP->xoff < MAX_OFFSET) theconf.xoff = GP->xoff;
if(GP->yoff && GP->yoff < MAX_OFFSET) theconf.yoff = GP->yoff;
if(GP->width && GP->width < MAX_OFFSET) theconf.width = GP->width;
if(GP->height && GP->height < MAX_OFFSET) theconf.height = GP->height;
if(fabs(GP->minexp - EXPOS_MIN) > DBL_EPSILON || theconf.minexp < DBL_EPSILON){
if(GP->minexp < DBL_EPSILON || GP->minexp > EXPOS_MAX) ERRX("Minimal exposition should be > 0 and < %g", EXPOS_MAX);
theconf.minexp = GP->minexp;
}
if(fabs(GP->maxexp - EXPOS_MAX) > DBL_EPSILON || theconf.maxexp < theconf.minexp){
if(GP->maxexp < theconf.minexp) ERRX("Maximal exposition should be greater than minimal");
theconf.maxexp = GP->maxexp;
}
if(GP->equalize && theconf.equalize == 0) theconf.equalize = 1;
if(fabs(GP->intensthres - DEFAULT_INTENSTHRES) > DBL_EPSILON){
if(GP->intensthres < DBL_EPSILON || GP->intensthres > 1.-DBL_EPSILON) ERRX("'intensthres' should be from 0 to 1");
theconf.intensthres = GP->intensthres;
}
if(GP->Naveraging != DEFAULT_NAVERAGE || theconf.naverage < 1){
if(GP->Naveraging < 1 || GP->Naveraging > NAVER_MAX) ERRX("N images for averaging should be from 1 to %d", NAVER_MAX);
theconf.naverage = GP->Naveraging;
}
if(GP->pusiservport != DEFAULT_PUSIPORT || theconf.stpserverport == 0){
if(GP->pusiservport < 1 || GP->pusiservport > 65535) ERRX("Wrong steppers' server port: %d", GP->pusiservport);
theconf.stpserverport = GP->pusiservport;
}
}
check4running(self, GP->pidfile);
DBG("%s started, snippets library version is %s\n", self, sl_libversion());
free(self); self = NULL;
signal(SIGTERM, signals); // kill (-15) - quit
signal(SIGHUP, SIG_IGN); // hup - ignore
signal(SIGINT, signals); // ctrl+C - quit
signal(SIGQUIT, signals); // ctrl+\ - quit
signal(SIGTSTP, SIG_IGN); // ignore ctrl+Z
while(1){ // guard for dead processes
childpid = fork();
if(childpid){ // father
LOGMSG("create child with PID %d\n", childpid);
DBG("Created child with PID %d\n", childpid);
wait(NULL);
LOGMSG("child %d died\n", childpid);
WARNX("Child %d died\n", childpid);
sleep(5);
}else{ // son
prctl(PR_SET_PDEATHSIG, SIGTERM); // send SIGTERM to child when parent dies
break; // go out to normal functional
}
}
setpostprocess(GP->processing);
if(GP->logXYname) openXYlog(GP->logXYname);
LOGMSG("Start application...");
LOGDBG("xtag=%g, ytag=%g", theconf.xtarget, theconf.ytarget);
openIOport(GP->ioport);
pthread_t inp_thread;
if(pthread_create(&inp_thread, NULL, procinp_thread, NULL)){
LOGERR("pthread_create() for image input failed");
ERR("pthread_create()");
}
while(1){
if(stopwork || pthread_kill(inp_thread, 0) == ESRCH){
DBG("close");
pthread_join(inp_thread, NULL);
DBG("out");
return 0;
}
};
return 0;
}