forked from JoakimSoderberg/catcierge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcatcierge_grabber.c
346 lines (288 loc) · 7.12 KB
/
catcierge_grabber.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
//
// This file is part of the Catcierge project.
//
// Copyright (c) Joakim Soderberg 2013-2015
//
// Catcierge 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 2 of the License, or
// (at your option) any later version.
//
// Catcierge 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 Catcierge. If not, see <http://www.gnu.org/licenses/>.
//
#include <signal.h>
#include "catcierge_config.h"
#include "catcierge_args.h"
#include "catcierge_log.h"
#include "catcierge_fsm.h"
#include "catcierge_output.h"
#include "catcierge_matcher.h"
#include "catcierge_sigusr.h"
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#ifndef _WIN32
#include <fcntl.h>
#endif // _WIN32
#ifdef WITH_ZMQ
#include <czmq.h>
#endif
#include <opencv2/core/version.hpp>
catcierge_grb_t grb;
#ifndef _WIN32
int pid_fd;
#define PID_PATH "/var/run/catcierge.pid"
int create_pid_file(const char *prog_name, const char *pid_path, int flags)
{
int fd;
char buf[128];
struct flock fl;
if ((fd = open(pid_path, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)) == -1)
{
fprintf(stderr, "Failed to open PID file \"%s\"\n", pid_path);
return -1;
}
if (flags & FD_CLOEXEC)
{
// Use fcntl instead of O_CLOEXEC on open since it is
// supported on more systems.
if ((flags = fcntl(fd, F_GETFD)) == -1)
{
fprintf(stderr, "Failed to get flags for PID file \"%s\"\n", pid_path);
}
else
{
flags |= FD_CLOEXEC;
if (fcntl(fd, F_SETFD, flags) == -1)
{
fprintf(stderr, "Failed to set flags for PID file \"%s\"\n", pid_path);
}
}
}
fl.l_type = F_WRLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 0;
fl.l_len = 0;
if (fcntl(fd, F_SETLK, &fl) == -1)
{
if ((errno == EAGAIN) || (errno == EACCES))
{
fprintf(stderr, "PID file '%s' is locked. '%s' is probably already running.\n",
pid_path, prog_name);
}
else
{
fprintf(stderr, "Unable to lock PID file '%s'\n", pid_path);
}
close(fd);
return -1;
}
if (ftruncate(fd, 0) == -1)
{
fprintf(stderr, "Failed to truncate PID file '%s\n'", pid_path);
close(fd);
return -1;
}
snprintf(buf, sizeof(buf), "%ld\n", (long)getpid());
if (write(fd, buf, strlen(buf)) != strlen(buf))
{
fprintf(stderr, "Failed to write PID file '%s'\n", pid_path);
close(fd);
return -1;
}
return fd;
}
#endif // _WIN32
static void sig_handler(int signo)
{
catcierge_args_t *args = &grb.args;
switch (signo)
{
case SIGINT:
{
static int sigint_count = 0;
CATLOG("Received SIGINT, stopping...\n");
// Force a quit if we're not in the loop
// or the SIGINT is a repeat.
if (!grb.running || (sigint_count > 0))
{
catcierge_do_unlock(&grb);
exit(0);
}
// Simply kill the loop and let the program do normal cleanup.
grb.running = 0;
sigint_count++;
break;
}
#ifndef _WIN32
case SIGUSR1:
{
CATLOG("Received SIGUSR1\n");
catcierge_handle_sigusr(&grb, args->sigusr1_str);
break;
}
case SIGUSR2:
{
CATLOG("Received SIGUSR2\n");
catcierge_handle_sigusr(&grb, args->sigusr2_str);
break;
}
#endif // _WIN32
}
}
void setup_sig_handlers()
{
if (signal(SIGINT, sig_handler) == SIG_ERR)
{
CATERR("Failed to set SIGINT handler\n");
}
#ifndef _WIN32
if (signal(SIGUSR1, sig_handler) == SIG_ERR)
{
CATERR("Failed to set SIGUSR1 handler (used to force unlock)\n");
}
if (signal(SIGUSR2, sig_handler) == SIG_ERR)
{
CATERR("Failed to set SIGUSR2 handler (used to force lockout)\n");
}
#endif // _WIN32
}
int main(int argc, char **argv)
{
catcierge_args_t *args = &grb.args;
fprintf(stderr, "\nCatcierge Grabber v" CATCIERGE_VERSION_STR " (" CATCIERGE_GIT_HASH_SHORT "");
// Was this built with changes in the git sources?
#ifdef CATCIERGE_GIT_TAINTED
fprintf(stderr, "-tainted");
#endif
fprintf(stderr, ")\n(C) Joakim Soderberg 2013-2017\n\n");
fprintf(stderr, "Library versions:\n");
fprintf(stderr, " OpenCV v%d.%d.%d\n", CV_MAJOR_VERSION, CV_MINOR_VERSION, CV_SUBMINOR_VERSION);
#ifdef WITH_ZMQ
fprintf(stderr, " CZMQ v%d.%d.%d\n", CZMQ_VERSION_MAJOR, CZMQ_VERSION_MINOR, CZMQ_VERSION_PATCH);
#endif
fprintf(stderr, "\n");
// TODO: Enable specifying pid path on command line.
#ifndef _WIN32
pid_fd = create_pid_file(argv[0], PID_PATH, FD_CLOEXEC);
#endif
if (catcierge_grabber_init(&grb))
{
fprintf(stderr, "Failed to init\n");
return -1;
}
if (catcierge_args_init(args, argv[0]))
{
fprintf(stderr, "Failed to init args\n");
return -1;
}
if (catcierge_args_parse(args, argc, argv))
{
return -1;
}
if (args->nocolor)
{
catcierge_nocolor = 1;
}
catcierge_print_settings(args);
setup_sig_handlers();
if (args->log_path)
{
if (!(grb.log_file = fopen(args->log_path, "a+")))
{
CATERR("Failed to open log file \"%s\"\n", args->log_path);
}
}
#ifdef RPI
if (catcierge_setup_gpio(&grb))
{
CATERR("Failed to setup GPIO pins\n");
return -1;
}
CATLOG("Initialized GPIO pins\n");
#endif // RPI
assert((args->matcher_type == MATCHER_TEMPLATE)
|| (args->matcher_type == MATCHER_HAAR));
if (catcierge_matcher_init(&grb.matcher, catcierge_get_matcher_args(args)))
{
CATERR("Failed to init %s matcher\n", grb.matcher->name);
return -1;
}
CATLOG("Initialized catcierge image recognition\n");
if (catcierge_output_init(&grb, &grb.output))
{
CATERR("Failed to init output template system\n");
return -1;
}
if (catcierge_output_load_templates(&grb.output,
args->inputs, args->input_count))
{
CATERR("Failed to load output templates\n");
return -1;
}
CATLOG("Initialized output templates\n");
#ifdef WITH_RFID
catcierge_init_rfid_readers(&grb);
#endif
if (catcierge_setup_camera(&grb))
{
CATERR("Failed to setup camera\n");
return -1;
}
#ifdef WITH_ZMQ
catcierge_zmq_init(&grb);
#endif
CATLOG("Starting detection!\n");
// TODO: Create a catcierge_grb_start(grb) function that does this instead.
catcierge_fsm_start(&grb);
// Run the program state machine.
do
{
if (!catcierge_timer_isactive(&grb.frame_timer))
{
catcierge_timer_start(&grb.frame_timer);
}
// Always feed the RFID readers.
#ifdef WITH_RFID
if ((args->rfid_inner_path || args->rfid_outer_path)
&& catcierge_rfid_ctx_service(&grb.rfid_ctx))
{
CATERRFPS("Failed to service RFID readers\n");
}
#endif // WITH_RFID
grb.img = catcierge_get_frame(&grb);
catcierge_run_state(&grb);
catcierge_print_spinner(&grb);
} while (
grb.running
#ifdef WITH_ZMQ
&& !zctx_interrupted
#endif
);
catcierge_matcher_destroy(&grb.matcher);
catcierge_output_destroy(&grb.output);
catcierge_destroy_camera(&grb);
#ifdef WITH_ZMQ
catcierge_zmq_destroy(&grb);
#endif
catcierge_grabber_destroy(&grb);
catcierge_args_destroy(&grb.args);
if (grb.log_file)
{
fclose(grb.log_file);
}
#ifndef _WIN32
if (pid_fd > 0)
{
close(pid_fd);
unlink(PID_PATH);
}
#endif
return 0;
}