2 This file is part of GNUnet.
3 Copyright (C) 2013-2019 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
25 #include "gnunet_util_lib.h"
27 #define LOG(fmt, ...) if (verbose == true) printf(fmt, ## __VA_ARGS__)
29 // Command line options
30 static char* device = "/dev/video0";
31 static int verbose = false;
32 static int silent = false;
35 static long unsigned int exit_code = 1;
37 // Helper process we started.
38 static struct GNUNET_OS_Process *p;
40 // Pipe used to communicate shutdown via signal.
41 static struct GNUNET_DISK_PipeHandle *sigpipe;
45 * Task triggered whenever we receive a SIGCHLD (child
46 * process died) or when user presses CTRL-C.
48 * @param cls closure, NULL
51 maint_child_death (void *cls)
53 enum GNUNET_OS_ProcessStatusType type;
56 GNUNET_OS_process_status (p, &type, &exit_code)) ||
57 (type != GNUNET_OS_PROCESS_EXITED) )
58 GNUNET_break (0 == GNUNET_OS_process_kill (p, GNUNET_TERM_SIG));
59 GNUNET_OS_process_destroy (p);
64 * Dispatch URIs to the appropriate GNUnet helper process
67 * @param uri uri to dispatch
68 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
69 * @param cfg configuration
72 gnunet_uri (void *cls, const char *uri, const char *cfgfile,
73 const struct GNUNET_CONFIGURATION_Handle *cfg)
79 struct GNUNET_SCHEDULER_Task * rt;
82 if (0 != strncasecmp ("gnunet://", uri, strlen ("gnunet://"))) {
84 _("Invalid URI: does not start with `%s'\n"),
88 uri += strlen ("gnunet://");
89 if (NULL == (slash = strchr (uri, '/')))
91 fprintf (stderr, _("Invalid URI: fails to specify subsystem\n"));
94 subsystem = GNUNET_strndup (uri, slash - uri);
96 GNUNET_CONFIGURATION_get_value_string (cfg,
101 fprintf (stderr, _("No handler known for subsystem `%s'\n"), subsystem);
102 GNUNET_free (subsystem);
105 GNUNET_free (subsystem);
106 rt = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
107 GNUNET_DISK_pipe_handle (sigpipe,
108 GNUNET_DISK_PIPE_END_READ),
109 &maint_child_death, NULL);
110 p = GNUNET_OS_start_process (GNUNET_NO, 0,
116 GNUNET_free (program);
118 GNUNET_SCHEDULER_cancel (rt);
123 * Main function that will be run by the scheduler.
126 * @param args remaining command-line arguments
127 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
128 * @param cfg configuration
134 const struct GNUNET_CONFIGURATION_Handle *cfg)
136 /* create a Processor */
137 LOG("Initializing\n");
138 zbar_processor_t *proc = zbar_processor_create(1);
140 // FIXME: Wrap all this into a function which returns an error on
141 // failure. And here ensure the processor is destroyed at the end.
143 /* configure the Processor */
144 zbar_processor_parse_config(proc, "enable");
146 /* initialize the Processor */
147 LOG("Opening video device %s\n", device);
148 // FIXME: error handling
149 zbar_processor_init(proc, device, 1);
151 /* enable the preview window */
152 zbar_processor_set_visible(proc, 1);
153 zbar_processor_set_active(proc, 1);
155 /* keep scanning until user provides key/mouse input */
156 //zbar_processor_user_wait(proc, -1);
158 // read at least one barcode (or until window closed)
161 n = zbar_process_one(proc, -1);
162 LOG("Got %i images\n", n);
163 // FIXME: Error handling (n = -1)
165 // hide the preview window
166 zbar_processor_set_active(proc, 0);
167 zbar_processor_set_visible(proc, 0);
170 const zbar_symbol_set_t* symbols = zbar_processor_get_results(proc);
171 const zbar_symbol_t* symbol = zbar_symbol_set_first_symbol(symbols);
173 if (symbol != NULL) {
174 const char* data = zbar_symbol_get_data(symbol);
175 LOG("Found %s \"%s\"\n",
176 zbar_get_symbol_name(zbar_symbol_get_type(symbol)), data);
178 gnunet_uri(cls, data, cfgfile, cfg);
179 if (exit_code != 0) {
180 printf("Failed to add URI %s\n", data);
182 printf("Added URI %s\n", data);
187 zbar_processor_destroy(proc);
192 main (int argc, char *const *argv)
195 struct GNUNET_GETOPT_CommandLineOption options[] = {
196 GNUNET_GETOPT_option_string ('d', "device", "DEVICE",
197 gettext_noop ("use video-device DEVICE (default: /dev/video0"),
199 GNUNET_GETOPT_option_flag ('\0', "verbose",
200 gettext_noop ("be verbose"),
202 GNUNET_GETOPT_option_flag ('s', "silent",
203 gettext_noop ("do not show preview windows"),
205 GNUNET_GETOPT_OPTION_END
207 ret = GNUNET_PROGRAM_run (argc,
210 gettext_noop ("Scan a QR code using a video device and import the uri read"),
211 options, &run, NULL);
212 return ((GNUNET_OK == ret) && (0 == exit_code)) ? 0 : 1;